DBA Data[Home] [Help]

PACKAGE BODY: APPS.WSM_LBJ_INTERFACE_PVT

Source


1 PACKAGE BODY WSM_LBJ_INTERFACE_PVT AS
2 /* $Header: WSMVLJIB.pls 120.37.12010000.4 2008/11/24 10:10:06 amayadav ship $ */
3 
4 -- BUG 3934661
5 -- when calling dbms_utility.get_hash_value use larger seed number
6 -- OLD: dbms_utility.get_hash_value(str, 1000, 5625);
7 -- NEW: dbms_utility.get_hash_value(str, 37, 1073741824);
8 
9 -- **********************************************************************************************
10 -- PACKAGE: WSM_LBJ_INTERFACE_PVT (OSFM 11.5.10 and above)
11 --      (1) This is to handle WSM Lot Based Job Interface
12 --          WSM_LOT_JOB_INTERFACE and WSM_LOT_JOB_DTL_INTERFACE
13 --      (2) The {{{ and }}} in the comments are used to allow better nevigation within
14 --          the source code, {{{ or {{ is the beginning of a block, }}} or }} is the end
15 --          Please maintain them to achieve better readability
16 -- **********************************************************************************************
17 
18 
19 -- ==============================================================================================
20 -- Global variable declarations
21 -- PS: Although they have been declared as private to the package,please note that each
22 -- concurrent request submitted has it's own session, hence each worker will have it's own
23 -- set of "global" variables and tables.
24 -- ==============================================================================================
25 
26 v_idx                               NUMBER;
27 v_wsli_idx                          NUMBER;
28 g_num_of_osp_exists                 NUMBER := 0;
29 g_op_seq_incr                       NUMBER := 10;
30 g_nvl_str                           VARCHAR2(6) := '!@#$56';
31 g_skip_wlbj                         NUMBER := 0;--OPTII-PERF
32 
33 g_charges_exist_all                 boolean := false;   -- bug 3423195
34 g_charges_exist_shopfloor           boolean := false;   -- bug 3423195
35 g_job_released_date                 date := null;       -- bug 3423195
36 
37 
38 -- ==============================================================================================
39 -- nested table types used to bulk bind data from wlji to the PL/SQL tables.
40 -- ==============================================================================================
41 type t_number                       is table of number       index by binary_integer;
42 type t_date                         is table of date         index by binary_integer;
43 type t_recoflag                     is table of varchar2(1)  index by binary_integer;
44 type t_errmsg                       is table of wsm_lot_job_interface.error_msg%type
45                                                              index by binary_integer;
46 type t_serial                       is table of VARCHAR2(30) index by binary_integer;
47 
48 
49 type t_wlji_err_code                is table of wsm_lot_job_interface.error_code%type;
50 type t_wlji_err_msg                 is table of wsm_lot_job_interface.error_msg%type;
51 type t_wlji_last_updt_date          is table of wsm_lot_job_interface.last_update_date%type;
52 type t_wlji_request_id              is table of wsm_lot_job_interface.request_id%type;
53 type t_wlji_program_id              is table of wsm_lot_job_interface.program_id%type;
54 type t_wlji_program_appl_id         is table of wsm_lot_job_interface.program_application_id%type;
55 type t_wlji_last_updt_by            is table of wsm_lot_job_interface.last_updated_by%type;
56 type t_wlji_creation_date           is table of wsm_lot_job_interface.creation_date%type;
57 type t_wlji_created_by              is table of wsm_lot_job_interface.created_by%type;
58 type t_wlji_last_updt_login         is table of wsm_lot_job_interface.last_update_login%type;
59 type t_wlji_prog_updt_date          is table of wsm_lot_job_interface.program_update_date%type;
60 type t_wlji_last_updt_by_name       is table of wsm_lot_job_interface.last_updated_by_name%type;
61 type t_wlji_created_by_name         is table of wsm_lot_job_interface.created_by_name%type;
62 type t_wlji_org_id                  is table of wsm_lot_job_interface.organization_id%type;
63 type t_wlji_org_code                is table of wsm_lot_job_interface.organization_code%type;
64 type t_wlji_wip_entity_id           is table of wsm_lot_job_interface.wip_entity_id%type;
65 type t_wlji_job_name                is table of wsm_lot_job_interface.job_name%type;
66 type t_wlji_lot_number              is table of wsm_lot_job_interface.lot_number%type;
67 type t_wlji_item_id                 is table of wsm_lot_job_interface.primary_item_id%type;
68 type t_wlji_header_id               is table of wsm_lot_job_interface.header_id%type;
69 type t_wlji_process_status          is table of wsm_lot_job_interface.process_status%type;
70 type t_wlji_process_phase           is table of wsm_lot_job_interface.process_phase%type;
71 type t_wlji_routing_reference_id    is table of wsm_lot_job_interface.routing_reference_id%type;
72 type t_wlji_completion_subinventory is table of wsm_lot_job_interface.completion_subinventory%type;
73 type t_wlji_completion_locator_id   is table of wsm_lot_job_interface.completion_locator_id%type;
74 type t_wlji_mode_flag               is table of wsm_lot_job_interface.mode_flag%type;
75 type t_wlji_group_id                is table of wsm_lot_job_interface.group_id%type;
76 type t_wlji_load_type               is table of wsm_lot_job_interface.load_type%type;
77 type t_wlji_status_type             is table of wsm_lot_job_interface.status_type%type;
78 type t_wlji_old_completion_date     is table of wsm_lot_job_interface.old_completion_date%type;
79 type t_wlji_bom_reference_id        is table of wsm_lot_job_interface.bom_reference_id%type;
80 type t_wlji_bom_revision_date       is table of wsm_lot_job_interface.bom_revision_date%type;
81 type t_wlji_routing_revision_date   is table of wsm_lot_job_interface.routing_revision_date%type;
82 type t_wlji_wip_supply_type         is table of wsm_lot_job_interface.wip_supply_type%type;
83 type t_wlji_class_code              is table of wsm_lot_job_interface.class_code%type;
84 type t_wlji_description             is table of wsm_lot_job_interface.description%type;
85 type t_wlji_firm_planned_flag       is table of wsm_lot_job_interface.firm_planned_flag%type;
86 type t_wlji_alt_routing_designator  is table of wsm_lot_job_interface.alternate_routing_designator%type;
87 type t_wlji_alt_bom_designator      is table of wsm_lot_job_interface.alternate_bom_designator%type;
88 type t_wlji_demand_class            is table of wsm_lot_job_interface.demand_class%type;
89 type t_wlji_start_quantity          is table of wsm_lot_job_interface.start_quantity%type;
90 type t_wlji_old_start_quantity      is table of wsm_lot_job_interface.old_start_quantity%type;
91 type t_wlji_error                   is table of wsm_lot_job_interface.error%type;
92 type t_wlji_fusd                    is table of wsm_lot_job_interface.first_unit_start_date%type;
93 type t_wlji_fucd                    is table of wsm_lot_job_interface.first_unit_completion_date%type;
94 type t_wlji_lusd                    is table of wsm_lot_job_interface.last_unit_start_date%type;
95 type t_wlji_lucd                    is table of wsm_lot_job_interface.last_unit_completion_date%type;
96 type t_wlji_scheduling_method       is table of wsm_lot_job_interface.scheduling_method%type;
97 type t_wlji_routing_revision        is table of wsm_lot_job_interface.routing_revision%type;
98 type t_wlji_bom_revision            is table of wsm_lot_job_interface.bom_revision%type;
99 type t_wlji_schedule_group_id       is table of wsm_lot_job_interface.schedule_group_id%type;
100 type t_wlji_schedule_group_name     is table of wsm_lot_job_interface.schedule_group_name%type;
101 type t_wlji_build_sequence          is table of wsm_lot_job_interface.build_sequence%type;
102 type t_wlji_net_quantity            is table of wsm_lot_job_interface.net_quantity%type;
103 type t_wlji_allow_explosion         is table of wsm_lot_job_interface.allow_explosion%type;
104 type t_wlji_old_status_type         is table of wsm_lot_job_interface.old_status_type%type;
105 type t_wlji_interface_id            is table of wsm_lot_job_interface.interface_id%type;
106 type t_wlji_coproducts_supply       is table of wsm_lot_job_interface.coproducts_supply%type;
107 type t_wlji_job_type                is table of wsm_lot_job_interface.job_type%type;
108 type t_wlji_source_code             is table of wsm_lot_job_interface.source_code%type;
109 type t_wlji_source_line_id          is table of wsm_lot_job_interface.source_line_id%type;
110 type t_wlji_project_id              is table of wsm_lot_job_interface.project_id%type;
111 type t_wlji_project_name            is table of wsm_lot_job_interface.project_name%type;
112 type t_wlji_task_id                 is table of wsm_lot_job_interface.task_id%type;
113 type t_wlji_delivery_id             is table of wsm_lot_job_interface.delivery_id%type;
114 type t_wlji_desc_flx_segs           is table of wsm_lot_job_interface.descriptive_flex_segments%type;
115 type t_wlji_project_number          is table of wsm_lot_job_interface.project_number%type;
116 type t_wlji_task_number             is table of wsm_lot_job_interface.task_number%type;
117 type t_wlji_project_costed          is table of wsm_lot_job_interface.project_costed%type;
118 type t_wlji_end_item_unit_number    is table of wsm_lot_job_interface.end_item_unit_number%type;
119 type t_wlji_overcompl_tol_type      is table of wsm_lot_job_interface.overcompletion_tolerance_type%type;
120 type t_wlji_overcompl_tol_value     is table of wsm_lot_job_interface.overcompletion_tolerance_value%type;
121 type t_wlji_kanban_card_id          is table of wsm_lot_job_interface.kanban_card_id%type;
122 type t_wlji_priority                is table of wsm_lot_job_interface.priority%type;
123 type t_wlji_due_date                is table of wsm_lot_job_interface.due_date%type;
124 type t_wlji_task_name               is table of wsm_lot_job_interface.task_name%type;
125 type t_wlji_process_type            is table of wsm_lot_job_interface.process_type%type;
126 type t_wlji_processing_work_days    is table of wsm_lot_job_interface.processing_work_days%type;
127 type t_wlji_compl_loc_segs          is table of wsm_lot_job_interface.completion_locator_segments%type;
128 type t_wlji_daily_production_rate   is table of wsm_lot_job_interface.daily_production_rate%type;
129 type t_wlji_line_id                 is table of wsm_lot_job_interface.line_id%type;
130 type t_wlji_lot_control_code        is table of wsm_lot_job_interface.lot_control_code%type;
131 type t_wlji_repetitive_schedule_id  is table of wsm_lot_job_interface.repetitive_schedule_id%type;
132 type t_wlji_parent_group_id         is table of wsm_lot_job_interface.parent_group_id%type;
133 type t_wlji_attribute_category      is table of wsm_lot_job_interface.attribute_category%type;
134 type t_wlji_attribute1              is table of wsm_lot_job_interface.attribute1%type;
135 type t_wlji_attribute2              is table of wsm_lot_job_interface.attribute2%type;
136 type t_wlji_attribute3              is table of wsm_lot_job_interface.attribute3%type;
137 type t_wlji_attribute4              is table of wsm_lot_job_interface.attribute4%type;
138 type t_wlji_attribute5              is table of wsm_lot_job_interface.attribute5%type;
139 type t_wlji_attribute6              is table of wsm_lot_job_interface.attribute6%type;
140 type t_wlji_attribute7              is table of wsm_lot_job_interface.attribute7%type;
141 type t_wlji_attribute8              is table of wsm_lot_job_interface.attribute8%type;
142 type t_wlji_attribute9              is table of wsm_lot_job_interface.attribute9%type;
143 type t_wlji_attribute10             is table of wsm_lot_job_interface.attribute10%type;
144 type t_wlji_attribute11             is table of wsm_lot_job_interface.attribute11%type;
145 type t_wlji_attribute12             is table of wsm_lot_job_interface.attribute12%type;
146 type t_wlji_attribute13             is table of wsm_lot_job_interface.attribute13%type;
147 type t_wlji_attribute14             is table of wsm_lot_job_interface.attribute14%type;
148 type t_wlji_attribute15             is table of wsm_lot_job_interface.attribute15%type;
149 type t_wlji_line_code               is table of wsm_lot_job_interface.line_code%type;
150 type t_wlji_primary_item_segs       is table of wsm_lot_job_interface.primary_item_segments%type;
151 type t_wlji_bom_ref_segs            is table of wsm_lot_job_interface.bom_reference_segments%type;
152 type t_wlji_rtg_ref_segs            is table of wsm_lot_job_interface.routing_reference_segments%type;
153 type t_wlji_date_released           is table of wsm_lot_job_interface.date_released%type;
154 type t_wlji_transaction_date        is table of wsm_lot_job_interface.transaction_date%type;
155 type t_wlji_num_of_children         is table of wsm_lot_job_interface.num_of_children%type;
156 --type t_wlji_inf_schedule_flag     is table of wsm_lot_job_interface.infinite_schedule_flag%type;
157 
158 -- ==============================================================================================
159 -- instantiating the tables used to bulk bind data from wlji to the PL/SQL tables.
160 -- ==============================================================================================
161 v_wlji_err_code                 t_wlji_err_code                 := t_wlji_err_code();
162 v_wlji_err_msg                  t_wlji_err_msg                  := t_wlji_err_msg();
163 v_wlji_last_updt_date           t_wlji_last_updt_date           := t_wlji_last_updt_date();
164 v_wlji_request_id               t_wlji_request_id               := t_wlji_request_id();
165 v_wlji_program_id               t_wlji_program_id               := t_wlji_program_id();
166 v_wlji_program_appl_id          t_wlji_program_appl_id          := t_wlji_program_appl_id();
167 v_wlji_last_updt_by             t_wlji_last_updt_by             := t_wlji_last_updt_by();
168 v_wlji_creation_date            t_wlji_creation_date            := t_wlji_creation_date();
169 v_wlji_created_by               t_wlji_created_by               := t_wlji_created_by();
170 v_wlji_last_updt_login          t_wlji_last_updt_login          := t_wlji_last_updt_login();
171 v_wlji_prog_updt_date           t_wlji_prog_updt_date           := t_wlji_prog_updt_date();
172 v_wlji_last_updt_by_name        t_wlji_last_updt_by_name        := t_wlji_last_updt_by_name();
173 v_wlji_created_by_name          t_wlji_created_by_name          := t_wlji_created_by_name();
174 v_wlji_org_id                   t_wlji_org_id                   := t_wlji_org_id();
175 v_wlji_org_code                 t_wlji_org_code                 := t_wlji_org_code();  -- Not used, should
176 v_wlji_wip_entity_id            t_wlji_wip_entity_id            := t_wlji_wip_entity_id();
177 v_wlji_job_name                 t_wlji_job_name                 := t_wlji_job_name();
178 v_wlji_lot_number               t_wlji_lot_number               := t_wlji_lot_number();
179 v_wlji_item_id                  t_wlji_item_id                  := t_wlji_item_id();
180 v_wlji_header_id                t_wlji_header_id                := t_wlji_header_id();
181 v_wlji_process_status           t_wlji_process_status           := t_wlji_process_status();
182 v_wlji_rtg_ref_id               t_wlji_routing_reference_id     := t_wlji_routing_reference_id();
183 v_wlji_compl_subinv             t_wlji_completion_subinventory  := t_wlji_completion_subinventory();
184 v_wlji_compl_loc_id             t_wlji_completion_locator_id    := t_wlji_completion_locator_id();
185 v_wlji_mode_flag                t_wlji_mode_flag                := t_wlji_mode_flag();
186 v_wlji_group_id                 t_wlji_group_id                 := t_wlji_group_id();
187 v_wlji_load_type                t_wlji_load_type                := t_wlji_load_type();
188 v_wlji_status_type              t_wlji_status_type              := t_wlji_status_type();
189 v_wlji_old_status_type          t_wlji_old_status_type          := t_wlji_old_status_type();  -- Not used
190 v_wlji_old_completion_date      t_wlji_old_completion_date      := t_wlji_old_completion_date();  -- Not used
191 v_wlji_bom_ref_id               t_wlji_bom_reference_id         := t_wlji_bom_reference_id();
192 v_wlji_bom_rev_date             t_wlji_bom_revision_date        := t_wlji_bom_revision_date();
193 v_wlji_rtg_rev_date             t_wlji_routing_revision_date    := t_wlji_routing_revision_date();
194 v_wlji_wip_supply_type          t_wlji_wip_supply_type          := t_wlji_wip_supply_type();
195 v_wlji_class_code               t_wlji_class_code               := t_wlji_class_code();
196 v_wlji_description              t_wlji_description              := t_wlji_description();
197 v_wlji_firm_planned_flag        t_wlji_firm_planned_flag        := t_wlji_firm_planned_flag();
198 v_wlji_alt_rtg                  t_wlji_alt_routing_designator   := t_wlji_alt_routing_designator();
199 v_wlji_alt_bom                  t_wlji_alt_bom_designator       := t_wlji_alt_bom_designator();
200 v_wlji_demand_class             t_wlji_demand_class             := t_wlji_demand_class();
201 v_wlji_start_quantity           t_wlji_start_quantity           := t_wlji_start_quantity();
202 v_wlji_old_start_quantity       t_wlji_old_start_quantity       := t_wlji_old_start_quantity();  -- Not used
203 v_wlji_error                    t_wlji_error                    := t_wlji_error();  -- Not used
204 v_wlji_process_phase            t_wlji_process_phase            := t_wlji_process_phase();  -- Not used
205 v_wlji_fusd                     t_wlji_fusd                     := t_wlji_fusd();
206 v_wlji_fucd                     t_wlji_fucd                     := t_wlji_fucd();  -- Not used
207 v_wlji_lusd                     t_wlji_lusd                     := t_wlji_lusd();  -- Not used
208 v_wlji_lucd                     t_wlji_lucd                     := t_wlji_lucd();
209 v_wlji_scheduling_method        t_wlji_scheduling_method        := t_wlji_scheduling_method();
210 v_wlji_rtg_revision             t_wlji_routing_revision         := t_wlji_routing_revision();
211 v_wlji_bom_revision             t_wlji_bom_revision             := t_wlji_bom_revision();
212 v_wlji_schedule_group_id        t_wlji_schedule_group_id        := t_wlji_schedule_group_id();
213 v_wlji_schedule_group_name      t_wlji_schedule_group_name      := t_wlji_schedule_group_name();
214 v_wlji_build_sequence           t_wlji_build_sequence           := t_wlji_build_sequence();
215 v_wlji_net_quantity             t_wlji_net_quantity             := t_wlji_net_quantity();
216 v_wlji_allow_explosion          t_wlji_allow_explosion          := t_wlji_allow_explosion();
217 v_wlji_interface_id             t_wlji_interface_id             := t_wlji_interface_id();            -- Not used
218 v_wlji_coproducts_supply        t_wlji_coproducts_supply        := t_wlji_coproducts_supply();
219 v_wlji_job_type                 t_wlji_job_type                 := t_wlji_job_type();
220 v_wlji_source_code              t_wlji_source_code              := t_wlji_source_code();
221 v_wlji_src_line_id              t_wlji_source_line_id           := t_wlji_source_line_id();
222 v_wlji_process_type             t_wlji_process_type             := t_wlji_process_type();
223 v_wlji_processing_work_days     t_wlji_processing_work_days     := t_wlji_processing_work_days(); -- Not used
224 v_wlji_daily_production_rate    t_wlji_daily_production_rate    := t_wlji_daily_production_rate();  -- Not used
225 v_wlji_line_id                  t_wlji_line_id                  := t_wlji_line_id();
226 v_wlji_lot_control_code         t_wlji_lot_control_code         := t_wlji_lot_control_code();  -- Not used
227 v_wlji_repetitive_schedule_id   t_wlji_repetitive_schedule_id   := t_wlji_repetitive_schedule_id();  -- Not used
228 v_wlji_parent_group_id          t_wlji_parent_group_id          := t_wlji_parent_group_id();  -- Not used
229 v_wlji_attribute_category       t_wlji_attribute_category       := t_wlji_attribute_category();
230 v_wlji_attribute1               t_wlji_attribute1               := t_wlji_attribute1();
231 v_wlji_attribute2               t_wlji_attribute2               := t_wlji_attribute2();
232 v_wlji_attribute3               t_wlji_attribute3               := t_wlji_attribute3();
233 v_wlji_attribute4               t_wlji_attribute4               := t_wlji_attribute4();
234 v_wlji_attribute5               t_wlji_attribute5               := t_wlji_attribute5();
235 v_wlji_attribute6               t_wlji_attribute6               := t_wlji_attribute6();
236 v_wlji_attribute7               t_wlji_attribute7               := t_wlji_attribute7();
237 v_wlji_attribute8               t_wlji_attribute8               := t_wlji_attribute8();
238 v_wlji_attribute9               t_wlji_attribute9               := t_wlji_attribute9();
239 v_wlji_attribute10              t_wlji_attribute10              := t_wlji_attribute10();
240 v_wlji_attribute11              t_wlji_attribute11              := t_wlji_attribute11();
241 v_wlji_attribute12              t_wlji_attribute12              := t_wlji_attribute12();
242 v_wlji_attribute13              t_wlji_attribute13              := t_wlji_attribute13();
243 v_wlji_attribute14              t_wlji_attribute14              := t_wlji_attribute14();
244 v_wlji_attribute15              t_wlji_attribute15              := t_wlji_attribute15();
245 v_wlji_line_code                t_wlji_line_code                := t_wlji_line_code();  -- Not used
246 v_wlji_primary_item_segs        t_wlji_primary_item_segs        := t_wlji_primary_item_segs();  -- Not used
247 v_wlji_bom_ref_segs             t_wlji_bom_ref_segs             := t_wlji_bom_ref_segs();  -- Not used
248 v_wlji_rtg_ref_segs             t_wlji_rtg_ref_segs             := t_wlji_rtg_ref_segs();  -- Not used
249 v_wlji_compl_loc_segs           t_wlji_compl_loc_segs           := t_wlji_compl_loc_segs();  -- Not used
250 v_wlji_desc_flx_segs            t_wlji_desc_flx_segs            := t_wlji_desc_flx_segs();   -- Not used
251 v_wlji_project_id               t_wlji_project_id               := t_wlji_project_id();  -- No Validation
252 v_wlji_project_name             t_wlji_project_name             := t_wlji_project_name();  -- Not used
253 v_wlji_project_number           t_wlji_project_number           := t_wlji_project_number();  -- Not used
254 v_wlji_project_costed           t_wlji_project_costed           := t_wlji_project_costed();  -- Not used
255 v_wlji_task_id                  t_wlji_task_id                  := t_wlji_task_id();  -- No validation
256 v_wlji_task_name                t_wlji_task_name                := t_wlji_task_name();  -- Not used
257 v_wlji_task_number              t_wlji_task_number              := t_wlji_task_number();  -- Not used
258 v_wlji_end_item_unit_number     t_wlji_end_item_unit_number     := t_wlji_end_item_unit_number();  -- No validation
259 v_wlji_overcompl_tol_type       t_wlji_overcompl_tol_type       := t_wlji_overcompl_tol_type();  -- No validation
260 v_wlji_overcompl_tol_value      t_wlji_overcompl_tol_value      := t_wlji_overcompl_tol_value();  -- No validation
261 v_wlji_kanban_card_id           t_wlji_kanban_card_id           := t_wlji_kanban_card_id();
262 v_wlji_priority                 t_wlji_priority                 := t_wlji_priority();
263 v_wlji_due_date                 t_wlji_due_date                 := t_wlji_due_date();
264 v_wlji_delivery_id              t_wlji_delivery_id              := t_wlji_delivery_id();  -- No validation
265 v_wlji_date_released            t_wlji_date_released            := t_wlji_date_released();
266 v_wlji_transaction_date         t_wlji_transaction_date         := t_wlji_transaction_date();
267 v_wlji_num_of_children          t_wlji_num_of_children          := t_wlji_num_of_children();
268 --v_wlji_inf_schedule_flag      t_wlji_inf_schedule_flag        := t_wlji_inf_schedule_flag();
269 v_new_job               NUMBER := NULL;
270 v_phantom_exists        NUMBER := NULL; --OPTII-PERF: 1-Phantom req exits,2-Phantom requirement does not exists
271 v_insert_wip            NUMBER := NULL; --OPTII-PERF  1-Insert wip data in create_jobcopies procedure
272                                 --            2-Phantom requirement does not exists
273 -- ==============================================================================================
274 -- corresponding table type and table declarations for wsm_starting_lots_interface
275 -- ==============================================================================================
276 type t_wsli_header_id           is table of wsm_starting_lots_interface.header_id%type;
277 type t_wsli_lot_number          is table of wsm_starting_lots_interface.lot_number%type;
278 type t_wsli_inventory_item_id   is table of wsm_starting_lots_interface.inventory_item_id%type;
279 type t_wsli_organization_id     is table of wsm_starting_lots_interface.organization_id%type;
280 type t_wsli_quantity            is table of wsm_starting_lots_interface.quantity%type;
281 type t_wsli_subinventory_code   is table of wsm_starting_lots_interface.subinventory_code%type;
282 type t_wsli_locator_id          is table of wsm_starting_lots_interface.locator_id%type;
283 type t_wsli_revision            is table of wsm_starting_lots_interface.revision%type;
284 type t_wsli_last_updated_by     is table of wsm_starting_lots_interface.last_updated_by%type;
285 type t_wsli_created_by          is table of wsm_starting_lots_interface.created_by%type;
286 type t_wsli_primary_uom_code    is table of mtl_system_items.primary_uom_code%type;
287 type t_wsli_comp_issue_qty      is table of wsm_starting_lots_interface.component_issue_quantity%type;
288 
289 v_wsli_hdr_id                   t_wsli_header_id                := t_wsli_header_id();
290 v_wsli_lot_number               t_wsli_lot_number               := t_wsli_lot_number();
291 v_wsli_inventory_item_id        t_wsli_inventory_item_id        := t_wsli_inventory_item_id();
292 v_wsli_organization_id          t_wsli_organization_id          := t_wsli_organization_id();
293 v_wsli_quantity                 t_wsli_quantity                 := t_wsli_quantity();
294 v_wsli_subinventory_code        t_wsli_subinventory_code        := t_wsli_subinventory_code();
295 v_wsli_locator_id               t_wsli_locator_id               := t_wsli_locator_id();
296 v_wsli_revision                 t_wsli_revision                 := t_wsli_revision();
297 v_wsli_last_updated_by          t_wsli_last_updated_by          := t_wsli_last_updated_by();
298 v_wsli_created_by               t_wsli_created_by               := t_wsli_created_by();
299 v_wsli_primary_uom_code         t_wsli_primary_uom_code         := t_wsli_primary_uom_code();
300 v_wsli_comp_issue_qty           t_wsli_comp_issue_qty           := t_wsli_comp_issue_qty();
301 
302 -- ==============================================================================================
303 -- creating an index by table that'll store the wsli values with header_id as the index for easy
304 -- validation
305 -- ==============================================================================================
306 type rec_wsli IS record (
307         lot_number              wsm_starting_lots_interface.lot_number%type,
308         inventory_item_id       wsm_starting_lots_interface.inventory_item_id%type,
309         organization_id         wsm_starting_lots_interface.organization_id%type,
310         quantity                wsm_starting_lots_interface.quantity%type,
311         subinventory_code       wsm_starting_lots_interface.subinventory_code%type,
312         locator_id              wsm_starting_lots_interface.locator_id%type,
313         revision                wsm_starting_lots_interface.revision%type,
314         last_updated_by         wsm_starting_lots_interface.last_updated_by%type,
315         created_by              wsm_starting_lots_interface.created_by%type,
316         primary_uom_code        mtl_system_items.primary_uom_code%type,
317         comp_issue_quantity     wsm_starting_lots_interface.component_issue_quantity%type
318 );
319 
320 v_rec_wsli                      rec_wsli;   -- Not used
321 
322 type t_wsli                     is table of rec_wsli index by binary_integer;
323 v_wsli                          t_wsli;
324 
325 -- ==============================================================================================
326 -- nested table types used to bulk bind data from wljdi to the PL/SQL tables.
327 -- ==============================================================================================
328 type t_wljdi_record_id              is table of wsm_lot_job_dtl_interface.record_id                 %type;
329 type t_wljdi_parent_header_id       is table of wsm_lot_job_dtl_interface.parent_header_id          %type;
330 type t_wljdi_group_id               is table of wsm_lot_job_dtl_interface.group_id                  %type;
331 type t_wljdi_org_id                 is table of wsm_lot_job_dtl_interface.organization_id           %type;
332 type t_wljdi_org_code               is table of wsm_lot_job_dtl_interface.organization_code         %type;
333 type t_wljdi_wip_entity_id          is table of wsm_lot_job_dtl_interface.wip_entity_id             %type;
334 type t_wljdi_job_name               is table of wsm_lot_job_dtl_interface.job_name                  %type;
335 type t_wljdi_err_code               is table of wsm_lot_job_dtl_interface.error_code                %type;
336 type t_wljdi_err_msg                is table of wsm_lot_job_dtl_interface.error_msg                 %type;
337 type t_wljdi_request_id             is table of wsm_lot_job_dtl_interface.request_id                %type;
338 type t_wljdi_program_id             is table of wsm_lot_job_dtl_interface.program_id                %type;
339 type t_wljdi_program_appl_id        is table of wsm_lot_job_dtl_interface.program_application_id    %type;
340 type t_wljdi_last_updt_by           is table of wsm_lot_job_dtl_interface.last_updated_by           %type;
341 type t_wljdi_last_updt_login        is table of wsm_lot_job_dtl_interface.last_update_login         %type;
342 type t_wljdi_last_updt_date         is table of wsm_lot_job_dtl_interface.last_update_date          %type;
343 type t_wljdi_prog_updt_date         is table of wsm_lot_job_dtl_interface.program_update_date       %type;
344 type t_wljdi_process_status         is table of wsm_lot_job_dtl_interface.process_status            %type;
345 
346 
347 -- BA: bug 3313463/3311695 8i compatibility
348 type t_wljdi_load_type              is table of wsm_lot_job_dtl_interface.load_type                 %type;
349 type t_wljdi_substitution_type      is table of wsm_lot_job_dtl_interface.substitution_type         %type;
350 type t_wljdi_job_op_seq_num         is table of wsm_lot_job_dtl_interface.job_op_seq_num            %type;
351 type t_wljdi_routing_op_seq_num     is table of wsm_lot_job_dtl_interface.routing_op_seq_num        %type;
352 type t_wljdi_next_rtg_op_seq_num    is table of wsm_lot_job_dtl_interface.next_routing_op_seq_num   %type;
353 
354 type t_wljdi_inventory_item_id_new  is table of wsm_lot_job_dtl_interface.inventory_item_id_new     %type;
355 type t_wljdi_inventory_item_new     is table of wsm_lot_job_dtl_interface.inventory_item_new        %type;
356 type t_wljdi_primary_item           is table of wsm_lot_job_dtl_interface.primary_item              %type;
357 type t_wljdi_primary_item_id        is table of wsm_lot_job_dtl_interface.primary_item_id           %type;
358 type t_wljdi_src_phantom_item       is table of wsm_lot_job_dtl_interface.src_phantom_item          %type;
359 type t_wljdi_src_phantom_item_id    is table of wsm_lot_job_dtl_interface.src_phantom_item_id       %type;
360 type t_wljdi_component_sequence_id  is table of wsm_lot_job_dtl_interface.component_sequence_id     %type;
361 type t_wljdi_date_required          is table of wsm_lot_job_dtl_interface.date_required             %type;
362 type t_wljdi_scheduled_quantity     is table of wsm_lot_job_dtl_interface.scheduled_quantity        %type;
363 type t_wljdi_required_quantity      is table of wsm_lot_job_dtl_interface.required_quantity         %type;
364 type t_wljdi_batch_id               is table of wsm_lot_job_dtl_interface.batch_id                  %type;
365 type t_wljdi_assigned_units         is table of wsm_lot_job_dtl_interface.assigned_units            %type;
366 type t_wljdi_instance_id_new        is table of wsm_lot_job_dtl_interface.instance_id_new           %type;
367 type t_wljdi_op_completion_date     is table of wsm_lot_job_dtl_interface.operation_completion_date %type;
368 type t_wljdi_op_start_date          is table of wsm_lot_job_dtl_interface.operation_start_date      %type;
369 type t_wljdi_op_yield               is table of wsm_lot_job_dtl_interface.operation_yield           %type;
370 type t_wljdi_resource_code_new      is table of wsm_lot_job_dtl_interface.resource_code_new         %type;
371 type t_wljdi_resource_id_new        is table of wsm_lot_job_dtl_interface.resource_id_new           %type;
372 type t_wljdi_resource_seq_num       is table of wsm_lot_job_dtl_interface.resource_seq_num          %type;
373 type t_wljdi_substitute_group_num   is table of wsm_lot_job_dtl_interface.substitute_group_num      %type;
374 type t_wljdi_replacement_group_num  is table of wsm_lot_job_dtl_interface.replacement_group_num     %type;
375 type t_wljdi_serial_number_new      is table of wsm_lot_job_dtl_interface.serial_number_new         %type;
376 type t_wljdi_start_date             is table of wsm_lot_job_dtl_interface.start_date                %type;
377 type t_wljdi_completion_date        is table of wsm_lot_job_dtl_interface.completion_date           %type;
378 -- EA: bug 3313463/3311695 8i compatibility
379 
380 /* ST : Detailed scheduling : start */
381 type t_wljdi_resource_hours         is table of wsm_lot_job_dtl_interface.resource_hours        %type;
382 type t_wljdi_firm_type              is table of wsm_lot_job_dtl_interface.firm_type             %type;
383 type t_wljdi_setup_id               is table of wsm_lot_job_dtl_interface.setup_id              %type;
384 type t_wljdi_setup_code               is table of wsm_lot_job_dtl_interface.setup_code              %type;
385 type t_wljdi_grp_seq_id             is table of wsm_lot_job_dtl_interface.group_sequence_id     %type;
386 type t_wljdi_grp_seq_num            is table of wsm_lot_job_dtl_interface.group_sequence_num    %type;
387 type t_wljdi_max_assg_units         is table of wsm_lot_job_dtl_interface.max_assigned_units    %type;
388 type t_wljdi_parent_seq_num         is table of wsm_lot_job_dtl_interface.parent_resource_seq_num   %type;
389 
390 /* Start : fields added to add resource */
391 type t_wljdi_department_id          is table of wsm_lot_job_dtl_interface.department_id         %type;
392 type t_wljdi_department_code        is table of wsm_lot_job_dtl_interface.department_code       %type;
393 type t_wljdi_activity_id            is table of wsm_lot_job_dtl_interface.activity_id           %type;
394 type t_wljdi_standard_rate_flag     is table of wsm_lot_job_dtl_interface.standard_rate_flag    %type;
395 type t_wljdi_usage_rate_or_amt      is table of wsm_lot_job_dtl_interface.usage_rate_or_amount  %type;
396 
397 type t_wljdi_basis_type         is table of wsm_lot_job_dtl_interface.basis_type                %type;
398 type t_wljdi_uom_code           is table of wsm_lot_job_dtl_interface.uom_code                  %type;
399 type t_wljdi_scheduled_flag     is table of wsm_lot_job_dtl_interface.scheduled_flag            %type;
400 
401 type t_wljdi_res_autocharge_type    is table of wsm_lot_job_dtl_interface.autocharge_type       %type;
402 type t_wljdi_schedule_seq_num       is table of wsm_lot_job_dtl_interface.schedule_seq_num      %type;
403 
404 type t_wljdi_attribute_category    is table of wsm_lot_job_dtl_interface.attribute_category     %type;
405 type t_wljdi_attribute1        is table of wsm_lot_job_dtl_interface.attribute1                 %type;
406 type t_wljdi_attribute2        is table of wsm_lot_job_dtl_interface.attribute2                 %type;
407 type t_wljdi_attribute3        is table of wsm_lot_job_dtl_interface.attribute3                 %type;
408 type t_wljdi_attribute4        is table of wsm_lot_job_dtl_interface.attribute4                 %type;
409 type t_wljdi_attribute5        is table of wsm_lot_job_dtl_interface.attribute5                 %type;
410 type t_wljdi_attribute6        is table of wsm_lot_job_dtl_interface.attribute6                 %type;
411 type t_wljdi_attribute7        is table of wsm_lot_job_dtl_interface.attribute7                 %type;
412 type t_wljdi_attribute8        is table of wsm_lot_job_dtl_interface.attribute8                 %type;
413 type t_wljdi_attribute9        is table of wsm_lot_job_dtl_interface.attribute9                 %type;
414 type t_wljdi_attribute10       is table of wsm_lot_job_dtl_interface.attribute10                %type;
415 type t_wljdi_attribute11       is table of wsm_lot_job_dtl_interface.attribute11                %type;
416 type t_wljdi_attribute12       is table of wsm_lot_job_dtl_interface.attribute12                %type;
417 type t_wljdi_attribute13       is table of wsm_lot_job_dtl_interface.attribute13                %type;
418 type t_wljdi_attribute14       is table of wsm_lot_job_dtl_interface.attribute14                %type;
419 type t_wljdi_attribute15       is table of wsm_lot_job_dtl_interface.attribute15                %type;
420 
421 -- Added For LBJ Interface Alcoa Enhancement
422 type t_wljdi_recommended       is table of wsm_lot_job_dtl_interface.recommended                %type;
423 type t_wljdi_std_op_code       is table of wsm_lot_job_dtl_interface.standard_operation_code    %type;
424 type t_wljdi_std_op_id         is table of wsm_lot_job_dtl_interface.standard_operation_id      %type;
425 type t_wljdi_op_yield_enable   is table of wsm_lot_job_dtl_interface.operation_yield_enabled    %type;
426 type t_wljdi_min_trf_qty       is table of wsm_lot_job_dtl_interface.minimum_transfer_quantity  %type;
427 type t_wljdi_backflush_flag    is table of wsm_lot_job_dtl_interface.backflush_flag             %type;
428 type t_wljdi_count_point_type  is table of wsm_lot_job_dtl_interface.count_point_type           %type;
429 type t_wljdi_description       is table of wsm_lot_job_dtl_interface.description                %type;
430 type t_wljdi_inc_in_rollup     is table of wsm_lot_job_dtl_interface.include_in_rollup          %type;
431 type t_wljdi_option_dep_flag   is table of wsm_lot_job_dtl_interface.option_dependent_flag      %type;
432 type t_wljdi_mrp_net_flag      is table of wsm_lot_job_dtl_interface.mrp_net_flag               %type;
433 type t_wljdi_auto_request_material is table of wsm_lot_job_dtl_interface.auto_request_material  %type;
434 type t_wljdi_comments          is table of wsm_lot_job_dtl_interface.comments                   %type;
435 type t_wljdi_inventory_item_id_old  is table of wsm_lot_job_dtl_interface.inventory_item_id_old     %type;
436 type t_wljdi_inventory_item_old     is table of wsm_lot_job_dtl_interface.inventory_item_old        %type;
437 type t_wljdi_wip_supply_type        is table of wsm_lot_job_dtl_interface.wip_supply_type           %type;
438 type t_wljdi_quantity_per_assembly  is table of wsm_lot_job_dtl_interface.quantity_per_assembly     %type;
439 type t_wljdi_supply_subinventory    is table of wsm_lot_job_dtl_interface.supply_subinventory       %type;
440 type t_wljdi_supply_locator_id      is table of wsm_lot_job_dtl_interface.supply_locator_id         %type;
441 type t_wljdi_applied_resource_units is table of wsm_lot_job_dtl_interface.applied_resource_units    %type;
442 type t_desc                         is table of bom_standard_operations.operation_description%type index by binary_integer;
443 type t_op_code                      is table of bom_standard_operations.operation_code%type index by binary_integer;
444 
445 /*Added below two types for bugfix:7248992 */
446 type t_wljdi_cum_yield             is table of wsm_lot_job_dtl_interface.cumulative_yield          %type;
447 type t_wljdi_rev_cum_yield         is table of wsm_lot_job_dtl_interface.reverse_cumulative_yield  %type;
448 /* end */
449 
450 /* ST : Detailed scheduling : end */
451 
452 -- ==============================================================================================
453 -- instantiating the tables used to bulk bind data from wljdi to the PL/SQL tables.
454 -- ==============================================================================================
455 v_wljdi_record_id               t_wljdi_record_id               := t_wljdi_record_id();
456 v_wljdi_parent_header_id        t_wljdi_parent_header_id        := t_wljdi_parent_header_id();
457 v_wljdi_group_id                t_wljdi_group_id                := t_wljdi_group_id();
458 v_wljdi_org_id                  t_wljdi_org_id                  := t_wljdi_org_id();
459 v_wljdi_org_code                t_wljdi_org_code                := t_wljdi_org_code();
460 v_wljdi_wip_entity_id           t_wljdi_wip_entity_id           := t_wljdi_wip_entity_id();
461 v_wljdi_job_name                t_wljdi_job_name                := t_wljdi_job_name();
462 v_wljdi_err_code                t_wljdi_err_code                := t_wljdi_err_code();
463 v_wljdi_err_msg                 t_wljdi_err_msg                 := t_wljdi_err_msg();
464 v_wljdi_request_id              t_wljdi_request_id              := t_wljdi_request_id();
465 v_wljdi_program_id              t_wljdi_program_id              := t_wljdi_program_id();
466 v_wljdi_program_appl_id         t_wljdi_program_appl_id         := t_wljdi_program_appl_id();
467 v_wljdi_last_updt_login         t_wljdi_last_updt_login         := t_wljdi_last_updt_login();
468 v_wljdi_last_updt_by            t_wljdi_last_updt_by            := t_wljdi_last_updt_by();
469 v_wljdi_last_updt_date          t_wljdi_last_updt_date          := t_wljdi_last_updt_date();
470 v_wljdi_prog_updt_date          t_wljdi_prog_updt_date          := t_wljdi_prog_updt_date();
471 v_wljdi_process_status          t_wljdi_process_status          := t_wljdi_process_status();
472 
473 
474 -- BA: bug 3313463/3311695 8i compatibility
475 v_wljdi_load_type               t_wljdi_load_type              := t_wljdi_load_type();
476 v_wljdi_substitution_type       t_wljdi_substitution_type      := t_wljdi_substitution_type();
477 v_wljdi_job_op_seq_num          t_wljdi_job_op_seq_num         := t_wljdi_job_op_seq_num();
478 v_wljdi_routing_op_seq_num      t_wljdi_routing_op_seq_num     := t_wljdi_routing_op_seq_num();
479 v_wljdi_next_rtg_op_seq_num     t_wljdi_next_rtg_op_seq_num    := t_wljdi_next_rtg_op_seq_num();
480 
481 v_wljdi_inventory_item_id_new   t_wljdi_inventory_item_id_new  := t_wljdi_inventory_item_id_new();
482 v_wljdi_inventory_item_new      t_wljdi_inventory_item_new     := t_wljdi_inventory_item_new();
483 v_wljdi_primary_item            t_wljdi_primary_item           := t_wljdi_primary_item();
484 v_wljdi_primary_item_id         t_wljdi_primary_item_id        := t_wljdi_primary_item_id();
485 v_wljdi_src_phantom_item        t_wljdi_src_phantom_item       := t_wljdi_src_phantom_item();
486 v_wljdi_src_phantom_item_id     t_wljdi_src_phantom_item_id    := t_wljdi_src_phantom_item_id();
487 v_wljdi_component_sequence_id   t_wljdi_component_sequence_id  := t_wljdi_component_sequence_id();
488 v_wljdi_date_required           t_wljdi_date_required          := t_wljdi_date_required();
489 v_wljdi_scheduled_quantity      t_wljdi_scheduled_quantity     := t_wljdi_scheduled_quantity();
490 v_wljdi_required_quantity       t_wljdi_required_quantity      := t_wljdi_required_quantity();
491 v_wljdi_batch_id                t_wljdi_batch_id               := t_wljdi_batch_id();
492 v_wljdi_assigned_units          t_wljdi_assigned_units         := t_wljdi_assigned_units();
493 v_wljdi_instance_id_new         t_wljdi_instance_id_new        := t_wljdi_instance_id_new();
494 v_wljdi_op_completion_date      t_wljdi_op_completion_date     := t_wljdi_op_completion_date();
495 v_wljdi_op_start_date           t_wljdi_op_start_date          := t_wljdi_op_start_date();
496 v_wljdi_op_yield                t_wljdi_op_yield               := t_wljdi_op_yield();
497 v_wljdi_resource_code_new       t_wljdi_resource_code_new      := t_wljdi_resource_code_new();
498 v_wljdi_resource_id_new         t_wljdi_resource_id_new        := t_wljdi_resource_id_new();
499 v_wljdi_resource_seq_num        t_wljdi_resource_seq_num       := t_wljdi_resource_seq_num();
500 v_wljdi_substitute_group_num    t_wljdi_substitute_group_num   := t_wljdi_substitute_group_num();
501 v_wljdi_replacement_group_num   t_wljdi_replacement_group_num  := t_wljdi_replacement_group_num();
502 v_wljdi_serial_number_new       t_wljdi_serial_number_new      := t_wljdi_serial_number_new();
503 v_wljdi_start_date              t_wljdi_start_date             := t_wljdi_start_date();
504 v_wljdi_completion_date         t_wljdi_completion_date        := t_wljdi_completion_date();
505 -- EA: bug 3313463/3311695 8i compatibility
506 
507 /* ST :  Detailed scheduling : start */
508 v_wljdi_firm_type           t_wljdi_firm_type              := t_wljdi_firm_type();
509 v_wljdi_setup_id            t_wljdi_setup_id               := t_wljdi_setup_id();
510 v_wljdi_setup_code          t_wljdi_setup_code             := t_wljdi_setup_code();
511 v_wljdi_grp_seq_id          t_wljdi_grp_seq_id             := t_wljdi_grp_seq_id();
512 v_wljdi_grp_seq_num         t_wljdi_grp_seq_num            := t_wljdi_grp_seq_num();
513 v_wljdi_max_assg_units      t_wljdi_max_assg_units         := t_wljdi_max_assg_units();
514 v_wljdi_parent_seq_num      t_wljdi_parent_seq_num         := t_wljdi_parent_seq_num();
515 v_wljdi_resource_hours      t_wljdi_resource_hours         := t_wljdi_resource_hours();
516 
517 /* Start : changes for adding resource ... */
518 v_wljdi_department_id       t_wljdi_department_id           := t_wljdi_department_id();
519 v_wljdi_department_code     t_wljdi_department_code         := t_wljdi_department_code();
520 v_wljdi_activity_id         t_wljdi_activity_id             := t_wljdi_activity_id();
521 v_wljdi_standard_rate_flag  t_wljdi_standard_rate_flag      := t_wljdi_standard_rate_flag();
522 v_wljdi_usage_rate_or_amt   t_wljdi_usage_rate_or_amt       := t_wljdi_usage_rate_or_amt();
523 
524 v_wljdi_basis_type          t_wljdi_basis_type          := t_wljdi_basis_type();
525 v_wljdi_uom_code            t_wljdi_uom_code            := t_wljdi_uom_code();
526 v_wljdi_scheduled_flag      t_wljdi_scheduled_flag      := t_wljdi_scheduled_flag();
527 
528 v_wljdi_res_autocharge_type     t_wljdi_res_autocharge_type := t_wljdi_res_autocharge_type();
529 v_wljdi_schedule_seq_num        t_wljdi_schedule_seq_num    := t_wljdi_schedule_seq_num();
530 
531 v_wljdi_attribute_category  t_wljdi_attribute_category  := t_wljdi_attribute_category();
532 v_wljdi_attribute1          t_wljdi_attribute1          := t_wljdi_attribute1();
533 v_wljdi_attribute2          t_wljdi_attribute2          := t_wljdi_attribute2();
534 v_wljdi_attribute3          t_wljdi_attribute3          := t_wljdi_attribute3();
535 v_wljdi_attribute4          t_wljdi_attribute4          := t_wljdi_attribute4();
536 v_wljdi_attribute5          t_wljdi_attribute5          := t_wljdi_attribute5();
537 v_wljdi_attribute6          t_wljdi_attribute6          := t_wljdi_attribute6();
538 v_wljdi_attribute7          t_wljdi_attribute7          := t_wljdi_attribute7();
539 v_wljdi_attribute8          t_wljdi_attribute8          := t_wljdi_attribute8();
540 v_wljdi_attribute9          t_wljdi_attribute9          := t_wljdi_attribute9();
541 v_wljdi_attribute10         t_wljdi_attribute10         := t_wljdi_attribute10();
542 v_wljdi_attribute11         t_wljdi_attribute11         := t_wljdi_attribute11();
543 v_wljdi_attribute12         t_wljdi_attribute12         := t_wljdi_attribute12();
544 v_wljdi_attribute13         t_wljdi_attribute13         := t_wljdi_attribute13();
545 v_wljdi_attribute14         t_wljdi_attribute14         := t_wljdi_attribute14();
546 v_wljdi_attribute15         t_wljdi_attribute15         := t_wljdi_attribute15();
547 
548 -- Added For LBJ Interface Alcoa Enhancement
549 v_wljdi_recommended         t_wljdi_recommended         := t_wljdi_recommended();
550 v_wljdi_std_op_code         t_wljdi_std_op_code         := t_wljdi_std_op_code();
551 v_wljdi_std_op_id           t_wljdi_std_op_id           := t_wljdi_std_op_id();
552 v_wljdi_op_yield_enable     t_wljdi_op_yield_enable     := t_wljdi_op_yield_enable();
553 v_wljdi_min_trf_qty         t_wljdi_min_trf_qty         := t_wljdi_min_trf_qty();
554 v_wljdi_backflush_flag      t_wljdi_backflush_flag      := t_wljdi_backflush_flag();
555 v_wljdi_count_point_type    t_wljdi_count_point_type    := t_wljdi_count_point_type();
556 v_wljdi_description         t_wljdi_description         := t_wljdi_description();
557 v_wljdi_inc_in_rollup       t_wljdi_inc_in_rollup       := t_wljdi_inc_in_rollup();
558 v_wljdi_option_dep_flag     t_wljdi_option_dep_flag     := t_wljdi_option_dep_flag();
559 v_wljdi_inventory_item_id_old   t_wljdi_inventory_item_id_old  := t_wljdi_inventory_item_id_old();
560 v_wljdi_inventory_item_old      t_wljdi_inventory_item_old     := t_wljdi_inventory_item_old();
561 v_wljdi_wip_supply_type         t_wljdi_wip_supply_type        := t_wljdi_wip_supply_type();
562 v_wljdi_quantity_per_assembly   t_wljdi_quantity_per_assembly  := t_wljdi_quantity_per_assembly();
563 v_wljdi_supply_subinventory     t_wljdi_supply_subinventory    := t_wljdi_supply_subinventory();
564 v_wljdi_supply_locator_id       t_wljdi_supply_locator_id      := t_wljdi_supply_locator_id();
565 v_wljdi_mrp_net_flag            t_wljdi_mrp_net_flag           := t_wljdi_mrp_net_flag();
566 v_wljdi_auto_request_material   t_wljdi_auto_request_material  := t_wljdi_auto_request_material();
567 v_wljdi_comments                t_wljdi_comments               := t_wljdi_comments();
568 v_wljdi_applied_resource_units  t_wljdi_applied_resource_units  := t_wljdi_applied_resource_units();
569 
570   /*Added for bugfix:7248992 */
571 v_wljdi_cum_yield               t_wljdi_cum_yield              := t_wljdi_cum_yield();
572 v_wljdi_rev_cum_yield           t_wljdi_rev_cum_yield          := t_wljdi_rev_cum_yield();
573 
574 type t_res_recom is table of varchar2(5) index by binary_integer;
575 type t_res_code is table of bom_resources.resource_code%type index by binary_integer;
576 
577 /* end */
578 /* ST : Detailed scheduling : end */
579 
580 type t_job_name                 is table of VARCHAR2(240) index by binary_integer;
581 
582 g_v_job_name                    t_job_name;
583 g_v_wip_entity_id               t_number;
584 
585 g_v_res_id                      t_number;
586 g_v_item_id                     t_number;
587 
588 
589 -- ==============================================================================================
590 -- instantiating the tables used to bulk bind data from wljdi to the PL/SQL tables.
591 -- ==============================================================================================
592 
593 -- BD: bug 3313463/3311695 8i compatibility
594 --type t_wljdi                    is table of wsm_lot_job_dtl_interface%rowtype index by binary_integer;
595 --v_wljdi                         t_wljdi;
596 -- ED: bug 3313463/3311695 8i compatibility
597 
598 -- ==============================================================================================
599 -- Some datatype to validate wljdi records
600 -- ==============================================================================================
601 
602 type rec_links IS record (
603         from_op_seq_num         number,
604         to_op_seq_num           number
605 );
606 type t_links                    is table of rec_links index by binary_integer;
607 
608 type rec_op IS record (
609         job_op_seq_num          number,
610         rtg_op_seq_num          number,
611         scheduled_quantity      number,
612         yield                   number,
613         start_date              date,
614         compltion_date          date,
615 	--Added for bug Fix 7248992
616 	cumulative_yield         number,
617         reverse_cumulative_yield number
618 );
619 type t_ops                      is table of rec_op index by binary_integer;
620 
621 /* ST : Detailed Schedduling : Record type for validating a resource to be added ... */
622 type new_res_rec IS record
623 (
624     job_op_seq_num                  NUMBER,
625     rtg_op_seq_num                  NUMBER,
626     resource_id                     NUMBER,
627     resource_code                   VARCHAR2(10),
628     dept_id                         NUMBER,
629     dept_code                       VARCHAR2(10), /* Modified from NUMBER for Bug 5469701 */
630     res_seq_num                     NUMBER,
631     sub_grp_num                     NUMBER,
632     rpl_grp_num                     NUMBER,
633     activity_id                     NUMBER,
634     standard_rate_flag              NUMBER,
635     assigned_units                  NUMBER,
636     usage_rate_or_amount            NUMBER,
637     usage_rate_or_amount_inverse    NUMBER,
638     uom_code                        VARCHAR2(3),
639     basis_type                      NUMBER,
640     scheduled_flag                  NUMBER,
641     autocharge_type                 NUMBER,
642     schedule_seq_num                NUMBER,
643     setup_id                        NUMBER,
644     setup_code                      VARCHAR2(10),
645     start_date                      DATE,
646     firm_type                       NUMBER,
647     grp_seq_id                      NUMBER,
648     grp_seq_num                     NUMBER,
649     max_assg_units                  NUMBER,
650     parent_seq_num                  NUMBER,
651     batch_id                        NUMBER,
652     completion_date                 DATE
653 );
654 
655 type new_res_tbl is table of new_res_rec index by binary_integer;
656 
657 type rec_op_res IS record (
658         job_op_seq_num          number,
659         rtg_op_seq_num          number,
660         resource_id             number,
661         res_seq_num             number,
662         sub_grp_num             number,
663         rpl_grp_num             number,
664         start_date              date,
665         /* ST : Detailed Scheduling start */
666         firm_type               number,
667         setup_id                number,
668         setup_code              VARCHAR2(10),
669         grp_seq_id              number,
670         grp_seq_num             number,
671         max_assg_units          number,
672         parent_seq_num          number,
673         batch_id                number,
674         assigned_units          number,
675         /* ST : Detailed Scheduling end */
676         completion_date         date,
677 		-- Added For LBJ Interface Alcoa Enhancement
678         applied_resource_units  number ,
679         uom_code                VARCHAR2(3),
680         autocharge_type         NUMBER,
681         usage_rate_or_amount    NUMBER,
682         scheduled_flag          NUMBER,
683         basis_type              NUMBER,
684         activity_id             NUMBER,
685         schedule_seq_num        NUMBER,
686         reco_flag               VARCHAR2(1),
687         standard_rate           NUMBER,
688         resource_code           VARCHAR2(10)
689 );
690 type t_op_res                   is table of rec_op_res index by binary_integer;
691 
692 type rec_res_ins IS record (
693         job_op_seq_num          number,
694         rtg_op_seq_num          number,
695         res_seq_num             number,
696         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
697         -- parent_res_seq_num   number,
698         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
699         instance_id             number,
700         serial_number           varchar2(30),
701         start_date              date,
702         completion_date         date,
703         batch_id                number
704 );
705 type t_res_ins                  is table of rec_res_ins index by binary_integer;
706 
707 type rec_res_use IS record (
708         job_op_seq_num          number,
709         rtg_op_seq_num          number,
710         res_seq_num             number,
711         instance_id             number,
712         serial_number           varchar2(30),
713         assigned_units          number,
714         start_date              date,
715         /* ST : Detailed scheduling : start */
716         parent_res_seq_num      number,
717         resource_hours          number,
718         /* ST : Detailed scheduling : end */
719         completion_date         date
720 );
721 type t_res_use                  is table of rec_res_use index by binary_integer;
722 
723 type rec_op_comp IS record (
724         job_op_seq_num          number,
725         rtg_op_seq_num          number,
726         item_id                 number,
727         primary_item_id         number,
728         src_phm_item_id         number,
729         component_seq_id        number,
730         date_required           date,
731         component_yield         number,
732         required_qty            number,
733         qpa                     number, -- ADD bug 3347985
734 	-- Added For LBJ Interface Alcoa Enhancement
735         item_old_id         number,
736         wip_supply_type     number,
737         supply_subinventory varchar2(10),
738         supply_locator      number,
739         mrp_net_flag        number
740 );
741 type t_op_comp                  is table of rec_op_comp index by binary_integer;
742 
743 
744 
745 -- **********************************************************************************************
746 -- handle_invalid_field
747 -- **********************************************************************************************
748 -- this is called to display message of WSM_INVALID_FIELD type.
749 -- sets the process_status to 3, sets values of error_code and error_message,
750 -- writes into fnd log and calls write_to_wie
751 
752 PROCEDURE handle_invalid_field (
753         p_field             in varchar2,
754         p_aux_string        in varchar2,
755         stmt_number         in number) is
756 
757 l_err_msg   varchar2(2000) := null;
758 
759 BEGIN
760     v_wlji_process_status(v_idx) := 3;
761     v_wlji_err_code(v_idx) := -1;
762     fnd_message.set_name('WSM','WSM_INVALID_FIELD');
763     fnd_message.set_token('FLD_NAME', p_field);
764     l_err_msg := fnd_message.get;
765     v_wlji_err_msg(v_idx) := l_err_msg;
766     fnd_file.put_line(fnd_file.log,
767         'Error: (stmt_num: '|| stmt_number ||') '||l_err_msg||' '||p_aux_string);
768     fnd_file.new_line(fnd_file.log, 3);
769 END handle_invalid_field;
770 
771 
772 
773 -- **********************************************************************************************
774 -- handle_errmsg_name
775 -- **********************************************************************************************
776 -- this is called to display message of any type which does not involve setting any token
777 -- sets the process_status to 3, sets values of error_code and error_message,
778 -- writes into fnd log and calls write_to_wie
779 
780 PROCEDURE handle_errmsg_name(
781         p_product           in varchar2,
782         p_message_name      in varchar2,
783         stmt_number         in number) is
784 
785 l_err_msg   varchar2(2000) := null;
786 
787 BEGIN
788     v_wlji_process_status(v_idx) := 3; --ERROR
789     v_wlji_err_code(v_idx) := -1;
790     fnd_message.set_name(p_product, p_message_name);
791     l_err_msg := fnd_message.get;
792     v_wlji_err_msg(v_idx) := l_err_msg;
793     fnd_file.put_line(fnd_file.log,
794         'Error: (stmt_num: '|| stmt_number||') '||l_err_msg);
795     fnd_file.new_line(fnd_file.log, 3);
796 END handle_errmsg_name;
797 
798 
799 
800 -- **********************************************************************************************
801 -- handle_error
802 -- **********************************************************************************************
803 PROCEDURE handle_error (
804         p_err_code          in number,
805         p_err_msg           in varchar2,
806         stmt_number         in number) is
807 BEGIN
808     v_wlji_process_status(v_idx) := 3; -- ERROR
809     v_wlji_err_code(v_idx) := p_err_code;
810     v_wlji_err_msg(v_idx)  := substr(p_err_msg,1,2000);
811     fnd_file.put_line(fnd_file.log,
812         'Error: (stmt_num: ' || stmt_number||') '||p_err_msg);
813     fnd_file.new_line(fnd_file.log, 3);
814 END handle_error;
815 
816 
817 -- **********************************************************************************************
818 -- HANDLE_WARNING
819 -- **********************************************************************************************
820 PROCEDURE handle_warning (
821         p_err_msg                   in varchar2,
822         p_header_id                 in number,
823         p_request_id                in number,
824         p_program_id                in number,
825         p_program_application_id    in number) is
826 
827 dummy_err_code  number;
828 dummy_err_msg   varchar2(2000);
829 
830 BEGIN
831     fnd_file.put_line(fnd_file.log, 'Warning: ' || p_err_msg);
832     wsmputil.WRITE_TO_WIE (
833             p_header_id,
834             substr(p_err_msg,1,2000),
835             p_request_id,
836             p_program_id,
837             p_program_application_id,
838             2,
839             dummy_err_code,
840             dummy_err_msg );
841 END handle_warning;
842 
843 
844 
845 -- **********************************************************************************************
846 -- FIND_COPY_ROUTING_START: local procedure
847 -- **********************************************************************************************
848 procedure find_copy_routing_start(
849         p_wip_entity_id         number,
850         x_start_op_seq_num      out nocopy number,
851         x_start_op_seq_id       out nocopy number,
852         x_err_code              out nocopy number,
853         x_err_msg               out nocopy varchar2) is
854 
855 l_char_temp varchar2(1) := 'S';
856 
857 begin
858     x_err_code := 0;
859     x_err_msg  := null;
860 
861     select wco.operation_seq_num,
862            wco.operation_sequence_id
863     into   x_start_op_seq_num,
864            x_start_op_seq_id
865     from   wsm_copy_operations wco
866     where  wco.wip_entity_id = p_wip_entity_id
867     and    NVL(wco.network_start_end, 'X') = l_char_temp; -- donot use 'S'
868 
869 exception
870     when no_data_found then
871         x_err_code := -2;
872         fnd_message.set_name('wsm','wsm_net_start_not_found');
873         x_err_msg:= fnd_message.get;
874         return;
875 
876     when others then
877         x_err_code := sqlcode;
878         x_err_msg  := 'find_copy_routing_start '|| substr(sqlerrm,1,60);
879         return;
880 
881 end find_copy_routing_start;
882 
883 
884 -- **********************************************************************************************
885 -- FIND_COPY_ROUTING_END: local procedure
886 -- **********************************************************************************************
887 procedure find_copy_routing_end(
888         p_wip_entity_id         number,
889         x_end_op_seq_num        out nocopy number,
890         x_end_op_seq_id         out nocopy number,
891         x_err_code              out nocopy number,
892         x_err_msg               out nocopy varchar2) is
893 
894  l_char_temp varchar2(1) := 'E';
895 
896 begin
897     x_err_code := 0;
898     x_err_msg  := null;
899 
900     select wco.operation_seq_num,
901            wco.operation_sequence_id
902     into   x_end_op_seq_num,
903            x_end_op_seq_id
904     from   wsm_copy_operations wco
905     where  wco.wip_entity_id = p_wip_entity_id
906     and    NVL(wco.network_start_end, 'X') = l_char_temp; -- donot use 'E'
907 
908 exception
909     when no_data_found then
910         x_err_code := -2;
911         fnd_message.set_name('wsm','wsm_net_end_not_found');
912         x_err_msg:= fnd_message.get;
913         return;
914 
915     when others then
916         x_err_code := sqlcode;
917         x_err_msg  := 'find_copy_routing_end '|| substr(sqlerrm,1,60);
918         return;
919 
920 end find_copy_routing_end;
921 
922 
923 -- **********************************************************************************************
924 -- FIND_COPY_OP_SEQ_ID: local procedure
925 -- **********************************************************************************************
926 procedure find_copy_op_seq_id  (
927         p_wip_entity_id                    number,
928         p_rtg_op_seq_num                in number,
929         x_op_seq_id             out nocopy number,
930         x_err_code              out nocopy number,
931         x_err_msg               out nocopy varchar2 ) is
932 begin
933     x_err_code := 0;
934 
935     select  nvl(operation_sequence_id, -1)
936     into    x_op_seq_id
937     from    wsm_copy_operations
938     where   wip_entity_id = p_wip_entity_id
939     and     operation_seq_num = p_rtg_op_seq_num;
940 exception
941     when others then
942         x_err_code := sqlcode;
943         x_err_msg  := 'find_copy_op_seq_id '|| substr(sqlerrm,1,60);
944 end find_copy_op_seq_id;
945 
946 
947 -- **********************************************************************************************
948 -- VALIDATE_ORG_ID_NAME: local validation procedure
949 -- **********************************************************************************************
950 
951 -- Org_id and org_code cannot be NULL together. Org_id is the driven information,
952 -- Only when org_id is not given will org_code be used to get org_id
953 
954 PROCEDURE validate_org_id_name(
955         p_org_id            in out nocopy number,
956         p_org_code          in out nocopy varchar2,
957         x_error_code        out nocopy number,
958         x_error_msg         out nocopy varchar2) is
959 
960 l_xst       boolean;
961 l_stmt_num  number;
962 
963 BEGIN
964 
965     x_error_code := 0;
966     x_error_msg := '';
967 l_stmt_num  := 10;
968     if p_org_id IS NULL then
969         if p_org_code IS NULL then
970             fnd_message.set_name('WSM','WSM_NULL_FIELD');
971             fnd_message.set_token('FLD_NAME', 'Organization ID and Code');
972             x_error_msg := fnd_message.get;
973             x_error_code := -1;
974             return;
975         end if;
976 
977         begin
978 l_stmt_num  := 20;
979 
980 	-- changed from org_organization_definitions to MTL_PARAMETERS for bug 5051885
981 	-- Performance issue on org_organization_definitions for full table scan on base tables
982 	-- SQL id 16640485
983 
984             select organization_id
985             into   p_org_id
986             from   MTL_PARAMETERS
987             where  organization_code = p_org_code;
988     -- bug 5051885
989         exception
990             when others then
991                 fnd_message.set_name('WSM','WSM_INVALID_FIELD');
992                 fnd_message.set_token('FLD_NAME', 'Organization Code');
993                 x_error_msg := fnd_message.get;
994                 x_error_code := -1;
995                 return;
996         end;
997     else
998         p_org_code := null; -- ignore org code if id is given
999     end if;
1000 
1001 l_stmt_num  := 30;
1002     -- now p_org_id is always available
1003     l_xst := true;
1004     l_xst := WSMPLCVA.v_org.exists(p_org_id);
1005     if l_xst = false then
1006         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1007         fnd_message.set_token('FLD_NAME', 'Organization ID');
1008         x_error_msg := fnd_message.get;
1009         x_error_code := -1;
1010         return;
1011     end if;
1012 EXCEPTION
1013     when others then
1014         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1015         fnd_message.set_token('FLD_NAME', 'Organization ID');
1016         x_error_msg := fnd_message.get;
1017         x_error_msg := 'validate_org_id_name (#' || l_stmt_num || ') ' || x_error_msg ;
1018         x_error_code := -1;
1019 END validate_org_id_name;
1020 
1021 
1022 -- **********************************************************************************************
1023 -- VALIDATE_WIP_ENTITY_ID_NAME: local validation procedure
1024 -- **********************************************************************************************
1025 
1026 -- For job creation, wip_entity_id will be ignored, if job_name is NULL, will default
1027 -- Will validate whether the job_name is used or not in the organization
1028 --
1029 -- For job update, wip_entity_id and job_name cannot be NULL together.
1030 -- Wip_entity_id is the driven information, only when wip_entity_id is NULL will
1031 -- job_name be used to get wip_entity_id
1032 -- Will validate the status of the job
1033 
1034 PROCEDURE validate_wip_entity_id_name(
1035         p_load_type         in number,  -- 5 job creation, 6 job update
1036         p_org_id            in number,
1037         p_wip_entity_id     in out nocopy number,
1038         p_job_name          in out nocopy varchar2,
1039         x_error_code        out nocopy number,
1040         x_error_msg         out nocopy varchar2) is
1041 
1042 l_temp_num          number;
1043 l_xst               boolean;
1044 l_stmt_num          number;
1045 l_job_name_hash     number;
1046 l_old_job_name      wsm_lot_job_interface.job_name%type; --fix for bug5990761
1047 BEGIN
1048 
1049     x_error_code := 0;
1050     x_error_msg := '';
1051 
1052 l_stmt_num := 10;
1053     -- Start : Fix for OPMConv project --
1054 --    if LENGTH(p_job_name) > 30 then
1055 --        fnd_message.set_name('WSM','WSM_JOB_NAME_THIRTY_CHAR');
1056 
1057     if LENGTH(p_job_name) > 80 then
1058         fnd_message.set_name('WSM','WSM_EXCEED_MAX_LEN');
1059         fnd_message.set_token('FLD_NAME', 'Job Name');
1060         fnd_message.set_token('VALUE', '80');
1061         x_error_msg := fnd_message.get;
1062         x_error_code := -1;
1063         return;
1064     end if;
1065     -- End : Fix for OPMConv project --
1066 
1067     if p_load_type = 5 then   -- Job creation
1068         if p_job_name is null then
1069 l_stmt_num := 20;
1070             -- Derive Job_Name.
1071             select FND_Profile.value('WIP_JOB_PREFIX') || wip_job_number_s.nextval
1072             into   p_job_name
1073             from   dual;
1074         end if;
1075 
1076         -- Be sure the provided Job_Name is not already in use.
1077         l_temp_num := 0;
1078         begin
1079 l_stmt_num := 30;
1080             select 1
1081             into   l_temp_num
1082             from   wip_entities
1083             where  wip_entity_name = p_job_name
1084             and    organization_id = p_org_id;
1085             if l_temp_num = 1 then
1086                 fnd_message.set_name('WIP','WIP_ML_JOB_NAME');
1087                 x_error_msg := fnd_message.get;
1088                 x_error_code := -1;
1089                 return;
1090             end if;
1091         exception
1092             when no_data_found then
1093                 null;
1094             when others then
1095                 fnd_message.set_name('WIP','WIP_ML_JOB_NAME');
1096                 x_error_msg := fnd_message.get;
1097                 x_error_code := -1;
1098                 return;
1099         end;
1100         p_wip_entity_id := null; -- Ignore wip_entity_id
1101 
1102     elsif p_load_type = 6 then    -- Job update
1103 
1104         l_xst := false;
1105         if p_wip_entity_id is null then
1106             if p_job_name is null then
1107                 fnd_message.set_name('WSM','WSM_NULL_FIELD');
1108                 fnd_message.set_token('FLD_NAME', 'Wip_Entity_Id and Job Name');
1109                 x_error_msg := fnd_message.get;
1110                 x_error_code := -1;
1111                 return;
1112             end if;
1113 
1114             l_job_name_hash := dbms_utility.get_hash_value(
1115                                     p_job_name || p_org_id,
1116                                     37, 1073741824);
1117             l_xst := g_v_job_name.exists(l_job_name_hash);
1118             if( NOT l_xst ) then
1119                 begin
1120 l_stmt_num := 40;
1121                     -- Get wip_entity_id by wip_entity_name
1122                     select wip_entity_id
1123                     into   p_wip_entity_id
1124                     from   wip_entities
1125                     where  wip_entity_name = p_job_name
1126                     and    organization_id = p_org_id;
1127                 exception
1128                     when others then
1129                         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1130                         fnd_message.set_token('FLD_NAME', 'Job_Name');
1131                         x_error_msg := fnd_message.get;
1132                         x_error_code := -1;
1133                         return;
1134                 end;
1135             else
1136                 p_wip_entity_id := g_v_wip_entity_id(l_job_name_hash);
1137             end if;
1138         end if;
1139 
1140         if( NOT l_xst ) then
1141             -- Now wip_entity_id is available, check job status
1142             begin
1143 l_stmt_num := 50;
1144 
1145                   --fix for bug5990761  allow update of job name for unreleased jobs
1146                 select decode(wdj.status_type, WIP_CONSTANTS.UNRELEASED,NVL(p_job_name,we.wip_entity_name),we.wip_entity_name),we.wip_entity_name
1147                 into   p_job_name,l_old_job_name
1148                 from   wip_discrete_jobs wdj,
1149                        wip_entities we
1150                 where  wdj.wip_entity_id = p_wip_entity_id
1151                 and    wdj.status_type in (
1152                         WIP_CONSTANTS.UNRELEASED,
1153                         WIP_CONSTANTS.RELEASED,
1154                         WIP_CONSTANTS.HOLD,
1155                         WIP_CONSTANTS.CANCELLED)
1156                 and    wdj.wip_entity_id = we.wip_entity_id
1157                 and    we.organization_id = p_org_id;
1158             exception
1159                 when others then
1160                     fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1161                     fnd_message.set_token('FLD_NAME', 'Wip_Entity_Id');
1162                     x_error_msg := fnd_message.get;
1163                     x_error_code := -1;
1164                     return;
1165             end;
1166 
1167 	-- fix for bug5990761 if wip entity id and job name are populated in WLJI, raise error
1168        -- if job name in WLJI conflicts with wip entity name of some other job in the same org.
1169           if (l_old_job_name <> p_job_name) then
1170           l_temp_num := 0;
1171             begin
1172                 select count(wip_entity_id)
1173                   into l_temp_num
1174                   from wip_entities
1175                  where wip_entity_name = p_job_name
1176                    and organization_id = p_org_id
1177                    and wip_entity_id <> p_wip_entity_id;
1178 
1179                 if (l_temp_num = 1) then
1180                    fnd_message.set_name('WIP','WIP_ML_JOB_NAME');
1181                    x_error_msg := fnd_message.get;
1182                    x_error_code := -1;
1183                    return;
1184                 end if;
1185             exception
1186                 when others then
1187                      fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1188                      fnd_message.set_token('FLD_NAME', 'Job_Name');
1189                      x_error_msg := fnd_message.get;
1190                      x_error_code := -1;
1191                      return;
1192             end;
1193           end if;
1194        -- end of additional validation added for 5990761
1195 
1196 
1197         end if;
1198 
1199         -- BA: 3512105
1200         if(WSMPUTIL.get_internal_copy_type(p_wip_entity_id) = 3) then
1201             fnd_message.set_name('WSM','WSM_NO_VALID_COPY');
1202             x_error_msg := fnd_message.get;
1203             x_error_code := -1;
1204             return;
1205         end if;
1206         -- BA: 3512105
1207 
1208     end if; -- Job update
1209 
1210 
1211 EXCEPTION
1212     when others then
1213         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1214         fnd_message.set_token('FLD_NAME', 'Wip_Entity_ID/Job_Name');
1215         x_error_msg := fnd_message.get;
1216         x_error_msg := 'validate_wip_entity_id_name (#' || l_stmt_num || ') ' || x_error_msg;
1217         x_error_code := -1;
1218 END validate_wip_entity_id_name;
1219 
1220 
1221 -- **********************************************************************************************
1222 -- DEFAULT_WLJDI_FIELDS: local procedure
1223 -- **********************************************************************************************
1224 
1225 procedure default_wljdi_fields(
1226         p_wip_entity_id     in number,
1227         p_org_id            in number,
1228         p_parent_header_id  in number,      -- header_id in WLJI, pass null if N/A
1229         p_group_id          in number,
1230         p_wo_records_exist  in varchar2,
1231         p_error_code        out nocopy number,
1232         p_error_msg         out nocopy varchar2) is
1233 
1234 cursor c_wljdi is
1235     select  record_id,
1236             -- ST Fix for bug 4285032 :
1237         -- APS will be populating 8 for resource instance information
1238         -- and 9 resource instance usage information..
1239         -- Map it to WSM load type 7 --> resource instance
1240         --                         4 --> resource instance usage..
1241             decode(load_type,8,7,
1242                          9,4,
1243                  load_type) load_type,
1244             substitution_type,
1245             job_op_seq_num,
1246             routing_op_seq_num,
1247             next_routing_op_seq_num,
1248             inventory_item_id_new,
1249             inventory_item_new,
1250             primary_item,
1251             primary_item_id,
1252             src_phantom_item,
1253             src_phantom_item_id,
1254             component_sequence_id,
1255             date_required,
1256             scheduled_quantity,
1257             required_quantity,
1258             batch_id,
1259             assigned_units,
1260             instance_id_new,
1261             operation_completion_date,
1262             operation_start_date,
1263             operation_yield,
1264             resource_code_new,
1265             resource_id_new,
1266             resource_seq_num,
1267             substitute_group_num,
1268             replacement_group_num,
1269             serial_number_new,
1270             start_date,
1271             completion_date,
1272             -- ST :  Detailed scheduling : start
1273             firm_type,
1274             setup_id,
1275             setup_code,
1276             group_sequence_id,
1277             group_sequence_num,
1278             max_assigned_units,
1279             parent_resource_seq_num,
1280             resource_hours,
1281             -- ST : Detailed Scheduling : Added for resource adding changes
1282             department_id          ,
1283             department_code ,
1284             -- ST : Detailed Scheduling : Added for resource adding changes : end
1285             -- Added For LBJ Interface Alcoa Enhancement
1286             recommended,
1287             standard_operation_code,
1288             standard_operation_id,
1289             operation_yield_enabled,
1290             minimum_transfer_quantity,
1291             backflush_flag,
1292             count_point_type,
1293             description,
1294             include_in_rollup,
1295             option_dependent_flag,
1296             inventory_item_id_old,
1297             inventory_item_old,
1298             wip_supply_type,
1299             cumulative_yield,/*Added for bugfix:7248992 */
1300             reverse_cumulative_yield/*Added for bugfix:7248992 */
1301     from    wsm_lot_job_dtl_interface
1302     where   parent_header_id = p_parent_header_id
1303     and     process_status = WIP_CONSTANTS.RUNNING;
1304 
1305 cursor c_wljdi_indp is
1306     select  record_id,
1307             -- ST Fix for bug 4285032 :
1308             -- APS will be populating 8 for resource instance information
1309             -- and 9 resource instance usage information..
1310             -- Map it to WSM load type 7 --> resource instance
1311             --                         4 --> resource instance usage..
1312             decode(load_type,8,7,
1313                              9,4,
1314                              load_type) load_type,
1315             substitution_type,
1316             job_op_seq_num,
1317             routing_op_seq_num,
1318             next_routing_op_seq_num,
1319             inventory_item_id_new,
1320             inventory_item_new,
1321             primary_item,
1322             primary_item_id,
1323             src_phantom_item,
1324             src_phantom_item_id,
1325             component_sequence_id,
1326             date_required,
1327             scheduled_quantity,
1328             required_quantity,
1329             batch_id,
1330             assigned_units,
1331             instance_id_new,
1332             operation_completion_date,
1333             operation_start_date,
1334             operation_yield,
1335             resource_code_new,
1336             resource_id_new,
1337             resource_seq_num,
1338             substitute_group_num,
1339             replacement_group_num,
1340             serial_number_new,
1341             start_date,
1342             completion_date,
1343             /* ST :  Detailed scheduling : start */
1344             firm_type,
1345             setup_id,
1346             setup_code,
1347             group_sequence_id,
1348             group_sequence_num,
1349             max_assigned_units,
1350             parent_resource_seq_num,
1351             resource_hours,
1352             /* ST :  Detailed scheduling : END */
1353             /* ST : Detailed Scheduling : Added for resource adding changes */
1354             department_id          ,
1355             department_code ,
1356             /* ST : Detailed Scheduling : Added for resource adding changes : end */
1357             -- Added For LBJ Interface Alcoa Enhancement
1358             recommended,
1359             standard_operation_code,
1360             standard_operation_id,
1361             operation_yield_enabled,
1362             minimum_transfer_quantity,
1363             backflush_flag,
1364             count_point_type,
1365             description,
1366             include_in_rollup,
1367             option_dependent_flag,
1368             inventory_item_id_old,
1369             inventory_item_old,
1370             wip_supply_type,
1371             cumulative_yield,/*Added for bugfix:7248992 */
1372             reverse_cumulative_yield/*Added for bugfix:7248992 */
1373     from    wsm_lot_job_dtl_interface
1374     where   parent_header_id IS NULL
1375     and     wip_entity_id     = p_wip_entity_id
1376     and     organization_id   = p_org_id
1377     and     process_status    = WIP_CONSTANTS.RUNNING
1378     and     NVL(group_id, -1) = NVL(p_group_id, -1);
1379 
1380 l_stmt_num                  number;
1381 l_d                         number;
1382 l_out                       number;
1383 l_hash_value                number;
1384 
1385 l_v_sub_grp_num             t_number;
1386 l_v_rpl_grp_num             t_number;
1387 l_v_res_seq_num             t_number;
1388 l_v_ins_id                  t_number;
1389 l_v_ser_num                 t_serial;
1390 l_v_item_id                 t_number;
1391 l_v_prm_item_id             t_number;
1392 l_v_phm_item_id             t_number;
1393 l_v_res_id                  t_number;
1394 l_v_record_id               t_number;
1395 
1396 /* ST : Detailed Scheduling start */
1397 l_v_dept_id             t_number;
1398 l_v_res_code            t_res_code;
1399 
1400 l_v_setup_id               t_number;  -- Added for bug fix 4704289
1401 
1402 l_index             number;
1403 /* ST : Detailed Scheduling end */
1404 
1405 l_load_type                 number;
1406 l_sub_type                  number;
1407 l_rtg_op_seq_num            number;
1408 l_job_op_seq_num            number;
1409 l_resource_id               number;
1410 l_sub_grp_num               number;
1411 l_rpl_grp_num               number;
1412 l_res_seq_num               number;
1413 
1414 /* ST : Detailed Scheduling : start */
1415 l_dept_id               number;
1416 l_par_res_seq_num       number;
1417 /* ST : Detailed Scheduling : end */
1418 
1419 l_setup_id				number;  -- Added for bug fix 4704289
1420 
1421 
1422 e_invalid_sub_load_type     exception;
1423 e_invalid_op_seq_num        exception;
1424 e_wljdi_common_error        exception;
1425 e_invalid_item_name         exception;
1426 e_invalid_resource_code     exception;
1427 e_no_res_seq_num            exception;
1428 
1429 /* ST : Detailed Scheduling : start */
1430 e_invalid_dept_code        exception;
1431 e_invalid_resource_id      exception;
1432 /* ST : Detailed Scheduling : end */
1433 
1434 e_invalid_setup_code	   exception;	 -- Added for bug fix 4704289
1435 
1436 -- Added For LBJ Interface Alcoa Enhancement
1437 l_v_op_start_date          t_date;
1438 l_v_op_completion_date     t_date;
1439 l_v_min_trf_qty            t_number;
1440 l_v_count_point_type       t_number;
1441 l_v_description            t_desc;
1442 l_v_backflush_flag         t_number;
1443 l_v_op_yield_enable        t_number;
1444 l_v_option_dep_flag        t_number;
1445 l_v_inc_in_rollup          t_number;
1446 l_v_std_op_id              t_number;
1447 l_v_std_op_code            t_op_code;
1448 l_v_recommended            t_recoflag;
1449 l_v_item_old_id             t_number;
1450 l_res_num                  number;
1451 l_res_exist                number;
1452 
1453 l_temp                     number;
1454 l_job_start_date           date;
1455 l_job_compl_date           date;
1456 l_xst                      number;
1457 
1458 e_inv_std_op               exception;
1459 e_invalid_min_trf_qty      exception;
1460 e_invalid_count_point      exception;
1461 e_invalid_backflush_flag   exception;
1462 e_dup_op_seq               exception;
1463 e_invalid_op_date          exception;
1464 --Alcoa Enhancement
1465 e_invalid_res_seq_num      exception;
1466 e_res_seq_num_exist        exception;
1467 e_invalid_routing_op_seq   exception;
1468 e_invalid_job_op_seq       exception;
1469 
1470 BEGIN
1471     ------------------------------------
1472     -- empty v_wljdi, load wljdi records
1473     ------------------------------------
1474     -- BA: bug 3313463/3311695 8i compatibility
1475     v_wljdi_record_id               := t_wljdi_record_id();
1476     v_wljdi_load_type               := t_wljdi_load_type();
1477     v_wljdi_substitution_type       := t_wljdi_substitution_type();
1478     v_wljdi_job_op_seq_num          := t_wljdi_job_op_seq_num();
1479     v_wljdi_routing_op_seq_num      := t_wljdi_routing_op_seq_num();
1480     v_wljdi_next_rtg_op_seq_num     := t_wljdi_next_rtg_op_seq_num();
1481 
1482     v_wljdi_inventory_item_id_new   := t_wljdi_inventory_item_id_new();
1483     v_wljdi_inventory_item_new      := t_wljdi_inventory_item_new();
1484     v_wljdi_primary_item            := t_wljdi_primary_item();
1485     v_wljdi_primary_item_id         := t_wljdi_primary_item_id();
1486     v_wljdi_src_phantom_item        := t_wljdi_src_phantom_item();
1487     v_wljdi_src_phantom_item_id     := t_wljdi_src_phantom_item_id();
1488     v_wljdi_component_sequence_id   := t_wljdi_component_sequence_id();
1489     v_wljdi_date_required           := t_wljdi_date_required();
1490     v_wljdi_scheduled_quantity      := t_wljdi_scheduled_quantity();
1491     v_wljdi_required_quantity       := t_wljdi_required_quantity();
1492     v_wljdi_batch_id                := t_wljdi_batch_id();
1493     v_wljdi_assigned_units          := t_wljdi_assigned_units();
1494     v_wljdi_instance_id_new         := t_wljdi_instance_id_new();
1495     v_wljdi_op_completion_date      := t_wljdi_op_completion_date();
1496     v_wljdi_op_start_date           := t_wljdi_op_start_date();
1497     v_wljdi_op_yield                := t_wljdi_op_yield();
1498     v_wljdi_resource_code_new       := t_wljdi_resource_code_new();
1499     v_wljdi_resource_id_new         := t_wljdi_resource_id_new();
1500     v_wljdi_resource_seq_num        := t_wljdi_resource_seq_num();
1501     v_wljdi_substitute_group_num    := t_wljdi_substitute_group_num();
1502     v_wljdi_replacement_group_num   := t_wljdi_replacement_group_num();
1503     v_wljdi_serial_number_new       := t_wljdi_serial_number_new();
1504     v_wljdi_start_date              := t_wljdi_start_date();
1505     v_wljdi_completion_date         := t_wljdi_completion_date();
1506 
1507     /* ST :  Detailed scheduling : start */
1508     v_wljdi_resource_hours          := t_wljdi_resource_hours();
1509     v_wljdi_firm_type               := t_wljdi_firm_type();
1510     v_wljdi_setup_id                := t_wljdi_setup_id();
1511     v_wljdi_setup_code              := t_wljdi_setup_code();
1512     v_wljdi_grp_seq_id              := t_wljdi_grp_seq_id();
1513     v_wljdi_grp_seq_num             := t_wljdi_grp_seq_num();
1514     v_wljdi_max_assg_units          := t_wljdi_max_assg_units();
1515     v_wljdi_parent_seq_num          := t_wljdi_parent_seq_num();
1516 
1517     /* Batch_id is already taken care */
1518 
1519     /* ST : Detailed Scheduling : Considered for adding a setup resource.... */
1520     v_wljdi_department_id           := t_wljdi_department_id();
1521     v_wljdi_department_code         := t_wljdi_department_code();
1522     /* ST : Detailed scheduling : end */
1523 
1524     -- Added For LBJ Interface Alcoa Enhancement
1525     v_wljdi_recommended             := t_wljdi_recommended();
1526     v_wljdi_std_op_code             := t_wljdi_std_op_code();
1527     v_wljdi_std_op_id               := t_wljdi_std_op_id();
1528     v_wljdi_op_yield_enable         := t_wljdi_op_yield_enable();
1529     v_wljdi_min_trf_qty             := t_wljdi_min_trf_qty();
1530     v_wljdi_backflush_flag          := t_wljdi_backflush_flag();
1531     v_wljdi_count_point_type        := t_wljdi_count_point_type();
1532     v_wljdi_description             := t_wljdi_description();
1533     v_wljdi_inc_in_rollup           := t_wljdi_inc_in_rollup();
1534     v_wljdi_option_dep_flag         := t_wljdi_option_dep_flag();
1535     v_wljdi_inventory_item_id_old   := t_wljdi_inventory_item_id_old();
1536     v_wljdi_inventory_item_old      := t_wljdi_inventory_item_old();
1537     v_wljdi_wip_supply_type         := t_wljdi_wip_supply_type();
1538 
1539 ---Added for BugFix 7248992
1540     v_wljdi_cum_yield               := t_wljdi_cum_yield();
1541     v_wljdi_rev_cum_yield           := t_wljdi_rev_cum_yield();
1542 
1543     -- EA: bug 3313463/3311695 8i compatibility
1544 
1545     if(p_parent_header_id IS NULL) then
1546         l_stmt_num := 10.1;
1547         --BC: bug 3313463/3311695
1548         open c_wljdi_indp;
1549         fetch c_wljdi_indp bulk collect into
1550             v_wljdi_record_id,
1551             v_wljdi_load_type,
1552             v_wljdi_substitution_type,
1553             v_wljdi_job_op_seq_num,
1554             v_wljdi_routing_op_seq_num,
1555             v_wljdi_next_rtg_op_seq_num,
1556             v_wljdi_inventory_item_id_new,
1557             v_wljdi_inventory_item_new,
1558             v_wljdi_primary_item,
1559             v_wljdi_primary_item_id,
1560             v_wljdi_src_phantom_item,
1561             v_wljdi_src_phantom_item_id,
1562             v_wljdi_component_sequence_id,
1563             v_wljdi_date_required,
1564             v_wljdi_scheduled_quantity,
1565             v_wljdi_required_quantity,
1566             v_wljdi_batch_id,
1567             v_wljdi_assigned_units,
1568             v_wljdi_instance_id_new,
1569             v_wljdi_op_completion_date,
1570             v_wljdi_op_start_date,
1571             v_wljdi_op_yield,
1572             v_wljdi_resource_code_new,
1573             v_wljdi_resource_id_new,
1574             v_wljdi_resource_seq_num,
1575             v_wljdi_substitute_group_num,
1576             v_wljdi_replacement_group_num,
1577             v_wljdi_serial_number_new,
1578             v_wljdi_start_date,
1579             v_wljdi_completion_date,
1580             /* ST :  Detailed scheduling : start */
1581             v_wljdi_firm_type   ,
1582             v_wljdi_setup_id    ,
1583             v_wljdi_setup_code  ,
1584             v_wljdi_grp_seq_id  ,
1585             v_wljdi_grp_seq_num ,
1586             v_wljdi_max_assg_units,
1587             v_wljdi_parent_seq_num,
1588             v_wljdi_resource_hours,
1589             /* ST :  Detailed scheduling : END */
1590             /* ST : Detailed Scheduling : Considered for adding a resource */
1591             v_wljdi_department_id      ,
1592             v_wljdi_department_code,
1593             /* ST : Detailed Scheduling :  end adding a resource changes */
1594             -- Added For LBJ Interface Alcoa Enhancement
1595             v_wljdi_recommended,
1596             v_wljdi_std_op_code,
1597             v_wljdi_std_op_id,
1598             v_wljdi_op_yield_enable,
1599             v_wljdi_min_trf_qty,
1600             v_wljdi_backflush_flag,
1601             v_wljdi_count_point_type,
1602             v_wljdi_description,
1603             v_wljdi_inc_in_rollup,
1604             v_wljdi_option_dep_flag,
1605             v_wljdi_inventory_item_id_old,
1606             v_wljdi_inventory_item_old,
1607             v_wljdi_wip_supply_type,
1608 	    v_wljdi_cum_yield,
1609 	    v_wljdi_rev_cum_yield
1610         ;
1611         close c_wljdi_indp;
1612         --EC: bug 3313463/3311695
1613     else
1614 l_stmt_num := 10.2;
1615         --BC: bug 3313463/3311695
1616         open c_wljdi;
1617         fetch c_wljdi bulk collect into
1618             v_wljdi_record_id,
1619             v_wljdi_load_type,
1620             v_wljdi_substitution_type,
1621             v_wljdi_job_op_seq_num,
1622             v_wljdi_routing_op_seq_num,
1623             v_wljdi_next_rtg_op_seq_num,
1624             v_wljdi_inventory_item_id_new,
1625             v_wljdi_inventory_item_new,
1626             v_wljdi_primary_item,
1627             v_wljdi_primary_item_id,
1628             v_wljdi_src_phantom_item,
1629             v_wljdi_src_phantom_item_id,
1630             v_wljdi_component_sequence_id,
1631             v_wljdi_date_required,
1632             v_wljdi_scheduled_quantity,
1633             v_wljdi_required_quantity,
1634             v_wljdi_batch_id,
1635             v_wljdi_assigned_units,
1636             v_wljdi_instance_id_new,
1637             v_wljdi_op_completion_date,
1638             v_wljdi_op_start_date,
1639             v_wljdi_op_yield,
1640             v_wljdi_resource_code_new,
1641             v_wljdi_resource_id_new,
1642             v_wljdi_resource_seq_num,
1643             v_wljdi_substitute_group_num,
1644             v_wljdi_replacement_group_num,
1645             v_wljdi_serial_number_new,
1646             v_wljdi_start_date,
1647             v_wljdi_completion_date,
1648             /* ST :  Detailed scheduling : start */
1649             v_wljdi_firm_type   ,
1650             v_wljdi_setup_id    ,
1651             v_wljdi_setup_code  ,
1652             v_wljdi_grp_seq_id  ,
1653             v_wljdi_grp_seq_num ,
1654             v_wljdi_max_assg_units,
1655             v_wljdi_parent_seq_num,
1656             v_wljdi_resource_hours,
1657             /* ST :  Detailed scheduling : END */
1658             /* ST : Detailed Scheduling : Considered for adding a resource */
1659             v_wljdi_department_id      ,
1660             v_wljdi_department_code,
1661             /* ST : Detailed Scheduling :  end adding a resource changes */
1662             -- Added For LBJ Interface Alcoa Enhancement
1663             v_wljdi_recommended,
1664             v_wljdi_std_op_code,
1665             v_wljdi_std_op_id,
1666             v_wljdi_op_yield_enable,
1667             v_wljdi_min_trf_qty,
1668             v_wljdi_backflush_flag,
1669             v_wljdi_count_point_type,
1670             v_wljdi_description,
1671             v_wljdi_inc_in_rollup,
1672             v_wljdi_option_dep_flag,
1673             v_wljdi_inventory_item_id_old,
1674             v_wljdi_inventory_item_old,
1675             v_wljdi_wip_supply_type,
1676 	    v_wljdi_cum_yield,
1677 	    v_wljdi_rev_cum_yield
1678         ;
1679         close c_wljdi;
1680         --EC: bug 3313463/3311695
1681     end if;
1682 
1683 
1684     ---------------------------------------
1685     -- validate required fields and default
1686     ---------------------------------------
1687 l_stmt_num := 20;
1688     -- clear output PL/SQL table
1689     l_out := 1;
1690     l_v_sub_grp_num.delete;
1691     l_v_rpl_grp_num.delete;
1692     l_v_res_seq_num.delete;
1693     l_v_ins_id.delete;
1694     l_v_ser_num.delete;
1695     l_v_item_id.delete;
1696     l_v_prm_item_id.delete;
1697     l_v_phm_item_id.delete;
1698     l_v_res_id.delete;
1699     l_v_record_id.delete;
1700 
1701     /* ST : Detailed Scheduling start */
1702     l_v_dept_id.delete;
1703     l_v_res_code.delete;
1704 	l_v_setup_id.delete;      -- Added for bug fix 4704289
1705     /* ST : Detailed Scheduling end */
1706 
1707     -- Added For LBJ Interface Alcoa Enhancement
1708     l_v_op_start_date.delete;
1709     l_v_op_completion_date.delete;
1710     l_v_min_trf_qty.delete;
1711     l_v_count_point_type.delete;
1712     l_v_description.delete;
1713     l_v_backflush_flag.delete;
1714     l_v_op_yield_enable.delete;
1715     l_v_option_dep_flag.delete;
1716     l_v_inc_in_rollup.delete;
1717     l_v_std_op_id.delete;
1718     l_v_std_op_code.delete;
1719     l_v_item_old_id.delete;
1720     l_v_recommended.delete;
1721 
1722     l_d := v_wljdi_record_id.first;
1723     while l_d IS NOT NULL
1724     loop
1725         l_load_type         := v_wljdi_load_type(l_d);
1726         l_sub_type          := v_wljdi_substitution_type(l_d);
1727         l_rtg_op_seq_num    := v_wljdi_routing_op_seq_num(l_d);
1728         l_job_op_seq_num    := v_wljdi_job_op_seq_num(l_d);
1729 
1730         -- We will ignore job_op_seq_num if p_wo_records_exist <> 'Y'
1731         if NOT(p_wo_records_exist = 'Y') then
1732             v_wljdi_job_op_seq_num(l_d) := NULL;
1733         end if;
1734 
1735         -- Added For LBJ Interface Alcoa Enhancement
1736         -- Validate job_op_seq_num.
1737         if v_wljdi_job_op_seq_num(l_d) is not null then
1738             begin
1739                 select 1
1740                 into l_xst
1741                 from wip_operations
1742                 where wip_entity_id = p_wip_entity_id
1743                 and organization_id = p_org_id
1744                 and operation_seq_num = v_wljdi_job_op_seq_num(l_d);
1745             exception
1746                 when others then
1747                     raise e_invalid_job_op_seq;
1748             end;
1749         end if;
1750 
1751 l_stmt_num := 30;
1752         -- validete substitution_type
1753         if l_sub_type not in (WSM_SUB_DEL, WSM_SUB_ADD, WSM_SUB_CHG,
1754                               WSM_SUB_REC, WSM_SUB_DIS)
1755         then
1756             -- get error message
1757             fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1758             fnd_message.set_token('FLD_NAME', 'substitution_type');
1759             p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1760                            || fnd_message.get;
1761             p_error_code := -1;
1762             raise e_wljdi_common_error;
1763         end if;
1764 
1765         ---------------------------------------------------------------------------
1766         -- {{ validete load_type, substitution_type combination and required fields
1767         ---------------------------------------------------------------------------
1768         if (l_load_type = WSM_LOAD_RES) then        -- resource
1769 l_stmt_num := 40.1;
1770             if l_sub_type NOT IN (WSM_SUB_DEL, WSM_SUB_ADD, WSM_SUB_CHG, WSM_SUB_REC)
1771             then
1772                 raise e_invalid_sub_load_type;
1773             end if;
1774             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL)
1775             then
1776                 raise e_invalid_op_seq_num;
1777             end if;
1778 
1779             -- check resource_id_new/resource_code_new
1780             -- Modified For LBJ Interface Alcoa Enhancement
1781             -- Resource New is not used while deleting resource.
1782             if l_sub_type <> WSM_SUB_DEL then
1783                 if( v_wljdi_resource_id_new(l_d) IS NULL) then
1784                     if v_wljdi_resource_code_new(l_d) IS NULL then
1785                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
1786                         fnd_message.set_token('FLD_NAME', 'resource_id_new/resource_code_new');
1787                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1788                                     || fnd_message.get;
1789                         p_error_code := -1;
1790                         raise e_wljdi_common_error;
1791                     else
1792                         -- get resource id from item name
1793                         l_hash_value := dbms_utility.get_hash_value(
1794                                                 v_wljdi_resource_code_new(l_d) || p_org_id,
1795                                                 37, 1073741824);
1796                         if(g_v_res_id.exists(l_hash_value)) then
1797                             v_wljdi_resource_id_new(l_d) := g_v_res_id(l_hash_value);
1798                         else
1799                             begin
1800 l_stmt_num := 40.2;
1801                                 select unique(resource_id)
1802                                 into   v_wljdi_resource_id_new(l_d)
1803                                 from   bom_resources
1804                                 where  resource_code = v_wljdi_resource_code_new(l_d)
1805                                 and    organization_id = p_org_id
1806                                 and    cost_code_type in (3,4);
1807                             exception
1808                                 when others then
1809                                     raise e_invalid_resource_code;
1810                             end;
1811                             g_v_res_id(l_hash_value) := v_wljdi_resource_id_new(l_d);
1812                         end if;
1813                     end if;
1814                 --end if;
1815                 /* ST : Detailed Scheduling..... */
1816                 elsif v_wljdi_resource_code_new(l_d) IS NULL then
1817                         begin   /* unique why...? ..... */
1818                                     select unique(resource_code)
1819                                     into   v_wljdi_resource_code_new(l_d)
1820                                     from   bom_resources
1821                                     where  resource_id = v_wljdi_resource_id_new(l_d)
1822                                     and    organization_id = p_org_id
1823                                     and    cost_code_type in (3,4);
1824 
1825                         exception
1826                                     when others then
1827                                     raise e_invalid_resource_id;
1828                         end;
1829                 end if;
1830 
1831                 if(v_wljdi_resource_id_new(l_d) IS NULL) then
1832                     fnd_message.set_name('WSM','WSM_NULL_FIELD');
1833                     fnd_message.set_token('FLD_NAME', 'resource_id_new');
1834                     p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1835                                 || fnd_message.get;
1836                     p_error_code := -1;
1837                     raise e_wljdi_common_error;
1838                 end if;
1839             else
1840                 if v_wljdi_resource_seq_num(l_d) is null then
1841                     raise e_invalid_res_seq_num;
1842                 end if;
1843 
1844                 if l_job_op_seq_num is not null then
1845                     BEGIN
1846                         l_xst := 0;
1847 
1848                         select resource_id
1849                         into v_wljdi_resource_id_new(l_d)
1850                         from   wip_operation_resources wor
1851                         where  wor.wip_entity_id = p_wip_entity_id
1852                         and    wor.operation_seq_num = l_job_op_seq_num
1853                         and    wor.resource_seq_num = v_wljdi_resource_seq_num(l_d);
1854                     EXCEPTION
1855                         when no_data_found then
1856                             l_xst := 1;
1857                         when others then
1858                             raise e_invalid_res_seq_num;
1859                     END;
1860                     if l_xst =1 then
1861                         begin
1862                             select resource_id
1863                             into v_wljdi_resource_id_new(l_d)
1864                             from   wip_sub_operation_resources wsor
1865                             where  wsor.wip_entity_id = p_wip_entity_id
1866                             and    wsor.operation_seq_num = l_job_op_seq_num
1867                             and    wsor.resource_seq_num = v_wljdi_resource_seq_num(l_d);
1868                         exception
1869                             when others then
1870                                 raise e_invalid_res_seq_num;
1871                         end;
1872                     end if;
1873                 elsif l_rtg_op_seq_num is not null then
1874                     BEGIN
1875                         l_xst := 0;
1876                         select  resource_id
1877                         into    v_wljdi_resource_id_new(l_d)
1878                         from    wsm_copy_op_resources wcor
1879                         where   wcor.wip_entity_id        = p_wip_entity_id
1880                         and     wcor.operation_seq_num    = l_rtg_op_seq_num
1881                         and     wcor.resource_seq_num = v_wljdi_resource_seq_num(l_d);
1882                     EXCEPTION
1883                         when others then
1884                             raise e_invalid_res_seq_num;
1885                     END;
1886 
1887                 end if;
1888 
1889             end if;
1890             /* ST : Detailed Scheduling..... end */
1891             --if(v_wljdi_resource_id_new(l_d) IS NULL or
1892             --   v_wljdi_substitute_group_num(l_d) IS NULL )
1893             --then
1894             --    fnd_message.set_name('WSM','WSM_NULL_FIELD');
1895             --    fnd_message.set_token('FLD_NAME', 'resource_id_new/substitute_group_num');
1896             --    p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1897             --                || fnd_message.get;
1898             --    p_error_code := -1;
1899             --    raise e_wljdi_common_error;
1900             --end if;
1901 	    --Modiified for Alcoa Enhancement
1902 --            if(v_wljdi_resource_id_new(l_d) IS NULL)
1903 --            then
1904 --                fnd_message.set_name('WSM','WSM_NULL_FIELD');
1905 --                fnd_message.set_token('FLD_NAME', 'resource_id_new');
1906 --                p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1907 --                            || fnd_message.get;
1908 --                p_error_code := -1;
1909 --                raise e_wljdi_common_error;
1910 --            end if;
1911 
1912             /* ST : Detailed Scheduling : Add code here to default the dept id from dept code provided.... */
1913             if (v_wljdi_department_code(l_d) IS NOT NULL) then
1914                     begin
1915                              select unique(department_id)
1916                              into   l_dept_id
1917                              from   bom_departments
1918                              where  department_code = v_wljdi_department_code(l_d)
1919                              and    department_id   = nvl(v_wljdi_department_id(l_d),department_id)
1920                              and    organization_id = p_org_id;
1921                         exception
1922                              when others then
1923                                      raise e_invalid_dept_code;
1924                      end;
1925              end if;
1926              v_wljdi_department_id(l_d) := l_dept_id;
1927              /* ST : Detailed Scheduling : end */
1928 
1929 
1930 			 /* SS : Detailed Scheduling : Add code here to default the setup id from
1931 			    setup code if setup id is entered as null. Added for bug fix 4704289  */
1932             if (v_wljdi_setup_id(l_d) IS NULL and v_wljdi_setup_code(l_d) is NOT NULL) then
1933 					begin
1934 
1935 							select  bst.setup_id
1936 							into    l_setup_id
1937 							from    BOM_SETUP_TYPES bst
1938 							where   bst.setup_code      = v_wljdi_setup_code(l_d)
1939 							and     bst.organization_id = p_org_id;
1940 
1941 							v_wljdi_setup_id(l_d) :=l_setup_id;
1942 					exception
1943 							when no_data_found then
1944 								raise e_invalid_setup_code;
1945 							when others then
1946 								null;
1947 					end;
1948 			end if;
1949 			/* SS : Detailed Scheduling : end.  Added for bug fix 4704289 */
1950 
1951         elsif (l_load_type = WSM_LOAD_COMP) then     -- component
1952 l_stmt_num := 50.1;
1953             if l_sub_type NOT IN (WSM_SUB_DEL, WSM_SUB_ADD, WSM_SUB_CHG, WSM_SUB_REC)
1954             then
1955                 raise e_invalid_sub_load_type;
1956             end if;
1957             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL)
1958             then
1959                 raise e_invalid_op_seq_num;
1960             end if;
1961 
1962            -- Added For LBJ Interface Alcoa Enhancement
1963             if l_sub_type = WSM_SUB_DEL then
1964                 if(v_wljdi_inventory_item_id_old(l_d) IS NULL) then
1965                     if v_wljdi_inventory_item_old(l_d) IS NULL THEN
1966                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
1967                         fnd_message.set_token('FLD_NAME', 'inventory_item_id_old/inventory_item_old');
1968                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1969                                     || fnd_message.get;
1970                         p_error_code := -1;
1971                         raise e_wljdi_common_error;
1972                     else
1973 l_stmt_num := 50;
1974                         begin
1975                             select unique(inventory_item_id)
1976                             into   v_wljdi_inventory_item_id_old(l_d)
1977                             from   mtl_system_items_kfv
1978                             where  concatenated_segments = v_wljdi_inventory_item_old(l_d)
1979                             and    organization_id = p_org_id;
1980                         exception
1981                             when others then
1982                                 raise e_invalid_item_name;
1983                         end;
1984                     end if;
1985                 else
1986                     begin
1987                         select concatenated_segments
1988                         into   v_wljdi_inventory_item_old(l_d)
1989                         from   mtl_system_items_kfv
1990                         where  inventory_item_id = v_wljdi_inventory_item_id_old(l_d)
1991                         and    organization_id = p_org_id;
1992                     exception
1993                         when others then
1994                             raise e_invalid_item_name;
1995                     end;
1996                 end if;
1997             end if;
1998 
1999             -- check inventory_item_id_new/inventory_item_new
2000             -- Modified For LBJ Interface Alcoa Enhancement
2001             if l_sub_type not in (WSM_SUB_DEL) then
2002                 if(v_wljdi_inventory_item_id_new(l_d) IS NULL) then
2003                     if v_wljdi_inventory_item_new(l_d) IS NULL then
2004                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
2005                         fnd_message.set_token('FLD_NAME', 'inventory_item_id_new/inventory_item_new');
2006                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2007                                     || fnd_message.get;
2008                         p_error_code := -1;
2009                         raise e_wljdi_common_error;
2010                     else
2011 l_stmt_num := 50.2;
2012                         -- get item id from item name
2013                         l_hash_value := dbms_utility.get_hash_value(
2014                                                 v_wljdi_inventory_item_new(l_d) || p_org_id,
2015                                                 37, 1073741824);
2016                         if(g_v_item_id.exists(l_hash_value)) then
2017                             v_wljdi_inventory_item_id_new(l_d) := g_v_item_id(l_hash_value);
2018                         else
2019                             begin
2020                                 select unique(inventory_item_id)
2021                                 into   v_wljdi_inventory_item_id_new(l_d)
2022                                 from   mtl_system_items_kfv
2023                                 where  concatenated_segments = v_wljdi_inventory_item_new(l_d)
2024                                 and    organization_id = p_org_id;
2025                             exception
2026                                 when others then
2027                                     raise e_invalid_item_name;
2028                             end;
2029                             g_v_item_id(l_hash_value) := v_wljdi_inventory_item_id_new(l_d);
2030                         end if;
2031                     end if;
2032                 else
2033                     begin
2034                         select concatenated_segments
2035                         into   v_wljdi_inventory_item_new(l_d)
2036                         from   mtl_system_items_kfv
2037                         where  inventory_item_id = v_wljdi_inventory_item_id_new(l_d)
2038                         and    organization_id = p_org_id;
2039                     exception
2040                         when others then
2041                             raise e_invalid_item_name;
2042                     end;
2043                 end if;
2044             end if;
2045 
2046             -- check primary_item_id/primary_item
2047             if(v_wljdi_primary_item_id(l_d) IS NULL) then
2048                 if v_wljdi_primary_item(l_d) IS NULL then
2049                     fnd_message.set_name('WSM','WSM_NULL_FIELD');
2050                     fnd_message.set_token('FLD_NAME', 'primary_item_id/primary_item');
2051                     p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2052                                 || fnd_message.get;
2053                     p_error_code := -1;
2054                     raise e_wljdi_common_error;
2055                 else
2056 l_stmt_num := 50.3;
2057                     -- get primary item id from item name
2058                     l_hash_value := dbms_utility.get_hash_value(
2059                                             v_wljdi_primary_item(l_d) || p_org_id,
2060                                             37, 1073741824);
2061                     if(g_v_item_id.exists(l_hash_value)) then
2062                         v_wljdi_primary_item_id(l_d) := g_v_item_id(l_hash_value);
2063                     else
2064                         begin
2065                             select unique(inventory_item_id)
2066                             into   v_wljdi_primary_item_id(l_d)
2067                             from   mtl_system_items_kfv
2068                             where  concatenated_segments = v_wljdi_primary_item(l_d)
2069                             and    organization_id = p_org_id;
2070                         exception
2071                             when others then
2072                                 raise e_invalid_item_name;
2073                         end;
2074                         g_v_item_id(l_hash_value) := v_wljdi_primary_item_id(l_d);
2075                     end if;
2076                 end if;
2077             end if;
2078 
2079             -- Modified for Alcoa Enhancement
2080             -- check src_phantom_item_id/phantom_item
2081             if v_wljdi_src_phantom_item_id(l_d) IS NULL then
2082                 if v_wljdi_src_phantom_item(l_d) IS NOT NULL then
2083 l_stmt_num := 50.4;
2084                     -- get item id from item name
2085                     l_hash_value := dbms_utility.get_hash_value(
2086                                             v_wljdi_src_phantom_item(l_d) || p_org_id,
2087                                             37, 1073741824);
2088                     if(g_v_item_id.exists(l_hash_value)) then
2089                         v_wljdi_src_phantom_item_id(l_d) := g_v_item_id(l_hash_value);
2090                     else
2091                         begin
2092                             select unique(inventory_item_id)
2093                             into   v_wljdi_src_phantom_item_id(l_d)
2094                             from   mtl_system_items_kfv
2095                             where  concatenated_segments = v_wljdi_src_phantom_item(l_d)
2096                             and    organization_id = p_org_id;
2097                         exception
2098                             when others then
2099                                 raise e_invalid_item_name;
2100                         end;
2101                         g_v_item_id(l_hash_value) := v_wljdi_src_phantom_item_id(l_d);
2102                     end if;
2103                 end if;
2104             else
2105                 begin
2106                     select concatenated_segments
2107                     into   v_wljdi_src_phantom_item(l_d)
2108                     from   mtl_system_items_kfv
2109                     where  inventory_item_id = v_wljdi_src_phantom_item_id(l_d)
2110                     and    organization_id = p_org_id;
2111                 exception
2112                     when others then
2113                         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2114                         fnd_message.set_token('FLD_NAME', 'Source_phantom_item_id/Source_phantom_item_name');
2115                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2116                         p_error_code := -1;
2117                         raise e_wljdi_common_error;
2118                 end;
2119             end if;
2120 
2121             -- Default source phantom Id for phantom component or it's child while deletion.
2122             if (l_sub_type = WSM_SUB_DEL AND v_wljdi_routing_op_seq_num(l_d) is not null) THEN
2123                  begin
2124                     select source_phantom_id
2125                     into   v_wljdi_src_phantom_item_id(l_d)
2126                     from   wsm_copy_requirement_ops
2127                     where  wip_entity_id=p_wip_entity_id
2128                     and    component_item_id = v_wljdi_inventory_item_id_old(l_d)
2129                     and    operation_seq_num in (v_wljdi_routing_op_seq_num(l_d), (-1)*v_wljdi_routing_op_seq_num(l_d));
2130                  exception
2131                     when others then
2132                       raise e_invalid_item_name;
2133                  end;
2134             end if;
2135 
2136             -- 1. For a parent phantom, phantom item id will be set to 0.
2137             -- 2. For a child of phantom, phantom item id will be set to parent phantom.
2138             -- 3. For a non-phantom, phantom item id will be set to -1.
2139             --  This will be used to sort the components in appropriate order when processing them.
2140 
2141             if v_wljdi_src_phantom_item_id(l_d) is not null then
2142                 if ( (v_wljdi_src_phantom_item_id(l_d)= v_wljdi_inventory_item_id_old(l_d) and
2143                       l_sub_type = WSM_SUB_DEL) OR
2144                      (v_wljdi_wip_supply_type(l_d)= WIP_CONSTANTS.PHANTOM and
2145                       l_sub_type = WSM_SUB_ADD and
2146                       v_wljdi_src_phantom_item_id(l_d)= v_wljdi_inventory_item_id_new(l_d)) ) then
2147                         v_wljdi_src_phantom_item_id(l_d):=0;
2148                 end if;
2149             else
2150                 if (v_wljdi_wip_supply_type(l_d)= WIP_CONSTANTS.PHANTOM and
2151                       l_sub_type = WSM_SUB_ADD) then
2152                     v_wljdi_src_phantom_item_id(l_d):= 0;
2153                 else
2154                     v_wljdi_src_phantom_item_id(l_d):= -1;
2155                 end if;
2156 
2157             end if;
2158 
2159             -- ignore the following fields
2160             v_wljdi_substitute_group_num(l_d)  := NULL;
2161             v_wljdi_replacement_group_num(l_d) := NULL;
2162             v_wljdi_resource_id_new(l_d)       := NULL;
2163             /* ST : Detailed Scheduling..... */
2164             v_wljdi_resource_code_new(l_d)     := NULL;
2165             /* ST : Detailed Scheduling..... */
2166             v_wljdi_resource_seq_num(l_d)      := NULL;
2167             v_wljdi_instance_id_new(l_d)       := NULL;
2168             v_wljdi_serial_number_new(l_d)     := NULL;
2169             if lbji_debug = 'Y' then
2170                 fnd_file.put_line(fnd_file.log,
2171                     'Ignore the following fields for load_type 2: substitute_group_num, '
2172                     || 'replacement_group_num, resource_id_new, resource_seq_num, '
2173                     || 'instance_id_new, serial_number_new');
2174             end if;
2175 
2176 
2177         elsif (l_load_type = WSM_LOAD_OP) then     -- operation: load_type 3 and 4 are the same
2178 l_stmt_num := 60.1;
2179 	    -- Modified For LBJ Interface Alcoa Enhancement
2180             if(l_sub_type NOT IN (WSM_SUB_CHG, WSM_SUB_REC, WSM_SUB_ADD, WSM_SUB_DEL)) then
2181                 raise e_invalid_sub_load_type;
2182             end if;
2183             -- Modified For LBJ Interface Alcoa Enhancement
2184             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL and l_sub_type <> WSM_SUB_ADD)
2185             then
2186                 raise e_invalid_op_seq_num;
2187             end if;
2188 
2189             -- Added For LBJ Interface Alcoa Enhancement
2190             if (l_sub_type = WSM_SUB_ADD) then
2191 
2192                 -- This code Defaults Standard operation Id.
2193                 if (v_wljdi_std_op_code(l_d) is not null) then
2194                     begin
2195                         select standard_operation_id
2196                         into v_wljdi_std_op_id(l_d)
2197                         from bom_standard_operations
2198                         where organization_id = p_org_id
2199                         and operation_code = v_wljdi_std_op_code(l_d)
2200                         and standard_operation_id = nvl(v_wljdi_std_op_id(l_d),standard_operation_id);
2201                     exception
2202                         when others then
2203                             raise e_inv_std_op;
2204                     end;
2205                 end if;
2206 
2207                 if (v_wljdi_std_op_id(l_d) is not null and
2208                     v_wljdi_std_op_code(l_d) is null) then
2209                     begin
2210                         select operation_code
2211                         into v_wljdi_std_op_code(l_d)
2212                         from bom_standard_operations
2213                         where organization_id = p_org_id
2214                         and standard_operation_id = v_wljdi_std_op_id(l_d);
2215                     exception
2216                         when others then
2217                             raise e_inv_std_op;
2218                     end;
2219                 end if;
2220 
2221                 if (v_wljdi_std_op_id(l_d) is not null) then
2222 
2223                     begin
2224                         select department_id,
2225                                nvl(v_wljdi_min_trf_qty(l_d),minimum_transfer_quantity),
2226                                nvl(v_wljdi_count_point_type(l_d),count_point_type),
2227                                nvl(v_wljdi_description(l_d),operation_description),
2228                                nvl(v_wljdi_backflush_flag(l_d),backflush_flag),
2229                                nvl(v_wljdi_op_yield_enable(l_d),operation_yield_enabled),
2230                                nvl(v_wljdi_option_dep_flag(l_d),option_dependent_flag),
2231                                nvl(v_wljdi_inc_in_rollup(l_d),include_in_rollup)
2232                         into  v_wljdi_department_id(l_d),
2233                               v_wljdi_min_trf_qty(l_d),
2234                               v_wljdi_count_point_type(l_d),
2235                               v_wljdi_description(l_d),
2236                               v_wljdi_backflush_flag(l_d),
2237                               v_wljdi_op_yield_enable(l_d),
2238                               v_wljdi_option_dep_flag(l_d),
2239                               v_wljdi_inc_in_rollup(l_d)
2240                         from   bom_standard_operations
2241                         where standard_operation_id = v_wljdi_std_op_id(l_d)
2242                         and  organization_id = p_org_id;
2243                     exception
2244                         when others then
2245                             raise e_inv_std_op;
2246                     end;
2247                 end if;
2248                 -- Validate Department
2249                 if (v_wljdi_department_code(l_d) IS NOT NULL) then
2250                     begin
2251                         select unique(department_id)
2252                         into   v_wljdi_department_id(l_d)
2253                         from   bom_departments
2254                         where  department_code = v_wljdi_department_code(l_d)
2255                         and    department_id   = nvl(v_wljdi_department_id(l_d),department_id)
2256                         and    organization_id = p_org_id;
2257                     exception
2258                         when others then
2259                             raise e_invalid_dept_code;
2260                     end;
2261                 else
2262                     begin
2263                         select 1
2264                         into   l_temp
2265                         from   bom_departments
2266                         where  department_id   = v_wljdi_department_id(l_d)
2267                         and    organization_id = p_org_id;
2268                     exception
2269                         when others then
2270                             raise e_invalid_dept_code;
2271                     end;
2272                 end if;
2273                 -- Validate Min trf Qty.
2274                 if (v_wljdi_min_trf_qty(l_d) < 0 ) then
2275                     raise e_invalid_min_trf_qty;
2276                 end if;
2277                 if (v_wljdi_min_trf_qty(l_d) is null) then
2278                     v_wljdi_min_trf_qty(l_d) := 0;
2279                 end if;
2280 
2281                 -- Validate Count Point
2282                 if (v_wljdi_count_point_type(l_d) is not null and
2283                     v_wljdi_count_point_type(l_d) not in (WIP_CONSTANTS.YES_AUTO,
2284                                                           WIP_CONSTANTS.NO_AUTO,
2285                                                           WIP_CONSTANTS.NO_DIRECT)) then
2286                     raise e_invalid_count_point;
2287                 end if;
2288                 if (v_wljdi_count_point_type(l_d) is null) then
2289                     v_wljdi_count_point_type(l_d) :=0;
2290                 end if;
2291 
2292                 -- Validate Backflush flag.
2293                 if (v_wljdi_backflush_flag(l_d) is not null and
2294                     v_wljdi_backflush_flag(l_d) not in (WIP_CONSTANTS.WBF_BF_NOPAGE ,
2295                                                         WIP_CONSTANTS.WBF_BF_PAGE)) then
2296                     raise e_invalid_backflush_flag;
2297                 end if;
2298                 if (v_wljdi_backflush_flag(l_d) is null) then
2299                     v_wljdi_backflush_flag(l_d) :=0;
2300                 end if;
2301 
2302                 if (v_wljdi_op_yield_enable(l_d) is null) then
2303                     v_wljdi_op_yield_enable(l_d) :=1;
2304                 end if;
2305 
2306                 if (v_wljdi_inc_in_rollup(l_d) is null) then
2307                     v_wljdi_inc_in_rollup(l_d) :=1;
2308                 end if;
2309 
2310                 if (v_wljdi_option_dep_flag(l_d) is null) then
2311                     v_wljdi_option_dep_flag(l_d) :=2;
2312                 end if;
2313                 l_temp :=0;
2314                 if (v_wljdi_routing_op_seq_num(l_d) is not null) then
2315                     l_temp :=0;
2316                     begin
2317                         select 1
2318                         into l_temp
2319                         from wsm_copy_operations
2320                         where wip_entity_id = p_wip_entity_id
2321                         and organization_id = p_org_id
2322                         and operation_seq_num = v_wljdi_routing_op_seq_num(l_d);
2323                     exception
2324                         when no_data_found then
2325                             l_temp :=0;
2326                         when others then
2327                             l_temp :=1;
2328                     end;
2329                 end if;
2330 
2331                 if (l_temp =1) then
2332                     raise e_dup_op_seq;
2333                 end if;
2334 
2335                 select  scheduled_start_date,
2336                         scheduled_completion_date
2337                 into    l_job_start_date,
2338                         l_job_compl_date
2339                 from    wip_discrete_jobs
2340                 where   wip_entity_id = p_wip_entity_id
2341                 and     organization_id = p_org_id;
2342 
2343                 if(v_wljdi_op_start_date(l_d) IS NOT NULL and v_wljdi_op_completion_date(l_d) IS NOT NULL) then
2344 
2345                     if( NVL(l_job_start_date, v_wljdi_op_start_date(l_d)) > v_wljdi_op_start_date(l_d) or
2346                         v_wljdi_op_start_date(l_d) > v_wljdi_op_completion_date(l_d) or
2347                         v_wljdi_op_completion_date(l_d) > NVL(l_job_compl_date, v_wljdi_op_completion_date(l_d)))
2348                     then
2349                         raise e_invalid_op_date;
2350                     end if;
2351                 elsif(v_wljdi_op_start_date(l_d) IS NOT NULL and v_wljdi_op_completion_date(l_d) IS NULL) then
2352 
2353                     if( NVL(l_job_start_date, v_wljdi_op_start_date(l_d)) > v_wljdi_op_start_date(l_d) or
2354                         v_wljdi_op_start_date(l_d) > NVL(l_job_compl_date, v_wljdi_op_start_date(l_d)))
2355                     then
2356                         raise e_invalid_op_date;
2357                     end if;
2358                     v_wljdi_op_completion_date(l_d) := l_job_compl_date;
2359 
2360                 elsif(v_wljdi_op_start_date(l_d) IS NULL and v_wljdi_op_completion_date(l_d) IS NOT NULL) then
2361 
2362                     if( NVL(l_job_start_date, v_wljdi_op_completion_date(l_d)) > v_wljdi_op_completion_date(l_d) or
2363                         v_wljdi_op_completion_date(l_d) > NVL(l_job_compl_date, v_wljdi_op_completion_date(l_d)))
2364                     then
2365                         raise e_invalid_op_date;
2366                     end if;
2367                     v_wljdi_op_start_date(l_d) := l_job_start_date;
2368                 else
2369                     v_wljdi_op_start_date(l_d) := l_job_start_date;
2370                     v_wljdi_op_completion_date(l_d) := l_job_compl_date;
2371                 end if;
2372 
2373             end if;
2374 
2375 
2376         elsif (l_load_type in (WSM_LOAD_RES_USE, WSM_LOAD_RES_INS)) then  -- resource usage/instance
2377 l_stmt_num := 70.1;
2378             if l_sub_type NOT IN (WSM_SUB_DEL, WSM_SUB_ADD, WSM_SUB_REC)
2379             then
2380                 raise e_invalid_sub_load_type;
2381             end if;
2382             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL)
2383             then
2384                 raise e_invalid_op_seq_num;
2385             end if;
2386 
2387             -- get resource_seq_num if it is null
2388             -- if l_job_op_seq_num IS NOT NULL, get from WOR
2389             -- otherwise get from WCOR
2390             l_res_seq_num := v_wljdi_resource_seq_num(l_d);
2391 
2392             -- ST : Bug fix 4240165 : Detailed Scheduling changes start
2393             l_par_res_seq_num := v_wljdi_parent_seq_num(l_d);
2394             -- ST : Bug fix 4240165 : Detailed Scheduling changes end
2395 
2396 
2397             if(l_res_seq_num IS NULL)
2398             then
2399                 l_resource_id := v_wljdi_resource_id_new(l_d);
2400                 l_sub_grp_num := v_wljdi_substitute_group_num(l_d);
2401                 l_rpl_grp_num := v_wljdi_replacement_group_num(l_d);
2402 
2403                 if(l_resource_id IS NULL) then
2404                     if v_wljdi_resource_code_new(l_d) IS NULL then
2405                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
2406                         fnd_message.set_token('FLD_NAME', 'resource_id_new/resource_code_new');
2407                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2408                                     || fnd_message.get;
2409                         p_error_code := -1;
2410                         raise e_wljdi_common_error;
2411                     else
2412                         -- get resource id from item name
2413                         l_hash_value := dbms_utility.get_hash_value(
2414                                                 v_wljdi_resource_code_new(l_d) || p_org_id,
2415                                                 37, 1073741824);
2416                         if(g_v_res_id.exists(l_hash_value)) then
2417                             l_resource_id := g_v_res_id(l_hash_value);
2418                         else
2419                             begin
2420 l_stmt_num := 70.2;
2421                                 select unique(resource_id)
2422                                 into   l_resource_id
2423                                 from   bom_resources
2424                                 where  resource_code = v_wljdi_resource_code_new(l_d)
2425                                 and    organization_id = p_org_id;
2426                             exception
2427                                 when others then
2428                                     raise e_invalid_resource_code;
2429                             end;
2430                             g_v_res_id(l_hash_value) := l_resource_id;
2431                         end if;
2432                     end if;
2433                 end if;
2434 
2435 
2436                 -- ST : Bug fix 4240165 : Detailed Scheduling changes start
2437                 IF (l_par_res_seq_num IS NULL) THEN
2438                 -- ST : Bug fix 4240165 : Detailed Scheduling changes end
2439 
2440                         if( l_job_op_seq_num IS NULL) then
2441                                 begin
2442                                         l_stmt_num  := 70.3;
2443                                         select  resource_seq_num
2444                                         into    l_res_seq_num
2445                                         from    wsm_copy_op_resources wcor
2446                                         where   wcor.wip_entity_id         = p_wip_entity_id
2447                                         and     wcor.organization_id       = p_org_id
2448                                         and     wcor.operation_seq_num     = l_rtg_op_seq_num
2449                                         and     wcor.resource_id           = l_resource_id
2450                                         and     NVL(wcor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
2451                                         and     NVL(wcor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
2452                                 exception
2453                                         when no_data_found then
2454                                                 raise e_no_res_seq_num;
2455 
2456                                         when too_many_rows then
2457                                                 raise e_no_res_seq_num;
2458                                 end;
2459                         else
2460                                 begin
2461                                         l_stmt_num  := 70.4;
2462                                         select  resource_seq_num
2463                                         into    l_res_seq_num
2464                                         from    wip_operation_resources wor
2465                                         where   wor.wip_entity_id         = p_wip_entity_id
2466                                         and     wor.organization_id       = p_org_id
2467                                         and     wor.operation_seq_num     = l_job_op_seq_num
2468                                         and     wor.resource_id           = l_resource_id
2469                                         and     NVL(wor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
2470                                         and     NVL(wor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
2471                                 exception
2472                                         when no_data_found then
2473                                                 raise e_no_res_seq_num;
2474                                         when too_many_rows then
2475                                                 raise e_no_res_seq_num;
2476                                 end;
2477                         end if;
2478                         v_wljdi_resource_seq_num(l_d) := l_res_seq_num;
2479                 end if;       -- l_parent_res_seq_num is NULL
2480                 -- ST : Bug fix 4240165 : Detailed Scheduling changes end
2481             end if; -- l_res_seq_num IS NULL
2482 l_stmt_num  := 70.5;
2483 
2484             -- ignore the following fields
2485             v_wljdi_substitute_group_num(l_d)  := NULL;
2486             v_wljdi_replacement_group_num(l_d) := NULL;
2487 
2488             -- ST : Bug fix 4240165 : Detailed Scheduling changes
2489             -- NULL it out only when Resource seq num is obtained...
2490             IF v_wljdi_resource_seq_num(l_d) IS NOT NULL THEN
2491                     v_wljdi_resource_id_new(l_d)       := NULL;
2492             END IF;
2493 
2494         elsif (l_load_type = WSM_LOAD_LINK) then     -- link: load_type must be 4 or 5
2495 l_stmt_num := 80;
2496             -- Modified For LBJ Interface Alcoa Enhancement
2497             if l_sub_type NOT IN (WSM_SUB_DIS, WSM_SUB_REC, WSM_SUB_DEL, WSM_SUB_ADD) then
2498                 raise e_invalid_sub_load_type;
2499             end if;
2500             if(--v_wljdi_routing_op_seq_num(l_d) IS NULL or
2501                v_wljdi_next_rtg_op_seq_num(l_d) IS NULL)
2502             then
2503                 fnd_message.set_name('WSM','WSM_NULL_FIELD');
2504                 --fnd_message.set_token('FLD_NAME', 'routing_op_seq_num and next_routing_op_seq_num');
2505                 fnd_message.set_token('FLD_NAME', 'next_routing_op_seq_num');
2506                 p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2507                             || fnd_message.get;
2508                 p_error_code := -1;
2509                 raise e_wljdi_common_error;
2510             end if;
2511 
2512         elsif (l_load_type = WSM_LOAD_LINK_OP) then     -- link or operation
2513 l_stmt_num := 90;
2514             if(l_sub_type NOT IN (WSM_SUB_CHG, WSM_SUB_REC)) then
2515                 raise e_invalid_sub_load_type;
2516             end if;
2517             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL)
2518             then
2519                 raise e_invalid_op_seq_num;
2520             end if;
2521 
2522         else
2523             fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2524             fnd_message.set_token('FLD_NAME', 'load_type');
2525             p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2526                            || fnd_message.get;
2527             p_error_code := -1;
2528             raise e_wljdi_common_error;
2529         end if;
2530         ---------------------------------------------------------------------------
2531         -- }} validete load_type, substitution_type combination and required fields
2532         ---------------------------------------------------------------------------
2533 l_stmt_num := 100;
2534 
2535         ---Added for Alcoa Enhancement
2536         if  upper(v_wljdi_recommended(l_d)) = 'Y' and l_sub_type= WSM_SUB_ADD then
2537             l_v_recommended(l_out) := 'Y';
2538         else
2539             l_v_recommended(l_out) := 'N';
2540         end if;
2541 
2542         l_v_sub_grp_num(l_out)  := v_wljdi_substitute_group_num(l_d);
2543         l_v_rpl_grp_num(l_out)  := v_wljdi_replacement_group_num(l_d);
2544         l_v_res_id(l_out)       := v_wljdi_resource_id_new(l_d);
2545         l_v_res_seq_num(l_out)  := v_wljdi_resource_seq_num(l_d);
2546         l_v_ins_id(l_out)       := v_wljdi_instance_id_new(l_d);
2547         l_v_ser_num(l_out)      := v_wljdi_serial_number_new(l_d);
2548         l_v_item_id(l_out)      := v_wljdi_inventory_item_id_new(l_d);
2549         l_v_prm_item_id(l_out)  := v_wljdi_primary_item_id(l_d);
2550         l_v_phm_item_id(l_out)  := v_wljdi_src_phantom_item_id(l_d);
2551         l_v_record_id(l_out)    := v_wljdi_record_id(l_d);
2552 
2553         /* ST : Detailed Scheduling start */
2554         l_v_dept_id(l_out)      := v_wljdi_department_id(l_d);
2555         l_v_res_code(l_out)     := v_wljdi_resource_code_new(l_d);
2556 		l_v_setup_id(l_out)		:= v_wljdi_setup_id(l_d);     -- Added for bug fix 4704289
2557 
2558         /* ST : Detailed Scheduling end   */
2559 
2560         -- Added For LBJ Interface Alcoa Enhancement
2561         l_v_op_start_date(l_out)      := v_wljdi_op_start_date(l_d);
2562         l_v_op_completion_date(l_out) := v_wljdi_op_completion_date(l_d);
2563         l_v_min_trf_qty(l_out)        := v_wljdi_min_trf_qty(l_d);
2564         l_v_count_point_type(l_out)   := v_wljdi_count_point_type(l_d);
2565         l_v_description(l_out)        := v_wljdi_description(l_d);
2566         l_v_backflush_flag(l_out)     := v_wljdi_backflush_flag(l_d);
2567         l_v_op_yield_enable(l_out)    := v_wljdi_op_yield_enable(l_d);
2568         l_v_option_dep_flag(l_out)    := v_wljdi_option_dep_flag(l_d);
2569         l_v_inc_in_rollup(l_out)      := v_wljdi_inc_in_rollup(l_d);
2570         l_v_std_op_id(l_out)          := v_wljdi_std_op_id(l_d);
2571         l_v_std_op_code(l_out)        := v_wljdi_std_op_code(l_d);
2572         l_v_item_old_id(l_out)        := v_wljdi_inventory_item_id_old(l_d);
2573 
2574         l_out := l_out + 1;
2575 
2576         l_d := v_wljdi_record_id.next(l_d);
2577     end loop;
2578 
2579     ---------------------------------------
2580     -- bulk update WLJDI
2581     ---------------------------------------
2582 l_stmt_num := 110;
2583     forall i in l_v_record_id.first..l_v_record_id.last
2584     update  wsm_lot_job_dtl_interface
2585     set     substitute_group_num    = l_v_sub_grp_num(i),
2586             replacement_group_num   = l_v_rpl_grp_num(i),
2587             resource_id_new         = l_v_res_id(i),
2588             resource_seq_num        = l_v_res_seq_num(i),
2589             instance_id_new         = l_v_ins_id(i),
2590             serial_number_new       = l_v_ser_num(i),
2591             inventory_item_id_new   = l_v_item_id(i),
2592             primary_item_id         = l_v_prm_item_id(i),
2593             src_phantom_item_id     = l_v_phm_item_id(i),
2594             /* ST : Detailed Scheduling start */
2595             resource_code_new       = l_v_res_code(i),
2596 			/* Added NVL on department_id for Bug 5471556. It is done so that it's not cleared
2597 			out if user entered department id alone so that it can be validated at later stage. */
2598             department_id           = nvl(l_v_dept_id(i),department_id),
2599 			setup_id				= l_v_setup_id(i),    -- Added for bug fix 4704289
2600             /* ST : Detailed Scheduling end   */
2601             -- Added For LBJ Interface Alcoa Enhancement
2602             standard_operation_id     =  l_v_std_op_id(i),
2603             operation_start_date      =  l_v_op_start_date(i),
2604             operation_completion_date =  l_v_op_completion_date(i),
2605             minimum_transfer_quantity =  l_v_min_trf_qty(i),
2606             count_point_type          =  l_v_count_point_type(i),
2607             description               =  l_v_description(i),
2608             backflush_flag            =  l_v_backflush_flag(i),
2609             operation_yield_enabled   =  l_v_op_yield_enable(i),
2610             option_dependent_flag     =  l_v_option_dep_flag(i),
2611             include_in_rollup         =  l_v_inc_in_rollup(i),
2612             standard_operation_code   =  l_v_std_op_code(i),
2613             recommended               =  l_v_recommended(i),
2614             inventory_item_id_old   = l_v_item_old_id(i)
2615     where   record_id = l_v_record_id(i);
2616 
2617 EXCEPTION
2618     when e_invalid_sub_load_type then
2619         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2620         fnd_message.set_token('FLD_NAME', 'load_type and substitution_type');
2621         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2622                     || fnd_message.get;
2623         p_error_code := -1;
2624         fnd_file.put_line(fnd_file.log, substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2625 
2626     when e_invalid_op_seq_num then
2627         fnd_message.set_name('WSM','WSM_NULL_FIELD');
2628         fnd_message.set_token('FLD_NAME', 'job_op_seq_num and routing_op_seq_num');
2629         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2630                     || fnd_message.get;
2631         p_error_code := -1;
2632         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '
2633                 || p_error_msg, 1, 2000));
2634 
2635     when e_invalid_resource_code then
2636         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2637         fnd_message.set_token('FLD_NAME', 'resource_code_new');
2638         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2639                     || fnd_message.get;
2640         p_error_code := -1;
2641         fnd_file.put_line(fnd_file.log,
2642             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2643                 || p_error_msg, 1, 2000));
2644 
2645     /* ST : Detailed Scheduling start */
2646 
2647     when e_invalid_resource_id then
2648         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2649         fnd_message.set_token('FLD_NAME', 'resource_id_new');
2650         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2651                     || fnd_message.get;
2652         p_error_code := -1;
2653         fnd_file.put_line(fnd_file.log,
2654             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2655                 || p_error_msg, 1, 2000));
2656 
2657     when e_invalid_dept_code then
2658         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2659         fnd_message.set_token('FLD_NAME', 'department_code/department id combination');
2660         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2661                     || fnd_message.get;
2662         p_error_code := -1;
2663         fnd_file.put_line(fnd_file.log,
2664             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2665                 || p_error_msg, 1, 2000));
2666 
2667     /* ST : Detailed Scheduling end */
2668 
2669     when e_invalid_item_name then
2670         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2671         fnd_message.set_token('FLD_NAME', 'item_id');
2672         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2673                     || fnd_message.get;
2674         p_error_code := -1;
2675         fnd_file.put_line(fnd_file.log,
2676             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2677                 || p_error_msg, 1, 2000));
2678 
2679     when e_no_res_seq_num then
2680         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2681                     || 'resource_seq_num cannot be uniquely found'; -- CZHDBG_MSG
2682         p_error_code := -1;
2683         fnd_file.put_line(fnd_file.log,
2684             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2685                 || p_error_msg, 1, 2000));
2686 
2687     when e_wljdi_common_error then
2688         fnd_file.put_line(fnd_file.log,
2689             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2690                 || p_error_msg, 1, 2000));
2691 
2692 	when e_invalid_setup_code then
2693 		fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2694         fnd_message.set_token('FLD_NAME', 'Setup Code ');
2695 		p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2696                     || fnd_message.get;
2697         p_error_code := -1;
2698         fnd_file.put_line(fnd_file.log,
2699             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2700                 || p_error_msg, 1, 2000));
2701 
2702    ---Added for LBJ Enhancement
2703 
2704     when e_res_seq_num_exist  then
2705         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2706         fnd_message.set_token('FLD_NAME', 'resource_seq_num/job_op_seq_num combination');
2707         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2708                     || fnd_message.get;
2709         p_error_code := -1;
2710         fnd_file.put_line(fnd_file.log,
2711             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2712                 || p_error_msg, 1, 2000));
2713 
2714     when e_invalid_res_seq_num then
2715         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2716         fnd_message.set_token('FLD_NAME', 'resource_seq_num/job_op_seq_num combination');
2717         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2718                     || fnd_message.get;
2719         p_error_code := -1;
2720         fnd_file.put_line(fnd_file.log,
2721             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2722                 || p_error_msg, 1, 2000));
2723     when e_inv_std_op then
2724         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2725         fnd_message.set_token('FLD_NAME', 'standard_operation_id/standard_operation_code combination');
2726         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2727         p_error_code := -1;
2728         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2729     when e_invalid_min_trf_qty then
2730         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2731         fnd_message.set_token('FLD_NAME', 'minimum_transfer_quantity');
2732         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2733         p_error_code := -1;
2734         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2735     when e_invalid_count_point then
2736         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2737         fnd_message.set_token('FLD_NAME', 'count_point_type');
2738         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2739         p_error_code := -1;
2740         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2741     when e_invalid_backflush_flag then
2742         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2743         fnd_message.set_token('FLD_NAME', 'backflush_flag');
2744         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2745         p_error_code := -1;
2746         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2747     when e_dup_op_seq then
2748         fnd_message.set_name('WSM','WSM_INVALID_OPERATION');
2749         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2750         p_error_code := -1;
2751         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2752     when e_invalid_op_date then
2753         fnd_message.set_name('WSM','WSM_OP_BAD_DATES');
2754         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2755         p_error_code := -1;
2756         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2757     when e_invalid_routing_op_seq then
2758         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2759         fnd_message.set_token('FLD_NAME', 'Routing_op_seq_num');
2760         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2761         p_error_code := -1;
2762         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2763     when e_invalid_job_op_seq then
2764         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2765         fnd_message.set_token('FLD_NAME', 'Job_op_seq_num');
2766         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2767         p_error_code := -1;
2768         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2769 
2770     when others then
2771         p_error_code := -1;
2772         p_error_msg := 'Error: default_wljdi_fields: (#'||
2773                         l_stmt_num||') '||SUBSTR(SQLERRM, 1,240);
2774 
2775 END default_wljdi_fields;
2776 
2777 
2778 -- **********************************************************************************************
2779 -- VALIDATE_WLJDI_PATH: local validation procedure
2780 -- **********************************************************************************************
2781 
2782 -- a PL/SQL table is passed
2783 
2784 PROCEDURE validate_wljdi_path (
2785         p_wip_entity_id     in number,
2786         p_end_op_seq_num    in number,
2787         p_links             in t_links,
2788         x_error_code        out nocopy number,
2789         x_error_msg         out nocopy varchar2) is
2790 
2791 l_xst               number;
2792 l_stmt_num          number;
2793 l_idx               number;
2794 l_to_op             number;
2795 l_fm_op             number;
2796 l_cnt               number := 0;
2797 
2798 e_invalid_link          exception;
2799 e_invalid_path_end      exception;
2800 
2801 BEGIN
2802 
2803     x_error_code := 0;
2804     x_error_msg := '';
2805 
2806 l_stmt_num  := 10;
2807 
2808     l_idx := p_end_op_seq_num;
2809 
2810     if(p_links.count = 0) then
2811         return; -- this means every link is discommended
2812     elsif NOT(p_links.EXISTS(l_idx)) then
2813         raise e_invalid_path_end;
2814     end if;
2815 
2816     while (p_links.EXISTS(l_idx))
2817     loop
2818         l_cnt := l_cnt + 1;
2819 
2820         l_fm_op := p_links(l_idx).from_op_seq_num;
2821         l_to_op := p_links(l_idx).to_op_seq_num;
2822 
2823         -- allow l_fm_op to be null, this is for jumped op
2824         exit when (l_fm_op IS NULL);
2825         BEGIN
2826             select  1
2827             into    l_xst
2828             from    wsm_copy_op_networks
2829             where   wip_entity_id = p_wip_entity_id
2830             and     from_op_seq_num = l_fm_op
2831             and     to_op_seq_num = l_to_op;
2832         EXCEPTION
2833             when others then
2834                 raise e_invalid_link;
2835         END;
2836 
2837         l_idx := l_fm_op;
2838     end loop;
2839 
2840     if(l_cnt <>  p_links.COUNT) then
2841         x_error_code := -1;
2842         fnd_message.set_name('WSM','WSM_INVALID_EXTRA_LINKS');
2843         x_error_msg := fnd_message.get;
2844     end if;
2845 
2846 EXCEPTION
2847     when e_invalid_path_end then
2848         x_error_code := -1;
2849         fnd_message.set_name('WSM','WSM_COPY_NO_PATH_END');
2850         fnd_message.set_token('END',p_end_op_seq_num);
2851         x_error_msg := fnd_message.get;
2852 
2853     when e_invalid_link then
2854         x_error_code := -1;
2855         fnd_message.set_name('WSM','WSM_INVALID_RECO_LINK');
2856         x_error_msg := fnd_message.get;
2857 
2858     when others then
2859         x_error_code := -1;
2860         x_error_msg := 'Error: validate_wljdi_path: (#'|| l_stmt_num||') '
2861                        || SUBSTR(SQLERRM, 1,240);
2862 END validate_wljdi_path;
2863 
2864 
2865 -- **********************************************************************************************
2866 -- VALIDATE_WLJDI_OPERATION: local validation procedure
2867 -- **********************************************************************************************
2868 
2869 PROCEDURE validate_wljdi_operation(
2870         p_wip_entity_id             in number,
2871         p_org_id                    in number,
2872         p_v_ops                     in t_ops,
2873         p_job_scheduled_start_date  in date,
2874         p_job_scheduled_compl_date  in date,
2875         p_job_scheduled_qty         in number,
2876         p_handle_wip_cur_op         in varchar2,
2877         x_error_code                out nocopy number,
2878         x_error_msg                 out nocopy varchar2) is
2879 
2880 l_recommended           varchar2(1);
2881 l_xst                   number;
2882 l_stmt_num              number;
2883 l_i                     number;
2884 
2885 l_rtg_op_seq_num        number;
2886 l_job_op_seq_num        number;
2887 l_scheduled_qty         number;
2888 l_op_yield              number;
2889 l_op_start_date         date;
2890 l_op_compl_date         date;
2891 l_op_start_date_old     date;
2892 l_op_compl_date_old     date;
2893 l_job_start_date        date; -- bug 3423195
2894 
2895 /*Added for bugfix:7248992 */
2896 l_cum_yield             number;
2897 l_rev_cum_yield         number;
2898 
2899 
2900 e_invalid_operation     exception;
2901 e_invalid_sche_qty      exception;
2902 e_invalid_op_yield      exception;
2903 e_invalid_date          exception;
2904 e_sche_qty_not_upd      exception;
2905 /*Added for bugfix:7248992 */
2906 e_invalid_cum_yield      exception;
2907 e_invalid_rev_cum_yield  exception;
2908 
2909 BEGIN
2910     x_error_code := 0;
2911     x_error_msg := '';
2912 
2913     l_i := p_v_ops.first;
2914     if(l_i IS NULL) then return; end if;
2915     l_rtg_op_seq_num := p_v_ops(l_i).rtg_op_seq_num;
2916     l_job_op_seq_num := p_v_ops(l_i).job_op_seq_num;
2917     l_scheduled_qty  := p_v_ops(l_i).scheduled_quantity;
2918     l_op_yield       := p_v_ops(l_i).yield;
2919     l_op_start_date  := p_v_ops(l_i).start_date;
2920     l_op_compl_date  := p_v_ops(l_i).compltion_date;
2921      /*Added for bugfix:7248992 */
2922      l_cum_yield      := p_v_ops(l_i).cumulative_yield;
2923      l_rev_cum_yield  := p_v_ops(l_i).reverse_cumulative_yield;
2924 
2925 l_stmt_num  := 10;
2926     -- do not check p_routing_op_seq_num and p_op_yield for current operation
2927     if(p_handle_wip_cur_op = 'N') then
2928         -- validate operation_seq_num
2929         BEGIN
2930             select  recommended,
2931                     reco_start_date,
2932                     reco_completion_date
2933             into    l_recommended,
2934                     l_op_start_date_old,
2935                     l_op_compl_date_old
2936             from    wsm_copy_operations
2937             where   wip_entity_id = p_wip_entity_id
2938             and     operation_seq_num = l_rtg_op_seq_num;
2939         EXCEPTION
2940             when others then
2941                 raise e_invalid_operation;
2942         END;
2943         -- BD bug 3545531 we will allow user to nullify the dates on WCO
2944         --l_op_start_date := NVL(l_op_start_date, l_op_start_date_old);
2945         --l_op_compl_date := NVL(l_op_compl_date, l_op_compl_date_old);
2946         -- ED bug 3545531
2947 
2948 l_stmt_num  := 20;
2949         -- validate op yield
2950         if l_op_yield IS NOT NULL then
2951             if (l_op_yield <= 0 or l_op_yield > 1) then
2952                 raise e_invalid_op_yield;
2953             end if;
2954         end if;
2955 
2956  /*Added for bugfix:7248992,validate cumulative yield */
2957 	 if l_cum_yield IS NOT NULL then
2958 	    if (l_cum_yield <= 0 or l_cum_yield > 1) then
2959 	       raise e_invalid_cum_yield;
2960 	    end if;
2961 	 end if;
2962 	 /*Added for bugfix:6857061,validate reverse yield */
2963 	 if l_rev_cum_yield IS NOT NULL then
2964 	    if (l_rev_cum_yield <= 0 or l_rev_cum_yield > 1) then
2965 	       raise e_invalid_rev_cum_yield;
2966 	    end if;
2967 	 end if;
2968 
2969 l_stmt_num  := 30;
2970         -- validate op scheduled_quantity
2971         -- 1. if the current op is not on the path, should not allow this change
2972         -- 2. it should be less than job quantity
2973 
2974         if l_recommended <> 'Y' and l_scheduled_qty IS NOT NULL then
2975             raise e_sche_qty_not_upd;
2976         end if;
2977 
2978         if l_scheduled_qty IS NOT NULL then
2979             if l_scheduled_qty <= 0 or
2980                l_scheduled_qty > p_job_scheduled_qty
2981             then
2982                 raise e_invalid_sche_qty;
2983             end if;
2984         end if;
2985 
2986     else    -- p_handle_wip_cur_op = 'Y'
2987         BEGIN
2988 l_stmt_num  := 100;
2989             select  first_unit_start_date,
2990                     last_unit_start_date
2991             into    l_op_start_date_old,
2992                     l_op_compl_date_old
2993             from    wip_operations
2994             where   wip_entity_id = p_wip_entity_id
2995             and     operation_seq_num = l_job_op_seq_num;
2996         EXCEPTION
2997             when others then
2998                 raise e_invalid_operation;
2999         END;
3000         l_op_start_date := NVL(l_op_start_date, l_op_start_date_old);
3001         l_op_compl_date := NVL(l_op_compl_date, l_op_compl_date_old);
3002 
3003         if l_scheduled_qty IS NOT NULL then
3004             if l_scheduled_qty <= 0 or
3005                l_scheduled_qty > p_job_scheduled_qty
3006             then
3007                 raise e_invalid_sche_qty;
3008             end if;
3009         end if;
3010 
3011     end if; -- p_handle_wip_cur_op = 'Y'
3012 
3013 l_stmt_num  := 200;
3014     -- validate op dates, null means 'do not change'
3015     -- BA: bug 3423195
3016     if(g_charges_exist_shopfloor) then
3017         l_job_start_date := g_job_released_date;
3018     else
3019         l_job_start_date := p_job_scheduled_start_date;
3020     end if;
3021     -- EA: bug 3423195
3022 
3023     if(l_op_start_date IS NOT NULL and l_op_compl_date IS NOT NULL) then
3024         --if( NVL(p_job_scheduled_start_date, l_op_start_date) > l_op_start_date or
3025         if( NVL(l_job_start_date, l_op_start_date) > l_op_start_date or
3026             l_op_start_date > l_op_compl_date or
3027             l_op_compl_date > NVL(p_job_scheduled_compl_date, l_op_compl_date))
3028         then
3029             raise e_invalid_date;
3030         end if;
3031     elsif(l_op_start_date IS NOT NULL and l_op_compl_date IS NULL) then
3032         --if( NVL(p_job_scheduled_start_date, l_op_start_date) > l_op_start_date or
3033         if( NVL(l_job_start_date, l_op_start_date) > l_op_start_date or
3034             l_op_start_date > NVL(p_job_scheduled_compl_date, l_op_start_date))
3035         then
3036             raise e_invalid_date;
3037         end if;
3038     elsif(l_op_start_date IS NULL and l_op_compl_date IS NOT NULL) then
3039         --if( NVL(p_job_scheduled_start_date, l_op_compl_date) > l_op_compl_date or
3040         if( NVL(l_job_start_date, l_op_compl_date) > l_op_compl_date or
3041             l_op_compl_date > NVL(p_job_scheduled_compl_date, l_op_compl_date))
3042         then
3043             raise e_invalid_date;
3044         end if;
3045     end if;
3046 
3047 EXCEPTION
3048     when e_invalid_operation then
3049         x_error_code := -1;
3050         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
3051         fnd_message.set_token('FLD_NAME', 'job_op_seq_num or routing_op_seq_num');
3052         x_error_msg := fnd_message.get;
3053 
3054     when e_invalid_op_yield then
3055         x_error_code := -1;
3056         fnd_message.set_name('WSM','WSM_OP_BAD_YIELD');
3057         x_error_msg := fnd_message.get;
3058 
3059     when e_invalid_sche_qty then
3060         x_error_code := -1;
3061         fnd_message.set_name('WSM','WSM_OP_BAD_SCHQTY');
3062         x_error_msg := fnd_message.get;
3063 
3064     when e_sche_qty_not_upd then
3065         x_error_code := -1;
3066         fnd_message.set_name('WSM','WSM_SCHQTY_NOT_UPDATABLE');
3067         x_error_msg := fnd_message.get;
3068 
3069     when e_invalid_date then
3070         x_error_code := -1;
3071         fnd_message.set_name('WSM','WSM_OP_BAD_DATES');
3072         x_error_msg := fnd_message.get;
3073 
3074      /*Added for bugfix:7248992 */
3075      when e_invalid_cum_yield then
3076 	 x_error_code := -1;
3077 	 fnd_message.set_name('WSM','WSM_OP_BAD_CUM_YIELD');
3078 	 x_error_msg := fnd_message.get;
3079 
3080      /*Added for bugfix:7248992 */
3081      when e_invalid_rev_cum_yield then
3082 	 x_error_code := -1;
3083 	 fnd_message.set_name('WSM','WSM_OP_BAD_REV_CUM_YIELD');
3084 	 x_error_msg := fnd_message.get;
3085 
3086     when others then
3087         x_error_code := -1;
3088         x_error_msg := 'Error: validate_wljdi_operation: (#'||l_stmt_num||') '
3089                        || SUBSTR(SQLERRM, 1,240);
3090 END validate_wljdi_operation;
3091 
3092 
3093 
3094 -- **********************************************************************************************
3095 -- VALIDATE_WLJDI_OP_RESOURCE: local validation procedure
3096 -- **********************************************************************************************
3097 
3098 PROCEDURE validate_wljdi_op_resource(
3099         p_wip_entity_id         in number,
3100         p_org_id                in number,
3101         p_v_res                 in out nocopy t_op_res,         -- indexed by resource_id
3102         p_handle_wip_cur_op     in varchar2,
3103         p_substitution_type     in number,
3104         x_error_code            out nocopy number,
3105         x_error_msg             out nocopy varchar2) is
3106 
3107 l_xst                   number;
3108 l_stmt_num              number;
3109 l_i                     number;
3110 l_j                     number;
3111 
3112 l_op_sdate              date;
3113 l_op_edate              date;
3114 l_sdate                 date;
3115 l_edate                 date;
3116 
3117 l_rtg_op_seq_num        number;
3118 l_job_op_seq_num        number;
3119 l_sub_grp_num           number;
3120 l_rpl_grp_num           number;
3121 l_resource_id           number;
3122 
3123 l_v_res_ids             t_number;
3124 e_invalid_resource      exception;
3125 e_invalid_rpl_grp       exception;
3126 e_invalid_date          exception;
3127 
3128 /* ST : Detailed Scheduling */
3129 l_setup_id              number;
3130 l_setup_code            VARCHAR2(10);
3131 l_max_assg_units        number;
3132 l_assg_units            number;
3133 l_batch_id              number;
3134 l_seq_id                number;
3135 l_seq_num               number;
3136 l_firm_type             number;
3137 
3138 e_invalid_seq                   exception;
3139 e_invalid_batch_max_assg_units  exception;
3140 e_invalid_max_assg_units        exception;
3141 e_invalid_setup_max_assg_units  exception;
3142 e_invalid_setup_id              exception;
3143 e_invalid_setup_code            exception;
3144 e_invalid_firm_type             exception;
3145 e_res_not_batchable             exception;
3146 /* ST : Detailed Scheduling */
3147 
3148  ---Added for Alcoa Enhancement
3149 
3150 l_app_res_units         number;
3151 l_res_seq_num           number;
3152 l_uom_code              VARCHAR2(5);
3153 l_uom_exist             number;
3154 l_autocharge_type       number;
3155 l_autocharge_exist      number;
3156 l_res_valid             number;
3157 l_sched_flag            number;
3158 l_sch_flag              number;
3159 l_basis_type            number;
3160 l_activity_id           number;
3161 l_res_code              varchar2(10);
3162 l_res_unit_of_measure     varchar2(3);
3163 l_res_default_basis_type  number;
3164 l_res_default_activity_id number;
3165 l_res_autocharge_type     number;
3166 l_res_standard_rate_flag  number;
3167 l_res_batchable           number;
3168 l_check_primary           number;
3169 e_res_id_null                   exception;
3170 e_assgnd_units_null             exception;
3171 e_invalid_uom                   exception;  ---Need to Verify as this not used
3172 e_invalid_autocharge            exception; ---Need to Verify as this not used
3173 e_invalid_field                 exception;  ---Need to Verify as this not used
3174 e_inv_appl_res_units             exception;
3175 e_wt_or_wcit_rec_exists          exception;
3176 e_po_req_rec_exists              exception;
3177 e_invalid_res_sub_grp_num        exception;
3178 e_invalid_res_reco_flag          exception;
3179 e_inv_res_basis_type            exception;
3180 e_inv_res_charge_type           exception;
3181 e_inv_res_sch_flag              exception;
3182 e_invalid_res_seq_num           exception;
3183 e_invalid_usg_rate              exception;
3184 e_invalid_res_dept              exception;
3185 e_inv_resource_charge_type      exception;
3186 
3187 
3188 cursor c_wsm_rpl_grp is
3189         select   resource_id
3190         from     wsm_copy_op_resources
3191         where    wip_entity_id         = p_wip_entity_id
3192         and      operation_seq_num     = l_rtg_op_seq_num
3193         and      NVL(substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3194         and      NVL(replacement_group_num, 0) = NVL(l_rpl_grp_num, 0)
3195         and      parent_resource_seq_num IS NULL;  -- ST : Detailed Scheduling.. Consider only the runtime resources
3196 
3197 cursor c_wip_rpl_grp is
3198         select   resource_id
3199         from     wip_operation_resources
3200         where    wip_entity_id         = p_wip_entity_id
3201         and      operation_seq_num     = l_job_op_seq_num
3202         and      NVL(substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3203         and      NVL(replacement_group_num, 0) = NVL(l_rpl_grp_num, 0)
3204         and      parent_resource_seq IS NULL -- -- ST : Detailed Scheduling.. Consider only the runtime resources
3205         union
3206         select   resource_id
3207         from     wip_sub_operation_resources
3208         where    wip_entity_id         = p_wip_entity_id
3209         and      operation_seq_num     = l_job_op_seq_num
3210         and      NVL(substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3211         and      NVL(replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
3212 
3213 BEGIN
3214 
3215 
3216     x_error_code := 0;
3217     x_error_msg := '';
3218     l_i := p_v_res.first;
3219 
3220     if(l_i IS NULL) then return; end if;
3221 
3222     l_rtg_op_seq_num := p_v_res(l_i).rtg_op_seq_num;
3223     l_job_op_seq_num := p_v_res(l_i).job_op_seq_num;
3224     l_sub_grp_num    := p_v_res(l_i).sub_grp_num;
3225     l_rpl_grp_num    := p_v_res(l_i).rpl_grp_num;
3226     --Alcoa Enhancement changes
3227     l_app_res_units  := 0;
3228     l_res_seq_num    := p_v_res(l_i).res_seq_num ;
3229 
3230     IF lbji_debug = 'Y' then
3231         fnd_file.put_line(fnd_file.log,'Validate_op_resource : l_rtg_op_seq_num : '|| l_rtg_op_seq_num || ' job op seq num ' || l_job_op_seq_num);
3232     END IF;
3233 
3234     -- get op start/completion date
3235     if( p_handle_wip_cur_op = 'Y') then
3236 l_stmt_num  := 10;
3237         -- validate job operation_seq_num
3238         select  wo.first_unit_start_date,
3239                 wo.last_unit_completion_date
3240         into    l_op_sdate,
3241                 l_op_edate
3242         from    wip_operations wo
3243         where   wo.wip_entity_id     = p_wip_entity_id
3244         and     wo.operation_seq_num = l_job_op_seq_num;
3245     else    -- p_handle_wip_cur_op = 'N'
3246 l_stmt_num  := 20;
3247         select  reco_start_date,
3248                 reco_completion_date
3249         into    l_op_sdate,
3250                 l_op_edate
3251         from    wsm_copy_operations
3252         where   wip_entity_id     = p_wip_entity_id
3253         and     operation_seq_num = l_rtg_op_seq_num;
3254     end if;
3255 
3256     IF lbji_debug = 'Y' then
3257         fnd_file.put_line(fnd_file.log,'Validate_op_resource : ');
3258     END IF;
3259 
3260     if(p_substitution_type = 3) then    -- update only
3261         l_resource_id := p_v_res(l_i).resource_id;
3262 
3263         if( p_handle_wip_cur_op = 'Y') then
3264             BEGIN
3265                 l_xst := 0;
3266 l_stmt_num  := 30;
3267                 select  1
3268                 into    l_xst
3269                 from    dual
3270                 where   exists (
3271                             select resource_id
3272                             from   wip_operation_resources wor
3273                             where  wor.wip_entity_id = p_wip_entity_id
3274                             and    wor.operation_seq_num = l_job_op_seq_num
3275                             and    wor.resource_id = l_resource_id
3276                             and    NVL(wor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3277                             and    NVL(wor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0)
3278                         ) or exists (
3279                             select resource_id
3280                             from   wip_sub_operation_resources wsor
3281                             where  wsor.wip_entity_id = p_wip_entity_id
3282                             and    wsor.operation_seq_num = l_job_op_seq_num
3283                             and    wsor.resource_id = l_resource_id
3284                             and    NVL(wsor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3285                             and    NVL(wsor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0)
3286                         );
3287                 if (l_xst = 0) then
3288                     raise e_invalid_resource;
3289                 end if;
3290             EXCEPTION
3291                 when others then
3292                     raise e_invalid_resource;
3293             END;
3294 
3295         else -- p_handle_wip_cur_op = 'N'
3296 l_stmt_num  := 40;
3297             BEGIN
3298                 l_xst := 0;
3299                 select  1
3300                 into    l_xst
3301                 from    wsm_copy_op_resources wcor
3302                 where   wcor.wip_entity_id        = p_wip_entity_id
3303                 and     wcor.operation_seq_num    = l_rtg_op_seq_num
3304                 and     wcor.resource_id          = l_resource_id
3305                 and     NVL(wcor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3306                 and     NVL(wcor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
3307             EXCEPTION
3308                 when no_data_found then
3309                     raise e_invalid_resource;
3310                 when others then
3311                     null;
3312             END;
3313         end if;
3314 
3315 l_stmt_num  := 50;
3316         -- validate dates
3317         l_sdate := p_v_res(l_i).start_date;
3318         l_edate := p_v_res(l_i).completion_date;
3319 
3320         if(l_sdate IS NOT NULL and l_edate IS NOT NULL) then
3321             if( NVL(l_op_sdate, l_sdate) > l_sdate or
3322                 l_sdate > l_edate or
3323                 l_edate > NVL(l_op_edate, l_edate))
3324             then
3325                 raise e_invalid_date;
3326             end if;
3327         elsif(l_sdate IS NOT NULL and l_edate IS NULL) then
3328             if( NVL(l_op_sdate, l_sdate) > l_sdate or
3329                 l_sdate > NVL(l_op_edate, l_sdate))
3330             then
3331                 raise e_invalid_date;
3332             end if;
3333         elsif(l_sdate IS NULL and l_edate IS NOT NULL) then
3334             if( NVL(l_op_sdate, l_edate) > l_edate or
3335                 l_edate > NVL(l_op_edate, l_edate))
3336             then
3337                 raise e_invalid_date;
3338             end if;
3339         end if;
3340 
3341         /* ST : Detailed Scheduling : Place the validations here.....
3342            Firm_type        Default: Null
3343            Setup_id         Default : Null.
3344 
3345                             Check if there exists any record in bom_resource_setups
3346                             for the setup_id and resource_id.If Setup Id is not null,
3347                             assigned units and Max Assigned units should be 1.
3348 
3349            Batch Id          Default: Null
3350                              If not null, assigned units and Max Assigned units should be 1.
3351 
3352           Sequence Number    Default: Null
3353           Sequence ID
3354                              Is applicable only when setup type is not null.
3355 
3356            Maximum Assigned Units  Default:assigned_units.Max_assigned_units should be greater than or equal to assigned units.
3357         */
3358 
3359         l_setup_id       := p_v_res(l_i).setup_id;
3360         l_setup_code     := p_v_res(l_i).setup_code;
3361         l_max_assg_units := p_v_res(l_i).max_assg_units;
3362         l_batch_id       := p_v_res(l_i).batch_id;
3363         l_seq_id         := p_v_res(l_i).grp_seq_id;
3364         l_seq_num        := p_v_res(l_i).grp_seq_num;
3365         l_assg_units     := p_v_res(l_i).assigned_units;
3366         l_firm_type      := p_v_res(l_i).firm_type;
3367 
3368 		-- bug fix 5471266: Start
3369         -- This allows max_assg_units and firm_type to be passed as null.
3370 
3371                 if l_firm_type is null then
3372                     l_firm_type:=0;
3373                 end if;
3374 
3375                 if l_max_assg_units is null then
3376                     l_max_assg_units := l_assg_units;
3377                 end if;
3378         -- bug fix 5471266: End
3379 
3380 
3381         if nvl(l_firm_type,-1) < 0 or  nvl(l_firm_type,-1)>7 then
3382                 raise e_invalid_firm_type;
3383         end if;
3384         if l_setup_id is NULL and l_setup_code is NOT NULL then
3385              begin
3386                     select  bst.setup_id
3387                     into    l_setup_id
3388                     from    BOM_SETUP_TYPES bst
3389                     where   bst.setup_code      = l_setup_code
3390                     and     bst.organization_id = p_org_id;
3391 
3392              exception
3393                 when no_data_found then
3394                         raise e_invalid_setup_code;
3395                     when others then
3396                         null;
3397              end;
3398         end if;
3399 
3400         if l_setup_id is not null then
3401                 BEGIN
3402 
3403                     l_xst := 0;
3404 
3405                     select  1
3406                     into    l_xst
3407                     from    bom_resource_setups brs
3408                     where   brs.resource_id     = l_resource_id
3409                     and     brs.organization_id = p_org_id
3410                     and     brs.setup_id        = l_setup_id;
3411 
3412                 EXCEPTION
3413                     when no_data_found then
3414                         raise e_invalid_setup_id;
3415                     when others then
3416                         null;
3417                 END;
3418         end if;
3419 
3420         if (l_batch_id is not null ) then
3421 
3422                 /* add a check if the resource is batchable.... */
3423                 begin
3424                     l_xst := 0;
3425 
3426                     select br.batchable
3427                     into l_xst
3428                     from bom_resources br
3429                     where resource_id = l_resource_id;
3430 
3431                     if l_xst <> 1 then
3432                         /* error out... */
3433                         raise e_res_not_batchable;
3434                     end if;
3435 
3436                 exception
3437                     when others then
3438                         /* error out.. */
3439                         raise e_res_not_batchable;
3440                 end;
3441 
3442                 if (l_max_assg_units <> 1 or l_assg_units <> 1) then
3443                     raise e_invalid_batch_max_assg_units;
3444                 end if;
3445         end if;
3446 
3447         if (l_seq_num is not null and l_seq_id is not null) then
3448 
3449                 if (l_setup_id is null)
3450                 then
3451                      raise e_invalid_seq;
3452                 end if;
3453 
3454                 if (l_setup_id is not null)
3455                    and
3456                    (l_max_assg_units <> 1 or l_assg_units <> 1)
3457                 then
3458                      raise e_invalid_setup_max_assg_units;
3459 
3460                 end if;
3461         end if;
3462 
3463         if nvl(l_max_assg_units,0) < l_assg_units then
3464                 raise e_invalid_max_assg_units;
3465         end if;
3466 
3467         /* ST : Detailed Scheduling end */
3468 
3469     elsif(p_substitution_type = 4) then    -- recommand
3470 
3471         if( p_handle_wip_cur_op = 'Y') then
3472 l_stmt_num  := 60;
3473             -- get all the resource_id in the same replacement group
3474             open c_wip_rpl_grp;
3475             fetch c_wip_rpl_grp bulk collect into l_v_res_ids;
3476             if(c_wip_rpl_grp%rowcount <=0) then
3477                 raise e_invalid_resource;
3478             end if;
3479             close c_wip_rpl_grp;
3480         else
3481 l_stmt_num  := 70;
3482             -- get all the resource_id in the same replacement group
3483             open c_wsm_rpl_grp;
3484             fetch c_wsm_rpl_grp bulk collect into l_v_res_ids;
3485             if(c_wsm_rpl_grp%rowcount <=0) then
3486                 raise e_invalid_resource;
3487             end if;
3488             close c_wsm_rpl_grp;
3489         end if;
3490 
3491 l_stmt_num  := 80;
3492         if(l_v_res_ids.COUNT > p_v_res.COUNT) then
3493             raise e_invalid_rpl_grp;
3494         elsif(l_v_res_ids.COUNT < p_v_res.COUNT) then   -- BUG3600450
3495             raise e_invalid_resource;                   -- BUG3600450
3496         end if;
3497 
3498         l_j := l_v_res_ids.first;
3499         while (l_j IS NOT NULL)
3500         loop
3501 l_stmt_num  := 90;
3502             l_resource_id := l_v_res_ids(l_j);
3503             if(NOT p_v_res.EXISTS(l_resource_id)) then
3504                 --raise e_invalid_rpl_grp;              -- BUG3600450
3505                 raise e_invalid_resource;               -- BUG3600450
3506             end if;
3507 
3508 l_stmt_num  := 100;
3509 
3510             -- validate dates
3511             l_sdate := p_v_res(l_resource_id).start_date;
3512             l_edate := p_v_res(l_resource_id).completion_date;
3513 
3514             if(l_sdate IS NOT NULL and l_edate IS NOT NULL) then
3515                 if( NVL(l_op_sdate, l_sdate) > l_sdate or
3516                     l_sdate > l_edate or
3517                     l_edate > NVL(l_op_edate, l_edate))
3518                 then
3519                     raise e_invalid_date;
3520                 end if;
3521             elsif(l_sdate IS NOT NULL and l_edate IS NULL) then
3522                 if( NVL(l_op_sdate, l_sdate) > l_sdate or
3523                     l_sdate > NVL(l_op_edate, l_sdate))
3524                 then
3525                     raise e_invalid_date;
3526                 end if;
3527             elsif(l_sdate IS NULL and l_edate IS NOT NULL) then
3528                 if( NVL(l_op_sdate, l_edate) > l_edate or
3529                     l_edate > NVL(l_op_edate, l_edate))
3530                 then
3531                     raise e_invalid_date;
3532                 end if;
3533             end if;
3534 
3535         l_stmt_num := 110;
3536         /* ST : Detailed Scheduling : Place the validations here.....
3537            Firm_type        Default: Null
3538            Setup_id         Default : Null.
3539 
3540                             Check if there exists any record in bom_resource_setups
3541                             for the setup_id and resource_id.If Setup Id is not null,
3542                             assigned units and Max Assigned units should be 1.
3543 
3544            Batch Id          Default: Null
3545                              If not null, assigned units and Max Assigned units should be 1.
3546 
3547           Sequence Number    Default: Null
3548           Sequence ID
3549                              Is applicable only when setup type is not null.
3550 
3551            Maximum Assigned Units  Default:assigned_units.Max_assigned_units should be greater than or equal to assigned units.
3552         */
3553 
3554 
3555         l_setup_id    := p_v_res(l_i).setup_id;
3556         l_setup_code    := p_v_res(l_i).setup_code;
3557         l_max_assg_units := p_v_res(l_i).max_assg_units;
3558         l_batch_id    := p_v_res(l_i).batch_id;
3559         l_seq_id      := p_v_res(l_i).grp_seq_id;
3560         l_seq_num     := p_v_res(l_i).grp_seq_num;
3561         l_assg_units  := p_v_res(l_i).assigned_units;
3562         l_firm_type      := p_v_res(l_i).firm_type;
3563 
3564         -- bug fix 5471266: Start
3565         -- This allows max_assg_units and firm_type to be passed as null.
3566 
3567                 if l_firm_type is null then
3568                     l_firm_type:=0;
3569                 end if;
3570 
3571                 if l_max_assg_units is null then
3572                     l_max_assg_units := l_assg_units;
3573                 end if;
3574         -- bug fix 5471266: End
3575 
3576 		if nvl(l_firm_type,-1) < 0 or  nvl(l_firm_type,-1)>7 then
3577                 raise e_invalid_firm_type;
3578         end if;
3579 
3580         l_stmt_num := 119.99;
3581          if l_setup_id is NULL and l_setup_code is NOT NULL then
3582              begin
3583                     select  bst.setup_id
3584                     into    l_setup_id
3585                     from    bom_setup_types bst
3586                     where   bst.setup_code      = l_setup_code
3587                     and     bst.organization_id = p_org_id;
3588 
3589              exception
3590                 when no_data_found then
3591                         raise e_invalid_setup_code;
3592                     when others then
3593                         null;
3594              end;
3595         end if;
3596 
3597         l_stmt_num := 120;
3598         if l_setup_id is not null then
3599             BEGIN
3600 
3601                 l_stmt_num := 130;
3602 
3603                 l_xst := 0;
3604 
3605                 select  1
3606                 into    l_xst
3607                 from    bom_resource_setups brs
3608                 where   brs.resource_id     = l_resource_id
3609                 and     brs.organization_id = p_org_id
3610                 and     brs.setup_id        = l_setup_id;
3611 
3612             EXCEPTION
3613                 when no_data_found then
3614                     raise e_invalid_setup_id;
3615                 when others then
3616                     null;
3617             END;
3618        end if;
3619        if (l_batch_id is not null ) then
3620 
3621                 /* add a check if the resource is batchable.... */
3622                 begin
3623                     l_xst := 0;
3624 
3625                     l_stmt_num := 140;
3626 
3627                     select br.batchable
3628                     into l_xst
3629                     from bom_resources br
3630                     where resource_id = l_resource_id;
3631 
3632                     if l_xst <> 1 then
3633                         /* error out... */
3634                         raise e_res_not_batchable;
3635                     end if;
3636 
3637                 exception
3638                     when others then
3639                         /* error out.. */
3640                         raise e_res_not_batchable;
3641                 end;
3642 
3643                 if (l_max_assg_units <> 1 or l_assg_units <> 1) then
3644                     raise e_invalid_batch_max_assg_units;
3645                 end if;
3646 
3647        end if;
3648        l_stmt_num := 150;
3649 
3650        if (l_seq_num is not null and l_seq_id is not null) then
3651 
3652                 if (l_setup_id is null)
3653                 then
3654                      raise e_invalid_seq;
3655                 end if;
3656 
3657                 if (l_setup_id is not null)
3658                    and
3659                    (l_max_assg_units <> 1 or l_assg_units <> 1)
3660                 then
3661                      raise e_invalid_setup_max_assg_units;
3662 
3663                 end if;
3664 
3665         end if;
3666 
3667         if nvl(l_max_assg_units,0) < l_assg_units then
3668                 raise e_invalid_max_assg_units;
3669         end if;
3670         /* ST : Detailed Scheduling end */
3671 
3672         l_j := l_v_res_ids.next(l_j);
3673       end loop;
3674     --Alcoa enahancement Start : Delete
3675     elsif(p_substitution_type = WSM_SUB_DEL) then    -- Delete only
3676         l_resource_id := p_v_res(l_i).resource_id;
3677 
3678         if l_res_seq_num is null then
3679             raise e_invalid_resource;
3680         end if;
3681 
3682         if( p_handle_wip_cur_op = 'Y') THEN
3683 
3684             BEGIN
3685                 l_xst := 0;
3686 
3687                 select 1,applied_resource_units
3688                 into l_xst,l_app_res_units
3689                 from   wip_operation_resources wor
3690                 where  wor.wip_entity_id = p_wip_entity_id
3691                 and    wor.operation_seq_num = l_job_op_seq_num
3692                 and    wor.resource_seq_num = l_res_seq_num;
3693 
3694                 if (l_xst=0) then
3695                     select 1,applied_resource_units
3696                     into l_xst,l_app_res_units
3697                     from   wip_sub_operation_resources wsor
3698                     where  wsor.wip_entity_id = p_wip_entity_id
3699                     and    wsor.operation_seq_num = l_job_op_seq_num
3700                     and    wsor.resource_seq_num = l_res_seq_num;
3701                 end if;
3702                 if (l_xst = 0) then
3703                     raise e_invalid_resource;
3704                 end if;
3705 
3706             EXCEPTION
3707                 when others then
3708                     raise e_invalid_resource;
3709             END;
3710 
3711             IF l_app_res_units <> 0 THEN
3712                raise e_inv_appl_res_units ;
3713             END IF  ;
3714 
3715             BEGIN -- Check whether there are any records in WT,WCTI
3716 
3717                 l_xst := 0;
3718 
3719                 select  1
3720                 into    l_xst
3721                 from    dual
3722                 where   exists ( select transaction_id
3723                                  from   wip_transactions wt
3724                                  where  wt.wip_entity_id      = p_wip_entity_id
3725                                  and    wt.organization_id    = p_org_id
3726                                  AND    wt.operation_seq_num  = l_job_op_seq_num
3727                                  and    wt.resource_seq_num   = l_res_seq_num
3728                                )
3729                      or exists ( select transaction_id
3730                                  from  wip_cost_txn_interface wcti
3731                                  where wcti.wip_entity_id     = p_wip_entity_id
3732                                  AND   wcti.organization_id   = p_org_id
3733                                  and   wcti.operation_seq_num = l_job_op_seq_num
3734                                  and   wcti.resource_seq_num  = l_res_seq_num
3735                         );
3736 
3737                 if (l_xst = 1 ) then
3738                     raise e_wt_or_wcit_rec_exists;
3739                 end if;
3740             EXCEPTION
3741                 when no_data_found then
3742                   null;
3743                 when others then
3744                     raise e_wt_or_wcit_rec_exists;
3745             END;
3746 
3747             BEGIN -- Check whether there are any PO/REQ Linked transactions
3748 
3749                 l_xst := 0;
3750 
3751                 select  1
3752                 into    l_xst
3753                 from    dual
3754                 where   exists ( select 'PO/REQ Linked'
3755                                  from    po_releases_all pr,
3756                                          po_headers_all ph,
3757                                          po_distributions_all pd,
3758                                          po_line_locations_all pll
3759                                  where  pd.po_line_id IS NOT NULL
3760                                  and    pd.line_location_id IS NOT NULL
3761                                  and    pd.wip_entity_id = p_wip_entity_id
3762                                  and    pd.destination_organization_id = p_org_id
3763                                  and    pd.wip_operation_seq_num = l_job_op_seq_num
3764                                  and    pd.wip_resource_seq_num = l_res_seq_num
3765                                  and    ph.po_header_id         = pd.po_header_id
3766                                  and    pll.line_location_id    = pd.line_location_id
3767                                  and    pr.po_release_id (+)    = pd.po_release_id
3768                                  and  ( pll.cancel_flag IS NULL OR
3769                                           pll.cancel_flag = 'N')
3770                                  and (pll.quantity_received <   (pll.quantity-pll.quantity_cancelled))
3771                                  and nvl(pll.closed_code,'OPEN') <> 'FINALLY CLOSED'
3772                                  UNION ALL
3773                                  select 'PO/REQ Linked'
3774                                  from   po_requisition_lines_all prl
3775                                  where  prl.wip_entity_id = p_wip_entity_id
3776                                  and    prl.destination_organization_id = p_org_id
3777                                  and    prl.wip_operation_seq_num = l_job_op_seq_num
3778                                  and    prl.wip_resource_seq_num  = l_res_seq_num
3779                                  and    nvl(PRL.cancel_flag, 'N') = 'N'
3780                                  and    prl.line_location_id is NULL
3781                                  UNION ALL
3782                                  select 'PO/REQ Linked'
3783                                  from   po_requisitions_interface_all pri
3784                                  where  pri.wip_entity_id = p_wip_entity_id
3785                                  and    pri.destination_organization_id = p_org_id
3786                                  and    pri.wip_operation_seq_num = l_job_op_seq_num
3787                                  and pri.wip_resource_seq_num = l_res_seq_num
3788                                );
3789 
3790                 if (l_xst = 1) then
3791                     raise e_po_req_rec_exists;
3792                 end if;
3793 
3794             EXCEPTION
3795                 when no_data_found then
3796                     null;
3797                 when others then
3798                     raise e_po_req_rec_exists;
3799             END;
3800 
3801         else -- p_handle_wip_cur_op = 'N'
3802 
3803             BEGIN
3804                 l_xst := 0;
3805                 select  1
3806                 into    l_xst
3807                 from    wsm_copy_op_resources wcor
3808                 where   wcor.wip_entity_id        = p_wip_entity_id
3809                 and     wcor.operation_seq_num    = l_rtg_op_seq_num
3810                 and     wcor.resource_seq_num = l_res_seq_num;
3811             EXCEPTION
3812                 when no_data_found then
3813                     raise e_invalid_resource;
3814                 when others then
3815                     null;
3816             END;
3817         end if;
3818         --Alcoa enahancement End : Delete
3819 
3820     elsif (p_substitution_type = WSM_SUB_ADD) then
3821 
3822         l_resource_id    := p_v_res(l_i).resource_id;
3823         l_assg_units     := p_v_res(l_i).assigned_units;
3824         l_setup_id       := p_v_res(l_i).setup_id;
3825         l_setup_code     := p_v_res(l_i).setup_code;
3826         l_max_assg_units := p_v_res(l_i).max_assg_units;
3827         l_batch_id       := p_v_res(l_i).batch_id;
3828         l_seq_id         := p_v_res(l_i).grp_seq_id;
3829         l_seq_num        := p_v_res(l_i).grp_seq_num;
3830         l_assg_units     := p_v_res(l_i).assigned_units;
3831         l_sch_flag       := p_v_res(l_i).scheduled_flag;
3832         l_basis_type     := p_v_res(l_i).basis_type;
3833         l_activity_id    := p_v_res(l_i).activity_id;
3834         l_firm_type      := p_v_res(l_i).firm_type;
3835 
3836         --Validating the Resource
3837         if l_resource_id  is not null then
3838             begin
3839                 select  unit_of_measure,
3840                         default_basis_type,
3841                         default_activity_id,
3842                         autocharge_type,
3843                         standard_rate_flag,
3844                         batchable,
3845                         resource_code
3846                 into    l_res_unit_of_measure,
3847                         l_res_default_basis_type,
3848                         l_res_default_activity_id,
3849                         l_res_autocharge_type,
3850                         l_res_standard_rate_flag,
3851                         l_res_batchable,
3852                         l_res_code
3853                 from    bom_resources
3854                 where   resource_id=l_resource_id
3855                 and     organization_id=p_org_id
3856                 and     nvl(disable_date,sysdate+1) > sysdate;
3857             exception
3858                 when others then
3859                     raise e_invalid_resource;
3860             end;
3861         else
3862             raise e_res_id_null;
3863         end if ;
3864 
3865         if l_activity_id is null then
3866             l_activity_id := l_res_default_activity_id;
3867         end if;
3868 
3869         if l_basis_type is null then
3870             l_basis_type := l_res_default_basis_type;
3871         elsif l_basis_type not in (WIP_CONSTANTS.PER_ITEM,
3872                                    WIP_CONSTANTS.PER_LOT) then
3873             raise e_inv_res_basis_type;
3874         end if;
3875 
3876         if l_res_autocharge_type = WIP_CONSTANTS.PO_MOVE then
3877             raise e_inv_res_charge_type;
3878         end if;
3879 
3880         if l_res_autocharge_type is null then
3881             raise e_inv_resource_charge_type;
3882         end if;
3883 
3884         if l_sch_flag is null then
3885             l_sch_flag := WIP_CONSTANTS.SCHED_NO;
3886         elsif l_sch_flag not in (WIP_CONSTANTS.SCHED_YES,
3887                                  WIP_CONSTANTS.SCHED_NO,
3888                                  WIP_CONSTANTS.SCHED_PRIOR,
3889                                  WIP_CONSTANTS.SCHED_NEXT) then
3890             raise e_inv_res_sch_flag;
3891         end if;
3892 
3893         if l_assg_units <=0 or l_assg_units is null then
3894                 raise e_assgnd_units_null;
3895         end if;
3896 
3897         if p_v_res(l_i).usage_rate_or_amount <=0 or p_v_res(l_i).usage_rate_or_amount is null then
3898             raise e_invalid_usg_rate;
3899         end if;
3900 
3901         if l_firm_type is null then
3902             l_firm_type:=0;
3903         end if;
3904 
3905         if l_max_assg_units is null then
3906             l_max_assg_units := l_assg_units;
3907         end if;
3908 
3909         if l_firm_type < 0 or l_firm_type > 7 then
3910             raise e_invalid_firm_type;
3911         end if;
3912 
3913         -- ignore setup_id/setup_code for runtime resource.
3914         if l_setup_id is not null and l_setup_code is not null then
3915             l_setup_id   := null;
3916             l_setup_code := null;
3917         end if;
3918 
3919         if l_batch_id is not null then
3920             if l_res_batchable <> 1 then
3921                 raise e_res_not_batchable;
3922             end if;
3923             if (l_max_assg_units <> 1 or l_assg_units <> 1) then
3924                     raise e_invalid_batch_max_assg_units;
3925             end if;
3926         end if;
3927 
3928         if l_seq_num is not null and l_seq_id is not null then
3929             raise e_invalid_seq;
3930         end if;
3931 
3932         if l_max_assg_units < l_assg_units then
3933                 raise e_invalid_max_assg_units;
3934         end if;
3935 
3936         l_sdate := p_v_res(l_i).start_date;
3937         l_edate := p_v_res(l_i).completion_date;
3938         l_op_sdate := null;
3939         l_op_edate := null;
3940         -- get op start/completion date
3941         if p_handle_wip_cur_op = 'Y' then
3942 l_stmt_num  := 10;
3943 
3944             begin
3945                 select  wo.first_unit_start_date,
3946                         wo.last_unit_completion_date
3947                 into    l_op_sdate,
3948                         l_op_edate
3949                 from    wip_operations wo,
3950                         bom_department_resources bdr
3951                 where   wo.wip_entity_id     = p_wip_entity_id
3952                 and     wo.operation_seq_num = l_job_op_seq_num
3953                 and     wo.organization_id   = p_org_id
3954                 and     bdr.resource_id      = l_resource_id
3955                 and     bdr.department_id    = wo.department_id;
3956             exception
3957                 when others then
3958                     raise e_invalid_res_dept;
3959             end;
3960             if l_res_seq_num is null then
3961                 select greatest(nvl(max(wor.resource_seq_num),0),nvl(max(wsor.resource_seq_num),0))+10
3962                 into   l_res_seq_num
3963                 from   wip_operation_resources wor,
3964                        wip_sub_operation_resources wsor
3965                 where  wor.organization_id = p_org_id
3966                 and    wor.wip_entity_id   = p_wip_entity_id
3967                 and    wor.operation_seq_num   = l_job_op_seq_num
3968                 and    wor.organization_id = wsor.organization_id
3969                 and    wor.wip_entity_id   = wsor.wip_entity_id
3970                 and    wor.operation_seq_num   = wsor.operation_seq_num;
3971             else
3972                 l_xst := 0;
3973                 begin
3974                     select 1
3975                     into   l_xst
3976                     from   wip_operation_resources wor
3977                     where  wor.organization_id = p_org_id
3978                     and    wor.wip_entity_id   = p_wip_entity_id
3979                     and    wor.operation_seq_num   = l_job_op_seq_num
3980                     and    wor.resource_seq_num  =l_res_seq_num;
3981                 exception
3982                     when no_data_found then
3983                         null;
3984                     when others then
3985                         raise e_invalid_res_seq_num;
3986                 end;
3987                 if l_xst=1 then
3988                     raise e_invalid_res_seq_num;
3989                 end if;
3990             end if;
3991 
3992             -- resource in the same sub/repl group should be recommended/decommended together.
3993             l_check_primary:=0;
3994             begin
3995                 select 'N'
3996                 into p_v_res(l_i).reco_flag
3997                 from dual
3998                 where exists( select 1
3999                               from wip_sub_operation_resources wsor
4000                               where wsor.organization_id   = p_org_id
4001                               and   wsor.wip_entity_id     = p_wip_entity_id
4002                               and   wsor.operation_seq_num = l_job_op_seq_num
4003                               and   nvl(wsor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4004                               and   nvl(wsor.replacement_group_num,0)  = nvl(p_v_res(l_i).rpl_grp_num,0));
4005             exception
4006                 when no_data_found then
4007                     l_check_primary := 1;
4008                 when others then
4009                     raise e_invalid_res_sub_grp_num;
4010             end;
4011             if l_check_primary=1 then
4012                 begin
4013                     select 'Y'
4014                     into p_v_res(l_i).reco_flag
4015                     from dual
4016                     where exists( select 1
4017                                   from wip_operation_resources wor
4018                                   where wor.organization_id   = p_org_id
4019                                   and   wor.wip_entity_id     = p_wip_entity_id
4020                                   and   wor.operation_seq_num = l_job_op_seq_num
4021                                   and   nvl(wor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4022                                   and   nvl(wor.replacement_group_num,0)  = nvl(p_v_res(l_i).rpl_grp_num,0));
4023                 exception
4024                     when no_data_found then
4025                         l_check_primary := 2;
4026                     when others then
4027                         raise e_invalid_res_sub_grp_num;
4028                 end;
4029             end if;
4030             if l_check_primary=2 then
4031                 begin
4032                     select 'N'
4033                     into p_v_res(l_i).reco_flag
4034                     from dual
4035                     where exists( select 1
4036                                   from wip_operation_resources wor
4037                                   where wor.organization_id   = p_org_id
4038                                   and   wor.wip_entity_id     = p_wip_entity_id
4039                                   and   wor.operation_seq_num = l_job_op_seq_num
4040                                   and   nvl(wor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1));
4041                 exception
4042                     when no_data_found then
4043                         p_v_res(l_i).reco_flag := 'Y';
4044                     when others then
4045                         raise e_invalid_res_sub_grp_num;
4046                 end;
4047             end if;
4048             if p_v_res(l_i).reco_flag ='N' then
4049                 if p_v_res(l_i).sub_grp_num is null then
4050                     raise e_invalid_res_sub_grp_num;
4051                 end if;
4052             end if;
4053 
4054         else    -- p_handle_wip_cur_op = 'N'
4055 l_stmt_num  := 20;
4056            begin
4057 
4058                select  reco_start_date,
4059                        reco_completion_date
4060                into    l_op_sdate,
4061                        l_op_edate
4062                from    wsm_copy_operations wco,
4063                        bom_department_resources bdr
4064                where   wip_entity_id     = p_wip_entity_id
4065                and     operation_seq_num = l_rtg_op_seq_num
4066                and     organization_id   = p_org_id
4067                and     bdr.resource_id      = l_resource_id
4068                and     bdr.department_id    =wco.department_id;
4069 
4070            exception
4071                 when others then
4072                     raise e_invalid_res_dept;
4073             end;
4074 
4075            if l_res_seq_num is null then
4076                 select nvl(max(resource_seq_num),0)+10
4077                 into   l_res_seq_num
4078                 from   wsm_copy_op_resources wor
4079                 where  wor.organization_id = p_org_id
4080                 and    wor.wip_entity_id   = p_wip_entity_id
4081                 and    wor.operation_seq_num   = l_rtg_op_seq_num;
4082             else
4083                 l_xst := 0;
4084                 begin
4085                     select 1
4086                     into   l_xst
4087                     from   wsm_copy_op_resources wor
4088                     where  wor.organization_id = p_org_id
4089                     and    wor.wip_entity_id   = p_wip_entity_id
4090                     and    wor.operation_seq_num   = l_rtg_op_seq_num
4091                     and    wor.resource_seq_num  =l_res_seq_num;
4092                 exception
4093                     when no_data_found then
4094                         null;
4095                     when others then
4096                         raise e_invalid_res_seq_num;
4097                 end;
4098                 if l_xst=1 then
4099                     raise e_invalid_res_seq_num;
4100                 end if;
4101 
4102             end if;
4103 
4104             -- resource in the same sub/repl group should be recommended/decommended together.
4105             l_check_primary:=0;
4106             begin
4107                 select 'N'
4108                 into p_v_res(l_i).reco_flag
4109                 from dual
4110                 where exists( select 1
4111                               from wsm_copy_op_resources wcor
4112                               where wcor.organization_id   = p_org_id
4113                               and   wcor.wip_entity_id     = p_wip_entity_id
4114                               and   wcor.operation_seq_num = l_rtg_op_seq_num
4115                               and   nvl(wcor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4116                               and   nvl(wcor.replacement_group_num,0)  = nvl(p_v_res(l_i).rpl_grp_num,0)
4117                               and   wcor.recommended           = 'N');
4118             exception
4119                 when no_data_found then
4120                     l_check_primary := 1;
4121                 when others then
4122                                                                                                                                                                 raise e_invalid_res_sub_grp_num;
4123             end;
4124 
4125             if l_check_primary=1 then
4126                 begin
4127                     select 'Y'
4128                     into p_v_res(l_i).reco_flag
4129                     from dual
4130                     where exists( select 1
4131                                   from wsm_copy_op_resources wcor
4132                                   where wcor.organization_id   = p_org_id
4133                                   and   wcor.wip_entity_id     = p_wip_entity_id
4134                                   and   wcor.operation_seq_num = l_rtg_op_seq_num
4135                                   and   nvl(wcor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4136                                   and   nvl(wcor.replacement_group_num,0)  = nvl(p_v_res(l_i).rpl_grp_num,0)
4137                                   and   wcor.recommended           = 'Y');
4138                 exception
4139                     when no_data_found then
4140                         l_check_primary := 2;
4141                     when others then
4142                                                                                                                                                                                                 raise e_invalid_res_sub_grp_num;
4143                 end;
4144             end if;
4145             if l_check_primary=2 then
4146                 begin
4147                     select 'N'
4148                     into p_v_res(l_i).reco_flag
4149                     from dual
4150                     where exists( select 1
4151                                   from wsm_copy_op_resources wcor
4152                                   where wcor.organization_id   = p_org_id
4153                                   and   wcor.wip_entity_id     = p_wip_entity_id
4154                                   and   wcor.operation_seq_num = l_rtg_op_seq_num
4155                                   and   nvl(wcor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4156                                   and   wcor.recommended           = 'Y');
4157                 exception
4158                     when no_data_found then
4159                         p_v_res(l_i).reco_flag := 'Y';
4160                     when others then
4161                                                                                                                                                                                                 raise e_invalid_res_sub_grp_num;
4162                 end;
4163             end if;
4164             if p_v_res(l_i).reco_flag ='N' then
4165                 if p_v_res(l_i).sub_grp_num is null then
4166                     raise e_invalid_res_sub_grp_num;
4167                 end if;
4168             end if;
4169         end if;
4170 
4171         if(l_sdate IS NOT NULL and l_edate IS NOT NULL) then
4172             if( NVL(l_op_sdate, l_sdate) > l_sdate or
4173                 l_sdate > l_edate or
4174                 l_edate > NVL(l_op_edate, l_edate))
4175             then
4176                 raise e_invalid_date;
4177             end if;
4178         elsif(l_sdate IS NOT NULL and l_edate IS NULL) then
4179             if( NVL(l_op_sdate, l_sdate) > l_sdate or
4180                 l_sdate > NVL(l_op_edate, l_sdate))
4181             then
4182                 raise e_invalid_date;
4183             end if;
4184         elsif(l_sdate IS NULL and l_edate IS NOT NULL) then
4185             if( NVL(l_op_sdate, l_edate) > l_edate or
4186                 l_edate > NVL(l_op_edate, l_edate))
4187             then
4188                 raise e_invalid_date;
4189             end if;
4190         end if;
4191 
4192         if l_sdate is null then
4193             l_sdate := l_op_sdate;
4194         end if;
4195 
4196         if l_edate is null then
4197             l_edate := l_op_edate;
4198         end if;
4199 
4200         if p_v_res(l_i).rpl_grp_num is null then
4201             p_v_res(l_i).rpl_grp_num     := 0;
4202         end if;
4203 
4204         p_v_res(l_i).res_seq_num     := l_res_seq_num;
4205         p_v_res(l_i).autocharge_type := l_res_autocharge_type;
4206         p_v_res(l_i).standard_rate   := nvl(l_res_standard_rate_flag,1);
4207         p_v_res(l_i).activity_id     := l_activity_id;
4208         p_v_res(l_i).basis_type      := l_basis_type ;
4209         p_v_res(l_i).uom_code        := l_res_unit_of_measure;
4210         p_v_res(l_i).scheduled_flag  := l_sch_flag;
4211         p_v_res(l_i).max_assg_units  := l_max_assg_units;
4212         p_v_res(l_i).firm_type       := l_firm_type;
4213         p_v_res(l_i).setup_id        := l_setup_id;
4214         p_v_res(l_i).setup_code      := l_setup_code;
4215         p_v_res(l_i).start_date      := l_sdate;
4216         p_v_res(l_i).completion_date := l_edate;
4217         p_v_res(l_i).resource_code   := l_res_code;
4218 
4219     end if; -- p_substitution_type = WSM_SUB_ADD
4220 
4221 EXCEPTION
4222     when e_invalid_resource then
4223         x_error_code := -1;
4224         fnd_message.set_name('WSM','WSM_RES_NOT_REQUIRED');
4225         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' || fnd_message.get;
4226         fnd_file.put_line(fnd_file.log,x_error_msg );
4227 
4228     when e_invalid_rpl_grp then
4229         x_error_code := -1;
4230         fnd_message.set_name('WSM','WSM_RES_PARTIAL_RPLGRP');
4231         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' || fnd_message.get;
4232         fnd_file.put_line(fnd_file.log,x_error_msg );
4233 
4234     when e_invalid_date then
4235         x_error_code := -1;
4236         fnd_message.set_name('WSM','WSM_RES_BAD_DATES');
4237         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4238         fnd_file.put_line(fnd_file.log,x_error_msg );
4239 
4240    /* ST : Detailed Scheduling start */
4241    when e_invalid_max_assg_units then
4242        x_error_code := -1;
4243        fnd_message.set_name('WSM','WSM_INVALID_MAX_ASSG');
4244        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4245        fnd_file.put_line(fnd_file.log,x_error_msg );
4246 
4247    when e_invalid_setup_max_assg_units then
4248        x_error_code := -1;
4249        fnd_message.set_name('WSM','WSM_INVALID_SETUP_MAX_ASSG');
4250        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' || fnd_message.get;
4251        fnd_file.put_line(fnd_file.log,x_error_msg );
4252 
4253    when e_invalid_seq then
4254        x_error_code := -1;
4255        fnd_message.set_name('WSM','WSM_INVALID_SEQ');  /* group seq nyum and group seq id... */
4256        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' || fnd_message.get;
4257        fnd_file.put_line(fnd_file.log,x_error_msg );
4258 
4259    when e_invalid_batch_max_assg_units then
4260        x_error_code := -1;
4261        fnd_message.set_name('WSM','WSM_INVALID_BATCH_MAX_ASSG');
4262        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4263        fnd_file.put_line(fnd_file.log,x_error_msg );
4264 
4265    when e_invalid_setup_id then
4266        x_error_code := -1;
4267        --fnd_message.set_name('WSM','WSM_INVALID_SETUP');
4268        fnd_message.set_name('WSM','WSM_INVALID_FIELD'); /*Bug 5488082*/
4269        fnd_message.set_token('FLD_NAME', ' setup_id ');
4270        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4271        fnd_file.put_line(fnd_file.log,x_error_msg );
4272 
4273    when e_invalid_firm_type then
4274         x_error_code := -1;
4275         /* add the error message..... */
4276         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4277         fnd_message.set_token('FLD_NAME', ' firm type ');
4278         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4279         fnd_file.put_line(fnd_file.log,x_error_msg );
4280 
4281    when e_res_not_batchable then
4282        x_error_code := -1;
4283        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4284        --fnd_message.set_token('FLD_NAME', ' batch id for resource');
4285        fnd_message.set_token('FLD_NAME', ' batch_id'); /*Bug 5487540 */
4286        --x_error_msg := fnd_message.get;
4287        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4288        fnd_file.put_line(fnd_file.log,x_error_msg );
4289 
4290    when e_invalid_setup_code then
4291             x_error_code := -1;
4292             /* add the error message..... */
4293             fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4294             fnd_message.set_token('FLD_NAME', 'Setup Code ');
4295             x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4296             fnd_file.put_line(fnd_file.log,x_error_msg );
4297 
4298    /* ST : Detailed Scheduling end */
4299 
4300    --Alcoa Enhancements Changes
4301    when e_res_id_null THEN
4302         x_error_code := -1;
4303         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4304         fnd_message.set_token('FLD_NAME', 'Resource_code/ Resource_id combination');
4305         x_error_msg := fnd_message.get;
4306 
4307    when e_assgnd_units_null then
4308        x_error_code := -1;
4309        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4310        fnd_message.set_token('FLD_NAME', 'Assigned_units');
4311        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4312        fnd_file.put_line(fnd_file.log,x_error_msg );
4313 
4314    when e_inv_appl_res_units THEN
4315         x_error_code := -1;
4316         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4317         fnd_message.set_token('FLD_NAME', 'Applied_units');
4318         x_error_msg := fnd_message.get;
4319 
4320      when e_wt_or_wcit_rec_exists THEN
4321         x_error_code := -1;
4322         fnd_message.set_name('WSM','WSM_WT_OR_WCIT_REC_EXIS');
4323         x_error_msg := fnd_message.get;
4324 
4325      when e_po_req_rec_exists THEN
4326         x_error_code := -1;
4327         fnd_message.set_name('WSM','WSM_PO_REC_REC_EXIS');
4328         x_error_msg := fnd_message.get;
4329 
4330    when e_invalid_res_sub_grp_num then
4331        x_error_code := -1;
4332        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4333        fnd_message.set_token('FLD_NAME', 'substitution_group_num');
4334        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4335        fnd_file.put_line(fnd_file.log,x_error_msg );
4336 
4337    when e_invalid_res_reco_flag then
4338        x_error_code := -1;
4339        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4340        fnd_message.set_token('FLD_NAME', 'recommended_flag');
4341        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4342        fnd_file.put_line(fnd_file.log,x_error_msg );
4343 
4344    when e_inv_res_basis_type then
4345        x_error_code := -1;
4346        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4347        fnd_message.set_token('FLD_NAME', 'basis_type');
4348        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4349        fnd_file.put_line(fnd_file.log,x_error_msg );
4350 
4351    when e_inv_res_charge_type then
4352        x_error_code := -1;
4353        fnd_message.set_name('WSM','WSM_CURR_OP_RES_PO_MOVE');
4354        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4355        fnd_file.put_line(fnd_file.log,x_error_msg );
4356 
4357    when e_inv_res_sch_flag then
4358        x_error_code := -1;
4359        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4360        fnd_message.set_token('FLD_NAME', 'scheduled_flag');
4361        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4362        fnd_file.put_line(fnd_file.log,x_error_msg );
4363 
4364     when e_invalid_res_seq_num then
4365         x_error_code := -1;
4366         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4367         fnd_message.set_token('FLD_NAME', 'resource_seq_num');
4368         x_error_msg := fnd_message.get;
4369         fnd_file.put_line(fnd_file.log,x_error_msg );
4370 
4371    when e_invalid_usg_rate then
4372        x_error_code := -1;
4373        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4374        fnd_message.set_token('FLD_NAME', 'usage_rate_or_amount');
4375        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4376        fnd_file.put_line(fnd_file.log,x_error_msg );
4377 
4378      when e_invalid_res_dept THEN
4379         x_error_code := -1;
4380         fnd_message.set_name('WSM','WSM_INVALID_RES_DEPT');
4381         x_error_msg := fnd_message.get;
4382 
4383    when e_inv_resource_charge_type then
4384        x_error_code := -1;
4385        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4386        fnd_message.set_token('FLD_NAME', 'Autocharge_type');
4387        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4388        fnd_file.put_line(fnd_file.log,x_error_msg );
4389 
4390     when others then
4391         x_error_code := -1;
4392         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') '
4393                        || SUBSTR(SQLERRM, 1,240);
4394 
4395         fnd_file.put_line(fnd_file.log,x_error_msg );
4396 
4397 END validate_wljdi_op_resource;
4398 
4399 
4400 -- **********************************************************************************************
4401 -- VALIDATE_WLJDI_RES_INSTANCE: local validation procedure
4402 -- **********************************************************************************************
4403 
4404 PROCEDURE validate_wljdi_res_instance(
4405         p_wip_entity_id         in number,
4406         p_org_id                in number,
4407         p_v_res_ins             in t_res_ins,
4408         p_substitution_type     in number,
4409         p_handle_wip_cur_op     in varchar2,
4410         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
4411         -- In case of instance information being passed for setup resource that is added in the same run
4412         -- return the resurce seq num of the setup resource...
4413         p_parent_res_seq_num    IN NUMBER,
4414         p_resource_id           IN NUMBER,
4415         x_setup_res_seq_num     OUT nocopy number,
4416         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
4417         x_error_code            out nocopy number,
4418         x_error_msg             out nocopy varchar2) is
4419 
4420 l_stmt_num              number;
4421 l_i                     number;
4422 l_j                     number;
4423 l_exists                number;
4424 
4425 l_op_recommended        varchar2(1) := 'N';
4426 l_res_recommended       varchar2(1) := 'N';
4427 l_sdate_match           number;
4428 l_edate_match           number;
4429 l_res_sdate             date;
4430 l_res_edate             date;
4431 l_sdate                 date;
4432 l_edate                 date;
4433 l_instance_id           number;
4434 l_serial_number         varchar2(30);
4435 l_batch_id              number;
4436 
4437 l_rtg_op_seq_num        number;
4438 l_job_op_seq_num        number;
4439 l_res_seq_num           number;
4440 l_resource_id           number;
4441 l_department_id         number;  /*Added Department Id for bug 5476647*/
4442 
4443 e_null_date             exception;
4444 e_invalid_date          exception;
4445 e_null_ins_id           exception;
4446 e_invalid_ins_id        exception;
4447 e_invalid_res_seq_num   exception;
4448 e_not_on_reco_path      exception;
4449 e_res_not_reco          exception;
4450 e_invalid_rtg_op_seq    exception;
4451 e_invalid_time_slot     exception;
4452 e_ins_not_exists        exception;
4453 e_no_res_seq_num    exception;
4454 
4455 BEGIN
4456 
4457 
4458     x_error_code := 0;
4459     x_error_msg := '';
4460     l_i := p_v_res_ins.first;
4461 
4462     IF lbji_debug = 'Y' then
4463         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance start ...');
4464     END IF;
4465 
4466 
4467     if(l_i IS NULL) then return; end if;
4468 
4469     -- in p_v_res_use, all records should have the same res_seq_num
4470     l_rtg_op_seq_num  := p_v_res_ins(l_i).rtg_op_seq_num;
4471     l_job_op_seq_num  := p_v_res_ins(l_i).job_op_seq_num;
4472     l_res_seq_num     := p_v_res_ins(l_i).res_seq_num;
4473 
4474     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
4475     IF l_res_seq_num IS NULL THEN
4476         IF lbji_debug = 'Y' then
4477                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : Setup resopurce : '
4478                              || ' Parent_res_seq_num : ' || p_parent_res_seq_num
4479                              || ' Parent res ID  : ' ||  p_resource_id
4480                              || ' l_rtg_op_seq_num : ' || l_rtg_op_seq_num);
4481         END IF;
4482 
4483         -- call is for a setup resource...
4484         IF p_handle_wip_cur_op = 'N' THEN
4485                 l_stmt_num  := 5;
4486                 BEGIN
4487                     select resource_seq_num
4488                     into l_res_seq_num
4489                     from wsm_copy_op_resources WCOR
4490                     where WCOR.wip_entity_id     = p_wip_entity_id
4491                     and   WCOR.organization_id   = p_org_id
4492                     and   WCOR.operation_seq_num = l_rtg_op_seq_num
4493                     and   WCOR.parent_resource_seq_num = p_parent_res_seq_num
4494                     and   WCOR.resource_id        = p_resource_id;
4495 
4496                 EXCEPTION
4497                     WHEN NO_DATA_FOUND THEN
4498                          IF lbji_debug = 'Y' then
4499                                 fnd_file.put_line(fnd_file.log,' No Resource...');
4500                          END IF;
4501                          raise e_no_res_seq_num;
4502                     WHEN TOO_MANY_ROWS THEN
4503                         IF lbji_debug = 'Y' then
4504                                 fnd_file.put_line(fnd_file.log,' No Unique Resource...');
4505                         END IF;
4506                         raise e_no_res_seq_num;
4507                 END;
4508         ELSE
4509                 l_stmt_num  := 6;
4510 
4511                 BEGIN
4512                     select resource_seq_num
4513                     into l_res_seq_num
4514                     from  wip_operation_resources WOR
4515                     where WOR.wip_entity_id      = p_wip_entity_id
4516                     and   WOR.organization_id    = p_org_id
4517                     and   WOR.operation_seq_num  = l_job_op_seq_num
4518                     and   WOR.parent_resource_seq = p_parent_res_seq_num
4519                     and   WOR.resource_id         = p_resource_id;
4520 
4521                 EXCEPTION
4522                     WHEN NO_DATA_FOUND THEN
4523                         raise e_no_res_seq_num;
4524                     WHEN TOO_MANY_ROWS THEN
4525                         raise e_no_res_seq_num;
4526                 END;
4527         END IF;
4528 
4529         x_setup_res_seq_num := l_res_seq_num;
4530 
4531     END IF;
4532     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
4533 
4534     if p_substitution_type in (WSM_SUB_DEL ) then
4535         -- CZHDBG do we need to check if resourced is charged ???
4536 l_stmt_num  := 10;
4537         l_exists := 0;
4538         if( p_handle_wip_cur_op = 'N') then
4539             BEGIN
4540 l_stmt_num  := 20.1;
4541                 select  1
4542                 into    l_exists
4543                 from    wsm_copy_op_resource_instances
4544                 where   wip_entity_id     = p_wip_entity_id
4545                 and     organization_id   = p_org_id
4546                 and     operation_seq_num = l_rtg_op_seq_num
4547                 and     resource_seq_num  = l_res_seq_num;
4548             EXCEPTION
4549                 when no_data_found then
4550                     raise e_ins_not_exists;
4551                 when others then
4552                     null;
4553             END;
4554         else    -- p_handle_wip_cur_op = 'Y'
4555 
4556             BEGIN
4557 l_stmt_num  := 20.2;
4558                 select  1
4559                 into    l_exists
4560                 from    wip_op_resource_instances
4561                 where   wip_entity_id     = p_wip_entity_id
4562                 and     organization_id   = p_org_id
4563                 and     operation_seq_num = l_job_op_seq_num
4564                 and     resource_seq_num  = l_res_seq_num;
4565             EXCEPTION
4566                 when no_data_found then
4567                     raise e_ins_not_exists;
4568                 when others then
4569                     null;
4570             END;
4571         end if;
4572 
4573     elsif p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC ) then
4574 
4575         -- check res seq num in job/copy, get resource start/end dates
4576         if( p_handle_wip_cur_op = 'N') then
4577 
4578             -- check if the operation is on the recommended path
4579             BEGIN
4580 l_stmt_num  := 30.0;
4581                 /*Added Department Id for bug 5476647*/
4582                 select  wco.recommended,wco.department_id
4583                 into    l_op_recommended,l_department_id
4584                 from    wsm_copy_operations wco
4585                 where   wco.wip_entity_id     = p_wip_entity_id
4586                 and     wco.operation_seq_num = l_rtg_op_seq_num;
4587             EXCEPTION
4588                 when others then
4589                     raise e_invalid_rtg_op_seq;
4590             END;
4591             if(l_op_recommended <> 'Y') then
4592                 raise e_not_on_reco_path;
4593             end if;
4594 
4595             BEGIN
4596 l_stmt_num  := 30.1;
4597                 select  reco_start_date,
4598                         reco_completion_date,
4599                         resource_id,
4600                         recommended
4601                 into    l_res_sdate,
4602                         l_res_edate,
4603                         l_resource_id,       -- this is used for validate instance_id
4604                         l_res_recommended
4605                 from    wsm_copy_op_resources
4606                 where   wip_entity_id     = p_wip_entity_id
4607                 and     organization_id   = p_org_id
4608                 and     operation_seq_num = l_rtg_op_seq_num
4609                 and     resource_seq_num  = l_res_seq_num;
4610             EXCEPTION
4611                 when no_data_found then
4612                     raise e_invalid_res_seq_num;
4613                 when others then
4614                     null;
4615             END;
4616 
4617             IF lbji_debug = 'Y' then
4618                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : handling curr op : l_rtg_op_seq_num ' || l_rtg_op_seq_num || ' resource seq num : '|| l_res_seq_num || ' wip entity id : ' || p_wip_entity_id);
4619             END IF;
4620 
4621             if(l_res_recommended <> 'Y') then
4622                 raise e_res_not_reco;
4623             end if;
4624 
4625         else    -- p_handle_wip_cur_op = 'Y'
4626 
4627             IF lbji_debug = 'Y' then
4628                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : handling curr op : job op seq num ' || l_job_op_seq_num || ' resource seq num : '|| l_res_seq_num || ' wip entity id : ' || p_wip_entity_id);
4629             END IF;
4630 
4631             BEGIN
4632 l_stmt_num  := 30.2;
4633                 select  wor.start_date,
4634                         wor.completion_date,
4635                         wor.resource_id,
4636                         wo.department_id
4637                 into    l_res_sdate,
4638                         l_res_edate,
4639                         l_resource_id,   -- this is used for validate instance_id
4640                         l_department_id    /*Added Department Id for bug 5476647*/
4641                 from    wip_operation_resources wor, wip_operations wo
4642                 where   wor.wip_entity_id     = p_wip_entity_id
4643                 and     wor.organization_id   = p_org_id
4644                 and     wor.operation_seq_num = l_job_op_seq_num
4645                 and     wor.resource_seq_num  = l_res_seq_num
4646                 and     wor.wip_entity_id     = wo.wip_entity_id
4647                 and     wor.organization_id   = wo.organization_id
4648                 and     wor.operation_seq_num = wo.operation_seq_num;
4649             EXCEPTION
4650                 when no_data_found then
4651                     raise e_invalid_res_seq_num;
4652                 when others then
4653                     raise e_invalid_res_seq_num;
4654             END;
4655 
4656 			IF lbji_debug = 'Y' then
4657                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num || ' start date : ' || to_char(l_res_sdate,'DD-MON-YYYY HH24:MI:SS'));
4658                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num || ' end  date : ' || to_char(l_res_edate,'DD-MON-YYYY HH24:MI:SS'));
4659             END IF;
4660 
4661         END IF;
4662 
4663         -- check all the records
4664         l_sdate_match := 0;
4665         l_edate_match := 0;
4666         while (l_i IS NOT NULL)
4667         loop
4668             l_instance_id   := p_v_res_ins(l_i).instance_id;
4669             l_serial_number := p_v_res_ins(l_i).serial_number;
4670             l_batch_id      := p_v_res_ins(l_i).batch_id;
4671             l_sdate         := p_v_res_ins(l_i).start_date;
4672             l_edate         := p_v_res_ins(l_i).completion_date;
4673 
4674             l_stmt_num  := 40.1;
4675 
4676             IF lbji_debug = 'Y' then
4677                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : op seq num ' || l_rtg_op_seq_num || ' res seq num : ' || l_res_seq_num);
4678                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num || ' start date : ' || to_char(l_sdate,'DD-MON-YYYY HH24:MI:SS'));
4679                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num || ' end  date : ' || to_char(l_edate,'DD-MON-YYYY HH24:MI:SS'));
4680                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num || ' Resource start date : ' || to_char(l_res_sdate,'DD-MON-YYYY HH24:MI:SS'));
4681                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num || ' Resource end  date : ' || to_char(l_res_edate,'DD-MON-YYYY HH24:MI:SS'));
4682             END IF;
4683 
4684             -- check instance_id and serial_number
4685             if(l_instance_id IS NULL ) then
4686                 raise e_null_ins_id;
4687             end if;
4688 l_stmt_num  := 40.2;
4689             IF lbji_debug = 'Y' then
4690                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num || ' Resource id : ' || l_resource_id || ' Instance id : ' || l_instance_id || ' serial number : ' || l_serial_number);
4691             END IF;
4692 
4693              BEGIN
4694                 /* Modified SQL for Bug 5476647. Since the SQL before was checking only whether the given instance
4695                    is valid for the resource. Now modified the SQL to check whether given instance is valid for the
4696                    department, resource combination */
4697                 select  1
4698                 into    l_exists
4699                 from    bom_dept_res_instances  bdri
4700                 where   bdri.resource_id = l_resource_id
4701                 and     bdri.department_id = l_department_id
4702                 and     bdri.instance_id = l_instance_id
4703                 and     NVL(bdri.serial_number, g_nvl_str) = NVL(l_serial_number, g_nvl_str);
4704 
4705                 /*l_exists :=0;
4706 
4707                 select  1
4708                 into    l_exists
4709                 from dual
4710                 where exists( select instance_id
4711                               from  bom_dept_res_instances  bdri
4712                               where bdri.resource_id = l_resource_id
4713                               and   bdri.instance_id = l_instance_id
4714                               and   NVL(bdri.serial_number, g_nvl_str) = NVL(l_serial_number, g_nvl_str) );
4715 
4716                if l_exists=0 then
4717                     raise e_invalid_ins_id;
4718                end if;*/
4719 
4720 
4721             EXCEPTION
4722                 when others then
4723                     raise e_invalid_ins_id;
4724             END;
4725 
4726 l_stmt_num  := 50.1;
4727 
4728             -- check start_date/completion_date
4729             if(l_sdate IS NULL or l_edate IS NULL) then
4730                 raise e_null_date;
4731             end if;
4732             if(l_sdate > l_edate or -- bug 3323417, was >=
4733                l_sdate < NVL(l_res_sdate, l_sdate) or
4734                l_edate > NVL(l_res_edate, l_edate))
4735             then
4736                 raise e_invalid_date;
4737             end if;
4738 l_stmt_num  := 50.2;
4739             -- check time slot
4740 
4741 
4742             if(l_sdate = l_res_sdate) then
4743                 l_sdate_match := 1;
4744             end if;
4745             if(l_edate = l_res_edate) then
4746                 l_edate_match := 1;
4747             end if;
4748 
4749             l_i := p_v_res_ins.next(l_i);
4750         end loop;
4751 
4752 l_stmt_num  := 60;
4753 
4754 
4755         if(l_sdate_match = 0 or l_edate_match = 0) then
4756             raise e_invalid_time_slot;
4757         end if;
4758 
4759     end if; -- p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC)
4760 
4761 EXCEPTION
4762     when e_res_not_reco then
4763         x_error_code := -1;
4764         fnd_message.set_name('WSM','WSM_INS_USE_NON_RECO');
4765         x_error_msg := fnd_message.get;
4766         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4767 
4768     when e_not_on_reco_path then
4769         x_error_code := -1;
4770         fnd_message.set_name('WSM','WSM_INS_USE_NON_RECO');
4771         x_error_msg := fnd_message.get;
4772         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4773 
4774 
4775     when e_invalid_rtg_op_seq then
4776         x_error_code := -1;
4777         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4778         fnd_message.set_token('FLD_NAME', 'routing_op_seq_num');
4779         x_error_msg := fnd_message.get;
4780 
4781         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4782 
4783 
4784     when e_invalid_res_seq_num then
4785         x_error_code := -1;
4786         fnd_message.set_name('WSM','WSM_RES_NOT_REQUIRED');
4787         x_error_msg := fnd_message.get;
4788 
4789         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4790 
4791 
4792     when e_ins_not_exists then
4793         x_error_code := -1;
4794         fnd_message.set_name('WSM','WSM_INS_NOT_EXISTS');
4795         x_error_msg := fnd_message.get;
4796 
4797         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4798 
4799 
4800     when e_null_ins_id then
4801         x_error_code := -1;
4802         fnd_message.set_name('WSM','WSM_NULL_FIELD');
4803         fnd_message.set_token('FLD_NAME', 'Instance_id');
4804         x_error_msg := fnd_message.get;
4805 
4806         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4807 
4808 
4809     when e_invalid_ins_id then
4810         x_error_code := -1;
4811         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4812         fnd_message.set_token('FLD_NAME', 'Instance_id and Serial_number');
4813         x_error_msg := fnd_message.get;
4814 
4815         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4816 
4817 
4818     when e_null_date then
4819         x_error_code := -1;
4820         fnd_message.set_name('WSM','WSM_NULL_FIELD');
4821         fnd_message.set_token('FLD_NAME', 'Start date and Completion date');
4822         x_error_msg := fnd_message.get;
4823 
4824         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4825 
4826 
4827     when e_invalid_date then
4828         x_error_code := -1;
4829         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4830         fnd_message.set_token('FLD_NAME', 'Resource instance scheduled dates');
4831         x_error_msg := fnd_message.get;
4832 
4833         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4834 
4835 
4836     when e_invalid_time_slot then
4837         x_error_code := -1;
4838         fnd_message.set_name('WSM','WSM_INS_BAD_TIME_SLOT');
4839         x_error_msg := fnd_message.get;
4840 
4841         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4842 
4843     -- ST : Detailed Scheduling...
4844     when e_no_res_seq_num then
4845         x_error_msg := 'resource_seq_num cannot be uniquely found'; -- CZHDBG_MSG
4846         x_error_code := -1;
4847         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4848     -- ST : Detailed Scheduling...
4849 
4850 	when others then
4851         x_error_code := -1;
4852         x_error_msg := 'Error: validate_wljdi_res_instance: (#'||l_stmt_num||') '
4853                        || SUBSTR(SQLERRM, 1,2000);
4854 
4855         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4856 
4857 
4858 END validate_wljdi_res_instance;
4859 
4860 
4861 -- **********************************************************************************************
4862 -- VALIDATE_WLJDI_RES_USAGE: local validation procedure
4863 -- **********************************************************************************************
4864 
4865 PROCEDURE validate_wljdi_res_usage(
4866         p_wip_entity_id         in number,
4867         p_org_id                in number,
4868         p_v_res_use             in t_res_use,
4869         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
4870         -- In case of instance information being passed for setup resource that is added in the same run
4871         -- return the resurce seq num of the setup resource...
4872         p_parent_res_seq_num    IN NUMBER,
4873         p_resource_id           IN NUMBER,
4874         x_setup_res_seq_num     OUT nocopy number,
4875         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
4876         p_substitution_type     in number,
4877         p_handle_wip_cur_op     in varchar2,
4878         x_error_code            out nocopy number,
4879         x_error_msg             out nocopy varchar2) is
4880 
4881 l_stmt_num              number;
4882 l_i                     number;
4883 l_j                     number;
4884 l_exists                number;
4885 
4886 l_op_recommended        varchar2(1) := 'N';
4887 l_res_recommended       varchar2(1) := 'N';
4888 l_sdate_match           number;
4889 l_edate_match           number;
4890 l_the_sdate             date;
4891 l_the_edate             date;
4892 l_sdate                 date;
4893 l_edate                 date;
4894 l_assigned_units        number;
4895 
4896 l_rtg_op_seq_num        number;
4897 l_job_op_seq_num        number;
4898 l_res_seq_num           number;
4899 l_instance_id           number;
4900 l_serial_num            varchar2(30);
4901 
4902 e_invalid_date          exception;
4903 e_null_date             exception;
4904 e_invalid_rtg_op_seq    exception;
4905 e_not_on_reco_path      exception;
4906 e_res_not_reco          exception;
4907 e_invalid_res_seq_num   exception;
4908 e_invalid_instance_id   exception;
4909 e_instances_exist       exception;
4910 e_invalid_asgned_units  exception;
4911 e_date_overlap          exception;
4912 e_invalid_time_slot     exception;
4913 e_usage_not_exists      exception;
4914 
4915 /* ST : Detailed Scheduling : start */
4916 e_invalid_ins_assigned_units  exception;
4917 e_no_res_seq_num              exception;
4918 /* ST : Detailed Scheduling : end  */
4919 
4920 BEGIN
4921 
4922     x_error_code := 0;
4923     x_error_msg := '';
4924     l_i := p_v_res_use.first;
4925     if(l_i IS NULL) then return; end if;
4926 
4927     -- in p_v_res_use, all records should hev the same res_seq_num
4928     l_rtg_op_seq_num := p_v_res_use(l_i).rtg_op_seq_num;
4929     l_job_op_seq_num := p_v_res_use(l_i).job_op_seq_num;
4930     l_res_seq_num    := p_v_res_use(l_i).res_seq_num;
4931     l_instance_id    := p_v_res_use(l_i).instance_id;      -- for instance usage
4932     l_serial_num     := p_v_res_use(l_i).serial_number;    -- for instance usage
4933 
4934     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
4935     IF l_res_seq_num IS NULL THEN
4936 
4937             -- call is for a setup resource...
4938             IF p_handle_wip_cur_op = 'N' THEN
4939                 BEGIN
4940                     select resource_seq_num
4941                     into l_res_seq_num
4942                     from wsm_copy_op_resources WCOR
4943                     where WCOR.wip_entity_id     = p_wip_entity_id
4944                     and   WCOR.organization_id   = p_org_id
4945                     and   WCOR.operation_seq_num = l_rtg_op_seq_num
4946                     and   WCOR.parent_resource_seq_num = p_parent_res_seq_num
4947                     and   WCOR.resource_id        = p_resource_id;
4948 
4949                 EXCEPTION
4950                     WHEN NO_DATA_FOUND THEN
4951                         raise e_no_res_seq_num;
4952                     WHEN TOO_MANY_ROWS THEN
4953                         raise e_no_res_seq_num;
4954                 END;
4955             ELSE
4956                 BEGIN
4957                     select resource_seq_num
4958                     into l_res_seq_num
4959                     from  wip_operation_resources WOR
4960                     where WOR.wip_entity_id      = p_wip_entity_id
4961                     and   WOR.organization_id    = p_org_id
4962                     and   WOR.operation_seq_num  = l_job_op_seq_num
4963                     and   WOR.parent_resource_seq = p_parent_res_seq_num
4964                     and   WOR.resource_id         = p_resource_id;
4965 
4966                 EXCEPTION
4967                     WHEN NO_DATA_FOUND THEN
4968                         raise e_no_res_seq_num;
4969                     WHEN TOO_MANY_ROWS THEN
4970                         raise e_no_res_seq_num;
4971                 END;
4972         END IF;
4973 
4974         x_setup_res_seq_num := l_res_seq_num;
4975 
4976     END IF;
4977     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
4978 
4979     if p_substitution_type in (WSM_SUB_DEL ) then
4980         -- CZHDBG: do we need to chech if resourced is charged ???
4981 l_stmt_num  := 10;
4982         l_exists := 0;
4983         if( p_handle_wip_cur_op = 'N') then
4984             BEGIN
4985 l_stmt_num  := 20.1;
4986                 select  1
4987                 into    l_exists
4988                 from    wsm_copy_op_resource_usage
4989                 where   wip_entity_id     = p_wip_entity_id
4990                 and     organization_id   = p_org_id
4991                 and     operation_seq_num = l_rtg_op_seq_num
4992                 and     resource_seq_num  = l_res_seq_num
4993                 and     NVL(instance_id, -1)          = NVL(l_instance_id, -1)
4994                 and     NVL(serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
4995             EXCEPTION
4996                 when no_data_found then
4997                     raise e_usage_not_exists;
4998                 when others then
4999                     null;
5000             END;
5001         else    -- p_handle_wip_cur_op = 'Y'
5002             BEGIN
5003 l_stmt_num  := 20.2;
5004                 select  1
5005                 into    l_exists
5006                 from    wip_operation_resource_usage
5007                 where   wip_entity_id     = p_wip_entity_id
5008                 and     organization_id   = p_org_id
5009                 and     operation_seq_num = l_job_op_seq_num
5010                 and     resource_seq_num  = l_res_seq_num
5011                 and     NVL(instance_id, -1)          = NVL(l_instance_id, -1)
5012                 and     NVL(serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
5013             EXCEPTION
5014                 when no_data_found then
5015                     raise e_usage_not_exists;
5016                 when others then
5017                     null;
5018             END;
5019         end if;
5020 
5021     elsif p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC ) then
5022 
5023         -- get resource or instance start/end dates
5024         if( p_handle_wip_cur_op = 'N') then
5025             BEGIN
5026 l_stmt_num  := 30.1;
5027                 select  wco.recommended,
5028                         wcor.recommended,
5029                         wcor.reco_start_date,
5030                         wcor.reco_completion_date
5031                 into    l_op_recommended,
5032                         l_res_recommended,
5033                         l_the_sdate,
5034                         l_the_edate
5035                 from    wsm_copy_operations   wco,
5036                         wsm_copy_op_resources wcor
5037                 where   wco.wip_entity_id      = p_wip_entity_id
5038                 and     wco.operation_seq_num  = l_rtg_op_seq_num
5039                 and     wcor.wip_entity_id     = wco.wip_entity_id
5040                 and     wcor.operation_seq_num = wco.operation_seq_num
5041                 and     wcor.resource_seq_num  = l_res_seq_num;
5042             EXCEPTION
5043                 when others then
5044                     raise e_invalid_rtg_op_seq;
5045             END;
5046 
5047 l_stmt_num  := 30.2;
5048             -- check if the operation is on the recommended path
5049             if(l_op_recommended <> 'Y') then
5050                 raise e_not_on_reco_path;
5051             end if;
5052             -- check if the the resource is recommended
5053             if(l_res_recommended <> 'Y') then
5054                 raise e_res_not_reco;
5055             end if;
5056 
5057             -- check if the resource has instance information
5058             l_exists := 0;
5059             BEGIN
5060 l_stmt_num  := 30.3;
5061                 select  1
5062                 into    l_exists
5063                 from    wsm_copy_op_resource_instances  wcori
5064                 where   wcori.wip_entity_id     = p_wip_entity_id
5065                 and     wcori.organization_id   = p_org_id
5066                 and     wcori.operation_seq_num = l_rtg_op_seq_num
5067                 and     wcori.resource_seq_num  = l_res_seq_num;
5068             EXCEPTION
5069                 when no_data_found then
5070                     l_exists := 0;
5071                 when too_many_rows then
5072                     l_exists := 1;
5073                 when others then
5074                     l_exists := 0;
5075             END;
5076 
5077 l_stmt_num  := 30.4;
5078             /* ST : Detailed Scheduling commenting out this code as resource usage info can be obt. when instances exist
5079             -- do not accept resource usage when instance info is available
5080             if(l_instance_id IS NULL and l_exists = 1) then
5081                 raise e_instances_exist;
5082             end if;
5083             */
5084 
5085             -- do not accept instance usage when instance info is not available
5086             if(l_instance_id IS NOT NULL and l_exists = 0) then
5087                 raise e_invalid_instance_id;
5088             end if;
5089 
5090             if(l_instance_id IS NOT NULL) then
5091                 -- get instance start/end dates
5092                 BEGIN
5093 l_stmt_num  := 30.5;
5094                     select  wcori.start_date,
5095                             wcori.completion_date
5096                     into    l_the_sdate,
5097                             l_the_edate
5098                     from    wsm_copy_op_resource_instances  wcori
5099                     where   wcori.wip_entity_id     = p_wip_entity_id
5100                     and     wcori.organization_id   = p_org_id
5101                     and     wcori.operation_seq_num = l_rtg_op_seq_num
5102                     and     wcori.resource_seq_num  = l_res_seq_num
5103                     and     wcori.instance_id       = l_instance_id
5104                     and     NVL(wcori.serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
5105                 EXCEPTION
5106                     when others then
5107                         raise e_invalid_instance_id;
5108                 END;
5109             end if;
5110 
5111         else    -- p_handle_wip_cur_op = 'Y'
5112 l_stmt_num  := 40.0;
5113             -- check if the resource has instance information
5114             l_exists := 0;
5115             -- Code for debug purpose.... ---- -- to uncomment replace all
5116             IF lbji_debug = 'Y' then
5117                 fnd_file.put_line(fnd_file.log,'Wip entity id  : ' || p_wip_entity_id
5118                                                || ' op seq num : ' || l_job_op_seq_num
5119                                    || ' res seq num : ' ||l_res_seq_num
5120                                    );
5121             END IF;
5122 
5123             BEGIN
5124 l_stmt_num  := 40.1;
5125                 select  1
5126                 into    l_exists
5127                 from    wip_op_resource_instances  wori
5128                 where   wori.wip_entity_id     = p_wip_entity_id
5129                 and     wori.organization_id   = p_org_id
5130                 and     wori.operation_seq_num = l_job_op_seq_num /* l_rtg_op_seq_num : ST : This has to be job op seq num */
5131                 and     wori.resource_seq_num  = l_res_seq_num;
5132             EXCEPTION
5133                 when no_data_found then
5134                     l_exists := 0;
5135                 when too_many_rows then
5136                     l_exists := 1;
5137                 when others then
5138                     l_exists := 0;
5139             END;
5140 
5141 l_stmt_num  := 40.2;
5142             -- do not accept resource usage when instance info is available
5143             /*
5144             if(l_instance_id IS NULL and l_exists = 1) then
5145                 raise e_instances_exist;
5146             end if;
5147             */
5148             -- do not accept instance usage when instance info is not available
5149             if(l_instance_id IS NOT NULL and l_exists = 0) then
5150                 raise e_invalid_instance_id;
5151             end if;
5152 
5153             if(l_instance_id IS NOT NULL) then
5154                 -- get instance start/end dates
5155                 BEGIN
5156 l_stmt_num  := 40.3;
5157                     select  start_date,
5158                             completion_date
5159                     into    l_the_sdate,
5160                             l_the_edate
5161                     from    wip_op_resource_instances
5162                     where   wip_entity_id     = p_wip_entity_id
5163                     and     organization_id   = p_org_id
5164                     and     operation_seq_num = l_job_op_seq_num
5165                     and     resource_seq_num  = l_res_seq_num
5166                     and     instance_id       = l_instance_id
5167                     and     NVL(serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
5168                 EXCEPTION
5169                     when others then
5170                         raise e_invalid_instance_id;
5171                 END;
5172             else
5173                 -- get resource start/end dates
5174                 BEGIN
5175 l_stmt_num  := 40.4;
5176                     select  start_date,
5177                             completion_date
5178                     into    l_the_sdate,
5179                             l_the_edate
5180                     from    wip_operation_resources wor
5181                     where   wip_entity_id     = p_wip_entity_id
5182                     and     organization_id   = p_org_id
5183                     and     operation_seq_num = l_job_op_seq_num
5184                     and     resource_seq_num  = l_res_seq_num;
5185                 EXCEPTION
5186                     when others then
5187                         raise e_invalid_res_seq_num;
5188                 END;
5189             end if;
5190         end if;
5191 
5192         -- first loop check single record
5193         l_sdate_match := 0;
5194         l_edate_match := 0;
5195         while (l_i IS NOT NULL)
5196         loop
5197             l_assigned_units := p_v_res_use(l_i).assigned_units;
5198             l_sdate          := p_v_res_use(l_i).start_date;
5199             l_edate          := p_v_res_use(l_i).completion_date;
5200 
5201             /* ST : Detailed scheduling : start */
5202             l_instance_id    := p_v_res_use(l_i).instance_id;
5203             /* ST : Detailed scheduling : end */
5204 
5205 l_stmt_num  := 50;
5206             -- check start_date/completion_date
5207             if(l_sdate IS NULL or l_edate IS NULL) then
5208                 raise e_null_date;
5209             end if;
5210             if(l_sdate > l_edate or         -- bug 3323417, was >=
5211                l_sdate < NVL(l_the_sdate, l_sdate) or
5212                l_edate > NVL(l_the_edate, l_edate))
5213             then
5214                 --fnd_file.put_line(fnd_file.log, 'CZHDBG: XXX'
5215                 --    || ' l_the_sdate = ' || TO_CHAR(l_the_sdate, 'DD-MON-YYYY HH24:MI:SS')
5216                 --    || ',l_the_edate = ' || TO_CHAR(l_the_edate, 'DD-MON-YYYY HH24:MI:SS')
5217                 --    || ' l_sdate = '     || TO_CHAR(l_sdate, 'DD-MON-YYYY HH24:MI:SS')
5218                 --    || ' l_edate = '     || TO_CHAR(l_edate, 'DD-MON-YYYY HH24:MI:SS')
5219                 --);
5220                 raise e_invalid_date;
5221             end if;
5222 
5223 l_stmt_num  := 60;
5224             -- check assigned_units
5225             if(l_assigned_units IS NULL or l_assigned_units <= 0) then
5226                 raise e_invalid_asgned_units;
5227             end if;
5228 
5229             -- check time slot
5230             if(l_sdate = l_the_sdate) then
5231                 l_sdate_match := 1;
5232             end if;
5233             if(l_edate = l_the_edate) then
5234                 l_edate_match := 1;
5235             end if;
5236 
5237             /* ST : Detailed scheduling : start */
5238             /* for resource instance usage assigned units should be equal to 1 */
5239             if l_instance_id is not null and l_assigned_units <> 1 then
5240                   raise e_invalid_ins_assigned_units;
5241             end if;
5242             /* ST : Detailed scheduling : end  */
5243             l_i := p_v_res_use.next(l_i);
5244         end loop;
5245 
5246 l_stmt_num  := 70;
5247         if(l_sdate_match = 0 or l_edate_match = 0) then
5248             raise e_invalid_time_slot;
5249         end if;
5250 
5251         -- second loop check relation between records
5252         l_i := p_v_res_use.first;
5253         while (l_i IS NOT NULL)
5254         loop
5255             l_sdate := p_v_res_use(l_i).start_date;
5256             l_edate := p_v_res_use(l_i).completion_date;
5257 
5258 l_stmt_num  := 80;
5259             -- check time overlap
5260             l_j := p_v_res_use.next(l_i);
5261             while (l_j IS NOT NULL)
5262             loop
5263                 if(p_v_res_use(l_j).start_date <= l_sdate and
5264                    p_v_res_use(l_j).completion_date > l_sdate) or
5265                   (l_sdate <= p_v_res_use(l_j).start_date and
5266                    l_edate > p_v_res_use(l_j).start_date)
5267                 then
5268                     raise e_date_overlap;
5269                 end if;
5270 
5271                 l_j := p_v_res_use.next(l_j);
5272             end loop;
5273 
5274             l_i := p_v_res_use.next(l_i);
5275         end loop;
5276     end if; -- p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC)
5277 
5278 EXCEPTION
5279     when e_not_on_reco_path then
5280         x_error_code := -1;
5281         fnd_message.set_name('WSM','WSM_INS_USE_NON_RECO');
5282         x_error_msg := fnd_message.get;
5283         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5284 
5285     when e_res_not_reco then
5286         x_error_code := -1;
5287         fnd_message.set_name('WSM','WSM_INS_USE_NON_RECO');
5288         x_error_msg := fnd_message.get;
5289         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5290 
5291     when e_invalid_rtg_op_seq then
5292         x_error_code := -1;
5293         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
5294         fnd_message.set_token('FLD_NAME', 'routing_op_seq_num');
5295         x_error_msg := fnd_message.get;
5296         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5297 
5298     when e_invalid_res_seq_num then
5299         x_error_code := -1;
5300         fnd_message.set_name('WSM','WSM_RES_NOT_REQUIRED');
5301         x_error_msg := fnd_message.get;
5302         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5303 
5304     when e_instances_exist then
5305         x_error_code := -1;
5306         fnd_message.set_name('WSM','WSM_INS_EXISTS');
5307         x_error_msg := fnd_message.get;
5308         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5309 
5310     when e_invalid_instance_id then
5311         x_error_code := -1;
5312         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
5313         --fnd_message.set_token('FLD_NAME', ' l_stmt_num : ' || l_stmt_num || ' Resource_seq_num / Instance_id / Serial_number');
5314         fnd_message.set_token('FLD_NAME', ' Resource_seq_num / Instance_id / Serial_number'); /*Bug 5488016 */
5315         x_error_msg := fnd_message.get;
5316         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5317 
5318     when e_invalid_asgned_units then
5319         x_error_code := -1;
5320         fnd_message.set_name('WSM','WSM_USE_BAD_ASGN_UNITS');
5321         x_error_msg := fnd_message.get;
5322         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5323 
5324     /* ST : Detailed Scheduling start */
5325     when e_invalid_ins_assigned_units then
5326         x_error_code := -1;
5327         fnd_message.set_name('WSM','WSM_INVALID_INS_ASSG_UNITS');
5328         x_error_msg := fnd_message.get;
5329         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5330 
5331     when e_no_res_seq_num then
5332         x_error_msg := 'validate_wljdi_res_usage :  resource_seq_num cannot be uniquely found'; -- CZHDBG_MSG
5333         x_error_code := -1;
5334         fnd_file.put_line(fnd_file.log,substrb(x_error_msg, 1, 2000));
5335 
5336     /* ST : Detailed Scheduling end */
5337 
5338     when e_usage_not_exists then
5339         x_error_code := -1;
5340         fnd_message.set_name('WSM','WSM_USE_NOT_EXISTS');
5341         x_error_msg := fnd_message.get;
5342         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5343 
5344     when e_null_date then
5345         x_error_code := -1;
5346         fnd_message.set_name('WSM','WSM_NULL_FIELD');
5347         fnd_message.set_token('FLD_NAME', 'Start date and Completion date');
5348         x_error_msg := fnd_message.get;
5349         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5350 
5351     when e_invalid_date then
5352         x_error_code := -1;
5353         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
5354         fnd_message.set_token('FLD_NAME', 'Resource usage scheduled dates');
5355         x_error_msg := fnd_message.get;
5356         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5357 
5358     when e_date_overlap then
5359         x_error_code := -1;
5360         fnd_message.set_name('WSM','WSM_USE_TIME_OVERLAP');
5361         x_error_msg := fnd_message.get;
5362         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5363 
5364     when e_invalid_time_slot then
5365         x_error_code := -1;
5366         fnd_message.set_name('WSM','WSM_USE_BAD_TIME_SLOT');
5367         x_error_msg := fnd_message.get;
5368         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5369 
5370     when others then
5371         x_error_code := -1;
5372         x_error_msg := 'Error: validate_wljdi_res_usage: (#'||l_stmt_num||') '
5373                        || SUBSTR(SQLERRM, 1,240);
5374         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5375 
5376 END validate_wljdi_res_usage;
5377 
5378 
5379 -- **********************************************************************************************
5380 -- VALIDATE_WLJDI_OP_COMPONENT: local validation procedure
5381 -- **********************************************************************************************
5382 
5383 PROCEDURE validate_wljdi_op_component(
5384         p_wip_entity_id         in number,
5385         p_org_id                in number,
5386         p_v_comp                in out nocopy t_op_comp,
5387         p_substitution_type     in number,
5388         p_handle_wip_cur_op     in varchar2,
5389         x_error_code            out nocopy number,
5390         x_error_msg             out nocopy varchar2) is
5391 
5392 l_stmt_num              number;
5393 l_i                     number;
5394 
5395 l_op_sdate              date;
5396 l_op_edate              date;
5397 l_op_qty                number;
5398 l_op_recommended        varchar2(1) := 'N';
5399 
5400 l_rtg_op_seq_num        number;
5401 l_job_op_seq_num        number;
5402 l_item_id               number;
5403 l_primary_item_id       number;
5404 l_src_phm_item_id       number;
5405 l_component_seq_id      number;
5406 l_date_req              date;
5407 l_req_qty               number;
5408 l_qpa                   number;
5409 l_bom_qpa               number; -- ADD: bug 3347985
5410 l_scheduled_qty         number;
5411 l_component_yield       number;
5412 
5413 -- Added For LBJ Interface Alcoa Enhancement
5414 l_item_old_id     number;
5415 l_comp_exist      number;
5416 l_quantity_issued number;
5417 l_temp            number;
5418 l_comp_duplicate  number;
5419 l_wip_valid       number;
5420 l_wip_supply_type number;
5421 l_job_qty         number;
5422 l_mrp_net_flag    number;
5423 l_phan_exist      number;
5424 l_src_phm_id      number;
5425 l_quantity_per_assembly number;
5426 l_supply_subinventory   varchar2(10);
5427 l_supply_locator_id     number;
5428 
5429 e_parent_phantom          exception;
5430 e_parent_phantom_comp     exception;
5431 e_no_parent_phantom       exception;
5432 e_invalid_item            exception;
5433 e_quantity_issued_error   exception;
5434 e_material_trans_err      exception;
5435 e_phantom_comp            exception;
5436 e_invalid_wip_supply_type exception;
5437 e_invalid_qpa             exception;
5438 e_inv_supply_info         exception;
5439 e_invalid_job             exception;
5440 e_dup_component           exception;
5441 e_no_assy_as_comp         exception;
5442 e_invalid_mrp_net_flag  exception;
5443 
5444 cursor c_wsm_phm_grp (
5445         c_wip_entity_id     number,
5446         c_rtg_op_seq_num    number,
5447         c_src_phm_item_id   number) is
5448     select   component_sequence_id,
5449              bill_quantity_per_assembly -- bug 3347985, was 'quantity_per_assembly'
5450     from     wsm_copy_requirement_ops
5451     where    wip_entity_id      = c_wip_entity_id
5452     and      operation_seq_num  = c_rtg_op_seq_num
5453     and      source_phantom_id  = c_src_phm_item_id
5454     order by component_sequence_id;
5455 
5456 l_v_phm_comp_seqs       t_number;
5457 l_v_phm_comp_qpa        t_number;
5458 
5459 e_req_qty_zero          exception; -- bug 7197440
5460 e_req_qty_sign_chg      exception; -- bug 7197440
5461 e_invalid_component     exception;
5462 e_invalid_phantom       exception;
5463 e_invalid_date          exception;
5464 e_req_qty_not_upd       exception;
5465 --e_req_qty_negative      exception; Changed for Bug# 7197440
5466 e_invalid_comp_yield    exception;
5467 
5468 BEGIN
5469 
5470     x_error_code := 0;
5471     x_error_msg := '';
5472     l_i := p_v_comp.first;
5473     if(l_i IS NULL) then return; end if;
5474 
5475     l_rtg_op_seq_num   := p_v_comp(l_i).rtg_op_seq_num;
5476     l_job_op_seq_num   := p_v_comp(l_i).job_op_seq_num;
5477     l_item_id          := p_v_comp(l_i).item_id;
5478     l_primary_item_id  := p_v_comp(l_i).primary_item_id;
5479     l_src_phm_item_id  := p_v_comp(l_i).src_phm_item_id;
5480     l_component_seq_id := p_v_comp(l_i).component_seq_id;
5481     l_date_req         := p_v_comp(l_i).date_required;
5482     l_req_qty          := p_v_comp(l_i).required_qty;
5483 
5484     -- Added For LBJ Interface Alcoa Enhancement
5485     l_item_old_id      := p_v_comp(l_i).item_old_id;
5486     l_wip_supply_type  := p_v_comp(l_i).wip_supply_type;
5487     l_mrp_net_flag     := p_v_comp(l_i).mrp_net_flag;
5488     l_quantity_per_assembly := p_v_comp(l_i).qpa ;
5489     l_supply_subinventory   := p_v_comp(l_i).supply_subinventory;
5490     l_supply_locator_id     := p_v_comp(l_i).supply_locator ;
5491 
5492 l_stmt_num  := 10;
5493     -- validate operation_seq_num, l_item_id
5494     if p_substitution_type in (WSM_SUB_CHG, WSM_SUB_REC) then
5495 
5496         -- get op start/end dates
5497         if( p_handle_wip_cur_op = 'Y') then
5498             BEGIN
5499 l_stmt_num  := 20;
5500                 select  wo.first_unit_start_date,
5501                         wo.last_unit_completion_date
5502                 into    l_op_sdate,
5503                         l_op_edate
5504                 from    wip_operations             wo,
5505                         wip_requirement_operations wro
5506                 where   wo.wip_entity_id         = p_wip_entity_id
5507                 and     wo.operation_seq_num     = l_job_op_seq_num
5508                 and     wro.wip_entity_id        = wo.wip_entity_id
5509                 and     wro.operation_seq_num    = wo.operation_seq_num
5510                 and     wro.inventory_item_id    = l_item_id;
5511             EXCEPTION
5512                 when no_data_found then
5513                     raise e_invalid_component;
5514             END;
5515 
5516         else    -- p_handle_wip_cur_op = 'N'
5517 
5518             if(l_src_phm_item_id IS NOT NULL AND
5519                l_src_phm_item_id <> -1)
5520             then
5521 
5522                 -- check l_rtg_op_seq_num
5523                 BEGIN
5524 l_stmt_num  := 30;
5525                     select  wco.reco_start_date,
5526                             wco.reco_completion_date,
5527                             wco.recommended,
5528                             wco.reco_scheduled_quantity
5529                     into    l_op_sdate,
5530                             l_op_edate,
5531                             l_op_recommended,
5532                             l_scheduled_qty
5533                     from    wsm_copy_operations wco
5534                     where   wco.wip_entity_id     = p_wip_entity_id
5535                     and     wco.operation_seq_num = l_rtg_op_seq_num;
5536                 EXCEPTION
5537                     when no_data_found then
5538                         raise e_invalid_component;
5539                 END;
5540 
5541 l_stmt_num  := 40.1;
5542                 -- get all the component_seq_id in the same phantom comp
5543                 for rec in c_wsm_phm_grp (
5544                         p_wip_entity_id,
5545                         l_rtg_op_seq_num,
5546                         l_src_phm_item_id)
5547                 loop
5548                     l_v_phm_comp_seqs(rec.component_sequence_id) := rec.component_sequence_id;
5549                     l_v_phm_comp_qpa(rec.component_sequence_id)  := rec.bill_quantity_per_assembly; -- bug 3347985
5550                 end loop;
5551 
5552 l_stmt_num  := 40.2;
5553                 -- should check whether all the components within the phantom
5554                 -- are send together
5555                 if(l_v_phm_comp_seqs.COUNT <> p_v_comp.COUNT) then
5556                     raise e_invalid_phantom;
5557                 end if;
5558                 l_i := p_v_comp.first;
5559                 while (l_i IS NOT NULL)
5560                 loop
5561 l_stmt_num  := 40.3;
5562                     l_component_seq_id := p_v_comp(l_i).component_seq_id;
5563                     if(NOT l_v_phm_comp_seqs.EXISTS(l_component_seq_id)) then
5564                         raise e_invalid_phantom;
5565                     end if;
5566 
5567                     -- check if component yield is going to be changed
5568                     -- by sending required_quantity
5569                     l_req_qty := p_v_comp(l_i).required_qty;
5570                     if(l_req_qty IS NOT NULL) then
5571                         -- bug 7197440 : Do not allow required quantity to be either zero or change of sign.
5572                         if(l_req_qty = 0) then
5573 					 raise e_req_qty_zero;
5574 		        elsif (sign(l_req_qty) <> sign(l_v_phm_comp_qpa(l_component_seq_id))) then
5575 					 raise e_req_qty_sign_chg;
5576 	                end if; -- end of fix for bug 7197440
5577 
5578                         if(l_op_recommended <> 'Y') then
5579                             raise e_req_qty_not_upd;
5580                         end if;
5581                         if(NVL(l_scheduled_qty, 0) <> 0) then
5582                             -- BC: bug 3347985
5583                             -- l_qpa             := l_v_phm_comp_qpa(l_component_seq_id);
5584                             -- l_component_yield := round(l_scheduled_qty*l_qpa/l_req_qty, 6);
5585                             l_bom_qpa         := l_v_phm_comp_qpa(l_component_seq_id);
5586                             l_component_yield := round(l_scheduled_qty*l_bom_qpa/l_req_qty, 6);
5587                             l_qpa             := l_req_qty/l_scheduled_qty;
5588                             -- EC: bug 3347985
5589                             if(l_component_yield <= 0) then
5590                                 raise e_invalid_comp_yield;
5591                             else
5592                                 p_v_comp(l_i).component_yield := l_component_yield;
5593                                 p_v_comp(l_i).qpa             := l_qpa;
5594                             end if;
5595                         else
5596                             p_v_comp(l_i).component_yield := null;
5597                             p_v_comp(l_i).qpa             := null;  -- ADD: bug 3347985
5598                         end if;
5599                     else
5600                         p_v_comp(l_i).component_yield := null;
5601                         p_v_comp(l_i).qpa             := null;  -- ADD: bug 3347985
5602                     end if;
5603 
5604                     l_i := p_v_comp.next(l_i);
5605                 end loop;
5606 
5607             else    -- l_src_phm_item_id IS NULL or -1
5608 
5609                 BEGIN
5610 l_stmt_num  := 50.1;
5611                     select  wco.reco_start_date,
5612                             wco.reco_completion_date,
5613                             wco.recommended,
5614                             wco.reco_scheduled_quantity,
5615                             wcro.bill_quantity_per_assembly -- bug 3347985 was 'quantity_per_assembly'
5616                     into    l_op_sdate,
5617                             l_op_edate,
5618                             l_op_recommended,
5619                             l_scheduled_qty,
5620                             l_bom_qpa
5621                     from    wsm_copy_operations      wco,
5622                             wsm_copy_requirement_ops wcro
5623                     where   wco.wip_entity_id         = p_wip_entity_id
5624                     and     wco.operation_seq_num     = l_rtg_op_seq_num
5625                     and     wcro.wip_entity_id        = wco.wip_entity_id
5626                     and     wcro.operation_seq_num    = wco.operation_seq_num
5627                     and     wcro.component_item_id    = l_item_id
5628                     and     wcro.primary_component_id = l_primary_item_id
5629                     and     wcro.source_phantom_id = -1; -- is NULL;
5630                 EXCEPTION
5631                     when no_data_found then
5632                         raise e_invalid_component;
5633                 END;
5634 
5635 l_stmt_num  := 50.2;
5636                 -- check if component yield is going to be changed
5637                 if(l_req_qty IS NOT NULL) then
5638                         -- bug 7197440 : Do not allow required quantity to be either zero or change of sign.
5639                         if(l_req_qty = 0) then
5640 					 raise e_req_qty_zero;
5641 		        elsif (sign(l_req_qty) <> sign(l_bom_qpa)) then
5642 					 raise e_req_qty_sign_chg;
5643 	                end if; -- end of fix for bug 7197440
5644 l_stmt_num  := 50.3;
5645                     if(l_op_recommended <> 'Y') then
5646                         raise e_req_qty_not_upd;
5647                     end if;
5648 l_stmt_num  := 50.4;
5649                     if(NVL(l_scheduled_qty, 0) <> 0) then
5650                         -- BC: bug 3347985
5651                         --l_component_yield := round(l_scheduled_qty*l_qpa/l_req_qty, 6);
5652                         l_component_yield := round(l_scheduled_qty*l_bom_qpa/l_req_qty, 6);
5653                         l_qpa             := l_req_qty/l_scheduled_qty;
5654                         -- EC: bug 3347985
5655                         if(l_component_yield <= 0) then
5656                             raise e_invalid_comp_yield;
5657                         else
5658                             p_v_comp(l_i).component_yield := l_component_yield;
5659                             p_v_comp(l_i).qpa             := l_qpa; -- ADD: bug 3347985
5660                         end if;
5661                     else
5662                         p_v_comp(l_i).component_yield := null;
5663                         p_v_comp(l_i).qpa             := null; -- ADD: bug 3347985
5664                     end if;
5665                 else
5666                     p_v_comp(l_i).component_yield := null;
5667                     p_v_comp(l_i).qpa             := null; -- ADD: bug 3347985
5668                 end if;
5669 
5670             end if; -- l_src_phm_item_id IS NULL or -1
5671 
5672         end if;
5673 
5674 l_stmt_num  := 60;
5675         -- validate dates
5676         if(l_date_req IS NOT NULL) then
5677             if( --NVL(l_op_sdate, l_date_req) > l_date_req or -- bug 3292689: removed per APS request
5678                 l_date_req > NVL(l_op_edate, l_date_req))
5679             then
5680                 raise e_invalid_date;
5681             end if;
5682         end if;
5683 
5684     end if; -- p_load_type in (WSM_SUB_CHG, WSM_SUB_REC)
5685     IF (p_substitution_type=WSM_SUB_DEL) THEN
5686 
5687         l_comp_exist := null;
5688         l_quantity_issued :=null;
5689         l_temp   :=null;
5690         if( p_handle_wip_cur_op = 'Y') then  --Current Operation  validation
5691 
5692             --Parent Phantom logic, not deleting the component of parent phantom
5693             if (l_job_op_seq_num<0) then
5694                 raise e_parent_phantom;
5695             end if;
5696 
5697             --Check if the inventory item id old is valid
5698             /*
5699             begin
5700                 select 1
5701                 into l_comp_exist
5702                 from mtl_system_items_kfv
5703                 where organization_id = p_org_id
5704                 and  inventory_item_id=l_item_old_id;
5705             exception
5706                 when others then
5707                     raise e_invalid_item;
5708             end; */
5709 
5710             begin
5711                 select  wro.quantity_issued
5712                 into  l_quantity_issued
5713                 from  wip_requirement_operations wro
5714                 where wro.wip_entity_id = p_wip_entity_id
5715                 and   wro.operation_seq_num = l_job_op_seq_num
5716                 and   wro.inventory_item_id = l_item_old_id
5717                 and   wro.organization_id = p_org_id;
5718 
5719             exception
5720                 when no_data_found then
5721                     null;
5722             end;
5723 
5724             --Quantity Issued validate
5725 
5726             if(l_quantity_issued >0) then
5727                 raise e_quantity_issued_error;
5728             end if;
5729 
5730             --Validation to check if the component has been Transacted
5731             begin
5732 
5733                 select  1
5734                 into  l_temp
5735                 from  dual
5736                 where  exists
5737                              (select 1
5738                              from  wip_requirement_operations wro
5739                              where  wro.wip_entity_id = p_wip_entity_id
5740                              and  wro.organization_id =p_org_id
5741                              and  wro.operation_seq_num = l_job_op_seq_num
5742                              and  wro.inventory_item_id = l_item_old_id
5743                              and  wro.quantity_issued <> 0)
5744                 or exists
5745                             (select 1
5746                             from  mtl_material_transactions_temp mmtt
5747                             where mmtt.transaction_source_id = p_wip_entity_id
5748                             and  mmtt.organization_id = p_org_id
5749                             and  mmtt.operation_seq_num = l_job_op_seq_num
5750                             and  mmtt.inventory_item_id =  l_item_old_id);
5751 
5752             exception
5753                 when no_data_found then
5754                     null;
5755             end;
5756 
5757             if (l_temp is not null) then
5758                 raise e_material_trans_err;
5759             end if;
5760 
5761         elsif( p_handle_wip_cur_op = 'N') THEN
5762             /* Support deletion of Parent Phantom component
5763             if (l_rtg_op_seq_num<0) then
5764                 raise e_parent_phantom;
5765             end if;
5766 
5767             --check if the inventory item id old is valid
5768             begin
5769                 select 1
5770                 into l_comp_exist
5771                 from mtl_system_items_kfv
5772                 where organization_id = p_org_id
5773                 and  inventory_item_id=l_item_old_id;
5774             exception
5775                 when others then
5776                     raise e_invalid_item;
5777             end;
5778             */
5779             -- Validate for parent phantom component that no child component exists.
5780             if l_src_phm_item_id=0 then
5781                 begin
5782                     select 1
5783                     into   l_phan_exist
5784                     from   dual
5785                     where  exists (select component_item_id
5786                                    from   wsm_copy_requirement_ops
5787                                    where  wip_entity_id     = p_wip_entity_id
5788                                    and    source_phantom_id = l_item_old_id
5789                                    and    component_item_id <>l_item_old_id
5790                                    and    operation_seq_num = l_rtg_op_seq_num);
5791                     raise e_parent_phantom_comp;
5792                 exception
5793                     when no_data_found then
5794                         null;
5795                 end;
5796             end if;
5797         end if; --p_handle_wip_cur_op end if
5798     ELSIF (p_substitution_type=WSM_SUB_ADD) THEN
5799 
5800         -- For a standard job, assembly should not be a component.
5801         begin
5802             select 1
5803             into l_comp_exist
5804             from dual
5805             where exists ( select 1
5806                            from wip_discrete_jobs
5807                            where wip_entity_id = p_wip_entity_id
5808                            and organization_id = p_org_id
5809                            and job_type = WIP_CONSTANTS.STANDARD
5810                            and primary_item_id = l_item_id );
5811 
5812             raise e_no_assy_as_comp;
5813 
5814         exception
5815             when no_data_found then
5816                 null;
5817         end;
5818 
5819         l_comp_exist := NULL;
5820         if( p_handle_wip_cur_op = 'Y') THEN
5821 
5822             /*begin
5823                 select 1
5824                 into l_comp_exist
5825                 from mtl_system_items_kfv
5826                 where inventory_item_id=l_item_id
5827                 and organization_id=p_org_id;
5828             exception
5829                 when others then
5830                     raise e_invalid_item;
5831             end;*/
5832 
5833             ---Validate wip supply type
5834 
5835             if l_wip_supply_type= WIP_CONSTANTS.PHANTOM then
5836                 raise e_phantom_comp;
5837             end if;
5838 
5839             if l_wip_supply_type not in (WIP_CONSTANTS.PUSH,
5840                                          WIP_CONSTANTS.ASSY_PULL,
5841                                          WIP_CONSTANTS.OP_PULL,
5842                                          WIP_CONSTANTS.BULK,
5843                                          WIP_CONSTANTS.VENDOR) or l_wip_supply_type is null then
5844                 raise e_invalid_wip_supply_type;
5845             end if;
5846 
5847             --check if component already defined for given job and operation.
5848             begin
5849                 select  1
5850                 into l_comp_duplicate
5851                 from wip_requirement_operations wro
5852                 where wro.wip_entity_id = p_wip_entity_id
5853                 and wro.operation_seq_num =l_job_op_seq_num
5854                 and wro.organization_id=p_org_id
5855                 and wro.inventory_item_id=l_item_id;
5856             exception
5857                 when others then
5858                     null;
5859             end;
5860 
5861             if l_comp_duplicate is not null then
5862                 raise e_dup_component;
5863             end if;
5864 
5865             --Validate the component dates
5866 
5867             begin
5868                 select wo.first_unit_start_date,
5869                        wo.last_unit_completion_date
5870                 into  l_op_sdate,
5871                       l_op_edate
5872                 from  wip_operations wo
5873                 where wo.wip_entity_id    = p_wip_entity_id
5874                 and  wo.operation_seq_num = l_job_op_seq_num;
5875             exception
5876                 when no_data_found then
5877                     raise e_invalid_job;
5878             end;
5879 
5880             if(l_date_req IS NOT NULL) then
5881                 if ( NVL(l_op_sdate, l_date_req) > l_date_req or
5882                     l_date_req > NVL(l_op_edate, l_date_req)) then
5883 
5884                     raise e_invalid_date;
5885                 end if;
5886             else
5887                 l_date_req := l_op_sdate ;
5888             end if;
5889 
5890             --Validating reqd qty
5891 
5892             if (l_req_qty<=0 or l_req_qty is null) then
5893                 raise e_req_qty_zero;
5894             end if;
5895 
5896             if l_quantity_per_assembly is null or l_quantity_per_assembly=0 then
5897                 raise e_invalid_qpa;
5898             end if;
5899 
5900             if l_mrp_net_flag is null or l_mrp_net_flag not in (WIP_CONSTANTS.YES,WIP_CONSTANTS.NO)then
5901                 raise e_invalid_mrp_net_flag;
5902             end if;
5903 
5904             -- For Adhoc components Bill QPA is considered as 1.
5905             p_v_comp(l_i).component_yield := round(1/l_quantity_per_assembly,6);
5906 
5907             if(l_wip_supply_type in (WIP_CONSTANTS.ASSY_PULL,WIP_CONSTANTS.OP_PULL) and
5908                l_supply_subinventory IS NULL) then
5909 
5910                 select wip_supply_subinventory, wip_supply_locator_id
5911                 into   l_supply_subinventory, l_supply_locator_id
5912                 from mtl_system_items
5913                 where inventory_item_id = l_item_id
5914                 and organization_id = p_org_id;
5915 
5916                 if l_supply_subinventory is null then
5917                     select default_pull_supply_subinv, default_pull_supply_locator_id
5918                     into l_supply_subinventory, l_supply_locator_id
5919                     from wip_parameters
5920                     where organization_id = p_org_id;
5921                 end if;
5922 
5923                 if l_supply_subinventory is null then
5924                     raise e_inv_supply_info;
5925                 end if;
5926             end if;
5927 
5928             p_v_comp(l_i).supply_subinventory := l_supply_subinventory;
5929             p_v_comp(l_i).supply_locator := l_supply_locator_id;
5930             p_v_comp(l_i).date_required  := l_date_req;
5931 
5932 
5933         ELSIF ( p_handle_wip_cur_op = 'N') THEN     --Future Operation start
5934 
5935             /*begin
5936                 select 1
5937                 into l_comp_exist
5938                 from mtl_system_items_kfv
5939                 where inventory_item_id=l_item_id
5940                 and organization_id=p_org_id;
5941             exception
5942                 when others then
5943                     raise e_invalid_item;
5944             end;*/
5945 
5946             if l_wip_supply_type not in (WIP_CONSTANTS.PUSH,
5947                                          WIP_CONSTANTS.ASSY_PULL,
5948                                          WIP_CONSTANTS.OP_PULL,
5949                                          WIP_CONSTANTS.BULK,
5950                                          WIP_CONSTANTS.VENDOR,
5951                                          WIP_CONSTANTS.PHANTOM) or l_wip_supply_type is null then
5952                 raise e_invalid_wip_supply_type;
5953             end if;
5954 
5955             --check if component already defined for given job and operation.
5956 
5957             begin
5958                 select  1
5959                 into  l_comp_duplicate
5960                 from  wsm_copy_requirement_ops wcro
5961                 where  wcro.wip_entity_id = p_wip_entity_id
5962                 and  wcro.organization_id=p_org_id
5963                 and  wcro.component_item_id=l_item_id
5964                 and  (wcro.operation_seq_num =l_rtg_op_seq_num or
5965                       wcro.operation_seq_num = -1*l_rtg_op_seq_num);
5966             exception
5967                 when others then
5968                     null;
5969             end;
5970 
5971             if l_comp_duplicate is not null then
5972             raise e_dup_component;
5973             end if;
5974 
5975             l_comp_duplicate := null;
5976 
5977             if l_src_phm_item_id <> -1 AND
5978                l_src_phm_item_id <> 0 then
5979 
5980                 begin
5981                     select  1
5982                     into  l_comp_duplicate
5983                     from  wsm_copy_requirement_ops wcro
5984                     where  wcro.wip_entity_id = p_wip_entity_id
5985                     and  wcro.organization_id=p_org_id
5986                     and  wcro.component_item_id=l_src_phm_item_id
5987                     and  wcro.operation_seq_num = -1*l_rtg_op_seq_num;
5988                 exception
5989                     when others then
5990                         raise e_no_parent_phantom;
5991                 end;
5992             end if;
5993 
5994             --Validate the Component Dates
5995 
5996             begin
5997                 select  wco.reco_start_date,wco.reco_completion_date
5998                 into    l_op_sdate,l_op_edate
5999                 from    wsm_copy_operations wco
6000                 where   wco.wip_entity_id         = p_wip_entity_id
6001                 and     wco.organization_id       = p_org_id
6002                 and     wco.operation_seq_num     = l_rtg_op_seq_num;
6003             exception
6004                 when no_data_found then
6005                     raise e_invalid_job;
6006             end;
6007 
6008             if(l_date_req IS NOT NULL) then
6009                 if( NVL(l_op_sdate, l_date_req) > l_date_req or
6010                     l_date_req > NVL(l_op_edate, l_date_req) )  then
6011                     raise e_invalid_date;
6012                 end if;
6013             else
6014                 l_date_req := l_op_sdate;
6015             end if;
6016 
6017             --Validating reqd qty
6018             if (l_req_qty<=0 or l_req_qty is null) then
6019                 raise e_req_qty_zero;
6020             end if;
6021 
6022             if l_quantity_per_assembly is null or l_quantity_per_assembly=0 then
6023                 raise e_invalid_qpa;
6024             end if;
6025 
6026             if l_mrp_net_flag is null or l_mrp_net_flag not in (WIP_CONSTANTS.YES,WIP_CONSTANTS.NO)then
6027                 raise e_invalid_mrp_net_flag;
6028             end if;
6029 
6030             -- For Adhoc components Bill QPA is considered as 1.
6031             p_v_comp(l_i).component_yield := round(1/l_quantity_per_assembly,6);
6032 
6033             if(l_wip_supply_type in (WIP_CONSTANTS.ASSY_PULL,WIP_CONSTANTS.OP_PULL) and
6034                l_supply_subinventory IS NULL) then
6035 
6036                 select wip_supply_subinventory, wip_supply_locator_id
6037                 into   l_supply_subinventory, l_supply_locator_id
6038                 from mtl_system_items
6039                 where inventory_item_id = l_item_id
6040                 and organization_id = p_org_id;
6041 
6042                 if l_supply_subinventory is null then
6043                     select default_pull_supply_subinv, default_pull_supply_locator_id
6044                     into l_supply_subinventory, l_supply_locator_id
6045                     from wip_parameters
6046                     where organization_id = p_org_id;
6047                 end if;
6048 
6049                 if l_supply_subinventory is null then
6050                     raise e_inv_supply_info;
6051                 end if;
6052             end if;
6053 
6054             p_v_comp(l_i).supply_subinventory := l_supply_subinventory;
6055             p_v_comp(l_i).supply_locator := l_supply_locator_id;
6056             p_v_comp(l_i).date_required  := l_date_req;
6057 
6058 
6059         end if;--p handle wip cur op = y
6060     end if; -- sub_type=Del and add
6061 
6062 EXCEPTION
6063     when e_invalid_component then
6064         x_error_code := -1;
6065         fnd_message.set_name('WSM','WSM_COMP_NOT_REQUIRED');
6066         x_error_msg := fnd_message.get;
6067 
6068     when e_invalid_comp_yield then
6069         x_error_code := -1;
6070         fnd_message.set_name('WSM','WSM_COMP_BAD_YIELD');
6071         x_error_msg := fnd_message.get;
6072 
6073     when e_req_qty_not_upd then
6074         x_error_code := -1;
6075         fnd_message.set_name('WSM','WSM_COMP_REQQTY_NON_RECO');
6076         x_error_msg := fnd_message.get;
6077 
6078     /*when e_req_qty_negative then
6079         x_error_code := -1;
6080         fnd_message.set_name('WSM','WSM_COMP_BAD_REQQTY');
6081         x_error_msg := fnd_message.get;*/
6082 
6083     when e_invalid_phantom then
6084         x_error_code := -1;
6085         fnd_message.set_name('WSM','WSM_COMP_PARTIAL_PHANTOM');
6086         x_error_msg := fnd_message.get;
6087 
6088     when e_invalid_date then
6089         x_error_code := -1;
6090         fnd_message.set_name('WSM','WSM_COMP_BAD_DATE');
6091         x_error_msg := fnd_message.get;
6092 
6093 ---Alcoa Enhancement Changes
6094     when e_parent_phantom then
6095         x_error_code := -1;
6096         fnd_message.set_name('WSM','WSM_PARENT_PHANTOM');
6097         x_error_msg := fnd_message.get;
6098 
6099     when e_parent_phantom_comp then
6100         x_error_code := -1;
6101         fnd_message.set_name('WSM','WSM_PARENT_PHANTOM_CHILD');
6102         x_error_msg := fnd_message.get;
6103 
6104     when e_no_parent_phantom then
6105         x_error_code := -1;
6106         fnd_message.set_name('WSM','WSM_NO_PARENT_PHANTOM');
6107         x_error_msg := fnd_message.get;
6108 
6109     when e_invalid_item then
6110         x_error_code := -1;
6111         fnd_message.set_name('WSM','WSM_INVALID_ITEM');
6112         x_error_msg := fnd_message.get;
6113 
6114     when e_quantity_issued_error then
6115         x_error_code := -1;
6116         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6117         fnd_message.set_token('FLD_NAME', 'Quantity_issued');
6118         x_error_msg := fnd_message.get;
6119 
6120     when e_material_trans_err then
6121         x_error_code := -1;
6122         fnd_message.set_name('WSM','WSM_MATERIAL_TRX');
6123         x_error_msg := fnd_message.get;
6124 
6125        when e_phantom_comp then
6126         x_error_code := -1;
6127         fnd_message.set_name('WSM','WSM_PHANTOM_COMPONENT');
6128         x_error_msg := fnd_message.get;
6129 
6130        when e_invalid_wip_supply_type then
6131         x_error_code := -1;
6132         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6133         fnd_message.set_token('FLD_NAME', 'Supply_type');
6134         x_error_msg := fnd_message.get;
6135 
6136         when e_invalid_qpa then
6137         x_error_code := -1;
6138         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6139         fnd_message.set_token('FLD_NAME', 'Quantity_per_assembly');
6140         x_error_msg := fnd_message.get;
6141 
6142        when e_inv_supply_info then
6143         x_error_code := -1;
6144         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6145         fnd_message.set_token('FLD_NAME', 'Supply subinventory/locator combination');
6146         x_error_msg := fnd_message.get;
6147 
6148         when e_invalid_job  then
6149         x_error_code := -1;
6150         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6151         fnd_message.set_token('FLD_NAME', 'Job_op_seq_num / Routing_op_seq_num');
6152         x_error_msg := fnd_message.get;
6153 
6154        when  e_dup_component then
6155         x_error_code := -1;
6156         fnd_message.set_name('WSM','WSM_COMPONENT_DUP');
6157         x_error_msg := fnd_message.get;
6158 
6159        when  e_no_assy_as_comp then
6160         x_error_code := -1;
6161         fnd_message.set_name('WSM','WSM_ASSY_AS_COMP');
6162         x_error_msg := fnd_message.get;
6163 
6164       when e_invalid_mrp_net_flag then
6165         x_error_code := -1;
6166         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6167         fnd_message.set_token('FLD_NAME', 'Mrp_net_flag');
6168         x_error_msg := fnd_message.get;
6169 
6170 	--Added for bug Fix #7197440
6171     when e_req_qty_zero then  -- bug #7197440
6172         x_error_code := -1;
6173         fnd_message.set_name('WSM','WSM_COPY_QTY_NON_ZERO');
6174         x_error_msg := fnd_message.get;
6175 
6176     when e_req_qty_sign_chg then --  bug #7197440
6177         x_error_code := -1;
6178         fnd_message.set_name('WSM','WSM_COPY_QTY_SIGN');
6179         x_error_msg := fnd_message.get;
6180 
6181     when others then
6182         x_error_code := -1;
6183         x_error_msg := 'Error: validate_wljdi_op_component: (#'||l_stmt_num||') '
6184                        || SUBSTR(SQLERRM, 1,240);
6185 END validate_wljdi_op_component;
6186 
6187 
6188 
6189 procedure update_cumulative_time (
6190         p_wip_entity_id         in number,
6191         p_operation_seq_num     in number,
6192         p_resource_seq_num      in number,
6193         p_instance_id           in number,
6194         p_serial_num            in varchar2,
6195         p_handle_wip_cur_op     in varchar2 )IS
6196 
6197 cursor wip_res_usage (p_wip_entity_id number,
6198                       p_operation_seq_num number,
6199                       p_resource_seq_num number) is
6200     select start_date,
6201            completion_date,
6202            cumulative_processing_time
6203     from   wip_operation_resource_usage
6204     where  wip_entity_id = p_wip_entity_id
6205     and    operation_seq_num = p_operation_seq_num
6206     and    resource_seq_num = p_resource_seq_num
6207     and    NVL(instance_id, -1)          = NVL(p_instance_id, -1)
6208     and    NVL(serial_number, g_nvl_str) = NVL(p_serial_num, g_nvl_str)
6209     order by start_date for update;
6210 
6211 cursor wsm_res_usage (p_wip_entity_id number,
6212                       p_operation_seq_num number,
6213                       p_resource_seq_num number) is
6214     select start_date,
6215            completion_date,
6216            cumulative_processing_time
6217     from   wsm_copy_op_resource_usage
6218     where  wip_entity_id = p_wip_entity_id
6219     and    operation_seq_num = p_operation_seq_num
6220     and    resource_seq_num = p_resource_seq_num
6221     and    NVL(instance_id, -1)          = NVL(p_instance_id, -1)
6222     and    NVL(serial_number, g_nvl_str) = NVL(p_serial_num, g_nvl_str)
6223     order by start_date for update;
6224 
6225 current_cpt number := 0;
6226 l_stmt_num  number;
6227 
6228 begin
6229 
6230     if (p_handle_wip_cur_op = 'Y') then
6231 l_stmt_num  := 10;
6232         for wip_row in wip_res_usage(p_wip_entity_id, p_operation_seq_num, p_resource_seq_num)
6233         loop
6234            current_cpt := current_cpt + wip_datetimes.datetime_diff_to_mins(
6235                                                 wip_row.completion_date,
6236                                                 wip_row.start_date);
6237            update wip_operation_resource_usage
6238            set    cumulative_processing_time = current_cpt
6239            where current of wip_res_usage;
6240         end loop;
6241     else
6242 
6243 l_stmt_num  := 20;
6244         for wsm_row in wsm_res_usage(p_wip_entity_id, p_operation_seq_num, p_resource_seq_num)
6245         loop
6246            current_cpt := current_cpt + wip_datetimes.datetime_diff_to_mins(
6247                                                 wsm_row.completion_date,
6248                                                 wsm_row.start_date);
6249            update wsm_copy_op_resource_usage
6250            set    cumulative_processing_time = current_cpt
6251            where current of wsm_res_usage;
6252         end loop;
6253     end if;
6254 
6255 end update_cumulative_time;
6256 
6257 
6258 
6259 -- **********************************************************************************************
6260 -- PROCESS_INTERFACE_ROWS
6261 -- **********************************************************************************************
6262 
6263 PROCEDURE  process_lbji_rows (
6264         retcode             out nocopy number,
6265         errbuf              out nocopy varchar2,
6266         p_group_id          in  number) is
6267 
6268 cursor c_wlji is
6269     select  header_id,
6270             group_id,
6271             organization_id,
6272             organization_code,
6273             wip_entity_id,
6274             job_name,
6275             load_type,
6276             process_status,
6277             error_code,
6278             error_msg,
6279             num_of_children
6280     from    wsm_lot_job_interface
6281     where   process_status = WIP_CONSTANTS.PENDING
6282     and     NVL(transaction_date, creation_date) <= sysdate+1
6283     and     NVL(group_id, -99) = NVL(p_group_id, NVL(group_id, -99))
6284     and     load_type in (5,6)
6285     order by load_type;
6286 
6287 cursor c_wljdi_no_parent is
6288     select  record_id,
6289             group_id,
6290             organization_id,
6291             organization_code,
6292             wip_entity_id,
6293             job_name,
6294             process_status,
6295             error_code,
6296             error_msg
6297     from    wsm_lot_job_dtl_interface
6298     where   process_status = WIP_CONSTANTS.PENDING
6299     and     parent_header_id IS NULL
6300     and     NVL(group_id, -99) = NVL(p_group_id, NVL(group_id, -99))
6301     and     transaction_date <= sysdate+1;
6302 
6303 
6304 l_header            number;
6305 l_group_id          number;
6306 l_worker_reqid      number;
6307 l_stmt_num          number;
6308 conc_status         boolean;
6309 row_count           number := 0;
6310 l_error_count       number := 0;
6311 
6312 l_temp_num          number := 0;
6313 l_h_rows            number := 0;
6314 l_h_valid_rows      number := 0;    -- num of valid rows in WLJI
6315 l_d_rows            number := 0;
6316 l_d_valid_rows      number := 0;    -- num of valid rows in WLJDI
6317 l_num_jobs          number;
6318 l_num_jobs_per      number := 30;
6319 l_err_msg           varchar2(2000);
6320 l_err_code          number;
6321 l_xst               boolean;
6322 l_h                 number;
6323 l_d                 number;
6324 l_wrk_cnt           number;
6325 l_job_cnt           number;
6326 
6327 l_reqt_id           number := fnd_global.conc_request_id;
6328 l_prog_id           number := fnd_global.conc_program_id;
6329 l_prog_appl_id      number := fnd_global.prog_appl_id;
6330 l_login_id          number := fnd_global.login_id;
6331 l_user_id           number := fnd_global.user_id;
6332 l_resp_id           number := fnd_global.resp_id;
6333 l_resp_appl_id      number := fnd_global.resp_appl_id;
6334 
6335 l_v_group_id        t_number;
6336 l_v_job_gid         t_number;
6337 l_v_group_no        t_number;
6338 
6339 l_job_name_hash     number;
6340 e_launch_worker     exception;
6341 
6342 
6343 BEGIN
6344 
6345 l_stmt_num := 10;
6346 
6347     fnd_file.put_line(fnd_file.log, 'Processing Interface rows...');
6348     retcode := 0;
6349     -- CZHDBG: comment out this because the completion status of the
6350     -- concurrent program cannot be set properly with this call. Why ???
6351     --fnd_global.apps_initialize(l_user_id, l_resp_id, l_resp_appl_id);
6352 
6353 l_stmt_num := 20;
6354     -- load organization PL/SQL table
6355     WSMPLCVA.load_org_table;
6356 
6357     ----------------------------------------------------------------
6358     -- {{ Check PENDING WLJI records, validate org, job id/name
6359     ----------------------------------------------------------------
6360 l_stmt_num := 30;
6361     open c_wlji;
6362     fetch c_wlji bulk collect into
6363         v_wlji_header_id,
6364         v_wlji_group_id,
6365         v_wlji_org_id,
6366         v_wlji_org_code,
6367         v_wlji_wip_entity_id,
6368         v_wlji_job_name,
6369         v_wlji_load_type,
6370         v_wlji_process_status,
6371         v_wlji_err_code,
6372         v_wlji_err_msg,
6373         v_wlji_num_of_children;
6374     if lbji_debug = 'Y' then
6375         fnd_file.put_line(fnd_file.log,
6376             'Number of records in WSM_LOT_JOB_INTERFACE: ' || c_wlji%rowcount);
6377     end if;
6378     l_h_rows := c_wlji%rowcount;
6379     close c_wlji;
6380 
6381     l_h_valid_rows := 0;
6382     if(l_h_rows > 0) then
6383         l_h := v_wlji_header_id.first;
6384         WHILE l_h <= v_wlji_header_id.last
6385         LOOP
6386             v_wlji_err_code(l_h)         := 0;
6387             v_wlji_err_msg(l_h)          := '';
6388             v_wlji_process_status(l_h)   := WIP_CONSTANTS.RUNNING;
6389 
6390             Declare
6391                 e_org_weid_error        exception;
6392             Begin -- {{ check org/job_name for this header_id
6393 l_stmt_num := 40;
6394                 -- Valiadte Org Id
6395                 validate_org_id_name (
6396                     v_wlji_org_id(l_h),
6397                     v_wlji_org_code(l_h),
6398                     l_err_code,
6399                     l_err_msg);
6400                 if l_err_code = -1 then
6401                     raise e_org_weid_error;
6402                 end if;
6403 l_stmt_num := 50;
6404                 -- Valiadte Job Name / WIP Entity ID
6405                 validate_wip_entity_id_name (
6406                     v_wlji_load_type(l_h),
6407                     v_wlji_org_id(l_h),
6408                     v_wlji_wip_entity_id(l_h),
6409                     v_wlji_job_name(l_h),
6410                     l_err_code,
6411                     l_err_msg);
6412                 if l_err_code = -1 then
6413                     raise e_org_weid_error;
6414                 end if;
6415 l_stmt_num := 60;
6416                 -- add valid job name to g_v_job_name
6417                 l_h_valid_rows := l_h_valid_rows + 1;
6418                 l_job_name_hash := dbms_utility.get_hash_value(
6419                                     v_wlji_job_name(l_h) || v_wlji_org_id(l_h),
6420                                     37, 1073741824);
6421                 g_v_job_name(l_job_name_hash)      := v_wlji_job_name(l_h);
6422                 g_v_wip_entity_id(l_job_name_hash) := v_wlji_wip_entity_id(l_h);
6423 
6424             Exception
6425 
6426                 when e_org_weid_error then
6427 l_stmt_num := 70;
6428                     v_wlji_process_status(l_h) := WIP_CONSTANTS.ERROR;
6429                     v_wlji_err_code(l_h) := -1;
6430                     v_wlji_err_msg(l_h) := l_err_msg;
6431                     fnd_file.put_line(fnd_file.log,
6432                         'Error (WLJI.Header_Id = ' || v_wlji_header_id(l_h) || ') : '
6433                         || l_err_msg);
6434                     l_error_count := l_error_count + 1;
6435             End; -- }} check org/job_name for this header_id
6436 
6437             l_h := v_wlji_header_id.next(l_h);
6438         END LOOP;
6439 
6440     end if;  -- if l_h_rows <>0
6441     ----------------------------------------------------------------
6442     -- }} Check PENDING WLJI records, validate org, job id/name
6443     ----------------------------------------------------------------
6444 
6445 
6446     -----------------------------------------------------------------
6447     -- {{ Check PENDING WLJDI records with parent_header_id = NULL
6448     -----------------------------------------------------------------
6449     if lbji_debug = 'Y' then
6450         fnd_file.put_line(fnd_file.log,
6451             'Checking wsm_lot_job_dtl_interface..');
6452     end if;
6453 
6454 l_stmt_num := 100;
6455     open c_wljdi_no_parent;
6456     fetch c_wljdi_no_parent bulk collect into
6457         v_wljdi_record_id,
6458         v_wljdi_group_id,
6459         v_wljdi_org_id,
6460         v_wljdi_org_code,
6461         v_wljdi_wip_entity_id,
6462         v_wljdi_job_name,
6463         v_wljdi_process_status,
6464         v_wljdi_err_code,
6465         v_wljdi_err_msg;
6466     if lbji_debug = 'Y' then
6467         fnd_file.put_line(fnd_file.log,
6468             'Number of independent records in WSM_LOT_JOB_DTL_INTERFACE: '
6469             || c_wljdi_no_parent%rowcount);
6470     end if;
6471     l_d_rows := c_wljdi_no_parent%rowcount;
6472     close c_wljdi_no_parent;
6473 
6474     l_d_valid_rows := 0;
6475     if(l_d_rows > 0) then
6476         l_d := v_wljdi_record_id.first;
6477         WHILE l_d <= v_wljdi_record_id.last
6478         LOOP
6479             v_wljdi_err_code(l_d)        := 0;
6480             v_wljdi_err_msg(l_d)         := NULL;
6481             v_wljdi_process_status(l_d)  := WIP_CONSTANTS.RUNNING;
6482 
6483             Declare
6484                 e_org_weid_error        exception;
6485             Begin -- {{ check org/job_name for this header_id
6486 l_stmt_num := 110;
6487                 -- Valiadte Org Id
6488                 validate_org_id_name (
6489                     v_wljdi_org_id(l_d),
6490                     v_wljdi_org_code(l_d),
6491                     l_err_code,
6492                     l_err_msg);
6493                 if l_err_code = -1 then
6494                     raise e_org_weid_error;
6495                 end if;
6496 l_stmt_num := 120;
6497                 -- Valiadte Job Name / WIP Entity ID
6498                 validate_wip_entity_id_name (
6499                     6,  -- Job update
6500                     v_wljdi_org_id(l_d),
6501                     v_wljdi_wip_entity_id(l_d),
6502                     v_wljdi_job_name(l_d),
6503                     l_err_code,
6504                     l_err_msg);
6505                 if l_err_code = -1 then
6506                     raise e_org_weid_error;
6507                 end if;
6508 
6509                 -- add valid job name to g_v_job_name
6510                 l_d_valid_rows := l_d_valid_rows + 1;
6511                 l_job_name_hash := dbms_utility.get_hash_value(
6512                                     v_wljdi_job_name(l_d) || v_wljdi_org_id(l_d),
6513                                     37, 1073741824);
6514                 g_v_job_name(l_job_name_hash)      := v_wljdi_job_name(l_d);
6515                 g_v_wip_entity_id(l_job_name_hash) := v_wljdi_wip_entity_id(l_d);
6516 
6517             Exception
6518                 when e_org_weid_error then
6519 l_stmt_num := 130;
6520                     v_wljdi_process_status(l_d) := WIP_CONSTANTS.ERROR;
6521                     v_wljdi_err_code(l_d) := -1;
6522                     v_wljdi_err_msg(l_d) := l_err_msg;
6523                     fnd_file.put_line(fnd_file.log,
6524                         'Error: WLJDI(Record_Id = ' || v_wljdi_record_id(l_d) ||') '
6525                         || l_err_msg);
6526                     fnd_file.new_line(fnd_file.log, 3);
6527                     l_error_count := l_error_count +1;
6528 
6529             End; -- }} check org/job_name for this header_id
6530 
6531             l_d := v_wljdi_record_id.next(l_d);
6532         END LOOP;
6533 
6534     end if; -- if l_d_rows <> 0
6535     -----------------------------------------------------------------
6536     -- }} Check PENDING WLJDI records with parent_header_id = NULL
6537     -----------------------------------------------------------------
6538 
6539 
6540     -- Now, all the valid records in WLJI and WLJDI has valid Job Name
6541     -- Will balance worker load according to the number of jobs, will
6542     -- assign all the records of the same job to one worker
6543     -- When p_group_id is given, assign all the job with that group_id to
6544     -- one worker
6545     l_num_jobs := g_v_job_name.COUNT;
6546     if lbji_debug = 'Y' then
6547         fnd_file.put_line(fnd_file.log,
6548             'Number of jobs to be processed: '|| l_num_jobs);
6549     end if;
6550 
6551     if p_group_id is not null then
6552         if(l_h_rows > 0) then
6553 l_stmt_num := 200;
6554             -- Update WLJDI for all record with parent_header_id = header_id
6555             l_h := v_wlji_header_id.first;
6556             WHILE l_h <= v_wlji_header_id.last
6557             LOOP
6558                 update wsm_lot_job_dtl_interface
6559                 set     process_status      = v_wlji_process_status(l_h),
6560                         group_id            = p_group_id,
6561                         organization_id     = v_wlji_org_id(l_h),
6562                         organization_code   = v_wlji_org_code(l_h),
6563                         wip_entity_id       = v_wlji_wip_entity_id(l_h),
6564                         job_name            = v_wlji_job_name(l_h),
6565                         -- BUG3600450 commented
6566                         --error_code          = v_wlji_err_code(l_h),
6567                         --error_msg           = 'See header (header_id=' ||
6568                         --                      v_wlji_header_id(l_h) || ') for error message',
6569                         program_id          = l_prog_id,
6570                         program_update_date = sysdate
6571                 where   parent_header_id = v_wlji_header_id(l_h);
6572                 v_wlji_num_of_children(l_h) := SQL%ROWCOUNT;
6573                 l_h := v_wlji_header_id.next(l_h);
6574             END LOOP;
6575 
6576 l_stmt_num := 210;
6577             -- bulk update wsm_lot_job_interface
6578             forall l_h in v_wlji_header_id.first..v_wlji_header_id.last
6579             update  wsm_lot_job_interface
6580             set     process_status              = v_wlji_process_status(l_h),
6581                     organization_id             = v_wlji_org_id(l_h),
6582                     organization_code           = v_wlji_org_code(l_h),
6583                     wip_entity_id               = v_wlji_wip_entity_id(l_h),
6584                     job_name                    = v_wlji_job_name(l_h),
6585                     error_code                  = v_wlji_err_code(l_h),
6586                     error_msg                   = v_wlji_err_msg(l_h),
6587                     num_of_children             = v_wlji_num_of_children(l_h),
6588                     program_id                  = l_prog_id,
6589                     program_update_date         = sysdate
6590             where   header_id = v_wlji_header_id(l_h);
6591 
6592         end if;  -- if l_h_rows <>0
6593 
6594         if(l_d_rows > 0) then
6595 l_stmt_num := 220;
6596             forall l_d in v_wljdi_record_id.first..v_wljdi_record_id.last
6597             update  wsm_lot_job_dtl_interface
6598             set     process_status              = v_wljdi_process_status(l_d),
6599                     organization_id             = v_wljdi_org_id(l_d),
6600                     organization_code           = v_wljdi_org_code(l_d),
6601                     wip_entity_id               = v_wljdi_wip_entity_id(l_d),
6602                     job_name                    = v_wljdi_job_name(l_d),
6603                     error_code                  = v_wljdi_err_code(l_d),
6604                     error_msg                   = v_wljdi_err_msg(l_d),
6605                     program_id                  = l_prog_id,
6606                     program_update_date         = sysdate
6607             where   record_id = v_wljdi_record_id(l_d);
6608         end if; -- if l_d_rows <> 0
6609 
6610 l_stmt_num := 230;
6611         if l_num_jobs > 0 then
6612             l_worker_reqid := FND_REQUEST.SUBMIT_REQUEST (
6613                             application => 'WSM',
6614                             program     => 'WSMLNCHW',
6615                             sub_request => FALSE,
6616                             argument1   => p_group_id,
6617                             argument2   => l_num_jobs );
6618             if l_worker_reqid = 0 then
6619                 raise e_launch_worker;
6620             else
6621                 COMMIT;
6622             end if;
6623             fnd_file.put_line(fnd_file.log, 'Request_id: '||l_worker_reqid||' submitted');
6624         else -- l_num_jobs <= 0
6625             fnd_file.put_line(fnd_file.log, 'No rows found in interface table');
6626         end if;
6627 
6628     else -- p_group_id is null
6629 
6630         if l_num_jobs > 0 then
6631 l_stmt_num := 300;
6632 
6633             if mod(l_num_jobs, g_no_of_workers) = 0 then
6634                 l_num_jobs_per := l_num_jobs / g_no_of_workers;
6635             else
6636                 l_num_jobs_per := floor(l_num_jobs / g_no_of_workers) + 1;
6637             end if;
6638 
6639             if l_num_jobs_per < g_batch_size then
6640                 l_num_jobs_per := g_batch_size;
6641             end if;
6642 
6643             l_job_cnt := 0;
6644             l_wrk_cnt := 0;
6645 
6646 l_stmt_num := 310;
6647             -- {{ balance load among workers by setting group_id
6648             if(l_h_rows > 0) then
6649                 l_h := v_wlji_header_id.first;
6650                 WHILE l_h <= v_wlji_header_id.last
6651                 LOOP
6652                     if v_wlji_process_status(l_h) <> WIP_CONSTANTS.ERROR then
6653                         l_job_name_hash := dbms_utility.get_hash_value(
6654                                             v_wlji_job_name(l_h) || v_wlji_org_id(l_h),
6655                                             37, 1073741824);
6656                         if not l_v_job_gid.exists(l_job_name_hash) then -- one new job name
6657                             if mod(l_job_cnt, l_num_jobs_per) = 0 then
6658                                 l_wrk_cnt := l_wrk_cnt + 1;  -- one more worker
6659                                 -- get a new group id
6660                                 select wsm_lot_job_interface_s.NEXTVAL
6661                                 into   l_group_id
6662                                 from   dual;
6663                                 l_v_group_id(l_wrk_cnt) := l_group_id;
6664                                 l_v_group_no(l_wrk_cnt) := 0;
6665                             end if;
6666                             l_job_cnt := l_job_cnt+1;
6667                             l_v_group_no(l_wrk_cnt) := l_v_group_no(l_wrk_cnt)+1;
6668                             l_v_job_gid(l_job_name_hash) := l_v_group_id(l_wrk_cnt);
6669                         end if;
6670                         v_wlji_group_id(l_h) := l_v_job_gid(l_job_name_hash);
6671                     end if;
6672                     l_h := v_wlji_header_id.next(l_h);
6673                 END LOOP;
6674             end if;
6675 
6676 l_stmt_num := 320;
6677             if(l_d_rows > 0) then
6678                 l_d := v_wljdi_record_id.first;
6679                 WHILE l_d <= v_wljdi_record_id.last
6680                 LOOP
6681                     if v_wljdi_process_status(l_d) <> WIP_CONSTANTS.ERROR then
6682                         l_job_name_hash := dbms_utility.get_hash_value(
6683                                             v_wljdi_job_name(l_d) || v_wljdi_org_id(l_d),
6684                                             37, 1073741824);
6685                         if not l_v_job_gid.exists(l_job_name_hash) then -- a new job
6686                             if mod(l_job_cnt, l_num_jobs_per) = 0 then
6687                                 l_wrk_cnt := l_wrk_cnt +1;  -- one more worker
6688                                 select wsm_lot_job_interface_s.NEXTVAL
6689                                 into   l_group_id
6690                                 from   dual;
6691                                 l_v_group_id(l_wrk_cnt) := l_group_id;
6692                                 l_v_group_no(l_wrk_cnt) := 0;
6693                             end if;
6694                             l_job_cnt := l_job_cnt+1;
6695                             l_v_group_no(l_wrk_cnt) := l_v_group_no(l_wrk_cnt)+1;
6696                             l_v_job_gid(l_job_name_hash) := l_group_id;
6697                         end if;
6698                         v_wljdi_group_id(l_d) := l_v_job_gid(l_job_name_hash);
6699                     end if;
6700                     l_d := v_wljdi_record_id.next(l_d);
6701                 END LOOP;
6702             end if;
6703             -- }} balance load among workers by setting group_id
6704 
6705         end if; -- if l_num_jobs > 0
6706 
6707         if(l_h_rows > 0) then
6708 
6709 l_stmt_num := 330;
6710             -- Update WLJDI for all record with parent_header_id = header_id
6711             l_h := v_wlji_header_id.first;
6712             WHILE l_h <= v_wlji_header_id.last
6713             LOOP
6714                 update wsm_lot_job_dtl_interface
6715                 set     process_status          = v_wlji_process_status(l_h),
6716                         group_id                = v_wlji_group_id(l_h),
6717                         organization_id         = v_wlji_org_id(l_h),
6718                         organization_code       = v_wlji_org_code(l_h),
6719                         wip_entity_id           = v_wlji_wip_entity_id(l_h),
6720                         job_name                = v_wlji_job_name(l_h),
6721                         error_code              = v_wlji_err_code(l_h),
6722                         error_msg               = v_wlji_err_msg(l_h),
6723                         program_id              = l_prog_id,
6724                         program_update_date     = sysdate
6725                 where   parent_header_id = v_wlji_header_id(l_h);
6726                 v_wlji_num_of_children(l_h) := SQL%ROWCOUNT;
6727                 l_h := v_wlji_header_id.next(l_h);
6728             END LOOP;
6729 
6730 l_stmt_num := 340;
6731             -- bulk update wsm_lot_job_interface
6732             forall l_h in v_wlji_header_id.first..v_wlji_header_id.last
6733             update  wsm_lot_job_interface
6734             set     process_status              = v_wlji_process_status(l_h),
6735                     group_id                    = v_wlji_group_id(l_h), -- assigned gid
6736                     organization_id             = v_wlji_org_id(l_h),
6737                     organization_code           = v_wlji_org_code(l_h),
6738                     wip_entity_id               = v_wlji_wip_entity_id(l_h),
6739                     job_name                    = v_wlji_job_name(l_h),
6740                     error_code                  = v_wlji_err_code(l_h),
6741                     error_msg                   = v_wlji_err_msg(l_h),
6742                     num_of_children             = v_wlji_num_of_children(l_h),
6743                     program_id                  = l_prog_id,
6744                     program_update_date         = sysdate
6745             where   header_id = v_wlji_header_id(l_h);
6746 
6747         end if;  -- if l_h_rows <>0
6748 
6749         if(l_d_rows > 0) then
6750 l_stmt_num := 350;
6751             forall l_d in v_wljdi_record_id.first..v_wljdi_record_id.last
6752             update  wsm_lot_job_dtl_interface
6753             set     process_status              = v_wljdi_process_status(l_d),
6754                     group_id                    = v_wljdi_group_id(l_d), -- assigned gid
6755                     organization_id             = v_wljdi_org_id(l_d),
6756                     organization_code           = v_wljdi_org_code(l_d),
6757                     wip_entity_id               = v_wljdi_wip_entity_id(l_d),
6758                     job_name                    = v_wljdi_job_name(l_d),
6759                     error_code                  = v_wljdi_err_code(l_d),
6760                     error_msg                   = v_wljdi_err_msg(l_d),
6761                     program_id                  = l_prog_id,
6762                     program_update_date         = sysdate
6763             where   record_id = v_wljdi_record_id(l_d);
6764         end if; -- if l_d_rows <> 0
6765 
6766         if l_num_jobs > 0 then
6767             fnd_file.put_line(fnd_file.log, 'Total pending jobs = '       || l_num_jobs);
6768             fnd_file.put_line(fnd_file.log, 'Number of workers = '        || g_no_of_workers);
6769             fnd_file.put_line(fnd_file.log, 'Batch size = '               || g_batch_size);
6770             fnd_file.put_line(fnd_file.log, 'Number of jobs per worker = '|| l_num_jobs_per);
6771             fnd_file.put_line(fnd_file.log, 'Number of workers launched= '|| l_wrk_cnt);
6772 
6773 l_stmt_num := 360;
6774             l_temp_num := l_v_group_id.first;
6775             WHILE l_temp_num <= l_v_group_id.last
6776             LOOP
6777                 l_worker_reqid := FND_REQUEST.SUBMIT_REQUEST (
6778                                 application => 'WSM',
6779                                 program     => 'WSMLNCHW',
6780                                 sub_request => FALSE,
6781                                 argument1   => l_v_group_id(l_temp_num),
6782                                 argument2   => l_v_group_no(l_temp_num) );
6783                 if l_worker_reqid = 0 then
6784                     raise e_launch_worker;
6785                 end if;
6786                 l_temp_num := l_v_group_id.next(l_temp_num);
6787             END LOOP;
6788             COMMIT;
6789 
6790         end if; -- if l_d_rows <> 0
6791 
6792     end if; -- p_group_id is null
6793 
6794     if l_error_count <> 0 then
6795         retcode := 1;
6796         errbuf := 'The manager marked at least one row as errored';
6797         fnd_file.put_line(fnd_file.log,errbuf);
6798         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
6799         COMMIT;
6800     end if;
6801 
6802 EXCEPTION
6803 
6804     when e_launch_worker then
6805         rollback;
6806         retcode := 1;
6807         errbuf := 'Error: process_interface_rows: (#'||l_stmt_num||') fail to launch worker';
6808         fnd_file.put_line(fnd_file.log,errbuf);
6809         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',errbuf);
6810         COMMIT;
6811 
6812     when others then
6813         rollback;
6814         retcode := 1;
6815         errbuf := 'Error: process_interface_rows: (#'||l_stmt_num||') '
6816                   || SUBSTR(SQLERRM, 1,240);
6817         fnd_file.put_line(fnd_file.log,errbuf);
6818         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',errbuf);
6819         COMMIT;
6820 
6821 END process_lbji_rows;
6822 
6823 
6824 
6825 -- **********************************************************************************************
6826 -- LAUNCH WORKER
6827 -- **********************************************************************************************
6828 
6829 PROCEDURE  launch_worker(
6830         retcode             out nocopy number,
6831         errbuf              out nocopy varchar2,
6832         p_group_id          in  number,
6833         p_alotted_jobs      in  number  ) is
6834 
6835 -- ==============================================================================================
6836 -- cursors used to bulk bind data from wlji to PL/SQL tables
6837 -- ==============================================================================================
6838 cursor csr_wlji is
6839     select   error_code,
6840              error_msg,
6841              --error,
6842              last_update_date,
6843              request_id,
6844              program_id,
6845              program_application_id,
6846              last_updated_by,
6847              creation_date,
6848              created_by,
6849              last_update_login,
6850              program_update_date,
6851              last_updated_by_name,
6852              created_by_name,
6853              organization_id,
6854              organization_code,
6855              wip_entity_id,
6856              job_name,
6857              lot_number,
6858              primary_item_id,
6859              header_id,
6860              process_status,
6861              --process_phase,
6862              routing_reference_id,
6863              completion_subinventory,
6864              completion_locator_id,
6865              mode_flag,
6866              group_id,
6867              load_type,
6868              status_type,
6869              --old_start_quantity,
6870              --old_completion_date,
6871              bom_reference_id,
6872              bom_revision_date,
6873              routing_revision_date,
6874              wip_supply_type,
6875              class_code,
6876              description,
6877              firm_planned_flag,
6878              alternate_routing_designator,
6879              alternate_bom_designator,
6880              demand_class,
6881              start_quantity,
6882              first_unit_start_date,
6883              --first_unit_completion_date,
6884              --last_unit_start_date,
6885              last_unit_completion_date,
6886              scheduling_method,
6887              routing_revision,
6888              bom_revision,
6889              schedule_group_id,
6890              schedule_group_name,
6891              build_sequence,
6892              net_quantity,
6893              allow_explosion,
6894              --old_status_type,
6895              --interface_id,
6896              coproducts_supply,
6897              source_code,
6898              source_line_id,
6899              process_type,
6900              --processing_work_days,
6901              --daily_production_rate,
6902              line_id,
6903              --line_code,
6904              --lot_control_code,
6905              --repetitive_schedule_id,
6906              --parent_group_id,
6907              attribute_category,
6908              attribute1,
6909              attribute2,
6910              attribute3,
6911              attribute4,
6912              attribute5,
6913              attribute6,
6914              attribute7,
6915              attribute8,
6916              attribute9,
6917              attribute10,
6918              attribute11,
6919              attribute12,
6920              attribute13,
6921              attribute14,
6922              attribute15,
6923              --primary_item_segments,
6924              --bom_reference_segments,
6925              --routing_reference_segments,
6926              --completion_locator_segments,
6927              --descriptive_flex_segments,
6928              project_id,
6929              --project_name,
6930              --project_number,
6931              --project_costed,
6932              task_id,
6933              --task_name,
6934              --task_number,
6935              delivery_id,
6936              end_item_unit_number,
6937              overcompletion_tolerance_type,
6938              overcompletion_tolerance_value,
6939              kanban_card_id,
6940              priority,
6941              due_date,
6942              job_type,
6943              date_released,
6944              transaction_date,
6945              num_of_children
6946              --, NVL(infinite_schedule_flag, 'N')
6947     from     wsm_lot_job_interface
6948     where    group_id = p_group_id
6949     and      process_status = 2 -- WIP_CONSTANTS.RUNNING;
6950     order by organization_id,
6951              load_type,
6952              NVL(transaction_date, creation_date),
6953              priority,
6954              due_date;
6955 
6956 cursor c_wljdi_jobs_no_parent is
6957     select  unique
6958             wip_entity_id,
6959             organization_id
6960     from    wsm_lot_job_dtl_interface
6961     where   process_status = WIP_CONSTANTS.RUNNING
6962     and     parent_header_id IS NULL
6963     and     group_id = p_group_id
6964     and     transaction_date <= sysdate+1
6965     order by organization_id;
6966 
6967 -- ==============================================================================================
6968 -- other variables
6969 -- ==============================================================================================
6970 
6971 -- control variables
6972 prev_rowcount                   number := 0;
6973 req_phase                       varchar2(2000);
6974 req_status                      varchar2(2000);
6975 req_devphase                    varchar2(2000);
6976 req_devstatus                   varchar2(2000);
6977 req_message                     varchar2(1000);
6978 req_wait                        boolean;
6979 conc_status                     boolean;
6980 
6981 l_del_int_prof_value            number;
6982 l_stmt_num                      number;
6983 l_error_count                   number := 0;
6984 l_warning_count                 number := 0;
6985 l_atleast_one_row_in_mmtt       number := 0;
6986 l_inv_worker_req_id             number := 0;
6987 l_mmtt_txn_hdr_id               number;
6988 l_txn_tmp_hdr_id                number;
6989 l_return_value                  number := 0;
6990 l_error_code                    number := 0;
6991 l_error_msg                     varchar2(2000) := null;
6992 l_aux_mesg                      varchar2(240)  := null;
6993 l_skip_update_wo                number;
6994 l_return_status                 varchar2(1);
6995 l_num_records                   number := 0;
6996 l_batch_grp_id                  number;
6997 l_wsli_loaded                   boolean;
6998 l_req_request_id                number;
6999 l_temp_num                      number := 0;
7000 l_temp_class                    varchar2(10);
7001 l_temp_boolean                  boolean;
7002 l_date_text                     varchar2(100);
7003 l_xst                           boolean := true;
7004 l_user                          number := fnd_global.user_id;
7005 l_h                             number;         -- index for WLJI records
7006 l_d                             number;         -- index for WLJDI records
7007 
7008 dummy_number                    number;
7009 dummy_varchar                   varchar2(3);
7010 dummy_date                      date;
7011 dummy_err_code                  number;
7012 dummy_err_msg                   varchar2(2000);
7013 dummy_segs                      varchar2(10000); -- dummy large varchar2
7014 
7015 -- local variables
7016 hash_str                        varchar2(100);
7017 hash_value                      number;
7018 l_rtg_seq_id                    number;
7019 l_common_rtg_seq_id             number;
7020 l_common_bom_seq_id             number;
7021 l_default_compl_subinv          varchar2(10);
7022 l_default_compl_loc_id          number;
7023 l_sub_loc_control               number;
7024 l_org_loc_control               number;
7025 l_item_loc_control              number;
7026 l_restrict_locators_code        number;
7027 l_item_id                       number;
7028 l_min_op_seq_num                number;
7029 l_department_id                 number;
7030 l_scrap_account_10              number;
7031 l_est_scrap_abs_account_10      number;
7032 l_rev_date                      date;
7033 l_source_item_rev               varchar2(3);
7034 --l_source_item_rev_date        date;           -- Del: bug 2963225
7035 l_rev_control_code              number;         -- Add: bug 2963225
7036 l_start_lot_revision            number;         -- Add: bug 2963225
7037 l_locator_id                    number;
7038 l_job_type                      number;
7039 l_kanban_size                   number;
7040 l_qoh                           number;
7041 l_att                           number;
7042 l_atr                           number;
7043 l_component_yield_factor        number;
7044 l_component_quantity            number;
7045 l_required_qpa                  number;
7046 l_quantity_tobe_issued          number;
7047 l_start_op_seq_id               number;
7048 l_start_op_seq_num              number;
7049 l_txnexist                      number;
7050 l_qntydiff                      number;
7051 l_mtl_locator_type              number;
7052 l_osp_op_seq_num                number;
7053 l_est_scrap_account             number;
7054 l_est_scrap_var_account         number;
7055 l_src_lot_number                wsm_starting_lots_interface.lot_number%type;
7056 l_src_inv_item_id               NUMBER;
7057 l_scheduling_mode               NUMBER; -- ADD BUG 3479666
7058 
7059 l_job_type_code                 varchar2(30);
7060 l_assembly_name                 varchar2(40);
7061 l_org_code                      varchar2(3);
7062 l_meaning                       varchar2(240);
7063 
7064 -------------------------------------------------------------------
7065 -- Attention: all these l_cur_% variables are used to store current
7066 -- values of a job for UPDATE, please do not change their values !!
7067 -------------------------------------------------------------------
7068 l_cur_coprod_supply             number;
7069 l_cur_schd_start_date           date;
7070 l_cur_schd_compl_date           date;
7071 l_cur_primary_item_id           number;
7072 l_cur_class_code                varchar2(10);
7073 l_cur_start_date                date;
7074 l_cur_complete_date             date;
7075 l_cur_start_qty                 number;
7076 l_cur_net_qty                   number;
7077 l_cur_bom_revision              varchar2(3);
7078 l_cur_rtg_revision              varchar2(3);
7079 l_cur_status_type               number:=0;
7080 l_cur_firm_planned_flag         number;
7081 l_cur_job_type                  number;
7082 l_cur_date_released             date;
7083 l_cur_bom_ref_id                number;
7084 l_cur_alt_bom                   varchar2(10);
7085 l_cur_rtg_ref_id                number;
7086 l_cur_alt_rtg                   varchar2(10);
7087 l_cur_bom_rev_date              date;
7088 l_cur_rtg_rev_date              date;
7089 l_cur_com_rtg_seq_id            number;
7090 l_cur_com_bom_seq_id            number;
7091 l_cur_wip_supply_type           number;
7092 l_cur_compl_subinv              varchar2(10);
7093 l_cur_compl_locator             number;
7094 l_cur_kanban_card_id            number;
7095 l_cur_schedule_group_id         number;
7096 l_cur_build_sequence            number;
7097 l_cur_line_id                   number;
7098 
7099 l_chg_bom_alt                   number;
7100 l_chg_rtg_alt                   number;
7101 l_chg_alt_flag                  number;
7102 l_chg_bom_ref                   number;
7103 l_chg_rtg_ref                   number;
7104 l_chg_ref_flag                  number;
7105 l_chg_rtg_rev                   number;     -- Add: bug3202534
7106 l_chg_bom_rev                   number;     -- Add: bug3202534
7107 l_chg_rev_flag                  number;     -- Add: bug3202534
7108 l_inf_sche_flag                 varchar2(1);
7109 --Bug 5112899: added follwoing columns
7110 l_basis_type                    NUMBER;
7111 l_include_yield                 NUMBER;
7112 l_bill_req_qpa                  NUMBER;
7113 l_mo_org_id                     NUMBER;                                   -- Add: bug5485653
7114 l_ou_id                         NUMBER;                                   -- Add: bug5485653
7115 l_org_acct_ctxt                 VARCHAR2(30):= 'Accounting Information';  -- Add: bug5485653
7116 
7117 l_wip_entity_name               varchar2(80); -- Add: bug 5242298
7118 l_dummy_job_name		varchar2(80); -- Add: bug 5242298
7119 
7120 /*  ADDED FOLLOWING VARIABLES FOR 12.1 SUBSTITUTE COMPONENT PROJECT ****************************************************
7121 **********************************************************************************************************************/
7122 
7123 l_is_src_lot_sub               NUMBER := 0;
7124 l_primary_comp_item_id         NUMBER := 0;
7125 l_sub_comp_bill_qpa            NUMBER := 0;
7126 l_sub_comp_qpa                 NUMBER := 0;
7127 l_segment1                     VARCHAR2(100);
7128 l_segment2                     VARCHAR2(100);
7129 l_segment3                     VARCHAR2(100);
7130 l_segment4                     VARCHAR2(100);
7131 l_segment5                     VARCHAR2(100);
7132 l_segment6                     VARCHAR2(100);
7133 l_segment7                     VARCHAR2(100);
7134 l_segment8                     VARCHAR2(100);
7135 l_segment9                     VARCHAR2(100);
7136 l_segment10                    VARCHAR2(100);
7137 l_segment11                    VARCHAR2(100);
7138 l_segment12                    VARCHAR2(100);
7139 l_segment13                    VARCHAR2(100);
7140 l_segment14                    VARCHAR2(100);
7141 l_segment15                    VARCHAR2(100);
7142 l_segment16                    VARCHAR2(100);
7143 l_segment17                    VARCHAR2(100);
7144 l_segment18                    VARCHAR2(100);
7145 l_segment19                    VARCHAR2(100);
7146 l_segment20                    VARCHAR2(100);
7147 
7148 
7149 
7150 /* END OF VARIABLES ADDED FOR 12.1 SUBSTITUTE COMPONENT PROJECT **********************************************************
7151 ***************************************************************************************************************************/
7152 -- defined exceptions
7153 e_invalid_id                    exception;
7154 e_invalid_job_name              exception;
7155 e_abort_request                 exception;
7156 e_build_job                     exception;
7157 e_update_job                    exception;
7158 e_invalid_qty                   exception;
7159 
7160 
7161 BEGIN   -- for launch_worker
7162 
7163     retcode := 0;       -- bugfix 2845397: set the code to 0 (success)
7164 
7165     SAVEPOINT savepoint_top;
7166 
7167 -- ==============================================================================================
7168 -- Bulk fetching data into PL/SQL tables for ease of validation
7169 -- ==============================================================================================
7170 
7171     begin
7172 l_stmt_num := 10;
7173         WSMPLCVA.load_org_table;
7174 
7175 l_stmt_num := 20;
7176         WSMPLCVA.load_subinventory;
7177 
7178 l_stmt_num := 30;
7179         WSMPLCVA.load_class_code;
7180         if lbji_debug = 'Y' then
7181             fnd_file.put_line(fnd_file.log,
7182                 'Loading org/subinv/class-code values into memory (once per worker).. OK.');
7183         end if;
7184     exception
7185         when others then
7186             raise e_abort_request;
7187     end;
7188 
7189 
7190 l_stmt_num := 40;
7191     open csr_wlji;
7192     LOOP  -- {{{ outer loop
7193 
7194         l_wsli_loaded := false; --i.e. run the load wsli routine for every batch
7195 
7196 -- ==============================================================================================
7197 -- Bulk fetching data from wlji to PL/SQL tables
7198 -- ==============================================================================================
7199 
7200         fetch csr_wlji bulk collect into
7201             v_wlji_err_code,
7202             v_wlji_err_msg,
7203             --v_wlji_error,
7204             v_wlji_last_updt_date,
7205             v_wlji_request_id,
7206             v_wlji_program_id,
7207             v_wlji_program_appl_id,
7208             v_wlji_last_updt_by,
7209             v_wlji_creation_date,
7210             v_wlji_created_by,
7211             v_wlji_last_updt_login,
7212             v_wlji_prog_updt_date,
7213             v_wlji_last_updt_by_name,
7214             v_wlji_created_by_name,
7215             v_wlji_org_id,
7216             v_wlji_org_code,
7217             v_wlji_wip_entity_id,
7218             v_wlji_job_name,
7219             v_wlji_lot_number,
7220             v_wlji_item_id,
7221             v_wlji_header_id,
7222             v_wlji_process_status,
7223             --v_wlji_process_phase,
7224             v_wlji_rtg_ref_id,
7225             v_wlji_compl_subinv,
7226             v_wlji_compl_loc_id,
7227             v_wlji_mode_flag,
7228             v_wlji_group_id,
7229             v_wlji_load_type,
7230             v_wlji_status_type,
7231             --v_wlji_old_start_quantity,
7232             --v_wlji_old_completion_date,
7233             v_wlji_bom_ref_id,
7234             v_wlji_bom_rev_date,
7235             v_wlji_rtg_rev_date,
7236             v_wlji_wip_supply_type,
7237             v_wlji_class_code,
7238             v_wlji_description,
7239             v_wlji_firm_planned_flag,
7240             v_wlji_alt_rtg,
7241             v_wlji_alt_bom,
7242             v_wlji_demand_class,
7243             v_wlji_start_quantity,
7244             v_wlji_fusd,
7245             --v_wlji_fucd,
7246             --v_wlji_lusd,
7247             v_wlji_lucd,
7248             v_wlji_scheduling_method,
7249             v_wlji_rtg_revision,
7250             v_wlji_bom_revision,
7251             v_wlji_schedule_group_id,
7252             v_wlji_schedule_group_name,
7253             v_wlji_build_sequence,
7254             v_wlji_net_quantity,
7255             v_wlji_allow_explosion,
7256             --v_wlji_old_status_type,
7257             --v_wlji_interface_id,
7258             v_wlji_coproducts_supply,
7259             v_wlji_source_code,
7260             v_wlji_src_line_id,
7261             v_wlji_process_type,
7262             --v_wlji_processing_work_days,
7263             --v_wlji_daily_production_rate,
7264             v_wlji_line_id,
7265             --v_wlji_line_code,
7266             --v_wlji_lot_control_code,
7267             --v_wlji_repetitive_schedule_id,
7268             --v_wlji_parent_group_id,
7269             v_wlji_attribute_category,
7270             v_wlji_attribute1,
7271             v_wlji_attribute2,
7272             v_wlji_attribute3,
7273             v_wlji_attribute4,
7274             v_wlji_attribute5,
7275             v_wlji_attribute6,
7276             v_wlji_attribute7,
7277             v_wlji_attribute8,
7278             v_wlji_attribute9,
7279             v_wlji_attribute10,
7280             v_wlji_attribute11,
7281             v_wlji_attribute12,
7282             v_wlji_attribute13,
7283             v_wlji_attribute14,
7284             v_wlji_attribute15,
7285             --v_wlji_primary_item_segs,
7286             --v_wlji_bom_ref_segs,
7287             --v_wlji_rtg_ref_segs,
7288             --v_wlji_compl_loc_segs,
7289             --v_wlji_desc_flx_segs,
7290             v_wlji_project_id,
7291             --v_wlji_project_name,
7292             --v_wlji_project_number,
7293             --v_wlji_project_costed,
7294             v_wlji_task_id,
7295             --v_wlji_task_name,
7296             --v_wlji_task_number,
7297             v_wlji_delivery_id,
7298             v_wlji_end_item_unit_number,
7299             v_wlji_overcompl_tol_type,
7300             v_wlji_overcompl_tol_value,
7301             v_wlji_kanban_card_id,
7302             v_wlji_priority,
7303             v_wlji_due_date,
7304             v_wlji_job_type,
7305             v_wlji_date_released,   --bugfix 2697295
7306             v_wlji_transaction_date,
7307             v_wlji_num_of_children
7308             --, v_wlji_inf_schedule_flag
7309         limit g_batch_size;
7310         if lbji_debug = 'Y' then
7311             fnd_file.put_line(fnd_file.log,
7312                 'Number of rows loaded for the current batch: '||csr_wlji%rowcount);
7313         end if;
7314 
7315         if csr_wlji%rowcount - prev_rowcount <> 0 then  -- {{ one batch
7316 l_stmt_num := 50;
7317             -- getting the header_id to be populated for mmtt
7318             select mtl_material_transactions_s.nextval
7319             into   l_mmtt_txn_hdr_id
7320             from   dual;
7321 
7322 l_stmt_num := 60;
7323             -- updating the group_id column of wlji with a number unique for this particular batch.
7324             -- This'll help when I select corresponding rows from wsli.
7325 
7326             select wsm_lot_job_interface_s.NEXTVAL
7327             into   l_batch_grp_id
7328             from   dual;
7329 
7330 l_stmt_num := 60.1;
7331             forall indx in v_wlji_header_id.first..v_wlji_header_id.last
7332             update wsm_lot_job_interface
7333             set    group_id = l_batch_grp_id
7334             where  header_id = v_wlji_header_id(indx);
7335 
7336 l_stmt_num := 60.2;
7337             l_h := v_wlji_header_id.first;
7338             WHILE l_h <= v_wlji_header_id.last
7339             LOOP
7340                 if(v_wlji_num_of_children(l_h) > 0) then
7341                     update wsm_lot_job_dtl_interface
7342                     set     group_id                = l_batch_grp_id
7343                     where   parent_header_id = v_wlji_header_id(l_h);
7344                 end if;
7345                 l_h := v_wlji_header_id.next(l_h);
7346             END LOOP;
7347 
7348 
7349 -- ==============================================================================================
7350 -- BEGIN: Processing of data {{{
7351 -- ==============================================================================================
7352 l_stmt_num := 70;
7353             v_idx := v_wlji_header_id.first;
7354             while v_idx <= v_wlji_header_id.last
7355             loop  -- {{{ inner loop
7356                 if lbji_debug = 'Y' then
7357                     fnd_file.put_line(fnd_file.log,'------------------------------------------------------');
7358                     fnd_file.put_line(fnd_file.log,' wlji.header_id: '|| v_wlji_header_id(v_idx));
7359                     fnd_file.put_line(fnd_file.log,' wlji.job name: ' || v_wlji_job_name(v_idx));
7360                     fnd_file.put_line(fnd_file.log,'------------------------------------------------------');
7361                 end if;
7362 
7363                 SAVEPOINT savepoint_skip_job;
7364 
7365                 BEGIN  -- {{{ main block
7366 
7367                     l_rtg_seq_id := '';
7368                     l_common_bom_seq_id := 0;
7369                     l_common_rtg_seq_id := 0;
7370                     l_error_code := 0;
7371                     l_return_value := 0;
7372                     l_error_msg:= NULL;
7373                     l_aux_mesg:= NULL;
7374                     l_return_status := FND_API.G_RET_STS_SUCCESS;
7375                     l_temp_num:= 0;
7376                     l_xst:= true;
7377                     hash_str := '';
7378                     hash_value := 0;
7379                     l_num_records:= 0;
7380 
7381                     l_default_compl_subinv := '';
7382                     l_default_compl_loc_id := 0;
7383                     l_sub_loc_control := 0;
7384                     l_org_loc_control := 0;
7385                     l_item_loc_control := 0;
7386                     l_restrict_locators_code := 0;
7387                     l_item_id := 0;
7388                     l_locator_id := 0;
7389                     l_rev_date := '';
7390                     l_date_text := '';
7391                     l_component_quantity := 0;
7392                     l_qoh := 0;
7393                     l_att := 0;
7394                     l_atr := 0;
7395                     l_component_yield_factor := 0;
7396                     l_required_qpa := 0;
7397                     l_quantity_tobe_issued := 0;
7398                     l_start_op_seq_id := 0;
7399                     l_start_op_seq_num := 0;
7400                     l_department_id := 0;
7401                     l_skip_update_wo:=0;
7402                     l_est_scrap_account     := NULL;
7403                     l_est_scrap_var_account := NULL;
7404                     l_src_lot_number        := NULL;
7405                     l_src_inv_item_id       := NULL;
7406 
7407                     dummy_number:=0;
7408                     dummy_varchar:='';
7409                     dummy_date:=sysdate;
7410                     dummy_err_code := 0;
7411                     dummy_err_msg := NULL;
7412                     dummy_segs := '';
7413 
7414                     l_chg_bom_ref  := 0;
7415                     l_chg_rtg_ref  := 0;
7416                     l_chg_ref_flag := 0;
7417 
7418                     l_chg_bom_alt  := 0;
7419                     l_chg_rtg_alt  := 0;
7420                     l_chg_alt_flag := 0;
7421 
7422                     l_chg_rtg_rev  := 0;    -- Add: bug3202534
7423                     l_chg_bom_rev  := 0;    -- Add: bug3202534
7424                     l_chg_rev_flag := 0;    -- Add: bug3202534
7425 
7426                     g_charges_exist_all  := false;      -- ADD: bug 3423195
7427                     g_charges_exist_shopfloor := false; -- ADD: bug 3423195
7428                     g_job_released_date  := null;       -- ADD: bug 3423195
7429 
7430                     v_wlji_err_code(v_idx)               := l_error_code;
7431                     v_wlji_err_msg(v_idx)                := l_error_msg;
7432                     v_wlji_request_id(v_idx)             := fnd_global.conc_request_id;
7433                     v_wlji_program_id(v_idx)             := fnd_global.conc_program_id;
7434                     v_wlji_program_appl_id(v_idx)        := fnd_global.prog_appl_id;
7435                     v_wlji_creation_date(v_idx)          := sysdate;
7436                     v_wlji_last_updt_login(v_idx)        := fnd_global.login_id;
7437                     v_wlji_last_updt_date(v_idx)         := sysdate;
7438                     v_wlji_prog_updt_date(v_idx)         := sysdate;
7439 
7440                     l_mo_org_id                          := v_wlji_org_id(v_idx); -- Add: bug5485653
7441                     ------------------------------------------------------------------
7442                     -- {{{ Begin: Validate and Default constants (load_type = 5 and 6)
7443                     ------------------------------------------------------------------
7444 l_stmt_num := 80;
7445                     if (v_wlji_load_type(v_idx) <> 5) AND (v_wlji_load_type(v_idx) <> 6) then
7446                         l_aux_mesg := 'Load Type must be either 5 or 6';
7447                         handle_invalid_field('LOAD TYPE', l_aux_mesg, l_stmt_num);
7448                         l_error_code := -1;
7449                         GOTO skip_validate_constants;
7450                     end if;
7451 
7452 l_stmt_num := 85;
7453                     -- Dafault: job_type = 3 is non-standard job. Anything else/null => standard job
7454                     if (v_wlji_job_type(v_idx) is null) or (v_wlji_job_type(v_idx) <> 3) then
7455                         v_wlji_job_type(v_idx) := 1;
7456                     end if;
7457 
7458 l_stmt_num := 95;
7459                     -- Validate: mode_flag cannot be 2 for non-standard jobs, can only be 1
7460                     if v_wlji_job_type(v_idx) = 3 then
7461                         if v_wlji_mode_flag(v_idx) = 2 then
7462                             l_aux_mesg := 'Mode Flag cannot be 2 for non-standard jobs';
7463                             handle_invalid_field('MODE FlAG', l_aux_mesg, l_stmt_num);
7464                             l_error_code := -1;
7465                             GOTO skip_validate_constants;
7466                         else
7467                             v_wlji_mode_flag(v_idx) := 1;
7468                         end if;
7469                     end if;
7470 
7471 l_stmt_num := 100;
7472                     -- Validate: SCHEDULING METHOD
7473                     if(v_wlji_scheduling_method(v_idx) NOT IN (1, 2, 3) ) then
7474                         l_aux_mesg := 'Scheduling method must be 1(routing), 2(leadtime) or 3(manual)';
7475                         handle_invalid_field('SCHEDULING METHOD', l_aux_mesg, l_stmt_num);
7476                         l_error_code := -1;
7477                         GOTO skip_validate_constants;
7478                     end if;
7479 
7480 l_stmt_num := 105;
7481                     -- Validate: ALLOW EXPLOSION
7482                     if UPPER(v_wlji_allow_explosion(v_idx)) = 'N' then
7483                         handle_errmsg_name('WSM', 'WSM_ALLOW_EXPL_Y', l_stmt_num);
7484                         l_error_code := -1;
7485                         GOTO skip_validate_constants;
7486                     else
7487                         v_wlji_allow_explosion(v_idx) := 'Y';
7488             end if;
7489 
7490 l_stmt_num := 110;
7491                     -- Validate: WIP SUPPLY TYPE
7492                     if v_wlji_wip_supply_type(v_idx) is null then
7493                         v_wlji_wip_supply_type(v_idx) := 7;
7494                     elsif v_wlji_wip_supply_type(v_idx) not in (1,2,3,4,5,7) then
7495                         l_aux_mesg := '';
7496                         handle_invalid_field('Wip Supply Type', l_aux_mesg, l_stmt_num);
7497                         l_error_code := -1;
7498                         GOTO skip_validate_constants;
7499                     end if;
7500 
7501 l_stmt_num := 115;
7502                     -- Validate: STATUS TYPE
7503                     if v_wlji_status_type(v_idx) not in (
7504                         WIP_CONSTANTS.UNRELEASED,
7505                         WIP_CONSTANTS.RELEASED,
7506                         WIP_CONSTANTS.HOLD,
7507                         WIP_CONSTANTS.CANCELLED
7508                     ) then
7509                         l_aux_mesg := '';
7510                         handle_invalid_field('STATUS TYPE', l_aux_mesg, l_stmt_num);
7511                         l_error_code := -1;
7512                         GOTO skip_validate_constants;
7513                     end if;
7514 
7515 l_stmt_num := 120;
7516                     -- Validate: check kanban card id support status
7517                     if WSMPVERS.get_osfm_release_version < '110509'
7518                        and v_wlji_kanban_card_id(v_idx) is not null then
7519                         handle_errmsg_name('WSM', 'WSM_KANBAN_NOT ALLOWED', l_stmt_num);
7520                         l_error_code := -1;
7521                         GOTO skip_validate_constants;
7522                     end if;
7523 
7524 << skip_validate_constants >>
7525 
7526                     if l_error_code <> 0 then
7527                         l_error_count := l_error_count + 1;
7528                         GOTO abort_this_job;
7529                     end if;
7530                     if lbji_debug = 'Y' then
7531                         fnd_file.put_line(fnd_file.log, 'Validating Constants.. OK.');
7532                     end if;
7533                     ----------------------------------------------------------------
7534                     -- }}} End: Validate and Default constants (load_type = 5 and 6)
7535                     ----------------------------------------------------------------
7536 
7537 
7538 l_stmt_num := 130;
7539                     ---------------------------------------------------------
7540                     -- Validate organization id  Mngr has done the validation
7541                     ---------------------------------------------------------
7542                     --validate_org_id_name (
7543                     --  v_wlji_org_id(v_idx),
7544                     --  v_wlji_org_code(v_idx),
7545                     --  l_err_code,
7546                     --  l_err_msg);
7547                     --if l_err_code = -1 then
7548                     --  handle_invalid_field('Organization Id', '', l_stmt_num);
7549                     --  l_error_count := l_error_count + 1;
7550                     --  GOTO abort_this_job;
7551                     --end if;
7552                     --if lbji_debug = 'Y' then
7553                     --  fnd_file.put_line(fnd_file.log, 'Check Organization Id.. OK');
7554                     --end if;
7555 
7556 l_stmt_num := 130.5;
7557                     select nvl(OP_SEQ_NUM_INCREMENT, 10)
7558                     into   g_op_seq_incr
7559                     from   wsm_parameters
7560                     where  ORGANIZATION_ID = v_wlji_org_id(v_idx);
7561 
7562 l_stmt_num := 140;
7563                     --------------------------------------
7564                     -- {{{ Begin: Validate job name and id
7565                     --------------------------------------
7566                     Begin
7567                         l_temp_num := 0;
7568                         if v_wlji_load_type(v_idx) = 5 then   -- Job creation
7569                             NULL;       -- Mngr has done the validation
7570                         elsif v_wlji_load_type(v_idx) = 6 then    -- Job update
7571 
7572                             if (v_wlji_wip_entity_id(v_idx) is null) then
7573                                 begin
7574 l_stmt_num := 140.1;
7575                                     -- Get wip_entity_id by wip_entity_name
7576                                     select wip_entity_id
7577                                     into   v_wlji_wip_entity_id(v_idx)
7578                                     from   wip_entities
7579                                     where  wip_entity_name = v_wlji_job_name(v_idx)
7580                                     and    organization_id = v_wlji_org_id(v_idx);
7581                                 exception
7582                                     when others then
7583                                         raise e_invalid_job_name;
7584                                 end;
7585                             end if;
7586 
7587                             -- Now wip_entity_id is available, check job status
7588                             begin
7589 l_stmt_num := 140.2;
7590                             -- For unreleased jobs allow job name update if wip_entity_id is not null - fix for bug5990761
7591                               select decode(wdj.status_type, WIP_CONSTANTS.UNRELEASED, NVL(v_wlji_job_name(v_idx),we.wip_entity_name), we.wip_entity_name)
7592                                 into   v_wlji_job_name(v_idx)
7593                                 from   wip_discrete_jobs wdj,
7594                                        wip_entities we
7595                                 where  wdj.wip_entity_id = v_wlji_wip_entity_id(v_idx)
7596                                 and    wdj.status_type in (
7597                                         WIP_CONSTANTS.UNRELEASED,
7598                                         WIP_CONSTANTS.RELEASED,
7599                                         WIP_CONSTANTS.HOLD,
7600                                         WIP_CONSTANTS.CANCELLED)
7601                                 and    wdj.wip_entity_id = we.wip_entity_id
7602                                 and    we.organization_id = v_wlji_org_id(v_idx);
7603                             exception
7604                                 when others then
7605                                     raise e_invalid_id;
7606                             end;
7607                         end if;
7608 
7609                     Exception
7610                         when e_invalid_id then
7611                             l_aux_mesg := '';
7612                             handle_invalid_field('Wip_Entity_Id', l_aux_mesg, l_stmt_num);
7613                             l_error_code := -1;
7614                             GOTO skip_job_id_valid;
7615 
7616                         when e_invalid_job_name then
7617                             l_aux_mesg := '';
7618                             handle_invalid_field('Job_Name', l_aux_mesg, l_stmt_num);
7619                             l_error_code := -1;
7620                             GOTO skip_job_id_valid;
7621                     End;
7622 
7623 l_stmt_num := 140.3;
7624                     -- removed for performance improvement
7625                     --l_return_value := wsmputil.CHECK_WMTI(   -- CZH.I
7626                     --            p_wip_entity_id    => null,
7627                     --            p_wip_entity_name  => v_wlji_job_name(v_idx),
7628                     --            p_transaction_date => null,
7629                     --            x_err_code         => l_error_code,
7630                     --            x_err_msg          => l_error_msg,
7631                     --            p_organization_id  => v_wlji_org_id(v_idx) );
7632                     --IF (l_return_value <> 0) THEN
7633                     --    handle_errmsg_name('WSM', 'WSM_PENDING_MOVE_TXNS', l_stmt_num);
7634                     --    l_error_code := -1;
7635                     --    GOTO skip_job_id_valid;
7636                     --END IF;
7637 
7638 l_stmt_num := 140.4;
7639                     -- bug 3453139 removed for this is no longer required
7640                     --l_return_value
7641                     --    := wsmputil.CHECK_WSMT(   -- CZH.I
7642                     --            p_wip_entity_id    => null,
7643                     --            p_wip_entity_name  => v_wlji_job_name(v_idx),
7644                     --            p_transaction_id   => null,
7645                     --            p_transaction_date => null,
7646                     --            x_err_code         => l_error_code,
7647                     --            x_err_msg          => l_error_msg,
7648                     --            p_organization_id  => v_wlji_org_id(v_idx) );
7649                     --IF (l_return_value <> 0) THEN
7650                     --    FND_MESSAGE.SET_NAME('WSM', 'WSM_PENDING_TXN');
7651                     --    fnd_message.set_token('TABLE', 'WSM_SPLIT_MERGE_TRANSACTIONS');
7652                     --    l_error_msg := fnd_message.get;
7653                     --    l_error_code := -1;
7654                     --    handle_error( l_error_code, l_error_msg, l_stmt_num);
7655                     --    GOTO skip_job_id_valid;
7656                     --ELSE
7657                     --    l_error_msg := null;
7658                     --END IF;
7659 
7660 << skip_job_id_valid >>
7661 
7662                     if l_error_code <> 0 then
7663                         l_error_count := l_error_count + 1;
7664                         GOTO abort_this_job;
7665                     end if;
7666                     if lbji_debug = 'Y' then
7667                         fnd_file.put_line(fnd_file.log, 'Validate Job Name and Id.. OK.');
7668                     end if;
7669                     -------------------------------------
7670                     -- }}} End: Validate job name and id
7671                     -------------------------------------
7672 
7673 
7674 l_stmt_num := 150;
7675                     ---------------------------
7676                     --  Validate Start quantity
7677                     ---------------------------
7678                     if v_wlji_start_quantity(v_idx) < 0 then
7679                         l_error_code := -1;
7680                         l_aux_mesg := 'Start Quantity cannot be negative';
7681                         handle_invalid_field('Start_Quantity', l_aux_mesg, l_stmt_num);
7682                         l_error_count := l_error_count + 1;
7683                         GOTO abort_this_job;
7684                     end if;
7685 
7686                     if v_wlji_load_type(v_idx) = 5 and
7687                        (v_wlji_start_quantity(v_idx) is NULL or
7688                         v_wlji_start_quantity(v_idx) = 0)
7689                     then
7690                         l_error_code := -1;
7691                         l_aux_mesg := 'Start Quantity cannot be NULL or 0 for job creation';
7692                         handle_invalid_field( 'Start_Quantity', l_aux_mesg, l_stmt_num);
7693                         l_error_count := l_error_count + 1;
7694                         GOTO abort_this_job;
7695                     end if;
7696                     if lbji_debug = 'Y' and v_wlji_mode_flag(v_idx) = 1 then
7697                         fnd_file.put_line(fnd_file.log, 'Validate Start Quantity.. OK.');
7698                     end if;
7699 
7700 
7701 l_stmt_num := 160;
7702                     -------------------------------------------------
7703                     -- For job update, save the current values in WDJ
7704                     -------------------------------------------------
7705                     if v_wlji_load_type(v_idx) = 6 then
7706                         select  primary_item_id,
7707                                 class_code,
7708                                 scheduled_start_date,
7709                                 scheduled_completion_date,
7710                                 start_quantity,
7711                                 net_quantity,
7712                                 status_type,
7713                                 firm_planned_flag,
7714                                 job_type,
7715                                 bom_reference_id,
7716                                 alternate_bom_designator,
7717                                 routing_reference_id,
7718                                 alternate_routing_designator,
7719                                 bom_revision_date,
7720                                 routing_revision_date,
7721                                 bom_revision,
7722                                 routing_revision,
7723                                 common_routing_sequence_id,
7724                                 common_bom_sequence_id,
7725                                 wip_supply_type,
7726                                 scheduled_start_date,
7727                                 scheduled_completion_date,
7728                                 coproducts_supply,
7729                                 kanban_card_id,
7730                                 completion_subinventory,
7731                                 completion_locator_id,
7732                                 date_released,
7733                                 schedule_group_id,
7734                                 build_sequence,
7735                                 line_id
7736                         into    l_cur_primary_item_id,
7737                                 l_cur_class_code,
7738                                 l_cur_start_date,
7739                                 l_cur_complete_date,
7740                                 l_cur_start_qty,
7741                                 l_cur_net_qty,
7742                                 l_cur_status_type,
7743                                 l_cur_firm_planned_flag,
7744                                 l_cur_job_type,
7745                                 l_cur_bom_ref_id,
7746                                 l_cur_alt_bom,
7747                                 l_cur_rtg_ref_id,
7748                                 l_cur_alt_rtg,
7749                                 l_cur_bom_rev_date,
7750                                 l_cur_rtg_rev_date,
7751                                 l_cur_bom_revision,
7752                                 l_cur_rtg_revision,
7753                                 l_cur_com_rtg_seq_id,
7754                                 l_cur_com_bom_seq_id,
7755                                 l_cur_wip_supply_type,
7756                                 l_cur_schd_start_date,
7757                                 l_cur_schd_compl_date,
7758                                 l_cur_coprod_supply,
7759                                 l_cur_kanban_card_id,
7760                                 l_cur_compl_subinv,
7761                                 l_cur_compl_locator,
7762                                 l_cur_date_released,
7763                                 l_cur_schedule_group_id,
7764                                 l_cur_build_sequence,
7765                                 l_cur_line_id
7766                         from    wip_discrete_jobs
7767                         where   wip_entity_id = v_wlji_wip_entity_id(v_idx)
7768                         and     organization_id = v_wlji_org_id(v_idx);
7769 
7770                         -- BA: bug 3423195
7771                         g_job_released_date := l_cur_date_released;
7772                         g_charges_exist_all := discrete_charges_exist(
7773                                         v_wlji_wip_entity_id(v_idx),
7774                                         v_wlji_org_id(v_idx),0);
7775                         g_charges_exist_shopfloor := discrete_charges_exist(
7776                                         v_wlji_wip_entity_id(v_idx),
7777                                         v_wlji_org_id(v_idx),1);
7778                         -- EA: bug 3423195
7779 
7780                         -- Currently changing job_type, wip_supply_type and
7781                         -- v_wlji_kanban_card_id is not supported
7782                         v_wlji_job_type(v_idx)        := l_cur_job_type;
7783                         v_wlji_wip_supply_type(v_idx) := l_cur_wip_supply_type;
7784                         v_wlji_kanban_card_id(v_idx)  := l_cur_kanban_card_id;
7785 
7786 						-- Added for bug fix 5291888
7787 						if l_cur_primary_item_id <> nvl(v_wlji_item_id(v_idx),l_cur_primary_item_id) then
7788 							l_error_code := -1;
7789                             handle_invalid_field( 'PRIMARY_ITEM_ID', l_aux_mesg, l_stmt_num);
7790                             l_error_count := l_error_count + 1;
7791                             GOTO abort_this_job;
7792 						end if;
7793                         -- For std job, assembly cannot be changed
7794                         if(l_cur_job_type = 1) then
7795                             v_wlji_item_id(v_idx) := l_cur_primary_item_id;
7796                         end if;
7797                     end if;
7798 
7799 
7800 l_stmt_num := 170;
7801                     ---------------------------------------------------------
7802                     -- {{{ Begin: Validate schedule group and build sequence
7803                     ---------------------------------------------------------
7804                     if v_wlji_job_type(v_idx) = 1 then
7805 
7806                         -- {{ validate schedule_group_id begin
7807                         if v_wlji_load_type(v_idx) = 6 then
7808                             v_wlji_schedule_group_id(v_idx)
7809                                 := NVL(v_wlji_schedule_group_id(v_idx), l_cur_schedule_group_id);
7810                         end if;
7811 
7812                         if (v_wlji_source_code(v_idx) = 'WICDOL' and
7813                             v_wlji_schedule_group_name(v_idx) IS NULL and
7814                             v_wlji_schedule_group_id(v_idx) IS NULL)
7815                         then
7816 l_stmt_num := 170.1;
7817                             insert into wip_schedule_groups (
7818                                     schedule_group_id,
7819                                     schedule_group_name,
7820                                     organization_id,
7821                                     description,
7822                                     created_by,
7823                                     last_updated_by,
7824                                     creation_date,
7825                                     last_update_date)
7826                             select  wip_schedule_groups_s.nextval,
7827                                     wds.name,
7828                                     v_wlji_org_id(v_idx),
7829                                     to_char(sysdate),
7830                                     l_user,
7831                                     l_user,
7832                                     sysdate,
7833                                     sysdate
7834                             from    wsh_new_deliveries wds
7835                             where   wds.delivery_id = v_wlji_delivery_id(v_idx)
7836                             and     not exists  (
7837                                         select  1
7838                                         from    wip_schedule_groups wsg
7839                                         where   wsg.organization_id = v_wlji_org_id(v_idx)
7840                                         and     wsg.schedule_group_name = wds.name);
7841 l_stmt_num := 170.2;
7842                             select wsg.schedule_group_name,
7843                                    wsg.schedule_group_id
7844                             into   v_wlji_schedule_group_name(v_idx),
7845                                    v_wlji_schedule_group_id(v_idx)
7846                             from   wip_schedule_groups wsg,
7847                                    wsh_new_deliveries  wds
7848                             where  wds.delivery_id = v_wlji_delivery_id(v_idx)
7849                             and    wsg.schedule_group_name = wds.name
7850                             and    wsg.organization_id = v_wlji_org_id(v_idx);
7851                         end if;
7852 
7853                         if (v_wlji_schedule_group_id(v_idx) is not NULL) then
7854 l_stmt_num := 170.3;
7855                             begin
7856                                 select 1
7857                                 into   l_temp_num
7858                                 from   wip_schedule_groups_val_v wsg
7859                                 where  wsg.schedule_group_id = v_wlji_schedule_group_id(v_idx)
7860                                 and    wsg.organization_id = v_wlji_org_id(v_idx);
7861                             exception
7862                                 when too_many_rows then
7863                                     l_temp_num := 1;
7864                             end;
7865                             if l_temp_num = 0 then
7866                                 l_error_code := -1;
7867                                 handle_errmsg_name('WIP', 'WIP_ML_SCHEDULE_GROUP', l_stmt_num);
7868                                 l_error_count := l_error_count + 1;
7869                                 GOTO abort_this_job;
7870                             end if;
7871                         end if;
7872                         if lbji_debug = 'Y' then
7873                             fnd_file.put_line(fnd_file.log, 'Schedule Group Id Verification.. OK');
7874                         end if;
7875                         -- }} validate schedule_group_id end
7876 
7877 l_stmt_num := 170.4;
7878                         -- {{ validate build_seq_id begin
7879                         if v_wlji_load_type(v_idx) = 6 then
7880                             v_wlji_build_sequence(v_idx)
7881                                 := NVL(v_wlji_build_sequence(v_idx), l_cur_build_sequence);
7882                             v_wlji_line_id(v_idx)
7883                                 := NVL(v_wlji_line_id(v_idx), l_cur_line_id);
7884                             v_wlji_schedule_group_id(v_idx)
7885                                 := NVL(v_wlji_schedule_group_id(v_idx), l_cur_schedule_group_id);
7886                         end if;
7887 
7888                         -- Only if v_wlji_build_sequence(v_idx) is not null, we want to execute
7889                         -- this WIP_VALIDATE function. Otherwise, DO NOT.
7890                         if v_wlji_build_sequence(v_idx) is not null and
7891                            v_wlji_build_sequence(v_idx) <> 0 Then
7892                             l_temp_boolean := WIP_Validate.build_sequence (
7893                                     p_build_sequence    => v_wlji_build_sequence(v_idx),
7894                                     p_wip_entity_id     => v_wlji_wip_entity_id(v_idx),
7895                                     p_organization_id   => v_wlji_org_id(v_idx),
7896                                     p_line_id           => v_wlji_line_id(v_idx),
7897                                     p_schedule_group_id => v_wlji_schedule_group_id(v_idx));
7898 
7899                             if NOT l_temp_boolean Then
7900                                 l_error_code := -1;
7901                                 handle_errmsg_name('WIP', 'WIP_ML_BUILD_SEQUENCE', l_stmt_num);
7902                                 l_error_count := l_error_count + 1;
7903                                 GOTO abort_this_job;
7904                             end if;
7905                         end if;
7906                         if lbji_debug = 'Y' then
7907                             fnd_file.put_line(fnd_file.log, 'Build Seq. Id Verification.. OK');
7908                         end if;
7909                         -- }} validate build_seq_id end
7910                     else -- job type = 3
7911                         -- previously we do not have validations for non-std job
7912                         v_wlji_build_sequence(v_idx)    := null;
7913                         v_wlji_line_id(v_idx)           := null;
7914                         v_wlji_schedule_group_id(v_idx) := null;
7915                     end if; -- job type
7916                     ---------------------------------------------------------
7917                     -- }}} End: Validate schedule group and build sequence
7918                     ---------------------------------------------------------
7919 
7920 
7921 -- ===========================================================================================
7922 -- BEGIN: Job Creation Validations {{{
7923 -- ===========================================================================================
7924                     if v_wlji_load_type(v_idx) = 5 then
7925                         -- for std lbj ignore v_wlji_bom_ref_id/v_wlji_rtg_ref_id
7926             if v_wlji_job_type(v_idx) = 1 then  -- added: bug 3862212
7927                             v_wlji_bom_ref_id(v_idx) := null;
7928                 v_wlji_rtg_ref_id(v_idx) := null;
7929             end if;                             -- added: bug 3862212
7930 
7931 l_stmt_num := 200;
7932                         -- Validate Mode: for job creation mode must be in (1, 2)
7933                         if v_wlji_mode_flag(v_idx) not in (1,2) then
7934                             l_aux_mesg := 'For job creation, Mode Flag should have value 1 or 2';
7935                             handle_invalid_field('MODE_FLAG', l_aux_mesg, l_stmt_num);
7936                             l_error_code := -1;
7937                             l_error_count := l_error_count + 1;
7938                             GOTO abort_this_job;
7939                         end if;
7940 
7941 l_stmt_num := 210;
7942                         -- Default lot_number
7943                         v_wlji_lot_number(v_idx) := v_wlji_job_name(v_idx);
7944                         if lbji_debug = 'Y' then
7945                             fnd_file.put_line(fnd_file.log, 'Lot Number Defaulted as Job Name.');
7946                         end if;
7947 
7948 l_stmt_num := 220;
7949                         -- Validate: FIRM_PLANNED_FLAG
7950                         if v_wlji_job_type(v_idx) = 1 then
7951                             if v_wlji_firm_planned_flag(v_idx) is null then
7952                                 v_wlji_firm_planned_flag(v_idx) := 2;
7953                             elsif v_wlji_firm_planned_flag(v_idx) <> 1 and
7954                                   v_wlji_firm_planned_flag(v_idx) <> 2
7955                             then
7956                                 l_aux_mesg := 'For job creation, Firm Planned Flag must be either 1 or 2';
7957                                 handle_invalid_field('FIRM_PLANNED_FLAG', l_aux_mesg, l_stmt_num);
7958                                 l_error_code := -1;
7959                                 l_error_count := l_error_count + 1;
7960                                 GOTO abort_this_job;
7961                             end if;
7962                         end if;
7963 
7964 
7965 l_stmt_num := 230;
7966                         -- Check WLSMI
7967                         -- make sure that no one is trying to create a wip lot out of
7968                         -- an inventory lot by splitting/merging/etc.
7969                         l_num_records := 0;
7970                         begin
7971                             select  1
7972                             into    l_num_records
7973                             from    wsm_starting_lots_interface    wsli,
7974                                     wsm_lot_split_merges_interface wlsmi
7975                             where   wsli.lot_number = v_wlji_job_name(v_idx)
7976                             and     wsli.header_id = wlsmi.header_id
7977                             and     wlsmi.process_status in (1,2);
7978                         exception
7979                             when too_many_rows then
7980                                 l_num_records := 1;
7981                             when no_data_found then
7982                                 NULL;
7983                         end;
7984                         if l_num_records <> 0 then
7985                             l_error_code := -1;
7986                             handle_errmsg_name('WSM', 'WSM_LOT_EXISTS', l_stmt_num);
7987                             l_error_count := l_error_count + 1;
7988                             GOTO abort_this_job;
7989                         end if;
7990                         if lbji_debug = 'Y' then
7991                             fnd_file.put_line(fnd_file.log, 'Checking existance of inv lots of the same name.. OK');
7992                         end if;
7993 
7994 
7995                         -----------------------------------
7996                         -- {{{ Begin: Validate WHO columns
7997                         -----------------------------------
7998 l_stmt_num := 240;
7999                         -- validate last_updt_by (job creation)
8000                         if v_wlji_last_updt_by(v_idx) is NULL then
8001                             v_wlji_last_updt_by(v_idx) := l_user;
8002                         else
8003                             l_num_records := 0;
8004                             l_xst := WSMPLCVA.v_user.exists(v_wlji_last_updt_by(v_idx));
8005                             if l_xst = false then
8006                                 begin
8007                                     select 1
8008                                     into   l_num_records
8009                                     from   fnd_user
8010                                     where  user_id = v_wlji_last_updt_by(v_idx)
8011                                     and    sysdate between start_date and nvl(end_date,sysdate+1);
8012                                 exception
8013                                     when too_many_rows then l_num_records := 1;
8014                                     when no_data_found then
8015                                         l_error_code := -1;
8016                                         l_aux_mesg := '';
8017                                         handle_invalid_field(
8018                                             'Last Updated By', l_aux_mesg, l_stmt_num);
8019                                 end;
8020                             end if;
8021 
8022                             if l_xst = false AND l_num_records <> 0 then
8023                                 WSMPLCVA.v_user(v_wlji_last_updt_by(v_idx))
8024                                     := v_wlji_last_updt_by(v_idx);
8025                             end if;
8026 
8027                             if l_error_code <> 0 then
8028                                 l_error_count := l_error_count + 1;
8029                                 GOTO abort_this_job;
8030                             end if;
8031                         end if;
8032                         if lbji_debug = 'Y' then
8033                             fnd_file.put_line(fnd_file.log, 'Last Updt By Validation.. OK.');
8034                         end if;
8035 
8036 l_stmt_num := 245;
8037                         -- validate created_by (job creation)
8038                         if v_wlji_created_by(v_idx) is NULL then
8039                             v_wlji_created_by(v_idx) := l_user;
8040                         else
8041                             l_num_records := 0;
8042                             l_xst := WSMPLCVA.v_user.exists(v_wlji_created_by(v_idx));
8043                             if l_xst = false then
8044                                 begin
8045                                     select 1
8046                                     into   l_num_records
8047                                     from   fnd_user
8048                                     where  user_id = v_wlji_created_by(v_idx)
8049                                     and    sysdate between start_date and nvl(end_date,sysdate+1);
8050                                 exception
8051                                     when too_many_rows then l_num_records := 1;
8052                                     when no_data_found then
8053                                         l_error_code := -1;
8054                                         l_aux_mesg := '';
8055                                         handle_invalid_field(
8056                                             'Created By', l_aux_mesg, l_stmt_num);
8057                                 end;
8058                             end if;
8059 
8060                             if l_xst = false AND l_num_records <> 0 then
8061                                 WSMPLCVA.v_user(v_wlji_created_by(v_idx))
8062                                     := v_wlji_created_by(v_idx);
8063                             end if;
8064 
8065                             if l_error_code <> 0 then
8066                                 l_error_count := l_error_count + 1;
8067                                 GOTO abort_this_job;
8068                             end if;
8069                         end if;
8070                         if lbji_debug = 'Y' then
8071                             fnd_file.put_line(fnd_file.log, 'Created By Validation.. OK.');
8072                         end if;
8073                         --------------------------------
8074                         -- }}} End: Validate WHO columns
8075                         --------------------------------
8076 
8077 
8078                         if v_wlji_mode_flag(v_idx) = 2 then
8079 l_stmt_num := 250;
8080                             -- Call load_wsli_data to load relevant rows from wsli into PL/SQL
8081                             -- tables for easy validation. This procedure will not be executed
8082                             -- if there's no mode_flag = 2 rec in wlji.
8083                             -- l_wsli_loaded (true/false) ensures that the procedure
8084                             -- is called only once per worker
8085                             if (l_wsli_loaded = false) then
8086                                 if lbji_debug = 'Y' then
8087                                     fnd_file.put_line(fnd_file.log,
8088                                         'Loading wsli for batch group id: ' ||l_batch_grp_id||'..');
8089                                 end if;
8090 
8091                                 load_wsli_data(l_batch_grp_id);
8092                                 l_wsli_loaded := true;
8093                             end if;
8094 
8095 l_stmt_num := 260;
8096                             -- Check that there's a row in wsli for this mode 2 job
8097                             l_xst := true;
8098                             if v_wlji_src_line_id(v_idx) is NULL then
8099                                 l_error_code := -1;
8100                                 handle_errmsg_name('WSM', 'WSM_START_LOT_REQUIRED', l_stmt_num);
8101                                 l_error_count := l_error_count + 1;
8102                                 GOTO abort_this_job;
8103                             else
8104                                 l_xst := v_wsli.exists(v_wlji_src_line_id(v_idx));
8105                                 if l_xst = false then
8106                                     l_error_code := -1;
8107                                     handle_errmsg_name('WSM', 'WSM_START_LOT_REQUIRED', l_stmt_num);
8108                                     l_error_count := l_error_count + 1;
8109                                     GOTO abort_this_job;
8110                                 end if;
8111                             end if;
8112                         end if;
8113 
8114 
8115 l_stmt_num := 270;
8116                         -- {{ Validate schedule dates (creation)
8117                         -- this should be done after item_id is validated
8118 
8119                         l_error_msg := null;
8120                         IF v_wlji_fusd(v_idx) IS NOT NULL AND
8121                            v_wlji_lucd(v_idx) IS NOT NULL
8122                         THEN
8123 l_stmt_num := 270.1;
8124                             if  v_wlji_fusd(v_idx) > v_wlji_lucd(v_idx) THEN
8125                                 handle_errmsg_name('WSM', 'WSM_FUSD_GT_LUCD', l_stmt_num);
8126                                 l_error_code := -1;
8127                                 l_error_count := l_error_count + 1;
8128                                 GOTO abort_this_job;
8129                             end if;
8130                         ELSIF v_wlji_fusd(v_idx) IS NULL AND
8131                               v_wlji_lucd(v_idx) IS NULL
8132                         THEN
8133 l_stmt_num := 270.2;
8134                             handle_errmsg_name('WSM', 'WSM_DATES_NULL', l_stmt_num);
8135                             l_error_code := -1;
8136                             l_error_count := l_error_count + 1;
8137                             GOTO abort_this_job;
8138                         END IF;
8139 
8140                         if (v_wlji_scheduling_method(v_idx) = 1 ) then
8141                             -- Since infinite scheduler will be called, do not have to call
8142                             -- leadtime based scheduler
8143                             -- However, to default routing/bom revision date, v_wlji_fusd is
8144                             -- needed. We cannot call infinite scheduler if job-leve copy is
8145                             -- not available, but to create a job-level copy, routing/bom
8146                             -- revision is a must.
8147                             -- If v_wlji_scheduling_method = 1, v_wlji_fusd is NULL, the
8148                             -- routing/bom revision date will be default to SYSDATE
8149                             null;
8150                         else
8151 
8152                             IF v_wlji_fusd(v_idx) IS NOT NULL AND
8153                                v_wlji_lucd(v_idx) IS NULL THEN
8154     l_stmt_num := 270.3;
8155                                 v_wlji_lucd(v_idx)
8156                                     := wsmputil.GET_SCHEDULED_DATE (
8157                                             v_wlji_org_id(v_idx),
8158                                             v_wlji_item_id(v_idx),
8159                                             'F',
8160                                             v_wlji_fusd(v_idx),
8161                                             l_error_code,
8162                                             l_error_msg,
8163                                             v_wlji_start_quantity(v_idx));     --Fixed bug # 2313574
8164                                 if lbji_debug = 'Y' then
8165                                     fnd_file.put_line(fnd_file.log,
8166                                         'Getting complete date based on item lead time');
8167                                 end if;
8168                             END IF;
8169 
8170                             IF v_wlji_fusd(v_idx) IS NULL AND
8171                                v_wlji_lucd(v_idx) IS NOT NULL THEN
8172     l_stmt_num := 270.4;
8173                                 v_wlji_fusd(v_idx)
8174                                     := wsmputil.GET_SCHEDULED_DATE (
8175                                             v_wlji_org_id(v_idx),
8176                                             v_wlji_item_id(v_idx),
8177                                             'B',
8178                                             v_wlji_lucd(v_idx),
8179                                             l_error_code,
8180                                             l_error_msg,
8181                                             v_wlji_start_quantity(v_idx));     --Fixed bug # 2313574
8182                                 if lbji_debug = 'Y' then
8183                                     fnd_file.put_line(fnd_file.log,
8184                                         'Getting start date based on item lead time');
8185                                 end if;
8186                             END IF;
8187                             if l_error_code <> 0 OR l_error_msg IS NOT NULL then
8188                                 handle_error( l_error_code, l_error_msg, l_stmt_num);
8189                                 l_error_code := -1;
8190                                 l_error_count := l_error_count + 1;
8191                                 GOTO abort_this_job;
8192                             end if;
8193 
8194                         end if;
8195 
8196                         if lbji_debug = 'Y' then
8197                             fnd_file.put_line(fnd_file.log, 'Date Validation.. OK.');
8198                         end if;
8199                         -- }} Validate schedule dates
8200 
8201 
8202 l_stmt_num := 280;
8203                         -- Validate coproduct-supply flag (creation)
8204                         IF v_wlji_coproducts_supply(v_idx) is NULL THEN
8205                             v_wlji_coproducts_supply(v_idx)
8206                                 := WSMPLCVA.v_org(v_wlji_org_id(v_idx)).COPRODUCTS_SUPPLY_DEFAULT;
8207                         --Bug 5463925: OR is replaced by AND.
8208                         --ELSIF (v_wlji_coproducts_supply(v_idx) <> 1 OR
8209                         ELSIF (v_wlji_coproducts_supply(v_idx) <> 1 AND
8210                                v_wlji_coproducts_supply(v_idx) <> 2) THEN
8211                             v_wlji_coproducts_supply(v_idx) := 2;
8212                         END IF;
8213 
8214                         if l_error_code <> 0 then
8215                             l_error_count := l_error_count + 1;
8216                             GOTO abort_this_job;
8217                         end if;
8218                         if lbji_debug = 'Y' then
8219                             fnd_file.put_line(fnd_file.log,
8220                                 'Co Product Supply Flag Validation.. OK.');
8221                         end if;
8222 
8223 
8224 l_stmt_num := 290;
8225                         -- Default description
8226                         -- this is to be called only for job creation.
8227                         if RTRIM(v_wlji_description(v_idx)) is NULL then
8228                             l_date_text := fnd_date.date_to_charDT(sysdate);
8229                             fnd_message.set_name('WIP','WIP_MLD_DESC');
8230                             fnd_message.set_token('LOAD_DATE', l_date_text, false);
8231                             v_wlji_description(v_idx) := FND_Message.get;
8232                         else
8233                             v_wlji_description(v_idx) := RTRIM(v_wlji_description(v_idx));
8234                         end if;
8235                         if lbji_debug = 'Y' then
8236                             fnd_file.put_line(fnd_file.log, 'Default Description.');
8237                         end if;
8238 
8239 
8240 l_stmt_num := 300;
8241                         --------------------------------------------------
8242                         -- {{{ Begin: Validation of a non-std job creation
8243                         --------------------------------------------------
8244                         if v_wlji_job_type(v_idx) = 3 then
8245 
8246                             -- validation_level = 0 => validations performed during job creation
8247                             -- Error Code and Message Guide
8248                             -- 1:  Routing Reference Cannot be Null
8249                             -- 2:  Invalid Assembly Item Id
8250                             -- 3:  Invalid Routing Reference Id
8251                             -- 4:  Invalid Bom Reference Id
8252                             -- 5:  Invalid Alternate Routing Designator
8253                             -- 0:  Invalid Alternate Bom Designator -- WARNING
8254                             -- 7:  Start Date cannot be greater than End Date
8255                             -- 8:  Both Start and End Dates must be Entered
8256                             -- 9:  Invalid Start Quantity
8257                             -- 10: Invalid Net Quantity
8258                             -- 11: Invalid Class Code
8259                             -- 12: Invalid Completion Locator Id
8260                             -- 13: Invalid Completion Subinventory
8261                             -- 14: Invalid Firm Planned Flag
8262 
8263                             l_error_code := 0;
8264                             l_error_msg := NULL;
8265                             wsmputil.validate_non_std_references(
8266                                 p_assembly_item_id       => v_wlji_item_id(v_idx),
8267                                 p_routing_reference_id   => v_wlji_rtg_ref_id(v_idx),
8268                                 p_bom_reference_id       => v_wlji_bom_ref_id(v_idx),
8269                                 p_alt_routing_designator => v_wlji_alt_rtg(v_idx),
8270                                 p_alt_bom_designator     => v_wlji_alt_bom(v_idx),
8271                                 p_organization_id        => v_wlji_org_id(v_idx),
8272                                 p_start_date             => v_wlji_fusd(v_idx),
8273                                 p_end_date               => v_wlji_lucd(v_idx),
8274                                 p_start_quantity         => v_wlji_start_quantity(v_idx),
8275                                 p_mrp_net_quantity       => v_wlji_net_quantity(v_idx),
8276                                 p_class_code             => v_wlji_class_code(v_idx),
8277                                 p_completion_subinventory=> v_wlji_compl_subinv(v_idx),
8278                                 p_completion_locator_id  => v_wlji_compl_loc_id(v_idx),
8279                                 p_firm_planned_flag      => v_wlji_firm_planned_flag(v_idx),
8280                                 p_bom_revision           => v_wlji_bom_revision(v_idx),
8281                                 p_bom_revision_date      => v_wlji_bom_rev_date(v_idx),
8282                                 p_routing_revision       => v_wlji_rtg_revision(v_idx),
8283                                 p_routing_revision_date  => v_wlji_rtg_rev_date(v_idx),
8284                                 x_routing_seq_id         => l_rtg_seq_id,
8285                                 x_bom_seq_id             => l_common_bom_seq_id, -- returns common bom
8286                                 validation_level         => 0,
8287                                 x_error_code             => l_error_code,
8288                                 x_err_msg                => l_error_msg);
8289                             if l_error_code = 0 and l_error_msg is not null then
8290                                 fnd_file.put_line(fnd_file.log, l_error_msg);
8291                                 l_error_msg := NULL;
8292                             end if;
8293                             if l_error_code <> 0 then
8294                                 handle_error( l_error_code, l_error_msg, l_stmt_num);
8295                                 l_error_code := -1;
8296                                 l_error_count := l_error_count + 1;
8297                                 GOTO abort_this_job;
8298                             end if;
8299 
8300                             -- Get common routing seq id
8301                             wsmputil.find_common_routing(
8302                                 p_routing_sequence_id        => l_rtg_seq_id,
8303                                 p_common_routing_sequence_id => l_common_rtg_seq_id,
8304                                 x_err_code                   => l_error_code,
8305                                 x_err_msg                    => l_error_msg);
8306                             if l_error_code <> 0 OR l_error_msg IS NOT NULL then
8307                                 handle_error( l_error_code, l_error_msg, l_stmt_num);
8308                                 l_error_code := -1;
8309                                 l_error_count := l_error_count + 1;
8310                                 GOTO abort_this_job;
8311                             end if;
8312                             if lbji_debug = 'Y' then
8313                                 fnd_file.put_line(fnd_file.log,
8314                                     'Validate Non-standard References.. OK.');
8315                             end if;
8316 
8317                         end if; -- non-standard validations end
8318                         ------------------------------------------------
8319                         -- }}} End: Validation of a non-std job creation
8320                         ------------------------------------------------
8321 
8322                         -----------------------------------------------------
8323                         -- {{{ Begin: validations for standard job creations
8324                         -----------------------------------------------------
8325                         if v_wlji_job_type(v_idx) = 1 then
8326 
8327 l_stmt_num := 310;
8328                             -- {{ Validate item id (std job creation)
8329                             if v_wlji_mode_flag(v_idx) = 1 then
8330                                 l_xst := WSMPLCVA.v_item.exists(v_wlji_item_id(v_idx));
8331                             else -- mode flag 2
8332                                 hash_str   := to_char(v_wlji_item_id(v_idx)) ||
8333                                               to_char(v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id);
8334                                 hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
8335 
8336                                 l_xst := WSMPLCVA.v_mode2_item.exists(hash_value) AND
8337                                          (WSMPLCVA.v_mode2_item(hash_value).INVENTORY_ITEM_ID
8338                                             = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id) AND
8339                                          (WSMPLCVA.v_mode2_item(hash_value).PRIMARY_ITEM_ID
8340                                             = v_wlji_item_id(v_idx));
8341                             end if;
8342 l_stmt_num := 310.1;
8343                             if l_xst = false then
8344                                 l_num_records := 0;
8345                                 begin
8346                                     select  1
8347                                     into    l_num_records
8348                                     from    mtl_system_items_kfv msi
8349                                     where   msi.inventory_item_id = v_wlji_item_id(v_idx)
8350                                     and     msi.organization_id   = v_wlji_org_id(v_idx)
8351                                     and     msi.lot_control_code  = 2;
8352                                 exception
8353                                     when too_many_rows then
8354                                         l_num_records := 1;
8355                                     when no_data_found then
8356                                         l_error_code := -1;
8357                                         handle_errmsg_name(
8358                                             'WSM', 'WSM_ASSEMBLY_NO_LOT', l_stmt_num);
8359                                 end;
8360 l_stmt_num := 310.2;
8361                                 if l_num_records <> 0 then
8362                                     l_num_records := 0;
8363                                     begin
8364                                         select  1
8365                                         into    l_num_records
8366                                         from    mtl_system_items_kfv msi
8367                                         where   msi.inventory_item_id = v_wlji_item_id(v_idx)
8368                                         and     msi.organization_id   = v_wlji_org_id(v_idx)
8369                     -- ST : Serial Support Project -----------------------------
8370                     -- In OSFM we do support Serial controlled assemblies.......
8371                                         and     msi.serial_number_control_code IN (1,2);
8372                     -- ST : Serial Support Project -----------------------------
8373                                     exception
8374                                         when too_many_rows then
8375                                             l_num_records := 1;
8376                                         when no_data_found then
8377                                             l_error_code := -1;
8378                                             handle_errmsg_name(
8379                                                 'WSM', 'WSM_ASSEMBLY_NOT_SERIAL', l_stmt_num);
8380                                     end;
8381                                 end if;
8382                             end if; -- l_xst = false
8383 
8384                             if l_xst = false AND l_num_records <> 0 then
8385                                 if v_wlji_mode_flag(v_idx) = 2 then
8386                                     WSMPLCVA.v_mode2_item(hash_value).INVENTORY_ITEM_ID
8387                                         := v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id;
8388                                     WSMPLCVA.v_mode2_item(hash_value).PRIMARY_ITEM_ID
8389                                         := v_wlji_item_id(v_idx);
8390                                 else -- mode flag = 1
8391                                     WSMPLCVA.v_item(v_wlji_item_id(v_idx)) := v_wlji_item_id(v_idx);
8392                                 end if;
8393                             end if;
8394 
8395                             if l_error_code <> 0 then
8396                                 l_error_count := l_error_count + 1;
8397                                 GOTO abort_this_job;
8398                             end if;
8399                             if lbji_debug = 'Y' then
8400                                 fnd_file.put_line(fnd_file.log, 'Validation of Item ID.. OK');
8401                             end if;
8402                             -- }} Validate item id
8403 
8404 
8405 l_stmt_num := 320;
8406                             -- {{ Validate net quantity (std job creation)
8407                             if (v_wlji_net_quantity(v_idx) < 0) then
8408                                 l_aux_mesg := 'Net Quantity should be > 0';
8409                                 handle_invalid_field('NET QUANTITY', l_aux_mesg, l_stmt_num);
8410                                 l_error_code := -1;
8411                                 l_error_count := l_error_count + 1;
8412                                 GOTO abort_this_job;
8413                             end if;
8414 
8415                             if (v_wlji_net_quantity(v_idx) IS NULL) then
8416                                 v_wlji_net_quantity(v_idx) := v_wlji_start_quantity(v_idx);
8417                             end if;
8418 
8419                             if v_wlji_net_quantity(v_idx) > v_wlji_start_quantity(v_idx) then
8420                                 l_aux_mesg := 'Net Quantity should be <= Start Quantity';
8421                                 handle_invalid_field('NET QUANTITY', l_aux_mesg, l_stmt_num);
8422                                 l_error_code := -1;
8423                                 l_error_count := l_error_count + 1;
8424                                 GOTO abort_this_job;
8425                             end if;
8426 
8427                             if lbji_debug = 'Y' then
8428                                 fnd_file.put_line(fnd_file.log, 'Net Quantity Validation.. OK.');
8429                             end if;
8430                             -- }} Validate net quantity
8431 
8432 l_stmt_num := 325;
8433                -- ST : Serial Support Project ----------------------
8434                --- Validate the start job qty and net qty ----------
8435                --- For serial controlled item have to be an integer...
8436                DECLARE
8437                 l_serial_ctrl_code NUMBER;
8438 
8439                BEGIN
8440                 select  msi.serial_number_control_code
8441                                 into    l_serial_ctrl_code
8442                                 from    mtl_system_items_kfv msi
8443                                 where   msi.inventory_item_id = v_wlji_item_id(v_idx)
8444                                 and     msi.organization_id   = v_wlji_org_id(v_idx);
8445 
8446                 IF l_serial_ctrl_code = 2 THEN  -- Predefined serial control...
8447 
8448                     IF floor(v_wlji_start_quantity(v_idx)) <> v_wlji_start_quantity(v_idx) THEN
8449                         -- error out...
8450                         -- Start qty cannot be non-integer...
8451                          fnd_message.set_name('WSM','WSM_INVALID_JOB_TXN_QTY');
8452                          l_aux_mesg := fnd_message.get;
8453                          handle_invalid_field('START QUANTITY', l_aux_mesg, l_stmt_num);
8454                          l_error_code := -1;
8455                          l_error_count := l_error_count + 1;
8456                          GOTO abort_this_job;
8457                     END IF;
8458 
8459                     IF floor(v_wlji_net_quantity(v_idx)) <> v_wlji_net_quantity(v_idx) THEN
8460                         -- error out...
8461                         -- Start qty cannot be non-integer...
8462                          fnd_message.set_name('WSM','WSM_INVALID_JOB_TXN_QTY');
8463                          l_aux_mesg := fnd_message.get;
8464                          handle_invalid_field('NET QUANTITY', l_aux_mesg, l_stmt_num);
8465                          l_error_code := -1;
8466                          l_error_count := l_error_count + 1;
8467                          GOTO abort_this_job;
8468                     END IF;
8469                 END IF;
8470                END;
8471                -- ST : Serial Support Project ----------------------
8472                --- Validate the start job qty and net qty ----------
8473 
8474 l_stmt_num := 330;
8475                             -- {{ Get l_common_rtg_seq_id  (std job creation)
8476                             begin
8477                             -- bugfix 2681637 validation of alternate designator should
8478                             -- check disable_date. (original bug for form 2558447)
8479                             -- view bom_routing_alternates_v does not have designator
8480                             -- disable_date infor.
8481 
8482                                select bor.routing_sequence_id,
8483                                       bor.COMPLETION_SUBINVENTORY,
8484                                       bor.COMPLETION_LOCATOR_ID
8485                                into   l_rtg_seq_id,
8486                                       l_default_compl_subinv,
8487                                       l_default_compl_loc_id
8488                                from   bom_operational_routings  bor,
8489                                       bom_alternate_designators bad
8490                                where  ((bor.alternate_routing_designator is null and
8491                                         bad.alternate_designator_code is null and
8492                                         bad.organization_id = -1) or
8493                                        (bor.alternate_routing_designator
8494                                             = bad.alternate_designator_code and
8495                                         bor.organization_id = bad.organization_id))
8496                                and    bor.organization_id = v_wlji_org_id(v_idx)
8497                                and    bor.assembly_item_id = v_wlji_item_id(v_idx)
8498                                and    NVL(bor.alternate_routing_designator, '&*')
8499                                           = NVL(v_wlji_alt_rtg(v_idx), '&*')
8500                                and    bor.routing_type = 1
8501                                and    bor.cfm_routing_flag = 3;
8502                                --Bug 5107339: Disable_date validation is not applicable here.
8503                                --and    trunc(nvl(bad.disable_date, sysdate + 1)) > trunc(sysdate);
8504 
8505                             exception
8506                                 when no_data_found then
8507                                     l_aux_mesg := '';
8508                                     handle_invalid_field(
8509                                         'ALTERNATE ROUTING DESIGNATOR', l_aux_mesg, l_stmt_num);
8510                                     l_error_code := -1;
8511                                     l_error_count := l_error_count + 1;
8512                                     GOTO abort_this_job;
8513                             end;
8514 
8515                             IF v_wlji_compl_subinv(v_idx) IS NULL AND
8516                                v_wlji_compl_loc_id(v_idx) IS NULL THEN
8517                                 v_wlji_compl_subinv(v_idx) := l_default_compl_subinv;
8518                                 v_wlji_compl_loc_id(v_idx) := l_default_compl_loc_id;
8519                             END IF;
8520 
8521                             IF v_wlji_compl_subinv(v_idx) IS NULL AND
8522                                v_wlji_compl_loc_id(v_idx) IS NOT NULL THEN
8523                                 v_wlji_compl_subinv(v_idx) := l_default_compl_subinv;
8524                             END IF;
8525 
8526                             IF v_wlji_compl_subinv(v_idx) IS NOT NULL AND
8527                                v_wlji_compl_loc_id(v_idx) IS NULL THEN
8528 l_stmt_num := 330.1;
8529                                 -- {{ validation of subinv begin
8530                                 hash_str   := to_char(v_wlji_org_id(v_idx))
8531                                               ||v_wlji_compl_subinv(v_idx);
8532                                 hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
8533                                 if WSMPLCVA.v_subinv.exists(hash_value) then
8534                                     NULL;
8535                                 else
8536                                     l_aux_mesg := '';
8537                                     handle_invalid_field(
8538                                         'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
8539                                     l_error_code := -1;
8540                                     l_error_count := l_error_count + 1;
8541                                     GOTO abort_this_job;
8542                                 end if;
8543                                 -- }} validation of subinv end
8544 l_stmt_num := 330.2;
8545                                 select locator_type
8546                                 into   l_mtl_locator_type
8547                                 from   mtl_secondary_inventories
8548                                 where  secondary_inventory_name
8549                                         = v_wlji_compl_subinv(v_idx)
8550                                 and    organization_id = v_wlji_org_id(v_idx);
8551 
8552                                 if v_wlji_compl_subinv(v_idx) = l_default_compl_subinv
8553                                 then
8554                                     v_wlji_compl_loc_id(v_idx) := l_default_compl_loc_id;
8555                                 else
8556                                     if l_mtl_locator_type = 2 then
8557                                         l_aux_mesg := '';
8558                                         handle_invalid_field(
8559                                             'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
8560                                         l_error_code := -1;
8561                                         l_error_count := l_error_count + 1;
8562                                         GOTO abort_this_job;
8563                                     end if;
8564                                 end if;
8565                             END IF;
8566 l_stmt_num := 330.3;
8567                             -- Get common routing seq id
8568                             l_error_msg := NULL;
8569                             wsmputil.find_common_routing(
8570                                 p_routing_sequence_id        => l_rtg_seq_id,
8571                                 p_common_routing_sequence_id => l_common_rtg_seq_id,
8572                                 x_err_code                   => l_error_code,
8573                                 x_err_msg                    => l_error_msg);
8574 
8575                             if l_error_code <> 0 OR l_error_msg IS NOT NULL then
8576                                 handle_error( l_error_code, l_error_msg, l_stmt_num);
8577                                 l_error_code := -1;
8578                                 l_error_count := l_error_count + 1;
8579                                 GOTO abort_this_job;
8580                             end if;
8581                             if lbji_debug = 'Y' then
8582                                 fnd_file.put_line(fnd_file.log,
8583                                     'Routing Sequence Id: '||l_rtg_seq_id ||
8584                                     ', Common Routing Sequence Id: '||l_common_rtg_seq_id);
8585                             end if;
8586                             -- }} get l_common_rtg_seq_id end
8587 
8588 
8589 l_stmt_num := 340;
8590                             -- {{ Get l_common_bom_seq_id (std job creation)
8591                             -- if the alternate_bom_designator has NULL in wlji, bill_seq_id
8592                             -- can have either NULL or a primary bom value. But if the
8593                             -- designator has ALT, then there must be a bill id for the
8594                             -- alternate bom.
8595                             IF v_wlji_alt_bom(v_idx) is NULL THEN
8596                                 begin
8597                                     SELECT  bom.common_bill_sequence_id
8598                                     INTO    l_common_bom_seq_id
8599                                     FROM    bom_bill_of_materials bom
8600                                     WHERE   bom.alternate_bom_designator is NULL
8601                                     AND     BOM.assembly_item_id = v_wlji_item_id(v_idx)
8602                                     AND     bom.organization_id = v_wlji_org_id(v_idx);
8603                                 exception
8604                                     WHEN NO_DATA_FOUND THEN
8605                                         NULL;
8606                                 end;
8607                             ELSE
8608 l_stmt_num := 345.1;
8609                                 begin
8610                                     SELECT  bom.common_bill_sequence_id
8611                                     INTO    l_common_bom_seq_id
8612                                     FROM    bom_bill_of_materials bom,
8613                                             bom_alternate_designators bad
8614                                     WHERE   ((bom.alternate_bom_designator is null and
8615                                               bad.alternate_designator_code is null and
8616                                               bad.organization_id = -1) OR
8617                                              (bom.alternate_bom_designator
8618                                                 = bad.alternate_designator_code and
8619                                               bom.organization_id = bad.organization_id))
8620                                     AND     bom.alternate_bom_designator
8621                                                 = v_wlji_alt_bom(v_idx)
8622                                     AND     BOM.assembly_item_id = v_wlji_item_id(v_idx)
8623                                     AND     bom.organization_id = v_wlji_org_id(v_idx);
8624                                     --Bug 5107339: Disable_date validation is not applicable here.
8625                                     --AND     trunc(nvl(bad.disable_date, sysdate + 1)) > trunc(sysdate);
8626 
8627                                 exception
8628                                     WHEN no_data_found  THEN
8629                                         l_aux_mesg := '';
8630                                         handle_invalid_field(
8631                                             'ALTERNATE BOM DESIGNATOR', l_aux_mesg, l_stmt_num);
8632                                         l_error_code := -1;
8633                                         l_error_count := l_error_count + 1;
8634                                         GOTO abort_this_job;
8635                                 end;
8636                             END IF;
8637                             if lbji_debug = 'Y' then
8638                                 fnd_file.put_line(fnd_file.log,
8639                                     'Getting Bill Sequence Id: '||l_common_bom_seq_id);
8640                             end if;
8641                             -- }} get l_common_bom_seq_id
8642 
8643 
8644 l_stmt_num := 350;
8645                             -- {{ Validate locator id (std job creation)
8646                             SELECT  nvl(msub.locator_type, 1) sub_loc_control,
8647                                     MP.stock_locator_control_code org_loc_control,
8648                                     MS.restrict_locators_code,
8649                                     MS.location_control_code item_loc_control
8650                             into    l_sub_loc_control,
8651                                     l_org_loc_control,
8652                                     l_restrict_locators_code,
8653                                     l_item_loc_control
8654                             FROM    mtl_system_items MS,
8655                                     mtl_secondary_inventories MSUB,
8656                                     mtl_parameters MP
8657                             WHERE   MP.organization_id = v_wlji_org_id(v_idx)
8658                             AND     MS.organization_id = v_wlji_org_id(v_idx)
8659                             AND     MS.inventory_item_id = v_wlji_item_id(v_idx)
8660                             AND     MSUB.secondary_inventory_name
8661                                         = v_wlji_compl_subinv(v_idx)
8662                             AND     MSUB.organization_id = v_wlji_org_id(v_idx);
8663 
8664                             l_locator_id := v_wlji_compl_loc_id(v_idx);
8665 
8666                             WIP_LOCATOR.validate(
8667                                     v_wlji_org_id(v_idx),
8668                                     v_wlji_item_id(v_idx),
8669                                     v_wlji_compl_subinv(v_idx),
8670                                     l_org_loc_control,
8671                                     l_sub_loc_control,
8672                                     l_item_loc_control,
8673                                     l_restrict_locators_code,
8674                                     NULL, NULL, NULL, NULL,
8675                                     l_locator_id,
8676                                     dummy_segs,
8677                                     l_temp_boolean);
8678 
8679                             IF not l_temp_boolean THEN
8680                                 l_aux_mesg := '';
8681                                 handle_invalid_field(
8682                                     'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
8683                                 l_error_code := -1;
8684                                 l_error_count := l_error_count + 1;
8685                                 GOTO abort_this_job;
8686                             end if;
8687                             if lbji_debug = 'Y' then
8688                                 fnd_file.put_line(fnd_file.log, 'Locator Id Validation.. OK.');
8689                             end if;
8690                             -- }} validate locator id
8691 
8692 
8693 l_stmt_num := 360;
8694                             -- {{ validate class code (std job creation)
8695                             begin
8696                                 IF v_wlji_class_code(v_idx) is NULL then
8697                                     begin
8698                                         select wse.default_acct_class_code
8699                                         into   v_wlji_class_code(v_idx)
8700                                         from   wsm_sector_extensions wse,
8701                                                wsm_item_extensions wie
8702                                         where  wie.inventory_item_id = v_wlji_item_id(v_idx)
8703                                         and    wie.organization_id = v_wlji_org_id(v_idx)
8704                                         and    wie.sector_extension_id = wse.sector_extension_id
8705                                         and    wie.organization_id = wse.organization_id;
8706                                     exception
8707                                         when no_data_found then
8708                                             v_wlji_class_code(v_idx) := NULL;
8709                                     end;
8710 
8711                                     IF v_wlji_class_code(v_idx) is NULL then
8712                                         begin
8713                                             select wse.default_acct_class_code
8714                                             into   v_wlji_class_code(v_idx)
8715                                             from   wsm_sector_extensions wse,
8716                                                    wsm_subinventory_extensions wve
8717                                             where  wve.secondary_inventory_name
8718                                                       = v_wlji_compl_subinv(v_idx)
8719                                             and    wve.organization_id = v_wlji_org_id(v_idx)
8720                                             and    wve.sector_extension_id = wse.sector_extension_id
8721                                             and    wve.organization_id = wse.organization_id;
8722                                         exception
8723                                             WHEN NO_DATA_FOUND THEN
8724                                                 v_wlji_class_code(v_idx) := NULL;
8725                                         end;
8726 
8727                                         IF v_wlji_class_code(v_idx) is NULL then
8728                                             v_wlji_class_code(v_idx)
8729                                                 := WSMPLCVA.v_org(v_wlji_org_id(v_idx)).DEFAULT_ACCT_CLASS_CODE;
8730                                         END IF;
8731                                     END IF;
8732 
8733                                     IF v_wlji_class_code(v_idx) IS NULL THEN
8734                                         raise no_data_found;
8735                                     END IF;
8736 
8737                                 ELSE
8738                                     hash_str   := to_char(v_wlji_org_id(v_idx))
8739                                                   ||v_wlji_class_code(v_idx);
8740                                     hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
8741                                     if WSMPLCVA.v_class_code.exists(hash_value) then
8742                                         NULL;
8743                                     else
8744                                         raise no_data_found;
8745                                     end if;
8746                                 END IF;
8747                             exception
8748                                 WHEN no_data_found  THEN
8749                                     l_aux_mesg := 'Class code may not be NULL';
8750                                     handle_invalid_field('CLASS_CODE', l_aux_mesg, l_stmt_num);
8751                                     l_error_code := -1;
8752                                     l_error_count := l_error_count + 1;
8753                                     GOTO abort_this_job;
8754                             end;
8755                             if lbji_debug = 'Y' then
8756                                 fnd_file.put_line(fnd_file.log, 'Class Code Validation.. OK.');
8757                             end if;
8758                             -- }} validate class code
8759 
8760 
8761 l_stmt_num := 370;
8762                             -- {{ Get revisions (std job creation)
8763                             -- this procedure is called only after date validation so that the
8764                             -- start date is not null. This is to be called only for job creation.
8765                             -- v_wlji_rtg_rev_date(v_idx) will be populated
8766 
8767                             if v_wlji_fusd(v_idx) > SYSDATE then
8768                                 l_rev_date := v_wlji_fusd(v_idx);
8769                             else
8770                                 l_rev_date := SYSDATE;
8771                             end if;
8772 
8773                             -- BA: bug 3437412 revision date should default to job start date
8774                             -- if v_wlji_fusd(v_idx) IS NULL (this situation can happen only when
8775                             -- scheduling_method=1 and fusd is not specified, l_rev_date = SYSDATE,
8776                             -- and routing/bom revision date will be default to SYSDATE,
8777                             if(v_wlji_rtg_rev_date(v_idx) IS NULL and
8778                                v_wlji_rtg_revision(v_idx) IS NULL) then
8779                                 v_wlji_rtg_rev_date(v_idx) := l_rev_date;
8780                             end if;
8781                             if(v_wlji_bom_rev_date(v_idx) IS NULL and
8782                                v_wlji_bom_revision(v_idx) IS NULL) then
8783                                 v_wlji_bom_rev_date(v_idx) := l_rev_date;
8784                             end if;
8785                             -- BA: bug 3437412 should default to job start date
8786 
8787                             wip_revisions.bom_revision (
8788                                     v_wlji_org_id(v_idx),
8789                                     v_wlji_item_id(v_idx),
8790                                     v_wlji_bom_revision(v_idx),
8791                                     v_wlji_bom_rev_date(v_idx),
8792                                     l_rev_date);
8793                             wip_revisions.routing_revision (
8794                                     v_wlji_org_id(v_idx),
8795                                     v_wlji_item_id(v_idx),
8796                                     v_wlji_rtg_revision(v_idx),
8797                                     v_wlji_rtg_rev_date(v_idx),
8798                                     l_rev_date);
8799 
8800                             if l_error_code <> 0 then
8801                                 l_error_code := -1;
8802                                 l_error_count := l_error_count + 1;
8803                                 GOTO abort_this_job;
8804                             end if;
8805                             if lbji_debug = 'Y' then
8806                                 fnd_file.put_line(fnd_file.log, 'Getting BOM/Routing Revisions.. OK.');
8807                             end if;
8808                             -- }} get revisions
8809 
8810                         end if; -- job type =1
8811                         -----------------------------------------------------
8812                         -- }}} End: validations for standard job creations
8813                         -----------------------------------------------------
8814 
8815 l_stmt_num := 380;
8816                         -- {{ Validate Date_Released, job_type 1 and 3
8817                         if (v_wlji_status_type(v_idx) = WIP_CONSTANTS.UNRELEASED) and
8818                            (v_wlji_date_released(v_idx) is not null ) then
8819 
8820                             v_wlji_date_released(v_idx) := null;
8821                             if lbji_debug = 'Y' then
8822                                 fnd_file.put_line(fnd_file.log,
8823                                     'Date_Released is ignored for unreleased job');
8824                             end if;
8825                         elsif (v_wlji_status_type(v_idx) = WIP_CONSTANTS.RELEASED ) then
8826                             if (v_wlji_date_released(v_idx) > sysdate ) then
8827                                 l_error_code := -1;
8828                                 handle_errmsg_name('WIP', 'WIP_INVALID_RELEASE_DATE', l_stmt_num);
8829                                 l_error_count := l_error_count + 1;
8830                                 GOTO abort_this_job;
8831                             elsif (v_wlji_date_released(v_idx) is null) then
8832                                 v_wlji_date_released(v_idx) := sysdate;
8833                             end if;
8834                         end if;
8835                         if lbji_debug = 'Y' then
8836                             fnd_file.put_line(fnd_file.log,
8837                                 'Date_Released Validation.. First Phase Done.');
8838                         end if;
8839                         -- }} Validate Date_Released
8840 
8841                         -- -------------------------------------------------------------------
8842                         -- {{{ Validations for the starting lot in wsli begin (std, creation)
8843                         -- -------------------------------------------------------------------
8844                         -- These validations are to be performed only for jobs of mode flag 2
8845                         -- Since mode 2 only support std job, v_wlji_job_type(v_indev) = 1
8846 
8847                         if v_wlji_mode_flag(v_idx) = 2 then
8848 l_stmt_num := 400;
8849                             -- last_updated_by
8850                             l_num_records := 0;
8851                             l_xst := WSMPLCVA.v_user.exists(
8852                                         v_wsli(v_wlji_src_line_id(v_idx)).last_updated_by);
8853                             if l_xst = false then
8854                                 begin
8855                                     select 1
8856                                     into   l_num_records
8857                                     from   fnd_user
8858                                     where  user_id = v_wsli(v_wlji_src_line_id(v_idx)).last_updated_by
8859                                     and    sysdate between start_date and nvl(end_date,sysdate+1);
8860                                 exception
8861                                     when too_many_rows then l_num_records := 1;
8862                                     when no_data_found then
8863                                         l_error_code := -1;
8864                                         l_aux_mesg := '';
8865                                         handle_invalid_field(
8866                                             'Last Updated By in WSM_STARTING_LOTS_INTERFACE',
8867                                             l_aux_mesg,
8868                                             l_stmt_num);
8869                                         l_error_count := l_error_count + 1;
8870                                         GOTO abort_this_job;
8871                                 end;
8872                             end if;
8873 
8874                             if l_xst = false AND l_num_records <> 0 then
8875                                 WSMPLCVA.v_user(v_wsli(v_wlji_src_line_id(v_idx)).last_updated_by)
8876                                     := v_wsli(v_wlji_src_line_id(v_idx)).last_updated_by;
8877                             end if;
8878 l_stmt_num := 410;
8879                             -- wsli.created_by
8880                             l_num_records := 0;
8881                             l_xst := WSMPLCVA.v_user.exists(v_wsli(v_wlji_src_line_id(v_idx)).created_by);
8882                             if l_xst = false then
8883                                 begin
8884                                     select 1
8885                                     into   l_num_records
8886                                     from   fnd_user
8887                                     where  user_id = v_wsli(v_wlji_src_line_id(v_idx)).created_by
8888                                     and    sysdate between start_date and nvl(end_date,sysdate+1);
8889                                 exception
8890                                     when too_many_rows then l_num_records := 1;
8891                                     when no_data_found then
8892                                         l_error_code := -1;
8893                                         l_aux_mesg := '';
8894                                         handle_invalid_field(
8895                                             'Created By in WSM_STARTING_LOTS_INTERFACE',
8896                                             l_aux_mesg,
8897                                             l_stmt_num);
8898                                         l_error_count := l_error_count + 1;
8899                                         GOTO abort_this_job;
8900                                 end;
8901                             end if;
8902 
8903                             if l_xst = false AND l_num_records <> 0 then
8904                                 WSMPLCVA.v_user(v_wsli(v_wlji_src_line_id(v_idx)).created_by)
8905                                     := v_wsli(v_wlji_src_line_id(v_idx)).created_by;
8906                             end if;
8907 
8908 l_stmt_num := 420;
8909                             -- Check.MMTT
8910                             begin
8911                                 select 1
8912                                 into   l_temp_num
8913                                 from   mtl_transaction_lots_temp
8914                                 where  lot_number = v_wsli(v_wlji_src_line_id(v_idx)).lot_number
8915                                 and    rownum = 1;
8916 
8917                                 if l_temp_num <>0 then
8918                                     select 0
8919                                     into   l_temp_num
8920                                     from   mtl_material_transactions_temp mmtt
8921                                     where  mmtt.organization_id        = v_wsli(v_wlji_src_line_id(v_idx)).organization_id
8922                                     and    mmtt.inventory_item_id      = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
8923                                     and    nvl(mmtt.lot_number, '@#$') = v_wsli(v_wlji_src_line_id(v_idx)).lot_number
8924                                     and    mmtt.subinventory_code      = v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code
8925                                     and    nvl(mmtt.locator_id, -9999) = nvl(v_wsli(v_wlji_src_line_id(v_idx)).locator_id, -9999)
8926                                     and    mmtt.transaction_type_id    = 42 -- miscellaneous receipt
8927                                     and    mmtt.transaction_action_id  = 27 -- receipt into stores
8928                                     and    mmtt.transaction_source_type_id = 13 -- inventory
8929                                     and    v_wsli(v_wlji_src_line_id(v_idx)).quantity = ((-1) * mmtt.transaction_quantity)
8930                                     and    mmtt.transaction_date = (
8931                                                 select max(mmtt2.transaction_date)
8932                                                 from   mtl_material_transactions_temp mmtt2
8933                                                 where  mmtt2.organization_id        = v_wsli(v_wlji_src_line_id(v_idx)).organization_id
8934                                                 and    mmtt2.inventory_item_id      = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
8935                                                 and    nvl(mmtt2.lot_number, '@#$') = nvl(v_wsli(v_wlji_src_line_id(v_idx)).lot_number, '@#$')
8936                                                 and    mmtt2.subinventory_code      = v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code
8937                                                 and    nvl(mmtt2.locator_id, -9999) = nvl(v_wsli(v_wlji_src_line_id(v_idx)).locator_id, -9999)
8938                                             );
8939                                 end if;
8940                                 If l_temp_num <> 0 then
8941                                     fnd_message.set_name('WSM', 'WSM_PENDING_TXN');
8942                                     fnd_message.set_token('TABLE','Starting Lot:' ||
8943                                         v_wsli(v_wlji_src_line_id(v_idx)).lot_number ||
8944                                         'Table: mtl_transaction_lots_temp ');
8945                                     l_error_msg := fnd_message.get;
8946                                     l_error_code := -1;
8947                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
8948                                     l_error_count := l_error_count + 1;
8949                                     GOTO abort_this_job;
8950                                 END IF;
8951 
8952                             exception
8953                                 when no_data_found Then
8954                                     null;
8955                             end;
8956 --Bug 5182520:Added validation on Material Status
8957 l_stmt_num := 422;
8958                             IF (WSMPUTIL.is_status_applicable(p_wms_installed =>NULL,
8959                               p_trx_status_enabled    => NULL,
8960                               p_trx_type_id           => 35,
8961                               p_lot_status_enabled    => NULL,
8962                               p_serial_status_enabled => NULL,
8963                               p_organization_id       => v_wsli(v_wlji_src_line_id(v_idx)).organization_id,
8964                               p_inventory_item_id     => v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
8965                               p_sub_code              => v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code,
8966                               p_locator_id            => v_wsli(v_wlji_src_line_id(v_idx)).locator_id,
8967                               p_lot_number            => v_wsli(v_wlji_src_line_id(v_idx)).lot_number,
8968                               p_serial_number         => NULL,
8969                               x_error_msg             => l_error_msg) = 'N') THEN
8970 
8971 	                            l_error_code := -1;
8972                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
8973                                     l_error_count := l_error_count + 1;
8974                                     GOTO abort_this_job;
8975 
8976                             END IF;
8977 
8978 l_stmt_num := 430;
8979                             -- {{ kanban begin
8980                             if v_wlji_kanban_card_id(v_idx) is not null then
8981                                 if WSMPLBJI.honor_kanban_size(
8982                                             v_wlji_org_id(v_idx),
8983                                             v_wlji_item_id(v_idx),
8984                                             v_wlji_compl_subinv(v_idx),
8985                                             v_wlji_compl_loc_id(v_idx),
8986                                             -1) = 1
8987                                 then
8988 l_stmt_num := 430.1;
8989                                     select kanban_size
8990                                     into   l_kanban_size
8991                                     from   mtl_kanban_cards
8992                                     where  kanban_card_id = v_wlji_kanban_card_id(v_idx);
8993                                     if v_wlji_start_quantity(v_idx) > l_kanban_size then
8994                                         v_wlji_start_quantity(v_idx) := l_kanban_size;
8995                                         v_wlji_net_quantity(v_idx)   := v_wlji_start_quantity(v_idx);
8996                                     end if;
8997                                 end if;
8998                             end if;
8999                             -- }} Kanban end
9000 
9001 l_stmt_num := 440;
9002                             -- ------------------------------------------------------------
9003                             -- Check if the component is required at first operation
9004                             -- Check component quantity
9005                             --
9006                             -- The code should be moved to where job details are imported
9007                             -- because user intention should be honored
9008                             -- ------------------------------------------------------------
9009 
9010 
9011                         end if; -- mode_flag 2
9012                         -- ----------------------------------------------------------------
9013                         -- }}} Validations for the starting lot in wsli end (std, creation)
9014                         -- ----------------------------------------------------------------
9015 
9016                     end if;  -- load_type = 5
9017 -- ===========================================================================================
9018 -- END: Job Creation Validations }}}
9019 -- ===========================================================================================
9020 
9021 
9022 
9023 -- ===========================================================================================
9024 -- BEGIN: Reschedule (Update) Job Validations {{{
9025 -- ===========================================================================================
9026                     if v_wlji_load_type(v_idx) = 6 then
9027                         --------------------------------------------
9028                         -- {{{ Begin: Update of alternates (std job)
9029                         --------------------------------------------
9030                         -- If the user wants to update an alternate to NULL value, he should
9031                         -- populate '-99' in the interface table. If NULL is populated, we
9032                         -- consider that user does not want to change ALT
9033                         if l_cur_job_type = 1 then  -- {{ if std-job
9034 l_stmt_num := 500;
9035                             l_chg_bom_alt :=0;
9036                             l_chg_rtg_alt :=0;
9037                             l_chg_alt_flag:=0;
9038 
9039                             if v_wlji_alt_bom(v_idx) is NOT NULL AND
9040                                ((l_cur_alt_bom is NULL AND
9041                                  v_wlji_alt_bom(v_idx) <> '-99') OR
9042                                 (l_cur_alt_bom is NOT NULL AND
9043                                  v_wlji_alt_bom(v_idx) <> l_cur_alt_bom))
9044                             then
9045                                 l_chg_bom_alt := 1;
9046                             end if;
9047 
9048                             if v_wlji_alt_rtg(v_idx) is NOT NULL AND
9049                                ((l_cur_alt_rtg is NULL AND
9050                                  v_wlji_alt_rtg(v_idx) <> '-99') OR
9051                                 (l_cur_alt_rtg is NOT NULL AND
9052                                  v_wlji_alt_rtg(v_idx) <> l_cur_alt_rtg))
9053                             then
9054                                 l_chg_rtg_alt := 1;
9055                             end if;
9056 
9057                             -- {{ Change bom/rtg alternate
9058                             if (l_chg_bom_alt = 1 OR l_chg_rtg_alt = 1) and
9059                                l_cur_status_type = 1
9060                             then
9061                                 l_chg_alt_flag := 1;
9062 
9063                                 -- populate local variables
9064                                 v_wlji_item_id(v_idx) := l_cur_primary_item_id;
9065 
9066                                 if l_chg_bom_alt = 1 then
9067                                     if v_wlji_alt_bom(v_idx) = '-99' then
9068                                         v_wlji_alt_bom(v_idx) := null;
9069                                     end if;
9070                                 else
9071                                     v_wlji_alt_bom(v_idx) := l_cur_alt_bom;
9072                                 end if;
9073 
9074                                 if l_chg_rtg_alt = 1 then
9075                                     if v_wlji_alt_rtg(v_idx) = '-99' then
9076                                         v_wlji_alt_rtg(v_idx) := null;
9077                                     end if;
9078                                 else
9079                                     v_wlji_alt_rtg(v_idx) := l_cur_alt_rtg;
9080                                 end if;
9081 
9082 l_stmt_num := 510;
9083                                 -- Now validate the alternate designators to get the NEW
9084                                 -- common routing and bom sequence id.
9085                                 -- Also get the completion subinventory and locator.
9086                                 -- If the user has provided them, use them after validation,
9087                                 -- else use these default values. Also validate/default
9088                                 -- the bom/rtg revision and revision-dates
9089                                 begin
9090                                     select bor.routing_sequence_id,
9091                                            bor.COMPLETION_SUBINVENTORY,
9092                                            bor.COMPLETION_LOCATOR_ID
9093                                     into   l_rtg_seq_id,
9094                                            l_default_compl_subinv,
9095                                            l_default_compl_loc_id
9096                                     from   bom_operational_routings bor,
9097                                            bom_alternate_designators bad
9098                                     where  ((bor.alternate_routing_designator is null and
9099                                              bad.alternate_designator_code is null and
9100                                              bad.organization_id = -1) OR
9101                                             (bor.alternate_routing_designator
9102                                                 = bad.alternate_designator_code and
9103                                              bor.organization_id = bad.organization_id))
9104                                     and    bor.organization_id = v_wlji_org_id(v_idx)
9105                                     and    bor.assembly_item_id = v_wlji_item_id(v_idx)
9106                                     and    NVL(bor.alternate_routing_designator, '&*')
9107                                                = NVL(v_wlji_alt_rtg(v_idx), '&*')
9108                                     and    bor.routing_type = 1
9109                                     and    bor.cfm_routing_flag = 3;
9110                                     --Bug 5107339: Disable_date validation is not applicable here.
9111                                     --and    trunc(nvl(bad.disable_date, sysdate + 1))
9112                                      --           > trunc(sysdate);
9113                                 exception
9114                                     when no_data_found then
9115                                         l_aux_mesg := '';
9116                                         handle_invalid_field(
9117                                             'Alternate Routing Designator',
9118                                             l_aux_mesg, l_stmt_num);
9119                                         l_error_code := -1;
9120                                         l_error_count := l_error_count + 1;
9121                                         GOTO skip_resched_validations;
9122                                 end;
9123 l_stmt_num := 520;
9124                                 -- find l_common_rtg_seq_id
9125                                 l_error_msg := NULL;
9126                                 wsmputil.find_common_routing(
9127                                     p_routing_sequence_id        => l_rtg_seq_id,
9128                                     p_common_routing_sequence_id => l_common_rtg_seq_id,
9129                                     x_err_code                   => l_error_code,
9130                                     x_err_msg                    => l_error_msg);
9131                                 if l_error_code <> 0 OR l_error_msg IS NOT NULL then
9132                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
9133                                     l_error_code := -1;
9134                                     l_error_count := l_error_count + 1;
9135                                     GOTO skip_resched_validations;
9136                                 end if;
9137 
9138 l_stmt_num := 530;
9139                                 IF v_wlji_compl_subinv(v_idx) IS NULL AND
9140                                    v_wlji_compl_loc_id(v_idx) IS NULL THEN
9141                                     v_wlji_compl_subinv(v_idx) := l_default_compl_subinv;
9142                                     v_wlji_compl_loc_id(v_idx) := l_default_compl_loc_id;
9143                                 END IF;
9144 
9145                                 IF v_wlji_compl_subinv(v_idx) IS NULL AND
9146                                    v_wlji_compl_loc_id(v_idx) IS NOT NULL THEN
9147                                     v_wlji_compl_subinv(v_idx) := l_default_compl_subinv;
9148                                 END IF;
9149 
9150                                 IF v_wlji_compl_subinv(v_idx) IS NOT NULL AND
9151                                    v_wlji_compl_loc_id(v_idx) IS NULL THEN
9152 
9153                                     hash_str   := to_char(v_wlji_org_id(v_idx))
9154                                                   ||v_wlji_compl_subinv(v_idx);
9155                                     hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
9156                                     if WSMPLCVA.v_subinv.exists(hash_value) then
9157                                         NULL;
9158                                     else
9159                                         l_aux_mesg := '';
9160                                         handle_invalid_field(
9161                                             'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
9162                                         l_error_code := -1;
9163                                         l_error_count := l_error_count + 1;
9164                                         GOTO skip_resched_validations;
9165                                     end if;
9166 
9167 l_stmt_num := 540;
9168                                     select locator_type
9169                                     into   l_mtl_locator_type
9170                                     from   mtl_secondary_inventories
9171                                     where  secondary_inventory_name
9172                                                = v_wlji_compl_subinv(v_idx)
9173                                     and    organization_id = v_wlji_org_id(v_idx);
9174 
9175                                     if v_wlji_compl_subinv(v_idx)
9176                                          = l_default_compl_subinv
9177                                     then
9178                                         v_wlji_compl_loc_id(v_idx)
9179                                              := l_default_compl_loc_id;
9180                                     else
9181                                         if l_mtl_locator_type = 2 then
9182                                             l_aux_mesg := '';
9183                                             handle_invalid_field(
9184                                                 'COMPLETION SUBINVENTORY',
9185                                                 l_aux_mesg, l_stmt_num);
9186                                             l_error_code := -1;
9187                                             l_error_count := l_error_count + 1;
9188                                             GOTO skip_resched_validations;
9189                                         end if;
9190                                     end if;
9191                                 END IF;
9192 
9193 l_stmt_num := 550;
9194                                 IF v_wlji_alt_bom(v_idx) is NULL THEN
9195                                     begin
9196                                         select  bom.common_bill_sequence_id
9197                                         into    l_common_bom_seq_id
9198                                         from    bom_bill_of_materials bom
9199                                         where   bom.alternate_bom_designator is null
9200                                         and     bom.assembly_item_id = v_wlji_item_id(v_idx)
9201                                         and     bom.organization_id = v_wlji_org_id(v_idx);
9202                                     exception
9203                                         WHEN NO_DATA_FOUND THEN
9204                                             NULL;
9205                                     end;
9206                                 ELSE
9207                                     begin
9208                                         select  bom.common_bill_sequence_id
9209                                         into    l_common_bom_seq_id
9210                                         from    bom_bill_of_materials bom,
9211                                                 bom_alternate_designators bad
9212                                         where   ((bom.alternate_bom_designator is null and
9213                                                   bad.alternate_designator_code is null and
9214                                                   bad.organization_id = -1) or
9215                                                  (bom.alternate_bom_designator
9216                                                       = bad.alternate_designator_code and
9217                                                   bom.organization_id = bad.organization_id))
9218                                         and     bom.alternate_bom_designator
9219                                                     = v_wlji_alt_bom(v_idx)
9220                                         and     bom.assembly_item_id = v_wlji_item_id(v_idx)
9221                                         and     bom.organization_id = v_wlji_org_id(v_idx);
9222                                         --Bug 5107339: Disable_date validation is not applicable here.
9223                                         --  and     trunc(nvl(bad.disable_date, sysdate + 1))
9224                                         --             > trunc(sysdate);
9225                                     exception
9226                                         WHEN no_data_found  THEN
9227                                             l_aux_mesg := '';
9228                                             handle_invalid_field(
9229                                                 'Alternate Bom Designator', l_aux_mesg, l_stmt_num);
9230                                             l_error_code := -1;
9231                                             l_error_count := l_error_count + 1;
9232                                             GOTO skip_resched_validations;
9233                                     end;
9234                                 END IF;
9235 
9236 l_stmt_num := 560;
9237                                 SELECT  nvl(msub.locator_type, 1) sub_loc_control,
9238                                         MP.stock_locator_control_code org_loc_control,
9239                                         MS.restrict_locators_code,
9240                                         MS.location_control_code item_loc_control
9241                                 into    l_sub_loc_control,
9242                                         l_org_loc_control,
9243                                         l_restrict_locators_code,
9244                                         l_item_loc_control
9245                                 FROM    mtl_system_items MS,
9246                                         mtl_secondary_inventories MSUB,
9247                                         mtl_parameters MP
9248                                 WHERE   MP.organization_id = v_wlji_org_id(v_idx)
9249                                 AND     MS.organization_id = v_wlji_org_id(v_idx)
9250                                 AND     MS.inventory_item_id = v_wlji_item_id(v_idx)
9251                                 AND     MSUB.secondary_inventory_name
9252                                             = v_wlji_compl_subinv(v_idx)
9253                                 AND     MSUB.organization_id = v_wlji_org_id(v_idx);
9254 
9255 l_stmt_num := 570;
9256                                 l_locator_id := v_wlji_compl_loc_id(v_idx);
9257                                 WIP_LOCATOR.validate(
9258                                         v_wlji_org_id(v_idx),
9259                                         v_wlji_item_id(v_idx),
9260                                         v_wlji_compl_subinv(v_idx),
9261                                         l_org_loc_control,
9262                                         l_sub_loc_control,
9263                                         l_item_loc_control,
9264                                         l_restrict_locators_code,
9265                                         NULL, NULL, NULL, NULL,
9266                                         l_locator_id,
9267                                         dummy_segs,
9268                                         l_temp_boolean);
9269                                 IF not l_temp_boolean THEN
9270                                     l_aux_mesg := '';
9271                                     handle_invalid_field(
9272                                         'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
9273                                     l_error_code := -1;
9274                                     l_error_count := l_error_count + 1;
9275                                     GOTO skip_resched_validations;
9276                                 end if;
9277 
9278 l_stmt_num := 580;
9279                                 if v_wlji_fusd(v_idx) is null then
9280                                     v_wlji_fusd(v_idx) := l_cur_start_date;
9281                                 end if;
9282 
9283                                 if v_wlji_fusd(v_idx) > SYSDATE then
9284                                     l_rev_date := v_wlji_fusd(v_idx);
9285                                 else
9286                                     l_rev_date := SYSDATE;
9287                                 end if;
9288 l_stmt_num := 590.1;
9289                                 wip_revisions.bom_revision (
9290                                         v_wlji_org_id(v_idx),
9291                                         v_wlji_item_id(v_idx),
9292                                         v_wlji_bom_revision(v_idx),
9293                                         v_wlji_bom_rev_date(v_idx),
9294                                         l_rev_date);
9295 l_stmt_num := 590.2;
9296                                 wip_revisions.routing_revision (
9297                                         v_wlji_org_id(v_idx),
9298                                         v_wlji_item_id(v_idx),
9299                                         v_wlji_rtg_revision(v_idx),
9300                                         v_wlji_rtg_rev_date(v_idx),
9301                                         l_rev_date);
9302 
9303                             end if; -- }} Change bom/routing alternate
9304 
9305                             if (l_chg_bom_alt = 1 OR l_chg_rtg_alt = 1) and
9306                                l_cur_status_type <> 1
9307                             then
9308                                 fnd_file.put_line(fnd_file.log,
9309                                     'Ignoring the following fields if provided by user: ' ||
9310                                     'Alternate_Routing_Designator, Alternate_Bom_Designator.');
9311                             end if;
9312 
9313                             v_wlji_bom_ref_id(v_idx) := null;
9314                             v_wlji_rtg_ref_id(v_idx) := null;
9315                             if l_chg_alt_flag = 0 then
9316                                 l_common_bom_seq_id        := l_cur_com_bom_seq_id;
9317                                 v_wlji_alt_bom(v_idx)      := l_cur_alt_bom;
9318                                 --v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date; -- DEL bug3202534
9319                                 --v_wlji_bom_revision(v_idx) := l_cur_bom_revision; -- DEL bug3202534
9320                                 l_common_rtg_seq_id        := l_cur_com_rtg_seq_id;
9321                                 v_wlji_alt_rtg(v_idx)      := l_cur_alt_rtg;
9322                                 --v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date; -- DEL bug3202534
9323                                 --v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision; -- DEL bug3202534
9324                                 --v_wlji_compl_subinv(v_idx) := l_cur_compl_subinv; Bug 4919218
9325                                 --v_wlji_compl_loc_id(v_idx) := l_cur_compl_locator; Bug 4919218
9326                                 -- Validation added for Bug 4919218: Start
9327 l_stmt_num := 590.3;
9328                                 IF v_wlji_compl_subinv(v_idx) IS NULL AND
9329                                    v_wlji_compl_loc_id(v_idx) IS NULL THEN
9330                                     v_wlji_compl_subinv(v_idx) := l_cur_compl_subinv;
9331                                     v_wlji_compl_loc_id(v_idx) := l_cur_compl_locator;
9332                                 END IF;
9333 
9334                                 IF v_wlji_compl_subinv(v_idx) IS NULL AND
9335                                    v_wlji_compl_loc_id(v_idx) IS NOT NULL THEN
9336                                     v_wlji_compl_subinv(v_idx) := l_cur_compl_subinv;
9337                                 END IF;
9338 
9339                                 IF v_wlji_compl_subinv(v_idx) IS NOT NULL AND
9340                                    v_wlji_compl_loc_id(v_idx) IS NULL THEN
9341 
9342                                     hash_str   := to_char(v_wlji_org_id(v_idx))
9343                                                   ||v_wlji_compl_subinv(v_idx);
9344                                     hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
9345                                     if WSMPLCVA.v_subinv.exists(hash_value) then
9346                                         NULL;
9347                                     else
9348                                         l_aux_mesg := '';
9349                                         handle_invalid_field(
9350                                             'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
9351                                         l_error_code := -1;
9352                                         l_error_count := l_error_count + 1;
9353                                         GOTO skip_resched_validations;
9354                                     end if;
9355 
9356 l_stmt_num := 590.4;
9357                                     select locator_type
9358                                     into   l_mtl_locator_type
9359                                     from   mtl_secondary_inventories
9360                                     where  secondary_inventory_name
9361                                                = v_wlji_compl_subinv(v_idx)
9362                                     and    organization_id = v_wlji_org_id(v_idx);
9363 
9364                                     if v_wlji_compl_subinv(v_idx)
9365                                          = l_cur_compl_subinv
9366                                     then
9367                                         v_wlji_compl_loc_id(v_idx)
9368                                              := l_cur_compl_locator;
9369                                     else
9370                                         if l_mtl_locator_type = 2 then
9371                                             l_aux_mesg := '';
9372                                             handle_invalid_field(
9373                                                 'COMPLETION LOCATOR',
9374                                                 l_aux_mesg, l_stmt_num);
9375                                             l_error_code := -1;
9376                                             l_error_count := l_error_count + 1;
9377                                             GOTO skip_resched_validations;
9378                                         end if;
9379                                     end if;
9380                                 END IF;
9381 
9382 l_stmt_num := 590.5;
9383                                 SELECT  nvl(msub.locator_type, 1) sub_loc_control,
9384                                         MP.stock_locator_control_code org_loc_control,
9385                                         MS.restrict_locators_code,
9386                                         MS.location_control_code item_loc_control
9387                                 into    l_sub_loc_control,
9388                                         l_org_loc_control,
9389                                         l_restrict_locators_code,
9390                                         l_item_loc_control
9391                                 FROM    mtl_system_items MS,
9392                                         mtl_secondary_inventories MSUB,
9393                                         mtl_parameters MP
9394                                 WHERE   MP.organization_id = v_wlji_org_id(v_idx)
9395                                 AND     MS.organization_id = v_wlji_org_id(v_idx)
9396                                 AND     MS.inventory_item_id = v_wlji_item_id(v_idx)
9397                                 AND     MSUB.secondary_inventory_name
9398                                             = v_wlji_compl_subinv(v_idx)
9399                                 AND     MSUB.organization_id = v_wlji_org_id(v_idx);
9400 
9401 l_stmt_num := 590.6;
9402                                 l_locator_id := v_wlji_compl_loc_id(v_idx);
9403                                 WIP_LOCATOR.validate(
9404                                         v_wlji_org_id(v_idx),
9405                                         v_wlji_item_id(v_idx),
9406                                         v_wlji_compl_subinv(v_idx),
9407                                         l_org_loc_control,
9408                                         l_sub_loc_control,
9409                                         l_item_loc_control,
9410                                         l_restrict_locators_code,
9411                                         NULL, NULL, NULL, NULL,
9412                                         l_locator_id,
9413                                         dummy_segs,
9414                                         l_temp_boolean);
9415                                 IF not l_temp_boolean THEN
9416                                     l_aux_mesg := '';
9417                                     handle_invalid_field(
9418                                         'COMPLETION LOCATOR', l_aux_mesg, l_stmt_num);
9419                                     l_error_code := -1;
9420                                     l_error_count := l_error_count + 1;
9421                                     GOTO skip_resched_validations;
9422                                 end if;
9423 
9424 l_stmt_num := 590.7;
9425                             -- Validation added for Bug 4919218: End
9426                             else
9427                                 if lbji_debug = 'Y' then
9428                                     fnd_file.put_line(fnd_file.log,
9429                                         'Update BOM/Routing alternates for standard job..');
9430                                 end if;
9431                             end if;
9432 
9433                         end if;     -- }} if std-job
9434                         ---------------------------------
9435                         -- }}} End: Update of alternates
9436                         ---------------------------------
9437 
9438                         --------------------------------------------
9439                         -- {{{ Begin: Update of references (non-std)
9440                         --------------------------------------------
9441                         -- Non standard job bom/routing reference update.
9442                         -- The assumption is that this module will NOT update the
9443                         -- qty/dates/etc.,they will be taken care in the later modules.
9444                         -- This module will just look at the reference info, and assume
9445                         -- qty/date/etc to be same as the old values.
9446                         -- Alt routing/bom will be taken care of here
9447                         if l_cur_job_type = 3 then      --  {{ if l_cur_job_type = 3
9448 l_stmt_num:= 600;
9449                             l_chg_bom_ref  := 0;
9450                             l_chg_rtg_ref  := 0;
9451                             l_chg_ref_flag := 0;
9452 
9453                             if v_wlji_rtg_ref_id(v_idx) is not null then
9454                                 l_chg_rtg_ref := 1;
9455                             end if;
9456 
9457                             if v_wlji_bom_ref_id(v_idx) is not null then
9458                                 l_chg_bom_ref := 1;
9459                             end if;
9460 
9461 
9462                             -- {{ only allow change for unreleased job, l_cur_status_type = 1
9463                             if (l_chg_bom_ref = 1 or l_chg_rtg_ref = 1) and
9464                                l_cur_status_type = 1
9465                             then
9466                                 l_chg_ref_flag := 1;
9467 l_stmt_num:= 610;
9468                                 if l_chg_bom_ref = 1 then
9469                                     wsmputil.validate_non_std_references(
9470                                         p_assembly_item_id       => null,
9471                                         p_routing_reference_id   => null,
9472                                         p_bom_reference_id       => v_wlji_bom_ref_id(v_idx),
9473                                         p_alt_routing_designator => null,
9474                                         p_alt_bom_designator     => v_wlji_alt_bom(v_idx),
9475                                         p_organization_id        => v_wlji_org_id(v_idx),
9476                                         p_start_date             => sysdate, -- not used after all
9477                                         p_end_date               => null,
9478                                         p_start_quantity         => null,
9479                                         p_mrp_net_quantity       => dummy_number,
9480                                         p_class_code             => null,
9481                                         p_completion_subinventory=> null,
9482                                         p_completion_locator_id  => null,
9483                                         p_firm_planned_flag      => dummy_number,
9484                                         p_bom_revision           => v_wlji_bom_revision(v_idx),
9485                                         p_bom_revision_date      => v_wlji_bom_rev_date(v_idx),
9486                                         p_routing_revision       => dummy_varchar,
9487                                         p_routing_revision_date  => dummy_date,
9488                                         x_routing_seq_id         => dummy_number,
9489                                         x_bom_seq_id             => l_common_bom_seq_id,
9490                                         validation_level         => 1,
9491                                         x_error_code             => l_error_code,
9492                                         x_err_msg                => l_error_msg);
9493 
9494                                     if l_error_code <> 0 then
9495                                         handle_error( l_error_code, l_error_msg, l_stmt_num);
9496                                         l_error_code := -1;
9497                                         GOTO skip_resched_validations;
9498                                     end if;
9499                                 end if;
9500 l_stmt_num:= 620;
9501                                 if l_chg_rtg_ref = 1 then
9502                                     wsmputil.validate_non_std_references(
9503                                         p_assembly_item_id       => null,
9504                                         p_routing_reference_id   => v_wlji_rtg_ref_id(v_idx),
9505                                         p_bom_reference_id       => null,
9506                                         p_alt_routing_designator => v_wlji_alt_rtg(v_idx),
9507                                         p_alt_bom_designator     => null,
9508                                         p_organization_id        => v_wlji_org_id(v_idx),
9509                                         p_start_date             => sysdate, -- not used after all
9510                                         p_end_date               => null,
9511                                         p_start_quantity         => null,
9512                                         p_mrp_net_quantity       => dummy_number,
9513                                         p_class_code             => null,
9514                                         p_completion_subinventory=> null,
9515                                         p_completion_locator_id  => null,
9516                                         p_firm_planned_flag      => dummy_number,
9517                                         p_bom_revision           => dummy_varchar,
9518                                         p_bom_revision_date      => dummy_date,
9519                                         p_routing_revision       => v_wlji_rtg_revision(v_idx),
9520                                         p_routing_revision_date  => v_wlji_rtg_rev_date(v_idx),
9521                                         x_routing_seq_id         => l_rtg_seq_id,
9522                                         x_bom_seq_id             => dummy_number,
9523                                         validation_level         => 2,
9524                                         x_error_code             => l_error_code,
9525                                         x_err_msg                => l_error_msg);
9526                                     if l_error_code <> 0 then
9527                                         handle_error( l_error_code, l_error_msg, l_stmt_num);
9528                                         l_error_code := -1;
9529                                         GOTO skip_resched_validations;
9530                                     end if;
9531 l_stmt_num:= 630;
9532                                     -- find the common rtg seq id
9533                                     l_error_msg := NULL;
9534                                     wsmputil.find_common_routing(
9535                                         p_routing_sequence_id        => l_rtg_seq_id,
9536                                         p_common_routing_sequence_id => l_common_rtg_seq_id,
9537                                         x_err_code                   => l_error_code,
9538                                         x_err_msg                    => l_error_msg);
9539 
9540                                      if l_error_code <> 0 OR l_error_msg IS NOT NULL then
9541                                         handle_error( l_error_code, l_error_msg, l_stmt_num);
9542                                         l_error_code := -1;
9543                                         GOTO skip_resched_validations;
9544                                     end if;
9545                                 end if;
9546 
9547                                 if l_chg_rtg_ref = 1 and l_chg_bom_ref = 0 then
9548                                     l_common_bom_seq_id        := l_cur_com_bom_seq_id;
9549                                     --v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date; -- DEL bug3202534
9550                                     --v_wlji_bom_revision(v_idx) := l_cur_bom_revision; -- DEL bug3202534
9551                                     v_wlji_bom_ref_id(v_idx)   := l_cur_bom_ref_id;
9552                                     v_wlji_alt_bom(v_idx)      := l_cur_alt_bom;
9553                                 end if;
9554 
9555                                 if l_chg_rtg_ref = 0 AND l_chg_bom_ref = 1 then
9556                                     l_common_rtg_seq_id        := l_cur_com_rtg_seq_id;
9557                                     --v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date; -- DEL bug3202534
9558                                     --v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision; -- DEL bug3202534
9559                                     v_wlji_rtg_ref_id(v_idx)   := l_cur_rtg_ref_id;
9560                                     v_wlji_alt_rtg(v_idx)      := l_cur_alt_rtg;
9561                                 end if;
9562 
9563                             end if; -- }} old_status_type = 1
9564 
9565                             -- user can update the bom_reference and the routing reference of a
9566                             -- non-standard job only if the job is unreleased.
9567                             if (l_chg_bom_ref = 1 or l_chg_rtg_ref = 1) and
9568                                 l_cur_status_type <> 1
9569                             then
9570                                 fnd_file.put_line(fnd_file.log,
9571                                     'Ignoring the following fields if provided by user: ' ||
9572                                     'Routing_Reference_Id, Bom_Reference_Id, ' ||
9573                                     'Alternate_Bom_Designator, Alternate_Routing_Designator');
9574                             end if;
9575 
9576                             if l_chg_ref_flag = 0 then
9577                                 l_common_bom_seq_id        := l_cur_com_bom_seq_id;
9578                                 --v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date; -- DEL bug3202534
9579                                 --v_wlji_bom_revision(v_idx) := l_cur_bom_revision; -- DEL bug3202534
9580                                 v_wlji_bom_ref_id(v_idx)   := l_cur_bom_ref_id;
9581                                 v_wlji_alt_bom(v_idx)      := l_cur_alt_bom;
9582                                 l_common_rtg_seq_id        := l_cur_com_rtg_seq_id;
9583                                 --v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date; -- DEL bug3202534
9584                                 --v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision; -- DEL bug3202534
9585                                 v_wlji_rtg_ref_id(v_idx)   := l_cur_rtg_ref_id;
9586                                 v_wlji_alt_rtg(v_idx)      := l_cur_alt_rtg;
9587                             else
9588                                 if lbji_debug = 'Y' then
9589                                     fnd_file.put_line(fnd_file.log,
9590                                         'Update BOM/Routing references for non-standard job..');
9591                                 end if;
9592                             end if;
9593 
9594                         end if; -- }} job type = 3
9595                         ---------------------------------
9596                         -- }}} End: Update of references
9597                         ---------------------------------
9598 
9599                         --------------------------------------------------
9600                         -- {{{ Begin: Update of revision and revision_date
9601                         --------------------------------------------------
9602                         -- BA: bug3202534
9603                         begin
9604 
9605                             l_chg_rtg_rev  := 0;
9606                             l_chg_bom_rev  := 0;
9607                             l_chg_rev_flag := 0;
9608 
9609 l_stmt_num:= 640;
9610                             if (v_wlji_rtg_revision(v_idx) is not null and
9611                                 v_wlji_rtg_revision(v_idx) <> l_cur_rtg_revision) or
9612                                (v_wlji_rtg_rev_date(v_idx) is not null and
9613                                 v_wlji_rtg_rev_date(v_idx) <> l_cur_rtg_rev_date)
9614                             then
9615                                 l_chg_rtg_rev := 1;
9616                             end if;
9617 
9618                             if (v_wlji_bom_revision(v_idx) is not null and
9619                                 v_wlji_bom_revision(v_idx) <> l_cur_bom_revision) or
9620                                (v_wlji_bom_rev_date(v_idx) is not null and
9621                                 v_wlji_bom_rev_date(v_idx) <> l_cur_bom_rev_date)
9622                             then
9623                                 l_chg_bom_rev := 1;
9624                             end if;
9625 
9626                             -- {{ only allow change for unreleased job, l_cur_status_type = 1
9627                             if (l_chg_bom_rev = 1 or l_chg_rtg_rev = 1) and
9628                                l_cur_status_type = 1
9629                             then
9630                                 l_chg_rev_flag := 1;
9631 l_stmt_num:= 650;
9632                                 if v_wlji_fusd(v_idx) is null then
9633                                     v_wlji_fusd(v_idx) := l_cur_start_date;
9634                                 end if;
9635 
9636                                 if v_wlji_fusd(v_idx) > SYSDATE then
9637                                     l_rev_date := v_wlji_fusd(v_idx);
9638                                 else
9639                                     l_rev_date := SYSDATE;
9640                                 end if;
9641 
9642                                 if l_chg_bom_rev = 1 then
9643                                     -- validate bom rev/rev_date
9644                                     if l_cur_job_type = 1 then -- standard job
9645 l_stmt_num:= 660.1;
9646                                         wip_revisions.bom_revision (
9647                                                 v_wlji_org_id(v_idx),
9648                                                 v_wlji_item_id(v_idx),
9649                                                 v_wlji_bom_revision(v_idx),
9650                                                 v_wlji_bom_rev_date(v_idx),
9651                                                 l_rev_date);
9652                                     else
9653                                         if(v_wlji_bom_ref_id(v_idx) IS NOT NULL) then
9654 l_stmt_num:= 660.2;
9655                                             wip_revisions.bom_revision (
9656                                                     v_wlji_org_id(v_idx),
9657                                                     v_wlji_bom_ref_id(v_idx),
9658                                                     v_wlji_bom_revision(v_idx),
9659                                                     v_wlji_bom_rev_date(v_idx),
9660                                                     l_rev_date);
9661                                         end if;
9662                                     end if;
9663                                 end if;
9664 
9665                                 if l_chg_rtg_rev = 1 then
9666                                     -- validate rtg rev/rev_date
9667                                     if l_cur_job_type = 1 then -- standard job
9668 l_stmt_num:= 670.1;
9669                                         wip_revisions.routing_revision (
9670                                                 v_wlji_org_id(v_idx),
9671                                                 v_wlji_item_id(v_idx),
9672                                                 v_wlji_rtg_revision(v_idx),
9673                                                 v_wlji_rtg_rev_date(v_idx),
9674                                                 l_rev_date);
9675                                     else
9676                                         if(v_wlji_rtg_ref_id(v_idx) IS NOT NULL) then
9677 l_stmt_num:= 670.2;
9678                                             wip_revisions.routing_revision (
9679                                                     v_wlji_org_id(v_idx),
9680                                                     v_wlji_rtg_ref_id(v_idx),
9681                                                     v_wlji_rtg_revision(v_idx),
9682                                                     v_wlji_rtg_rev_date(v_idx),
9683                                                     l_rev_date);
9684                                         end if;
9685                                     end if;
9686                                 end if;
9687 
9688                                 if l_chg_rtg_rev = 1 and l_chg_bom_rev = 0 then
9689                                     v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date;
9690                                     v_wlji_bom_revision(v_idx) := l_cur_bom_revision;
9691                                 end if;
9692 
9693                                 if l_chg_rtg_rev = 0 and l_chg_bom_rev = 1 then
9694                                     v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date;
9695                                     v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision;
9696                                 end if;
9697 
9698                             end if; -- }} old_status_type = 1
9699 
9700                             -- user can update the revision and revision date of a
9701                             -- job only if the job is unreleased.
9702                             if (l_chg_rtg_rev = 1 or l_chg_bom_rev = 1) and
9703                                 l_cur_status_type <> 1
9704                             then
9705                                 fnd_file.put_line(fnd_file.log,
9706                                     'Ignoring the following fields if provided by user: ' ||
9707                                     'Routing_Revision, Routing_Revision_Date, '||
9708                                     'Bom_Revision, Bom_Revision_Date, ' );
9709                             end if;
9710 
9711                             if l_chg_rev_flag = 0 then
9712                                 v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date;
9713                                 v_wlji_bom_revision(v_idx) := l_cur_bom_revision;
9714                                 v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date;
9715                                 v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision;
9716                             else
9717                                 if lbji_debug = 'Y' then
9718                                     fnd_file.put_line(fnd_file.log,
9719                                         'Update BOM/Routing revision or revision date ...');
9720                                 end if;
9721                             end if;
9722                         end;
9723                         -- EA: bug3202534
9724                         ------------------------------------------------
9725                         -- }}} End: Update of revision and revision_date
9726                         ------------------------------------------------
9727 
9728 
9729 l_stmt_num := 700;
9730                         -- Validation before updating other columns
9731                         -- if the user wants to update only the reference, the wo/wro/wor/woy
9732                         -- updates are not needed
9733                         if (l_cur_job_type = 3) AND
9734                            (
9735                             ((v_wlji_firm_planned_flag(v_idx) is null) or
9736                              (v_wlji_firm_planned_flag(v_idx) is not null and
9737                               v_wlji_firm_planned_flag(v_idx)=l_cur_firm_planned_flag))
9738                             and
9739                             ((v_wlji_status_type(v_idx) is null) or
9740                              (v_wlji_status_type(v_idx) is not null and
9741                               v_wlji_status_type(v_idx)=l_cur_status_type))
9742                             and
9743                             ((v_wlji_date_released(v_idx) is null) or
9744                              (v_wlji_date_released(v_idx) is not null and
9745                               v_wlji_date_released(v_idx)=l_cur_date_released))
9746                             and
9747                             ((v_wlji_start_quantity(v_idx) is null) or
9748                              (v_wlji_start_quantity(v_idx) is not null and
9749                               v_wlji_start_quantity(v_idx)=l_cur_start_qty))
9750                             and
9751                             ((v_wlji_net_quantity(v_idx) is null) or
9752                              (v_wlji_net_quantity(v_idx) is not null and
9753                               v_wlji_net_quantity(v_idx)=l_cur_net_qty))
9754                             and
9755                             ((v_wlji_lucd(v_idx) is null) or
9756                              (v_wlji_lucd(v_idx) is not null and
9757                               v_wlji_lucd(v_idx)=l_cur_schd_compl_date))
9758                             and
9759                             ((v_wlji_fusd(v_idx) is null) or
9760                              (v_wlji_fusd(v_idx) is not null and
9761                               v_wlji_fusd(v_idx)=l_cur_schd_start_date))
9762                             and
9763                             ((v_wlji_coproducts_supply(v_idx) is null) or
9764                              (v_wlji_coproducts_supply(v_idx) is not null and
9765                               v_wlji_coproducts_supply(v_idx)=l_cur_coprod_supply))
9766                            )
9767                         then
9768                             l_skip_update_wo:= 1;
9769                         end if;
9770                         if lbji_debug = 'Y' then
9771                             if l_skip_update_wo = 1 then
9772                                 fnd_file.put_line(fnd_file.log,
9773                                     'Planning to skip update of wo, wor, wro');
9774                             else
9775                                 fnd_file.put_line(fnd_file.log,
9776                                     'Planning to update wo, wor, wro');
9777                             end if;
9778                         end if;
9779 
9780 
9781 l_stmt_num := 710;
9782                         -- if the old firm_planned flag was 1, and the user populated null
9783                         -- while updating the job, NO changes in quantity and date allowed.
9784                         -- If the user changed the flag, this change is considered BEFORE
9785                         -- making a decision whether to allow update of qnty/date or not.
9786                         if v_wlji_firm_planned_flag(v_idx) is NULL then
9787                             v_wlji_firm_planned_flag(v_idx) := l_cur_firm_planned_flag;
9788                         end if;
9789 
9790                         -- for non-std jobs, the firm flag is always 2. User cannot update it to 1.
9791                         if l_cur_job_type = 3 then
9792                             if v_wlji_firm_planned_flag(v_idx) is not null and
9793                                v_wlji_firm_planned_flag(v_idx) <> 2 then
9794                                 l_aux_mesg := '';
9795                                 handle_invalid_field('Firm_Planned_Flag', l_aux_mesg, l_stmt_num);
9796                                 l_error_code := -1;
9797                                 GOTO skip_resched_validations;
9798                             end if;
9799                         end if;
9800 
9801 l_stmt_num := 720;
9802                         -- {{ Validate status
9803                         IF v_wlji_status_type(v_idx) is null THEN
9804                             v_wlji_status_type(v_idx) := l_cur_status_type;
9805                             -- update of released date only is not allowed
9806                             v_wlji_date_released(v_idx) := l_cur_date_released;
9807                         ELSIF v_wlji_status_type(v_idx) = l_cur_status_type THEN
9808                             -- update of released date only is not allowed
9809                             v_wlji_date_released(v_idx) := l_cur_date_released;
9810                         END IF;
9811 
9812                         -- status type should be one of RELEASED, HOLD, CANCELLED, UNRELEASED
9813                         if v_wlji_status_type(v_idx) not in (1,3,6,7) then
9814                             l_aux_mesg := '';
9815                             handle_invalid_field('Status Type', l_aux_mesg, l_stmt_num);
9816                             l_error_code := -1;
9817                             GOTO skip_resched_validations;
9818                         else  -- {{ valid status type
9819                             if l_cur_status_type = 7   then     -- cancelled
9820                                 l_error_code := -1;
9821                                 handle_errmsg_name(
9822                                     'WSM','WSM_RESCHEDULE_CANCEL_JOB',l_stmt_num);
9823                                 GOTO skip_resched_validations;
9824                             end if;
9825 l_stmt_num := 720.1;
9826                             -- if the job is being released from an unreleased state...
9827                             if l_cur_date_released is null and
9828                                v_wlji_status_type(v_idx) = 3
9829                             then
9830                                 if (v_wlji_date_released(v_idx) is null ) then
9831                                     v_wlji_date_released(v_idx) := sysdate;
9832                                 end if;
9833                                 if (v_wlji_date_released(v_idx) > sysdate ) then
9834                                     l_error_code := -1;
9835                                     handle_errmsg_name(
9836                                         'WIP','WIP_INVALID_RELEASE_DATE',l_stmt_num);
9837                                     GOTO skip_resched_validations;
9838                                 end if;
9839 
9840                                 -- BEGIN: BUG3126650
9841                                 l_temp_num := WSMPUTIL.GET_INV_ACCT_PERIOD(
9842                                                 x_err_code         => l_error_code,
9843                                                 x_err_msg          => l_error_msg,
9844                                                 p_organization_id  => v_wlji_org_id(v_idx),
9845                                                 p_date             => trunc(nvl(v_wlji_date_released(v_idx),sysdate)));
9846                                 IF (l_error_code <> 0) THEN
9847                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
9848                                     GOTO skip_resched_validations;
9849                                 END IF;
9850                                 -- END: BUG3126650
9851 
9852                             elsif l_cur_date_released is not null and
9853                                 v_wlji_status_type(v_idx) = 1
9854                             then
9855                                 v_wlji_date_released(v_idx) := null;
9856                             else
9857                                 -- ignore release date populated by user otherwise
9858                                 v_wlji_date_released(v_idx) := l_cur_date_released;
9859                             end if;
9860 
9861                             if ((l_cur_status_type = 3 and v_wlji_status_type(v_idx) = 1) OR
9862                                 (l_cur_status_type = 6 and v_wlji_status_type(v_idx) = 1))
9863                             then
9864                                 if (g_charges_exist_all) then
9865                                     l_error_code := -1;
9866                                     handle_errmsg_name(
9867                                         'WIP','WIP_UNRLS_JOB/SCHED',l_stmt_num);
9868                                     GOTO skip_resched_validations;
9869 
9870                                 else
9871 l_stmt_num := 720.2;
9872                                     -- osp begin
9873                                     if wip_osp.po_req_exists (
9874                                             v_wlji_wip_entity_id(v_idx),
9875                                             null,
9876                                             v_wlji_org_id(v_idx),
9877                                             null, 5)
9878                                     then
9879                                         fnd_message.set_name('WSM', 'WSM_JOB_PURCHASE_REQ');
9880                                         l_error_msg := fnd_message.get;
9881                                         l_warning_count := l_warning_count + 1;
9882                                         handle_warning(
9883                                             p_err_msg    => l_error_msg,
9884                                             p_header_id  => v_wlji_header_id(v_idx),
9885                                             p_request_id => v_wlji_request_id(v_idx),
9886                                             p_program_id => v_wlji_program_id(v_idx),
9887                                             p_program_application_id => v_wlji_program_appl_id(v_idx));
9888                                     end if;
9889                                     -- osp end
9890                                 end if;
9891                             end if;
9892 l_stmt_num := 720.3;
9893                             if (l_cur_status_type IN (1,6)) and
9894                                (v_wlji_status_type(v_idx) = 3) and
9895                                 wsmputil.WSM_ESA_ENABLED(
9896                                             p_wip_entity_id => v_wlji_wip_entity_id(v_idx),
9897                                             err_code        => l_error_code,
9898                                             err_msg         => l_error_msg,
9899                                             p_org_id        => '',
9900                                             p_job_type      => '') = 1
9901                             then
9902 
9903                                 if v_wlji_class_code(v_idx) is null then
9904                                     l_temp_class := l_cur_class_code;
9905                                 else
9906                                     l_temp_class := v_wlji_class_code(v_idx);
9907                                 end if;
9908 l_stmt_num := 720.4;
9909                                 select est_scrap_account,
9910                                        est_scrap_var_account
9911                                 into   l_est_scrap_account,
9912                                        l_est_scrap_var_account
9913                                 from   wip_accounting_classes
9914                                 where  class_code = l_temp_class
9915                                 and    organization_id = v_wlji_org_id(v_idx);
9916 
9917                                 if l_est_scrap_account is null or
9918                                    l_est_scrap_var_account is null
9919                                 then
9920                                     fnd_message.set_name('WSM','WSM_NO_WAC_SCRAP_ACC');
9921                                     fnd_message.set_token('CC', l_temp_class);
9922                                     l_error_msg := fnd_message.get;
9923                                     l_error_code := -1;
9924                                     handle_error(l_error_code, l_error_msg,  l_stmt_num);
9925                                     GOTO skip_resched_validations;
9926                                 end if;
9927                                     if l_est_scrap_account is null or
9928                                     l_est_scrap_var_account is null
9929                                 then
9930                                     fnd_message.set_name('WSM','WSM_NO_WAC_SCRAP_ACC');
9931                                     fnd_message.set_token('CC', l_temp_class);
9932                                     l_error_msg := fnd_message.get;
9933                                     l_error_code := -1;
9934                                     handle_error(l_error_code, l_error_msg,  l_stmt_num);
9935                                     GOTO skip_resched_validations;
9936                                 end if;
9937                             end if;
9938                         end if;  -- }} valid status type
9939                         if lbji_debug = 'Y' then
9940                             fnd_file.put_line(fnd_file.log,
9941                                     'Validate reschedule Job_Status .. OK');
9942                         end if;
9943                         -- }} Validate status
9944 
9945 l_stmt_num := 730;
9946                         -- {{ Validate quantity
9947                         IF v_wlji_start_quantity(v_idx) IS NULL THEN
9948                             v_wlji_start_quantity(v_idx) := l_cur_start_qty;
9949                         ELSIF  v_wlji_start_quantity(v_idx) = l_cur_start_qty THEN
9950                             NULL;
9951                         -- Bug 5661656: Allow Job to be rescheduled(Update Qty/Dates) when firm_planned_flag is also updated.
9952                         -- ELSIF v_wlji_firm_planned_flag(v_idx) = 1 THEN
9953                         ELSIF v_wlji_firm_planned_flag(v_idx) = 1 AND NVL(l_cur_firm_planned_flag, 2) = 1 THEN
9954                             l_error_code := -1;
9955                             handle_errmsg_name('WSM', 'WSM_JOB_FIRM', l_stmt_num);
9956                             GOTO skip_resched_validations;
9957                         ELSE
9958                             begin
9959                                 if (l_cur_status_type IN (3,6) AND
9960                                     v_wlji_status_type(v_idx) IN (1,3,6)) then
9961                                     if (g_charges_exist_all) then
9962                                         raise e_invalid_qty;
9963                                     end if;
9964                                 end if;
9965                             exception
9966                                 when e_invalid_qty then
9967                                     l_error_code := -1;
9968                                     handle_errmsg_name('WSM', 'WSM_QNTY_NOCHANGE', l_stmt_num);
9969                                     GOTO skip_resched_validations;
9970                             end;
9971                         END IF;
9972                         if lbji_debug = 'Y' then
9973                             fnd_file.put_line(fnd_file.log,
9974                                     'Validate reschedule Start_Quantity .. OK');
9975                         end if;
9976                         -- }} Validate quantity
9977 
9978 l_stmt_num := 740;
9979                         -- {{ osp begin
9980                         -- create requisitions/additional reuisitions under following conditions:
9981                         -- 1. user updates only status from unreleased to released, quantity unchanged
9982                         -- 2. user updates only quantity (increases) for a released job, status is unchanged
9983                         -- 3. user updates staus to released, and increases quantity.
9984 
9985                         if WSMPLCVA.v_org(v_wlji_org_id(v_idx)).PO_CREATION_TIME
9986                            <> WIP_CONSTANTS.MANUAL_CREATION
9987                         then
9988                             if (v_wlji_status_type(v_idx) = 3 and l_cur_status_type = 1) then
9989                                 if wsmputil.check_osp_operation(
9990                                         v_wlji_wip_entity_id(v_idx),
9991                                         l_osp_op_seq_num,
9992                                         v_wlji_org_id(v_idx))
9993                                 then
9994                                     g_num_of_osp_exists := g_num_of_osp_exists + 1;
9995                                     wip_osp.create_requisition(
9996                                         p_wip_entity_id          => v_wlji_wip_entity_id(v_idx),
9997                                         p_organization_id        => v_wlji_org_id(v_idx),
9998                                         p_repetitive_schedule_id => null,
9999                                         p_operation_seq_num      => l_osp_op_seq_num,
10000                                         p_resource_seq_num       => null,
10001                                         p_run_reqimport          => wip_constants.no);
10002                                 end if; -- check_osp_operation
10003                             end if;
10004 l_stmt_num := 740.1;
10005                             if (v_wlji_start_quantity(v_idx) > l_cur_start_qty) AND
10006                                v_wlji_status_type(v_idx) = 3
10007                             then
10008                                 wip_osp.create_additional_req(
10009                                     p_wip_entity_id          => v_wlji_wip_entity_id(v_idx),
10010                                     p_organization_id        => v_wlji_org_id(v_idx),
10011                                     p_repetitive_schedule_id => null,
10012                                     p_added_quantity         => (v_wlji_start_quantity(v_idx) - l_cur_start_qty),
10013                                     p_op_seq                 => null);
10014                             end if;
10015                         end if; -- WIP_CONSTANTS.MANUAL_CREATION
10016                         -- }} osp end
10017 
10018 l_stmt_num := 750;
10019                         -- {{ Validate net-quantity
10020                         if v_wlji_net_quantity(v_idx) < 0 or
10021                                 v_wlji_net_quantity(v_idx) > v_wlji_start_quantity(v_idx) then
10022                                 l_aux_mesg := '';
10023                                 handle_invalid_field('Net Quantity', l_aux_mesg, l_stmt_num);
10024                                 l_error_code := -1;
10025                                 GOTO skip_resched_validations;
10026                         end if;
10027 
10028                         if l_cur_job_type = 1 then
10029                             if (v_wlji_net_quantity(v_idx) is not null) and
10030                                (v_wlji_net_quantity(v_idx) <> l_cur_start_qty) and
10031                                -- Bug 5661656: Allow Job to be rescheduled(Update Qty/Dates) when firm_planned_flag is also updated.
10032                                -- (v_wlji_firm_planned_flag(v_idx) = 1) then
10033                                (v_wlji_firm_planned_flag(v_idx) = 1 AND NVL(l_cur_firm_planned_flag, 2) = 1) then
10034                                 l_error_code := -1;
10035                                 handle_errmsg_name('WSM', 'WSM_JOB_FIRM', l_stmt_num);
10036                                 GOTO skip_resched_validations;
10037                             end if;
10038 
10039                             if (v_wlji_net_quantity(v_idx) is not null) and
10040                                (v_wlji_start_quantity(v_idx) is null) and
10041                                (v_wlji_net_quantity(v_idx) > l_cur_start_qty) then
10042                                 l_aux_mesg := '';
10043                                 handle_invalid_field('Net Quantity', l_aux_mesg, l_stmt_num);
10044                                 l_error_code := -1;
10045                                 GOTO skip_resched_validations;
10046                             end if;
10047 l_stmt_num := 750.1;
10048                             if v_wlji_net_quantity(v_idx) is null then
10049                                 begin
10050                                     select decode(wdj.primary_item_id, null, 0,
10051                                                   decode(wdj.net_quantity,
10052                                                          wdj.start_quantity,
10053                                                          v_wlji_start_quantity(v_idx),
10054                                                          least(wdj.net_quantity,
10055                                                                nvl(v_wlji_start_quantity(v_idx),
10056                                                                    wdj.net_quantity))))
10057                                     into   v_wlji_net_quantity(v_idx)
10058                                     from   wip_discrete_jobs wdj
10059                                     where  wdj.wip_entity_id = v_wlji_wip_entity_id(v_idx)
10060                                     and    wdj.organization_id = v_wlji_org_id(v_idx);
10061                                 exception
10062                                     when others then
10063                                         l_error_code := SQLCODE;
10064                                         l_error_msg  :=
10065                                             'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
10066                                            ||SUBSTR(SQLERRM, 1,1000);
10067                                         handle_error(l_error_code, l_error_msg, l_stmt_num);
10068                                         GOTO skip_resched_validations;
10069                                 end;
10070                             end if;
10071                         end if; -- job_type
10072                         if lbji_debug = 'Y' then
10073                             fnd_file.put_line(fnd_file.log,
10074                                     'Validate reschedule Net_Quantity .. OK');
10075                         end if;
10076                         -- }} Validate net-quantity
10077 
10078 l_stmt_num := 750.2;
10079             -- ST : Serial Support Project ----------------------
10080             --- Validate the start job qty and net qty ----------
10081             --- For serial controlled item have to be an integer...
10082             DECLARE
10083                 l_serial_ctrl_code NUMBER;
10084 
10085             BEGIN
10086                 select  msi.serial_number_control_code
10087                                 into    l_serial_ctrl_code
10088                                 from    mtl_system_items_kfv msi,wip_discrete_jobs wdj
10089                                 where   msi.inventory_item_id = wdj.primary_item_id
10090                                 and     msi.organization_id   = v_wlji_org_id(v_idx)
10091                 and     wdj.wip_entity_id = v_wlji_wip_entity_id(v_idx)
10092                 and     wdj.organization_id = v_wlji_org_id(v_idx);
10093 
10094                 IF l_serial_ctrl_code = 2 THEN  -- Predefined serial control...
10095 
10096                     IF (v_wlji_start_quantity(v_idx) IS NOT NULL) AND
10097                        (floor(v_wlji_start_quantity(v_idx)) <> v_wlji_start_quantity(v_idx))
10098                     THEN
10099                         -- error out...
10100                         -- Start qty cannot be non-integer...
10101                          fnd_message.set_name('WSM','WSM_INVALID_JOB_TXN_QTY');
10102                          l_aux_mesg := fnd_message.get;
10103                          handle_invalid_field('START QUANTITY', l_aux_mesg, l_stmt_num);
10104                          l_error_code := -1;
10105                          l_error_count := l_error_count + 1;
10106                          GOTO skip_resched_validations;
10107                     END IF;
10108 
10109                     IF (v_wlji_net_quantity(v_idx) IS NOT NULL) and
10110                        floor(v_wlji_net_quantity(v_idx)) <> v_wlji_net_quantity(v_idx)
10111                     THEN
10112                         -- error out...
10113                         -- Start qty cannot be non-integer...
10114                          fnd_message.set_name('WSM','WSM_INVALID_JOB_TXN_QTY');
10115                          l_aux_mesg := fnd_message.get;
10116                          handle_invalid_field('NET QUANTITY', l_aux_mesg, l_stmt_num);
10117                          l_error_code := -1;
10118                          l_error_count := l_error_count + 1;
10119                          GOTO skip_resched_validations;
10120                     END IF;
10121                 END IF;
10122                END;
10123                -- ST : Serial Support Project ----------------------
10124                --- Validate the start job qty and net qty ----------
10125 
10126 l_stmt_num := 760;
10127                         -- {{ Validate dates (for update)
10128                         -- Bug 5661656: Allow Job to be rescheduled(Update Qty/Dates) when firm_planned_flag is also updated.
10129                         -- if ((v_wlji_firm_planned_flag(v_idx) = 1) AND
10130                         if ((v_wlji_firm_planned_flag(v_idx) = 1 AND NVL(l_cur_firm_planned_flag, 2) = 1) AND
10131                             (((v_wlji_fusd(v_idx) IS NOT NULL) AND
10132                               (v_wlji_fusd(v_idx) <> l_cur_start_date))
10133                              OR
10134                              ((v_wlji_lucd(v_idx) IS NOT NULL) AND
10135                               (v_wlji_lucd(v_idx) <> l_cur_complete_date))))
10136                         then
10137                             l_error_code := -1;
10138                              handle_errmsg_name('WSM', 'WSM_JOB_FIRM', l_stmt_num);
10139                             GOTO skip_resched_validations;
10140                         end if;
10141 
10142                         IF ( ((v_wlji_fusd(v_idx) IS NULL) AND
10143                               (v_wlji_lucd(v_idx) IS NULL))
10144                              OR
10145                              ((v_wlji_fusd(v_idx) IS NOT NULL) AND
10146                               (v_wlji_lucd(v_idx) IS NOT NULL) AND
10147                               (v_wlji_scheduling_method(v_idx) <> 1)) )
10148                         THEN
10149                             v_wlji_scheduling_method(v_idx) := 3;
10150                         END IF;
10151 
10152                         l_error_msg := NULL;
10153                         IF v_wlji_scheduling_method(v_idx) = 3 THEN
10154 
10155                             if (v_wlji_fusd(v_idx) is null) then
10156                                 v_wlji_fusd(v_idx) := l_cur_start_date;
10157                             end if;
10158                             if (v_wlji_lucd(v_idx) is null)  then
10159                                 v_wlji_lucd(v_idx) := l_cur_complete_date;
10160                             end if;
10161 
10162                             if ((v_wlji_fusd(v_idx) <> l_cur_start_date) and
10163                                 g_charges_exist_shopfloor)
10164                             then
10165                                 -- bugfix 3284761, in option c, we will ignore v_wlji_fusd
10166                                 -- if it is different form l_cur_start_date and charge exists
10167                                 v_wlji_fusd(v_idx) := l_cur_start_date;
10168                             end if;
10169 
10170                             if  (NOT(g_charges_exist_shopfloor) and -- added bug 3423195
10171                                  v_wlji_fusd(v_idx) > v_wlji_lucd(v_idx)) then
10172                                 l_aux_mesg := '';
10173                                 handle_errmsg_name('WSM', 'WSM_FUSD_GT_LUCD', l_stmt_num);
10174                                 l_error_code := -1;
10175                                 GOTO skip_resched_validations;
10176                             end if;
10177 
10178                         -- BA: BUG 3479666
10179                         ELSIF v_wlji_scheduling_method(v_idx) = 1 THEN
10180                             if(v_wlji_fusd(v_idx) is not null) then
10181                                 l_scheduling_mode := WIP_CONSTANTS.FORWARDS;
10182                             else
10183                                 l_scheduling_mode := WIP_CONSTANTS.BACKWARDS;
10184                             end if;
10185                         -- EA: BUG 3479666
10186 
10187                         ELSIF v_wlji_scheduling_method(v_idx) = 2 THEN
10188                             if (v_wlji_fusd(v_idx) is not null) then
10189 l_stmt_num := 760.1;
10190                                 IF ((v_wlji_fusd(v_idx) <> l_cur_start_date) and g_charges_exist_shopfloor)
10191                                 THEN
10192                                     l_aux_mesg := '';
10193                                     handle_invalid_field('Start Date', l_aux_mesg, l_stmt_num);
10194                                     l_error_code := -1;
10195                                     GOTO skip_resched_validations;
10196                                 ELSE
10197 l_stmt_num := 760.2;
10198                                     v_wlji_lucd(v_idx) := wsmputil.GET_SCHEDULED_DATE(
10199                                             v_wlji_org_id(v_idx),
10200                                             v_wlji_item_id(v_idx),
10201                                             'F',
10202                                             v_wlji_fusd(v_idx),
10203                                             l_error_code,
10204                                             l_error_msg,
10205                                             v_wlji_start_quantity(v_idx));  --Fixed bug # 2313574
10206                                 END IF;
10207                             elsif (v_wlji_lucd(v_idx) is not null) then
10208 l_stmt_num := 760.3;
10209                                 v_wlji_fusd(v_idx) := wsmputil.GET_SCHEDULED_DATE (
10210                                         v_wlji_org_id(v_idx),
10211                                         v_wlji_item_id(v_idx),
10212                                         'B',
10213                                         v_wlji_lucd(v_idx),
10214                                         l_error_code,
10215                                         l_error_msg,
10216                                         v_wlji_start_quantity(v_idx));  --Fixed bug # 2313574
10217                             end if;
10218 
10219                         END IF;
10220 
10221                         if l_error_code <> 0 OR l_error_msg IS NOT NULL then
10222                             handle_error( l_error_code, l_error_msg, l_stmt_num);
10223                             l_error_code := -1;
10224                             GOTO skip_resched_validations;
10225                         end if;
10226                         if lbji_debug = 'Y' then
10227                             fnd_file.put_line(fnd_file.log,
10228                                     'Validate reschedule Dates .. OK');
10229                         end if;
10230                         -- }} Validate dates
10231 
10232 
10233 l_stmt_num := 770;
10234                         -- {{ validate coproducts
10235                         if ((v_wlji_coproducts_supply(v_idx) IS NOT NULL) and
10236                            (v_wlji_coproducts_supply(v_idx) NOT IN (1,2))) then
10237                             l_aux_mesg := '';
10238                             handle_invalid_field('Coproducts Supply', l_aux_mesg, l_stmt_num);
10239                             l_error_code := -1;
10240                             GOTO skip_resched_validations;
10241                         end if;
10242                         if lbji_debug = 'Y' then
10243                             fnd_file.put_line(fnd_file.log,
10244                                     'Validate reschedule Coproducts_Supply .. OK');
10245                         end if;
10246                         -- }} validate coproducts
10247 
10248 << skip_resched_validations >>
10249 
10250                         if l_error_code <> 0 then
10251                             l_error_count := l_error_count + 1;
10252                             GOTO abort_this_job;
10253                         end if;
10254 
10255                         if l_cur_job_type = 3 then
10256                             if l_cur_status_type = 3 then
10257                                 l_common_rtg_seq_id := l_cur_com_rtg_seq_id;
10258                                 l_common_bom_seq_id := l_cur_com_bom_seq_id;
10259                             end if;
10260                             if l_cur_status_type = 1 then
10261                                 if l_chg_bom_ref = 0 and l_chg_rtg_ref = 0 then
10262                                     l_common_rtg_seq_id := l_cur_com_rtg_seq_id;
10263                                     l_common_bom_seq_id := l_cur_com_bom_seq_id;
10264                                 end if;
10265                             end if;
10266                         end if; -- job type=3
10267 
10268                     end if; --load_type 6
10269 
10270 -- ===========================================================================================
10271 -- END: Reschedule (Update) Job Validations  }}}
10272 -- ===========================================================================================
10273 
10274 
10275 
10276 -- ==============================================================================================
10277 -- BEGIN: WRITING INTO BASE TABLES {{{
10278 -- ==============================================================================================
10279 
10280                     -- ---------------------------------------------------
10281                     -- {{{ Begin: Write WE, WDJ etc. for job creation only
10282                     -- ---------------------------------------------------
10283                     if v_wlji_load_type(v_idx) = 5 then
10284 l_stmt_num := 800;
10285             g_skip_wlbj := 1;   --OPTII-PERF
10286                         build_job_header_info(
10287                             p_common_routing_sequence_id=> l_common_rtg_seq_id,
10288                             p_common_bill_sequence_id   => l_common_bom_seq_id,
10289                             p_status_type               => v_wlji_status_type(v_idx),
10290                             p_class_code                => v_wlji_class_code(v_idx),
10291                             p_org_id                    => v_wlji_org_id(v_idx),
10292                             p_wip_entity_id             => v_wlji_wip_entity_id(v_idx),
10293                             p_last_updt_date            => v_wlji_last_updt_date(v_idx),
10294                             p_last_updt_by              => v_wlji_last_updt_by(v_idx),
10295                             p_creation_date             => v_wlji_creation_date(v_idx),
10296                             p_created_by                => v_wlji_created_by(v_idx),
10297                             p_last_updt_login           => v_wlji_last_updt_login(v_idx),
10298                             p_request_id                => v_wlji_request_id(v_idx),
10299                             p_program_appl_id           => v_wlji_program_appl_id(v_idx),
10300                             p_program_id                => v_wlji_program_id(v_idx),
10301                             p_prog_updt_date            => v_wlji_prog_updt_date(v_idx),
10302                             p_source_line_id            => v_wlji_src_line_id(v_idx),
10303                             p_source_code               => v_wlji_source_code(v_idx),
10304                             p_description               => v_wlji_description(v_idx),
10305                             p_item                      => v_wlji_item_id(v_idx),
10306                             p_job_type                  => v_wlji_job_type(v_idx),
10307                             p_bom_reference_id          => v_wlji_bom_ref_id(v_idx),
10308                             p_routing_reference_id      => v_wlji_rtg_ref_id(v_idx),
10309                             p_firm_planned_flag         => v_wlji_firm_planned_flag(v_idx),
10310                             p_wip_supply_type           => v_wlji_wip_supply_type(v_idx),
10311                             -- it can be null if we need to call infinite scheduler, but wip
10312                             -- does not allow inserting null, insert sysdate temporarily
10313                             p_job_scheduled_start_date  => nvl(v_wlji_fusd(v_idx), sysdate),
10314                             p_job_scheduled_compl_date  => nvl(v_wlji_lucd(v_idx), sysdate),
10315                             p_start_quantity            => v_wlji_start_quantity(v_idx),
10316                             p_net_quantity              => v_wlji_net_quantity(v_idx),
10317                             p_coproducts_supply         => v_wlji_coproducts_supply(v_idx),
10318                             p_bom_revision              => v_wlji_bom_revision(v_idx),
10319                             p_routing_revision          => v_wlji_rtg_revision(v_idx),
10320                             p_bom_revision_date         => v_wlji_bom_rev_date(v_idx),
10321                             p_routing_revision_date     => v_wlji_rtg_rev_date(v_idx),
10322                             p_lot_number                => v_wlji_lot_number(v_idx),
10323                             p_alt_bom_designator        => v_wlji_alt_bom(v_idx),
10324                             p_alt_routing_designator    => v_wlji_alt_rtg(v_idx),
10325                             p_priority                  => v_wlji_priority(v_idx),
10326                             p_due_date                  => v_wlji_due_date(v_idx),
10327                             p_attribute_category        => v_wlji_attribute_category(v_idx),
10328                             p_attribute1                => v_wlji_attribute1(v_idx),
10329                             p_attribute2                => v_wlji_attribute2(v_idx),
10330                             p_attribute3                => v_wlji_attribute3(v_idx),
10331                             p_attribute4                => v_wlji_attribute4(v_idx),
10332                             p_attribute5                => v_wlji_attribute5(v_idx),
10333                             p_attribute6                => v_wlji_attribute6(v_idx),
10334                             p_attribute7                => v_wlji_attribute7(v_idx),
10335                             p_attribute8                => v_wlji_attribute8(v_idx),
10336                             p_attribute9                => v_wlji_attribute9(v_idx),
10337                             p_attribute10               => v_wlji_attribute10(v_idx),
10338                             p_attribute11               => v_wlji_attribute11(v_idx),
10339                             p_attribute12               => v_wlji_attribute12(v_idx),
10340                             p_attribute13               => v_wlji_attribute13(v_idx),
10341                             p_attribute14               => v_wlji_attribute14(v_idx),
10342                             p_attribute15               => v_wlji_attribute15(v_idx),
10343                             p_job_name                  => v_wlji_job_name(v_idx),
10344                             p_completion_subinventory   => v_wlji_compl_subinv(v_idx),
10345                             p_completion_locator_id     => v_wlji_compl_loc_id(v_idx),
10346                             p_demand_class              => v_wlji_demand_class(v_idx),
10347                             p_project_id                => v_wlji_project_id(v_idx),
10348                             p_task_id                   => v_wlji_task_id(v_idx),
10349                             p_schedule_group_id         => v_wlji_schedule_group_id(v_idx),
10350                             p_build_sequence            => v_wlji_build_sequence(v_idx),
10351                             p_line_id                   => v_wlji_line_id(v_idx),
10352                             p_kanban_card_id            => v_wlji_kanban_card_id(v_idx),
10353                             p_overcompl_tol_type        => v_wlji_overcompl_tol_type(v_idx),
10354                             p_overcompl_tol_value       => v_wlji_overcompl_tol_value(v_idx),
10355                             p_end_item_unit_number      => v_wlji_end_item_unit_number(v_idx),
10356                             p_src_client_server         => 0,
10357                             p_po_creation_time          => WSMPLCVA.v_org(v_wlji_org_id(v_idx)).PO_CREATION_TIME,
10358                             p_date_released             => v_wlji_date_released(v_idx),
10359                             p_error_code                => l_error_code,
10360                             p_error_msg                 => l_error_msg);
10361                         g_skip_wlbj := NULL; --OPTII-PERF
10362                         if l_error_code <> 0 then
10363                             handle_error(l_error_code, l_error_msg,  l_stmt_num);
10364                             l_error_count := l_error_count + 1;
10365                             GOTO abort_this_job;
10366                         end if;
10367 
10368 
10369                     end if; -- load_type 5
10370                     -- ---------------------------------------------------
10371                     -- }}}   End: Write WE, WDJ etc. for job creation only
10372                     -- ---------------------------------------------------
10373 
10374                     -- ----------------------------------------------------------------
10375                     -- {{{ Begin: Write WO's and WCO's, job creation and alt/ref change
10376                     -- ----------------------------------------------------------------
10377 
10378                     -- in such cases, WCO records will be created first, and WO records
10379                     -- will be created by looking at WCO records
10380 
10381                     if v_wlji_load_type(v_idx) = 5 or
10382                        l_chg_alt_flag = 1 or
10383                        l_chg_ref_flag = 1 or
10384                        l_chg_rev_flag = 1       -- ADD: bug3202534
10385                     then
10386                        v_new_job := 1;
10387                         declare
10388                             temp_common_bom_seq_id   number;
10389                         begin
10390 l_stmt_num := 810;
10391                             if(l_chg_alt_flag = 1 or l_chg_ref_flag = 1 or l_chg_rev_flag = 1) then
10392                                 -- Delete WO records
10393                                 delete from wip_operations
10394                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10395                                 delete from wip_operation_yields
10396                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10397                                 delete from wip_operation_resources
10398                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10399                                 delete from wip_requirement_operations
10400                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10401                                 delete from wip_sub_operation_resources
10402                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10403                                 delete from wip_operation_resource_usage
10404                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10405                                 delete from wip_op_resource_instances
10406                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10407                                 v_new_job := 2;
10408                             end if;
10409 l_stmt_num := 820;
10410                             -- {{ Check for disabled ops in the network,job_type 1 and 3
10411                             if (wsmputil.network_with_disabled_op(
10412                                     l_common_rtg_seq_id,
10413                                     v_wlji_rtg_rev_date(v_idx),
10414                                     l_error_code,
10415                                     l_error_msg) = 1)
10416                             then
10417                                 fnd_message.set_name('WSM','WSM_NET_HAS_DISABLED_OP');
10418                                 l_error_msg := fnd_message.get;
10419                                 l_warning_count := l_warning_count + 1;
10420                                 handle_warning(
10421                                     p_err_msg               => l_error_msg,
10422                                     p_header_id             => v_wlji_header_id(v_idx),
10423                                     p_request_id            => v_wlji_request_id(v_idx),
10424                                     p_program_id            => v_wlji_program_id(v_idx),
10425                                     p_program_application_id=> v_wlji_program_appl_id(v_idx));
10426 
10427                             end if;
10428                             -- }} Check for disabled ops in the network
10429 
10430                             if(l_chg_ref_flag = 1) then
10431                                 temp_common_bom_seq_id := null;
10432                             else
10433                                 temp_common_bom_seq_id := l_common_bom_seq_id;
10434                             end if;
10435 l_stmt_num := 830;
10436                             if(v_wlji_scheduling_method(v_idx) = 1) then
10437                                 l_inf_sche_flag := 'Y';
10438                             else
10439                                 l_inf_sche_flag := 'N';
10440                             end if;
10441                             v_insert_wip := 1; --OPTII-PERF:By default,it is 1 for new jobs.
10442                                                --If phantom requirement exists, this will be Set to No.
10443                             build_job_copy_info(
10444                                 p_common_routing_sequence_id    => l_common_rtg_seq_id,
10445                                 p_common_bill_sequence_id       => temp_common_bom_seq_id,
10446                                 p_org_id                        => v_wlji_org_id(v_idx),
10447                                 p_wip_entity_id                 => v_wlji_wip_entity_id(v_idx),
10448                                 p_last_updt_date                => v_wlji_last_updt_date(v_idx),
10449                                 p_last_updt_by                  => v_wlji_last_updt_by(v_idx),
10450                                 p_creation_date                 => v_wlji_creation_date(v_idx),
10451                                 p_created_by                    => v_wlji_created_by(v_idx),
10452                                 p_last_updt_login               => v_wlji_last_updt_login(v_idx),
10453                                 p_request_id                    => v_wlji_request_id(v_idx),
10454                                 p_program_appl_id               => v_wlji_program_appl_id(v_idx),
10455                                 p_program_id                    => v_wlji_program_id(v_idx),
10456                                 p_prog_updt_date                => v_wlji_prog_updt_date(v_idx),
10457                                 p_item                          => v_wlji_item_id(v_idx),
10458                                 p_bom_reference_id              => v_wlji_bom_ref_id(v_idx),
10459                                 p_routing_reference_id          => v_wlji_rtg_ref_id(v_idx),
10460                                 p_wip_supply_type               => v_wlji_wip_supply_type(v_idx),
10461                                 p_job_scheduled_start_date      => v_wlji_fusd(v_idx),
10462                                 p_job_scheduled_compl_date      => v_wlji_lucd(v_idx),
10463                                 p_start_quantity                => v_wlji_start_quantity(v_idx),
10464                                 p_bom_revision_date             => v_wlji_bom_rev_date(v_idx),
10465                                 p_routing_revision_date         => v_wlji_rtg_rev_date(v_idx),
10466                                 p_alt_bom_designator            => v_wlji_alt_bom(v_idx),
10467                                 p_alt_routing_designator        => v_wlji_alt_rtg(v_idx),
10468                                 p_header_id                     => v_wlji_header_id(v_idx),
10469                                 p_num_of_children               => v_wlji_num_of_children(v_idx),
10470                                 p_infinite_schedule             => l_inf_sche_flag,
10471                                 p_error_code                    => l_error_code,
10472                                 p_error_msg                     => l_error_msg);
10473                             if l_error_code <> 0 then
10474                                 handle_error(l_error_code, l_error_msg,  l_stmt_num);
10475                                 l_error_count := l_error_count + 1;
10476                                 GOTO abort_this_job;
10477                             end if;
10478 
10479 
10480                             if v_wlji_mode_flag(v_idx) = 2 then
10481 l_stmt_num := 840;
10482                                 -- BA: bug 3299026 do not allow serial controlled component
10483                                 select  SERIAL_NUMBER_CONTROL_CODE,
10484                                         revision_qty_control_code       -- Add bug 2963225
10485                                 into    dummy_number,
10486                                         l_rev_control_code              -- Add bug 2963225
10487                                 from    mtl_system_items_kfv msi
10488                                 where   msi.inventory_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10489                                 and     msi.organization_id = v_wlji_org_id(v_idx);
10490 
10491                                 if(dummy_number <> 1) then
10492                                     l_error_code := -1;
10493                                     handle_errmsg_name('WSM',
10494                                                        'WSM_SERIAL_COMP_NOT_SUPPORTED',
10495                                                        l_stmt_num);
10496                                     l_error_count := l_error_count + 1;
10497                                     GOTO abort_this_job;
10498                                 end if;
10499                                 --EA bug 3299026
10500 
10501                                 -- {{ Check the component required at 1st op
10502 l_stmt_num := 840.1;
10503                                 find_copy_routing_start(
10504                                     v_wlji_wip_entity_id(v_idx),
10505                                     l_start_op_seq_num,
10506                                     l_start_op_seq_id,
10507                                     l_error_code,
10508                                     l_error_msg);
10509                                 if l_error_code <> 0 OR l_error_msg IS NOT NULL then
10510                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
10511                                     l_error_code := -1;
10512                                     l_error_count := l_error_count + 1;
10513                                     GOTO abort_this_job;
10514                                 end if;
10515 l_stmt_num := 840.2;
10516 
10517   /****************ADDED FOLLOWING CODE FOR 12.1 SUBSTITUTE COMPONENT PROJECT TO CHECK WHETHER THE SOURCE LOT IS OF PRIMARY COMPONENT
10518   OR SUBSTITUTE COMPONENT ***********************************************************************************************************/
10519 
10520 
10521                         BEGIN
10522 
10523                                 SELECT 1,
10524                                        bic.component_item_id,
10525                                        bsc.substitute_item_quantity,
10526                                        (bsc.substitute_item_quantity/bic.component_yield_factor)
10527                                  INTO  l_is_src_lot_sub,
10528                                        l_primary_comp_item_id,
10529                                        l_sub_comp_bill_qpa,
10530                                        l_sub_comp_qpa
10531                                  FROM  bom_bill_of_materials bom,
10532                                        bom_inventory_components bic,
10533                                        bom_substitute_components bsc
10534                                  WHERE bsc.SUBSTITUTE_COMPONENT_ID = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10535                                    AND bic.COMPONENT_SEQUENCE_ID = bsc.COMPONENT_SEQUENCE_ID
10536                                    AND bom.organization_id =v_wlji_org_id(v_idx)
10537                                    AND bic.bill_sequence_id = bom.common_bill_sequence_id
10538                                    AND bom.common_bill_sequence_id =l_common_bom_seq_id
10539                                    AND bic.implementation_date is not null
10540                                    AND bic.effectivity_date <= sysdate and nvl(bic.disable_date,sysdate +1) > sysdate
10541                                    AND (bic.operation_seq_num =  l_start_op_seq_num OR bic.operation_seq_num = 1)
10542                                    AND bic.item_num = (SELECT min(item_num)
10543                                                        FROM bom_bill_of_materials bom3,
10544                                                             bom_inventory_components bic3,
10545                                                             bom_substitute_components bsc3
10546                                                       WHERE bsc3.SUBSTITUTE_COMPONENT_ID = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10547                                                         AND bic3.COMPONENT_SEQUENCE_ID = bsc3.COMPONENT_SEQUENCE_ID
10548                                                         AND bom3.organization_id =v_wlji_org_id(v_idx)
10549                                                         AND bic3.bill_sequence_id = bom3.common_bill_sequence_id
10550                                                         AND bom3.common_bill_sequence_id = l_common_bom_seq_id
10551                                                         AND bic3.implementation_date is not null
10552                                                         AND bic3.effectivity_date <= sysdate
10553                                                         AND nvl(bic3.disable_date,sysdate +1) > sysdate)
10554                                   AND NOT EXISTS
10555                                             ( SELECT 1  FROM bom_bill_of_materials bom2,
10556                                                              bom_inventory_components bic2
10557                                               WHERE bic2.component_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10558                                               AND bom2.organization_id =v_wlji_org_id(v_idx)
10559                                               AND bic2.bill_sequence_id = bom2.common_bill_sequence_id
10560                                               AND bom2.common_bill_sequence_id =  l_common_bom_seq_id
10561                                               AND bic.effectivity_date <= sysdate and nvl(bic.disable_date,sysdate +1) > sysdate
10562                                               AND bic2.implementation_date is not null
10563                                               AND (bic2.operation_seq_num =  l_start_op_seq_num OR bic.operation_seq_num = 1));
10564 
10565                           EXCEPTION
10566                                   WHEN OTHERS THEN
10567                                     l_is_src_lot_sub :=0;
10568 
10569 
10570 
10571                           END;
10572 
10573 
10574 /* END CHANGES FOR 12.1 SUBSTITUTE COMPONENT PROJECT**********************************************************
10575 *************************************************************************************************************/
10576 
10577 		                --Bug 5112899:Using the below sql to read
10578 				-- BILL_QUANTITY_PER_ASSEMBLY and basis_type.
10579 				--Sum is removed as there can not be more than
10580 				--1 record in BOM for a given comp item id.
10581 				--select  sum(quantity_per_assembly)
10582 
10583 			     /* CHANGED TO DECODE FOR 12.1 SUBSTITUTE COMPONENT PROJECT */
10584 
10585 			            /* 	select  quantity_per_assembly,BILL_QUANTITY_PER_ASSEMBLY, */
10586 				select  decode(l_is_src_lot_sub,1,l_sub_comp_qpa,quantity_per_assembly),
10587                                         decode(l_is_src_lot_sub,1,l_sub_comp_bill_qpa,BILL_QUANTITY_PER_ASSEMBLY),
10588 				        basis_type
10589 				into   l_required_qpa,l_bill_req_qpa,l_basis_type
10590 				from    wsm_copy_requirement_ops wcro
10591                                 where   wcro.wip_entity_id = v_wlji_wip_entity_id(v_idx)
10592                                /* and     wcro.component_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id */
10593                                 and     wcro.component_item_id = decode(l_is_src_lot_sub,1,l_primary_comp_item_id,v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id)
10594 
10595                                 and     source_phantom_id = -1  -- is null
10596                                 and     recommended = 'Y'
10597 				and     wcro.operation_seq_num = l_start_op_seq_num;
10598 				--Bug 5112899:wcro.operation_seq_num equal to 1 need not be
10599 				--start operation.
10600                                 --and     (wcro.operation_seq_num = l_start_op_seq_num or
10601                                 --         wcro.operation_seq_num= 1);
10602                                 --if(l_required_qpa = 0) then           -- bug 2963225
10603                                 if(NVL(l_required_qpa, 0) = 0) then     -- bug 2963225
10604                                     l_error_code := -1;
10605                                     fnd_message.set_name('WSM','WSM_INVALID_BOM_ROUT');
10606                                     l_error_msg := fnd_message.get;
10607                                     handle_error(l_error_code, l_error_msg, l_stmt_num);
10608                                     l_error_count := l_error_count + 1;
10609                                     GOTO abort_this_job;
10610                                 end if;
10611 
10612                                 if lbji_debug = 'Y' then
10613                                     fnd_file.put_line(fnd_file.log,
10614                                             'Verify if component is required at the first operation... OK');
10615                                 end if;
10616                                 -- }} Check the component required at first op
10617 l_stmt_num := 850;
10618                                 -- {{ validate starting lot is not phantom
10619                                 l_temp_num := 0;
10620                                 begin
10621                                     select  1
10622                                     into    l_temp_num
10623                                     from    wsm_copy_requirement_ops wcro
10624                                     where   wcro.wip_entity_id = v_wlji_wip_entity_id(v_idx)
10625                                     /*and     wcro.component_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id */
10626                                     and     wcro.component_item_id = decode(l_is_src_lot_sub,1,l_primary_comp_item_id,v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id)
10627                                     and     recommended = 'Y'
10628                                     and     (wcro.operation_seq_num = l_start_op_seq_num or
10629                                              wcro.operation_seq_num= 1)
10630                                     and     nvl(wcro.wip_supply_type, 1) = 6;
10631                                 exception
10632                                     when too_many_rows then
10633                                         l_temp_num := 1;
10634                                     when others then
10635                                         null;
10636                                 end;
10637                                 if(l_temp_num = 1) then
10638                                     l_error_code := -1;
10639                                     handle_errmsg_name(
10640                                         'WSM', 'WSM_PHANTOM_COMP_NOT_ALLOWED', l_stmt_num);
10641                                     l_error_count := l_error_count + 1;
10642                                     GOTO abort_this_job;
10643                                 end if;
10644                                 if lbji_debug = 'Y' then
10645                                     fnd_file.put_line(fnd_file.log,
10646                                         'Verify that the starting lot is not phantom.. OK');
10647                                 end if;
10648                                 -- }} validate starting lot is not phantom
10649 
10650                                 -- {{ Validate component quantity
10651 				--Bug 5112899: Start of changes
10652 l_stmt_num := 860.1;
10653                                select include_component_yield
10654 			       into   l_include_yield
10655 			       from   wip_parameters
10656 			       where  organization_id = v_wlji_org_id(v_idx);
10657 l_stmt_num := 860.2;
10658 			        --Bug 5112899: Start of changes
10659                                 IF v_wsli(v_wlji_src_line_id(v_idx)).comp_issue_quantity IS NULL THEN
10660                                    if l_basis_type is NULL or l_basis_type = 1 then
10661 					 If l_include_yield = 1 or l_include_yield is NULL then
10662 			        --Bug 5112899: End of changes
10663 					   l_quantity_tobe_issued
10664 					    := l_required_qpa * v_wlji_start_quantity(v_idx);
10665 				--Bug 5112899: Start of changes
10666 					 else -- l_include_yield
10667 					   l_quantity_tobe_issued
10668 					    := l_bill_req_qpa * v_wlji_start_quantity(v_idx);
10669 					 end if;-- l_include_yield
10670 			           else --basis type
10671 			              If l_include_yield is null or  l_include_yield = 1 then
10672 					   l_quantity_tobe_issued:= l_required_qpa;
10673 				      else
10674 				           l_quantity_tobe_issued:= l_bill_req_qpa;
10675 				      end if;
10676 			           end if;--basis_type.
10677                                 END IF; -- comp_issue_quantity
10678 			       --Bug 5112899: End of changes
10679 l_stmt_num := 860.3;
10680                                 if v_wsli(v_wlji_src_line_id(v_idx)).comp_issue_quantity is not null then
10681                                     if v_wsli(v_wlji_src_line_id(v_idx)).comp_issue_quantity <= 0 then
10682                                         l_error_code := -1;
10683                                         handle_errmsg_name('WSM','WSM_QTY_ISSUE_NO_NEG', l_stmt_num);
10684                                     else
10685                                         l_quantity_tobe_issued
10686                                             := v_wsli(v_wlji_src_line_id(v_idx)).comp_issue_quantity;
10687                                     end if;
10688                                 end if;
10689 
10690 l_stmt_num := 870;
10691                                 -- if the user does not provide a revision in wsm_starting_lots_interface
10692                                 -- then get the current revision and use that to issue components. if user
10693                                 -- provides one, validate it
10694                                 l_source_item_rev := v_wsli(v_wlji_src_line_id(v_idx)).revision;
10695                                 declare
10696                                     err_lot_revision    exception;  -- Add: bug 2963225
10697                                 begin
10698                                     -- BC: bug 2963225 this is to validate the revision of the item
10699                                     -- which is not right, instead, we should validate / default
10700                                     -- the revision of the starting lot, so comment out the following
10701 
10702                                     --l_rev_date := sysdate;
10703                                     --wip_revisions.bom_revision (
10704                                     --    v_wlji_org_id(v_idx),
10705                                     --    v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
10706                                     --    l_source_item_rev,
10707                                     --    l_source_item_rev_date,
10708                                     --    l_rev_date);
10709 
10710                                     if l_rev_control_code = 1 then -- not revision control
10711                                         if(l_source_item_rev is not null) then
10712                                             raise err_lot_revision;
10713                                         end if;
10714                                     else    -- revision control item
10715                                         select  revision
10716                                         into    l_start_lot_revision
10717                                         from    WSM_source_lots_v
10718                                         where   lot_number = v_wsli(v_wlji_src_line_id(v_idx)).lot_number
10719                                         and     organization_id = v_wlji_org_id(v_idx)
10720                                         and     inventory_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id;
10721 
10722                                         if(l_source_item_rev is null) then
10723                                             v_wsli(v_wlji_src_line_id(v_idx)).revision := l_start_lot_revision;
10724                                         elsif(l_source_item_rev <> l_start_lot_revision) then
10725                                             raise err_lot_revision;
10726                                         end if;
10727                                     end if;
10728                                     -- EC: bug 2963225
10729 
10730                                 exception
10731                                     when err_lot_revision then  -- Add: bug 2963225
10732                                         l_error_code := -1;
10733                                         l_error_count := l_error_count +1;
10734                                         handle_invalid_field('Component Lot Revision', '', l_stmt_num);
10735                                         GOTO abort_this_job;
10736 
10737                                     when others then
10738                                         l_error_code := -1;
10739                                         l_error_count := l_error_count +1;
10740                                         handle_invalid_field('Component Lot Revision', '', l_stmt_num);
10741                                         GOTO abort_this_job;
10742                                 end;
10743 l_stmt_num := 880;
10744                                 wsmputil.return_att_quantity(
10745                                     p_org_id     => v_wlji_org_id(v_idx),
10746                                     p_item_id    => v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
10747                                     p_rev        => v_wsli(v_wlji_src_line_id(v_idx)).revision,
10748                                     p_lot_no     => v_wsli(v_wlji_src_line_id(v_idx)).lot_number,
10749                                     p_subinv     => v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code,
10750                                     p_locator_id => v_wsli(v_wlji_src_line_id(v_idx)).locator_id,
10751                                     p_qoh        => l_qoh,
10752                                     p_atr        => l_atr,
10753                                     p_att        => l_att,
10754                                     p_err_code   => l_error_code,
10755                                     p_err_msg    => l_error_msg );
10756 
10757                                 if l_error_code <> 0 then
10758                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
10759                                     l_error_count := l_error_count + 1;
10760                                     GOTO abort_this_job;
10761                                 end if;
10762                                 if l_quantity_tobe_issued > l_att then
10763                                     -- bug 3741740 remove this validation because we allow issue
10764                                     -- less quantity than job required quantity through form
10765                                     -- we will issue all the quantity if qty_tobe_issued > l_att
10766                                     l_quantity_tobe_issued := l_att;
10767                                     --l_error_code := -1;
10768                                     --handle_errmsg_name('WSM', 'WSM_INSUFFICIENT_QTY', l_stmt_num);
10769                                     --l_error_count := l_error_count + 1;
10770                                     --GOTO abort_this_job;
10771                                 end if;
10772                                 if lbji_debug = 'Y' then
10773                                     fnd_file.put_line(fnd_file.log, 'Mode 2 Quantity Verification... OK');
10774                                 end if;
10775                                 -- }} Validate component quantity
10776                                 l_src_lot_number := v_wsli(v_wlji_src_line_id(v_idx)).lot_number;     -- LOTATTR
10777                                 l_src_inv_item_id := v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id; -- LOTATTR
10778 
10779                             end if; -- mode_flag 2
10780 
10781 l_stmt_num := 890;
10782                             build_job_detail_info(
10783                                 p_common_routing_sequence_id    => l_common_rtg_seq_id,
10784                                 p_common_bill_sequence_id       => temp_common_bom_seq_id,
10785                                 p_status_type                   => v_wlji_status_type(v_idx),
10786                                 p_org_id                        => v_wlji_org_id(v_idx),
10787                                 p_wip_entity_id                 => v_wlji_wip_entity_id(v_idx),
10788                                 p_last_updt_date                => v_wlji_last_updt_date(v_idx),
10789                                 p_last_updt_by                  => v_wlji_last_updt_by(v_idx),
10790                                 p_creation_date                 => v_wlji_creation_date(v_idx),
10791                                 p_created_by                    => v_wlji_created_by(v_idx),
10792                                 p_last_updt_login               => v_wlji_last_updt_login(v_idx),
10793                                 p_request_id                    => v_wlji_request_id(v_idx),
10794                                 p_program_appl_id               => v_wlji_program_appl_id(v_idx),
10795                                 p_program_id                    => v_wlji_program_id(v_idx),
10796                                 p_prog_updt_date                => v_wlji_prog_updt_date(v_idx),
10797                                 p_item                          => v_wlji_item_id(v_idx),
10798                                 p_job_type                      => v_wlji_job_type(v_idx),
10799                                 p_bom_reference_id              => v_wlji_bom_ref_id(v_idx),
10800                                 p_routing_reference_id          => v_wlji_rtg_ref_id(v_idx),
10801                                 p_wip_supply_type               => v_wlji_wip_supply_type(v_idx),
10802                                 p_job_scheduled_start_date      => v_wlji_fusd(v_idx),
10803                                 p_job_scheduled_compl_date      => v_wlji_lucd(v_idx),
10804                                 p_start_quantity                => v_wlji_start_quantity(v_idx),
10805                                 p_bom_revision_date             => v_wlji_bom_rev_date(v_idx),
10806                                 p_routing_revision_date         => v_wlji_rtg_rev_date(v_idx),
10807                                 p_alt_bom_designator            => v_wlji_alt_bom(v_idx),
10808                                 p_alt_routing_designator        => v_wlji_alt_rtg(v_idx),
10809                                 p_rtg_op_seq_num                => null,
10810                                 p_error_code                    => l_error_code,
10811                                 p_error_msg                     => l_error_msg,
10812                                 -- BA:bug 3311985, call build_job_detail_info with new parameter
10813                                 p_src_client_server             => 0,
10814                                 p_po_creation_time              => WSMPLCVA.v_org(v_wlji_org_id(v_idx)).PO_CREATION_TIME
10815                                 -- EA:bug 3311985, call build_job_detail_info with new parameter
10816                             );
10817                             if l_error_code <> 0 then
10818                                 handle_error(l_error_code, l_error_msg,  l_stmt_num);
10819                                 l_error_count := l_error_count + 1;
10820                                 GOTO abort_this_job;
10821                             end if;
10822 
10823 /****************ADDED FOLLOWING CODE FOR 12.1 SUBSTITUTE COMPONENT PROJECT TO UPDATE WIP_REQIREMENT_OPERATIONS TO REPLACE REQUIREMENT OF PRIMARY COMPONENT
10824  ***********************************************************************************************************/
10825 
10826                              IF (l_is_src_lot_sub = 1 AND v_wlji_mode_flag(v_idx) = 2 ) THEN
10827 
10828                                BEGIN
10829 
10830                                    SELECT segment1,segment2,segment3,segment4,
10831                                           segment5,segment6,segment7,segment8,
10832                                           segment9,segment10,segment11,segment12,
10833                                           segment13,segment14,segment15,segment16,
10834                                           segment17,segment18,segment19,segment20
10835                                     INTO
10836                                           l_segment1,l_segment2,l_segment3,l_segment4,
10837                                           l_segment5,l_segment6,l_segment7,l_segment8,
10838                                           l_segment9,l_segment10,l_segment11,l_segment12,
10839                                           l_segment13,l_segment14,l_segment15,l_segment16,
10840                                           l_segment17,l_segment18,l_segment19,l_segment20
10841                                     FROM  mtl_system_items
10842                                     WHERE inventory_item_id =  v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10843                                      AND  organization_id=v_wlji_org_id(v_idx);
10844 
10845                                     EXCEPTION
10846                                      WHEN OTHERS THEN
10847                                        NULL;
10848 
10849                                  END;
10850 
10851                              UPDATE wip_requirement_operations
10852                                SET  inventory_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
10853                                     required_quantity = ((required_quantity * l_sub_comp_bill_qpa)/quantity_per_assembly),
10854                                     quantity_per_assembly = l_sub_comp_bill_qpa,
10855                                     segment1 = l_segment1,
10856                                     segment2 = l_segment2,
10857                                     segment3 = l_segment3,
10858                                     segment4 = l_segment4,
10859                                     segment5 = l_segment5,
10860                                     segment6 = l_segment6,
10861                                     segment7 = l_segment7,
10862                                     segment8 = l_segment8,
10863                                     segment9 = l_segment9,
10864                                     segment10 = l_segment10,
10865                                     segment11 = l_segment11,
10866                                     segment12 = l_segment12,
10867                                     segment13 = l_segment13,
10868                                     segment14 = l_segment14,
10869                                     segment15 = l_segment15,
10870                                     segment16 = l_segment16,
10871                                     segment17 = l_segment17,
10872                                     segment18 = l_segment18,
10873                                     segment19 = l_segment19,
10874                                     segment20 = l_segment20,
10875                                     released_quantity = ((released_quantity * l_sub_comp_bill_qpa)/quantity_per_assembly)
10876                               WHERE wip_entity_id = v_wlji_wip_entity_id(v_idx)
10877                                 AND inventory_item_id = l_primary_comp_item_id;
10878 
10879 
10880 
10881                               END IF;
10882 
10883 /* END CHANGES FOR 12.1 SUBSTITUTE COMPONENT PROJECT**********************************************************
10884 *************************************************************************************************************/
10885 
10886                         exception
10887                             when others then
10888                                 l_error_code := SQLCODE;
10889                                 l_error_msg  :=
10890                                     'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
10891                                    ||SUBSTR(SQLERRM, 1,1000);
10892                                 handle_error(l_error_code, l_error_msg, l_stmt_num);
10893                                 l_error_count := l_error_count + 1;
10894                                 GOTO abort_this_job;
10895                         end;
10896 
10897                     end if;
10898                     -- ----------------------------------------------------------------
10899                     -- }}} End: Write WO's and WCO's, job creation and alt/ref change
10900                     -- ----------------------------------------------------------------
10901 
10902                     -- -------------------------------
10903                     -- {{{ Begin: Write for job update
10904                     -- -------------------------------
10905                     if v_wlji_load_type(v_idx) = 6 then
10906 
10907                         -- {{ do not skip update
10908                         -- if the user wants to update only the references, the following
10909                         -- updates are not needed
10910                         if (l_skip_update_wo = 0 and
10911                             v_wlji_allow_explosion(v_idx) = 'Y' and
10912                             v_wlji_status_type(v_idx) <> 7 and
10913                             l_chg_alt_flag <> 1 and
10914                             l_chg_ref_flag <> 1 and
10915                             l_chg_rev_flag <> 1)
10916                         then
10917                             Begin
10918 l_stmt_num := 900;
10919                                 if g_charges_exist_shopfloor then
10920                                     l_txnexist := 1;
10921                                 else
10922                                     l_txnexist := 0;
10923                                 end if;
10924 l_stmt_num := 910;
10925                                 if v_wlji_start_quantity(v_idx) is null then      --bug4540762: begin
10926                                    v_wlji_start_quantity(v_idx) := l_cur_start_qty;
10927                                 end if;                                           --bug4540762: end
10928 
10929                                 if v_wlji_start_quantity(v_idx) <> l_cur_start_qty then
10930                                     l_qntydiff := 1;
10931                                 else
10932                                     l_qntydiff := 0;
10933                                 end if;
10934 
10935                                 update wip_operations
10936                                 set    -- BD: BUG 3479666
10937                                        -- in option C, we should not change operation start/completion
10938                                        -- date based on job header level information
10939                                        --
10940                                        --first_unit_start_date
10941                                        --    = decode(l_txnexist,
10942                                        --             0, nvl(v_wlji_fusd(v_idx), first_unit_start_date),      -- bug 3394520
10943                                        --             first_unit_start_date),
10944                                        --first_unit_completion_date
10945                                        --    = decode(l_txnexist,
10946                                        --             0, nvl(v_wlji_lucd(v_idx), first_unit_completion_date), -- bug 3394520
10947                                        --             first_unit_completion_date),
10948                                        --last_unit_start_date
10949                                        --    = decode(l_txnexist,
10950                                        --             0, nvl(v_wlji_fusd(v_idx), last_unit_start_date),       -- bug 3394520
10951                                        --             last_unit_start_date),
10952                                        --last_unit_completion_date
10953                                        --    = decode(l_txnexist,
10954                                        --             0, nvl(v_wlji_lucd(v_idx), last_unit_completion_date),  -- bug 3394520
10955                                        --             last_unit_completion_date),
10956                                        -- ED: BUG 3479666
10957                                        scheduled_quantity
10958                                            = round(v_wlji_start_quantity(v_idx),
10959                                                    wip_constants.max_displayed_precision),
10960                                        quantity_in_queue
10961                                            = decode(v_wlji_status_type(v_idx),
10962                                                     1, 0,
10963                                                     3, decode(l_cur_status_type,
10964                                                               1, decode(operation_seq_num,
10965                                                                         g_op_seq_incr,
10966                                                                         round(v_wlji_start_quantity(v_idx),
10967                                                                               wip_constants.max_displayed_precision),
10968                                                                         quantity_in_queue),
10969                                                               decode(l_qntydiff,
10970                                                                      1, decode(operation_seq_num,
10971                                                                                g_op_seq_incr,
10972                                                                                round(v_wlji_start_quantity(v_idx),
10973                                                                                      wip_constants.max_displayed_precision),
10974                                                                                quantity_in_queue),
10975                                                                      quantity_in_queue)),
10976                                                     quantity_in_queue),
10977                                        last_updated_by        = v_wlji_last_updt_by(v_idx),
10978                                        last_update_date       = sysdate,
10979                                        last_update_login      = v_wlji_last_updt_login(v_idx),
10980                                        program_update_date    = sysdate,
10981                                        request_id             = v_wlji_request_id(v_idx),
10982                                        program_application_id = v_wlji_program_appl_id(v_idx),
10983                                        program_id             = v_wlji_program_id(v_idx)
10984                                 where  organization_id        = v_wlji_org_id(v_idx)
10985                                 and    wip_entity_id          = v_wlji_wip_entity_id(v_idx);
10986                                 if lbji_debug = 'Y' then
10987                                     fnd_file.put_line(fnd_file.log,
10988                                         'Updated '||SQL%ROWCOUNT||' rows into wo');
10989                                 end if;
10990 
10991 l_stmt_num := 920.1;
10992                                 -- bug 3203505 change order with updating wro
10993                                 --Bug 5456260:In Option C,resource dates should not be
10994                                 --updated with the header dates.
10995                                 /*******************
10996                                 update wip_operation_resources
10997                                 set    start_date             = decode(l_txnexist,
10998                                                                        0, nvl(v_wlji_fusd(v_idx), start_date),      -- bug 3394520
10999                                                                        start_date),
11000                                        completion_date        = decode(l_txnexist,
11001                                                                        0, nvl(v_wlji_lucd(v_idx), completion_date), -- bug 3394520
11002                                                                        completion_date),
11003                                        last_updated_by        = v_wlji_last_updt_by(v_idx),
11004                                        last_update_date       = sysdate,
11005                                        last_update_login      = v_wlji_last_updt_login(v_idx),
11006                                        program_update_date    = sysdate,
11007                                        request_id             = v_wlji_request_id(v_idx),
11008                                        program_application_id = v_wlji_program_appl_id(v_idx),
11009                                        program_id             = v_wlji_program_id(v_idx)
11010                                 where  organization_id = v_wlji_org_id(v_idx)
11011                                 and    wip_entity_id = v_wlji_wip_entity_id(v_idx);
11012                                 if lbji_debug = 'Y' then
11013                                     fnd_file.put_line(fnd_file.log,
11014                                         'Updated '||SQL%ROWCOUNT||' rows into wor');
11015                                 end if;
11016                                 *******************/
11017 l_stmt_num := 920.2;
11018                                 --LBM enh : changed the expression for required quantity
11019                                 update wip_requirement_operations wro
11020                                 set    wro.date_required = (
11021                                             select nvl(min(first_unit_start_date), v_wlji_fusd(v_idx))
11022                                             from   wip_operations
11023                                             where  organization_id = v_wlji_org_id(v_idx)
11024                                             and    wip_entity_id = v_wlji_wip_entity_id(v_idx)
11025                                             and    operation_seq_num = abs(wro.operation_seq_num)),
11026                                        last_updated_by        = v_wlji_last_updt_by(v_idx),
11027                                        last_update_date       = sysdate,
11028                                        last_update_login      = v_wlji_last_updt_login(v_idx),
11029                                        request_id             = v_wlji_request_id(v_idx),
11030                                        program_update_date    = sysdate,
11031                                        program_id             = v_wlji_program_id(v_idx),
11032                                        program_application_id = v_wlji_program_appl_id(v_idx),
11033                                        required_quantity      = ((wro.quantity_per_assembly/wro.component_yield_factor) * decode(
11034                                                           wro.basis_type, 2, 1, round(v_wlji_start_quantity(v_idx), 6)))
11035                                 where  organization_id        = v_wlji_org_id(v_idx)
11036                                 and    wip_entity_id = v_wlji_wip_entity_id(v_idx);
11037                                 if lbji_debug = 'Y' then
11038                                     fnd_file.put_line(fnd_file.log,
11039                                         'Updated '||SQL%ROWCOUNT||' rows into wro');
11040                                 end if;
11041 l_stmt_num := 920.3;
11042                                 -- abb H: optional scrap accounting
11043 
11044                                 if (l_cur_status_type IN (1,6) and v_wlji_status_type(v_idx) = 3) and
11045                                     wsmputil.wsm_esa_enabled(
11046                                             p_wip_entity_id => v_wlji_wip_entity_id(v_idx),
11047                                             err_code        => l_error_code,
11048                                             err_msg         => l_error_msg,
11049                                             p_org_id        => '',
11050                                             p_job_type      => '') = 1
11051                                 then
11052 l_stmt_num := 920.31;
11053                                     select min(operation_seq_num)
11054                                     into   l_min_op_seq_num
11055                                     from   wip_operations
11056                                     where  wip_entity_id =  v_wlji_wip_entity_id(v_idx);
11057 l_stmt_num := 920.32;
11058                                     select bd.scrap_account,
11059                                            bd.est_absorption_account,
11060                                            wo.department_id
11061                                     into   l_scrap_account_10,
11062                                            l_est_scrap_abs_account_10,
11063                                            l_department_id
11064                                     from   bom_departments bd,
11065                                            wip_operations wo
11066                                     where  wo.wip_entity_id = v_wlji_wip_entity_id(v_idx)
11067                                     and    wo.operation_seq_num = l_min_op_seq_num
11068                                     and    bd.department_id = wo.department_id;
11069 
11070                                     if l_scrap_account_10 is null or l_est_scrap_abs_account_10 is null then
11071                                         l_error_code := -1;
11072                                         fnd_message.set_name('WSM','WSM_NO_SCRAP_ACC');
11073                                         fnd_message.set_token('DEPT_ID',to_char(l_department_id));
11074                                         l_error_msg := fnd_message.get;
11075                                         handle_error(l_error_code, l_error_msg, l_stmt_num);
11076                                         l_error_count := l_error_count + 1;
11077                                         GOTO abort_this_job;
11078                                     end if;
11079 l_stmt_num := 920.33;
11080                                     update wip_operation_yields woy
11081                                     set    scrap_account = nvl(l_scrap_account_10, woy.scrap_account),
11082                                            est_scrap_absorb_account = nvl(l_est_scrap_abs_account_10,
11083                                                                           woy.est_scrap_absorb_account)
11084                                     where  wip_entity_id = v_wlji_wip_entity_id(v_idx)
11085                                     and    operation_seq_num = l_min_op_seq_num;
11086 
11087                                     if lbji_debug = 'Y' then
11088                                         fnd_file.put_line(fnd_file.log, 'Updated '||SQL%ROWCOUNT||' rows into woy');
11089                                     end if;
11090 
11091                                 end if;
11092 
11093                             Exception
11094                                 when others then
11095                                     l_error_code := SQLCODE;
11096                                     l_error_msg  :=
11097                                         'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
11098                                        ||SUBSTR(SQLERRM, 1,1000);
11099                                     handle_error(l_error_code, l_error_msg,  l_stmt_num);
11100                                     l_error_count := l_error_count + 1;
11101                                     GOTO abort_this_job;
11102                             End;
11103                         end if;
11104                         -- }} do not skip update
11105 
11106 l_stmt_num := 930;
11107                         Begin
11108                             if l_cur_status_type <> 1 AND v_wlji_status_type(v_idx) = 1 then
11109                                 delete from wip_period_balances
11110                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx)
11111                                 and    organization_id = v_wlji_org_id(v_idx);
11112                                 if lbji_debug = 'Y' then
11113                                     fnd_file.put_line(fnd_file.log,
11114                                         'Deleted '||SQL%ROWCOUNT||' rows from WIP_PERIOD_BALANCES');
11115                                 end if;
11116                             end if;
11117 
11118 l_stmt_num := 940;
11119                             if v_wlji_status_type(v_idx) = 7 then --cancelled
11120 
11121                                 -- {{ osp begin
11122                                 if wip_osp.po_req_exists (
11123                                         v_wlji_wip_entity_id(v_idx),
11124                                         null,
11125                                         v_wlji_org_id(v_idx),
11126                                         null, 5)
11127                                 then
11128                                     fnd_message.set_name('WIP', 'WIP_CANCEL_JOB/SCHED_OPEN_PO');
11129                                     l_error_msg := fnd_message.get;
11130                                     l_warning_count := l_warning_count + 1;
11131                                     handle_warning(
11132                                         p_err_msg    => l_error_msg,
11133                                         p_header_id  => v_wlji_header_id(v_idx),
11134                                         p_request_id => v_wlji_request_id(v_idx),
11135                                         p_program_id => v_wlji_program_id(v_idx),
11136                                         p_program_application_id => v_wlji_program_appl_id(v_idx));
11137                                 end if;
11138                                 -- }} osp end
11139 l_stmt_num := 950.1;
11140                                 wip_picking_pvt.cancel_allocations(
11141                                         v_wlji_wip_entity_id(v_idx),
11142                                         5,
11143                                         NULL,
11144                                         l_return_status,
11145                                         l_error_msg);
11146 
11147                                 if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11148                                     handle_error(l_return_status, l_error_msg, l_stmt_num);
11149                                     l_error_count := l_error_count + 1;
11150                                     GOTO abort_this_job;
11151                                 else
11152 l_stmt_num := 950.2;
11153                                 --AH Add:SO LBJ changes--
11154 
11155                                 If wsm_reservations_pvt.check_reservation_exists(p_wip_entity_id    => v_wlji_wip_entity_id(v_idx),
11156                                                          p_org_id       => v_wlji_org_id(v_idx),
11157                                                          p_inventory_item_id    => v_wlji_item_id(v_idx)
11158                                                         )
11159                                 then
11160 l_stmt_num := 950.3;
11161                                     wsm_reservations_pvt.modify_reservations_jobupdate (
11162 
11163                                             p_wip_entity_id     => v_wlji_wip_entity_id(v_idx),
11164                                             p_old_net_qty       => round(v_wlji_net_quantity(v_idx),wip_constants.max_displayed_precision),
11165                                             p_new_net_qty       => round(v_wlji_net_quantity(v_idx),wip_constants.max_displayed_precision),
11166                                             p_inventory_item_id     => v_wlji_item_id(v_idx),
11167                                             p_org_id        => v_wlji_org_id(v_idx),
11168                                             p_status_type       => v_wlji_status_type(v_idx),
11169                                             x_return_status     => l_return_status,
11170                                             x_msg_count         => l_error_count,
11171                                             x_msg_data      => l_error_msg
11172                                                                    );
11173                                     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11174                                         handle_error(l_return_status, l_error_msg, l_stmt_num);
11175                                         l_error_count := l_error_count + 1;
11176                                         GOTO abort_this_job;
11177                                     end if;
11178                                 end if;
11179 
11180                                 --AH Add:SO LBJ changes--
11181 								--Added for Fixing bug 5242298. Txn type 2 adds sector extn for jobs which are cancelled
11182 l_stmt_num := 950.4;
11183 								l_wip_entity_name := WSMPOPRN.update_job_name
11184 									   (p_wip_entity_id   => v_wlji_wip_entity_id(v_idx),
11185                        					p_subinventory    => v_wlji_compl_subinv(v_idx),
11186 										p_org_id          => v_wlji_org_id(v_idx),
11187 										p_txn_type        =>  2,  -- COMPLETION
11188               							p_update_flag     => TRUE,
11189       									p_dup_job_name    => l_dummy_job_name,
11190        									x_error_code      => l_error_code,
11191 										x_error_msg       => l_error_msg);
11192 
11193 								if l_error_code <> 0 then
11194 										l_return_status := FND_API.G_RET_STS_ERROR;
11195                                         handle_error(l_return_status, l_error_msg, l_stmt_num);
11196                                         l_error_count := l_error_count + 1;
11197                                         GOTO abort_this_job;
11198                                 end if;
11199 
11200 								--Added for Fixing bug 5242298
11201 l_stmt_num := 950.5;
11202                                 update wip_discrete_jobs wdj
11203                                     set    status_type = 7
11204                                     where  wdj.wip_entity_id = v_wlji_wip_entity_id(v_idx);
11205                                     if lbji_debug = 'Y' then
11206                                         fnd_file.put_line(fnd_file.log,
11207                                             'Updated status type to 7 in wdj');
11208                                     end if;
11209                                 end if;
11210 
11211 l_stmt_num := 960;
11212                                 --{{ Kanban begin
11213                                 if v_wlji_kanban_card_id(v_idx) is not null then
11214                                     inv_kanban_pvt.update_card_supply_status(
11215                                         x_return_status  => l_return_status,
11216                                         p_kanban_card_id => v_wlji_kanban_card_id(v_idx),
11217                                         p_supply_status  => inv_kanban_pvt.g_supply_status_exception);
11218 
11219                                     if ( l_return_status <> fnd_api.g_ret_sts_success ) then
11220                                         l_error_code := -1;
11221                                         fnd_message.set_name('WSM', 'WSM_KNBN_CARD_STS_FAIL');
11222 l_stmt_num := 960.1;
11223                                         select meaning
11224                                         into   l_meaning
11225                                         from   mfg_lookups
11226                                         where  lookup_type = 'MTL_KANBAN_SUPPLY_STATUS'
11227                                         and    lookup_code = 7
11228                                         and    upper(enabled_flag) = 'Y';
11229 
11230                                         fnd_message.set_token('STATUS',l_meaning);
11231                                         l_error_msg := fnd_message.get;
11232                                         handle_error(l_error_code, l_error_msg,  l_stmt_num);
11233                                         l_error_count := l_error_count + 1;
11234                                         GOTO abort_this_job;
11235                                     end if;
11236 
11237                                     update wip_discrete_jobs
11238                                     set    kanban_card_id = null
11239                                     where  wip_entity_id =  v_wlji_wip_entity_id(v_idx);
11240 
11241                                 end if;
11242                                 --}} Kanban end
11243 
11244                             else  -- v_wlji_status_type(v_idx) <> 7
11245 l_stmt_num := 970.1;
11246                             --AH Add:SO LBJ changes--
11247                             DECLARE
11248                             l_net_qty_old NUMBER;
11249                             BEGIN
11250 
11251                             If wsm_reservations_pvt.check_reservation_exists(p_wip_entity_id    => v_wlji_wip_entity_id(v_idx),
11252                                                      p_org_id       => v_wlji_org_id(v_idx),
11253                                                      p_inventory_item_id    => v_wlji_item_id(v_idx)
11254                                                     )
11255                             then
11256                                 select net_quantity
11257                                 into   l_net_qty_old
11258                                 from   WIP_DISCRETE_JOBS
11259                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
11260 
11261                                 if v_wlji_net_quantity(v_idx) < l_net_qty_old then --net qty has become lesser.So reduce rsvtn
11262 
11263 										wsm_reservations_pvt.modify_reservations_jobupdate (
11264 											p_wip_entity_id     => v_wlji_wip_entity_id(v_idx),
11265 											p_old_net_qty       => l_net_qty_old ,
11266 											p_new_net_qty       => round(v_wlji_net_quantity(v_idx),wip_constants.max_displayed_precision),
11267 											p_inventory_item_id     => v_wlji_item_id(v_idx),
11268 											p_org_id        => v_wlji_org_id(v_idx),
11269 											p_status_type       => v_wlji_status_type(v_idx),
11270 											x_return_status     => l_return_status,
11271 											x_msg_count         => l_error_count,
11272 											x_msg_data      => l_error_msg
11273 														);
11274 										if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11275 											handle_error(l_return_status, l_error_msg, l_stmt_num);
11276 											l_error_count := l_error_count + 1;
11277 											GOTO abort_this_job;
11278 										end if;
11279 								end if;
11280                             end if;
11281 
11282                             END;
11283                             --AH Add:SO LBJ changes--
11284                             UPDATE  WIP_DISCRETE_JOBS WDJ
11285                             set     last_updated_by             = v_wlji_last_updt_by(v_idx),
11286                                     last_update_login           = v_wlji_last_updt_login(v_idx),
11287                                     request_id                  = v_wlji_request_id(v_idx),
11288                                     program_application_id      = v_wlji_program_appl_id(v_idx),
11289                                     program_id                  = v_wlji_program_id(v_idx),
11290                                     program_update_date         = sysdate,
11291                                     last_update_date            = sysdate,
11292                                     bom_reference_id            = v_wlji_bom_ref_id(v_idx),
11293                                     routing_reference_id        = v_wlji_rtg_ref_id(v_idx),
11294                                     common_bom_sequence_id      = l_common_bom_seq_id,
11295                                     common_routing_sequence_id  = l_common_rtg_seq_id,
11296                                     bom_revision                = v_wlji_bom_revision(v_idx),
11297                                     routing_revision            = v_wlji_rtg_revision(v_idx),
11298                                     bom_revision_date           = v_wlji_bom_rev_date(v_idx),
11299                                     routing_revision_date       = v_wlji_rtg_rev_date(v_idx),
11300                                     alternate_bom_designator    = v_wlji_alt_bom(v_idx),
11301                                     alternate_routing_designator= v_wlji_alt_rtg(v_idx),
11302                                     firm_planned_flag           = v_wlji_firm_planned_flag(v_idx),
11303                                     start_quantity              = nvl(round(v_wlji_start_quantity(v_idx),
11304                                                                             wip_constants.max_displayed_precision),
11305                                                                   wdj.start_quantity),
11306                                     net_quantity                = nvl(round(v_wlji_net_quantity(v_idx),
11307                                                                             wip_constants.max_displayed_precision),
11308                                                                   wdj.net_quantity),
11309                                     lot_number                  = decode(wdj.status_type, WIP_CONSTANTS.UNRELEASED,
11310                                                                               v_wlji_job_name(v_idx),wdj.lot_number),   -- fix for bug5990761
11311                                     status_type                 = nvl(v_wlji_status_type(v_idx),wdj.status_type),
11312                                     date_released               = v_wlji_date_released(v_idx), -- bug 2697295
11313                                     scheduled_start_date        = decode(l_txnexist, 0,
11314                                                                          trunc(NVL(v_wlji_fusd(v_idx), wdj.scheduled_start_date),'MI'),
11315                                                                          wdj.scheduled_start_date),
11316                                     scheduled_completion_date   = trunc(NVL(v_wlji_lucd(v_idx),wdj.scheduled_completion_date), 'MI'),
11317                                     completion_locator_id       = v_wlji_compl_loc_id(v_idx),
11318                                     completion_subinventory     = v_wlji_compl_subinv(v_idx),
11319                                     coproducts_supply           = nvl(v_wlji_coproducts_supply(v_idx), wdj.coproducts_supply),
11320                                     -- BA: BUG3272873
11321                                     source_code = nvl(v_wlji_source_code(v_idx),wdj.source_code),
11322                                     source_line_id = nvl(v_wlji_src_line_id(v_idx),wdj.source_line_id),
11323                                     overcompletion_tolerance_type = nvl(v_wlji_overcompl_tol_type(v_idx),
11324                                                         wdj.overcompletion_tolerance_type),
11325                                     overcompletion_tolerance_value = nvl(v_wlji_overcompl_tol_value(v_idx),
11326                                                         wdj.overcompletion_tolerance_value),
11327                                     priority = nvl(v_wlji_priority(v_idx),wdj.priority),
11328                                     due_date = nvl(v_wlji_due_date(v_idx),wdj.due_date),
11329                                     attribute_category = nvl(v_wlji_attribute_category(v_idx),wdj.attribute_category),
11330                                     attribute1 = nvl(v_wlji_attribute1(v_idx),wdj.attribute1),
11331                                     attribute2 = nvl(v_wlji_attribute2(v_idx),wdj.attribute2),
11332                                     attribute3 = nvl(v_wlji_attribute3(v_idx),wdj.attribute3),
11333                                     attribute4 = nvl(v_wlji_attribute4(v_idx),wdj.attribute4),
11334                                     attribute5 = nvl(v_wlji_attribute5(v_idx),wdj.attribute5),
11335                                     attribute6 = nvl(v_wlji_attribute6(v_idx),wdj.attribute6),
11336                                     attribute7 = nvl(v_wlji_attribute7(v_idx),wdj.attribute7),
11337                                     attribute8 = nvl(v_wlji_attribute8(v_idx),wdj.attribute8),
11338                                     attribute9 = nvl(v_wlji_attribute9(v_idx),wdj.attribute9),
11339                                     attribute10 = nvl(v_wlji_attribute10(v_idx),wdj.attribute10),
11340                                     attribute11 = nvl(v_wlji_attribute11(v_idx),wdj.attribute11),
11341                                     attribute12 = nvl(v_wlji_attribute12(v_idx),wdj.attribute12),
11342                                     attribute13 = nvl(v_wlji_attribute13(v_idx),wdj.attribute13),
11343                                     attribute14 = nvl(v_wlji_attribute14(v_idx),wdj.attribute14),
11344                                     attribute15 = nvl(v_wlji_attribute15(v_idx),wdj.attribute15),
11345                                     -- EA: BUG3272873
11346                                     est_scrap_account           = nvl(l_est_scrap_account, wdj.est_scrap_account),
11347                                     est_scrap_var_account       = nvl(l_est_scrap_var_account, wdj.est_scrap_var_account),
11348                                     description                 = nvl(RTRIM(v_wlji_description(v_idx)), wdj.description)
11349                                     -- CZHDBG: how about fusd and lucd ???
11350                             where   wdj.wip_entity_id     = v_wlji_wip_entity_id(v_idx);
11351 
11352                             end if; -- update jobs for which status is not cancelled
11353                             if lbji_debug = 'Y' then
11354                                 fnd_file.put_line(fnd_file.log,
11355                                     'Updated '||SQL%ROWCOUNT||' rows of wdj');
11356                             end if;
11357 l_stmt_num := 970.2;
11358                             UPDATE WIP_ENTITIES WE
11359                             set    description            = nvl(v_wlji_description(v_idx), we.description),
11360 			           wip_entity_name        = decode(nvl(v_wlji_status_type(v_idx),-1),7,wip_entity_name,
11361                                                                        decode(l_cur_status_type,1, v_wlji_job_name(v_idx),wip_entity_name)), --fix for bug5990761
11362                                    last_updated_by        = v_wlji_last_updt_by(v_idx),
11363                                    last_update_login      = v_wlji_last_updt_login(v_idx),
11364                                    request_id             = v_wlji_request_id(v_idx),
11365                                    program_application_id = v_wlji_program_appl_id(v_idx),
11366                                    program_id             = v_wlji_program_id(v_idx),
11367                                    program_update_date    = v_wlji_prog_updt_date(v_idx),
11368                                    last_update_date       = v_wlji_last_updt_date(v_idx)
11369                             where  we.wip_entity_id       = v_wlji_wip_entity_id(v_idx);
11370                             if lbji_debug = 'Y' then
11371                                 fnd_file.put_line(fnd_file.log,
11372                                     'Updated '||SQL%ROWCOUNT||' rows into we');
11373                             end if;
11374 
11375                         Exception
11376                            when others then
11377                            l_error_code := SQLCODE;
11378                            l_error_msg  :=
11379                                 'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
11380                                ||SUBSTR(SQLERRM, 1,1000);
11381                            handle_error(l_error_code, l_error_msg,  l_stmt_num);
11382                            l_error_count := l_error_count + 1;
11383                            GOTO abort_this_job;
11384                         End;
11385 
11386                         ---------------------------------------------
11387                         -- Mark recommendations on the job level copy
11388                         ---------------------------------------------
11389                         -- this is for update only
11390                         if(v_wlji_num_of_children(v_idx) > 0) then
11391 l_stmt_num := 980;
11392                             import_lot_job_details (
11393                                 p_wip_entity_id             => v_wlji_wip_entity_id(v_idx),
11394                                 p_org_id                    => v_wlji_org_id(v_idx),
11395                                 p_wo_records_exist          => 'Y',            -- p_wo_records_exist
11396                                 p_parent_header_id          => v_wlji_header_id(v_idx),
11397                                 p_job_scheduled_start_date  => null,           -- p_job_scheduled_start_date
11398                                 p_job_scheduled_compl_date  => null,           -- p_job_scheduled_compl_date
11399                                 p_job_scheduled_quantity    => null,
11400                                 p_group_id                  => null,
11401                                 p_last_updt_date            => v_wlji_last_updt_date(v_idx),
11402                                 p_last_updt_by              => v_wlji_last_updt_by(v_idx),
11403                                 p_last_updt_login           => v_wlji_last_updt_login(v_idx),
11404                                 p_creation_date             => v_wlji_creation_date(v_idx),
11405                                 p_created_by                => v_wlji_created_by(v_idx),
11406                                 p_request_id                => v_wlji_request_id(v_idx),
11407                                 p_program_appl_id           => v_wlji_program_appl_id(v_idx),
11408                                 p_program_id                => v_wlji_program_id(v_idx),
11409                                 p_prog_updt_date            => v_wlji_prog_updt_date(v_idx),
11410                                 p_error_code                => l_error_code,
11411                                 p_error_msg                 => l_error_msg);
11412                             if (l_error_code <> 0) then
11413                                 handle_error(l_error_code, l_error_msg,  l_stmt_num);
11414                                 l_error_count := l_error_count + 1;
11415                                 GOTO abort_this_job;
11416                             end if;
11417                         end if;
11418 
11419 l_stmt_num := 985;
11420                         -- BA 3479666
11421                         -- OSFM will call infinite scheduler if scheduling_method = 1
11422                         -- and on_rec_path = Yes
11423 
11424                         if(v_wlji_scheduling_method(v_idx) = 1 ) then
11425                             DECLARE
11426                                 l_returnstatus      varchar2(10);
11427                                 l_on_reco_path      varchar2(10);
11428                                 l_start_date        date := null;
11429                                 l_end_date          date := null;
11430 
11431                             BEGIN
11432                                 select on_rec_path
11433                                 into   l_on_reco_path
11434                                 from   wsm_lot_based_jobs
11435                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
11436 l_stmt_num := 985.1;
11437                                 if(l_on_reco_path = 'Y' ) then
11438                                     if(l_scheduling_mode = WIP_CONSTANTS.FORWARDS) then
11439                                         l_start_date := v_wlji_fusd(v_idx);
11440                                         l_end_date   := null;
11441                                     else
11442                                         l_start_date := null;
11443                                         l_end_date   := v_wlji_lucd(v_idx);
11444                                     end if;
11445 
11446                                     wsm_infinite_scheduler_pvt.schedule (
11447                                             p_initmsglist   => fnd_api.g_true,
11448                                             p_enddebug      => fnd_api.g_true,
11449                                             p_orgID         => v_wlji_org_id(v_idx),
11450                                             p_wipEntityID   => v_wlji_wip_entity_id(v_idx),
11451                                             p_scheduleMode  => l_scheduling_mode,
11452                                             p_startDate     => l_start_date,
11453                                             p_endDate       => l_end_date,
11454                                             p_opSeqNum      => null,
11455                                             p_resSeqNum     => null,
11456                                             x_returnstatus  => l_returnstatus,
11457                                             x_errorMsg      => l_error_msg
11458                                     );
11459                                     if(l_returnstatus <> fnd_api.g_ret_sts_success) then
11460                                         handle_error(-1, l_error_msg,  l_stmt_num);
11461                                         l_error_count := l_error_count + 1;
11462                                         goto abort_this_job;
11463                                     end if;
11464                                 end if;
11465                             END;
11466                         end if;
11467                         -- EA 3479666
11468 
11469                     end if; -- load_type 6
11470                     -- ----------------------------------
11471                     -- }}} End: Write for job update
11472                     -- ----------------------------------
11473 
11474 l_stmt_num := 990;
11475                     -- {{ Releasing job, insert period balance
11476                     if (v_wlji_load_type(v_idx) = 6 AND
11477                         v_wlji_status_type(v_idx) = WIP_CONSTANTS.RELEASED and
11478                         l_cur_status_type = WIP_CONSTANTS.UNRELEASED)
11479                     then
11480 
11481                         --
11482                         -- Begin R12Dev Update to set the current_job/rtg_op_seq_num on WLBJ
11483                         --Bug 4748165
11484                       /*  update wsm_lot_based_jobs
11485                         set    (current_job_op_seq_num,
11486                                 current_rtg_op_seq_num)
11487                                =(select wo.operation_seq_num,
11488                                          wo.wsm_op_seq_num
11489                                   from   wip_operations wo
11490                                   where  organization_id = v_wlji_org_id(v_idx)
11491                                   and    wip_entity_id   = v_wlji_wip_entity_id(v_idx)
11492                                   and    wo.quantity_in_queue
11493                                        + wo.quantity_running
11494                                        + wo.quantity_waiting_to_move  > 0)
11495                         where  wip_entity_id   = v_wlji_wip_entity_id(v_idx)
11496                         and    organization_id = v_wlji_org_id(v_idx); */
11497                         --Commented out above query since WSMPVCPY updates both. Bug 4748165
11498                         -- End R12Dev
11499                         --
11500 
11501 
11502                         if ((v_wlji_load_type(v_idx) = 6) AND (v_wlji_class_code(v_idx) is NULL)) then
11503                             v_wlji_class_code(v_idx) := l_cur_class_code;
11504                         end if;
11505                         fnd_file.put_line(fnd_file.log, 'Date Released: '||v_wlji_date_released(v_idx));
11506                         insert_into_period_balances (
11507                             p_wip_entity_id   => v_wlji_wip_entity_id(v_idx),
11508                             p_organization_id => v_wlji_org_id(v_idx),
11509                             p_class_code      => v_wlji_class_code(v_idx),
11510                             p_release_date    => v_wlji_date_released(v_idx),
11511                             p_error_code      => l_error_code,
11512                             p_err_msg         => l_error_msg);
11513 
11514                         if l_error_code <> 0 then
11515                             handle_error(l_error_code, l_error_msg,  l_stmt_num);
11516                             l_error_count := l_error_count + 1;
11517                             GOTO abort_this_job;
11518                         end if;
11519                     end if;
11520                     -- }} Releasing job, insert period balance
11521 
11522             /* LotAttr */
11523                     if l_cur_status_type in (0,1,3,6) THEN  -- Initialized Value,
11524                                         --Unreleased, released, Hold
11525 l_stmt_num := 995;
11526                         IF (lbji_debug='Y') THEN
11527                             fnd_file.put_line(fnd_file.log, 'Before Calling WSM_LotAttr_PVT.create_update_lotattr');
11528                         END IF;
11529                         WSM_LotAttr_PVT.create_update_lotattr(
11530                                 x_err_code          => l_error_code,
11531                                 x_err_msg           => l_error_msg,
11532                                 p_wip_entity_id     => v_wlji_wip_entity_id(v_idx),
11533                                 p_org_id            => v_wlji_org_id(v_idx),
11534                                 p_intf_txn_id       => v_wlji_header_id(v_idx),
11535                                 p_intf_src_code     => 'WSM',
11536                                 p_src_lot_number    => l_src_lot_number,
11537                                 p_src_inv_item_id   => l_src_inv_item_id);
11538                         IF (l_error_code <> 0) THEN
11539                             handle_error(l_error_code, l_error_msg,  l_stmt_num);
11540                             l_error_count := l_error_count + 1;
11541                             GOTO abort_this_job;
11542                         END IF;
11543                         IF (lbji_debug='Y') THEN
11544                            fnd_file.put_line(fnd_file.log, 'WSM_LotAttr_PVT.create_update_lotattr returned Success');
11545                         END IF;
11546 
11547                     end if; -- l_cur_status_type in (1,3,6)
11548 
11549             -- Invoke the serial code...
11550             -- ST : Serial Support Project ...........................---------------------------------------------------------------
11551             DECLARE
11552 
11553             l_msg_count NUMBER := 0;
11554             l_return_status VARCHAR2(1);
11555             l_err_msg       VARCHAR2(2000);
11556             l_log_level     NUMBER := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
11557             l_param_tbl WSM_Log_PVT.param_tbl_type;
11558             BEGIN
11559 
11560             IF (lbji_debug='Y') THEN
11561                 fnd_file.put_line(fnd_file.log, 'Before WSM_Serial_support_PVT.LBJ_serial_intf_proc');
11562             END IF;
11563             l_stmt_num := 995.1;
11564 
11565             WSM_Serial_support_PVT.LBJ_serial_intf_proc( p_header_id         => v_wlji_header_id(v_idx),
11566                                      p_wip_entity_id         => v_wlji_wip_entity_id(v_idx),
11567                                      p_organization_id           => v_wlji_org_id(v_idx),
11568                                      p_inventory_item_id     => v_wlji_item_id(v_idx),
11569                                      x_return_status         => l_return_status,
11570                                      x_error_count       => l_msg_count,
11571                                      x_error_msg             => l_err_msg
11572                                     );
11573             l_stmt_num := 995.2;
11574 
11575             if l_return_status = FND_API.G_RET_STS_SUCCESS then
11576                 IF (lbji_debug='Y') THEN
11577                     fnd_file.put_line(fnd_file.log, 'WSM_Serial_support_PVT.LBJ_serial_intf_proc returned Success');
11578                 END IF;
11579                 ELSE
11580                 IF (lbji_debug='Y') THEN
11581                     fnd_file.put_line(fnd_file.log, 'WSM_Serial_support_PVT.LBJ_serial_intf_proc returned failure');
11582                 END IF;
11583 
11584                 IF (l_msg_count = 1)  THEN
11585                     handle_error(-1, l_err_msg,  l_stmt_num);
11586                 ELSIF (l_msg_count > 1)  THEN
11587                     FOR i IN 1..l_msg_count LOOP
11588                     l_err_msg := fnd_msg_pub.get( p_msg_index => l_msg_count - i + 1,
11589                                           p_encoded   => FND_API.G_FALSE
11590                                      );
11591                     handle_error(-1, l_err_msg,  l_stmt_num);
11592                     END LOOP;
11593                 ELSE
11594                     l_err_msg := 'WSM_Serial_support_PVT.LBJ_serial_intf_proc returned failure';
11595                     handle_error(-1, l_err_msg,  l_stmt_num);
11596                 END IF;
11597                 l_error_count := l_error_count + 1;
11598                 GOTO abort_this_job;
11599             END IF;
11600 
11601            END;
11602            --------------------------------------------------------------------------------------------------------------------
11603 
11604 -- ==============================================================================================
11605 -- END: WRITING INTO BASE TABLES }}}
11606 -- ==============================================================================================
11607 
11608 l_stmt_num := 1000;
11609                     ---------------------------------------------------
11610                     -- {{{ Begin: material transaction for mode 2 jobs
11611                     ---------------------------------------------------
11612                     if v_wlji_mode_flag(v_idx) = 2 then
11613                         IF WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_ORG_ACC_PERIODS is null OR
11614                            WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_ORG_ACC_PERIODS = 0
11615                         then
11616                             l_error_code := -1;
11617                             handle_errmsg_name('WIP', 'WIP_NO_ACCT_PERIOD', l_stmt_num);
11618                             GOTO skip_mat_trans;
11619                         end if;
11620                         if WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_STK_LOC_CNTRL is null then
11621                             WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_STK_LOC_CNTRL := 1;
11622                         end if;
11623 
11624 l_stmt_num := 1010;
11625                         Begin  -- material transaction
11626                             insert into mtl_material_transactions_temp(
11627                                 last_update_date,
11628                                 creation_date,
11629                                 last_updated_by,
11630                                 created_by,
11631                                 last_update_login,
11632                                 transaction_header_id,
11633                                 transaction_source_id,
11634                                 inventory_item_id,
11635                                 organization_id,
11636                                 revision,
11637                                 subinventory_code,
11638                                 locator_id,
11639                                 transaction_quantity,
11640                                 primary_quantity,
11641                                 transaction_uom,
11642                                 transaction_type_id,
11643                                 transaction_action_id,
11644                                 transaction_source_type_id,
11645                                 transaction_date,
11646                                 acct_period_id,
11647                                 source_code,
11648                                 source_line_id,
11649                                 wip_entity_type,
11650                                 negative_req_flag,
11651                                 operation_seq_num,
11652                                 wip_supply_type,
11653                                 wip_commit_flag,
11654                                 process_flag,
11655                                 posting_flag,
11656                                 transaction_temp_id
11657                             ) values (
11658                                 v_wlji_last_updt_date(v_idx),
11659                                 v_wlji_creation_date(v_idx),
11660                                 v_wlji_last_updt_by(v_idx),
11661                                 v_wlji_created_by(v_idx),
11662                                 v_wlji_last_updt_login(v_idx),
11663                                 l_mmtt_txn_hdr_id,
11664                                 v_wlji_wip_entity_id(v_idx),
11665                                 v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
11666                                 v_wlji_org_id(v_idx),
11667                                 v_wsli(v_wlji_src_line_id(v_idx)).revision,
11668                                 v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code,
11669                                 v_wsli(v_wlji_src_line_id(v_idx)).locator_id,
11670                                 -l_quantity_tobe_issued,
11671                                 -l_quantity_tobe_issued,
11672                                 v_wsli(v_wlji_src_line_id(v_idx)).primary_uom_code,
11673                                 35,       -- Transaction_Type_Id
11674                                 1,        -- Transaction_Action_Id
11675                                 5,        -- Transaction_Source_Type_Id
11676                                 SYSDATE,  -- Transaction_Date
11677                                 WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_ORG_ACC_PERIODS,
11678                                 'WSM',    -- Source_Code
11679                                 to_char(v_wlji_src_line_id(v_idx)),
11680                                 5,        -- Wip_Entity_Type
11681                                 1,        -- Neg Req Flag
11682                                 10,       -- Op Seq
11683                                 '',       -- Supply Type
11684                                 'N',      -- Wip_Commit_Flag
11685                                 'Y',      -- Process_Flag
11686                                 'Y',      -- Posting_Flag
11687                                 mtl_material_transactions_s.nextval
11688                             ) RETURNING transaction_temp_id into l_txn_tmp_hdr_id;
11689                             if lbji_debug = 'Y' then
11690                                 fnd_file.put_line(fnd_file.log,
11691                                     'Inserted '||SQL%ROWCOUNT||' rows into mmtt');
11692                             end if;
11693 
11694 l_stmt_num := 1020;
11695                             INSERT INTO MTL_TRANSACTION_LOTS_TEMP (
11696                                 transaction_temp_id,
11697                                 last_update_date,
11698                                 creation_date,
11699                                 last_updated_by,
11700                                 created_by,
11701                                 last_update_login,
11702                                 transaction_quantity,
11703                                 primary_quantity,
11704                                 lot_number
11705                             ) values (
11706                                 l_txn_tmp_hdr_id,
11707                                 v_wlji_last_updt_date(v_idx),
11708                                 v_wlji_creation_date(v_idx),
11709                                 v_wlji_last_updt_by(v_idx),
11710                                 v_wlji_created_by(v_idx),
11711                                 v_wlji_last_updt_login(v_idx),
11712                                 -l_quantity_tobe_issued,
11713                                 -l_quantity_tobe_issued,
11714                                 v_wsli(v_wlji_src_line_id(v_idx)).lot_number);
11715                             if lbji_debug = 'Y' then
11716                                 fnd_file.put_line(fnd_file.log,
11717                                     'Inserted '||SQL%ROWCOUNT||' rows into mtlt');
11718                             end if;
11719 
11720 l_stmt_num := 1030;
11721                             UPDATE WIP_REQUIREMENT_OPERATIONS
11722                             set    wip_supply_type = 1
11723                             where  wip_entity_id = v_wlji_wip_entity_id(v_idx)
11724                             and    operation_seq_num = 10
11725                             and    inventory_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id;
11726 
11727                         Exception  -- material transaction
11728                             when others then
11729                                 l_error_code := SQLCODE;
11730                                 l_error_msg  :=
11731                                     'WSM_LBJ_INTERFACE_PVT.launch_worker: stmt# '||l_stmt_num||' '
11732                                    ||SUBSTR(SQLERRM, 1,1000);
11733                                 handle_error(l_error_code, l_error_msg,  l_stmt_num);
11734                                 GOTO skip_mat_trans;
11735                         End; -- material transaction
11736 
11737 << skip_mat_trans >>
11738 
11739                         if l_error_code <> 0 then
11740                             l_error_count := l_error_count + 1;
11741                             GOTO abort_this_job;
11742                         end if;
11743 
11744                         l_atleast_one_row_in_mmtt := l_atleast_one_row_in_mmtt + 1;
11745 
11746                     end if; -- for mode 2
11747                     ---------------------------------------------------
11748                     -- }}} End: material transaction for mode 2 jobs
11749                     ---------------------------------------------------
11750 
11751                 EXCEPTION
11752                     when others then
11753                         l_error_code := SQLCODE;
11754                         l_error_msg  :=
11755                             'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
11756                            ||SUBSTR(SQLERRM, 1,1000);
11757                         handle_error(l_error_code, l_error_msg,  l_stmt_num);
11758                         l_error_count := l_error_count + 1;
11759                 END; -- }}} main block
11760 
11761 
11762 <<abort_this_job>>
11763 
11764 
11765 l_stmt_num := 1040;
11766                 -- {{ write into output file
11767                 -- note that this is a rudimentary piece of commentary on the job created,
11768                 -- or failed to create because there's no customer requirement on this.
11769                 if v_wlji_load_type(v_idx) = 5 then
11770 l_stmt_num := 1040.1;
11771 	-- changed from org_organization_definitions to MTL_PARAMETERS for bug 5051885
11772 	-- Performance issue on org_organization_definitions for full table scan on base tables
11773 	-- SQL id 16641913
11774 
11775                     select organization_code
11776                     into   l_org_code
11777                     from   MTL_PARAMETERS
11778                     where  organization_id = v_wlji_org_id(v_idx);
11779     -- bug 5051885
11780 l_stmt_num := 1040.2;
11781                     select meaning
11782                     into   l_job_type_code
11783                     from   mfg_lookups
11784                     where  lookup_type = 'WIP_DISCRETE_JOB'
11785                     and    lookup_code = v_wlji_job_type(v_idx);
11786 
11787 l_stmt_num := 1040.3;
11788                     begin
11789                         select unique(concatenated_segments)
11790                         into   l_assembly_name
11791                         from   mtl_system_items_kfv
11792                         where  inventory_item_id = v_wlji_item_id(v_idx)
11793                         and    organization_id = v_wlji_org_id(v_idx);
11794                     exception
11795                         when others then
11796                             l_assembly_name := 'Unknown';
11797                     end;
11798                     fnd_file.put_line(fnd_file.output, '-------------------------------------------------------');
11799                     fnd_file.put_line(fnd_file.output, '                 Lot Based Job Creation                ');
11800                     fnd_file.put_line(fnd_file.output, '-------------------------------------------------------');
11801                     fnd_file.put_line(fnd_file.output, '   Organization: ' || l_org_code);
11802                     fnd_file.put_line(fnd_file.output, '       Job_name: ' || v_wlji_job_name(v_idx));
11803                     fnd_file.put_line(fnd_file.output, '       Job Type: ' || l_job_type_code);
11804                     fnd_file.put_line(fnd_file.output, '       Assembly: ' || l_assembly_name);
11805                     fnd_file.put_line(fnd_file.output, '       Quantity: ' || v_wlji_start_quantity(v_idx));
11806                     fnd_file.put_line(fnd_file.output, '     Start Date: ' || v_wlji_fusd(v_idx));
11807                     fnd_file.put_line(fnd_file.output, 'Completion Date: ' || v_wlji_lucd(v_idx));
11808                     fnd_file.put_line(fnd_file.output, '    Kanban Card: ' || v_wlji_kanban_card_id(v_idx));
11809                     if v_wlji_process_status(v_idx) <> 3 then
11810                         fnd_file.put_line(fnd_file.output, 'Process Status: Success');
11811                     else
11812                         fnd_file.put_line(fnd_file.output, 'Process Status: Falied To Create');
11813                     end if;
11814                     if v_wlji_process_status(v_idx) = 3 then
11815                         fnd_file.put_line(fnd_file.output, 'Error Message: ' || v_wlji_err_msg(v_idx));
11816                     end if;
11817                     fnd_file.put_line(fnd_file.output, '-------------------------------------------------------');
11818                 end if; -- load type 5
11819                 -- }} write into output file
11820 
11821 
11822                 -- mark the rows without error to be deleted
11823                 if v_wlji_err_code(v_idx) <> 0 then
11824                     rollback to savepoint_skip_job;
11825                 end if;
11826 
11827 l_stmt_num := 1050;
11828                 if v_wlji_process_status(v_idx) <> 3 then
11829                     v_wlji_process_status(v_idx) := 5; -- 5 : complete without error
11830                     if lbji_debug = 'Y' then
11831                         fnd_file.put_line(fnd_file.log,
11832                             'Everything OK, changing the status of the row to 5..');
11833                     end if;
11834                 else
11835                     -- {{ kanban begin
11836                     if v_wlji_kanban_card_id(v_idx) is not null then
11837                         inv_kanban_pvt.Update_Card_Supply_Status(
11838                                 x_return_status  => l_return_status,
11839                                 p_Kanban_Card_Id => v_wlji_kanban_card_id(v_idx),
11840                                 p_Supply_Status  => inv_kanban_pvt.g_supply_status_Exception);
11841 
11842                         if ( l_return_status <> fnd_api.g_ret_sts_success ) then
11843                             l_error_code := -1;
11844                             fnd_message.set_name('WSM', 'WSM_KNBN_CARD_STS_FAIL');
11845                             select meaning
11846                             into   l_meaning
11847                             from   mfg_lookups
11848                             where  lookup_type = 'MTL_KANBAN_SUPPLY_STATUS'
11849                             and    lookup_code = 7
11850                             and    upper(enabled_flag) = 'Y';
11851 
11852                             fnd_message.set_token('STATUS',l_meaning);
11853                             l_error_msg := fnd_message.get;
11854                             handle_error(l_error_code, l_error_msg,  l_stmt_num);
11855                             l_error_count := l_error_count + 1;
11856                         end if;
11857 
11858                         update wip_discrete_jobs
11859                         set    kanban_card_id = null
11860                         where  wip_entity_id =  v_wlji_wip_entity_id(v_idx);
11861 
11862                     end if;
11863                     -- }} kanban end
11864 
11865                     dummy_err_code := 0;
11866                     dummy_err_msg := NULL;
11867                     wsmputil.WRITE_TO_WIE (
11868                         v_wlji_header_id(v_idx),
11869                         substr(v_wlji_err_msg(v_idx),1,2000),
11870                         v_wlji_request_id(v_idx),
11871                         v_wlji_program_id(v_idx),
11872                         v_wlji_program_appl_id(v_idx),
11873                         1,
11874                         dummy_err_code,
11875                         dummy_err_msg );
11876 
11877                     if dummy_err_code <> 0 then
11878                         fnd_file.put_line(fnd_file.log, 'WARNING ');
11879                         fnd_file.put_line(fnd_file.log,
11880                             'WSM_LBJ_INTERFACE_PVT.launch_worker: '||dummy_err_msg);
11881                         l_error_count := l_error_count + 1;
11882                     end if;
11883                 end if;
11884 
11885                 v_idx := v_wlji_header_id.next(v_idx);
11886 
11887             end loop; -- }}} inner loop
11888 
11889 -- ==============================================================================================
11890 -- END: Processing of data }}}
11891 -- ==============================================================================================
11892 
11893             -- RETCODE return values
11894             --      0: success
11895             --      1: success with warning
11896             --      2: error
11897             -- RETCODE return values
11898 
11899             if l_warning_count <> 0 then
11900                 retcode := 1;
11901                 errbuf := 'The interface process produced at least one warning message';
11902                 fnd_file.put_line(fnd_file.log,errbuf);
11903                 conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
11904             end if;
11905 
11906             if l_error_count <> 0 then
11907                 retcode := 1;
11908                 errbuf := 'The interface process marked at least one row as errored';
11909                 fnd_file.put_line(fnd_file.log,errbuf);
11910                 conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
11911             end if;
11912 
11913 l_stmt_num := 1060;
11914             -- bulk update wsm_lot_job_interface
11915             forall i in v_wlji_process_status.first..v_wlji_process_status.last
11916             update  wsm_lot_job_interface
11917             set     process_status = v_wlji_process_status(i),
11918                     error_code     = v_wlji_err_code(i),
11919                     error_msg      = v_wlji_err_msg(i),
11920                     request_id     = v_wlji_request_id(i),
11921                     program_id     = v_wlji_program_id(i),
11922                     program_application_id = v_wlji_program_appl_id(i)
11923             where   header_id = v_wlji_header_id(i);
11924 
11925         -- ST : Serial Support Project ...........................----------------------------------------------
11926         -- bulk update WSM_SERIAL_TXN_INTERFACE
11927         forall i in v_wlji_process_status.first..v_wlji_process_status.last
11928             update  WSM_SERIAL_TXN_INTERFACE
11929             set     process_status = v_wlji_process_status(i),
11930                     error_message  = v_wlji_err_msg(i),
11931                     request_id     = v_wlji_request_id(i),
11932                     program_id     = v_wlji_program_id(i),
11933                     program_application_id = v_wlji_program_appl_id(i)
11934             where   header_id = v_wlji_header_id(i);
11935         -- ST : Serial Support Project ...........................----------------------------------------------
11936 
11937 l_stmt_num := 1060.1;
11938 
11939             forall i in v_wlji_process_status.first..v_wlji_process_status.last
11940             update  wsm_lot_job_dtl_interface
11941             set     process_status = v_wlji_process_status(i),
11942                     error_code     = v_wlji_err_code(i),
11943                     error_msg      = decode(v_wlji_process_status(i),   -- BUG3600450
11944                                             3, 'See header (header_id=' ||
11945                                                   v_wlji_header_id(i) || ') for error message',
11946                                             null),
11947                     request_id     = v_wlji_request_id(i),
11948                     program_id     = v_wlji_program_id(i),
11949                     program_application_id = v_wlji_program_appl_id(i)
11950             where   parent_header_id = v_wlji_header_id(i)
11951             and     v_wlji_num_of_children(i) > 0;
11952 
11953 
11954 l_stmt_num := 1070;
11955             -- {{ launch INV worker
11956             if l_atleast_one_row_in_mmtt <>0 THEN
11957                 fnd_file.put_line(fnd_file.log,
11958                     'Invoking Inventory API with header id: '||to_char(l_mmtt_txn_hdr_id));
11959                 --OPTII-PERF:Call Inv API to reduce the waiting time.
11960                 /*
11961                 l_inv_worker_req_id := FND_REQUEST.submit_request (
11962                                     'INV', 'INCTCW', NULL, NULL, FALSE,
11963                                     to_char(l_mmtt_txn_hdr_id),
11964                                     --'1', NULL, NULL); -- bug 3733798
11965                     '4', NULL, NULL);   -- bug 3733798
11966 
11967                 COMMIT; -- CZHDBG: why?
11968 
11969                 fnd_file.put_line(fnd_file.log,
11970                     'Material Transaction temp_header_id is '||to_char(l_mmtt_txn_hdr_id));
11971 
11972                 if l_inv_worker_req_id = 0 then
11973 l_stmt_num := 1080;
11974                     retcode := 1;
11975                     errbuf:= 'Inventory worker returned failure '||
11976                              '(Transaction_header_id=' ||l_mmtt_txn_hdr_id||') : '
11977                              || SUBSTRB(SQLERRM,1,1000);
11978                     fnd_file.put_line(fnd_file.log,errbuf);
11979                     conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
11980                     update  wsm_lot_job_interface
11981                     set     process_status = 4,
11982                             error_code = -2,
11983                             error_msg = l_error_msg
11984                     where   mode_flag = 2;
11985 
11986                 else -- req_id <> 0
11987 l_stmt_num := 1090;
11988                     fnd_file.put_line(fnd_file.log,'Inventory Transaction Worker request_id is '
11989                             ||to_char(l_inv_worker_req_id));
11990                     req_wait := FND_CONCURRENT.WAIT_FOR_REQUEST (
11991                                 request_id => l_inv_worker_req_id,
11992                                 interval   => 10, -- 10 seconds interval
11993                                 max_wait   => 36000, -- 10 Hours maximum wait.
11994                                 phase      => req_phase,
11995                                 status     => req_status,
11996                                 dev_phase  => req_devphase,
11997                                 dev_status => req_devstatus,
11998                                 message    => req_message);
11999 
12000                     fnd_file.put_line(fnd_file.log,
12001                         'Inventory Transaction Worker status is '||req_status);
12002                     fnd_file.put_line(fnd_file.log,
12003                         'Inventory Transaction Worker Completion Message: '||req_message);
12004 
12005                     if  req_devphase <> 'COMPLETE' OR  req_devstatus <> 'NORMAL' THEN
12006                         retcode := 1;
12007                         errbuf:= 'Inventory worker returned failure '||
12008                                  '(Transaction_header_id=' ||l_mmtt_txn_hdr_id||') : '||
12009                                  SUBSTRB(SQLERRM,1,1000);
12010                         fnd_file.put_line(fnd_file.log,errbuf);
12011                         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12012                     end if;
12013 
12014                     check_errored_mmtt_records(l_mmtt_txn_hdr_id, l_error_code, l_error_msg);
12015 
12016                     if (l_error_code <> 0) or (l_error_msg is not null ) then
12017                         retcode := 1;
12018                         errbuf:= 'Errored Records in mmtt ' ||
12019                                  '(Transaction_header_id=' ||l_mmtt_txn_hdr_id||') : '||
12020                                  SUBSTRB(SQLERRM,1,1000);
12021                         fnd_file.put_line(fnd_file.log,errbuf);
12022                         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12023                     end if;
12024 
12025                 end if;  -- req_id
12026                 */
12027                 retcode := INV_LPN_TRX_PUB.PROCESS_LPN_TRX(
12028                                      p_trx_hdr_id =>l_mmtt_txn_hdr_id,
12029                                      p_commit => 'F',
12030                                      x_proc_msg => req_message,
12031                                      p_proc_mode => 1,
12032                                      p_process_trx => 'T',
12033                                      p_atomic => 'T',
12034                                      p_business_flow_code =>NULL);
12035                 IF retcode <> 0 THEN
12036                    declare
12037                        x_msgcnt NUMBER;
12038                        x_msg_data VARCHAR2(256);
12039                    begin
12040 
12041                      FND_MSG_PUB.Count_And_Get(
12042                         p_encoded => 'T',
12043                         p_count => x_msgcnt,
12044                         p_data  => x_msg_data);
12045 
12046                        IF x_msgcnt > 0 THEN
12047                              -- There are messages in the stack so display them
12048                              for  x in 1..x_msgcnt LOOP
12049                                x_msg_data  := fnd_msg_pub.get(x, 'F');
12050                                fnd_file.put_line(fnd_file.log,'Inventory API Error at statement number ' || l_stmt_num || ' : ' || x_msg_data);
12051                              end loop;
12052                        ELSE
12053                            fnd_message.set_name(
12054                                                     application => 'INV',
12055                                                     name        => 'INV_PROCESS_FAILED');
12056                            fnd_message.set_token(
12057                                                     token    => 'PROCESS',
12058                                                     value    => 'TRXNS',
12059                                                     translate => TRUE);
12060                            x_msg_data := fnd_message.get;
12061                            fnd_file.put_line(fnd_file.log,'Inventory API Error at statement number ' || l_stmt_num || ' : ' || x_msg_data);
12062                        END IF;
12063                    end;
12064 
12065                 END IF;--End of check on retcode
12066 
12067 
12068             end if; -- l_atleast_one_row_in_mmtt
12069             -- }} launch INV worker
12070 
12071             -- delete marked rows (without error rows) from wlji and wljdi
12072             Begin
12073                 -- bug 3126758
12074                 l_del_int_prof_value := fnd_profile.value('WSM_INTERFACE_HISTORY_DAYS');
12075 
12076                 if l_atleast_one_row_in_mmtt <> 0 then
12077 l_stmt_num := 1100;
12078                     delete from wsm_starting_lots_interface
12079                     where  header_id IN (
12080                             select  wlji.source_line_id
12081                             from    wsm_lot_job_interface wlji
12082                             where   wlji.process_status = 5
12083                             --and     wlji.group_id = l_batch_grp_id
12084                             and     NVL(transaction_date, creation_date)
12085                                         <= decode(l_del_int_prof_value,
12086                                                   null,
12087                                                   NVL(transaction_date, creation_date) -1,
12088                                                   SYSDATE-l_del_int_prof_value)
12089                            );
12090                     if lbji_debug = 'Y' then
12091                         fnd_file.put_line(fnd_file.log,
12092                             'Deleted '||SQL%ROWCOUNT||' rows from wsli');
12093                     end if;
12094                 end if;
12095 
12096 l_stmt_num := 1100.1;
12097                 delete from wsm_lot_job_dtl_interface
12098                 where  parent_header_id IN (
12099                         select  wlji.header_id
12100                         from    wsm_lot_job_interface wlji
12101                         where   wlji.process_status = 5
12102                         --and     wlji.group_id = l_batch_grp_id
12103                         and     NVL(transaction_date, creation_date)
12104                                     <= decode(l_del_int_prof_value,
12105                                               null,
12106                                               NVL(transaction_date, creation_date) -1,
12107                                               SYSDATE-l_del_int_prof_value)
12108                        );
12109 l_stmt_num := 1100.2;
12110 
12111         -- ST : Serial Support Project ...........................----------------------------------------------
12112         -- Delete successful rows from WSM_SERIAL_TXN_INTERFACE
12113         delete from WSM_SERIAL_TXN_INTERFACE
12114         where header_id IN
12115                 (select  wlji.header_id
12116                          from    wsm_lot_job_interface wlji
12117                          where   wlji.process_status = 5
12118                          and     NVL(transaction_date, creation_date)
12119                                     <= decode(l_del_int_prof_value,
12120                                               null,
12121                                               NVL(transaction_date, creation_date) -1,
12122                                               SYSDATE-l_del_int_prof_value)
12123                        );
12124         -- ST : Serial Support Project ...........................----------------------------------------------
12125 
12126 l_stmt_num := 1100.25;
12127 
12128                 delete  from wsm_lot_job_interface
12129                 where   process_status = 5
12130                 --and     group_id = l_batch_grp_id
12131                 and     NVL(transaction_date, creation_date)
12132                             <= decode(l_del_int_prof_value,
12133                                       null,
12134                                       NVL(transaction_date, creation_date) -1,
12135                                       SYSDATE-l_del_int_prof_value);
12136                 if lbji_debug = 'Y' then
12137                     fnd_file.put_line(fnd_file.log,
12138                         'Deleted '||SQL%ROWCOUNT||' rows from wlji');
12139                 end if;
12140 
12141 l_stmt_num := 1100.3;
12142                 -- this is for those without header
12143                 delete  from wsm_lot_job_dtl_interface
12144                 where   process_status = 5
12145                 --and     group_id = l_batch_grp_id
12146                 and     NVL(transaction_date, creation_date)
12147                             <= decode(l_del_int_prof_value,
12148                                       null,
12149                                       NVL(transaction_date, creation_date) -1,
12150                                       SYSDATE-l_del_int_prof_value);
12151                 if lbji_debug = 'Y' then
12152                     fnd_file.put_line(fnd_file.log,
12153                         'Deleted '||SQL%ROWCOUNT||' rows from wljdi');
12154                 end if;
12155 
12156             Exception
12157                 when others then
12158                     retcode := 1;
12159                     errbuf := 'Deletion of successful rows from interface table(s) failed';
12160                     fnd_file.put_line(fnd_file.log,errbuf);
12161                     conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12162             End;
12163 
12164             COMMIT; -- commit a batch
12165 
12166         else    -- }} if csr_wlji%rowcount - prev_rowcount <> 0, one batch
12167             exit;   -- exit the outer loop
12168         end if;
12169 
12170         prev_rowcount := csr_wlji%rowcount;
12171 
12172     END LOOP; -- }}} outer loop
12173     close csr_wlji;
12174 
12175 
12176 -- ==============================================================================================
12177 -- BEGIN: handle WLJDI records with parent_header_id = NULL {{{
12178 -- ==============================================================================================
12179     -- reset savepoint savepoint_top since all the records with header are done.
12180     SAVEPOINT savepoint_top;
12181 
12182     DECLARE
12183         l_v_d_we_id_indp    t_number;
12184         l_v_d_org_id_indp   t_number;
12185         l_d_jobs            number := 0;
12186         l_v_d_we_id_err     t_number;
12187         l_v_d_org_id_err    t_number;
12188         l_v_d_emsg_err      t_errmsg;
12189         l_v_d_ecode_err     t_number;
12190         l_d_idx_err         number := 0;
12191         l_v_d_we_id_ok      t_number;
12192         l_v_d_org_id_ok     t_number;
12193         l_d_idx_ok          number := 0;
12194     BEGIN
12195 l_stmt_num := 1200;
12196         open c_wljdi_jobs_no_parent;
12197         fetch c_wljdi_jobs_no_parent bulk collect into
12198                 l_v_d_we_id_indp,
12199                 l_v_d_org_id_indp;
12200         l_d_jobs := c_wljdi_jobs_no_parent%rowcount;
12201         close c_wljdi_jobs_no_parent;
12202 
12203         if(l_d_jobs > 0) then
12204             if lbji_debug = 'Y' then
12205                 fnd_file.new_line(fnd_file.log, 2);
12206                 fnd_file.put_line(fnd_file.log,
12207                     'Handling WLJDI records with parent_header_id = NULL...');
12208             end if;
12209 
12210             l_d := l_v_d_org_id_indp.first;
12211             l_d_idx_err := 1;
12212             l_d_idx_ok := 1;
12213             while (l_d IS NOT NULL)
12214             loop
12215 
12216                 SAVEPOINT savepoint_skip_job2;
12217 l_stmt_num := 1210;
12218                 import_lot_job_details(
12219                     p_wip_entity_id             => l_v_d_we_id_indp(l_d),
12220                     p_org_id                    => l_v_d_org_id_indp(l_d),
12221                     p_wo_records_exist          => 'Y',            -- p_wo_records_exist
12222                     p_parent_header_id          => null,
12223                     p_job_scheduled_start_date  => null,           -- p_job_scheduled_start_date
12224                     p_job_scheduled_compl_date  => null,           -- p_job_scheduled_compl_date
12225                     p_job_scheduled_quantity    => null,
12226                     p_group_id                  => p_group_id,
12227                     p_last_updt_date            => sysdate,
12228                     p_last_updt_by              => fnd_global.user_id,
12229                     p_last_updt_login           => fnd_global.login_id,
12230                     p_creation_date             => sysdate,
12231                     p_created_by                => fnd_global.user_id,
12232                     p_request_id                => fnd_global.conc_request_id,
12233                     p_program_appl_id           => fnd_global.prog_appl_id,
12234                     p_program_id                => fnd_global.conc_program_id,
12235                     p_prog_updt_date            => sysdate,
12236                     p_error_code                => l_error_code,
12237                     p_error_msg                 => l_error_msg);
12238                 if (l_error_code <> 0) then
12239                     l_error_count := l_error_count + 1;
12240                     l_v_d_we_id_err(l_d_idx_err)  := l_v_d_we_id_indp(l_d);
12241                     l_v_d_org_id_err(l_d_idx_err) := l_v_d_org_id_indp(l_d);
12242                     l_v_d_ecode_err(l_d_idx_err)  := l_error_code;
12243                     l_v_d_emsg_err(l_d_idx_err)   := l_error_msg;
12244                     l_d_idx_err := l_d_idx_err +1;
12245                     ROLLBACK to savepoint_skip_job2;
12246                 else
12247                     l_v_d_we_id_ok(l_d_idx_ok)   := l_v_d_we_id_indp(l_d);
12248                     l_v_d_org_id_ok(l_d_idx_ok)  := l_v_d_org_id_indp(l_d);
12249                     l_d_idx_ok := l_d_idx_ok +1;
12250                     COMMIT;     -- commit each job
12251                 end if;
12252 
12253                 l_d := l_v_d_org_id_indp.next(l_d);
12254             end loop;
12255 
12256 l_stmt_num := 1220;
12257             if(l_d_idx_err > 1) then
12258                 forall i in l_v_d_we_id_err.first..l_v_d_we_id_err.last
12259                     update  wsm_lot_job_dtl_interface
12260                     set     process_status = 3,
12261                             error_code     = l_v_d_ecode_err(i),
12262                             error_msg      = l_v_d_emsg_err(i),
12263                             request_id     = fnd_global.conc_request_id,
12264                             program_id     = fnd_global.conc_program_id,
12265                             program_application_id = fnd_global.prog_appl_id
12266                     where   parent_header_id IS NULL
12267                     and     process_status = WIP_CONSTANTS.RUNNING
12268                     and     wip_entity_id = l_v_d_we_id_err(i)
12269                     and     organization_id = l_v_d_org_id_err(i);
12270 
12271                     errbuf := 'The interface process marked at least one row as errored';
12272                     fnd_file.put_line(fnd_file.log,errbuf);
12273                     conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12274 
12275             end if;
12276 l_stmt_num := 1230;
12277             if(l_d_idx_ok > 1) then
12278                 forall i in l_v_d_we_id_ok.first..l_v_d_we_id_ok.last
12279                     delete  from wsm_lot_job_dtl_interface
12280                     where   parent_header_id IS NULL
12281                     and     process_status = WIP_CONSTANTS.RUNNING
12282                     and     wip_entity_id = l_v_d_we_id_ok(i)
12283                     and     organization_id = l_v_d_org_id_ok(i);
12284             end if;
12285         end if; -- l_d_jobs > 0
12286     END;
12287 
12288 -- ==============================================================================================
12289 -- END: handle WLJDI records with parent_header_id = NULL }}}
12290 -- ==============================================================================================
12291 
12292 
12293 l_stmt_num := 1300;
12294     -- {{ osp begin, launch import requisition
12295     if g_num_of_osp_exists <> 0 then
12296 
12297         /*Added Code to set MOAC parameter for bug 5485653  */
12298 
12299         select to_number(ORG_INFORMATION3) into l_ou_id
12300         from HR_ORGANIZATION_INFORMATION
12301         where ORGANIZATION_ID = l_mo_org_id
12302         and ORG_INFORMATION_CONTEXT = l_org_acct_ctxt;
12303 
12304         FND_REQUEST.SET_ORG_ID (l_ou_id);
12305 
12306         l_req_request_id := fnd_request.submit_request(
12307                 'PO', 'REQIMPORT', NULL, NULL, FALSE,'WIP', NULL, 'ITEM',
12308                 NULL,'N', 'Y', chr(0), NULL, NULL, NULL,
12309                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12310                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12311                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12312                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12313                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12314                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12315                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12316                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12317                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
12318         );
12319 
12320         fnd_file.put_line(fnd_file.log,'Concurrent Request for Requisition Inport Submitted');
12321         fnd_file.put_line(fnd_file.log,'Request_id: '||l_req_request_id);
12322     end if;
12323     -- }} osp end
12324 
12325 l_stmt_num := 1310;
12326     -- phantom project
12327     delete from bom_explosion_temp
12328     where  group_id = wsmpwrot.explosion_group_id;
12329     wsmpwrot.explosion_group_id   := null;
12330     wsmpwrot.use_phantom_routings := null;
12331 
12332     if l_error_count = 0 then
12333         retcode := 0;
12334         errbuf := '';
12335         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('SUCCESS',errbuf);
12336     end if;
12337 
12338     COMMIT;
12339 
12340 
12341 EXCEPTION  -- for launch_worker
12342     when e_abort_request then
12343         rollback to savepoint_top;
12344         retcode := 2;
12345         errbuf := 'WSM_LBJ_INTERFACE_PVT.launch_worker (#'||l_stmt_num||') '
12346                   || SUBSTR(SQLERRM, 1,240);
12347         fnd_file.put_line(fnd_file.log,errbuf);
12348         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',errbuf);
12349         wsmputil.WRITE_TO_WIE (
12350             0,
12351             substr(errbuf,1,2000),
12352             fnd_global.conc_request_id,
12353             fnd_global.conc_program_id,
12354             fnd_global.prog_appl_id,
12355             1,
12356             dummy_err_code,
12357             dummy_err_msg );
12358         COMMIT;
12359 
12360     when others then
12361         rollback to savepoint_top;
12362         retcode := 2;
12363         errbuf := 'WSM_LBJ_INTERFACE_PVT.launch_worker (#'||l_stmt_num||') '
12364                   || SUBSTR(SQLERRM, 1,240);
12365         fnd_file.put_line(fnd_file.log,errbuf);
12366         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',errbuf);
12367 
12368         wsmputil.WRITE_TO_WIE (
12369             0,
12370             substr(errbuf,1,2000),
12371             fnd_global.conc_request_id,
12372             fnd_global.conc_program_id,
12373             fnd_global.prog_appl_id,
12374             1,
12375             dummy_err_code,
12376             dummy_err_msg );
12377         COMMIT;
12378 END launch_worker;
12379 
12380 
12381 
12382 -- **********************************************************************************************
12383 -- PROCEDURE build_job_header_info
12384 -- **********************************************************************************************
12385 
12386 -------------------------------------------------------------------------------------------------
12387 --
12388 -- Note that a new wip_entity_id will be generated
12389 --
12390 -- p_src_client_server: 1     => this procedure is being called from client side,
12391 --                      other => server side call.
12392 --
12393 -- p_po_creation_time: => pass the value of po_creation_time in wip_parameters for the org
12394 -------------------------------------------------------------------------------------------------
12395 
12396 --
12397 -- R12Dev: for secondary quantity
12398 -- Overload the procedure
12399 --
12400 
12401 procedure build_job_header_info(
12402         p_common_routing_sequence_id    in number,
12403         p_common_bill_sequence_id       in number,
12404         p_status_type                   in number,
12405         p_class_code                    in varchar2,
12406         p_org_id                        in number,
12407         p_wip_entity_id                 in out nocopy number,
12408         p_last_updt_date                in date,
12409         p_last_updt_by                  in number,
12410         p_creation_date                 in date,
12411         p_created_by                    in number,
12412         p_last_updt_login               in number,
12413         p_request_id                    in number,
12414         p_program_appl_id               in number,
12415         p_program_id                    in number,
12416         p_prog_updt_date                in date,
12417         p_source_line_id                in number,
12418         p_source_code                   in varchar2,
12419         p_description                   in varchar2,
12420         p_item                          in number,
12421         p_job_type                      in number,
12422         p_bom_reference_id              in number,
12423         p_routing_reference_id          in number,
12424         p_firm_planned_flag             in number,
12425         p_wip_supply_type               in number,
12426         p_job_scheduled_start_date      in date,
12427         p_job_scheduled_compl_date      in date,
12428         p_start_quantity                in number,
12429         p_net_quantity                  in number,
12430         p_coproducts_supply             in number,
12431         p_bom_revision                  in varchar2,
12432         p_routing_revision              in varchar2,
12433         p_bom_revision_date             in date,
12434         p_routing_revision_date         in date,
12435         p_lot_number                    in varchar2,
12436         p_alt_bom_designator            in varchar2,
12437         p_alt_routing_designator        in varchar2,
12438         p_priority                      in number,
12439         p_due_date                      in date,
12440         p_attribute_category            in varchar2,
12441         p_attribute1                    in varchar2,
12442         p_attribute2                    in varchar2,
12443         p_attribute3                    in varchar2,
12444         p_attribute4                    in varchar2,
12445         p_attribute5                    in varchar2,
12446         p_attribute6                    in varchar2,
12447         p_attribute7                    in varchar2,
12448         p_attribute8                    in varchar2,
12449         p_attribute9                    in varchar2,
12450         p_attribute10                   in varchar2,
12451         p_attribute11                   in varchar2,
12452         p_attribute12                   in varchar2,
12453         p_attribute13                   in varchar2,
12454         p_attribute14                   in varchar2,
12455         p_attribute15                   in varchar2,
12456         p_job_name                      in varchar2,
12457         p_completion_subinventory       in varchar2,
12458         p_completion_locator_id         in number,
12459         p_demand_class                  in varchar2,
12460         p_project_id                    in number,
12461         p_task_id                       in number,
12462         p_schedule_group_id             in number,
12463         p_build_sequence                in number,
12464         p_line_id                       in number,
12465         p_kanban_card_id                in number,
12466         p_overcompl_tol_type            in number,
12467         p_overcompl_tol_value           in number,
12468         p_end_item_unit_number          in number,
12469         p_src_client_server             in number,
12470         p_po_creation_time              in number,
12471         p_date_released                 in date,
12472         p_error_code                    out nocopy number,
12473         p_error_msg                     out nocopy varchar2) is
12474 
12475 l_wjsq_uom_code         tbl_wjsq_uom_code;
12476 l_wjsq_start_quantity   tbl_wjsq_start_quantity;
12477 
12478 BEGIN
12479 
12480     l_wjsq_uom_code.delete();
12481     l_wjsq_start_quantity.delete();
12482 
12483     build_job_header_info(
12484         p_common_routing_sequence_id   =>  p_common_routing_sequence_id    ,
12485         p_common_bill_sequence_id      =>  p_common_bill_sequence_id       ,
12486         p_status_type                  =>  p_status_type                   ,
12487         p_class_code                   =>  p_class_code                    ,
12488         p_org_id                       =>  p_org_id                        ,
12489         p_wip_entity_id                =>  p_wip_entity_id                 ,
12490         p_last_updt_date               =>  p_last_updt_date                ,
12491         p_last_updt_by                 =>  p_last_updt_by                  ,
12492         p_creation_date                =>  p_creation_date                 ,
12493         p_created_by                   =>  p_created_by                    ,
12494         p_last_updt_login              =>  p_last_updt_login               ,
12495         p_request_id                   =>  p_request_id                    ,
12496         p_program_appl_id              =>  p_program_appl_id               ,
12497         p_program_id                   =>  p_program_id                    ,
12498         p_prog_updt_date               =>  p_prog_updt_date                ,
12499         p_source_line_id               =>  p_source_line_id                ,
12500         p_source_code                  =>  p_source_code                   ,
12501         p_description                  =>  p_description                   ,
12502         p_item                         =>  p_item                          ,
12503         p_job_type                     =>  p_job_type                      ,
12504         p_bom_reference_id             =>  p_bom_reference_id              ,
12505         p_routing_reference_id         =>  p_routing_reference_id          ,
12506         p_firm_planned_flag            =>  p_firm_planned_flag             ,
12507         p_wip_supply_type              =>  p_wip_supply_type               ,
12508         p_job_scheduled_start_date     =>  p_job_scheduled_start_date      ,
12509         p_job_scheduled_compl_date     =>  p_job_scheduled_compl_date      ,
12510         p_start_quantity               =>  p_start_quantity                ,
12511         p_net_quantity                 =>  p_net_quantity                  ,
12512         p_coproducts_supply            =>  p_coproducts_supply             ,
12513         p_bom_revision                 =>  p_bom_revision                  ,
12514         p_routing_revision             =>  p_routing_revision              ,
12515         p_bom_revision_date            =>  p_bom_revision_date             ,
12516         p_routing_revision_date        =>  p_routing_revision_date         ,
12517         p_lot_number                   =>  p_lot_number                    ,
12518         p_alt_bom_designator           =>  p_alt_bom_designator            ,
12519         p_alt_routing_designator       =>  p_alt_routing_designator        ,
12520         p_priority                     =>  p_priority                      ,
12521         p_due_date                     =>  p_due_date                      ,
12522         p_attribute_category           =>  p_attribute_category            ,
12523         p_attribute1                   =>  p_attribute1                    ,
12524         p_attribute2                   =>  p_attribute2                    ,
12525         p_attribute3                   =>  p_attribute3                    ,
12526         p_attribute4                   =>  p_attribute4                    ,
12527         p_attribute5                   =>  p_attribute5                    ,
12528         p_attribute6                   =>  p_attribute6                    ,
12529         p_attribute7                   =>  p_attribute7                    ,
12530         p_attribute8                   =>  p_attribute8                    ,
12531         p_attribute9                   =>  p_attribute9                    ,
12532         p_attribute10                  =>  p_attribute10                   ,
12533         p_attribute11                  =>  p_attribute11                   ,
12534         p_attribute12                  =>  p_attribute12                   ,
12535         p_attribute13                  =>  p_attribute13                   ,
12536         p_attribute14                  =>  p_attribute14                   ,
12537         p_attribute15                  =>  p_attribute15                   ,
12538         p_job_name                     =>  p_job_name                      ,
12539         p_completion_subinventory      =>  p_completion_subinventory       ,
12540         p_completion_locator_id        =>  p_completion_locator_id         ,
12541         p_demand_class                 =>  p_demand_class                  ,
12542         p_project_id                   =>  p_project_id                    ,
12543         p_task_id                      =>  p_task_id                       ,
12544         p_schedule_group_id            =>  p_schedule_group_id             ,
12545         p_build_sequence               =>  p_build_sequence                ,
12546         p_line_id                      =>  p_line_id                       ,
12547         p_kanban_card_id               =>  p_kanban_card_id                ,
12548         p_overcompl_tol_type           =>  p_overcompl_tol_type            ,
12549         p_overcompl_tol_value          =>  p_overcompl_tol_value           ,
12550         p_end_item_unit_number         =>  p_end_item_unit_number          ,
12551         p_src_client_server            =>  p_src_client_server             ,
12552         p_po_creation_time             =>  p_po_creation_time              ,
12553         p_date_released                =>  p_date_released                 ,
12554         p_wjsq_uom_code                =>  l_wjsq_uom_code                 ,
12555         p_wjsq_start_quantity          =>  l_wjsq_start_quantity           ,
12556         p_error_code                   =>  p_error_code                    ,
12557         p_error_msg                    =>  p_error_msg                     );
12558 
12559 END build_job_header_info;
12560 
12561 
12562 
12563 -- **********************************************************************************************
12564 -- PROCEDURE build_job_copy_info
12565 -- **********************************************************************************************
12566 
12567 procedure build_job_copy_info (
12568         p_common_routing_sequence_id    in number,
12569         p_common_bill_sequence_id       in number,
12570         p_org_id                        in number,
12571         p_wip_entity_id                 in number,
12572         p_last_updt_date                in date,
12573         p_last_updt_by                  in number,
12574         p_creation_date                 in date,
12575         p_created_by                    in number,
12576         p_last_updt_login               in number,
12577         p_request_id                    in number,
12578         p_program_appl_id               in number,
12579         p_program_id                    in number,
12580         p_prog_updt_date                in date,
12581         p_item                          in number,
12582         p_bom_reference_id              in number,
12583         p_routing_reference_id          in number,
12584         p_wip_supply_type               in number,
12585         p_job_scheduled_start_date      in date,
12586         p_job_scheduled_compl_date      in date,
12587         p_start_quantity                in number,
12588         p_bom_revision_date             in date,
12589         p_routing_revision_date         in date,
12590         p_alt_bom_designator            in varchar2,
12591         p_alt_routing_designator        in varchar2,
12592         p_header_id                     in number,      -- header_id in WLJI, pass null if N/A
12593         p_num_of_children               in number,      -- number of children in WLJDI
12594         p_infinite_schedule             in varchar2,    -- call infinite scheduler or not
12595         p_error_code                    out nocopy number,
12596         p_error_msg                     out nocopy varchar2) is
12597 
12598 l_stmt_num              number;
12599 e_build_job_dtl         exception;
12600 
12601 l_sch_mode              number  := null;
12602 l_sch_date              date    := null;
12603 
12604 l_scheduled_start_date  date;
12605 l_scheduled_compl_date  date;
12606 
12607 l_bill_sequence_id      number; -- BUG: 3341350/3286849
12608 l_bill_item_id          number;
12609 l_common_bill_seq_id    number; -- BUG: 3981616
12610 
12611 l_op_seq_id             NUMBER; --Bug 5104459
12612 
12613 BEGIN
12614     p_error_code := 0;
12615     p_error_msg := '';
12616 
12617     l_scheduled_start_date := p_job_scheduled_start_date;
12618     l_scheduled_compl_date := p_job_scheduled_compl_date;
12619 
12620     if(p_infinite_schedule = 'Y') then
12621         if(p_job_scheduled_start_date IS NULL) then
12622             l_sch_mode := WIP_CONSTANTS.BACKWARDS;
12623             l_sch_date := p_job_scheduled_compl_date;
12624         else
12625             l_sch_mode := WIP_CONSTANTS.FORWARDS;
12626             l_sch_date := p_job_scheduled_start_date;
12627         end if;
12628         -- force import_lot_job_details to get scheduled_start_date
12629         -- and scheduled_compl_date from WDJ in such case
12630         l_scheduled_start_date := null;
12631         l_scheduled_compl_date := null;
12632     end if;
12633 
12634     -- BA: BUG: 3341350/3286849
12635 l_stmt_num := 5;
12636     l_bill_sequence_id := WSMPUTIL.GET_JOB_BOM_SEQ_ID(p_wip_entity_id);
12637     if(l_bill_sequence_id = -1) then
12638         p_error_msg  := 'Fail to get bill sequence id (wip_entity_id='||p_wip_entity_id ||')';
12639         p_error_code := -1;
12640         raise e_build_job_dtl;
12641     end if;
12642     -- EA: BUG: 3341350/3286849
12643 
12644    -- BA: BUG 3981616 l_common_bill_seq_id is set to be null if p_common_bill_sequence_id = 0
12645    if(p_common_bill_sequence_id = 0) then
12646       l_common_bill_seq_id := NULL;
12647    else
12648       l_common_bill_seq_id := p_common_bill_sequence_id;
12649    end if;
12650    -- EA: BUG 3981616
12651 
12652     if(p_routing_reference_id IS NULL) then
12653         l_bill_item_id := p_item;
12654     else
12655         l_bill_item_id := p_bom_reference_id;
12656     end if;
12657 
12658 l_stmt_num := 10;
12659     -- Create the job level copy
12660     --OPTII-PERF:Find if phantom exists or not.
12661     begin
12662          select 1 into v_phantom_exists
12663          from  bom_inventory_components
12664          where bill_sequence_id = l_common_bill_seq_id
12665          and   p_bom_revision_date between effectivity_date and nvl(disable_date,p_bom_revision_date+1)
12666          and   wip_supply_type = 6
12667          and   rownum = 1;
12668 
12669          v_phantom_exists := 1;
12670 
12671     exception
12672          when others then
12673             v_phantom_exists := 2;
12674 
12675     end;
12676 
12677     if v_insert_wip = 1 then
12678        --if  v_phantom_exists = 1 then
12679        if  v_phantom_exists = 1 then
12680            v_insert_wip := 2;
12681        end if;
12682        --If job dtls are populated wo should be populated after the
12683        -- copy tables are updated with the data from dtls table.
12684        if  p_num_of_children > 0 then
12685            v_insert_wip := 2;
12686        end if;
12687     end if;
12688     --Start changes for Bug 5104459
12689     l_stmt_num := 11;
12690 
12691     wsmputil.FIND_ROUTING_START(
12692                             p_common_routing_sequence_id,
12693                             p_routing_revision_date,
12694                             l_op_seq_id,
12695                             p_error_code,
12696                             p_error_msg);
12697 
12698      if p_error_code <> 0 then
12699                     raise e_build_job_dtl;
12700      end if;
12701 
12702      l_stmt_num := 12;
12703 
12704      wsmputil.FIND_ROUTING_END(
12705                             p_common_routing_sequence_id,
12706                             p_routing_revision_date,
12707                             l_op_seq_id,
12708                             p_error_code,
12709                             p_error_msg);
12710 
12711      if p_error_code <> 0 then
12712               raise e_build_job_dtl;
12713      end if;
12714 
12715      --End changes for Bug 5104459
12716 
12717     wsm_jobcopies_pvt.create_jobcopies (
12718             x_err_buf               => p_error_msg,
12719             x_err_code              => p_error_code,
12720             p_wip_entity_id         => p_wip_entity_id,
12721             p_org_id                => p_org_id,
12722             p_primary_item_id       => p_item,
12723             p_routing_item_id       => nvl(p_routing_reference_id, p_item),      -- newly added ??
12724             p_alt_rtg_desig         => p_alt_routing_designator,    -- newly added
12725             p_rtg_seq_id            => null,                        -- newly added, not used in API
12726             p_common_rtg_seq_id     => p_common_routing_sequence_id,
12727             p_rtg_rev_date          => p_routing_revision_date,
12728             p_bill_item_id          => l_bill_item_id,              -- newly added ??
12729             p_alt_bom_desig         => p_alt_bom_designator,
12730             p_bill_seq_id           => l_bill_sequence_id,          -- newly added
12731             --p_common_bill_seq_id  => p_common_bill_sequence_id,   -- bug 3981616
12732             p_common_bill_seq_id    => l_common_bill_seq_id,        -- bug 3981616
12733             p_bom_rev_date          => p_bom_revision_date,
12734             p_wip_supply_type       => p_wip_supply_type,
12735             p_last_update_date      => p_last_updt_date,
12736             p_last_updated_by       => p_last_updt_by,
12737             p_last_update_login     => p_last_updt_login,
12738             p_creation_date         => p_creation_date,
12739             p_created_by            => p_created_by,
12740             p_request_id            => p_request_id,
12741             p_program_app_id        => p_program_appl_id,
12742             p_program_id            => p_program_id,
12743             p_program_update_date   => p_prog_updt_date,
12744             p_inf_sch_flag          => p_infinite_schedule,
12745             p_inf_sch_mode          => l_sch_mode,
12746             p_inf_sch_date          => l_sch_date,
12747             --OPTII-PERF:Following new arguments are added.
12748             p_new_job               => v_new_job,
12749             p_insert_wip            => v_insert_wip,
12750             p_phantom_exists        => v_phantom_exists,
12751             p_charges_exist         => 2);
12752 
12753             v_new_job := NULL;
12754             --v_insert_wip := NULL;
12755             --v_phantom_exists := NULL;
12756     -- BC: bug 3400858/3400858
12757     --if (p_error_code <> 0) then
12758 
12759     --if (p_error_code <> 0) OR (p_error_code IS NOT NULL) OR -- No error
12760     --    (p_error_code <> -1)    -- Warning
12761     --then
12762     --    raise e_build_job_dtl;
12763     --end if;
12764 
12765     if (p_error_code = 0) or
12766         (p_error_code is null) or -- no error
12767         (p_error_code = -1)    -- warning
12768     then
12769         p_error_code := 0;
12770         p_error_msg := null;
12771     else
12772         raise e_build_job_dtl;
12773     end if;
12774     -- EC: bug 3303267/3400858
12775 
12776     if lbji_debug = 'Y' then
12777         fnd_file.put_line(fnd_file.log, 'Make job-level BOM/RTG copy ...');
12778     end if;
12779 
12780 l_stmt_num := 20;
12781     -- Mark recommendations on the job level copy
12782     if(p_num_of_children > 0) then
12783         import_lot_job_details (
12784             p_wip_entity_id             => p_wip_entity_id,
12785             p_org_id                    => p_org_id,
12786             p_wo_records_exist          => 'N',                -- p_wo_records_exist
12787             p_parent_header_id          => p_header_id,
12788             p_job_scheduled_start_date  => l_scheduled_start_date,
12789             p_job_scheduled_compl_date  => l_scheduled_compl_date,
12790             p_job_scheduled_quantity    => p_start_quantity,
12791             p_group_id                  => null,
12792             p_last_updt_date            => p_last_updt_date,
12793             p_last_updt_by              => p_last_updt_by,
12794             p_last_updt_login           => p_last_updt_login,
12795             p_creation_date             => p_creation_date,
12796             p_created_by                => p_created_by,
12797             p_request_id                => p_request_id,
12798             p_program_appl_id           => p_program_appl_id,
12799             p_program_id                => p_program_id,
12800             p_prog_updt_date            => p_prog_updt_date,
12801             p_error_code                => p_error_code,
12802             p_error_msg                 => p_error_msg);
12803         if (p_error_code <> 0) then
12804             raise e_build_job_dtl;
12805         end if;
12806     end if;
12807 
12808 EXCEPTION
12809     when e_build_job_dtl then
12810         -- p_error_code/p_error_msg was set
12811         fnd_file.put_line(fnd_file.log,
12812             substr('build_job_copy_info: (#'||l_stmt_num||') '
12813                    ||p_error_msg, 1, 2000));
12814 
12815     when others then
12816         p_error_code := SQLCODE;
12817         p_error_msg := substr('build_job_copy_info: (#'||l_stmt_num||') '
12818                        ||SQLERRM, 1, 2000);
12819         fnd_file.put_line(fnd_file.log, substr(p_error_msg, 1, 2000));
12820 END build_job_copy_info;
12821 
12822 
12823 -- **********************************************************************************************
12824 -- PROCEDURE build_job_detail_info
12825 -- **********************************************************************************************
12826 
12827 -------------------------------------------------------------------------------------------------
12828 -- For job creation, p_rtg_op_seq_num = rtg op_seq_num of the first operation or null
12829 -- For bouns, p_rtg_op_seq_num = rtg op_seq_num of the starting operation on 'the' path
12830 -- This procedure is only called during
12831 --     (1)job creation
12832 --     (2)change bom/rtg alt
12833 --     (3)change rbom/rtg reference
12834 -- in all such cases, WO records are considered as not available
12835 -------------------------------------------------------------------------------------------------
12836 
12837 procedure build_job_detail_info(
12838         p_common_routing_sequence_id    in number,
12839         p_common_bill_sequence_id       in number,
12840         p_status_type                   in number,
12841         p_org_id                        in number,
12842         p_wip_entity_id                 in number,
12843         p_last_updt_date                in date,
12844         p_last_updt_by                  in number,
12845         p_creation_date                 in date,
12846         p_created_by                    in number,
12847         p_last_updt_login               in number,
12848         p_request_id                    in number,
12849         p_program_appl_id               in number,
12850         p_program_id                    in number,
12851         p_prog_updt_date                in date,
12852         p_item                          in number,
12853         p_job_type                      in number,
12854         p_bom_reference_id              in number,
12855         p_routing_reference_id          in number,
12856         p_wip_supply_type               in number,
12857         p_job_scheduled_start_date      in date,        -- not used
12858         p_job_scheduled_compl_date      in date,        -- not used
12859         p_start_quantity                in number,
12860         p_bom_revision_date             in date,
12861         p_routing_revision_date         in date,
12862         p_alt_bom_designator            in varchar2,
12863         p_alt_routing_designator        in varchar2,
12864         p_rtg_op_seq_num                in number,
12865         p_error_code                    out nocopy number,
12866         p_error_msg                     out nocopy varchar2) is
12867 
12868 BEGIN
12869 
12870     build_job_detail_info(
12871             p_common_routing_sequence_id => p_common_routing_sequence_id,
12872             p_common_bill_sequence_id    => p_common_bill_sequence_id,
12873             p_status_type                => p_status_type,
12874             p_org_id                     => p_org_id,
12875             p_wip_entity_id              => p_wip_entity_id,
12876             p_last_updt_date             => p_last_updt_date,
12877             p_last_updt_by               => p_last_updt_by,
12878             p_creation_date              => p_creation_date,
12879             p_created_by                 => p_created_by,
12880             p_last_updt_login            => p_last_updt_login,
12881             p_request_id                 => p_request_id,
12882             p_program_appl_id            => p_program_appl_id,
12883             p_program_id                 => p_program_id,
12884             p_prog_updt_date             => p_prog_updt_date,
12885             p_item                       => p_item,
12886             p_job_type                   => p_job_type,
12887             p_bom_reference_id           => p_bom_reference_id,
12888             p_routing_reference_id       => p_routing_reference_id,
12889             p_wip_supply_type            => p_wip_supply_type,
12890             p_job_scheduled_start_date   => p_job_scheduled_start_date,
12891             p_job_scheduled_compl_date   => p_job_scheduled_compl_date,
12892             p_start_quantity             => p_start_quantity,
12893             p_bom_revision_date          => p_bom_revision_date,
12894             p_routing_revision_date      => p_routing_revision_date,
12895             p_alt_bom_designator         => p_alt_bom_designator,
12896             p_alt_routing_designator     => p_alt_routing_designator,
12897             p_rtg_op_seq_num             => p_rtg_op_seq_num,
12898             p_error_code                 => p_error_code,
12899             p_error_msg                  => p_error_msg,
12900             p_src_client_server          => 0,
12901             p_po_creation_time           => wip_constants.manual_creation); -- default is not to create PO
12902 
12903 END build_job_detail_info;
12904 
12905 
12906 /*******************************************************************************
12907 
12908    Procedure to check if a resource is costed.....
12909 
12910 ********************************************************************************/
12911 
12912 Procedure check_resource_costed (p_wip_entity_id     in         number,
12913                      p_org_id            in         number,
12914                  p_res_op_seq_num    in         number,
12915                  p_op_seq_num        in         number,
12916                  x_error_code        out nocopy number,
12917                  x_error_msg         out nocopy varchar2
12918                 )
12919                 is
12920 
12921 cursor c_lot_wcti_check is
12922       select 1
12923       from wip_cost_txn_interface
12924       where organization_id = p_org_id
12925       and wip_entity_id = p_wip_entity_id
12926       and operation_seq_num = p_op_seq_num
12927       and resource_seq_num = p_res_op_seq_num
12928       and rownum = 1;
12929 
12930 cursor c_lot_wt_check is
12931       select 1
12932       from wip_transactions
12933       where organization_id = p_org_id
12934       and wip_entity_id = p_wip_entity_id
12935       and operation_seq_num = p_op_seq_num
12936       and resource_seq_num = p_res_op_seq_num
12937       and rownum = 1;
12938 
12939 l_stmt_num number;
12940 begin
12941 
12942     x_error_code := 0;
12943     x_error_msg  := null;
12944 
12945     l_stmt_num := 10;
12946 
12947     for c_lot_wcti_check_rec in c_lot_wcti_check loop
12948         x_error_code := -1;  /* costed .. */
12949     end loop;
12950 
12951     l_stmt_num := 20;
12952 
12953     for c_lot_wt_check_rec in c_lot_wt_check loop
12954         x_error_code := -1; /* costed.... */
12955     end loop;
12956 
12957 exception
12958     when others then
12959         /* add the message also.... */
12960 
12961         x_error_code := -1;
12962         x_error_msg  := 'check_resource_costed : ' || l_stmt_num || ' : ' || SQLCODE || substrb(SQLERRM,1,1000);
12963         fnd_file.put_line(fnd_file.log,x_error_msg);
12964 
12965 end check_resource_costed;
12966 
12967 /*******************************************************************************************
12968 
12969     Procedure to delete the setup info of a resource that is being replaced or deleted...
12970 
12971 ********************************************************************************************/
12972 
12973 Procedure delete_setup_info(p_wip_entity_id     in         number,
12974                 p_org_id            in         number,
12975                 p_par_res_seq_num   in     number,
12976                 p_op_seq_num    in         number,
12977                 p_handle_curr_op    in         varchar2,
12978                 x_error_code        out nocopy number,
12979                 x_error_msg         out nocopy varchar2
12980                )
12981                is
12982 
12983 e_res_costed  exception;
12984 
12985 l_setup_exists NUMBER := 0;
12986 l_res_costed   number := 0;
12987 cursor c_setup_res(c_par_res_seq_num NUMBER) is
12988        select resource_id,resource_seq_num,operation_seq_num,applied_resource_units
12989        from wip_operation_resources wor
12990        where  wor.organization_id = p_org_id
12991        and    wor.wip_entity_id   = p_wip_entity_id
12992        and    wor.operation_seq_num   = p_op_seq_num
12993        and    wor.parent_resource_seq  = c_par_res_seq_num;
12994 
12995 l_stmt_num  number;
12996 
12997 begin
12998 
12999     /* open the cursor and loop through the setup resources and check if any of them is costed...
13000       if so return an error....
13001     */
13002     l_stmt_num := 10;
13003 
13004     l_res_costed := 0;
13005     l_setup_exists := 0;
13006 
13007     if (p_handle_curr_op = 'Y') then
13008         IF lbji_debug = 'Y' then
13009                 fnd_file.put_line(fnd_file.log,'delete setup info: WIP table change.... : ');
13010         END IF;
13011 
13012         l_stmt_num := 20;
13013 
13014         for l_setup_res in c_setup_res(p_par_res_seq_num) loop
13015 
13016             l_setup_exists := 1;
13017             /* call to check if costs exost for the particular setup resource... */
13018             if nvl(l_setup_res.applied_resource_units,0) <> 0 then
13019                 /* error out as costed.... */
13020                 l_stmt_num := 30;
13021                 raise e_res_costed;
13022             else
13023                 x_error_code := 0;
13024                 x_error_msg  := null;
13025 
13026                 l_res_costed := 0;
13027 
13028                 l_stmt_num := 35;
13029                 IF lbji_debug = 'Y' then
13030                         fnd_file.put_line(fnd_file.log,'delete setup info:call to check_resource_costed .... : '
13031                                   || ' Resource seq num : ' || l_setup_res.resource_seq_num
13032                                   || ' Operation seq num : ' || p_op_seq_num
13033                                   );
13034                 END IF;
13035 
13036                 check_resource_costed ( p_wip_entity_id     => p_wip_entity_id,
13037                             p_org_id            => p_org_id,
13038                             p_res_op_seq_num    => l_setup_res.resource_seq_num,
13039                             p_op_seq_num        => p_op_seq_num,
13040                             x_error_code        => x_error_code,
13041                             x_error_msg     => x_error_msg
13042                               );
13043 
13044                 l_stmt_num := 40;
13045 
13046                 if x_error_code <> 0 then
13047                     /* error out... */
13048                     l_res_costed := 1;
13049                     exit;
13050                 end if;
13051 
13052             end if;
13053         end loop;
13054 
13055         l_stmt_num := 50;
13056 
13057         if l_res_costed = 0 and l_setup_exists <> 0 then
13058 
13059             l_stmt_num := 55;
13060             /* delete the instance information ... */
13061             delete from wip_op_resource_instances wori
13062             where wori.organization_id  = p_org_id
13063             and    wori.wip_entity_id   = p_wip_entity_id
13064             and    wori.operation_seq_num  = p_op_seq_num
13065             and    wori.RESOURCE_SEQ_NUM  in (select resource_seq_num
13066                               from wip_operation_resources wor
13067                               where wor.organization_id = p_org_id
13068                               and    wor.wip_entity_id   = p_wip_entity_id
13069                               and    wor.operation_seq_num   = p_op_seq_num
13070                               and    wor.parent_resource_seq = p_par_res_seq_num
13071                               );
13072 
13073             l_stmt_num := 60;
13074 
13075             /* delete the usage information .... */
13076             delete from wip_operation_resource_usage woru
13077             where woru.organization_id  = p_org_id
13078             and    woru.wip_entity_id   = p_wip_entity_id
13079             and    woru.operation_seq_num  = p_op_seq_num
13080             and    woru.resource_seq_num  in (select resource_seq_num
13081                               from wip_operation_resources wor
13082                               where wor.organization_id = p_org_id
13083                               and    wor.wip_entity_id   = p_wip_entity_id
13084                               and    wor.operation_seq_num   = p_op_seq_num
13085                               and    wor.parent_resource_seq = p_par_res_seq_num
13086                               );
13087 
13088             l_stmt_num := 70;
13089 
13090             /* delete the setup resources.... */
13091             delete from wip_operation_resources wor
13092             where wor.organization_id = p_org_id
13093             and    wor.wip_entity_id   = p_wip_entity_id
13094             and    wor.operation_seq_num   = p_op_seq_num
13095             and    wor.parent_resource_seq = p_par_res_seq_num;
13096 
13097         elsif l_res_costed <> 0 then
13098             l_stmt_num := 80;
13099             raise e_res_costed;
13100         end if;
13101 
13102 
13103     else
13104 
13105         /* delete the instance information ... */
13106         l_stmt_num := 90;
13107 
13108         l_setup_exists := 0;
13109         select count(*)
13110         into   l_setup_exists
13111         from   wsm_copy_op_resources wcor
13112         where  wcor.organization_id = p_org_id
13113         and    wcor.wip_entity_id   = p_wip_entity_id
13114         and    wcor.operation_seq_num   = p_op_seq_num
13115         and    wcor.parent_resource_seq_num = p_par_res_seq_num
13116         and    rownum = 1;
13117 
13118         IF l_setup_exists <> 0 THEN
13119                 delete from wsm_copy_op_resource_instances wcori
13120                 where wcori.organization_id  = p_org_id
13121                 and    wcori.wip_entity_id   = p_wip_entity_id
13122                 and    wcori.operation_seq_num  = p_op_seq_num
13123                 and    wcori.resource_seq_num  in (select resource_seq_num
13124                                   from wsm_copy_op_resources wcor
13125                                   where wcor.organization_id = p_org_id
13126                                   and    wcor.wip_entity_id   = p_wip_entity_id
13127                                   and    wcor.operation_seq_num   = p_op_seq_num
13128                                   and    wcor.parent_resource_seq_num = p_par_res_seq_num
13129                                   );
13130                 /* delete the usage information .... */
13131                 l_stmt_num := 100;
13132                 delete from wsm_copy_op_resource_usage wcoru
13133                 where wcoru.organization_id  = p_org_id
13134                 and    wcoru.wip_entity_id   = p_wip_entity_id
13135                 and    wcoru.operation_seq_num  = p_op_seq_num
13136                 and    wcoru.resource_seq_num  in (select resource_seq_num
13137                                   from wsm_copy_op_resources wcor
13138                                   where wcor.organization_id = p_org_id
13139                                   and    wcor.wip_entity_id   = p_wip_entity_id
13140                                   and    wcor.operation_seq_num   = p_op_seq_num
13141                                   and    wcor.parent_resource_seq_num = p_par_res_seq_num
13142                                   );
13143                 /* delete the setup resources.... */
13144                 l_stmt_num := 110;
13145 
13146                 delete from wsm_copy_op_resources wcor
13147                 where wcor.organization_id = p_org_id
13148                 and    wcor.wip_entity_id   = p_wip_entity_id
13149                 and    wcor.operation_seq_num   = p_op_seq_num
13150                 and    wcor.parent_resource_seq_num = p_par_res_seq_num;
13151         END IF;
13152 
13153     end if;
13154 
13155     x_error_code := 0;
13156 
13157 exception
13158     when e_res_costed then
13159 
13160         fnd_message.set_name('WSM','WSM_OP_RES_COSTED');
13161         x_error_msg := fnd_message.get;
13162         x_error_code := -1;
13163         fnd_file.put_line(fnd_file.log,'delete_setup_info : ' || l_stmt_num || ' : ' || x_error_msg);
13164 
13165     when others then
13166         x_error_code := -1;
13167         x_error_msg  := 'check_resource_costed : ' || l_stmt_num || ' : ' || SQLCODE || substrb(SQLERRM,1,1000);
13168         fnd_file.put_line(fnd_file.log,x_error_msg);
13169 
13170 end delete_setup_info;
13171 
13172 /**************************************************************************************
13173 
13174 Procedure Added to validate the new resource + its setup resources being added (or)
13175 
13176  validate setup resources being added for a particular resource
13177 
13178 ***************************************************************************************/
13179 
13180 Procedure validate_new_resource(p_wip_entity_id     in               number,
13181                                 p_org_id            in               number,
13182                                 -- p_par_res_index  in               number,
13183                                 p_setup_index_tbl   in               t_number,
13184                                 p_res_recom         in               t_res_recom,
13185                                 p_handle_wip_cur_op in               varchar2,
13186                                 p_job_op_seq_num    in               number,
13187                                 p_rtg_op_seq_num    in               number,
13188                                 p_new_res_tbl       in  out nocopy   new_res_tbl,
13189                                 x_error_code        in  out nocopy   number,
13190                                 x_error_msg         in  out nocopy   varchar2
13191                                 ) is
13192 
13193 
13194 l_par_res_seq_num       number;
13195 l_exists                number;
13196 l_setup_exists          number;
13197 l_dummy                 number;
13198 l_batchable             number;
13199 l_field_name            varchar2(100);
13200 l_error_code            number;
13201 l_error_msg             varchar2(2000);
13202 l_stmt_num              number;
13203 l_index                 number;
13204 
13205 l_max_resource_seq_num  number;
13206 
13207 l_new_res_seq_tbl       t_number;
13208 
13209 l_res_id                number;
13210 l_res_recommended       varchar2(10);
13211 
13212 l_hash_value            number;
13213 l_hash_str              varchar2(500);
13214 
13215 l_res_num1              number;
13216 l_res_num2              number;
13217 
13218 l_res_auto_charge_type  number;
13219 
13220 l_hour_uom              varchar2(50);
13221 l_hour_uom_class        varchar2(200);
13222 l_uom_time_class_flag   boolean;
13223 l_uom_class             varchar2(10);
13224 l_autocharge_type       number;
13225 
13226 e_no_po_move_res        exception;
13227 e_invalid_res_seq_num   exception;
13228 e_invalid_par_seq       exception;
13229 e_invalid_res           exception;
13230 e_invalid_res_dates     exception;
13231 e_invalid_res_seq       exception;
13232 e_invalid_seq           exception;
13233 e_invalid_setup_id      exception;
13234 e_invalid_setup_code    exception;
13235 e_setup_costed          exception;
13236 
13237 e_invalid_field         exception;
13238 
13239 e_invalid_max_assg_units        exception;
13240 e_invalid_setup_max_assg_units  exception;
13241 e_invalid_batch_max_assg_units  exception;
13242 e_res_not_batchable             exception;
13243 
13244 cursor c_setup_res(c_par_res_seq_num NUMBER) is
13245        select resource_id,resource_seq_num,operation_seq_num
13246        from wip_operation_resources wor
13247        where  wor.organization_id = p_org_id
13248        and    wor.wip_entity_id   = p_wip_entity_id
13249        and    wor.operation_seq_num   = p_job_op_seq_num
13250        and    wor.parent_resource_seq  = c_par_res_seq_num;
13251 
13252 begin
13253     if p_handle_wip_cur_op = 'Y' then
13254 
13255         l_stmt_num := 10;
13256         /* get the maximum resource seq num.... for the current operation...*/
13257         begin
13258 
13259             select max(resource_seq_num)
13260             into   l_res_num1
13261             from   wip_operation_resources wor
13262             where  wor.organization_id = p_org_id
13263             and    wor.wip_entity_id   = p_wip_entity_id
13264             and    wor.operation_seq_num   = p_job_op_seq_num;
13265 
13266         exception
13267             when no_data_found then
13268                 null;
13269         end;
13270 
13271         /* get the parent resource seq num */
13272         l_par_res_seq_num := p_new_res_tbl(p_new_res_tbl.first).parent_seq_num;
13273 
13274         /* Indicates that there is no parent resource being added... ..... */
13275         /*
13276             Validations to be done...
13277             i)   check if any parent resource exists ( consider only WIP_OPERATION_RESOURCES )
13278             ii)  Check if any setup resources already exist....
13279             iii) Delete the setup resources...( if no cost incurred )
13280         */
13281 
13282         l_stmt_num := 20;
13283         begin
13284 
13285             l_exists := 0;
13286 
13287             select 1
13288             into   l_exists
13289             from   wip_operation_resources wor
13290             where  wor.organization_id = p_org_id
13291             and    wor.wip_entity_id   = p_wip_entity_id
13292             and    wor.operation_seq_num   = p_job_op_seq_num
13293             and    resource_seq_num    = l_par_res_seq_num;
13294 
13295         exception
13296             when no_data_found then
13297                 raise e_invalid_par_seq;
13298 
13299         end;
13300 
13301         l_stmt_num := 30;
13302         begin
13303             l_setup_exists := 0;
13304 
13305             select 1
13306             into   l_setup_exists
13307             from   wip_operation_resources wor
13308             where  wor.organization_id = p_org_id
13309             and    wor.wip_entity_id   = p_wip_entity_id
13310             and    wor.operation_seq_num   = p_job_op_seq_num
13311             and    wor.parent_resource_seq = l_par_res_seq_num
13312             and    rownum =1 ;
13313 
13314         exception
13315             when no_data_found then
13316                 l_setup_exists := 0;
13317         end;
13318 
13319         l_stmt_num := 40;
13320         IF lbji_debug = 'Y' THEN
13321                 fnd_file.put_line(fnd_file.log,'validate new resource : ' || l_stmt_num || ' l_setup_exists at current op ..... ' || l_setup_exists);
13322         END IF;
13323 
13324         if l_setup_exists = 1  then
13325             /* open the cursor and loop through the setup resources and check if any of them is costed...
13326             if so return an error....*/
13327 
13328             l_error_code := 0;
13329             l_error_msg  := null;
13330 
13331             l_stmt_num := 50;
13332             IF lbji_debug = 'Y' THEN
13333                 fnd_file.put_line(fnd_file.log,'validate new resource : ' || l_stmt_num || ' calling delete setup info ...' );
13334             END IF;
13335 
13336             delete_setup_info( p_wip_entity_id     => p_wip_entity_id,
13337                        p_org_id            => p_org_id,
13338                        p_par_res_seq_num   => l_par_res_seq_num,
13339                        p_op_seq_num        => p_job_op_seq_num,
13340                        p_handle_curr_op    => 'Y',
13341                        x_error_code        => l_error_code,
13342                        x_error_msg         => l_error_msg
13343                      );
13344 
13345             l_stmt_num := 60;
13346 
13347             if l_error_code <> 0 then
13348                 raise e_setup_costed;
13349             end if;
13350             l_stmt_num := 70;
13351 
13352             /* delete the setup resources.... */
13353             delete from wip_operation_resources wor
13354             where wor.organization_id = p_org_id
13355             and    wor.wip_entity_id   = p_wip_entity_id
13356             and    wor.operation_seq_num   = p_job_op_seq_num
13357             and    wor.parent_resource_seq = l_par_res_seq_num;
13358         end if;
13359 
13360         l_stmt_num := 75;
13361         begin
13362             select max(resource_seq_num)
13363             into   l_res_num2
13364             from   wip_sub_operation_resources wsor
13365             where  wsor.organization_id = p_org_id
13366             and    wsor.wip_entity_id   = p_wip_entity_id
13367             and    wsor.operation_seq_num   = p_job_op_seq_num;
13368 
13369         exception
13370             when no_data_found then
13371                 null;
13372         end;
13373 
13374         if l_res_num1 > l_res_num2 then
13375             l_max_resource_seq_num := l_res_num1;
13376         elsif l_res_num2 > l_res_num1 then
13377             l_max_resource_seq_num := l_res_num2;
13378         end if;
13379 
13380         l_max_resource_seq_num := nvl(l_max_resource_seq_num,10);
13381 
13382         l_stmt_num := 78;
13383 
13384         -- Code for debug purpose.... ----
13385         IF lbji_debug = 'Y' THEN
13386                 fnd_file.put_line(fnd_file.log,'validate new resource : ' || l_stmt_num || ' l_max_resource_seq_num ..... '|| l_max_resource_seq_num);
13387         END IF;
13388 
13389     else
13390         l_par_res_seq_num := p_new_res_tbl(p_new_res_tbl.first).parent_seq_num;
13391 
13392         /* Indicates that there is no parent resource being added... ..... */
13393         /*
13394             Validations to be done...
13395             i)   check if any parent resource exists
13396             ii)  Check if any setup resources already exist....
13397             iii) Delete the setup resources...( if no cost incurred )
13398         */
13399         l_stmt_num := 100;
13400 
13401         begin
13402 
13403             l_res_id := 0;
13404 
13405             select wcor.resource_id,wcor.recommended
13406             into   l_res_id,l_res_recommended
13407             from   wsm_copy_op_resources wcor
13408             where  wcor.organization_id = p_org_id
13409             and    wcor.wip_entity_id   = p_wip_entity_id
13410             and    wcor.operation_seq_num    = p_rtg_op_seq_num
13411             and    wcor.resource_seq_num     = l_par_res_seq_num;
13412 
13413             /* sh. i add cond to check if recommeded res.... */
13414 
13415             if l_res_recommended = 'Y' then
13416                 /* Ideally should check here if this resource is getting recommended...*/
13417                 l_hash_str := to_char(p_wip_entity_id) || ':' || to_char(p_rtg_op_seq_num) || ':' || to_char(l_res_id);
13418                 l_hash_value := dbms_utility.get_hash_value(l_hash_str,35,1048576);
13419 
13420                 if (p_res_recom.exists(l_hash_value) and p_res_recom(l_hash_value) = 'N') then
13421                     raise e_invalid_par_seq;
13422                 end if;
13423 
13424             elsif l_res_recommended = 'N' then
13425 
13426                 /* Ideally should check here if this resource is getting recommended...*/
13427                 l_hash_str := to_char(p_wip_entity_id) || ':' || to_char(p_rtg_op_seq_num) || ':' || to_char(l_res_id);
13428                 l_hash_value := dbms_utility.get_hash_value(l_hash_str,35,1048576);
13429 
13430                 if not(p_res_recom.exists(l_hash_value) and p_res_recom(l_hash_value) = 'Y') then
13431                     raise e_invalid_par_seq;
13432                 end if;
13433 
13434             else
13435                 raise e_invalid_par_seq;
13436             end if;
13437         exception
13438             when no_data_found then
13439 
13440                 raise e_invalid_par_seq;
13441 
13442         end;
13443 
13444         l_stmt_num := 110;
13445 
13446         l_error_code := 0;
13447         l_error_msg  := null;
13448         delete_setup_info( p_wip_entity_id     => p_wip_entity_id,
13449                    p_org_id            => p_org_id,
13450                    p_par_res_seq_num   => l_par_res_seq_num,
13451                    p_op_seq_num        => p_rtg_op_seq_num,
13452                    p_handle_curr_op    => 'N',
13453                    x_error_code        => l_error_code,
13454                    x_error_msg         => l_error_msg
13455                  );
13456 
13457         if l_error_code <> 0 then
13458             raise e_setup_costed;
13459         end if;
13460 
13461         l_stmt_num := 120;
13462 
13463         begin
13464             select max(resource_seq_num)
13465             into   l_max_resource_seq_num
13466             from   wsm_copy_op_resources wcor
13467             where  wcor.organization_id = p_org_id
13468             and    wcor.wip_entity_id   = p_wip_entity_id
13469             and    wcor.operation_seq_num   = p_rtg_op_seq_num;
13470 
13471         exception
13472             when no_data_found then
13473                 null;
13474         end;
13475 
13476         l_stmt_num := 122;
13477 
13478         l_max_resource_seq_num := nvl(l_max_resource_seq_num,10);
13479 
13480     end if;
13481 
13482     -- l_new_res_seq_tbl.delete;
13483     l_stmt_num := 125;
13484 
13485     /* get the maximum sequence number and add ten.... */
13486     /* Loop on each record... the validations will be common for all the parent resource and setup resource also....*/
13487     l_index := p_new_res_tbl.first;
13488 
13489     while l_index  is not null loop
13490         /* do all the validations here...... */
13491 
13492         /*  i)   resource seq num not nulll
13493             ii)  start date < completion date
13494             iii) validity of the resource... sh not be disabled..
13495             iv)  autocharge not null
13496             v)   UOM validation
13497             vi)  assigned units > 0
13498             vii) all DS validations....
13499         */
13500 
13501         l_stmt_num := 140.1;
13502         if p_new_res_tbl(l_index).res_seq_num is NULL then
13503 
13504             l_max_resource_seq_num := l_max_resource_seq_num + 10;
13505             p_new_res_tbl(l_index).res_seq_num := l_max_resource_seq_num;
13506 
13507             /* unfortunately have to update the global table also.... */
13508             v_wljdi_resource_seq_num(l_index) := l_max_resource_seq_num;
13509 
13510         elsif p_new_res_tbl(l_index).res_seq_num <=0 then
13511             /* error out... */
13512             l_field_name := 'res_seq_num -- invalid field value';
13513             raise e_invalid_field;
13514         end if;
13515 
13516         l_stmt_num := 140.2;
13517         if l_new_res_seq_tbl.exists(p_new_res_tbl(l_index).res_seq_num) then
13518             /* error out.... */
13519             l_field_name := 'res_seq_num -- duplicate';
13520             raise e_invalid_field;
13521         else
13522             l_new_res_seq_tbl(p_new_res_tbl(l_index).res_seq_num) := (p_new_res_tbl(l_index).res_seq_num);
13523         end if;
13524 
13525         if p_new_res_tbl(l_index).rpl_grp_num is NULL then
13526             /* error out */
13527             l_field_name := 'replacement_grp_num';
13528             raise e_invalid_field;
13529         end if;
13530         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
13531         -- Query up the fields from BR and then validate them...
13532         BEGIN
13533 
13534             select (BR.autocharge_type),
13535                    (BR.unit_of_measure),
13536                    nvl(p_new_res_tbl(l_index).basis_type,BR.default_basis_type),
13537                    uom.uom_class,
13538                    br.batchable,
13539                    nvl(BR.standard_rate_flag,2)
13540             into   p_new_res_tbl(l_index).autocharge_type,
13541                    p_new_res_tbl(l_index).uom_code,
13542                    p_new_res_tbl(l_index).basis_type,
13543                    l_uom_class,
13544                    l_batchable,
13545                    p_new_res_tbl(l_index).standard_rate_flag
13546             from bom_resources BR,
13547                  mtl_units_of_measure_vl uom
13548             where br.resource_id =  p_new_res_tbl(l_index).resource_id
13549             and autocharge_type = nvl(p_new_res_tbl(l_index).autocharge_type,autocharge_type)
13550             and nvl(p_new_res_tbl(l_index).uom_code,br.unit_of_measure) = br.unit_of_measure -- uom.uom_code
13551             and br.unit_of_measure = uom.uom_code
13552             and nvl(BR.standard_rate_flag,-1) = nvl(nvl(p_new_res_tbl(l_index).standard_rate_flag,BR.standard_rate_flag),-1);
13553 
13554         EXCEPTION
13555             WHEN NO_DATA_FOUND THEN
13556                 l_field_name := 'Resource Information';
13557                 raise e_invalid_field;
13558 
13559         END;
13560 
13561         if p_new_res_tbl(l_index).autocharge_type = 4 then
13562             raise e_no_po_move_res;
13563         end if;
13564 
13565 
13566         if nvl(p_new_res_tbl(l_index).assigned_units,-1) <= 0 then
13567             -- error out.... --
13568             l_field_name := 'assigned units';
13569             raise e_invalid_field;
13570         end if;
13571 
13572         if nvl(p_new_res_tbl(l_index).usage_rate_or_amount,-1) <=0 then
13573             -- error out --
13574             l_field_name := 'usage_rate_or_amount';
13575             raise e_invalid_field;
13576         end if;
13577 
13578 
13579         if p_new_res_tbl(l_index).basis_type <> 2 then
13580             -- error out --
13581             l_field_name := 'basis type for setup resource';
13582             raise e_invalid_field;
13583         end if;
13584 
13585 
13586         if p_new_res_tbl(l_index).scheduled_flag is NULL then
13587             -- error out --
13588             l_field_name := 'schedule_flag';
13589             raise e_invalid_field;
13590         else
13591             begin
13592                 select 1
13593                 into l_dummy
13594                 from mfg_lookups
13595                 where LOOKUP_TYPE  = 'BOM_RESOURCE_SCHEDULE_TYPE'
13596                 and   lookup_code = p_new_res_tbl(l_index).scheduled_flag;
13597             exception
13598                 when others then
13599                     -- error out --
13600                     l_field_name := 'schedule_flag';
13601                     raise e_invalid_field;
13602             end;
13603         end if;
13604 
13605         l_stmt_num := 142;
13606 
13607         -- Combined validations for UOM and autocharge... --
13608         l_hour_uom := FND_PROFILE.value('BOM:HOUR_UOM_CODE');
13609         l_hour_uom_class := wip_op_resources_utilities.get_uom_class(l_hour_uom);
13610         l_autocharge_type := p_new_res_tbl(l_index).autocharge_type;
13611 
13612         l_stmt_num := 145;
13613 
13614         if l_hour_uom_class = l_uom_class then
13615             l_uom_time_class_flag := true;
13616         else
13617             l_uom_time_class_flag := false;
13618         end if;
13619 
13620         l_stmt_num := 148;
13621 
13622         if (p_new_res_tbl(l_index).usage_rate_or_amount < 0 and (l_autocharge_type in (3,4) or l_uom_time_class_flag)) then
13623             /* error out... */
13624             l_field_name := 'Invalid usage rate for the autocharge/UOM combination';
13625             raise e_invalid_field;
13626         end if;
13627 
13628         l_stmt_num := 150;
13629         if p_handle_wip_cur_op = 'Y' then
13630 
13631             l_stmt_num := 155;
13632             /* check in WOR... */
13633             begin
13634                 select 1
13635                 into l_dummy
13636                 from wip_operation_resources wor
13637                 where wor.organization_id = p_org_id
13638                 and wor.wip_entity_id  = p_wip_entity_id
13639                 and wor.operation_seq_num = p_job_op_seq_num
13640                 and wor.resource_seq_num = p_new_res_tbl(l_index).res_seq_num;
13641 
13642                 raise e_invalid_res_seq_num;
13643 
13644             exception
13645                 when no_data_found then
13646                     null;
13647             end;
13648 
13649             /* check in WSOR.... */
13650             l_stmt_num := 157;
13651 
13652             begin
13653                 select 1
13654                 into l_dummy
13655                 from wip_sub_operation_resources wsor
13656                 where wsor.organization_id = p_org_id
13657                 and wsor.wip_entity_id  = p_wip_entity_id
13658                 and wsor.operation_seq_num = p_job_op_seq_num
13659                 and wsor.resource_seq_num = p_new_res_tbl(l_index).res_seq_num;
13660 
13661                 raise e_invalid_res_seq_num;
13662 
13663             exception
13664                 when no_data_found then
13665                    null;
13666             end;
13667 
13668             l_stmt_num := 160;
13669 
13670             begin
13671 
13672                 select nvl(p_new_res_tbl(l_index).dept_id,wo.department_id)
13673                 into p_new_res_tbl(l_index).dept_id
13674                 from bom_resources br,bom_department_resources bdr, wip_operations wo
13675                 where br.resource_id = p_new_res_tbl(l_index).resource_id
13676                 and  br.resource_code = p_new_res_tbl(l_index).resource_code
13677                 and (br.disable_date > sysdate  or br.disable_date is null)
13678                 and br.organization_id = p_org_id
13679                 and br.autocharge_type = p_new_res_tbl(l_index).autocharge_type
13680                 and bdr.resource_id = p_new_res_tbl(l_index).resource_id
13681                 and bdr.department_id = nvl(p_new_res_tbl(l_index).dept_id,wo.department_id)
13682                 and wo.organization_id = p_org_id
13683                 and wo.wip_entity_id  = p_wip_entity_id
13684                 and wo.operation_seq_num = p_job_op_seq_num;
13685 
13686                 l_stmt_num := 160.1;
13687 
13688             exception
13689                 when no_data_found then
13690                     /* Not a valid resource error out...*/
13691                     raise e_invalid_res;
13692 
13693                 when too_many_rows then
13694                     /* Ideally not possible */
13695                     /* what to do in this case..... */
13696                     raise e_invalid_res;
13697             end;
13698 
13699             l_stmt_num := 170;
13700             begin
13701                 select 1
13702                 into l_dummy
13703                 from wip_operations wo
13704                 where wo.wip_entity_id = p_wip_entity_id
13705                 and   wo.organization_id = p_org_id
13706                 and   wo.operation_seq_num = p_job_op_seq_num
13707                 and   p_new_res_tbl(l_index).start_date >= wo.first_unit_start_date
13708                 and   p_new_res_tbl(l_index).start_date <=  nvl(last_unit_start_date,p_new_res_tbl(l_index).start_date)
13709                 and   p_new_res_tbl(l_index).completion_date <= nvl(wo.last_unit_completion_date,p_new_res_tbl(l_index).completion_date)
13710                 and   p_new_res_tbl(l_index).completion_date >= nvl(wo.last_unit_start_date,p_new_res_tbl(l_index).completion_date);
13711 
13712             exception
13713                 when no_data_found then
13714                     /* invlaid dates */
13715                     raise e_invalid_res_dates;
13716             end;
13717         else
13718             l_stmt_num := 180;
13719             begin
13720                 select 1
13721                 into l_dummy
13722                 from wsm_copy_op_resources wcor
13723                 where wcor.organization_id = p_org_id
13724                 and wcor.wip_entity_id  = p_wip_entity_id
13725                 and wcor.operation_seq_num = p_rtg_op_seq_num
13726                 and wcor.resource_seq_num = p_new_res_tbl(l_index).res_seq_num;
13727 
13728                 raise e_invalid_res_seq;
13729 
13730             exception
13731                 when no_data_found then
13732                     null;
13733             end;
13734 
13735             l_stmt_num := 185;
13736             -- There are two SQLs for more meaningful messages...
13737             begin
13738 
13739                 select nvl(p_new_res_tbl(l_index).dept_id,wco.department_id)
13740                 into p_new_res_tbl(l_index).dept_id
13741                 from bom_resources br,bom_department_resources bdr, wsm_copy_operations wco
13742                 where br.resource_id = p_new_res_tbl(l_index).resource_id
13743                 and  br.resource_code = p_new_res_tbl(l_index).resource_code
13744                 and (br.disable_date > sysdate  or br.disable_date is null)
13745                 and br.organization_id = p_org_id
13746                 and bdr.resource_id = p_new_res_tbl(l_index).resource_id
13747                 and bdr.department_id = nvl(nvl(p_new_res_tbl(l_index).dept_id,wco.department_id),bdr.department_id)
13748                 and wco.organization_id = p_org_id
13749                 and wco.wip_entity_id  = p_wip_entity_id
13750                 and wco.operation_seq_num = p_rtg_op_seq_num;
13751 
13752             exception
13753                 when no_data_found then
13754                     /* Not a valid resource error out...*/
13755                     raise e_invalid_res;
13756 
13757                 when too_many_rows then
13758                     /* Not possible ideally */
13759                     /* what to do in this case..... */
13760                     raise e_invalid_res;
13761             end;
13762 
13763             l_stmt_num := 190;
13764             begin
13765                 select 1
13766                 into l_dummy
13767                 from wsm_copy_operations wco
13768                 where wco.organization_id = p_org_id
13769                 and   wco.wip_entity_id   = p_wip_entity_id
13770                 and   wco.operation_seq_num = p_rtg_op_seq_num
13771                 and   p_new_res_tbl(l_index).start_date >= wco.reco_start_date
13772                 and   p_new_res_tbl(l_index).start_date <=  nvl(reco_completion_date,p_new_res_tbl(l_index).start_date)
13773                 -- and   nvl(p_new_res_tbl(l_index).completion_date,nvl(reco_completion_date,sysdate)) <= nvl(reco_completion_date,sysdate)
13774                 and  ( (p_new_res_tbl(l_index).completion_date IS NULL and reco_completion_date IS NULL)
13775                        OR
13776                        ( (p_new_res_tbl(l_index).completion_date IS NOT NULL and reco_completion_date IS NOT NULL)
13777                           AND
13778                      (p_new_res_tbl(l_index).completion_date <= reco_completion_date)
13779                        )
13780                        OR
13781                        (p_new_res_tbl(l_index).completion_date IS NOT NULL and reco_completion_date IS NULL)
13782                       )
13783                 and   nvl(p_new_res_tbl(l_index).completion_date,nvl(reco_completion_date,wco.reco_start_date)) >= wco.reco_start_date;
13784 
13785             exception
13786                 when no_data_found then
13787                     /* invlaid dates */
13788                     raise e_invalid_res_dates;
13789             end;
13790 
13791 
13792         end if;
13793 
13794         /* Non-NULL validations */
13795         /* Prposed ones are ..
13796            i) sh we validate UOM code....
13797         */
13798 
13799         /* DS : validations */
13800         l_stmt_num := 200;
13801         if p_new_res_tbl(l_index).setup_id is NULL and p_new_res_tbl(l_index).setup_code is NOT NULL then
13802              begin
13803                     select  bst.setup_id
13804                     into    p_new_res_tbl(l_index).setup_id
13805                     from    bom_setup_types bst
13806                     where   bst.setup_code      = p_new_res_tbl(l_index).setup_code
13807                     and     bst.organization_id = p_org_id;
13808 
13809              exception
13810                 when no_data_found then
13811                         raise e_invalid_setup_code;
13812                     when others then
13813                         null;
13814              end;
13815         end if;
13816         if p_new_res_tbl(l_index).setup_id is not null then
13817 
13818             l_stmt_num := 210;
13819             BEGIN
13820 
13821                 select  1
13822                 into    l_dummy
13823                 from    bom_resource_setups brs
13824                 where   brs.resource_id     = p_new_res_tbl(l_index).resource_id
13825                 and     brs.organization_id = p_org_id
13826                 and     brs.setup_id        = p_new_res_tbl(l_index).setup_id;
13827 
13828             EXCEPTION
13829                 when no_data_found then
13830                     raise e_invalid_setup_id;
13831 
13832             END;
13833         end if;
13834         l_stmt_num := 220;
13835 
13836         if (p_new_res_tbl(l_index).batch_id is not null ) then
13837 
13838             l_stmt_num := 230;
13839 
13840             if l_batchable <> 1 then
13841                 -- error out..
13842                 raise e_res_not_batchable;
13843             end if;
13844 
13845             if (p_new_res_tbl(l_index).max_assg_units <> 1 or p_new_res_tbl(l_index).assigned_units <> 1)
13846             then
13847                  raise e_invalid_batch_max_assg_units;
13848             end if;
13849         end if;
13850 
13851         if (p_new_res_tbl(l_index).grp_seq_id is not null and p_new_res_tbl(l_index).grp_seq_num is not null) then
13852             l_stmt_num := 240;
13853             if (p_new_res_tbl(l_index).setup_id is null)
13854             then
13855                  raise e_invalid_seq;
13856             end if;
13857 
13858             if (p_new_res_tbl(l_index).setup_id  is not null)
13859                and
13860                (p_new_res_tbl(l_index).max_assg_units <> 1 or p_new_res_tbl(l_index).assigned_units <> 1)
13861             then
13862                  raise e_invalid_setup_max_assg_units;
13863              end if;
13864 
13865         end if;
13866 
13867         l_stmt_num := 250;
13868 		-- Modified the condition for fixing bug 5471266.
13869 		-- This allows max_assg_units to be passed as null while adding setup resource.
13870         /* if nvl(p_new_res_tbl(l_index).max_assg_units,0) < p_new_res_tbl(l_index).assigned_units then */
13871 		if nvl(p_new_res_tbl(l_index).max_assg_units,p_new_res_tbl(l_index).assigned_units) < p_new_res_tbl(l_index).assigned_units then
13872                 raise e_invalid_max_assg_units;
13873         end if;
13874 
13875         -- ST Fix for bug 4240165
13876         -- Now copy the values derived into the global table also..
13877         v_wljdi_uom_code(l_index)               := p_new_res_tbl(l_index).uom_code;
13878         v_wljdi_basis_type(l_index)             := p_new_res_tbl(l_index).basis_type;
13879         v_wljdi_res_autocharge_type(l_index)    := p_new_res_tbl(l_index).autocharge_type;
13880         v_wljdi_standard_rate_flag(l_index)     := p_new_res_tbl(l_index).standard_rate_flag;
13881         v_wljdi_department_id(l_index)          := p_new_res_tbl(l_index).dept_id;
13882 
13883         l_index := p_new_res_tbl.next(l_index);
13884 
13885     end loop;
13886 
13887     x_error_code := 0;
13888 
13889 exception
13890 
13891     when e_invalid_res_seq_num then
13892 
13893         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13894         fnd_message.set_token('FLD_NAME', ' resource sequence number ');
13895         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13896 
13897         fnd_file.put_line(fnd_file.log,x_error_msg);
13898 
13899         x_error_code := -1;
13900 
13901     when e_invalid_par_seq  then
13902         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13903         fnd_message.set_token('FLD_NAME', ' parent resource sequence number ');
13904         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13905         fnd_file.put_line(fnd_file.log,x_error_msg);
13906         x_error_code := -1;
13907 
13908     when e_invalid_res   then
13909         --Bug 4704351:Resource id validation is already done in the prev sql.So
13910         --this exception is used for resource and department mismatch.
13911         --fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13912         --fnd_message.set_token('FLD_NAME', ' resource id ');
13913         fnd_message.set_name('WSM','WSM_INVALID_RES_DEPT');
13914         --Set up Resource should be from the same department as Parent Resource
13915         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13916         fnd_file.put_line(fnd_file.log,x_error_msg);
13917         x_error_code := -1;
13918 
13919     when e_invalid_res_dates  then
13920         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13921         fnd_message.set_token('FLD_NAME', ' resource start/completion dates ');
13922         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13923         fnd_file.put_line(fnd_file.log,x_error_msg);
13924         x_error_code := -1;
13925 
13926     when e_invalid_res_seq  then
13927 
13928         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13929         fnd_message.set_token('FLD_NAME', ' resource seq num ');
13930         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13931         fnd_file.put_line(fnd_file.log,x_error_msg);
13932         x_error_code := -1;
13933 
13934     when e_invalid_seq then
13935         fnd_message.set_name('WSM','WSM_INVALID_SEQ');
13936         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get ;
13937         fnd_file.put_line(fnd_file.log,x_error_msg);
13938         x_error_code := -1;
13939 
13940     when e_invalid_setup_id then
13941         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13942         fnd_message.set_token('FLD_NAME', ' setup id ');
13943         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get ;
13944         fnd_file.put_line(fnd_file.log,x_error_msg);
13945         x_error_code := -1;
13946 
13947     when e_invalid_field  then
13948 	    /*Modified error handling mechanism for bug 5469917. */
13949 	    fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13950         fnd_message.set_token('FLD_NAME', l_field_name);
13951         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13952         fnd_file.put_line(fnd_file.log,x_error_msg);
13953         x_error_code := -1;
13954 
13955     when e_invalid_max_assg_units then
13956 
13957         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13958         fnd_message.set_token('FLD_NAME', ' maximum assigned units ');
13959         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13960         fnd_file.put_line(fnd_file.log,x_error_msg);
13961         x_error_code := -1;
13962 
13963     when e_invalid_setup_max_assg_units then
13964 
13965         fnd_message.set_name('WSM','WSM_INVALID_SETUP_MAX_ASSG');
13966 
13967         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13968 
13969         fnd_file.put_line(fnd_file.log,x_error_msg);
13970         x_error_code := -1;
13971 
13972     when e_setup_costed then
13973 
13974         fnd_message.set_name('WSM','WSM_INVALID_SETUP_MAX_ASSG');
13975 
13976         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13977         fnd_file.put_line(fnd_file.log,x_error_msg);
13978         x_error_code := -1;
13979 
13980     when e_invalid_batch_max_assg_units then
13981 
13982         fnd_message.set_name('WSM','WSM_INVALID_BATCH_MAX_ASSG');
13983 
13984         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13985         x_error_code := -1;
13986         fnd_file.put_line(fnd_file.log,x_error_msg);
13987 
13988     when e_res_not_batchable then
13989 
13990         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13991         fnd_message.set_token('FLD_NAME', ' batch id for resource ');
13992 
13993         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
13994         fnd_file.put_line(fnd_file.log,x_error_msg);
13995         x_error_code := -1;
13996 
13997     when e_no_po_move_res then
13998         fnd_message.set_name('WSM','WSM_CURR_OP_RES_PO_MOVE');
13999         fnd_message.set_token('FLD_NAME', ' resource ');
14000         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14001         fnd_file.put_line(fnd_file.log,x_error_msg);
14002         x_error_code := -1;
14003 
14004     when others then
14005         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || SQLCODE || ' : ' || substrb(SQLERRM,1,1000) ;
14006         fnd_file.put_line(fnd_file.log,x_error_msg);
14007         x_error_code := -1;
14008 
14009 end validate_new_resource;
14010 
14011 /* ST : Detailed scheduling start */
14012 /***************************************************************************
14013 
14014 Procedure to take care of
14015 
14016 i) setup resources to an existing resource
14017 
14018 All these additions either at current op or future op
14019 
14020 ***************************************************************************/
14021 
14022 Procedure add_resource(p_wip_entity_id      in      number,
14023                        p_org_id             in      number,
14024                        p_job_op_seq_num     in      number,
14025                        p_cur_job_op_seq_num in      number,
14026                        p_rtg_op_seq_num     in      number,
14027                        p_load_type          in      number,
14028                        p_sub_type           in      number,
14029                        p_res_recom          in      t_res_recom,
14030                        p_last_updt_date     in      date,
14031                        p_last_updt_by       in      number,
14032                        p_creation_date      in      date,
14033                        p_created_by         in      number,
14034                        p_last_updt_login    in      number,
14035                        p_request_id         in      number,
14036                        p_program_appl_id    in      number,
14037                        p_program_id         in      number,
14038                        p_prog_updt_date     in      date,
14039                        p_global_index       in out nocopy   number,
14040                        x_error_code         in out nocopy   number,
14041                        x_error_msg          in out nocopy   varchar2
14042                       )
14043                       is
14044 
14045 
14046 l_new_res_tbl           new_res_tbl;
14047 l_setup_index_tbl       t_number;
14048 l_par_res_index         number;
14049 l_temp                  number;
14050 
14051 l_error_code            number;
14052 l_error_msg             varchar2(2000);
14053 l_stmt_num              number := 0;
14054 
14055 
14056 l_index             number;
14057 l_found             number;
14058 
14059 begin
14060 
14061     l_stmt_num := 10;
14062 
14063     x_error_code := 0;
14064     x_error_msg  := null;
14065 
14066     /* This is primarily used to delete both the resource and its setup from the v_wldji... so that the Adding is once for the resource and
14067        its setup resource
14068     ... */
14069 
14070     l_new_res_tbl.delete;
14071     l_par_res_index := null;
14072     l_setup_index_tbl.delete;
14073 
14074     /* ST : Detailed Scheduling : Support adding resources to current op */
14075     /* Update the WIP tables in this case..... */
14076     --Bug 5474321: Setup resource should be imported even for previous operations.
14077     --if ( ((p_job_op_seq_num IS NOT NULL) and (p_job_op_seq_num = p_cur_job_op_seq_num) )
14078     if ( ((p_job_op_seq_num IS NOT NULL) )
14079          OR
14080          ((p_rtg_op_seq_num IS NOT NULL))
14081        )
14082     then
14083 
14084         l_stmt_num := 20;
14085         /*
14086           First clear the validation table of records
14087           First load it into the validation table of records ( index will be the resource_id )
14088           Check if has a setup res ( loop through.... )
14089           if found add that record also to the validation table of records
14090           call the validate_add_new_res procedure
14091           if successful then add the resources in the validation
14092           Load the resources into  local PL/SQL tables and delete them from v_wldji_
14093         */
14094 
14095         if v_wljdi_parent_seq_num(p_global_index) is not null then /* Indicates that it is a setup resource....*/
14096 
14097             l_stmt_num := 30;
14098             /* Search through the v_wldji_ for with similar load type for its parent resource...and also sibling setup resources*/
14099             /* Load this into the validation PL/SQL table... */
14100 
14101             l_temp := p_global_index;
14102             l_setup_index_tbl(l_temp) := p_global_index;
14103 
14104             l_new_res_tbl(l_temp).job_op_seq_num                := v_wljdi_job_op_seq_num(l_temp);
14105             l_new_res_tbl(l_temp).rtg_op_seq_num                := v_wljdi_routing_op_seq_num(l_temp);
14106             l_new_res_tbl(l_temp).resource_id                   := v_wljdi_resource_id_new(l_temp);
14107             l_new_res_tbl(l_temp).resource_code                 := v_wljdi_resource_code_new(l_temp);
14108             l_new_res_tbl(l_temp).dept_id                       := v_wljdi_department_id(l_temp);
14109             l_new_res_tbl(l_temp).dept_code                     := v_wljdi_department_code(l_temp);
14110             l_new_res_tbl(l_temp).res_seq_num                   := v_wljdi_resource_seq_num(l_temp);
14111             l_new_res_tbl(l_temp).sub_grp_num                   := v_wljdi_substitute_group_num(l_temp);
14112             l_new_res_tbl(l_temp).rpl_grp_num                   := v_wljdi_replacement_group_num(l_temp);
14113             l_new_res_tbl(l_temp).activity_id                   := v_wljdi_activity_id(l_temp);
14114             l_new_res_tbl(l_temp).standard_rate_flag            := v_wljdi_standard_rate_flag(l_temp);
14115             l_new_res_tbl(l_temp).assigned_units                := v_wljdi_assigned_units(l_temp);
14116             l_new_res_tbl(l_temp).usage_rate_or_amount          := v_wljdi_usage_rate_or_amt(l_temp);
14117             l_new_res_tbl(l_temp).usage_rate_or_amount_inverse  := 0; /* calculate it shiva ..... */
14118             l_new_res_tbl(l_temp).uom_code                      := v_wljdi_uom_code(l_temp);
14119             l_new_res_tbl(l_temp).basis_type                    := v_wljdi_basis_type(l_temp);
14120             l_new_res_tbl(l_temp).scheduled_flag                := v_wljdi_scheduled_flag(l_temp);
14121             l_new_res_tbl(l_temp).autocharge_type               := v_wljdi_res_autocharge_type(l_temp);
14122             l_new_res_tbl(l_temp).schedule_seq_num              := v_wljdi_schedule_seq_num(l_temp);
14123             l_new_res_tbl(l_temp).setup_id                      := v_wljdi_setup_id(l_temp);
14124             l_new_res_tbl(l_temp).setup_code                    := v_wljdi_setup_code(l_temp);
14125             l_new_res_tbl(l_temp).start_date                    := v_wljdi_start_date(l_temp);
14126             l_new_res_tbl(l_temp).firm_type                     := v_wljdi_firm_type(l_temp);
14127             l_new_res_tbl(l_temp).grp_seq_id                    := v_wljdi_grp_seq_id(l_temp);
14128             l_new_res_tbl(l_temp).grp_seq_num                   := v_wljdi_grp_seq_num(l_temp);
14129             l_new_res_tbl(l_temp).max_assg_units                := v_wljdi_max_assg_units(l_temp);
14130             l_new_res_tbl(l_temp).parent_seq_num                := v_wljdi_parent_seq_num(l_temp);
14131             l_new_res_tbl(l_temp).batch_id                      := v_wljdi_batch_id(l_temp);
14132             l_new_res_tbl(l_temp).completion_date               := v_wljdi_completion_date(l_temp);
14133 
14134             l_temp := v_wljdi_record_id.next(p_global_index);
14135 
14136             l_stmt_num := 35;
14137             /* start the search */
14138             while (l_temp is not null) loop
14139 
14140                    -- Removed the above code (see the previous revision)
14141                    -- Cannot add a parent resource...
14142                    if ( (v_wljdi_parent_seq_num(l_temp) = v_wljdi_parent_seq_num(p_global_index)) and
14143                     (v_wljdi_load_type(l_temp)     = p_load_type) and
14144                     (v_wljdi_substitution_type(l_temp)     = p_sub_type) and
14145                     ((v_wljdi_job_op_seq_num(l_temp)        = p_job_op_seq_num)
14146                        or
14147                           (v_wljdi_routing_op_seq_num(l_temp)   = p_rtg_op_seq_num)
14148                         )
14149                      )
14150                    then
14151                         l_stmt_num := 50;
14152 
14153                         /* indicates a sibling setup resource */
14154                         -- We are using a separate index so that it will be contiguous..
14155                         -- or else we will get an error during the BULK INSERT
14156 
14157                         l_setup_index_tbl(l_temp) := l_temp;
14158 
14159                         l_new_res_tbl(l_temp).job_op_seq_num                := v_wljdi_job_op_seq_num(l_temp);
14160                         l_new_res_tbl(l_temp).rtg_op_seq_num                := v_wljdi_routing_op_seq_num(l_temp);
14161                         l_new_res_tbl(l_temp).resource_id                   := v_wljdi_resource_id_new(l_temp);
14162                         l_new_res_tbl(l_temp).resource_code                 := v_wljdi_resource_code_new(l_temp);
14163                         l_new_res_tbl(l_temp).dept_id                       := v_wljdi_department_id(l_temp);
14164                         l_new_res_tbl(l_temp).dept_code                     := v_wljdi_department_code(l_temp);
14165                         l_new_res_tbl(l_temp).res_seq_num                   := v_wljdi_resource_seq_num(l_temp);
14166                         l_new_res_tbl(l_temp).sub_grp_num                   := v_wljdi_substitute_group_num(l_temp);
14167                         l_new_res_tbl(l_temp).rpl_grp_num                   := v_wljdi_replacement_group_num(l_temp);
14168                         l_new_res_tbl(l_temp).activity_id                   := v_wljdi_activity_id(l_temp);
14169                         l_new_res_tbl(l_temp).standard_rate_flag            := v_wljdi_standard_rate_flag(l_temp);
14170                         l_new_res_tbl(l_temp).assigned_units                := v_wljdi_assigned_units(l_temp);
14171                         l_new_res_tbl(l_temp).usage_rate_or_amount          := v_wljdi_usage_rate_or_amt(l_temp);
14172                         l_new_res_tbl(l_temp).usage_rate_or_amount_inverse  := 0; /* calculate it shiva ..... */
14173                         l_new_res_tbl(l_temp).uom_code                      := v_wljdi_uom_code(l_temp);
14174                         l_new_res_tbl(l_temp).basis_type                    := v_wljdi_basis_type(l_temp);
14175                         l_new_res_tbl(l_temp).scheduled_flag                := v_wljdi_scheduled_flag(l_temp);
14176                         l_new_res_tbl(l_temp).autocharge_type               := v_wljdi_res_autocharge_type(l_temp);
14177                         l_new_res_tbl(l_temp).schedule_seq_num              := v_wljdi_schedule_seq_num(l_temp);
14178                         l_new_res_tbl(l_temp).setup_id                      := v_wljdi_setup_id(l_temp);
14179                         l_new_res_tbl(l_temp).setup_code                    := v_wljdi_setup_code(l_temp);
14180                         l_new_res_tbl(l_temp).start_date                    := v_wljdi_start_date(l_temp);
14181                         l_new_res_tbl(l_temp).firm_type                     := v_wljdi_firm_type(l_temp);
14182                         l_new_res_tbl(l_temp).grp_seq_id                    := v_wljdi_grp_seq_id(l_temp);
14183                         l_new_res_tbl(l_temp).grp_seq_num                   := v_wljdi_grp_seq_num(l_temp);
14184                         l_new_res_tbl(l_temp).max_assg_units                := v_wljdi_max_assg_units(l_temp);
14185                         l_new_res_tbl(l_temp).parent_seq_num                := v_wljdi_parent_seq_num(l_temp);
14186                         l_new_res_tbl(l_temp).batch_id                      := v_wljdi_batch_id(l_temp);
14187                         l_new_res_tbl(l_temp).completion_date               := v_wljdi_completion_date(l_temp);
14188 
14189                    end if;
14190 
14191                    l_temp := v_wljdi_record_id.next(l_temp);
14192 
14193              end loop;
14194 
14195         end if;
14196     else
14197         /* error out.... */
14198         fnd_message.set_name('WSM','WSM_NULL_FIELD');
14199         fnd_message.set_token('FLD_NAME', 'job_op_seq_num/rtg_op_seq_num');
14200         x_error_msg := fnd_message.get;
14201         x_error_code := -1;
14202         return; --raise e_wljdi_common_error;
14203     end if;
14204     l_stmt_num := 80;
14205 
14206     --Bug 5474321: Setup resource should be imported even for previous operations.
14207     --if ( (p_job_op_seq_num IS NOT NULL) and (p_job_op_seq_num = p_cur_job_op_seq_num) ) then
14208     if ( p_job_op_seq_num IS NOT NULL) THEN
14209 
14210 
14211         l_stmt_num := 90;
14212         l_error_code := 0;
14213         l_error_msg  := null;
14214 
14215         l_stmt_num := 100;
14216         /* call the validate procedure now.... */
14217         validate_new_resource(p_wip_entity_id     => p_wip_entity_id,
14218                               p_org_id            => p_org_id,
14219                               p_new_res_tbl       => l_new_res_tbl,
14220                               p_setup_index_tbl   => l_setup_index_tbl,
14221                               p_res_recom         => p_res_recom,
14222                               p_handle_wip_cur_op => 'Y',
14223                               p_job_op_seq_num    => p_job_op_seq_num,
14224                               p_rtg_op_seq_num    => null,
14225                               x_error_code        => l_error_code,
14226                               x_error_msg         => l_error_msg);
14227 
14228         if l_error_code <> 0 then
14229             /* error out validation failed,,,, set p_error_msg to the error message */
14230             x_error_msg  := l_error_msg;
14231             x_error_code := -1;
14232             return; --raise e_wljdi_common_error;
14233         end if;
14234         l_stmt_num := 110;
14235 
14236         forall i in l_setup_index_tbl.first..l_setup_index_tbl.last
14237 
14238             /* issue the insert statement */
14239              insert into wip_operation_resources
14240              (
14241                 wip_entity_id,
14242                 operation_seq_num,
14243                 resource_seq_num,
14244                 organization_id,
14245                 department_id,
14246                 --repetitive_schedule_id,
14247 
14248                 resource_id,
14249                 uom_code,
14250                 basis_type,
14251                 usage_rate_or_amount,
14252 
14253                 activity_id,
14254                 scheduled_flag,
14255                 assigned_units,
14256 
14257                 maximum_assigned_units,
14258                 batch_id,
14259                 firm_flag,
14260                 group_sequence_id,
14261                 group_sequence_number,
14262                 parent_resource_seq,
14263 
14264                 autocharge_type,
14265                 standard_rate_flag,
14266                 applied_resource_units,
14267                 applied_resource_value,
14268                 start_date,
14269                 completion_date,
14270 
14271                 attribute_category,
14272                 attribute1,
14273                 attribute2,
14274                 attribute3,
14275                 attribute4,
14276                 attribute5,
14277                 attribute6,
14278                 attribute7,
14279                 attribute8,
14280                 attribute9,
14281                 attribute10,
14282                 attribute11,
14283                 attribute12,
14284                 attribute13,
14285                 attribute14,
14286                 attribute15,
14287 
14288                 schedule_seq_num,
14289                 substitute_group_num,
14290                 replacement_group_num,
14291                 --principle_flag,
14292                 setup_id,
14293 
14294                 last_update_date,
14295                 last_updated_by,
14296                 creation_date,
14297                 created_by,
14298                 last_update_login,
14299                 request_id,
14300                 program_application_id,
14301                 program_id,
14302                 program_update_date
14303                 )
14304             values
14305             (
14306                p_wip_entity_id,
14307                p_job_op_seq_num,
14308                v_wljdi_resource_seq_num(i),
14309                p_org_id,
14310                v_wljdi_department_id(i),
14311                -- check,
14312 
14313                v_wljdi_resource_id_new(i),
14314                v_wljdi_uom_code(i),
14315                v_wljdi_basis_type(i),
14316                v_wljdi_usage_rate_or_amt(i),
14317 
14318                v_wljdi_activity_id(i),
14319                v_wljdi_scheduled_flag(i),
14320                v_wljdi_assigned_units(i),
14321                /* Modified Query by Adding NVL during insert of max_assg_units. Bug 5471266 */
14322                nvl(v_wljdi_max_assg_units(i),v_wljdi_assigned_units(i)),
14323                v_wljdi_batch_id(i),
14324 			   /* Modified Query by Adding NVL during insert of firm type. Bug 5471266 */
14325                nvl(v_wljdi_firm_type(i),0),
14326                v_wljdi_grp_seq_id(i),
14327                v_wljdi_grp_seq_num(i),
14328                v_wljdi_parent_seq_num(i),
14329 
14330                v_wljdi_res_autocharge_type(i),
14331                v_wljdi_standard_rate_flag(i),
14332                0,
14333                0,
14334                v_wljdi_start_date(i),
14335                v_wljdi_completion_date(i),
14336 
14337                v_wljdi_attribute_category(i),
14338                v_wljdi_attribute1(i),
14339                v_wljdi_attribute2(i),
14340                v_wljdi_attribute3(i),
14341                v_wljdi_attribute4(i),
14342                v_wljdi_attribute5(i),
14343                v_wljdi_attribute6(i),
14344                v_wljdi_attribute7(i),
14345                v_wljdi_attribute8(i),
14346                v_wljdi_attribute9(i),
14347                v_wljdi_attribute10(i),
14348                v_wljdi_attribute11(i),
14349                v_wljdi_attribute12(i),
14350                v_wljdi_attribute13(i),
14351                v_wljdi_attribute14(i),
14352                v_wljdi_attribute15(i),
14353 
14354                v_wljdi_schedule_seq_num(i),
14355                v_wljdi_substitute_group_num(i),
14356                v_wljdi_replacement_group_num(i),
14357                v_wljdi_setup_id(i),
14358 
14359                p_last_updt_date,
14360                p_last_updt_by,
14361                p_creation_date,
14362                p_created_by,
14363                p_last_updt_login,
14364                p_request_id,
14365                p_program_appl_id,
14366                p_program_id,
14367                p_prog_updt_date
14368             );
14369 
14370         l_index := l_setup_index_tbl.first;
14371 
14372         while l_index is not null loop
14373             if v_wljdi_res_autocharge_type(l_index) = 3 then
14374 
14375                 wip_osp.create_requisition(
14376                                  p_wip_entity_id          => p_wip_entity_id,
14377                                  p_organization_id        => p_org_id,
14378                                  p_repetitive_schedule_id => null,
14379                                  p_operation_seq_num      => p_job_op_seq_num,
14380                                  p_resource_seq_num       => v_wljdi_resource_seq_num(l_index),
14381                                  p_run_reqimport          => wip_constants.no
14382                               );
14383 
14384                 g_num_of_osp_exists := nvl(g_num_of_osp_exists,0) + 1;
14385 
14386             end if;
14387 
14388             l_index := l_setup_index_tbl.next(l_index);
14389 
14390         end loop;
14391 
14392     elsif (p_rtg_op_seq_num IS NOT NULL) then
14393 
14394         l_stmt_num := 120;
14395 
14396         l_error_code := 0;
14397         l_error_msg  := null;
14398         /* call the validate procedure now.... */
14399         validate_new_resource(p_wip_entity_id     => p_wip_entity_id,
14400                               p_org_id            => p_org_id,
14401                               p_new_res_tbl       => l_new_res_tbl,
14402                               p_setup_index_tbl   => l_setup_index_tbl,
14403                               p_res_recom         => p_res_recom,
14404                               p_handle_wip_cur_op => 'N',
14405                               p_job_op_seq_num    => null,
14406                               p_rtg_op_seq_num    => p_rtg_op_seq_num,
14407                               x_error_code        => l_error_code,
14408                               x_error_msg         => l_error_msg);
14409 
14410         if l_error_code <> 0 then
14411             /* error out validation failed,,,, set p_error_msg to the error message */
14412             x_error_msg  := l_error_msg;
14413             x_error_code := -1;
14414             return; --raise e_wljdi_common_error;
14415         end if;
14416 
14417         l_stmt_num := 130;
14418         forall i in l_setup_index_tbl.first..l_setup_index_tbl.last
14419 
14420             /* issue the insert statement */
14421              insert into wsm_copy_op_resources
14422              (
14423                 wip_entity_id,
14424                 operation_seq_num,
14425                 resource_seq_num,
14426                 organization_id,
14427                 department_id,
14428                 --repetitive_schedule_id,
14429                 recommended,
14430 
14431                 resource_id,
14432                 resource_code,
14433                 uom_code,
14434                 basis_type,
14435                 usage_rate_or_amount,
14436                 usage_rate_or_amount_inverse,
14437                 activity_id,
14438                 schedule_flag,
14439                 assigned_units,
14440 
14441                 max_assigned_units,
14442                 batch_id,
14443                 firm_type,
14444                 group_sequence_id,
14445                 group_sequence_num,
14446                 parent_resource_seq_num,
14447 
14448                 autocharge_type,
14449                 standard_rate_flag,
14450                 reco_start_date,
14451                 reco_completion_date,
14452 
14453                 attribute_category,
14454                 attribute1,
14455                 attribute2,
14456                 attribute3,
14457                 attribute4,
14458                 attribute5,
14459                 attribute6,
14460                 attribute7,
14461                 attribute8,
14462                 attribute9,
14463                 attribute10,
14464                 attribute11,
14465                 attribute12,
14466                 attribute13,
14467                 attribute14,
14468                 attribute15,
14469 
14470                 schedule_seq_num,
14471                 substitute_group_num,
14472                 replacement_group_num,
14473                 --principle_flag,
14474                 setup_id,
14475 
14476                 last_update_date,
14477                 last_updated_by,
14478                 creation_date,
14479                 created_by,
14480                 last_update_login,
14481                 request_id,
14482                 program_application_id,
14483                 program_id,
14484                 program_update_date
14485                 )
14486             values
14487             (
14488                p_wip_entity_id,
14489                p_rtg_op_seq_num,
14490                v_wljdi_resource_seq_num(i),
14491                p_org_id,
14492                v_wljdi_department_id(i),
14493                -- check,
14494                'Y',
14495 
14496                v_wljdi_resource_id_new(i),
14497                v_wljdi_resource_code_new(i),
14498                v_wljdi_uom_code(i),
14499                v_wljdi_basis_type(i),
14500                v_wljdi_usage_rate_or_amt(i),
14501                round(1/v_wljdi_usage_rate_or_amt(i),6),
14502                v_wljdi_activity_id(i),
14503                v_wljdi_scheduled_flag(i),
14504                v_wljdi_assigned_units(i),
14505                /* Modified Query by Adding NVL during insert of max_assg_units. Bug 5471266 */
14506                nvl(v_wljdi_max_assg_units(i),v_wljdi_assigned_units(i)),
14507                v_wljdi_batch_id(i),
14508 			   /* Modified Query by Adding NVL during insert of firm type. Bug 5471266 */
14509                nvl(v_wljdi_firm_type(i),0),
14510                v_wljdi_grp_seq_id(i),
14511                v_wljdi_grp_seq_num(i),
14512                v_wljdi_parent_seq_num(i),
14513 
14514                v_wljdi_res_autocharge_type(i),
14515                v_wljdi_standard_rate_flag(i),
14516                v_wljdi_start_date(i),
14517                v_wljdi_completion_date(i),
14518 
14519                v_wljdi_attribute_category(i),
14520                v_wljdi_attribute1(i),
14521                v_wljdi_attribute2(i),
14522                v_wljdi_attribute3(i),
14523                v_wljdi_attribute4(i),
14524                v_wljdi_attribute5(i),
14525                v_wljdi_attribute6(i),
14526                v_wljdi_attribute7(i),
14527                v_wljdi_attribute8(i),
14528                v_wljdi_attribute9(i),
14529                v_wljdi_attribute10(i),
14530                v_wljdi_attribute11(i),
14531                v_wljdi_attribute12(i),
14532                v_wljdi_attribute13(i),
14533                v_wljdi_attribute14(i),
14534                v_wljdi_attribute15(i),
14535 
14536                v_wljdi_schedule_seq_num(i),
14537                v_wljdi_substitute_group_num(i),
14538                v_wljdi_replacement_group_num(i),
14539                -- not there...
14540                v_wljdi_setup_id(i),
14541 
14542                p_last_updt_date,
14543                p_last_updt_by,
14544                p_creation_date,
14545                p_created_by,
14546                p_last_updt_login,
14547                p_request_id,
14548                p_program_appl_id,
14549                p_program_id,
14550                p_prog_updt_date
14551             );
14552 
14553     else
14554 
14555        /* error out.... cannt delete previous op resources.... */
14556        fnd_message.set_name('WSM','WSM_NULL_FIELD');
14557        fnd_message.set_token('FLD_NAME', 'job_op_seq_num/rtg_op_seq_num');
14558        x_error_msg := fnd_message.get;
14559        x_error_code := -1;
14560        return; --raise e_wljdi_common_error;
14561 
14562     end if;
14563 
14564     l_stmt_num := 140;
14565 
14566     --Bug 5474321: Setup resource should be imported even for previous operations.
14567     --if ( ((p_job_op_seq_num IS NOT NULL) and (p_job_op_seq_num = p_cur_job_op_seq_num) )
14568     if ( ((p_job_op_seq_num IS NOT NULL) )
14569          OR
14570          ((p_rtg_op_seq_num IS NOT NULL))
14571        )
14572     then
14573 
14574         l_stmt_num := 150;
14575         /* check if this part has to come under an if clause....*/
14576         /* no significance..... */
14577         l_index := p_global_index; /* current position of p_global_index which will be deleted... */
14578         p_global_index := null;
14579 
14580         IF l_setup_index_tbl.exists(l_index) THEN
14581             l_index := v_wljdi_record_id.next(l_index);
14582         END IF;
14583 
14584         -- Try to get a resource record...
14585         while l_index is not null loop
14586 
14587             IF lbji_debug = 'Y' THEN
14588                 fnd_file.put_line(fnd_file.log,'Add Resource : Finding the next one : Current ID : ' || v_wljdi_record_id(l_index));
14589             END IF;
14590 
14591             IF (not(l_setup_index_tbl.exists(l_index))
14592                 and v_wljdi_load_type(l_index) = WSM_LOAD_RES)
14593             Then
14594                 -- ok.. this is for  Resource
14595                 IF lbji_debug = 'Y' THEN
14596                         fnd_file.put_line(fnd_file.log,'Add Resource : Found the next record : ' ||  v_wljdi_record_id(l_index));
14597                 END IF;
14598                 p_global_index := l_index;
14599                 exit;
14600             END IF;
14601 
14602             l_index := v_wljdi_record_id.next(l_index);
14603         end loop;
14604         IF lbji_debug = 'Y' THEN
14605                 fnd_file.put_line(fnd_file.log,'Add Resource : Next Record ID : ' || p_global_index);
14606         END IF;
14607 
14608         /* delete the fetched records... */
14609         l_index := l_setup_index_tbl.first;
14610 
14611         while l_index is not null loop
14612 
14613             v_wljdi_record_id.delete(l_index);
14614             v_wljdi_load_type.delete(l_index);
14615             v_wljdi_substitution_type.delete(l_index);
14616             v_wljdi_job_op_seq_num.delete(l_index);
14617             v_wljdi_routing_op_seq_num.delete(l_index);
14618             v_wljdi_next_rtg_op_seq_num.delete(l_index);
14619             v_wljdi_inventory_item_id_new.delete(l_index);
14620             v_wljdi_inventory_item_new.delete(l_index);
14621             v_wljdi_primary_item.delete(l_index);
14622             v_wljdi_primary_item_id.delete(l_index);
14623             v_wljdi_src_phantom_item.delete(l_index);
14624             v_wljdi_src_phantom_item_id.delete(l_index);
14625             v_wljdi_component_sequence_id.delete(l_index);
14626             v_wljdi_date_required.delete(l_index);
14627             v_wljdi_scheduled_quantity.delete(l_index);
14628             v_wljdi_required_quantity.delete(l_index);
14629             v_wljdi_batch_id.delete(l_index);
14630             v_wljdi_assigned_units.delete(l_index);
14631             v_wljdi_instance_id_new.delete(l_index);
14632             v_wljdi_op_completion_date.delete(l_index);
14633             v_wljdi_op_start_date.delete(l_index);
14634             v_wljdi_op_yield.delete(l_index);
14635             v_wljdi_resource_code_new.delete(l_index);
14636             v_wljdi_resource_id_new.delete(l_index);
14637             v_wljdi_resource_seq_num.delete(l_index);
14638             v_wljdi_substitute_group_num.delete(l_index);
14639             v_wljdi_replacement_group_num.delete(l_index);
14640             v_wljdi_serial_number_new.delete(l_index);
14641             v_wljdi_start_date.delete(l_index);
14642             v_wljdi_completion_date.delete(l_index);
14643 
14644             v_wljdi_firm_type.delete(l_index);
14645             v_wljdi_setup_id.delete(l_index);
14646             v_wljdi_grp_seq_id.delete(l_index);
14647             v_wljdi_grp_seq_num.delete(l_index);
14648             v_wljdi_max_assg_units.delete(l_index);
14649             v_wljdi_parent_seq_num.delete(l_index);
14650             v_wljdi_resource_hours.delete(l_index);
14651 
14652             v_wljdi_department_id.delete(l_index);
14653             v_wljdi_department_code.delete(l_index);
14654             v_wljdi_activity_id.delete(l_index);
14655             v_wljdi_standard_rate_flag.delete(l_index);
14656             v_wljdi_usage_rate_or_amt.delete(l_index);
14657             v_wljdi_basis_type.delete(l_index);
14658             v_wljdi_uom_code.delete(l_index);
14659             v_wljdi_scheduled_flag.delete(l_index);
14660             v_wljdi_res_autocharge_type.delete(l_index);
14661             v_wljdi_schedule_seq_num.delete(l_index);
14662 
14663              v_wljdi_attribute_category.delete(l_index);
14664              v_wljdi_attribute1.delete(l_index);
14665              v_wljdi_attribute2.delete(l_index);
14666              v_wljdi_attribute3.delete(l_index);
14667              v_wljdi_attribute4.delete(l_index);
14668              v_wljdi_attribute5.delete(l_index);
14669              v_wljdi_attribute6.delete(l_index);
14670              v_wljdi_attribute7.delete(l_index);
14671              v_wljdi_attribute8.delete(l_index);
14672              v_wljdi_attribute9.delete(l_index);
14673              v_wljdi_attribute10.delete(l_index);
14674              v_wljdi_attribute11.delete(l_index);
14675              v_wljdi_attribute12.delete(l_index);
14676              v_wljdi_attribute13.delete(l_index);
14677              v_wljdi_attribute14.delete(l_index);
14678              v_wljdi_attribute15.delete(l_index);
14679 
14680              l_index := l_setup_index_tbl.next(l_index);
14681 
14682         end loop;
14683     end if;
14684 
14685     l_stmt_num := 160;
14686 
14687     x_error_code := 0;
14688 
14689 exception
14690 
14691     when others then
14692         x_error_msg  := 'WSM_LBJ_INTERFACE_PVT : add_resource :: ' || l_stmt_num || ' : ' || substrb(SQLERRM,1,2000);
14693         fnd_file.put_line(fnd_file.log,x_error_msg);
14694         x_error_code := -1;
14695 
14696 end add_resource;
14697 
14698 -- **********************************************************************************************
14699 -- PROCEDURE import_lot_job_details
14700 -- **********************************************************************************************
14701 
14702 -- If one line has a fetal error, we will abort all the process
14703 
14704 procedure import_lot_job_details (
14705         p_wip_entity_id                 in number,
14706         p_org_id                        in number,
14707         p_wo_records_exist              in varchar2,    -- 'Y' or 'N'
14708         p_parent_header_id              in number,      -- header_id in WLJI, pass null if N/A
14709         p_job_scheduled_start_date      in date,
14710         p_job_scheduled_compl_date      in date,
14711         p_job_scheduled_quantity        in number,
14712         p_group_id                      in number,
14713         p_last_updt_date                in date,
14714         p_last_updt_by                  in number,
14715         p_creation_date                 in date,
14716         p_created_by                    in number,
14717         p_last_updt_login               in number,
14718         p_request_id                    in number,
14719         p_program_appl_id               in number,
14720         p_program_id                    in number,
14721         p_prog_updt_date                in date,
14722         p_error_code                    out nocopy number,
14723         p_error_msg                     out nocopy varchar2) is
14724 
14725 
14726 l_stmt_num                  number;
14727 l_d                         number;         -- index for WLJDI records
14728 l_d_saved                   number;
14729 l_temp                      number;
14730 l_exists                    number := 0;
14731 l_swap                      number := 0;    -- ADD: bug 3537390
14732 l_in                        number;
14733 l_out                       number;
14734 l_idx_out_phm               number;
14735 l_idx2                      number;
14736 l_status                    VARCHAR2(10);
14737 l_msg_count                 number;
14738 
14739 l_hash_str                  varchar2(200);  -- bug 3373496
14740 l_hash_value                number;         -- bug 3373496
14741 l_v_dup_usage               t_number;       -- bug 3373496
14742 
14743 
14744 
14745 l_res_first                 number  := NULL;
14746 l_comp_first                number  := NULL;
14747 l_op_first                  number  := NULL;
14748 l_res_use_first             number  := NULL;
14749 l_link_first                number  := NULL;
14750 l_op_link_first             number  := NULL;
14751 l_res_ins_first             number  := NULL;
14752 l_change_path               boolean := false;
14753 l_with_discommand           boolean := false;
14754 l_path_changed              boolean := false;
14755 l_job_status                number;
14756 l_job_start_qty             number;
14757 l_job_qty_scrapped          number;
14758 
14759 l_load_type                 number;
14760 l_sub_type                  number;
14761 l_end_op_seq_num            number:= NULL;
14762 l_end_op_seq_id             number:= NULL;
14763 l_start_op_seq_num          number:= NULL;
14764 l_start_op_seq_id           number:= NULL;
14765 l_cur_op_seq_id             number := NULL;
14766 l_cur_rtg_op_seq_num        number := NULL;
14767 l_cur_job_op_seq_num        number := NULL;
14768 l_on_reco_path              varchar2(1) := 'N';
14769 l_cur_job_qty               number := NULL;
14770 l_rtg_op_seq_num            number;
14771 l_job_op_seq_num            number;
14772 l_fm_op                     number;
14773 l_to_op                     number;
14774 l_op_sdate                  date;
14775 l_op_edate                  date;
14776 l_op_qty                    number;
14777 l_op_yield                  number;
14778 l_resource_id               number;
14779 l_res_seq_num               number;
14780 l_sub_grp_num               number;
14781 l_rpl_grp_num               number;
14782 l_instance_id               number;
14783 l_serial_num                varchar2(30);
14784 /*Added for bugfix:7248992 */
14785 l_cum_yield                 number;
14786 l_rev_cum_yield             number;
14787 
14788 l_item_id                   number;
14789 l_primary_item_id           number;
14790 l_src_phm_item_id           number;
14791 l_component_seq_id          number;
14792 l_job_start_date            date;
14793 l_job_compl_date            date;
14794 l_reco_flag                 varchar2(1);
14795 l_use_phantom_routings      number;
14796 
14797 /* ST : Detailed Scheduling */
14798 l_resource_hours            number;
14799 l_index                     number;
14800 l_parent_res_seq_num        number;
14801 l_setup_res_seq_num         number;
14802 
14803 l_v_disco_res_seq_num        t_number;
14804 /* ST : Detailed Scheduling */
14805 
14806 l_v_links                   t_links;
14807 l_v_ops                     t_ops;
14808 l_v_res                     t_op_res;
14809 l_v_res_ins                 t_res_ins;
14810 l_v_res_use                 t_res_use;
14811 l_v_comp                    t_op_comp;
14812 
14813 l_v_path_seq_num            t_number;
14814 l_v_path_ops                t_number;
14815 l_v_fm_ops                  t_number;
14816 l_v_to_ops                  t_number;
14817 
14818 l_v_rtg_op_seq              t_number;
14819 l_v_res_seq_num             t_number;
14820 l_v_instance_id             t_number;
14821 l_v_serial_num              t_serial;
14822 l_v_start_date              t_date;
14823 l_v_compl_date              t_date;
14824 l_v_recoflag                t_recoflag;
14825 
14826 l_v_op_yield                t_number;
14827 l_v_op_qty                  t_number;
14828 
14829 l_v_res_ids                 t_number;
14830 l_v_res_sub_grp             t_number;
14831 l_v_res_rpl_grp             t_number;
14832 l_v_disco_res_ids           t_number;
14833 l_v_disco_res_rpl_grp       t_number;
14834 
14835 -- ST : Detailed Scheduling....
14836 l_v_res_seq_nums            t_number;
14837 
14838 -- These two tables store the RES SEQ NUM and OP SEQ NUM for which setup resources where added..
14839 type t_setup is table of varchar2(1) index by varchar2(100);
14840 
14841 l_v_setup_op_res_num        t_setup;
14842 l_temp_par_res_seq          number;
14843 -- ST : Detailed Scheduling......
14844 
14845 l_v_res_ins_batch_id        t_number;
14846 
14847 l_v_res_use_asgn_units      t_number;
14848 
14849 l_v_comp_item_id            t_number;
14850 l_v_comp_prm_item_id        t_number;
14851 l_v_comp_phm_item_id        t_number;
14852 l_v_comp_comp_seq_id        t_number;
14853 l_v_comp_reqd_date          t_date;
14854 l_v_comp_yield              t_number;
14855 l_v_comp_qpa                t_number; -- ADD bug 3347985
14856 l_v_disco_item_id           t_number;
14857 l_v_disco_comp_seq_id       t_number;
14858 l_v_disco_src_phm_id        t_number;
14859 
14860 /* ST : Detailed Scheduling start */
14861 l_v_res_firm_type       t_number;
14862 l_v_res_setup_id        t_number;
14863 l_v_res_grp_seq_id      t_number;
14864 l_v_res_grp_seq_num     t_number;
14865 l_v_res_max_assg_units      t_number;
14866 l_v_res_parent_seq_num      t_number;
14867 l_v_res_batch_id        t_number;
14868 l_v_res_assg_units      t_number;
14869 
14870 l_v_resource_hours      t_number;
14871 
14872 l_v_res_recom              t_res_recom;
14873 /* ST : Detailed Scheduling end */
14874 
14875 /*Added for bugfix:7248992 */
14876 l_v_cum_yield                 t_number;
14877 l_v_rev_cum_yield            t_number;
14878 
14879 cursor c_wljdi is
14880     select  record_id,
14881             -- ST Fix for bug 4285032 :
14882             -- APS will be populating 8 for resource instance information
14883             -- and 9 resource instance usage information..
14884             -- Map it to WSM load type 7 --> resource instance
14885             --                         4 --> resource instance usage..
14886             decode(load_type,8,7,
14887                              9,4,
14888                  load_type) load_type,
14889             substitution_type,
14890             job_op_seq_num,
14891             routing_op_seq_num,
14892             next_routing_op_seq_num,
14893             inventory_item_id_new,
14894             inventory_item_new,
14895             primary_item,
14896             primary_item_id,
14897             src_phantom_item,
14898             src_phantom_item_id,
14899             component_sequence_id,
14900             date_required,
14901             scheduled_quantity,
14902             required_quantity,
14903             batch_id,
14904             assigned_units,
14905             instance_id_new,
14906             operation_completion_date,
14907             operation_start_date,
14908             operation_yield,
14909             resource_code_new,
14910             resource_id_new,
14911             resource_seq_num,
14912             substitute_group_num,
14913             replacement_group_num,
14914             serial_number_new,
14915             start_date,
14916             completion_date,
14917             /* ST : Detailed Scheduling */
14918             firm_type,
14919             setup_id,
14920             setup_code,
14921             group_sequence_id,
14922             group_sequence_num,
14923             max_assigned_units,
14924             parent_resource_seq_num,
14925             resource_hours,
14926             /* ST : Detailed Scheduling */
14927             /* ST : Detailed Scheduling : Added for resource adding changes */
14928             department_id          ,
14929             department_code        ,
14930             activity_id        ,
14931             standard_rate_flag     ,
14932             usage_rate_or_amount   ,
14933             basis_type          ,
14934             uom_code            ,
14935             scheduled_flag      ,
14936             autocharge_type    ,
14937             schedule_seq_num       ,
14938             attribute_category     ,
14939             attribute1         ,
14940             attribute2         ,
14941             attribute3         ,
14942             attribute4         ,
14943             attribute5         ,
14944             attribute6         ,
14945             attribute7         ,
14946             attribute8         ,
14947             attribute9         ,
14948             attribute10        ,
14949             attribute11        ,
14950             attribute12        ,
14951             attribute13        ,
14952             attribute14        ,
14953             attribute15        ,
14954             /* ST : Detailed Scheduling : Added for resource adding changes : end */
14955             -- Added For LBJ Interface Alcoa Enhancement
14956             recommended,
14957             standard_operation_code,
14958             standard_operation_id,
14959             operation_yield_enabled,
14960             minimum_transfer_quantity,
14961             backflush_flag,
14962             count_point_type,
14963             description,
14964             include_in_rollup,
14965             option_dependent_flag,
14966             inventory_item_id_old,
14967             inventory_item_old,
14968             wip_supply_type,
14969             quantity_per_assembly,
14970             supply_subinventory,
14971             supply_locator_id,
14972             mrp_net_flag,
14973             auto_request_material,
14974             comments,
14975             applied_resource_units,
14976 	    cumulative_yield, /*Added for bugfix:7248992 */
14977 	    reverse_cumulative_yield/*Added for bugfix:7248992 */
14978     from    wsm_lot_job_dtl_interface
14979     where   parent_header_id = p_parent_header_id
14980     and     process_status = WIP_CONSTANTS.RUNNING
14981     order by load_type,
14982              NVL(parent_resource_seq_num,0), -- Detailed Scheduling Code review remark...
14983              -- (Moved parent resource_seq_num before substitution_type so that setup resources are processed after runtime resource
14984              -- ST : Detailed Scheduling Added to grpup the new setup resources for a runtime resource together
14985              -- Or else we'll get a error in the BULK INSERT of add_resource procedure
14986              substitution_type,
14987              NVL(job_op_seq_num, 0), NVL(routing_op_seq_num, 0),
14988              NVL(substitute_group_num, -1), NVL(replacement_group_num, 0),
14989              NVL(resource_seq_num, 0),
14990              NVL(instance_id_new, 0), NVL(serial_number_new, 0),
14991              DECODE(load_type,WSM_SUB_DEL,NVL(src_phantom_item_id, 0)) desc,
14992              DECODE(load_type,WSM_SUB_ADD,NVL(src_phantom_item_id, 0)
14993                              ,WSM_SUB_REC,NVL(src_phantom_item_id, 0)
14994                              ,WSM_SUB_DIS,NVL(src_phantom_item_id, 0)
14995                              ,WSM_SUB_CHG,NVL(src_phantom_item_id, 0)) asc,
14996              transaction_date;
14997 
14998 cursor c_wljdi_indp is
14999     select  record_id,
15000             -- ST Fix for bug 4285032 :
15001         -- APS will be populating 8 for resource instance information
15002         -- and 9 resource instance usage information..
15003         -- Map it to WSM load type 7 --> resource instance
15004         --                         4 --> resource instance usage..
15005             decode(load_type,8,7,
15006                          9,4,
15007                  load_type) load_type,
15008             substitution_type,
15009             job_op_seq_num,
15010             routing_op_seq_num,
15011             next_routing_op_seq_num,
15012             inventory_item_id_new,
15013             inventory_item_new,
15014             primary_item,
15015             primary_item_id,
15016             src_phantom_item,
15017             src_phantom_item_id,
15018             component_sequence_id,
15019             date_required,
15020             scheduled_quantity,
15021             required_quantity,
15022             batch_id,
15023             assigned_units,
15024             instance_id_new,
15025             operation_completion_date,
15026             operation_start_date,
15027             operation_yield,
15028             resource_code_new,
15029             resource_id_new,
15030             resource_seq_num,
15031             substitute_group_num,
15032             replacement_group_num,
15033             serial_number_new,
15034             start_date,
15035             completion_date,
15036             /* ST : Detailed Scheduling */
15037             firm_type,
15038             setup_id,
15039             setup_code,
15040             group_sequence_id,
15041             group_sequence_num,
15042             max_assigned_units,
15043             parent_resource_seq_num,
15044             resource_hours,
15045             /* ST : Detailed Scheduling */
15046             /* ST : Detailed Scheduling : Added for resource adding changes */
15047             department_id          ,
15048             department_code        ,
15049             activity_id        ,
15050             standard_rate_flag     ,
15051             usage_rate_or_amount   ,
15052             --usage_rate_or_amount_inve ,
15053             basis_type          ,
15054             uom_code            ,
15055             scheduled_flag      ,
15056             --resource_offset_percent   ,
15057             autocharge_type    ,
15058             schedule_seq_num       ,
15059             --principle_flag       ,
15060             --change_notice    ,
15061             --acd_type         ,
15062             --original_system_reference,
15063             attribute_category     ,
15064             attribute1         ,
15065             attribute2         ,
15066             attribute3         ,
15067             attribute4         ,
15068             attribute5         ,
15069             attribute6         ,
15070             attribute7         ,
15071             attribute8         ,
15072             attribute9         ,
15073             attribute10        ,
15074             attribute11        ,
15075             attribute12        ,
15076             attribute13        ,
15077             attribute14        ,
15078             attribute15        ,
15079             /* ST : Detailed Scheduling : Added for resource adding changes : end */
15080             -- Added For LBJ Interface Alcoa Enhancement
15081             recommended,
15082             standard_operation_code,
15083             standard_operation_id,
15084             operation_yield_enabled,
15085             minimum_transfer_quantity,
15086             backflush_flag,
15087             count_point_type,
15088             description,
15089             include_in_rollup,
15090             option_dependent_flag,
15091             inventory_item_id_old,
15092             inventory_item_old,
15093             wip_supply_type,
15094             quantity_per_assembly,
15095             supply_subinventory,
15096             supply_locator_id,
15097             mrp_net_flag,
15098             auto_request_material,
15099             comments,
15100             applied_resource_units,
15101 	    cumulative_yield, /*Added for bugfix:7248992 */
15102 	    reverse_cumulative_yield/*Added for bugfix:7248992 */
15103     from    wsm_lot_job_dtl_interface
15104     where   parent_header_id IS NULL
15105     and     wip_entity_id     = p_wip_entity_id
15106     and     organization_id   = p_org_id
15107     and     process_status    = WIP_CONSTANTS.RUNNING
15108     and     NVL(group_id, -1) = NVL(p_group_id, -1)
15109     order by load_type,
15110              NVL(parent_resource_seq_num,0),
15111              -- Detailed Scheduling Code review remark...
15112              -- (Moved parent resource_seq_num before substitution_type so that setup resources are processed after runtime resource
15113              -- ST : Detailed Scheduling Added to grpup the new setup resources for a runtime resource together
15114              -- Or else we'll get a error in the BULK INSERT of add_resource procedure
15115              substitution_type,
15116              NVL(job_op_seq_num, 0), NVL(routing_op_seq_num, 0),
15117              NVL(substitute_group_num, -1), NVL(replacement_group_num, 0),
15118              NVL(resource_seq_num, 0),
15119              NVL(instance_id_new, 0), NVL(serial_number_new, 0),
15120              DECODE(load_type,WSM_SUB_DEL,NVL(src_phantom_item_id, 0)) desc,
15121              DECODE(load_type,WSM_SUB_ADD,NVL(src_phantom_item_id, 0)
15122                              ,WSM_SUB_REC,NVL(src_phantom_item_id, 0)
15123                              ,WSM_SUB_DIS,NVL(src_phantom_item_id, 0)
15124                              ,WSM_SUB_CHG,NVL(src_phantom_item_id, 0)) asc,
15125              transaction_date;
15126 
15127 cursor c_the_path (
15128         c_wip_entity_id     number,
15129         c_end_op_seq_num    number) is
15130     select     from_op_seq_num,
15131                to_op_seq_num
15132     from       wsm_copy_op_networks
15133     start with (to_op_seq_num = c_end_op_seq_num and
15134                 recommended = 'Y' and
15135                 wip_entity_id = c_wip_entity_id)
15136     connect by (to_op_seq_num = prior from_op_seq_num and
15137                 recommended = 'Y' and
15138                 wip_entity_id = c_wip_entity_id);
15139 
15140 cursor c_disco_res (
15141         c_wip_entity_id     number,
15142         c_rtg_op_seq_num    number,
15143         c_sub_grp_num       number,
15144         c_rpl_grp_num       number) is
15145     select   resource_id,
15146              replacement_group_num,
15147              resource_seq_num  /* ST : Detailed Scheduling added.... */
15148     from     wsm_copy_op_resources
15149     where    wip_entity_id          = c_wip_entity_id
15150     and      operation_seq_num      = c_rtg_op_seq_num
15151     and      nvl(substitute_group_num, -1)  = nvl(c_sub_grp_num, -1)
15152     and      nvl(replacement_group_num, 0) <> nvl(c_rpl_grp_num, 0);
15153 
15154 cursor c_disco_comp (
15155         c_wip_entity_id     number,
15156         c_rtg_op_seq_num    number,
15157         c_item_id           number,
15158         c_primary_item_id   number,
15159         c_src_phantom_id    number) is
15160     select   wcro.component_item_id,
15161              wcro.component_sequence_id,
15162              wcro.source_phantom_id
15163     from     wsm_copy_requirement_ops wcro
15164     where    wcro.wip_entity_id     = c_wip_entity_id
15165     and      wcro.operation_seq_num = c_rtg_op_seq_num
15166     and      wcro.component_item_id <> c_item_id
15167     and      wcro.primary_component_id = c_primary_item_id
15168     and      wcro.source_phantom_id = -1 -- IS NULL
15169     union
15170     select   wcro.component_item_id,
15171              wcro.component_sequence_id,
15172              wcro.source_phantom_id
15173     from     wsm_copy_requirement_ops wcro
15174     where    wcro.wip_entity_id        = c_wip_entity_id
15175     and      wcro.operation_seq_num    = c_rtg_op_seq_num
15176     and      wcro.primary_component_id = c_primary_item_id
15177     and      wcro.source_phantom_id <> -1 -- IS NOT NULL
15178     and      wcro.source_phantom_id <> c_src_phantom_id;
15179 
15180 
15181 e_wljdi_common_error        exception;
15182 e_invalid_discommend_path   exception;
15183 e_invalid_recommend_path    exception;
15184 e_invalid_sub_load_type     exception;
15185 e_invalid_op_seq_num        exception;
15186 e_invalid_item_name         exception;
15187 e_invalid_resource_name     exception;
15188 e_null_comp_seq_id          exception;
15189 
15190 -- Added For LBJ Interface Alcoa Enhancement
15191 l_link_exists     number;
15192 l_from_op_seq_id  number;
15193 l_to_op_seq_id    number;
15194 l_validate_network number := -1;
15195 l_scrap_account   number;
15196 l_est_absorption_account number;
15197 l_department_code varchar2(10);
15198 l_op_seq_id       number;
15199 l_item_old_id     number;
15200 x_return_status   varchar2(1);
15201 l_Supply_subinventory     varchar2(20);
15202 l_Supply_locator_id       number;
15203 l_mrp_net_flag    number;
15204 l_valid_comp      boolean;
15205 l_applied_resource_units   number;
15206 l_resource_seq_num         number;
15207 --Modified for Alcoa Enhanement as the exception is not used
15208 --e_no_supply_sub      exception;
15209 
15210 BEGIN
15211 
15212     SAVEPOINT start_import_details;
15213 
15214     p_error_code         := 0;
15215     p_error_msg          := '';
15216     l_cur_job_op_seq_num := null;
15217     l_cur_rtg_op_seq_num := null;
15218     l_cur_op_seq_id      := null;
15219 
15220 l_stmt_num := 5.1;
15221     -- get the op_seq_num of the routing end
15222     find_copy_routing_end(
15223         p_wip_entity_id,
15224         l_end_op_seq_num,
15225         l_end_op_seq_id,
15226         p_error_code,
15227         p_error_msg);
15228     if p_error_code <> 0 then
15229         raise e_wljdi_common_error;
15230     end if;
15231 
15232 l_stmt_num := 5.2;
15233     -- get the op_seq_num of the routing start
15234     find_copy_routing_start(
15235         p_wip_entity_id,
15236         l_start_op_seq_num,
15237         l_start_op_seq_id,
15238         p_error_code,
15239         p_error_msg);
15240     if p_error_code <> 0 then
15241         raise e_wljdi_common_error;
15242     end if;
15243 
15244 
15245     -- if p_wo_records_exist = 'Y', get current operation information here!
15246     if p_wo_records_exist = 'Y' then
15247 
15248 l_stmt_num := 10.1;
15249         select  status_type,
15250                 start_quantity,
15251                 quantity_scrapped
15252         into    l_job_status,
15253                 l_job_start_qty,
15254                 l_job_qty_scrapped
15255         from    wip_discrete_jobs
15256         where   wip_entity_id = p_wip_entity_id;
15257 
15258         if(l_job_status = WIP_CONSTANTS.UNRELEASED) then
15259             l_cur_job_op_seq_num := g_op_seq_incr;
15260             l_cur_rtg_op_seq_num := l_start_op_seq_num;
15261             l_cur_op_seq_id      := l_start_op_seq_id;
15262             l_cur_job_qty        := l_job_start_qty;
15263         else
15264             begin
15265 l_stmt_num := 10.2;
15266                 select  operation_seq_num,
15267                         operation_sequence_id,
15268                         quantity_in_queue + quantity_running + quantity_waiting_to_move
15269                 into    l_cur_job_op_seq_num,
15270                         l_cur_op_seq_id,
15271                         l_cur_job_qty
15272                 from    wip_operations
15273                 where   wip_entity_id = p_wip_entity_id
15274                 and     (quantity_in_queue <> 0 or
15275                          quantity_running <> 0 or
15276                          quantity_waiting_to_move <> 0);
15277                 if(l_cur_op_seq_id IS NOT NULL) then
15278                     begin
15279 l_stmt_num := 10.3;
15280                         select operation_seq_num
15281                         into   l_cur_rtg_op_seq_num
15282                         from   wsm_copy_operations
15283                         where  wip_entity_id = p_wip_entity_id
15284                         and    operation_sequence_id = l_cur_op_seq_id;
15285                     exception
15286                         when no_data_found then
15287                             NULL;
15288                     end;
15289                 end if;
15290             exception
15291                 when no_data_found then
15292                     if (l_job_status IN ( WIP_CONSTANTS.COMP_CHRG,
15293                                           WIP_CONSTANTS.COMP_NOCHRG,
15294                                           WIP_CONSTANTS.CLOSED) )
15295                     then
15296                         l_cur_job_op_seq_num := null;
15297                         l_cur_rtg_op_seq_num := l_end_op_seq_num;
15298                         l_cur_op_seq_id      := l_end_op_seq_id;
15299                         l_cur_job_qty        := l_job_start_qty - l_job_qty_scrapped;
15300                     end if;
15301             end;
15302         end if;
15303 
15304     else -- p_wo_records_exist = 'N'
15305         l_cur_rtg_op_seq_num := l_start_op_seq_num;
15306         l_cur_job_qty        := p_job_scheduled_quantity;
15307     end if;
15308 
15309     -- get job start/compl date if not specified
15310     l_job_start_date := p_job_scheduled_start_date;
15311     l_job_compl_date := p_job_scheduled_compl_date;
15312     if(l_job_start_date IS NULL or l_job_compl_date IS NULL) then
15313 l_stmt_num := 10.4;
15314         select  scheduled_start_date,
15315                 scheduled_completion_date
15316         into    l_job_start_date,
15317                 l_job_compl_date
15318         from    wip_discrete_jobs
15319         where   wip_entity_id = p_wip_entity_id;
15320     end if;
15321 
15322     -----------------------------------------------------
15323     -- default WLJDI values, and validate required fields
15324     -----------------------------------------------------
15325 l_stmt_num := 20;
15326     default_wljdi_fields(
15327         p_wip_entity_id    => p_wip_entity_id,
15328         p_org_id           => p_org_id,
15329         p_parent_header_id => p_parent_header_id,
15330         p_group_id         => p_group_id,
15331         p_wo_records_exist => p_wo_records_exist,
15332         p_error_code       => p_error_code,
15333         p_error_msg        => p_error_msg);
15334     if p_error_code <> 0 then
15335         raise e_wljdi_common_error;
15336     end if;
15337 
15338 
15339 
15340     ---------------------------------------
15341     -- empty v_wljdi and load WLJDI records
15342     ---------------------------------------
15343     -- BA: bug 3313463/3311695 8i compatibility
15344     v_wljdi_record_id               := t_wljdi_record_id();
15345     v_wljdi_load_type               := t_wljdi_load_type();
15346     v_wljdi_substitution_type       := t_wljdi_substitution_type();
15347     v_wljdi_job_op_seq_num          := t_wljdi_job_op_seq_num();
15348     v_wljdi_routing_op_seq_num      := t_wljdi_routing_op_seq_num();
15349     v_wljdi_next_rtg_op_seq_num     := t_wljdi_next_rtg_op_seq_num();
15350 
15351     v_wljdi_inventory_item_id_new   := t_wljdi_inventory_item_id_new();
15352     v_wljdi_inventory_item_new      := t_wljdi_inventory_item_new();
15353     v_wljdi_primary_item            := t_wljdi_primary_item();
15354     v_wljdi_primary_item_id         := t_wljdi_primary_item_id();
15355     v_wljdi_src_phantom_item        := t_wljdi_src_phantom_item();
15356     v_wljdi_src_phantom_item_id     := t_wljdi_src_phantom_item_id();
15357     v_wljdi_component_sequence_id   := t_wljdi_component_sequence_id();
15358     v_wljdi_date_required           := t_wljdi_date_required();
15359     v_wljdi_scheduled_quantity      := t_wljdi_scheduled_quantity();
15360     v_wljdi_required_quantity       := t_wljdi_required_quantity();
15361     v_wljdi_batch_id                := t_wljdi_batch_id();
15362     v_wljdi_assigned_units          := t_wljdi_assigned_units();
15363     v_wljdi_instance_id_new         := t_wljdi_instance_id_new();
15364     v_wljdi_op_completion_date      := t_wljdi_op_completion_date();
15365     v_wljdi_op_start_date           := t_wljdi_op_start_date();
15366     v_wljdi_op_yield                := t_wljdi_op_yield();
15367     v_wljdi_resource_code_new       := t_wljdi_resource_code_new();
15368     v_wljdi_resource_id_new         := t_wljdi_resource_id_new();
15369     v_wljdi_resource_seq_num        := t_wljdi_resource_seq_num();
15370     v_wljdi_substitute_group_num    := t_wljdi_substitute_group_num();
15371     v_wljdi_replacement_group_num   := t_wljdi_replacement_group_num();
15372     v_wljdi_serial_number_new       := t_wljdi_serial_number_new();
15373     v_wljdi_start_date              := t_wljdi_start_date();
15374     v_wljdi_completion_date         := t_wljdi_completion_date();
15375     -- EA: bug 3313463/3311695 8i compatibility
15376 
15377     /* ST : Detailed scheduling */
15378     v_wljdi_firm_type           := t_wljdi_firm_type();
15379     v_wljdi_setup_id            := t_wljdi_setup_id();
15380     v_wljdi_setup_code          := t_wljdi_setup_code();
15381     v_wljdi_grp_seq_id          := t_wljdi_grp_seq_id();
15382     v_wljdi_grp_seq_num         := t_wljdi_grp_seq_num();
15383     v_wljdi_max_assg_units      := t_wljdi_max_assg_units();
15384     v_wljdi_parent_seq_num      := t_wljdi_parent_seq_num();
15385     v_wljdi_resource_hours      := t_wljdi_resource_hours();
15386 
15387     /* ST : Detailed Scheduling : Considered for adding a setup resource.... */
15388     v_wljdi_department_id       := t_wljdi_department_id();
15389     v_wljdi_department_code     := t_wljdi_department_code();
15390 
15391     v_wljdi_activity_id         := t_wljdi_activity_id();
15392     v_wljdi_standard_rate_flag  := t_wljdi_standard_rate_flag();
15393     v_wljdi_usage_rate_or_amt   := t_wljdi_usage_rate_or_amt();
15394     v_wljdi_basis_type          := t_wljdi_basis_type();
15395     v_wljdi_uom_code            := t_wljdi_uom_code();
15396     v_wljdi_scheduled_flag      := t_wljdi_scheduled_flag();
15397     v_wljdi_res_autocharge_type := t_wljdi_res_autocharge_type();
15398     v_wljdi_schedule_seq_num    := t_wljdi_schedule_seq_num();
15399 
15400     v_wljdi_attribute_category  := t_wljdi_attribute_category();
15401     v_wljdi_attribute1          := t_wljdi_attribute1();
15402     v_wljdi_attribute2          := t_wljdi_attribute2();
15403     v_wljdi_attribute3          := t_wljdi_attribute3();
15404     v_wljdi_attribute4          := t_wljdi_attribute4();
15405     v_wljdi_attribute5          := t_wljdi_attribute5();
15406     v_wljdi_attribute6          := t_wljdi_attribute6();
15407     v_wljdi_attribute7          := t_wljdi_attribute7();
15408     v_wljdi_attribute8          := t_wljdi_attribute8();
15409     v_wljdi_attribute9          := t_wljdi_attribute9();
15410     v_wljdi_attribute10         := t_wljdi_attribute10();
15411     v_wljdi_attribute11         := t_wljdi_attribute11();
15412     v_wljdi_attribute12         := t_wljdi_attribute12();
15413     v_wljdi_attribute13         := t_wljdi_attribute13();
15414     v_wljdi_attribute14         := t_wljdi_attribute14();
15415     v_wljdi_attribute15         := t_wljdi_attribute15();
15416     /* ST : Detailed scheduling : end */
15417 
15418     -- Added For LBJ Interface Alcoa Enhancement
15419     v_wljdi_recommended         := t_wljdi_recommended();
15420     v_wljdi_std_op_code         := t_wljdi_std_op_code();
15421     v_wljdi_std_op_id           := t_wljdi_std_op_id();
15422     v_wljdi_op_yield_enable     := t_wljdi_op_yield_enable();
15423     v_wljdi_min_trf_qty         := t_wljdi_min_trf_qty();
15424     v_wljdi_backflush_flag      := t_wljdi_backflush_flag();
15425     v_wljdi_count_point_type    := t_wljdi_count_point_type();
15426     v_wljdi_description         := t_wljdi_description();
15427     v_wljdi_inc_in_rollup       := t_wljdi_inc_in_rollup();
15428     v_wljdi_option_dep_flag     := t_wljdi_option_dep_flag();
15429     v_wljdi_inventory_item_id_old   := t_wljdi_inventory_item_id_old();
15430     v_wljdi_inventory_item_old      := t_wljdi_inventory_item_old();
15431     v_wljdi_wip_supply_type         := t_wljdi_wip_supply_type();
15432     v_wljdi_quantity_per_assembly   := t_wljdi_quantity_per_assembly();
15433     v_wljdi_supply_subinventory     := t_wljdi_supply_subinventory();
15434     v_wljdi_supply_locator_id       := t_wljdi_supply_locator_id();
15435     v_wljdi_mrp_net_flag            := t_wljdi_mrp_net_flag();
15436     v_wljdi_auto_request_material   := t_wljdi_auto_request_material();
15437     v_wljdi_comments                := t_wljdi_comments();
15438     v_wljdi_applied_resource_units  := t_wljdi_applied_resource_units();
15439 
15440     /*Added for bugfix:6857061 */
15441     v_wljdi_cum_yield               := t_wljdi_cum_yield();
15442     v_wljdi_rev_cum_yield           := t_wljdi_rev_cum_yield();
15443 
15444     if(p_parent_header_id IS NULL) then
15445 l_stmt_num := 20.1;
15446         --BC: bug 3313463/3311695
15447         open c_wljdi_indp;
15448         fetch c_wljdi_indp bulk collect into
15449             v_wljdi_record_id,
15450             v_wljdi_load_type,
15451             v_wljdi_substitution_type,
15452             v_wljdi_job_op_seq_num,
15453             v_wljdi_routing_op_seq_num,
15454             v_wljdi_next_rtg_op_seq_num,
15455             v_wljdi_inventory_item_id_new,
15456             v_wljdi_inventory_item_new,
15457             v_wljdi_primary_item,
15458             v_wljdi_primary_item_id,
15459             v_wljdi_src_phantom_item,
15460             v_wljdi_src_phantom_item_id,
15461             v_wljdi_component_sequence_id,
15462             v_wljdi_date_required,
15463             v_wljdi_scheduled_quantity,
15464             v_wljdi_required_quantity,
15465             v_wljdi_batch_id,
15466             v_wljdi_assigned_units,
15467             v_wljdi_instance_id_new,
15468             v_wljdi_op_completion_date,
15469             v_wljdi_op_start_date,
15470             v_wljdi_op_yield,
15471             v_wljdi_resource_code_new,
15472             v_wljdi_resource_id_new,
15473             v_wljdi_resource_seq_num,
15474             v_wljdi_substitute_group_num,
15475             v_wljdi_replacement_group_num,
15476             v_wljdi_serial_number_new,
15477             v_wljdi_start_date,
15478             v_wljdi_completion_date,
15479             /* ST : Detailed Scheduling start */
15480             v_wljdi_firm_type,
15481             v_wljdi_setup_id,
15482             v_wljdi_setup_code,
15483             v_wljdi_grp_seq_id,
15484             v_wljdi_grp_seq_num,
15485             v_wljdi_max_assg_units,
15486             v_wljdi_parent_seq_num,
15487             v_wljdi_resource_hours,
15488             /* ST : Detailed Scheduling end */
15489             /* ST : Detailed Scheduling : Considered for adding a resource */
15490             v_wljdi_department_id      ,
15491             v_wljdi_department_code        ,
15492             v_wljdi_activity_id        ,
15493             v_wljdi_standard_rate_flag     ,
15494             v_wljdi_usage_rate_or_amt      ,
15495             v_wljdi_basis_type         ,
15496             v_wljdi_uom_code           ,
15497             v_wljdi_scheduled_flag     ,
15498             v_wljdi_res_autocharge_type    ,
15499             v_wljdi_schedule_seq_num       ,
15500 
15501             v_wljdi_attribute_category     ,
15502             v_wljdi_attribute1         ,
15503             v_wljdi_attribute2         ,
15504             v_wljdi_attribute3         ,
15505             v_wljdi_attribute4         ,
15506             v_wljdi_attribute5         ,
15507             v_wljdi_attribute6         ,
15508             v_wljdi_attribute7         ,
15509             v_wljdi_attribute8         ,
15510             v_wljdi_attribute9         ,
15511             v_wljdi_attribute10            ,
15512             v_wljdi_attribute11        ,
15513             v_wljdi_attribute12        ,
15514             v_wljdi_attribute13        ,
15515             v_wljdi_attribute14        ,
15516             v_wljdi_attribute15        ,
15517             /* ST : Detailed Scheduling :  end adding a resource changes */
15518             -- Added For LBJ Interface Alcoa Enhancement
15519             v_wljdi_recommended,
15520             v_wljdi_std_op_code,
15521             v_wljdi_std_op_id,
15522             v_wljdi_op_yield_enable,
15523             v_wljdi_min_trf_qty,
15524             v_wljdi_backflush_flag,
15525             v_wljdi_count_point_type,
15526             v_wljdi_description,
15527             v_wljdi_inc_in_rollup,
15528             v_wljdi_option_dep_flag,
15529             v_wljdi_inventory_item_id_old,
15530             v_wljdi_inventory_item_old,
15531             v_wljdi_wip_supply_type,
15532             v_wljdi_quantity_per_assembly,
15533             v_wljdi_supply_subinventory,
15534             v_wljdi_supply_locator_id,
15535             v_wljdi_mrp_net_flag,
15536             v_wljdi_auto_request_material,
15537             v_wljdi_comments,
15538             v_wljdi_applied_resource_units,
15539 	    v_wljdi_cum_yield,
15540 	    v_wljdi_rev_cum_yield
15541         ;
15542         close c_wljdi_indp;
15543         --EC: bug 3313463/3311695
15544     else
15545 l_stmt_num := 20.2;
15546         --BC: bug 3313463/3311695
15547         open c_wljdi;
15548         fetch c_wljdi bulk collect into
15549             v_wljdi_record_id,
15550             v_wljdi_load_type,
15551             v_wljdi_substitution_type,
15552             v_wljdi_job_op_seq_num,
15553             v_wljdi_routing_op_seq_num,
15554             v_wljdi_next_rtg_op_seq_num,
15555             v_wljdi_inventory_item_id_new,
15556             v_wljdi_inventory_item_new,
15557             v_wljdi_primary_item,
15558             v_wljdi_primary_item_id,
15559             v_wljdi_src_phantom_item,
15560             v_wljdi_src_phantom_item_id,
15561             v_wljdi_component_sequence_id,
15562             v_wljdi_date_required,
15563             v_wljdi_scheduled_quantity,
15564             v_wljdi_required_quantity,
15565             v_wljdi_batch_id,
15566             v_wljdi_assigned_units,
15567             v_wljdi_instance_id_new,
15568             v_wljdi_op_completion_date,
15569             v_wljdi_op_start_date,
15570             v_wljdi_op_yield,
15571             v_wljdi_resource_code_new,
15572             v_wljdi_resource_id_new,
15573             v_wljdi_resource_seq_num,
15574             v_wljdi_substitute_group_num,
15575             v_wljdi_replacement_group_num,
15576             v_wljdi_serial_number_new,
15577             v_wljdi_start_date,
15578             v_wljdi_completion_date,
15579             /* ST : Detailed Scheduling start */
15580             v_wljdi_firm_type,
15581             v_wljdi_setup_id,
15582             v_wljdi_setup_code,
15583             v_wljdi_grp_seq_id,
15584             v_wljdi_grp_seq_num,
15585             v_wljdi_max_assg_units,
15586             v_wljdi_parent_seq_num,
15587             v_wljdi_resource_hours,
15588             /* ST : Detailed Scheduling end */
15589             /* ST : Detailed Scheduling : Considered for adding a resource */
15590             v_wljdi_department_id      ,
15591             v_wljdi_department_code        ,
15592             v_wljdi_activity_id        ,
15593             v_wljdi_standard_rate_flag     ,
15594             v_wljdi_usage_rate_or_amt      ,
15595             v_wljdi_basis_type         ,
15596             v_wljdi_uom_code           ,
15597             v_wljdi_scheduled_flag     ,
15598             v_wljdi_res_autocharge_type    ,
15599             v_wljdi_schedule_seq_num       ,
15600 
15601             v_wljdi_attribute_category     ,
15602             v_wljdi_attribute1         ,
15603             v_wljdi_attribute2         ,
15604             v_wljdi_attribute3         ,
15605             v_wljdi_attribute4         ,
15606             v_wljdi_attribute5         ,
15607             v_wljdi_attribute6         ,
15608             v_wljdi_attribute7         ,
15609             v_wljdi_attribute8         ,
15610             v_wljdi_attribute9         ,
15611             v_wljdi_attribute10            ,
15612             v_wljdi_attribute11        ,
15613             v_wljdi_attribute12        ,
15614             v_wljdi_attribute13        ,
15615             v_wljdi_attribute14        ,
15616             v_wljdi_attribute15        ,
15617             /* ST : Detailed Scheduling :  end adding a resource changes */
15618             -- Added For LBJ Interface Alcoa Enhancement
15619             v_wljdi_recommended,
15620             v_wljdi_std_op_code,
15621             v_wljdi_std_op_id,
15622             v_wljdi_op_yield_enable,
15623             v_wljdi_min_trf_qty,
15624             v_wljdi_backflush_flag,
15625             v_wljdi_count_point_type,
15626             v_wljdi_description,
15627             v_wljdi_inc_in_rollup,
15628             v_wljdi_option_dep_flag,
15629             v_wljdi_inventory_item_id_old,
15630             v_wljdi_inventory_item_old,
15631             v_wljdi_wip_supply_type,
15632             v_wljdi_quantity_per_assembly,
15633             v_wljdi_supply_subinventory,
15634             v_wljdi_supply_locator_id,
15635             v_wljdi_mrp_net_flag,
15636             v_wljdi_auto_request_material,
15637             v_wljdi_comments,
15638             v_wljdi_applied_resource_units,
15639 	    v_wljdi_cum_yield,
15640 	    v_wljdi_rev_cum_yield
15641         ;
15642         close c_wljdi;
15643         --EC: bug 3313463/3311695
15644     end if;
15645 
15646     l_d := v_wljdi_record_id.first;
15647     while l_d IS NOT NULL
15648     loop
15649         l_load_type      := v_wljdi_load_type(l_d);
15650         l_sub_type       := v_wljdi_substitution_type(l_d);
15651         l_rtg_op_seq_num := v_wljdi_routing_op_seq_num(l_d);
15652         l_job_op_seq_num := v_wljdi_job_op_seq_num(l_d);
15653 
15654 l_stmt_num := 40;
15655         if (l_load_type = WSM_LOAD_RES) then            -- resource
15656             if (l_res_first IS NULL) then
15657                 l_res_first := l_d;
15658             end if;
15659 
15660         elsif (l_load_type = WSM_LOAD_COMP) then        -- component
15661             if (l_comp_first IS NULL) then
15662                 l_comp_first := l_d;
15663             end if;
15664 
15665         elsif (l_load_type = WSM_LOAD_OP) then          -- operation: load_type 3 and 4 are the same
15666             if (l_op_first IS NULL) then
15667                 l_op_first := l_d;
15668             end if;
15669        ---Added for Alcoa Enhancement
15670             if (l_sub_type = WSM_SUB_ADD) then
15671                 l_validate_network := 0;
15672             end if;
15673 
15674         elsif (l_load_type = WSM_LOAD_RES_USE) then     -- resource usage
15675             if (l_res_use_first IS NULL) then
15676                 l_res_use_first := l_d;
15677             end if;
15678 
15679         elsif (l_load_type = WSM_LOAD_LINK) then        -- link: load_type must be 4 or 5
15680             if (l_sub_type = WSM_SUB_DIS) then
15681                 l_with_discommand := true;
15682             end if;
15683             if (l_link_first IS NULL) then
15684                 l_link_first := l_d;
15685             end if;
15686             -- Added For LBJ Interface Alcoa Enhancement
15687             l_validate_network := 0;
15688 
15689         elsif (l_load_type = WSM_LOAD_LINK_OP) then     -- link or operation
15690             if (l_op_link_first IS NULL) then
15691                 l_op_link_first := l_d;
15692             end if;
15693             -- Added For LBJ Interface Alcoa Enhancement
15694             l_validate_network := 0;
15695         elsif (l_load_type = WSM_LOAD_RES_INS) then     -- resource instance
15696             if (l_res_ins_first IS NULL) then
15697                 l_res_ins_first := l_d;
15698             end if;
15699 
15700         else
15701             fnd_message.set_name('WSM','WSM_INVALID_FIELD');
15702             fnd_message.set_token('FLD_NAME', 'Load_type');
15703             p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
15704                         || fnd_message.get;
15705             p_error_code := -1;
15706             raise e_wljdi_common_error;
15707         end if;
15708         ---------------------------------------------------------------------------
15709         -- }} validete load_type, substitution_type combination and required fields
15710         ---------------------------------------------------------------------------
15711 
15712         l_d := v_wljdi_record_id.next(l_d);
15713     end loop;
15714     ------------------------------------------------
15715     -- Add Operation should precede add operation link.
15716     -- Added For LBJ Interface Alcoa Enhancement
15717     ------------------------------------------------
15718     if NOT(l_op_first IS NULL) then
15719         l_d :=l_op_first;
15720         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) =WSM_LOAD_OP)
15721         loop
15722             if (v_wljdi_substitution_type(l_d) = WSM_SUB_ADD) then
15723 
15724                 select scrap_account,est_absorption_account,department_code
15725                 into l_scrap_account,l_est_absorption_account,l_department_code
15726                 from bom_departments
15727                 where department_id = v_wljdi_department_id(l_d)
15728                 and organization_id = p_org_id;
15729 
15730                 select decode(sign(min(operation_sequence_id)),-1,min(operation_sequence_id)-1,-2),
15731                        decode(v_wljdi_routing_op_seq_num(l_d),null,max(operation_seq_num)+g_op_seq_incr,v_wljdi_routing_op_seq_num(l_d))
15732                 into l_op_seq_id,v_wljdi_routing_op_seq_num(l_d)
15733                 from wsm_copy_operations
15734                 where wip_entity_id = p_wip_entity_id
15735                 and organization_id = p_org_id;
15736 
15737                 if v_wljdi_recommended(l_d) is null then
15738                     v_wljdi_recommended(l_d) :='N';
15739                 end if;
15740 
15741                 if v_wljdi_scheduled_quantity(l_d) IS NOT NULL then
15742                     if v_wljdi_scheduled_quantity(l_d) <= 0 or v_wljdi_scheduled_quantity(l_d) > l_cur_job_qty then
15743                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
15744                         fnd_message.set_token('FLD_NAME', 'scheduled_quantity');
15745                         p_error_msg  := fnd_message.get;
15746                         p_error_code := - 1;
15747                         raise e_wljdi_common_error;
15748                     end if;
15749                 else
15750                     v_wljdi_scheduled_quantity(l_d) := l_cur_job_qty;
15751                 end if;
15752 
15753                 insert into wsm_copy_operations
15754                 (WIP_ENTITY_ID,
15755                  OPERATION_SEQ_NUM,
15756                  RECOMMENDED,
15757                  RECO_PATH_SEQ_NUM,
15758                  NETWORK_START_END,
15759                  RECO_SCHEDULED_QUANTITY,
15760                  RECO_START_DATE,
15761                  RECO_COMPLETION_DATE,
15762                  OPERATION_SEQUENCE_ID,
15763                  ROUTING_SEQUENCE_ID,
15764                  ORGANIZATION_ID,
15765                  STANDARD_OPERATION_ID,
15766                  STANDARD_OPERATION_CODE,
15767                  DEPARTMENT_ID,
15768                  DEPARTMENT_CODE,
15769                  SCRAP_ACCOUNT,
15770                  EST_ABSORPTION_ACCOUNT,
15771                  OPERATION_LEAD_TIME_PERCENT,
15772                  MINIMUM_TRANSFER_QUANTITY,
15773                  COUNT_POINT_TYPE,
15774                  OPERATION_DESCRIPTION,
15775                  EFFECTIVITY_DATE,
15776                  DISABLE_DATE,
15777                  BACKFLUSH_FLAG,
15778                  OPTION_DEPENDENT_FLAG,
15779                  OPERATION_TYPE,
15780                  REFERENCE_FLAG,
15781                  YIELD,
15782                  CUMULATIVE_YIELD,
15783                  REVERSE_CUMULATIVE_YIELD,
15784                  LABOR_TIME_CALC,
15785                  MACHINE_TIME_CALC,
15786                  TOTAL_TIME_CALC,
15787                  LABOR_TIME_USER,
15788                  MACHINE_TIME_USER,
15789                  TOTAL_TIME_USER,
15790                  NET_PLANNING_PERCENT,
15791                  X_COORDINATE,
15792                  Y_COORDINATE,
15793                  INCLUDE_IN_ROLLUP,
15794                  OPERATION_YIELD_ENABLED,
15795                  OLD_OPERATION_SEQUENCE_ID,
15796                  ACD_TYPE,
15797                  REVISED_ITEM_SEQUENCE_ID,
15798                  CHANGE_NOTICE,
15799                  ECO_FOR_PRODUCTION,
15800                  SHUTDOWN_TYPE,
15801                  ACTUAL_IPK,
15802                  CRITICAL_TO_QUALITY,
15803                  VALUE_ADDED,
15804                  LAST_UPDATE_DATE,
15805                  LAST_UPDATED_BY,
15806                  LAST_UPDATE_LOGIN,
15807                  CREATION_DATE,
15808                  CREATED_BY,
15809                  REQUEST_ID,
15810                  PROGRAM_APPLICATION_ID,
15811                  PROGRAM_ID,
15812                  PROGRAM_UPDATE_DATE,
15813                  ATTRIBUTE_CATEGORY,
15814                  ATTRIBUTE1,
15815                  ATTRIBUTE2,
15816                  ATTRIBUTE3,
15817                  ATTRIBUTE4,
15818                  ATTRIBUTE5,
15819                  ATTRIBUTE6,
15820                  ATTRIBUTE7,
15821                  ATTRIBUTE8,
15822                  ATTRIBUTE9,
15823                  ATTRIBUTE10,
15824                  ATTRIBUTE11,
15825                  ATTRIBUTE12,
15826                  ATTRIBUTE13,
15827                  ATTRIBUTE14,
15828                  ATTRIBUTE15,
15829                  ORIGINAL_SYSTEM_REFERENCE)
15830                 values
15831                 (p_wip_entity_id,
15832                  v_wljdi_routing_op_seq_num(l_d),
15833                  v_wljdi_recommended(l_d),        --recommended
15834                  null,
15835                  null,
15836                  v_wljdi_scheduled_quantity(l_d), --reco_scheduled_quantity
15837                  v_wljdi_op_start_date(l_d),      --reco_start_date
15838                  v_wljdi_op_completion_date(l_d), --reco_completion_date
15839                  l_op_seq_id,
15840                  -1,
15841                  p_org_id,
15842                  v_wljdi_std_op_id(l_d),
15843                  v_wljdi_std_op_code(l_d),
15844                  v_wljdi_department_id(l_d),
15845                  l_department_code,
15846                  l_scrap_account,
15847                  l_est_absorption_account,
15848                  null,
15849                  v_wljdi_min_trf_qty(l_d),
15850                  v_wljdi_count_point_type(l_d),
15851                  v_wljdi_description(l_d),
15852                  p_creation_date,
15853                  null,
15854                  v_wljdi_backflush_flag(l_d),
15855                  v_wljdi_option_dep_flag(l_d),
15856                  1,
15857                  2,
15858                  1,
15859                  v_wljdi_cum_yield(l_d),
15860                  v_wljdi_rev_cum_yield(l_d),
15861                  null,
15862                  null,
15863                  null,
15864                  null,
15865                  null,
15866                  null,
15867                  null,
15868                  null,
15869                  null,
15870                  v_wljdi_inc_in_rollup(l_d),
15871                  v_wljdi_op_yield_enable(l_d),
15872                  null,
15873                  null,
15874                  null,
15875                  null,
15876                  null,
15877                  null,
15878                  null,
15879                  null,
15880                  null,
15881                  p_last_updt_date,
15882                  p_last_updt_by,
15883                  p_last_updt_login,
15884                  p_creation_date,
15885                  p_created_by,
15886                  p_request_id,
15887                  p_program_appl_id,
15888                  p_program_id,
15889                  p_prog_updt_date,
15890                  v_wljdi_attribute_category(l_d),
15891                  v_wljdi_attribute1(l_d) ,
15892                  v_wljdi_attribute2(l_d) ,
15893                  v_wljdi_attribute3(l_d) ,
15894                  v_wljdi_attribute4(l_d) ,
15895                  v_wljdi_attribute5(l_d) ,
15896                  v_wljdi_attribute6(l_d) ,
15897                  v_wljdi_attribute7(l_d) ,
15898                  v_wljdi_attribute8(l_d) ,
15899                  v_wljdi_attribute9(l_d) ,
15900                  v_wljdi_attribute10(l_d),
15901                  v_wljdi_attribute11(l_d),
15902                  v_wljdi_attribute12(l_d),
15903                  v_wljdi_attribute13(l_d),
15904                  v_wljdi_attribute14(l_d),
15905                  v_wljdi_attribute15(l_d),
15906                  null);
15907 
15908                 if (v_wljdi_std_op_id(l_d) is not null) then
15909 
15910                     -- Validate if a resource of charge type PO_MOVE is present.
15911                     begin
15912                         l_temp :=0;
15913                         select 1
15914                         into l_temp
15915                         from dual
15916                         where exists ( SELECT 1
15917                                        FROM   bom_std_op_resources  bsor
15918                                        WHERE  bsor.standard_operation_id = v_wljdi_std_op_id(l_d)
15919                                        AND    bsor.autocharge_type = WIP_CONSTANTS.PO_MOVE)
15920                            or exists ( SELECT 1
15921                                        FROM   bom_std_sub_op_resources  bsor
15922                                        WHERE  bsor.standard_operation_id = v_wljdi_std_op_id(l_d)
15923                                        AND    bsor.autocharge_type = WIP_CONSTANTS.PO_MOVE);
15924                         if l_temp=1 then
15925                             fnd_message.set_name('WSM','WSM_OP_PO_MOVE');
15926                             p_error_msg  := fnd_message.get;
15927                             p_error_code := - 1;
15928                             raise e_wljdi_common_error;
15929                         end if;
15930                     exception
15931                         when no_data_found then
15932                             null;
15933                         when others then
15934                             fnd_message.set_name('WSM','WSM_OP_PO_MOVE');
15935                             p_error_msg  := fnd_message.get;
15936                             p_error_code := - 1;
15937                             raise e_wljdi_common_error;
15938                     end;
15939                     l_temp :=0;
15940                     -- This insert statement adds primary resources for standard operation.
15941 
15942                     INSERT INTO WSM_COPY_OP_RESOURCES
15943                     (wip_entity_id,
15944                      operation_seq_num,
15945                      resource_seq_num,
15946                      organization_id,
15947                      substitute_group_num,
15948                      replacement_group_num,
15949                      recommended,
15950                      reco_start_date,
15951                      reco_completion_date,
15952                      resource_id,
15953                      resource_code,
15954                      department_id,
15955                      phantom_flag,
15956                      phantom_op_seq_num,
15957                      phantom_item_id,
15958                      activity_id,
15959                      standard_rate_flag,
15960                      assigned_units,
15961                      max_assigned_units,
15962                      firm_type,
15963                      usage_rate_or_amount,
15964                      usage_rate_or_amount_inverse,
15965                      uom_code,
15966                      basis_type,
15967                      schedule_flag,
15968                      resource_offset_percent,
15969                      autocharge_type,
15970                      schedule_seq_num,
15971                      principle_flag,
15972                      setup_id,
15973                      change_notice,
15974                      acd_type,
15975                      last_update_date,
15976                      last_updated_by,
15977                      last_update_login,
15978                      creation_date,
15979                      created_by,
15980                      request_id,
15981                      program_application_id,
15982                      program_id,
15983                      program_update_date,
15984                      attribute_category,
15985                      attribute1,
15986                      attribute2,
15987                      attribute3,
15988                      attribute4,
15989                      attribute5,
15990                      attribute6,
15991                      attribute7,
15992                      attribute8,
15993                      attribute9,
15994                      attribute10,
15995                      attribute11,
15996                      attribute12,
15997                      attribute13,
15998                      attribute14,
15999                      attribute15,
16000                      original_system_reference
16001                     )
16002                     select
16003                      wco.wip_entity_id,
16004                      wco.operation_seq_num,
16005                      bsor.resource_seq_num,
16006                      wco.organization_id,
16007                      bsor.substitute_group_num,
16008                      0,                              -- repl_grp_no
16009                      'Y',
16010                      v_wljdi_op_start_date(l_d),
16011                      v_wljdi_op_completion_date(l_d),
16012                      bsor.resource_id,
16013                      br.resource_code,
16014                      v_wljdi_department_id(l_d),
16015                      null,
16016                      null,
16017                      null,
16018                      bsor.activity_id,
16019                      bsor.standard_rate_flag,
16020                      bsor.assigned_units,
16021                      bsor.assigned_units,
16022                      0,                      -- Unfirmed
16023                      bsor.usage_rate_or_amount,
16024                      bsor.usage_rate_or_amount_inverse,
16025                      br.unit_of_measure,
16026                      bsor.basis_type,
16027                      bsor.schedule_flag,
16028                      null,
16029                      bsor.autocharge_type,
16030                      null,
16031                      null,
16032                      null,
16033                      null,
16034                      null,
16035                      p_last_updt_date,
16036                      p_last_updt_by,
16037                      p_last_updt_login,
16038                      p_creation_date,
16039                      p_created_by,
16040                      p_request_id,
16041                      p_program_appl_id,
16042                      p_program_id,
16043                      p_prog_updt_date,
16044                      bsor.attribute_category,
16045                      bsor.attribute1,
16046                      bsor.attribute2,
16047                      bsor.attribute3,
16048                      bsor.attribute4,
16049                      bsor.attribute5,
16050                      bsor.attribute6,
16051                      bsor.attribute7,
16052                      bsor.attribute8,
16053                      bsor.attribute9,
16054                      bsor.attribute10,
16055                      bsor.attribute11,
16056                      bsor.attribute12,
16057                      bsor.attribute13,
16058                      bsor.attribute14,
16059                      bsor.attribute15,
16060                      null
16061                     from   wsm_copy_operations wco,
16062                            bom_resources br,
16063                            bom_std_op_resources bsor
16064                     where  bsor.standard_operation_id = v_wljdi_std_op_id(l_d)
16065                     and    br.resource_id = bsor.resource_id
16066                     and    nvl(br.disable_date, SYSDATE + 1) > SYSDATE
16067                     and    wco.organization_id = p_org_id
16068                     and    wco.wip_entity_id = p_wip_entity_id
16069                     and    wco.operation_seq_num = v_wljdi_routing_op_seq_num(l_d);
16070 
16071                     -- This insert statement adds substitute resources for standard operation.
16072 
16073                     INSERT INTO WSM_COPY_OP_RESOURCES
16074                     (wip_entity_id,
16075                      operation_seq_num,
16076                      resource_seq_num,
16077                      organization_id,
16078                      substitute_group_num,
16079                      replacement_group_num,
16080                      recommended,
16081                      reco_start_date,
16082                      reco_completion_date,
16083                      resource_id,
16084                      resource_code,
16085                      department_id,
16086                      phantom_flag,
16087                      phantom_op_seq_num,
16088                      phantom_item_id,
16089                      activity_id,
16090                      standard_rate_flag,
16091                      assigned_units,
16092                      max_assigned_units,
16093                      firm_type,
16094                      usage_rate_or_amount,
16095                      usage_rate_or_amount_inverse,
16096                      uom_code,
16097                      basis_type,
16098                      schedule_flag,
16099                      resource_offset_percent,
16100                      autocharge_type,
16101                      schedule_seq_num,
16102                      principle_flag,
16103                      setup_id,
16104                      change_notice,
16105                      acd_type,
16106                      last_update_date,
16107                      last_updated_by,
16108                      last_update_login,
16109                      creation_date,
16110                      created_by,
16111                      request_id,
16112                      program_application_id,
16113                      program_id,
16114                      program_update_date,
16115                      attribute_category,
16116                      attribute1,
16117                      attribute2,
16118                      attribute3,
16119                      attribute4,
16120                      attribute5,
16121                      attribute6,
16122                      attribute7,
16123                      attribute8,
16124                      attribute9,
16125                      attribute10,
16126                      attribute11,
16127                      attribute12,
16128                      attribute13,
16129                      attribute14,
16130                      attribute15,
16131                      original_system_reference
16132                     )
16133                     select
16134                      wco.wip_entity_id,
16135                      wco.operation_seq_num,
16136                      wcor.resource_seq_num+rownum,
16137                      wco.organization_id,
16138                      bsor.substitute_group_num,
16139                      bsor.replacement_group_num,
16140                      'N',
16141                      v_wljdi_op_start_date(l_d),
16142                      v_wljdi_op_completion_date(l_d),
16143                      bsor.resource_id,
16144                      br.resource_code,
16145                      v_wljdi_department_id(l_d),
16146                      null,
16147                      null,
16148                      null,
16149                      bsor.activity_id,
16150                      bsor.standard_rate_flag,
16151                      bsor.assigned_units,
16152                      bsor.assigned_units,
16153                      0,                      -- Unfirmed
16154                      bsor.usage_rate_or_amount,
16155                      bsor.usage_rate_or_amount_inverse,
16156                      br.unit_of_measure,
16157                      bsor.basis_type,
16158                      bsor.schedule_flag,
16159                      null,
16160                      bsor.autocharge_type,
16161                      bsor.schedule_seq_num,
16162                      null,
16163                      null,
16164                      null,
16165                      null,
16166                      p_last_updt_date,
16167                      p_last_updt_by,
16168                      p_last_updt_login,
16169                      p_creation_date,
16170                      p_created_by,
16171                      p_request_id,
16172                      p_program_appl_id,
16173                      p_program_id,
16174                      p_prog_updt_date,
16175                      bsor.attribute_category,
16176                      bsor.attribute1,
16177                      bsor.attribute2,
16178                      bsor.attribute3,
16179                      bsor.attribute4,
16180                      bsor.attribute5,
16181                      bsor.attribute6,
16182                      bsor.attribute7,
16183                      bsor.attribute8,
16184                      bsor.attribute9,
16185                      bsor.attribute10,
16186                      bsor.attribute11,
16187                      bsor.attribute12,
16188                      bsor.attribute13,
16189                      bsor.attribute14,
16190                      bsor.attribute15,
16191                      null
16192                     from   wsm_copy_operations wco,
16193                            bom_resources br,
16194                            bom_std_sub_op_resources bsor,
16195                            wsm_copy_op_resources wcor
16196                     where  bsor.standard_operation_id = v_wljdi_std_op_id(l_d)
16197                     and    br.resource_id = bsor.resource_id
16198                     and    nvl(br.disable_date, SYSDATE + 1) > SYSDATE
16199                     and    wco.organization_id = p_org_id
16200                     and    wco.wip_entity_id = p_wip_entity_id
16201                     and    wco.operation_seq_num = v_wljdi_routing_op_seq_num(l_d)
16202                     and    exists (select 1
16203                                    from bom_std_op_resources bsor1
16204                                    where bsor1.standard_operation_id = bsor.standard_operation_id
16205                                    and bsor1.substitute_group_num = bsor.substitute_group_num )
16206                     and    wcor.wip_entity_id = wco.wip_entity_id
16207                     and    wcor.operation_seq_num = wco.operation_seq_num
16208                     and    wcor.resource_seq_num = (select max(resource_seq_num)
16209                                                     from   wsm_copy_op_resources wcor1
16210                                                     where  wip_entity_id = wco.wip_entity_id
16211                                                     and operation_seq_num = wco.operation_seq_num);
16212 
16213                     -- Create default Usages for the added primary resources having schedule flag other than 'No'.
16214                     insert into wsm_copy_op_resource_usage
16215                     (wip_entity_id,
16216                      operation_seq_num,
16217                      resource_seq_num,
16218                      organization_id,
16219                      start_date,
16220                      completion_date,
16221                      assigned_units,
16222                      cumulative_processing_time,
16223                      last_update_date,
16224                      last_updated_by,
16225                      creation_date,
16226                      created_by,
16227                      last_update_login,
16228                      request_id,
16229                      program_application_id,
16230                      program_id,
16231                      program_update_date)
16232                     select
16233                      wcor.wip_entity_id,
16234                      wcor.operation_seq_num,
16235                      wcor.resource_seq_num,
16236                      wcor.organization_id,
16237                      wcor.reco_start_date,
16238                      wcor.reco_completion_date,
16239                      wcor.assigned_units,
16240                      to_number(round((wcor.reco_completion_date - wcor.reco_start_date)*1440)), -- Convert to Minutes.
16241                      p_last_updt_date,
16242                      p_last_updt_by,
16243                      p_creation_date,
16244                      p_created_by,
16245                      p_last_updt_login,
16246                      p_request_id,
16247                      p_program_appl_id,
16248                      p_program_id,
16249                      p_prog_updt_date
16250                     from wsm_copy_op_resources wcor
16251                     where wcor.organization_id = p_org_id
16252                     and wcor.wip_entity_id = p_wip_entity_id
16253                     and wcor.operation_seq_num = v_wljdi_routing_op_seq_num(l_d)
16254                     and wcor.recommended = 'Y'
16255                     and wcor.schedule_flag <> WIP_CONSTANTS.SCHED_NO;
16256 
16257                 end if;
16258             end if;
16259             l_d := v_wljdi_record_id.next(l_d);
16260         end loop;
16261     end if;
16262 
16263     l_d_saved := NULL;  -- hold the first l_d for link dis/recommendation
16264     l_path_changed := false;
16265     if NOT(l_link_first IS NULL ) then
16266         -- delete PL/SQL table
16267         l_v_links.delete;
16268         l_v_fm_ops.delete;
16269         l_v_to_ops.delete;
16270         l_v_path_ops.delete;
16271         l_v_path_seq_num.delete;
16272 
16273         -- Added For LBJ Interface Alcoa Enhancement
16274         -- Added to support Add/Delete links.
16275         l_d := l_link_first;
16276         while (l_d IS NOT NULL and
16277                v_wljdi_load_type(l_d) =WSM_LOAD_LINK and
16278                v_wljdi_substitution_type(l_d) in (WSM_SUB_ADD,WSM_SUB_DEL) )
16279         loop
16280             if v_wljdi_substitution_type(l_d) = WSM_SUB_DEL then
16281 
16282                 delete wsm_copy_op_networks
16283                 where wip_entity_id= p_wip_entity_id
16284                 and from_op_seq_num= v_wljdi_routing_op_seq_num(l_d)
16285                 and to_op_seq_num =  v_wljdi_next_rtg_op_seq_num(l_d);
16286 
16287                 if sql%notfound then
16288                     fnd_message.set_name('WSM','WSM_INV_DEL_LINK');
16289                     p_error_msg  := fnd_message.get;
16290                     p_error_code := - 1;
16291                     raise e_wljdi_common_error;
16292                 end if;
16293             else   /* This is for substitution_type add. */
16294                 begin
16295                     select 1 into l_link_exists
16296                     from wsm_copy_op_networks
16297                     where wip_entity_id = p_wip_entity_id and
16298                     from_op_seq_num = v_wljdi_routing_op_seq_num(l_d) and
16299                     to_op_seq_num = v_wljdi_next_rtg_op_seq_num(l_d);
16300                 exception
16301                     when no_data_found then
16302                         l_link_exists := 0;
16303                     when too_many_rows then
16304                         fnd_message.set_name('WSM','WSM_DUP_NETWORK_LINK');
16305                         p_error_msg  := fnd_message.get;
16306                         p_error_code := - 1;
16307                         raise e_wljdi_common_error;
16308                 end;
16309 
16310                 if l_link_exists <> 0 then
16311                     fnd_message.set_name('WSM','WSM_DUP_NETWORK_LINK');
16312                     p_error_msg  := fnd_message.get;
16313                     p_error_code := - 1;
16314                     raise e_wljdi_common_error;
16315                 end if;
16316 
16317                 begin
16318 
16319                     select operation_sequence_id
16320                     into l_from_op_seq_id
16321                     from wsm_copy_operations
16322                     where wip_entity_id = p_wip_entity_id
16323                     and operation_seq_num = v_wljdi_routing_op_seq_num(l_d);
16324 
16325                     select operation_sequence_id
16326                     into l_to_op_seq_id
16327                     from wsm_copy_operations
16328                     where wip_entity_id = p_wip_entity_id
16329                     and operation_seq_num = v_wljdi_next_rtg_op_seq_num(l_d);
16330 
16331                 exception
16332                     when no_data_found then
16333                         fnd_message.set_name('WSM','WSM_DUP_NETWORK_LINK');
16334                         p_error_msg  := fnd_message.get;
16335                         p_error_code := - 1;
16336                         raise e_wljdi_common_error;
16337                     when others then
16338                         fnd_message.set_name('WSM','WSM_INV_NETWORK_LINK');
16339                         p_error_msg  := fnd_message.get;
16340                         p_error_code := - 1;
16341                         raise e_wljdi_common_error;
16342                 end;
16343 
16344                 insert into wsm_copy_op_networks(
16345                  wip_entity_id,
16346                  from_op_seq_num,
16347                  to_op_seq_num,
16348                  from_op_seq_id,
16349                  to_op_seq_id,
16350                  recommended,
16351                  routing_sequence_id,
16352                  transition_type,
16353                  planning_pct,
16354                  last_update_date,
16355                  last_updated_by,
16356                  last_update_login,
16357                  creation_date,
16358                  created_by,
16359                  request_id,
16360                  program_application_id,
16361                  program_id,
16362                  program_update_date,
16363                  attribute_category,
16364                  attribute1,
16365                  attribute2,
16366                  attribute3,
16367                  attribute4,
16368                  attribute5,
16369                  attribute6,
16370                  attribute7,
16371                  attribute8,
16372                  attribute9,
16373                  attribute10,
16374                  attribute11,
16375                  attribute12,
16376                  attribute13,
16377                  attribute14,
16378                  attribute15,
16379                  original_system_reference)
16380                 values(
16381                  p_wip_entity_id,
16382                  v_wljdi_routing_op_seq_num(l_d),
16383                  v_wljdi_next_rtg_op_seq_num(l_d),
16384                  l_from_op_seq_id,
16385                  l_to_op_seq_id,
16386                  v_wljdi_recommended(l_d),
16387                  -1,
16388                  2,
16389                  null,
16390                  p_last_updt_date,
16391                  p_last_updt_by,
16392                  p_last_updt_login,
16393                  p_creation_date,
16394                  p_created_by,
16395                  p_request_id,
16396                  p_program_appl_id,
16397                  p_program_id,
16398                  p_prog_updt_date,
16399                  v_wljdi_attribute_category(l_d),
16400                  v_wljdi_attribute1(l_d),
16401                  v_wljdi_attribute2(l_d),
16402                  v_wljdi_attribute3(l_d),
16403                  v_wljdi_attribute4(l_d),
16404                  v_wljdi_attribute5(l_d),
16405                  v_wljdi_attribute6(l_d),
16406                  v_wljdi_attribute7(l_d),
16407                  v_wljdi_attribute8(l_d),
16408                  v_wljdi_attribute9(l_d),
16409                  v_wljdi_attribute10(l_d),
16410                  v_wljdi_attribute11(l_d),
16411                  v_wljdi_attribute12(l_d),
16412                  v_wljdi_attribute13(l_d),
16413                  v_wljdi_attribute14(l_d),
16414                  v_wljdi_attribute15(l_d),
16415                  null
16416                  );
16417             end if;
16418             l_d := v_wljdi_record_id.next(l_d);
16419         end loop;
16420 
16421 ---Modified for Alcoa Enhancement
16422 
16423     ------------------------------------------------
16424     -- {{ validete the path when the path is changed
16425     ------------------------------------------------
16426   /*  l_d_saved := NULL;  -- hold the first l_d for link dis/recommendation
16427     l_path_changed := false;
16428     if NOT(l_link_first IS NULL and l_op_link_first IS NULL) then
16429         -- delete PL/SQL table
16430         l_v_links.delete;
16431         l_v_fm_ops.delete;
16432         l_v_to_ops.delete;
16433         l_v_path_ops.delete;
16434         l_v_path_seq_num.delete;
16435      if l_with_discommand then   -- {{ with discommand record(s)
16436 l_stmt_num := 60.1;
16437             -- get recommended path from WCON, and saved to PL/SQL table
16438             open c_the_path (
16439                 p_wip_entity_id,
16440                 l_end_op_seq_num);
16441             loop
16442                 fetch c_the_path
16443                 into  l_fm_op,
16444                       l_to_op;
16445                 exit when c_the_path%notfound;
16446                 l_v_links(l_to_op).from_op_seq_num := l_fm_op;
16447                 l_v_links(l_to_op).to_op_seq_num   := l_to_op;
16448             end loop;
16449             close c_the_path;
16450 
16451 l_stmt_num := 60.2;
16452             -- delete discommended paths from PL/SQL table
16453             l_d := l_link_first;
16454             while (l_d IS NOT NULL and v_wljdi_load_type(l_d) =WSM_LOAD_LINK)
16455             loop
16456                 if(v_wljdi_substitution_type(l_d) = WSM_SUB_DIS) then
16457                     if(l_d_saved IS NULL) then
16458                         l_d_saved := l_d;
16459                     end if;
16460                     l_fm_op := v_wljdi_routing_op_seq_num(l_d);
16461                     l_to_op := v_wljdi_next_rtg_op_seq_num(l_d);
16462                     if( l_v_links.EXISTS(l_to_op) ) then
16463                         if(l_v_links(l_to_op).from_op_seq_num = l_fm_op) then
16464                             l_v_links.delete(l_to_op);
16465                             l_path_changed := true;
16466                         else
16467                             raise e_invalid_discommend_path;
16468                         end if;
16469                     else
16470                         raise e_invalid_discommend_path;
16471                     end if;
16472                 end if;
16473                 l_d := v_wljdi_record_id.next(l_d);
16474             end loop;
16475         end if;      */               -- }} with discommand record(s)
16476 
16477 l_stmt_num := 60.3;
16478         -- add recommended paths to PL/SQL table
16479         l_d := NVL(l_link_first, l_op_link_first);
16480         while (l_d IS NOT NULL and
16481                v_wljdi_load_type(l_d) IN (WSM_LOAD_LINK, WSM_LOAD_LINK_OP))
16482         loop
16483             l_load_type := v_wljdi_load_type(l_d);
16484             l_sub_type  := v_wljdi_substitution_type(l_d);
16485             l_fm_op     := v_wljdi_routing_op_seq_num(l_d);
16486             l_to_op     := v_wljdi_next_rtg_op_seq_num(l_d);
16487 
16488             if(l_load_type = WSM_LOAD_LINK and l_sub_type in (WSM_SUB_REC,WSM_SUB_DIS)) or
16489               (l_load_type = WSM_LOAD_LINK_OP and l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC) and
16490                (l_fm_op IS NOT NULL) and (l_to_op IS NOT NULL) )
16491             then
16492                ---Modified for Alcoa Enhancement
16493 	      /*	if(l_d_saved IS NULL) then
16494                     l_d_saved := l_d;
16495                 end if;
16496                 if( l_v_links.EXISTS(l_to_op) ) then
16497                     if(l_v_links(l_to_op).from_op_seq_num <> l_fm_op) then
16498                         raise e_invalid_recommend_path;
16499                     end if;
16500                 else
16501                     l_v_links(l_to_op).from_op_seq_num := l_fm_op;
16502                     l_v_links(l_to_op).to_op_seq_num   := l_to_op;
16503                     l_path_changed := true;
16504                 end if;*/
16505                 update  wsm_copy_op_networks
16506                 set     recommended            = decode(l_sub_type,WSM_SUB_DIS,'N',
16507                                                                    WSM_SUB_REC,'Y',
16508                                                                    WSM_SUB_CHG,'Y',recommended),
16509                         last_update_date       = p_last_updt_date,
16510                         last_updated_by        = p_last_updt_by,
16511                         last_update_login      = p_last_updt_login,
16512                         request_id             = p_request_id,
16513                         program_application_id = p_program_appl_id,
16514                         program_id             = p_program_id,
16515                         program_update_date    = p_prog_updt_date
16516                 where   wip_entity_id   = p_wip_entity_id
16517                 and     from_op_seq_num = l_fm_op
16518                 and     to_op_seq_num   = l_to_op;
16519 
16520                 if sql%notfound and l_sub_type = WSM_SUB_DIS then
16521                     raise e_invalid_discommend_path;
16522                 end if;
16523 
16524                 if sql%notfound and l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC) then
16525                     raise e_invalid_recommend_path;
16526                 end if;
16527             else
16528                 if ( (l_fm_op IS NULL) or (l_to_op IS NULL) )then
16529                     fnd_message.set_name('WSM','WSM_INVALID_FIELD');
16530                     fnd_message.set_token('FLD_NAME', 'Routing_op_seq and Next_routing_op_seq Combination.');
16531                     p_error_msg  := 'WLJDI(record_id ='||v_wljdi_record_id(l_d)||') '||fnd_message.get;
16532                     p_error_code := - 1;
16533                     raise e_wljdi_common_error;
16534 		end if;
16535             end if;
16536             l_d := v_wljdi_record_id.next(l_d);
16537         end loop;
16538 
16539 l_stmt_num := 60.4;
16540 ---Modified for Alcoa Enhancement
16541        /* if( l_path_changed = true) then
16542 
16543             -- validate the paths
16544             validate_wljdi_path(
16545                 p_wip_entity_id  => p_wip_entity_id,
16546                 p_end_op_seq_num => l_end_op_seq_num,
16547                 p_links          => l_v_links,
16548                 x_error_code     => p_error_code,
16549                 x_error_msg      => p_error_msg);
16550             if p_error_code <> 0 then
16551                 l_d := l_d_saved;   -- l_d can be set to null
16552                 raise e_wljdi_common_error;
16553             end if;
16554             if lbji_debug = 'Y' then
16555                 fnd_file.put_line(fnd_file.log, 'Mark new recommended path ... OK.');
16556             end if;
16557 
16558 
16559             -- {{ Begin: Update WCO, WCON to mark the recommended operation
16560 l_stmt_num := 70;
16561 
16562             l_on_reco_path := 'N';
16563 
16564             if(l_v_links.count = 0) then    -- Every link is discommended
16565                 l_out := 1;
16566                 l_v_path_ops(l_out)     := l_end_op_seq_num;
16567                 l_v_path_seq_num(l_out) := 1;
16568                 if( nvl(l_cur_rtg_op_seq_num, -1) = l_end_op_seq_num )
16569                 then
16570                     l_on_reco_path := 'Y';
16571                 end if;
16572             else
16573                 l_in := l_end_op_seq_num;
16574                 l_out := 1;
16575                 while ((l_in IS NOT NULL) and (l_v_links.exists(l_in)))
16576                 loop
16577                     l_v_fm_ops(l_out)   := l_v_links(l_in).from_op_seq_num;
16578                     l_v_to_ops(l_out)   := l_v_links(l_in).to_op_seq_num;
16579                     l_v_path_ops(l_out) := l_v_to_ops(l_out);
16580                     if( nvl(l_cur_rtg_op_seq_num, -1) = l_v_to_ops(l_out) )
16581                     then
16582                         l_on_reco_path := 'Y';
16583                     end if;
16584                     l_out := l_out + 1;
16585                     l_in  := l_v_links(l_in).from_op_seq_num;
16586                 end loop;
16587 
16588                 -- l_in is now l_v_links(last).from_op_seq_num;
16589                 if(l_in IS NOT NULL) then
16590                     l_v_path_ops(l_out) := l_in;
16591                     if(nvl(l_cur_rtg_op_seq_num, -1) = l_in )
16592                     then
16593                         l_on_reco_path := 'Y';
16594                     end if;
16595                 end if;
16596 
16597                 -- set the l_v_path_seq_num
16598                 for l_out in 1..l_v_path_ops.count
16599                 loop
16600                     l_v_path_seq_num(l_out) := l_v_path_ops.count +1 - l_out;
16601                 end loop;
16602 
16603             end if;
16604 
16605             -- We should mark WCON and WCO
16606             -- OSFM will not nullify all the dates information when the path is
16607             -- changed, nor will infinite scheduler be called. User can go to
16608             -- Lot Based Job Form to infinte schedule a job if he chooses to
16609 
16610 l_stmt_num := 70.1;
16611             -- reset WCON
16612             update  wsm_copy_op_networks
16613             set     recommended            = 'N',
16614                     last_update_date       = p_last_updt_date,
16615                     last_updated_by        = p_last_updt_by,
16616                     last_update_login      = p_last_updt_login,
16617                     request_id             = p_request_id,
16618                     program_application_id = p_program_appl_id,
16619                     program_id             = p_program_id,
16620                     program_update_date    = p_prog_updt_date
16621             where   wip_entity_id = p_wip_entity_id;
16622 
16623 l_stmt_num := 70.2;
16624             -- update WCON
16625             if(l_v_fm_ops.count <> 0) then
16626                 forall i in l_v_fm_ops.first..l_v_fm_ops.last
16627                     update  wsm_copy_op_networks
16628                     set     recommended            = 'Y',
16629                             last_update_date       = p_last_updt_date,
16630                             last_updated_by        = p_last_updt_by,
16631                             last_update_login      = p_last_updt_login,
16632                             request_id             = p_request_id,
16633                             program_application_id = p_program_appl_id,
16634                             program_id             = p_program_id,
16635                             program_update_date    = p_prog_updt_date
16636                     where   wip_entity_id = p_wip_entity_id
16637                     and     from_op_seq_num = l_v_fm_ops(i)
16638                     and     to_op_seq_num = l_v_to_ops(i);
16639             end if;
16640 
16641 l_stmt_num := 70.3;
16642             -- reset WCO
16643             update  wsm_copy_operations
16644             set     recommended            = 'N',
16645                     reco_path_seq_num      = null,
16646                     last_update_date       = p_last_updt_date,
16647                     last_updated_by        = p_last_updt_by,
16648                     last_update_login      = p_last_updt_login,
16649                     request_id             = p_request_id,
16650                     program_application_id = p_program_appl_id,
16651                     program_id             = p_program_id,
16652                     program_update_date    = p_prog_updt_date
16653             where   wip_entity_id = p_wip_entity_id;
16654 
16655 l_stmt_num := 70.4;
16656             -- mark the recomended operations
16657             forall i in 1..l_v_path_ops.count
16658                 update  wsm_copy_operations
16659                 set     recommended            = 'Y',
16660                         reco_path_seq_num      = l_v_path_seq_num(i),
16661                         last_update_date       = p_last_updt_date,
16662                         last_updated_by        = p_last_updt_by,
16663                         last_update_login      = p_last_updt_login,
16664                         request_id             = p_request_id,
16665                         program_application_id = p_program_appl_id,
16666                         program_id             = p_program_id,
16667                         program_update_date    = p_prog_updt_date
16668                 where   wip_entity_id = p_wip_entity_id
16669                 and     operation_seq_num = l_v_path_ops(i);
16670             -- }} End: Update WCO, WCON to mark the recommended operation
16671 
16672 l_stmt_num := 70.5;
16673             -- mark wlbj.on_rec_path flag
16674             update  wsm_lot_based_jobs
16675             set     on_rec_path = l_on_reco_path
16676             where   wip_entity_id = p_wip_entity_id
16677             and     organization_id = p_org_id;
16678 
16679         end if; -- if( l_path_changed = true)*/
16680 
16681     end if;
16682     ------------------------------------------------
16683     -- }} validete the path when the path is changed
16684     ------------------------------------------------
16685 l_stmt_num := 90;
16686 
16687     ------------------------------------------------
16688     -- {{ validate Network
16689     -- Added For LBJ Interface Alcoa Enhancement
16690     ------------------------------------------------
16691     if l_validate_network=0 then
16692 
16693         l_validate_network := WSMPUTIL.validate_job_network(p_wip_entity_id => p_wip_entity_id,
16694                                                             x_err_code      => p_error_code,
16695                                                             x_err_msg       => p_error_msg);
16696 
16697         if l_validate_network =1 then
16698             raise e_wljdi_common_error;
16699         end if;
16700 
16701         -- delete PL/SQL table
16702         l_v_links.delete;
16703         l_v_fm_ops.delete;
16704         l_v_to_ops.delete;
16705         l_v_path_ops.delete;
16706         l_v_path_seq_num.delete;
16707         l_on_reco_path :='N';
16708 
16709         -- Update WCO based on added or deleted links.
16710 
16711         open c_the_path (p_wip_entity_id,
16712                          l_end_op_seq_num);
16713         loop
16714             fetch c_the_path into  l_fm_op,l_to_op;
16715             exit when c_the_path%notfound;
16716 
16717             l_v_links(l_to_op).from_op_seq_num := l_fm_op;
16718             l_v_links(l_to_op).to_op_seq_num   := l_to_op;
16719 
16720         end loop;
16721         close c_the_path;
16722 
16723         if(l_v_links.count = 0) then    -- Every link is discommended
16724 
16725             l_out := 1;
16726             l_v_path_ops(l_out)     := l_end_op_seq_num;
16727             l_v_path_seq_num(l_out) := 1;
16728             if( nvl(l_cur_rtg_op_seq_num, -1) = l_end_op_seq_num )
16729             then
16730                 l_on_reco_path := 'Y';
16731             end if;
16732 
16733         else
16734 
16735             l_in := l_end_op_seq_num;
16736             l_out := 1;
16737             while ((l_in IS NOT NULL) and (l_v_links.exists(l_in)))
16738             loop
16739                 l_v_fm_ops(l_out)   := l_v_links(l_in).from_op_seq_num;
16740                 l_v_to_ops(l_out)   := l_v_links(l_in).to_op_seq_num;
16741                 l_v_path_ops(l_out) := l_v_to_ops(l_out);
16742                 if( nvl(l_cur_rtg_op_seq_num, -1) = l_v_to_ops(l_out) )
16743                 then
16744                     l_on_reco_path := 'Y';
16745                 end if;
16746                 l_out := l_out + 1;
16747                 l_in  := l_v_links(l_in).from_op_seq_num;
16748             end loop;
16749 
16750             -- l_in should have from_op of last link in recommended path when traversed from last_op.
16751             if(l_in IS NOT NULL) then
16752                 l_v_path_ops(l_out) := l_in;
16753                 if(nvl(l_cur_rtg_op_seq_num, -1) = l_in )
16754                 then
16755                     l_on_reco_path := 'Y';
16756                 end if;
16757             end if;
16758 
16759             -- set path_seq_num
16760             for l_out in 1..l_v_path_ops.count
16761             loop
16762                 l_v_path_seq_num(l_out) := l_v_path_ops.count +1 - l_out;
16763             end loop;
16764 
16765         end if;
16766 
16767         -- reset WCO
16768         update  wsm_copy_operations
16769         set     recommended            = 'N',
16770                 reco_path_seq_num      = null,
16771                 last_update_date       = p_last_updt_date,
16772                 last_updated_by        = p_last_updt_by,
16773                 last_update_login      = p_last_updt_login,
16774                 request_id             = p_request_id,
16775                 program_application_id = p_program_appl_id,
16776                 program_id             = p_program_id,
16777                 program_update_date    = p_prog_updt_date
16778         where   wip_entity_id = p_wip_entity_id;
16779 
16780         -- mark the recomended operations
16781         forall i in 1..l_v_path_ops.count
16782             update  wsm_copy_operations
16783             set     recommended            = 'Y',
16784                     reco_path_seq_num      = l_v_path_seq_num(i),
16785                     last_update_date       = p_last_updt_date,
16786                     last_updated_by        = p_last_updt_by,
16787                     last_update_login      = p_last_updt_login,
16788                     request_id             = p_request_id,
16789                     program_application_id = p_program_appl_id,
16790                     program_id             = p_program_id,
16791                     program_update_date    = p_prog_updt_date
16792             where   wip_entity_id = p_wip_entity_id
16793             and     operation_seq_num = l_v_path_ops(i);
16794 
16795         -- Null out dates for non-recommended operations.
16796         update  wsm_copy_operations
16797         set     reco_start_date      = null,
16798                 reco_completion_date = null
16799         where   wip_entity_id = p_wip_entity_id
16800         and     recommended = 'N';
16801 
16802         -- mark wlbj.on_rec_path flag
16803         update  wsm_lot_based_jobs
16804         set     on_rec_path = l_on_reco_path
16805         where   wip_entity_id = p_wip_entity_id
16806         and     organization_id = p_org_id;
16807 
16808         -- delete PL/SQL table
16809         l_v_links.delete;
16810         l_v_fm_ops.delete;
16811         l_v_to_ops.delete;
16812         l_v_path_ops.delete;
16813         l_v_path_seq_num.delete;
16814         l_on_reco_path :='N';
16815     end if;
16816     ------------------------------------------------
16817     -- }} validate Network
16818     ------------------------------------------------
16819 
16820 l_stmt_num := 100;
16821     ------------------------------------------------
16822     -- {{ validete operation
16823     ------------------------------------------------
16824     -- for load_type 3, sub_type 3 and 4,
16825     -- for load_type 6, sub_type 4,
16826     -- will only update operation_start_date, operation_completion_date,
16827     -- operation_yield, scheduled_quantity
16828 
16829     if NOT(l_op_first IS NULL and l_op_link_first IS NULL) then
16830         -- clear output PL/SQL table
16831         l_out := 0;
16832         l_v_rtg_op_seq.delete;
16833         l_v_op_yield.delete;
16834         l_v_op_qty.delete;
16835         l_v_start_date.delete;
16836         l_v_compl_date.delete;
16837  /*Added for bugfix:7248992 */
16838         l_v_cum_yield.delete;
16839         l_v_rev_cum_yield.delete;
16840 
16841 
16842         l_d := NVL(l_op_first, l_op_link_first);
16843         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) IN (WSM_LOAD_OP, WSM_LOAD_LINK_OP))
16844         loop
16845             l_load_type     := v_wljdi_load_type(l_d);
16846             l_sub_type      := v_wljdi_substitution_type(l_d);
16847             l_rtg_op_seq_num:= v_wljdi_routing_op_seq_num(l_d);
16848             l_job_op_seq_num:= v_wljdi_job_op_seq_num(l_d);
16849             l_op_qty        := v_wljdi_scheduled_quantity(l_d);
16850             l_op_yield      := v_wljdi_op_yield(l_d);
16851             l_op_sdate      := v_wljdi_op_start_date(l_d);
16852             l_op_edate      := v_wljdi_op_completion_date(l_d);
16853             /*Added for bugfix:7248992 */
16854             l_cum_yield     := v_wljdi_cum_yield(l_d);
16855             l_rev_cum_yield := v_wljdi_rev_cum_yield(l_d);
16856 
16857 
16858             l_v_ops.delete;
16859             l_v_ops(1).job_op_seq_num     := l_job_op_seq_num;
16860             l_v_ops(1).rtg_op_seq_num     := l_rtg_op_seq_num;
16861             l_v_ops(1).scheduled_quantity := l_op_qty;
16862             l_v_ops(1).yield              := l_op_yield;
16863             l_v_ops(1).start_date         := l_op_sdate;
16864             l_v_ops(1).compltion_date     := l_op_edate;
16865             /*Added for bugfix:7248992 */
16866             l_v_ops(1).cumulative_yield         := l_cum_yield;
16867             l_v_ops(1).reverse_cumulative_yield := l_rev_cum_yield;
16868 
16869             -- {{ Update WIP table
16870  -- Added For LBJ Interface Alcoa Enhancement
16871             if l_sub_type not in (WSM_SUB_DEL, WSM_SUB_ADD) then
16872 		    if(l_job_op_seq_num IS NOT NULL) then
16873 			if(l_job_op_seq_num = l_cur_job_op_seq_num) then
16874 	l_stmt_num := 110.1;
16875 			    validate_wljdi_operation (
16876 				p_wip_entity_id            => p_wip_entity_id,
16877 				p_org_id                   => p_org_id,
16878 				p_v_ops                    => l_v_ops,
16879 				p_job_scheduled_start_date => l_job_start_date,
16880 				p_job_scheduled_compl_date => l_job_compl_date,
16881 				p_job_scheduled_qty        => l_cur_job_qty,
16882 				p_handle_wip_cur_op        => 'Y',
16883 				x_error_code               => p_error_code,
16884 				x_error_msg                => p_error_msg );
16885 			    if p_error_code <> 0 then
16886 				raise e_wljdi_common_error;
16887 			    end if;
16888 	l_stmt_num := 110.2;
16889 			    -- update WO for current operation
16890 			    UPDATE WIP_OPERATIONS
16891 			    set     scheduled_quantity         = NVL(l_op_qty,   scheduled_quantity),
16892 				    first_unit_start_date      = NVL(l_op_sdate, first_unit_start_date),
16893 				    first_unit_completion_date = NVL(l_op_edate, first_unit_completion_date),
16894 				    last_unit_start_date       = NVL(l_op_sdate, last_unit_start_date),
16895 				    last_unit_completion_date  = NVL(l_op_edate, last_unit_completion_date),
16896 				    last_update_date           = p_last_updt_date,
16897 				    last_updated_by            = p_last_updt_by,
16898 				    last_update_login          = p_last_updt_login,
16899 				    request_id                 = p_request_id,
16900 				    program_application_id     = p_program_appl_id,
16901 				    program_id                 = p_program_id,
16902 				    program_update_date        = p_prog_updt_date
16903 			    where   wip_entity_id = p_wip_entity_id
16904 			    and     operation_seq_num = l_job_op_seq_num;
16905 
16906 			else    -- l_job_op_seq_num <> l_cur_job_op_seq_num
16907 			    -- this can happen when recommendations arrive after the job
16908 			    -- is moved. should give a warning message
16909 			    fnd_file.put_line(fnd_file.log,
16910 				'Warning: operation recommendations for current operation are ignored ' ||
16911 				'(record_id = ' || l_d || ')' );
16912 			end if;
16913 
16914 		    -- }} Update WIP table
16915 		    -- {{ Update WSM_COPY table
16916 		    elsif(l_rtg_op_seq_num IS NOT NULL) then
16917 	l_stmt_num := 120;
16918 			validate_wljdi_operation (
16919 			    p_wip_entity_id             => p_wip_entity_id,
16920 			    p_org_id                    => p_org_id,
16921 			    p_v_ops                     => l_v_ops,
16922 			    p_job_scheduled_start_date  => l_job_start_date,
16923 			    p_job_scheduled_compl_date  => l_job_compl_date,
16924 			    p_job_scheduled_qty         => l_cur_job_qty,
16925 			    p_handle_wip_cur_op         => 'N',
16926 			    x_error_code                => p_error_code,
16927 			    x_error_msg                 => p_error_msg );
16928 			if p_error_code <> 0 then
16929 			    raise e_wljdi_common_error;
16930 			end if;
16931 
16932 			-- save validated info to an array
16933 			l_v_rtg_op_seq(l_out) := l_rtg_op_seq_num;
16934 			l_v_op_yield(l_out)   := l_op_yield;
16935 			l_v_op_qty(l_out)     := l_op_qty;
16936 			l_v_start_date(l_out) := l_op_sdate;
16937 			l_v_compl_date(l_out) := l_op_edate;
16938 
16939 			/*Added for bugfix:7248992 */
16940 			l_v_cum_yield(l_out)       := l_cum_yield;
16941 			l_v_rev_cum_yield(l_out)   := l_rev_cum_yield;
16942 
16943 			IF lbji_debug = 'Y' THEN
16944 				 fnd_file.put_line(fnd_file.log,'After validate_wljdi_operation : ' || l_stmt_num ||
16945 						   ' Operation start date : ' || to_char(l_op_sdate,'DD-MON-YYYY HH24:MI:SS') ||
16946 						   ' Operation end date :' || to_char(l_op_edate,'DD-MON-YYYY HH24:MI:SS'));
16947 			END IF;
16948 			l_out := l_out +1;
16949 		    end if;
16950 		    -- }} Update WSM_COPY table
16951             -- Added For LBJ Interface Alcoa Enhancement
16952             else
16953                 if l_sub_type = WSM_SUB_DEL then
16954                     if l_rtg_op_seq_num is null then
16955                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
16956                         fnd_message.set_token('FLD_NAME', 'Routing_op_seq_num');
16957                         p_error_msg := fnd_message.get;
16958                         p_error_code := -1;
16959                         raise e_wljdi_common_error;
16960                     else
16961                         -- Validate for start or end operation.
16962                         l_temp :=0;
16963                         begin
16964                             select 1
16965                             into l_temp
16966                             from wsm_copy_operations wco
16967                             where wco.wip_entity_id = p_wip_entity_id
16968                             and wco.organization_id = p_org_id
16969                             and wco.operation_seq_num = l_rtg_op_seq_num
16970                             and wco.network_start_end is null;
16971                         exception
16972                             when others then
16973                                 fnd_message.set_name('WSM','WSM_NW_START_END_NO_DEL');
16974                                 p_error_msg := fnd_message.get;
16975                                 p_error_code := -1;
16976                                 raise e_wljdi_common_error;
16977                         end;
16978                         -- Validate for any existing links with this operation.
16979                         l_temp :=0;
16980 
16981                         begin
16982 
16983                             select 1
16984                             into l_temp
16985                             from dual
16986                             where exists ( select 1
16987                                            from wsm_copy_op_networks
16988                                            where wip_entity_id = p_wip_entity_id
16989                                            and from_op_seq_num = l_rtg_op_seq_num
16990                                            UNION ALL
16991                                            select 1
16992                                            from wsm_copy_op_networks
16993                                            where wip_entity_id = p_wip_entity_id
16994                                            and to_op_seq_num = l_rtg_op_seq_num);
16995 
16996                             if l_temp=1 THEN
16997 
16998                                 fnd_message.set_name('WSM','WSM_NET_LINK_EXIST');
16999                                 p_error_msg := fnd_message.get;
17000                                 p_error_code := -1;
17001                                 raise e_wljdi_common_error;
17002                             end if;
17003 
17004                         exception
17005                             when no_data_found then
17006                                 null;
17007                             when others then
17008                                 fnd_message.set_name('WSM','WSM_NET_LINK_EXIST');
17009                                 p_error_msg := fnd_message.get;
17010                                 p_error_code := -1;
17011                                 raise e_wljdi_common_error;
17012                         end;
17013                     end if;
17014 
17015                     delete from wsm_copy_operations
17016                     where wip_entity_id = p_wip_entity_id
17017                     and operation_seq_num = l_rtg_op_seq_num;
17018 
17019                     delete from wsm_copy_op_resources
17020                     where wip_entity_id = p_wip_entity_id
17021                     and operation_seq_num = l_rtg_op_seq_num;
17022 
17023                     delete from wsm_copy_op_resource_instances
17024                     where wip_entity_id = p_wip_entity_id
17025                     and operation_seq_num = l_rtg_op_seq_num;
17026 
17027                     delete from wsm_copy_op_resource_usage
17028                     where wip_entity_id = p_wip_entity_id
17029                     and operation_seq_num = l_rtg_op_seq_num;
17030 
17031                     delete from wsm_copy_requirement_ops
17032                     where wip_entity_id = p_wip_entity_id
17033                     and operation_seq_num = l_rtg_op_seq_num;
17034 
17035                 end if;
17036             end if;
17037             l_d := v_wljdi_record_id.next(l_d);
17038             if(l_d IS NOT NULL and v_wljdi_load_type(l_d) IN (4, 5)) then
17039                 l_d := l_op_link_first;         -- skip load_type = 4, 5
17040             end if;
17041         end loop;
17042         if lbji_debug = 'Y' then
17043             fnd_file.put_line(fnd_file.log, 'Mark recommended operations ... OK.');
17044         end if;
17045 
17046 l_stmt_num := 110;
17047         -- Update WCO, to mark changes
17048         if(l_out > 0) then
17049             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
17050                 UPDATE  WSM_COPY_OPERATIONS
17051                 set     yield                   = NVL(l_v_op_yield(i), yield),
17052                         reco_scheduled_quantity = NVL(l_v_op_qty(i),   reco_scheduled_quantity),
17053                         --BC: bug 3545531 we will allow user to nullify the dates on WCO
17054                         --reco_start_date         = NVL(l_v_start_date(i), reco_start_date),
17055                         --reco_completion_date    = NVL(l_v_compl_date(i), reco_completion_date),
17056                         cumulative_yield        = NVL(l_v_cum_yield(i), cumulative_yield),/*Added for bugfix:7248992 */
17057                         reverse_cumulative_yield= NVL(l_v_rev_cum_yield(i),reverse_cumulative_yield),/*Added for bugfix:7248992 */
17058                         reco_start_date         = l_v_start_date(i),
17059                         reco_completion_date    = l_v_compl_date(i),
17060                         --EC: bug 3545531
17061                         last_update_date        = p_last_updt_date,
17062                         last_updated_by         = p_last_updt_by,
17063                         last_update_login       = p_last_updt_login,
17064                         request_id              = p_request_id,
17065                         program_application_id  = p_program_appl_id,
17066                         program_id              = p_program_id,
17067                         program_update_date     = p_prog_updt_date
17068                 where   wip_entity_id = p_wip_entity_id
17069                 and     operation_seq_num = l_v_rtg_op_seq(i);
17070         end if;
17071 
17072     end if;
17073     ------------------------------------------------
17074     -- }} validete operation
17075     ------------------------------------------------
17076 
17077 l_stmt_num := 200;
17078     ------------------------------------------------
17079     -- {{ validete operation resources
17080     ------------------------------------------------
17081     if l_res_first IS NOT NULL then
17082         -- clear output PL/SQL table
17083         l_out := 0;
17084         l_v_rtg_op_seq.delete;
17085         l_v_start_date.delete;
17086         l_v_compl_date.delete;
17087         l_v_recoflag.delete;
17088         l_v_res_ids.delete;
17089         l_v_res_sub_grp.delete;
17090         l_v_res_rpl_grp.delete;
17091 
17092         /* ST : Detailed Scheduling start */
17093         l_v_res_seq_nums.delete;
17094         l_v_res_firm_type.delete;
17095         l_v_res_setup_id.delete;
17096         l_v_res_grp_seq_id.delete;
17097         l_v_res_grp_seq_num.delete;
17098         l_v_res_max_assg_units.delete;
17099         l_v_res_parent_seq_num.delete;
17100         l_v_res_batch_id.delete;
17101         l_v_res_assg_units.delete;
17102 
17103         /* will store the recommendations..... */
17104         l_v_res_recom.delete;
17105 
17106         -- ST : Detailed Scheduling..
17107         -- We'll store the data here quite simple..
17108         -- l_v_setup_op_res_num('Y-10-10') := 'Y';
17109         -- l_v_setup_op_res_num('(handle_curr_op)-(Op seq Num)-(resource seq num)') := 'Y';
17110         -- Indicates that setup resource has been added for the
17111         -- resource seq num 10, operation 10 in (WIP_OPERATIONS) --> handle curr op is Y'
17112         l_v_setup_op_res_num.delete;
17113         -- ST : Detailed Scheduling...
17114 
17115 
17116         l_d := l_res_first;
17117         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) = WSM_LOAD_RES)
17118         loop
17119 
17120         l_stmt_num := 201;
17121 
17122             l_load_type       := v_wljdi_load_type(l_d);
17123             l_sub_type        := v_wljdi_substitution_type(l_d);
17124             l_job_op_seq_num  := v_wljdi_job_op_seq_num(l_d);     -- current op only
17125             l_rtg_op_seq_num  := v_wljdi_routing_op_seq_num(l_d); -- future op only
17126             l_resource_id     := v_wljdi_resource_id_new(l_d);
17127             l_sub_grp_num     := v_wljdi_substitute_group_num(l_d);
17128             l_rpl_grp_num     := nvl(v_wljdi_replacement_group_num(l_d), 0);
17129             --Alcoa Enhancement
17130             l_applied_resource_units := v_wljdi_applied_resource_units(l_d);
17131             l_resource_seq_num       := v_wljdi_resource_seq_num(l_d) ;
17132 
17133 --          Modified for Alcoa Enhancement
17134 --            if l_sub_type = (WSM_SUB_DEL) then
17135 --                fnd_file.put_line(fnd_file.log,
17136 --                                'Warning: deleting resources for current operation '||
17137 --                                'is currently not supported and ignored.' -- CZHDBG_MSG
17138 --                                );
17139 --            end if;
17140 ---Added for Alcoa Enhancement
17141                --Alcoa Enhancement Resource Start : Delete
17142                if l_sub_type = WSM_SUB_DEL THEN
17143                    if (l_job_op_seq_num IS NOT NULL) then  -- Current Operation
17144                        if (l_job_op_seq_num = l_cur_job_op_seq_num) THEN
17145 
17146                            -- pack data into PL/SQL table l_v_res for validation
17147                            l_v_res.delete;
17148                            l_v_res(l_resource_id).job_op_seq_num  := l_job_op_seq_num;
17149                            l_v_res(l_resource_id).resource_id     := l_resource_id;
17150                            l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
17151                            l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
17152                            l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
17153                            l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
17154 
17155                             /* ST : Detailed Scheduling start */
17156                            l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17157                            l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
17158                            l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
17159                            l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
17160                            l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
17161                            l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
17162                            l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
17163                            l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
17164                            l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
17165                            l_v_res(l_resource_id).applied_resource_units  := v_wljdi_applied_resource_units(l_d);
17166 
17167                            /* ST : Detailed Scheduling end */
17168 
17169                            validate_wljdi_op_resource(
17170                                    p_wip_entity_id     => p_wip_entity_id,
17171                                    p_org_id            => p_org_id,
17172                                    p_v_res             => l_v_res,
17173                                    p_handle_wip_cur_op => 'Y',
17174                                    p_substitution_type => l_sub_type,
17175                                    x_error_code        => p_error_code,
17176                                    x_error_msg         => p_error_msg );
17177 
17178                            if p_error_code <> 0 then
17179                                raise e_wljdi_common_error;
17180                            end if;
17181 
17182                            delete from wip_operation_resources wor
17183                            where  wor.wip_entity_id     = p_wip_entity_id
17184                            and    wor.operation_seq_num = l_job_op_seq_num
17185                            and    wor.resource_seq_num  = l_resource_seq_num
17186                            and    wor.organization_id   = p_org_id;
17187 
17188                            delete from wip_sub_operation_resources wsor
17189                            where  wsor.wip_entity_id     = p_wip_entity_id
17190                            and    wsor.operation_seq_num = l_job_op_seq_num
17191                            and    wsor.resource_seq_num  = l_resource_seq_num
17192                            and    wsor.organization_id   = p_org_id;
17193 
17194                            delete from wip_op_resource_instances wori
17195                            where  wori.wip_entity_id     = p_wip_entity_id
17196                            and    wori.operation_seq_num = l_job_op_seq_num
17197                            and    wori.resource_seq_num  = l_resource_seq_num
17198                            and    wori.organization_id   = p_org_id;
17199 
17200                            delete from wip_operation_resource_usage woru
17201                            where  woru.wip_entity_id     = p_wip_entity_id
17202                            and    woru.operation_seq_num = l_job_op_seq_num
17203                            and    woru.resource_seq_num  = l_resource_seq_num
17204                            and    woru.organization_id   = p_org_id;
17205 
17206                            delete_setup_info( p_wip_entity_id     => p_wip_entity_id,
17207                                               p_org_id            => p_org_id,
17208                                               p_par_res_seq_num   => l_resource_seq_num,
17209                                               p_op_seq_num        => l_job_op_seq_num,
17210                                               p_handle_curr_op    => 'Y',
17211                                               x_error_code        => p_error_code,
17212                                               x_error_msg         => p_error_msg);
17213 
17214                            /* Need to verify if primary resources in a substitution group are completely deleted,
17215                               substitute resources in the same substitution group should also be deleted as they will
17216                               be orphaned records.*/
17217 
17218                            if p_error_code <> 0 then
17219                                raise e_wljdi_common_error;
17220                            end if;
17221 
17222                       else
17223                           fnd_file.put_line(fnd_file.log,
17224                           'Warning: Deleting or Adding resources for executed operations '||
17225                           'are currently not supported and ignored.');
17226                       end if;
17227                    elsif(l_rtg_op_seq_num IS NOT NULL) then  -- Future operation
17228 
17229                        -- pack data into PL/SQL table l_v_res for validation
17230                        l_v_res.delete;
17231                        l_v_res(l_resource_id).rtg_op_seq_num  := l_rtg_op_seq_num;
17232                        l_v_res(l_resource_id).resource_id     := l_resource_id;
17233                        l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
17234                        l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
17235                        l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
17236                        l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
17237                         /* ST : Detailed Scheduling start */
17238                        l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17239                        l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
17240                        l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
17241                        l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
17242                        l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
17243                        l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
17244                        l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
17245                        l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
17246                        l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
17247                        l_v_res(l_resource_id).applied_resource_units  := v_wljdi_applied_resource_units(l_d);
17248 
17249                        /* ST : Detailed Scheduling end */
17250 
17251                        validate_wljdi_op_resource(
17252                              p_wip_entity_id     => p_wip_entity_id,
17253                              p_org_id            => p_org_id,
17254                              p_v_res             => l_v_res,
17255                              p_handle_wip_cur_op => 'N',
17256                              p_substitution_type => l_sub_type,
17257                              x_error_code        => p_error_code,
17258                              x_error_msg         => p_error_msg );
17259 
17260                        if p_error_code <> 0 then
17261                          raise e_wljdi_common_error;
17262                        end if;
17263 
17264                        delete from wsm_copy_op_resource_usage wcoru
17265                        where  wcoru.wip_entity_id     = p_wip_entity_id
17266                        and    wcoru.operation_seq_num = l_rtg_op_seq_num
17267                        and    wcoru.resource_seq_num  = l_resource_seq_num
17268                        and    wcoru.organization_id   = p_org_id ;
17269 
17270                        delete from wsm_copy_op_resources wcor
17271                        where  wcor.wip_entity_id      = p_wip_entity_id
17272                        and    wcor.operation_seq_num  = l_rtg_op_seq_num
17273                        and    wcor.resource_seq_num   = l_resource_seq_num
17274                        and    wcor.organization_id    = p_org_id;
17275 
17276                        delete from wsm_copy_op_resource_instances wcori
17277                        where  wcori.wip_entity_id      = p_wip_entity_id
17278                        and    wcori.operation_seq_num  = l_rtg_op_seq_num
17279                        and    wcori.resource_seq_num   = l_resource_seq_num
17280                        and    wcori.organization_id    = p_org_id;
17281 
17282                        delete_setup_info( p_wip_entity_id     => p_wip_entity_id,
17283                                           p_org_id            => p_org_id,
17284                                           p_par_res_seq_num   => l_resource_seq_num,
17285                                           p_op_seq_num        => l_rtg_op_seq_num,
17286                                           p_handle_curr_op    => 'N',
17287                                           x_error_code        => p_error_code,
17288                                           x_error_msg         => p_error_msg);
17289 
17290                        if p_error_code <> 0 then
17291                            raise e_wljdi_common_error;
17292                        end if;
17293 
17294                    end if;
17295 
17296                end if;
17297  --Alcoa Enhancement Resource End : Delete
17298 
17299                --Alcoa Enhancement Resource Start : Add
17300                if l_sub_type  = (WSM_SUB_ADD) and v_wljdi_parent_seq_num(l_d) IS NULL THEN
17301 l_stmt_num := 201;
17302                    if (l_job_op_seq_num IS NOT NULL) then  -- Current Operation
17303                        l_v_res.delete;
17304                        if (l_job_op_seq_num = l_cur_job_op_seq_num) then
17305 
17306                            l_v_res(l_resource_id).job_op_seq_num  := l_job_op_seq_num;
17307                            l_v_res(l_resource_id).resource_id     := l_resource_id;
17308                            l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
17309                            l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
17310                            l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
17311                            l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
17312 
17313                             /* ST : Detailed Scheduling start */
17314                            l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17315                            l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
17316                            l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
17317                            l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
17318                            l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
17319                            l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
17320                            l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
17321                            l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
17322                            l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
17323                            l_v_res(l_resource_id).applied_resource_units  := v_wljdi_applied_resource_units(l_d);
17324                            l_v_res(l_resource_id).uom_code         := v_wljdi_uom_code(l_d);
17325                            l_v_res(l_resource_id).autocharge_type  := v_wljdi_res_autocharge_type(l_d);
17326                            l_v_res(l_resource_id).usage_rate_or_amount :=v_wljdi_usage_rate_or_amt(l_d);
17327                            l_v_res(l_resource_id).scheduled_flag       := v_wljdi_scheduled_flag(l_d);
17328                            l_v_res(l_resource_id).reco_flag            := v_wljdi_recommended(l_d);
17329                            l_v_res(l_resource_id).basis_type            := v_wljdi_basis_type(l_d);
17330                            l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17331 
17332                            validate_wljdi_op_resource(
17333                                    p_wip_entity_id     => p_wip_entity_id,
17334                                    p_org_id            => p_org_id,
17335                                    p_v_res             => l_v_res,
17336                                    p_handle_wip_cur_op => 'Y',
17337                                    p_substitution_type => l_sub_type,
17338                                    x_error_code        => p_error_code,
17339                                    x_error_msg         => p_error_msg );
17340 
17341                            if p_error_code <> 0 then
17342                                raise e_wljdi_common_error;
17343                            end if;
17344 
17345                            IF l_v_res(l_resource_id).reco_flag = 'Y' then
17346 
17347                                insert into wip_operation_resources
17348                                (wip_entity_id,
17349                                 operation_seq_num,
17350                                 resource_seq_num,
17351                                 organization_id,
17352                                 department_id,
17353                                 resource_id,
17354                                 uom_code,
17355                                 basis_type,
17356                                 usage_rate_or_amount,
17357                                 activity_id,
17358                                 scheduled_flag,
17359                                 assigned_units,
17360                                 maximum_assigned_units,
17361                                 batch_id,
17362                                 firm_flag,
17363                                 group_sequence_id,
17364                                 group_sequence_number,
17365                                 parent_resource_seq,
17366                                 autocharge_type,
17367                                 standard_rate_flag,
17368                                 applied_resource_units,
17369                                 applied_resource_value,
17370                                 start_date,
17371                                 completion_date,
17372                                 attribute_category,
17373                                 attribute1,
17374                                 attribute2,
17375                                 attribute3,
17376                                 attribute4,
17377                                 attribute5,
17378                                 attribute6,
17379                                 attribute7,
17380                                 attribute8,
17381                                 attribute9,
17382                                 attribute10,
17383                                 attribute11,
17384                                 attribute12,
17385                                 attribute13,
17386                                 attribute14,
17387                                 attribute15,
17388                                 schedule_seq_num,
17389                                 substitute_group_num,
17390                                 replacement_group_num,
17391                                 setup_id,
17392                                 last_update_date,
17393                                 last_updated_by,
17394                                 creation_date,
17395                                 created_by,
17396                                 last_update_login,
17397                                 request_id,
17398                                 program_application_id,
17399                                 program_id,
17400                                 program_update_date
17401                                )
17402                                values
17403                                (p_wip_entity_id,
17404                                 l_job_op_seq_num,
17405                                 l_v_res(l_resource_id).res_seq_num,
17406                                 p_org_id,
17407                                 v_wljdi_department_id(l_d),
17408                                 v_wljdi_resource_id_new(l_d),
17409                                 l_v_res(l_resource_id).uom_code,
17410                                 l_v_res(l_resource_id).basis_type,
17411                                 v_wljdi_usage_rate_or_amt(l_d),
17412                                 l_v_res(l_resource_id).activity_id,
17413                                 l_v_res(l_resource_id).scheduled_flag,
17414                                 v_wljdi_assigned_units(l_d),
17415                                 l_v_res(l_resource_id).max_assg_units,
17416                                 v_wljdi_batch_id(l_d),
17417                                 l_v_res(l_resource_id).firm_type,
17418                                 v_wljdi_grp_seq_id(l_d),
17419                                 v_wljdi_grp_seq_num(l_d),
17420                                 null,
17421                                 l_v_res(l_resource_id).autocharge_type,
17422                                 l_v_res(l_resource_id).standard_rate,
17423                                 0,
17424                                 0,
17425                                 l_v_res(l_resource_id).start_date,
17426                                 l_v_res(l_resource_id).completion_date,
17427                                 v_wljdi_attribute_category(l_d),
17428                                 v_wljdi_attribute1(l_d),
17429                                 v_wljdi_attribute2(l_d),
17430                                 v_wljdi_attribute3(l_d),
17431                                 v_wljdi_attribute4(l_d),
17432                                 v_wljdi_attribute5(l_d),
17433                                 v_wljdi_attribute6(l_d),
17434                                 v_wljdi_attribute7(l_d),
17435                                 v_wljdi_attribute8(l_d),
17436                                 v_wljdi_attribute9(l_d),
17437                                 v_wljdi_attribute10(l_d),
17438                                 v_wljdi_attribute11(l_d),
17439                                 v_wljdi_attribute12(l_d),
17440                                 v_wljdi_attribute13(l_d),
17441                                 v_wljdi_attribute14(l_d),
17442                                 v_wljdi_attribute15(l_d),
17443                                 v_wljdi_schedule_seq_num(l_d),
17444                                 v_wljdi_substitute_group_num(l_d),
17445                                 l_v_res(l_resource_id).rpl_grp_num,
17446                                 l_v_res(l_resource_id).setup_id,
17447                                 p_last_updt_date,
17448                                 p_last_updt_by,
17449                                 p_creation_date,
17450                                 p_created_by,
17451                                 p_last_updt_login,
17452                                 p_request_id,
17453                                 p_program_appl_id,
17454                                 p_program_id,
17455                                 p_prog_updt_date);
17456 
17457                                 -- If resource is PO_RECIPT, we should create requisition.
17458                                 if l_v_res(l_resource_id).autocharge_type = WIP_CONSTANTS.PO_RECEIPT then
17459 
17460                                     wip_osp.create_requisition(
17461                                        p_wip_entity_id          => p_wip_entity_id,
17462                                        p_organization_id        => p_org_id,
17463                                        p_repetitive_schedule_id => null,
17464                                        p_operation_seq_num      => l_job_op_seq_num,
17465                                        p_resource_seq_num       => l_v_res(l_resource_id).res_seq_num,
17466                                        p_run_reqimport          => wip_constants.no);
17467 
17468                                     g_num_of_osp_exists := nvl(g_num_of_osp_exists,0) + 1;
17469 
17470                                 end if;
17471                            ELSE
17472                                insert into wip_sub_operation_resources
17473                                (wip_entity_id,
17474                                 operation_seq_num,
17475                                 resource_seq_num,
17476                                 organization_id,
17477                                 department_id,
17478                                 last_update_date,
17479                                 last_updated_by,
17480                                 creation_date,
17481                                 created_by,
17482                                 last_update_login,
17483                                 resource_id,
17484                                 uom_code,
17485                                 basis_type,
17486                                 usage_rate_or_amount,
17487                                 activity_id,
17488                                 scheduled_flag,
17489                                 assigned_units,
17490                                 maximum_assigned_units,
17491                                 autocharge_type,
17492                                 standard_rate_flag,
17493                                 applied_resource_units,
17494                                 applied_resource_value,
17495                                 attribute_category,
17496                                 attribute1,
17497                                 attribute2,
17498                                 attribute3,
17499                                 attribute4,
17500                                 attribute5,
17501                                 attribute6,
17502                                 attribute7,
17503                                 attribute8,
17504                                 attribute9,
17505                                 attribute10,
17506                                 attribute11,
17507                                 attribute12,
17508                                 attribute13,
17509                                 attribute14,
17510                                 attribute15,
17511                                 completion_date,
17512                                 start_date,
17513                                 schedule_seq_num,
17514                                 substitute_group_num,
17515                                 replacement_group_num,
17516                                 setup_id,
17517                                 request_id,
17518                                 program_application_id,
17519                                 program_id,
17520                                 program_update_date
17521                                 )
17522                                values
17523                                (p_wip_entity_id,
17524                                 l_job_op_seq_num,
17525                                 l_v_res(l_resource_id).res_seq_num,
17526                                 p_org_id,
17527                                 v_wljdi_department_id(l_d),
17528                                 p_last_updt_date,
17529                                 p_last_updt_by,
17530                                 p_creation_date,
17531                                 p_created_by,
17532                                 p_last_updt_login,
17533                                 v_wljdi_resource_id_new(l_d),
17534                                 l_v_res(l_resource_id).uom_code,
17535                                 l_v_res(l_resource_id).basis_type,
17536                                 v_wljdi_usage_rate_or_amt(l_d),
17537                                 l_v_res(l_resource_id).activity_id,
17538                                 l_v_res(l_resource_id).scheduled_flag,
17539                                 v_wljdi_assigned_units(l_d),
17540                                 l_v_res(l_resource_id).max_assg_units,
17541                                 l_v_res(l_resource_id).autocharge_type,
17542                                 l_v_res(l_resource_id).standard_rate,
17543                                 0,
17544                                 0,
17545                                 v_wljdi_attribute_category(l_d),
17546                                 v_wljdi_attribute1(l_d),
17547                                 v_wljdi_attribute2(l_d),
17548                                 v_wljdi_attribute3(l_d),
17549                                 v_wljdi_attribute4(l_d),
17550                                 v_wljdi_attribute5(l_d),
17551                                 v_wljdi_attribute6(l_d),
17552                                 v_wljdi_attribute7(l_d),
17553                                 v_wljdi_attribute8(l_d),
17554                                 v_wljdi_attribute9(l_d),
17555                                 v_wljdi_attribute10(l_d),
17556                                 v_wljdi_attribute11(l_d),
17557                                 v_wljdi_attribute12(l_d),
17558                                 v_wljdi_attribute13(l_d),
17559                                 v_wljdi_attribute14(l_d),
17560                                 v_wljdi_attribute15(l_d),
17561                                 l_v_res(l_resource_id).start_date,
17562                                 l_v_res(l_resource_id).completion_date,
17563                                 v_wljdi_schedule_seq_num(l_d),
17564                                 v_wljdi_substitute_group_num(l_d),
17565                                 l_v_res(l_resource_id).rpl_grp_num,
17566                                 l_v_res(l_resource_id).setup_id,
17567                                 p_request_id,
17568                                 p_program_appl_id,
17569                                 p_program_id,
17570                                 p_prog_updt_date);
17571                           END IF;
17572 
17573                           if l_v_res(l_resource_id).scheduled_flag <> WIP_CONSTANTS.SCHED_NO and
17574                              l_v_res(l_resource_id).reco_flag = 'Y' then
17575 
17576                               insert into wip_operation_resource_usage
17577                               (wip_entity_id,
17578                                operation_seq_num,
17579                                resource_seq_num,
17580                                organization_id,
17581                                start_date,
17582                                completion_date,
17583                                assigned_units,
17584                                last_update_date,
17585                                last_updated_by,
17586                                creation_date,
17587                                created_by,
17588                                last_update_login,
17589                                request_id,
17590                                program_application_id,
17591                                program_id,
17592                                program_update_date,
17593                                cumulative_processing_time)
17594                               values
17595                               (p_wip_entity_id,
17596                                l_job_op_seq_num,
17597                                l_v_res(l_resource_id).res_seq_num,
17598                                p_org_id,
17599                                l_v_res(l_resource_id).start_date,
17600                                l_v_res(l_resource_id).completion_date,
17601                                v_wljdi_assigned_units(l_d),
17602                                p_last_updt_date,
17603                                p_last_updt_by,
17604                                p_creation_date,
17605                                p_created_by,
17606                                p_last_updt_login,
17607                                p_request_id,
17608                                p_program_appl_id,
17609                                p_program_id,
17610                                p_prog_updt_date,
17611                                to_number(round((l_v_res(l_resource_id).completion_date - l_v_res(l_resource_id).start_date)*1440)));
17612                            end if;
17613                       else
17614                           fnd_file.put_line(fnd_file.log,
17615                                  'Warning: Deleting or Adding resources for executed operations '||
17616                                  'is currently not supported and ignored.');
17617                       END if;
17618                   ELSIF(l_rtg_op_seq_num IS NOT NULL) THEN
17619 
17620                       l_v_res.delete;
17621                       l_v_res(l_resource_id).rtg_op_seq_num  := l_rtg_op_seq_num;
17622                       l_v_res(l_resource_id).resource_id     := l_resource_id;
17623                       l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
17624                       l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
17625                       l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
17626                       l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
17627 
17628                        /* ST : Detailed Scheduling start */
17629                       l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17630                       l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
17631                       l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
17632                       l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
17633                       l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
17634                       l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
17635                       l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
17636                       l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
17637                       l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
17638                       l_v_res(l_resource_id).applied_resource_units  := v_wljdi_applied_resource_units(l_d);
17639                       l_v_res(l_resource_id).uom_code         := v_wljdi_uom_code(l_d);
17640                       l_v_res(l_resource_id).autocharge_type  := v_wljdi_res_autocharge_type(l_d);
17641                       l_v_res(l_resource_id).usage_rate_or_amount :=v_wljdi_usage_rate_or_amt(l_d);
17642                       l_v_res(l_resource_id).scheduled_flag := v_wljdi_scheduled_flag(l_d);
17643                       l_v_res(l_resource_id).reco_flag      := v_wljdi_recommended(l_d);
17644                       l_v_res(l_resource_id).resource_code  := v_wljdi_resource_code_new(l_d);
17645                       l_v_res(l_resource_id).basis_type     := v_wljdi_basis_type(l_d);
17646                       l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17647 
17648                       validate_wljdi_op_resource(
17649                               p_wip_entity_id     => p_wip_entity_id,
17650                               p_org_id            => p_org_id,
17651                               p_v_res             => l_v_res,
17652                               p_handle_wip_cur_op => 'N',
17653                               p_substitution_type => l_sub_type,
17654                               x_error_code        => p_error_code,
17655                               x_error_msg         => p_error_msg );
17656 
17657                       if p_error_code <> 0 then
17658                           raise e_wljdi_common_error;
17659                       end if;
17660 
17661                       INSERT INTO WSM_COPY_OP_RESOURCES
17662                       (wip_entity_id,
17663                        operation_seq_num,
17664                        resource_seq_num,
17665                        organization_id,
17666                        substitute_group_num,
17667                        replacement_group_num,
17668                        recommended,
17669                        reco_start_date,
17670                        reco_completion_date,
17671                        resource_id,
17672                        resource_code,
17673                        department_id,
17674                        phantom_flag,
17675                        phantom_op_seq_num,
17676                        phantom_item_id,
17677                        activity_id,
17678                        standard_rate_flag,
17679                        assigned_units,
17680                        max_assigned_units,
17681                        firm_type,
17682                        usage_rate_or_amount,
17683                        usage_rate_or_amount_inverse,
17684                        uom_code,
17685                        basis_type,
17686                        schedule_flag,
17687                        resource_offset_percent,
17688                        autocharge_type,
17689                        schedule_seq_num,
17690                        principle_flag,
17691                        setup_id,
17692                        change_notice,
17693                        acd_type,
17694                        last_update_date,
17695                        last_updated_by,
17696                        last_update_login,
17697                        creation_date,
17698                        created_by,
17699                        request_id,
17700                        program_application_id,
17701                        program_id,
17702                        program_update_date,
17703                        attribute_category,
17704                        attribute1,
17705                        attribute2,
17706                        attribute3,
17707                        attribute4,
17708                        attribute5,
17709                        attribute6,
17710                        attribute7,
17711                        attribute8,
17712                        attribute9,
17713                        attribute10,
17714                        attribute11,
17715                        attribute12,
17716                        attribute13,
17717                        attribute14,
17718                        attribute15,
17719                        original_system_reference
17720                        )
17721                        values
17722                        (p_wip_entity_id,
17723                         l_rtg_op_seq_num,
17724                         l_v_res(l_resource_id).res_seq_num,
17725                         p_org_id,
17726                         v_wljdi_substitute_group_num(l_d),
17727                         nvl(v_wljdi_replacement_group_num(l_d),0),
17728                         l_v_res(l_resource_id).reco_flag,
17729                         l_v_res(l_resource_id).start_date,
17730                         l_v_res(l_resource_id).completion_date,
17731                         v_wljdi_resource_id_new(l_d),
17732                         l_v_res(l_resource_id).resource_code,
17733                         v_wljdi_department_id(l_d),
17734                         null,
17735                         null,
17736                         null,
17737                         l_v_res(l_resource_id).activity_id,
17738                         l_v_res(l_resource_id).standard_rate,
17739                         v_wljdi_assigned_units(l_d),
17740                         l_v_res(l_resource_id).max_assg_units,
17741                         l_v_res(l_resource_id).firm_type,
17742                         v_wljdi_usage_rate_or_amt(l_d),
17743                         1/v_wljdi_usage_rate_or_amt(l_d),
17744                         l_v_res(l_resource_id).uom_code,
17745                         l_v_res(l_resource_id).basis_type,
17746                         l_v_res(l_resource_id).scheduled_flag,
17747                         null,
17748                         l_v_res(l_resource_id).autocharge_type,
17749                         v_wljdi_schedule_seq_num(l_d),
17750                         null,
17751                         l_v_res(l_resource_id).setup_id,
17752                         null,
17753                         null,
17754                         p_last_updt_date,
17755                         p_last_updt_by,
17756                         p_last_updt_login,
17757                         p_creation_date,
17758                         p_created_by,
17759                         p_request_id,
17760                         p_program_appl_id,
17761                         p_program_id,
17762                         p_prog_updt_date,
17763                         v_wljdi_attribute_category(l_d),
17764                         v_wljdi_attribute1(l_d),
17765                         v_wljdi_attribute2(l_d),
17766                         v_wljdi_attribute3(l_d),
17767                         v_wljdi_attribute4(l_d),
17768                         v_wljdi_attribute5(l_d),
17769                         v_wljdi_attribute6(l_d),
17770                         v_wljdi_attribute7(l_d),
17771                         v_wljdi_attribute8(l_d),
17772                         v_wljdi_attribute9(l_d),
17773                         v_wljdi_attribute10(l_d),
17774                         v_wljdi_attribute11(l_d),
17775                         v_wljdi_attribute12(l_d),
17776                         v_wljdi_attribute13(l_d),
17777                         v_wljdi_attribute14(l_d),
17778                         v_wljdi_attribute15(l_d),
17779                         null);
17780 
17781                         if l_v_res(l_resource_id).reco_flag='Y' and
17782                            l_v_res(l_resource_id).scheduled_flag <> WIP_CONSTANTS.SCHED_NO then
17783 
17784                             insert into wsm_copy_op_resource_usage
17785                             (wip_entity_id,
17786                              operation_seq_num,
17787                              resource_seq_num,
17788                              organization_id,
17789                              start_date,
17790                              completion_date,
17791                              assigned_units,
17792                              cumulative_processing_time,
17793                              last_update_date,
17794                              last_updated_by,
17795                              creation_date,
17796                              created_by,
17797                              last_update_login,
17798                              request_id,
17799                              program_application_id,
17800                              program_id,
17801                              program_update_date)
17802                             values
17803                             (p_wip_entity_id,
17804                             l_rtg_op_seq_num,
17805                             l_v_res(l_resource_id).res_seq_num,
17806                             p_org_id,
17807                             l_v_res(l_resource_id).start_date,
17808                             l_v_res(l_resource_id).completion_date,
17809                             v_wljdi_assigned_units(l_d),
17810                             to_number(round((l_v_res(l_resource_id).completion_date - l_v_res(l_resource_id).start_date)*1440)),
17811                             p_last_updt_date,
17812                             p_last_updt_by,
17813                             p_creation_date,
17814                             p_created_by,
17815                             p_last_updt_login,
17816                             p_request_id,
17817                             p_program_appl_id,
17818                             p_program_id,
17819                             p_prog_updt_date);
17820 
17821                        end if;
17822 
17823                    END if; --l_job_op_seq_num not null
17824                END if; --wsm_sub_add
17825                --Alcoa Enhancement Resource End : Add
17826 
17827             -- can only a setup resource..
17828             if l_sub_type  = (WSM_SUB_ADD) and v_wljdi_parent_seq_num(l_d) IS NOT NULL then
17829 
17830                 /* This is primarily used to delete both the resource and its setup from the v_wldji... so that the Adding is once for the resource and
17831                 its setup resource
17832                 ... */
17833 
17834                 l_stmt_num := 202;
17835 
17836                 p_error_code := 0;
17837                 p_error_msg  := null;
17838 
17839                 l_temp_par_res_seq := v_wljdi_parent_seq_num(l_d);
17840 
17841                 add_resource (p_wip_entity_id       => p_wip_entity_id,
17842                               p_org_id              => p_org_id,
17843                               p_job_op_seq_num      => l_job_op_seq_num,
17844                               p_cur_job_op_seq_num  => l_cur_job_op_seq_num,
17845                               p_rtg_op_seq_num      => l_rtg_op_seq_num,
17846                               p_load_type           => l_load_type,
17847                               p_sub_type            => l_sub_type,
17848                               p_res_recom           => l_v_res_recom,
17849                               p_last_updt_date      => p_last_updt_date,
17850                               p_last_updt_by        => p_last_updt_by,
17851                               p_creation_date       => p_creation_date,
17852                               p_created_by          => p_created_by,
17853                               p_last_updt_login     => p_last_updt_login,
17854                               p_request_id          => p_request_id,
17855                               p_program_appl_id     => p_program_appl_id,
17856                               p_program_id          => p_program_id,
17857                               p_prog_updt_date      => p_prog_updt_date,
17858                               p_global_index        => l_d,
17859                               x_error_code          => p_error_code,
17860                               x_error_msg           => p_error_msg
17861                              );
17862 
17863                 l_stmt_num := 203;
17864 
17865                 if p_error_code <> 0 then
17866                         raise e_wljdi_common_error;
17867                 end if;
17868 
17869                 -- ST : Detailed Scheduling ...
17870                 -- If setup resources are added for a resource, keep track of it..
17871                 IF l_job_op_seq_num IS NOT NULL THEN
17872 
17873                         -- Added to WIP Operations...
17874                         fnd_file.put_line(fnd_file.log,'Adding : ' || 'Y'||'-'||l_job_op_seq_num||'-'||l_temp_par_res_seq);
17875                         l_v_setup_op_res_num('Y'||'-'||l_job_op_seq_num||'-'||l_temp_par_res_seq) := 'Y';
17876 
17877                 ELSIF l_rtg_op_seq_num IS NOT NULL THEN
17878 
17879                         fnd_file.put_line(fnd_file.log,'Adding : ' || 'N'||'-'||l_rtg_op_seq_num||'-'||l_temp_par_res_seq);
17880                         l_v_setup_op_res_num('N'||'-'||l_rtg_op_seq_num||'-'||l_temp_par_res_seq) := 'Y';
17881 
17882                 END IF;
17883 
17884                 l_stmt_num := 204;
17885 --        Modified for Alcoa Enhancement
17886 --        elsif l_sub_type  = (WSM_SUB_ADD) and v_wljdi_parent_seq_num(l_d) IS NULL then
17887 --                fnd_file.put_line(fnd_file.log,
17888 --                                'Warning: Add a runtime resources is currently not supported and ignored.' -- CZHDBG_MSG
17889 --                                 );
17890 --
17891 --                l_d := v_wljdi_record_id.next(l_d);
17892 
17893         elsif l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC) then
17894                 -- {{ Update WIP table
17895                 if(l_job_op_seq_num IS NOT NULL) then
17896 
17897                     if(l_job_op_seq_num = l_cur_job_op_seq_num) then
17898 l_stmt_num := 250.1;
17899                         -- pack data into PL/SQL table l_v_res for validation
17900                         l_v_res.delete;
17901                         l_v_res(l_resource_id).job_op_seq_num  := l_job_op_seq_num;
17902                         l_v_res(l_resource_id).resource_id     := l_resource_id;
17903                         l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
17904                         l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
17905                         l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
17906                         l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
17907 
17908                         /* ST : Detailed Scheduling start */
17909                         l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17910                         l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
17911                         l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
17912                         l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
17913                         l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
17914                         l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
17915                         l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
17916                         l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
17917                         l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
17918                         /* ST : Detailed Scheduling end */
17919 
17920                         if(l_sub_type = WSM_SUB_REC) then
17921                             -- get all the resource in the same rpl grp
17922                             -- this local search will change index l_d
17923                             l_temp := v_wljdi_record_id.next(l_d);
17924                             while(l_temp IS NOT NULL and
17925                                   v_wljdi_load_type(l_temp)             = l_load_type and
17926                                   v_wljdi_substitution_type(l_temp)     = l_sub_type and
17927                                   v_wljdi_job_op_seq_num(l_temp)        = l_job_op_seq_num and
17928                                   nvl(v_wljdi_substitute_group_num(l_temp), -1) = nvl(l_sub_grp_num, -1) and
17929                                   nvl(v_wljdi_replacement_group_num(l_temp), 0) = nvl(l_rpl_grp_num, 0) )
17930                             loop
17931                                 l_d := l_temp;
17932                                 l_resource_id := v_wljdi_resource_id_new(l_d);
17933                                 l_v_res(l_resource_id).job_op_seq_num  := l_job_op_seq_num;
17934                                 l_v_res(l_resource_id).resource_id     := l_resource_id;
17935                                 l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
17936                                 l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
17937                                 l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
17938                                 l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
17939 
17940                                 /* ST : Detailed Scheduling start */
17941                                 l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17942                                 l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
17943                                 l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
17944                                 l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
17945                                 l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
17946                                 l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
17947                                 l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
17948                                 l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
17949                                 l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
17950                                 /* ST : Detailed Scheduling end */
17951 
17952                                 l_temp := v_wljdi_record_id.next(l_d);
17953                             end loop;
17954                         end if;
17955 
17956                         l_stmt_num := 250.2;
17957 
17958                         validate_wljdi_op_resource( p_wip_entity_id     => p_wip_entity_id,
17959                                                     p_org_id            => p_org_id,
17960                                                     p_v_res             => l_v_res,
17961                                                     p_handle_wip_cur_op => 'Y',
17962                                                     p_substitution_type => l_sub_type,
17963                                                     x_error_code        => p_error_code,
17964                                                     x_error_msg         => p_error_msg );
17965                         if p_error_code <> 0 then
17966                             raise e_wljdi_common_error;
17967                         end if;
17968 
17969 
17970                         -- Delete only for resources for which setup resources are not added..
17971                         -- Delete the all the setup resources of the resources which are being updated...
17972                         l_index := l_v_res.first;
17973                         while l_index is not null loop
17974                                 IF not(l_v_setup_op_res_num.exists('Y'||'-'||l_job_op_seq_num||'-'||l_v_res(l_index).res_seq_num)) THEN
17975                                         p_error_code := 0;
17976                                         p_error_msg  := null;
17977                                         IF lbji_debug = 'Y' THEN
17978                                                 fnd_file.put_line(fnd_file.log,'import job details : before calling delete setup resource '
17979                                                                 || ' parent res.... : ' || l_v_res(l_index).res_seq_num );
17980                                         END IF;
17981 
17982                                         -- ST : Detailed Scheduling... should we remove the resources' setup resources that are being replaced....
17983 
17984                                         delete_setup_info( p_wip_entity_id             => p_wip_entity_id,
17985                                                            p_org_id                    => p_org_id,
17986                                                            p_par_res_seq_num           => l_v_res(l_index).res_seq_num,
17987                                                            p_op_seq_num                => l_job_op_seq_num,
17988                                                            p_handle_curr_op            => 'Y',
17989                                                            x_error_code                => p_error_code,
17990                                                            x_error_msg                 => p_error_msg
17991                                                          );
17992 
17993                                          if p_error_code <> 0 then
17994                                                 -- error out...
17995                                                 raise e_wljdi_common_error;
17996                                          end if;
17997                                 END IF;
17998                                 l_index := l_v_res.next(l_index);
17999                         end loop;
18000 
18001                         if(l_sub_type = WSM_SUB_REC) then
18002                             -- swap the records in WOR and WSOR
18003                             -- This wip procdure will take care of applied_resource_units
18004                             -- and WORU/WORI records
18005 
18006                             -- BA: bug 3537390 should check if we need to call REPLACE_RESOURCES
18007                             l_swap := 0;
18008                             begin
18009                                 l_stmt_num := 250.3;
18010                                 select   0
18011                                 into     l_swap
18012                                 from     wip_operation_resources
18013                                 where    wip_entity_id         = p_wip_entity_id
18014                                 and      operation_seq_num     = l_job_op_seq_num
18015                                 and      NVL(substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
18016                                 and      NVL(replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
18017                             exception
18018                                 when no_data_found then
18019                                     l_swap := 1;
18020                                 when too_many_rows then
18021                                     l_swap := 0;
18022                             end;
18023                             -- EA: bug 3537390
18024 
18025                             if(l_swap = 1) then         -- bug 3537390
18026                                 begin
18027                                     l_stmt_num := 250.4;
18028                                     select  1
18029                                     into    l_exists
18030                                     from    wip_cost_txn_interface
18031                                     where   wip_entity_id = p_wip_entity_id
18032                                     and     transaction_type = 1
18033                                     and     operation_seq_num = l_job_op_seq_num
18034                                     and     organization_id = p_org_id;
18035                                 exception
18036                                     when no_data_found then
18037                                         l_exists := 0;
18038                                     when too_many_rows then
18039                                         l_exists := 1;
18040                                 end;
18041                                 if(l_exists = 1) then
18042                                     fnd_message.set_name(application => 'WIP',
18043                                                          name        => 'WIP_REPLACE_APPLIED_RES');
18044                                     p_error_msg := fnd_message.get;
18045                                     p_error_code := -1;
18046                                     raise e_wljdi_common_error;
18047                                 end if;
18048                                 l_stmt_num := 250.5;
18049 
18050                                 WIP_SUB_OP_RESOURCES_PKG.REPLACE_RESOURCES(
18051                                     l_wip_entity_id           => p_wip_entity_id,
18052                                     l_operation_seq_num       => l_job_op_seq_num,
18053                                     l_substitute_group_num    => l_sub_grp_num,
18054                                     l_replacement_group_num   => l_rpl_grp_num,
18055                                     x_status                  => l_status,
18056                                     x_msg_count               => l_msg_count,
18057                                     x_msg_data                => p_error_msg);
18058 
18059                                 if(l_status <> fnd_api.g_ret_sts_success) then
18060                                     p_error_code := -1;
18061                                     p_error_msg := 'Error: WIP_SUB_OP_RESOURCES_PKG.REPLACE_RESOURCES ' || p_error_msg;
18062                                     raise e_wljdi_common_error;
18063                                 end if;
18064 
18065                             end if; -- l_swap = 1 (bug 3537390)
18066                         end if;
18067 
18068 
18069                         if(l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC)) then
18070                             -- update the records in WOR or WSOR
18071                                 l_resource_id := l_v_res.first;
18072                                 l_stmt_num := 250.6;
18073                                 while(l_resource_id IS NOT NULL)    -- bug 3736602 added loop
18074                                 loop
18075 
18076                                         UPDATE WIP_OPERATION_RESOURCES
18077                                         set     start_date              = NVL(l_v_res(l_resource_id).start_date,
18078                                                                                 start_date),
18079                                                 completion_date         = NVL(l_v_res(l_resource_id).completion_date,
18080                                                                                 completion_date),
18081 
18082                                                 /* ST : Detailed Scheduling start */
18083                                                 firm_flag               =  NVL(l_v_res(l_resource_id).firm_type,firm_flag),
18084                                                 setup_id                =  NVL(l_v_res(l_resource_id).setup_id,setup_id),
18085                                                 group_sequence_id       =  NVL(l_v_res(l_resource_id).grp_seq_id, group_sequence_id),
18086                                                 group_sequence_number   =  NVL(l_v_res(l_resource_id).grp_seq_num, group_sequence_number),
18087                                                 assigned_units          =  NVL(l_v_res(l_resource_id).assigned_units,assigned_units),
18088                                                 maximum_assigned_units  =  NVL(l_v_res(l_resource_id).max_assg_units,maximum_assigned_units),
18089                                                 --parent_resource_seq   =  NVL(l_v_res(l_resource_id).parent_seq_num,parent_resource_seq),
18090                                                 batch_id                =  NVL(l_v_res(l_resource_id).batch_id,batch_id),
18091                                                 /* ST : Detailed Scheduling end */
18092                                                 last_update_date        = p_last_updt_date,
18093                                                 last_updated_by         = p_last_updt_by,
18094                                                 last_update_login       = p_last_updt_login,
18095                                                 creation_date           = p_creation_date,
18096                                                 created_by              = p_created_by,
18097                                                 request_id              = p_request_id,
18098                                                 program_application_id  = p_program_appl_id,
18099                                                 program_id              = p_program_id,
18100                                                 program_update_date     = p_prog_updt_date
18101                                             where   wip_entity_id         = p_wip_entity_id
18102                                             and     organization_id       = p_org_id
18103                                             and     operation_seq_num     = l_job_op_seq_num
18104                                             and     resource_id           = l_resource_id
18105                                             -- ST : Detailed Scheduling .. use resource seq num when possible...
18106                                             and     resource_seq_num      = nvl(l_v_res(l_resource_id).res_seq_num,resource_seq_num)
18107                                             -- ST : Detailed Scheduling end...
18108                                             and     nvl(substitute_group_num, -1) = nvl(l_sub_grp_num, -1)
18109                                             and     nvl(replacement_group_num, 0) = nvl(l_rpl_grp_num, 0);
18110 
18111                                             l_resource_id := l_v_res.next(l_resource_id);
18112                                 end loop;
18113                                 -- bug 3736602 added loop
18114                                 l_stmt_num := 250.7;
18115                                 if(SQL%ROWCOUNT = 0) then
18116                                         -- change should be made in WSOR
18117                                         l_resource_id := l_v_res.first;
18118                                         while(l_resource_id IS NOT NULL)    -- bug 3736602 added loop
18119                                         loop
18120 
18121                                                 UPDATE WIP_SUB_OPERATION_RESOURCES
18122                                                 set     start_date              = NVL(l_v_res(l_resource_id).start_date,
18123                                                                                       start_date),
18124                                                         completion_date         = NVL(l_v_res(l_resource_id).completion_date,
18125                                                                                       completion_date),
18126                                                         /* ST : Detailed Scheduling start */
18127                                                         assigned_units          = NVL(l_v_res(l_resource_id).assigned_units,assigned_units),
18128                                                         maximum_assigned_units  = NVL(l_v_res(l_resource_id).max_assg_units,maximum_assigned_units),
18129                                                         /* ST : Detailed Scheduling end */
18130                                                         last_update_date        = p_last_updt_date,
18131                                                         last_updated_by         = p_last_updt_by,
18132                                                         last_update_login       = p_last_updt_login,
18133                                                         creation_date           = p_creation_date,
18134                                                         created_by              = p_created_by,
18135                                                         request_id              = p_request_id,
18136                                                         program_application_id  = p_program_appl_id,
18137                                                         program_id              = p_program_id,
18138                                                         program_update_date     = p_prog_updt_date
18139                                                 where   wip_entity_id         = p_wip_entity_id
18140                                                 and     organization_id       = p_org_id
18141                                                 and     operation_seq_num     = l_job_op_seq_num
18142                                                 and     resource_id           = l_resource_id
18143                                                 -- ST : Detailed Scheduling .. use resource seq num when possible...
18144                                                 and     resource_seq_num      = nvl(l_v_res(l_resource_id).res_seq_num,resource_seq_num)
18145                                                 -- ST : Detailed Scheduling end...
18146                                                 and     nvl(substitute_group_num, -1) = nvl(l_sub_grp_num, -1)
18147                                                 and     nvl(replacement_group_num, 0) = nvl(l_rpl_grp_num, 0);
18148 
18149                                                 l_resource_id := l_v_res.next(l_resource_id);
18150                                         end loop;                       -- bug 3736602 added loop
18151                                 end if;
18152                         end if;
18153 
18154                     else    -- l_job_op_seq_num <> l_cur_job_op_seq_num
18155                         -- this can happen when recommendations arrive after the job
18156                         -- is moved. should give a warning message
18157                         fnd_file.put_line(fnd_file.log,
18158                             'Warning: resource recommendations for current operation are ignored ' ||
18159                             '(record_id = ' || l_d || ')' );
18160                     end if;
18161                     -- }} Update WIP table
18162 
18163                 -- {{ Update WSM_COPY table
18164                 elsif(l_rtg_op_seq_num IS NOT NULL) then
18165 l_stmt_num := 260.1;
18166                     fnd_file.put_line(fnd_file.log,'import job details : Resource Updation...' || ' Rtg Op seq Num : ' || l_rtg_op_seq_num);
18167 
18168                     -- pack data into PL/SQL table l_v_res for validation
18169                     l_v_res.delete;
18170                     l_v_res(l_resource_id).rtg_op_seq_num  := l_rtg_op_seq_num;
18171                     l_v_res(l_resource_id).resource_id     := l_resource_id;
18172                     l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
18173                     l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
18174                     l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
18175                     l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
18176 
18177                     /* ST : Detailed Scheduling start */
18178                     l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
18179                     l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
18180                     l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
18181                     l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
18182                     l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
18183                     l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
18184                     l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
18185                     l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
18186                     l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
18187                     /* ST : Detailed Scheduling end */
18188 
18189                     if(l_sub_type = WSM_SUB_REC) then
18190                         -- get all the resource in the same rpl grp
18191                         -- this local search will change index l_d
18192                         l_temp := v_wljdi_record_id.next(l_d);
18193                         while(l_temp IS NOT NULL and
18194                               v_wljdi_load_type(l_temp)             = l_load_type and
18195                               v_wljdi_substitution_type(l_temp)     = l_sub_type and
18196                               v_wljdi_routing_op_seq_num(l_temp)    = l_rtg_op_seq_num and
18197                               nvl(v_wljdi_substitute_group_num(l_temp), -1) = nvl(l_sub_grp_num, -1) and
18198                               nvl(v_wljdi_replacement_group_num(l_temp), 0) = nvl(l_rpl_grp_num, 0) )
18199                         loop
18200                             l_d := l_temp;
18201                             l_resource_id := v_wljdi_resource_id_new(l_d);
18202                             l_v_res(l_resource_id).rtg_op_seq_num  := l_rtg_op_seq_num;
18203                             l_v_res(l_resource_id).resource_id     := l_resource_id;
18204                             l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
18205                             l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
18206                             l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
18207                             l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
18208 
18209                             /* ST : Detailed Scheduling start */
18210                             l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
18211                             l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
18212                             l_v_res(l_resource_id).setup_id    := v_wljdi_setup_id(l_d);
18213                             l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
18214                             l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
18215                             l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
18216                             l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
18217                             l_v_res(l_resource_id).batch_id    := v_wljdi_batch_id(l_d);
18218                             l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
18219                             /* ST : Detailed Scheduling end */
18220 
18221                             IF lbji_debug = 'Y' THEN
18222                                     fnd_file.put_line(fnd_file.log,'import job details : Recomm. the resource..' || ' Rtg Op seq Num : ' || l_rtg_op_seq_num
18223                                             || ' Res ID .... : ' || l_resource_id
18224                                             || ' date : ' || v_wljdi_start_date(l_d) );
18225                             END IF;
18226 
18227                             l_temp := v_wljdi_record_id.next(l_d);
18228                         end loop;
18229                     end if;
18230 
18231                     l_stmt_num := 260.2;
18232 
18233                     validate_wljdi_op_resource(
18234                             p_wip_entity_id     => p_wip_entity_id,
18235                             p_org_id            => p_org_id,
18236                             p_v_res             => l_v_res,
18237                             p_handle_wip_cur_op => 'N',
18238                             p_substitution_type => l_sub_type,
18239                             x_error_code        => p_error_code,
18240                             x_error_msg         => p_error_msg );
18241 
18242                     if p_error_code <> 0 then
18243                         raise e_wljdi_common_error;
18244                     end if;
18245 
18246                     l_stmt_num := 260.21;
18247 
18248 
18249                     -- Delete the setup resources for the resources for whci setup resources where not added in this run...
18250                     -- these resources are recommended one..
18251                     -- Delete the all the setup resources of the resources which are being updated...
18252 
18253                     l_index :=  l_v_res.first;
18254 
18255                     while l_index is not null loop
18256                         --- fnd_file.put_line(fnd_file.log,'import job details : Loop resource counter : ' ||  l_index);
18257                         IF not(l_v_setup_op_res_num.exists('N'||'-'||l_rtg_op_seq_num||'-'||l_v_res(l_index).res_seq_num)) THEN
18258 
18259                             p_error_code := 0;
18260                             p_error_msg  := null;
18261                             l_stmt_num := 260.22;
18262                             -- ST : Detailed Scheduling... should we remove the resources' setup resources that are being replaced....
18263                             delete_setup_info( p_wip_entity_id             => p_wip_entity_id,
18264                                        p_org_id                    => p_org_id,
18265                                        p_par_res_seq_num           => l_v_res(l_index).res_seq_num,
18266                                        p_op_seq_num                => l_rtg_op_seq_num,
18267                                        p_handle_curr_op            => 'N',
18268                                        x_error_code                => p_error_code,
18269                                        x_error_msg                 => p_error_msg
18270                                      );
18271 
18272                             l_stmt_num := 260.23;
18273 
18274                             if p_error_code <> 0 then
18275                                 -- error out...
18276                                 raise e_wljdi_common_error;
18277                             end if;
18278                         END IF;
18279                         l_stmt_num := 260.24;
18280                         l_index := l_v_res.next(l_index);
18281                     end loop;
18282 
18283                     l_stmt_num := 260.3;
18284 
18285                     -- save validated info to an array
18286                     if(l_sub_type = WSM_SUB_CHG) then
18287                         l_resource_id := l_v_res.first;
18288                         l_v_rtg_op_seq(l_out)  := l_rtg_op_seq_num;
18289                         l_v_res_ids(l_out)     := l_resource_id;
18290                         l_v_res_sub_grp(l_out) := l_sub_grp_num;
18291                         l_v_res_rpl_grp(l_out) := l_rpl_grp_num;
18292                         l_v_start_date(l_out)  := l_v_res(l_resource_id).start_date;
18293                         l_v_compl_date(l_out)  := l_v_res(l_resource_id).completion_date;
18294 
18295                         /* ST : Detailed Scheduling start */
18296                         l_v_res_seq_nums(l_out)         := l_v_res(l_resource_id).res_seq_num ;
18297                         l_v_res_firm_type(l_out)        := l_v_res(l_resource_id).firm_type ;
18298                         l_v_res_setup_id(l_out)         := l_v_res(l_resource_id).setup_id ;
18299                         l_v_res_grp_seq_id(l_out)       := l_v_res(l_resource_id).grp_seq_id  ;
18300                         l_v_res_grp_seq_num(l_out)      := l_v_res(l_resource_id).grp_seq_num  ;
18301                         l_v_res_max_assg_units(l_out)   := l_v_res(l_resource_id).max_assg_units ;
18302                         l_v_res_parent_seq_num(l_out)   := l_v_res(l_resource_id).parent_seq_num ;
18303                         l_v_res_batch_id(l_out)         := l_v_res(l_resource_id).batch_id;
18304                         l_v_res_assg_units(l_out)       := l_v_res(l_resource_id).assigned_units;
18305                         /* ST : Detailed Scheduling end */
18306 
18307                         l_v_recoflag(l_out)    := NULL;
18308                         l_out := l_out +1;
18309 
18310                     elsif(l_sub_type = WSM_SUB_REC) then
18311                         l_resource_id := l_v_res.first;
18312                         while (l_resource_id IS NOT NULL)
18313                         loop
18314                             l_v_rtg_op_seq(l_out)  := l_rtg_op_seq_num;
18315                             l_v_res_ids(l_out)     := l_resource_id;
18316                             l_v_res_sub_grp(l_out) := l_sub_grp_num;
18317                             l_v_res_rpl_grp(l_out) := l_rpl_grp_num;
18318                             l_v_start_date(l_out)  := l_v_res(l_resource_id).start_date;
18319                             l_v_compl_date(l_out)  := l_v_res(l_resource_id).completion_date;
18320 
18321                             /* ST : Detailed Scheduling start */
18322                             l_v_res_seq_nums(l_out)   := l_v_res(l_resource_id).res_seq_num ;
18323                             l_v_res_firm_type(l_out)      := l_v_res(l_resource_id).firm_type ;
18324                             l_v_res_setup_id(l_out)       := l_v_res(l_resource_id).setup_id ;
18325                             l_v_res_grp_seq_id(l_out)     := l_v_res(l_resource_id).grp_seq_id  ;
18326                             l_v_res_grp_seq_num(l_out)    := l_v_res(l_resource_id).grp_seq_num  ;
18327                             l_v_res_max_assg_units(l_out) := l_v_res(l_resource_id).max_assg_units ;
18328                             l_v_res_parent_seq_num(l_out) := l_v_res(l_resource_id).parent_seq_num ;
18329                             l_v_res_batch_id(l_out)       := l_v_res(l_resource_id).batch_id;
18330                             l_v_res_assg_units(l_out)     := l_v_res(l_resource_id).assigned_units;
18331                             /* ST : Detailed Scheduling end */
18332 
18333                             IF lbji_debug = 'Y' THEN
18334                                     fnd_file.put_line(fnd_file.log,'import job details : Recomm. the resource..' || ' Rtg Op seq Num : ' || l_rtg_op_seq_num
18335                                                         || ' Res ID .... : ' || l_resource_id
18336                                                         || ' date : ' || l_v_res(l_resource_id).start_date );
18337                             END IF;
18338 
18339                             l_v_recoflag(l_out)    := 'Y';
18340 
18341                             /* here do it..... */
18342                             l_hash_str := to_char(p_wip_entity_id) || ':' || to_char(l_rtg_op_seq_num) || ':' || to_char(l_resource_id);
18343                             l_hash_value := dbms_utility.get_hash_value(l_hash_str,35,1048576); /* 2^20 */
18344                             l_v_res_recom(l_hash_value) := 'Y';
18345                             /* ........................... */
18346 
18347                             l_resource_id := l_v_res.NEXT(l_resource_id);
18348                             l_out := l_out +1;
18349 
18350                         end loop;
18351                         l_stmt_num := 260.4;
18352                         -- get the other resource with different l_rpl_grp_num
18353                         l_v_disco_res_ids.delete;
18354                         l_v_disco_res_rpl_grp.delete;
18355 
18356                         /* ST : Detailed Scheduling added... */
18357                         l_v_disco_res_seq_num.delete;
18358                         /* ST : Detailed Scheduling added... */
18359 
18360                         open c_disco_res (
18361                             p_wip_entity_id,
18362                             l_rtg_op_seq_num,
18363                             l_sub_grp_num,
18364                             l_rpl_grp_num);
18365 
18366                         fetch c_disco_res bulk collect into
18367                             l_v_disco_res_ids,
18368                             l_v_disco_res_rpl_grp,
18369                             l_v_disco_res_seq_num; /* ST : Detailed Scheduling added... */
18370 
18371                          if(c_disco_res%rowcount > 0) then
18372                                 l_idx2 := l_v_disco_res_ids.first;
18373 
18374                                 while(l_idx2 IS NOT NULL)
18375                                 loop
18376                                        l_v_rtg_op_seq(l_out)  := l_rtg_op_seq_num;
18377                                        l_v_res_ids(l_out)     := l_v_disco_res_ids(l_idx2);
18378 
18379                                        l_v_res_sub_grp(l_out) := l_sub_grp_num;
18380                                        l_v_res_rpl_grp(l_out) := l_v_disco_res_rpl_grp(l_idx2);
18381                                        l_v_start_date(l_out)  := NULL;     -- CZHDBG: why nullify?
18382                                        l_v_compl_date(l_out)  := NULL;     -- CZHDBG: why nullify?
18383                                        l_v_recoflag(l_out)    := 'N';
18384 
18385                                        /* ST : Detailed scheduling... if we have to change the DS populated data.. add code here to
18386                                           null the new fields
18387                                        */
18388                                        l_v_res_seq_nums(l_out)     := l_v_disco_res_seq_num(l_idx2);
18389                                        l_v_res_firm_type(l_out)    := 0; /* none */
18390                                        l_v_res_setup_id(l_out)     := null;
18391                                        l_v_res_grp_seq_id(l_out)   := null;
18392                                        l_v_res_grp_seq_id(l_out)   := null;
18393                                        l_v_res_batch_id(l_out)     := null;
18394                                        l_v_res_max_assg_units(l_out)   := null;
18395                                        l_v_res_assg_units(l_out)   := null;
18396 
18397                                         IF lbji_debug = 'Y' THEN
18398                                                fnd_file.put_line(fnd_file.log,'import job details : Replacing the resource..' || ' Rtg Op seq Num : ' || l_rtg_op_seq_num
18399                                                                 || ' parent res.... : ' || l_v_disco_res_seq_num(l_idx2) );
18400                                         END IF;
18401                                         /* here do it..... */
18402                                         /* not required... but check it with maruthi...
18403                                         since
18404                                         i) Add setup resources to resource X
18405                                         ii) Replace resource X.
18406 
18407                                         This shouldnt be a problem only that we will still insert setup resources and then delete it here...
18408                                         */
18409 
18410                                         l_hash_str := to_char(p_wip_entity_id) || ':' || to_char(l_rtg_op_seq_num) || ':' || to_char(l_v_disco_res_ids(l_idx2));
18411                                         l_hash_value := dbms_utility.get_hash_value(l_hash_str,35,1048576); -- 2^20
18412                                         l_v_res_recom(l_hash_value) := 'N';
18413                                         /* ........................... */
18414                                         /* Add code here to delete the setup resource info.....*/
18415                                         p_error_code := 0;
18416                                         p_error_msg  := null;
18417 
18418                                         /* ST : Detailed Scheduling... should we remove the resources' setup resources that are being replaced.... */
18419 
18420                                         delete_setup_info( p_wip_entity_id             => p_wip_entity_id,
18421                                                            p_org_id                    => p_org_id,
18422                                                            p_par_res_seq_num           => l_v_disco_res_seq_num(l_idx2),
18423                                                            p_op_seq_num                => l_rtg_op_seq_num,
18424                                                            p_handle_curr_op            => 'N',
18425                                                            x_error_code                => p_error_code,
18426                                                            x_error_msg                 => p_error_msg
18427                                                          );
18428 
18429                                         if p_error_code <> 0 then
18430                                                 /* error out... */
18431                                                 raise e_wljdi_common_error;
18432                                         end if;
18433                                         l_out := l_out +1;
18434                                         l_idx2 := l_v_disco_res_ids.next(l_idx2);
18435 
18436                                 end loop;
18437                         end if;
18438                         close c_disco_res;
18439                     end if;  -- l_sub_type = WSM_SUB_REC
18440                 end if;     -- l_rtg_op_seq_num IS NOT NULL
18441                 -- }} Update WSM_COPY table
18442                 ---l_d := v_wljdi_record_id.next(l_d);  Updated for Alcoa Enhancement
18443             end if; -- l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC)
18444 	    l_d := v_wljdi_record_id.next(l_d);    ---Added for Alcoa Enhancement
18445         end loop;
18446 
18447         -- Update WCOR
18448         if(l_v_rtg_op_seq.first IS NOT NULL) then
18449             l_stmt_num := 270.1;
18450             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
18451                 UPDATE  WSM_COPY_OP_RESOURCES
18452                 set     reco_start_date         = l_v_start_date(i),
18453                         reco_completion_date    = l_v_compl_date(i),
18454                         recommended             = NVL(l_v_recoflag(i),  recommended),
18455 
18456                         /* ST : Detailed Scheduling start */
18457 						/* Modified Query by Adding NVL during update for firm_type. bug 5471266
18458 						   Already assig_units, max_assg_units were having nvl in it.*/
18459                         firm_type           = NVL( l_v_res_firm_type(i) , firm_type),
18460                         setup_id            = l_v_res_setup_id(i),
18461                         group_sequence_id   = l_v_res_grp_seq_id(i),
18462                         group_sequence_num  = l_v_res_grp_seq_id(i),
18463                         max_assigned_units  = nvl( l_v_res_max_assg_units(i) , max_assigned_units),
18464                         assigned_units      = nvl( l_v_res_assg_units(i)   , assigned_units),
18465                         batch_id            = l_v_res_batch_id(i),
18466                         /* ST : Detailed Scheduling end */
18467 
18468                         last_update_date        = p_last_updt_date,
18469                         last_updated_by         = p_last_updt_by,
18470                         last_update_login       = p_last_updt_login,
18471                         request_id              = p_request_id,
18472                         program_application_id  = p_program_appl_id,
18473                         program_id              = p_program_id,
18474                         program_update_date     = p_prog_updt_date
18475                 where   wip_entity_id         = p_wip_entity_id
18476                 and     operation_seq_num     = l_v_rtg_op_seq(i)
18477                 and     resource_id           = l_v_res_ids(i)
18478                 and     resource_seq_num      = nvl(l_v_res_seq_nums(i),resource_seq_num)
18479                 and     nvl(substitute_group_num, -1) = nvl(l_v_res_sub_grp(i), -1)
18480                 and     nvl(replacement_group_num, 0) = nvl(l_v_res_rpl_grp(i), 0);
18481 
18482                 -- will not delete this because resource dates on non-reco operation
18483                 -- will be kept. Gantt will not show and usage/instance on non-reco
18484                 -- path
18485 
18486                 l_stmt_num := 270.2;
18487                 -- delete resource usage/instance records
18488                 --delete  WSM_COPY_OP_RESOURCE_USAGE
18489                 --where   wip_entity_id         = p_wip_entity_id
18490                 --and     (operation_seq_num,
18491                 --         resource_seq_num) in (
18492                 --        select operation_seq_num,
18493                 --               resource_seq_num
18494                 --        from   WSM_COPY_OP_RESOURCES
18495                 --        where  wip_entity_id = p_wip_entity_id
18496                 --        and    recommended <> 'Y' );
18497                 l_stmt_num := 270.3;
18498                 --delete  WSM_COPY_OP_RESOURCE_INSTANCES
18499                 --where   wip_entity_id         = p_wip_entity_id
18500                 --and     (operation_seq_num,
18501                 --         resource_seq_num) in (
18502                 --        select operation_seq_num,
18503                 --               resource_seq_num
18504                 --        from   WSM_COPY_OP_RESOURCES
18505                 --        where  wip_entity_id = p_wip_entity_id
18506                 --        and    recommended <> 'Y' );
18507 
18508         end if;
18509         if lbji_debug = 'Y' then
18510             fnd_file.put_line(fnd_file.log, 'Mark recommended operation resources ... OK.');
18511         end if;
18512 
18513     end if;
18514     ------------------------------------------------
18515     -- }} validete operation resources
18516     ------------------------------------------------
18517 
18518 l_stmt_num := 300;
18519     ------------------------------------------------
18520     -- {{ validete operation resources instances
18521     ------------------------------------------------
18522     if l_res_ins_first IS NOT NULL then
18523         -- clear output PL/SQL table
18524         l_out := 0;
18525         l_v_rtg_op_seq.delete;
18526         l_v_res_seq_num.delete;
18527         l_v_start_date.delete;
18528         l_v_compl_date.delete;
18529         l_v_instance_id.delete;
18530         l_v_serial_num.delete;
18531     l_v_res_ins_batch_id.delete;
18532 
18533         l_d := l_res_ins_first;
18534         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) = WSM_LOAD_RES_INS)
18535         loop
18536             l_load_type      := v_wljdi_load_type(l_d);
18537             l_sub_type       := v_wljdi_substitution_type(l_d);
18538             l_job_op_seq_num := v_wljdi_job_op_seq_num(l_d);     -- current op only
18539             l_rtg_op_seq_num := v_wljdi_routing_op_seq_num(l_d); -- future op only
18540             l_res_seq_num    := v_wljdi_resource_seq_num(l_d);
18541 
18542             -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18543             l_resource_id               := v_wljdi_resource_id_new(l_d);
18544             l_parent_res_seq_num    := v_wljdi_parent_seq_num(l_d);
18545             -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18546 
18547             -- {{ Update WIP table
18548             if(l_job_op_seq_num IS NOT NULL) then
18549 
18550                 if(l_job_op_seq_num = l_cur_job_op_seq_num) then
18551                     l_stmt_num := 310.1;
18552                     -- pack data into PL/SQL table l_v_res_ins for validation
18553                     l_v_res_ins.delete;
18554                     l_in := 1;
18555                     l_v_res_ins(l_in).job_op_seq_num := l_job_op_seq_num;
18556                     l_v_res_ins(l_in).res_seq_num    := l_res_seq_num;
18557                     l_v_res_ins(l_in).instance_id    := v_wljdi_instance_id_new(l_d);
18558                     l_v_res_ins(l_in).serial_number  := v_wljdi_serial_number_new(l_d);
18559                     l_v_res_ins(l_in).start_date     := v_wljdi_start_date(l_d);
18560                     l_v_res_ins(l_in).completion_date:= v_wljdi_completion_date(l_d);
18561                     l_v_res_ins(l_in).batch_id       := v_wljdi_batch_id(l_d);
18562 
18563                     l_stmt_num := 310.2;
18564                     l_temp := v_wljdi_record_id.next(l_d);
18565 
18566                     -- get all records with the same res_seq_num or belonging to the same resource...
18567                     while(l_temp IS NOT NULL and
18568                           v_wljdi_load_type(l_temp)         = l_load_type and
18569                           v_wljdi_substitution_type(l_temp) = l_sub_type and
18570                           v_wljdi_job_op_seq_num(l_temp)    = l_job_op_seq_num and
18571                           ( (v_wljdi_resource_seq_num(l_temp)  = l_res_seq_num)
18572                              -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18573                              OR
18574                              ( nvl(v_wljdi_resource_seq_num(l_temp),-1) = nvl(l_res_seq_num,-1) and
18575                                v_wljdi_parent_seq_num(l_d)              = l_parent_res_seq_num   and
18576                                v_wljdi_resource_id_new(l_d)             = l_resource_id
18577                              )
18578                            -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18579                           )
18580                          )
18581                     loop
18582                         l_d  := l_temp;      -- move index l_d!!
18583                         l_in := l_in+1;
18584                         l_v_res_ins(l_in).job_op_seq_num := l_job_op_seq_num;
18585                         l_v_res_ins(l_in).res_seq_num    := l_res_seq_num;
18586                         l_v_res_ins(l_in).instance_id    := v_wljdi_instance_id_new(l_d);
18587                         l_v_res_ins(l_in).serial_number  := v_wljdi_serial_number_new(l_d);
18588                         l_v_res_ins(l_in).start_date     := v_wljdi_start_date(l_d);
18589                         l_v_res_ins(l_in).completion_date:= v_wljdi_completion_date(l_d);
18590                         l_v_res_ins(l_in).batch_id       := v_wljdi_batch_id(l_d);
18591 
18592                         l_temp := v_wljdi_record_id.next(l_d);
18593                     end loop;
18594 
18595                     l_stmt_num := 310.3;
18596                     -- valid resource usage
18597                     validate_wljdi_res_instance(
18598                         p_wip_entity_id     => p_wip_entity_id,
18599                         p_org_id            => p_org_id,
18600                         p_v_res_ins         => l_v_res_ins,
18601                         p_substitution_type => l_sub_type,
18602                         p_handle_wip_cur_op => 'Y',
18603                         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18604                         p_parent_res_seq_num => l_parent_res_seq_num,
18605                         p_resource_id        => l_resource_id,
18606                         x_setup_res_seq_num  => l_setup_res_seq_num,
18607                         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18608                         x_error_code        => p_error_code,
18609                         x_error_msg         => p_error_msg);
18610 
18611                     if p_error_code <> 0 then
18612                         raise e_wljdi_common_error;
18613                     end if;
18614 
18615                     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18616                     if l_res_seq_num IS NULL THEN
18617                         l_res_seq_num := l_setup_res_seq_num;
18618                     end if;
18619                     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18620 
18621                     l_stmt_num := 310.4;
18622                     -- delete all existing records for that resource_seq_num
18623                     -- CZHDBG: do we need to check if resource is charged ???
18624                     delete from wip_op_resource_instances
18625                     where  wip_entity_id     = p_wip_entity_id
18626                     and    organization_id   = p_org_id
18627                     and    operation_seq_num = l_job_op_seq_num
18628                     and    resource_seq_num  = l_res_seq_num;
18629 
18630                     l_stmt_num := 310.5;
18631                     delete from wip_operation_resource_usage
18632                     where  wip_entity_id     = p_wip_entity_id
18633                     and    organization_id   = p_org_id
18634                     and    operation_seq_num = l_job_op_seq_num
18635                     and    resource_seq_num  = l_res_seq_num;
18636 
18637                     if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC) then
18638                            l_in := l_v_res_ins.first;
18639 
18640                             while (l_in IS NOT NULL)
18641                             loop
18642                                     l_stmt_num := 310.6;
18643                                     insert into wip_op_resource_instances(
18644                                         wip_entity_id  ,
18645                                         organization_id,
18646                                         operation_seq_num,
18647                                         resource_seq_num,
18648                                         instance_id,
18649                                         serial_number,
18650                                         start_date,
18651                                         completion_date,
18652                                         batch_id ,
18653                                         last_update_date,
18654                                         last_updated_by,
18655                                         creation_date,
18656                                         created_by,
18657                                         last_update_login
18658                                     ) values (
18659                                         p_wip_entity_id,
18660                                         p_org_id,
18661                                         l_job_op_seq_num,
18662                                         l_res_seq_num,
18663                                         l_v_res_ins(l_in).instance_id,
18664                                         l_v_res_ins(l_in).serial_number,
18665                                         l_v_res_ins(l_in).start_date,
18666                                         l_v_res_ins(l_in).completion_date,
18667                                         l_v_res_ins(l_in).batch_id,
18668                                         p_last_updt_date,
18669                                         p_last_updt_by,
18670                                         p_creation_date,
18671                                         p_created_by,
18672                                         p_last_updt_login);
18673 
18674                                     l_in  := l_v_res_ins.next(l_in);
18675                             end loop;
18676                     end if;
18677 
18678                 else    -- l_job_op_seq_num <> l_cur_job_op_seq_num
18679                     -- this can happen when recommendations arrive after the job
18680                     -- is moved. should give a warning message
18681                     fnd_file.put_line(fnd_file.log,
18682                         'Warning: resource instance recommendations for current operation are ignored ' ||
18683                         '(record_id = ' || l_d || ')' );
18684                 end if;
18685                 -- }} Update WIP table
18686 
18687             -- {{ Update WSM_COPY table
18688             elsif(l_rtg_op_seq_num IS NOT NULL) then
18689 
18690                 l_stmt_num := 320.1;
18691                 -- pack data into PL/SQL table l_v_res for validation
18692                 l_v_res_ins.delete;
18693                 l_in := 1;
18694                 l_v_res_ins(l_in).rtg_op_seq_num := l_rtg_op_seq_num;
18695                 l_v_res_ins(l_in).res_seq_num    := l_res_seq_num;
18696                 l_v_res_ins(l_in).instance_id    := v_wljdi_instance_id_new(l_d);
18697                 l_v_res_ins(l_in).serial_number  := v_wljdi_serial_number_new(l_d);
18698                 l_v_res_ins(l_in).start_date     := v_wljdi_start_date(l_d);
18699                 l_v_res_ins(l_in).completion_date:= v_wljdi_completion_date(l_d);
18700                 l_v_res_ins(l_in).batch_id       := v_wljdi_batch_id(l_d);
18701 
18702 
18703                 l_stmt_num := 320.2;
18704                 l_temp := v_wljdi_record_id.next(l_d);
18705                 -- get all records with the same res_seq_num
18706                 while(l_temp IS NOT NULL and
18707                       v_wljdi_load_type(l_temp)          = l_load_type and
18708                       v_wljdi_substitution_type(l_temp)  = l_sub_type and
18709                       v_wljdi_routing_op_seq_num(l_temp) = l_rtg_op_seq_num and
18710                       (  (  v_wljdi_resource_seq_num(l_temp)   = l_res_seq_num)
18711                          -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18712                          OR
18713                          (  (nvl(v_wljdi_resource_seq_num(l_temp),-1) = nvl(l_res_seq_num,-1)   and
18714                             (v_wljdi_parent_seq_num(l_d)              = l_parent_res_seq_num)   and
18715                             (v_wljdi_resource_id_new(l_d)             = l_resource_id))
18716                          -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18717                          )
18718                       )
18719                      )
18720                 loop
18721                     l_d := l_temp;      -- move index l_d!!
18722                     l_in := l_in+1;
18723                     l_v_res_ins(l_in).rtg_op_seq_num := l_rtg_op_seq_num;
18724                     l_v_res_ins(l_in).res_seq_num    := l_res_seq_num;
18725                     l_v_res_ins(l_in).instance_id    := v_wljdi_instance_id_new(l_d);
18726                     l_v_res_ins(l_in).serial_number  := v_wljdi_serial_number_new(l_d);
18727                     l_v_res_ins(l_in).start_date     := v_wljdi_start_date(l_d);
18728                     l_v_res_ins(l_in).completion_date:= v_wljdi_completion_date(l_d);
18729                     l_v_res_ins(l_in).batch_id       := v_wljdi_batch_id(l_d);
18730                     l_temp := v_wljdi_record_id.next(l_d);
18731                 end loop;
18732 
18733 
18734 l_stmt_num := 320.3;
18735 
18736                 -- valid resource usage
18737                 validate_wljdi_res_instance(
18738                     p_wip_entity_id     => p_wip_entity_id,
18739                     p_org_id            => p_org_id,
18740                     p_v_res_ins         => l_v_res_ins,
18741                     p_substitution_type => l_sub_type,
18742                     p_handle_wip_cur_op => 'N',
18743                     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18744                     p_parent_res_seq_num => l_parent_res_seq_num,
18745                     p_resource_id        => l_resource_id,
18746                     x_setup_res_seq_num  => l_setup_res_seq_num,
18747                     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18748                     x_error_code        => p_error_code,
18749                     x_error_msg         => p_error_msg);
18750 
18751                 if p_error_code <> 0 then
18752                     raise e_wljdi_common_error;
18753                 end if;
18754 
18755                 -- l_res_seq_num should always be available now
18756                 l_res_seq_num := l_v_res_ins(1).res_seq_num;
18757 
18758                 -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18759                 if l_res_seq_num IS NULL THEN
18760                         l_res_seq_num := l_setup_res_seq_num;
18761                 end if;
18762                 -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18763 
18764                 l_stmt_num := 320.4;
18765                 -- delete all existing records for that resource_seq_num
18766                 delete from wsm_copy_op_resource_instances
18767                 where  wip_entity_id     = p_wip_entity_id
18768                 and    organization_id   = p_org_id
18769                 and    operation_seq_num = l_rtg_op_seq_num
18770                 and    resource_seq_num  = l_res_seq_num;
18771 l_stmt_num := 320.5;
18772                 delete from wsm_copy_op_resource_usage
18773                 where  wip_entity_id     = p_wip_entity_id
18774                 and    organization_id   = p_org_id
18775                 and    operation_seq_num = l_rtg_op_seq_num
18776                 and    resource_seq_num  = l_res_seq_num;
18777 l_stmt_num := 320.6;
18778                 -- save validated data into an array for balk update
18779                 if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC) then
18780                     l_in := l_v_res_ins.first;
18781                     while (l_in IS NOT NULL)
18782                     loop
18783                         l_v_rtg_op_seq(l_out)       := l_rtg_op_seq_num;
18784                         l_v_res_seq_num(l_out)      := l_res_seq_num;
18785                         l_v_instance_id(l_out)      := l_v_res_ins(l_in).instance_id;
18786                         l_v_serial_num(l_out)       := l_v_res_ins(l_in).serial_number;
18787                         l_v_start_date(l_out)       := l_v_res_ins(l_in).start_date;
18788                         l_v_compl_date(l_out)       := l_v_res_ins(l_in).completion_date;
18789                         l_v_res_ins_batch_id(l_out) := l_v_res_ins(l_in).batch_id;
18790                         l_out := l_out +1;
18791                         l_in  := l_v_res_ins.next(l_in);
18792                     end loop;
18793                 end if;
18794             end if;
18795             -- }} Update WSM_COPY table
18796 
18797             l_d := v_wljdi_record_id.next(l_d);
18798         end loop;
18799 
18800         -- creating WCORI records
18801         if(l_v_rtg_op_seq.first IS NOT NULL) then
18802             l_stmt_num := 330;
18803             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
18804                 insert into wsm_copy_op_resource_instances(
18805                     wip_entity_id  ,
18806                     organization_id,
18807                     operation_seq_num,
18808                     resource_seq_num,
18809                     instance_id,
18810                     serial_number,
18811                     start_date ,
18812                     completion_date,
18813                     batch_id,
18814                     last_update_date,
18815                     last_updated_by,
18816                     creation_date,
18817                     created_by,
18818                     last_update_login,
18819                     request_id,
18820                     program_application_id,
18821                     program_id,
18822                     program_update_date
18823                 ) values (
18824                     p_wip_entity_id,
18825                     p_org_id,
18826                     l_v_rtg_op_seq(i),
18827                     l_v_res_seq_num(i),
18828                     l_v_instance_id(i),
18829                     l_v_serial_num(i),
18830                     l_v_start_date(i),
18831                     l_v_compl_date(i),
18832                     l_v_res_ins_batch_id(i),
18833                     p_last_updt_date,
18834                     p_last_updt_by,
18835                     p_creation_date,
18836                     p_created_by,
18837                     p_last_updt_login,
18838                     p_request_id,
18839                     p_program_appl_id,
18840                     p_program_id,
18841                     p_prog_updt_date);
18842 
18843          if lbji_debug = 'Y' then
18844             fnd_file.put_line(fnd_file.log, 'Inserted resource instance ... OK.');
18845          end if;
18846 
18847         end if;
18848         if lbji_debug = 'Y' then
18849             fnd_file.put_line(fnd_file.log, 'Mark recommended resource instance ... OK.');
18850         end if;
18851 
18852     end if; -- resource instance recommendations exist
18853     ------------------------------------------------
18854     -- }} validete operation resources instances
18855     ------------------------------------------------
18856 
18857 
18858 l_stmt_num := 400;
18859     ------------------------------------------------
18860     -- {{ validete operation resources usage
18861     ------------------------------------------------
18862 
18863     if l_res_use_first IS NOT NULL then
18864         -- clear output PL/SQL table
18865         l_out := 0;
18866         l_v_rtg_op_seq.delete;
18867         l_v_res_seq_num.delete;
18868         l_v_instance_id.delete;
18869         l_v_serial_num.delete;
18870         l_v_start_date.delete;
18871         l_v_compl_date.delete;
18872         l_v_res_use_asgn_units.delete;
18873 
18874         /* ST : Detailed scheduling : start */
18875         l_v_resource_hours.delete;
18876         /* ST : Detailed scheduling : end */
18877 
18878         l_d := l_res_use_first;
18879         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) = WSM_LOAD_RES_USE)
18880         loop
18881             l_load_type      := v_wljdi_load_type(l_d);
18882             l_sub_type       := v_wljdi_substitution_type(l_d);
18883             l_job_op_seq_num := v_wljdi_job_op_seq_num(l_d);     -- current op only
18884             l_rtg_op_seq_num := v_wljdi_routing_op_seq_num(l_d); -- future op only
18885             l_res_seq_num    := v_wljdi_resource_seq_num(l_d);
18886             l_instance_id    := v_wljdi_instance_id_new(l_d);
18887             l_serial_num     := v_wljdi_serial_number_new(l_d);
18888 
18889             /* ST : Detailed scheduling : start */
18890             l_resource_hours := v_wljdi_resource_hours(l_d);
18891             /* ST : Detailed scheduling : end */
18892 
18893             -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18894             l_resource_id               := v_wljdi_resource_id_new(l_d);
18895             l_parent_res_seq_num    := v_wljdi_parent_seq_num(l_d);
18896             -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18897 
18898             if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC, WSM_SUB_DEL) then
18899 
18900                 -- {{ Update WIP table
18901                 if(l_job_op_seq_num IS NOT NULL) then
18902 
18903                     if(l_job_op_seq_num = l_cur_job_op_seq_num) then
18904                         l_stmt_num := 410.1;
18905                         -- pack data into PL/SQL table l_v_res_use for validation
18906                         l_v_res_use.delete;
18907                         l_in := 1;
18908                         l_v_res_use(l_in).job_op_seq_num := l_job_op_seq_num;
18909                         l_v_res_use(l_in).res_seq_num    := l_res_seq_num;
18910                         l_v_res_use(l_in).instance_id    := l_instance_id;
18911                         l_v_res_use(l_in).serial_number  := l_serial_num;
18912                         l_v_res_use(l_in).assigned_units := v_wljdi_assigned_units(l_d);
18913                         l_v_res_use(l_in).start_date     := v_wljdi_start_date(l_d);
18914                         l_v_res_use(l_in).completion_date:= v_wljdi_completion_date(l_d);
18915 
18916                         /* ST : Detailed scheduling : start */
18917                         l_v_res_use(l_in).resource_hours := v_wljdi_resource_hours(l_d);
18918                         /* ST : Detailed scheduling : end */
18919 
18920                         -- BA: bug 3373496 to filter duplicated resource usage records
18921                         l_v_dup_usage.delete;
18922                         l_hash_str   := to_char(l_v_res_use(l_in).start_date, 'DD-MON-YYYY HH24:MI:SS') ||
18923                                         to_char(l_v_res_use(l_in).completion_date, 'DD-MON-YYYY HH24:MI:SS');
18924                         l_hash_value := dbms_utility.get_hash_value(l_hash_str, 37, 1073741824);
18925                         l_v_dup_usage(l_hash_value) := l_hash_value;
18926                         -- EA: bug 3373496
18927 
18928 l_stmt_num := 410.2;
18929                         l_temp := v_wljdi_record_id.next(l_d);
18930                         -- get all records with the same res_seq_num, instance_id and serial_num
18931                         while(l_temp IS NOT NULL and
18932                               v_wljdi_load_type(l_temp)         = l_load_type and
18933                               v_wljdi_substitution_type(l_temp) = l_sub_type and
18934                               v_wljdi_job_op_seq_num(l_temp)    = l_job_op_seq_num and
18935                               ( (v_wljdi_resource_seq_num(l_temp)  = l_res_seq_num)
18936                                 -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18937                                 OR
18938                                 ( nvl(v_wljdi_resource_seq_num(l_temp),-1) = nvl(l_res_seq_num,-1) and
18939                                  (v_wljdi_parent_seq_num(l_d) = l_parent_res_seq_num)              and
18940                                  (v_wljdi_resource_id_new(l_d)     = l_resource_id)
18941                                 )
18942                                 -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18943                               ) and
18944                               NVL(v_wljdi_instance_id_new(l_temp), -1)= NVL(l_instance_id, -1) and
18945                               NVL(v_wljdi_serial_number_new(l_temp), g_nvl_str) = NVL(l_serial_num, g_nvl_str)
18946                              )
18947                         loop
18948                             l_d := l_temp;      -- move index l_d!!
18949 
18950                             -- BA: bug 3373496 to filter duplicated resource usage records
18951                             l_hash_str   := to_char(v_wljdi_start_date(l_d), 'DD-MON-YYYY HH24:MI:SS') ||
18952                                             to_char(v_wljdi_completion_date(l_d), 'DD-MON-YYYY HH24:MI:SS');
18953                             l_hash_value := dbms_utility.get_hash_value(l_hash_str, 37, 1073741824);
18954                             -- EA: bug 3373496
18955 
18956                             -- BC: bug 3373496 to filter duplicated resource usage records
18957                             -- only new value will be added
18958 
18959                             --if(l_v_dup_usage(l_hash_value) IS NULL) then      -- bug 3401727
18960                             if(NOT l_v_dup_usage.exists(l_hash_value)) then     -- bug 3401727
18961                                 l_in := l_in+1;
18962                                 l_v_res_use(l_in).job_op_seq_num := l_job_op_seq_num;
18963                                 l_v_res_use(l_in).res_seq_num    := l_res_seq_num;
18964                                 l_v_res_use(l_in).instance_id    := l_instance_id;
18965                                 l_v_res_use(l_in).serial_number  := l_serial_num;
18966                                 l_v_res_use(l_in).assigned_units := v_wljdi_assigned_units(l_d);
18967                                 l_v_res_use(l_in).start_date     := v_wljdi_start_date(l_d);
18968                                 l_v_res_use(l_in).completion_date:= v_wljdi_completion_date(l_d);
18969 
18970                                 /* ST : Detailed scheduling : start */
18971                                 l_v_res_use(l_in).resource_hours := v_wljdi_resource_hours(l_d);
18972                                 /* ST : Detailed scheduling : end */
18973 
18974                                 l_v_dup_usage(l_hash_value) := l_hash_value;
18975                             else
18976                                 -- dup usage record
18977                                 fnd_file.put_line(fnd_file.log,
18978                                     'Warning: duplicated resource usage recommendations are ignored ' ||
18979                                     '(record_id = ' || l_d || ').');
18980                             end if;
18981                             -- EC: bug 3373496
18982 
18983                             l_temp := v_wljdi_record_id.next(l_d);
18984                         end loop;
18985 
18986 l_stmt_num := 410.3;
18987                         -- valid resource usage
18988                         validate_wljdi_res_usage(
18989                             p_wip_entity_id     => p_wip_entity_id,
18990                             p_org_id            => p_org_id,
18991                             p_v_res_use         => l_v_res_use,
18992                             p_substitution_type => l_sub_type,
18993                             p_handle_wip_cur_op => 'Y',
18994                             -- ST : Bug fix 4240165 : Detailed Scheduling changes start
18995                             p_parent_res_seq_num => l_parent_res_seq_num,
18996                             p_resource_id    => l_resource_id,
18997                             x_setup_res_seq_num  => l_setup_res_seq_num,
18998                             -- ST : Bug fix 4240165 : Detailed Scheduling changes end
18999                             x_error_code        => p_error_code,
19000                             x_error_msg         => p_error_msg);
19001                         if p_error_code <> 0 then
19002                             raise e_wljdi_common_error;
19003                         end if;
19004 
19005                         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19006                         if l_res_seq_num IS NULL THEN
19007                                 l_res_seq_num := l_setup_res_seq_num;
19008                         end if;
19009                         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19010 
19011 l_stmt_num := 410.4;
19012                         -- delete all existing records for that resource_seq_num
19013                         delete from wip_operation_resource_usage
19014                         where  wip_entity_id        = p_wip_entity_id
19015                         and    organization_id      = p_org_id
19016                         and    operation_seq_num    = l_job_op_seq_num
19017                         and    resource_seq_num     = l_res_seq_num
19018                         and    NVL(instance_id, -1) = NVL(l_instance_id, -1)
19019                         and    NVL(serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
19020 
19021                         if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC) then
19022                             l_in := l_v_res_use.first;
19023                             while (l_in IS NOT NULL)
19024                             loop
19025 l_stmt_num := 410.5;
19026                                 insert into wip_operation_resource_usage(
19027                                     wip_entity_id  ,
19028                                     organization_id,
19029                                     operation_seq_num,
19030                                     resource_seq_num,
19031                                     instance_id,
19032                                     serial_number,
19033                                     start_date ,
19034                                     completion_date,
19035                                     assigned_units ,
19036                                     -- resource_hours,  /* ST : Added for Detailed Scheduling */
19037                                     last_update_date,
19038                                     last_updated_by,
19039                                     creation_date,
19040                                     created_by,
19041                                     last_update_login,
19042                                     request_id,
19043                                     program_application_id,
19044                                     program_id,
19045                                     program_update_date
19046                                 ) values (
19047                                     p_wip_entity_id,
19048                                     p_org_id,
19049                                     l_job_op_seq_num,
19050                                     l_res_seq_num,
19051                                     l_instance_id,
19052                                     l_serial_num,
19053                                     l_v_res_use(l_in).start_date,
19054                                     l_v_res_use(l_in).completion_date,
19055                                     l_v_res_use(l_in).assigned_units,
19056                                     -- l_v_res_use(l_in).resource_hours, /* ST : Added for Detailed Scheduling */
19057                                     p_last_updt_date,
19058                                     p_last_updt_by,
19059                                     p_creation_date,
19060                                     p_created_by,
19061                                     p_last_updt_login,
19062                                     p_request_id,
19063                                     p_program_appl_id,
19064                                     p_program_id,
19065                                     p_prog_updt_date);
19066 
19067                                 update_cumulative_time(
19068                                     p_wip_entity_id      => p_wip_entity_id,
19069                                     p_operation_seq_num  => l_job_op_seq_num,
19070                                     p_resource_seq_num   => l_res_seq_num,
19071                                     p_instance_id        => l_instance_id,
19072                                     p_serial_num         => l_serial_num,
19073                                     p_handle_wip_cur_op  => 'Y' );
19074 
19075                                 l_in  := l_v_res_use.next(l_in);
19076                             end loop;
19077                         end if;
19078 
19079                     else -- l_job_op_seq_num <> l_cur_job_op_seq_num
19080                         -- this can happen when recommendations arrive after the job
19081                         -- is moved. should give a warning message
19082                         fnd_file.put_line(fnd_file.log,
19083                             'Warning: resource usage recommendations for current operation is ignored ' ||
19084                             '(record_id = ' || l_d || ').');
19085                     end if;
19086                 -- }} Update WIP table
19087 
19088                 -- {{ Update WSM_COPY table
19089                 elsif(l_rtg_op_seq_num IS NOT NULL) then
19090                     l_stmt_num := 420.1;
19091                     -- pack data into PL/SQL table l_v_res for validation
19092                     l_v_res_use.delete;
19093                     l_in := 1;
19094                     l_v_res_use(l_in).rtg_op_seq_num := l_rtg_op_seq_num;
19095                     l_v_res_use(l_in).res_seq_num    := l_res_seq_num;
19096                     l_v_res_use(l_in).instance_id    := l_instance_id;
19097                     l_v_res_use(l_in).serial_number  := l_serial_num;
19098                     l_v_res_use(l_in).assigned_units := v_wljdi_assigned_units(l_d);
19099                     l_v_res_use(l_in).start_date     := v_wljdi_start_date(l_d);
19100                     l_v_res_use(l_in).completion_date:= v_wljdi_completion_date(l_d);
19101 
19102                     /* ST : Detailed scheduling : start */
19103                     l_v_res_use(l_in).resource_hours := v_wljdi_resource_hours(l_d);
19104                     /* ST : Detailed scheduling : end */
19105 
19106                     -- BA: bug 3373496 to filter duplicated resource usage records
19107                     l_v_dup_usage.delete;
19108                     l_hash_str   := to_char(l_v_res_use(l_in).start_date, 'DD-MON-YYYY HH24:MI:SS') ||
19109                                     to_char(l_v_res_use(l_in).completion_date, 'DD-MON-YYYY HH24:MI:SS');
19110                     l_hash_value := dbms_utility.get_hash_value(l_hash_str, 37, 1073741824);
19111                     l_v_dup_usage(l_hash_value) := l_hash_value;
19112                     -- EA: bug 3373496
19113 
19114 l_stmt_num := 420.2;
19115                     l_temp := v_wljdi_record_id.next(l_d);
19116                     -- get all records with the same res_seq_num
19117                     while(l_temp IS NOT NULL and
19118                           v_wljdi_load_type(l_temp)          = l_load_type and
19119                           v_wljdi_substitution_type(l_temp)  = l_sub_type and
19120                           v_wljdi_routing_op_seq_num(l_temp) = l_rtg_op_seq_num and
19121                           (  (v_wljdi_resource_seq_num(l_temp)   = l_res_seq_num)
19122                               -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19123                               OR
19124                               ( nvl(v_wljdi_resource_seq_num(l_temp),-1) = nvl(l_res_seq_num,-1)        and
19125                                 (v_wljdi_parent_seq_num(l_d) = l_parent_res_seq_num)                      and
19126                                  (v_wljdi_resource_id_new(l_d)     = l_resource_id)
19127                               )
19128                               -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19129                           )
19130                           and
19131                           NVL(v_wljdi_instance_id_new(l_temp), -1) = NVL(l_instance_id, -1) and
19132                           NVL(v_wljdi_serial_number_new(l_temp), g_nvl_str) = NVL(l_serial_num, g_nvl_str))
19133                     loop
19134                         l_d := l_temp;      -- move index l_d!!
19135 
19136                         -- BA: bug 3373496 to filter duplicated resource usage records
19137                         l_hash_str   := to_char(v_wljdi_start_date(l_d), 'DD-MON-YYYY HH24:MI:SS') ||
19138                                         to_char(v_wljdi_completion_date(l_d), 'DD-MON-YYYY HH24:MI:SS');
19139                         l_hash_value := dbms_utility.get_hash_value(l_hash_str, 37, 1073741824);
19140                         -- EA: bug 3373496
19141 
19142                         -- BC: bug 3373496 to filter duplicated resource usage records
19143                         -- only new value will be added
19144 
19145                         --if(l_v_dup_usage(l_hash_value) IS NULL) then          -- bug 3401727
19146                         if(NOT l_v_dup_usage.exists(l_hash_value)) then         -- bug 3401727
19147                             l_in := l_in+1;
19148                             l_v_res_use(l_in).rtg_op_seq_num := l_rtg_op_seq_num;
19149                             l_v_res_use(l_in).res_seq_num    := l_res_seq_num;
19150                             l_v_res_use(l_in).instance_id    := l_instance_id;
19151                             l_v_res_use(l_in).serial_number  := l_serial_num;
19152                             l_v_res_use(l_in).assigned_units := v_wljdi_assigned_units(l_d);
19153                             l_v_res_use(l_in).start_date     := v_wljdi_start_date(l_d);
19154                             l_v_res_use(l_in).completion_date:= v_wljdi_completion_date(l_d);
19155 
19156                             /* ST : Detailed scheduling : start */
19157                             l_v_res_use(l_in).resource_hours := v_wljdi_resource_hours(l_d);
19158                             /* ST : Detailed scheduling : end */
19159 
19160                             l_v_dup_usage(l_hash_value) := l_hash_value;
19161                         else
19162                             -- dup usage record
19163                             fnd_file.put_line(fnd_file.log,
19164                                 'Warning: duplicated resource usage recommendations are ignored ' ||
19165                                 '(record_id = ' || l_d || ').');
19166                         end if;
19167                         -- EC: bug 3373496
19168 
19169                         l_temp := v_wljdi_record_id.next(l_d);
19170                     end loop;
19171 
19172 l_stmt_num := 420.3;
19173                     -- valid resource usage
19174                     validate_wljdi_res_usage(
19175                         p_wip_entity_id     => p_wip_entity_id,
19176                         p_org_id            => p_org_id,
19177                         p_v_res_use         => l_v_res_use,
19178                         p_substitution_type => l_sub_type,
19179                         p_handle_wip_cur_op => 'N',
19180                         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19181                         p_parent_res_seq_num => l_parent_res_seq_num,
19182                         p_resource_id        => l_resource_id,
19183                         x_setup_res_seq_num  => l_setup_res_seq_num,
19184                         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19185                         x_error_code        => p_error_code,
19186                         x_error_msg         => p_error_msg);
19187                     if p_error_code <> 0 then
19188                         raise e_wljdi_common_error;
19189                     end if;
19190 
19191                     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19192                     if l_res_seq_num IS NULL THEN
19193                         l_res_seq_num := l_setup_res_seq_num;
19194                     end if;
19195                     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19196 
19197 l_stmt_num := 420.4;
19198                     -- delete all existing records for that resource_seq_num
19199                     delete from wsm_copy_op_resource_usage
19200                     where  wip_entity_id     = p_wip_entity_id
19201                     and    organization_id   = p_org_id
19202                     and    operation_seq_num = l_rtg_op_seq_num
19203                     and    resource_seq_num  = l_res_seq_num;
19204 
19205                     -- save validated data into an array for balk update
19206                     if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC) then
19207                         l_in := l_v_res_use.first;
19208                         while (l_in IS NOT NULL)
19209                         loop
19210                             l_v_rtg_op_seq(l_out)         := l_rtg_op_seq_num;
19211                             l_v_res_seq_num(l_out)        := l_res_seq_num;
19212                             l_v_instance_id(l_out)        := l_instance_id;
19213                             l_v_serial_num(l_out)         := l_serial_num;
19214                             l_v_start_date(l_out)         := l_v_res_use(l_in).start_date;
19215                             l_v_compl_date(l_out)         := l_v_res_use(l_in).completion_date;
19216                             l_v_res_use_asgn_units(l_out) := l_v_res_use(l_in).assigned_units;
19217 
19218                             /* ST : Detailed scheduling : start */
19219                             l_v_resource_hours(l_out)     := l_v_res_use(l_in).resource_hours;
19220                             /* ST : Detailed scheduling : end */
19221 
19222                             l_out := l_out +1;
19223                             l_in  := l_v_res_use.next(l_in);
19224                         end loop;
19225                     end if;
19226                 end if;
19227                 -- }} Update WSM_COPY table
19228 
19229             end if; -- p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC, WSM_SUB_DEL)
19230             l_d := v_wljdi_record_id.next(l_d);
19231         end loop;
19232 
19233         -- creating WCORU records
19234         if(l_v_rtg_op_seq.first IS NOT NULL) then
19235 l_stmt_num := 430;
19236             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
19237                 insert into wsm_copy_op_resource_usage(
19238                     wip_entity_id  ,
19239                     organization_id,
19240                     operation_seq_num,
19241                     resource_seq_num,
19242                     instance_id,
19243                     serial_number,
19244                     start_date ,
19245                     completion_date,
19246                     assigned_units ,
19247                     resource_hours,  /* ST : Added for Detailed Scheduling */
19248                     last_update_date,
19249                     last_updated_by,
19250                     creation_date,
19251                     created_by,
19252                     last_update_login,
19253                     request_id,
19254                     program_application_id,
19255                     program_id,
19256                     program_update_date
19257                 ) values (
19258                     p_wip_entity_id,
19259                     p_org_id,
19260                     l_v_rtg_op_seq(i),
19261                     l_v_res_seq_num(i),
19262                     l_v_instance_id(i),
19263                     l_v_serial_num(i),
19264                     l_v_start_date(i),
19265                     l_v_compl_date(i),
19266                     l_v_res_use_asgn_units(i),
19267                     l_v_resource_hours(i),  /* ST : Added for Detailed Scheduling */
19268                     p_last_updt_date,
19269                     p_last_updt_by,
19270                     p_creation_date,
19271                     p_created_by,
19272                     p_last_updt_login,
19273                     p_request_id,
19274                     p_program_appl_id,
19275                     p_program_id,
19276                     p_prog_updt_date);
19277 
19278             for i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
19279             loop
19280                 update_cumulative_time(
19281                     p_wip_entity_id      => p_wip_entity_id,
19282                     p_operation_seq_num  => l_v_rtg_op_seq(i),
19283                     p_resource_seq_num   => l_v_res_seq_num(i),
19284                     p_instance_id        => l_v_instance_id(i),
19285                     p_serial_num         => l_v_serial_num(i),
19286                     p_handle_wip_cur_op  => 'N' );
19287             end loop;
19288 
19289         end if;
19290         if lbji_debug = 'Y' then
19291             fnd_file.put_line(fnd_file.log, 'Mark recommended resource usage ... OK.');
19292         end if;
19293 
19294     end if; -- resource usage recommendations exist
19295     ------------------------------------------------
19296     -- }} validete operation resources usage
19297     ------------------------------------------------
19298 
19299 
19300 l_stmt_num := 500;
19301     ------------------------------------------------
19302     -- {{ validete component
19303     ------------------------------------------------
19304     if l_comp_first IS NOT NULL then
19305         -- clear output PL/SQL table
19306         l_out := 0;
19307         l_v_rtg_op_seq.delete;
19308         l_v_comp_item_id.delete;
19309         l_v_comp_prm_item_id.delete;
19310         l_v_comp_phm_item_id.delete;
19311         l_v_comp_reqd_date.delete;
19312         l_v_recoflag.delete;
19313         l_v_comp_yield.delete;
19314         l_v_comp_qpa.delete;
19315 
19316         l_d := l_comp_first;
19317         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) = WSM_LOAD_COMP)
19318         loop
19319             l_load_type       := v_wljdi_load_type(l_d);
19320             l_sub_type        := v_wljdi_substitution_type(l_d);
19321             l_job_op_seq_num  := v_wljdi_job_op_seq_num(l_d);         -- current op only
19322             l_rtg_op_seq_num  := v_wljdi_routing_op_seq_num(l_d);     -- future op only
19323             l_item_id         := v_wljdi_inventory_item_id_new(l_d);  -- should not be null
19324             l_primary_item_id := v_wljdi_primary_item_id(l_d);        -- should not be null
19325             l_src_phm_item_id := v_wljdi_src_phantom_item_id(l_d);    -- can be null
19326             l_component_seq_id:= v_wljdi_component_sequence_id(l_d);  -- should not be null
19327             -- Added For LBJ Interface Alcoa Enhancement
19328             l_item_old_id         := v_wljdi_inventory_item_id_old(l_d);
19329 
19330             if l_sub_type in (WSM_SUB_DEL, WSM_SUB_ADD) then
19331 --               Modified for Alcoa Enhancement
19332 --                if(l_job_op_seq_num IS NULL) then
19333 --l_stmt_num := 510;
19334 --                    fnd_message.set_name('WSM','WSM_NULL_FIELD');
19335 --                    fnd_message.set_token('FLD_NAME', 'job_op_seq_num');
19336 --                    p_error_msg := fnd_message.get;
19337 --                    p_error_code := -1;
19338 --                    raise e_wljdi_common_error;
19339 --                else
19340 --                    fnd_file.put_line(fnd_file.log,
19341 --                        'Warning: deleting or adding components for current operation '||
19342 --                        'is currently not supported and ignored.' -- CZHDBG_MSG
19343 --                    );
19344 --                end if;
19345 			l_stmt_num := 510;
19346 			-- Added For LBJ Interface Alcoa Enhancement
19347 			IF(l_job_op_seq_num IS NOT NULL) THEN
19348 			    IF (l_job_op_seq_num=l_cur_job_op_seq_num) THEN
19349 				IF (l_sub_type=WSM_SUB_DEL) THEN
19350 				    l_in := 1;
19351 					l_v_comp.delete;
19352 					l_v_comp(l_in).job_op_seq_num   := l_job_op_seq_num;
19353 					l_v_comp(l_in).item_id          := l_item_id;
19354 					l_v_comp(l_in).item_old_id      := l_item_old_id;
19355 					l_v_comp(l_in).primary_item_id  := l_primary_item_id;
19356 					l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
19357 					l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
19358 	l_stmt_num := 750.2;
19359 					validate_wljdi_op_component(
19360 					    p_wip_entity_id     => p_wip_entity_id,
19361 					    p_org_id            => p_org_id,
19362 					    p_v_comp            => l_v_comp,
19363 					    p_substitution_type => l_sub_type,
19364 					    p_handle_wip_cur_op => 'Y',
19365 					    x_error_code        => p_error_code,
19366 					    x_error_msg         => p_error_msg);
19367 					if p_error_code <> 0 then
19368 					    raise e_wljdi_common_error;
19369 					end if;
19370 
19371 					delete wip_requirement_operations
19372 					where wip_entity_id = p_wip_entity_id
19373 					and organization_id   = p_org_id
19374 					and operation_seq_num = l_job_op_seq_num
19375 					and inventory_item_id = l_item_old_id;
19376 
19377 					wip_picking_pvt.cancel_comp_allocations(
19378 					    p_wip_entity_id     => p_wip_entity_id,
19379 					    p_operation_seq_num => l_job_op_seq_num,
19380 					    p_inventory_item_id => l_item_old_id,
19381 					    p_wip_entity_type   => WIP_CONSTANTS.LOTBASED,
19382 					    x_return_status     => x_return_status,
19383 					    x_msg_data          => p_error_msg);
19384 
19385 					if (x_return_status <> fnd_api.g_ret_sts_success) then
19386 					    raise e_wljdi_common_error;
19387 					end if;
19388 
19389 				ELSIF (l_sub_type=WSM_SUB_ADD) then
19390 	l_stmt_num:=59.9;
19391 				    l_in := 1;
19392 				    l_v_comp.delete;
19393 				    l_v_comp(l_in).job_op_seq_num   := l_job_op_seq_num;
19394 				    l_v_comp(l_in).item_id          := l_item_id;
19395 				    l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
19396 				    l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
19397 				    l_v_comp(l_in).wip_supply_type  := v_wljdi_wip_supply_type(l_d);
19398 				    l_v_comp(l_in).qpa                  := v_wljdi_quantity_per_assembly(l_d);
19399 				    l_v_comp(l_in).supply_subinventory  := v_wljdi_supply_subinventory(l_d);
19400 				    l_v_comp(l_in).supply_locator       := v_wljdi_supply_locator_id(l_d);
19401 				    l_v_comp(l_in).mrp_net_flag         := v_wljdi_mrp_net_flag(l_d);
19402 
19403 				    if v_wljdi_recommended(l_d) = 'N' then
19404 					fnd_file.put_line(fnd_file.log,'Warning: Addition of non-recommended component for current operation are ignored ' || '(record_id = ' || l_d || ').' );
19405 					l_valid_comp := false;
19406 				    else
19407 					l_valid_comp := true;
19408 				    end if;
19409 	l_stmt_num := 750.2;
19410 				    if l_valid_comp then
19411 
19412 					validate_wljdi_op_component(
19413 					p_wip_entity_id     => p_wip_entity_id,
19414 					p_org_id            => p_org_id,
19415 					p_v_comp            => l_v_comp,
19416 					p_substitution_type => l_sub_type,
19417 					p_handle_wip_cur_op => 'Y',
19418 					x_error_code        => p_error_code,
19419 					x_error_msg         => p_error_msg);
19420 
19421 					if p_error_code <> 0 then
19422 					    raise e_wljdi_common_error;
19423 					end if;
19424 
19425 					INSERT INTO WIP_REQUIREMENT_OPERATIONS
19426 					(inventory_item_id,
19427 						organization_id,
19428 						wip_entity_id,
19429 						operation_seq_num,
19430 						repetitive_schedule_id,
19431 						last_update_date,
19432 						last_updated_by,
19433 						creation_date,
19434 						created_by,
19435 						last_update_login,
19436 						component_sequence_id,
19437 						wip_supply_type,
19438 						date_required,
19439 						required_quantity,
19440 						quantity_issued,
19441 						quantity_per_assembly,
19442 						supply_subinventory,
19443 						supply_locator_id,
19444 						mrp_net_flag,
19445 						auto_request_material,
19446 						comments,
19447 						attribute_category,
19448 						attribute1,
19449 						attribute2,
19450 						attribute3,
19451 						attribute4,
19452 						attribute5,
19453 						attribute6,
19454 						attribute7,
19455 						attribute8,
19456 						attribute9,
19457 						attribute10,
19458 						attribute11,
19459 						attribute12,
19460 						attribute13,
19461 						attribute14,
19462 						attribute15,
19463 						request_id,
19464 						program_application_id,
19465 						program_id,
19466 						program_update_date,
19467 						segment1,
19468 						segment2,
19469 						segment3,
19470 						segment4,
19471 						segment5,
19472 						segment6,
19473 						segment7,
19474 						segment8,
19475 						segment9,
19476 						segment10,
19477 						segment11,
19478 						segment12,
19479 						segment13,
19480 						segment14,
19481 						segment15,
19482 						segment16,
19483 						segment17,
19484 						segment18,
19485 						segment19,
19486 						segment20,
19487 						department_id,
19488 						costed_quantity_issued,
19489 						costed_quantity_relieved,
19490 						released_quantity)
19491 					       select
19492 						l_v_comp(l_in).item_id,
19493 						p_org_id,
19494 						p_wip_entity_id,
19495 						l_v_comp(l_in).job_op_seq_num,
19496 						null,
19497 						p_last_updt_date,
19498 						p_last_updt_by,
19499 						p_creation_date,
19500 						p_created_by,
19501 						p_last_updt_login,
19502 						null,
19503 						l_v_comp(l_in).wip_supply_type,
19504 						l_v_comp(l_in).date_required,
19505 						l_v_comp(l_in).required_qty,
19506 						0,
19507 						l_v_comp(l_in).qpa,
19508 						l_v_comp(l_in).supply_subinventory,
19509 						l_v_comp(l_in).supply_locator,
19510 						v_wljdi_mrp_net_flag(l_d),
19511 						v_wljdi_auto_request_material(l_d),
19512 						v_wljdi_comments(l_d),
19513 						v_wljdi_attribute_category(l_d),
19514 						v_wljdi_attribute1(l_d),
19515 						v_wljdi_attribute2(l_d),
19516 						v_wljdi_attribute3(l_d),
19517 						v_wljdi_attribute4(l_d),
19518 						v_wljdi_attribute5(l_d),
19519 						v_wljdi_attribute6(l_d),
19520 						v_wljdi_attribute7(l_d),
19521 						v_wljdi_attribute8(l_d),
19522 						v_wljdi_attribute9(l_d),
19523 						v_wljdi_attribute10(l_d),
19524 						v_wljdi_attribute11(l_d),
19525 						v_wljdi_attribute12(l_d),
19526 						v_wljdi_attribute13(l_d),
19527 						v_wljdi_attribute14(l_d),
19528 						v_wljdi_attribute15(l_d),
19529 						p_request_id,
19530 						p_program_appl_id,
19531 						p_program_id,
19532 						p_prog_updt_date,
19533 						msi.segment1,
19534 						msi.segment2,
19535 						msi.segment3,
19536 						msi.segment4,
19537 						msi.segment5,
19538 						msi.segment6,
19539 						msi.segment7,
19540 						msi.segment8,
19541 						msi.segment9,
19542 						msi.segment10,
19543 						msi.segment11,
19544 						msi.segment12,
19545 						msi.segment13,
19546 						msi.segment14,
19547 						msi.segment15,
19548 						msi.segment16,
19549 						msi.segment17,
19550 						msi.segment18,
19551 						msi.segment19,
19552 						msi.segment20,
19553 						wo.department_id,
19554 						null,
19555 						null,
19556 						round(l_cur_job_qty*l_v_comp(l_in).qpa, 6)
19557 					from    wip_operations wo,
19558 						mtl_system_items msi
19559 					where   wo.operation_seq_num = l_v_comp(l_in).job_op_seq_num
19560 					and     wo.wip_entity_id= p_wip_entity_id
19561 					and     msi.inventory_item_id = l_v_comp(l_in).item_id
19562 					and     msi.organization_id = wo.organization_id
19563 					and     msi.organization_id = p_org_id;
19564 				    end if;
19565 				end if; -- WSM_SUB_DEL AND ADD
19566 			    else
19567 				fnd_file.put_line(fnd_file.log,
19568 				'Warning: Deleting or Adding components for executed operations '||
19569 				'is currently not supported and ignored.');
19570 			    end if;  --current operation
19571 			ELSIF (l_rtg_op_seq_num IS NOT NULL) THEN
19572 			    IF (l_sub_type=WSM_SUB_DEL) THEN
19573 				l_in := 1;
19574 				l_v_comp.delete;
19575 				l_v_comp(l_in).rtg_op_seq_num   := l_rtg_op_seq_num;
19576 				l_v_comp(l_in).item_id          := l_item_id;
19577 				l_v_comp(l_in).item_old_id      := l_item_old_id;
19578 				l_v_comp(l_in).src_phm_item_id :=v_wljdi_src_phantom_item_id(l_d);
19579 				l_v_comp(l_in).wip_supply_type  := v_wljdi_wip_supply_type(l_d);
19580 			      --l_v_comp(l_in).primary_item_id  := l_primary_item_id;
19581 			      --l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
19582 			      --l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
19583 	l_stmt_num := 750.21;
19584 
19585 				validate_wljdi_op_component(
19586 				    p_wip_entity_id     => p_wip_entity_id,
19587 				    p_org_id            => p_org_id,
19588 				    p_v_comp            => l_v_comp,
19589 				    p_substitution_type => l_sub_type,
19590 				    p_handle_wip_cur_op => 'N',
19591 				    x_error_code        => p_error_code,
19592 				    x_error_msg         => p_error_msg);
19593 
19594 				if p_error_code <> 0 then
19595 				    raise e_wljdi_common_error;
19596 				end if;
19597 
19598 				if v_wljdi_src_phantom_item_id(l_d)=0 then
19599 				    delete  wsm_copy_requirement_ops
19600 				    where  operation_seq_num=(-1)*l_rtg_op_seq_num
19601 				    and  wip_entity_id =p_wip_entity_id
19602 				    and  component_item_id =l_item_old_id;
19603 				else
19604 				    delete  wsm_copy_requirement_ops
19605 				    where  operation_seq_num=l_rtg_op_seq_num
19606 				    and  wip_entity_id =p_wip_entity_id
19607 				    and  component_item_id =l_item_old_id;
19608 				end if;
19609 
19610 			ELSIF (l_sub_type=WSM_SUB_ADD) THEN
19611 	l_stmt_num:=79.9;
19612 			    l_in := 1;
19613 			    l_v_comp.delete;
19614 			    l_v_comp(l_in).rtg_op_seq_num   := l_rtg_op_seq_num;
19615 			    l_v_comp(l_in).item_id          := l_item_id;
19616 			    l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
19617 			    l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
19618 			    l_v_comp(l_in).wip_supply_type  := v_wljdi_wip_supply_type(l_d);
19619 			    l_v_comp(l_in).qpa                  := v_wljdi_quantity_per_assembly(l_d);
19620 			    l_v_comp(l_in).supply_subinventory  := v_wljdi_supply_subinventory(l_d);
19621 			    l_v_comp(l_in).supply_locator       := v_wljdi_supply_locator_id(l_d);
19622 			    l_v_comp(l_in).mrp_net_flag         := v_wljdi_mrp_net_flag(l_d);
19623 			    l_v_comp(l_in).src_phm_item_id    := v_wljdi_src_phantom_item_id(l_d);
19624 
19625 	l_stmt_num := 750.2;
19626 			    validate_wljdi_op_component(
19627 				p_wip_entity_id     => p_wip_entity_id,
19628 				p_org_id            => p_org_id,
19629 				p_v_comp            => l_v_comp,
19630 				p_substitution_type => l_sub_type,
19631 				p_handle_wip_cur_op => 'N',
19632 				x_error_code        => p_error_code,
19633 				x_error_msg         => p_error_msg);
19634 			    if p_error_code <> 0 then
19635 				raise e_wljdi_common_error;
19636 			    end if;
19637 
19638 			     INSERT INTO WSM_COPY_REQUIREMENT_OPS
19639 			     (wip_entity_id,
19640 			      operation_seq_num,
19641 			      component_item_id,
19642 			      primary_component_id,
19643 			      component_sequence_id,
19644 			      source_phantom_id,
19645 			      bill_sequence_id,
19646 			      effectivity_date,
19647 			      planning_factor,
19648 			      quantity_related,
19649 			      include_in_cost_rollup,
19650 			      check_atp,
19651 			      bom_item_type,
19652 			      recommended,
19653 			      reco_date_required,
19654 			      department_id,
19655 			      organization_id,
19656 			      wip_supply_type,
19657 			      supply_subinventory,
19658 			      supply_locator_id,
19659 			      quantity_per_assembly,
19660 			      bill_quantity_per_assembly,
19661 			      component_yield_factor,
19662 			      component_remarks,
19663 			      last_update_date,
19664 			      last_updated_by,
19665 			      last_update_login,
19666 			      creation_date,
19667 			      created_by,
19668 			      request_id,
19669 			      program_application_id,
19670 			      program_id,
19671 			      program_update_date,
19672 			      attribute_category,
19673 			      attribute1,
19674 			      attribute2,
19675 			      attribute3,
19676 			      attribute4,
19677 			      attribute5,
19678 			      attribute6,
19679 			      attribute7,
19680 			      attribute8,
19681 			      attribute9,
19682 			      attribute10,
19683 			      attribute11,
19684 			      attribute12,
19685 			      attribute13,
19686 			      attribute14,
19687 			      attribute15)
19688 			      select
19689 			       p_wip_entity_id,
19690 			       decode(l_v_comp(l_in).wip_supply_type, WIP_CONSTANTS.PHANTOM,(-1*l_rtg_op_seq_num),l_rtg_op_seq_num),
19691 			       l_v_comp(l_in).item_id,
19692 			       decode(nvl(v_wljdi_src_phantom_item_id(l_d),-1),-1,l_v_comp(l_in).item_id,
19693 									0,l_v_comp(l_in).item_id, v_wljdi_src_phantom_item_id(l_d)),
19694 			       -1,               -- Since not in bill
19695 			       decode(nvl(v_wljdi_src_phantom_item_id(l_d),-1), 0 , l_v_comp(l_in).item_id,
19696 									       -1, -1, v_wljdi_src_phantom_item_id(l_d)),
19697 			       -1,               -- Since not in bill
19698 			       p_creation_date,  -- Effective as on created date for job
19699 			       0,                -- Since it's not a planned component
19700 			       2,                -- No quantity relation
19701 			       1,                -- Include in cost rollup
19702 			       2,                -- No ATP
19703 			       4,                -- Standard component
19704 			       v_wljdi_recommended(l_d),
19705 			       l_v_comp(l_in).date_required,
19706 			       wco.department_id,
19707 			       p_org_id,
19708 			       l_v_comp(l_in).wip_supply_type,
19709 			       l_v_comp(l_in).supply_subinventory,
19710 			       l_v_comp(l_in).supply_locator,
19711 			       l_v_comp(l_in).qpa,
19712 			       1,
19713 			       l_v_comp(l_in).component_yield,
19714 			       v_wljdi_comments(l_d),
19715 			       p_last_updt_date,
19716 			       p_last_updt_by,
19717 			       p_last_updt_login,
19718 			       p_creation_date,
19719 			       p_created_by,
19720 			       p_request_id,
19721 			       p_program_appl_id,
19722 			       p_program_id,
19723 			       p_prog_updt_date,
19724 			       v_wljdi_attribute_category(l_d),
19725 			       v_wljdi_attribute1(l_d),
19726 			       v_wljdi_attribute2(l_d),
19727 			       v_wljdi_attribute3(l_d),
19728 			       v_wljdi_attribute4(l_d),
19729 			       v_wljdi_attribute5(l_d),
19730 			       v_wljdi_attribute6(l_d),
19731 			       v_wljdi_attribute7(l_d),
19732 			       v_wljdi_attribute8(l_d),
19733 			       v_wljdi_attribute9(l_d),
19734 			       v_wljdi_attribute10(l_d),
19735 			       v_wljdi_attribute11(l_d),
19736 			       v_wljdi_attribute12(l_d),
19737 			       v_wljdi_attribute13(l_d),
19738 			       v_wljdi_attribute14(l_d),
19739 			       v_wljdi_attribute15(l_d)
19740 			      from
19741 			      wsm_copy_operations wco
19742 			      where wco.wip_entity_id = p_wip_entity_id
19743 			      and wco.organization_id = p_org_id
19744 			      and wco.operation_seq_num= l_rtg_op_seq_num;
19745 
19746 			END IF;
19747 		    END if;
19748             elsif l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC) then
19749 
19750                 -- {{ Update WIP table
19751                 if(l_job_op_seq_num IS NOT NULL) then
19752                     if(l_job_op_seq_num = l_cur_job_op_seq_num) then
19753 l_stmt_num := 550.1;
19754                         -- pack data into PL/SQL table l_v_comp for validation
19755                         -- for current operation src_phantom_item_id is ignored
19756                         -- bacause we insert consolidated componet info into WRO
19757                         -- also, only item in WRO can be updated/recommeded
19758                         l_in := 1;
19759                         l_v_comp.delete;
19760                         l_v_comp(l_in).job_op_seq_num   := l_job_op_seq_num;
19761                         l_v_comp(l_in).item_id          := l_item_id;
19762                         l_v_comp(l_in).primary_item_id  := l_primary_item_id;
19763                         l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
19764                         l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
19765 l_stmt_num := 550.2;
19766                         validate_wljdi_op_component(
19767                             p_wip_entity_id     => p_wip_entity_id,
19768                             p_org_id            => p_org_id,
19769                             p_v_comp            => l_v_comp,
19770                             p_substitution_type => l_sub_type,
19771                             p_handle_wip_cur_op => 'Y',
19772                             x_error_code        => p_error_code,
19773                             x_error_msg         => p_error_msg);
19774                         if p_error_code <> 0 then
19775                             raise e_wljdi_common_error;
19776                         end if;
19777 
19778 l_stmt_num := 550.3;
19779                         UPDATE WIP_REQUIREMENT_OPERATIONS
19780                         set     date_required           = l_v_comp(l_in).date_required,
19781                                 last_update_date        = p_last_updt_date,
19782                                 last_updated_by         = p_last_updt_by,
19783                                 last_update_login       = p_last_updt_login,
19784                                 request_id              = p_request_id,
19785                                 program_application_id  = p_program_appl_id,
19786                                 program_id              = p_program_id,
19787                                 program_update_date     = p_prog_updt_date
19788                         where   wip_entity_id     = p_wip_entity_id
19789                         and     organization_id   = p_org_id
19790                         and     operation_seq_num = l_job_op_seq_num
19791                         and     inventory_item_id = l_item_id;
19792 
19793                     else -- l_job_op_seq_num <> l_cur_job_op_seq_num
19794                         -- this can happen when recommendations arrive after the job is moved.
19795                         -- OSFM will give a warning message, and ignore this record
19796                         fnd_file.put_line(fnd_file.log,
19797                             'Warning: component recommendations for current operation are ignored ' ||
19798                             '(record_id = ' || l_d || ').' );
19799                     end if;
19800                 -- }} Update WIP table
19801 
19802                 -- {{ Update WSM_COPY table
19803                 elsif(l_rtg_op_seq_num IS NOT NULL) then
19804 l_stmt_num := 560.1;
19805                     -- pack data into PL/SQL table l_v_comp for validation
19806                     if(l_src_phm_item_id IS NULL) then
19807                         l_component_seq_id := NULL;
19808                     end if;
19809                     l_in := 1;
19810                     l_v_comp.delete;
19811                     l_v_comp(l_in).rtg_op_seq_num   := l_rtg_op_seq_num;
19812                     l_v_comp(l_in).item_id          := l_item_id;
19813                     l_v_comp(l_in).primary_item_id  := l_primary_item_id;
19814                     l_v_comp(l_in).src_phm_item_id  := l_src_phm_item_id;
19815                     l_v_comp(l_in).component_seq_id := l_component_seq_id;
19816                     l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
19817                     l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
19818                     l_v_comp(l_in).component_yield  := NULL;
19819 
19820                     -- handle phantom components, l_component_seq_id is required
19821                     if(l_src_phm_item_id IS NOT NULL) then
19822 
19823 l_stmt_num := 560.2;
19824                         SELECT  nvl(use_phantom_routings, 0)
19825                         INTO    l_use_phantom_routings
19826                         FROM    BOM_PARAMETERS
19827                         WHERE   ORGANIZATION_ID = p_org_id;
19828 
19829                         IF (l_use_phantom_routings = 1 and l_sub_type = WSM_SUB_REC) THEN
19830                             fnd_message.set_name('WSM','WSM_COMP_USE_PHANTOM_RTG');
19831                             p_error_msg := fnd_message.get;
19832                             p_error_code := -1;
19833                             raise e_wljdi_common_error;
19834                         end if;
19835 
19836                         if(l_component_seq_id IS NULL) then
19837                             raise e_null_comp_seq_id;
19838                         end if;
19839 
19840                         -- should send all the component with the same l_src_phm_item_id
19841                         -- hence, a local search should be done
19842                         -- this local search will change index l_d
19843                         l_temp := v_wljdi_record_id.next(l_d);
19844                         while(l_temp IS NOT NULL and
19845                               v_wljdi_load_type(l_temp)           = l_load_type and
19846                               v_wljdi_substitution_type(l_temp)   = l_sub_type and
19847                               v_wljdi_routing_op_seq_num(l_temp)  = l_rtg_op_seq_num and
19848                               v_wljdi_src_phantom_item_id(l_temp) = l_src_phm_item_id)
19849                         loop
19850                             l_d := l_temp;
19851                             l_component_seq_id := v_wljdi_component_sequence_id(l_d);
19852                             if(l_component_seq_id IS NULL) then
19853                                 raise e_null_comp_seq_id;
19854                             end if;
19855                             l_in := l_in +1;
19856                             l_v_comp(l_in).rtg_op_seq_num   := l_rtg_op_seq_num;
19857                             l_v_comp(l_in).item_id          := v_wljdi_inventory_item_id_new(l_d) ;
19858                             l_v_comp(l_in).primary_item_id  := l_primary_item_id;
19859                             l_v_comp(l_in).src_phm_item_id  := l_src_phm_item_id;
19860                             l_v_comp(l_in).component_seq_id := l_component_seq_id;
19861                             l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
19862                             l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
19863                             l_v_comp(l_in).component_yield  := NULL;
19864                             l_temp := v_wljdi_record_id.next(l_d);
19865                         end loop;
19866                     end if;
19867 
19868 l_stmt_num := 560.3;
19869                     validate_wljdi_op_component(
19870                         p_wip_entity_id     => p_wip_entity_id,
19871                         p_org_id            => p_org_id,
19872                         p_v_comp            => l_v_comp,
19873                         p_substitution_type => l_sub_type,
19874                         p_handle_wip_cur_op => 'N',
19875                         x_error_code        => p_error_code,
19876                         x_error_msg         => p_error_msg);
19877                     if p_error_code <> 0 then
19878                         raise e_wljdi_common_error;
19879                     end if;
19880 
19881 l_stmt_num := 560.4;
19882                     -- save validated data into an array for balk update
19883                     if(l_sub_type = WSM_SUB_CHG) then
19884                         l_reco_flag := NULL;
19885                     elsif(l_sub_type = WSM_SUB_REC) then
19886                         l_reco_flag := 'Y';
19887                     end if;
19888 
19889                     l_in := l_v_comp.first;
19890                     while (l_in IS NOT NULL)
19891                     loop
19892                         l_v_rtg_op_seq (l_out)      := l_v_comp(l_in).rtg_op_seq_num;
19893                         l_v_comp_item_id(l_out)     := l_v_comp(l_in).item_id;
19894                         l_v_comp_prm_item_id(l_out) := l_v_comp(l_in).primary_item_id;
19895                         l_v_comp_phm_item_id(l_out) := l_v_comp(l_in).src_phm_item_id;
19896                         l_v_comp_comp_seq_id(l_out) := l_v_comp(l_in).component_seq_id;
19897                         l_v_comp_reqd_date(l_out)   := l_v_comp(l_in).date_required;
19898                         l_v_comp_yield(l_out)       := l_v_comp(l_in).component_yield;
19899                         l_v_comp_qpa(l_out)         := l_v_comp(l_in).qpa;  -- ADD bug 3347985
19900                         l_v_recoflag(l_out)         := l_reco_flag;
19901                         l_out := l_out +1;
19902                         l_in  := l_v_comp.next(l_in);
19903                     end loop;
19904 
19905                     if(l_sub_type = WSM_SUB_REC) then
19906 l_stmt_num := 560.5;
19907                         -- get the discommended components
19908                         l_v_disco_item_id.delete;
19909                         l_v_disco_comp_seq_id.delete;
19910                         l_v_disco_src_phm_id.delete;
19911                         open c_disco_comp(
19912                             p_wip_entity_id,
19913                             l_rtg_op_seq_num,
19914                             l_item_id,
19915                             l_primary_item_id,
19916                             l_src_phm_item_id);
19917                         fetch c_disco_comp bulk collect into
19918                             l_v_disco_item_id,
19919                             l_v_disco_comp_seq_id,
19920                             l_v_disco_src_phm_id;
19921                         if(c_disco_comp%rowcount > 0) then
19922                             l_in := l_v_disco_item_id.first;
19923                             while(l_in IS NOT NULL)
19924                             loop
19925                                 l_v_rtg_op_seq(l_out)       := l_rtg_op_seq_num;
19926                                 l_v_comp_item_id(l_out)     := l_v_disco_item_id(l_in);
19927                                 l_v_comp_prm_item_id(l_out) := l_primary_item_id;
19928                                 l_v_comp_phm_item_id(l_out) := l_v_disco_src_phm_id(l_in);
19929                                 l_v_comp_comp_seq_id(l_out) := l_v_disco_comp_seq_id(l_in);
19930                                 l_v_comp_yield(l_out)       := NULL;
19931                                 l_v_comp_qpa(l_out)         := NULL; -- ADD bug 3347985
19932                                 l_v_comp_reqd_date(l_out)   := NULL;
19933                                 l_v_recoflag(l_out)         := 'N';
19934                                 l_out := l_out +1;
19935                                 l_in := l_v_disco_item_id.next(l_in);
19936                             end loop;
19937                         end if;
19938                         close c_disco_comp;
19939                     end if;  -- l_sub_type = WSM_SUB_REC
19940                 end if;     -- l_rtg_op_seq_num IS NOT NULL
19941                 -- }} Update WSM_COPY table
19942 
19943             end if; -- l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC)
19944 
19945             l_d := v_wljdi_record_id.next(l_d);
19946         end loop;
19947 
19948 l_stmt_num := 570;
19949         -- Update WCRO
19950         if(l_v_rtg_op_seq.first IS NOT NULL) then
19951             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
19952 
19953                 -- Note: wcro.source_phantom_id is a NOT NULL column now,
19954                 --       l_v_comp_phm_item_id(i) can be null or -1,
19955                 --       should treate them as the same,
19956 
19957                 update  wsm_copy_requirement_ops
19958                 set     reco_date_required      = NVL(l_v_comp_reqd_date(i), reco_date_required),
19959                         component_yield_factor  = nvl(l_v_comp_yield(i), component_yield_factor),
19960                         quantity_per_assembly   = nvl(l_v_comp_qpa(i), quantity_per_assembly), -- ADD bug 3347985
19961                         recommended             = NVL(l_v_recoflag(i), recommended),
19962                         last_update_date        = p_last_updt_date,
19963                         last_updated_by         = p_last_updt_by,
19964                         last_update_login       = p_last_updt_login,
19965                         request_id              = p_request_id,
19966                         program_application_id  = p_program_appl_id,
19967                         program_id              = p_program_id,
19968                         program_update_date     = p_prog_updt_date
19969                 where   wip_entity_id         = p_wip_entity_id
19970                 and     operation_seq_num     = l_v_rtg_op_seq(i)
19971                 and     component_item_id     = l_v_comp_item_id(i)
19972                 and     primary_component_id  = l_v_comp_prm_item_id(i)
19973                 --and   nvl(source_phantom_id, -1)
19974                 --          = nvl(nvl(l_v_comp_phm_item_id(i), source_phantom_id), -1)
19975                 and     source_phantom_id = nvl(l_v_comp_phm_item_id(i), -1)
19976                 and     component_sequence_id = decode(l_v_comp_phm_item_id(i),
19977                                                        null, component_sequence_id,
19978                                                        -1, component_sequence_id,
19979                                                        l_v_comp_comp_seq_id(i));
19980         end if;
19981         if lbji_debug = 'Y' then
19982             fnd_file.put_line(fnd_file.log, 'Mark recommended operation components ... OK.');
19983         end if;
19984 
19985     end if;
19986     ------------------------------------------------
19987     -- }} validete component
19988     ------------------------------------------------
19989 
19990 
19991 EXCEPTION
19992     when e_invalid_recommend_path then
19993         ROLLBACK to start_import_details;
19994         fnd_message.set_name('WSM','WSM_INVALID_RECO_LINK');
19995         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
19996                     || fnd_message.get;
19997         p_error_code := -1;
19998         fnd_file.put_line(fnd_file.log,
19999             substr('import_lot_job_details: (#'||l_stmt_num||') '
20000                    || p_error_msg, 1, 2000));
20001 
20002     when e_invalid_discommend_path then
20003         ROLLBACK to start_import_details;
20004         fnd_message.set_name('WSM','WSM_INVALID_DISCO_LINK');
20005         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20006                        || fnd_message.get;
20007         p_error_code := -1;
20008         fnd_file.put_line(fnd_file.log,
20009             substr('import_lot_job_details: (#'||l_stmt_num||') '
20010                    || p_error_msg, 1, 2000));
20011 
20012 
20013     when e_invalid_sub_load_type then
20014         ROLLBACK to start_import_details;
20015         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
20016         fnd_message.set_token('FLD_NAME', 'load_type and substitution_type');
20017         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20018                        || fnd_message.get;
20019         p_error_code := -1;
20020         fnd_file.put_line(fnd_file.log,
20021             substr('import_lot_job_details: (#'||l_stmt_num||') '
20022                    || p_error_msg, 1, 2000));
20023 
20024     when e_invalid_op_seq_num then
20025         ROLLBACK to start_import_details;
20026         fnd_message.set_name('WSM','WSM_NULL_FIELD');
20027         fnd_message.set_token('FLD_NAME', 'job_op_seq_num and routing_op_seq_num');
20028         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20029                        || fnd_message.get;
20030         p_error_code := -1;
20031         fnd_file.put_line(fnd_file.log,
20032             substr('import_lot_job_details: (#'||l_stmt_num||') '
20033                    || p_error_msg, 1, 2000));
20034 
20035     when e_invalid_resource_name then
20036         ROLLBACK to start_import_details;
20037         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20038                     || 'Error: Cannot get resource_id from resource name';
20039         p_error_code := -1;
20040         fnd_file.put_line(fnd_file.log,
20041             substr('import_lot_job_details: (#'||l_stmt_num||') '
20042                    || p_error_msg, 1, 2000));
20043 
20044     when e_null_comp_seq_id then
20045         ROLLBACK to start_import_details;
20046         fnd_message.set_name('WSM','WSM_NULL_FIELD');
20047         fnd_message.set_token('FLD_NAME', 'component_seq_id');
20048         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20049                        || fnd_message.get;
20050         p_error_code := -1;
20051         fnd_file.put_line(fnd_file.log,
20052             substr('import_lot_job_details: (#'||l_stmt_num||') '
20053                    || p_error_msg, 1, 2000));
20054 
20055     when e_invalid_item_name then
20056         ROLLBACK to start_import_details;
20057         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20058                        || 'Error: Cannot get item_id from item name';
20059         p_error_code := -1;
20060         fnd_file.put_line(fnd_file.log,
20061             substr('import_lot_job_details: (#'||l_stmt_num||') '
20062                    || p_error_msg, 1, 2000));
20063 
20064     when e_wljdi_common_error then
20065         ROLLBACK to start_import_details;
20066         -- p_error_code/p_error_msg was set
20067         if(l_d IS NOT NULL) then
20068             p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20069                            || p_error_msg;
20070         end if;
20071         fnd_file.put_line(fnd_file.log,
20072             substr('import_lot_job_details: (#'||l_stmt_num||') '
20073                    || p_error_msg, 1, 2000));
20074 
20075     when others then
20076         ROLLBACK to start_import_details;
20077         p_error_code := SQLCODE;
20078         p_error_msg := substr('import_lot_job_details: (#'||l_stmt_num||') '
20079                               ||SQLERRM, 1, 2000);
20080         fnd_file.put_line(fnd_file.log, p_error_msg);
20081 END import_lot_job_details;
20082 
20083 
20084 
20085 
20086 -- **********************************************************************************************
20087 -- PROCEDURE load_wsli_data
20088 -- **********************************************************************************************
20089 
20090 PROCEDURE load_wsli_data (p_group_id IN NUMBER) IS
20091 
20092 -- ==============================================================================================
20093 -- cursors used to bulk bind data from wsli to PL/SQL tables
20094 -- ==============================================================================================
20095 cursor c_wsli_1 is
20096 select
20097         wsli.header_id,
20098         wsli.lot_number,
20099         wsli.inventory_item_id,
20100         wsli.organization_id,
20101         wsli.quantity,
20102         wsli.subinventory_code,
20103         wsli.locator_id,
20104         wsli.revision,
20105         wsli.last_updated_by,
20106         wsli.created_by,
20107         msi.primary_uom_code,
20108         wsli.component_issue_quantity
20109 from    wsm_starting_lots_interface wsli,
20110         wsm_lot_job_interface wlji,
20111         mtl_system_items msi
20112 where   wsli.header_id = wlji.source_line_id
20113 and     wlji.group_id = p_group_id
20114 and     wlji.process_status = 2 -- WIP_CONSTANTS.running
20115 and     wlji.mode_flag = 2
20116 and     msi.inventory_item_id = wsli.inventory_item_id
20117 and     msi.organization_id = wsli.organization_id;
20118 
20119 BEGIN
20120 
20121 -- ==============================================================================================
20122 -- bulk fetching data from wsli to PL/SQL tables
20123 -- ==============================================================================================
20124     open c_wsli_1;
20125     fetch c_wsli_1 bulk collect into
20126         v_wsli_hdr_id,
20127         v_wsli_lot_number,
20128         v_wsli_inventory_item_id,
20129         v_wsli_organization_id,
20130         v_wsli_quantity,
20131         v_wsli_subinventory_code,
20132         v_wsli_locator_id,
20133         v_wsli_revision,
20134         v_wsli_last_updated_by,
20135         v_wsli_created_by,
20136         v_wsli_primary_uom_code,
20137         v_wsli_comp_issue_qty;
20138 
20139 -- ==============================================================================================
20140 -- transfering wsli data into index by PL/SQL table for ease of validation
20141 -- ==============================================================================================
20142     v_wsli_idx := v_wsli_hdr_id.first;
20143     while v_wsli_idx <= v_wsli_hdr_id.last
20144     loop
20145         v_wsli(v_wsli_hdr_id(v_wsli_idx)).lot_number          := v_wsli_lot_number(v_wsli_idx);
20146         v_wsli(v_wsli_hdr_id(v_wsli_idx)).inventory_item_id   := v_wsli_inventory_item_id(v_wsli_idx);
20147         v_wsli(v_wsli_hdr_id(v_wsli_idx)).organization_id     := v_wsli_organization_id(v_wsli_idx);
20148         v_wsli(v_wsli_hdr_id(v_wsli_idx)).quantity            := v_wsli_quantity(v_wsli_idx);
20149         v_wsli(v_wsli_hdr_id(v_wsli_idx)).subinventory_code   := v_wsli_subinventory_code(v_wsli_idx);
20150         v_wsli(v_wsli_hdr_id(v_wsli_idx)).locator_id          := v_wsli_locator_id(v_wsli_idx);
20151         v_wsli(v_wsli_hdr_id(v_wsli_idx)).revision            := v_wsli_revision(v_wsli_idx);
20152         v_wsli(v_wsli_hdr_id(v_wsli_idx)).last_updated_by     := v_wsli_last_updated_by(v_wsli_idx);
20153         v_wsli(v_wsli_hdr_id(v_wsli_idx)).created_by          := v_wsli_created_by(v_wsli_idx);
20154         v_wsli(v_wsli_hdr_id(v_wsli_idx)).primary_uom_code    := v_wsli_primary_uom_code(v_wsli_idx);
20155         v_wsli(v_wsli_hdr_id(v_wsli_idx)).comp_issue_quantity := v_wsli_comp_issue_qty(v_wsli_idx);
20156 
20157         v_wsli_idx := v_wsli_hdr_id.next(v_wsli_idx);
20158     end loop;
20159 
20160     close c_wsli_1;
20161 
20162 END load_wsli_data;
20163 
20164 
20165 
20166 -- **********************************************************************************************
20167 -- PROCEDURE check_errored_mmtt_records
20168 -- **********************************************************************************************
20169 
20170 PROCEDURE check_errored_mmtt_records (
20171         p_header_id         in number,
20172         x_err_code          out nocopy number,
20173         x_err_msg           out nocopy varchar2) is
20174 l_transaction_temp_id   NUMBER;
20175 l_wsli_header_id        NUMBER;
20176 l_wlji_header_id        NUMBER;
20177 l_interface_id          NUMBER;
20178 l_err_code              VARCHAR2(240);
20179 l_err_explanation       VARCHAR2(240);
20180 l_stmt_num              NUMBER;
20181 
20182 cursor recs is
20183         select  mmtt.transaction_temp_id,
20184                 mmtt.error_code,
20185                 mmtt.error_explanation,
20186                 wsli.header_id,
20187                 wlji.header_id
20188         from    mtl_material_transactions_temp mmtt,
20189                 wsm_starting_lots_interface wsli,
20190                 wsm_lot_job_interface wlji
20191         where   mmtt.transaction_header_id = p_header_id
20192         and     mmtt.source_line_id = wsli.header_id
20193         and     wsli.header_id = wlji.source_line_id;
20194 BEGIN
20195 
20196     open recs;
20197     loop
20198         fetch recs
20199         into    l_transaction_temp_id,
20200                 l_err_code,
20201                 l_err_explanation,
20202                 l_wsli_header_id,
20203                 l_wlji_header_id;
20204 
20205         exit when recs%notfound;
20206 
20207         update  wsm_lot_job_interface wljia
20208         set     wljia.process_status = 4,
20209                 wljia.error_code = -2,
20210                 wljia.error_msg = substr(l_err_explanation,1,240)
20211         where   wljia.header_id = l_wlji_header_id;
20212 
20213         x_err_code := -2;
20214     close recs;
20215     END LOOP;
20216 
20217 EXCEPTION
20218 
20219     when others then
20220         x_err_code := SQLCODE;
20221         x_err_msg := 'check_errored_mmtt_records' ||
20222                      '(#'||l_stmt_num||'), (Header_Id=' || l_wlji_header_id||') '
20223                      || SUBSTRB(SQLERRM,1,1000);
20224 END check_errored_mmtt_records;
20225 
20226 
20227 
20228 
20229 
20230 
20231 -- **********************************************************************************************
20232 -- FUNCTION DISCRETE_CHARGES_EXIST
20233 -- **********************************************************************************************
20234 
20235 
20236 FUNCTION discrete_charges_exist(
20237         p_wip_entity_id         in  number,
20238         p_organization_id       in  number,
20239         p_check_mode            in  number )
20240 return boolean is
20241 
20242 retnvalue       boolean;
20243 charges_exist   varchar2(2);
20244 l_stmt_num      number;
20245 
20246 cursor check_discrete_charges is
20247         select  distinct 'x'
20248         from    wip_discrete_jobs   wdj,
20249                 wip_period_balances wpb
20250         where   wdj.wip_entity_id = wpb.wip_entity_id
20251                 and wdj.organization_id = wpb.organization_id
20252                 and wdj.wip_entity_id = p_wip_entity_id
20253                 and wdj.organization_id = p_organization_id
20254                 and (  wdj.quantity_completed <> 0
20255                     or wdj.quantity_scrapped <> 0
20256                     or wpb.tl_resource_in <> 0
20257                     or wpb.tl_overhead_in <> 0
20258                     or wpb.tl_outside_processing_in <> 0
20259                     or wpb.pl_material_in <> 0
20260                     or wpb.pl_material_overhead_in <> 0
20261                     or wpb.pl_resource_in <> 0
20262                     or wpb.pl_overhead_in <> 0
20263                     or wpb.pl_outside_processing_in <> 0
20264                     or wpb.tl_material_out <> 0
20265                     or wpb.tl_resource_out <> 0
20266                     or wpb.tl_overhead_out <> 0
20267                     or wpb.tl_outside_processing_out <> 0
20268                     or wpb.pl_material_out <> 0
20269                     or wpb.pl_material_overhead_out <> 0
20270                     or wpb.pl_resource_out <> 0
20271                     or wpb.pl_overhead_out <> 0
20272                     or wpb.pl_outside_processing_out <> 0
20273                     or exists (
20274                         select 'x'
20275                         from   wip_requirement_operations
20276                         where  organization_id = p_organization_id
20277                         and    wip_entity_id = p_wip_entity_id
20278                         and    quantity_issued <> 0)
20279                     or exists (
20280                         select 'x'
20281                         from   wip_move_txn_interface
20282                         where  organization_id = p_organization_id
20283                         and    wip_entity_id = p_wip_entity_id)
20284                     or exists (
20285                         select 'x'
20286                         from   wsm_lot_move_txn_interface
20287                         where  organization_id = p_organization_id
20288                         and    wip_entity_id = p_wip_entity_id)
20289                     or exists (
20290                         select 'x'
20291                         from   wip_cost_txn_interface
20292                         where  organization_id = p_organization_id
20293                         and    wip_entity_id = p_wip_entity_id)
20294                     or exists (
20295                         select 'x'
20296                         from   mtl_material_transactions_temp
20297                         where  organization_id = p_organization_id
20298                         and    transaction_source_type_id = 5
20299                         and    transaction_source_id = p_wip_entity_id)
20300                     or exists (
20301                         select 'x'
20302                         from   wip_move_transactions
20303                         where  organization_id = p_organization_id
20304                         and    wip_entity_id = p_wip_entity_id)
20305                     /* ST bug fix 3493984 : commented this code */
20306             /*or exists (
20307                         select 'x'
20308                         from   wsm_sm_resulting_jobs
20309                         where  organization_id = p_organization_id
20310                         and    wip_entity_id = p_wip_entity_id) */
20311             /* ST bug fix 3493984 : Added code to consider only successful WIP lot transactions */
20312             or exists (
20313                 select 'x'
20314                         from  wsm_sm_resulting_jobs wsrj
20315                         where  wsrj.organization_id = p_organization_id
20316                         and    wsrj.wip_entity_id = p_wip_entity_id
20317             and    exists (select transaction_id
20318                        from wsm_split_merge_transactions wsmt
20319                        where wsmt.transaction_id = wsrj.transaction_id
20320                        and wsmt.status = 4 )
20321                 )
20322            /* ST bug fix 3493984 : end */
20323            or exists (
20324                         select 'x'
20325                         from   wip_operation_resources
20326                         where  organization_id = p_organization_id
20327                         and    wip_entity_id = p_wip_entity_id
20328                         and    applied_resource_units <> 0));
20329 
20330 --check for only shop floor transactions
20331 
20332 cursor check_discrete_charges_1 is
20333         select  distinct 'x'
20334         from    wip_discrete_jobs   wdj,
20335                 wip_period_balances wpb
20336         where   wdj.wip_entity_id = wpb.wip_entity_id
20337                 and wdj.organization_id = wpb.organization_id
20338                 and wdj.wip_entity_id = p_wip_entity_id
20339                 and wdj.organization_id = p_organization_id
20340                 and (  wdj.quantity_completed <> 0
20341                     or wdj.quantity_scrapped <> 0
20342                     or wpb.tl_resource_in <> 0
20343                     or wpb.tl_overhead_in <> 0
20344                     or wpb.tl_outside_processing_in <> 0
20345                     or wpb.pl_resource_in <> 0
20346                     or wpb.pl_overhead_in <> 0
20347                     or wpb.pl_outside_processing_in <> 0
20348                     or wpb.tl_resource_out <> 0
20349                     or wpb.tl_overhead_out <> 0
20350                     or wpb.tl_outside_processing_out <> 0
20351                     or wpb.pl_resource_out <> 0
20352                     or wpb.pl_overhead_out <> 0
20353                     or wpb.pl_outside_processing_out <> 0
20354                     or exists (
20355                         select 'x'
20356                         from   wip_move_txn_interface
20357                         where  organization_id = p_organization_id
20358                         and    wip_entity_id = p_wip_entity_id)
20359                     or exists (
20360                         select 'x'
20361                         from   wsm_lot_move_txn_interface
20362                         where  organization_id = p_organization_id
20363                         and    wip_entity_id = p_wip_entity_id)
20364                     or exists (
20365                         select 'x'
20366                         from   wip_cost_txn_interface
20367                         where  organization_id = p_organization_id
20368                         and    wip_entity_id = p_wip_entity_id)
20369                     or exists (
20370                         select 'x'
20371                         from   wip_move_transactions
20372                         where  organization_id = p_organization_id
20373                         and    wip_entity_id = p_wip_entity_id)
20374                     or exists (
20375                         select 'x'
20376                         from   wsm_sm_resulting_jobs
20377                         where  organization_id = p_organization_id
20378                         and    wip_entity_id = p_wip_entity_id)
20379                     or exists (
20380                         select 'x'
20381                         from   wip_operation_resources
20382                         where  organization_id = p_organization_id
20383                         and    wip_entity_id = p_wip_entity_id
20384                         and    applied_resource_units <> 0));
20385 BEGIN
20386 
20387     retnvalue := FALSE;
20388 
20389     if (p_check_mode = 1) then
20390         open check_discrete_charges_1;
20391         fetch check_discrete_charges_1 into charges_exist;
20392 
20393         IF (check_discrete_charges_1%FOUND) THEN
20394             retnvalue := TRUE;
20395             close  check_discrete_charges_1;
20396             RETURN retnvalue;
20397         ELSE
20398             close  check_discrete_charges_1;
20399         END IF;
20400 
20401     else
20402         open check_discrete_charges;
20403         fetch check_discrete_charges into charges_exist;
20404 
20405         IF (check_discrete_charges%FOUND) THEN
20406             retnvalue := TRUE;
20407             close  check_discrete_charges;
20408             RETURN retnvalue;
20409         ELSE
20410             close  check_discrete_charges;
20411         END IF;
20412 
20413     end if;
20414 
20415     RETURN retnvalue;
20416 
20417 END discrete_charges_exist;
20418 
20419 
20420 
20421 -- **********************************************************************************************
20422 -- PROCEDURE insert_into_period_balances
20423 -- **********************************************************************************************
20424 
20425 PROCEDURE insert_into_period_balances (
20426         p_wip_entity_id         in number,
20427         p_organization_id       in number,
20428         p_class_code            in varchar2,
20429         p_release_date          in date,
20430         p_error_code            out nocopy number,
20431         p_err_msg               out nocopy varchar2 ) is
20432 
20433 x_user_id       number := fnd_global.user_id;
20434 x_login_id      number := fnd_global.login_id;
20435 --l_inv_period_id number;   -- BUG3126650
20436 l_cnt           number;     -- bug 3571360
20437 
20438 BEGIN
20439 
20440     -- BD: bugfix 3299811, this is a regression of bugfix 3126650
20441     --l_inv_period_id := wsmputil.get_inv_acct_period (
20442     --        x_err_code         => p_error_code,
20443     --        x_err_msg          => p_err_msg,
20444     --        p_organization_id  => p_organization_id,
20445     --        p_date             => trunc(nvl(p_release_date, sysdate)) );
20446     --if(p_error_code <> 0) then
20447     --    p_error_code := -1;
20448     --    fnd_message.set_name('WIP', 'WIP_NO_ACCT_PERIOD');
20449     --    fnd_message.set_token('FLD_NAME','Wip Accounting Period');
20450     --    p_err_msg := fnd_message.get;
20451     --    return;
20452     --end if;
20453     -- ED: bugfix 3299811
20454 
20455     insert into wip_period_balances (
20456             acct_period_id,
20457             wip_entity_id,
20458             last_update_date,
20459             last_updated_by,
20460             creation_date,
20461             created_by,
20462             last_update_login,
20463             organization_id,
20464             class_type,
20465             tl_resource_in,
20466             tl_overhead_in,
20467             tl_outside_processing_in,
20468             pl_material_in,
20469             pl_material_overhead_in,
20470             pl_resource_in,
20471             pl_overhead_in,
20472             pl_outside_processing_in,
20473             tl_material_out,
20474             tl_resource_out,
20475             tl_overhead_out,
20476             tl_outside_processing_out,
20477             pl_material_out,
20478             pl_material_overhead_out,
20479             pl_resource_out,
20480             pl_overhead_out,
20481             pl_outside_processing_out,
20482             pl_material_overhead_var,
20483             pl_material_var,
20484             pl_outside_processing_var,
20485             pl_overhead_var,
20486             pl_resource_var,
20487             tl_material_var,
20488             tl_outside_processing_var,
20489             tl_overhead_var,
20490             tl_resource_var,
20491             tl_material_overhead_out,
20492             tl_material_overhead_var)
20493     select  oap.acct_period_id,
20494             p_wip_entity_id,
20495             sysdate, x_user_id,
20496             sysdate, x_user_id, x_login_id,
20497             p_organization_id, wc.class_type,
20498             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20499             0, 0, 0, 0, 0, 0, 0, 0
20500      from   org_acct_periods oap,
20501             wip_accounting_classes wc
20502      where  wc.class_code = p_class_code
20503      and    wc.organization_id = p_organization_id
20504      and    oap.organization_id = p_organization_id
20505      and    oap.schedule_close_date >=
20506                  trunc(inv_le_timezone_pub.get_le_day_for_inv_org(
20507                         nvl(p_release_date, sysdate),
20508                         p_organization_id))
20509      and    oap.period_close_date is null
20510      and    not exists (
20511                  select 'balance record already there'
20512                  from   wip_period_balances wpb
20513                  where  wpb.wip_entity_id = p_wip_entity_id
20514                  and    wpb.acct_period_id = oap.acct_period_id
20515                  and    wpb.organization_id = oap.organization_id);
20516 
20517     l_cnt := SQL%ROWCOUNT;      -- bug 3571360
20518 
20519     -- BD: bugfix 3299811, this is a regression of bugfix 3126650
20520     --select  l_inv_period_id,
20521     --        p_wip_entity_id,
20522     --        sysdate, x_user_id,
20523     --        sysdate, x_user_id, x_login_id,
20524     --        p_organization_id,
20525     --        wc.class_type,
20526     --        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20527     --        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20528     --        0, 0, 0, 0, 0, 0, 0, 0
20529     --from    wip_accounting_classes wc
20530     --where   wc.class_code = p_class_code
20531     --and     wc.organization_id = p_organization_id
20532     --and     not exists (
20533     --             select 'balance record already there'
20534     --             from   wip_period_balances wpb
20535     --             where  wpb.wip_entity_id = p_wip_entity_id
20536     --             and    wpb.acct_period_id = l_inv_period_id
20537     --             and    wpb.organization_id = p_organization_id);
20538     -- ED: bugfix 3299811
20539 
20540 
20541     if lbji_debug = 'Y' then
20542         fnd_file.put_line(fnd_file.log, 'Inserted '||l_cnt||' rows into wip_period_balances');
20543     end if;
20544 
20545     --if SQL%NOTFOUND then      -- bug 3571360
20546     if l_cnt <= 0 then          -- bug 3571360
20547         p_error_code := -1;
20548         fnd_message.set_name('WIP', 'WIP_NO_ACCT_PERIOD');
20549         fnd_message.set_token('FLD_NAME','Wip Accounting Period');
20550         p_err_msg := fnd_message.get;
20551     end if;
20552 
20553 exception
20554     when others then
20555         p_err_msg := 'insert_into_period_balances: '|| substr(SQLERRM,1,200);
20556         p_error_code := SQLCODE;
20557 
20558 end insert_into_period_balances;
20559 
20560 
20561 
20562 -- **********************************************************************************************
20563 -- PROCEDURE build_job_detail_info (overloaded)
20564 -- **********************************************************************************************
20565 
20566 procedure build_job_detail_info(
20567         p_common_routing_sequence_id    in number,
20568         p_common_bill_sequence_id       in number,
20569         p_status_type                   in number,
20570         p_org_id                        in number,
20571         p_wip_entity_id                 in number,
20572         p_last_updt_date                in date,
20573         p_last_updt_by                  in number,
20574         p_creation_date                 in date,
20575         p_created_by                    in number,
20576         p_last_updt_login               in number,
20577         p_request_id                    in number,
20578         p_program_appl_id               in number,
20579         p_program_id                    in number,
20580         p_prog_updt_date                in date,
20581         p_item                          in number,      -- not used
20582         p_job_type                      in number,
20583         p_bom_reference_id              in number,      -- not used
20584         p_routing_reference_id          in number,      -- not used
20585         p_wip_supply_type               in number,
20586         p_job_scheduled_start_date      in date,        -- not used
20587         p_job_scheduled_compl_date      in date,        -- not used
20588         p_start_quantity                in number,
20589         p_bom_revision_date             in date,
20590         p_routing_revision_date         in date,
20591         p_alt_bom_designator            in varchar2,
20592         p_alt_routing_designator        in varchar2,
20593         p_rtg_op_seq_num                in number,
20594         p_error_code                    out nocopy number,
20595         p_error_msg                     out nocopy varchar2,
20596         p_src_client_server             in number,      -- bug 3311985 new parameter
20597         p_po_creation_time              in number) IS   -- bug 3311985 new parameter
20598 
20599 l_stmt_num              number;
20600 l_job_op_seq_num        number;
20601 l_rtg_op_seq_num0       number;
20602 l_rtg_op_seq_num        number;
20603 l_start_op_seq_num      number;
20604 l_start_op_seq_id       number;
20605 l_op_seq_id             number;
20606 l_temp_num              number;
20607 l_max_op_seq_num        number;
20608 e_build_job_dtl         exception;
20609 
20610 cursor the_path_cur is (
20611     select     from_op_seq_num,
20612                from_op_seq_id,
20613                level
20614     from       wsm_copy_op_networks
20615     start with (to_op_seq_num = l_rtg_op_seq_num0 and
20616                 recommended = 'Y' and
20617                 wip_entity_id = p_wip_entity_id)
20618     connect by (to_op_seq_num = prior from_op_seq_num and
20619                 recommended = 'Y' and
20620                 wip_entity_id = p_wip_entity_id)
20621     union
20622     select     l_rtg_op_seq_num0 "from_op_seq_num",
20623                l_op_seq_id,
20624                -1
20625     from       dual
20626 ) order by 2 desc;
20627 
20628 BEGIN
20629     p_error_code := 0;
20630     p_error_msg := '';
20631 
20632 l_stmt_num := 10;
20633     -- The following two call should be called when job copy is available
20634     find_copy_routing_start(
20635         p_wip_entity_id,
20636         l_start_op_seq_num,
20637         l_start_op_seq_id,
20638         p_error_code,
20639         p_error_msg);
20640     if p_error_code <> 0 then
20641         raise e_build_job_dtl;
20642     end if;
20643 
20644 l_stmt_num := 20;
20645     if p_rtg_op_seq_num is null then -- first operation
20646         l_rtg_op_seq_num0 := l_start_op_seq_num;
20647     else
20648         l_rtg_op_seq_num0 := p_rtg_op_seq_num;
20649     end if;
20650 
20651     find_copy_op_seq_id (
20652         p_wip_entity_id,
20653         l_rtg_op_seq_num0,
20654         l_op_seq_id,
20655         p_error_code,
20656         p_error_msg);
20657     if p_error_code <> 0 then
20658         raise e_build_job_dtl;
20659     end if;
20660 
20661 l_stmt_num := 30;
20662     -- osp begin
20663     if(l_op_seq_id <> -1) then
20664         if wsmputil.check_po_move (
20665                 p_sequence_id      => l_op_seq_id,
20666                 p_sequence_id_type => 'O',
20667                 p_routing_rev_date => p_routing_revision_date,
20668                 x_err_code         => p_error_code,
20669                 x_err_msg          => p_error_msg)
20670         then
20671             fnd_message.set_name('WSM','WSM_FIRST_OP_PO_MOVE');
20672             p_error_code := -1;
20673             p_error_msg := fnd_message.get;
20674             raise e_build_job_dtl;
20675         end if;
20676     end if;
20677     --osp end
20678 
20679     --l_job_op_seq_num := 0; This initialization has been moved inside the if loop for bug 4647901
20680 
20681     IF v_insert_wip is NULL or v_insert_wip  = 2 THEN
20682 
20683 	l_job_op_seq_num := 0;
20684     --OPTII-PERF: For jobs without phantom requirement,
20685     --WIP Tables are updated while creating the job copies
20686 l_stmt_num := 40;
20687         OPEN the_path_cur;
20688         LOOP
20689             FETCH the_path_cur
20690             INTO  l_rtg_op_seq_num,
20691                   l_op_seq_id,
20692                   l_temp_num;
20693             EXIT when the_path_cur%NOTFOUND;
20694 
20695             l_job_op_seq_num := l_job_op_seq_num + g_op_seq_incr;
20696 
20697 l_stmt_num := 50;
20698             -- will call new procedure to insert WO, WOR, WRO, WOY, WSOR
20699             WSMPOPRN.copy_plan_to_execution(
20700                     x_error_code                => p_error_code,
20701                     x_error_msg                 => p_error_msg,
20702                     p_org_id                    => p_org_id,
20703                     p_wip_entity_id             => p_wip_entity_id,
20704                     p_to_job_op_seq_num         => l_job_op_seq_num,
20705                     p_to_rtg_op_seq_num         => l_rtg_op_seq_num,
20706                     p_to_op_seq_id              => l_op_seq_id,
20707                     p_reco_op_flag              => 'Y',
20708                     p_txn_quantity              => p_start_quantity,
20709                     p_txn_date                  => p_last_updt_date,
20710                     p_user                      => p_last_updt_by,
20711                     p_login                     => p_last_updt_login,
20712                     p_request_id                => p_request_id,
20713                     p_program_application_id    => p_program_appl_id,
20714                     p_program_id                => p_program_id,
20715                     p_dup_val_ignore            => 'N',
20716                     p_start_quantity            => p_start_quantity);
20717 
20718             if p_error_code <> 0 then
20719                 raise e_build_job_dtl;
20720             end if;
20721 
20722         END LOOP;
20723         CLOSE the_path_cur;
20724 
20725         -- {{ Update quantity
20726 l_stmt_num := 80;
20727         select max(operation_seq_num)
20728         into   l_max_op_seq_num
20729         from   wip_operations
20730         where  wip_entity_id = p_wip_entity_id;
20731 
20732 l_stmt_num := 90;
20733         update  wip_operations wo
20734         set     wo.previous_operation_seq_num = (
20735                     select max(operation_seq_num)
20736                     from   wip_operations
20737                     where  wip_entity_id = p_wip_entity_id
20738                     and    operation_seq_num < wo.operation_seq_num),
20739                 wo.next_operation_seq_num = (
20740                     select min(operation_seq_num)
20741                     from   wip_operations
20742                     where  wip_entity_id = p_wip_entity_id
20743                     and    operation_seq_num > wo.operation_seq_num),
20744                 wo.quantity_in_queue =
20745                     decode(operation_seq_num,
20746                            l_max_op_seq_num,
20747                            (decode(p_status_type,
20748                                    3, round(p_start_quantity,
20749                                             wip_constants.max_displayed_precision),
20750                                    0)),
20751                            0)
20752         where   wo.wip_entity_id = p_wip_entity_id;
20753         if lbji_debug = 'Y' then
20754             fnd_file.put_line(fnd_file.log,'Update Quantity, Prev/Next Op in wo(l_stmt_num='
20755                                            ||l_stmt_num||'): '|| SQL%ROWCOUNT);
20756         end if;
20757 
20758 l_stmt_num := 100;
20759         /*This SQL is commneted as per bug 5125991
20760 		update wip_operations
20761         set    quantity_completed = p_start_quantity
20762         where  wip_entity_id = p_wip_entity_id
20763         and    operation_seq_num  < l_max_op_seq_num;
20764         if lbji_debug = 'Y' then
20765             fnd_file.put_line(fnd_file.log,'Update Quantity_Completed in wo(l_stmt_num='
20766                                             ||l_stmt_num||'): '|| SQL%ROWCOUNT);
20767         end if;   */
20768 
20769 l_stmt_num := 110;
20770         update wip_requirement_operations
20771         set    quantity_issued = required_quantity
20772         where  wip_entity_id = p_wip_entity_id
20773         and    operation_seq_num < l_max_op_seq_num
20774         and    wip_supply_type not in (2, 4, 5, 6); -- Fix for bug #2685463
20775         if lbji_debug = 'Y' then
20776             fnd_file.put_line(fnd_file.log,'Update Quantity_Issued in wro(l_stmt_num='
20777                                            ||l_stmt_num||'): '|| SQL%ROWCOUNT);
20778         end if;
20779         -- }} Update quantity
20780         if (p_status_type = 3) then
20781                 update wsm_lot_based_jobs
20782                 set    (current_job_op_seq_num,
20783                         current_rtg_op_seq_num)
20784                        =(select wo.operation_seq_num,
20785                                  wo.wsm_op_seq_num
20786                           from   wip_operations wo
20787                           where  organization_id = p_org_id
20788                           and    wip_entity_id   = p_wip_entity_id
20789                           and    wo.quantity_in_queue
20790                                + wo.quantity_running
20791                                + wo.quantity_waiting_to_move  > 0)
20792                 where  wip_entity_id   = p_wip_entity_id
20793                 and    organization_id = p_org_id;
20794        end if;
20795     END IF;--Check on v_insert_wip
20796             v_insert_wip := NULL;
20797             v_phantom_exists := NULL;
20798 
20799 --OPTII-PERF: Updating current job and routing op seq num is moved inside the
20800 --check on v_insert_wip
20801     --
20802     -- Begin R12Dev Update to set the current_job/rtg_op_seq_num on WLBJ
20803     --
20804     /*
20805     if (p_status_type = 3) then
20806 
20807         update wsm_lot_based_jobs
20808         set    (current_job_op_seq_num,
20809                 current_rtg_op_seq_num)
20810                =(select wo.operation_seq_num,
20811                          wo.wsm_op_seq_num
20812                   from   wip_operations wo
20813                   where  organization_id = p_org_id
20814                   and    wip_entity_id   = p_wip_entity_id
20815                   and    wo.quantity_in_queue
20816                        + wo.quantity_running
20817                        + wo.quantity_waiting_to_move  > 0)
20818         where  wip_entity_id   = p_wip_entity_id
20819         and    organization_id = p_org_id;
20820     end if;
20821 */
20822     --
20823     -- End R12Dev
20824     --
20825 
20826     -- bug 3311985: osp code moved from build_job_header_info
20827     -- osp begin -- this has to be put after insertion into wip_entities
20828     if p_status_type = 3 and p_po_creation_time <> wip_constants.manual_creation then
20829         if p_src_client_server = 1 then
20830 l_stmt_num := 130;
20831             if wsmputil.check_osp_operation(p_wip_entity_id, l_job_op_seq_num, p_org_id) then
20832                 g_num_of_osp_exists := g_num_of_osp_exists + 1;
20833                 wip_osp.create_requisition(
20834                         p_wip_entity_id          => p_wip_entity_id,
20835                         p_organization_id        => p_org_id,
20836                         p_repetitive_schedule_id => null,
20837                         p_operation_seq_num      => l_job_op_seq_num,
20838                         p_resource_seq_num       => null,
20839                         p_run_reqimport          => wip_constants.yes);
20840             end if; -- check_osp_operation
20841         else
20842 l_stmt_num := 140;
20843             -- if build_job_info is called from form, only then P_Run_ReqImport should be YES, i.e.
20844             -- the requisition import concurrent request should be launched immediately.
20845             if wsmputil.check_osp_operation(p_wip_entity_id, l_job_op_seq_num, p_org_id) then
20846                 g_num_of_osp_exists := g_num_of_osp_exists + 1;
20847                 wip_osp.create_requisition(
20848                         p_wip_entity_id          => p_wip_entity_id,
20849                         p_organization_id        => p_org_id,
20850                         p_repetitive_schedule_id => null,
20851                         p_operation_seq_num      => l_job_op_seq_num,
20852                         p_resource_seq_num       => null,
20853                         p_run_reqimport          => wip_constants.no);
20854             end if; -- check_osp_operation
20855         end if; --  p_src_client_server
20856     end if; --  p_status_type = 3
20857     -- osp end
20858 
20859 EXCEPTION
20860     when e_build_job_dtl then
20861         -- p_error_code/p_error_msg was set
20862         fnd_file.put_line(fnd_file.log,
20863             substr('build_job_detail_info (#'||l_stmt_num||') '
20864                    ||p_error_msg, 1, 2000));
20865 
20866     when others then
20867         p_error_code := SQLCODE;
20868         p_error_msg := substr('build_job_detail_info (#'||l_stmt_num||') '
20869                               || SQLERRM, 1, 2000);
20870         fnd_file.put_line(fnd_file.log, substr(p_error_msg, 1, 2000));
20871 END build_job_detail_info;
20872 
20873 
20874 -- **********************************************************************************************
20875 -- PROCEDURE build_job_header_info (overloaded)
20876 -- **********************************************************************************************
20877 
20878 procedure build_job_header_info(
20879         p_common_routing_sequence_id    in number,
20880         p_common_bill_sequence_id       in number,
20881         p_status_type                   in number,
20882         p_class_code                    in varchar2,
20883         p_org_id                        in number,
20884         p_wip_entity_id                 in out nocopy number,
20885         p_last_updt_date                in date,
20886         p_last_updt_by                  in number,
20887         p_creation_date                 in date,
20888         p_created_by                    in number,
20889         p_last_updt_login               in number,
20890         p_request_id                    in number,
20891         p_program_appl_id               in number,
20892         p_program_id                    in number,
20893         p_prog_updt_date                in date,
20894         p_source_line_id                in number,
20895         p_source_code                   in varchar2,
20896         p_description                   in varchar2,
20897         p_item                          in number,
20898         p_job_type                      in number,
20899         p_bom_reference_id              in number,
20900         p_routing_reference_id          in number,
20901         p_firm_planned_flag             in number,
20902         p_wip_supply_type               in number,
20903         p_job_scheduled_start_date      in date,
20904         p_job_scheduled_compl_date      in date,
20905         p_start_quantity                in number,
20906         p_net_quantity                  in number,
20907         p_coproducts_supply             in number,
20908         p_bom_revision                  in varchar2,
20909         p_routing_revision              in varchar2,
20910         p_bom_revision_date             in date,
20911         p_routing_revision_date         in date,
20912         p_lot_number                    in varchar2,
20913         p_alt_bom_designator            in varchar2,
20914         p_alt_routing_designator        in varchar2,
20915         p_priority                      in number,
20916         p_due_date                      in date,
20917         p_attribute_category            in varchar2,
20918         p_attribute1                    in varchar2,
20919         p_attribute2                    in varchar2,
20920         p_attribute3                    in varchar2,
20921         p_attribute4                    in varchar2,
20922         p_attribute5                    in varchar2,
20923         p_attribute6                    in varchar2,
20924         p_attribute7                    in varchar2,
20925         p_attribute8                    in varchar2,
20926         p_attribute9                    in varchar2,
20927         p_attribute10                   in varchar2,
20928         p_attribute11                   in varchar2,
20929         p_attribute12                   in varchar2,
20930         p_attribute13                   in varchar2,
20931         p_attribute14                   in varchar2,
20932         p_attribute15                   in varchar2,
20933         p_job_name                      in varchar2,
20934         p_completion_subinventory       in varchar2,
20935         p_completion_locator_id         in number,
20936         p_demand_class                  in varchar2,
20937         p_project_id                    in number,
20938         p_task_id                       in number,
20939         p_schedule_group_id             in number,
20940         p_build_sequence                in number,
20941         p_line_id                       in number,
20942         p_kanban_card_id                in number,
20943         p_overcompl_tol_type            in number,
20944         p_overcompl_tol_value           in number,
20945         p_end_item_unit_number          in number,
20946         p_src_client_server             in number,
20947         p_po_creation_time              in number,
20948         p_date_released                 in date,
20949         p_wjsq_uom_code                 in tbl_wjsq_uom_code,
20950         p_wjsq_start_quantity           in tbl_wjsq_start_quantity,
20951         p_error_code                    out nocopy number,
20952         p_error_msg                     out nocopy varchar2) is
20953 
20954 
20955 l_stmt_num                      number;
20956 l_material_account              number;
20957 l_material_overhead_account     number;
20958 l_resource_account              number;
20959 l_outside_processing_account    number;
20960 l_material_variance_account     number;
20961 l_resource_variance_account     number;
20962 l_outside_proc_var_acc          number;
20963 l_std_cost_adjustment_account   number;
20964 l_overhead_account              number;
20965 l_overhead_variance_account     number;
20966 l_po_creation_time              number;
20967 l_est_scrap_account             number;
20968 l_est_scrap_var_account         number;
20969 l_job_op_seq_num                number := NULL;
20970 l_return_status                 varchar2(1);
20971 l_translated_meaning            varchar2(240);
20972 
20973 e_build_job                     exception;
20974 
20975 
20976 BEGIN
20977     p_error_code := 0;
20978     p_error_msg := '';
20979 
20980 
20981 l_stmt_num := 100;
20982     -- get a new wip entity id
20983     select wip_entities_s.nextval
20984     into   p_wip_entity_id
20985     from   dual;
20986 
20987 
20988 l_stmt_num := 110;
20989     -- get wip accounting class information
20990     select  wac.material_account,
20991             wac.material_overhead_account,
20992             wac.resource_account,
20993             wac.outside_processing_account,
20994             wac.material_variance_account,
20995             wac.resource_variance_account,
20996             wac.outside_proc_variance_account,
20997             wac.std_cost_adjustment_account,
20998             wac.overhead_account,
20999             wac.overhead_variance_account,
21000             params.po_creation_time,
21001             wac.est_scrap_account,
21002             wac.est_scrap_var_account
21003     into    l_material_account,
21004             l_material_overhead_account,
21005             l_resource_account,
21006             l_outside_processing_account,
21007             l_material_variance_account,
21008             l_resource_variance_account,
21009             l_outside_proc_var_acc,
21010             l_std_cost_adjustment_account,
21011             l_overhead_account,
21012             l_overhead_variance_account,
21013             l_po_creation_time,
21014             l_est_scrap_account,
21015             l_est_scrap_var_account
21016     from    wip_accounting_classes wac,
21017             wip_parameters params
21018     where   wac.class_code(+)= p_class_code
21019     and     wac.organization_id(+)= p_org_id
21020     and     params.organization_id = p_org_id;
21021 
21022 
21023 l_stmt_num := 120;
21024     -- check scrap account
21025     if wsmputil.wsm_esa_enabled(
21026             p_wip_entity_id => null,
21027             err_code        => p_error_code,
21028             err_msg         => p_error_msg,
21029             p_org_id        => p_org_id,
21030             p_job_type      => p_job_type) = 1 and
21031        (l_est_scrap_account is null or l_est_scrap_var_account is null)
21032     then
21033         fnd_message.set_name('WSM','WSM_NO_WAC_SCRAP_ACC');
21034         fnd_message.set_token('CC',p_class_code);
21035         p_error_code := -1;
21036         p_error_msg := fnd_message.get;
21037         raise e_build_job;
21038     end if;
21039 
21040 
21041 l_stmt_num := 130;
21042     -- insert into WDJ
21043     insert into wip_discrete_jobs (
21044         wip_entity_id,
21045         organization_id,
21046         last_update_date,
21047         last_updated_by,
21048         creation_date,
21049         created_by,
21050         last_update_login,
21051         request_id,
21052         program_application_id,
21053         program_id,
21054         program_update_date,
21055         source_line_id,
21056         source_code,
21057         description,
21058         status_type,
21059         date_released,
21060         primary_item_id,
21061         bom_reference_id,
21062         routing_reference_id,
21063         firm_planned_flag,
21064         job_type,
21065         wip_supply_type,
21066         class_code,
21067         material_account,
21068         material_overhead_account,
21069         resource_account,
21070         outside_processing_account,
21071         material_variance_account,
21072         resource_variance_account,
21073         outside_proc_variance_account,
21074         std_cost_adjustment_account,
21075         overhead_account,
21076         overhead_variance_account,
21077         scheduled_start_date,
21078         scheduled_completion_date,
21079         start_quantity,
21080         quantity_completed,
21081         quantity_scrapped,
21082         net_quantity,
21083         common_bom_sequence_id,
21084         common_routing_sequence_id,
21085         bom_revision,
21086         routing_revision,
21087         bom_revision_date,
21088         routing_revision_date,
21089         lot_number,
21090         alternate_bom_designator,
21091         alternate_routing_designator,
21092         completion_subinventory,
21093         completion_locator_id,
21094         demand_class,
21095         project_id,
21096         task_id,
21097         schedule_group_id,
21098         build_sequence,
21099         line_id,
21100         kanban_card_id,
21101         overcompletion_tolerance_type,
21102         overcompletion_tolerance_value,
21103         end_item_unit_number,
21104         po_creation_time,
21105         priority,
21106         due_date,
21107         attribute_category,
21108         attribute1,
21109         attribute2,
21110         attribute3,
21111         attribute4,
21112         attribute5,
21113         attribute6,
21114         attribute7,
21115         attribute8,
21116         attribute9,
21117         attribute10,
21118         attribute11,
21119         attribute12,
21120         attribute13,
21121         attribute14,
21122         attribute15,
21123         est_scrap_account,
21124         est_scrap_var_account,
21125         coproducts_supply
21126     ) values (
21127         p_wip_entity_id,
21128         p_org_id,
21129         p_last_updt_date,
21130         p_last_updt_by,
21131         p_creation_date,
21132         p_created_by,
21133         p_last_updt_login,
21134         p_request_id,
21135         p_program_appl_id,
21136         p_program_id,
21137         p_prog_updt_date,
21138         p_source_line_id,
21139         p_source_code,
21140         p_description,
21141         p_status_type,
21142         -- BC: bug 3382472
21143         --NVL(p_date_released, decode(p_status_type, wip_constants.unreleased, null, sysdate)),
21144         NVL(p_date_released, decode(p_status_type,
21145                                     wip_constants.released, sysdate,
21146                                     null)),
21147         -- EC: bug 3382472
21148         p_item,
21149         decode(p_job_type, 3, p_bom_reference_id, null),
21150         decode(p_job_type, 3, p_routing_reference_id, null),
21151         p_firm_planned_flag,
21152         decode(p_job_type, 3, wip_constants.nonstandard, wip_constants.standard),
21153         p_wip_supply_type,
21154         p_class_code,
21155         l_material_account,
21156         l_material_overhead_account,
21157         l_resource_account,
21158         l_outside_processing_account,
21159         l_material_variance_account,
21160         l_resource_variance_account,
21161         l_outside_proc_var_acc,
21162         l_std_cost_adjustment_account,
21163         l_overhead_account,
21164         l_overhead_variance_account,
21165         trunc(p_job_scheduled_start_date,'mi'),     --round(p_job_scheduled_start_date,'mi'),
21166         trunc(p_job_scheduled_compl_date,'mi'),     --round(p_job_scheduled_compl_date,'mi'),
21167         round(p_start_quantity, wip_constants.max_displayed_precision),
21168         0, -- quantity_completed
21169         0, -- quantity_scrapped
21170         round(p_net_quantity, wip_constants.max_displayed_precision),
21171         p_common_bill_sequence_id,
21172         p_common_routing_sequence_id,
21173         p_bom_revision,
21174         p_routing_revision,
21175         p_bom_revision_date,
21176         p_routing_revision_date,
21177         p_lot_number,
21178         p_alt_bom_designator,
21179         p_alt_routing_designator,
21180         p_completion_subinventory,
21181         p_completion_locator_id,
21182         p_demand_class,
21183         p_project_id,
21184         p_task_id,
21185         p_schedule_group_id,
21186         p_build_sequence,
21187         p_line_id,
21188         p_kanban_card_id,
21189         p_overcompl_tol_type,
21190         p_overcompl_tol_value,
21191         p_end_item_unit_number,
21192         l_po_creation_time,
21193         p_priority,
21194         p_due_date,
21195         p_attribute_category,
21196         p_attribute1,
21197         p_attribute2,
21198         p_attribute3,
21199         p_attribute4,
21200         p_attribute5,
21201         p_attribute6,
21202         p_attribute7,
21203         p_attribute8,
21204         p_attribute9,
21205         p_attribute10,
21206         p_attribute11,
21207         p_attribute12,
21208         p_attribute13,
21209         p_attribute14,
21210         p_attribute15,
21211         l_est_scrap_account,
21212         l_est_scrap_var_account,
21213         p_coproducts_supply
21214     );
21215     if lbji_debug = 'Y' then
21216         fnd_file.put_line(fnd_file.log, 'Inserted '||SQL%ROWCOUNT||' rows into wdj');
21217     end if;
21218 
21219 
21220 l_stmt_num := 135;
21221     -- insert into WLBJ
21222     if g_skip_wlbj <> 1 THEN --OPTII-PERF: This will be created in create_jobcopies
21223         insert into wsm_lot_based_jobs (
21224         wip_entity_id,
21225         organization_id,
21226         on_rec_path,
21227         internal_copy_type,
21228         last_update_date,
21229         last_updated_by,
21230         creation_date,
21231         created_by,
21232         last_update_login,
21233         request_id,
21234         program_application_id,
21235         program_id,
21236         program_update_date
21237         ) values (
21238         p_wip_entity_id,
21239         p_org_id,
21240         'Y',
21241         1,
21242         p_last_updt_date,
21243         p_last_updt_by,
21244         p_creation_date,
21245         p_created_by,
21246         p_last_updt_login,
21247         p_request_id,
21248         p_program_appl_id,
21249         p_program_id,
21250         p_prog_updt_date
21251         );
21252         if lbji_debug = 'Y' then
21253         fnd_file.put_line(fnd_file.log, 'Inserted '||SQL%ROWCOUNT||' rows into wlbj');
21254         end if;
21255     END IF;--Check on g_skip_wlbj
21256    g_skip_wlbj := 0; --OPTII-PERF
21257 l_stmt_num := 140;
21258     -- insert into WE
21259    /* Added for bug 6861458.*/
21260    begin
21261     insert into wip_entities (
21262         wip_entity_id,
21263         organization_id,
21264         last_update_date,
21265         last_updated_by,
21266         creation_date,
21267         created_by,
21268         last_update_login,
21269         request_id,
21270         program_application_id,
21271         program_id,
21272         program_update_date,
21273         wip_entity_name,
21274         entity_type,
21275         description,
21276         primary_item_id,
21277         gen_object_id
21278     ) values (
21279         p_wip_entity_id,
21280         p_org_id,
21281         p_last_updt_date,
21282         p_last_updt_by,
21283         p_creation_date,
21284         p_created_by,
21285         p_last_updt_login,
21286         p_request_id,
21287         p_program_appl_id,
21288         p_program_id,
21289         p_prog_updt_date,
21290         p_job_name,
21291         5,
21292         p_description,
21293         p_item,
21294         mtl_gen_object_id_s.nextval
21295     );
21296    exception
21297    when others then
21298        fnd_message.set_name('WSM', 'WSM_UNIQUE_LOT_CREATE');
21299        p_error_code := -1;
21300        p_error_msg := fnd_message.get;
21301        raise e_build_job;
21302    end;
21303     if lbji_debug = 'Y' then
21304         fnd_file.put_line(fnd_file.log, 'Inserted '||SQL%ROWCOUNT||' rows into we');
21305     end if;
21306 
21307 
21308 l_stmt_num := 150;
21309     -- call update_card_supply_status to set the status of the kanban card to InProcess,
21310     -- if a card reference exists
21311     if ( p_kanban_card_id is not null ) then
21312         inv_kanban_pvt.update_card_supply_status(
21313                 x_return_status      => l_return_status,
21314                 p_kanban_card_id     => p_kanban_card_id,
21315                 p_supply_status      => inv_kanban_pvt.g_supply_status_InProcess,
21316                 p_document_type      => inv_kanban_pvt.G_Doc_type_lot_job,
21317                 p_document_header_id => p_wip_entity_id,
21318                 p_Document_detail_Id => null,
21319                 p_replenish_quantity => p_start_quantity);
21320 
21321         if ( l_return_status <> fnd_api.g_ret_sts_success ) then
21322             select meaning
21323             into   l_translated_meaning
21324             from   mfg_lookups
21325             where  lookup_type = 'MTL_KANBAN_SUPPLY_STATUS'
21326             and    lookup_code = 5
21327             and    upper(enabled_flag) = 'Y';
21328 
21329             fnd_message.set_name('WSM', 'WSM_KNBN_CARD_STS_FAIL');
21330             fnd_message.set_token('STATUS',l_translated_meaning);
21331             p_error_code := -1;
21332             p_error_msg := fnd_message.get;
21333             raise e_build_job;
21334         end if;
21335     end if;
21336 
21337 l_stmt_num := 160;
21338     if p_status_type = WIP_CONSTANTS.RELEASED then
21339         insert_into_period_balances (
21340                 p_wip_entity_id   => p_wip_entity_id,
21341                 p_organization_id => p_org_id,
21342                 p_class_code      => p_class_code,
21343                 p_release_date    => p_date_released,
21344                 p_error_code      => p_error_code,
21345                 p_err_msg         => p_error_msg );
21346         if p_error_code <> 0 then
21347             raise e_build_job;
21348         end if;
21349     end if;
21350 
21351     -- bug 3311985: osp code moved to build_job_detail_info
21352 
21353 
21354 --
21355 -- Begin R12Dev: for secondary quantity
21356 --
21357 l_stmt_num := 170;
21358     if(p_wjsq_uom_code.COUNT = 0) then -- No secondary quantities are passed
21359         insert INTO WSM_JOB_SECONDARY_QUANTITIES (
21360                WIP_ENTITY_ID,
21361                ORGANIZATION_ID,
21362                UOM_CODE,
21363                START_QUANTITY,
21364                CURRENT_QUANTITY,
21365                CURRENTLY_ACTIVE,
21366                LAST_UPDATE_DATE,
21367                LAST_UPDATED_BY,
21368                LAST_UPDATE_LOGIN,
21369                CREATION_DATE,
21370                CREATED_BY
21371         )
21372         select
21373                p_wip_entity_id,
21374                p_org_id,
21375                wsu.UOM_CODE,
21376                null,
21377                null,
21378                1, -- CURRENTLY_ACTIVE = Yes
21379                p_last_updt_date,
21380                p_last_updt_by,
21381                p_last_updt_login,
21382                p_creation_date,
21383                p_created_by
21384         from   WSM_SECONDARY_UOMS wsu
21385         where  INVENTORY_ITEM_ID = p_item
21386         and    ORGANIZATION_ID   = p_org_id;
21387     else
21388         forall i in p_wjsq_uom_code.first..p_wjsq_uom_code.last
21389             insert into WSM_JOB_SECONDARY_QUANTITIES
21390             (
21391                WIP_ENTITY_ID,
21392                ORGANIZATION_ID,
21393                UOM_CODE,
21394                START_QUANTITY,
21395                CURRENT_QUANTITY,
21396                CURRENTLY_ACTIVE,
21397                LAST_UPDATE_DATE,
21398                LAST_UPDATED_BY,
21399                LAST_UPDATE_LOGIN,
21400                CREATION_DATE,
21401                CREATED_BY
21402             ) values (
21403                p_wip_entity_id,
21404                p_org_id,
21405                p_wjsq_uom_code(i),
21406                p_wjsq_start_quantity(i),
21407                null,
21408                1, -- CURRENTLY_ACTIVE = Yes
21409                p_last_updt_date,
21410                p_last_updt_by,
21411                p_last_updt_login,
21412                p_creation_date,
21413                p_created_by
21414             );
21415     end if;
21416 --
21417 -- End R12Dev: for secondary quantity
21418 --
21419 
21420 EXCEPTION
21421 
21422     when e_build_job then
21423         -- p_error_code and p_error_msg were set
21424         fnd_file.put_line(fnd_file.log,substr('build_job_header_info: (#'||l_stmt_num||') '
21425                           ||p_error_msg, 1, 2000));
21426 
21427 
21428     when others then
21429         p_error_code := SQLCODE;
21430         p_error_msg := substr('build_job_header_info: (#'||l_stmt_num||') '
21431                               ||SQLERRM, 1, 2000);
21432         fnd_file.put_line(fnd_file.log, substr(p_error_msg, 1, 2000));
21433 
21434 END build_job_header_info;
21435 
21436 
21437 END WSM_LBJ_INTERFACE_PVT;