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.44.12020000.5 2013/02/22 09:12:49 kswarna 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 ----The below defined plsql tables are used for updating the DFF attributes.
159 
160 type t_attribute_category           is table of VARCHAR2(30)  index by binary_integer;
161 type t_attribute                    is table of VARCHAR2(150) index by binary_integer;
162 
163 -- ==============================================================================================
164 -- instantiating the tables used to bulk bind data from wlji to the PL/SQL tables.
165 -- ==============================================================================================
166 v_wlji_err_code                 t_wlji_err_code                 := t_wlji_err_code();
167 v_wlji_err_msg                  t_wlji_err_msg                  := t_wlji_err_msg();
168 v_wlji_last_updt_date           t_wlji_last_updt_date           := t_wlji_last_updt_date();
169 v_wlji_request_id               t_wlji_request_id               := t_wlji_request_id();
170 v_wlji_program_id               t_wlji_program_id               := t_wlji_program_id();
171 v_wlji_program_appl_id          t_wlji_program_appl_id          := t_wlji_program_appl_id();
172 v_wlji_last_updt_by             t_wlji_last_updt_by             := t_wlji_last_updt_by();
173 v_wlji_creation_date            t_wlji_creation_date            := t_wlji_creation_date();
174 v_wlji_created_by               t_wlji_created_by               := t_wlji_created_by();
175 v_wlji_last_updt_login          t_wlji_last_updt_login          := t_wlji_last_updt_login();
176 v_wlji_prog_updt_date           t_wlji_prog_updt_date           := t_wlji_prog_updt_date();
177 v_wlji_last_updt_by_name        t_wlji_last_updt_by_name        := t_wlji_last_updt_by_name();
178 v_wlji_created_by_name          t_wlji_created_by_name          := t_wlji_created_by_name();
179 v_wlji_org_id                   t_wlji_org_id                   := t_wlji_org_id();
180 v_wlji_org_code                 t_wlji_org_code                 := t_wlji_org_code();  -- Not used, should
181 v_wlji_wip_entity_id            t_wlji_wip_entity_id            := t_wlji_wip_entity_id();
182 v_wlji_job_name                 t_wlji_job_name                 := t_wlji_job_name();
183 v_wlji_lot_number               t_wlji_lot_number               := t_wlji_lot_number();
184 v_wlji_item_id                  t_wlji_item_id                  := t_wlji_item_id();
185 v_wlji_header_id                t_wlji_header_id                := t_wlji_header_id();
186 v_wlji_process_status           t_wlji_process_status           := t_wlji_process_status();
187 v_wlji_rtg_ref_id               t_wlji_routing_reference_id     := t_wlji_routing_reference_id();
188 v_wlji_compl_subinv             t_wlji_completion_subinventory  := t_wlji_completion_subinventory();
189 v_wlji_compl_loc_id             t_wlji_completion_locator_id    := t_wlji_completion_locator_id();
190 v_wlji_mode_flag                t_wlji_mode_flag                := t_wlji_mode_flag();
191 v_wlji_group_id                 t_wlji_group_id                 := t_wlji_group_id();
192 v_wlji_load_type                t_wlji_load_type                := t_wlji_load_type();
193 v_wlji_status_type              t_wlji_status_type              := t_wlji_status_type();
194 v_wlji_old_status_type          t_wlji_old_status_type          := t_wlji_old_status_type();  -- Not used
195 v_wlji_old_completion_date      t_wlji_old_completion_date      := t_wlji_old_completion_date();  -- Not used
196 v_wlji_bom_ref_id               t_wlji_bom_reference_id         := t_wlji_bom_reference_id();
197 v_wlji_bom_rev_date             t_wlji_bom_revision_date        := t_wlji_bom_revision_date();
198 v_wlji_rtg_rev_date             t_wlji_routing_revision_date    := t_wlji_routing_revision_date();
199 v_wlji_wip_supply_type          t_wlji_wip_supply_type          := t_wlji_wip_supply_type();
200 v_wlji_class_code               t_wlji_class_code               := t_wlji_class_code();
201 v_wlji_description              t_wlji_description              := t_wlji_description();
202 v_wlji_firm_planned_flag        t_wlji_firm_planned_flag        := t_wlji_firm_planned_flag();
203 v_wlji_alt_rtg                  t_wlji_alt_routing_designator   := t_wlji_alt_routing_designator();
204 v_wlji_alt_bom                  t_wlji_alt_bom_designator       := t_wlji_alt_bom_designator();
205 v_wlji_demand_class             t_wlji_demand_class             := t_wlji_demand_class();
206 v_wlji_start_quantity           t_wlji_start_quantity           := t_wlji_start_quantity();
207 v_wlji_old_start_quantity       t_wlji_old_start_quantity       := t_wlji_old_start_quantity();  -- Not used
208 v_wlji_error                    t_wlji_error                    := t_wlji_error();  -- Not used
209 v_wlji_process_phase            t_wlji_process_phase            := t_wlji_process_phase();  -- Not used
210 v_wlji_fusd                     t_wlji_fusd                     := t_wlji_fusd();
211 v_wlji_fucd                     t_wlji_fucd                     := t_wlji_fucd();  -- Not used
212 v_wlji_lusd                     t_wlji_lusd                     := t_wlji_lusd();  -- Not used
213 v_wlji_lucd                     t_wlji_lucd                     := t_wlji_lucd();
214 v_wlji_scheduling_method        t_wlji_scheduling_method        := t_wlji_scheduling_method();
215 v_wlji_rtg_revision             t_wlji_routing_revision         := t_wlji_routing_revision();
216 v_wlji_bom_revision             t_wlji_bom_revision             := t_wlji_bom_revision();
217 v_wlji_schedule_group_id        t_wlji_schedule_group_id        := t_wlji_schedule_group_id();
218 v_wlji_schedule_group_name      t_wlji_schedule_group_name      := t_wlji_schedule_group_name();
219 v_wlji_build_sequence           t_wlji_build_sequence           := t_wlji_build_sequence();
220 v_wlji_net_quantity             t_wlji_net_quantity             := t_wlji_net_quantity();
221 v_wlji_allow_explosion          t_wlji_allow_explosion          := t_wlji_allow_explosion();
222 v_wlji_interface_id             t_wlji_interface_id             := t_wlji_interface_id();            -- Not used
223 v_wlji_coproducts_supply        t_wlji_coproducts_supply        := t_wlji_coproducts_supply();
224 v_wlji_job_type                 t_wlji_job_type                 := t_wlji_job_type();
225 v_wlji_source_code              t_wlji_source_code              := t_wlji_source_code();
226 v_wlji_src_line_id              t_wlji_source_line_id           := t_wlji_source_line_id();
227 v_wlji_process_type             t_wlji_process_type             := t_wlji_process_type();
228 v_wlji_processing_work_days     t_wlji_processing_work_days     := t_wlji_processing_work_days(); -- Not used
229 v_wlji_daily_production_rate    t_wlji_daily_production_rate    := t_wlji_daily_production_rate();  -- Not used
230 v_wlji_line_id                  t_wlji_line_id                  := t_wlji_line_id();
231 v_wlji_lot_control_code         t_wlji_lot_control_code         := t_wlji_lot_control_code();  -- Not used
232 v_wlji_repetitive_schedule_id   t_wlji_repetitive_schedule_id   := t_wlji_repetitive_schedule_id();  -- Not used
233 v_wlji_parent_group_id          t_wlji_parent_group_id          := t_wlji_parent_group_id();  -- Not used
234 v_wlji_attribute_category       t_wlji_attribute_category       := t_wlji_attribute_category();
235 v_wlji_attribute1               t_wlji_attribute1               := t_wlji_attribute1();
236 v_wlji_attribute2               t_wlji_attribute2               := t_wlji_attribute2();
237 v_wlji_attribute3               t_wlji_attribute3               := t_wlji_attribute3();
238 v_wlji_attribute4               t_wlji_attribute4               := t_wlji_attribute4();
239 v_wlji_attribute5               t_wlji_attribute5               := t_wlji_attribute5();
240 v_wlji_attribute6               t_wlji_attribute6               := t_wlji_attribute6();
241 v_wlji_attribute7               t_wlji_attribute7               := t_wlji_attribute7();
242 v_wlji_attribute8               t_wlji_attribute8               := t_wlji_attribute8();
243 v_wlji_attribute9               t_wlji_attribute9               := t_wlji_attribute9();
244 v_wlji_attribute10              t_wlji_attribute10              := t_wlji_attribute10();
245 v_wlji_attribute11              t_wlji_attribute11              := t_wlji_attribute11();
246 v_wlji_attribute12              t_wlji_attribute12              := t_wlji_attribute12();
247 v_wlji_attribute13              t_wlji_attribute13              := t_wlji_attribute13();
248 v_wlji_attribute14              t_wlji_attribute14              := t_wlji_attribute14();
249 v_wlji_attribute15              t_wlji_attribute15              := t_wlji_attribute15();
250 v_wlji_line_code                t_wlji_line_code                := t_wlji_line_code();  -- Not used
251 v_wlji_primary_item_segs        t_wlji_primary_item_segs        := t_wlji_primary_item_segs();  -- Not used
252 v_wlji_bom_ref_segs             t_wlji_bom_ref_segs             := t_wlji_bom_ref_segs();  -- Not used
253 v_wlji_rtg_ref_segs             t_wlji_rtg_ref_segs             := t_wlji_rtg_ref_segs();  -- Not used
254 v_wlji_compl_loc_segs           t_wlji_compl_loc_segs           := t_wlji_compl_loc_segs();  -- Not used
255 v_wlji_desc_flx_segs            t_wlji_desc_flx_segs            := t_wlji_desc_flx_segs();   -- Not used
256 v_wlji_project_id               t_wlji_project_id               := t_wlji_project_id();  -- No Validation
257 v_wlji_project_name             t_wlji_project_name             := t_wlji_project_name();  -- Not used
258 v_wlji_project_number           t_wlji_project_number           := t_wlji_project_number();  -- Not used
259 v_wlji_project_costed           t_wlji_project_costed           := t_wlji_project_costed();  -- Not used
260 v_wlji_task_id                  t_wlji_task_id                  := t_wlji_task_id();  -- No validation
261 v_wlji_task_name                t_wlji_task_name                := t_wlji_task_name();  -- Not used
262 v_wlji_task_number              t_wlji_task_number              := t_wlji_task_number();  -- Not used
263 v_wlji_end_item_unit_number     t_wlji_end_item_unit_number     := t_wlji_end_item_unit_number();  -- No validation
264 v_wlji_overcompl_tol_type       t_wlji_overcompl_tol_type       := t_wlji_overcompl_tol_type();  -- No validation
265 v_wlji_overcompl_tol_value      t_wlji_overcompl_tol_value      := t_wlji_overcompl_tol_value();  -- No validation
266 v_wlji_kanban_card_id           t_wlji_kanban_card_id           := t_wlji_kanban_card_id();
267 v_wlji_priority                 t_wlji_priority                 := t_wlji_priority();
268 v_wlji_due_date                 t_wlji_due_date                 := t_wlji_due_date();
269 v_wlji_delivery_id              t_wlji_delivery_id              := t_wlji_delivery_id();  -- No validation
270 v_wlji_date_released            t_wlji_date_released            := t_wlji_date_released();
271 v_wlji_transaction_date         t_wlji_transaction_date         := t_wlji_transaction_date();
272 v_wlji_num_of_children          t_wlji_num_of_children          := t_wlji_num_of_children();
273 --v_wlji_inf_schedule_flag      t_wlji_inf_schedule_flag        := t_wlji_inf_schedule_flag();
274 v_new_job               NUMBER := NULL;
275 v_phantom_exists        NUMBER := NULL; --OPTII-PERF: 1-Phantom req exits,2-Phantom requirement does not exists
276 v_insert_wip            NUMBER := NULL; --OPTII-PERF  1-Insert wip data in create_jobcopies procedure
277                                 --            2-Phantom requirement does not exists
278 -- ==============================================================================================
279 -- corresponding table type and table declarations for wsm_starting_lots_interface
280 -- ==============================================================================================
281 type t_wsli_header_id           is table of wsm_starting_lots_interface.header_id%type;
282 type t_wsli_lot_number          is table of wsm_starting_lots_interface.lot_number%type;
283 type t_wsli_inventory_item_id   is table of wsm_starting_lots_interface.inventory_item_id%type;
284 type t_wsli_organization_id     is table of wsm_starting_lots_interface.organization_id%type;
285 type t_wsli_quantity            is table of wsm_starting_lots_interface.quantity%type;
286 type t_wsli_subinventory_code   is table of wsm_starting_lots_interface.subinventory_code%type;
287 type t_wsli_locator_id          is table of wsm_starting_lots_interface.locator_id%type;
288 type t_wsli_revision            is table of wsm_starting_lots_interface.revision%type;
289 type t_wsli_last_updated_by     is table of wsm_starting_lots_interface.last_updated_by%type;
290 type t_wsli_created_by          is table of wsm_starting_lots_interface.created_by%type;
291 type t_wsli_primary_uom_code    is table of mtl_system_items.primary_uom_code%type;
292 type t_wsli_comp_issue_qty      is table of wsm_starting_lots_interface.component_issue_quantity%type;
293 
294 v_wsli_hdr_id                   t_wsli_header_id                := t_wsli_header_id();
295 v_wsli_lot_number               t_wsli_lot_number               := t_wsli_lot_number();
296 v_wsli_inventory_item_id        t_wsli_inventory_item_id        := t_wsli_inventory_item_id();
297 v_wsli_organization_id          t_wsli_organization_id          := t_wsli_organization_id();
298 v_wsli_quantity                 t_wsli_quantity                 := t_wsli_quantity();
299 v_wsli_subinventory_code        t_wsli_subinventory_code        := t_wsli_subinventory_code();
300 v_wsli_locator_id               t_wsli_locator_id               := t_wsli_locator_id();
301 v_wsli_revision                 t_wsli_revision                 := t_wsli_revision();
302 v_wsli_last_updated_by          t_wsli_last_updated_by          := t_wsli_last_updated_by();
303 v_wsli_created_by               t_wsli_created_by               := t_wsli_created_by();
304 v_wsli_primary_uom_code         t_wsli_primary_uom_code         := t_wsli_primary_uom_code();
305 v_wsli_comp_issue_qty           t_wsli_comp_issue_qty           := t_wsli_comp_issue_qty();
306 
307 -- ==============================================================================================
308 -- creating an index by table that'll store the wsli values with header_id as the index for easy
309 -- validation
310 -- ==============================================================================================
311 type rec_wsli IS record (
312         lot_number              wsm_starting_lots_interface.lot_number%type,
313         inventory_item_id       wsm_starting_lots_interface.inventory_item_id%type,
314         organization_id         wsm_starting_lots_interface.organization_id%type,
315         quantity                wsm_starting_lots_interface.quantity%type,
316         subinventory_code       wsm_starting_lots_interface.subinventory_code%type,
317         locator_id              wsm_starting_lots_interface.locator_id%type,
318         revision                wsm_starting_lots_interface.revision%type,
319         last_updated_by         wsm_starting_lots_interface.last_updated_by%type,
320         created_by              wsm_starting_lots_interface.created_by%type,
321         primary_uom_code        mtl_system_items.primary_uom_code%type,
322         comp_issue_quantity     wsm_starting_lots_interface.component_issue_quantity%type
323 );
324 
325 v_rec_wsli                      rec_wsli;   -- Not used
326 
327 type t_wsli                     is table of rec_wsli index by binary_integer;
328 v_wsli                          t_wsli;
329 
330 -- ==============================================================================================
331 -- nested table types used to bulk bind data from wljdi to the PL/SQL tables.
332 -- ==============================================================================================
333 type t_wljdi_record_id              is table of wsm_lot_job_dtl_interface.record_id                 %type;
334 type t_wljdi_parent_header_id       is table of wsm_lot_job_dtl_interface.parent_header_id          %type;
335 type t_wljdi_group_id               is table of wsm_lot_job_dtl_interface.group_id                  %type;
336 type t_wljdi_org_id                 is table of wsm_lot_job_dtl_interface.organization_id           %type;
337 type t_wljdi_org_code               is table of wsm_lot_job_dtl_interface.organization_code         %type;
338 type t_wljdi_wip_entity_id          is table of wsm_lot_job_dtl_interface.wip_entity_id             %type;
339 type t_wljdi_job_name               is table of wsm_lot_job_dtl_interface.job_name                  %type;
340 type t_wljdi_err_code               is table of wsm_lot_job_dtl_interface.error_code                %type;
341 type t_wljdi_err_msg                is table of wsm_lot_job_dtl_interface.error_msg                 %type;
342 type t_wljdi_request_id             is table of wsm_lot_job_dtl_interface.request_id                %type;
343 type t_wljdi_program_id             is table of wsm_lot_job_dtl_interface.program_id                %type;
344 type t_wljdi_program_appl_id        is table of wsm_lot_job_dtl_interface.program_application_id    %type;
345 type t_wljdi_last_updt_by           is table of wsm_lot_job_dtl_interface.last_updated_by           %type;
346 type t_wljdi_last_updt_login        is table of wsm_lot_job_dtl_interface.last_update_login         %type;
347 type t_wljdi_last_updt_date         is table of wsm_lot_job_dtl_interface.last_update_date          %type;
348 type t_wljdi_prog_updt_date         is table of wsm_lot_job_dtl_interface.program_update_date       %type;
349 type t_wljdi_process_status         is table of wsm_lot_job_dtl_interface.process_status            %type;
350 
351 
352 -- BA: bug 3313463/3311695 8i compatibility
353 type t_wljdi_load_type              is table of wsm_lot_job_dtl_interface.load_type                 %type;
354 type t_wljdi_substitution_type      is table of wsm_lot_job_dtl_interface.substitution_type         %type;
355 type t_wljdi_job_op_seq_num         is table of wsm_lot_job_dtl_interface.job_op_seq_num            %type;
356 type t_wljdi_routing_op_seq_num     is table of wsm_lot_job_dtl_interface.routing_op_seq_num        %type;
357 type t_wljdi_next_rtg_op_seq_num    is table of wsm_lot_job_dtl_interface.next_routing_op_seq_num   %type;
358 
359 type t_wljdi_inventory_item_id_new  is table of wsm_lot_job_dtl_interface.inventory_item_id_new     %type;
360 type t_wljdi_inventory_item_new     is table of wsm_lot_job_dtl_interface.inventory_item_new        %type;
361 type t_wljdi_primary_item           is table of wsm_lot_job_dtl_interface.primary_item              %type;
362 type t_wljdi_primary_item_id        is table of wsm_lot_job_dtl_interface.primary_item_id           %type;
363 type t_wljdi_src_phantom_item       is table of wsm_lot_job_dtl_interface.src_phantom_item          %type;
364 type t_wljdi_src_phantom_item_id    is table of wsm_lot_job_dtl_interface.src_phantom_item_id       %type;
365 type t_wljdi_component_sequence_id  is table of wsm_lot_job_dtl_interface.component_sequence_id     %type;
366 type t_wljdi_date_required          is table of wsm_lot_job_dtl_interface.date_required             %type;
367 type t_wljdi_scheduled_quantity     is table of wsm_lot_job_dtl_interface.scheduled_quantity        %type;
368 type t_wljdi_required_quantity      is table of wsm_lot_job_dtl_interface.required_quantity         %type;
369 type t_wljdi_batch_id               is table of wsm_lot_job_dtl_interface.batch_id                  %type;
370 type t_wljdi_assigned_units         is table of wsm_lot_job_dtl_interface.assigned_units            %type;
371 type t_wljdi_instance_id_new        is table of wsm_lot_job_dtl_interface.instance_id_new           %type;
372 type t_wljdi_op_completion_date     is table of wsm_lot_job_dtl_interface.operation_completion_date %type;
373 type t_wljdi_op_start_date          is table of wsm_lot_job_dtl_interface.operation_start_date      %type;
374 type t_wljdi_op_yield               is table of wsm_lot_job_dtl_interface.operation_yield           %type;
375 type t_wljdi_resource_code_new      is table of wsm_lot_job_dtl_interface.resource_code_new         %type;
376 type t_wljdi_resource_id_new        is table of wsm_lot_job_dtl_interface.resource_id_new           %type;
377 type t_wljdi_resource_seq_num       is table of wsm_lot_job_dtl_interface.resource_seq_num          %type;
378 type t_wljdi_substitute_group_num   is table of wsm_lot_job_dtl_interface.substitute_group_num      %type;
379 type t_wljdi_replacement_group_num  is table of wsm_lot_job_dtl_interface.replacement_group_num     %type;
380 type t_wljdi_serial_number_new      is table of wsm_lot_job_dtl_interface.serial_number_new         %type;
381 type t_wljdi_start_date             is table of wsm_lot_job_dtl_interface.start_date                %type;
382 type t_wljdi_completion_date        is table of wsm_lot_job_dtl_interface.completion_date           %type;
383 -- EA: bug 3313463/3311695 8i compatibility
384 
385 /* ST : Detailed scheduling : start */
386 type t_wljdi_resource_hours         is table of wsm_lot_job_dtl_interface.resource_hours        %type;
387 type t_wljdi_firm_type              is table of wsm_lot_job_dtl_interface.firm_type             %type;
388 type t_wljdi_setup_id               is table of wsm_lot_job_dtl_interface.setup_id              %type;
389 type t_wljdi_setup_code               is table of wsm_lot_job_dtl_interface.setup_code              %type;
390 type t_wljdi_grp_seq_id             is table of wsm_lot_job_dtl_interface.group_sequence_id     %type;
391 type t_wljdi_grp_seq_num            is table of wsm_lot_job_dtl_interface.group_sequence_num    %type;
392 type t_wljdi_max_assg_units         is table of wsm_lot_job_dtl_interface.max_assigned_units    %type;
393 type t_wljdi_parent_seq_num         is table of wsm_lot_job_dtl_interface.parent_resource_seq_num   %type;
394 
395 /* Start : fields added to add resource */
396 type t_wljdi_department_id          is table of wsm_lot_job_dtl_interface.department_id         %type;
397 type t_wljdi_department_code        is table of wsm_lot_job_dtl_interface.department_code       %type;
398 type t_wljdi_activity_id            is table of wsm_lot_job_dtl_interface.activity_id           %type;
399 type t_wljdi_standard_rate_flag     is table of wsm_lot_job_dtl_interface.standard_rate_flag    %type;
400 type t_wljdi_usage_rate_or_amt      is table of wsm_lot_job_dtl_interface.usage_rate_or_amount  %type;
401 
402 type t_wljdi_basis_type         is table of wsm_lot_job_dtl_interface.basis_type                %type;
403 type t_wljdi_uom_code           is table of wsm_lot_job_dtl_interface.uom_code                  %type;
404 type t_wljdi_scheduled_flag     is table of wsm_lot_job_dtl_interface.scheduled_flag            %type;
405 
406 type t_wljdi_res_autocharge_type    is table of wsm_lot_job_dtl_interface.autocharge_type       %type;
407 type t_wljdi_schedule_seq_num       is table of wsm_lot_job_dtl_interface.schedule_seq_num      %type;
408 
409 type t_wljdi_attribute_category    is table of wsm_lot_job_dtl_interface.attribute_category     %type;
410 type t_wljdi_attribute1        is table of wsm_lot_job_dtl_interface.attribute1                 %type;
411 type t_wljdi_attribute2        is table of wsm_lot_job_dtl_interface.attribute2                 %type;
412 type t_wljdi_attribute3        is table of wsm_lot_job_dtl_interface.attribute3                 %type;
413 type t_wljdi_attribute4        is table of wsm_lot_job_dtl_interface.attribute4                 %type;
414 type t_wljdi_attribute5        is table of wsm_lot_job_dtl_interface.attribute5                 %type;
415 type t_wljdi_attribute6        is table of wsm_lot_job_dtl_interface.attribute6                 %type;
416 type t_wljdi_attribute7        is table of wsm_lot_job_dtl_interface.attribute7                 %type;
417 type t_wljdi_attribute8        is table of wsm_lot_job_dtl_interface.attribute8                 %type;
418 type t_wljdi_attribute9        is table of wsm_lot_job_dtl_interface.attribute9                 %type;
419 type t_wljdi_attribute10       is table of wsm_lot_job_dtl_interface.attribute10                %type;
420 type t_wljdi_attribute11       is table of wsm_lot_job_dtl_interface.attribute11                %type;
421 type t_wljdi_attribute12       is table of wsm_lot_job_dtl_interface.attribute12                %type;
422 type t_wljdi_attribute13       is table of wsm_lot_job_dtl_interface.attribute13                %type;
423 type t_wljdi_attribute14       is table of wsm_lot_job_dtl_interface.attribute14                %type;
424 type t_wljdi_attribute15       is table of wsm_lot_job_dtl_interface.attribute15                %type;
425 
426 -- Added For LBJ Interface Alcoa Enhancement
427 type t_wljdi_recommended       is table of wsm_lot_job_dtl_interface.recommended                %type;
428 type t_wljdi_std_op_code       is table of wsm_lot_job_dtl_interface.standard_operation_code    %type;
429 type t_wljdi_std_op_id         is table of wsm_lot_job_dtl_interface.standard_operation_id      %type;
430 type t_wljdi_op_yield_enable   is table of wsm_lot_job_dtl_interface.operation_yield_enabled    %type;
431 type t_wljdi_min_trf_qty       is table of wsm_lot_job_dtl_interface.minimum_transfer_quantity  %type;
432 type t_wljdi_backflush_flag    is table of wsm_lot_job_dtl_interface.backflush_flag             %type;
433 type t_wljdi_count_point_type  is table of wsm_lot_job_dtl_interface.count_point_type           %type;
434 type t_wljdi_description       is table of wsm_lot_job_dtl_interface.description                %type;
435 type t_wljdi_inc_in_rollup     is table of wsm_lot_job_dtl_interface.include_in_rollup          %type;
436 type t_wljdi_option_dep_flag   is table of wsm_lot_job_dtl_interface.option_dependent_flag      %type;
437 type t_wljdi_mrp_net_flag      is table of wsm_lot_job_dtl_interface.mrp_net_flag               %type;
438 type t_wljdi_auto_request_material is table of wsm_lot_job_dtl_interface.auto_request_material  %type;
439 type t_wljdi_comments          is table of wsm_lot_job_dtl_interface.comments                   %type;
440 type t_wljdi_inventory_item_id_old  is table of wsm_lot_job_dtl_interface.inventory_item_id_old     %type;
441 type t_wljdi_inventory_item_old     is table of wsm_lot_job_dtl_interface.inventory_item_old        %type;
442 type t_wljdi_wip_supply_type        is table of wsm_lot_job_dtl_interface.wip_supply_type           %type;
443 type t_wljdi_quantity_per_assembly  is table of wsm_lot_job_dtl_interface.quantity_per_assembly     %type;
444 type t_wljdi_supply_subinventory    is table of wsm_lot_job_dtl_interface.supply_subinventory       %type;
445 type t_wljdi_supply_locator_id      is table of wsm_lot_job_dtl_interface.supply_locator_id         %type;
446 type t_wljdi_applied_resource_units is table of wsm_lot_job_dtl_interface.applied_resource_units    %type;
447 type t_desc                         is table of bom_standard_operations.operation_description%type index by binary_integer;
448 type t_op_code                      is table of bom_standard_operations.operation_code%type index by binary_integer;
449 
450 /*Added below two types for bugfix:7248992 */
451 type t_wljdi_cum_yield             is table of wsm_lot_job_dtl_interface.cumulative_yield          %type;
452 type t_wljdi_rev_cum_yield         is table of wsm_lot_job_dtl_interface.reverse_cumulative_yield  %type;
453 /* end */
454 
455 /* ST : Detailed scheduling : end */
456 
457 -- ==============================================================================================
458 -- instantiating the tables used to bulk bind data from wljdi to the PL/SQL tables.
459 -- ==============================================================================================
460 v_wljdi_record_id               t_wljdi_record_id               := t_wljdi_record_id();
461 v_wljdi_parent_header_id        t_wljdi_parent_header_id        := t_wljdi_parent_header_id();
462 v_wljdi_group_id                t_wljdi_group_id                := t_wljdi_group_id();
463 v_wljdi_org_id                  t_wljdi_org_id                  := t_wljdi_org_id();
464 v_wljdi_org_code                t_wljdi_org_code                := t_wljdi_org_code();
465 v_wljdi_wip_entity_id           t_wljdi_wip_entity_id           := t_wljdi_wip_entity_id();
466 v_wljdi_job_name                t_wljdi_job_name                := t_wljdi_job_name();
467 v_wljdi_err_code                t_wljdi_err_code                := t_wljdi_err_code();
468 v_wljdi_err_msg                 t_wljdi_err_msg                 := t_wljdi_err_msg();
469 v_wljdi_request_id              t_wljdi_request_id              := t_wljdi_request_id();
470 v_wljdi_program_id              t_wljdi_program_id              := t_wljdi_program_id();
471 v_wljdi_program_appl_id         t_wljdi_program_appl_id         := t_wljdi_program_appl_id();
472 v_wljdi_last_updt_login         t_wljdi_last_updt_login         := t_wljdi_last_updt_login();
473 v_wljdi_last_updt_by            t_wljdi_last_updt_by            := t_wljdi_last_updt_by();
474 v_wljdi_last_updt_date          t_wljdi_last_updt_date          := t_wljdi_last_updt_date();
475 v_wljdi_prog_updt_date          t_wljdi_prog_updt_date          := t_wljdi_prog_updt_date();
476 v_wljdi_process_status          t_wljdi_process_status          := t_wljdi_process_status();
477 
478 
479 -- BA: bug 3313463/3311695 8i compatibility
480 v_wljdi_load_type               t_wljdi_load_type              := t_wljdi_load_type();
481 v_wljdi_substitution_type       t_wljdi_substitution_type      := t_wljdi_substitution_type();
482 v_wljdi_job_op_seq_num          t_wljdi_job_op_seq_num         := t_wljdi_job_op_seq_num();
483 v_wljdi_routing_op_seq_num      t_wljdi_routing_op_seq_num     := t_wljdi_routing_op_seq_num();
484 v_wljdi_next_rtg_op_seq_num     t_wljdi_next_rtg_op_seq_num    := t_wljdi_next_rtg_op_seq_num();
485 
486 v_wljdi_inventory_item_id_new   t_wljdi_inventory_item_id_new  := t_wljdi_inventory_item_id_new();
487 v_wljdi_inventory_item_new      t_wljdi_inventory_item_new     := t_wljdi_inventory_item_new();
488 v_wljdi_primary_item            t_wljdi_primary_item           := t_wljdi_primary_item();
489 v_wljdi_primary_item_id         t_wljdi_primary_item_id        := t_wljdi_primary_item_id();
490 v_wljdi_src_phantom_item        t_wljdi_src_phantom_item       := t_wljdi_src_phantom_item();
491 v_wljdi_src_phantom_item_id     t_wljdi_src_phantom_item_id    := t_wljdi_src_phantom_item_id();
492 v_wljdi_component_sequence_id   t_wljdi_component_sequence_id  := t_wljdi_component_sequence_id();
493 v_wljdi_date_required           t_wljdi_date_required          := t_wljdi_date_required();
494 v_wljdi_scheduled_quantity      t_wljdi_scheduled_quantity     := t_wljdi_scheduled_quantity();
495 v_wljdi_required_quantity       t_wljdi_required_quantity      := t_wljdi_required_quantity();
496 v_wljdi_batch_id                t_wljdi_batch_id               := t_wljdi_batch_id();
497 v_wljdi_assigned_units          t_wljdi_assigned_units         := t_wljdi_assigned_units();
498 v_wljdi_instance_id_new         t_wljdi_instance_id_new        := t_wljdi_instance_id_new();
499 v_wljdi_op_completion_date      t_wljdi_op_completion_date     := t_wljdi_op_completion_date();
500 v_wljdi_op_start_date           t_wljdi_op_start_date          := t_wljdi_op_start_date();
501 v_wljdi_op_yield                t_wljdi_op_yield               := t_wljdi_op_yield();
502 v_wljdi_resource_code_new       t_wljdi_resource_code_new      := t_wljdi_resource_code_new();
503 v_wljdi_resource_id_new         t_wljdi_resource_id_new        := t_wljdi_resource_id_new();
504 v_wljdi_resource_seq_num        t_wljdi_resource_seq_num       := t_wljdi_resource_seq_num();
505 v_wljdi_substitute_group_num    t_wljdi_substitute_group_num   := t_wljdi_substitute_group_num();
506 v_wljdi_replacement_group_num   t_wljdi_replacement_group_num  := t_wljdi_replacement_group_num();
507 v_wljdi_serial_number_new       t_wljdi_serial_number_new      := t_wljdi_serial_number_new();
508 v_wljdi_start_date              t_wljdi_start_date             := t_wljdi_start_date();
509 v_wljdi_completion_date         t_wljdi_completion_date        := t_wljdi_completion_date();
510 -- EA: bug 3313463/3311695 8i compatibility
511 
512 /* ST :  Detailed scheduling : start */
513 v_wljdi_firm_type           t_wljdi_firm_type              := t_wljdi_firm_type();
514 v_wljdi_setup_id            t_wljdi_setup_id               := t_wljdi_setup_id();
515 v_wljdi_setup_code          t_wljdi_setup_code             := t_wljdi_setup_code();
516 v_wljdi_grp_seq_id          t_wljdi_grp_seq_id             := t_wljdi_grp_seq_id();
517 v_wljdi_grp_seq_num         t_wljdi_grp_seq_num            := t_wljdi_grp_seq_num();
518 v_wljdi_max_assg_units      t_wljdi_max_assg_units         := t_wljdi_max_assg_units();
519 v_wljdi_parent_seq_num      t_wljdi_parent_seq_num         := t_wljdi_parent_seq_num();
520 v_wljdi_resource_hours      t_wljdi_resource_hours         := t_wljdi_resource_hours();
521 
522 /* Start : changes for adding resource ... */
523 v_wljdi_department_id       t_wljdi_department_id           := t_wljdi_department_id();
524 v_wljdi_department_code     t_wljdi_department_code         := t_wljdi_department_code();
525 v_wljdi_activity_id         t_wljdi_activity_id             := t_wljdi_activity_id();
526 v_wljdi_standard_rate_flag  t_wljdi_standard_rate_flag      := t_wljdi_standard_rate_flag();
527 v_wljdi_usage_rate_or_amt   t_wljdi_usage_rate_or_amt       := t_wljdi_usage_rate_or_amt();
528 
529 v_wljdi_basis_type          t_wljdi_basis_type          := t_wljdi_basis_type();
530 v_wljdi_uom_code            t_wljdi_uom_code            := t_wljdi_uom_code();
531 v_wljdi_scheduled_flag      t_wljdi_scheduled_flag      := t_wljdi_scheduled_flag();
532 
533 v_wljdi_res_autocharge_type     t_wljdi_res_autocharge_type := t_wljdi_res_autocharge_type();
534 v_wljdi_schedule_seq_num        t_wljdi_schedule_seq_num    := t_wljdi_schedule_seq_num();
535 
536 v_wljdi_attribute_category  t_wljdi_attribute_category  := t_wljdi_attribute_category();
537 v_wljdi_attribute1          t_wljdi_attribute1          := t_wljdi_attribute1();
538 v_wljdi_attribute2          t_wljdi_attribute2          := t_wljdi_attribute2();
539 v_wljdi_attribute3          t_wljdi_attribute3          := t_wljdi_attribute3();
540 v_wljdi_attribute4          t_wljdi_attribute4          := t_wljdi_attribute4();
541 v_wljdi_attribute5          t_wljdi_attribute5          := t_wljdi_attribute5();
542 v_wljdi_attribute6          t_wljdi_attribute6          := t_wljdi_attribute6();
543 v_wljdi_attribute7          t_wljdi_attribute7          := t_wljdi_attribute7();
544 v_wljdi_attribute8          t_wljdi_attribute8          := t_wljdi_attribute8();
545 v_wljdi_attribute9          t_wljdi_attribute9          := t_wljdi_attribute9();
546 v_wljdi_attribute10         t_wljdi_attribute10         := t_wljdi_attribute10();
547 v_wljdi_attribute11         t_wljdi_attribute11         := t_wljdi_attribute11();
548 v_wljdi_attribute12         t_wljdi_attribute12         := t_wljdi_attribute12();
549 v_wljdi_attribute13         t_wljdi_attribute13         := t_wljdi_attribute13();
550 v_wljdi_attribute14         t_wljdi_attribute14         := t_wljdi_attribute14();
551 v_wljdi_attribute15         t_wljdi_attribute15         := t_wljdi_attribute15();
552 
553 -- Added For LBJ Interface Alcoa Enhancement
554 v_wljdi_recommended         t_wljdi_recommended         := t_wljdi_recommended();
555 v_wljdi_std_op_code         t_wljdi_std_op_code         := t_wljdi_std_op_code();
556 v_wljdi_std_op_id           t_wljdi_std_op_id           := t_wljdi_std_op_id();
557 v_wljdi_op_yield_enable     t_wljdi_op_yield_enable     := t_wljdi_op_yield_enable();
558 v_wljdi_min_trf_qty         t_wljdi_min_trf_qty         := t_wljdi_min_trf_qty();
559 v_wljdi_backflush_flag      t_wljdi_backflush_flag      := t_wljdi_backflush_flag();
560 v_wljdi_count_point_type    t_wljdi_count_point_type    := t_wljdi_count_point_type();
561 v_wljdi_description         t_wljdi_description         := t_wljdi_description();
562 v_wljdi_inc_in_rollup       t_wljdi_inc_in_rollup       := t_wljdi_inc_in_rollup();
563 v_wljdi_option_dep_flag     t_wljdi_option_dep_flag     := t_wljdi_option_dep_flag();
564 v_wljdi_inventory_item_id_old   t_wljdi_inventory_item_id_old  := t_wljdi_inventory_item_id_old();
565 v_wljdi_inventory_item_old      t_wljdi_inventory_item_old     := t_wljdi_inventory_item_old();
566 v_wljdi_wip_supply_type         t_wljdi_wip_supply_type        := t_wljdi_wip_supply_type();
567 v_wljdi_quantity_per_assembly   t_wljdi_quantity_per_assembly  := t_wljdi_quantity_per_assembly();
568 v_wljdi_supply_subinventory     t_wljdi_supply_subinventory    := t_wljdi_supply_subinventory();
569 v_wljdi_supply_locator_id       t_wljdi_supply_locator_id      := t_wljdi_supply_locator_id();
570 v_wljdi_mrp_net_flag            t_wljdi_mrp_net_flag           := t_wljdi_mrp_net_flag();
571 v_wljdi_auto_request_material   t_wljdi_auto_request_material  := t_wljdi_auto_request_material();
572 v_wljdi_comments                t_wljdi_comments               := t_wljdi_comments();
573 v_wljdi_applied_resource_units  t_wljdi_applied_resource_units  := t_wljdi_applied_resource_units();
574 
575   /*Added for bugfix:7248992 */
576 v_wljdi_cum_yield               t_wljdi_cum_yield              := t_wljdi_cum_yield();
577 v_wljdi_rev_cum_yield           t_wljdi_rev_cum_yield          := t_wljdi_rev_cum_yield();
578 
579 type t_res_recom is table of varchar2(5) index by binary_integer;
580 type t_res_code is table of bom_resources.resource_code%type index by binary_integer;
581 
582 /* end */
583 /* ST : Detailed scheduling : end */
584 
585 type t_job_name                 is table of VARCHAR2(240) index by binary_integer;
586 
587 g_v_job_name                    t_job_name;
588 g_v_wip_entity_id               t_number;
589 
590 g_v_res_id                      t_number;
591 g_v_item_id                     t_number;
592 
593 
594 -- ==============================================================================================
595 -- instantiating the tables used to bulk bind data from wljdi to the PL/SQL tables.
596 -- ==============================================================================================
597 
598 -- BD: bug 3313463/3311695 8i compatibility
599 --type t_wljdi                    is table of wsm_lot_job_dtl_interface%rowtype index by binary_integer;
600 --v_wljdi                         t_wljdi;
601 -- ED: bug 3313463/3311695 8i compatibility
602 
603 -- ==============================================================================================
604 -- Some datatype to validate wljdi records
605 -- ==============================================================================================
606 
607 type rec_links IS record (
608         from_op_seq_num         number,
609         to_op_seq_num           number
610 );
611 type t_links                    is table of rec_links index by binary_integer;
612 
613 type rec_op IS record (
614         job_op_seq_num          number,
615         rtg_op_seq_num          number,
616         scheduled_quantity      number,
617         yield                   number,
618         start_date              date,
619         compltion_date          date,
620 	--Added for bug Fix 7248992
621 	cumulative_yield         number,
622         reverse_cumulative_yield number
623 );
624 type t_ops                      is table of rec_op index by binary_integer;
625 
626 /* ST : Detailed Schedduling : Record type for validating a resource to be added ... */
627 type new_res_rec IS record
628 (
629     job_op_seq_num                  NUMBER,
630     rtg_op_seq_num                  NUMBER,
631     resource_id                     NUMBER,
632     resource_code                   VARCHAR2(10),
633     dept_id                         NUMBER,
634     dept_code                       VARCHAR2(10), /* Modified from NUMBER for Bug 5469701 */
635     res_seq_num                     NUMBER,
636     sub_grp_num                     NUMBER,
637     rpl_grp_num                     NUMBER,
638     activity_id                     NUMBER,
639     standard_rate_flag              NUMBER,
640     assigned_units                  NUMBER,
641     usage_rate_or_amount            NUMBER,
642     usage_rate_or_amount_inverse    NUMBER,
643     uom_code                        VARCHAR2(3),
644     basis_type                      NUMBER,
645     scheduled_flag                  NUMBER,
646     autocharge_type                 NUMBER,
647     schedule_seq_num                NUMBER,
648     setup_id                        NUMBER,
649     setup_code                      VARCHAR2(10),
650     start_date                      DATE,
651     firm_type                       NUMBER,
652     grp_seq_id                      NUMBER,
653     grp_seq_num                     NUMBER,
654     max_assg_units                  NUMBER,
655     parent_seq_num                  NUMBER,
656     batch_id                        NUMBER,
657     completion_date                 DATE
658 );
659 
660 type new_res_tbl is table of new_res_rec index by binary_integer;
661 
662 type rec_op_res IS record (
663         job_op_seq_num          number,
664         rtg_op_seq_num          number,
665         resource_id             number,
666         res_seq_num             number,
667         sub_grp_num             number,
668         rpl_grp_num             number,
669         start_date              date,
670         /* ST : Detailed Scheduling start */
671         firm_type               number,
672         setup_id                number,
673         setup_code              VARCHAR2(10),
674         grp_seq_id              number,
675         grp_seq_num             number,
676         max_assg_units          number,
677         parent_seq_num          number,
678         batch_id                number,
679         assigned_units          number,
680         /* ST : Detailed Scheduling end */
681         completion_date         date,
682 		-- Added For LBJ Interface Alcoa Enhancement
683         applied_resource_units  number ,
684         uom_code                VARCHAR2(3),
685         autocharge_type         NUMBER,
686         usage_rate_or_amount    NUMBER,
687         scheduled_flag          NUMBER,
688         basis_type              NUMBER,
689         activity_id             NUMBER,
690         schedule_seq_num        NUMBER,
691         reco_flag               VARCHAR2(1),
692         standard_rate           NUMBER,
693         resource_code           VARCHAR2(10)
694 );
695 type t_op_res                   is table of rec_op_res index by binary_integer;
696 
697 type rec_res_ins IS record (
698         job_op_seq_num          number,
699         rtg_op_seq_num          number,
700         res_seq_num             number,
701         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
702         -- parent_res_seq_num   number,
703         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
704         instance_id             number,
705         serial_number           varchar2(30),
706         start_date              date,
707         completion_date         date,
708         batch_id                number
709 );
710 type t_res_ins                  is table of rec_res_ins index by binary_integer;
711 
712 type rec_res_use IS record (
713         job_op_seq_num          number,
714         rtg_op_seq_num          number,
715         res_seq_num             number,
716         instance_id             number,
717         serial_number           varchar2(30),
718         assigned_units          number,
719         start_date              date,
720         /* ST : Detailed scheduling : start */
721         parent_res_seq_num      number,
722         resource_hours          number,
723         /* ST : Detailed scheduling : end */
724         completion_date         date
725 );
726 type t_res_use                  is table of rec_res_use index by binary_integer;
727 
728 type rec_op_comp IS record (
729         job_op_seq_num          number,
730         rtg_op_seq_num          number,
731         item_id                 number,
732         primary_item_id         number,
733         src_phm_item_id         number,
734         component_seq_id        number,
735         date_required           date,
736         component_yield         number,
737         required_qty            number,
738         qpa                     number, -- ADD bug 3347985
739 	-- Added For LBJ Interface Alcoa Enhancement
740         item_old_id         number,
741         wip_supply_type     number,
742         supply_subinventory varchar2(10),
743         supply_locator      number,
744         mrp_net_flag        number
745 );
746 type t_op_comp                  is table of rec_op_comp index by binary_integer;
747 
748 
749 
750 -- **********************************************************************************************
751 -- handle_invalid_field
752 -- **********************************************************************************************
753 -- this is called to display message of WSM_INVALID_FIELD type.
754 -- sets the process_status to 3, sets values of error_code and error_message,
755 -- writes into fnd log and calls write_to_wie
756 
757 PROCEDURE handle_invalid_field (
758         p_field             in varchar2,
759         p_aux_string        in varchar2,
760         stmt_number         in number) is
761 
762 l_err_msg   varchar2(2000) := null;
763 
764 BEGIN
765     v_wlji_process_status(v_idx) := 3;
766     v_wlji_err_code(v_idx) := -1;
767     fnd_message.set_name('WSM','WSM_INVALID_FIELD');
768     fnd_message.set_token('FLD_NAME', p_field);
769     l_err_msg := fnd_message.get;
770     v_wlji_err_msg(v_idx) := l_err_msg;
771     fnd_file.put_line(fnd_file.log,
772         'Error: (stmt_num: '|| stmt_number ||') '||l_err_msg||' '||p_aux_string);
773     fnd_file.new_line(fnd_file.log, 3);
774 END handle_invalid_field;
775 
776 
777 
778 -- **********************************************************************************************
779 -- handle_errmsg_name
780 -- **********************************************************************************************
781 -- this is called to display message of any type which does not involve setting any token
782 -- sets the process_status to 3, sets values of error_code and error_message,
783 -- writes into fnd log and calls write_to_wie
784 
785 PROCEDURE handle_errmsg_name(
786         p_product           in varchar2,
787         p_message_name      in varchar2,
788         stmt_number         in number) is
789 
790 l_err_msg   varchar2(2000) := null;
791 
792 BEGIN
793     v_wlji_process_status(v_idx) := 3; --ERROR
794     v_wlji_err_code(v_idx) := -1;
795     fnd_message.set_name(p_product, p_message_name);
796     l_err_msg := fnd_message.get;
797     v_wlji_err_msg(v_idx) := l_err_msg;
798     fnd_file.put_line(fnd_file.log,
799         'Error: (stmt_num: '|| stmt_number||') '||l_err_msg);
800     fnd_file.new_line(fnd_file.log, 3);
801 END handle_errmsg_name;
802 
803 
804 
805 -- **********************************************************************************************
806 -- handle_error
807 -- **********************************************************************************************
808 PROCEDURE handle_error (
809         p_err_code          in number,
810         p_err_msg           in varchar2,
811         stmt_number         in number) is
812 BEGIN
813     v_wlji_process_status(v_idx) := 3; -- ERROR
814     v_wlji_err_code(v_idx) := p_err_code;
815     v_wlji_err_msg(v_idx)  := substr(p_err_msg,1,2000);
816     fnd_file.put_line(fnd_file.log,
817         'Error: (stmt_num: ' || stmt_number||') '||p_err_msg);
818     fnd_file.new_line(fnd_file.log, 3);
819 END handle_error;
820 
821 
822 -- **********************************************************************************************
823 -- HANDLE_WARNING
824 -- **********************************************************************************************
825 PROCEDURE handle_warning (
826         p_err_msg                   in varchar2,
827         p_header_id                 in number,
828         p_request_id                in number,
829         p_program_id                in number,
830         p_program_application_id    in number) is
831 
832 dummy_err_code  number;
833 dummy_err_msg   varchar2(2000);
834 
835 BEGIN
836     fnd_file.put_line(fnd_file.log, 'Warning: ' || p_err_msg);
837     wsmputil.WRITE_TO_WIE (
838             p_header_id,
839             substr(p_err_msg,1,2000),
840             p_request_id,
841             p_program_id,
842             p_program_application_id,
843             2,
844             dummy_err_code,
845             dummy_err_msg );
846 END handle_warning;
847 
848 
849 
850 -- **********************************************************************************************
851 -- FIND_COPY_ROUTING_START: local procedure
852 -- **********************************************************************************************
853 procedure find_copy_routing_start(
854         p_wip_entity_id         number,
855         x_start_op_seq_num      out nocopy number,
856         x_start_op_seq_id       out nocopy number,
857         x_err_code              out nocopy number,
858         x_err_msg               out nocopy varchar2) is
859 
860 l_char_temp varchar2(1) := 'S';
861 
862 begin
863     x_err_code := 0;
864     x_err_msg  := null;
865 
866     select wco.operation_seq_num,
867            wco.operation_sequence_id
868     into   x_start_op_seq_num,
869            x_start_op_seq_id
870     from   wsm_copy_operations wco
871     where  wco.wip_entity_id = p_wip_entity_id
872     and    NVL(wco.network_start_end, 'X') = l_char_temp; -- donot use 'S'
873 
874 exception
875     when no_data_found then
876         x_err_code := -2;
877         fnd_message.set_name('wsm','wsm_net_start_not_found');
878         x_err_msg:= fnd_message.get;
879         return;
880 
881     when others then
882         x_err_code := sqlcode;
883         x_err_msg  := 'find_copy_routing_start '|| substr(sqlerrm,1,60);
884         return;
885 
886 end find_copy_routing_start;
887 
888 
889 -- **********************************************************************************************
890 -- FIND_COPY_ROUTING_END: local procedure
891 -- **********************************************************************************************
892 procedure find_copy_routing_end(
893         p_wip_entity_id         number,
894         x_end_op_seq_num        out nocopy number,
895         x_end_op_seq_id         out nocopy number,
896         x_err_code              out nocopy number,
897         x_err_msg               out nocopy varchar2) is
898 
899  l_char_temp varchar2(1) := 'E';
900 
901 begin
902     x_err_code := 0;
903     x_err_msg  := null;
904 
905     select wco.operation_seq_num,
906            wco.operation_sequence_id
907     into   x_end_op_seq_num,
908            x_end_op_seq_id
909     from   wsm_copy_operations wco
910     where  wco.wip_entity_id = p_wip_entity_id
911     and    NVL(wco.network_start_end, 'X') = l_char_temp; -- donot use 'E'
912 
913 exception
914     when no_data_found then
915         x_err_code := -2;
916         fnd_message.set_name('wsm','wsm_net_end_not_found');
917         x_err_msg:= fnd_message.get;
918         return;
919 
920     when others then
921         x_err_code := sqlcode;
922         x_err_msg  := 'find_copy_routing_end '|| substr(sqlerrm,1,60);
923         return;
924 
925 end find_copy_routing_end;
926 
927 
928 -- **********************************************************************************************
929 -- FIND_COPY_OP_SEQ_ID: local procedure
930 -- **********************************************************************************************
931 procedure find_copy_op_seq_id  (
932         p_wip_entity_id                    number,
933         p_rtg_op_seq_num                in number,
934         x_op_seq_id             out nocopy number,
935         x_err_code              out nocopy number,
936         x_err_msg               out nocopy varchar2 ) is
937 begin
938     x_err_code := 0;
939 
940     select  nvl(operation_sequence_id, -1)
941     into    x_op_seq_id
942     from    wsm_copy_operations
943     where   wip_entity_id = p_wip_entity_id
944     and     operation_seq_num = p_rtg_op_seq_num;
945 exception
946     when others then
947         x_err_code := sqlcode;
948         x_err_msg  := 'find_copy_op_seq_id '|| substr(sqlerrm,1,60);
949 end find_copy_op_seq_id;
950 
951 
952 -- **********************************************************************************************
953 -- VALIDATE_ORG_ID_NAME: local validation procedure
954 -- **********************************************************************************************
955 
956 -- Org_id and org_code cannot be NULL together. Org_id is the driven information,
957 -- Only when org_id is not given will org_code be used to get org_id
958 
959 PROCEDURE validate_org_id_name(
960         p_org_id            in out nocopy number,
961         p_org_code          in out nocopy varchar2,
962         x_error_code        out nocopy number,
963         x_error_msg         out nocopy varchar2) is
964 
965 l_xst       boolean;
966 l_stmt_num  number;
967 
968 BEGIN
969 
970     x_error_code := 0;
971     x_error_msg := '';
972 l_stmt_num  := 10;
973     if p_org_id IS NULL then
974         if p_org_code IS NULL then
975             fnd_message.set_name('WSM','WSM_NULL_FIELD');
976             fnd_message.set_token('FLD_NAME', 'Organization ID and Code');
977             x_error_msg := fnd_message.get;
978             x_error_code := -1;
979             return;
980         end if;
981 
982         begin
983 l_stmt_num  := 20;
984 
985 	-- changed from org_organization_definitions to MTL_PARAMETERS for bug 5051885
986 	-- Performance issue on org_organization_definitions for full table scan on base tables
987 	-- SQL id 16640485
988 
989             select organization_id
990             into   p_org_id
991             from   MTL_PARAMETERS
992             where  organization_code = p_org_code;
993     -- bug 5051885
994         exception
995             when others then
996                 fnd_message.set_name('WSM','WSM_INVALID_FIELD');
997                 fnd_message.set_token('FLD_NAME', 'Organization Code');
998                 x_error_msg := fnd_message.get;
999                 x_error_code := -1;
1000                 return;
1001         end;
1002     else
1003         p_org_code := null; -- ignore org code if id is given
1004     end if;
1005 
1006 l_stmt_num  := 30;
1007     -- now p_org_id is always available
1008     l_xst := true;
1009     l_xst := WSMPLCVA.v_org.exists(p_org_id);
1010     if l_xst = false then
1011         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1012         fnd_message.set_token('FLD_NAME', 'Organization ID');
1013         x_error_msg := fnd_message.get;
1014         x_error_code := -1;
1015         return;
1016     end if;
1017 EXCEPTION
1018     when others then
1019         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1020         fnd_message.set_token('FLD_NAME', 'Organization ID');
1021         x_error_msg := fnd_message.get;
1022         x_error_msg := 'validate_org_id_name (#' || l_stmt_num || ') ' || x_error_msg ;
1023         x_error_code := -1;
1024 END validate_org_id_name;
1025 
1026 
1027 -- **********************************************************************************************
1028 -- VALIDATE_WIP_ENTITY_ID_NAME: local validation procedure
1029 -- **********************************************************************************************
1030 
1031 -- For job creation, wip_entity_id will be ignored, if job_name is NULL, will default
1032 -- Will validate whether the job_name is used or not in the organization
1033 --
1034 -- For job update, wip_entity_id and job_name cannot be NULL together.
1035 -- Wip_entity_id is the driven information, only when wip_entity_id is NULL will
1036 -- job_name be used to get wip_entity_id
1037 -- Will validate the status of the job
1038 
1039 PROCEDURE validate_wip_entity_id_name(
1040         p_load_type         in number,  -- 5 job creation, 6 job update
1041         p_org_id            in number,
1042         p_wip_entity_id     in out nocopy number,
1043         p_job_name          in out nocopy varchar2,
1044         x_error_code        out nocopy number,
1045         x_error_msg         out nocopy varchar2) is
1046 
1047 l_temp_num          number;
1048 l_xst               boolean;
1049 l_stmt_num          number;
1050 l_job_name_hash     number;
1051 l_old_job_name      wsm_lot_job_interface.job_name%type; --fix for bug5990761
1052 BEGIN
1053 
1054     x_error_code := 0;
1055     x_error_msg := '';
1056 
1057 l_stmt_num := 10;
1058     -- Start : Fix for OPMConv project --
1059 --    if LENGTH(p_job_name) > 30 then
1060 --        fnd_message.set_name('WSM','WSM_JOB_NAME_THIRTY_CHAR');
1061 
1062     if LENGTH(p_job_name) > 80 then
1063         fnd_message.set_name('WSM','WSM_EXCEED_MAX_LEN');
1064         fnd_message.set_token('FLD_NAME', 'Job Name');
1065         fnd_message.set_token('VALUE', '80');
1066         x_error_msg := fnd_message.get;
1067         x_error_code := -1;
1068         return;
1069     end if;
1070     -- End : Fix for OPMConv project --
1071 
1072     if p_load_type = 5 then   -- Job creation
1073         if p_job_name is null then
1074 l_stmt_num := 20;
1075             -- Derive Job_Name.
1076             select FND_Profile.value('WIP_JOB_PREFIX') || wip_job_number_s.nextval
1077             into   p_job_name
1078             from   dual;
1079         end if;
1080 
1081         -- Be sure the provided Job_Name is not already in use.
1082         l_temp_num := 0;
1083         begin
1084 l_stmt_num := 30;
1085             select 1
1086             into   l_temp_num
1087             from   wip_entities
1088             where  wip_entity_name = p_job_name
1089             and    organization_id = p_org_id;
1090             if l_temp_num = 1 then
1091                 fnd_message.set_name('WIP','WIP_ML_JOB_NAME');
1092                 x_error_msg := fnd_message.get;
1093                 x_error_code := -1;
1094                 return;
1095             end if;
1096         exception
1097             when no_data_found then
1098                 null;
1099             when others then
1100                 fnd_message.set_name('WIP','WIP_ML_JOB_NAME');
1101                 x_error_msg := fnd_message.get;
1102                 x_error_code := -1;
1103                 return;
1104         end;
1105         p_wip_entity_id := null; -- Ignore wip_entity_id
1106 
1107     elsif p_load_type = 6 then    -- Job update
1108 
1109         l_xst := false;
1110         if p_wip_entity_id is null then
1111             if p_job_name is null then
1112                 fnd_message.set_name('WSM','WSM_NULL_FIELD');
1113                 fnd_message.set_token('FLD_NAME', 'Wip_Entity_Id and Job Name');
1114                 x_error_msg := fnd_message.get;
1115                 x_error_code := -1;
1116                 return;
1117             end if;
1118 
1119             l_job_name_hash := dbms_utility.get_hash_value(
1120                                     p_job_name || p_org_id,
1121                                     37, 1073741824);
1122             l_xst := g_v_job_name.exists(l_job_name_hash);
1123             if( NOT l_xst ) then
1124                 begin
1125 l_stmt_num := 40;
1126                     -- Get wip_entity_id by wip_entity_name
1127                     select wip_entity_id
1128                     into   p_wip_entity_id
1129                     from   wip_entities
1130                     where  wip_entity_name = p_job_name
1131                     and    organization_id = p_org_id;
1132                 exception
1133                     when others then
1134                         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1135                         fnd_message.set_token('FLD_NAME', 'Job_Name');
1136                         x_error_msg := fnd_message.get;
1137                         x_error_code := -1;
1138                         return;
1139                 end;
1140             else
1141                 p_wip_entity_id := g_v_wip_entity_id(l_job_name_hash);
1142             end if;
1143         end if;
1144 
1145         if( NOT l_xst ) then
1146             -- Now wip_entity_id is available, check job status
1147             begin
1148 l_stmt_num := 50;
1149 
1150                   --fix for bug5990761  allow update of job name for unreleased jobs
1151                 select decode(wdj.status_type, WIP_CONSTANTS.UNRELEASED,NVL(p_job_name,we.wip_entity_name),we.wip_entity_name),we.wip_entity_name
1152                 into   p_job_name,l_old_job_name
1153                 from   wip_discrete_jobs wdj,
1154                        wip_entities we
1155                 where  wdj.wip_entity_id = p_wip_entity_id
1156                 and    wdj.status_type in (
1157                         WIP_CONSTANTS.UNRELEASED,
1158                         WIP_CONSTANTS.RELEASED,
1159                         WIP_CONSTANTS.HOLD,
1160                         WIP_CONSTANTS.CANCELLED)
1161                 and    wdj.wip_entity_id = we.wip_entity_id
1162                 and    we.organization_id = p_org_id;
1163             exception
1164                 when others then
1165                     fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1166                     fnd_message.set_token('FLD_NAME', 'Wip_Entity_Id');
1167                     x_error_msg := fnd_message.get;
1168                     x_error_code := -1;
1169                     return;
1170             end;
1171 
1172 	-- fix for bug5990761 if wip entity id and job name are populated in WLJI, raise error
1173        -- if job name in WLJI conflicts with wip entity name of some other job in the same org.
1174           if (l_old_job_name <> p_job_name) then
1175           l_temp_num := 0;
1176             begin
1177                 select count(wip_entity_id)
1178                   into l_temp_num
1179                   from wip_entities
1180                  where wip_entity_name = p_job_name
1181                    and organization_id = p_org_id
1182                    and wip_entity_id <> p_wip_entity_id;
1183 
1184                 if (l_temp_num = 1) then
1185                    fnd_message.set_name('WIP','WIP_ML_JOB_NAME');
1186                    x_error_msg := fnd_message.get;
1187                    x_error_code := -1;
1188                    return;
1189                 end if;
1190             exception
1191                 when others then
1192                      fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1193                      fnd_message.set_token('FLD_NAME', 'Job_Name');
1194                      x_error_msg := fnd_message.get;
1195                      x_error_code := -1;
1196                      return;
1197             end;
1198           end if;
1199        -- end of additional validation added for 5990761
1200 
1201 
1202         end if;
1203 
1204         -- BA: 3512105
1205         if(WSMPUTIL.get_internal_copy_type(p_wip_entity_id) = 3) then
1206             fnd_message.set_name('WSM','WSM_NO_VALID_COPY');
1207             x_error_msg := fnd_message.get;
1208             x_error_code := -1;
1209             return;
1210         end if;
1211         -- BA: 3512105
1212 
1213     end if; -- Job update
1214 
1215 
1216 EXCEPTION
1217     when others then
1218         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1219         fnd_message.set_token('FLD_NAME', 'Wip_Entity_ID/Job_Name');
1220         x_error_msg := fnd_message.get;
1221         x_error_msg := 'validate_wip_entity_id_name (#' || l_stmt_num || ') ' || x_error_msg;
1222         x_error_code := -1;
1223 END validate_wip_entity_id_name;
1224 
1225 
1226 -- **********************************************************************************************
1227 -- DEFAULT_WLJDI_FIELDS: local procedure
1228 -- **********************************************************************************************
1229 
1230 procedure default_wljdi_fields(
1231         p_wip_entity_id     in number,
1232         p_org_id            in number,
1233         p_parent_header_id  in number,      -- header_id in WLJI, pass null if N/A
1234         p_group_id          in number,
1235         p_wo_records_exist  in varchar2,
1236         p_error_code        out nocopy number,
1237         p_error_msg         out nocopy varchar2) is
1238 
1239 cursor c_wljdi is
1240     select  record_id,
1241             -- ST Fix for bug 4285032 :
1242         -- APS will be populating 8 for resource instance information
1243         -- and 9 resource instance usage information..
1244         -- Map it to WSM load type 7 --> resource instance
1245         --                         4 --> resource instance usage..
1246             decode(load_type,8,7,
1247                          9,4,
1248                  load_type) load_type,
1249             substitution_type,
1250             job_op_seq_num,
1251             routing_op_seq_num,
1252             next_routing_op_seq_num,
1253             inventory_item_id_new,
1254             inventory_item_new,
1255             primary_item,
1256             primary_item_id,
1257             src_phantom_item,
1258             src_phantom_item_id,
1259             component_sequence_id,
1260             date_required,
1261             scheduled_quantity,
1262             required_quantity,
1263             batch_id,
1264             assigned_units,
1265             instance_id_new,
1266             operation_completion_date,
1267             operation_start_date,
1268             operation_yield,
1269             resource_code_new,
1270             resource_id_new,
1271             resource_seq_num,
1272             substitute_group_num,
1273             replacement_group_num,
1274             serial_number_new,
1275             start_date,
1276             completion_date,
1277             -- ST :  Detailed scheduling : start
1278             firm_type,
1279             setup_id,
1280             setup_code,
1281             group_sequence_id,
1282             group_sequence_num,
1283             max_assigned_units,
1284             parent_resource_seq_num,
1285             resource_hours,
1286             -- ST : Detailed Scheduling : Added for resource adding changes
1287             department_id          ,
1288             department_code ,
1289             -- ST : Detailed Scheduling : Added for resource adding changes : end
1290             -- Added For LBJ Interface Alcoa Enhancement
1291             recommended,
1292             standard_operation_code,
1293             standard_operation_id,
1294             operation_yield_enabled,
1295             minimum_transfer_quantity,
1296             backflush_flag,
1297             count_point_type,
1298             description,
1299             include_in_rollup,
1300             option_dependent_flag,
1301             inventory_item_id_old,
1302             inventory_item_old,
1303             wip_supply_type,
1304             cumulative_yield,/*Added for bugfix:7248992 */
1305             reverse_cumulative_yield/*Added for bugfix:7248992 */
1306     from    wsm_lot_job_dtl_interface
1307     where   parent_header_id = p_parent_header_id
1308     and     process_status = WIP_CONSTANTS.RUNNING;
1309 
1310 cursor c_wljdi_indp is
1311     select  record_id,
1312             -- ST Fix for bug 4285032 :
1313             -- APS will be populating 8 for resource instance information
1314             -- and 9 resource instance usage information..
1315             -- Map it to WSM load type 7 --> resource instance
1316             --                         4 --> resource instance usage..
1317             decode(load_type,8,7,
1318                              9,4,
1319                              load_type) load_type,
1320             substitution_type,
1321             job_op_seq_num,
1322             routing_op_seq_num,
1323             next_routing_op_seq_num,
1324             inventory_item_id_new,
1325             inventory_item_new,
1326             primary_item,
1327             primary_item_id,
1328             src_phantom_item,
1329             src_phantom_item_id,
1330             component_sequence_id,
1331             date_required,
1332             scheduled_quantity,
1333             required_quantity,
1334             batch_id,
1335             assigned_units,
1336             instance_id_new,
1337             operation_completion_date,
1338             operation_start_date,
1339             operation_yield,
1340             resource_code_new,
1341             resource_id_new,
1342             resource_seq_num,
1343             substitute_group_num,
1344             replacement_group_num,
1345             serial_number_new,
1346             start_date,
1347             completion_date,
1348             /* ST :  Detailed scheduling : start */
1349             firm_type,
1350             setup_id,
1351             setup_code,
1352             group_sequence_id,
1353             group_sequence_num,
1354             max_assigned_units,
1355             parent_resource_seq_num,
1356             resource_hours,
1357             /* ST :  Detailed scheduling : END */
1358             /* ST : Detailed Scheduling : Added for resource adding changes */
1359             department_id          ,
1360             department_code ,
1361             /* ST : Detailed Scheduling : Added for resource adding changes : end */
1362             -- Added For LBJ Interface Alcoa Enhancement
1363             recommended,
1364             standard_operation_code,
1365             standard_operation_id,
1366             operation_yield_enabled,
1367             minimum_transfer_quantity,
1368             backflush_flag,
1369             count_point_type,
1370             description,
1371             include_in_rollup,
1372             option_dependent_flag,
1373             inventory_item_id_old,
1374             inventory_item_old,
1375             wip_supply_type,
1376             cumulative_yield,/*Added for bugfix:7248992 */
1377             reverse_cumulative_yield/*Added for bugfix:7248992 */
1378     from    wsm_lot_job_dtl_interface
1379     where   parent_header_id IS NULL
1380     and     wip_entity_id     = p_wip_entity_id
1381     and     organization_id   = p_org_id
1382     and     process_status    = WIP_CONSTANTS.RUNNING
1383     and     NVL(group_id, -1) = NVL(p_group_id, -1);
1384 
1385 l_stmt_num                  number;
1386 l_d                         number;
1387 l_out                       number;
1388 l_hash_value                number;
1389 
1390 l_v_sub_grp_num             t_number;
1391 l_v_rpl_grp_num             t_number;
1392 l_v_res_seq_num             t_number;
1393 l_v_ins_id                  t_number;
1394 l_v_ser_num                 t_serial;
1395 l_v_item_id                 t_number;
1396 l_v_prm_item_id             t_number;
1397 l_v_phm_item_id             t_number;
1398 l_v_res_id                  t_number;
1399 l_v_record_id               t_number;
1400 
1401 /* ST : Detailed Scheduling start */
1402 l_v_dept_id             t_number;
1403 l_v_res_code            t_res_code;
1404 
1405 l_v_setup_id               t_number;  -- Added for bug fix 4704289
1406 
1407 l_index             number;
1408 /* ST : Detailed Scheduling end */
1409 
1410 l_load_type                 number;
1411 l_sub_type                  number;
1412 l_rtg_op_seq_num            number;
1413 l_job_op_seq_num            number;
1414 l_resource_id               number;
1415 l_sub_grp_num               number;
1416 l_rpl_grp_num               number;
1417 l_res_seq_num               number;
1418 
1419 /* ST : Detailed Scheduling : start */
1420 l_dept_id               number;
1421 l_par_res_seq_num       number;
1422 /* ST : Detailed Scheduling : end */
1423 
1424 l_setup_id				number;  -- Added for bug fix 4704289
1425 
1426 
1427 e_invalid_sub_load_type     exception;
1428 e_invalid_op_seq_num        exception;
1429 e_wljdi_common_error        exception;
1430 e_invalid_item_name         exception;
1431 e_invalid_resource_code     exception;
1432 e_no_res_seq_num            exception;
1433 
1434 /* ST : Detailed Scheduling : start */
1435 e_invalid_dept_code        exception;
1436 e_invalid_resource_id      exception;
1437 /* ST : Detailed Scheduling : end */
1438 
1439 e_invalid_setup_code	   exception;	 -- Added for bug fix 4704289
1440 
1441 -- Added For LBJ Interface Alcoa Enhancement
1442 l_v_op_start_date          t_date;
1443 l_v_op_completion_date     t_date;
1444 l_v_min_trf_qty            t_number;
1445 l_v_count_point_type       t_number;
1446 l_v_description            t_desc;
1447 l_v_backflush_flag         t_number;
1448 l_v_op_yield_enable        t_number;
1449 l_v_option_dep_flag        t_number;
1450 l_v_inc_in_rollup          t_number;
1451 l_v_std_op_id              t_number;
1452 l_v_std_op_code            t_op_code;
1453 l_v_recommended            t_recoflag;
1454 l_v_item_old_id             t_number;
1455 l_res_num                  number;
1456 l_res_exist                number;
1457 
1458 l_temp                     number;
1459 l_job_start_date           date;
1460 l_job_compl_date           date;
1461 l_xst                      number;
1462 
1463 e_inv_std_op               exception;
1464 e_invalid_min_trf_qty      exception;
1465 e_invalid_count_point      exception;
1466 e_invalid_backflush_flag   exception;
1467 e_dup_op_seq               exception;
1468 e_invalid_op_date          exception;
1469 --Alcoa Enhancement
1470 e_invalid_res_seq_num      exception;
1471 e_res_seq_num_exist        exception;
1472 e_invalid_routing_op_seq   exception;
1473 e_invalid_job_op_seq       exception;
1474 
1475 BEGIN
1476     ------------------------------------
1477     -- empty v_wljdi, load wljdi records
1478     ------------------------------------
1479     -- BA: bug 3313463/3311695 8i compatibility
1480     v_wljdi_record_id               := t_wljdi_record_id();
1481     v_wljdi_load_type               := t_wljdi_load_type();
1482     v_wljdi_substitution_type       := t_wljdi_substitution_type();
1483     v_wljdi_job_op_seq_num          := t_wljdi_job_op_seq_num();
1484     v_wljdi_routing_op_seq_num      := t_wljdi_routing_op_seq_num();
1485     v_wljdi_next_rtg_op_seq_num     := t_wljdi_next_rtg_op_seq_num();
1486 
1487     v_wljdi_inventory_item_id_new   := t_wljdi_inventory_item_id_new();
1488     v_wljdi_inventory_item_new      := t_wljdi_inventory_item_new();
1489     v_wljdi_primary_item            := t_wljdi_primary_item();
1490     v_wljdi_primary_item_id         := t_wljdi_primary_item_id();
1491     v_wljdi_src_phantom_item        := t_wljdi_src_phantom_item();
1492     v_wljdi_src_phantom_item_id     := t_wljdi_src_phantom_item_id();
1493     v_wljdi_component_sequence_id   := t_wljdi_component_sequence_id();
1494     v_wljdi_date_required           := t_wljdi_date_required();
1495     v_wljdi_scheduled_quantity      := t_wljdi_scheduled_quantity();
1496     v_wljdi_required_quantity       := t_wljdi_required_quantity();
1497     v_wljdi_batch_id                := t_wljdi_batch_id();
1498     v_wljdi_assigned_units          := t_wljdi_assigned_units();
1499     v_wljdi_instance_id_new         := t_wljdi_instance_id_new();
1500     v_wljdi_op_completion_date      := t_wljdi_op_completion_date();
1501     v_wljdi_op_start_date           := t_wljdi_op_start_date();
1502     v_wljdi_op_yield                := t_wljdi_op_yield();
1503     v_wljdi_resource_code_new       := t_wljdi_resource_code_new();
1504     v_wljdi_resource_id_new         := t_wljdi_resource_id_new();
1505     v_wljdi_resource_seq_num        := t_wljdi_resource_seq_num();
1506     v_wljdi_substitute_group_num    := t_wljdi_substitute_group_num();
1507     v_wljdi_replacement_group_num   := t_wljdi_replacement_group_num();
1508     v_wljdi_serial_number_new       := t_wljdi_serial_number_new();
1509     v_wljdi_start_date              := t_wljdi_start_date();
1510     v_wljdi_completion_date         := t_wljdi_completion_date();
1511 
1512     /* ST :  Detailed scheduling : start */
1513     v_wljdi_resource_hours          := t_wljdi_resource_hours();
1514     v_wljdi_firm_type               := t_wljdi_firm_type();
1515     v_wljdi_setup_id                := t_wljdi_setup_id();
1516     v_wljdi_setup_code              := t_wljdi_setup_code();
1517     v_wljdi_grp_seq_id              := t_wljdi_grp_seq_id();
1518     v_wljdi_grp_seq_num             := t_wljdi_grp_seq_num();
1519     v_wljdi_max_assg_units          := t_wljdi_max_assg_units();
1520     v_wljdi_parent_seq_num          := t_wljdi_parent_seq_num();
1521 
1522     /* Batch_id is already taken care */
1523 
1524     /* ST : Detailed Scheduling : Considered for adding a setup resource.... */
1525     v_wljdi_department_id           := t_wljdi_department_id();
1526     v_wljdi_department_code         := t_wljdi_department_code();
1527     /* ST : Detailed scheduling : end */
1528 
1529     -- Added For LBJ Interface Alcoa Enhancement
1530     v_wljdi_recommended             := t_wljdi_recommended();
1531     v_wljdi_std_op_code             := t_wljdi_std_op_code();
1532     v_wljdi_std_op_id               := t_wljdi_std_op_id();
1533     v_wljdi_op_yield_enable         := t_wljdi_op_yield_enable();
1534     v_wljdi_min_trf_qty             := t_wljdi_min_trf_qty();
1535     v_wljdi_backflush_flag          := t_wljdi_backflush_flag();
1536     v_wljdi_count_point_type        := t_wljdi_count_point_type();
1537     v_wljdi_description             := t_wljdi_description();
1538     v_wljdi_inc_in_rollup           := t_wljdi_inc_in_rollup();
1539     v_wljdi_option_dep_flag         := t_wljdi_option_dep_flag();
1540     v_wljdi_inventory_item_id_old   := t_wljdi_inventory_item_id_old();
1541     v_wljdi_inventory_item_old      := t_wljdi_inventory_item_old();
1542     v_wljdi_wip_supply_type         := t_wljdi_wip_supply_type();
1543 
1544 ---Added for BugFix 7248992
1545     v_wljdi_cum_yield               := t_wljdi_cum_yield();
1546     v_wljdi_rev_cum_yield           := t_wljdi_rev_cum_yield();
1547 
1548     -- EA: bug 3313463/3311695 8i compatibility
1549 
1550     if(p_parent_header_id IS NULL) then
1551         l_stmt_num := 10.1;
1552         --BC: bug 3313463/3311695
1553         open c_wljdi_indp;
1554         fetch c_wljdi_indp bulk collect into
1555             v_wljdi_record_id,
1556             v_wljdi_load_type,
1557             v_wljdi_substitution_type,
1558             v_wljdi_job_op_seq_num,
1559             v_wljdi_routing_op_seq_num,
1560             v_wljdi_next_rtg_op_seq_num,
1561             v_wljdi_inventory_item_id_new,
1562             v_wljdi_inventory_item_new,
1563             v_wljdi_primary_item,
1564             v_wljdi_primary_item_id,
1565             v_wljdi_src_phantom_item,
1566             v_wljdi_src_phantom_item_id,
1567             v_wljdi_component_sequence_id,
1568             v_wljdi_date_required,
1569             v_wljdi_scheduled_quantity,
1570             v_wljdi_required_quantity,
1571             v_wljdi_batch_id,
1572             v_wljdi_assigned_units,
1573             v_wljdi_instance_id_new,
1574             v_wljdi_op_completion_date,
1575             v_wljdi_op_start_date,
1576             v_wljdi_op_yield,
1577             v_wljdi_resource_code_new,
1578             v_wljdi_resource_id_new,
1579             v_wljdi_resource_seq_num,
1580             v_wljdi_substitute_group_num,
1581             v_wljdi_replacement_group_num,
1582             v_wljdi_serial_number_new,
1583             v_wljdi_start_date,
1584             v_wljdi_completion_date,
1585             /* ST :  Detailed scheduling : start */
1586             v_wljdi_firm_type   ,
1587             v_wljdi_setup_id    ,
1588             v_wljdi_setup_code  ,
1589             v_wljdi_grp_seq_id  ,
1590             v_wljdi_grp_seq_num ,
1591             v_wljdi_max_assg_units,
1592             v_wljdi_parent_seq_num,
1593             v_wljdi_resource_hours,
1594             /* ST :  Detailed scheduling : END */
1595             /* ST : Detailed Scheduling : Considered for adding a resource */
1596             v_wljdi_department_id      ,
1597             v_wljdi_department_code,
1598             /* ST : Detailed Scheduling :  end adding a resource changes */
1599             -- Added For LBJ Interface Alcoa Enhancement
1600             v_wljdi_recommended,
1601             v_wljdi_std_op_code,
1602             v_wljdi_std_op_id,
1603             v_wljdi_op_yield_enable,
1604             v_wljdi_min_trf_qty,
1605             v_wljdi_backflush_flag,
1606             v_wljdi_count_point_type,
1607             v_wljdi_description,
1608             v_wljdi_inc_in_rollup,
1609             v_wljdi_option_dep_flag,
1610             v_wljdi_inventory_item_id_old,
1611             v_wljdi_inventory_item_old,
1612             v_wljdi_wip_supply_type,
1613 	    v_wljdi_cum_yield,
1614 	    v_wljdi_rev_cum_yield
1615         ;
1616         close c_wljdi_indp;
1617         --EC: bug 3313463/3311695
1618     else
1619 l_stmt_num := 10.2;
1620         --BC: bug 3313463/3311695
1621         open c_wljdi;
1622         fetch c_wljdi bulk collect into
1623             v_wljdi_record_id,
1624             v_wljdi_load_type,
1625             v_wljdi_substitution_type,
1626             v_wljdi_job_op_seq_num,
1627             v_wljdi_routing_op_seq_num,
1628             v_wljdi_next_rtg_op_seq_num,
1629             v_wljdi_inventory_item_id_new,
1630             v_wljdi_inventory_item_new,
1631             v_wljdi_primary_item,
1632             v_wljdi_primary_item_id,
1633             v_wljdi_src_phantom_item,
1634             v_wljdi_src_phantom_item_id,
1635             v_wljdi_component_sequence_id,
1636             v_wljdi_date_required,
1637             v_wljdi_scheduled_quantity,
1638             v_wljdi_required_quantity,
1639             v_wljdi_batch_id,
1640             v_wljdi_assigned_units,
1641             v_wljdi_instance_id_new,
1642             v_wljdi_op_completion_date,
1643             v_wljdi_op_start_date,
1644             v_wljdi_op_yield,
1645             v_wljdi_resource_code_new,
1646             v_wljdi_resource_id_new,
1647             v_wljdi_resource_seq_num,
1648             v_wljdi_substitute_group_num,
1649             v_wljdi_replacement_group_num,
1650             v_wljdi_serial_number_new,
1651             v_wljdi_start_date,
1652             v_wljdi_completion_date,
1653             /* ST :  Detailed scheduling : start */
1654             v_wljdi_firm_type   ,
1655             v_wljdi_setup_id    ,
1656             v_wljdi_setup_code  ,
1657             v_wljdi_grp_seq_id  ,
1658             v_wljdi_grp_seq_num ,
1659             v_wljdi_max_assg_units,
1660             v_wljdi_parent_seq_num,
1661             v_wljdi_resource_hours,
1662             /* ST :  Detailed scheduling : END */
1663             /* ST : Detailed Scheduling : Considered for adding a resource */
1664             v_wljdi_department_id      ,
1665             v_wljdi_department_code,
1666             /* ST : Detailed Scheduling :  end adding a resource changes */
1667             -- Added For LBJ Interface Alcoa Enhancement
1668             v_wljdi_recommended,
1669             v_wljdi_std_op_code,
1670             v_wljdi_std_op_id,
1671             v_wljdi_op_yield_enable,
1672             v_wljdi_min_trf_qty,
1673             v_wljdi_backflush_flag,
1674             v_wljdi_count_point_type,
1675             v_wljdi_description,
1676             v_wljdi_inc_in_rollup,
1677             v_wljdi_option_dep_flag,
1678             v_wljdi_inventory_item_id_old,
1679             v_wljdi_inventory_item_old,
1680             v_wljdi_wip_supply_type,
1681 	    v_wljdi_cum_yield,
1682 	    v_wljdi_rev_cum_yield
1683         ;
1684         close c_wljdi;
1685         --EC: bug 3313463/3311695
1686     end if;
1687 
1688 
1689     ---------------------------------------
1690     -- validate required fields and default
1691     ---------------------------------------
1692 l_stmt_num := 20;
1693     -- clear output PL/SQL table
1694     l_out := 1;
1695     l_v_sub_grp_num.delete;
1696     l_v_rpl_grp_num.delete;
1697     l_v_res_seq_num.delete;
1698     l_v_ins_id.delete;
1699     l_v_ser_num.delete;
1700     l_v_item_id.delete;
1701     l_v_prm_item_id.delete;
1702     l_v_phm_item_id.delete;
1703     l_v_res_id.delete;
1704     l_v_record_id.delete;
1705 
1706     /* ST : Detailed Scheduling start */
1707     l_v_dept_id.delete;
1708     l_v_res_code.delete;
1709 	l_v_setup_id.delete;      -- Added for bug fix 4704289
1710     /* ST : Detailed Scheduling end */
1711 
1712     -- Added For LBJ Interface Alcoa Enhancement
1713     l_v_op_start_date.delete;
1714     l_v_op_completion_date.delete;
1715     l_v_min_trf_qty.delete;
1716     l_v_count_point_type.delete;
1717     l_v_description.delete;
1718     l_v_backflush_flag.delete;
1719     l_v_op_yield_enable.delete;
1720     l_v_option_dep_flag.delete;
1721     l_v_inc_in_rollup.delete;
1722     l_v_std_op_id.delete;
1723     l_v_std_op_code.delete;
1724     l_v_item_old_id.delete;
1725     l_v_recommended.delete;
1726 
1727     l_d := v_wljdi_record_id.first;
1728     while l_d IS NOT NULL
1729     loop
1730         l_load_type         := v_wljdi_load_type(l_d);
1731         l_sub_type          := v_wljdi_substitution_type(l_d);
1732         l_rtg_op_seq_num    := v_wljdi_routing_op_seq_num(l_d);
1733         l_job_op_seq_num    := v_wljdi_job_op_seq_num(l_d);
1734 
1735         -- We will ignore job_op_seq_num if p_wo_records_exist <> 'Y'
1736         if NOT(p_wo_records_exist = 'Y') then
1737             v_wljdi_job_op_seq_num(l_d) := NULL;
1738         end if;
1739 
1740         -- Added For LBJ Interface Alcoa Enhancement
1741         -- Validate job_op_seq_num.
1742         if v_wljdi_job_op_seq_num(l_d) is not null then
1743             begin
1744                 select 1
1745                 into l_xst
1746                 from wip_operations
1747                 where wip_entity_id = p_wip_entity_id
1748                 and organization_id = p_org_id
1749                 and operation_seq_num = v_wljdi_job_op_seq_num(l_d);
1750             exception
1751                 when others then
1752                     raise e_invalid_job_op_seq;
1753             end;
1754         end if;
1755 
1756 l_stmt_num := 30;
1757         -- validete substitution_type
1758         if l_sub_type not in (WSM_SUB_DEL, WSM_SUB_ADD, WSM_SUB_CHG,
1759                               WSM_SUB_REC, WSM_SUB_DIS)
1760         then
1761             -- get error message
1762             fnd_message.set_name('WSM','WSM_INVALID_FIELD');
1763             fnd_message.set_token('FLD_NAME', 'substitution_type');
1764             p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1765                            || fnd_message.get;
1766             p_error_code := -1;
1767             raise e_wljdi_common_error;
1768         end if;
1769 
1770         ---------------------------------------------------------------------------
1771         -- {{ validete load_type, substitution_type combination and required fields
1772         ---------------------------------------------------------------------------
1773         if (l_load_type = WSM_LOAD_RES) then        -- resource
1774 l_stmt_num := 40.1;
1775             if l_sub_type NOT IN (WSM_SUB_DEL, WSM_SUB_ADD, WSM_SUB_CHG, WSM_SUB_REC)
1776             then
1777                 raise e_invalid_sub_load_type;
1778             end if;
1779             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL)
1780             then
1781                 raise e_invalid_op_seq_num;
1782             end if;
1783 
1784             -- check resource_id_new/resource_code_new
1785             -- Modified For LBJ Interface Alcoa Enhancement
1786             -- Resource New is not used while deleting resource.
1787             if l_sub_type <> WSM_SUB_DEL then
1788                 if( v_wljdi_resource_id_new(l_d) IS NULL) then
1789                     if v_wljdi_resource_code_new(l_d) IS NULL then
1790                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
1791                         fnd_message.set_token('FLD_NAME', 'resource_id_new/resource_code_new');
1792                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1793                                     || fnd_message.get;
1794                         p_error_code := -1;
1795                         raise e_wljdi_common_error;
1796                     else
1797                         -- get resource id from item name
1798                         l_hash_value := dbms_utility.get_hash_value(
1799                                                 v_wljdi_resource_code_new(l_d) || p_org_id,
1800                                                 37, 1073741824);
1801                         if(g_v_res_id.exists(l_hash_value)) then
1802                             v_wljdi_resource_id_new(l_d) := g_v_res_id(l_hash_value);
1803                         else
1804                             begin
1805 l_stmt_num := 40.2;
1806                                 select unique(resource_id)
1807                                 into   v_wljdi_resource_id_new(l_d)
1808                                 from   bom_resources
1809                                 where  resource_code = v_wljdi_resource_code_new(l_d)
1810                                 and    organization_id = p_org_id
1811                                 and    cost_code_type in (3,4);
1812                             exception
1813                                 when others then
1814                                     raise e_invalid_resource_code;
1815                             end;
1816                             g_v_res_id(l_hash_value) := v_wljdi_resource_id_new(l_d);
1817                         end if;
1818                     end if;
1819                 --end if;
1820                 /* ST : Detailed Scheduling..... */
1821                 elsif v_wljdi_resource_code_new(l_d) IS NULL then
1822                         begin   /* unique why...? ..... */
1823                                     select unique(resource_code)
1824                                     into   v_wljdi_resource_code_new(l_d)
1825                                     from   bom_resources
1826                                     where  resource_id = v_wljdi_resource_id_new(l_d)
1827                                     and    organization_id = p_org_id
1828                                     and    cost_code_type in (3,4);
1829 
1830                         exception
1831                                     when others then
1832                                     raise e_invalid_resource_id;
1833                         end;
1834                 end if;
1835 
1836                 if(v_wljdi_resource_id_new(l_d) IS NULL) then
1837                     fnd_message.set_name('WSM','WSM_NULL_FIELD');
1838                     fnd_message.set_token('FLD_NAME', 'resource_id_new');
1839                     p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1840                                 || fnd_message.get;
1841                     p_error_code := -1;
1842                     raise e_wljdi_common_error;
1843                 end if;
1844             else
1845                 if v_wljdi_resource_seq_num(l_d) is null then
1846                     raise e_invalid_res_seq_num;
1847                 end if;
1848 
1849                 if l_job_op_seq_num is not null then
1850                     BEGIN
1851                         l_xst := 0;
1852 
1853                         select resource_id
1854                         into v_wljdi_resource_id_new(l_d)
1855                         from   wip_operation_resources wor
1856                         where  wor.wip_entity_id = p_wip_entity_id
1857                         and    wor.operation_seq_num = l_job_op_seq_num
1858                         and    wor.resource_seq_num = v_wljdi_resource_seq_num(l_d);
1859                     EXCEPTION
1860                         when no_data_found then
1861                             l_xst := 1;
1862                         when others then
1863                             raise e_invalid_res_seq_num;
1864                     END;
1865                     if l_xst =1 then
1866                         begin
1867                             select resource_id
1868                             into v_wljdi_resource_id_new(l_d)
1869                             from   wip_sub_operation_resources wsor
1870                             where  wsor.wip_entity_id = p_wip_entity_id
1871                             and    wsor.operation_seq_num = l_job_op_seq_num
1872                             and    wsor.resource_seq_num = v_wljdi_resource_seq_num(l_d);
1873                         exception
1874                             when others then
1875                                 raise e_invalid_res_seq_num;
1876                         end;
1877                     end if;
1878                 elsif l_rtg_op_seq_num is not null then
1879                     BEGIN
1880                         l_xst := 0;
1881                         select  resource_id
1882                         into    v_wljdi_resource_id_new(l_d)
1883                         from    wsm_copy_op_resources wcor
1884                         where   wcor.wip_entity_id        = p_wip_entity_id
1885                         and     wcor.operation_seq_num    = l_rtg_op_seq_num
1886                         and     wcor.resource_seq_num = v_wljdi_resource_seq_num(l_d);
1887                     EXCEPTION
1888                         when others then
1889                             raise e_invalid_res_seq_num;
1890                     END;
1891 
1892                 end if;
1893 
1894             end if;
1895             /* ST : Detailed Scheduling..... end */
1896             --if(v_wljdi_resource_id_new(l_d) IS NULL or
1897             --   v_wljdi_substitute_group_num(l_d) IS NULL )
1898             --then
1899             --    fnd_message.set_name('WSM','WSM_NULL_FIELD');
1900             --    fnd_message.set_token('FLD_NAME', 'resource_id_new/substitute_group_num');
1901             --    p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1902             --                || fnd_message.get;
1903             --    p_error_code := -1;
1904             --    raise e_wljdi_common_error;
1905             --end if;
1906 	    --Modiified for Alcoa Enhancement
1907 --            if(v_wljdi_resource_id_new(l_d) IS NULL)
1908 --            then
1909 --                fnd_message.set_name('WSM','WSM_NULL_FIELD');
1910 --                fnd_message.set_token('FLD_NAME', 'resource_id_new');
1911 --                p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1912 --                            || fnd_message.get;
1913 --                p_error_code := -1;
1914 --                raise e_wljdi_common_error;
1915 --            end if;
1916 
1917             /* ST : Detailed Scheduling : Add code here to default the dept id from dept code provided.... */
1918             if (v_wljdi_department_code(l_d) IS NOT NULL) then
1919                     begin
1920                              select unique(department_id)
1921                              into   l_dept_id
1922                              from   bom_departments
1923                              where  department_code = v_wljdi_department_code(l_d)
1924                              and    department_id   = nvl(v_wljdi_department_id(l_d),department_id)
1925                              and    organization_id = p_org_id;
1926                         exception
1927                              when others then
1928                                      raise e_invalid_dept_code;
1929                      end;
1930              end if;
1931              v_wljdi_department_id(l_d) := l_dept_id;
1932              /* ST : Detailed Scheduling : end */
1933 
1934 
1935 			 /* SS : Detailed Scheduling : Add code here to default the setup id from
1936 			    setup code if setup id is entered as null. Added for bug fix 4704289  */
1937             if (v_wljdi_setup_id(l_d) IS NULL and v_wljdi_setup_code(l_d) is NOT NULL) then
1938 					begin
1939 
1940 							select  bst.setup_id
1941 							into    l_setup_id
1942 							from    BOM_SETUP_TYPES bst
1943 							where   bst.setup_code      = v_wljdi_setup_code(l_d)
1944 							and     bst.organization_id = p_org_id;
1945 
1946 							v_wljdi_setup_id(l_d) :=l_setup_id;
1947 					exception
1948 							when no_data_found then
1949 								raise e_invalid_setup_code;
1950 							when others then
1951 								null;
1952 					end;
1953 			end if;
1954 			/* SS : Detailed Scheduling : end.  Added for bug fix 4704289 */
1955 
1956         elsif (l_load_type = WSM_LOAD_COMP) then     -- component
1957 l_stmt_num := 50.1;
1958             if l_sub_type NOT IN (WSM_SUB_DEL, WSM_SUB_ADD, WSM_SUB_CHG, WSM_SUB_REC)
1959             then
1960                 raise e_invalid_sub_load_type;
1961             end if;
1962             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL)
1963             then
1964                 raise e_invalid_op_seq_num;
1965             end if;
1966 
1967            -- Added For LBJ Interface Alcoa Enhancement
1968             if l_sub_type = WSM_SUB_DEL then
1969                 if(v_wljdi_inventory_item_id_old(l_d) IS NULL) then
1970                     if v_wljdi_inventory_item_old(l_d) IS NULL THEN
1971                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
1972                         fnd_message.set_token('FLD_NAME', 'inventory_item_id_old/inventory_item_old');
1973                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
1974                                     || fnd_message.get;
1975                         p_error_code := -1;
1976                         raise e_wljdi_common_error;
1977                     else
1978 l_stmt_num := 50;
1979                         begin
1980                             select unique(inventory_item_id)
1981                             into   v_wljdi_inventory_item_id_old(l_d)
1982                             from   mtl_system_items_kfv
1983                             where  concatenated_segments = v_wljdi_inventory_item_old(l_d)
1984                             and    organization_id = p_org_id;
1985                         exception
1986                             when others then
1987                                 raise e_invalid_item_name;
1988                         end;
1989                     end if;
1990                 else
1991                     begin
1992                         select concatenated_segments
1993                         into   v_wljdi_inventory_item_old(l_d)
1994                         from   mtl_system_items_kfv
1995                         where  inventory_item_id = v_wljdi_inventory_item_id_old(l_d)
1996                         and    organization_id = p_org_id;
1997                     exception
1998                         when others then
1999                             raise e_invalid_item_name;
2000                     end;
2001                 end if;
2002             end if;
2003 
2004             -- check inventory_item_id_new/inventory_item_new
2005             -- Modified For LBJ Interface Alcoa Enhancement
2006             if l_sub_type not in (WSM_SUB_DEL) then
2007                 if(v_wljdi_inventory_item_id_new(l_d) IS NULL) then
2008                     if v_wljdi_inventory_item_new(l_d) IS NULL then
2009                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
2010                         fnd_message.set_token('FLD_NAME', 'inventory_item_id_new/inventory_item_new');
2011                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2012                                     || fnd_message.get;
2013                         p_error_code := -1;
2014                         raise e_wljdi_common_error;
2015                     else
2016 l_stmt_num := 50.2;
2017                         -- get item id from item name
2018                         l_hash_value := dbms_utility.get_hash_value(
2019                                                 v_wljdi_inventory_item_new(l_d) || p_org_id,
2020                                                 37, 1073741824);
2021                         if(g_v_item_id.exists(l_hash_value)) then
2022                             v_wljdi_inventory_item_id_new(l_d) := g_v_item_id(l_hash_value);
2023                         else
2024                             begin
2025                                 select unique(inventory_item_id)
2026                                 into   v_wljdi_inventory_item_id_new(l_d)
2027                                 from   mtl_system_items_kfv
2028                                 where  concatenated_segments = v_wljdi_inventory_item_new(l_d)
2029                                 and    organization_id = p_org_id;
2030                             exception
2031                                 when others then
2032                                     raise e_invalid_item_name;
2033                             end;
2034                             g_v_item_id(l_hash_value) := v_wljdi_inventory_item_id_new(l_d);
2035                         end if;
2036                     end if;
2037                 else
2038                     begin
2039                         select concatenated_segments
2040                         into   v_wljdi_inventory_item_new(l_d)
2041                         from   mtl_system_items_kfv
2042                         where  inventory_item_id = v_wljdi_inventory_item_id_new(l_d)
2043                         and    organization_id = p_org_id;
2044                     exception
2045                         when others then
2046                             raise e_invalid_item_name;
2047                     end;
2048                 end if;
2049             end if;
2050 
2051             -- check primary_item_id/primary_item
2052             if(v_wljdi_primary_item_id(l_d) IS NULL) then
2053                 if v_wljdi_primary_item(l_d) IS NULL then
2054                     fnd_message.set_name('WSM','WSM_NULL_FIELD');
2055                     fnd_message.set_token('FLD_NAME', 'primary_item_id/primary_item');
2056                     p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2057                                 || fnd_message.get;
2058                     p_error_code := -1;
2059                     raise e_wljdi_common_error;
2060                 else
2061 l_stmt_num := 50.3;
2062                     -- get primary item id from item name
2063                     l_hash_value := dbms_utility.get_hash_value(
2064                                             v_wljdi_primary_item(l_d) || p_org_id,
2065                                             37, 1073741824);
2066                     if(g_v_item_id.exists(l_hash_value)) then
2067                         v_wljdi_primary_item_id(l_d) := g_v_item_id(l_hash_value);
2068                     else
2069                         begin
2070                             select unique(inventory_item_id)
2071                             into   v_wljdi_primary_item_id(l_d)
2072                             from   mtl_system_items_kfv
2073                             where  concatenated_segments = v_wljdi_primary_item(l_d)
2074                             and    organization_id = p_org_id;
2075                         exception
2076                             when others then
2077                                 raise e_invalid_item_name;
2078                         end;
2079                         g_v_item_id(l_hash_value) := v_wljdi_primary_item_id(l_d);
2080                     end if;
2081                 end if;
2082             end if;
2083 
2084             -- Modified for Alcoa Enhancement
2085             -- check src_phantom_item_id/phantom_item
2086             if v_wljdi_src_phantom_item_id(l_d) IS NULL then
2087                 if v_wljdi_src_phantom_item(l_d) IS NOT NULL then
2088 l_stmt_num := 50.4;
2089                     -- get item id from item name
2090                     l_hash_value := dbms_utility.get_hash_value(
2091                                             v_wljdi_src_phantom_item(l_d) || p_org_id,
2092                                             37, 1073741824);
2093                     if(g_v_item_id.exists(l_hash_value)) then
2094                         v_wljdi_src_phantom_item_id(l_d) := g_v_item_id(l_hash_value);
2095                     else
2096                         begin
2097                             select unique(inventory_item_id)
2098                             into   v_wljdi_src_phantom_item_id(l_d)
2099                             from   mtl_system_items_kfv
2100                             where  concatenated_segments = v_wljdi_src_phantom_item(l_d)
2101                             and    organization_id = p_org_id;
2102                         exception
2103                             when others then
2104                                 raise e_invalid_item_name;
2105                         end;
2106                         g_v_item_id(l_hash_value) := v_wljdi_src_phantom_item_id(l_d);
2107                     end if;
2108                 end if;
2109             else
2110                 begin
2111                     select concatenated_segments
2112                     into   v_wljdi_src_phantom_item(l_d)
2113                     from   mtl_system_items_kfv
2114                     where  inventory_item_id = v_wljdi_src_phantom_item_id(l_d)
2115                     and    organization_id = p_org_id;
2116                 exception
2117                     when others then
2118                         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2119                         fnd_message.set_token('FLD_NAME', 'Source_phantom_item_id/Source_phantom_item_name');
2120                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2121                         p_error_code := -1;
2122                         raise e_wljdi_common_error;
2123                 end;
2124             end if;
2125 
2126             -- Default source phantom Id for phantom component or it's child while deletion.
2127             if (l_sub_type = WSM_SUB_DEL AND v_wljdi_routing_op_seq_num(l_d) is not null) THEN
2128                  begin
2129                     select source_phantom_id
2130                     into   v_wljdi_src_phantom_item_id(l_d)
2131                     from   wsm_copy_requirement_ops
2132                     where  wip_entity_id=p_wip_entity_id
2133                     and    component_item_id = v_wljdi_inventory_item_id_old(l_d)
2134                     and    operation_seq_num in (v_wljdi_routing_op_seq_num(l_d), (-1)*v_wljdi_routing_op_seq_num(l_d));
2135                  exception
2136                     when others then
2137                       raise e_invalid_item_name;
2138                  end;
2139             end if;
2140 
2141             -- 1. For a parent phantom, phantom item id will be set to 0.
2142             -- 2. For a child of phantom, phantom item id will be set to parent phantom.
2143             -- 3. For a non-phantom, phantom item id will be set to -1.
2144             --  This will be used to sort the components in appropriate order when processing them.
2145 
2146             if v_wljdi_src_phantom_item_id(l_d) is not null then
2147                 if ( (v_wljdi_src_phantom_item_id(l_d)= v_wljdi_inventory_item_id_old(l_d) and
2148                       l_sub_type = WSM_SUB_DEL) OR
2149                      (v_wljdi_wip_supply_type(l_d)= WIP_CONSTANTS.PHANTOM and
2150                       l_sub_type = WSM_SUB_ADD and
2151                       v_wljdi_src_phantom_item_id(l_d)= v_wljdi_inventory_item_id_new(l_d)) ) then
2152                         v_wljdi_src_phantom_item_id(l_d):=0;
2153                 end if;
2154             else
2155                 if (v_wljdi_wip_supply_type(l_d)= WIP_CONSTANTS.PHANTOM and
2156                       l_sub_type = WSM_SUB_ADD) then
2157                     v_wljdi_src_phantom_item_id(l_d):= 0;
2158                 else
2159                     v_wljdi_src_phantom_item_id(l_d):= -1;
2160                 end if;
2161 
2162             end if;
2163 
2164             -- ignore the following fields
2165             v_wljdi_substitute_group_num(l_d)  := NULL;
2166             v_wljdi_replacement_group_num(l_d) := NULL;
2167             v_wljdi_resource_id_new(l_d)       := NULL;
2168             /* ST : Detailed Scheduling..... */
2169             v_wljdi_resource_code_new(l_d)     := NULL;
2170             /* ST : Detailed Scheduling..... */
2171             v_wljdi_resource_seq_num(l_d)      := NULL;
2172             v_wljdi_instance_id_new(l_d)       := NULL;
2173             v_wljdi_serial_number_new(l_d)     := NULL;
2174             if lbji_debug = 'Y' then
2175                 fnd_file.put_line(fnd_file.log,
2176                     'Ignore the following fields for load_type 2: substitute_group_num, '
2177                     || 'replacement_group_num, resource_id_new, resource_seq_num, '
2178                     || 'instance_id_new, serial_number_new');
2179             end if;
2180 
2181 
2182         elsif (l_load_type = WSM_LOAD_OP) then     -- operation: load_type 3 and 4 are the same
2183 l_stmt_num := 60.1;
2184 	    -- Modified For LBJ Interface Alcoa Enhancement
2185             if(l_sub_type NOT IN (WSM_SUB_CHG, WSM_SUB_REC, WSM_SUB_ADD, WSM_SUB_DEL)) then
2186                 raise e_invalid_sub_load_type;
2187             end if;
2188             -- Modified For LBJ Interface Alcoa Enhancement
2189             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL and l_sub_type <> WSM_SUB_ADD)
2190             then
2191                 raise e_invalid_op_seq_num;
2192             end if;
2193 
2194             -- Added For LBJ Interface Alcoa Enhancement
2195             if (l_sub_type = WSM_SUB_ADD) then
2196 
2197                 -- This code Defaults Standard operation Id.
2198                 if (v_wljdi_std_op_code(l_d) is not null) then
2199                     begin
2200                         select standard_operation_id
2201                         into v_wljdi_std_op_id(l_d)
2202                         from bom_standard_operations
2203                         where organization_id = p_org_id
2204                         and operation_code = v_wljdi_std_op_code(l_d)
2205                         and standard_operation_id = nvl(v_wljdi_std_op_id(l_d),standard_operation_id);
2206                     exception
2207                         when others then
2208                             raise e_inv_std_op;
2209                     end;
2210                 end if;
2211 
2212                 if (v_wljdi_std_op_id(l_d) is not null and
2213                     v_wljdi_std_op_code(l_d) is null) then
2214                     begin
2215                         select operation_code
2216                         into v_wljdi_std_op_code(l_d)
2217                         from bom_standard_operations
2218                         where organization_id = p_org_id
2219                         and standard_operation_id = v_wljdi_std_op_id(l_d);
2220                     exception
2221                         when others then
2222                             raise e_inv_std_op;
2223                     end;
2224                 end if;
2225 
2226                 if (v_wljdi_std_op_id(l_d) is not null) then
2227 
2228                     begin
2229                         select department_id,
2230                                nvl(v_wljdi_min_trf_qty(l_d),minimum_transfer_quantity),
2231                                nvl(v_wljdi_count_point_type(l_d),count_point_type),
2232                                nvl(v_wljdi_description(l_d),operation_description),
2233                                nvl(v_wljdi_backflush_flag(l_d),backflush_flag),
2234                                nvl(v_wljdi_op_yield_enable(l_d),operation_yield_enabled),
2235                                nvl(v_wljdi_option_dep_flag(l_d),option_dependent_flag),
2236                                nvl(v_wljdi_inc_in_rollup(l_d),include_in_rollup)
2237                         into  v_wljdi_department_id(l_d),
2238                               v_wljdi_min_trf_qty(l_d),
2239                               v_wljdi_count_point_type(l_d),
2240                               v_wljdi_description(l_d),
2241                               v_wljdi_backflush_flag(l_d),
2242                               v_wljdi_op_yield_enable(l_d),
2243                               v_wljdi_option_dep_flag(l_d),
2244                               v_wljdi_inc_in_rollup(l_d)
2245                         from   bom_standard_operations
2246                         where standard_operation_id = v_wljdi_std_op_id(l_d)
2247                         and  organization_id = p_org_id;
2248                     exception
2249                         when others then
2250                             raise e_inv_std_op;
2251                     end;
2252                 end if;
2253                 -- Validate Department
2254                 if (v_wljdi_department_code(l_d) IS NOT NULL) then
2255                     begin
2256                         select unique(department_id)
2257                         into   v_wljdi_department_id(l_d)
2258                         from   bom_departments
2259                         where  department_code = v_wljdi_department_code(l_d)
2260                         and    department_id   = nvl(v_wljdi_department_id(l_d),department_id)
2261                         and    organization_id = p_org_id;
2262                     exception
2263                         when others then
2264                             raise e_invalid_dept_code;
2265                     end;
2266                 else
2267                     begin
2268                         select 1
2269                         into   l_temp
2270                         from   bom_departments
2271                         where  department_id   = v_wljdi_department_id(l_d)
2272                         and    organization_id = p_org_id;
2273                     exception
2274                         when others then
2275                             raise e_invalid_dept_code;
2276                     end;
2277                 end if;
2278                 -- Validate Min trf Qty.
2279                 if (v_wljdi_min_trf_qty(l_d) < 0 ) then
2280                     raise e_invalid_min_trf_qty;
2281                 end if;
2282                 if (v_wljdi_min_trf_qty(l_d) is null) then
2283                     v_wljdi_min_trf_qty(l_d) := 0;
2284                 end if;
2285 
2286                 -- Validate Count Point
2287                 if (v_wljdi_count_point_type(l_d) is not null and
2288                     v_wljdi_count_point_type(l_d) not in (WIP_CONSTANTS.YES_AUTO,
2289                                                           WIP_CONSTANTS.NO_AUTO,
2290                                                           WIP_CONSTANTS.NO_DIRECT)) then
2291                     raise e_invalid_count_point;
2292                 end if;
2293                 if (v_wljdi_count_point_type(l_d) is null) then
2294                     v_wljdi_count_point_type(l_d) :=0;
2295                 end if;
2296 
2297                 -- Validate Backflush flag.
2298                 if (v_wljdi_backflush_flag(l_d) is not null and
2299                     v_wljdi_backflush_flag(l_d) not in (WIP_CONSTANTS.WBF_BF_NOPAGE ,
2300                                                         WIP_CONSTANTS.WBF_BF_PAGE)) then
2301                     raise e_invalid_backflush_flag;
2302                 end if;
2303                 if (v_wljdi_backflush_flag(l_d) is null) then
2304                     v_wljdi_backflush_flag(l_d) :=0;
2305                 end if;
2306 
2307                 if (v_wljdi_op_yield_enable(l_d) is null) then
2308                     v_wljdi_op_yield_enable(l_d) :=1;
2309                 end if;
2310 
2311                 if (v_wljdi_inc_in_rollup(l_d) is null) then
2312                     v_wljdi_inc_in_rollup(l_d) :=1;
2313                 end if;
2314 
2315                 if (v_wljdi_option_dep_flag(l_d) is null) then
2316                     v_wljdi_option_dep_flag(l_d) :=2;
2317                 end if;
2318                 l_temp :=0;
2319                 if (v_wljdi_routing_op_seq_num(l_d) is not null) then
2320                     l_temp :=0;
2321                     begin
2322                         select 1
2323                         into l_temp
2324                         from wsm_copy_operations
2325                         where wip_entity_id = p_wip_entity_id
2326                         and organization_id = p_org_id
2327                         and operation_seq_num = v_wljdi_routing_op_seq_num(l_d);
2328                     exception
2329                         when no_data_found then
2330                             l_temp :=0;
2331                         when others then
2332                             l_temp :=1;
2333                     end;
2334                 end if;
2335 
2336                 if (l_temp =1) then
2337                     raise e_dup_op_seq;
2338                 end if;
2339 
2340                 select  scheduled_start_date,
2341                         scheduled_completion_date
2342                 into    l_job_start_date,
2343                         l_job_compl_date
2344                 from    wip_discrete_jobs
2345                 where   wip_entity_id = p_wip_entity_id
2346                 and     organization_id = p_org_id;
2347 
2348                 if(v_wljdi_op_start_date(l_d) IS NOT NULL and v_wljdi_op_completion_date(l_d) IS NOT NULL) then
2349 
2350                     if( NVL(l_job_start_date, v_wljdi_op_start_date(l_d)) > v_wljdi_op_start_date(l_d) or
2351                         v_wljdi_op_start_date(l_d) > v_wljdi_op_completion_date(l_d) or
2352                         v_wljdi_op_completion_date(l_d) > NVL(l_job_compl_date, v_wljdi_op_completion_date(l_d)))
2353                     then
2354                         raise e_invalid_op_date;
2355                     end if;
2356                 elsif(v_wljdi_op_start_date(l_d) IS NOT NULL and v_wljdi_op_completion_date(l_d) IS NULL) then
2357 
2358                     if( NVL(l_job_start_date, v_wljdi_op_start_date(l_d)) > v_wljdi_op_start_date(l_d) or
2359                         v_wljdi_op_start_date(l_d) > NVL(l_job_compl_date, v_wljdi_op_start_date(l_d)))
2360                     then
2361                         raise e_invalid_op_date;
2362                     end if;
2363                     v_wljdi_op_completion_date(l_d) := l_job_compl_date;
2364 
2365                 elsif(v_wljdi_op_start_date(l_d) IS NULL and v_wljdi_op_completion_date(l_d) IS NOT NULL) then
2366 
2367                     if( NVL(l_job_start_date, v_wljdi_op_completion_date(l_d)) > v_wljdi_op_completion_date(l_d) or
2368                         v_wljdi_op_completion_date(l_d) > NVL(l_job_compl_date, v_wljdi_op_completion_date(l_d)))
2369                     then
2370                         raise e_invalid_op_date;
2371                     end if;
2372                     v_wljdi_op_start_date(l_d) := l_job_start_date;
2373                 else
2374                     v_wljdi_op_start_date(l_d) := l_job_start_date;
2375                     v_wljdi_op_completion_date(l_d) := l_job_compl_date;
2376                 end if;
2377 
2378             end if;
2379 
2380 
2381         elsif (l_load_type in (WSM_LOAD_RES_USE, WSM_LOAD_RES_INS)) then  -- resource usage/instance
2382 l_stmt_num := 70.1;
2383             if l_sub_type NOT IN (WSM_SUB_DEL, WSM_SUB_ADD, WSM_SUB_REC)
2384             then
2385                 raise e_invalid_sub_load_type;
2386             end if;
2387             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL)
2388             then
2389                 raise e_invalid_op_seq_num;
2390             end if;
2391 
2392             -- get resource_seq_num if it is null
2393             -- if l_job_op_seq_num IS NOT NULL, get from WOR
2394             -- otherwise get from WCOR
2395             l_res_seq_num := v_wljdi_resource_seq_num(l_d);
2396 
2397             -- ST : Bug fix 4240165 : Detailed Scheduling changes start
2398             l_par_res_seq_num := v_wljdi_parent_seq_num(l_d);
2399             -- ST : Bug fix 4240165 : Detailed Scheduling changes end
2400 
2401 
2402             if(l_res_seq_num IS NULL)
2403             then
2404                 l_resource_id := v_wljdi_resource_id_new(l_d);
2405                 l_sub_grp_num := v_wljdi_substitute_group_num(l_d);
2406                 l_rpl_grp_num := v_wljdi_replacement_group_num(l_d);
2407 
2408                 if(l_resource_id IS NULL) then
2409                     if v_wljdi_resource_code_new(l_d) IS NULL then
2410                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
2411                         fnd_message.set_token('FLD_NAME', 'resource_id_new/resource_code_new');
2412                         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2413                                     || fnd_message.get;
2414                         p_error_code := -1;
2415                         raise e_wljdi_common_error;
2416                     else
2417                         -- get resource id from item name
2418                         l_hash_value := dbms_utility.get_hash_value(
2419                                                 v_wljdi_resource_code_new(l_d) || p_org_id,
2420                                                 37, 1073741824);
2421                         if(g_v_res_id.exists(l_hash_value)) then
2422                             l_resource_id := g_v_res_id(l_hash_value);
2423                         else
2424                             begin
2425 l_stmt_num := 70.2;
2426                                 select unique(resource_id)
2427                                 into   l_resource_id
2428                                 from   bom_resources
2429                                 where  resource_code = v_wljdi_resource_code_new(l_d)
2430                                 and    organization_id = p_org_id;
2431                             exception
2432                                 when others then
2433                                     raise e_invalid_resource_code;
2434                             end;
2435                             g_v_res_id(l_hash_value) := l_resource_id;
2436                         end if;
2437                     end if;
2438                 end if;
2439 
2440 
2441                 -- ST : Bug fix 4240165 : Detailed Scheduling changes start
2442                 IF (l_par_res_seq_num IS NULL) THEN
2443                 -- ST : Bug fix 4240165 : Detailed Scheduling changes end
2444 
2445                         if( l_job_op_seq_num IS NULL) then
2446                                 begin
2447                                         l_stmt_num  := 70.3;
2448                                         select  resource_seq_num
2449                                         into    l_res_seq_num
2450                                         from    wsm_copy_op_resources wcor
2451                                         where   wcor.wip_entity_id         = p_wip_entity_id
2452                                         and     wcor.organization_id       = p_org_id
2453                                         and     wcor.operation_seq_num     = l_rtg_op_seq_num
2454                                         and     wcor.resource_id           = l_resource_id
2455                                         and     NVL(wcor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
2456                                         and     NVL(wcor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
2457                                 exception
2458                                         when no_data_found then
2459                                                 raise e_no_res_seq_num;
2460 
2461                                         when too_many_rows then
2462                                                 raise e_no_res_seq_num;
2463                                 end;
2464                         else
2465                                 begin
2466                                         l_stmt_num  := 70.4;
2467                                         select  resource_seq_num
2468                                         into    l_res_seq_num
2469                                         from    wip_operation_resources wor
2470                                         where   wor.wip_entity_id         = p_wip_entity_id
2471                                         and     wor.organization_id       = p_org_id
2472                                         and     wor.operation_seq_num     = l_job_op_seq_num
2473                                         and     wor.resource_id           = l_resource_id
2474                                         and     NVL(wor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
2475                                         and     NVL(wor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
2476                                 exception
2477                                         when no_data_found then
2478                                                 raise e_no_res_seq_num;
2479                                         when too_many_rows then
2480                                                 raise e_no_res_seq_num;
2481                                 end;
2482                         end if;
2483                         v_wljdi_resource_seq_num(l_d) := l_res_seq_num;
2484                 end if;       -- l_parent_res_seq_num is NULL
2485                 -- ST : Bug fix 4240165 : Detailed Scheduling changes end
2486             end if; -- l_res_seq_num IS NULL
2487 l_stmt_num  := 70.5;
2488 
2489             -- ignore the following fields
2490             v_wljdi_substitute_group_num(l_d)  := NULL;
2491             v_wljdi_replacement_group_num(l_d) := NULL;
2492 
2493             -- ST : Bug fix 4240165 : Detailed Scheduling changes
2494             -- NULL it out only when Resource seq num is obtained...
2495             IF v_wljdi_resource_seq_num(l_d) IS NOT NULL THEN
2496                     v_wljdi_resource_id_new(l_d)       := NULL;
2497             END IF;
2498 
2499         elsif (l_load_type = WSM_LOAD_LINK) then     -- link: load_type must be 4 or 5
2500 l_stmt_num := 80;
2501             -- Modified For LBJ Interface Alcoa Enhancement
2502             if l_sub_type NOT IN (WSM_SUB_DIS, WSM_SUB_REC, WSM_SUB_DEL, WSM_SUB_ADD) then
2503                 raise e_invalid_sub_load_type;
2504             end if;
2505             if(--v_wljdi_routing_op_seq_num(l_d) IS NULL or
2506                v_wljdi_next_rtg_op_seq_num(l_d) IS NULL)
2507             then
2508                 fnd_message.set_name('WSM','WSM_NULL_FIELD');
2509                 --fnd_message.set_token('FLD_NAME', 'routing_op_seq_num and next_routing_op_seq_num');
2510                 fnd_message.set_token('FLD_NAME', 'next_routing_op_seq_num');
2511                 p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2512                             || fnd_message.get;
2513                 p_error_code := -1;
2514                 raise e_wljdi_common_error;
2515             end if;
2516 
2517         elsif (l_load_type = WSM_LOAD_LINK_OP) then     -- link or operation
2518 l_stmt_num := 90;
2519             if(l_sub_type NOT IN (WSM_SUB_CHG, WSM_SUB_REC)) then
2520                 raise e_invalid_sub_load_type;
2521             end if;
2522             if(l_rtg_op_seq_num IS NULL and l_job_op_seq_num IS NULL)
2523             then
2524                 raise e_invalid_op_seq_num;
2525             end if;
2526 
2527         else
2528             fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2529             fnd_message.set_token('FLD_NAME', 'load_type');
2530             p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2531                            || fnd_message.get;
2532             p_error_code := -1;
2533             raise e_wljdi_common_error;
2534         end if;
2535         ---------------------------------------------------------------------------
2536         -- }} validete load_type, substitution_type combination and required fields
2537         ---------------------------------------------------------------------------
2538 l_stmt_num := 100;
2539 
2540         ---Added for Alcoa Enhancement
2541         if  upper(v_wljdi_recommended(l_d)) = 'Y' and l_sub_type= WSM_SUB_ADD then
2542             l_v_recommended(l_out) := 'Y';
2543         else
2544             l_v_recommended(l_out) := 'N';
2545         end if;
2546 
2547         l_v_sub_grp_num(l_out)  := v_wljdi_substitute_group_num(l_d);
2548         l_v_rpl_grp_num(l_out)  := v_wljdi_replacement_group_num(l_d);
2549         l_v_res_id(l_out)       := v_wljdi_resource_id_new(l_d);
2550         l_v_res_seq_num(l_out)  := v_wljdi_resource_seq_num(l_d);
2551         l_v_ins_id(l_out)       := v_wljdi_instance_id_new(l_d);
2552         l_v_ser_num(l_out)      := v_wljdi_serial_number_new(l_d);
2553         l_v_item_id(l_out)      := v_wljdi_inventory_item_id_new(l_d);
2554         l_v_prm_item_id(l_out)  := v_wljdi_primary_item_id(l_d);
2555         l_v_phm_item_id(l_out)  := v_wljdi_src_phantom_item_id(l_d);
2556         l_v_record_id(l_out)    := v_wljdi_record_id(l_d);
2557 
2558         /* ST : Detailed Scheduling start */
2559         l_v_dept_id(l_out)      := v_wljdi_department_id(l_d);
2560         l_v_res_code(l_out)     := v_wljdi_resource_code_new(l_d);
2561 		l_v_setup_id(l_out)		:= v_wljdi_setup_id(l_d);     -- Added for bug fix 4704289
2562 
2563         /* ST : Detailed Scheduling end   */
2564 
2565         -- Added For LBJ Interface Alcoa Enhancement
2566         l_v_op_start_date(l_out)      := v_wljdi_op_start_date(l_d);
2567         l_v_op_completion_date(l_out) := v_wljdi_op_completion_date(l_d);
2568         l_v_min_trf_qty(l_out)        := v_wljdi_min_trf_qty(l_d);
2569         l_v_count_point_type(l_out)   := v_wljdi_count_point_type(l_d);
2570         l_v_description(l_out)        := v_wljdi_description(l_d);
2571         l_v_backflush_flag(l_out)     := v_wljdi_backflush_flag(l_d);
2572         l_v_op_yield_enable(l_out)    := v_wljdi_op_yield_enable(l_d);
2573         l_v_option_dep_flag(l_out)    := v_wljdi_option_dep_flag(l_d);
2574         l_v_inc_in_rollup(l_out)      := v_wljdi_inc_in_rollup(l_d);
2575         l_v_std_op_id(l_out)          := v_wljdi_std_op_id(l_d);
2576         l_v_std_op_code(l_out)        := v_wljdi_std_op_code(l_d);
2577         l_v_item_old_id(l_out)        := v_wljdi_inventory_item_id_old(l_d);
2578 
2579         l_out := l_out + 1;
2580 
2581         l_d := v_wljdi_record_id.next(l_d);
2582     end loop;
2583 
2584     ---------------------------------------
2585     -- bulk update WLJDI
2586     ---------------------------------------
2587 l_stmt_num := 110;
2588     forall i in l_v_record_id.first..l_v_record_id.last
2589     update  wsm_lot_job_dtl_interface
2590     set     substitute_group_num    = l_v_sub_grp_num(i),
2591             replacement_group_num   = l_v_rpl_grp_num(i),
2592             resource_id_new         = l_v_res_id(i),
2593             resource_seq_num        = l_v_res_seq_num(i),
2594             instance_id_new         = l_v_ins_id(i),
2595             serial_number_new       = l_v_ser_num(i),
2596             inventory_item_id_new   = l_v_item_id(i),
2597             primary_item_id         = l_v_prm_item_id(i),
2598             src_phantom_item_id     = l_v_phm_item_id(i),
2599             /* ST : Detailed Scheduling start */
2600             resource_code_new       = l_v_res_code(i),
2601 			/* Added NVL on department_id for Bug 5471556. It is done so that it's not cleared
2602 			out if user entered department id alone so that it can be validated at later stage. */
2603             department_id           = nvl(l_v_dept_id(i),department_id),
2604 			setup_id				= l_v_setup_id(i),    -- Added for bug fix 4704289
2605             /* ST : Detailed Scheduling end   */
2606             -- Added For LBJ Interface Alcoa Enhancement
2607             standard_operation_id     =  l_v_std_op_id(i),
2608             operation_start_date      =  l_v_op_start_date(i),
2609             operation_completion_date =  l_v_op_completion_date(i),
2610             minimum_transfer_quantity =  l_v_min_trf_qty(i),
2611             count_point_type          =  l_v_count_point_type(i),
2612             description               =  l_v_description(i),
2613             backflush_flag            =  l_v_backflush_flag(i),
2614             operation_yield_enabled   =  l_v_op_yield_enable(i),
2615             option_dependent_flag     =  l_v_option_dep_flag(i),
2616             include_in_rollup         =  l_v_inc_in_rollup(i),
2617             standard_operation_code   =  l_v_std_op_code(i),
2618             recommended               =  l_v_recommended(i),
2619             inventory_item_id_old   = l_v_item_old_id(i)
2620     where   record_id = l_v_record_id(i);
2621 
2622 EXCEPTION
2623     when e_invalid_sub_load_type then
2624         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2625         fnd_message.set_token('FLD_NAME', 'load_type and substitution_type');
2626         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2627                     || fnd_message.get;
2628         p_error_code := -1;
2629         fnd_file.put_line(fnd_file.log, substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2630 
2631     when e_invalid_op_seq_num then
2632         fnd_message.set_name('WSM','WSM_NULL_FIELD');
2633         fnd_message.set_token('FLD_NAME', 'job_op_seq_num and routing_op_seq_num');
2634         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2635                     || fnd_message.get;
2636         p_error_code := -1;
2637         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '
2638                 || p_error_msg, 1, 2000));
2639 
2640     when e_invalid_resource_code then
2641         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2642         fnd_message.set_token('FLD_NAME', 'resource_code_new');
2643         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2644                     || fnd_message.get;
2645         p_error_code := -1;
2646         fnd_file.put_line(fnd_file.log,
2647             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2648                 || p_error_msg, 1, 2000));
2649 
2650     /* ST : Detailed Scheduling start */
2651 
2652     when e_invalid_resource_id then
2653         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2654         fnd_message.set_token('FLD_NAME', 'resource_id_new');
2655         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2656                     || fnd_message.get;
2657         p_error_code := -1;
2658         fnd_file.put_line(fnd_file.log,
2659             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2660                 || p_error_msg, 1, 2000));
2661 
2662     when e_invalid_dept_code then
2663         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2664         fnd_message.set_token('FLD_NAME', 'department_code/department id combination');
2665         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2666                     || fnd_message.get;
2667         p_error_code := -1;
2668         fnd_file.put_line(fnd_file.log,
2669             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2670                 || p_error_msg, 1, 2000));
2671 
2672     /* ST : Detailed Scheduling end */
2673 
2674     when e_invalid_item_name then
2675         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2676         fnd_message.set_token('FLD_NAME', 'item_id');
2677         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2678                     || fnd_message.get;
2679         p_error_code := -1;
2680         fnd_file.put_line(fnd_file.log,
2681             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2682                 || p_error_msg, 1, 2000));
2683 
2684     when e_no_res_seq_num then
2685         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2686                     || 'resource_seq_num cannot be uniquely found'; -- CZHDBG_MSG
2687         p_error_code := -1;
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_wljdi_common_error then
2693         fnd_file.put_line(fnd_file.log,
2694             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2695                 || p_error_msg, 1, 2000));
2696 
2697 	when e_invalid_setup_code then
2698 		fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2699         fnd_message.set_token('FLD_NAME', 'Setup Code ');
2700 		p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2701                     || fnd_message.get;
2702         p_error_code := -1;
2703         fnd_file.put_line(fnd_file.log,
2704             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2705                 || p_error_msg, 1, 2000));
2706 
2707    ---Added for LBJ Enhancement
2708 
2709     when e_res_seq_num_exist  then
2710         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2711         fnd_message.set_token('FLD_NAME', 'resource_seq_num/job_op_seq_num combination');
2712         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2713                     || fnd_message.get;
2714         p_error_code := -1;
2715         fnd_file.put_line(fnd_file.log,
2716             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2717                 || p_error_msg, 1, 2000));
2718 
2719     when e_invalid_res_seq_num then
2720         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2721         fnd_message.set_token('FLD_NAME', 'resource_seq_num/job_op_seq_num combination');
2722         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
2723                     || fnd_message.get;
2724         p_error_code := -1;
2725         fnd_file.put_line(fnd_file.log,
2726             substr('default_wljdi_fields: (#'||l_stmt_num||') '
2727                 || p_error_msg, 1, 2000));
2728     when e_inv_std_op then
2729         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2730         fnd_message.set_token('FLD_NAME', 'standard_operation_id/standard_operation_code combination');
2731         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2732         p_error_code := -1;
2733         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2734     when e_invalid_min_trf_qty then
2735         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2736         fnd_message.set_token('FLD_NAME', 'minimum_transfer_quantity');
2737         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2738         p_error_code := -1;
2739         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2740     when e_invalid_count_point then
2741         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2742         fnd_message.set_token('FLD_NAME', 'count_point_type');
2743         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2744         p_error_code := -1;
2745         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2746     when e_invalid_backflush_flag then
2747         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2748         fnd_message.set_token('FLD_NAME', 'backflush_flag');
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_dup_op_seq then
2753         fnd_message.set_name('WSM','WSM_INVALID_OPERATION');
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_op_date then
2758         fnd_message.set_name('WSM','WSM_OP_BAD_DATES');
2759         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2760         p_error_code := -1;
2761         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2762     when e_invalid_routing_op_seq then
2763         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2764         fnd_message.set_token('FLD_NAME', 'Routing_op_seq_num');
2765         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2766         p_error_code := -1;
2767         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2768     when e_invalid_job_op_seq then
2769         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
2770         fnd_message.set_token('FLD_NAME', 'Job_op_seq_num');
2771         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '|| fnd_message.get;
2772         p_error_code := -1;
2773         fnd_file.put_line(fnd_file.log,substr('default_wljdi_fields: (#'||l_stmt_num||') '|| p_error_msg, 1, 2000));
2774 
2775     when others then
2776         p_error_code := -1;
2777         p_error_msg := 'Error: default_wljdi_fields: (#'||
2778                         l_stmt_num||') '||SUBSTR(SQLERRM, 1,240);
2779 
2780 END default_wljdi_fields;
2781 
2782 
2783 -- **********************************************************************************************
2784 -- VALIDATE_WLJDI_PATH: local validation procedure
2785 -- **********************************************************************************************
2786 
2787 -- a PL/SQL table is passed
2788 
2789 PROCEDURE validate_wljdi_path (
2790         p_wip_entity_id     in number,
2791         p_end_op_seq_num    in number,
2792         p_links             in t_links,
2793         x_error_code        out nocopy number,
2794         x_error_msg         out nocopy varchar2) is
2795 
2796 l_xst               number;
2797 l_stmt_num          number;
2798 l_idx               number;
2799 l_to_op             number;
2800 l_fm_op             number;
2801 l_cnt               number := 0;
2802 
2803 e_invalid_link          exception;
2804 e_invalid_path_end      exception;
2805 
2806 BEGIN
2807 
2808     x_error_code := 0;
2809     x_error_msg := '';
2810 
2811 l_stmt_num  := 10;
2812 
2813     l_idx := p_end_op_seq_num;
2814 
2815     if(p_links.count = 0) then
2816         return; -- this means every link is discommended
2817     elsif NOT(p_links.EXISTS(l_idx)) then
2818         raise e_invalid_path_end;
2819     end if;
2820 
2821     while (p_links.EXISTS(l_idx))
2822     loop
2823         l_cnt := l_cnt + 1;
2824 
2825         l_fm_op := p_links(l_idx).from_op_seq_num;
2826         l_to_op := p_links(l_idx).to_op_seq_num;
2827 
2828         -- allow l_fm_op to be null, this is for jumped op
2829         exit when (l_fm_op IS NULL);
2830         BEGIN
2831             select  1
2832             into    l_xst
2833             from    wsm_copy_op_networks
2834             where   wip_entity_id = p_wip_entity_id
2835             and     from_op_seq_num = l_fm_op
2836             and     to_op_seq_num = l_to_op;
2837         EXCEPTION
2838             when others then
2839                 raise e_invalid_link;
2840         END;
2841 
2842         l_idx := l_fm_op;
2843     end loop;
2844 
2845     if(l_cnt <>  p_links.COUNT) then
2846         x_error_code := -1;
2847         fnd_message.set_name('WSM','WSM_INVALID_EXTRA_LINKS');
2848         x_error_msg := fnd_message.get;
2849     end if;
2850 
2851 EXCEPTION
2852     when e_invalid_path_end then
2853         x_error_code := -1;
2854         fnd_message.set_name('WSM','WSM_COPY_NO_PATH_END');
2855         fnd_message.set_token('END',p_end_op_seq_num);
2856         x_error_msg := fnd_message.get;
2857 
2858     when e_invalid_link then
2859         x_error_code := -1;
2860         fnd_message.set_name('WSM','WSM_INVALID_RECO_LINK');
2861         x_error_msg := fnd_message.get;
2862 
2863     when others then
2864         x_error_code := -1;
2865         x_error_msg := 'Error: validate_wljdi_path: (#'|| l_stmt_num||') '
2866                        || SUBSTR(SQLERRM, 1,240);
2867 END validate_wljdi_path;
2868 
2869 
2870 -- **********************************************************************************************
2871 -- VALIDATE_WLJDI_OPERATION: local validation procedure
2872 -- **********************************************************************************************
2873 
2874 PROCEDURE validate_wljdi_operation(
2875         p_wip_entity_id             in number,
2876         p_org_id                    in number,
2877         p_v_ops                     in t_ops,
2878         p_job_scheduled_start_date  in date,
2879         p_job_scheduled_compl_date  in date,
2880         p_job_scheduled_qty         in number,
2881         p_handle_wip_cur_op         in varchar2,
2882         x_error_code                out nocopy number,
2883         x_error_msg                 out nocopy varchar2) is
2884 
2885 l_recommended           varchar2(1);
2886 l_xst                   number;
2887 l_stmt_num              number;
2888 l_i                     number;
2889 
2890 l_rtg_op_seq_num        number;
2891 l_job_op_seq_num        number;
2892 l_scheduled_qty         number;
2893 l_op_yield              number;
2894 l_op_start_date         date;
2895 l_op_compl_date         date;
2896 l_op_start_date_old     date;
2897 l_op_compl_date_old     date;
2898 l_job_start_date        date; -- bug 3423195
2899 
2900 /*Added for bugfix:7248992 */
2901 l_cum_yield             number;
2902 l_rev_cum_yield         number;
2903 
2904 
2905 e_invalid_operation     exception;
2906 e_invalid_sche_qty      exception;
2907 e_invalid_op_yield      exception;
2908 e_invalid_date          exception;
2909 e_sche_qty_not_upd      exception;
2910 /*Added for bugfix:7248992 */
2911 e_invalid_cum_yield      exception;
2912 e_invalid_rev_cum_yield  exception;
2913 
2914 BEGIN
2915     x_error_code := 0;
2916     x_error_msg := '';
2917 
2918     l_i := p_v_ops.first;
2919     if(l_i IS NULL) then return; end if;
2920     l_rtg_op_seq_num := p_v_ops(l_i).rtg_op_seq_num;
2921     l_job_op_seq_num := p_v_ops(l_i).job_op_seq_num;
2922     l_scheduled_qty  := p_v_ops(l_i).scheduled_quantity;
2923     l_op_yield       := p_v_ops(l_i).yield;
2924     l_op_start_date  := p_v_ops(l_i).start_date;
2925     l_op_compl_date  := p_v_ops(l_i).compltion_date;
2926      /*Added for bugfix:7248992 */
2927      l_cum_yield      := p_v_ops(l_i).cumulative_yield;
2928      l_rev_cum_yield  := p_v_ops(l_i).reverse_cumulative_yield;
2929 
2930 l_stmt_num  := 10;
2931     -- do not check p_routing_op_seq_num and p_op_yield for current operation
2932     if(p_handle_wip_cur_op = 'N') then
2933         -- validate operation_seq_num
2934         BEGIN
2935             select  recommended,
2936                     reco_start_date,
2937                     reco_completion_date
2938             into    l_recommended,
2939                     l_op_start_date_old,
2940                     l_op_compl_date_old
2941             from    wsm_copy_operations
2942             where   wip_entity_id = p_wip_entity_id
2943             and     operation_seq_num = l_rtg_op_seq_num;
2944         EXCEPTION
2945             when others then
2946                 raise e_invalid_operation;
2947         END;
2948         -- BD bug 3545531 we will allow user to nullify the dates on WCO
2949         --l_op_start_date := NVL(l_op_start_date, l_op_start_date_old);
2950         --l_op_compl_date := NVL(l_op_compl_date, l_op_compl_date_old);
2951         -- ED bug 3545531
2952 
2953 l_stmt_num  := 20;
2954         -- validate op yield
2955         if l_op_yield IS NOT NULL then
2956             if (l_op_yield <= 0 or l_op_yield > 1) then
2957                 raise e_invalid_op_yield;
2958             end if;
2959         end if;
2960 
2961  /*Added for bugfix:7248992,validate cumulative yield */
2962 	 if l_cum_yield IS NOT NULL then
2963 	    if (l_cum_yield <= 0 or l_cum_yield > 1) then
2964 	       raise e_invalid_cum_yield;
2965 	    end if;
2966 	 end if;
2967 	 /*Added for bugfix:6857061,validate reverse yield */
2968 	 if l_rev_cum_yield IS NOT NULL then
2969 	    if (l_rev_cum_yield <= 0 or l_rev_cum_yield > 1) then
2970 	       raise e_invalid_rev_cum_yield;
2971 	    end if;
2972 	 end if;
2973 
2974 l_stmt_num  := 30;
2975         -- validate op scheduled_quantity
2976         -- 1. if the current op is not on the path, should not allow this change
2977         -- 2. it should be less than job quantity
2978 
2979         if l_recommended <> 'Y' and l_scheduled_qty IS NOT NULL then
2980             raise e_sche_qty_not_upd;
2981         end if;
2982 
2983         if l_scheduled_qty IS NOT NULL then
2984             if l_scheduled_qty <= 0 or
2985                l_scheduled_qty > p_job_scheduled_qty
2986             then
2987                 raise e_invalid_sche_qty;
2988             end if;
2989         end if;
2990 
2991     else    -- p_handle_wip_cur_op = 'Y'
2992         BEGIN
2993 l_stmt_num  := 100;
2994             select  first_unit_start_date,
2995                     last_unit_start_date
2996             into    l_op_start_date_old,
2997                     l_op_compl_date_old
2998             from    wip_operations
2999             where   wip_entity_id = p_wip_entity_id
3000             and     operation_seq_num = l_job_op_seq_num;
3001         EXCEPTION
3002             when others then
3003                 raise e_invalid_operation;
3004         END;
3005         l_op_start_date := NVL(l_op_start_date, l_op_start_date_old);
3006         l_op_compl_date := NVL(l_op_compl_date, l_op_compl_date_old);
3007 
3008         if l_scheduled_qty IS NOT NULL then
3009             if l_scheduled_qty <= 0 or
3010                l_scheduled_qty > p_job_scheduled_qty
3011             then
3012                 raise e_invalid_sche_qty;
3013             end if;
3014         end if;
3015 
3016     end if; -- p_handle_wip_cur_op = 'Y'
3017 
3018 l_stmt_num  := 200;
3019     -- validate op dates, null means 'do not change'
3020     -- BA: bug 3423195
3021     if(g_charges_exist_shopfloor) then
3022         l_job_start_date := g_job_released_date;
3023     else
3024         l_job_start_date := p_job_scheduled_start_date;
3025     end if;
3026     -- EA: bug 3423195
3027 
3028     if(l_op_start_date IS NOT NULL and l_op_compl_date IS NOT NULL) then
3029         --if( NVL(p_job_scheduled_start_date, l_op_start_date) > l_op_start_date or
3030         if( NVL(l_job_start_date, l_op_start_date) > l_op_start_date or
3031             l_op_start_date > l_op_compl_date or
3032             l_op_compl_date > NVL(p_job_scheduled_compl_date, l_op_compl_date))
3033         then
3034             raise e_invalid_date;
3035         end if;
3036     elsif(l_op_start_date IS NOT NULL and l_op_compl_date IS NULL) then
3037         --if( NVL(p_job_scheduled_start_date, l_op_start_date) > l_op_start_date or
3038         if( NVL(l_job_start_date, l_op_start_date) > l_op_start_date or
3039             l_op_start_date > NVL(p_job_scheduled_compl_date, l_op_start_date))
3040         then
3041             raise e_invalid_date;
3042         end if;
3043     elsif(l_op_start_date IS NULL and l_op_compl_date IS NOT NULL) then
3044         --if( NVL(p_job_scheduled_start_date, l_op_compl_date) > l_op_compl_date or
3045         if( NVL(l_job_start_date, l_op_compl_date) > l_op_compl_date or
3046             l_op_compl_date > NVL(p_job_scheduled_compl_date, l_op_compl_date))
3047         then
3048             raise e_invalid_date;
3049         end if;
3050     end if;
3051 
3052 EXCEPTION
3053     when e_invalid_operation then
3054         x_error_code := -1;
3055         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
3056         fnd_message.set_token('FLD_NAME', 'job_op_seq_num or routing_op_seq_num');
3057         x_error_msg := fnd_message.get;
3058 
3059     when e_invalid_op_yield then
3060         x_error_code := -1;
3061         fnd_message.set_name('WSM','WSM_OP_BAD_YIELD');
3062         x_error_msg := fnd_message.get;
3063 
3064     when e_invalid_sche_qty then
3065         x_error_code := -1;
3066         fnd_message.set_name('WSM','WSM_OP_BAD_SCHQTY');
3067         x_error_msg := fnd_message.get;
3068 
3069     when e_sche_qty_not_upd then
3070         x_error_code := -1;
3071         fnd_message.set_name('WSM','WSM_SCHQTY_NOT_UPDATABLE');
3072         x_error_msg := fnd_message.get;
3073 
3074     when e_invalid_date then
3075         x_error_code := -1;
3076         fnd_message.set_name('WSM','WSM_OP_BAD_DATES');
3077         x_error_msg := fnd_message.get;
3078 
3079      /*Added for bugfix:7248992 */
3080      when e_invalid_cum_yield then
3081 	 x_error_code := -1;
3082 	 fnd_message.set_name('WSM','WSM_OP_BAD_CUM_YIELD');
3083 	 x_error_msg := fnd_message.get;
3084 
3085      /*Added for bugfix:7248992 */
3086      when e_invalid_rev_cum_yield then
3087 	 x_error_code := -1;
3088 	 fnd_message.set_name('WSM','WSM_OP_BAD_REV_CUM_YIELD');
3089 	 x_error_msg := fnd_message.get;
3090 
3091     when others then
3092         x_error_code := -1;
3093         x_error_msg := 'Error: validate_wljdi_operation: (#'||l_stmt_num||') '
3094                        || SUBSTR(SQLERRM, 1,240);
3095 END validate_wljdi_operation;
3096 
3097 
3098 
3099 -- **********************************************************************************************
3100 -- VALIDATE_WLJDI_OP_RESOURCE: local validation procedure
3101 -- **********************************************************************************************
3102 
3103 PROCEDURE validate_wljdi_op_resource(
3104         p_wip_entity_id         in number,
3105         p_org_id                in number,
3106         p_v_res                 in out nocopy t_op_res,         -- indexed by resource_id
3107         p_handle_wip_cur_op     in varchar2,
3108         p_substitution_type     in number,
3109         x_error_code            out nocopy number,
3110         x_error_msg             out nocopy varchar2) is
3111 
3112 l_xst                   number;
3113 l_stmt_num              number;
3114 l_i                     number;
3115 l_j                     number;
3116 
3117 l_op_sdate              date;
3118 l_op_edate              date;
3119 l_sdate                 date;
3120 l_edate                 date;
3121 
3122 l_rtg_op_seq_num        number;
3123 l_job_op_seq_num        number;
3124 l_sub_grp_num           number;
3125 l_rpl_grp_num           number;
3126 l_resource_id           number;
3127 
3128 l_v_res_ids             t_number;
3129 e_invalid_resource      exception;
3130 e_invalid_rpl_grp       exception;
3131 e_invalid_date          exception;
3132 
3133 /* ST : Detailed Scheduling */
3134 l_setup_id              number;
3135 l_setup_code            VARCHAR2(10);
3136 l_max_assg_units        number;
3137 l_assg_units            number;
3138 l_batch_id              number;
3139 l_seq_id                number;
3140 l_seq_num               number;
3141 l_firm_type             number;
3142 
3143 e_invalid_seq                   exception;
3144 e_invalid_batch_max_assg_units  exception;
3145 e_invalid_max_assg_units        exception;
3146 e_invalid_setup_max_assg_units  exception;
3147 e_invalid_setup_id              exception;
3148 e_invalid_setup_code            exception;
3149 e_invalid_firm_type             exception;
3150 e_res_not_batchable             exception;
3151 /* ST : Detailed Scheduling */
3152 
3153  ---Added for Alcoa Enhancement
3154 
3155 l_app_res_units         number;
3156 l_res_seq_num           number;
3157 l_uom_code              VARCHAR2(5);
3158 l_uom_exist             number;
3159 l_autocharge_type       number;
3160 l_autocharge_exist      number;
3161 l_res_valid             number;
3162 l_sched_flag            number;
3163 l_sch_flag              number;
3164 l_basis_type            number;
3165 l_activity_id           number;
3166 l_res_code              varchar2(10);
3167 l_res_unit_of_measure     varchar2(3);
3168 l_res_default_basis_type  number;
3169 l_res_default_activity_id number;
3170 l_res_autocharge_type     number;
3171 l_res_standard_rate_flag  number;
3172 l_res_batchable           number;
3173 l_check_primary           number;
3174 e_res_id_null                   exception;
3175 e_assgnd_units_null             exception;
3176 e_invalid_uom                   exception;  ---Need to Verify as this not used
3177 e_invalid_autocharge            exception; ---Need to Verify as this not used
3178 e_invalid_field                 exception;  ---Need to Verify as this not used
3179 e_inv_appl_res_units             exception;
3180 e_wt_or_wcit_rec_exists          exception;
3181 e_po_req_rec_exists              exception;
3182 e_invalid_res_sub_grp_num        exception;
3183 e_invalid_res_reco_flag          exception;
3184 e_inv_res_basis_type            exception;
3185 e_inv_res_charge_type           exception;
3186 e_inv_res_sch_flag              exception;
3187 e_invalid_res_seq_num           exception;
3188 e_invalid_usg_rate              exception;
3189 e_invalid_res_dept              exception;
3190 e_inv_resource_charge_type      exception;
3191 
3192 
3193 cursor c_wsm_rpl_grp is
3194         select   resource_id
3195         from     wsm_copy_op_resources
3196         where    wip_entity_id         = p_wip_entity_id
3197         and      operation_seq_num     = l_rtg_op_seq_num
3198         and      NVL(substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3199         and      NVL(replacement_group_num, 0) = NVL(l_rpl_grp_num, 0)
3200         and      parent_resource_seq_num IS NULL;  -- ST : Detailed Scheduling.. Consider only the runtime resources
3201 
3202 cursor c_wip_rpl_grp is
3203         select   resource_id
3204         from     wip_operation_resources
3205         where    wip_entity_id         = p_wip_entity_id
3206         and      operation_seq_num     = l_job_op_seq_num
3207         and      NVL(substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3208         and      NVL(replacement_group_num, 0) = NVL(l_rpl_grp_num, 0)
3209         and      parent_resource_seq IS NULL -- -- ST : Detailed Scheduling.. Consider only the runtime resources
3210         union
3211         select   resource_id
3212         from     wip_sub_operation_resources
3213         where    wip_entity_id         = p_wip_entity_id
3214         and      operation_seq_num     = l_job_op_seq_num
3215         and      NVL(substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3216         and      NVL(replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
3217 
3218 BEGIN
3219 
3220 
3221     x_error_code := 0;
3222     x_error_msg := '';
3223     l_i := p_v_res.first;
3224 
3225     if(l_i IS NULL) then return; end if;
3226 
3227     l_rtg_op_seq_num := p_v_res(l_i).rtg_op_seq_num;
3228     l_job_op_seq_num := p_v_res(l_i).job_op_seq_num;
3229     l_sub_grp_num    := p_v_res(l_i).sub_grp_num;
3230     l_rpl_grp_num    := p_v_res(l_i).rpl_grp_num;
3231     --Alcoa Enhancement changes
3232     l_app_res_units  := 0;
3233     l_res_seq_num    := p_v_res(l_i).res_seq_num ;
3234 
3235     IF lbji_debug = 'Y' then
3236         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);
3237     END IF;
3238 
3239     -- get op start/completion date
3240     if( p_handle_wip_cur_op = 'Y') then
3241 l_stmt_num  := 10;
3242         -- validate job operation_seq_num
3243         select  wo.first_unit_start_date,
3244                 wo.last_unit_completion_date
3245         into    l_op_sdate,
3246                 l_op_edate
3247         from    wip_operations wo
3248         where   wo.wip_entity_id     = p_wip_entity_id
3249         and     wo.operation_seq_num = l_job_op_seq_num;
3250     else    -- p_handle_wip_cur_op = 'N'
3251 l_stmt_num  := 20;
3252         select  reco_start_date,
3253                 reco_completion_date
3254         into    l_op_sdate,
3255                 l_op_edate
3256         from    wsm_copy_operations
3257         where   wip_entity_id     = p_wip_entity_id
3258         and     operation_seq_num = l_rtg_op_seq_num;
3259     end if;
3260 
3261     IF lbji_debug = 'Y' then
3262         fnd_file.put_line(fnd_file.log,'Validate_op_resource : ');
3263     END IF;
3264 
3265     if(p_substitution_type = 3) then    -- update only
3266         l_resource_id := p_v_res(l_i).resource_id;
3267 
3268         if( p_handle_wip_cur_op = 'Y') then
3269             BEGIN
3270                 l_xst := 0;
3271 l_stmt_num  := 30;
3272                 select  1
3273                 into    l_xst
3274                 from    dual
3275                 where   exists (
3276                             select resource_id
3277                             from   wip_operation_resources wor
3278                             where  wor.wip_entity_id = p_wip_entity_id
3279                             and    wor.operation_seq_num = l_job_op_seq_num
3280                             and    wor.resource_id = l_resource_id
3281                             and    NVL(wor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3282                             and    NVL(wor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0)
3283                         ) or exists (
3284                             select resource_id
3285                             from   wip_sub_operation_resources wsor
3286                             where  wsor.wip_entity_id = p_wip_entity_id
3287                             and    wsor.operation_seq_num = l_job_op_seq_num
3288                             and    wsor.resource_id = l_resource_id
3289                             and    NVL(wsor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3290                             and    NVL(wsor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0)
3291                         );
3292                 if (l_xst = 0) then
3293                     raise e_invalid_resource;
3294                 end if;
3295             EXCEPTION
3296                 when others then
3297                     raise e_invalid_resource;
3298             END;
3299 
3300         else -- p_handle_wip_cur_op = 'N'
3301 l_stmt_num  := 40;
3302             BEGIN
3303                 l_xst := 0;
3304                 select  1
3305                 into    l_xst
3306                 from    wsm_copy_op_resources wcor
3307                 where   wcor.wip_entity_id        = p_wip_entity_id
3308                 and     wcor.operation_seq_num    = l_rtg_op_seq_num
3309                 and     wcor.resource_id          = l_resource_id
3310                 and     NVL(wcor.substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
3311                 and     NVL(wcor.replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
3312             EXCEPTION
3313                 when no_data_found then
3314                     raise e_invalid_resource;
3315                 when others then
3316                     null;
3317             END;
3318         end if;
3319 
3320 l_stmt_num  := 50;
3321         -- validate dates
3322         l_sdate := p_v_res(l_i).start_date;
3323         l_edate := p_v_res(l_i).completion_date;
3324 
3325         if(l_sdate IS NOT NULL and l_edate IS NOT NULL) then
3326             if( NVL(l_op_sdate, l_sdate) > l_sdate or
3327                 l_sdate > l_edate or
3328                 l_edate > NVL(l_op_edate, l_edate))
3329             then
3330                 raise e_invalid_date;
3331             end if;
3332         elsif(l_sdate IS NOT NULL and l_edate IS NULL) then
3333             if( NVL(l_op_sdate, l_sdate) > l_sdate or
3334                 l_sdate > NVL(l_op_edate, l_sdate))
3335             then
3336                 raise e_invalid_date;
3337             end if;
3338         elsif(l_sdate IS NULL and l_edate IS NOT NULL) then
3339             if( NVL(l_op_sdate, l_edate) > l_edate or
3340                 l_edate > NVL(l_op_edate, l_edate))
3341             then
3342                 raise e_invalid_date;
3343             end if;
3344         end if;
3345 
3346         /* ST : Detailed Scheduling : Place the validations here.....
3347            Firm_type        Default: Null
3348            Setup_id         Default : Null.
3349 
3350                             Check if there exists any record in bom_resource_setups
3351                             for the setup_id and resource_id.If Setup Id is not null,
3352                             assigned units and Max Assigned units should be 1.
3353 
3354            Batch Id          Default: Null
3355                              If not null, assigned units and Max Assigned units should be 1.
3356 
3357           Sequence Number    Default: Null
3358           Sequence ID
3359                              Is applicable only when setup type is not null.
3360 
3361            Maximum Assigned Units  Default:assigned_units.Max_assigned_units should be greater than or equal to assigned units.
3362         */
3363 
3364         l_setup_id       := p_v_res(l_i).setup_id;
3365         l_setup_code     := p_v_res(l_i).setup_code;
3366         l_max_assg_units := p_v_res(l_i).max_assg_units;
3367         l_batch_id       := p_v_res(l_i).batch_id;
3368         l_seq_id         := p_v_res(l_i).grp_seq_id;
3369         l_seq_num        := p_v_res(l_i).grp_seq_num;
3370         l_assg_units     := p_v_res(l_i).assigned_units;
3371         l_firm_type      := p_v_res(l_i).firm_type;
3372 
3373 		-- bug fix 5471266: Start
3374         -- This allows max_assg_units and firm_type to be passed as null.
3375 
3376                 if l_firm_type is null then
3377                     l_firm_type:=0;
3378                 end if;
3379 
3380                 if l_max_assg_units is null then
3381                     l_max_assg_units := l_assg_units;
3382                 end if;
3383         -- bug fix 5471266: End
3384 
3385 
3386         if nvl(l_firm_type,-1) < 0 or  nvl(l_firm_type,-1)>7 then
3387                 raise e_invalid_firm_type;
3388         end if;
3389         if l_setup_id is NULL and l_setup_code is NOT NULL then
3390              begin
3391                     select  bst.setup_id
3392                     into    l_setup_id
3393                     from    BOM_SETUP_TYPES bst
3394                     where   bst.setup_code      = l_setup_code
3395                     and     bst.organization_id = p_org_id;
3396 
3397              exception
3398                 when no_data_found then
3399                         raise e_invalid_setup_code;
3400                     when others then
3401                         null;
3402              end;
3403         end if;
3404 
3405         if l_setup_id is not null then
3406                 BEGIN
3407 
3408                     l_xst := 0;
3409 
3410                     select  1
3411                     into    l_xst
3412                     from    bom_resource_setups brs
3413                     where   brs.resource_id     = l_resource_id
3414                     and     brs.organization_id = p_org_id
3415                     and     brs.setup_id        = l_setup_id;
3416 
3417                 EXCEPTION
3418                     when no_data_found then
3419                         raise e_invalid_setup_id;
3420                     when others then
3421                         null;
3422                 END;
3423         end if;
3424 
3425         if (l_batch_id is not null ) then
3426 
3427                 /* add a check if the resource is batchable.... */
3428                 begin
3429                     l_xst := 0;
3430 
3431                     select br.batchable
3432                     into l_xst
3433                     from bom_resources br
3434                     where resource_id = l_resource_id;
3435 
3436                     if l_xst <> 1 then
3437                         /* error out... */
3438                         raise e_res_not_batchable;
3439                     end if;
3440 
3441                 exception
3442                     when others then
3443                         /* error out.. */
3444                         raise e_res_not_batchable;
3445                 end;
3446 
3447                 if (l_max_assg_units <> 1 or l_assg_units <> 1) then
3448                     raise e_invalid_batch_max_assg_units;
3449                 end if;
3450         end if;
3451 
3452         if (l_seq_num is not null and l_seq_id is not null) then
3453 
3454                 if (l_setup_id is null)
3455                 then
3456                      raise e_invalid_seq;
3457                 end if;
3458 
3459                 if (l_setup_id is not null)
3460                    and
3461                    (l_max_assg_units <> 1 or l_assg_units <> 1)
3462                 then
3463                      raise e_invalid_setup_max_assg_units;
3464 
3465                 end if;
3466         end if;
3467 
3468         if nvl(l_max_assg_units,0) < l_assg_units then
3469                 raise e_invalid_max_assg_units;
3470         end if;
3471 
3472         /* ST : Detailed Scheduling end */
3473 
3474     elsif(p_substitution_type = 4) then    -- recommand
3475 
3476         if( p_handle_wip_cur_op = 'Y') then
3477 l_stmt_num  := 60;
3478             -- get all the resource_id in the same replacement group
3479             open c_wip_rpl_grp;
3480             fetch c_wip_rpl_grp bulk collect into l_v_res_ids;
3481             if(c_wip_rpl_grp%rowcount <=0) then
3482                 raise e_invalid_resource;
3483             end if;
3484             close c_wip_rpl_grp;
3485         else
3486 l_stmt_num  := 70;
3487             -- get all the resource_id in the same replacement group
3488             open c_wsm_rpl_grp;
3489             fetch c_wsm_rpl_grp bulk collect into l_v_res_ids;
3490             if(c_wsm_rpl_grp%rowcount <=0) then
3491                 raise e_invalid_resource;
3492             end if;
3493             close c_wsm_rpl_grp;
3494         end if;
3495 
3496 l_stmt_num  := 80;
3497         if(l_v_res_ids.COUNT > p_v_res.COUNT) then
3498             raise e_invalid_rpl_grp;
3499         elsif(l_v_res_ids.COUNT < p_v_res.COUNT) then   -- BUG3600450
3500             raise e_invalid_resource;                   -- BUG3600450
3501         end if;
3502 
3503         l_j := l_v_res_ids.first;
3504         while (l_j IS NOT NULL)
3505         loop
3506 l_stmt_num  := 90;
3507             l_resource_id := l_v_res_ids(l_j);
3508             if(NOT p_v_res.EXISTS(l_resource_id)) then
3509                 --raise e_invalid_rpl_grp;              -- BUG3600450
3510                 raise e_invalid_resource;               -- BUG3600450
3511             end if;
3512 
3513 l_stmt_num  := 100;
3514 
3515             -- validate dates
3516             l_sdate := p_v_res(l_resource_id).start_date;
3517             l_edate := p_v_res(l_resource_id).completion_date;
3518 
3519             if(l_sdate IS NOT NULL and l_edate IS NOT NULL) then
3520                 if( NVL(l_op_sdate, l_sdate) > l_sdate or
3521                     l_sdate > l_edate or
3522                     l_edate > NVL(l_op_edate, l_edate))
3523                 then
3524                     raise e_invalid_date;
3525                 end if;
3526             elsif(l_sdate IS NOT NULL and l_edate IS NULL) then
3527                 if( NVL(l_op_sdate, l_sdate) > l_sdate or
3528                     l_sdate > NVL(l_op_edate, l_sdate))
3529                 then
3530                     raise e_invalid_date;
3531                 end if;
3532             elsif(l_sdate IS NULL and l_edate IS NOT NULL) then
3533                 if( NVL(l_op_sdate, l_edate) > l_edate or
3534                     l_edate > NVL(l_op_edate, l_edate))
3535                 then
3536                     raise e_invalid_date;
3537                 end if;
3538             end if;
3539 
3540         l_stmt_num := 110;
3541         /* ST : Detailed Scheduling : Place the validations here.....
3542            Firm_type        Default: Null
3543            Setup_id         Default : Null.
3544 
3545                             Check if there exists any record in bom_resource_setups
3546                             for the setup_id and resource_id.If Setup Id is not null,
3547                             assigned units and Max Assigned units should be 1.
3548 
3549            Batch Id          Default: Null
3550                              If not null, assigned units and Max Assigned units should be 1.
3551 
3552           Sequence Number    Default: Null
3553           Sequence ID
3554                              Is applicable only when setup type is not null.
3555 
3556            Maximum Assigned Units  Default:assigned_units.Max_assigned_units should be greater than or equal to assigned units.
3557         */
3558 
3559 
3560         l_setup_id    := p_v_res(l_resource_id).setup_id;
3561         l_setup_code    := p_v_res(l_resource_id).setup_code;
3562         l_max_assg_units := p_v_res(l_resource_id).max_assg_units;
3563         l_batch_id    := p_v_res(l_resource_id).batch_id;
3564         l_seq_id      := p_v_res(l_resource_id).grp_seq_id;
3565         l_seq_num     := p_v_res(l_resource_id).grp_seq_num;
3566         l_assg_units  := p_v_res(l_resource_id).assigned_units;
3567         l_firm_type      := p_v_res(l_resource_id).firm_type;
3568 
3569         -- bug fix 5471266: Start
3570         -- This allows max_assg_units and firm_type to be passed as null.
3571 
3572                 if l_firm_type is null then
3573                     l_firm_type:=0;
3574                 end if;
3575 
3576                 if l_max_assg_units is null then
3577                     l_max_assg_units := l_assg_units;
3578                 end if;
3579         -- bug fix 5471266: End
3580 
3581 		if nvl(l_firm_type,-1) < 0 or  nvl(l_firm_type,-1)>7 then
3582                 raise e_invalid_firm_type;
3583         end if;
3584 
3585         l_stmt_num := 119.99;
3586          if l_setup_id is NULL and l_setup_code is NOT NULL then
3587              begin
3588                     select  bst.setup_id
3589                     into    l_setup_id
3590                     from    bom_setup_types bst
3591                     where   bst.setup_code      = l_setup_code
3592                     and     bst.organization_id = p_org_id;
3593 
3594              exception
3595                 when no_data_found then
3596                         raise e_invalid_setup_code;
3597                     when others then
3598                         null;
3599              end;
3600         end if;
3601 
3602         l_stmt_num := 120;
3603         if l_setup_id is not null then
3604             BEGIN
3605 
3606                 l_stmt_num := 130;
3607 
3608                 l_xst := 0;
3609 
3610                 select  1
3611                 into    l_xst
3612                 from    bom_resource_setups brs
3613                 where   brs.resource_id     = l_resource_id
3614                 and     brs.organization_id = p_org_id
3615                 and     brs.setup_id        = l_setup_id;
3616 
3617             EXCEPTION
3618                 when no_data_found then
3619                     raise e_invalid_setup_id;
3620                 when others then
3621                     null;
3622             END;
3623        end if;
3624        if (l_batch_id is not null ) then
3625 
3626                 /* add a check if the resource is batchable.... */
3627                 begin
3628                     l_xst := 0;
3629 
3630                     l_stmt_num := 140;
3631 
3632                     select br.batchable
3633                     into l_xst
3634                     from bom_resources br
3635                     where resource_id = l_resource_id;
3636 
3637                     if l_xst <> 1 then
3638                         /* error out... */
3639                         raise e_res_not_batchable;
3640                     end if;
3641 
3642                 exception
3643                     when others then
3644                         /* error out.. */
3645                         raise e_res_not_batchable;
3646                 end;
3647 
3648                 if (l_max_assg_units <> 1 or l_assg_units <> 1) then
3649                     raise e_invalid_batch_max_assg_units;
3650                 end if;
3651 
3652        end if;
3653        l_stmt_num := 150;
3654 
3655        if (l_seq_num is not null and l_seq_id is not null) then
3656 
3657                 if (l_setup_id is null)
3658                 then
3659                      raise e_invalid_seq;
3660                 end if;
3661 
3662                 if (l_setup_id is not null)
3663                    and
3664                    (l_max_assg_units <> 1 or l_assg_units <> 1)
3665                 then
3666                      raise e_invalid_setup_max_assg_units;
3667 
3668                 end if;
3669 
3670         end if;
3671 
3672         if nvl(l_max_assg_units,0) < l_assg_units then
3673                 raise e_invalid_max_assg_units;
3674         end if;
3675         /* ST : Detailed Scheduling end */
3676 
3677         l_j := l_v_res_ids.next(l_j);
3678       end loop;
3679     --Alcoa enahancement Start : Delete
3680     elsif(p_substitution_type = WSM_SUB_DEL) then    -- Delete only
3681         l_resource_id := p_v_res(l_i).resource_id;
3682 
3683         if l_res_seq_num is null then
3684             raise e_invalid_resource;
3685         end if;
3686 
3687         if( p_handle_wip_cur_op = 'Y') THEN
3688 
3689             BEGIN
3690                 l_xst := 0;
3691 
3692                 select 1,applied_resource_units
3693                 into l_xst,l_app_res_units
3694                 from   wip_operation_resources wor
3695                 where  wor.wip_entity_id = p_wip_entity_id
3696                 and    wor.operation_seq_num = l_job_op_seq_num
3697                 and    wor.resource_seq_num = l_res_seq_num;
3698 
3699                 if (l_xst=0) then
3700                     select 1,applied_resource_units
3701                     into l_xst,l_app_res_units
3702                     from   wip_sub_operation_resources wsor
3703                     where  wsor.wip_entity_id = p_wip_entity_id
3704                     and    wsor.operation_seq_num = l_job_op_seq_num
3705                     and    wsor.resource_seq_num = l_res_seq_num;
3706                 end if;
3707                 if (l_xst = 0) then
3708                     raise e_invalid_resource;
3709                 end if;
3710 
3711             EXCEPTION
3712                 when others then
3713                     raise e_invalid_resource;
3714             END;
3715 
3716             IF l_app_res_units <> 0 THEN
3717                raise e_inv_appl_res_units ;
3718             END IF  ;
3719 
3720             BEGIN -- Check whether there are any records in WT,WCTI
3721 
3722                 l_xst := 0;
3723 
3724                 select  1
3725                 into    l_xst
3726                 from    dual
3727                 where   exists ( select transaction_id
3728                                  from   wip_transactions wt
3729                                  where  wt.wip_entity_id      = p_wip_entity_id
3730                                  and    wt.organization_id    = p_org_id
3731                                  AND    wt.operation_seq_num  = l_job_op_seq_num
3732                                  and    wt.resource_seq_num   = l_res_seq_num
3733                                )
3734                      or exists ( select transaction_id
3735                                  from  wip_cost_txn_interface wcti
3736                                  where wcti.wip_entity_id     = p_wip_entity_id
3737                                  AND   wcti.organization_id   = p_org_id
3738                                  and   wcti.operation_seq_num = l_job_op_seq_num
3739                                  and   wcti.resource_seq_num  = l_res_seq_num
3740                         );
3741 
3742                 if (l_xst = 1 ) then
3743                     raise e_wt_or_wcit_rec_exists;
3744                 end if;
3745             EXCEPTION
3746                 when no_data_found then
3747                   null;
3748                 when others then
3749                     raise e_wt_or_wcit_rec_exists;
3750             END;
3751 
3752             BEGIN -- Check whether there are any PO/REQ Linked transactions
3753 
3754                 l_xst := 0;
3755 
3756                 select  1
3757                 into    l_xst
3758                 from    dual
3759                 where   exists ( select 'PO/REQ Linked'
3760                                  from    po_releases_all pr,
3761                                          po_headers_all ph,
3762                                          po_distributions_all pd,
3763                                          po_line_locations_all pll
3764                                  where  pd.po_line_id IS NOT NULL
3765                                  and    pd.line_location_id IS NOT NULL
3766                                  and    pd.wip_entity_id = p_wip_entity_id
3767                                  and    pd.destination_organization_id = p_org_id
3768                                  and    pd.wip_operation_seq_num = l_job_op_seq_num
3769                                  and    pd.wip_resource_seq_num = l_res_seq_num
3770                                  and    ph.po_header_id         = pd.po_header_id
3771                                  and    pll.line_location_id    = pd.line_location_id
3772                                  and    pr.po_release_id (+)    = pd.po_release_id
3773                                  and  ( pll.cancel_flag IS NULL OR
3774                                           pll.cancel_flag = 'N')
3775                                  and (pll.quantity_received <   (pll.quantity-pll.quantity_cancelled))
3776                                  and nvl(pll.closed_code,'OPEN') <> 'FINALLY CLOSED'
3777                                  UNION ALL
3778                                  select 'PO/REQ Linked'
3779                                  from   po_requisition_lines_all prl
3780                                  where  prl.wip_entity_id = p_wip_entity_id
3781                                  and    prl.destination_organization_id = p_org_id
3782                                  and    prl.wip_operation_seq_num = l_job_op_seq_num
3783                                  and    prl.wip_resource_seq_num  = l_res_seq_num
3784                                  and    nvl(PRL.cancel_flag, 'N') = 'N'
3785                                  and    prl.line_location_id is NULL
3786                                  UNION ALL
3787                                  select 'PO/REQ Linked'
3788                                  from   po_requisitions_interface_all pri
3789                                  where  pri.wip_entity_id = p_wip_entity_id
3790                                  and    pri.destination_organization_id = p_org_id
3791                                  and    pri.wip_operation_seq_num = l_job_op_seq_num
3792                                  and pri.wip_resource_seq_num = l_res_seq_num
3793                                );
3794 
3795                 if (l_xst = 1) then
3796                     raise e_po_req_rec_exists;
3797                 end if;
3798 
3799             EXCEPTION
3800                 when no_data_found then
3801                     null;
3802                 when others then
3803                     raise e_po_req_rec_exists;
3804             END;
3805 
3806         else -- p_handle_wip_cur_op = 'N'
3807 
3808             BEGIN
3809                 l_xst := 0;
3810                 select  1
3811                 into    l_xst
3812                 from    wsm_copy_op_resources wcor
3813                 where   wcor.wip_entity_id        = p_wip_entity_id
3814                 and     wcor.operation_seq_num    = l_rtg_op_seq_num
3815                 and     wcor.resource_seq_num = l_res_seq_num;
3816             EXCEPTION
3817                 when no_data_found then
3818                     raise e_invalid_resource;
3819                 when others then
3820                     null;
3821             END;
3822         end if;
3823         --Alcoa enahancement End : Delete
3824 
3825     elsif (p_substitution_type = WSM_SUB_ADD) then
3826 
3827         l_resource_id    := p_v_res(l_i).resource_id;
3828         l_assg_units     := p_v_res(l_i).assigned_units;
3829         l_setup_id       := p_v_res(l_i).setup_id;
3830         l_setup_code     := p_v_res(l_i).setup_code;
3831         l_max_assg_units := p_v_res(l_i).max_assg_units;
3832         l_batch_id       := p_v_res(l_i).batch_id;
3833         l_seq_id         := p_v_res(l_i).grp_seq_id;
3834         l_seq_num        := p_v_res(l_i).grp_seq_num;
3835         l_assg_units     := p_v_res(l_i).assigned_units;
3836         l_sch_flag       := p_v_res(l_i).scheduled_flag;
3837         l_basis_type     := p_v_res(l_i).basis_type;
3838         l_activity_id    := p_v_res(l_i).activity_id;
3839         l_firm_type      := p_v_res(l_i).firm_type;
3840 
3841         --Validating the Resource
3842         if l_resource_id  is not null then
3843             begin
3844                 select  unit_of_measure,
3845                         default_basis_type,
3846                         default_activity_id,
3847                         autocharge_type,
3848                         standard_rate_flag,
3849                         batchable,
3850                         resource_code
3851                 into    l_res_unit_of_measure,
3852                         l_res_default_basis_type,
3853                         l_res_default_activity_id,
3854                         l_res_autocharge_type,
3855                         l_res_standard_rate_flag,
3856                         l_res_batchable,
3857                         l_res_code
3858                 from    bom_resources
3859                 where   resource_id=l_resource_id
3860                 and     organization_id=p_org_id
3861                 and     nvl(disable_date,sysdate+1) > sysdate;
3862             exception
3863                 when others then
3864                     raise e_invalid_resource;
3865             end;
3866         else
3867             raise e_res_id_null;
3868         end if ;
3869 
3870         if l_activity_id is null then
3871             l_activity_id := l_res_default_activity_id;
3872         end if;
3873 
3874         if l_basis_type is null then
3875             l_basis_type := l_res_default_basis_type;
3876         elsif l_basis_type not in (WIP_CONSTANTS.PER_ITEM,
3877                                    WIP_CONSTANTS.PER_LOT) then
3878             raise e_inv_res_basis_type;
3879         end if;
3880 
3881         if l_res_autocharge_type = WIP_CONSTANTS.PO_MOVE then
3882             raise e_inv_res_charge_type;
3883         end if;
3884 
3885         if l_res_autocharge_type is null then
3886             raise e_inv_resource_charge_type;
3887         end if;
3888 
3889         if l_sch_flag is null then
3890             l_sch_flag := WIP_CONSTANTS.SCHED_NO;
3891         elsif l_sch_flag not in (WIP_CONSTANTS.SCHED_YES,
3892                                  WIP_CONSTANTS.SCHED_NO,
3893                                  WIP_CONSTANTS.SCHED_PRIOR,
3894                                  WIP_CONSTANTS.SCHED_NEXT) then
3895             raise e_inv_res_sch_flag;
3896         end if;
3897 
3898         if l_assg_units <=0 or l_assg_units is null then
3899                 raise e_assgnd_units_null;
3900         end if;
3901 
3902         if p_v_res(l_i).usage_rate_or_amount <=0 or p_v_res(l_i).usage_rate_or_amount is null then
3903             raise e_invalid_usg_rate;
3904         end if;
3905 
3906         if l_firm_type is null then
3907             l_firm_type:=0;
3908         end if;
3909 
3910         if l_max_assg_units is null then
3911             l_max_assg_units := l_assg_units;
3912         end if;
3913 
3914         if l_firm_type < 0 or l_firm_type > 7 then
3915             raise e_invalid_firm_type;
3916         end if;
3917 
3918         -- ignore setup_id/setup_code for runtime resource.
3919         if l_setup_id is not null and l_setup_code is not null then
3920             l_setup_id   := null;
3921             l_setup_code := null;
3922         end if;
3923 
3924         if l_batch_id is not null then
3925             if l_res_batchable <> 1 then
3926                 raise e_res_not_batchable;
3927             end if;
3928             if (l_max_assg_units <> 1 or l_assg_units <> 1) then
3929                     raise e_invalid_batch_max_assg_units;
3930             end if;
3931         end if;
3932 
3933         if l_seq_num is not null and l_seq_id is not null then
3934             raise e_invalid_seq;
3935         end if;
3936 
3937         if l_max_assg_units < l_assg_units then
3938                 raise e_invalid_max_assg_units;
3939         end if;
3940 
3941         l_sdate := p_v_res(l_i).start_date;
3942         l_edate := p_v_res(l_i).completion_date;
3943         l_op_sdate := null;
3944         l_op_edate := null;
3945         -- get op start/completion date
3946         if p_handle_wip_cur_op = 'Y' then
3947 l_stmt_num  := 10;
3948 
3949             begin
3950                 select  wo.first_unit_start_date,
3951                         wo.last_unit_completion_date
3952                 into    l_op_sdate,
3953                         l_op_edate
3954                 from    wip_operations wo,
3955                         bom_department_resources bdr
3956                 where   wo.wip_entity_id     = p_wip_entity_id
3957                 and     wo.operation_seq_num = l_job_op_seq_num
3958                 and     wo.organization_id   = p_org_id
3959                 and     bdr.resource_id      = l_resource_id
3960                 and     bdr.department_id    = wo.department_id;
3961             exception
3962                 when others then
3963                     raise e_invalid_res_dept;
3964             end;
3965             if l_res_seq_num is null then
3966                 select greatest(nvl(max(wor.resource_seq_num),0),nvl(max(wsor.resource_seq_num),0))+10
3967                 into   l_res_seq_num
3968                 from   wip_operation_resources wor,
3969                        wip_sub_operation_resources wsor
3970                 where  wor.organization_id = p_org_id
3971                 and    wor.wip_entity_id   = p_wip_entity_id
3972                 and    wor.operation_seq_num   = l_job_op_seq_num
3973                 and    wor.organization_id = wsor.organization_id
3974                 and    wor.wip_entity_id   = wsor.wip_entity_id
3975                 and    wor.operation_seq_num   = wsor.operation_seq_num;
3976             else
3977                 l_xst := 0;
3978                 begin
3979                     select 1
3980                     into   l_xst
3981                     from   wip_operation_resources wor
3982                     where  wor.organization_id = p_org_id
3983                     and    wor.wip_entity_id   = p_wip_entity_id
3984                     and    wor.operation_seq_num   = l_job_op_seq_num
3985                     and    wor.resource_seq_num  =l_res_seq_num;
3986                 exception
3987                     when no_data_found then
3988                         null;
3989                     when others then
3990                         raise e_invalid_res_seq_num;
3991                 end;
3992                 if l_xst=1 then
3993                     raise e_invalid_res_seq_num;
3994                 end if;
3995             end if;
3996 
3997             -- resource in the same sub/repl group should be recommended/decommended together.
3998             l_check_primary:=0;
3999             begin
4000                 select 'N'
4001                 into p_v_res(l_i).reco_flag
4002                 from dual
4003                 where exists( select 1
4004                               from wip_sub_operation_resources wsor
4005                               where wsor.organization_id   = p_org_id
4006                               and   wsor.wip_entity_id     = p_wip_entity_id
4007                               and   wsor.operation_seq_num = l_job_op_seq_num
4008                               and   nvl(wsor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4009                               and   nvl(wsor.replacement_group_num,0)  = nvl(p_v_res(l_i).rpl_grp_num,0));
4010             exception
4011                 when no_data_found then
4012                     l_check_primary := 1;
4013                 when others then
4014                     raise e_invalid_res_sub_grp_num;
4015             end;
4016             if l_check_primary=1 then
4017                 begin
4018                     select 'Y'
4019                     into p_v_res(l_i).reco_flag
4020                     from dual
4021                     where exists( select 1
4022                                   from wip_operation_resources wor
4023                                   where wor.organization_id   = p_org_id
4024                                   and   wor.wip_entity_id     = p_wip_entity_id
4025                                   and   wor.operation_seq_num = l_job_op_seq_num
4026                                   and   nvl(wor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4027                                   and   nvl(wor.replacement_group_num,0)  = nvl(p_v_res(l_i).rpl_grp_num,0));
4028                 exception
4029                     when no_data_found then
4030                         l_check_primary := 2;
4031                     when others then
4032                         raise e_invalid_res_sub_grp_num;
4033                 end;
4034             end if;
4035             if l_check_primary=2 then
4036                 begin
4037                     select 'N'
4038                     into p_v_res(l_i).reco_flag
4039                     from dual
4040                     where exists( select 1
4041                                   from wip_operation_resources wor
4042                                   where wor.organization_id   = p_org_id
4043                                   and   wor.wip_entity_id     = p_wip_entity_id
4044                                   and   wor.operation_seq_num = l_job_op_seq_num
4045                                   and   nvl(wor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1));
4046                 exception
4047                     when no_data_found then
4048                         p_v_res(l_i).reco_flag := 'Y';
4049                     when others then
4050                         raise e_invalid_res_sub_grp_num;
4051                 end;
4052             end if;
4053             if p_v_res(l_i).reco_flag ='N' then
4054                 if p_v_res(l_i).sub_grp_num is null then
4055                     raise e_invalid_res_sub_grp_num;
4056                 end if;
4057             end if;
4058 
4059         else    -- p_handle_wip_cur_op = 'N'
4060 l_stmt_num  := 20;
4061            begin
4062 
4063                select  reco_start_date,
4064                        reco_completion_date
4065                into    l_op_sdate,
4066                        l_op_edate
4067                from    wsm_copy_operations wco,
4068                        bom_department_resources bdr
4069                where   wip_entity_id     = p_wip_entity_id
4070                and     operation_seq_num = l_rtg_op_seq_num
4071                and     organization_id   = p_org_id
4072                and     bdr.resource_id      = l_resource_id
4073                and     bdr.department_id    =wco.department_id;
4074 
4075            exception
4076                 when others then
4077                     raise e_invalid_res_dept;
4078             end;
4079 
4080            if l_res_seq_num is null then
4081                 select nvl(max(resource_seq_num),0)+10
4082                 into   l_res_seq_num
4083                 from   wsm_copy_op_resources wor
4084                 where  wor.organization_id = p_org_id
4085                 and    wor.wip_entity_id   = p_wip_entity_id
4086                 and    wor.operation_seq_num   = l_rtg_op_seq_num;
4087             else
4088                 l_xst := 0;
4089                 begin
4090                     select 1
4091                     into   l_xst
4092                     from   wsm_copy_op_resources wor
4093                     where  wor.organization_id = p_org_id
4094                     and    wor.wip_entity_id   = p_wip_entity_id
4095                     and    wor.operation_seq_num   = l_rtg_op_seq_num
4096                     and    wor.resource_seq_num  =l_res_seq_num;
4097                 exception
4098                     when no_data_found then
4099                         null;
4100                     when others then
4101                         raise e_invalid_res_seq_num;
4102                 end;
4103                 if l_xst=1 then
4104                     raise e_invalid_res_seq_num;
4105                 end if;
4106 
4107             end if;
4108 
4109             -- resource in the same sub/repl group should be recommended/decommended together.
4110             l_check_primary:=0;
4111             begin
4112                 select 'N'
4113                 into p_v_res(l_i).reco_flag
4114                 from dual
4115                 where exists( select 1
4116                               from wsm_copy_op_resources wcor
4117                               where wcor.organization_id   = p_org_id
4118                               and   wcor.wip_entity_id     = p_wip_entity_id
4119                               and   wcor.operation_seq_num = l_rtg_op_seq_num
4120                               and   nvl(wcor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4121                               and   nvl(wcor.replacement_group_num,0)  = nvl(p_v_res(l_i).rpl_grp_num,0)
4122                               and   wcor.recommended           = 'N');
4123             exception
4124                 when no_data_found then
4125                     l_check_primary := 1;
4126                 when others then
4127                                                                                                                                                                 raise e_invalid_res_sub_grp_num;
4128             end;
4129 
4130             if l_check_primary=1 then
4131                 begin
4132                     select 'Y'
4133                     into p_v_res(l_i).reco_flag
4134                     from dual
4135                     where exists( select 1
4136                                   from wsm_copy_op_resources wcor
4137                                   where wcor.organization_id   = p_org_id
4138                                   and   wcor.wip_entity_id     = p_wip_entity_id
4139                                   and   wcor.operation_seq_num = l_rtg_op_seq_num
4140                                   and   nvl(wcor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4141                                   and   nvl(wcor.replacement_group_num,0)  = nvl(p_v_res(l_i).rpl_grp_num,0)
4142                                   and   wcor.recommended           = 'Y');
4143                 exception
4144                     when no_data_found then
4145                         l_check_primary := 2;
4146                     when others then
4147                                                                                                                                                                                                 raise e_invalid_res_sub_grp_num;
4148                 end;
4149             end if;
4150             if l_check_primary=2 then
4151                 begin
4152                     select 'N'
4153                     into p_v_res(l_i).reco_flag
4154                     from dual
4155                     where exists( select 1
4156                                   from wsm_copy_op_resources wcor
4157                                   where wcor.organization_id   = p_org_id
4158                                   and   wcor.wip_entity_id     = p_wip_entity_id
4159                                   and   wcor.operation_seq_num = l_rtg_op_seq_num
4160                                   and   nvl(wcor.substitute_group_num,-1)  = nvl(p_v_res(l_i).sub_grp_num,-1)
4161                                   and   wcor.recommended           = 'Y');
4162                 exception
4163                     when no_data_found then
4164                         p_v_res(l_i).reco_flag := 'Y';
4165                     when others then
4166                                                                                                                                                                                                 raise e_invalid_res_sub_grp_num;
4167                 end;
4168             end if;
4169             if p_v_res(l_i).reco_flag ='N' then
4170                 if p_v_res(l_i).sub_grp_num is null then
4171                     raise e_invalid_res_sub_grp_num;
4172                 end if;
4173             end if;
4174         end if;
4175 
4176         if(l_sdate IS NOT NULL and l_edate IS NOT NULL) then
4177             if( NVL(l_op_sdate, l_sdate) > l_sdate or
4178                 l_sdate > l_edate or
4179                 l_edate > NVL(l_op_edate, l_edate))
4180             then
4181                 raise e_invalid_date;
4182             end if;
4183         elsif(l_sdate IS NOT NULL and l_edate IS NULL) then
4184             if( NVL(l_op_sdate, l_sdate) > l_sdate or
4185                 l_sdate > NVL(l_op_edate, l_sdate))
4186             then
4187                 raise e_invalid_date;
4188             end if;
4189         elsif(l_sdate IS NULL and l_edate IS NOT NULL) then
4190             if( NVL(l_op_sdate, l_edate) > l_edate or
4191                 l_edate > NVL(l_op_edate, l_edate))
4192             then
4193                 raise e_invalid_date;
4194             end if;
4195         end if;
4196 
4197         if l_sdate is null then
4198             l_sdate := l_op_sdate;
4199         end if;
4200 
4201         if l_edate is null then
4202             l_edate := l_op_edate;
4203         end if;
4204 
4205         if p_v_res(l_i).rpl_grp_num is null then
4206             p_v_res(l_i).rpl_grp_num     := 0;
4207         end if;
4208 
4209         p_v_res(l_i).res_seq_num     := l_res_seq_num;
4210         p_v_res(l_i).autocharge_type := l_res_autocharge_type;
4211         p_v_res(l_i).standard_rate   := nvl(l_res_standard_rate_flag,1);
4212         p_v_res(l_i).activity_id     := l_activity_id;
4213         p_v_res(l_i).basis_type      := l_basis_type ;
4214         p_v_res(l_i).uom_code        := l_res_unit_of_measure;
4215         p_v_res(l_i).scheduled_flag  := l_sch_flag;
4216         p_v_res(l_i).max_assg_units  := l_max_assg_units;
4217         p_v_res(l_i).firm_type       := l_firm_type;
4218         p_v_res(l_i).setup_id        := l_setup_id;
4219         p_v_res(l_i).setup_code      := l_setup_code;
4220         p_v_res(l_i).start_date      := l_sdate;
4221         p_v_res(l_i).completion_date := l_edate;
4222         p_v_res(l_i).resource_code   := l_res_code;
4223 
4224     end if; -- p_substitution_type = WSM_SUB_ADD
4225 
4226 EXCEPTION
4227     when e_invalid_resource then
4228         x_error_code := -1;
4229         fnd_message.set_name('WSM','WSM_RES_NOT_REQUIRED');
4230         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' || fnd_message.get;
4231         fnd_file.put_line(fnd_file.log,x_error_msg );
4232 
4233     when e_invalid_rpl_grp then
4234         x_error_code := -1;
4235         fnd_message.set_name('WSM','WSM_RES_PARTIAL_RPLGRP');
4236         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' || fnd_message.get;
4237         fnd_file.put_line(fnd_file.log,x_error_msg );
4238 
4239     when e_invalid_date then
4240         x_error_code := -1;
4241         fnd_message.set_name('WSM','WSM_RES_BAD_DATES');
4242         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4243         fnd_file.put_line(fnd_file.log,x_error_msg );
4244 
4245    /* ST : Detailed Scheduling start */
4246    when e_invalid_max_assg_units then
4247        x_error_code := -1;
4248        fnd_message.set_name('WSM','WSM_INVALID_MAX_ASSG');
4249        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4250        fnd_file.put_line(fnd_file.log,x_error_msg );
4251 
4252    when e_invalid_setup_max_assg_units then
4253        x_error_code := -1;
4254        fnd_message.set_name('WSM','WSM_INVALID_SETUP_MAX_ASSG');
4255        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' || fnd_message.get;
4256        fnd_file.put_line(fnd_file.log,x_error_msg );
4257 
4258    when e_invalid_seq then
4259        x_error_code := -1;
4260        fnd_message.set_name('WSM','WSM_INVALID_SEQ');  /* group seq nyum and group seq id... */
4261        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' || fnd_message.get;
4262        fnd_file.put_line(fnd_file.log,x_error_msg );
4263 
4264    when e_invalid_batch_max_assg_units then
4265        x_error_code := -1;
4266        fnd_message.set_name('WSM','WSM_INVALID_BATCH_MAX_ASSG');
4267        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4268        fnd_file.put_line(fnd_file.log,x_error_msg );
4269 
4270    when e_invalid_setup_id then
4271        x_error_code := -1;
4272        --fnd_message.set_name('WSM','WSM_INVALID_SETUP');
4273        fnd_message.set_name('WSM','WSM_INVALID_FIELD'); /*Bug 5488082*/
4274        fnd_message.set_token('FLD_NAME', ' setup_id ');
4275        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4276        fnd_file.put_line(fnd_file.log,x_error_msg );
4277 
4278    when e_invalid_firm_type then
4279         x_error_code := -1;
4280         /* add the error message..... */
4281         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4282         fnd_message.set_token('FLD_NAME', ' firm type ');
4283         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4284         fnd_file.put_line(fnd_file.log,x_error_msg );
4285 
4286    when e_res_not_batchable then
4287        x_error_code := -1;
4288        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4289        --fnd_message.set_token('FLD_NAME', ' batch id for resource');
4290        fnd_message.set_token('FLD_NAME', ' batch_id'); /*Bug 5487540 */
4291        --x_error_msg := fnd_message.get;
4292        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4293        fnd_file.put_line(fnd_file.log,x_error_msg );
4294 
4295    when e_invalid_setup_code then
4296             x_error_code := -1;
4297             /* add the error message..... */
4298             fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4299             fnd_message.set_token('FLD_NAME', 'Setup Code ');
4300             x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4301             fnd_file.put_line(fnd_file.log,x_error_msg );
4302 
4303    /* ST : Detailed Scheduling end */
4304 
4305    --Alcoa Enhancements Changes
4306    when e_res_id_null THEN
4307         x_error_code := -1;
4308         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4309         fnd_message.set_token('FLD_NAME', 'Resource_code/ Resource_id combination');
4310         x_error_msg := fnd_message.get;
4311 
4312    when e_assgnd_units_null then
4313        x_error_code := -1;
4314        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4315        fnd_message.set_token('FLD_NAME', 'Assigned_units');
4316        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4317        fnd_file.put_line(fnd_file.log,x_error_msg );
4318 
4319    when e_inv_appl_res_units THEN
4320         x_error_code := -1;
4321         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4322         fnd_message.set_token('FLD_NAME', 'Applied_units');
4323         x_error_msg := fnd_message.get;
4324 
4325      when e_wt_or_wcit_rec_exists THEN
4326         x_error_code := -1;
4327         fnd_message.set_name('WSM','WSM_WT_OR_WCIT_REC_EXIS');
4328         x_error_msg := fnd_message.get;
4329 
4330      when e_po_req_rec_exists THEN
4331         x_error_code := -1;
4332         fnd_message.set_name('WSM','WSM_PO_REC_REC_EXIS');
4333         x_error_msg := fnd_message.get;
4334 
4335    when e_invalid_res_sub_grp_num then
4336        x_error_code := -1;
4337        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4338        fnd_message.set_token('FLD_NAME', 'substitution_group_num');
4339        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4340        fnd_file.put_line(fnd_file.log,x_error_msg );
4341 
4342    when e_invalid_res_reco_flag then
4343        x_error_code := -1;
4344        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4345        fnd_message.set_token('FLD_NAME', 'recommended_flag');
4346        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4347        fnd_file.put_line(fnd_file.log,x_error_msg );
4348 
4349    when e_inv_res_basis_type then
4350        x_error_code := -1;
4351        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4352        fnd_message.set_token('FLD_NAME', 'basis_type');
4353        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4354        fnd_file.put_line(fnd_file.log,x_error_msg );
4355 
4356    when e_inv_res_charge_type then
4357        x_error_code := -1;
4358        fnd_message.set_name('WSM','WSM_CURR_OP_RES_PO_MOVE');
4359        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4360        fnd_file.put_line(fnd_file.log,x_error_msg );
4361 
4362    when e_inv_res_sch_flag then
4363        x_error_code := -1;
4364        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4365        fnd_message.set_token('FLD_NAME', 'scheduled_flag');
4366        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4367        fnd_file.put_line(fnd_file.log,x_error_msg );
4368 
4369     when e_invalid_res_seq_num then
4370         x_error_code := -1;
4371         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4372         fnd_message.set_token('FLD_NAME', 'resource_seq_num');
4373         x_error_msg := fnd_message.get;
4374         fnd_file.put_line(fnd_file.log,x_error_msg );
4375 
4376    when e_invalid_usg_rate then
4377        x_error_code := -1;
4378        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4379        fnd_message.set_token('FLD_NAME', 'usage_rate_or_amount');
4380        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4381        fnd_file.put_line(fnd_file.log,x_error_msg );
4382 
4383      when e_invalid_res_dept THEN
4384         x_error_code := -1;
4385         fnd_message.set_name('WSM','WSM_INVALID_RES_DEPT');
4386         x_error_msg := fnd_message.get;
4387 
4388    when e_inv_resource_charge_type then
4389        x_error_code := -1;
4390        fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4391        fnd_message.set_token('FLD_NAME', 'Autocharge_type');
4392        x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') ' ||  fnd_message.get;
4393        fnd_file.put_line(fnd_file.log,x_error_msg );
4394 
4395     when others then
4396         x_error_code := -1;
4397         x_error_msg := 'Error: validate_wljdi_op_resource: (#'|| l_stmt_num||') '
4398                        || SUBSTR(SQLERRM, 1,240);
4399 
4400         fnd_file.put_line(fnd_file.log,x_error_msg );
4401 
4402 END validate_wljdi_op_resource;
4403 
4404 
4405 -- **********************************************************************************************
4406 -- VALIDATE_WLJDI_RES_INSTANCE: local validation procedure
4407 -- **********************************************************************************************
4408 
4409 PROCEDURE validate_wljdi_res_instance(
4410         p_wip_entity_id         in number,
4411         p_org_id                in number,
4412         p_v_res_ins             in t_res_ins,
4413         p_substitution_type     in number,
4414         p_handle_wip_cur_op     in varchar2,
4415         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
4416         -- In case of instance information being passed for setup resource that is added in the same run
4417         -- return the resurce seq num of the setup resource...
4418         p_parent_res_seq_num    IN NUMBER,
4419         p_resource_id           IN NUMBER,
4420         x_setup_res_seq_num     OUT nocopy number,
4421         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
4422         x_error_code            out nocopy number,
4423         x_error_msg             out nocopy varchar2) is
4424 
4425 l_stmt_num              number;
4426 l_i                     number;
4427 l_j                     number;
4428 l_exists                number;
4429 
4430 l_op_recommended        varchar2(1) := 'N';
4431 l_res_recommended       varchar2(1) := 'N';
4432 l_sdate_match           number;
4433 l_edate_match           number;
4434 l_res_sdate             date;
4435 l_res_edate             date;
4436 l_sdate                 date;
4437 l_edate                 date;
4438 l_instance_id           number;
4439 l_serial_number         varchar2(30);
4440 l_batch_id              number;
4441 
4442 l_rtg_op_seq_num        number;
4443 l_job_op_seq_num        number;
4444 l_res_seq_num           number;
4445 l_resource_id           number;
4446 l_department_id         number;  /*Added Department Id for bug 5476647*/
4447 
4448 e_null_date             exception;
4449 e_invalid_date          exception;
4450 e_null_ins_id           exception;
4451 e_invalid_ins_id        exception;
4452 e_invalid_res_seq_num   exception;
4453 e_not_on_reco_path      exception;
4454 e_res_not_reco          exception;
4455 e_invalid_rtg_op_seq    exception;
4456 e_invalid_time_slot     exception;
4457 e_ins_not_exists        exception;
4458 e_no_res_seq_num    exception;
4459 
4460 BEGIN
4461 
4462 
4463     x_error_code := 0;
4464     x_error_msg := '';
4465     l_i := p_v_res_ins.first;
4466 
4467     IF lbji_debug = 'Y' then
4468         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance start ...');
4469     END IF;
4470 
4471 
4472     if(l_i IS NULL) then return; end if;
4473 
4474     -- in p_v_res_use, all records should have the same res_seq_num
4475     l_rtg_op_seq_num  := p_v_res_ins(l_i).rtg_op_seq_num;
4476     l_job_op_seq_num  := p_v_res_ins(l_i).job_op_seq_num;
4477     l_res_seq_num     := p_v_res_ins(l_i).res_seq_num;
4478 
4479     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
4480     IF l_res_seq_num IS NULL THEN
4481         IF lbji_debug = 'Y' then
4482                 fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : Setup resopurce : '
4483                              || ' Parent_res_seq_num : ' || p_parent_res_seq_num
4484                              || ' Parent res ID  : ' ||  p_resource_id
4485                              || ' l_rtg_op_seq_num : ' || l_rtg_op_seq_num);
4486         END IF;
4487 
4488         -- call is for a setup resource...
4489         IF p_handle_wip_cur_op = 'N' THEN
4490                 l_stmt_num  := 5;
4491                 BEGIN
4492                     select resource_seq_num
4493                     into l_res_seq_num
4494                     from wsm_copy_op_resources WCOR
4495                     where WCOR.wip_entity_id     = p_wip_entity_id
4496                     and   WCOR.organization_id   = p_org_id
4497                     and   WCOR.operation_seq_num = l_rtg_op_seq_num
4498                     and   WCOR.parent_resource_seq_num = p_parent_res_seq_num
4499                     and   WCOR.resource_id        = p_resource_id;
4500 
4501                 EXCEPTION
4502                     WHEN NO_DATA_FOUND THEN
4503                          IF lbji_debug = 'Y' then
4504                                 fnd_file.put_line(fnd_file.log,' No Resource...');
4505                          END IF;
4506                          raise e_no_res_seq_num;
4507                     WHEN TOO_MANY_ROWS THEN
4508                         IF lbji_debug = 'Y' then
4509                                 fnd_file.put_line(fnd_file.log,' No Unique Resource...');
4510                         END IF;
4511                         raise e_no_res_seq_num;
4512                 END;
4513         ELSE
4514                 l_stmt_num  := 6;
4515 
4516                 BEGIN
4517                     select resource_seq_num
4518                     into l_res_seq_num
4519                     from  wip_operation_resources WOR
4520                     where WOR.wip_entity_id      = p_wip_entity_id
4521                     and   WOR.organization_id    = p_org_id
4522                     and   WOR.operation_seq_num  = l_job_op_seq_num
4523                     and   WOR.parent_resource_seq = p_parent_res_seq_num
4524                     and   WOR.resource_id         = p_resource_id;
4525 
4526                 EXCEPTION
4527                     WHEN NO_DATA_FOUND THEN
4528                         raise e_no_res_seq_num;
4529                     WHEN TOO_MANY_ROWS THEN
4530                         raise e_no_res_seq_num;
4531                 END;
4532         END IF;
4533 
4534         x_setup_res_seq_num := l_res_seq_num;
4535 
4536     END IF;
4537     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
4538 
4539     if p_substitution_type in (WSM_SUB_DEL ) then
4540         -- CZHDBG do we need to check if resourced is charged ???
4541 l_stmt_num  := 10;
4542         l_exists := 0;
4543         if( p_handle_wip_cur_op = 'N') then
4544             BEGIN
4545 l_stmt_num  := 20.1;
4546                 select  1
4547                 into    l_exists
4548                 from    wsm_copy_op_resource_instances
4549                 where   wip_entity_id     = p_wip_entity_id
4550                 and     organization_id   = p_org_id
4551                 and     operation_seq_num = l_rtg_op_seq_num
4552                 and     resource_seq_num  = l_res_seq_num;
4553             EXCEPTION
4554                 when no_data_found then
4555                     raise e_ins_not_exists;
4556                 when others then
4557                     null;
4558             END;
4559         else    -- p_handle_wip_cur_op = 'Y'
4560 
4561             BEGIN
4562 l_stmt_num  := 20.2;
4563                 select  1
4564                 into    l_exists
4565                 from    wip_op_resource_instances
4566                 where   wip_entity_id     = p_wip_entity_id
4567                 and     organization_id   = p_org_id
4568                 and     operation_seq_num = l_job_op_seq_num
4569                 and     resource_seq_num  = l_res_seq_num;
4570             EXCEPTION
4571                 when no_data_found then
4572                     raise e_ins_not_exists;
4573                 when others then
4574                     null;
4575             END;
4576         end if;
4577 
4578     elsif p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC ) then
4579 
4580         -- check res seq num in job/copy, get resource start/end dates
4581         if( p_handle_wip_cur_op = 'N') then
4582 
4583             -- check if the operation is on the recommended path
4584             BEGIN
4585 l_stmt_num  := 30.0;
4586                 /*Added Department Id for bug 5476647*/
4587                 select  wco.recommended,wco.department_id
4588                 into    l_op_recommended,l_department_id
4589                 from    wsm_copy_operations wco
4590                 where   wco.wip_entity_id     = p_wip_entity_id
4591                 and     wco.operation_seq_num = l_rtg_op_seq_num;
4592             EXCEPTION
4593                 when others then
4594                     raise e_invalid_rtg_op_seq;
4595             END;
4596             if(l_op_recommended <> 'Y') then
4597                 raise e_not_on_reco_path;
4598             end if;
4599 
4600             BEGIN
4601 l_stmt_num  := 30.1;
4602                 select  reco_start_date,
4603                         reco_completion_date,
4604                         resource_id,
4605                         recommended
4606                 into    l_res_sdate,
4607                         l_res_edate,
4608                         l_resource_id,       -- this is used for validate instance_id
4609                         l_res_recommended
4610                 from    wsm_copy_op_resources
4611                 where   wip_entity_id     = p_wip_entity_id
4612                 and     organization_id   = p_org_id
4613                 and     operation_seq_num = l_rtg_op_seq_num
4614                 and     resource_seq_num  = l_res_seq_num;
4615             EXCEPTION
4616                 when no_data_found then
4617                     raise e_invalid_res_seq_num;
4618                 when others then
4619                     null;
4620             END;
4621 
4622             IF lbji_debug = 'Y' then
4623                 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);
4624             END IF;
4625 
4626             if(l_res_recommended <> 'Y') then
4627                 raise e_res_not_reco;
4628             end if;
4629 
4630         else    -- p_handle_wip_cur_op = 'Y'
4631 
4632             IF lbji_debug = 'Y' then
4633                 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);
4634             END IF;
4635 
4636             BEGIN
4637 l_stmt_num  := 30.2;
4638                 select  wor.start_date,
4639                         wor.completion_date,
4640                         wor.resource_id,
4641                         wo.department_id
4642                 into    l_res_sdate,
4643                         l_res_edate,
4644                         l_resource_id,   -- this is used for validate instance_id
4645                         l_department_id    /*Added Department Id for bug 5476647*/
4646                 from    wip_operation_resources wor, wip_operations wo
4647                 where   wor.wip_entity_id     = p_wip_entity_id
4648                 and     wor.organization_id   = p_org_id
4649                 and     wor.operation_seq_num = l_job_op_seq_num
4650                 and     wor.resource_seq_num  = l_res_seq_num
4651                 and     wor.wip_entity_id     = wo.wip_entity_id
4652                 and     wor.organization_id   = wo.organization_id
4653                 and     wor.operation_seq_num = wo.operation_seq_num;
4654             EXCEPTION
4655                 when no_data_found then
4656                     raise e_invalid_res_seq_num;
4657                 when others then
4658                     raise e_invalid_res_seq_num;
4659             END;
4660 
4661 			IF lbji_debug = 'Y' then
4662                 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'));
4663                 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'));
4664             END IF;
4665 
4666         END IF;
4667 
4668         -- check all the records
4669         l_sdate_match := 0;
4670         l_edate_match := 0;
4671         while (l_i IS NOT NULL)
4672         loop
4673             l_instance_id   := p_v_res_ins(l_i).instance_id;
4674             l_serial_number := p_v_res_ins(l_i).serial_number;
4675             l_batch_id      := p_v_res_ins(l_i).batch_id;
4676             l_sdate         := p_v_res_ins(l_i).start_date;
4677             l_edate         := p_v_res_ins(l_i).completion_date;
4678 
4679             l_stmt_num  := 40.1;
4680 
4681             IF lbji_debug = 'Y' then
4682                 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);
4683                 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'));
4684                 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'));
4685                 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'));
4686                 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'));
4687             END IF;
4688 
4689             -- check instance_id and serial_number
4690             if(l_instance_id IS NULL ) then
4691                 raise e_null_ins_id;
4692             end if;
4693 l_stmt_num  := 40.2;
4694             IF lbji_debug = 'Y' then
4695                 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);
4696             END IF;
4697 
4698              BEGIN
4699                 /* Modified SQL for Bug 5476647. Since the SQL before was checking only whether the given instance
4700                    is valid for the resource. Now modified the SQL to check whether given instance is valid for the
4701                    department, resource combination */
4702                 select  1
4703                 into    l_exists
4704                 from    bom_dept_res_instances  bdri
4705                 where   bdri.resource_id = l_resource_id
4706                 and     bdri.department_id = l_department_id
4707                 and     bdri.instance_id = l_instance_id
4708                 and     NVL(bdri.serial_number, g_nvl_str) = NVL(l_serial_number, g_nvl_str);
4709 
4710                 /*l_exists :=0;
4711 
4712                 select  1
4713                 into    l_exists
4714                 from dual
4715                 where exists( select instance_id
4716                               from  bom_dept_res_instances  bdri
4717                               where bdri.resource_id = l_resource_id
4718                               and   bdri.instance_id = l_instance_id
4719                               and   NVL(bdri.serial_number, g_nvl_str) = NVL(l_serial_number, g_nvl_str) );
4720 
4721                if l_exists=0 then
4722                     raise e_invalid_ins_id;
4723                end if;*/
4724 
4725 
4726             EXCEPTION
4727                 when others then
4728                     raise e_invalid_ins_id;
4729             END;
4730 
4731 l_stmt_num  := 50.1;
4732 
4733             -- check start_date/completion_date
4734             if(l_sdate IS NULL or l_edate IS NULL) then
4735                 raise e_null_date;
4736             end if;
4737             if(l_sdate > l_edate or -- bug 3323417, was >=
4738                l_sdate < NVL(l_res_sdate, l_sdate) or
4739                l_edate > NVL(l_res_edate, l_edate))
4740             then
4741                 raise e_invalid_date;
4742             end if;
4743 l_stmt_num  := 50.2;
4744             -- check time slot
4745 
4746 
4747             if(l_sdate = l_res_sdate) then
4748                 l_sdate_match := 1;
4749             end if;
4750             if(l_edate = l_res_edate) then
4751                 l_edate_match := 1;
4752             end if;
4753 
4754             l_i := p_v_res_ins.next(l_i);
4755         end loop;
4756 
4757 l_stmt_num  := 60;
4758 
4759 
4760         if(l_sdate_match = 0 or l_edate_match = 0) then
4761             raise e_invalid_time_slot;
4762         end if;
4763 
4764     end if; -- p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC)
4765 
4766 EXCEPTION
4767     when e_res_not_reco then
4768         x_error_code := -1;
4769         fnd_message.set_name('WSM','WSM_INS_USE_NON_RECO');
4770         x_error_msg := fnd_message.get;
4771         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4772 
4773     when e_not_on_reco_path then
4774         x_error_code := -1;
4775         fnd_message.set_name('WSM','WSM_INS_USE_NON_RECO');
4776         x_error_msg := fnd_message.get;
4777         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4778 
4779 
4780     when e_invalid_rtg_op_seq then
4781         x_error_code := -1;
4782         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4783         fnd_message.set_token('FLD_NAME', 'routing_op_seq_num');
4784         x_error_msg := fnd_message.get;
4785 
4786         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4787 
4788 
4789     when e_invalid_res_seq_num then
4790         x_error_code := -1;
4791         fnd_message.set_name('WSM','WSM_RES_NOT_REQUIRED');
4792         x_error_msg := fnd_message.get;
4793 
4794         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4795 
4796 
4797     when e_ins_not_exists then
4798         x_error_code := -1;
4799         fnd_message.set_name('WSM','WSM_INS_NOT_EXISTS');
4800         x_error_msg := fnd_message.get;
4801 
4802         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4803 
4804 
4805     when e_null_ins_id then
4806         x_error_code := -1;
4807         fnd_message.set_name('WSM','WSM_NULL_FIELD');
4808         fnd_message.set_token('FLD_NAME', 'Instance_id');
4809         x_error_msg := fnd_message.get;
4810 
4811         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4812 
4813 
4814     when e_invalid_ins_id then
4815         x_error_code := -1;
4816         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4817         fnd_message.set_token('FLD_NAME', 'Instance_id and Serial_number');
4818         x_error_msg := fnd_message.get;
4819 
4820         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4821 
4822 
4823     when e_null_date then
4824         x_error_code := -1;
4825         fnd_message.set_name('WSM','WSM_NULL_FIELD');
4826         fnd_message.set_token('FLD_NAME', 'Start date and Completion date');
4827         x_error_msg := fnd_message.get;
4828 
4829         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4830 
4831 
4832     when e_invalid_date then
4833         x_error_code := -1;
4834         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
4835         fnd_message.set_token('FLD_NAME', 'Resource instance scheduled dates');
4836         x_error_msg := fnd_message.get;
4837 
4838         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4839 
4840 
4841     when e_invalid_time_slot then
4842         x_error_code := -1;
4843         fnd_message.set_name('WSM','WSM_INS_BAD_TIME_SLOT');
4844         x_error_msg := fnd_message.get;
4845 
4846         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4847 
4848     -- ST : Detailed Scheduling...
4849     when e_no_res_seq_num then
4850         x_error_msg := 'resource_seq_num cannot be uniquely found'; -- CZHDBG_MSG
4851         x_error_code := -1;
4852         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4853     -- ST : Detailed Scheduling...
4854 
4855 	when others then
4856         x_error_code := -1;
4857         x_error_msg := 'Error: validate_wljdi_res_instance: (#'||l_stmt_num||') '
4858                        || SUBSTR(SQLERRM, 1,2000);
4859 
4860         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_instance : ' || l_stmt_num  || ' error : ' || x_error_msg);
4861 
4862 
4863 END validate_wljdi_res_instance;
4864 
4865 
4866 -- **********************************************************************************************
4867 -- VALIDATE_WLJDI_RES_USAGE: local validation procedure
4868 -- **********************************************************************************************
4869 
4870 PROCEDURE validate_wljdi_res_usage(
4871         p_wip_entity_id         in number,
4872         p_org_id                in number,
4873         p_v_res_use             in t_res_use,
4874         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
4875         -- In case of instance information being passed for setup resource that is added in the same run
4876         -- return the resurce seq num of the setup resource...
4877         p_parent_res_seq_num    IN NUMBER,
4878         p_resource_id           IN NUMBER,
4879         x_setup_res_seq_num     OUT nocopy number,
4880         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
4881         p_substitution_type     in number,
4882         p_handle_wip_cur_op     in varchar2,
4883         x_error_code            out nocopy number,
4884         x_error_msg             out nocopy varchar2) is
4885 
4886 l_stmt_num              number;
4887 l_i                     number;
4888 l_j                     number;
4889 l_exists                number;
4890 
4891 l_op_recommended        varchar2(1) := 'N';
4892 l_res_recommended       varchar2(1) := 'N';
4893 l_sdate_match           number;
4894 l_edate_match           number;
4895 l_the_sdate             date;
4896 l_the_edate             date;
4897 l_sdate                 date;
4898 l_edate                 date;
4899 l_assigned_units        number;
4900 
4901 l_rtg_op_seq_num        number;
4902 l_job_op_seq_num        number;
4903 l_res_seq_num           number;
4904 l_instance_id           number;
4905 l_serial_num            varchar2(30);
4906 
4907 e_invalid_date          exception;
4908 e_null_date             exception;
4909 e_invalid_rtg_op_seq    exception;
4910 e_not_on_reco_path      exception;
4911 e_res_not_reco          exception;
4912 e_invalid_res_seq_num   exception;
4913 e_invalid_instance_id   exception;
4914 e_instances_exist       exception;
4915 e_invalid_asgned_units  exception;
4916 e_date_overlap          exception;
4917 e_invalid_time_slot     exception;
4918 e_usage_not_exists      exception;
4919 
4920 /* ST : Detailed Scheduling : start */
4921 e_invalid_ins_assigned_units  exception;
4922 e_no_res_seq_num              exception;
4923 /* ST : Detailed Scheduling : end  */
4924 
4925 BEGIN
4926 
4927     x_error_code := 0;
4928     x_error_msg := '';
4929     l_i := p_v_res_use.first;
4930     if(l_i IS NULL) then return; end if;
4931 
4932     -- in p_v_res_use, all records should hev the same res_seq_num
4933     l_rtg_op_seq_num := p_v_res_use(l_i).rtg_op_seq_num;
4934     l_job_op_seq_num := p_v_res_use(l_i).job_op_seq_num;
4935     l_res_seq_num    := p_v_res_use(l_i).res_seq_num;
4936     l_instance_id    := p_v_res_use(l_i).instance_id;      -- for instance usage
4937     l_serial_num     := p_v_res_use(l_i).serial_number;    -- for instance usage
4938 
4939     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
4940     IF l_res_seq_num IS NULL THEN
4941 
4942             -- call is for a setup resource...
4943             IF p_handle_wip_cur_op = 'N' THEN
4944                 BEGIN
4945                     select resource_seq_num
4946                     into l_res_seq_num
4947                     from wsm_copy_op_resources WCOR
4948                     where WCOR.wip_entity_id     = p_wip_entity_id
4949                     and   WCOR.organization_id   = p_org_id
4950                     and   WCOR.operation_seq_num = l_rtg_op_seq_num
4951                     and   WCOR.parent_resource_seq_num = p_parent_res_seq_num
4952                     and   WCOR.resource_id        = p_resource_id;
4953 
4954                 EXCEPTION
4955                     WHEN NO_DATA_FOUND THEN
4956                         raise e_no_res_seq_num;
4957                     WHEN TOO_MANY_ROWS THEN
4958                         raise e_no_res_seq_num;
4959                 END;
4960             ELSE
4961                 BEGIN
4962                     select resource_seq_num
4963                     into l_res_seq_num
4964                     from  wip_operation_resources WOR
4965                     where WOR.wip_entity_id      = p_wip_entity_id
4966                     and   WOR.organization_id    = p_org_id
4967                     and   WOR.operation_seq_num  = l_job_op_seq_num
4968                     and   WOR.parent_resource_seq = p_parent_res_seq_num
4969                     and   WOR.resource_id         = p_resource_id;
4970 
4971                 EXCEPTION
4972                     WHEN NO_DATA_FOUND THEN
4973                         raise e_no_res_seq_num;
4974                     WHEN TOO_MANY_ROWS THEN
4975                         raise e_no_res_seq_num;
4976                 END;
4977         END IF;
4978 
4979         x_setup_res_seq_num := l_res_seq_num;
4980 
4981     END IF;
4982     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
4983 
4984     if p_substitution_type in (WSM_SUB_DEL ) then
4985         -- CZHDBG: do we need to chech if resourced is charged ???
4986 l_stmt_num  := 10;
4987         l_exists := 0;
4988         if( p_handle_wip_cur_op = 'N') then
4989             BEGIN
4990 l_stmt_num  := 20.1;
4991                 select  1
4992                 into    l_exists
4993                 from    wsm_copy_op_resource_usage
4994                 where   wip_entity_id     = p_wip_entity_id
4995                 and     organization_id   = p_org_id
4996                 and     operation_seq_num = l_rtg_op_seq_num
4997                 and     resource_seq_num  = l_res_seq_num
4998                 and     NVL(instance_id, -1)          = NVL(l_instance_id, -1)
4999                 and     NVL(serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
5000             EXCEPTION
5001                 when no_data_found then
5002                     raise e_usage_not_exists;
5003                 when others then
5004                     null;
5005             END;
5006         else    -- p_handle_wip_cur_op = 'Y'
5007             BEGIN
5008 l_stmt_num  := 20.2;
5009                 select  1
5010                 into    l_exists
5011                 from    wip_operation_resource_usage
5012                 where   wip_entity_id     = p_wip_entity_id
5013                 and     organization_id   = p_org_id
5014                 and     operation_seq_num = l_job_op_seq_num
5015                 and     resource_seq_num  = l_res_seq_num
5016                 and     NVL(instance_id, -1)          = NVL(l_instance_id, -1)
5017                 and     NVL(serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
5018             EXCEPTION
5019                 when no_data_found then
5020                     raise e_usage_not_exists;
5021                 when others then
5022                     null;
5023             END;
5024         end if;
5025 
5026     elsif p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC ) then
5027 
5028         -- get resource or instance start/end dates
5029         if( p_handle_wip_cur_op = 'N') then
5030             BEGIN
5031 l_stmt_num  := 30.1;
5032                 select  wco.recommended,
5033                         wcor.recommended,
5034                         wcor.reco_start_date,
5035                         wcor.reco_completion_date
5036                 into    l_op_recommended,
5037                         l_res_recommended,
5038                         l_the_sdate,
5039                         l_the_edate
5040                 from    wsm_copy_operations   wco,
5041                         wsm_copy_op_resources wcor
5042                 where   wco.wip_entity_id      = p_wip_entity_id
5043                 and     wco.operation_seq_num  = l_rtg_op_seq_num
5044                 and     wcor.wip_entity_id     = wco.wip_entity_id
5045                 and     wcor.operation_seq_num = wco.operation_seq_num
5046                 and     wcor.resource_seq_num  = l_res_seq_num;
5047             EXCEPTION
5048                 when others then
5049                     raise e_invalid_rtg_op_seq;
5050             END;
5051 
5052 l_stmt_num  := 30.2;
5053             -- check if the operation is on the recommended path
5054             if(l_op_recommended <> 'Y') then
5055                 raise e_not_on_reco_path;
5056             end if;
5057             -- check if the the resource is recommended
5058             if(l_res_recommended <> 'Y') then
5059                 raise e_res_not_reco;
5060             end if;
5061 
5062             -- check if the resource has instance information
5063             l_exists := 0;
5064             BEGIN
5065 l_stmt_num  := 30.3;
5066                 select  1
5067                 into    l_exists
5068                 from    wsm_copy_op_resource_instances  wcori
5069                 where   wcori.wip_entity_id     = p_wip_entity_id
5070                 and     wcori.organization_id   = p_org_id
5071                 and     wcori.operation_seq_num = l_rtg_op_seq_num
5072                 and     wcori.resource_seq_num  = l_res_seq_num;
5073             EXCEPTION
5074                 when no_data_found then
5075                     l_exists := 0;
5076                 when too_many_rows then
5077                     l_exists := 1;
5078                 when others then
5079                     l_exists := 0;
5080             END;
5081 
5082 l_stmt_num  := 30.4;
5083             /* ST : Detailed Scheduling commenting out this code as resource usage info can be obt. when instances exist
5084             -- do not accept resource usage when instance info is available
5085             if(l_instance_id IS NULL and l_exists = 1) then
5086                 raise e_instances_exist;
5087             end if;
5088             */
5089 
5090             -- do not accept instance usage when instance info is not available
5091             if(l_instance_id IS NOT NULL and l_exists = 0) then
5092                 raise e_invalid_instance_id;
5093             end if;
5094 
5095             if(l_instance_id IS NOT NULL) then
5096                 -- get instance start/end dates
5097                 BEGIN
5098 l_stmt_num  := 30.5;
5099                     select  wcori.start_date,
5100                             wcori.completion_date
5101                     into    l_the_sdate,
5102                             l_the_edate
5103                     from    wsm_copy_op_resource_instances  wcori
5104                     where   wcori.wip_entity_id     = p_wip_entity_id
5105                     and     wcori.organization_id   = p_org_id
5106                     and     wcori.operation_seq_num = l_rtg_op_seq_num
5107                     and     wcori.resource_seq_num  = l_res_seq_num
5108                     and     wcori.instance_id       = l_instance_id
5109                     and     NVL(wcori.serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
5110                 EXCEPTION
5111                     when others then
5112                         raise e_invalid_instance_id;
5113                 END;
5114             end if;
5115 
5116         else    -- p_handle_wip_cur_op = 'Y'
5117 l_stmt_num  := 40.0;
5118             -- check if the resource has instance information
5119             l_exists := 0;
5120             -- Code for debug purpose.... ---- -- to uncomment replace all
5121             IF lbji_debug = 'Y' then
5122                 fnd_file.put_line(fnd_file.log,'Wip entity id  : ' || p_wip_entity_id
5123                                                || ' op seq num : ' || l_job_op_seq_num
5124                                    || ' res seq num : ' ||l_res_seq_num
5125                                    );
5126             END IF;
5127 
5128             BEGIN
5129 l_stmt_num  := 40.1;
5130                 select  1
5131                 into    l_exists
5132                 from    wip_op_resource_instances  wori
5133                 where   wori.wip_entity_id     = p_wip_entity_id
5134                 and     wori.organization_id   = p_org_id
5135                 and     wori.operation_seq_num = l_job_op_seq_num /* l_rtg_op_seq_num : ST : This has to be job op seq num */
5136                 and     wori.resource_seq_num  = l_res_seq_num;
5137             EXCEPTION
5138                 when no_data_found then
5139                     l_exists := 0;
5140                 when too_many_rows then
5141                     l_exists := 1;
5142                 when others then
5143                     l_exists := 0;
5144             END;
5145 
5146 l_stmt_num  := 40.2;
5147             -- do not accept resource usage when instance info is available
5148             /*
5149             if(l_instance_id IS NULL and l_exists = 1) then
5150                 raise e_instances_exist;
5151             end if;
5152             */
5153             -- do not accept instance usage when instance info is not available
5154             if(l_instance_id IS NOT NULL and l_exists = 0) then
5155                 raise e_invalid_instance_id;
5156             end if;
5157 
5158             if(l_instance_id IS NOT NULL) then
5159                 -- get instance start/end dates
5160                 BEGIN
5161 l_stmt_num  := 40.3;
5162                     select  start_date,
5163                             completion_date
5164                     into    l_the_sdate,
5165                             l_the_edate
5166                     from    wip_op_resource_instances
5167                     where   wip_entity_id     = p_wip_entity_id
5168                     and     organization_id   = p_org_id
5169                     and     operation_seq_num = l_job_op_seq_num
5170                     and     resource_seq_num  = l_res_seq_num
5171                     and     instance_id       = l_instance_id
5172                     and     NVL(serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
5173                 EXCEPTION
5174                     when others then
5175                         raise e_invalid_instance_id;
5176                 END;
5177             else
5178                 -- get resource start/end dates
5179                 BEGIN
5180 l_stmt_num  := 40.4;
5181                     select  start_date,
5182                             completion_date
5183                     into    l_the_sdate,
5184                             l_the_edate
5185                     from    wip_operation_resources wor
5186                     where   wip_entity_id     = p_wip_entity_id
5187                     and     organization_id   = p_org_id
5188                     and     operation_seq_num = l_job_op_seq_num
5189                     and     resource_seq_num  = l_res_seq_num;
5190                 EXCEPTION
5191                     when others then
5192                         raise e_invalid_res_seq_num;
5193                 END;
5194             end if;
5195         end if;
5196 
5197         -- first loop check single record
5198         l_sdate_match := 0;
5199         l_edate_match := 0;
5200         while (l_i IS NOT NULL)
5201         loop
5202             l_assigned_units := p_v_res_use(l_i).assigned_units;
5203             l_sdate          := p_v_res_use(l_i).start_date;
5204             l_edate          := p_v_res_use(l_i).completion_date;
5205 
5206             /* ST : Detailed scheduling : start */
5207             l_instance_id    := p_v_res_use(l_i).instance_id;
5208             /* ST : Detailed scheduling : end */
5209 
5210 l_stmt_num  := 50;
5211             -- check start_date/completion_date
5212             if(l_sdate IS NULL or l_edate IS NULL) then
5213                 raise e_null_date;
5214             end if;
5215             if(l_sdate > l_edate or         -- bug 3323417, was >=
5216                l_sdate < NVL(l_the_sdate, l_sdate) or
5217                l_edate > NVL(l_the_edate, l_edate))
5218             then
5219                 --fnd_file.put_line(fnd_file.log, 'CZHDBG: XXX'
5220                 --    || ' l_the_sdate = ' || TO_CHAR(l_the_sdate, 'DD-MON-YYYY HH24:MI:SS')
5221                 --    || ',l_the_edate = ' || TO_CHAR(l_the_edate, 'DD-MON-YYYY HH24:MI:SS')
5222                 --    || ' l_sdate = '     || TO_CHAR(l_sdate, 'DD-MON-YYYY HH24:MI:SS')
5223                 --    || ' l_edate = '     || TO_CHAR(l_edate, 'DD-MON-YYYY HH24:MI:SS')
5224                 --);
5225                 raise e_invalid_date;
5226             end if;
5227 
5228 l_stmt_num  := 60;
5229             -- check assigned_units
5230             if(l_assigned_units IS NULL or l_assigned_units <= 0) then
5231                 raise e_invalid_asgned_units;
5232             end if;
5233 
5234             -- check time slot
5235             if(l_sdate = l_the_sdate) then
5236                 l_sdate_match := 1;
5237             end if;
5238             if(l_edate = l_the_edate) then
5239                 l_edate_match := 1;
5240             end if;
5241 
5242             /* ST : Detailed scheduling : start */
5243             /* for resource instance usage assigned units should be equal to 1 */
5244             if l_instance_id is not null and l_assigned_units <> 1 then
5245                   raise e_invalid_ins_assigned_units;
5246             end if;
5247             /* ST : Detailed scheduling : end  */
5248             l_i := p_v_res_use.next(l_i);
5249         end loop;
5250 
5251 l_stmt_num  := 70;
5252         if(l_sdate_match = 0 or l_edate_match = 0) then
5253             raise e_invalid_time_slot;
5254         end if;
5255 
5256         -- second loop check relation between records
5257         l_i := p_v_res_use.first;
5258         while (l_i IS NOT NULL)
5259         loop
5260             l_sdate := p_v_res_use(l_i).start_date;
5261             l_edate := p_v_res_use(l_i).completion_date;
5262 
5263 l_stmt_num  := 80;
5264             -- check time overlap
5265             l_j := p_v_res_use.next(l_i);
5266             while (l_j IS NOT NULL)
5267             loop
5268                 if(p_v_res_use(l_j).start_date < l_sdate and
5269                    p_v_res_use(l_j).completion_date > l_sdate) or
5270                   (l_sdate < p_v_res_use(l_j).start_date and
5271                    l_edate > p_v_res_use(l_j).start_date)
5272                 then
5273                     raise e_date_overlap;
5274                 end if;
5275 
5276                 l_j := p_v_res_use.next(l_j);
5277             end loop;
5278 
5279             l_i := p_v_res_use.next(l_i);
5280         end loop;
5281     end if; -- p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC)
5282 
5283 EXCEPTION
5284     when e_not_on_reco_path then
5285         x_error_code := -1;
5286         fnd_message.set_name('WSM','WSM_INS_USE_NON_RECO');
5287         x_error_msg := fnd_message.get;
5288         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5289 
5290     when e_res_not_reco then
5291         x_error_code := -1;
5292         fnd_message.set_name('WSM','WSM_INS_USE_NON_RECO');
5293         x_error_msg := fnd_message.get;
5294         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5295 
5296     when e_invalid_rtg_op_seq then
5297         x_error_code := -1;
5298         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
5299         fnd_message.set_token('FLD_NAME', 'routing_op_seq_num');
5300         x_error_msg := fnd_message.get;
5301         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5302 
5303     when e_invalid_res_seq_num then
5304         x_error_code := -1;
5305         fnd_message.set_name('WSM','WSM_RES_NOT_REQUIRED');
5306         x_error_msg := fnd_message.get;
5307         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5308 
5309     when e_instances_exist then
5310         x_error_code := -1;
5311         fnd_message.set_name('WSM','WSM_INS_EXISTS');
5312         x_error_msg := fnd_message.get;
5313         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5314 
5315     when e_invalid_instance_id then
5316         x_error_code := -1;
5317         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
5318         --fnd_message.set_token('FLD_NAME', ' l_stmt_num : ' || l_stmt_num || ' Resource_seq_num / Instance_id / Serial_number');
5319         fnd_message.set_token('FLD_NAME', ' Resource_seq_num / Instance_id / Serial_number'); /*Bug 5488016 */
5320         x_error_msg := fnd_message.get;
5321         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5322 
5323     when e_invalid_asgned_units then
5324         x_error_code := -1;
5325         fnd_message.set_name('WSM','WSM_USE_BAD_ASGN_UNITS');
5326         x_error_msg := fnd_message.get;
5327         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5328 
5329     /* ST : Detailed Scheduling start */
5330     when e_invalid_ins_assigned_units then
5331         x_error_code := -1;
5332         fnd_message.set_name('WSM','WSM_INVALID_INS_ASSG_UNITS');
5333         x_error_msg := fnd_message.get;
5334         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5335 
5336     when e_no_res_seq_num then
5337         x_error_msg := 'validate_wljdi_res_usage :  resource_seq_num cannot be uniquely found'; -- CZHDBG_MSG
5338         x_error_code := -1;
5339         fnd_file.put_line(fnd_file.log,substrb(x_error_msg, 1, 2000));
5340 
5341     /* ST : Detailed Scheduling end */
5342 
5343     when e_usage_not_exists then
5344         x_error_code := -1;
5345         fnd_message.set_name('WSM','WSM_USE_NOT_EXISTS');
5346         x_error_msg := fnd_message.get;
5347         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5348 
5349     when e_null_date then
5350         x_error_code := -1;
5351         fnd_message.set_name('WSM','WSM_NULL_FIELD');
5352         fnd_message.set_token('FLD_NAME', 'Start date and Completion date');
5353         x_error_msg := fnd_message.get;
5354         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5355 
5356     when e_invalid_date then
5357         x_error_code := -1;
5358         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
5359         fnd_message.set_token('FLD_NAME', 'Resource usage scheduled dates');
5360         x_error_msg := fnd_message.get;
5361         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5362 
5363     when e_date_overlap then
5364         x_error_code := -1;
5365         fnd_message.set_name('WSM','WSM_USE_TIME_OVERLAP');
5366         x_error_msg := fnd_message.get;
5367         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5368 
5369     when e_invalid_time_slot then
5370         x_error_code := -1;
5371         fnd_message.set_name('WSM','WSM_USE_BAD_TIME_SLOT');
5372         x_error_msg := fnd_message.get;
5373         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5374 
5375     when others then
5376         x_error_code := -1;
5377         x_error_msg := 'Error: validate_wljdi_res_usage: (#'||l_stmt_num||') '
5378                        || SUBSTR(SQLERRM, 1,240);
5379         fnd_file.put_line(fnd_file.log,'validate_wljdi_res_usage : ' ||  l_stmt_num || ' : ' || x_error_msg);
5380 
5381 END validate_wljdi_res_usage;
5382 
5383 
5384 -- **********************************************************************************************
5385 -- VALIDATE_WLJDI_OP_COMPONENT: local validation procedure
5386 -- **********************************************************************************************
5387 
5388 PROCEDURE validate_wljdi_op_component(
5389         p_wip_entity_id         in number,
5390         p_org_id                in number,
5391         p_v_comp                in out nocopy t_op_comp,
5392         p_substitution_type     in number,
5393         p_handle_wip_cur_op     in varchar2,
5394         x_error_code            out nocopy number,
5395         x_error_msg             out nocopy varchar2) is
5396 
5397 l_stmt_num              number;
5398 l_i                     number;
5399 
5400 l_op_sdate              date;
5401 l_op_edate              date;
5402 l_op_qty                number;
5403 l_op_recommended        varchar2(1) := 'N';
5404 
5405 l_rtg_op_seq_num        number;
5406 l_job_op_seq_num        number;
5407 l_item_id               number;
5408 l_primary_item_id       number;
5409 l_src_phm_item_id       number;
5410 l_component_seq_id      number;
5411 l_date_req              date;
5412 l_req_qty               number;
5413 l_qpa                   number;
5414 l_bom_qpa               number; -- ADD: bug 3347985
5415 l_scheduled_qty         number;
5416 l_component_yield       number;
5417 
5418 -- Added For LBJ Interface Alcoa Enhancement
5419 l_item_old_id     number;
5420 l_comp_exist      number;
5421 l_quantity_issued number;
5422 l_temp            number;
5423 l_comp_duplicate  number;
5424 l_wip_valid       number;
5425 l_wip_supply_type number;
5426 l_job_qty         number;
5427 l_mrp_net_flag    number;
5428 l_phan_exist      number;
5429 l_src_phm_id      number;
5430 l_quantity_per_assembly number;
5431 l_supply_subinventory   varchar2(10);
5432 l_supply_locator_id     number;
5433 l_sched_qty        number;
5434 
5435 e_parent_phantom          exception;
5436 e_parent_phantom_comp     exception;
5437 e_no_parent_phantom       exception;
5438 e_invalid_item            exception;
5439 e_quantity_issued_error   exception;
5440 e_material_trans_err      exception;
5441 e_phantom_comp            exception;
5442 e_invalid_wip_supply_type exception;
5443 e_invalid_qpa             exception;
5444 e_inv_supply_info         exception;
5445 e_invalid_job             exception;
5446 e_dup_component           exception;
5447 e_no_assy_as_comp         exception;
5448 e_invalid_mrp_net_flag  exception;
5449 
5450 cursor c_wsm_phm_grp (
5451         c_wip_entity_id     number,
5452         c_rtg_op_seq_num    number,
5453         c_src_phm_item_id   number) is
5454     select   component_sequence_id,
5455              bill_quantity_per_assembly -- bug 3347985, was 'quantity_per_assembly'
5456     from     wsm_copy_requirement_ops
5457     where    wip_entity_id      = c_wip_entity_id
5458     and      operation_seq_num  = c_rtg_op_seq_num
5459     and      source_phantom_id  = c_src_phm_item_id
5460     order by component_sequence_id;
5461 
5462 l_v_phm_comp_seqs       t_number;
5463 l_v_phm_comp_qpa        t_number;
5464 
5465 e_req_qty_zero          exception; -- bug 7197440
5466 e_req_qty_sign_chg      exception; -- bug 7197440
5467 e_invalid_component     exception;
5468 e_invalid_phantom       exception;
5469 e_invalid_date          exception;
5470 e_req_qty_not_upd       exception;
5471 --e_req_qty_negative      exception; Changed for Bug# 7197440
5472 e_invalid_comp_yield    exception;
5473 
5474 BEGIN
5475 
5476     x_error_code := 0;
5477     x_error_msg := '';
5478     l_i := p_v_comp.first;
5479     if(l_i IS NULL) then return; end if;
5480 
5481     l_rtg_op_seq_num   := p_v_comp(l_i).rtg_op_seq_num;
5482     l_job_op_seq_num   := p_v_comp(l_i).job_op_seq_num;
5483     l_item_id          := p_v_comp(l_i).item_id;
5484     l_primary_item_id  := p_v_comp(l_i).primary_item_id;
5485     l_src_phm_item_id  := p_v_comp(l_i).src_phm_item_id;
5486     l_component_seq_id := p_v_comp(l_i).component_seq_id;
5487     l_date_req         := p_v_comp(l_i).date_required;
5488     l_req_qty          := p_v_comp(l_i).required_qty;
5489 
5490     -- Added For LBJ Interface Alcoa Enhancement
5491     l_item_old_id      := p_v_comp(l_i).item_old_id;
5492     l_wip_supply_type  := p_v_comp(l_i).wip_supply_type;
5493     l_mrp_net_flag     := p_v_comp(l_i).mrp_net_flag;
5494     l_quantity_per_assembly := p_v_comp(l_i).qpa ;
5495     l_supply_subinventory   := p_v_comp(l_i).supply_subinventory;
5496     l_supply_locator_id     := p_v_comp(l_i).supply_locator ;
5497 
5498 l_stmt_num  := 10;
5499     -- validate operation_seq_num, l_item_id
5500     if p_substitution_type in (WSM_SUB_CHG, WSM_SUB_REC) then
5501 
5502         -- get op start/end dates
5503         if( p_handle_wip_cur_op = 'Y') then
5504             BEGIN
5505 l_stmt_num  := 20;
5506                 select  wo.first_unit_start_date,
5507                         wo.last_unit_completion_date
5508                 into    l_op_sdate,
5509                         l_op_edate
5510                 from    wip_operations             wo,
5511                         wip_requirement_operations wro
5512                 where   wo.wip_entity_id         = p_wip_entity_id
5513                 and     wo.operation_seq_num     = l_job_op_seq_num
5514                 and     wro.wip_entity_id        = wo.wip_entity_id
5515                 and     wro.operation_seq_num    = wo.operation_seq_num
5516                 and     wro.inventory_item_id    = l_item_id;
5517             EXCEPTION
5518                 when no_data_found then
5519                     raise e_invalid_component;
5520             END;
5521 
5522         else    -- p_handle_wip_cur_op = 'N'
5523 
5524             if(l_src_phm_item_id IS NOT NULL AND
5525                l_src_phm_item_id <> -1)
5526             then
5527 
5528                 -- check l_rtg_op_seq_num
5529                 BEGIN
5530 l_stmt_num  := 30;
5531                     select  wco.reco_start_date,
5532                             wco.reco_completion_date,
5533                             wco.recommended,
5534                             wco.reco_scheduled_quantity
5535                     into    l_op_sdate,
5536                             l_op_edate,
5537                             l_op_recommended,
5538                             l_scheduled_qty
5539                     from    wsm_copy_operations wco
5540                     where   wco.wip_entity_id     = p_wip_entity_id
5541                     and     wco.operation_seq_num = l_rtg_op_seq_num;
5542                 EXCEPTION
5543                     when no_data_found then
5544                         raise e_invalid_component;
5545                 END;
5546 
5547 l_stmt_num  := 40.1;
5548                 -- get all the component_seq_id in the same phantom comp
5549                 for rec in c_wsm_phm_grp (
5550                         p_wip_entity_id,
5551                         l_rtg_op_seq_num,
5552                         l_src_phm_item_id)
5553                 loop
5554                     l_v_phm_comp_seqs(rec.component_sequence_id) := rec.component_sequence_id;
5555                     l_v_phm_comp_qpa(rec.component_sequence_id)  := rec.bill_quantity_per_assembly; -- bug 3347985
5556                 end loop;
5557 
5558 l_stmt_num  := 40.2;
5559                 -- should check whether all the components within the phantom
5560                 -- are send together
5561                 if(l_v_phm_comp_seqs.COUNT <> p_v_comp.COUNT) then
5562                     raise e_invalid_phantom;
5563                 end if;
5564                 l_i := p_v_comp.first;
5565                 while (l_i IS NOT NULL)
5566                 loop
5567 l_stmt_num  := 40.3;
5568                     l_component_seq_id := p_v_comp(l_i).component_seq_id;
5569                     if(NOT l_v_phm_comp_seqs.EXISTS(l_component_seq_id)) then
5570                         raise e_invalid_phantom;
5571                     end if;
5572 
5573                     -- check if component yield is going to be changed
5574                     -- by sending required_quantity
5575                     l_req_qty := p_v_comp(l_i).required_qty;
5576                     if(l_req_qty IS NOT NULL) then
5577                         -- bug 7197440 : Do not allow required quantity to be either zero or change of sign.
5578                         if(l_req_qty = 0) then
5579 					 raise e_req_qty_zero;
5580 		        elsif (sign(l_req_qty) <> sign(l_v_phm_comp_qpa(l_component_seq_id))) then
5581 					 raise e_req_qty_sign_chg;
5582 	                end if; -- end of fix for bug 7197440
5583 
5584                         if(l_op_recommended <> 'Y') then
5585                             raise e_req_qty_not_upd;
5586                         end if;
5587                         if(NVL(l_scheduled_qty, 0) <> 0) then
5588                             -- BC: bug 3347985
5589                             -- l_qpa             := l_v_phm_comp_qpa(l_component_seq_id);
5590                             -- l_component_yield := round(l_scheduled_qty*l_qpa/l_req_qty, 6);
5591                             l_bom_qpa         := l_v_phm_comp_qpa(l_component_seq_id);
5592                             l_component_yield := round(l_scheduled_qty*l_bom_qpa/l_req_qty, 6);
5593                             l_qpa             := l_req_qty/l_scheduled_qty;
5594                             -- EC: bug 3347985
5595                             if(l_component_yield <= 0) then
5596                                 raise e_invalid_comp_yield;
5597                             else
5598                                 p_v_comp(l_i).component_yield := l_component_yield;
5599                                 p_v_comp(l_i).qpa             := l_qpa;
5600                             end if;
5601                         else
5602                             p_v_comp(l_i).component_yield := null;
5603                             p_v_comp(l_i).qpa             := null;  -- ADD: bug 3347985
5604                         end if;
5605                     else
5606                         p_v_comp(l_i).component_yield := null;
5607                         p_v_comp(l_i).qpa             := null;  -- ADD: bug 3347985
5608                     end if;
5609 
5610                     l_i := p_v_comp.next(l_i);
5611                 end loop;
5612 
5613             else    -- l_src_phm_item_id IS NULL or -1
5614 
5615                 BEGIN
5616 l_stmt_num  := 50.1;
5617                     select  wco.reco_start_date,
5618                             wco.reco_completion_date,
5619                             wco.recommended,
5620                             wco.reco_scheduled_quantity,
5621                             wcro.bill_quantity_per_assembly -- bug 3347985 was 'quantity_per_assembly'
5622                     into    l_op_sdate,
5623                             l_op_edate,
5624                             l_op_recommended,
5625                             l_scheduled_qty,
5626                             l_bom_qpa
5627                     from    wsm_copy_operations      wco,
5628                             wsm_copy_requirement_ops wcro
5629                     where   wco.wip_entity_id         = p_wip_entity_id
5630                     and     wco.operation_seq_num     = l_rtg_op_seq_num
5631                     and     wcro.wip_entity_id        = wco.wip_entity_id
5632                     and     wcro.operation_seq_num    = wco.operation_seq_num
5633                     and     wcro.component_item_id    = l_item_id
5634                     and     wcro.primary_component_id = l_primary_item_id
5635                     and     wcro.source_phantom_id = -1; -- is NULL;
5636                 EXCEPTION
5637                     when no_data_found then
5638                         raise e_invalid_component;
5639                 END;
5640 
5641 l_stmt_num  := 50.2;
5642                 -- check if component yield is going to be changed
5643                 if(l_req_qty IS NOT NULL) then
5644                         -- bug 7197440 : Do not allow required quantity to be either zero or change of sign.
5645                         if(l_req_qty = 0) then
5646 					 raise e_req_qty_zero;
5647 		        elsif (sign(l_req_qty) <> sign(l_bom_qpa)) then
5648 					 raise e_req_qty_sign_chg;
5649 	                end if; -- end of fix for bug 7197440
5650 l_stmt_num  := 50.3;
5651                     if(l_op_recommended <> 'Y') then
5652                         raise e_req_qty_not_upd;
5653                     end if;
5654 l_stmt_num  := 50.4;
5655                     if(NVL(l_scheduled_qty, 0) <> 0) then
5656                         -- BC: bug 3347985
5657                         --l_component_yield := round(l_scheduled_qty*l_qpa/l_req_qty, 6);
5658                         l_component_yield := round(l_scheduled_qty*l_bom_qpa/l_req_qty, 6);
5659                         l_qpa             := l_req_qty/l_scheduled_qty;
5660                         -- EC: bug 3347985
5661                         if(l_component_yield <= 0) then
5662                             raise e_invalid_comp_yield;
5663                         else
5664                             p_v_comp(l_i).component_yield := l_component_yield;
5665                             p_v_comp(l_i).qpa             := l_qpa; -- ADD: bug 3347985
5666                         end if;
5667                     else
5668                         p_v_comp(l_i).component_yield := null;
5669                         p_v_comp(l_i).qpa             := null; -- ADD: bug 3347985
5670                     end if;
5671                 else
5672                     p_v_comp(l_i).component_yield := null;
5673                     p_v_comp(l_i).qpa             := null; -- ADD: bug 3347985
5674                 end if;
5675 
5676             end if; -- l_src_phm_item_id IS NULL or -1
5677 
5678         end if;
5679 
5680 l_stmt_num  := 60;
5681         -- validate dates
5682         if(l_date_req IS NOT NULL) then
5683             if( --NVL(l_op_sdate, l_date_req) > l_date_req or -- bug 3292689: removed per APS request
5684                 l_date_req > NVL(l_op_edate, l_date_req))
5685             then
5686                 raise e_invalid_date;
5687             end if;
5688         end if;
5689 
5690     end if; -- p_load_type in (WSM_SUB_CHG, WSM_SUB_REC)
5691     IF (p_substitution_type=WSM_SUB_DEL) THEN
5692 
5693         l_comp_exist := null;
5694         l_quantity_issued :=null;
5695         l_temp   :=null;
5696         if( p_handle_wip_cur_op = 'Y') then  --Current Operation  validation
5697 
5698             --Parent Phantom logic, not deleting the component of parent phantom
5699             if (l_job_op_seq_num<0) then
5700                 raise e_parent_phantom;
5701             end if;
5702 
5703             --Check if the inventory item id old is valid
5704             /*
5705             begin
5706                 select 1
5707                 into l_comp_exist
5708                 from mtl_system_items_kfv
5709                 where organization_id = p_org_id
5710                 and  inventory_item_id=l_item_old_id;
5711             exception
5712                 when others then
5713                     raise e_invalid_item;
5714             end; */
5715 
5716             begin
5717                 select  wro.quantity_issued
5718                 into  l_quantity_issued
5719                 from  wip_requirement_operations wro
5720                 where wro.wip_entity_id = p_wip_entity_id
5721                 and   wro.operation_seq_num = l_job_op_seq_num
5722                 and   wro.inventory_item_id = l_item_old_id
5723                 and   wro.organization_id = p_org_id;
5724 
5725             exception
5726                 when no_data_found then
5727                     null;
5728             end;
5729 
5730             --Quantity Issued validate
5731 
5732             if(l_quantity_issued >0) then
5733                 raise e_quantity_issued_error;
5734             end if;
5735 
5736             --Validation to check if the component has been Transacted
5737             begin
5738 
5739                 select  1
5740                 into  l_temp
5741                 from  dual
5742                 where  exists
5743                              (select 1
5744                              from  wip_requirement_operations wro
5745                              where  wro.wip_entity_id = p_wip_entity_id
5746                              and  wro.organization_id =p_org_id
5747                              and  wro.operation_seq_num = l_job_op_seq_num
5748                              and  wro.inventory_item_id = l_item_old_id
5749                              and  wro.quantity_issued <> 0)
5750                 or exists
5751                             (select 1
5752                             from  mtl_material_transactions_temp mmtt
5753                             where mmtt.transaction_source_id = p_wip_entity_id
5754                             and  mmtt.organization_id = p_org_id
5755                             and  mmtt.operation_seq_num = l_job_op_seq_num
5756                             and  mmtt.inventory_item_id =  l_item_old_id);
5757 
5758             exception
5759                 when no_data_found then
5760                     null;
5761             end;
5762 
5763             if (l_temp is not null) then
5764                 raise e_material_trans_err;
5765             end if;
5766 
5767         elsif( p_handle_wip_cur_op = 'N') THEN
5768             /* Support deletion of Parent Phantom component
5769             if (l_rtg_op_seq_num<0) then
5770                 raise e_parent_phantom;
5771             end if;
5772 
5773             --check if the inventory item id old is valid
5774             begin
5775                 select 1
5776                 into l_comp_exist
5777                 from mtl_system_items_kfv
5778                 where organization_id = p_org_id
5779                 and  inventory_item_id=l_item_old_id;
5780             exception
5781                 when others then
5782                     raise e_invalid_item;
5783             end;
5784             */
5785             -- Validate for parent phantom component that no child component exists.
5786             if l_src_phm_item_id=0 then
5787                 begin
5788                     select 1
5789                     into   l_phan_exist
5790                     from   dual
5791                     where  exists (select component_item_id
5792                                    from   wsm_copy_requirement_ops
5793                                    where  wip_entity_id     = p_wip_entity_id
5794                                    and    source_phantom_id = l_item_old_id
5795                                    and    component_item_id <>l_item_old_id
5796                                    and    operation_seq_num = l_rtg_op_seq_num);
5797                     raise e_parent_phantom_comp;
5798                 exception
5799                     when no_data_found then
5800                         null;
5801                 end;
5802             end if;
5803         end if; --p_handle_wip_cur_op end if
5804     ELSIF (p_substitution_type=WSM_SUB_ADD) THEN
5805 
5806         -- For a standard job, assembly should not be a component.
5807         begin
5808             select 1
5809             into l_comp_exist
5810             from dual
5811             where exists ( select 1
5812                            from wip_discrete_jobs
5813                            where wip_entity_id = p_wip_entity_id
5814                            and organization_id = p_org_id
5815                            and job_type = WIP_CONSTANTS.STANDARD
5816                            and primary_item_id = l_item_id );
5817 
5818             raise e_no_assy_as_comp;
5819 
5820         exception
5821             when no_data_found then
5822                 null;
5823         end;
5824 
5825         l_comp_exist := NULL;
5826         if( p_handle_wip_cur_op = 'Y') THEN
5827 
5828             /*begin
5829                 select 1
5830                 into l_comp_exist
5831                 from mtl_system_items_kfv
5832                 where inventory_item_id=l_item_id
5833                 and organization_id=p_org_id;
5834             exception
5835                 when others then
5836                     raise e_invalid_item;
5837             end;*/
5838 
5839             ---Validate wip supply type
5840 
5841             if l_wip_supply_type= WIP_CONSTANTS.PHANTOM then
5842                 raise e_phantom_comp;
5843             end if;
5844 
5845             if l_wip_supply_type not in (WIP_CONSTANTS.PUSH,
5846                                          WIP_CONSTANTS.ASSY_PULL,
5847                                          WIP_CONSTANTS.OP_PULL,
5848                                          WIP_CONSTANTS.BULK,
5849                                          WIP_CONSTANTS.VENDOR) or l_wip_supply_type is null then
5850                 raise e_invalid_wip_supply_type;
5851             end if;
5852 
5853             --check if component already defined for given job and operation.
5854             begin
5855                 select  1
5856                 into l_comp_duplicate
5857                 from wip_requirement_operations wro
5858                 where wro.wip_entity_id = p_wip_entity_id
5859                 and wro.operation_seq_num =l_job_op_seq_num
5860                 and wro.organization_id=p_org_id
5861                 and wro.inventory_item_id=l_item_id;
5862             exception
5863                 when others then
5864                     null;
5865             end;
5866 
5867             if l_comp_duplicate is not null then
5868                 raise e_dup_component;
5869             end if;
5870 
5871             --Validate the component dates
5872 
5873             begin
5874                 select wo.first_unit_start_date,
5875                        wo.last_unit_completion_date,
5876                        wo.scheduled_quantity
5877                 into  l_op_sdate,
5878                       l_op_edate,
5879                       l_sched_qty
5880                 from  wip_operations wo
5881                 where wo.wip_entity_id    = p_wip_entity_id
5882                 and  wo.operation_seq_num = l_job_op_seq_num;
5883             exception
5884                 when no_data_found then
5885                     raise e_invalid_job;
5886             end;
5887 
5888             if(l_date_req IS NOT NULL) then
5889                 if ( NVL(l_op_sdate, l_date_req) > l_date_req or
5890                     l_date_req > NVL(l_op_edate, l_date_req)) then
5891 
5892                     raise e_invalid_date;
5893                 end if;
5894             else
5895                 l_date_req := l_op_sdate ;
5896             end if;
5897 
5898             --Validating reqd qty
5899 
5900             if (l_req_qty=0 or l_req_qty is null) then
5901                 raise e_req_qty_zero;
5902             end if;
5903 
5904             /*if l_quantity_per_assembly is null or l_quantity_per_assembly=0 then
5905                 raise e_invalid_qpa;
5906             end if;*/
5907             l_quantity_per_assembly := (l_req_qty/l_sched_qty);
5908 
5909             if l_mrp_net_flag is null or l_mrp_net_flag not in (WIP_CONSTANTS.YES,WIP_CONSTANTS.NO)then
5910                 raise e_invalid_mrp_net_flag;
5911             end if;
5912 
5913             -- For Adhoc components Bill QPA is considered as 1.
5914             --p_v_comp(l_i).component_yield := round(1/l_quantity_per_assembly,6);
5915             p_v_comp(l_i).component_yield := 1;
5916             p_v_comp(l_i).qpa:=l_quantity_per_assembly;
5917 
5918             if(l_wip_supply_type in (WIP_CONSTANTS.ASSY_PULL,WIP_CONSTANTS.OP_PULL) and
5919                l_supply_subinventory IS NULL) then
5920 
5921                 select wip_supply_subinventory, wip_supply_locator_id
5922                 into   l_supply_subinventory, l_supply_locator_id
5923                 from mtl_system_items
5924                 where inventory_item_id = l_item_id
5925                 and organization_id = p_org_id;
5926 
5927                 if l_supply_subinventory is null then
5928                     select default_pull_supply_subinv, default_pull_supply_locator_id
5929                     into l_supply_subinventory, l_supply_locator_id
5930                     from wip_parameters
5931                     where organization_id = p_org_id;
5932                 end if;
5933 
5934                 if l_supply_subinventory is null then
5935                     raise e_inv_supply_info;
5936                 end if;
5937             end if;
5938 
5939             p_v_comp(l_i).supply_subinventory := l_supply_subinventory;
5940             p_v_comp(l_i).supply_locator := l_supply_locator_id;
5941             p_v_comp(l_i).date_required  := l_date_req;
5942 
5943 
5944         ELSIF ( p_handle_wip_cur_op = 'N') THEN     --Future Operation start
5945 
5946             /*begin
5947                 select 1
5948                 into l_comp_exist
5949                 from mtl_system_items_kfv
5950                 where inventory_item_id=l_item_id
5951                 and organization_id=p_org_id;
5952             exception
5953                 when others then
5954                     raise e_invalid_item;
5955             end;*/
5956 
5957             if l_wip_supply_type not in (WIP_CONSTANTS.PUSH,
5958                                          WIP_CONSTANTS.ASSY_PULL,
5959                                          WIP_CONSTANTS.OP_PULL,
5960                                          WIP_CONSTANTS.BULK,
5961                                          WIP_CONSTANTS.VENDOR,
5962                                          WIP_CONSTANTS.PHANTOM) or l_wip_supply_type is null then
5963                 raise e_invalid_wip_supply_type;
5964             end if;
5965 
5966             --check if component already defined for given job and operation.
5967 
5968             begin
5969                 select  1
5970                 into  l_comp_duplicate
5971                 from  wsm_copy_requirement_ops wcro
5972                 where  wcro.wip_entity_id = p_wip_entity_id
5973                 and  wcro.organization_id=p_org_id
5974                 and  wcro.component_item_id=l_item_id
5975                 and  (wcro.operation_seq_num =l_rtg_op_seq_num or
5976                       wcro.operation_seq_num = -1*l_rtg_op_seq_num);
5977             exception
5978                 when others then
5979                     null;
5980             end;
5981 
5982             if l_comp_duplicate is not null then
5983             raise e_dup_component;
5984             end if;
5985 
5986             l_comp_duplicate := null;
5987 
5988             if l_src_phm_item_id <> -1 AND
5989                l_src_phm_item_id <> 0 then
5990 
5991                 begin
5992                     select  1
5993                     into  l_comp_duplicate
5994                     from  wsm_copy_requirement_ops wcro
5995                     where  wcro.wip_entity_id = p_wip_entity_id
5996                     and  wcro.organization_id=p_org_id
5997                     and  wcro.component_item_id=l_src_phm_item_id
5998                     and  wcro.operation_seq_num = -1*l_rtg_op_seq_num;
5999                 exception
6000                     when others then
6001                         raise e_no_parent_phantom;
6002                 end;
6003             end if;
6004 
6005             --Validate the Component Dates
6006 
6007             begin
6008                 select  wco.reco_start_date,wco.reco_completion_date,wco.reco_scheduled_quantity
6009                 into    l_op_sdate,l_op_edate,l_sched_qty
6010                 from    wsm_copy_operations wco
6011                 where   wco.wip_entity_id         = p_wip_entity_id
6012                 and     wco.organization_id       = p_org_id
6013                 and     wco.operation_seq_num     = l_rtg_op_seq_num;
6014             exception
6015                 when no_data_found then
6016                     raise e_invalid_job;
6017             end;
6018 
6019             if(l_date_req IS NOT NULL) then
6020                 if( NVL(l_op_sdate, l_date_req) > l_date_req or
6021                     l_date_req > NVL(l_op_edate, l_date_req) )  then
6022                     raise e_invalid_date;
6023                 end if;
6024             else
6025                 l_date_req := l_op_sdate;
6026             end if;
6027 
6028             --Validating reqd qty
6029             if (l_req_qty=0 or l_req_qty is null) then
6030                 raise e_req_qty_zero;
6031             end if;
6032 
6033             /* if l_quantity_per_assembly is null or l_quantity_per_assembly=0 then
6034                 raise e_invalid_qpa;
6035                end if; */
6036 
6037             l_quantity_per_assembly := (l_req_qty/l_sched_qty);
6038 
6039             if l_mrp_net_flag is null or l_mrp_net_flag not in (WIP_CONSTANTS.YES,WIP_CONSTANTS.NO)then
6040                 raise e_invalid_mrp_net_flag;
6041             end if;
6042 
6043             -- For Adhoc components Bill QPA is considered as 1.
6044             --p_v_comp(l_i).component_yield := round(1/l_quantity_per_assembly,6);
6045 
6046             p_v_comp(l_i).component_yield := 1;
6047 	           p_v_comp(l_i).qpa:=l_quantity_per_assembly;
6048 
6049             if(l_wip_supply_type in (WIP_CONSTANTS.ASSY_PULL,WIP_CONSTANTS.OP_PULL) and
6050                l_supply_subinventory IS NULL) then
6051 
6052                 select wip_supply_subinventory, wip_supply_locator_id
6053                 into   l_supply_subinventory, l_supply_locator_id
6054                 from mtl_system_items
6055                 where inventory_item_id = l_item_id
6056                 and organization_id = p_org_id;
6057 
6058                 if l_supply_subinventory is null then
6059                     select default_pull_supply_subinv, default_pull_supply_locator_id
6060                     into l_supply_subinventory, l_supply_locator_id
6061                     from wip_parameters
6062                     where organization_id = p_org_id;
6063                 end if;
6064 
6065                 if l_supply_subinventory is null then
6066                     raise e_inv_supply_info;
6067                 end if;
6068             end if;
6069 
6070             p_v_comp(l_i).supply_subinventory := l_supply_subinventory;
6071             p_v_comp(l_i).supply_locator := l_supply_locator_id;
6072             p_v_comp(l_i).date_required  := l_date_req;
6073 
6074 
6075         end if;--p handle wip cur op = y
6076     end if; -- sub_type=Del and add
6077 
6078 EXCEPTION
6079     when e_invalid_component then
6080         x_error_code := -1;
6081         fnd_message.set_name('WSM','WSM_COMP_NOT_REQUIRED');
6082         x_error_msg := fnd_message.get;
6083 
6084     when e_invalid_comp_yield then
6085         x_error_code := -1;
6086         fnd_message.set_name('WSM','WSM_COMP_BAD_YIELD');
6087         x_error_msg := fnd_message.get;
6088 
6089     when e_req_qty_not_upd then
6090         x_error_code := -1;
6091         fnd_message.set_name('WSM','WSM_COMP_REQQTY_NON_RECO');
6092         x_error_msg := fnd_message.get;
6093 
6094     /*when e_req_qty_negative then
6095         x_error_code := -1;
6096         fnd_message.set_name('WSM','WSM_COMP_BAD_REQQTY');
6097         x_error_msg := fnd_message.get;*/
6098 
6099     when e_invalid_phantom then
6100         x_error_code := -1;
6101         fnd_message.set_name('WSM','WSM_COMP_PARTIAL_PHANTOM');
6102         x_error_msg := fnd_message.get;
6103 
6104     when e_invalid_date then
6105         x_error_code := -1;
6106         fnd_message.set_name('WSM','WSM_COMP_BAD_DATE');
6107         x_error_msg := fnd_message.get;
6108 
6109 ---Alcoa Enhancement Changes
6110     when e_parent_phantom then
6111         x_error_code := -1;
6112         fnd_message.set_name('WSM','WSM_PARENT_PHANTOM');
6113         x_error_msg := fnd_message.get;
6114 
6115     when e_parent_phantom_comp then
6116         x_error_code := -1;
6117         fnd_message.set_name('WSM','WSM_PARENT_PHANTOM_CHILD');
6118         x_error_msg := fnd_message.get;
6119 
6120     when e_no_parent_phantom then
6121         x_error_code := -1;
6122         fnd_message.set_name('WSM','WSM_NO_PARENT_PHANTOM');
6123         x_error_msg := fnd_message.get;
6124 
6125     when e_invalid_item then
6126         x_error_code := -1;
6127         fnd_message.set_name('WSM','WSM_INVALID_ITEM');
6128         x_error_msg := fnd_message.get;
6129 
6130     when e_quantity_issued_error then
6131         x_error_code := -1;
6132         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6133         fnd_message.set_token('FLD_NAME', 'Quantity_issued');
6134         x_error_msg := fnd_message.get;
6135 
6136     when e_material_trans_err then
6137         x_error_code := -1;
6138         fnd_message.set_name('WSM','WSM_MATERIAL_TRX');
6139         x_error_msg := fnd_message.get;
6140 
6141        when e_phantom_comp then
6142         x_error_code := -1;
6143         fnd_message.set_name('WSM','WSM_PHANTOM_COMPONENT');
6144         x_error_msg := fnd_message.get;
6145 
6146        when e_invalid_wip_supply_type then
6147         x_error_code := -1;
6148         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6149         fnd_message.set_token('FLD_NAME', 'Supply_type');
6150         x_error_msg := fnd_message.get;
6151 
6152         when e_invalid_qpa then
6153         x_error_code := -1;
6154         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6155         fnd_message.set_token('FLD_NAME', 'Quantity_per_assembly');
6156         x_error_msg := fnd_message.get;
6157 
6158        when e_inv_supply_info then
6159         x_error_code := -1;
6160         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6161         fnd_message.set_token('FLD_NAME', 'Supply subinventory/locator combination');
6162         x_error_msg := fnd_message.get;
6163 
6164         when e_invalid_job  then
6165         x_error_code := -1;
6166         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6167         fnd_message.set_token('FLD_NAME', 'Job_op_seq_num / Routing_op_seq_num');
6168         x_error_msg := fnd_message.get;
6169 
6170        when  e_dup_component then
6171         x_error_code := -1;
6172         fnd_message.set_name('WSM','WSM_COMPONENT_DUP');
6173         x_error_msg := fnd_message.get;
6174 
6175        when  e_no_assy_as_comp then
6176         x_error_code := -1;
6177         fnd_message.set_name('WSM','WSM_ASSY_AS_COMP');
6178         x_error_msg := fnd_message.get;
6179 
6180       when e_invalid_mrp_net_flag then
6181         x_error_code := -1;
6182         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
6183         fnd_message.set_token('FLD_NAME', 'Mrp_net_flag');
6184         x_error_msg := fnd_message.get;
6185 
6186 	--Added for bug Fix #7197440
6187     when e_req_qty_zero then  -- bug #7197440
6188         x_error_code := -1;
6189         fnd_message.set_name('WSM','WSM_COPY_QTY_NON_ZERO');
6190         x_error_msg := fnd_message.get;
6191 
6192     when e_req_qty_sign_chg then --  bug #7197440
6193         x_error_code := -1;
6194         fnd_message.set_name('WSM','WSM_COPY_QTY_SIGN');
6195         x_error_msg := fnd_message.get;
6196 
6197     when others then
6198         x_error_code := -1;
6199         x_error_msg := 'Error: validate_wljdi_op_component: (#'||l_stmt_num||') '
6200                        || SUBSTR(SQLERRM, 1,240);
6201 END validate_wljdi_op_component;
6202 
6203 
6204 
6205 procedure update_cumulative_time (
6206         p_wip_entity_id         in number,
6207         p_operation_seq_num     in number,
6208         p_resource_seq_num      in number,
6209         p_instance_id           in number,
6210         p_serial_num            in varchar2,
6211         p_handle_wip_cur_op     in varchar2 )IS
6212 
6213 cursor wip_res_usage (p_wip_entity_id number,
6214                       p_operation_seq_num number,
6215                       p_resource_seq_num number) is
6216     select start_date,
6217            completion_date,
6218            cumulative_processing_time
6219     from   wip_operation_resource_usage
6220     where  wip_entity_id = p_wip_entity_id
6221     and    operation_seq_num = p_operation_seq_num
6222     and    resource_seq_num = p_resource_seq_num
6223     and    NVL(instance_id, -1)          = NVL(p_instance_id, -1)
6224     and    NVL(serial_number, g_nvl_str) = NVL(p_serial_num, g_nvl_str)
6225     order by start_date for update;
6226 
6227 cursor wsm_res_usage (p_wip_entity_id number,
6228                       p_operation_seq_num number,
6229                       p_resource_seq_num number) is
6230     select start_date,
6231            completion_date,
6232            cumulative_processing_time
6233     from   wsm_copy_op_resource_usage
6234     where  wip_entity_id = p_wip_entity_id
6235     and    operation_seq_num = p_operation_seq_num
6236     and    resource_seq_num = p_resource_seq_num
6237     and    NVL(instance_id, -1)          = NVL(p_instance_id, -1)
6238     and    NVL(serial_number, g_nvl_str) = NVL(p_serial_num, g_nvl_str)
6239     order by start_date for update;
6240 
6241 current_cpt number := 0;
6242 l_stmt_num  number;
6243 
6244 begin
6245 
6246     if (p_handle_wip_cur_op = 'Y') then
6247 l_stmt_num  := 10;
6248         for wip_row in wip_res_usage(p_wip_entity_id, p_operation_seq_num, p_resource_seq_num)
6249         loop
6250            current_cpt := current_cpt + wip_datetimes.datetime_diff_to_mins(
6251                                                 wip_row.completion_date,
6252                                                 wip_row.start_date);
6253            update wip_operation_resource_usage
6254            set    cumulative_processing_time = current_cpt
6255            where current of wip_res_usage;
6256         end loop;
6257     else
6258 
6259 l_stmt_num  := 20;
6260         for wsm_row in wsm_res_usage(p_wip_entity_id, p_operation_seq_num, p_resource_seq_num)
6261         loop
6262            current_cpt := current_cpt + wip_datetimes.datetime_diff_to_mins(
6263                                                 wsm_row.completion_date,
6264                                                 wsm_row.start_date);
6265            update wsm_copy_op_resource_usage
6266            set    cumulative_processing_time = current_cpt
6267            where current of wsm_res_usage;
6268         end loop;
6269     end if;
6270 
6271 end update_cumulative_time;
6272 
6273 
6274 
6275 -- **********************************************************************************************
6276 -- PROCESS_INTERFACE_ROWS
6277 -- **********************************************************************************************
6278 
6279 PROCEDURE  process_lbji_rows (
6280         retcode             out nocopy number,
6281         errbuf              out nocopy varchar2,
6282         p_group_id          in  number) is
6283 
6284 cursor c_wlji is
6285     select  header_id,
6286             group_id,
6287             organization_id,
6288             organization_code,
6289             wip_entity_id,
6290             job_name,
6291             load_type,
6292             process_status,
6293             error_code,
6294             error_msg,
6295             num_of_children
6296     from    wsm_lot_job_interface
6297     where   process_status = WIP_CONSTANTS.PENDING
6298     and     NVL(transaction_date, creation_date) <= sysdate+1
6299     and     NVL(group_id, -99) = NVL(p_group_id, NVL(group_id, -99))
6300     and     load_type in (5,6)
6301     order by load_type;
6302 
6303 cursor c_wljdi_no_parent is
6304     select  record_id,
6305             group_id,
6306             organization_id,
6307             organization_code,
6308             wip_entity_id,
6309             job_name,
6310             process_status,
6311             error_code,
6312             error_msg
6313     from    wsm_lot_job_dtl_interface
6314     where   process_status = WIP_CONSTANTS.PENDING
6315     and     parent_header_id IS NULL
6316     and     NVL(group_id, -99) = NVL(p_group_id, NVL(group_id, -99))
6317     and     transaction_date <= sysdate+1;
6318 
6319 
6320 l_header            number;
6321 l_group_id          number;
6322 l_worker_reqid      number;
6323 l_stmt_num          number;
6324 conc_status         boolean;
6325 row_count           number := 0;
6326 l_error_count       number := 0;
6327 
6328 l_temp_num          number := 0;
6329 l_h_rows            number := 0;
6330 l_h_valid_rows      number := 0;    -- num of valid rows in WLJI
6331 l_d_rows            number := 0;
6332 l_d_valid_rows      number := 0;    -- num of valid rows in WLJDI
6333 l_num_jobs          number;
6334 l_num_jobs_per      number := 30;
6335 l_err_msg           varchar2(2000);
6336 l_err_code          number;
6337 l_xst               boolean;
6338 l_h                 number;
6339 l_d                 number;
6340 l_wrk_cnt           number;
6341 l_job_cnt           number;
6342 
6343 l_reqt_id           number := fnd_global.conc_request_id;
6344 l_prog_id           number := fnd_global.conc_program_id;
6345 l_prog_appl_id      number := fnd_global.prog_appl_id;
6346 l_login_id          number := fnd_global.login_id;
6347 l_user_id           number := fnd_global.user_id;
6348 l_resp_id           number := fnd_global.resp_id;
6349 l_resp_appl_id      number := fnd_global.resp_appl_id;
6350 
6351 l_v_group_id        t_number;
6352 l_v_job_gid         t_number;
6353 l_v_group_no        t_number;
6354 
6355 l_job_name_hash     number;
6356 e_launch_worker     exception;
6357 
6358 
6359 BEGIN
6360 
6361 l_stmt_num := 10;
6362 
6363     fnd_file.put_line(fnd_file.log, 'Processing Interface rows...');
6364     retcode := 0;
6365     -- CZHDBG: comment out this because the completion status of the
6366     -- concurrent program cannot be set properly with this call. Why ???
6367     --fnd_global.apps_initialize(l_user_id, l_resp_id, l_resp_appl_id);
6368 
6369 l_stmt_num := 20;
6370     -- load organization PL/SQL table
6371     WSMPLCVA.load_org_table;
6372 
6373     ----------------------------------------------------------------
6374     -- {{ Check PENDING WLJI records, validate org, job id/name
6375     ----------------------------------------------------------------
6376 l_stmt_num := 30;
6377     open c_wlji;
6378     fetch c_wlji bulk collect into
6379         v_wlji_header_id,
6380         v_wlji_group_id,
6381         v_wlji_org_id,
6382         v_wlji_org_code,
6383         v_wlji_wip_entity_id,
6384         v_wlji_job_name,
6385         v_wlji_load_type,
6386         v_wlji_process_status,
6387         v_wlji_err_code,
6388         v_wlji_err_msg,
6389         v_wlji_num_of_children;
6390     if lbji_debug = 'Y' then
6391         fnd_file.put_line(fnd_file.log,
6392             'Number of records in WSM_LOT_JOB_INTERFACE: ' || c_wlji%rowcount);
6393     end if;
6394     l_h_rows := c_wlji%rowcount;
6395     close c_wlji;
6396 
6397     l_h_valid_rows := 0;
6398     if(l_h_rows > 0) then
6399         l_h := v_wlji_header_id.first;
6400         WHILE l_h <= v_wlji_header_id.last
6401         LOOP
6402             v_wlji_err_code(l_h)         := 0;
6403             v_wlji_err_msg(l_h)          := '';
6404             v_wlji_process_status(l_h)   := WIP_CONSTANTS.RUNNING;
6405 
6406             Declare
6407                 e_org_weid_error        exception;
6408             Begin -- {{ check org/job_name for this header_id
6409 l_stmt_num := 40;
6410                 -- Valiadte Org Id
6411                 validate_org_id_name (
6412                     v_wlji_org_id(l_h),
6413                     v_wlji_org_code(l_h),
6414                     l_err_code,
6415                     l_err_msg);
6416                 if l_err_code = -1 then
6417                     raise e_org_weid_error;
6418                 end if;
6419 l_stmt_num := 50;
6420                 -- Valiadte Job Name / WIP Entity ID
6421                 validate_wip_entity_id_name (
6422                     v_wlji_load_type(l_h),
6423                     v_wlji_org_id(l_h),
6424                     v_wlji_wip_entity_id(l_h),
6425                     v_wlji_job_name(l_h),
6426                     l_err_code,
6427                     l_err_msg);
6428                 if l_err_code = -1 then
6429                     raise e_org_weid_error;
6430                 end if;
6431 l_stmt_num := 60;
6432                 -- add valid job name to g_v_job_name
6433                 l_h_valid_rows := l_h_valid_rows + 1;
6434                 l_job_name_hash := dbms_utility.get_hash_value(
6435                                     v_wlji_job_name(l_h) || v_wlji_org_id(l_h),
6436                                     37, 1073741824);
6437                 g_v_job_name(l_job_name_hash)      := v_wlji_job_name(l_h);
6438                 g_v_wip_entity_id(l_job_name_hash) := v_wlji_wip_entity_id(l_h);
6439 
6440             Exception
6441 
6442                 when e_org_weid_error then
6443 l_stmt_num := 70;
6444                     v_wlji_process_status(l_h) := WIP_CONSTANTS.ERROR;
6445                     v_wlji_err_code(l_h) := -1;
6446                     v_wlji_err_msg(l_h) := l_err_msg;
6447                     fnd_file.put_line(fnd_file.log,
6448                         'Error (WLJI.Header_Id = ' || v_wlji_header_id(l_h) || ') : '
6449                         || l_err_msg);
6450                     l_error_count := l_error_count + 1;
6451             End; -- }} check org/job_name for this header_id
6452 
6453             l_h := v_wlji_header_id.next(l_h);
6454         END LOOP;
6455 
6456     end if;  -- if l_h_rows <>0
6457     ----------------------------------------------------------------
6458     -- }} Check PENDING WLJI records, validate org, job id/name
6459     ----------------------------------------------------------------
6460 
6461 
6462     -----------------------------------------------------------------
6463     -- {{ Check PENDING WLJDI records with parent_header_id = NULL
6464     -----------------------------------------------------------------
6465     if lbji_debug = 'Y' then
6466         fnd_file.put_line(fnd_file.log,
6467             'Checking wsm_lot_job_dtl_interface..');
6468     end if;
6469 
6470 l_stmt_num := 100;
6471     open c_wljdi_no_parent;
6472     fetch c_wljdi_no_parent bulk collect into
6473         v_wljdi_record_id,
6474         v_wljdi_group_id,
6475         v_wljdi_org_id,
6476         v_wljdi_org_code,
6477         v_wljdi_wip_entity_id,
6478         v_wljdi_job_name,
6479         v_wljdi_process_status,
6480         v_wljdi_err_code,
6481         v_wljdi_err_msg;
6482     if lbji_debug = 'Y' then
6483         fnd_file.put_line(fnd_file.log,
6484             'Number of independent records in WSM_LOT_JOB_DTL_INTERFACE: '
6485             || c_wljdi_no_parent%rowcount);
6486     end if;
6487     l_d_rows := c_wljdi_no_parent%rowcount;
6488     close c_wljdi_no_parent;
6489 
6490     l_d_valid_rows := 0;
6491     if(l_d_rows > 0) then
6492         l_d := v_wljdi_record_id.first;
6493         WHILE l_d <= v_wljdi_record_id.last
6494         LOOP
6495             v_wljdi_err_code(l_d)        := 0;
6496             v_wljdi_err_msg(l_d)         := NULL;
6497             v_wljdi_process_status(l_d)  := WIP_CONSTANTS.RUNNING;
6498 
6499             Declare
6500                 e_org_weid_error        exception;
6501             Begin -- {{ check org/job_name for this header_id
6502 l_stmt_num := 110;
6503                 -- Valiadte Org Id
6504                 validate_org_id_name (
6505                     v_wljdi_org_id(l_d),
6506                     v_wljdi_org_code(l_d),
6507                     l_err_code,
6508                     l_err_msg);
6509                 if l_err_code = -1 then
6510                     raise e_org_weid_error;
6511                 end if;
6512 l_stmt_num := 120;
6513                 -- Valiadte Job Name / WIP Entity ID
6514                 validate_wip_entity_id_name (
6515                     6,  -- Job update
6516                     v_wljdi_org_id(l_d),
6517                     v_wljdi_wip_entity_id(l_d),
6518                     v_wljdi_job_name(l_d),
6519                     l_err_code,
6520                     l_err_msg);
6521                 if l_err_code = -1 then
6522                     raise e_org_weid_error;
6523                 end if;
6524 
6525                 -- add valid job name to g_v_job_name
6526                 l_d_valid_rows := l_d_valid_rows + 1;
6527                 l_job_name_hash := dbms_utility.get_hash_value(
6528                                     v_wljdi_job_name(l_d) || v_wljdi_org_id(l_d),
6529                                     37, 1073741824);
6530                 g_v_job_name(l_job_name_hash)      := v_wljdi_job_name(l_d);
6531                 g_v_wip_entity_id(l_job_name_hash) := v_wljdi_wip_entity_id(l_d);
6532 
6533             Exception
6534                 when e_org_weid_error then
6535 l_stmt_num := 130;
6536                     v_wljdi_process_status(l_d) := WIP_CONSTANTS.ERROR;
6537                     v_wljdi_err_code(l_d) := -1;
6538                     v_wljdi_err_msg(l_d) := l_err_msg;
6539                     fnd_file.put_line(fnd_file.log,
6540                         'Error: WLJDI(Record_Id = ' || v_wljdi_record_id(l_d) ||') '
6541                         || l_err_msg);
6542                     fnd_file.new_line(fnd_file.log, 3);
6543                     l_error_count := l_error_count +1;
6544 
6545             End; -- }} check org/job_name for this header_id
6546 
6547             l_d := v_wljdi_record_id.next(l_d);
6548         END LOOP;
6549 
6550     end if; -- if l_d_rows <> 0
6551     -----------------------------------------------------------------
6552     -- }} Check PENDING WLJDI records with parent_header_id = NULL
6553     -----------------------------------------------------------------
6554 
6555 
6556     -- Now, all the valid records in WLJI and WLJDI has valid Job Name
6557     -- Will balance worker load according to the number of jobs, will
6558     -- assign all the records of the same job to one worker
6559     -- When p_group_id is given, assign all the job with that group_id to
6560     -- one worker
6561     l_num_jobs := g_v_job_name.COUNT;
6562     if lbji_debug = 'Y' then
6563         fnd_file.put_line(fnd_file.log,
6564             'Number of jobs to be processed: '|| l_num_jobs);
6565     end if;
6566 
6567     if p_group_id is not null then
6568         if(l_h_rows > 0) then
6569 l_stmt_num := 200;
6570             -- Update WLJDI for all record with parent_header_id = header_id
6571             l_h := v_wlji_header_id.first;
6572             WHILE l_h <= v_wlji_header_id.last
6573             LOOP
6574                 update wsm_lot_job_dtl_interface
6575                 set     process_status      = v_wlji_process_status(l_h),
6576                         group_id            = p_group_id,
6577                         organization_id     = v_wlji_org_id(l_h),
6578                         organization_code   = v_wlji_org_code(l_h),
6579                         wip_entity_id       = v_wlji_wip_entity_id(l_h),
6580                         job_name            = v_wlji_job_name(l_h),
6581                         -- BUG3600450 commented
6582                         --error_code          = v_wlji_err_code(l_h),
6583                         --error_msg           = 'See header (header_id=' ||
6584                         --                      v_wlji_header_id(l_h) || ') for error message',
6585                         program_id          = l_prog_id,
6586                         program_update_date = sysdate
6587                 where   parent_header_id = v_wlji_header_id(l_h);
6588                 v_wlji_num_of_children(l_h) := SQL%ROWCOUNT;
6589                 l_h := v_wlji_header_id.next(l_h);
6590             END LOOP;
6591 
6592 l_stmt_num := 210;
6593             -- bulk update wsm_lot_job_interface
6594             forall l_h in v_wlji_header_id.first..v_wlji_header_id.last
6595             update  wsm_lot_job_interface
6596             set     process_status              = v_wlji_process_status(l_h),
6597                     organization_id             = v_wlji_org_id(l_h),
6598                     organization_code           = v_wlji_org_code(l_h),
6599                     wip_entity_id               = v_wlji_wip_entity_id(l_h),
6600                     job_name                    = v_wlji_job_name(l_h),
6601                     error_code                  = v_wlji_err_code(l_h),
6602                     error_msg                   = v_wlji_err_msg(l_h),
6603                     num_of_children             = v_wlji_num_of_children(l_h),
6604                     program_id                  = l_prog_id,
6605                     program_update_date         = sysdate
6606             where   header_id = v_wlji_header_id(l_h);
6607 
6608         end if;  -- if l_h_rows <>0
6609 
6610         if(l_d_rows > 0) then
6611 l_stmt_num := 220;
6612             forall l_d in v_wljdi_record_id.first..v_wljdi_record_id.last
6613             update  wsm_lot_job_dtl_interface
6614             set     process_status              = v_wljdi_process_status(l_d),
6615                     organization_id             = v_wljdi_org_id(l_d),
6616                     organization_code           = v_wljdi_org_code(l_d),
6617                     wip_entity_id               = v_wljdi_wip_entity_id(l_d),
6618                     job_name                    = v_wljdi_job_name(l_d),
6619                     error_code                  = v_wljdi_err_code(l_d),
6620                     error_msg                   = v_wljdi_err_msg(l_d),
6621                     program_id                  = l_prog_id,
6622                     program_update_date         = sysdate
6623             where   record_id = v_wljdi_record_id(l_d);
6624         end if; -- if l_d_rows <> 0
6625 
6626 l_stmt_num := 230;
6627         if l_num_jobs > 0 then
6628             l_worker_reqid := FND_REQUEST.SUBMIT_REQUEST (
6629                             application => 'WSM',
6630                             program     => 'WSMLNCHW',
6631                             sub_request => FALSE,
6632                             argument1   => p_group_id,
6633                             argument2   => l_num_jobs );
6634             if l_worker_reqid = 0 then
6635                 raise e_launch_worker;
6636             else
6637                 COMMIT;
6638             end if;
6639             fnd_file.put_line(fnd_file.log, 'Request_id: '||l_worker_reqid||' submitted');
6640         else -- l_num_jobs <= 0
6641             fnd_file.put_line(fnd_file.log, 'No rows found in interface table');
6642         end if;
6643 
6644     else -- p_group_id is null
6645 
6646         if l_num_jobs > 0 then
6647 l_stmt_num := 300;
6648 
6649             if mod(l_num_jobs, g_no_of_workers) = 0 then
6650                 l_num_jobs_per := l_num_jobs / g_no_of_workers;
6651             else
6652                 l_num_jobs_per := floor(l_num_jobs / g_no_of_workers) + 1;
6653             end if;
6654 
6655             if l_num_jobs_per < g_batch_size then
6656                 l_num_jobs_per := g_batch_size;
6657             end if;
6658 
6659             l_job_cnt := 0;
6660             l_wrk_cnt := 0;
6661 
6662 l_stmt_num := 310;
6663             -- {{ balance load among workers by setting group_id
6664             if(l_h_rows > 0) then
6665                 l_h := v_wlji_header_id.first;
6666                 WHILE l_h <= v_wlji_header_id.last
6667                 LOOP
6668                     if v_wlji_process_status(l_h) <> WIP_CONSTANTS.ERROR then
6669                         l_job_name_hash := dbms_utility.get_hash_value(
6670                                             v_wlji_job_name(l_h) || v_wlji_org_id(l_h),
6671                                             37, 1073741824);
6672                         if not l_v_job_gid.exists(l_job_name_hash) then -- one new job name
6673                             if mod(l_job_cnt, l_num_jobs_per) = 0 then
6674                                 l_wrk_cnt := l_wrk_cnt + 1;  -- one more worker
6675                                 -- get a new group id
6676                                 select wsm_lot_job_interface_s.NEXTVAL
6677                                 into   l_group_id
6678                                 from   dual;
6679                                 l_v_group_id(l_wrk_cnt) := l_group_id;
6680                                 l_v_group_no(l_wrk_cnt) := 0;
6681                             end if;
6682                             l_job_cnt := l_job_cnt+1;
6683                             l_v_group_no(l_wrk_cnt) := l_v_group_no(l_wrk_cnt)+1;
6684                             l_v_job_gid(l_job_name_hash) := l_v_group_id(l_wrk_cnt);
6685                         end if;
6686                         v_wlji_group_id(l_h) := l_v_job_gid(l_job_name_hash);
6687                     end if;
6688                     l_h := v_wlji_header_id.next(l_h);
6689                 END LOOP;
6690             end if;
6691 
6692 l_stmt_num := 320;
6693             if(l_d_rows > 0) then
6694                 l_d := v_wljdi_record_id.first;
6695                 WHILE l_d <= v_wljdi_record_id.last
6696                 LOOP
6697                     if v_wljdi_process_status(l_d) <> WIP_CONSTANTS.ERROR then
6698                         l_job_name_hash := dbms_utility.get_hash_value(
6699                                             v_wljdi_job_name(l_d) || v_wljdi_org_id(l_d),
6700                                             37, 1073741824);
6701                         if not l_v_job_gid.exists(l_job_name_hash) then -- a new job
6702                             if mod(l_job_cnt, l_num_jobs_per) = 0 then
6703                                 l_wrk_cnt := l_wrk_cnt +1;  -- one more worker
6704                                 select wsm_lot_job_interface_s.NEXTVAL
6705                                 into   l_group_id
6706                                 from   dual;
6707                                 l_v_group_id(l_wrk_cnt) := l_group_id;
6708                                 l_v_group_no(l_wrk_cnt) := 0;
6709                             end if;
6710                             l_job_cnt := l_job_cnt+1;
6711                             l_v_group_no(l_wrk_cnt) := l_v_group_no(l_wrk_cnt)+1;
6712                             l_v_job_gid(l_job_name_hash) := l_group_id;
6713                         end if;
6714                         v_wljdi_group_id(l_d) := l_v_job_gid(l_job_name_hash);
6715                     end if;
6716                     l_d := v_wljdi_record_id.next(l_d);
6717                 END LOOP;
6718             end if;
6719             -- }} balance load among workers by setting group_id
6720 
6721         end if; -- if l_num_jobs > 0
6722 
6723         if(l_h_rows > 0) then
6724 
6725 l_stmt_num := 330;
6726             -- Update WLJDI for all record with parent_header_id = header_id
6727             l_h := v_wlji_header_id.first;
6728             WHILE l_h <= v_wlji_header_id.last
6729             LOOP
6730                 update wsm_lot_job_dtl_interface
6731                 set     process_status          = v_wlji_process_status(l_h),
6732                         group_id                = v_wlji_group_id(l_h),
6733                         organization_id         = v_wlji_org_id(l_h),
6734                         organization_code       = v_wlji_org_code(l_h),
6735                         wip_entity_id           = v_wlji_wip_entity_id(l_h),
6736                         job_name                = v_wlji_job_name(l_h),
6737                         error_code              = v_wlji_err_code(l_h),
6738                         error_msg               = v_wlji_err_msg(l_h),
6739                         program_id              = l_prog_id,
6740                         program_update_date     = sysdate
6741                 where   parent_header_id = v_wlji_header_id(l_h);
6742                 v_wlji_num_of_children(l_h) := SQL%ROWCOUNT;
6743                 l_h := v_wlji_header_id.next(l_h);
6744             END LOOP;
6745 
6746 l_stmt_num := 340;
6747             -- bulk update wsm_lot_job_interface
6748             forall l_h in v_wlji_header_id.first..v_wlji_header_id.last
6749             update  wsm_lot_job_interface
6750             set     process_status              = v_wlji_process_status(l_h),
6751                     group_id                    = v_wlji_group_id(l_h), -- assigned gid
6752                     organization_id             = v_wlji_org_id(l_h),
6753                     organization_code           = v_wlji_org_code(l_h),
6754                     wip_entity_id               = v_wlji_wip_entity_id(l_h),
6755                     job_name                    = v_wlji_job_name(l_h),
6756                     error_code                  = v_wlji_err_code(l_h),
6757                     error_msg                   = v_wlji_err_msg(l_h),
6758                     num_of_children             = v_wlji_num_of_children(l_h),
6759                     program_id                  = l_prog_id,
6760                     program_update_date         = sysdate
6761             where   header_id = v_wlji_header_id(l_h);
6762 
6763         end if;  -- if l_h_rows <>0
6764 
6765         if(l_d_rows > 0) then
6766 l_stmt_num := 350;
6767             forall l_d in v_wljdi_record_id.first..v_wljdi_record_id.last
6768             update  wsm_lot_job_dtl_interface
6769             set     process_status              = v_wljdi_process_status(l_d),
6770                     group_id                    = v_wljdi_group_id(l_d), -- assigned gid
6771                     organization_id             = v_wljdi_org_id(l_d),
6772                     organization_code           = v_wljdi_org_code(l_d),
6773                     wip_entity_id               = v_wljdi_wip_entity_id(l_d),
6774                     job_name                    = v_wljdi_job_name(l_d),
6775                     error_code                  = v_wljdi_err_code(l_d),
6776                     error_msg                   = v_wljdi_err_msg(l_d),
6777                     program_id                  = l_prog_id,
6778                     program_update_date         = sysdate
6779             where   record_id = v_wljdi_record_id(l_d);
6780         end if; -- if l_d_rows <> 0
6781 
6782         if l_num_jobs > 0 then
6783             fnd_file.put_line(fnd_file.log, 'Total pending jobs = '       || l_num_jobs);
6784             fnd_file.put_line(fnd_file.log, 'Number of workers = '        || g_no_of_workers);
6785             fnd_file.put_line(fnd_file.log, 'Batch size = '               || g_batch_size);
6786             fnd_file.put_line(fnd_file.log, 'Number of jobs per worker = '|| l_num_jobs_per);
6787             fnd_file.put_line(fnd_file.log, 'Number of workers launched= '|| l_wrk_cnt);
6788 
6789 l_stmt_num := 360;
6790             l_temp_num := l_v_group_id.first;
6791             WHILE l_temp_num <= l_v_group_id.last
6792             LOOP
6793                 l_worker_reqid := FND_REQUEST.SUBMIT_REQUEST (
6794                                 application => 'WSM',
6795                                 program     => 'WSMLNCHW',
6796                                 sub_request => FALSE,
6797                                 argument1   => l_v_group_id(l_temp_num),
6798                                 argument2   => l_v_group_no(l_temp_num) );
6799                 if l_worker_reqid = 0 then
6800                     raise e_launch_worker;
6801                 end if;
6802                 l_temp_num := l_v_group_id.next(l_temp_num);
6803             END LOOP;
6804             COMMIT;
6805 
6806         end if; -- if l_d_rows <> 0
6807 
6808     end if; -- p_group_id is null
6809 
6810     if l_error_count <> 0 then
6811         retcode := 1;
6812         errbuf := 'The manager marked at least one row as errored';
6813         fnd_file.put_line(fnd_file.log,errbuf);
6814         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
6815         COMMIT;
6816     end if;
6817 
6818 EXCEPTION
6819 
6820     when e_launch_worker then
6821         rollback;
6822         retcode := 1;
6823         errbuf := 'Error: process_interface_rows: (#'||l_stmt_num||') fail to launch worker';
6824         fnd_file.put_line(fnd_file.log,errbuf);
6825         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',errbuf);
6826         COMMIT;
6827 
6828     when others then
6829         rollback;
6830         retcode := 1;
6831         errbuf := 'Error: process_interface_rows: (#'||l_stmt_num||') '
6832                   || SUBSTR(SQLERRM, 1,240);
6833         fnd_file.put_line(fnd_file.log,errbuf);
6834         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',errbuf);
6835         COMMIT;
6836 
6837 END process_lbji_rows;
6838 
6839 
6840 
6841 -- **********************************************************************************************
6842 -- LAUNCH WORKER
6843 -- **********************************************************************************************
6844 
6845 PROCEDURE  launch_worker(
6846         retcode             out nocopy number,
6847         errbuf              out nocopy varchar2,
6848         p_group_id          in  number,
6849         p_alotted_jobs      in  number  ) is
6850 
6851 -- ==============================================================================================
6852 -- cursors used to bulk bind data from wlji to PL/SQL tables
6853 -- ==============================================================================================
6854 cursor csr_wlji is
6855     select   error_code,
6856              error_msg,
6857              --error,
6858              last_update_date,
6859              request_id,
6860              program_id,
6861              program_application_id,
6862              last_updated_by,
6863              creation_date,
6864              created_by,
6865              last_update_login,
6866              program_update_date,
6867              last_updated_by_name,
6868              created_by_name,
6869              organization_id,
6870              organization_code,
6871              wip_entity_id,
6872              job_name,
6873              lot_number,
6874              primary_item_id,
6875              header_id,
6876              process_status,
6877              --process_phase,
6878              routing_reference_id,
6879              completion_subinventory,
6880              completion_locator_id,
6881              mode_flag,
6882              group_id,
6883              load_type,
6884              status_type,
6885              --old_start_quantity,
6886              --old_completion_date,
6887              bom_reference_id,
6888              bom_revision_date,
6889              routing_revision_date,
6890              wip_supply_type,
6891              class_code,
6892              description,
6893              firm_planned_flag,
6894              alternate_routing_designator,
6895              alternate_bom_designator,
6896              demand_class,
6897              start_quantity,
6898              first_unit_start_date,
6899              --first_unit_completion_date,
6900              --last_unit_start_date,
6901              last_unit_completion_date,
6902              scheduling_method,
6903              routing_revision,
6904              bom_revision,
6905              schedule_group_id,
6906              schedule_group_name,
6907              build_sequence,
6908              net_quantity,
6909              allow_explosion,
6910              --old_status_type,
6911              --interface_id,
6912              coproducts_supply,
6913              source_code,
6914              source_line_id,
6915              process_type,
6916              --processing_work_days,
6917              --daily_production_rate,
6918              line_id,
6919              --line_code,
6920              --lot_control_code,
6921              --repetitive_schedule_id,
6922              --parent_group_id,
6923              attribute_category,
6924              attribute1,
6925              attribute2,
6926              attribute3,
6927              attribute4,
6928              attribute5,
6929              attribute6,
6930              attribute7,
6931              attribute8,
6932              attribute9,
6933              attribute10,
6934              attribute11,
6935              attribute12,
6936              attribute13,
6937              attribute14,
6938              attribute15,
6939              --primary_item_segments,
6940              --bom_reference_segments,
6941              --routing_reference_segments,
6942              --completion_locator_segments,
6943              --descriptive_flex_segments,
6944              project_id,
6945              --project_name,
6946              --project_number,
6947              --project_costed,
6948              task_id,
6949              --task_name,
6950              --task_number,
6951              delivery_id,
6952              end_item_unit_number,
6953              overcompletion_tolerance_type,
6954              overcompletion_tolerance_value,
6955              kanban_card_id,
6956              priority,
6957              due_date,
6958              job_type,
6959              date_released,
6960              transaction_date,
6961              num_of_children
6962              --, NVL(infinite_schedule_flag, 'N')
6963     from     wsm_lot_job_interface
6964     where    group_id = p_group_id
6965     and      process_status = 2 -- WIP_CONSTANTS.RUNNING;
6966     order by organization_id,
6967              load_type,
6968              NVL(transaction_date, creation_date),
6969              priority,
6970              due_date;
6971 
6972 cursor c_wljdi_jobs_no_parent is
6973     select  unique
6974             wip_entity_id,
6975             organization_id
6976     from    wsm_lot_job_dtl_interface
6977     where   process_status = WIP_CONSTANTS.RUNNING
6978     and     parent_header_id IS NULL
6979     and     group_id = p_group_id
6980     and     transaction_date <= sysdate+1
6981     order by organization_id;
6982 
6983 -- ==============================================================================================
6984 -- other variables
6985 -- ==============================================================================================
6986 
6987 -- control variables
6988 prev_rowcount                   number := 0;
6989 req_phase                       varchar2(2000);
6990 req_status                      varchar2(2000);
6991 req_devphase                    varchar2(2000);
6992 req_devstatus                   varchar2(2000);
6993 req_message                     varchar2(1000);
6994 req_wait                        boolean;
6995 conc_status                     boolean;
6996 
6997 l_del_int_prof_value            number;
6998 l_stmt_num                      number;
6999 l_error_count                   number := 0;
7000 l_warning_count                 number := 0;
7001 l_atleast_one_row_in_mmtt       number := 0;
7002 l_inv_worker_req_id             number := 0;
7003 l_mmtt_txn_hdr_id               number;
7004 l_txn_tmp_hdr_id                number;
7005 l_return_value                  number := 0;
7006 l_error_code                    number := 0;
7007 l_error_msg                     varchar2(2000) := null;
7008 l_aux_mesg                      varchar2(240)  := null;
7009 l_skip_update_wo                number;
7010 l_return_status                 varchar2(1);
7011 l_num_records                   number := 0;
7012 l_batch_grp_id                  number;
7013 l_wsli_loaded                   boolean;
7014 l_req_request_id                number;
7015 l_temp_num                      number := 0;
7016 l_temp_class                    varchar2(10);
7017 l_temp_boolean                  boolean;
7018 l_date_text                     varchar2(100);
7019 l_xst                           boolean := true;
7020 l_user                          number := fnd_global.user_id;
7021 l_h                             number;         -- index for WLJI records
7022 l_d                             number;         -- index for WLJDI records
7023 
7024 dummy_number                    number;
7025 dummy_varchar                   varchar2(3);
7026 dummy_date                      date;
7027 dummy_err_code                  number;
7028 dummy_err_msg                   varchar2(2000);
7029 dummy_segs                      varchar2(10000); -- dummy large varchar2
7030 
7031 -- local variables
7032 hash_str                        varchar2(100);
7033 hash_value                      number;
7034 l_rtg_seq_id                    number;
7035 l_common_rtg_seq_id             number;
7036 l_common_bom_seq_id             number;
7037 l_default_compl_subinv          varchar2(10);
7038 l_default_compl_loc_id          number;
7039 l_sub_loc_control               number;
7040 l_org_loc_control               number;
7041 l_item_loc_control              number;
7042 l_restrict_locators_code        number;
7043 l_item_id                       number;
7044 l_min_op_seq_num                number;
7045 l_department_id                 number;
7046 l_scrap_account_10              number;
7047 l_est_scrap_abs_account_10      number;
7048 l_rev_date                      date;
7049 l_source_item_rev               varchar2(3);
7050 --l_source_item_rev_date        date;           -- Del: bug 2963225
7051 l_rev_control_code              number;         -- Add: bug 2963225
7052 l_start_lot_revision            varchar2(3);         -- Add: bug 2963225
7053 l_locator_id                    number;
7054 l_job_type                      number;
7055 l_kanban_size                   number;
7056 l_qoh                           number;
7057 l_att                           number;
7058 l_atr                           number;
7059 l_component_yield_factor        number;
7060 l_component_quantity            number;
7061 l_required_qpa                  number;
7062 l_quantity_tobe_issued          number;
7063 l_start_op_seq_id               number;
7064 l_start_op_seq_num              number;
7065 l_txnexist                      number;
7066 l_qntydiff                      number;
7067 l_mtl_locator_type              number;
7068 l_osp_op_seq_num                number;
7069 l_est_scrap_account             number;
7070 l_est_scrap_var_account         number;
7071 l_src_lot_number                wsm_starting_lots_interface.lot_number%type;
7072 l_src_inv_item_id               NUMBER;
7073 l_scheduling_mode               NUMBER; -- ADD BUG 3479666
7074 
7075 l_job_type_code                 varchar2(30);
7076 l_assembly_name                 varchar2(40);
7077 l_org_code                      varchar2(3);
7078 l_meaning                       varchar2(240);
7079 
7080 -------------------------------------------------------------------
7081 -- Attention: all these l_cur_% variables are used to store current
7082 -- values of a job for UPDATE, please do not change their values !!
7083 -------------------------------------------------------------------
7084 l_cur_coprod_supply             number;
7085 l_cur_schd_start_date           date;
7086 l_cur_schd_compl_date           date;
7087 l_cur_primary_item_id           number;
7088 l_cur_class_code                varchar2(10);
7089 l_cur_start_date                date;
7090 l_cur_complete_date             date;
7091 l_cur_start_qty                 number;
7092 l_cur_net_qty                   number;
7093 l_cur_bom_revision              varchar2(3);
7094 l_cur_rtg_revision              varchar2(3);
7095 l_cur_status_type               number:=0;
7096 l_cur_firm_planned_flag         number;
7097 l_cur_job_type                  number;
7098 l_cur_date_released             date;
7099 l_cur_bom_ref_id                number;
7100 l_cur_alt_bom                   varchar2(10);
7101 l_cur_rtg_ref_id                number;
7102 l_cur_alt_rtg                   varchar2(10);
7103 l_cur_bom_rev_date              date;
7104 l_cur_rtg_rev_date              date;
7105 l_cur_com_rtg_seq_id            number;
7106 l_cur_com_bom_seq_id            number;
7107 l_cur_wip_supply_type           number;
7108 l_cur_compl_subinv              varchar2(10);
7109 l_cur_compl_locator             number;
7110 l_cur_kanban_card_id            number;
7111 l_cur_schedule_group_id         number;
7112 l_cur_build_sequence            number;
7113 l_cur_line_id                   number;
7114 
7115 l_chg_bom_alt                   number;
7116 l_chg_rtg_alt                   number;
7117 l_chg_alt_flag                  number;
7118 l_chg_bom_ref                   number;
7119 l_chg_rtg_ref                   number;
7120 l_chg_ref_flag                  number;
7121 l_chg_rtg_rev                   number;     -- Add: bug3202534
7122 l_chg_bom_rev                   number;     -- Add: bug3202534
7123 l_chg_rev_flag                  number;     -- Add: bug3202534
7124 l_inf_sche_flag                 varchar2(1);
7125 --Bug 5112899: added follwoing columns
7126 l_basis_type                    NUMBER;
7127 l_include_yield                 NUMBER;
7128 l_bill_req_qpa                  NUMBER;
7129 l_mo_org_id                     NUMBER;                                   -- Add: bug5485653
7130 l_ou_id                         NUMBER;                                   -- Add: bug5485653
7131 l_org_acct_ctxt                 VARCHAR2(30):= 'Accounting Information';  -- Add: bug5485653
7132 
7133 l_wip_entity_name               varchar2(80); -- Add: bug 5242298
7134 l_dummy_job_name		varchar2(80); -- Add: bug 5242298
7135 
7136 /*  ADDED FOLLOWING VARIABLES FOR 12.1 SUBSTITUTE COMPONENT PROJECT ****************************************************
7137 **********************************************************************************************************************/
7138 
7139 l_is_src_lot_sub               NUMBER := 0;
7140 l_primary_comp_item_id         NUMBER := 0;
7141 l_sub_comp_bill_qpa            NUMBER := 0;
7142 l_sub_comp_qpa                 NUMBER := 0;
7143 l_segment1                     VARCHAR2(100);
7144 l_segment2                     VARCHAR2(100);
7145 l_segment3                     VARCHAR2(100);
7146 l_segment4                     VARCHAR2(100);
7147 l_segment5                     VARCHAR2(100);
7148 l_segment6                     VARCHAR2(100);
7149 l_segment7                     VARCHAR2(100);
7150 l_segment8                     VARCHAR2(100);
7151 l_segment9                     VARCHAR2(100);
7152 l_segment10                    VARCHAR2(100);
7153 l_segment11                    VARCHAR2(100);
7154 l_segment12                    VARCHAR2(100);
7155 l_segment13                    VARCHAR2(100);
7156 l_segment14                    VARCHAR2(100);
7157 l_segment15                    VARCHAR2(100);
7158 l_segment16                    VARCHAR2(100);
7159 l_segment17                    VARCHAR2(100);
7160 l_segment18                    VARCHAR2(100);
7161 l_segment19                    VARCHAR2(100);
7162 l_segment20                    VARCHAR2(100);
7163 
7164 
7165 
7166 /* END OF VARIABLES ADDED FOR 12.1 SUBSTITUTE COMPONENT PROJECT **********************************************************
7167 ***************************************************************************************************************************/
7168 -- defined exceptions
7169 e_invalid_id                    exception;
7170 e_invalid_job_name              exception;
7171 e_abort_request                 exception;
7172 e_build_job                     exception;
7173 e_update_job                    exception;
7174 e_invalid_qty                   exception;
7175 
7176 
7177 BEGIN   -- for launch_worker
7178 
7179     retcode := 0;       -- bugfix 2845397: set the code to 0 (success)
7180 
7181     SAVEPOINT savepoint_top;
7182 
7183 -- ==============================================================================================
7184 -- Bulk fetching data into PL/SQL tables for ease of validation
7185 -- ==============================================================================================
7186 
7187     begin
7188 l_stmt_num := 10;
7189         WSMPLCVA.load_org_table;
7190 
7191 l_stmt_num := 20;
7192         WSMPLCVA.load_subinventory;
7193 
7194 l_stmt_num := 30;
7195         WSMPLCVA.load_class_code;
7196         if lbji_debug = 'Y' then
7197             fnd_file.put_line(fnd_file.log,
7198                 'Loading org/subinv/class-code values into memory (once per worker).. OK.');
7199         end if;
7200     exception
7201         when others then
7202             raise e_abort_request;
7203     end;
7204 
7205 
7206 l_stmt_num := 40;
7207     open csr_wlji;
7208     LOOP  -- {{{ outer loop
7209 
7210         l_wsli_loaded := false; --i.e. run the load wsli routine for every batch
7211 
7212 -- ==============================================================================================
7213 -- Bulk fetching data from wlji to PL/SQL tables
7214 -- ==============================================================================================
7215 
7216         fetch csr_wlji bulk collect into
7217             v_wlji_err_code,
7218             v_wlji_err_msg,
7219             --v_wlji_error,
7220             v_wlji_last_updt_date,
7221             v_wlji_request_id,
7222             v_wlji_program_id,
7223             v_wlji_program_appl_id,
7224             v_wlji_last_updt_by,
7225             v_wlji_creation_date,
7226             v_wlji_created_by,
7227             v_wlji_last_updt_login,
7228             v_wlji_prog_updt_date,
7229             v_wlji_last_updt_by_name,
7230             v_wlji_created_by_name,
7231             v_wlji_org_id,
7232             v_wlji_org_code,
7233             v_wlji_wip_entity_id,
7234             v_wlji_job_name,
7235             v_wlji_lot_number,
7236             v_wlji_item_id,
7237             v_wlji_header_id,
7238             v_wlji_process_status,
7239             --v_wlji_process_phase,
7240             v_wlji_rtg_ref_id,
7241             v_wlji_compl_subinv,
7242             v_wlji_compl_loc_id,
7243             v_wlji_mode_flag,
7244             v_wlji_group_id,
7245             v_wlji_load_type,
7246             v_wlji_status_type,
7247             --v_wlji_old_start_quantity,
7248             --v_wlji_old_completion_date,
7249             v_wlji_bom_ref_id,
7250             v_wlji_bom_rev_date,
7251             v_wlji_rtg_rev_date,
7252             v_wlji_wip_supply_type,
7253             v_wlji_class_code,
7254             v_wlji_description,
7255             v_wlji_firm_planned_flag,
7256             v_wlji_alt_rtg,
7257             v_wlji_alt_bom,
7258             v_wlji_demand_class,
7259             v_wlji_start_quantity,
7260             v_wlji_fusd,
7261             --v_wlji_fucd,
7262             --v_wlji_lusd,
7263             v_wlji_lucd,
7264             v_wlji_scheduling_method,
7265             v_wlji_rtg_revision,
7266             v_wlji_bom_revision,
7267             v_wlji_schedule_group_id,
7268             v_wlji_schedule_group_name,
7269             v_wlji_build_sequence,
7270             v_wlji_net_quantity,
7271             v_wlji_allow_explosion,
7272             --v_wlji_old_status_type,
7273             --v_wlji_interface_id,
7274             v_wlji_coproducts_supply,
7275             v_wlji_source_code,
7276             v_wlji_src_line_id,
7277             v_wlji_process_type,
7278             --v_wlji_processing_work_days,
7279             --v_wlji_daily_production_rate,
7280             v_wlji_line_id,
7281             --v_wlji_line_code,
7282             --v_wlji_lot_control_code,
7283             --v_wlji_repetitive_schedule_id,
7284             --v_wlji_parent_group_id,
7285             v_wlji_attribute_category,
7286             v_wlji_attribute1,
7287             v_wlji_attribute2,
7288             v_wlji_attribute3,
7289             v_wlji_attribute4,
7290             v_wlji_attribute5,
7291             v_wlji_attribute6,
7292             v_wlji_attribute7,
7293             v_wlji_attribute8,
7294             v_wlji_attribute9,
7295             v_wlji_attribute10,
7296             v_wlji_attribute11,
7297             v_wlji_attribute12,
7298             v_wlji_attribute13,
7299             v_wlji_attribute14,
7300             v_wlji_attribute15,
7301             --v_wlji_primary_item_segs,
7302             --v_wlji_bom_ref_segs,
7303             --v_wlji_rtg_ref_segs,
7304             --v_wlji_compl_loc_segs,
7305             --v_wlji_desc_flx_segs,
7306             v_wlji_project_id,
7307             --v_wlji_project_name,
7308             --v_wlji_project_number,
7309             --v_wlji_project_costed,
7310             v_wlji_task_id,
7311             --v_wlji_task_name,
7312             --v_wlji_task_number,
7313             v_wlji_delivery_id,
7314             v_wlji_end_item_unit_number,
7315             v_wlji_overcompl_tol_type,
7316             v_wlji_overcompl_tol_value,
7317             v_wlji_kanban_card_id,
7318             v_wlji_priority,
7319             v_wlji_due_date,
7320             v_wlji_job_type,
7321             v_wlji_date_released,   --bugfix 2697295
7322             v_wlji_transaction_date,
7323             v_wlji_num_of_children
7324             --, v_wlji_inf_schedule_flag
7325         limit g_batch_size;
7326         if lbji_debug = 'Y' then
7327             fnd_file.put_line(fnd_file.log,
7328                 'Number of rows loaded for the current batch: '||csr_wlji%rowcount);
7329         end if;
7330 
7331         if csr_wlji%rowcount - prev_rowcount <> 0 then  -- {{ one batch
7332 l_stmt_num := 50;
7333             -- getting the header_id to be populated for mmtt
7334             select mtl_material_transactions_s.nextval
7335             into   l_mmtt_txn_hdr_id
7336             from   dual;
7337 
7338 l_stmt_num := 60;
7339             -- updating the group_id column of wlji with a number unique for this particular batch.
7340             -- This'll help when I select corresponding rows from wsli.
7341 
7342             select wsm_lot_job_interface_s.NEXTVAL
7343             into   l_batch_grp_id
7344             from   dual;
7345 
7346 l_stmt_num := 60.1;
7347             forall indx in v_wlji_header_id.first..v_wlji_header_id.last
7348             update wsm_lot_job_interface
7349             set    group_id = l_batch_grp_id
7350             where  header_id = v_wlji_header_id(indx);
7351 
7352 l_stmt_num := 60.2;
7353             l_h := v_wlji_header_id.first;
7354             WHILE l_h <= v_wlji_header_id.last
7355             LOOP
7356                 if(v_wlji_num_of_children(l_h) > 0) then
7357                     update wsm_lot_job_dtl_interface
7358                     set     group_id                = l_batch_grp_id
7359                     where   parent_header_id = v_wlji_header_id(l_h);
7360                 end if;
7361                 l_h := v_wlji_header_id.next(l_h);
7362             END LOOP;
7363 
7364 
7365 -- ==============================================================================================
7366 -- BEGIN: Processing of data {{{
7367 -- ==============================================================================================
7368 l_stmt_num := 70;
7369             v_idx := v_wlji_header_id.first;
7370             while v_idx <= v_wlji_header_id.last
7371             loop  -- {{{ inner loop
7372                 if lbji_debug = 'Y' then
7373                     fnd_file.put_line(fnd_file.log,'------------------------------------------------------');
7374                     fnd_file.put_line(fnd_file.log,' wlji.header_id: '|| v_wlji_header_id(v_idx));
7375                     fnd_file.put_line(fnd_file.log,' wlji.job name: ' || v_wlji_job_name(v_idx));
7376                     fnd_file.put_line(fnd_file.log,'------------------------------------------------------');
7377                 end if;
7378 
7379                 SAVEPOINT savepoint_skip_job;
7380 
7381                 BEGIN  -- {{{ main block
7382 
7383                     l_rtg_seq_id := '';
7384                     l_common_bom_seq_id := 0;
7385                     l_common_rtg_seq_id := 0;
7386                     l_error_code := 0;
7387                     l_return_value := 0;
7388                     l_error_msg:= NULL;
7389                     l_aux_mesg:= NULL;
7390                     l_return_status := FND_API.G_RET_STS_SUCCESS;
7391                     l_temp_num:= 0;
7392                     l_xst:= true;
7393                     hash_str := '';
7394                     hash_value := 0;
7395                     l_num_records:= 0;
7396 
7397                     l_default_compl_subinv := '';
7398                     l_default_compl_loc_id := 0;
7399                     l_sub_loc_control := 0;
7400                     l_org_loc_control := 0;
7401                     l_item_loc_control := 0;
7402                     l_restrict_locators_code := 0;
7403                     l_item_id := 0;
7404                     l_locator_id := 0;
7405                     l_rev_date := '';
7406                     l_date_text := '';
7407                     l_component_quantity := 0;
7408                     l_qoh := 0;
7409                     l_att := 0;
7410                     l_atr := 0;
7411                     l_component_yield_factor := 0;
7412                     l_required_qpa := 0;
7413                     l_quantity_tobe_issued := 0;
7414                     l_start_op_seq_id := 0;
7415                     l_start_op_seq_num := 0;
7416                     l_department_id := 0;
7417                     l_skip_update_wo:=0;
7418                     l_est_scrap_account     := NULL;
7419                     l_est_scrap_var_account := NULL;
7420                     l_src_lot_number        := NULL;
7421                     l_src_inv_item_id       := NULL;
7422 
7423                     dummy_number:=0;
7424                     dummy_varchar:='';
7425                     dummy_date:=sysdate;
7426                     dummy_err_code := 0;
7427                     dummy_err_msg := NULL;
7428                     dummy_segs := '';
7429 
7430                     l_chg_bom_ref  := 0;
7431                     l_chg_rtg_ref  := 0;
7432                     l_chg_ref_flag := 0;
7433 
7434                     l_chg_bom_alt  := 0;
7435                     l_chg_rtg_alt  := 0;
7436                     l_chg_alt_flag := 0;
7437 
7438                     l_chg_rtg_rev  := 0;    -- Add: bug3202534
7439                     l_chg_bom_rev  := 0;    -- Add: bug3202534
7440                     l_chg_rev_flag := 0;    -- Add: bug3202534
7441 
7442                     g_charges_exist_all  := false;      -- ADD: bug 3423195
7443                     g_charges_exist_shopfloor := false; -- ADD: bug 3423195
7444                     g_job_released_date  := null;       -- ADD: bug 3423195
7445 
7446                     v_wlji_err_code(v_idx)               := l_error_code;
7447                     v_wlji_err_msg(v_idx)                := l_error_msg;
7448                     v_wlji_request_id(v_idx)             := fnd_global.conc_request_id;
7449                     v_wlji_program_id(v_idx)             := fnd_global.conc_program_id;
7450                     v_wlji_program_appl_id(v_idx)        := fnd_global.prog_appl_id;
7451                     v_wlji_creation_date(v_idx)          := sysdate;
7452                     v_wlji_last_updt_login(v_idx)        := fnd_global.login_id;
7453                     v_wlji_last_updt_date(v_idx)         := sysdate;
7454                     v_wlji_prog_updt_date(v_idx)         := sysdate;
7455 
7456                     l_mo_org_id                          := v_wlji_org_id(v_idx); -- Add: bug5485653
7457                     ------------------------------------------------------------------
7458                     -- {{{ Begin: Validate and Default constants (load_type = 5 and 6)
7459                     ------------------------------------------------------------------
7460 l_stmt_num := 80;
7461                     if (v_wlji_load_type(v_idx) <> 5) AND (v_wlji_load_type(v_idx) <> 6) then
7462                         l_aux_mesg := 'Load Type must be either 5 or 6';
7463                         handle_invalid_field('LOAD TYPE', l_aux_mesg, l_stmt_num);
7464                         l_error_code := -1;
7465                         GOTO skip_validate_constants;
7466                     end if;
7467 
7468 l_stmt_num := 85;
7469                     -- Dafault: job_type = 3 is non-standard job. Anything else/null => standard job
7470                     if (v_wlji_job_type(v_idx) is null) or (v_wlji_job_type(v_idx) <> 3) then
7471                         v_wlji_job_type(v_idx) := 1;
7472                     end if;
7473 
7474 l_stmt_num := 95;
7475                     -- Validate: mode_flag cannot be 2 for non-standard jobs, can only be 1
7476                     if v_wlji_job_type(v_idx) = 3 then
7477                         if v_wlji_mode_flag(v_idx) = 2 then
7478                             l_aux_mesg := 'Mode Flag cannot be 2 for non-standard jobs';
7479                             handle_invalid_field('MODE FlAG', l_aux_mesg, l_stmt_num);
7480                             l_error_code := -1;
7481                             GOTO skip_validate_constants;
7482                         else
7483                             v_wlji_mode_flag(v_idx) := 1;
7484                         end if;
7485                     end if;
7486 
7487 l_stmt_num := 100;
7488                     -- Validate: SCHEDULING METHOD
7489                     if(v_wlji_scheduling_method(v_idx) NOT IN (1, 2, 3) ) then
7490                         l_aux_mesg := 'Scheduling method must be 1(routing), 2(leadtime) or 3(manual)';
7491                         handle_invalid_field('SCHEDULING METHOD', l_aux_mesg, l_stmt_num);
7492                         l_error_code := -1;
7493                         GOTO skip_validate_constants;
7494                     end if;
7495 
7496 l_stmt_num := 105;
7497                     -- Validate: ALLOW EXPLOSION
7498                     if UPPER(v_wlji_allow_explosion(v_idx)) = 'N' then
7499                         handle_errmsg_name('WSM', 'WSM_ALLOW_EXPL_Y', l_stmt_num);
7500                         l_error_code := -1;
7501                         GOTO skip_validate_constants;
7502                     else
7503                         v_wlji_allow_explosion(v_idx) := 'Y';
7504             end if;
7505 
7506 l_stmt_num := 110;
7507                     -- Validate: WIP SUPPLY TYPE
7508                     if v_wlji_wip_supply_type(v_idx) is null then
7509                         v_wlji_wip_supply_type(v_idx) := 7;
7510                     elsif v_wlji_wip_supply_type(v_idx) not in (1,2,3,4,5,7) then
7511                         l_aux_mesg := '';
7512                         handle_invalid_field('Wip Supply Type', l_aux_mesg, l_stmt_num);
7513                         l_error_code := -1;
7514                         GOTO skip_validate_constants;
7515                     end if;
7516 
7517 l_stmt_num := 115;
7518                     -- Validate: STATUS TYPE
7519                     if v_wlji_status_type(v_idx) not in (
7520                         WIP_CONSTANTS.UNRELEASED,
7521                         WIP_CONSTANTS.RELEASED,
7522                         WIP_CONSTANTS.HOLD,
7523                         WIP_CONSTANTS.CANCELLED
7524                     ) then
7525                         l_aux_mesg := '';
7526                         handle_invalid_field('STATUS TYPE', l_aux_mesg, l_stmt_num);
7527                         l_error_code := -1;
7528                         GOTO skip_validate_constants;
7529                     end if;
7530 
7531 l_stmt_num := 120;
7532                     -- Validate: check kanban card id support status
7533                     if WSMPVERS.get_osfm_release_version < '110509'
7534                        and v_wlji_kanban_card_id(v_idx) is not null then
7535                         handle_errmsg_name('WSM', 'WSM_KANBAN_NOT ALLOWED', l_stmt_num);
7536                         l_error_code := -1;
7537                         GOTO skip_validate_constants;
7538                     end if;
7539 
7540 << skip_validate_constants >>
7541 
7542                     if l_error_code <> 0 then
7543                         l_error_count := l_error_count + 1;
7544                         GOTO abort_this_job;
7545                     end if;
7546                     if lbji_debug = 'Y' then
7547                         fnd_file.put_line(fnd_file.log, 'Validating Constants.. OK.');
7548                     end if;
7549                     ----------------------------------------------------------------
7550                     -- }}} End: Validate and Default constants (load_type = 5 and 6)
7551                     ----------------------------------------------------------------
7552 
7553 
7554 l_stmt_num := 130;
7555                     ---------------------------------------------------------
7556                     -- Validate organization id  Mngr has done the validation
7557                     ---------------------------------------------------------
7558                     --validate_org_id_name (
7559                     --  v_wlji_org_id(v_idx),
7560                     --  v_wlji_org_code(v_idx),
7561                     --  l_err_code,
7562                     --  l_err_msg);
7563                     --if l_err_code = -1 then
7564                     --  handle_invalid_field('Organization Id', '', l_stmt_num);
7565                     --  l_error_count := l_error_count + 1;
7566                     --  GOTO abort_this_job;
7567                     --end if;
7568                     --if lbji_debug = 'Y' then
7569                     --  fnd_file.put_line(fnd_file.log, 'Check Organization Id.. OK');
7570                     --end if;
7571 
7572 l_stmt_num := 130.5;
7573                     select nvl(OP_SEQ_NUM_INCREMENT, 10)
7574                     into   g_op_seq_incr
7575                     from   wsm_parameters
7576                     where  ORGANIZATION_ID = v_wlji_org_id(v_idx);
7577 
7578 l_stmt_num := 140;
7579                     --------------------------------------
7580                     -- {{{ Begin: Validate job name and id
7581                     --------------------------------------
7582                     Begin
7583                         l_temp_num := 0;
7584                         if v_wlji_load_type(v_idx) = 5 then   -- Job creation
7585                             NULL;       -- Mngr has done the validation
7586                         elsif v_wlji_load_type(v_idx) = 6 then    -- Job update
7587 
7588                             if (v_wlji_wip_entity_id(v_idx) is null) then
7589                                 begin
7590 l_stmt_num := 140.1;
7591                                     -- Get wip_entity_id by wip_entity_name
7592                                     select wip_entity_id
7593                                     into   v_wlji_wip_entity_id(v_idx)
7594                                     from   wip_entities
7595                                     where  wip_entity_name = v_wlji_job_name(v_idx)
7596                                     and    organization_id = v_wlji_org_id(v_idx);
7597                                 exception
7598                                     when others then
7599                                         raise e_invalid_job_name;
7600                                 end;
7601                             end if;
7602 
7603                             -- Now wip_entity_id is available, check job status
7604                             begin
7605 l_stmt_num := 140.2;
7606                             -- For unreleased jobs allow job name update if wip_entity_id is not null - fix for bug5990761
7607                               select decode(wdj.status_type, WIP_CONSTANTS.UNRELEASED, NVL(v_wlji_job_name(v_idx),we.wip_entity_name), we.wip_entity_name)
7608                                 into   v_wlji_job_name(v_idx)
7609                                 from   wip_discrete_jobs wdj,
7610                                        wip_entities we
7611                                 where  wdj.wip_entity_id = v_wlji_wip_entity_id(v_idx)
7612                                 and    wdj.status_type in (
7613                                         WIP_CONSTANTS.UNRELEASED,
7614                                         WIP_CONSTANTS.RELEASED,
7615                                         WIP_CONSTANTS.HOLD,
7616                                         WIP_CONSTANTS.CANCELLED)
7617                                 and    wdj.wip_entity_id = we.wip_entity_id
7618                                 and    we.organization_id = v_wlji_org_id(v_idx);
7619                             exception
7620                                 when others then
7621                                     raise e_invalid_id;
7622                             end;
7623                         end if;
7624 
7625                     Exception
7626                         when e_invalid_id then
7627                             l_aux_mesg := '';
7628                             handle_invalid_field('Wip_Entity_Id', l_aux_mesg, l_stmt_num);
7629                             l_error_code := -1;
7630                             GOTO skip_job_id_valid;
7631 
7632                         when e_invalid_job_name then
7633                             l_aux_mesg := '';
7634                             handle_invalid_field('Job_Name', l_aux_mesg, l_stmt_num);
7635                             l_error_code := -1;
7636                             GOTO skip_job_id_valid;
7637                     End;
7638 
7639 l_stmt_num := 140.3;
7640                     -- removed for performance improvement
7641                     --l_return_value := wsmputil.CHECK_WMTI(   -- CZH.I
7642                     --            p_wip_entity_id    => null,
7643                     --            p_wip_entity_name  => v_wlji_job_name(v_idx),
7644                     --            p_transaction_date => null,
7645                     --            x_err_code         => l_error_code,
7646                     --            x_err_msg          => l_error_msg,
7647                     --            p_organization_id  => v_wlji_org_id(v_idx) );
7648                     --IF (l_return_value <> 0) THEN
7649                     --    handle_errmsg_name('WSM', 'WSM_PENDING_MOVE_TXNS', l_stmt_num);
7650                     --    l_error_code := -1;
7651                     --    GOTO skip_job_id_valid;
7652                     --END IF;
7653 
7654 l_stmt_num := 140.4;
7655                     -- bug 3453139 removed for this is no longer required
7656                     --l_return_value
7657                     --    := wsmputil.CHECK_WSMT(   -- CZH.I
7658                     --            p_wip_entity_id    => null,
7659                     --            p_wip_entity_name  => v_wlji_job_name(v_idx),
7660                     --            p_transaction_id   => null,
7661                     --            p_transaction_date => null,
7662                     --            x_err_code         => l_error_code,
7663                     --            x_err_msg          => l_error_msg,
7664                     --            p_organization_id  => v_wlji_org_id(v_idx) );
7665                     --IF (l_return_value <> 0) THEN
7666                     --    FND_MESSAGE.SET_NAME('WSM', 'WSM_PENDING_TXN');
7667                     --    fnd_message.set_token('TABLE', 'WSM_SPLIT_MERGE_TRANSACTIONS');
7668                     --    l_error_msg := fnd_message.get;
7669                     --    l_error_code := -1;
7670                     --    handle_error( l_error_code, l_error_msg, l_stmt_num);
7671                     --    GOTO skip_job_id_valid;
7672                     --ELSE
7673                     --    l_error_msg := null;
7674                     --END IF;
7675 
7676 << skip_job_id_valid >>
7677 
7678                     if l_error_code <> 0 then
7679                         l_error_count := l_error_count + 1;
7680                         GOTO abort_this_job;
7681                     end if;
7682                     if lbji_debug = 'Y' then
7683                         fnd_file.put_line(fnd_file.log, 'Validate Job Name and Id.. OK.');
7684                     end if;
7685                     -------------------------------------
7686                     -- }}} End: Validate job name and id
7687                     -------------------------------------
7688 
7689 
7690 l_stmt_num := 150;
7691                     ---------------------------
7692                     --  Validate Start quantity
7693                     ---------------------------
7694                     if v_wlji_start_quantity(v_idx) < 0 then
7695                         l_error_code := -1;
7696                         l_aux_mesg := 'Start Quantity cannot be negative';
7697                         handle_invalid_field('Start_Quantity', l_aux_mesg, l_stmt_num);
7698                         l_error_count := l_error_count + 1;
7699                         GOTO abort_this_job;
7700                     end if;
7701 
7702                     if v_wlji_load_type(v_idx) = 5 and
7703                        (v_wlji_start_quantity(v_idx) is NULL or
7704                         v_wlji_start_quantity(v_idx) = 0)
7705                     then
7706                         l_error_code := -1;
7707                         l_aux_mesg := 'Start Quantity cannot be NULL or 0 for job creation';
7708                         handle_invalid_field( 'Start_Quantity', l_aux_mesg, l_stmt_num);
7709                         l_error_count := l_error_count + 1;
7710                         GOTO abort_this_job;
7711                     end if;
7712                     if lbji_debug = 'Y' and v_wlji_mode_flag(v_idx) = 1 then
7713                         fnd_file.put_line(fnd_file.log, 'Validate Start Quantity.. OK.');
7714                     end if;
7715 
7716 
7717 l_stmt_num := 160;
7718                     -------------------------------------------------
7719                     -- For job update, save the current values in WDJ
7720                     -------------------------------------------------
7721                     if v_wlji_load_type(v_idx) = 6 then
7722                         select  primary_item_id,
7723                                 class_code,
7724                                 scheduled_start_date,
7725                                 scheduled_completion_date,
7726                                 start_quantity,
7727                                 net_quantity,
7728                                 status_type,
7729                                 firm_planned_flag,
7730                                 job_type,
7731                                 bom_reference_id,
7732                                 alternate_bom_designator,
7733                                 routing_reference_id,
7734                                 alternate_routing_designator,
7735                                 bom_revision_date,
7736                                 routing_revision_date,
7737                                 bom_revision,
7738                                 routing_revision,
7739                                 common_routing_sequence_id,
7740                                 common_bom_sequence_id,
7741                                 wip_supply_type,
7742                                 scheduled_start_date,
7743                                 scheduled_completion_date,
7744                                 coproducts_supply,
7745                                 kanban_card_id,
7746                                 completion_subinventory,
7747                                 completion_locator_id,
7748                                 date_released,
7749                                 schedule_group_id,
7750                                 build_sequence,
7751                                 line_id
7752                         into    l_cur_primary_item_id,
7753                                 l_cur_class_code,
7754                                 l_cur_start_date,
7755                                 l_cur_complete_date,
7756                                 l_cur_start_qty,
7757                                 l_cur_net_qty,
7758                                 l_cur_status_type,
7759                                 l_cur_firm_planned_flag,
7760                                 l_cur_job_type,
7761                                 l_cur_bom_ref_id,
7762                                 l_cur_alt_bom,
7763                                 l_cur_rtg_ref_id,
7764                                 l_cur_alt_rtg,
7765                                 l_cur_bom_rev_date,
7766                                 l_cur_rtg_rev_date,
7767                                 l_cur_bom_revision,
7768                                 l_cur_rtg_revision,
7769                                 l_cur_com_rtg_seq_id,
7770                                 l_cur_com_bom_seq_id,
7771                                 l_cur_wip_supply_type,
7772                                 l_cur_schd_start_date,
7773                                 l_cur_schd_compl_date,
7774                                 l_cur_coprod_supply,
7775                                 l_cur_kanban_card_id,
7776                                 l_cur_compl_subinv,
7777                                 l_cur_compl_locator,
7778                                 l_cur_date_released,
7779                                 l_cur_schedule_group_id,
7780                                 l_cur_build_sequence,
7781                                 l_cur_line_id
7782                         from    wip_discrete_jobs
7783                         where   wip_entity_id = v_wlji_wip_entity_id(v_idx)
7784                         and     organization_id = v_wlji_org_id(v_idx);
7785 
7786                         -- BA: bug 3423195
7787                         g_job_released_date := l_cur_date_released;
7788                         g_charges_exist_all := discrete_charges_exist(
7789                                         v_wlji_wip_entity_id(v_idx),
7790                                         v_wlji_org_id(v_idx),0);
7791                         g_charges_exist_shopfloor := discrete_charges_exist(
7792                                         v_wlji_wip_entity_id(v_idx),
7793                                         v_wlji_org_id(v_idx),1);
7794                         -- EA: bug 3423195
7795 
7796                         -- Currently changing job_type, wip_supply_type and
7797                         -- v_wlji_kanban_card_id is not supported
7798                         v_wlji_job_type(v_idx)        := l_cur_job_type;
7799                         v_wlji_wip_supply_type(v_idx) := l_cur_wip_supply_type;
7800                         v_wlji_kanban_card_id(v_idx)  := l_cur_kanban_card_id;
7801 
7802 						-- Added for bug fix 5291888
7803 						if l_cur_primary_item_id <> nvl(v_wlji_item_id(v_idx),l_cur_primary_item_id) then
7804 							l_error_code := -1;
7805                             handle_invalid_field( 'PRIMARY_ITEM_ID', l_aux_mesg, l_stmt_num);
7806                             l_error_count := l_error_count + 1;
7807                             GOTO abort_this_job;
7808 						end if;
7809                         -- For std job, assembly cannot be changed
7810                         if(l_cur_job_type = 1) then
7811                             v_wlji_item_id(v_idx) := l_cur_primary_item_id;
7812                         end if;
7813                         v_wlji_start_quantity(v_idx) := nvl(v_wlji_start_quantity(v_idx),l_cur_start_qty);
7814                     end if;
7815 
7816 
7817 l_stmt_num := 170;
7818                     ---------------------------------------------------------
7819                     -- {{{ Begin: Validate schedule group and build sequence
7820                     ---------------------------------------------------------
7821                     if v_wlji_job_type(v_idx) = 1 then
7822 
7823                  -- Start Bug Fix : 6676148
7824                  -- Default schedule_group_id from schedule_group_name
7825                         if v_wlji_schedule_group_name(v_idx) is not null and
7826                          v_wlji_schedule_group_id(v_idx) is null then
7827                               begin
7828                                 select schedule_group_id
7829                                 into v_wlji_schedule_group_id(v_idx)
7830                                 from wip_schedule_groups_val_v
7831                                 where organization_id = v_wlji_org_id(v_idx)
7832                                 and schedule_group_name =v_wlji_schedule_group_name(v_idx);
7833                               exception
7834                                 when others then
7835                                    l_error_code := -1;
7836                                    handle_errmsg_name('WIP', 'WIP_ML_SCHEDULE_GROUP', l_stmt_num);
7837                                    l_error_count := l_error_count + 1;
7838                                    GOTO abort_this_job;
7839                               end;
7840                         end if;
7841                         -- End Bug Fix : 6676148
7842 
7843                         -- {{ validate schedule_group_id begin
7844                         if v_wlji_load_type(v_idx) = 6 then
7845                             v_wlji_schedule_group_id(v_idx)
7846                                 := NVL(v_wlji_schedule_group_id(v_idx), l_cur_schedule_group_id);
7847                         end if;
7848 
7849                         if (v_wlji_source_code(v_idx) = 'WICDOL' and
7850                             v_wlji_schedule_group_name(v_idx) IS NULL and
7851                             v_wlji_schedule_group_id(v_idx) IS NULL)
7852                         then
7853 l_stmt_num := 170.1;
7854                             insert into wip_schedule_groups (
7855                                     schedule_group_id,
7856                                     schedule_group_name,
7857                                     organization_id,
7858                                     description,
7859                                     created_by,
7860                                     last_updated_by,
7861                                     creation_date,
7862                                     last_update_date)
7863                             select  wip_schedule_groups_s.nextval,
7864                                     wds.name,
7865                                     v_wlji_org_id(v_idx),
7866                                     to_char(sysdate),
7867                                     l_user,
7868                                     l_user,
7869                                     sysdate,
7870                                     sysdate
7871                             from    wsh_new_deliveries wds
7872                             where   wds.delivery_id = v_wlji_delivery_id(v_idx)
7873                             and     not exists  (
7874                                         select  1
7875                                         from    wip_schedule_groups wsg
7876                                         where   wsg.organization_id = v_wlji_org_id(v_idx)
7877                                         and     wsg.schedule_group_name = wds.name);
7878 l_stmt_num := 170.2;
7879                             select wsg.schedule_group_name,
7880                                    wsg.schedule_group_id
7881                             into   v_wlji_schedule_group_name(v_idx),
7882                                    v_wlji_schedule_group_id(v_idx)
7883                             from   wip_schedule_groups wsg,
7884                                    wsh_new_deliveries  wds
7885                             where  wds.delivery_id = v_wlji_delivery_id(v_idx)
7886                             and    wsg.schedule_group_name = wds.name
7887                             and    wsg.organization_id = v_wlji_org_id(v_idx);
7888                         end if;
7889 
7890                         -- Start Bug Fix : 6676148
7891                if (v_wlji_schedule_group_id(v_idx) is not NULL and
7892                             v_wlji_schedule_group_id(v_idx) <> fnd_api.g_miss_num) then
7893 l_stmt_num := 170.3;
7894                             begin
7895                                 select 1
7896                                 into   l_temp_num
7897                                 from   wip_schedule_groups_val_v wsg
7898                                 where  wsg.schedule_group_id = v_wlji_schedule_group_id(v_idx)
7899                                 and    wsg.organization_id = v_wlji_org_id(v_idx);
7900                             exception
7901                                 when too_many_rows then
7902                                     l_temp_num := 1;
7903                                 when no_data_found then
7904                                     l_temp_num := 0;
7905                             end;
7906                             if l_temp_num = 0 then
7907                                 l_error_code := -1;
7908                                 handle_errmsg_name('WIP', 'WIP_ML_SCHEDULE_GROUP', l_stmt_num);
7909                                 l_error_count := l_error_count + 1;
7910                                 GOTO abort_this_job;
7911                             end if;
7912                         end if;
7913                         if lbji_debug = 'Y' then
7914                             fnd_file.put_line(fnd_file.log, 'Schedule Group Id Verification.. OK');
7915                         end if;
7916                         -- }} validate schedule_group_id end
7917 
7918 l_stmt_num := 170.4;
7919                         -- {{ validate build_seq_id begin
7920                         if v_wlji_load_type(v_idx) = 6 then
7921                             v_wlji_build_sequence(v_idx)
7922                                 := NVL(v_wlji_build_sequence(v_idx), l_cur_build_sequence);
7923                             v_wlji_line_id(v_idx)
7924                                 := NVL(v_wlji_line_id(v_idx), l_cur_line_id);
7925                             v_wlji_schedule_group_id(v_idx)
7926                                 := NVL(v_wlji_schedule_group_id(v_idx), l_cur_schedule_group_id);
7927                         end if;
7928 
7929                         -- Only if v_wlji_build_sequence(v_idx) is not null, we want to execute
7930                         -- this WIP_VALIDATE function. Otherwise, DO NOT.
7931                         if v_wlji_build_sequence(v_idx) is not null and
7932                            v_wlji_build_sequence(v_idx) <> 0 Then
7933                             l_temp_boolean := WIP_Validate.build_sequence (
7934                                     p_build_sequence    => v_wlji_build_sequence(v_idx),
7935                                     p_wip_entity_id     => v_wlji_wip_entity_id(v_idx),
7936                                     p_organization_id   => v_wlji_org_id(v_idx),
7937                                     p_line_id           => v_wlji_line_id(v_idx),
7938                                     p_schedule_group_id => v_wlji_schedule_group_id(v_idx));
7939 
7940                             if NOT l_temp_boolean Then
7941                                 l_error_code := -1;
7942                                 handle_errmsg_name('WIP', 'WIP_ML_BUILD_SEQUENCE', l_stmt_num);
7943                                 l_error_count := l_error_count + 1;
7944                                 GOTO abort_this_job;
7945                             end if;
7946                         end if;
7947                         if lbji_debug = 'Y' then
7948                             fnd_file.put_line(fnd_file.log, 'Build Seq. Id Verification.. OK');
7949                         end if;
7950                         -- }} validate build_seq_id end
7951                     else -- job type = 3
7952                         -- previously we do not have validations for non-std job
7953                         v_wlji_build_sequence(v_idx)    := null;
7954                         v_wlji_line_id(v_idx)           := null;
7955                          -- Start Bug Fix : 6676148
7956                         --v_wlji_schedule_group_id(v_idx) := null;
7957                         -- Default schedule_group_id from schedule_group_name
7958 
7959                          if v_wlji_schedule_group_name(v_idx) is not null and
7960                             v_wlji_schedule_group_id(v_idx) is null then
7961 
7962                              begin
7963 
7964                              select schedule_group_id
7965                              into v_wlji_schedule_group_id(v_idx)
7966                              from wip_schedule_groups_val_v
7967                              where organization_id = v_wlji_org_id(v_idx)
7968                              and schedule_group_name =v_wlji_schedule_group_name(v_idx);
7969 
7970                              exception
7971                                 when others then
7972                                    l_error_code := -1;
7973                                    handle_errmsg_name('WIP', 'WIP_ML_SCHEDULE_GROUP', l_stmt_num);
7974                                    l_error_count := l_error_count + 1;
7975                                    GOTO abort_this_job;
7976                          end;
7977 
7978                          end if;
7979 
7980                         -- {{ validate schedule_group_id begin
7981                         if v_wlji_load_type(v_idx) = 6 then
7982                             v_wlji_schedule_group_id(v_idx)
7983                                 := NVL(v_wlji_schedule_group_id(v_idx), l_cur_schedule_group_id);
7984                         end if;
7985 
7986                         if (v_wlji_schedule_group_id(v_idx) is not NULL and
7987                             v_wlji_schedule_group_id(v_idx) <> fnd_api.g_miss_num) then
7988 l_stmt_num := 170.5;
7989                             begin
7990                                 select 1
7991                                 into   l_temp_num
7992                                 from   wip_schedule_groups_val_v wsg
7993                                 where  wsg.schedule_group_id = v_wlji_schedule_group_id(v_idx)
7994                                 and    wsg.organization_id = v_wlji_org_id(v_idx);
7995                             exception
7996                                 when too_many_rows then
7997                                     l_temp_num := 1;
7998                                 when no_data_found then
7999                                     l_temp_num := 0;
8000                             end;
8001                             if l_temp_num = 0 then
8002                                 l_error_code := -1;
8003                                 handle_errmsg_name('WIP', 'WIP_ML_SCHEDULE_GROUP', l_stmt_num);
8004                                 l_error_count := l_error_count + 1;
8005                                 GOTO abort_this_job;
8006                             end if;
8007                         end if;
8008                         if lbji_debug = 'Y' then
8009                             fnd_file.put_line(fnd_file.log, 'Schedule Group Id Verification.. OK');
8010                         end if;
8011                         -- }} validate schedule_group_id end
8012                         -- End Bug Fix : 6676148
8013                     end if; -- job type
8014                     ---------------------------------------------------------
8015                     -- }}} End: Validate schedule group and build sequence
8016                     ---------------------------------------------------------
8017 
8018 
8019 -- ===========================================================================================
8020 -- BEGIN: Job Creation Validations {{{
8021 -- ===========================================================================================
8022                     if v_wlji_load_type(v_idx) = 5 then
8023                         -- for std lbj ignore v_wlji_bom_ref_id/v_wlji_rtg_ref_id
8024             if v_wlji_job_type(v_idx) = 1 then  -- added: bug 3862212
8025                             v_wlji_bom_ref_id(v_idx) := null;
8026                 v_wlji_rtg_ref_id(v_idx) := null;
8027             end if;                             -- added: bug 3862212
8028 
8029 l_stmt_num := 200;
8030                         -- Validate Mode: for job creation mode must be in (1, 2)
8031                         if v_wlji_mode_flag(v_idx) not in (1,2) then
8032                             l_aux_mesg := 'For job creation, Mode Flag should have value 1 or 2';
8033                             handle_invalid_field('MODE_FLAG', l_aux_mesg, l_stmt_num);
8034                             l_error_code := -1;
8035                             l_error_count := l_error_count + 1;
8036                             GOTO abort_this_job;
8037                         end if;
8038 
8039 l_stmt_num := 210;
8040                         -- Default lot_number
8041                         v_wlji_lot_number(v_idx) := v_wlji_job_name(v_idx);
8042                         if lbji_debug = 'Y' then
8043                             fnd_file.put_line(fnd_file.log, 'Lot Number Defaulted as Job Name.');
8044                         end if;
8045 
8046 l_stmt_num := 220;
8047                         -- Validate: FIRM_PLANNED_FLAG
8048                         if v_wlji_job_type(v_idx) = 1 then
8049                             if v_wlji_firm_planned_flag(v_idx) is null then
8050                                 v_wlji_firm_planned_flag(v_idx) := 2;
8051                             elsif v_wlji_firm_planned_flag(v_idx) <> 1 and
8052                                   v_wlji_firm_planned_flag(v_idx) <> 2
8053                             then
8054                                 l_aux_mesg := 'For job creation, Firm Planned Flag must be either 1 or 2';
8055                                 handle_invalid_field('FIRM_PLANNED_FLAG', l_aux_mesg, l_stmt_num);
8056                                 l_error_code := -1;
8057                                 l_error_count := l_error_count + 1;
8058                                 GOTO abort_this_job;
8059                             end if;
8060                         end if;
8061 
8062 
8063 l_stmt_num := 230;
8064                         -- Check WLSMI
8065                         -- make sure that no one is trying to create a wip lot out of
8066                         -- an inventory lot by splitting/merging/etc.
8067                         l_num_records := 0;
8068                         begin
8069                             select  1
8070                             into    l_num_records
8071                             from    wsm_starting_lots_interface    wsli,
8072                                     wsm_lot_split_merges_interface wlsmi
8073                             where   wsli.lot_number = v_wlji_job_name(v_idx)
8074                             and     wsli.header_id = wlsmi.header_id
8075                             and     wlsmi.process_status in (1,2);
8076                         exception
8077                             when too_many_rows then
8078                                 l_num_records := 1;
8079                             when no_data_found then
8080                                 NULL;
8081                         end;
8082                         if l_num_records <> 0 then
8083                             l_error_code := -1;
8084                             handle_errmsg_name('WSM', 'WSM_LOT_EXISTS', l_stmt_num);
8085                             l_error_count := l_error_count + 1;
8086                             GOTO abort_this_job;
8087                         end if;
8088                         if lbji_debug = 'Y' then
8089                             fnd_file.put_line(fnd_file.log, 'Checking existance of inv lots of the same name.. OK');
8090                         end if;
8091 
8092 
8093                         -----------------------------------
8094                         -- {{{ Begin: Validate WHO columns
8095                         -----------------------------------
8096 l_stmt_num := 240;
8097                         -- validate last_updt_by (job creation)
8098                         if v_wlji_last_updt_by(v_idx) is NULL then
8099                             v_wlji_last_updt_by(v_idx) := l_user;
8100                         else
8101                             l_num_records := 0;
8102                             l_xst := WSMPLCVA.v_user.exists(v_wlji_last_updt_by(v_idx));
8103                             if l_xst = false then
8104                                 begin
8105                                     select 1
8106                                     into   l_num_records
8107                                     from   fnd_user
8108                                     where  user_id = v_wlji_last_updt_by(v_idx)
8109                                     and    sysdate between start_date and nvl(end_date,sysdate+1);
8110                                 exception
8111                                     when too_many_rows then l_num_records := 1;
8112                                     when no_data_found then
8113                                         l_error_code := -1;
8114                                         l_aux_mesg := '';
8115                                         handle_invalid_field(
8116                                             'Last Updated By', l_aux_mesg, l_stmt_num);
8117                                 end;
8118                             end if;
8119 
8120                             if l_xst = false AND l_num_records <> 0 then
8121                                 WSMPLCVA.v_user(v_wlji_last_updt_by(v_idx))
8122                                     := v_wlji_last_updt_by(v_idx);
8123                             end if;
8124 
8125                             if l_error_code <> 0 then
8126                                 l_error_count := l_error_count + 1;
8127                                 GOTO abort_this_job;
8128                             end if;
8129                         end if;
8130                         if lbji_debug = 'Y' then
8131                             fnd_file.put_line(fnd_file.log, 'Last Updt By Validation.. OK.');
8132                         end if;
8133 
8134 l_stmt_num := 245;
8135                         -- validate created_by (job creation)
8136                         if v_wlji_created_by(v_idx) is NULL then
8137                             v_wlji_created_by(v_idx) := l_user;
8138                         else
8139                             l_num_records := 0;
8140                             l_xst := WSMPLCVA.v_user.exists(v_wlji_created_by(v_idx));
8141                             if l_xst = false then
8142                                 begin
8143                                     select 1
8144                                     into   l_num_records
8145                                     from   fnd_user
8146                                     where  user_id = v_wlji_created_by(v_idx)
8147                                     and    sysdate between start_date and nvl(end_date,sysdate+1);
8148                                 exception
8149                                     when too_many_rows then l_num_records := 1;
8150                                     when no_data_found then
8151                                         l_error_code := -1;
8152                                         l_aux_mesg := '';
8153                                         handle_invalid_field(
8154                                             'Created By', l_aux_mesg, l_stmt_num);
8155                                 end;
8156                             end if;
8157 
8158                             if l_xst = false AND l_num_records <> 0 then
8159                                 WSMPLCVA.v_user(v_wlji_created_by(v_idx))
8160                                     := v_wlji_created_by(v_idx);
8161                             end if;
8162 
8163                             if l_error_code <> 0 then
8164                                 l_error_count := l_error_count + 1;
8165                                 GOTO abort_this_job;
8166                             end if;
8167                         end if;
8168                         if lbji_debug = 'Y' then
8169                             fnd_file.put_line(fnd_file.log, 'Created By Validation.. OK.');
8170                         end if;
8171                         --------------------------------
8172                         -- }}} End: Validate WHO columns
8173                         --------------------------------
8174 
8175 
8176                         if v_wlji_mode_flag(v_idx) = 2 then
8177 l_stmt_num := 250;
8178                             -- Call load_wsli_data to load relevant rows from wsli into PL/SQL
8179                             -- tables for easy validation. This procedure will not be executed
8180                             -- if there's no mode_flag = 2 rec in wlji.
8181                             -- l_wsli_loaded (true/false) ensures that the procedure
8182                             -- is called only once per worker
8183                             if (l_wsli_loaded = false) then
8184                                 if lbji_debug = 'Y' then
8185                                     fnd_file.put_line(fnd_file.log,
8186                                         'Loading wsli for batch group id: ' ||l_batch_grp_id||'..');
8187                                 end if;
8188 
8189                                 load_wsli_data(l_batch_grp_id);
8190                                 l_wsli_loaded := true;
8191                             end if;
8192 
8193 l_stmt_num := 260;
8194                             -- Check that there's a row in wsli for this mode 2 job
8195                             l_xst := true;
8196                             if v_wlji_src_line_id(v_idx) is NULL then
8197                                 l_error_code := -1;
8198                                 handle_errmsg_name('WSM', 'WSM_START_LOT_REQUIRED', l_stmt_num);
8199                                 l_error_count := l_error_count + 1;
8200                                 GOTO abort_this_job;
8201                             else
8202                                 l_xst := v_wsli.exists(v_wlji_src_line_id(v_idx));
8203                                 if l_xst = false then
8204                                     l_error_code := -1;
8205                                     handle_errmsg_name('WSM', 'WSM_START_LOT_REQUIRED', l_stmt_num);
8206                                     l_error_count := l_error_count + 1;
8207                                     GOTO abort_this_job;
8208                                 end if;
8209                             end if;
8210                         end if;
8211 
8212 
8213 l_stmt_num := 270;
8214                         -- {{ Validate schedule dates (creation)
8215                         -- this should be done after item_id is validated
8216 
8217                         l_error_msg := null;
8218                         IF v_wlji_fusd(v_idx) IS NOT NULL AND
8219                            v_wlji_lucd(v_idx) IS NOT NULL
8220                         THEN
8221 l_stmt_num := 270.1;
8222                             if  v_wlji_fusd(v_idx) > v_wlji_lucd(v_idx) THEN
8223                                 handle_errmsg_name('WSM', 'WSM_FUSD_GT_LUCD', l_stmt_num);
8224                                 l_error_code := -1;
8225                                 l_error_count := l_error_count + 1;
8226                                 GOTO abort_this_job;
8227                             end if;
8228                         ELSIF v_wlji_fusd(v_idx) IS NULL AND
8229                               v_wlji_lucd(v_idx) IS NULL
8230                         THEN
8231 l_stmt_num := 270.2;
8232                             handle_errmsg_name('WSM', 'WSM_DATES_NULL', l_stmt_num);
8233                             l_error_code := -1;
8234                             l_error_count := l_error_count + 1;
8235                             GOTO abort_this_job;
8236                         END IF;
8237 
8238                         if (v_wlji_scheduling_method(v_idx) = 1 ) then
8239                             -- Since infinite scheduler will be called, do not have to call
8240                             -- leadtime based scheduler
8241                             -- However, to default routing/bom revision date, v_wlji_fusd is
8242                             -- needed. We cannot call infinite scheduler if job-leve copy is
8243                             -- not available, but to create a job-level copy, routing/bom
8244                             -- revision is a must.
8245                             -- If v_wlji_scheduling_method = 1, v_wlji_fusd is NULL, the
8246                             -- routing/bom revision date will be default to SYSDATE
8247                             null;
8248                         else
8249 
8250                             IF v_wlji_fusd(v_idx) IS NOT NULL AND
8251                                v_wlji_lucd(v_idx) IS NULL THEN
8252     l_stmt_num := 270.3;
8253                                 v_wlji_lucd(v_idx)
8254                                     := wsmputil.GET_SCHEDULED_DATE (
8255                                             v_wlji_org_id(v_idx),
8256                                             v_wlji_item_id(v_idx),
8257                                             'F',
8258                                             v_wlji_fusd(v_idx),
8259                                             l_error_code,
8260                                             l_error_msg,
8261                                             v_wlji_start_quantity(v_idx));     --Fixed bug # 2313574
8262                                 if lbji_debug = 'Y' then
8263                                     fnd_file.put_line(fnd_file.log,
8264                                         'Getting complete date based on item lead time');
8265                                 end if;
8266                             END IF;
8267 
8268                             IF v_wlji_fusd(v_idx) IS NULL AND
8269                                v_wlji_lucd(v_idx) IS NOT NULL THEN
8270     l_stmt_num := 270.4;
8271                                 v_wlji_fusd(v_idx)
8272                                     := wsmputil.GET_SCHEDULED_DATE (
8273                                             v_wlji_org_id(v_idx),
8274                                             v_wlji_item_id(v_idx),
8275                                             'B',
8276                                             v_wlji_lucd(v_idx),
8277                                             l_error_code,
8278                                             l_error_msg,
8279                                             v_wlji_start_quantity(v_idx));     --Fixed bug # 2313574
8280                                 if lbji_debug = 'Y' then
8281                                     fnd_file.put_line(fnd_file.log,
8282                                         'Getting start date based on item lead time');
8283                                 end if;
8284                             END IF;
8285                             if l_error_code <> 0 OR l_error_msg IS NOT NULL then
8286                                 handle_error( l_error_code, l_error_msg, l_stmt_num);
8287                                 l_error_code := -1;
8288                                 l_error_count := l_error_count + 1;
8289                                 GOTO abort_this_job;
8290                             end if;
8291 
8292                         end if;
8293 
8294                         if lbji_debug = 'Y' then
8295                             fnd_file.put_line(fnd_file.log, 'Date Validation.. OK.');
8296                         end if;
8297                         -- }} Validate schedule dates
8298 
8299 
8300 l_stmt_num := 280;
8301                         -- Validate coproduct-supply flag (creation)
8302                         IF v_wlji_coproducts_supply(v_idx) is NULL THEN
8303                             v_wlji_coproducts_supply(v_idx)
8304                                 := WSMPLCVA.v_org(v_wlji_org_id(v_idx)).COPRODUCTS_SUPPLY_DEFAULT;
8305                         --Bug 5463925: OR is replaced by AND.
8306                         --ELSIF (v_wlji_coproducts_supply(v_idx) <> 1 OR
8307                         ELSIF (v_wlji_coproducts_supply(v_idx) <> 1 AND
8308                                v_wlji_coproducts_supply(v_idx) <> 2) THEN
8309                             v_wlji_coproducts_supply(v_idx) := 2;
8310                         END IF;
8311 
8312                         if l_error_code <> 0 then
8313                             l_error_count := l_error_count + 1;
8314                             GOTO abort_this_job;
8315                         end if;
8316                         if lbji_debug = 'Y' then
8317                             fnd_file.put_line(fnd_file.log,
8318                                 'Co Product Supply Flag Validation.. OK.');
8319                         end if;
8320 
8321 
8322 l_stmt_num := 290;
8323                         -- Default description
8324                         -- this is to be called only for job creation.
8325                         if RTRIM(v_wlji_description(v_idx)) is NULL then
8326                             l_date_text := fnd_date.date_to_charDT(dateval =>sysdate,calendar_aware => 1);
8327                             fnd_message.set_name('WIP','WIP_MLD_DESC');
8328                             fnd_message.set_token('LOAD_DATE', l_date_text, false);
8329                             v_wlji_description(v_idx) := FND_Message.get;
8330                         else
8331                             v_wlji_description(v_idx) := RTRIM(v_wlji_description(v_idx));
8332                         end if;
8333                         if lbji_debug = 'Y' then
8334                             fnd_file.put_line(fnd_file.log, 'Default Description.');
8335                         end if;
8336 
8337 
8338 l_stmt_num := 300;
8339                         --------------------------------------------------
8340                         -- {{{ Begin: Validation of a non-std job creation
8341                         --------------------------------------------------
8342                         if v_wlji_job_type(v_idx) = 3 then
8343 
8344                             -- validation_level = 0 => validations performed during job creation
8345                             -- Error Code and Message Guide
8346                             -- 1:  Routing Reference Cannot be Null
8347                             -- 2:  Invalid Assembly Item Id
8348                             -- 3:  Invalid Routing Reference Id
8349                             -- 4:  Invalid Bom Reference Id
8350                             -- 5:  Invalid Alternate Routing Designator
8351                             -- 0:  Invalid Alternate Bom Designator -- WARNING
8352                             -- 7:  Start Date cannot be greater than End Date
8353                             -- 8:  Both Start and End Dates must be Entered
8354                             -- 9:  Invalid Start Quantity
8355                             -- 10: Invalid Net Quantity
8356                             -- 11: Invalid Class Code
8357                             -- 12: Invalid Completion Locator Id
8358                             -- 13: Invalid Completion Subinventory
8359                             -- 14: Invalid Firm Planned Flag
8360 
8361                             l_error_code := 0;
8362                             l_error_msg := NULL;
8363                             wsmputil.validate_non_std_references(
8364                                 p_assembly_item_id       => v_wlji_item_id(v_idx),
8365                                 p_routing_reference_id   => v_wlji_rtg_ref_id(v_idx),
8366                                 p_bom_reference_id       => v_wlji_bom_ref_id(v_idx),
8367                                 p_alt_routing_designator => v_wlji_alt_rtg(v_idx),
8368                                 p_alt_bom_designator     => v_wlji_alt_bom(v_idx),
8369                                 p_organization_id        => v_wlji_org_id(v_idx),
8370                                 p_start_date             => v_wlji_fusd(v_idx),
8371                                 p_end_date               => v_wlji_lucd(v_idx),
8372                                 p_start_quantity         => v_wlji_start_quantity(v_idx),
8373                                 p_mrp_net_quantity       => v_wlji_net_quantity(v_idx),
8374                                 p_class_code             => v_wlji_class_code(v_idx),
8375                                 p_completion_subinventory=> v_wlji_compl_subinv(v_idx),
8376                                 p_completion_locator_id  => v_wlji_compl_loc_id(v_idx),
8377                                 p_firm_planned_flag      => v_wlji_firm_planned_flag(v_idx),
8378                                 p_bom_revision           => v_wlji_bom_revision(v_idx),
8379                                 p_bom_revision_date      => v_wlji_bom_rev_date(v_idx),
8380                                 p_routing_revision       => v_wlji_rtg_revision(v_idx),
8381                                 p_routing_revision_date  => v_wlji_rtg_rev_date(v_idx),
8382                                 x_routing_seq_id         => l_rtg_seq_id,
8383                                 x_bom_seq_id             => l_common_bom_seq_id, -- returns common bom
8384                                 validation_level         => 0,
8385                                 x_error_code             => l_error_code,
8386                                 x_err_msg                => l_error_msg);
8387                             if l_error_code = 0 and l_error_msg is not null then
8388                                 fnd_file.put_line(fnd_file.log, l_error_msg);
8389                                 l_error_msg := NULL;
8390                             end if;
8391                             if l_error_code <> 0 then
8392                                 handle_error( l_error_code, l_error_msg, l_stmt_num);
8393                                 l_error_code := -1;
8394                                 l_error_count := l_error_count + 1;
8395                                 GOTO abort_this_job;
8396                             end if;
8397 
8398                             -- Get common routing seq id
8399                             wsmputil.find_common_routing(
8400                                 p_routing_sequence_id        => l_rtg_seq_id,
8401                                 p_common_routing_sequence_id => l_common_rtg_seq_id,
8402                                 x_err_code                   => l_error_code,
8403                                 x_err_msg                    => l_error_msg);
8404                             if l_error_code <> 0 OR l_error_msg IS NOT NULL then
8405                                 handle_error( l_error_code, l_error_msg, l_stmt_num);
8406                                 l_error_code := -1;
8407                                 l_error_count := l_error_count + 1;
8408                                 GOTO abort_this_job;
8409                             end if;
8410                             if lbji_debug = 'Y' then
8411                                 fnd_file.put_line(fnd_file.log,
8412                                     'Validate Non-standard References.. OK.');
8413                             end if;
8414 
8415                         end if; -- non-standard validations end
8416                         ------------------------------------------------
8417                         -- }}} End: Validation of a non-std job creation
8418                         ------------------------------------------------
8419 
8420                         -----------------------------------------------------
8421                         -- {{{ Begin: validations for standard job creations
8422                         -----------------------------------------------------
8423                         if v_wlji_job_type(v_idx) = 1 then
8424 
8425 l_stmt_num := 310;
8426                             -- {{ Validate item id (std job creation)
8427                             if v_wlji_mode_flag(v_idx) = 1 then
8428                                 l_xst := WSMPLCVA.v_item.exists(v_wlji_item_id(v_idx));
8429                             else -- mode flag 2
8430                                 hash_str   := to_char(v_wlji_item_id(v_idx)) ||
8431                                               to_char(v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id);
8432                                 hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
8433 
8434                                 l_xst := WSMPLCVA.v_mode2_item.exists(hash_value) AND
8435                                          (WSMPLCVA.v_mode2_item(hash_value).INVENTORY_ITEM_ID
8436                                             = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id) AND
8437                                          (WSMPLCVA.v_mode2_item(hash_value).PRIMARY_ITEM_ID
8438                                             = v_wlji_item_id(v_idx));
8439                             end if;
8440 l_stmt_num := 310.1;
8441                             if l_xst = false then
8442                                 l_num_records := 0;
8443                                 begin
8444                                     select  1
8445                                     into    l_num_records
8446                                     from    mtl_system_items_kfv msi
8447                                     where   msi.inventory_item_id = v_wlji_item_id(v_idx)
8448                                     and     msi.organization_id   = v_wlji_org_id(v_idx)
8449                                     and     msi.lot_control_code  = 2;
8450                                 exception
8451                                     when too_many_rows then
8452                                         l_num_records := 1;
8453                                     when no_data_found then
8454                                         l_error_code := -1;
8455                                         handle_errmsg_name(
8456                                             'WSM', 'WSM_ASSEMBLY_NO_LOT', l_stmt_num);
8457                                 end;
8458 l_stmt_num := 310.2;
8459                                 if l_num_records <> 0 then
8460                                     l_num_records := 0;
8461                                     begin
8462                                         select  1
8463                                         into    l_num_records
8464                                         from    mtl_system_items_kfv msi
8465                                         where   msi.inventory_item_id = v_wlji_item_id(v_idx)
8466                                         and     msi.organization_id   = v_wlji_org_id(v_idx)
8467                     -- ST : Serial Support Project -----------------------------
8468                     -- In OSFM we do support Serial controlled assemblies.......
8469                                         and     msi.serial_number_control_code IN (1,2);
8470                     -- ST : Serial Support Project -----------------------------
8471                                     exception
8472                                         when too_many_rows then
8473                                             l_num_records := 1;
8474                                         when no_data_found then
8475                                             l_error_code := -1;
8476                                             handle_errmsg_name(
8477                                                 'WSM', 'WSM_ASSEMBLY_NOT_SERIAL', l_stmt_num);
8478                                     end;
8479                                 end if;
8480                             end if; -- l_xst = false
8481 
8482                             if l_xst = false AND l_num_records <> 0 then
8483                                 if v_wlji_mode_flag(v_idx) = 2 then
8484                                     WSMPLCVA.v_mode2_item(hash_value).INVENTORY_ITEM_ID
8485                                         := v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id;
8486                                     WSMPLCVA.v_mode2_item(hash_value).PRIMARY_ITEM_ID
8487                                         := v_wlji_item_id(v_idx);
8488                                 else -- mode flag = 1
8489                                     WSMPLCVA.v_item(v_wlji_item_id(v_idx)) := v_wlji_item_id(v_idx);
8490                                 end if;
8491                             end if;
8492 
8493                             if l_error_code <> 0 then
8494                                 l_error_count := l_error_count + 1;
8495                                 GOTO abort_this_job;
8496                             end if;
8497                             if lbji_debug = 'Y' then
8498                                 fnd_file.put_line(fnd_file.log, 'Validation of Item ID.. OK');
8499                             end if;
8500                             -- }} Validate item id
8501 
8502 
8503 l_stmt_num := 320;
8504                             -- {{ Validate net quantity (std job creation)
8505                             if (v_wlji_net_quantity(v_idx) < 0) then
8506                                 l_aux_mesg := 'Net Quantity should be > 0';
8507                                 handle_invalid_field('NET QUANTITY', l_aux_mesg, l_stmt_num);
8508                                 l_error_code := -1;
8509                                 l_error_count := l_error_count + 1;
8510                                 GOTO abort_this_job;
8511                             end if;
8512 
8513                             if (v_wlji_net_quantity(v_idx) IS NULL) then
8514                                 v_wlji_net_quantity(v_idx) := v_wlji_start_quantity(v_idx);
8515                             end if;
8516 
8517                             if v_wlji_net_quantity(v_idx) > v_wlji_start_quantity(v_idx) then
8518                                 l_aux_mesg := 'Net Quantity should be <= Start Quantity';
8519                                 handle_invalid_field('NET QUANTITY', l_aux_mesg, l_stmt_num);
8520                                 l_error_code := -1;
8521                                 l_error_count := l_error_count + 1;
8522                                 GOTO abort_this_job;
8523                             end if;
8524 
8525                             if lbji_debug = 'Y' then
8526                                 fnd_file.put_line(fnd_file.log, 'Net Quantity Validation.. OK.');
8527                             end if;
8528                             -- }} Validate net quantity
8529 
8530 l_stmt_num := 325;
8531                -- ST : Serial Support Project ----------------------
8532                --- Validate the start job qty and net qty ----------
8533                --- For serial controlled item have to be an integer...
8534                DECLARE
8535                 l_serial_ctrl_code NUMBER;
8536 
8537                BEGIN
8538                 select  msi.serial_number_control_code
8539                                 into    l_serial_ctrl_code
8540                                 from    mtl_system_items_kfv msi
8541                                 where   msi.inventory_item_id = v_wlji_item_id(v_idx)
8542                                 and     msi.organization_id   = v_wlji_org_id(v_idx);
8543 
8544                 IF l_serial_ctrl_code = 2 THEN  -- Predefined serial control...
8545 
8546                     IF floor(v_wlji_start_quantity(v_idx)) <> v_wlji_start_quantity(v_idx) THEN
8547                         -- error out...
8548                         -- Start qty cannot be non-integer...
8549                          fnd_message.set_name('WSM','WSM_INVALID_JOB_TXN_QTY');
8550                          l_aux_mesg := fnd_message.get;
8551                          handle_invalid_field('START QUANTITY', l_aux_mesg, l_stmt_num);
8552                          l_error_code := -1;
8553                          l_error_count := l_error_count + 1;
8554                          GOTO abort_this_job;
8555                     END IF;
8556 
8557                     IF floor(v_wlji_net_quantity(v_idx)) <> v_wlji_net_quantity(v_idx) THEN
8558                         -- error out...
8559                         -- Start qty cannot be non-integer...
8560                          fnd_message.set_name('WSM','WSM_INVALID_JOB_TXN_QTY');
8561                          l_aux_mesg := fnd_message.get;
8562                          handle_invalid_field('NET QUANTITY', l_aux_mesg, l_stmt_num);
8563                          l_error_code := -1;
8564                          l_error_count := l_error_count + 1;
8565                          GOTO abort_this_job;
8566                     END IF;
8567                 END IF;
8568                END;
8569                -- ST : Serial Support Project ----------------------
8570                --- Validate the start job qty and net qty ----------
8571 
8572 l_stmt_num := 330;
8573                             -- {{ Get l_common_rtg_seq_id  (std job creation)
8574                             begin
8575                             -- bugfix 2681637 validation of alternate designator should
8576                             -- check disable_date. (original bug for form 2558447)
8577                             -- view bom_routing_alternates_v does not have designator
8578                             -- disable_date infor.
8579 
8580                                select bor.routing_sequence_id,
8581                                       bor.COMPLETION_SUBINVENTORY,
8582                                       bor.COMPLETION_LOCATOR_ID
8583                                into   l_rtg_seq_id,
8584                                       l_default_compl_subinv,
8585                                       l_default_compl_loc_id
8586                                from   bom_operational_routings  bor,
8587                                       bom_alternate_designators bad
8588                                where  ((bor.alternate_routing_designator is null and
8589                                         bad.alternate_designator_code is null and
8590                                         bad.organization_id = -1) or
8591                                        (bor.alternate_routing_designator
8592                                             = bad.alternate_designator_code and
8593                                         bor.organization_id = bad.organization_id))
8594                                and    bor.organization_id = v_wlji_org_id(v_idx)
8595                                and    bor.assembly_item_id = v_wlji_item_id(v_idx)
8596                                and    NVL(bor.alternate_routing_designator, '&*')
8597                                           = NVL(v_wlji_alt_rtg(v_idx), '&*')
8598                                and    bor.routing_type = 1
8599                                and    bor.cfm_routing_flag = 3;
8600                                --Bug 5107339: Disable_date validation is not applicable here.
8601                                --and    trunc(nvl(bad.disable_date, sysdate + 1)) > trunc(sysdate);
8602 
8603                             exception
8604                                 when no_data_found then
8605                                     l_aux_mesg := '';
8606                                     handle_invalid_field(
8607                                         'ALTERNATE ROUTING DESIGNATOR', l_aux_mesg, l_stmt_num);
8608                                     l_error_code := -1;
8609                                     l_error_count := l_error_count + 1;
8610                                     GOTO abort_this_job;
8611                             end;
8612 
8613                             IF v_wlji_compl_subinv(v_idx) IS NULL AND
8614                                v_wlji_compl_loc_id(v_idx) IS NULL THEN
8615                                 v_wlji_compl_subinv(v_idx) := l_default_compl_subinv;
8616                                 v_wlji_compl_loc_id(v_idx) := l_default_compl_loc_id;
8617                             END IF;
8618 
8619                             IF v_wlji_compl_subinv(v_idx) IS NULL AND
8620                                v_wlji_compl_loc_id(v_idx) IS NOT NULL THEN
8621                                 v_wlji_compl_subinv(v_idx) := l_default_compl_subinv;
8622                             END IF;
8623 
8624                             IF v_wlji_compl_subinv(v_idx) IS NOT NULL AND
8625                                v_wlji_compl_loc_id(v_idx) IS NULL THEN
8626 l_stmt_num := 330.1;
8627                                 -- {{ validation of subinv begin
8628                                 hash_str   := to_char(v_wlji_org_id(v_idx))
8629                                               ||v_wlji_compl_subinv(v_idx);
8630                                 hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
8631                                 if WSMPLCVA.v_subinv.exists(hash_value) then
8632                                     NULL;
8633                                 else
8634                                     l_aux_mesg := '';
8635                                     handle_invalid_field(
8636                                         'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
8637                                     l_error_code := -1;
8638                                     l_error_count := l_error_count + 1;
8639                                     GOTO abort_this_job;
8640                                 end if;
8641                                 -- }} validation of subinv end
8642 l_stmt_num := 330.2;
8643                                 select locator_type
8644                                 into   l_mtl_locator_type
8645                                 from   mtl_secondary_inventories
8646                                 where  secondary_inventory_name
8647                                         = v_wlji_compl_subinv(v_idx)
8648                                 and    organization_id = v_wlji_org_id(v_idx);
8649 
8650                                 if v_wlji_compl_subinv(v_idx) = l_default_compl_subinv
8651                                 then
8652                                     v_wlji_compl_loc_id(v_idx) := l_default_compl_loc_id;
8653                                 else
8654                                     if l_mtl_locator_type = 2 then
8655                                         l_aux_mesg := '';
8656                                         handle_invalid_field(
8657                                             'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
8658                                         l_error_code := -1;
8659                                         l_error_count := l_error_count + 1;
8660                                         GOTO abort_this_job;
8661                                     end if;
8662                                 end if;
8663                             END IF;
8664 l_stmt_num := 330.3;
8665                             -- Get common routing seq id
8666                             l_error_msg := NULL;
8667                             wsmputil.find_common_routing(
8668                                 p_routing_sequence_id        => l_rtg_seq_id,
8669                                 p_common_routing_sequence_id => l_common_rtg_seq_id,
8670                                 x_err_code                   => l_error_code,
8671                                 x_err_msg                    => l_error_msg);
8672 
8673                             if l_error_code <> 0 OR l_error_msg IS NOT NULL then
8674                                 handle_error( l_error_code, l_error_msg, l_stmt_num);
8675                                 l_error_code := -1;
8676                                 l_error_count := l_error_count + 1;
8677                                 GOTO abort_this_job;
8678                             end if;
8679                             if lbji_debug = 'Y' then
8680                                 fnd_file.put_line(fnd_file.log,
8681                                     'Routing Sequence Id: '||l_rtg_seq_id ||
8682                                     ', Common Routing Sequence Id: '||l_common_rtg_seq_id);
8683                             end if;
8684                             -- }} get l_common_rtg_seq_id end
8685 
8686 
8687 l_stmt_num := 340;
8688                             -- {{ Get l_common_bom_seq_id (std job creation)
8689                             -- if the alternate_bom_designator has NULL in wlji, bill_seq_id
8690                             -- can have either NULL or a primary bom value. But if the
8691                             -- designator has ALT, then there must be a bill id for the
8692                             -- alternate bom.
8693                             IF v_wlji_alt_bom(v_idx) is NULL THEN
8694                                 begin
8695                                     SELECT  bom.common_bill_sequence_id
8696                                     INTO    l_common_bom_seq_id
8697                                     FROM    bom_bill_of_materials bom
8698                                     WHERE   bom.alternate_bom_designator is NULL
8699                                     AND     BOM.assembly_item_id = v_wlji_item_id(v_idx)
8700                                     AND     bom.organization_id = v_wlji_org_id(v_idx);
8701                                 exception
8702                                     WHEN NO_DATA_FOUND THEN
8703                                         NULL;
8704                                 end;
8705                             ELSE
8706 l_stmt_num := 345.1;
8707                                 begin
8708                                     SELECT  bom.common_bill_sequence_id
8709                                     INTO    l_common_bom_seq_id
8710                                     FROM    bom_bill_of_materials bom,
8711                                             bom_alternate_designators bad
8712                                     WHERE   ((bom.alternate_bom_designator is null and
8713                                               bad.alternate_designator_code is null and
8714                                               bad.organization_id = -1) OR
8715                                              (bom.alternate_bom_designator
8716                                                 = bad.alternate_designator_code and
8717                                               bom.organization_id = bad.organization_id))
8718                                     AND     bom.alternate_bom_designator
8719                                                 = v_wlji_alt_bom(v_idx)
8720                                     AND     BOM.assembly_item_id = v_wlji_item_id(v_idx)
8721                                     AND     bom.organization_id = v_wlji_org_id(v_idx);
8722                                     --Bug 5107339: Disable_date validation is not applicable here.
8723                                     --AND     trunc(nvl(bad.disable_date, sysdate + 1)) > trunc(sysdate);
8724 
8725                                 exception
8726                                     WHEN no_data_found  THEN
8727                                         l_aux_mesg := '';
8728                                         handle_invalid_field(
8729                                             'ALTERNATE BOM DESIGNATOR', l_aux_mesg, l_stmt_num);
8730                                         l_error_code := -1;
8731                                         l_error_count := l_error_count + 1;
8732                                         GOTO abort_this_job;
8733                                 end;
8734                             END IF;
8735                             if lbji_debug = 'Y' then
8736                                 fnd_file.put_line(fnd_file.log,
8737                                     'Getting Bill Sequence Id: '||l_common_bom_seq_id);
8738                             end if;
8739                             -- }} get l_common_bom_seq_id
8740 
8741 
8742 l_stmt_num := 350;
8743                             -- {{ Validate locator id (std job creation)
8744                             SELECT  nvl(msub.locator_type, 1) sub_loc_control,
8745                                     MP.stock_locator_control_code org_loc_control,
8746                                     MS.restrict_locators_code,
8747                                     MS.location_control_code item_loc_control
8748                             into    l_sub_loc_control,
8749                                     l_org_loc_control,
8750                                     l_restrict_locators_code,
8751                                     l_item_loc_control
8752                             FROM    mtl_system_items MS,
8753                                     mtl_secondary_inventories MSUB,
8754                                     mtl_parameters MP
8755                             WHERE   MP.organization_id = v_wlji_org_id(v_idx)
8756                             AND     MS.organization_id = v_wlji_org_id(v_idx)
8757                             AND     MS.inventory_item_id = v_wlji_item_id(v_idx)
8758                             AND     MSUB.secondary_inventory_name
8759                                         = v_wlji_compl_subinv(v_idx)
8760                             AND     MSUB.organization_id = v_wlji_org_id(v_idx);
8761 
8762                             l_locator_id := v_wlji_compl_loc_id(v_idx);
8763 
8764                             WIP_LOCATOR.validate(
8765                                     v_wlji_org_id(v_idx),
8766                                     v_wlji_item_id(v_idx),
8767                                     v_wlji_compl_subinv(v_idx),
8768                                     l_org_loc_control,
8769                                     l_sub_loc_control,
8770                                     l_item_loc_control,
8771                                     l_restrict_locators_code,
8772                                     NULL, NULL, NULL, NULL,
8773                                     l_locator_id,
8774                                     dummy_segs,
8775                                     l_temp_boolean);
8776 
8777                             IF not l_temp_boolean THEN
8778                                 l_aux_mesg := '';
8779                                 handle_invalid_field(
8780                                     'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
8781                                 l_error_code := -1;
8782                                 l_error_count := l_error_count + 1;
8783                                 GOTO abort_this_job;
8784                             end if;
8785                             if lbji_debug = 'Y' then
8786                                 fnd_file.put_line(fnd_file.log, 'Locator Id Validation.. OK.');
8787                             end if;
8788                             -- }} validate locator id
8789 
8790 
8791 l_stmt_num := 360;
8792                             -- {{ validate class code (std job creation)
8793                             begin
8794                                 IF v_wlji_class_code(v_idx) is NULL then
8795                                     begin
8796                                         select wse.default_acct_class_code
8797                                         into   v_wlji_class_code(v_idx)
8798                                         from   wsm_sector_extensions wse,
8799                                                wsm_item_extensions wie
8800                                         where  wie.inventory_item_id = v_wlji_item_id(v_idx)
8801                                         and    wie.organization_id = v_wlji_org_id(v_idx)
8802                                         and    wie.sector_extension_id = wse.sector_extension_id
8803                                         and    wie.organization_id = wse.organization_id;
8804                                     exception
8805                                         when no_data_found then
8806                                             v_wlji_class_code(v_idx) := NULL;
8807                                     end;
8808 
8809                                     IF v_wlji_class_code(v_idx) is NULL then
8810                                         begin
8811                                             select wse.default_acct_class_code
8812                                             into   v_wlji_class_code(v_idx)
8813                                             from   wsm_sector_extensions wse,
8814                                                    wsm_subinventory_extensions wve
8815                                             where  wve.secondary_inventory_name
8816                                                       = v_wlji_compl_subinv(v_idx)
8817                                             and    wve.organization_id = v_wlji_org_id(v_idx)
8818                                             and    wve.sector_extension_id = wse.sector_extension_id
8819                                             and    wve.organization_id = wse.organization_id;
8820                                         exception
8821                                             WHEN NO_DATA_FOUND THEN
8822                                                 v_wlji_class_code(v_idx) := NULL;
8823                                         end;
8824 
8825                                         IF v_wlji_class_code(v_idx) is NULL then
8826                                             v_wlji_class_code(v_idx)
8827                                                 := WSMPLCVA.v_org(v_wlji_org_id(v_idx)).DEFAULT_ACCT_CLASS_CODE;
8828                                         END IF;
8829                                     END IF;
8830 
8831                                     IF v_wlji_class_code(v_idx) IS NULL THEN
8832                                         raise no_data_found;
8833                                     END IF;
8834 
8835                                 ELSE
8836                                     hash_str   := to_char(v_wlji_org_id(v_idx))
8837                                                   ||v_wlji_class_code(v_idx);
8838                                     hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
8839                                     if WSMPLCVA.v_class_code.exists(hash_value) then
8840                                         NULL;
8841                                     else
8842                                         raise no_data_found;
8843                                     end if;
8844                                 END IF;
8845                             exception
8846                                 WHEN no_data_found  THEN
8847                                     l_aux_mesg := 'Class code may not be NULL';
8848                                     handle_invalid_field('CLASS_CODE', l_aux_mesg, l_stmt_num);
8849                                     l_error_code := -1;
8850                                     l_error_count := l_error_count + 1;
8851                                     GOTO abort_this_job;
8852                             end;
8853                             if lbji_debug = 'Y' then
8854                                 fnd_file.put_line(fnd_file.log, 'Class Code Validation.. OK.');
8855                             end if;
8856                             -- }} validate class code
8857 
8858 
8859 l_stmt_num := 370;
8860                             -- {{ Get revisions (std job creation)
8861                             -- this procedure is called only after date validation so that the
8862                             -- start date is not null. This is to be called only for job creation.
8863                             -- v_wlji_rtg_rev_date(v_idx) will be populated
8864 
8865                             if v_wlji_fusd(v_idx) > SYSDATE then
8866                                 l_rev_date := v_wlji_fusd(v_idx);
8867                             else
8868                                 l_rev_date := SYSDATE;
8869                             end if;
8870 
8871                             -- BA: bug 3437412 revision date should default to job start date
8872                             -- if v_wlji_fusd(v_idx) IS NULL (this situation can happen only when
8873                             -- scheduling_method=1 and fusd is not specified, l_rev_date = SYSDATE,
8874                             -- and routing/bom revision date will be default to SYSDATE,
8875                             if(v_wlji_rtg_rev_date(v_idx) IS NULL and
8876                                v_wlji_rtg_revision(v_idx) IS NULL) then
8877                                 v_wlji_rtg_rev_date(v_idx) := l_rev_date;
8878                             end if;
8879                             if(v_wlji_bom_rev_date(v_idx) IS NULL and
8880                                v_wlji_bom_revision(v_idx) IS NULL) then
8881                                 v_wlji_bom_rev_date(v_idx) := l_rev_date;
8882                             end if;
8883                             -- BA: bug 3437412 should default to job start date
8884 
8885                             wip_revisions.bom_revision (
8886                                     v_wlji_org_id(v_idx),
8887                                     v_wlji_item_id(v_idx),
8888                                     v_wlji_bom_revision(v_idx),
8889                                     v_wlji_bom_rev_date(v_idx),
8890                                     l_rev_date);
8891                             wip_revisions.routing_revision (
8892                                     v_wlji_org_id(v_idx),
8893                                     v_wlji_item_id(v_idx),
8894                                     v_wlji_rtg_revision(v_idx),
8895                                     v_wlji_rtg_rev_date(v_idx),
8896                                     l_rev_date);
8897 
8898                             if l_error_code <> 0 then
8899                                 l_error_code := -1;
8900                                 l_error_count := l_error_count + 1;
8901                                 GOTO abort_this_job;
8902                             end if;
8903                             if lbji_debug = 'Y' then
8904                                 fnd_file.put_line(fnd_file.log, 'Getting BOM/Routing Revisions.. OK.');
8905                             end if;
8906                             -- }} get revisions
8907 
8908                         end if; -- job type =1
8909                         -----------------------------------------------------
8910                         -- }}} End: validations for standard job creations
8911                         -----------------------------------------------------
8912 
8913 l_stmt_num := 380;
8914                         -- {{ Validate Date_Released, job_type 1 and 3
8915                         if (v_wlji_status_type(v_idx) = WIP_CONSTANTS.UNRELEASED) and
8916                            (v_wlji_date_released(v_idx) is not null ) then
8917 
8918                             v_wlji_date_released(v_idx) := null;
8919                             if lbji_debug = 'Y' then
8920                                 fnd_file.put_line(fnd_file.log,
8921                                     'Date_Released is ignored for unreleased job');
8922                             end if;
8923                         elsif (v_wlji_status_type(v_idx) = WIP_CONSTANTS.RELEASED ) then
8924                             if (v_wlji_date_released(v_idx) > sysdate ) then
8925                                 l_error_code := -1;
8926                                 handle_errmsg_name('WIP', 'WIP_INVALID_RELEASE_DATE', l_stmt_num);
8927                                 l_error_count := l_error_count + 1;
8928                                 GOTO abort_this_job;
8929                             elsif (v_wlji_date_released(v_idx) is null) then
8930                                 v_wlji_date_released(v_idx) := sysdate;
8931                             end if;
8932                         end if;
8933                         if lbji_debug = 'Y' then
8934                             fnd_file.put_line(fnd_file.log,
8935                                 'Date_Released Validation.. First Phase Done.');
8936                         end if;
8937                         -- }} Validate Date_Released
8938 
8939                         -- -------------------------------------------------------------------
8940                         -- {{{ Validations for the starting lot in wsli begin (std, creation)
8941                         -- -------------------------------------------------------------------
8942                         -- These validations are to be performed only for jobs of mode flag 2
8943                         -- Since mode 2 only support std job, v_wlji_job_type(v_indev) = 1
8944 
8945                         if v_wlji_mode_flag(v_idx) = 2 then
8946 l_stmt_num := 400;
8947                             -- last_updated_by
8948                             l_num_records := 0;
8949                             l_xst := WSMPLCVA.v_user.exists(
8950                                         v_wsli(v_wlji_src_line_id(v_idx)).last_updated_by);
8951                             if l_xst = false then
8952                                 begin
8953                                     select 1
8954                                     into   l_num_records
8955                                     from   fnd_user
8956                                     where  user_id = v_wsli(v_wlji_src_line_id(v_idx)).last_updated_by
8957                                     and    sysdate between start_date and nvl(end_date,sysdate+1);
8958                                 exception
8959                                     when too_many_rows then l_num_records := 1;
8960                                     when no_data_found then
8961                                         l_error_code := -1;
8962                                         l_aux_mesg := '';
8963                                         handle_invalid_field(
8964                                             'Last Updated By in WSM_STARTING_LOTS_INTERFACE',
8965                                             l_aux_mesg,
8966                                             l_stmt_num);
8967                                         l_error_count := l_error_count + 1;
8968                                         GOTO abort_this_job;
8969                                 end;
8970                             end if;
8971 
8972                             if l_xst = false AND l_num_records <> 0 then
8973                                 WSMPLCVA.v_user(v_wsli(v_wlji_src_line_id(v_idx)).last_updated_by)
8974                                     := v_wsli(v_wlji_src_line_id(v_idx)).last_updated_by;
8975                             end if;
8976 l_stmt_num := 410;
8977                             -- wsli.created_by
8978                             l_num_records := 0;
8979                             l_xst := WSMPLCVA.v_user.exists(v_wsli(v_wlji_src_line_id(v_idx)).created_by);
8980                             if l_xst = false then
8981                                 begin
8982                                     select 1
8983                                     into   l_num_records
8984                                     from   fnd_user
8985                                     where  user_id = v_wsli(v_wlji_src_line_id(v_idx)).created_by
8986                                     and    sysdate between start_date and nvl(end_date,sysdate+1);
8987                                 exception
8988                                     when too_many_rows then l_num_records := 1;
8989                                     when no_data_found then
8990                                         l_error_code := -1;
8991                                         l_aux_mesg := '';
8992                                         handle_invalid_field(
8993                                             'Created By in WSM_STARTING_LOTS_INTERFACE',
8994                                             l_aux_mesg,
8995                                             l_stmt_num);
8996                                         l_error_count := l_error_count + 1;
8997                                         GOTO abort_this_job;
8998                                 end;
8999                             end if;
9000 
9001                             if l_xst = false AND l_num_records <> 0 then
9002                                 WSMPLCVA.v_user(v_wsli(v_wlji_src_line_id(v_idx)).created_by)
9003                                     := v_wsli(v_wlji_src_line_id(v_idx)).created_by;
9004                             end if;
9005 
9006 l_stmt_num := 420;
9007                             -- Check.MMTT
9008                             begin
9009                                 select 1
9010                                 into   l_temp_num
9011                                 from   mtl_transaction_lots_temp
9012                                 where  lot_number = v_wsli(v_wlji_src_line_id(v_idx)).lot_number
9013                                 and    rownum = 1;
9014 
9015                                 if l_temp_num <>0 then
9016                                     select 0
9017                                     into   l_temp_num
9018                                     from   mtl_material_transactions_temp mmtt
9019                                     where  mmtt.organization_id        = v_wsli(v_wlji_src_line_id(v_idx)).organization_id
9020                                     and    mmtt.inventory_item_id      = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
9021                                     and    nvl(mmtt.lot_number, '@#$') = v_wsli(v_wlji_src_line_id(v_idx)).lot_number
9022                                     and    mmtt.subinventory_code      = v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code
9023                                     and    nvl(mmtt.locator_id, -9999) = nvl(v_wsli(v_wlji_src_line_id(v_idx)).locator_id, -9999)
9024                                     and    mmtt.transaction_type_id    = 42 -- miscellaneous receipt
9025                                     and    mmtt.transaction_action_id  = 27 -- receipt into stores
9026                                     and    mmtt.transaction_source_type_id = 13 -- inventory
9027                                     and    v_wsli(v_wlji_src_line_id(v_idx)).quantity = ((-1) * mmtt.transaction_quantity)
9028                                     and    mmtt.transaction_date = (
9029                                                 select max(mmtt2.transaction_date)
9030                                                 from   mtl_material_transactions_temp mmtt2
9031                                                 where  mmtt2.organization_id        = v_wsli(v_wlji_src_line_id(v_idx)).organization_id
9032                                                 and    mmtt2.inventory_item_id      = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
9033                                                 and    nvl(mmtt2.lot_number, '@#$') = nvl(v_wsli(v_wlji_src_line_id(v_idx)).lot_number, '@#$')
9034                                                 and    mmtt2.subinventory_code      = v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code
9035                                                 and    nvl(mmtt2.locator_id, -9999) = nvl(v_wsli(v_wlji_src_line_id(v_idx)).locator_id, -9999)
9036                                             );
9037                                 end if;
9038                                 If l_temp_num <> 0 then
9039                                     fnd_message.set_name('WSM', 'WSM_PENDING_TXN');
9040                                     fnd_message.set_token('TABLE','Starting Lot:' ||
9041                                         v_wsli(v_wlji_src_line_id(v_idx)).lot_number ||
9042                                         'Table: mtl_transaction_lots_temp ');
9043                                     l_error_msg := fnd_message.get;
9044                                     l_error_code := -1;
9045                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
9046                                     l_error_count := l_error_count + 1;
9047                                     GOTO abort_this_job;
9048                                 END IF;
9049 
9050                             exception
9051                                 when no_data_found Then
9052                                     null;
9053                             end;
9054 --Bug 5182520:Added validation on Material Status
9055 l_stmt_num := 422;
9056                             IF (WSMPUTIL.is_status_applicable(p_wms_installed =>NULL,
9057                               p_trx_status_enabled    => NULL,
9058                               p_trx_type_id           => 35,
9059                               p_lot_status_enabled    => NULL,
9060                               p_serial_status_enabled => NULL,
9061                               p_organization_id       => v_wsli(v_wlji_src_line_id(v_idx)).organization_id,
9062                               p_inventory_item_id     => v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
9063                               p_sub_code              => v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code,
9064                               p_locator_id            => v_wsli(v_wlji_src_line_id(v_idx)).locator_id,
9065                               p_lot_number            => v_wsli(v_wlji_src_line_id(v_idx)).lot_number,
9066                               p_serial_number         => NULL,
9067                               x_error_msg             => l_error_msg) = 'N') THEN
9068 
9069 	                            l_error_code := -1;
9070                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
9071                                     l_error_count := l_error_count + 1;
9072                                     GOTO abort_this_job;
9073 
9074                             END IF;
9075 
9076 l_stmt_num := 430;
9077                             -- {{ kanban begin
9078                             if v_wlji_kanban_card_id(v_idx) is not null then
9079                                 if WSMPLBJI.honor_kanban_size(
9080                                             v_wlji_org_id(v_idx),
9081                                             v_wlji_item_id(v_idx),
9082                                             v_wlji_compl_subinv(v_idx),
9083                                             v_wlji_compl_loc_id(v_idx),
9084                                             -1) = 1
9085                                 then
9086 l_stmt_num := 430.1;
9087                                     select kanban_size
9088                                     into   l_kanban_size
9089                                     from   mtl_kanban_cards
9090                                     where  kanban_card_id = v_wlji_kanban_card_id(v_idx);
9091                                     if v_wlji_start_quantity(v_idx) > l_kanban_size then
9092                                         v_wlji_start_quantity(v_idx) := l_kanban_size;
9093                                         v_wlji_net_quantity(v_idx)   := v_wlji_start_quantity(v_idx);
9094                                     end if;
9095                                 end if;
9096                             end if;
9097                             -- }} Kanban end
9098 
9099 l_stmt_num := 440;
9100                             -- ------------------------------------------------------------
9101                             -- Check if the component is required at first operation
9102                             -- Check component quantity
9103                             --
9104                             -- The code should be moved to where job details are imported
9105                             -- because user intention should be honored
9106                             -- ------------------------------------------------------------
9107 
9108 
9109                         end if; -- mode_flag 2
9110                         -- ----------------------------------------------------------------
9111                         -- }}} Validations for the starting lot in wsli end (std, creation)
9112                         -- ----------------------------------------------------------------
9113 
9114                     end if;  -- load_type = 5
9115 -- ===========================================================================================
9116 -- END: Job Creation Validations }}}
9117 -- ===========================================================================================
9118 
9119 
9120 
9121 -- ===========================================================================================
9122 -- BEGIN: Reschedule (Update) Job Validations {{{
9123 -- ===========================================================================================
9124                     if v_wlji_load_type(v_idx) = 6 then
9125                         --------------------------------------------
9126                         -- {{{ Begin: Update of alternates (std job)
9127                         --------------------------------------------
9128                         -- If the user wants to update an alternate to NULL value, he should
9129                         -- populate '-99' in the interface table. If NULL is populated, we
9130                         -- consider that user does not want to change ALT
9131                         if l_cur_job_type = 1 then  -- {{ if std-job
9132 l_stmt_num := 500;
9133                             l_chg_bom_alt :=0;
9134                             l_chg_rtg_alt :=0;
9135                             l_chg_alt_flag:=0;
9136 
9137                             if v_wlji_alt_bom(v_idx) is NOT NULL AND
9138                                ((l_cur_alt_bom is NULL AND
9139                                  v_wlji_alt_bom(v_idx) <> '-99') OR
9140                                 (l_cur_alt_bom is NOT NULL AND
9141                                  v_wlji_alt_bom(v_idx) <> l_cur_alt_bom))
9142                             then
9143                                 l_chg_bom_alt := 1;
9144                             end if;
9145 
9146                             if v_wlji_alt_rtg(v_idx) is NOT NULL AND
9147                                ((l_cur_alt_rtg is NULL AND
9148                                  v_wlji_alt_rtg(v_idx) <> '-99') OR
9149                                 (l_cur_alt_rtg is NOT NULL AND
9150                                  v_wlji_alt_rtg(v_idx) <> l_cur_alt_rtg))
9151                             then
9152                                 l_chg_rtg_alt := 1;
9153                             end if;
9154 
9155                             -- {{ Change bom/rtg alternate
9156                             if (l_chg_bom_alt = 1 OR l_chg_rtg_alt = 1) and
9157                                l_cur_status_type = 1
9158                             then
9159                                 l_chg_alt_flag := 1;
9160 
9161                                 -- populate local variables
9162                                 v_wlji_item_id(v_idx) := l_cur_primary_item_id;
9163 
9164                                 if l_chg_bom_alt = 1 then
9165                                     if v_wlji_alt_bom(v_idx) = '-99' then
9166                                         v_wlji_alt_bom(v_idx) := null;
9167                                     end if;
9168                                 else
9169                                     v_wlji_alt_bom(v_idx) := l_cur_alt_bom;
9170                                 end if;
9171 
9172                                 if l_chg_rtg_alt = 1 then
9173                                     if v_wlji_alt_rtg(v_idx) = '-99' then
9174                                         v_wlji_alt_rtg(v_idx) := null;
9175                                     end if;
9176                                 else
9177                                     v_wlji_alt_rtg(v_idx) := l_cur_alt_rtg;
9178                                 end if;
9179 
9180 l_stmt_num := 510;
9181                                 -- Now validate the alternate designators to get the NEW
9182                                 -- common routing and bom sequence id.
9183                                 -- Also get the completion subinventory and locator.
9184                                 -- If the user has provided them, use them after validation,
9185                                 -- else use these default values. Also validate/default
9186                                 -- the bom/rtg revision and revision-dates
9187                                 begin
9188                                     select bor.routing_sequence_id,
9189                                            bor.COMPLETION_SUBINVENTORY,
9190                                            bor.COMPLETION_LOCATOR_ID
9191                                     into   l_rtg_seq_id,
9192                                            l_default_compl_subinv,
9193                                            l_default_compl_loc_id
9194                                     from   bom_operational_routings bor,
9195                                            bom_alternate_designators bad
9196                                     where  ((bor.alternate_routing_designator is null and
9197                                              bad.alternate_designator_code is null and
9198                                              bad.organization_id = -1) OR
9199                                             (bor.alternate_routing_designator
9200                                                 = bad.alternate_designator_code and
9201                                              bor.organization_id = bad.organization_id))
9202                                     and    bor.organization_id = v_wlji_org_id(v_idx)
9203                                     and    bor.assembly_item_id = v_wlji_item_id(v_idx)
9204                                     and    NVL(bor.alternate_routing_designator, '&*')
9205                                                = NVL(v_wlji_alt_rtg(v_idx), '&*')
9206                                     and    bor.routing_type = 1
9207                                     and    bor.cfm_routing_flag = 3;
9208                                     --Bug 5107339: Disable_date validation is not applicable here.
9209                                     --and    trunc(nvl(bad.disable_date, sysdate + 1))
9210                                      --           > trunc(sysdate);
9211                                 exception
9212                                     when no_data_found then
9213                                         l_aux_mesg := '';
9214                                         handle_invalid_field(
9215                                             'Alternate Routing Designator',
9216                                             l_aux_mesg, l_stmt_num);
9217                                         l_error_code := -1;
9218                                         l_error_count := l_error_count + 1;
9219                                         GOTO skip_resched_validations;
9220                                 end;
9221 l_stmt_num := 520;
9222                                 -- find l_common_rtg_seq_id
9223                                 l_error_msg := NULL;
9224                                 wsmputil.find_common_routing(
9225                                     p_routing_sequence_id        => l_rtg_seq_id,
9226                                     p_common_routing_sequence_id => l_common_rtg_seq_id,
9227                                     x_err_code                   => l_error_code,
9228                                     x_err_msg                    => l_error_msg);
9229                                 if l_error_code <> 0 OR l_error_msg IS NOT NULL then
9230                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
9231                                     l_error_code := -1;
9232                                     l_error_count := l_error_count + 1;
9233                                     GOTO skip_resched_validations;
9234                                 end if;
9235 
9236 l_stmt_num := 530;
9237                                 IF v_wlji_compl_subinv(v_idx) IS NULL AND
9238                                    v_wlji_compl_loc_id(v_idx) IS NULL THEN
9239                                     v_wlji_compl_subinv(v_idx) := l_default_compl_subinv;
9240                                     v_wlji_compl_loc_id(v_idx) := l_default_compl_loc_id;
9241                                 END IF;
9242 
9243                                 IF v_wlji_compl_subinv(v_idx) IS NULL AND
9244                                    v_wlji_compl_loc_id(v_idx) IS NOT NULL THEN
9245                                     v_wlji_compl_subinv(v_idx) := l_default_compl_subinv;
9246                                 END IF;
9247 
9248                                 IF v_wlji_compl_subinv(v_idx) IS NOT NULL AND
9249                                    v_wlji_compl_loc_id(v_idx) IS NULL THEN
9250 
9251                                     hash_str   := to_char(v_wlji_org_id(v_idx))
9252                                                   ||v_wlji_compl_subinv(v_idx);
9253                                     hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
9254                                     if WSMPLCVA.v_subinv.exists(hash_value) then
9255                                         NULL;
9256                                     else
9257                                         l_aux_mesg := '';
9258                                         handle_invalid_field(
9259                                             'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
9260                                         l_error_code := -1;
9261                                         l_error_count := l_error_count + 1;
9262                                         GOTO skip_resched_validations;
9263                                     end if;
9264 
9265 l_stmt_num := 540;
9266                                     select locator_type
9267                                     into   l_mtl_locator_type
9268                                     from   mtl_secondary_inventories
9269                                     where  secondary_inventory_name
9270                                                = v_wlji_compl_subinv(v_idx)
9271                                     and    organization_id = v_wlji_org_id(v_idx);
9272 
9273                                     if v_wlji_compl_subinv(v_idx)
9274                                          = l_default_compl_subinv
9275                                     then
9276                                         v_wlji_compl_loc_id(v_idx)
9277                                              := l_default_compl_loc_id;
9278                                     else
9279                                         if l_mtl_locator_type = 2 then
9280                                             l_aux_mesg := '';
9281                                             handle_invalid_field(
9282                                                 'COMPLETION SUBINVENTORY',
9283                                                 l_aux_mesg, l_stmt_num);
9284                                             l_error_code := -1;
9285                                             l_error_count := l_error_count + 1;
9286                                             GOTO skip_resched_validations;
9287                                         end if;
9288                                     end if;
9289                                 END IF;
9290 
9291 l_stmt_num := 550;
9292                                 IF v_wlji_alt_bom(v_idx) is NULL THEN
9293                                     begin
9294                                         select  bom.common_bill_sequence_id
9295                                         into    l_common_bom_seq_id
9296                                         from    bom_bill_of_materials bom
9297                                         where   bom.alternate_bom_designator is null
9298                                         and     bom.assembly_item_id = v_wlji_item_id(v_idx)
9299                                         and     bom.organization_id = v_wlji_org_id(v_idx);
9300                                     exception
9301                                         WHEN NO_DATA_FOUND THEN
9302                                             NULL;
9303                                     end;
9304                                 ELSE
9305                                     begin
9306                                         select  bom.common_bill_sequence_id
9307                                         into    l_common_bom_seq_id
9308                                         from    bom_bill_of_materials bom,
9309                                                 bom_alternate_designators bad
9310                                         where   ((bom.alternate_bom_designator is null and
9311                                                   bad.alternate_designator_code is null and
9312                                                   bad.organization_id = -1) or
9313                                                  (bom.alternate_bom_designator
9314                                                       = bad.alternate_designator_code and
9315                                                   bom.organization_id = bad.organization_id))
9316                                         and     bom.alternate_bom_designator
9317                                                     = v_wlji_alt_bom(v_idx)
9318                                         and     bom.assembly_item_id = v_wlji_item_id(v_idx)
9319                                         and     bom.organization_id = v_wlji_org_id(v_idx);
9320                                         --Bug 5107339: Disable_date validation is not applicable here.
9321                                         --  and     trunc(nvl(bad.disable_date, sysdate + 1))
9322                                         --             > trunc(sysdate);
9323                                     exception
9324                                         WHEN no_data_found  THEN
9325                                             l_aux_mesg := '';
9326                                             handle_invalid_field(
9327                                                 'Alternate Bom Designator', l_aux_mesg, l_stmt_num);
9328                                             l_error_code := -1;
9329                                             l_error_count := l_error_count + 1;
9330                                             GOTO skip_resched_validations;
9331                                     end;
9332                                 END IF;
9333 
9334 l_stmt_num := 560;
9335                                 SELECT  nvl(msub.locator_type, 1) sub_loc_control,
9336                                         MP.stock_locator_control_code org_loc_control,
9337                                         MS.restrict_locators_code,
9338                                         MS.location_control_code item_loc_control
9339                                 into    l_sub_loc_control,
9340                                         l_org_loc_control,
9341                                         l_restrict_locators_code,
9342                                         l_item_loc_control
9343                                 FROM    mtl_system_items MS,
9344                                         mtl_secondary_inventories MSUB,
9345                                         mtl_parameters MP
9346                                 WHERE   MP.organization_id = v_wlji_org_id(v_idx)
9347                                 AND     MS.organization_id = v_wlji_org_id(v_idx)
9348                                 AND     MS.inventory_item_id = v_wlji_item_id(v_idx)
9349                                 AND     MSUB.secondary_inventory_name
9350                                             = v_wlji_compl_subinv(v_idx)
9351                                 AND     MSUB.organization_id = v_wlji_org_id(v_idx);
9352 
9353 l_stmt_num := 570;
9354                                 l_locator_id := v_wlji_compl_loc_id(v_idx);
9355                                 WIP_LOCATOR.validate(
9356                                         v_wlji_org_id(v_idx),
9357                                         v_wlji_item_id(v_idx),
9358                                         v_wlji_compl_subinv(v_idx),
9359                                         l_org_loc_control,
9360                                         l_sub_loc_control,
9361                                         l_item_loc_control,
9362                                         l_restrict_locators_code,
9363                                         NULL, NULL, NULL, NULL,
9364                                         l_locator_id,
9365                                         dummy_segs,
9366                                         l_temp_boolean);
9367                                 IF not l_temp_boolean THEN
9368                                     l_aux_mesg := '';
9369                                     handle_invalid_field(
9370                                         'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
9371                                     l_error_code := -1;
9372                                     l_error_count := l_error_count + 1;
9373                                     GOTO skip_resched_validations;
9374                                 end if;
9375 
9376 l_stmt_num := 580;
9377                                 if v_wlji_fusd(v_idx) is null then
9378                                     v_wlji_fusd(v_idx) := l_cur_start_date;
9379                                 end if;
9380 
9381                                 if v_wlji_fusd(v_idx) > SYSDATE then
9382                                     l_rev_date := v_wlji_fusd(v_idx);
9383                                 else
9384                                     l_rev_date := SYSDATE;
9385                                 end if;
9386 l_stmt_num := 590.1;
9387                                 wip_revisions.bom_revision (
9388                                         v_wlji_org_id(v_idx),
9389                                         v_wlji_item_id(v_idx),
9390                                         v_wlji_bom_revision(v_idx),
9391                                         v_wlji_bom_rev_date(v_idx),
9392                                         l_rev_date);
9393 l_stmt_num := 590.2;
9394                                 wip_revisions.routing_revision (
9395                                         v_wlji_org_id(v_idx),
9396                                         v_wlji_item_id(v_idx),
9397                                         v_wlji_rtg_revision(v_idx),
9398                                         v_wlji_rtg_rev_date(v_idx),
9399                                         l_rev_date);
9400 
9401                             end if; -- }} Change bom/routing alternate
9402 
9403                             if (l_chg_bom_alt = 1 OR l_chg_rtg_alt = 1) and
9404                                l_cur_status_type <> 1
9405                             then
9406                                 fnd_file.put_line(fnd_file.log,
9407                                     'Ignoring the following fields if provided by user: ' ||
9408                                     'Alternate_Routing_Designator, Alternate_Bom_Designator.');
9409                             end if;
9410 
9411                             v_wlji_bom_ref_id(v_idx) := null;
9412                             v_wlji_rtg_ref_id(v_idx) := null;
9413                             if l_chg_alt_flag = 0 then
9414                                 l_common_bom_seq_id        := l_cur_com_bom_seq_id;
9415                                 v_wlji_alt_bom(v_idx)      := l_cur_alt_bom;
9416                                 --v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date; -- DEL bug3202534
9417                                 --v_wlji_bom_revision(v_idx) := l_cur_bom_revision; -- DEL bug3202534
9418                                 l_common_rtg_seq_id        := l_cur_com_rtg_seq_id;
9419                                 v_wlji_alt_rtg(v_idx)      := l_cur_alt_rtg;
9420                                 --v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date; -- DEL bug3202534
9421                                 --v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision; -- DEL bug3202534
9422                                 --v_wlji_compl_subinv(v_idx) := l_cur_compl_subinv; Bug 4919218
9423                                 --v_wlji_compl_loc_id(v_idx) := l_cur_compl_locator; Bug 4919218
9424                                 -- Validation added for Bug 4919218: Start
9425 l_stmt_num := 590.3;
9426                                 IF v_wlji_compl_subinv(v_idx) IS NULL AND
9427                                    v_wlji_compl_loc_id(v_idx) IS NULL THEN
9428                                     v_wlji_compl_subinv(v_idx) := l_cur_compl_subinv;
9429                                     v_wlji_compl_loc_id(v_idx) := l_cur_compl_locator;
9430                                 END IF;
9431 
9432                                 IF v_wlji_compl_subinv(v_idx) IS NULL AND
9433                                    v_wlji_compl_loc_id(v_idx) IS NOT NULL THEN
9434                                     v_wlji_compl_subinv(v_idx) := l_cur_compl_subinv;
9435                                 END IF;
9436 
9437                                 IF v_wlji_compl_subinv(v_idx) IS NOT NULL AND
9438                                    v_wlji_compl_loc_id(v_idx) IS NULL THEN
9439 
9440                                     hash_str   := to_char(v_wlji_org_id(v_idx))
9441                                                   ||v_wlji_compl_subinv(v_idx);
9442                                     hash_value := dbms_utility.get_hash_value(hash_str, 37, 1073741824);
9443                                     if WSMPLCVA.v_subinv.exists(hash_value) then
9444                                         NULL;
9445                                     else
9446                                         l_aux_mesg := '';
9447                                         handle_invalid_field(
9448                                             'COMPLETION SUBINVENTORY', l_aux_mesg, l_stmt_num);
9449                                         l_error_code := -1;
9450                                         l_error_count := l_error_count + 1;
9451                                         GOTO skip_resched_validations;
9452                                     end if;
9453 
9454 l_stmt_num := 590.4;
9455                                     select locator_type
9456                                     into   l_mtl_locator_type
9457                                     from   mtl_secondary_inventories
9458                                     where  secondary_inventory_name
9459                                                = v_wlji_compl_subinv(v_idx)
9460                                     and    organization_id = v_wlji_org_id(v_idx);
9461 
9462                                     if v_wlji_compl_subinv(v_idx)
9463                                          = l_cur_compl_subinv
9464                                     then
9465                                         v_wlji_compl_loc_id(v_idx)
9466                                              := l_cur_compl_locator;
9467                                     else
9468                                         if l_mtl_locator_type = 2 then
9469                                             l_aux_mesg := '';
9470                                             handle_invalid_field(
9471                                                 'COMPLETION LOCATOR',
9472                                                 l_aux_mesg, l_stmt_num);
9473                                             l_error_code := -1;
9474                                             l_error_count := l_error_count + 1;
9475                                             GOTO skip_resched_validations;
9476                                         end if;
9477                                     end if;
9478                                 END IF;
9479 
9480 l_stmt_num := 590.5;
9481                                 SELECT  nvl(msub.locator_type, 1) sub_loc_control,
9482                                         MP.stock_locator_control_code org_loc_control,
9483                                         MS.restrict_locators_code,
9484                                         MS.location_control_code item_loc_control
9485                                 into    l_sub_loc_control,
9486                                         l_org_loc_control,
9487                                         l_restrict_locators_code,
9488                                         l_item_loc_control
9489                                 FROM    mtl_system_items MS,
9490                                         mtl_secondary_inventories MSUB,
9491                                         mtl_parameters MP
9492                                 WHERE   MP.organization_id = v_wlji_org_id(v_idx)
9493                                 AND     MS.organization_id = v_wlji_org_id(v_idx)
9494                                 AND     MS.inventory_item_id = v_wlji_item_id(v_idx)
9495                                 AND     MSUB.secondary_inventory_name
9496                                             = v_wlji_compl_subinv(v_idx)
9497                                 AND     MSUB.organization_id = v_wlji_org_id(v_idx);
9498 
9499 l_stmt_num := 590.6;
9500                                 l_locator_id := v_wlji_compl_loc_id(v_idx);
9501                                 WIP_LOCATOR.validate(
9502                                         v_wlji_org_id(v_idx),
9503                                         v_wlji_item_id(v_idx),
9504                                         v_wlji_compl_subinv(v_idx),
9505                                         l_org_loc_control,
9506                                         l_sub_loc_control,
9507                                         l_item_loc_control,
9508                                         l_restrict_locators_code,
9509                                         NULL, NULL, NULL, NULL,
9510                                         l_locator_id,
9511                                         dummy_segs,
9512                                         l_temp_boolean);
9513                                 IF not l_temp_boolean THEN
9514                                     l_aux_mesg := '';
9515                                     handle_invalid_field(
9516                                         'COMPLETION LOCATOR', l_aux_mesg, l_stmt_num);
9517                                     l_error_code := -1;
9518                                     l_error_count := l_error_count + 1;
9519                                     GOTO skip_resched_validations;
9520                                 end if;
9521 
9522 l_stmt_num := 590.7;
9523                             -- Validation added for Bug 4919218: End
9524                             else
9525                                 if lbji_debug = 'Y' then
9526                                     fnd_file.put_line(fnd_file.log,
9527                                         'Update BOM/Routing alternates for standard job..');
9528                                 end if;
9529                             end if;
9530 
9531                         end if;     -- }} if std-job
9532                         ---------------------------------
9533                         -- }}} End: Update of alternates
9534                         ---------------------------------
9535 
9536                         --------------------------------------------
9537                         -- {{{ Begin: Update of references (non-std)
9538                         --------------------------------------------
9539                         -- Non standard job bom/routing reference update.
9540                         -- The assumption is that this module will NOT update the
9541                         -- qty/dates/etc.,they will be taken care in the later modules.
9542                         -- This module will just look at the reference info, and assume
9543                         -- qty/date/etc to be same as the old values.
9544                         -- Alt routing/bom will be taken care of here
9545                         if l_cur_job_type = 3 then      --  {{ if l_cur_job_type = 3
9546 l_stmt_num:= 600;
9547                             l_chg_bom_ref  := 0;
9548                             l_chg_rtg_ref  := 0;
9549                             l_chg_ref_flag := 0;
9550 
9551                             if v_wlji_rtg_ref_id(v_idx) is not null then
9552                                 l_chg_rtg_ref := 1;
9553                             end if;
9554 
9555                             if v_wlji_bom_ref_id(v_idx) is not null then
9556                                 l_chg_bom_ref := 1;
9557                             end if;
9558 
9559 
9560                             -- {{ only allow change for unreleased job, l_cur_status_type = 1
9561                             if (l_chg_bom_ref = 1 or l_chg_rtg_ref = 1) and
9562                                l_cur_status_type = 1
9563                             then
9564                                 l_chg_ref_flag := 1;
9565 l_stmt_num:= 610;
9566                                 if l_chg_bom_ref = 1 then
9567                                     wsmputil.validate_non_std_references(
9568                                         p_assembly_item_id       => null,
9569                                         p_routing_reference_id   => null,
9570                                         p_bom_reference_id       => v_wlji_bom_ref_id(v_idx),
9571                                         p_alt_routing_designator => null,
9572                                         p_alt_bom_designator     => v_wlji_alt_bom(v_idx),
9573                                         p_organization_id        => v_wlji_org_id(v_idx),
9574                                         p_start_date             => sysdate, -- not used after all
9575                                         p_end_date               => null,
9576                                         p_start_quantity         => null,
9577                                         p_mrp_net_quantity       => dummy_number,
9578                                         p_class_code             => null,
9579                                         p_completion_subinventory=> null,
9580                                         p_completion_locator_id  => null,
9581                                         p_firm_planned_flag      => dummy_number,
9582                                         p_bom_revision           => v_wlji_bom_revision(v_idx),
9583                                         p_bom_revision_date      => v_wlji_bom_rev_date(v_idx),
9584                                         p_routing_revision       => dummy_varchar,
9585                                         p_routing_revision_date  => dummy_date,
9586                                         x_routing_seq_id         => dummy_number,
9587                                         x_bom_seq_id             => l_common_bom_seq_id,
9588                                         validation_level         => 1,
9589                                         x_error_code             => l_error_code,
9590                                         x_err_msg                => l_error_msg);
9591 
9592                                     if l_error_code <> 0 then
9593                                         handle_error( l_error_code, l_error_msg, l_stmt_num);
9594                                         l_error_code := -1;
9595                                         GOTO skip_resched_validations;
9596                                     end if;
9597                                 end if;
9598 l_stmt_num:= 620;
9599                                 if l_chg_rtg_ref = 1 then
9600                                     wsmputil.validate_non_std_references(
9601                                         p_assembly_item_id       => null,
9602                                         p_routing_reference_id   => v_wlji_rtg_ref_id(v_idx),
9603                                         p_bom_reference_id       => null,
9604                                         p_alt_routing_designator => v_wlji_alt_rtg(v_idx),
9605                                         p_alt_bom_designator     => null,
9606                                         p_organization_id        => v_wlji_org_id(v_idx),
9607                                         p_start_date             => sysdate, -- not used after all
9608                                         p_end_date               => null,
9609                                         p_start_quantity         => null,
9610                                         p_mrp_net_quantity       => dummy_number,
9611                                         p_class_code             => null,
9612                                         p_completion_subinventory=> null,
9613                                         p_completion_locator_id  => null,
9614                                         p_firm_planned_flag      => dummy_number,
9615                                         p_bom_revision           => dummy_varchar,
9616                                         p_bom_revision_date      => dummy_date,
9617                                         p_routing_revision       => v_wlji_rtg_revision(v_idx),
9618                                         p_routing_revision_date  => v_wlji_rtg_rev_date(v_idx),
9619                                         x_routing_seq_id         => l_rtg_seq_id,
9620                                         x_bom_seq_id             => dummy_number,
9621                                         validation_level         => 2,
9622                                         x_error_code             => l_error_code,
9623                                         x_err_msg                => l_error_msg);
9624                                     if l_error_code <> 0 then
9625                                         handle_error( l_error_code, l_error_msg, l_stmt_num);
9626                                         l_error_code := -1;
9627                                         GOTO skip_resched_validations;
9628                                     end if;
9629 l_stmt_num:= 630;
9630                                     -- find the common rtg seq id
9631                                     l_error_msg := NULL;
9632                                     wsmputil.find_common_routing(
9633                                         p_routing_sequence_id        => l_rtg_seq_id,
9634                                         p_common_routing_sequence_id => l_common_rtg_seq_id,
9635                                         x_err_code                   => l_error_code,
9636                                         x_err_msg                    => l_error_msg);
9637 
9638                                      if l_error_code <> 0 OR l_error_msg IS NOT NULL then
9639                                         handle_error( l_error_code, l_error_msg, l_stmt_num);
9640                                         l_error_code := -1;
9641                                         GOTO skip_resched_validations;
9642                                     end if;
9643                                 end if;
9644 
9645                                 if l_chg_rtg_ref = 1 and l_chg_bom_ref = 0 then
9646                                     l_common_bom_seq_id        := l_cur_com_bom_seq_id;
9647                                     --v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date; -- DEL bug3202534
9648                                     --v_wlji_bom_revision(v_idx) := l_cur_bom_revision; -- DEL bug3202534
9649                                     v_wlji_bom_ref_id(v_idx)   := l_cur_bom_ref_id;
9650                                     v_wlji_alt_bom(v_idx)      := l_cur_alt_bom;
9651                                 end if;
9652 
9653                                 if l_chg_rtg_ref = 0 AND l_chg_bom_ref = 1 then
9654                                     l_common_rtg_seq_id        := l_cur_com_rtg_seq_id;
9655                                     --v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date; -- DEL bug3202534
9656                                     --v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision; -- DEL bug3202534
9657                                     v_wlji_rtg_ref_id(v_idx)   := l_cur_rtg_ref_id;
9658                                     v_wlji_alt_rtg(v_idx)      := l_cur_alt_rtg;
9659                                 end if;
9660 
9661                             end if; -- }} old_status_type = 1
9662 
9663                             -- user can update the bom_reference and the routing reference of a
9664                             -- non-standard job only if the job is unreleased.
9665                             if (l_chg_bom_ref = 1 or l_chg_rtg_ref = 1) and
9666                                 l_cur_status_type <> 1
9667                             then
9668                                 fnd_file.put_line(fnd_file.log,
9669                                     'Ignoring the following fields if provided by user: ' ||
9670                                     'Routing_Reference_Id, Bom_Reference_Id, ' ||
9671                                     'Alternate_Bom_Designator, Alternate_Routing_Designator');
9672                             end if;
9673 
9674                             if l_chg_ref_flag = 0 then
9675                                 l_common_bom_seq_id        := l_cur_com_bom_seq_id;
9676                                 --v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date; -- DEL bug3202534
9677                                 --v_wlji_bom_revision(v_idx) := l_cur_bom_revision; -- DEL bug3202534
9678                                 v_wlji_bom_ref_id(v_idx)   := l_cur_bom_ref_id;
9679                                 v_wlji_alt_bom(v_idx)      := l_cur_alt_bom;
9680                                 l_common_rtg_seq_id        := l_cur_com_rtg_seq_id;
9681                                 --v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date; -- DEL bug3202534
9682                                 --v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision; -- DEL bug3202534
9683                                 v_wlji_rtg_ref_id(v_idx)   := l_cur_rtg_ref_id;
9684                                 v_wlji_alt_rtg(v_idx)      := l_cur_alt_rtg;
9685                             else
9686                                 if lbji_debug = 'Y' then
9687                                     fnd_file.put_line(fnd_file.log,
9688                                         'Update BOM/Routing references for non-standard job..');
9689                                 end if;
9690                             end if;
9691 
9692                         end if; -- }} job type = 3
9693                         ---------------------------------
9694                         -- }}} End: Update of references
9695                         ---------------------------------
9696 
9697                         --------------------------------------------------
9698                         -- {{{ Begin: Update of revision and revision_date
9699                         --------------------------------------------------
9700                         -- BA: bug3202534
9701                         begin
9702 
9703                             l_chg_rtg_rev  := 0;
9704                             l_chg_bom_rev  := 0;
9705                             l_chg_rev_flag := 0;
9706 
9707 l_stmt_num:= 640;
9708                             if (v_wlji_rtg_revision(v_idx) is not null and
9709                                 v_wlji_rtg_revision(v_idx) <> l_cur_rtg_revision) or
9710                                (v_wlji_rtg_rev_date(v_idx) is not null and
9711                                 v_wlji_rtg_rev_date(v_idx) <> l_cur_rtg_rev_date)
9712                             then
9713                                 l_chg_rtg_rev := 1;
9714                             end if;
9715 
9716                             if (v_wlji_bom_revision(v_idx) is not null and
9717                                 v_wlji_bom_revision(v_idx) <> l_cur_bom_revision) or
9718                                (v_wlji_bom_rev_date(v_idx) is not null and
9719                                 v_wlji_bom_rev_date(v_idx) <> l_cur_bom_rev_date)
9720                             then
9721                                 l_chg_bom_rev := 1;
9722                             end if;
9723 
9724                             -- {{ only allow change for unreleased job, l_cur_status_type = 1
9725                             if (l_chg_bom_rev = 1 or l_chg_rtg_rev = 1) and
9726                                l_cur_status_type = 1
9727                             then
9728                                 l_chg_rev_flag := 1;
9729 l_stmt_num:= 650;
9730                                 if v_wlji_fusd(v_idx) is null then
9731                                     v_wlji_fusd(v_idx) := l_cur_start_date;
9732                                 end if;
9733 
9734                                 if v_wlji_fusd(v_idx) > SYSDATE then
9735                                     l_rev_date := v_wlji_fusd(v_idx);
9736                                 else
9737                                     l_rev_date := SYSDATE;
9738                                 end if;
9739 
9740                                 if l_chg_bom_rev = 1 then
9741                                     -- validate bom rev/rev_date
9742                                     if l_cur_job_type = 1 then -- standard job
9743 l_stmt_num:= 660.1;
9744                                         wip_revisions.bom_revision (
9745                                                 v_wlji_org_id(v_idx),
9746                                                 v_wlji_item_id(v_idx),
9747                                                 v_wlji_bom_revision(v_idx),
9748                                                 v_wlji_bom_rev_date(v_idx),
9749                                                 l_rev_date);
9750                                     else
9751                                         if(v_wlji_bom_ref_id(v_idx) IS NOT NULL) then
9752 l_stmt_num:= 660.2;
9753                                             wip_revisions.bom_revision (
9754                                                     v_wlji_org_id(v_idx),
9755                                                     v_wlji_bom_ref_id(v_idx),
9756                                                     v_wlji_bom_revision(v_idx),
9757                                                     v_wlji_bom_rev_date(v_idx),
9758                                                     l_rev_date);
9759                                         end if;
9760                                     end if;
9761                                 end if;
9762 
9763                                 if l_chg_rtg_rev = 1 then
9764                                     -- validate rtg rev/rev_date
9765                                     if l_cur_job_type = 1 then -- standard job
9766 l_stmt_num:= 670.1;
9767                                         wip_revisions.routing_revision (
9768                                                 v_wlji_org_id(v_idx),
9769                                                 v_wlji_item_id(v_idx),
9770                                                 v_wlji_rtg_revision(v_idx),
9771                                                 v_wlji_rtg_rev_date(v_idx),
9772                                                 l_rev_date);
9773                                     else
9774                                         if(v_wlji_rtg_ref_id(v_idx) IS NOT NULL) then
9775 l_stmt_num:= 670.2;
9776                                             wip_revisions.routing_revision (
9777                                                     v_wlji_org_id(v_idx),
9778                                                     v_wlji_rtg_ref_id(v_idx),
9779                                                     v_wlji_rtg_revision(v_idx),
9780                                                     v_wlji_rtg_rev_date(v_idx),
9781                                                     l_rev_date);
9782                                         end if;
9783                                     end if;
9784                                 end if;
9785 
9786                                 if l_chg_rtg_rev = 1 and l_chg_bom_rev = 0 then
9787                                     v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date;
9788                                     v_wlji_bom_revision(v_idx) := l_cur_bom_revision;
9789                                 end if;
9790 
9791                                 if l_chg_rtg_rev = 0 and l_chg_bom_rev = 1 then
9792                                     v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date;
9793                                     v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision;
9794                                 end if;
9795 
9796                             end if; -- }} old_status_type = 1
9797 
9798                             -- user can update the revision and revision date of a
9799                             -- job only if the job is unreleased.
9800                             if (l_chg_rtg_rev = 1 or l_chg_bom_rev = 1) and
9801                                 l_cur_status_type <> 1
9802                             then
9803                                 fnd_file.put_line(fnd_file.log,
9804                                     'Ignoring the following fields if provided by user: ' ||
9805                                     'Routing_Revision, Routing_Revision_Date, '||
9806                                     'Bom_Revision, Bom_Revision_Date, ' );
9807                             end if;
9808 
9809                             if l_chg_rev_flag = 0 then
9810                                 v_wlji_bom_rev_date(v_idx) := l_cur_bom_rev_date;
9811                                 v_wlji_bom_revision(v_idx) := l_cur_bom_revision;
9812                                 v_wlji_rtg_rev_date(v_idx) := l_cur_rtg_rev_date;
9813                                 v_wlji_rtg_revision(v_idx) := l_cur_rtg_revision;
9814                             else
9815                                 if lbji_debug = 'Y' then
9816                                     fnd_file.put_line(fnd_file.log,
9817                                         'Update BOM/Routing revision or revision date ...');
9818                                 end if;
9819                             end if;
9820                         end;
9821                         -- EA: bug3202534
9822                         ------------------------------------------------
9823                         -- }}} End: Update of revision and revision_date
9824                         ------------------------------------------------
9825 
9826 
9827 l_stmt_num := 700;
9828                         -- Validation before updating other columns
9829                         -- if the user wants to update only the reference, the wo/wro/wor/woy
9830                         -- updates are not needed
9831                         if (l_cur_job_type = 3) AND
9832                            (
9833                             ((v_wlji_firm_planned_flag(v_idx) is null) or
9834                              (v_wlji_firm_planned_flag(v_idx) is not null and
9835                               v_wlji_firm_planned_flag(v_idx)=l_cur_firm_planned_flag))
9836                             and
9837                             ((v_wlji_status_type(v_idx) is null) or
9838                              (v_wlji_status_type(v_idx) is not null and
9839                               v_wlji_status_type(v_idx)=l_cur_status_type))
9840                             and
9841                             ((v_wlji_date_released(v_idx) is null) or
9842                              (v_wlji_date_released(v_idx) is not null and
9843                               v_wlji_date_released(v_idx)=l_cur_date_released))
9844                             and
9845                             ((v_wlji_start_quantity(v_idx) is null) or
9846                              (v_wlji_start_quantity(v_idx) is not null and
9847                               v_wlji_start_quantity(v_idx)=l_cur_start_qty))
9848                             and
9849                             ((v_wlji_net_quantity(v_idx) is null) or
9850                              (v_wlji_net_quantity(v_idx) is not null and
9851                               v_wlji_net_quantity(v_idx)=l_cur_net_qty))
9852                             and
9853                             ((v_wlji_lucd(v_idx) is null) or
9854                              (v_wlji_lucd(v_idx) is not null and
9855                               v_wlji_lucd(v_idx)=l_cur_schd_compl_date))
9856                             and
9857                             ((v_wlji_fusd(v_idx) is null) or
9858                              (v_wlji_fusd(v_idx) is not null and
9859                               v_wlji_fusd(v_idx)=l_cur_schd_start_date))
9860                             and
9861                             ((v_wlji_coproducts_supply(v_idx) is null) or
9862                              (v_wlji_coproducts_supply(v_idx) is not null and
9863                               v_wlji_coproducts_supply(v_idx)=l_cur_coprod_supply))
9864                            )
9865                         then
9866                             l_skip_update_wo:= 1;
9867                         end if;
9868                         if lbji_debug = 'Y' then
9869                             if l_skip_update_wo = 1 then
9870                                 fnd_file.put_line(fnd_file.log,
9871                                     'Planning to skip update of wo, wor, wro');
9872                             else
9873                                 fnd_file.put_line(fnd_file.log,
9874                                     'Planning to update wo, wor, wro');
9875                             end if;
9876                         end if;
9877 
9878 
9879 l_stmt_num := 710;
9880                         -- if the old firm_planned flag was 1, and the user populated null
9881                         -- while updating the job, NO changes in quantity and date allowed.
9882                         -- If the user changed the flag, this change is considered BEFORE
9883                         -- making a decision whether to allow update of qnty/date or not.
9884                         if v_wlji_firm_planned_flag(v_idx) is NULL then
9885                             v_wlji_firm_planned_flag(v_idx) := l_cur_firm_planned_flag;
9886                         end if;
9887 
9888                         -- for non-std jobs, the firm flag is always 2. User cannot update it to 1.
9889                         if l_cur_job_type = 3 then
9890                             if v_wlji_firm_planned_flag(v_idx) is not null and
9891                                v_wlji_firm_planned_flag(v_idx) <> 2 then
9892                                 l_aux_mesg := '';
9893                                 handle_invalid_field('Firm_Planned_Flag', l_aux_mesg, l_stmt_num);
9894                                 l_error_code := -1;
9895                                 GOTO skip_resched_validations;
9896                             end if;
9897                         end if;
9898 
9899 l_stmt_num := 720;
9900                         -- {{ Validate status
9901                         IF v_wlji_status_type(v_idx) is null THEN
9902                             v_wlji_status_type(v_idx) := l_cur_status_type;
9903                             -- update of released date only is not allowed
9904                             v_wlji_date_released(v_idx) := l_cur_date_released;
9905                         ELSIF v_wlji_status_type(v_idx) = l_cur_status_type THEN
9906                             -- update of released date only is not allowed
9907                             v_wlji_date_released(v_idx) := l_cur_date_released;
9908                         END IF;
9909 
9910                         -- status type should be one of RELEASED, HOLD, CANCELLED, UNRELEASED
9911                         if v_wlji_status_type(v_idx) not in (1,3,6,7) then
9912                             l_aux_mesg := '';
9913                             handle_invalid_field('Status Type', l_aux_mesg, l_stmt_num);
9914                             l_error_code := -1;
9915                             GOTO skip_resched_validations;
9916                         else  -- {{ valid status type
9917                             if l_cur_status_type = 7   then     -- cancelled
9918                                 l_error_code := -1;
9919                                 handle_errmsg_name(
9920                                     'WSM','WSM_RESCHEDULE_CANCEL_JOB',l_stmt_num);
9921                                 GOTO skip_resched_validations;
9922                             end if;
9923 l_stmt_num := 720.1;
9924                             -- if the job is being released from an unreleased state...
9925                             if l_cur_date_released is null and
9926                                v_wlji_status_type(v_idx) = 3
9927                             then
9928                                 if (v_wlji_date_released(v_idx) is null ) then
9929                                     v_wlji_date_released(v_idx) := sysdate;
9930                                 end if;
9931                                 if (v_wlji_date_released(v_idx) > sysdate ) then
9932                                     l_error_code := -1;
9933                                     handle_errmsg_name(
9934                                         'WIP','WIP_INVALID_RELEASE_DATE',l_stmt_num);
9935                                     GOTO skip_resched_validations;
9936                                 end if;
9937 
9938                                 -- BEGIN: BUG3126650
9939                                 l_temp_num := WSMPUTIL.GET_INV_ACCT_PERIOD(
9940                                                 x_err_code         => l_error_code,
9941                                                 x_err_msg          => l_error_msg,
9942                                                 p_organization_id  => v_wlji_org_id(v_idx),
9943                                                 p_date             => trunc(nvl(v_wlji_date_released(v_idx),sysdate)));
9944                                 IF (l_error_code <> 0) THEN
9945                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
9946                                     GOTO skip_resched_validations;
9947                                 END IF;
9948                                 -- END: BUG3126650
9949 
9950                             elsif l_cur_date_released is not null and
9951                                 v_wlji_status_type(v_idx) = 1
9952                             then
9953                                 v_wlji_date_released(v_idx) := null;
9954                             else
9955                                 -- ignore release date populated by user otherwise
9956                                 v_wlji_date_released(v_idx) := l_cur_date_released;
9957                             end if;
9958 
9959                             if ((l_cur_status_type = 3 and v_wlji_status_type(v_idx) = 1) OR
9960                                 (l_cur_status_type = 6 and v_wlji_status_type(v_idx) = 1))
9961                             then
9962                                 if (g_charges_exist_all) then
9963                                     l_error_code := -1;
9964                                     handle_errmsg_name(
9965                                         'WIP','WIP_UNRLS_JOB/SCHED',l_stmt_num);
9966                                     GOTO skip_resched_validations;
9967 
9968                                 else
9969 l_stmt_num := 720.2;
9970                                     -- osp begin
9971                                     if wip_osp.po_req_exists (
9972                                             v_wlji_wip_entity_id(v_idx),
9973                                             null,
9974                                             v_wlji_org_id(v_idx),
9975                                             null, 5)
9976                                     then
9977                                         fnd_message.set_name('WSM', 'WSM_JOB_PURCHASE_REQ');
9978                                         l_error_msg := fnd_message.get;
9979                                         l_warning_count := l_warning_count + 1;
9980                                         handle_warning(
9981                                             p_err_msg    => l_error_msg,
9982                                             p_header_id  => v_wlji_header_id(v_idx),
9983                                             p_request_id => v_wlji_request_id(v_idx),
9984                                             p_program_id => v_wlji_program_id(v_idx),
9985                                             p_program_application_id => v_wlji_program_appl_id(v_idx));
9986                                     end if;
9987                                     -- osp end
9988                                 end if;
9989                             end if;
9990 l_stmt_num := 720.3;
9991                             if (l_cur_status_type IN (1,6)) and
9992                                (v_wlji_status_type(v_idx) = 3) and
9993                                 wsmputil.WSM_ESA_ENABLED(
9994                                             p_wip_entity_id => v_wlji_wip_entity_id(v_idx),
9995                                             err_code        => l_error_code,
9996                                             err_msg         => l_error_msg,
9997                                             p_org_id        => '',
9998                                             p_job_type      => '') = 1
9999                             then
10000 
10001                                 if v_wlji_class_code(v_idx) is null then
10002                                     l_temp_class := l_cur_class_code;
10003                                 else
10004                                     l_temp_class := v_wlji_class_code(v_idx);
10005                                 end if;
10006 l_stmt_num := 720.4;
10007                                 select est_scrap_account,
10008                                        est_scrap_var_account
10009                                 into   l_est_scrap_account,
10010                                        l_est_scrap_var_account
10011                                 from   wip_accounting_classes
10012                                 where  class_code = l_temp_class
10013                                 and    organization_id = v_wlji_org_id(v_idx);
10014 
10015                                 if l_est_scrap_account is null or
10016                                    l_est_scrap_var_account is null
10017                                 then
10018                                     fnd_message.set_name('WSM','WSM_NO_WAC_SCRAP_ACC');
10019                                     fnd_message.set_token('CC', l_temp_class);
10020                                     l_error_msg := fnd_message.get;
10021                                     l_error_code := -1;
10022                                     handle_error(l_error_code, l_error_msg,  l_stmt_num);
10023                                     GOTO skip_resched_validations;
10024                                 end if;
10025                                     if l_est_scrap_account is null or
10026                                     l_est_scrap_var_account is null
10027                                 then
10028                                     fnd_message.set_name('WSM','WSM_NO_WAC_SCRAP_ACC');
10029                                     fnd_message.set_token('CC', l_temp_class);
10030                                     l_error_msg := fnd_message.get;
10031                                     l_error_code := -1;
10032                                     handle_error(l_error_code, l_error_msg,  l_stmt_num);
10033                                     GOTO skip_resched_validations;
10034                                 end if;
10035                             end if;
10036                         end if;  -- }} valid status type
10037                         if lbji_debug = 'Y' then
10038                             fnd_file.put_line(fnd_file.log,
10039                                     'Validate reschedule Job_Status .. OK');
10040                         end if;
10041                         -- }} Validate status
10042 
10043 l_stmt_num := 730;
10044                         -- {{ Validate quantity
10045                         IF v_wlji_start_quantity(v_idx) IS NULL THEN
10046                             v_wlji_start_quantity(v_idx) := l_cur_start_qty;
10047                         ELSIF  v_wlji_start_quantity(v_idx) = l_cur_start_qty THEN
10048                             NULL;
10049                         -- Bug 5661656: Allow Job to be rescheduled(Update Qty/Dates) when firm_planned_flag is also updated.
10050                         -- ELSIF v_wlji_firm_planned_flag(v_idx) = 1 THEN
10051                         ELSIF v_wlji_firm_planned_flag(v_idx) = 1 AND NVL(l_cur_firm_planned_flag, 2) = 1 THEN
10052                             l_error_code := -1;
10053                             handle_errmsg_name('WSM', 'WSM_JOB_FIRM', l_stmt_num);
10054                             GOTO skip_resched_validations;
10055                         ELSE
10056                             begin
10057                                 if (l_cur_status_type IN (3,6) AND
10058                                     v_wlji_status_type(v_idx) IN (1,3,6)) then
10059                                     if (g_charges_exist_all) then
10060                                         raise e_invalid_qty;
10061                                     end if;
10062                                 end if;
10063                             exception
10064                                 when e_invalid_qty then
10065                                     l_error_code := -1;
10066                                     handle_errmsg_name('WSM', 'WSM_QNTY_NOCHANGE', l_stmt_num);
10067                                     GOTO skip_resched_validations;
10068                             end;
10069                         END IF;
10070                         if lbji_debug = 'Y' then
10071                             fnd_file.put_line(fnd_file.log,
10072                                     'Validate reschedule Start_Quantity .. OK');
10073                         end if;
10074                         -- }} Validate quantity
10075 
10076 l_stmt_num := 740;
10077                         -- {{ osp begin
10078                         -- create requisitions/additional reuisitions under following conditions:
10079                         -- 1. user updates only status from unreleased to released, quantity unchanged
10080                         -- 2. user updates only quantity (increases) for a released job, status is unchanged
10081                         -- 3. user updates staus to released, and increases quantity.
10082 
10083                         if WSMPLCVA.v_org(v_wlji_org_id(v_idx)).PO_CREATION_TIME
10084                            <> WIP_CONSTANTS.MANUAL_CREATION
10085                         then
10086                             if (v_wlji_status_type(v_idx) = 3 and l_cur_status_type = 1) then
10087                                 if wsmputil.check_osp_operation(
10088                                         v_wlji_wip_entity_id(v_idx),
10089                                         l_osp_op_seq_num,
10090                                         v_wlji_org_id(v_idx))
10091                                 then
10092                                     g_num_of_osp_exists := g_num_of_osp_exists + 1;
10093                                     wip_osp.create_requisition(
10094                                         p_wip_entity_id          => v_wlji_wip_entity_id(v_idx),
10095                                         p_organization_id        => v_wlji_org_id(v_idx),
10096                                         p_repetitive_schedule_id => null,
10097                                         p_operation_seq_num      => l_osp_op_seq_num,
10098                                         p_resource_seq_num       => null,
10099                                         p_run_reqimport          => wip_constants.no);
10100                                 end if; -- check_osp_operation
10101                             end if;
10102 l_stmt_num := 740.1;
10103                             if (v_wlji_start_quantity(v_idx) > l_cur_start_qty) AND
10104                                v_wlji_status_type(v_idx) = 3
10105                             then
10106                                 wip_osp.create_additional_req(
10107                                     p_wip_entity_id          => v_wlji_wip_entity_id(v_idx),
10108                                     p_organization_id        => v_wlji_org_id(v_idx),
10109                                     p_repetitive_schedule_id => null,
10110                                     p_added_quantity         => (v_wlji_start_quantity(v_idx) - l_cur_start_qty),
10111                                     p_op_seq                 => null);
10112                             end if;
10113                         end if; -- WIP_CONSTANTS.MANUAL_CREATION
10114                         -- }} osp end
10115 
10116 l_stmt_num := 750;
10117                         -- {{ Validate net-quantity
10118                         if v_wlji_net_quantity(v_idx) < 0 or
10119                                 v_wlji_net_quantity(v_idx) > v_wlji_start_quantity(v_idx) then
10120                                 l_aux_mesg := '';
10121                                 handle_invalid_field('Net Quantity', l_aux_mesg, l_stmt_num);
10122                                 l_error_code := -1;
10123                                 GOTO skip_resched_validations;
10124                         end if;
10125 
10126                         if l_cur_job_type = 1 then
10127                             if (v_wlji_net_quantity(v_idx) is not null) and
10128                                (v_wlji_net_quantity(v_idx) <> l_cur_start_qty) and
10129                                -- Bug 5661656: Allow Job to be rescheduled(Update Qty/Dates) when firm_planned_flag is also updated.
10130                                -- (v_wlji_firm_planned_flag(v_idx) = 1) then
10131                                (v_wlji_firm_planned_flag(v_idx) = 1 AND NVL(l_cur_firm_planned_flag, 2) = 1) then
10132                                 l_error_code := -1;
10133                                 handle_errmsg_name('WSM', 'WSM_JOB_FIRM', l_stmt_num);
10134                                 GOTO skip_resched_validations;
10135                             end if;
10136 
10137                             if (v_wlji_net_quantity(v_idx) is not null) and
10138                                (v_wlji_start_quantity(v_idx) is null) and
10139                                (v_wlji_net_quantity(v_idx) > l_cur_start_qty) then
10140                                 l_aux_mesg := '';
10141                                 handle_invalid_field('Net Quantity', l_aux_mesg, l_stmt_num);
10142                                 l_error_code := -1;
10143                                 GOTO skip_resched_validations;
10144                             end if;
10145 l_stmt_num := 750.1;
10146                             if v_wlji_net_quantity(v_idx) is null then
10147                                 begin
10148                                     select decode(wdj.primary_item_id, null, 0,
10149                                                   decode(wdj.net_quantity,
10150                                                          wdj.start_quantity,
10151                                                          v_wlji_start_quantity(v_idx),
10152                                                          least(wdj.net_quantity,
10153                                                                nvl(v_wlji_start_quantity(v_idx),
10154                                                                    wdj.net_quantity))))
10155                                     into   v_wlji_net_quantity(v_idx)
10156                                     from   wip_discrete_jobs wdj
10157                                     where  wdj.wip_entity_id = v_wlji_wip_entity_id(v_idx)
10158                                     and    wdj.organization_id = v_wlji_org_id(v_idx);
10159                                 exception
10160                                     when others then
10161                                         l_error_code := SQLCODE;
10162                                         l_error_msg  :=
10163                                             'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
10164                                            ||SUBSTR(SQLERRM, 1,1000);
10165                                         handle_error(l_error_code, l_error_msg, l_stmt_num);
10166                                         GOTO skip_resched_validations;
10167                                 end;
10168                             end if;
10169                         end if; -- job_type
10170                         if lbji_debug = 'Y' then
10171                             fnd_file.put_line(fnd_file.log,
10172                                     'Validate reschedule Net_Quantity .. OK');
10173                         end if;
10174                         -- }} Validate net-quantity
10175 
10176 l_stmt_num := 750.2;
10177             -- ST : Serial Support Project ----------------------
10178             --- Validate the start job qty and net qty ----------
10179             --- For serial controlled item have to be an integer...
10180             DECLARE
10181                 l_serial_ctrl_code NUMBER;
10182 
10183             BEGIN
10184                 select  msi.serial_number_control_code
10185                                 into    l_serial_ctrl_code
10186                                 from    mtl_system_items_kfv msi,wip_discrete_jobs wdj
10187                                 where   msi.inventory_item_id = wdj.primary_item_id
10188                                 and     msi.organization_id   = v_wlji_org_id(v_idx)
10189                 and     wdj.wip_entity_id = v_wlji_wip_entity_id(v_idx)
10190                 and     wdj.organization_id = v_wlji_org_id(v_idx);
10191 
10192                 IF l_serial_ctrl_code = 2 THEN  -- Predefined serial control...
10193 
10194                     IF (v_wlji_start_quantity(v_idx) IS NOT NULL) AND
10195                        (floor(v_wlji_start_quantity(v_idx)) <> v_wlji_start_quantity(v_idx))
10196                     THEN
10197                         -- error out...
10198                         -- Start qty cannot be non-integer...
10199                          fnd_message.set_name('WSM','WSM_INVALID_JOB_TXN_QTY');
10200                          l_aux_mesg := fnd_message.get;
10201                          handle_invalid_field('START QUANTITY', l_aux_mesg, l_stmt_num);
10202                          l_error_code := -1;
10203                          l_error_count := l_error_count + 1;
10204                          GOTO skip_resched_validations;
10205                     END IF;
10206 
10207                     IF (v_wlji_net_quantity(v_idx) IS NOT NULL) and
10208                        floor(v_wlji_net_quantity(v_idx)) <> v_wlji_net_quantity(v_idx)
10209                     THEN
10210                         -- error out...
10211                         -- Start qty cannot be non-integer...
10212                          fnd_message.set_name('WSM','WSM_INVALID_JOB_TXN_QTY');
10213                          l_aux_mesg := fnd_message.get;
10214                          handle_invalid_field('NET QUANTITY', l_aux_mesg, l_stmt_num);
10215                          l_error_code := -1;
10216                          l_error_count := l_error_count + 1;
10217                          GOTO skip_resched_validations;
10218                     END IF;
10219                 END IF;
10220                END;
10221                -- ST : Serial Support Project ----------------------
10222                --- Validate the start job qty and net qty ----------
10223 
10224 l_stmt_num := 760;
10225                         -- {{ Validate dates (for update)
10226                         -- Bug 5661656: Allow Job to be rescheduled(Update Qty/Dates) when firm_planned_flag is also updated.
10227                         -- if ((v_wlji_firm_planned_flag(v_idx) = 1) AND
10228                         if ((v_wlji_firm_planned_flag(v_idx) = 1 AND NVL(l_cur_firm_planned_flag, 2) = 1) AND
10229                             (((v_wlji_fusd(v_idx) IS NOT NULL) AND
10230                               (v_wlji_fusd(v_idx) <> l_cur_start_date))
10231                              OR
10232                              ((v_wlji_lucd(v_idx) IS NOT NULL) AND
10233                               (v_wlji_lucd(v_idx) <> l_cur_complete_date))))
10234                         then
10235                             l_error_code := -1;
10236                              handle_errmsg_name('WSM', 'WSM_JOB_FIRM', l_stmt_num);
10237                             GOTO skip_resched_validations;
10238                         end if;
10239 
10240                         IF ( ((v_wlji_fusd(v_idx) IS NULL) AND
10241                               (v_wlji_lucd(v_idx) IS NULL))
10242                              OR
10243                              ((v_wlji_fusd(v_idx) IS NOT NULL) AND
10244                               (v_wlji_lucd(v_idx) IS NOT NULL) AND
10245                               (v_wlji_scheduling_method(v_idx) <> 1)) )
10246                         THEN
10247                             v_wlji_scheduling_method(v_idx) := 3;
10248                         END IF;
10249 
10250                         l_error_msg := NULL;
10251                         IF v_wlji_scheduling_method(v_idx) = 3 THEN
10252 
10253                             if (v_wlji_fusd(v_idx) is null) then
10254                                 v_wlji_fusd(v_idx) := l_cur_start_date;
10255                             end if;
10256                             if (v_wlji_lucd(v_idx) is null)  then
10257                                 v_wlji_lucd(v_idx) := l_cur_complete_date;
10258                             end if;
10259 
10260                             if ((v_wlji_fusd(v_idx) <> l_cur_start_date) and
10261                                 g_charges_exist_shopfloor)
10262                             then
10263                                 -- bugfix 3284761, in option c, we will ignore v_wlji_fusd
10264                                 -- if it is different form l_cur_start_date and charge exists
10265                                 v_wlji_fusd(v_idx) := l_cur_start_date;
10266                             end if;
10267 
10268                             if  (NOT(g_charges_exist_shopfloor) and -- added bug 3423195
10269                                  v_wlji_fusd(v_idx) > v_wlji_lucd(v_idx)) then
10270                                 l_aux_mesg := '';
10271                                 handle_errmsg_name('WSM', 'WSM_FUSD_GT_LUCD', l_stmt_num);
10272                                 l_error_code := -1;
10273                                 GOTO skip_resched_validations;
10274                             end if;
10275 
10276                         -- BA: BUG 3479666
10277                         ELSIF v_wlji_scheduling_method(v_idx) = 1 THEN
10278                             if(v_wlji_fusd(v_idx) is not null) then
10279                                 l_scheduling_mode := WIP_CONSTANTS.FORWARDS;
10280                             else
10281                                 l_scheduling_mode := WIP_CONSTANTS.BACKWARDS;
10282                             end if;
10283                         -- EA: BUG 3479666
10284 
10285                         ELSIF v_wlji_scheduling_method(v_idx) = 2 THEN
10286                             if (v_wlji_fusd(v_idx) is not null) then
10287 l_stmt_num := 760.1;
10288                                 IF ((v_wlji_fusd(v_idx) <> l_cur_start_date) and g_charges_exist_shopfloor)
10289                                 THEN
10290                                     l_aux_mesg := '';
10291                                     handle_invalid_field('Start Date', l_aux_mesg, l_stmt_num);
10292                                     l_error_code := -1;
10293                                     GOTO skip_resched_validations;
10294                                 ELSE
10295 l_stmt_num := 760.2;
10296                                     v_wlji_lucd(v_idx) := wsmputil.GET_SCHEDULED_DATE(
10297                                             v_wlji_org_id(v_idx),
10298                                             v_wlji_item_id(v_idx),
10299                                             'F',
10300                                             v_wlji_fusd(v_idx),
10301                                             l_error_code,
10302                                             l_error_msg,
10303                                             v_wlji_start_quantity(v_idx));  --Fixed bug # 2313574
10304                                 END IF;
10305                             elsif (v_wlji_lucd(v_idx) is not null) then
10306 l_stmt_num := 760.3;
10307                                 v_wlji_fusd(v_idx) := wsmputil.GET_SCHEDULED_DATE (
10308                                         v_wlji_org_id(v_idx),
10309                                         v_wlji_item_id(v_idx),
10310                                         'B',
10311                                         v_wlji_lucd(v_idx),
10312                                         l_error_code,
10313                                         l_error_msg,
10314                                         v_wlji_start_quantity(v_idx));  --Fixed bug # 2313574
10315                             end if;
10316 
10317                         END IF;
10318 
10319                         if l_error_code <> 0 OR l_error_msg IS NOT NULL then
10320                             handle_error( l_error_code, l_error_msg, l_stmt_num);
10321                             l_error_code := -1;
10322                             GOTO skip_resched_validations;
10323                         end if;
10324                         if lbji_debug = 'Y' then
10325                             fnd_file.put_line(fnd_file.log,
10326                                     'Validate reschedule Dates .. OK');
10327                         end if;
10328                         -- }} Validate dates
10329 
10330 
10331 l_stmt_num := 770;
10332                         -- {{ validate coproducts
10333                         if ((v_wlji_coproducts_supply(v_idx) IS NOT NULL) and
10334                            (v_wlji_coproducts_supply(v_idx) NOT IN (1,2))) then
10335                             l_aux_mesg := '';
10336                             handle_invalid_field('Coproducts Supply', l_aux_mesg, l_stmt_num);
10337                             l_error_code := -1;
10338                             GOTO skip_resched_validations;
10339                         end if;
10340                         if lbji_debug = 'Y' then
10341                             fnd_file.put_line(fnd_file.log,
10342                                     'Validate reschedule Coproducts_Supply .. OK');
10343                         end if;
10344                         -- }} validate coproducts
10345 
10346 << skip_resched_validations >>
10347 
10348                         if l_error_code <> 0 then
10349                             l_error_count := l_error_count + 1;
10350                             GOTO abort_this_job;
10351                         end if;
10352 
10353                         if l_cur_job_type = 3 then
10354                             if l_cur_status_type = 3 then
10355                                 l_common_rtg_seq_id := l_cur_com_rtg_seq_id;
10356                                 l_common_bom_seq_id := l_cur_com_bom_seq_id;
10357                             end if;
10358                             if l_cur_status_type = 1 then
10359                                 if l_chg_bom_ref = 0 and l_chg_rtg_ref = 0 then
10360                                     l_common_rtg_seq_id := l_cur_com_rtg_seq_id;
10361                                     l_common_bom_seq_id := l_cur_com_bom_seq_id;
10362                                 end if;
10363                             end if;
10364                         end if; -- job type=3
10365 
10366                     end if; --load_type 6
10367 
10368 -- ===========================================================================================
10369 -- END: Reschedule (Update) Job Validations  }}}
10370 -- ===========================================================================================
10371 
10372 
10373 
10374 -- ==============================================================================================
10375 -- BEGIN: WRITING INTO BASE TABLES {{{
10376 -- ==============================================================================================
10377 
10378                     -- ---------------------------------------------------
10379                     -- {{{ Begin: Write WE, WDJ etc. for job creation only
10380                     -- ---------------------------------------------------
10381                     if v_wlji_load_type(v_idx) = 5 then
10382 l_stmt_num := 800;
10383             g_skip_wlbj := 1;   --OPTII-PERF
10384                         build_job_header_info(
10385                             p_common_routing_sequence_id=> l_common_rtg_seq_id,
10386                             p_common_bill_sequence_id   => l_common_bom_seq_id,
10387                             p_status_type               => v_wlji_status_type(v_idx),
10388                             p_class_code                => v_wlji_class_code(v_idx),
10389                             p_org_id                    => v_wlji_org_id(v_idx),
10390                             p_wip_entity_id             => v_wlji_wip_entity_id(v_idx),
10391                             p_last_updt_date            => v_wlji_last_updt_date(v_idx),
10392                             p_last_updt_by              => v_wlji_last_updt_by(v_idx),
10393                             p_creation_date             => v_wlji_creation_date(v_idx),
10394                             p_created_by                => v_wlji_created_by(v_idx),
10395                             p_last_updt_login           => v_wlji_last_updt_login(v_idx),
10396                             p_request_id                => v_wlji_request_id(v_idx),
10397                             p_program_appl_id           => v_wlji_program_appl_id(v_idx),
10398                             p_program_id                => v_wlji_program_id(v_idx),
10399                             p_prog_updt_date            => v_wlji_prog_updt_date(v_idx),
10400                             p_source_line_id            => v_wlji_src_line_id(v_idx),
10401                             p_source_code               => v_wlji_source_code(v_idx),
10402                             p_description               => v_wlji_description(v_idx),
10403                             p_item                      => v_wlji_item_id(v_idx),
10404                             p_job_type                  => v_wlji_job_type(v_idx),
10405                             p_bom_reference_id          => v_wlji_bom_ref_id(v_idx),
10406                             p_routing_reference_id      => v_wlji_rtg_ref_id(v_idx),
10407                             p_firm_planned_flag         => v_wlji_firm_planned_flag(v_idx),
10408                             p_wip_supply_type           => v_wlji_wip_supply_type(v_idx),
10409                             -- it can be null if we need to call infinite scheduler, but wip
10410                             -- does not allow inserting null, insert sysdate temporarily
10411                             p_job_scheduled_start_date  => nvl(v_wlji_fusd(v_idx), sysdate),
10412                             p_job_scheduled_compl_date  => nvl(v_wlji_lucd(v_idx), sysdate),
10413                             p_start_quantity            => v_wlji_start_quantity(v_idx),
10414                             p_net_quantity              => v_wlji_net_quantity(v_idx),
10415                             p_coproducts_supply         => v_wlji_coproducts_supply(v_idx),
10416                             p_bom_revision              => v_wlji_bom_revision(v_idx),
10417                             p_routing_revision          => v_wlji_rtg_revision(v_idx),
10418                             p_bom_revision_date         => v_wlji_bom_rev_date(v_idx),
10419                             p_routing_revision_date     => v_wlji_rtg_rev_date(v_idx),
10420                             p_lot_number                => v_wlji_lot_number(v_idx),
10421                             p_alt_bom_designator        => v_wlji_alt_bom(v_idx),
10422                             p_alt_routing_designator    => v_wlji_alt_rtg(v_idx),
10423                             p_priority                  => v_wlji_priority(v_idx),
10424                             p_due_date                  => v_wlji_due_date(v_idx),
10425                             p_attribute_category        => v_wlji_attribute_category(v_idx),
10426                             p_attribute1                => v_wlji_attribute1(v_idx),
10427                             p_attribute2                => v_wlji_attribute2(v_idx),
10428                             p_attribute3                => v_wlji_attribute3(v_idx),
10429                             p_attribute4                => v_wlji_attribute4(v_idx),
10430                             p_attribute5                => v_wlji_attribute5(v_idx),
10431                             p_attribute6                => v_wlji_attribute6(v_idx),
10432                             p_attribute7                => v_wlji_attribute7(v_idx),
10433                             p_attribute8                => v_wlji_attribute8(v_idx),
10434                             p_attribute9                => v_wlji_attribute9(v_idx),
10435                             p_attribute10               => v_wlji_attribute10(v_idx),
10436                             p_attribute11               => v_wlji_attribute11(v_idx),
10437                             p_attribute12               => v_wlji_attribute12(v_idx),
10438                             p_attribute13               => v_wlji_attribute13(v_idx),
10439                             p_attribute14               => v_wlji_attribute14(v_idx),
10440                             p_attribute15               => v_wlji_attribute15(v_idx),
10441                             p_job_name                  => v_wlji_job_name(v_idx),
10442                             p_completion_subinventory   => v_wlji_compl_subinv(v_idx),
10443                             p_completion_locator_id     => v_wlji_compl_loc_id(v_idx),
10444                             p_demand_class              => v_wlji_demand_class(v_idx),
10445                             p_project_id                => v_wlji_project_id(v_idx),
10446                             p_task_id                   => v_wlji_task_id(v_idx),
10447                             p_schedule_group_id         => v_wlji_schedule_group_id(v_idx),
10448                             p_build_sequence            => v_wlji_build_sequence(v_idx),
10449                             p_line_id                   => v_wlji_line_id(v_idx),
10450                             p_kanban_card_id            => v_wlji_kanban_card_id(v_idx),
10451                             p_overcompl_tol_type        => v_wlji_overcompl_tol_type(v_idx),
10452                             p_overcompl_tol_value       => v_wlji_overcompl_tol_value(v_idx),
10453                             p_end_item_unit_number      => v_wlji_end_item_unit_number(v_idx),
10454                             p_src_client_server         => 0,
10455                             p_po_creation_time          => WSMPLCVA.v_org(v_wlji_org_id(v_idx)).PO_CREATION_TIME,
10456                             p_date_released             => v_wlji_date_released(v_idx),
10457                             p_error_code                => l_error_code,
10458                             p_error_msg                 => l_error_msg);
10459                         g_skip_wlbj := NULL; --OPTII-PERF
10460                         if l_error_code <> 0 then
10461                             handle_error(l_error_code, l_error_msg,  l_stmt_num);
10462                             l_error_count := l_error_count + 1;
10463                             GOTO abort_this_job;
10464                         end if;
10465 
10466 
10467                     end if; -- load_type 5
10468                     -- ---------------------------------------------------
10469                     -- }}}   End: Write WE, WDJ etc. for job creation only
10470                     -- ---------------------------------------------------
10471 
10472                     -- ----------------------------------------------------------------
10473                     -- {{{ Begin: Write WO's and WCO's, job creation and alt/ref change
10474                     -- ----------------------------------------------------------------
10475 
10476                     -- in such cases, WCO records will be created first, and WO records
10477                     -- will be created by looking at WCO records
10478 
10479                     if v_wlji_load_type(v_idx) = 5 or
10480                        l_chg_alt_flag = 1 or
10481                        l_chg_ref_flag = 1 or
10482                        l_chg_rev_flag = 1       -- ADD: bug3202534
10483                     then
10484                        v_new_job := 1;
10485                         declare
10486                             temp_common_bom_seq_id   number;
10487                         begin
10488 l_stmt_num := 810;
10489                             if(l_chg_alt_flag = 1 or l_chg_ref_flag = 1 or l_chg_rev_flag = 1) then
10490                                 -- Delete WO records
10491                                 delete from wip_operations
10492                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10493                                 delete from wip_operation_yields
10494                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10495                                 delete from wip_operation_resources
10496                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10497                                 delete from wip_requirement_operations
10498                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10499                                 delete from wip_sub_operation_resources
10500                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10501                                 delete from wip_operation_resource_usage
10502                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10503                                 delete from wip_op_resource_instances
10504                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
10505                                 v_new_job := 2;
10506                             end if;
10507 l_stmt_num := 820;
10508                             -- {{ Check for disabled ops in the network,job_type 1 and 3
10509                             if (wsmputil.network_with_disabled_op(
10510                                     l_common_rtg_seq_id,
10511                                     v_wlji_rtg_rev_date(v_idx),
10512                                     l_error_code,
10513                                     l_error_msg) = 1)
10514                             then
10515                                 fnd_message.set_name('WSM','WSM_NET_HAS_DISABLED_OP');
10516                                 l_error_msg := fnd_message.get;
10517                                 l_warning_count := l_warning_count + 1;
10518                                 handle_warning(
10519                                     p_err_msg               => l_error_msg,
10520                                     p_header_id             => v_wlji_header_id(v_idx),
10521                                     p_request_id            => v_wlji_request_id(v_idx),
10522                                     p_program_id            => v_wlji_program_id(v_idx),
10523                                     p_program_application_id=> v_wlji_program_appl_id(v_idx));
10524 
10525                             end if;
10526                             -- }} Check for disabled ops in the network
10527 
10528                             if(l_chg_ref_flag = 1) then
10529                                 temp_common_bom_seq_id := null;
10530                             else
10531                                 temp_common_bom_seq_id := l_common_bom_seq_id;
10532                             end if;
10533 l_stmt_num := 830;
10534                             if(v_wlji_scheduling_method(v_idx) = 1) then
10535                                 l_inf_sche_flag := 'Y';
10536                             else
10537                                 l_inf_sche_flag := 'N';
10538                             end if;
10539                             v_insert_wip := 1; --OPTII-PERF:By default,it is 1 for new jobs.
10540                                                --If phantom requirement exists, this will be Set to No.
10541                             build_job_copy_info(
10542                                 p_common_routing_sequence_id    => l_common_rtg_seq_id,
10543                                 p_common_bill_sequence_id       => temp_common_bom_seq_id,
10544                                 p_org_id                        => v_wlji_org_id(v_idx),
10545                                 p_wip_entity_id                 => v_wlji_wip_entity_id(v_idx),
10546                                 p_last_updt_date                => v_wlji_last_updt_date(v_idx),
10547                                 p_last_updt_by                  => v_wlji_last_updt_by(v_idx),
10548                                 p_creation_date                 => v_wlji_creation_date(v_idx),
10549                                 p_created_by                    => v_wlji_created_by(v_idx),
10550                                 p_last_updt_login               => v_wlji_last_updt_login(v_idx),
10551                                 p_request_id                    => v_wlji_request_id(v_idx),
10552                                 p_program_appl_id               => v_wlji_program_appl_id(v_idx),
10553                                 p_program_id                    => v_wlji_program_id(v_idx),
10554                                 p_prog_updt_date                => v_wlji_prog_updt_date(v_idx),
10555                                 p_item                          => v_wlji_item_id(v_idx),
10556                                 p_bom_reference_id              => v_wlji_bom_ref_id(v_idx),
10557                                 p_routing_reference_id          => v_wlji_rtg_ref_id(v_idx),
10558                                 p_wip_supply_type               => v_wlji_wip_supply_type(v_idx),
10559                                 p_job_scheduled_start_date      => v_wlji_fusd(v_idx),
10560                                 p_job_scheduled_compl_date      => v_wlji_lucd(v_idx),
10561                                 p_start_quantity                => v_wlji_start_quantity(v_idx),
10562                                 p_bom_revision_date             => v_wlji_bom_rev_date(v_idx),
10563                                 p_routing_revision_date         => v_wlji_rtg_rev_date(v_idx),
10564                                 p_alt_bom_designator            => v_wlji_alt_bom(v_idx),
10565                                 p_alt_routing_designator        => v_wlji_alt_rtg(v_idx),
10566                                 p_header_id                     => v_wlji_header_id(v_idx),
10567                                 p_num_of_children               => v_wlji_num_of_children(v_idx),
10568                                 p_infinite_schedule             => l_inf_sche_flag,
10569                                 p_error_code                    => l_error_code,
10570                                 p_error_msg                     => l_error_msg);
10571                             if l_error_code <> 0 then
10572                                 handle_error(l_error_code, l_error_msg,  l_stmt_num);
10573                                 l_error_count := l_error_count + 1;
10574                                 GOTO abort_this_job;
10575                             end if;
10576 
10577 
10578                             if v_wlji_mode_flag(v_idx) = 2 then
10579 l_stmt_num := 840;
10580                                 -- BA: bug 3299026 do not allow serial controlled component
10581                                 select  SERIAL_NUMBER_CONTROL_CODE,
10582                                         revision_qty_control_code       -- Add bug 2963225
10583                                 into    dummy_number,
10584                                         l_rev_control_code              -- Add bug 2963225
10585                                 from    mtl_system_items_kfv msi
10586                                 where   msi.inventory_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10587                                 and     msi.organization_id = v_wlji_org_id(v_idx);
10588 
10589                                 -- Added code to restrict serial tagged components also.
10590                                 if( dummy_number <> 1 or
10591                                     inv_cache.get_serial_tagged(v_wlji_org_id(v_idx),
10592                                                                 v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
10593                                                                 WIP_CONSTANTS.ISSCOMP_TYPE ) = WIP_CONSTANTS.SER_TAGGED ) then
10594                                     l_error_code := -1;
10595                                     handle_errmsg_name('WSM',
10596                                                        'WSM_SERIAL_COMP_NOT_SUPPORTED',
10597                                                        l_stmt_num);
10598                                     l_error_count := l_error_count + 1;
10599                                     GOTO abort_this_job;
10600                                 end if;
10601                                 --EA bug 3299026
10602 
10603                                 -- {{ Check the component required at 1st op
10604 l_stmt_num := 840.1;
10605                                 find_copy_routing_start(
10606                                     v_wlji_wip_entity_id(v_idx),
10607                                     l_start_op_seq_num,
10608                                     l_start_op_seq_id,
10609                                     l_error_code,
10610                                     l_error_msg);
10611                                 if l_error_code <> 0 OR l_error_msg IS NOT NULL then
10612                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
10613                                     l_error_code := -1;
10614                                     l_error_count := l_error_count + 1;
10615                                     GOTO abort_this_job;
10616                                 end if;
10617 l_stmt_num := 840.2;
10618 
10619   /****************ADDED FOLLOWING CODE FOR 12.1 SUBSTITUTE COMPONENT PROJECT TO CHECK WHETHER THE SOURCE LOT IS OF PRIMARY COMPONENT
10620   OR SUBSTITUTE COMPONENT ***********************************************************************************************************/
10621 
10622 
10623                         BEGIN
10624 
10625                                 SELECT 1,
10626                                        bic.component_item_id,
10627                                        bsc.substitute_item_quantity,
10628                                        (bsc.substitute_item_quantity/nvl(bic.component_yield_factor,1))
10629                                  INTO  l_is_src_lot_sub,
10630                                        l_primary_comp_item_id,
10631                                        l_sub_comp_bill_qpa,
10632                                        l_sub_comp_qpa
10633                                  FROM  bom_bill_of_materials bom,
10634                                        bom_inventory_components bic,
10635                                        bom_substitute_components bsc
10636                                  WHERE bsc.SUBSTITUTE_COMPONENT_ID = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10637                                    AND bic.COMPONENT_SEQUENCE_ID = bsc.COMPONENT_SEQUENCE_ID
10638                                    AND bom.organization_id =v_wlji_org_id(v_idx)
10639                                    AND bic.bill_sequence_id = bom.common_bill_sequence_id
10640                                    AND bom.common_bill_sequence_id =l_common_bom_seq_id
10641                                    AND bic.implementation_date is not null
10642                                    AND bic.effectivity_date <= sysdate and nvl(bic.disable_date,sysdate +1) > sysdate
10643                                    AND (bic.operation_seq_num =  l_start_op_seq_num OR bic.operation_seq_num = 1)
10644                                    AND bic.item_num = (SELECT min(item_num)
10645                                                        FROM bom_bill_of_materials bom3,
10646                                                             bom_inventory_components bic3,
10647                                                             bom_substitute_components bsc3
10648                                                       WHERE bsc3.SUBSTITUTE_COMPONENT_ID = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10649                                                         AND bic3.COMPONENT_SEQUENCE_ID = bsc3.COMPONENT_SEQUENCE_ID
10650                                                         AND bom3.organization_id =v_wlji_org_id(v_idx)
10651                                                         AND bic3.bill_sequence_id = bom3.common_bill_sequence_id
10652                                                         AND bom3.common_bill_sequence_id = l_common_bom_seq_id
10653                                                         AND bic3.implementation_date is not null
10654                                                         AND bic3.effectivity_date <= sysdate
10655                                                         AND nvl(bic3.disable_date,sysdate +1) > sysdate)
10656                                   AND NOT EXISTS
10657                                             ( SELECT 1  FROM bom_bill_of_materials bom2,
10658                                                              bom_inventory_components bic2
10659                                               WHERE bic2.component_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10660                                               AND bom2.organization_id =v_wlji_org_id(v_idx)
10661                                               AND bic2.bill_sequence_id = bom2.common_bill_sequence_id
10662                                               AND bom2.common_bill_sequence_id =  l_common_bom_seq_id
10663                                               AND bic.effectivity_date <= sysdate and nvl(bic.disable_date,sysdate +1) > sysdate
10664                                               AND bic2.implementation_date is not null
10665                                               AND (bic2.operation_seq_num =  l_start_op_seq_num OR bic.operation_seq_num = 1));
10666 
10667                           EXCEPTION
10668                                   WHEN OTHERS THEN
10669                                     l_is_src_lot_sub :=0;
10670 
10671 
10672 
10673                           END;
10674 
10675 
10676 /* END CHANGES FOR 12.1 SUBSTITUTE COMPONENT PROJECT**********************************************************
10677 *************************************************************************************************************/
10678 
10679 		                --Bug 5112899:Using the below sql to read
10680 				-- BILL_QUANTITY_PER_ASSEMBLY and basis_type.
10681 				--Sum is removed as there can not be more than
10682 				--1 record in BOM for a given comp item id.
10683 				--select  sum(quantity_per_assembly)
10684 
10685 			     /* CHANGED TO DECODE FOR 12.1 SUBSTITUTE COMPONENT PROJECT */
10686 
10687 			            /* 	select  quantity_per_assembly,BILL_QUANTITY_PER_ASSEMBLY, */
10688 				select  decode(l_is_src_lot_sub,1,l_sub_comp_qpa,quantity_per_assembly),
10689                                         decode(l_is_src_lot_sub,1,l_sub_comp_bill_qpa,BILL_QUANTITY_PER_ASSEMBLY),
10690 				        basis_type
10691 				into   l_required_qpa,l_bill_req_qpa,l_basis_type
10692 				from    wsm_copy_requirement_ops wcro
10693                                 where   wcro.wip_entity_id = v_wlji_wip_entity_id(v_idx)
10694                                /* and     wcro.component_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id */
10695                                 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)
10696 
10697                                 and     source_phantom_id = -1  -- is null
10698                                 and     recommended = 'Y'
10699 				and     wcro.operation_seq_num = l_start_op_seq_num;
10700 				--Bug 5112899:wcro.operation_seq_num equal to 1 need not be
10701 				--start operation.
10702                                 --and     (wcro.operation_seq_num = l_start_op_seq_num or
10703                                 --         wcro.operation_seq_num= 1);
10704                                 --if(l_required_qpa = 0) then           -- bug 2963225
10705                                 if(NVL(l_required_qpa, 0) = 0) then     -- bug 2963225
10706                                     l_error_code := -1;
10707                                     fnd_message.set_name('WSM','WSM_INVALID_BOM_ROUT');
10708                                     l_error_msg := fnd_message.get;
10709                                     handle_error(l_error_code, l_error_msg, l_stmt_num);
10710                                     l_error_count := l_error_count + 1;
10711                                     GOTO abort_this_job;
10712                                 end if;
10713 
10714                                 if lbji_debug = 'Y' then
10715                                     fnd_file.put_line(fnd_file.log,
10716                                             'Verify if component is required at the first operation... OK');
10717                                 end if;
10718                                 -- }} Check the component required at first op
10719 l_stmt_num := 850;
10720                                 -- {{ validate starting lot is not phantom
10721                                 l_temp_num := 0;
10722                                 begin
10723                                     select  1
10724                                     into    l_temp_num
10725                                     from    wsm_copy_requirement_ops wcro
10726                                     where   wcro.wip_entity_id = v_wlji_wip_entity_id(v_idx)
10727                                     /*and     wcro.component_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id */
10728                                     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)
10729                                     and     recommended = 'Y'
10730                                     and     (wcro.operation_seq_num = l_start_op_seq_num or
10731                                              wcro.operation_seq_num= 1)
10732                                     and     nvl(wcro.wip_supply_type, 1) = 6;
10733                                 exception
10734                                     when too_many_rows then
10735                                         l_temp_num := 1;
10736                                     when others then
10737                                         null;
10738                                 end;
10739                                 if(l_temp_num = 1) then
10740                                     l_error_code := -1;
10741                                     handle_errmsg_name(
10742                                         'WSM', 'WSM_PHANTOM_COMP_NOT_ALLOWED', l_stmt_num);
10743                                     l_error_count := l_error_count + 1;
10744                                     GOTO abort_this_job;
10745                                 end if;
10746                                 if lbji_debug = 'Y' then
10747                                     fnd_file.put_line(fnd_file.log,
10748                                         'Verify that the starting lot is not phantom.. OK');
10749                                 end if;
10750                                 -- }} validate starting lot is not phantom
10751 
10752                                 -- {{ Validate component quantity
10753 				--Bug 5112899: Start of changes
10754 l_stmt_num := 860.1;
10755                                select include_component_yield
10756 			       into   l_include_yield
10757 			       from   wip_parameters
10758 			       where  organization_id = v_wlji_org_id(v_idx);
10759 l_stmt_num := 860.2;
10760 			        --Bug 5112899: Start of changes
10761                                 IF v_wsli(v_wlji_src_line_id(v_idx)).comp_issue_quantity IS NULL THEN
10762                                    if l_basis_type is NULL or l_basis_type = 1 then
10763 					 If l_include_yield = 1 or l_include_yield is NULL then
10764 			        --Bug 5112899: End of changes
10765 					   l_quantity_tobe_issued
10766 					    := l_required_qpa * v_wlji_start_quantity(v_idx);
10767 				--Bug 5112899: Start of changes
10768 					 else -- l_include_yield
10769 					   l_quantity_tobe_issued
10770 					    := l_bill_req_qpa * v_wlji_start_quantity(v_idx);
10771 					 end if;-- l_include_yield
10772 			           else --basis type
10773 			              If l_include_yield is null or  l_include_yield = 1 then
10774 					   l_quantity_tobe_issued:= l_required_qpa;
10775 				      else
10776 				           l_quantity_tobe_issued:= l_bill_req_qpa;
10777 				      end if;
10778 			           end if;--basis_type.
10779                                 END IF; -- comp_issue_quantity
10780 			       --Bug 5112899: End of changes
10781 l_stmt_num := 860.3;
10782                                 if v_wsli(v_wlji_src_line_id(v_idx)).comp_issue_quantity is not null then
10783                                     if v_wsli(v_wlji_src_line_id(v_idx)).comp_issue_quantity <= 0 then
10784                                         l_error_code := -1;
10785                                         handle_errmsg_name('WSM','WSM_QTY_ISSUE_NO_NEG', l_stmt_num);
10786                                     else
10787                                         l_quantity_tobe_issued
10788                                             := v_wsli(v_wlji_src_line_id(v_idx)).comp_issue_quantity;
10789                                     end if;
10790                                 end if;
10791 
10792 l_stmt_num := 870;
10793                                 -- if the user does not provide a revision in wsm_starting_lots_interface
10794                                 -- then get the current revision and use that to issue components. if user
10795                                 -- provides one, validate it
10796                                 l_source_item_rev := v_wsli(v_wlji_src_line_id(v_idx)).revision;
10797                                 declare
10798                                     err_lot_revision    exception;  -- Add: bug 2963225
10799                                 begin
10800                                     -- BC: bug 2963225 this is to validate the revision of the item
10801                                     -- which is not right, instead, we should validate / default
10802                                     -- the revision of the starting lot, so comment out the following
10803 
10804                                     --l_rev_date := sysdate;
10805                                     --wip_revisions.bom_revision (
10806                                     --    v_wlji_org_id(v_idx),
10807                                     --    v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
10808                                     --    l_source_item_rev,
10809                                     --    l_source_item_rev_date,
10810                                     --    l_rev_date);
10811 
10812                                     if l_rev_control_code = 1 then -- not revision control
10813                                         if(l_source_item_rev is not null) then
10814                                             raise err_lot_revision;
10815                                         end if;
10816                                     else    -- revision control item
10817                                         select  revision
10818                                         into    l_start_lot_revision
10819                                         from    WSM_source_lots_v
10820                                         where   lot_number = v_wsli(v_wlji_src_line_id(v_idx)).lot_number
10821                                         and     organization_id = v_wlji_org_id(v_idx)
10822                                         and     inventory_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id;
10823 
10824                                         if(l_source_item_rev is null) then
10825                                             v_wsli(v_wlji_src_line_id(v_idx)).revision := l_start_lot_revision;
10826                                         elsif(l_source_item_rev <> l_start_lot_revision) then
10827                                             raise err_lot_revision;
10828                                         end if;
10829                                     end if;
10830                                     -- EC: bug 2963225
10831 
10832                                 exception
10833                                     when err_lot_revision then  -- Add: bug 2963225
10834                                         l_error_code := -1;
10835                                         l_error_count := l_error_count +1;
10836                                         handle_invalid_field('Component Lot Revision', '', l_stmt_num);
10837                                         GOTO abort_this_job;
10838 
10839                                     when others then
10840                                         l_error_code := -1;
10841                                         l_error_count := l_error_count +1;
10842                                         handle_invalid_field('Component Lot Revision', '', l_stmt_num);
10843                                         GOTO abort_this_job;
10844                                 end;
10845 l_stmt_num := 880;
10846                                 wsmputil.return_att_quantity(
10847                                     p_org_id     => v_wlji_org_id(v_idx),
10848                                     p_item_id    => v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
10849                                     p_rev        => v_wsli(v_wlji_src_line_id(v_idx)).revision,
10850                                     p_lot_no     => v_wsli(v_wlji_src_line_id(v_idx)).lot_number,
10851                                     p_subinv     => v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code,
10852                                     p_locator_id => v_wsli(v_wlji_src_line_id(v_idx)).locator_id,
10853                                     p_qoh        => l_qoh,
10854                                     p_atr        => l_atr,
10855                                     p_att        => l_att,
10856                                     p_err_code   => l_error_code,
10857                                     p_err_msg    => l_error_msg );
10858 
10859                                 if l_error_code <> 0 then
10860                                     handle_error( l_error_code, l_error_msg, l_stmt_num);
10861                                     l_error_count := l_error_count + 1;
10862                                     GOTO abort_this_job;
10863                                 end if;
10864                                 if l_quantity_tobe_issued > l_att then
10865                                     -- bug 3741740 remove this validation because we allow issue
10866                                     -- less quantity than job required quantity through form
10867                                     -- we will issue all the quantity if qty_tobe_issued > l_att
10868                                     l_quantity_tobe_issued := l_att;
10869                                     --l_error_code := -1;
10870                                     --handle_errmsg_name('WSM', 'WSM_INSUFFICIENT_QTY', l_stmt_num);
10871                                     --l_error_count := l_error_count + 1;
10872                                     --GOTO abort_this_job;
10873                                 end if;
10874                                 if lbji_debug = 'Y' then
10875                                     fnd_file.put_line(fnd_file.log, 'Mode 2 Quantity Verification... OK');
10876                                 end if;
10877                                 -- }} Validate component quantity
10878                                 l_src_lot_number := v_wsli(v_wlji_src_line_id(v_idx)).lot_number;     -- LOTATTR
10879                                 l_src_inv_item_id := v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id; -- LOTATTR
10880 
10881                             end if; -- mode_flag 2
10882 
10883 l_stmt_num := 890;
10884                             build_job_detail_info(
10885                                 p_common_routing_sequence_id    => l_common_rtg_seq_id,
10886                                 p_common_bill_sequence_id       => temp_common_bom_seq_id,
10887                                 p_status_type                   => v_wlji_status_type(v_idx),
10888                                 p_org_id                        => v_wlji_org_id(v_idx),
10889                                 p_wip_entity_id                 => v_wlji_wip_entity_id(v_idx),
10890                                 p_last_updt_date                => v_wlji_last_updt_date(v_idx),
10891                                 p_last_updt_by                  => v_wlji_last_updt_by(v_idx),
10892                                 p_creation_date                 => v_wlji_creation_date(v_idx),
10893                                 p_created_by                    => v_wlji_created_by(v_idx),
10894                                 p_last_updt_login               => v_wlji_last_updt_login(v_idx),
10895                                 p_request_id                    => v_wlji_request_id(v_idx),
10896                                 p_program_appl_id               => v_wlji_program_appl_id(v_idx),
10897                                 p_program_id                    => v_wlji_program_id(v_idx),
10898                                 p_prog_updt_date                => v_wlji_prog_updt_date(v_idx),
10899                                 p_item                          => v_wlji_item_id(v_idx),
10900                                 p_job_type                      => v_wlji_job_type(v_idx),
10901                                 p_bom_reference_id              => v_wlji_bom_ref_id(v_idx),
10902                                 p_routing_reference_id          => v_wlji_rtg_ref_id(v_idx),
10903                                 p_wip_supply_type               => v_wlji_wip_supply_type(v_idx),
10904                                 p_job_scheduled_start_date      => v_wlji_fusd(v_idx),
10905                                 p_job_scheduled_compl_date      => v_wlji_lucd(v_idx),
10906                                 p_start_quantity                => v_wlji_start_quantity(v_idx),
10907                                 p_bom_revision_date             => v_wlji_bom_rev_date(v_idx),
10908                                 p_routing_revision_date         => v_wlji_rtg_rev_date(v_idx),
10909                                 p_alt_bom_designator            => v_wlji_alt_bom(v_idx),
10910                                 p_alt_routing_designator        => v_wlji_alt_rtg(v_idx),
10911                                 p_rtg_op_seq_num                => null,
10912                                 p_error_code                    => l_error_code,
10913                                 p_error_msg                     => l_error_msg,
10914                                 -- BA:bug 3311985, call build_job_detail_info with new parameter
10915                                 p_src_client_server             => 0,
10916                                 p_po_creation_time              => WSMPLCVA.v_org(v_wlji_org_id(v_idx)).PO_CREATION_TIME
10917                                 -- EA:bug 3311985, call build_job_detail_info with new parameter
10918                             );
10919                             if l_error_code <> 0 then
10920                                 handle_error(l_error_code, l_error_msg,  l_stmt_num);
10921                                 l_error_count := l_error_count + 1;
10922                                 GOTO abort_this_job;
10923                             end if;
10924 
10925 /****************ADDED FOLLOWING CODE FOR 12.1 SUBSTITUTE COMPONENT PROJECT TO UPDATE WIP_REQIREMENT_OPERATIONS TO REPLACE REQUIREMENT OF PRIMARY COMPONENT
10926  ***********************************************************************************************************/
10927 
10928                              IF (l_is_src_lot_sub = 1 AND v_wlji_mode_flag(v_idx) = 2 ) THEN
10929 
10930                                BEGIN
10931 
10932                                    SELECT segment1,segment2,segment3,segment4,
10933                                           segment5,segment6,segment7,segment8,
10934                                           segment9,segment10,segment11,segment12,
10935                                           segment13,segment14,segment15,segment16,
10936                                           segment17,segment18,segment19,segment20
10937                                     INTO
10938                                           l_segment1,l_segment2,l_segment3,l_segment4,
10939                                           l_segment5,l_segment6,l_segment7,l_segment8,
10940                                           l_segment9,l_segment10,l_segment11,l_segment12,
10941                                           l_segment13,l_segment14,l_segment15,l_segment16,
10942                                           l_segment17,l_segment18,l_segment19,l_segment20
10943                                     FROM  mtl_system_items
10944                                     WHERE inventory_item_id =  v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id
10945                                      AND  organization_id=v_wlji_org_id(v_idx);
10946 
10947                                     EXCEPTION
10948                                      WHEN OTHERS THEN
10949                                        NULL;
10950 
10951                                  END;
10952 
10953                              UPDATE wip_requirement_operations
10954                                SET  inventory_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
10955                                     required_quantity = ((required_quantity * l_sub_comp_bill_qpa)/quantity_per_assembly),
10956                                     quantity_per_assembly = l_sub_comp_bill_qpa,
10957                                     segment1 = l_segment1,
10958                                     segment2 = l_segment2,
10959                                     segment3 = l_segment3,
10960                                     segment4 = l_segment4,
10961                                     segment5 = l_segment5,
10962                                     segment6 = l_segment6,
10963                                     segment7 = l_segment7,
10964                                     segment8 = l_segment8,
10965                                     segment9 = l_segment9,
10966                                     segment10 = l_segment10,
10967                                     segment11 = l_segment11,
10968                                     segment12 = l_segment12,
10969                                     segment13 = l_segment13,
10970                                     segment14 = l_segment14,
10971                                     segment15 = l_segment15,
10972                                     segment16 = l_segment16,
10973                                     segment17 = l_segment17,
10974                                     segment18 = l_segment18,
10975                                     segment19 = l_segment19,
10976                                     segment20 = l_segment20,
10977                                     released_quantity = ((released_quantity * l_sub_comp_bill_qpa)/quantity_per_assembly)
10978                               WHERE wip_entity_id = v_wlji_wip_entity_id(v_idx)
10979                                 AND inventory_item_id = l_primary_comp_item_id;
10980 
10981 
10982 
10983                               END IF;
10984 
10985 /* END CHANGES FOR 12.1 SUBSTITUTE COMPONENT PROJECT**********************************************************
10986 *************************************************************************************************************/
10987 
10988                         exception
10989                             when others then
10990                                 l_error_code := SQLCODE;
10991                                 l_error_msg  :=
10992                                     'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
10993                                    ||SUBSTR(SQLERRM, 1,1000);
10994                                 handle_error(l_error_code, l_error_msg, l_stmt_num);
10995                                 l_error_count := l_error_count + 1;
10996                                 GOTO abort_this_job;
10997                         end;
10998 
10999                     end if;
11000                     -- ----------------------------------------------------------------
11001                     -- }}} End: Write WO's and WCO's, job creation and alt/ref change
11002                     -- ----------------------------------------------------------------
11003 
11004                     -- -------------------------------
11005                     -- {{{ Begin: Write for job update
11006                     -- -------------------------------
11007                     if v_wlji_load_type(v_idx) = 6 then
11008 
11009                         -- {{ do not skip update
11010                         -- if the user wants to update only the references, the following
11011                         -- updates are not needed
11012                         if (l_skip_update_wo = 0 and
11013                             v_wlji_allow_explosion(v_idx) = 'Y' and
11014                             v_wlji_status_type(v_idx) <> 7 and
11015                             l_chg_alt_flag <> 1 and
11016                             l_chg_ref_flag <> 1 and
11017                             l_chg_rev_flag <> 1)
11018                         then
11019                             Begin
11020 l_stmt_num := 900;
11021                                 if g_charges_exist_shopfloor then
11022                                     l_txnexist := 1;
11023                                 else
11024                                     l_txnexist := 0;
11025                                 end if;
11026 l_stmt_num := 910;
11027                                 if v_wlji_start_quantity(v_idx) is null then      --bug4540762: begin
11028                                    v_wlji_start_quantity(v_idx) := l_cur_start_qty;
11029                                 end if;                                           --bug4540762: end
11030 
11031                                 if v_wlji_start_quantity(v_idx) <> l_cur_start_qty then
11032                                     l_qntydiff := 1;
11033                                 else
11034                                     l_qntydiff := 0;
11035                                 end if;
11036 
11037                                 update wip_operations
11038                                 set    -- BD: BUG 3479666
11039                                        -- in option C, we should not change operation start/completion
11040                                        -- date based on job header level information
11041                                        --
11042                                        --first_unit_start_date
11043                                        --    = decode(l_txnexist,
11044                                        --             0, nvl(v_wlji_fusd(v_idx), first_unit_start_date),      -- bug 3394520
11045                                        --             first_unit_start_date),
11046                                        --first_unit_completion_date
11047                                        --    = decode(l_txnexist,
11048                                        --             0, nvl(v_wlji_lucd(v_idx), first_unit_completion_date), -- bug 3394520
11049                                        --             first_unit_completion_date),
11050                                        --last_unit_start_date
11051                                        --    = decode(l_txnexist,
11052                                        --             0, nvl(v_wlji_fusd(v_idx), last_unit_start_date),       -- bug 3394520
11053                                        --             last_unit_start_date),
11054                                        --last_unit_completion_date
11055                                        --    = decode(l_txnexist,
11056                                        --             0, nvl(v_wlji_lucd(v_idx), last_unit_completion_date),  -- bug 3394520
11057                                        --             last_unit_completion_date),
11058                                        -- ED: BUG 3479666
11059                                        scheduled_quantity
11060                                            = round(v_wlji_start_quantity(v_idx),
11061                                                    wip_constants.max_displayed_precision),
11062                                        quantity_in_queue
11063                                            = decode(v_wlji_status_type(v_idx),
11064                                                     1, 0,
11065                                                     3, decode(l_cur_status_type,
11066                                                               1, decode(operation_seq_num,
11067                                                                         g_op_seq_incr,
11068                                                                         round(v_wlji_start_quantity(v_idx),
11069                                                                               wip_constants.max_displayed_precision),
11070                                                                         quantity_in_queue),
11071                                                               decode(l_qntydiff,
11072                                                                      1, decode(operation_seq_num,
11073                                                                                g_op_seq_incr,
11074                                                                                round(v_wlji_start_quantity(v_idx),
11075                                                                                      wip_constants.max_displayed_precision),
11076                                                                                quantity_in_queue),
11077                                                                      quantity_in_queue)),
11078                                                     quantity_in_queue),
11079                                        last_updated_by        = v_wlji_last_updt_by(v_idx),
11080                                        last_update_date       = sysdate,
11081                                        last_update_login      = v_wlji_last_updt_login(v_idx),
11082                                        program_update_date    = sysdate,
11083                                        request_id             = v_wlji_request_id(v_idx),
11084                                        program_application_id = v_wlji_program_appl_id(v_idx),
11085                                        program_id             = v_wlji_program_id(v_idx)
11086                                 where  organization_id        = v_wlji_org_id(v_idx)
11087                                 and    wip_entity_id          = v_wlji_wip_entity_id(v_idx);
11088                                 if lbji_debug = 'Y' then
11089                                     fnd_file.put_line(fnd_file.log,
11090                                         'Updated '||SQL%ROWCOUNT||' rows into wo');
11091                                 end if;
11092 
11093 l_stmt_num := 920.1;
11094                                 -- bug 3203505 change order with updating wro
11095                                 --Bug 5456260:In Option C,resource dates should not be
11096                                 --updated with the header dates.
11097                                 /*******************
11098                                 update wip_operation_resources
11099                                 set    start_date             = decode(l_txnexist,
11100                                                                        0, nvl(v_wlji_fusd(v_idx), start_date),      -- bug 3394520
11101                                                                        start_date),
11102                                        completion_date        = decode(l_txnexist,
11103                                                                        0, nvl(v_wlji_lucd(v_idx), completion_date), -- bug 3394520
11104                                                                        completion_date),
11105                                        last_updated_by        = v_wlji_last_updt_by(v_idx),
11106                                        last_update_date       = sysdate,
11107                                        last_update_login      = v_wlji_last_updt_login(v_idx),
11108                                        program_update_date    = sysdate,
11109                                        request_id             = v_wlji_request_id(v_idx),
11110                                        program_application_id = v_wlji_program_appl_id(v_idx),
11111                                        program_id             = v_wlji_program_id(v_idx)
11112                                 where  organization_id = v_wlji_org_id(v_idx)
11113                                 and    wip_entity_id = v_wlji_wip_entity_id(v_idx);
11114                                 if lbji_debug = 'Y' then
11115                                     fnd_file.put_line(fnd_file.log,
11116                                         'Updated '||SQL%ROWCOUNT||' rows into wor');
11117                                 end if;
11118                                 *******************/
11119 l_stmt_num := 920.2;
11120                                 --LBM enh : changed the expression for required quantity
11121                                 update wip_requirement_operations wro
11122                                 set    wro.date_required = (
11123                                             select nvl(min(first_unit_start_date), v_wlji_fusd(v_idx))
11124                                             from   wip_operations
11125                                             where  organization_id = v_wlji_org_id(v_idx)
11126                                             and    wip_entity_id = v_wlji_wip_entity_id(v_idx)
11127                                             and    operation_seq_num = abs(wro.operation_seq_num)),
11128                                        last_updated_by        = v_wlji_last_updt_by(v_idx),
11129                                        last_update_date       = sysdate,
11130                                        last_update_login      = v_wlji_last_updt_login(v_idx),
11131                                        request_id             = v_wlji_request_id(v_idx),
11132                                        program_update_date    = sysdate,
11133                                        program_id             = v_wlji_program_id(v_idx),
11134                                        program_application_id = v_wlji_program_appl_id(v_idx),
11135                                        required_quantity      = ((wro.quantity_per_assembly/nvl(wro.component_yield_factor,1)) * decode(
11136                                                           wro.basis_type, 2, 1, round(v_wlji_start_quantity(v_idx), 6)))
11137                                 where  organization_id        = v_wlji_org_id(v_idx)
11138                                 and    wip_entity_id = v_wlji_wip_entity_id(v_idx);
11139                                 if lbji_debug = 'Y' then
11140                                     fnd_file.put_line(fnd_file.log,
11141                                         'Updated '||SQL%ROWCOUNT||' rows into wro');
11142                                 end if;
11143 l_stmt_num := 920.3;
11144                                 -- abb H: optional scrap accounting
11145 
11146                                 if (l_cur_status_type IN (1,6) and v_wlji_status_type(v_idx) = 3) and
11147                                     wsmputil.wsm_esa_enabled(
11148                                             p_wip_entity_id => v_wlji_wip_entity_id(v_idx),
11149                                             err_code        => l_error_code,
11150                                             err_msg         => l_error_msg,
11151                                             p_org_id        => '',
11152                                             p_job_type      => '') = 1
11153                                 then
11154 l_stmt_num := 920.31;
11155                                     select min(operation_seq_num)
11156                                     into   l_min_op_seq_num
11157                                     from   wip_operations
11158                                     where  wip_entity_id =  v_wlji_wip_entity_id(v_idx);
11159 l_stmt_num := 920.32;
11160                                     select bd.scrap_account,
11161                                            bd.est_absorption_account,
11162                                            wo.department_id
11163                                     into   l_scrap_account_10,
11164                                            l_est_scrap_abs_account_10,
11165                                            l_department_id
11166                                     from   bom_departments bd,
11167                                            wip_operations wo
11168                                     where  wo.wip_entity_id = v_wlji_wip_entity_id(v_idx)
11169                                     and    wo.operation_seq_num = l_min_op_seq_num
11170                                     and    bd.department_id = wo.department_id;
11171 
11172                                     if l_scrap_account_10 is null or l_est_scrap_abs_account_10 is null then
11173                                         l_error_code := -1;
11174                                         fnd_message.set_name('WSM','WSM_NO_SCRAP_ACC');
11175                                         fnd_message.set_token('DEPT_ID',to_char(l_department_id));
11176                                         l_error_msg := fnd_message.get;
11177                                         handle_error(l_error_code, l_error_msg, l_stmt_num);
11178                                         l_error_count := l_error_count + 1;
11179                                         GOTO abort_this_job;
11180                                     end if;
11181 l_stmt_num := 920.33;
11182                                     update wip_operation_yields woy
11183                                     set    scrap_account = nvl(l_scrap_account_10, woy.scrap_account),
11184                                            est_scrap_absorb_account = nvl(l_est_scrap_abs_account_10,
11185                                                                           woy.est_scrap_absorb_account)
11186                                     where  wip_entity_id = v_wlji_wip_entity_id(v_idx)
11187                                     and    operation_seq_num = l_min_op_seq_num;
11188 
11189                                     if lbji_debug = 'Y' then
11190                                         fnd_file.put_line(fnd_file.log, 'Updated '||SQL%ROWCOUNT||' rows into woy');
11191                                     end if;
11192 
11193                                 end if;
11194 
11195                             Exception
11196                                 when others then
11197                                     l_error_code := SQLCODE;
11198                                     l_error_msg  :=
11199                                         'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
11200                                        ||SUBSTR(SQLERRM, 1,1000);
11201                                     handle_error(l_error_code, l_error_msg,  l_stmt_num);
11202                                     l_error_count := l_error_count + 1;
11203                                     GOTO abort_this_job;
11204                             End;
11205                         end if;
11206                         -- }} do not skip update
11207 
11208 l_stmt_num := 930;
11209                         Begin
11210                             if l_cur_status_type <> 1 AND v_wlji_status_type(v_idx) = 1 then
11211                                 delete from wip_period_balances
11212                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx)
11213                                 and    organization_id = v_wlji_org_id(v_idx);
11214                                 if lbji_debug = 'Y' then
11215                                     fnd_file.put_line(fnd_file.log,
11216                                         'Deleted '||SQL%ROWCOUNT||' rows from WIP_PERIOD_BALANCES');
11217                                 end if;
11218                             end if;
11219 
11220 l_stmt_num := 940;
11221                             if v_wlji_status_type(v_idx) = 7 then --cancelled
11222 
11223                                 -- {{ osp begin
11224                                 if wip_osp.po_req_exists (
11225                                         v_wlji_wip_entity_id(v_idx),
11226                                         null,
11227                                         v_wlji_org_id(v_idx),
11228                                         null, 5)
11229                                 then
11230                                     fnd_message.set_name('WIP', 'WIP_CANCEL_JOB/SCHED_OPEN_PO');
11231                                     l_error_msg := fnd_message.get;
11232                                     l_warning_count := l_warning_count + 1;
11233                                     handle_warning(
11234                                         p_err_msg    => l_error_msg,
11235                                         p_header_id  => v_wlji_header_id(v_idx),
11236                                         p_request_id => v_wlji_request_id(v_idx),
11237                                         p_program_id => v_wlji_program_id(v_idx),
11238                                         p_program_application_id => v_wlji_program_appl_id(v_idx));
11239                                 end if;
11240                                 -- }} osp end
11241 l_stmt_num := 950.1;
11242                                 wip_picking_pvt.cancel_allocations(
11243                                         v_wlji_wip_entity_id(v_idx),
11244                                         5,
11245                                         NULL,
11246                                         l_return_status,
11247                                         l_error_msg);
11248 
11249                                 if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11250                                     handle_error(l_return_status, l_error_msg, l_stmt_num);
11251                                     l_error_count := l_error_count + 1;
11252                                     GOTO abort_this_job;
11253                                 else
11254 l_stmt_num := 950.2;
11255                                 --AH Add:SO LBJ changes--
11256 
11257                                 If wsm_reservations_pvt.check_reservation_exists(p_wip_entity_id    => v_wlji_wip_entity_id(v_idx),
11258                                                          p_org_id       => v_wlji_org_id(v_idx),
11259                                                          p_inventory_item_id    => v_wlji_item_id(v_idx)
11260                                                         )
11261                                 then
11262 l_stmt_num := 950.3;
11263                                     wsm_reservations_pvt.modify_reservations_jobupdate (
11264 
11265                                             p_wip_entity_id     => v_wlji_wip_entity_id(v_idx),
11266                                             p_old_net_qty       => round(v_wlji_net_quantity(v_idx),wip_constants.max_displayed_precision),
11267                                             p_new_net_qty       => round(v_wlji_net_quantity(v_idx),wip_constants.max_displayed_precision),
11268                                             p_inventory_item_id     => v_wlji_item_id(v_idx),
11269                                             p_org_id        => v_wlji_org_id(v_idx),
11270                                             p_status_type       => v_wlji_status_type(v_idx),
11271                                             x_return_status     => l_return_status,
11272                                             x_msg_count         => l_error_count,
11273                                             x_msg_data      => l_error_msg
11274                                                                    );
11275                                     if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11276                                         handle_error(l_return_status, l_error_msg, l_stmt_num);
11277                                         l_error_count := l_error_count + 1;
11278                                         GOTO abort_this_job;
11279                                     end if;
11280                                 end if;
11281 
11282                                 --AH Add:SO LBJ changes--
11283 								--Added for Fixing bug 5242298. Txn type 2 adds sector extn for jobs which are cancelled
11284 l_stmt_num := 950.4;
11285 								l_wip_entity_name := WSMPOPRN.update_job_name
11286 									   (p_wip_entity_id   => v_wlji_wip_entity_id(v_idx),
11287                        					p_subinventory    => v_wlji_compl_subinv(v_idx),
11288 										p_org_id          => v_wlji_org_id(v_idx),
11289 										p_txn_type        =>  2,  -- COMPLETION
11290               							p_update_flag     => TRUE,
11291       									p_dup_job_name    => l_dummy_job_name,
11292        									x_error_code      => l_error_code,
11293 										x_error_msg       => l_error_msg);
11294 
11295 								if l_error_code <> 0 then
11296 										l_return_status := FND_API.G_RET_STS_ERROR;
11297                                         handle_error(l_return_status, l_error_msg, l_stmt_num);
11298                                         l_error_count := l_error_count + 1;
11299                                         GOTO abort_this_job;
11300                                 end if;
11301 
11302 								--Added for Fixing bug 5242298
11303 l_stmt_num := 950.5;
11304                                 update wip_discrete_jobs wdj
11305                                     set    status_type = 7
11306                                     where  wdj.wip_entity_id = v_wlji_wip_entity_id(v_idx);
11307                                     if lbji_debug = 'Y' then
11308                                         fnd_file.put_line(fnd_file.log,
11309                                             'Updated status type to 7 in wdj');
11310                                     end if;
11311                                 end if;
11312 
11313 l_stmt_num := 960;
11314                                 --{{ Kanban begin
11315                                 if v_wlji_kanban_card_id(v_idx) is not null then
11316                                     inv_kanban_pvt.update_card_supply_status(
11317                                         x_return_status  => l_return_status,
11318                                         p_kanban_card_id => v_wlji_kanban_card_id(v_idx),
11319                                         p_supply_status  => inv_kanban_pvt.g_supply_status_exception);
11320 
11321                                     if ( l_return_status <> fnd_api.g_ret_sts_success ) then
11322                                         l_error_code := -1;
11323                                         fnd_message.set_name('WSM', 'WSM_KNBN_CARD_STS_FAIL');
11324 l_stmt_num := 960.1;
11325                                         select meaning
11326                                         into   l_meaning
11327                                         from   mfg_lookups
11328                                         where  lookup_type = 'MTL_KANBAN_SUPPLY_STATUS'
11329                                         and    lookup_code = 7
11330                                         and    upper(enabled_flag) = 'Y';
11331 
11332                                         fnd_message.set_token('STATUS',l_meaning);
11333                                         l_error_msg := fnd_message.get;
11334                                         handle_error(l_error_code, l_error_msg,  l_stmt_num);
11335                                         l_error_count := l_error_count + 1;
11336                                         GOTO abort_this_job;
11337                                     end if;
11338 
11339                                     update wip_discrete_jobs
11340                                     set    kanban_card_id = null
11341                                     where  wip_entity_id =  v_wlji_wip_entity_id(v_idx);
11342 
11343                                 end if;
11344                                 --}} Kanban end
11345 
11346                             else  -- v_wlji_status_type(v_idx) <> 7
11347 l_stmt_num := 970.1;
11348                             --AH Add:SO LBJ changes--
11349                             DECLARE
11350                             l_net_qty_old NUMBER;
11351                             BEGIN
11352 
11353                             If wsm_reservations_pvt.check_reservation_exists(p_wip_entity_id    => v_wlji_wip_entity_id(v_idx),
11354                                                      p_org_id       => v_wlji_org_id(v_idx),
11355                                                      p_inventory_item_id    => v_wlji_item_id(v_idx)
11356                                                     )
11357                             then
11358                                 select net_quantity
11359                                 into   l_net_qty_old
11360                                 from   WIP_DISCRETE_JOBS
11361                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
11362 
11363                                 if v_wlji_net_quantity(v_idx) < l_net_qty_old then --net qty has become lesser.So reduce rsvtn
11364 
11365 										wsm_reservations_pvt.modify_reservations_jobupdate (
11366 											p_wip_entity_id     => v_wlji_wip_entity_id(v_idx),
11367 											p_old_net_qty       => l_net_qty_old ,
11368 											p_new_net_qty       => round(v_wlji_net_quantity(v_idx),wip_constants.max_displayed_precision),
11369 											p_inventory_item_id     => v_wlji_item_id(v_idx),
11370 											p_org_id        => v_wlji_org_id(v_idx),
11371 											p_status_type       => v_wlji_status_type(v_idx),
11372 											x_return_status     => l_return_status,
11373 											x_msg_count         => l_error_count,
11374 											x_msg_data      => l_error_msg
11375 														);
11376 										if l_return_status <> FND_API.G_RET_STS_SUCCESS then
11377 											handle_error(l_return_status, l_error_msg, l_stmt_num);
11378 											l_error_count := l_error_count + 1;
11379 											GOTO abort_this_job;
11380 										end if;
11381 								end if;
11382                             end if;
11383 
11384                             END;
11385                             --AH Add:SO LBJ changes--
11386                             UPDATE  WIP_DISCRETE_JOBS WDJ
11387                             set     last_updated_by             = v_wlji_last_updt_by(v_idx),
11388                                     last_update_login           = v_wlji_last_updt_login(v_idx),
11389                                     request_id                  = v_wlji_request_id(v_idx),
11390                                     program_application_id      = v_wlji_program_appl_id(v_idx),
11391                                     program_id                  = v_wlji_program_id(v_idx),
11392                                     program_update_date         = sysdate,
11393                                     last_update_date            = sysdate,
11394                                     bom_reference_id            = v_wlji_bom_ref_id(v_idx),
11395                                     routing_reference_id        = v_wlji_rtg_ref_id(v_idx),
11396                                     common_bom_sequence_id      = l_common_bom_seq_id,
11397                                     common_routing_sequence_id  = l_common_rtg_seq_id,
11398                                     bom_revision                = v_wlji_bom_revision(v_idx),
11399                                     routing_revision            = v_wlji_rtg_revision(v_idx),
11400                                     bom_revision_date           = v_wlji_bom_rev_date(v_idx),
11401                                     routing_revision_date       = v_wlji_rtg_rev_date(v_idx),
11402                                     alternate_bom_designator    = v_wlji_alt_bom(v_idx),
11403                                     alternate_routing_designator= v_wlji_alt_rtg(v_idx),
11404                                     firm_planned_flag           = v_wlji_firm_planned_flag(v_idx),
11405                                     start_quantity              = nvl(round(v_wlji_start_quantity(v_idx),
11406                                                                             wip_constants.max_displayed_precision),
11407                                                                   wdj.start_quantity),
11408                                     net_quantity                = nvl(round(v_wlji_net_quantity(v_idx),
11409                                                                             wip_constants.max_displayed_precision),
11410                                                                   wdj.net_quantity),
11411                                     lot_number                  = decode(wdj.status_type, WIP_CONSTANTS.UNRELEASED,
11412                                                                               v_wlji_job_name(v_idx),wdj.lot_number),   -- fix for bug5990761
11413                                     status_type                 = nvl(v_wlji_status_type(v_idx),wdj.status_type),
11414                                     date_released               = v_wlji_date_released(v_idx), -- bug 2697295
11415                                     scheduled_start_date        = decode(l_txnexist, 0,
11416                                                                          trunc(NVL(v_wlji_fusd(v_idx), wdj.scheduled_start_date),'MI'),
11417                                                                          wdj.scheduled_start_date),
11418                                     scheduled_completion_date   = trunc(NVL(v_wlji_lucd(v_idx),wdj.scheduled_completion_date), 'MI'),
11419                                     completion_locator_id       = v_wlji_compl_loc_id(v_idx),
11420                                     completion_subinventory     = v_wlji_compl_subinv(v_idx),
11421                                     coproducts_supply           = nvl(v_wlji_coproducts_supply(v_idx), wdj.coproducts_supply),
11422                                     -- BA: BUG3272873
11423                                     source_code = nvl(v_wlji_source_code(v_idx),wdj.source_code),
11424                                     source_line_id = nvl(v_wlji_src_line_id(v_idx),wdj.source_line_id),
11425                                     overcompletion_tolerance_type = nvl(v_wlji_overcompl_tol_type(v_idx),
11426                                                         wdj.overcompletion_tolerance_type),
11427                                     overcompletion_tolerance_value = nvl(v_wlji_overcompl_tol_value(v_idx),
11428                                                         wdj.overcompletion_tolerance_value),
11429                                     priority = nvl(v_wlji_priority(v_idx),wdj.priority),
11430                                     due_date = nvl(v_wlji_due_date(v_idx),wdj.due_date),
11431                                     attribute_category = nvl(v_wlji_attribute_category(v_idx),wdj.attribute_category),
11432                                     attribute1 = nvl(v_wlji_attribute1(v_idx),wdj.attribute1),
11433                                     attribute2 = nvl(v_wlji_attribute2(v_idx),wdj.attribute2),
11434                                     attribute3 = nvl(v_wlji_attribute3(v_idx),wdj.attribute3),
11435                                     attribute4 = nvl(v_wlji_attribute4(v_idx),wdj.attribute4),
11436                                     attribute5 = nvl(v_wlji_attribute5(v_idx),wdj.attribute5),
11437                                     attribute6 = nvl(v_wlji_attribute6(v_idx),wdj.attribute6),
11438                                     attribute7 = nvl(v_wlji_attribute7(v_idx),wdj.attribute7),
11439                                     attribute8 = nvl(v_wlji_attribute8(v_idx),wdj.attribute8),
11440                                     attribute9 = nvl(v_wlji_attribute9(v_idx),wdj.attribute9),
11441                                     attribute10 = nvl(v_wlji_attribute10(v_idx),wdj.attribute10),
11442                                     attribute11 = nvl(v_wlji_attribute11(v_idx),wdj.attribute11),
11443                                     attribute12 = nvl(v_wlji_attribute12(v_idx),wdj.attribute12),
11444                                     attribute13 = nvl(v_wlji_attribute13(v_idx),wdj.attribute13),
11445                                     attribute14 = nvl(v_wlji_attribute14(v_idx),wdj.attribute14),
11446                                     attribute15 = nvl(v_wlji_attribute15(v_idx),wdj.attribute15),
11447                                     -- EA: BUG3272873
11448                                     est_scrap_account           = nvl(l_est_scrap_account, wdj.est_scrap_account),
11449                                     est_scrap_var_account       = nvl(l_est_scrap_var_account, wdj.est_scrap_var_account),
11450                                     description                 = nvl(RTRIM(v_wlji_description(v_idx)), wdj.description),
11451                                     schedule_group_id           = decode(v_wlji_schedule_group_id(v_idx),fnd_api.g_miss_num,null,v_wlji_schedule_group_id(v_idx))  -- Bug : 6676148
11452                                     -- CZHDBG: how about fusd and lucd ???
11453                             where   wdj.wip_entity_id     = v_wlji_wip_entity_id(v_idx);
11454 
11455                             end if; -- update jobs for which status is not cancelled
11456                             if lbji_debug = 'Y' then
11457                                 fnd_file.put_line(fnd_file.log,
11458                                     'Updated '||SQL%ROWCOUNT||' rows of wdj');
11459                             end if;
11460 l_stmt_num := 970.2;
11461                             UPDATE WIP_ENTITIES WE
11462                             set    description            = nvl(v_wlji_description(v_idx), we.description),
11463 			           wip_entity_name        = decode(nvl(v_wlji_status_type(v_idx),-1),7,wip_entity_name,
11464                                                                        decode(l_cur_status_type,1, v_wlji_job_name(v_idx),wip_entity_name)), --fix for bug5990761
11465                                    last_updated_by        = v_wlji_last_updt_by(v_idx),
11466                                    last_update_login      = v_wlji_last_updt_login(v_idx),
11467                                    request_id             = v_wlji_request_id(v_idx),
11468                                    program_application_id = v_wlji_program_appl_id(v_idx),
11469                                    program_id             = v_wlji_program_id(v_idx),
11470                                    program_update_date    = v_wlji_prog_updt_date(v_idx),
11471                                    last_update_date       = v_wlji_last_updt_date(v_idx)
11472                             where  we.wip_entity_id       = v_wlji_wip_entity_id(v_idx);
11473                             if lbji_debug = 'Y' then
11474                                 fnd_file.put_line(fnd_file.log,
11475                                     'Updated '||SQL%ROWCOUNT||' rows into we');
11476                             end if;
11477 
11478                         Exception
11479                            when others then
11480                            l_error_code := SQLCODE;
11481                            l_error_msg  :=
11482                                 'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
11483                                ||SUBSTR(SQLERRM, 1,1000);
11484                            handle_error(l_error_code, l_error_msg,  l_stmt_num);
11485                            l_error_count := l_error_count + 1;
11486                            GOTO abort_this_job;
11487                         End;
11488 
11489                         ---------------------------------------------
11490                         -- Mark recommendations on the job level copy
11491                         ---------------------------------------------
11492                         -- this is for update only
11493                         if(v_wlji_num_of_children(v_idx) > 0) then
11494 l_stmt_num := 980;
11495                             import_lot_job_details (
11496                                 p_wip_entity_id             => v_wlji_wip_entity_id(v_idx),
11497                                 p_org_id                    => v_wlji_org_id(v_idx),
11498                                 p_wo_records_exist          => 'Y',            -- p_wo_records_exist
11499                                 p_parent_header_id          => v_wlji_header_id(v_idx),
11500                                 p_job_scheduled_start_date  => null,           -- p_job_scheduled_start_date
11501                                 p_job_scheduled_compl_date  => null,           -- p_job_scheduled_compl_date
11502                                 p_job_scheduled_quantity    => null,
11503                                 p_group_id                  => null,
11504                                 p_last_updt_date            => v_wlji_last_updt_date(v_idx),
11505                                 p_last_updt_by              => v_wlji_last_updt_by(v_idx),
11506                                 p_last_updt_login           => v_wlji_last_updt_login(v_idx),
11507                                 p_creation_date             => v_wlji_creation_date(v_idx),
11508                                 p_created_by                => v_wlji_created_by(v_idx),
11509                                 p_request_id                => v_wlji_request_id(v_idx),
11510                                 p_program_appl_id           => v_wlji_program_appl_id(v_idx),
11511                                 p_program_id                => v_wlji_program_id(v_idx),
11512                                 p_prog_updt_date            => v_wlji_prog_updt_date(v_idx),
11513                                 p_error_code                => l_error_code,
11514                                 p_error_msg                 => l_error_msg);
11515                             if (l_error_code <> 0) then
11516                                 handle_error(l_error_code, l_error_msg,  l_stmt_num);
11517                                 l_error_count := l_error_count + 1;
11518                                 GOTO abort_this_job;
11519                             end if;
11520                         end if;
11521 
11522 l_stmt_num := 985;
11523                         -- BA 3479666
11524                         -- OSFM will call infinite scheduler if scheduling_method = 1
11525                         -- and on_rec_path = Yes
11526 
11527                         if(v_wlji_scheduling_method(v_idx) = 1 ) then
11528                             DECLARE
11529                                 l_returnstatus      varchar2(10);
11530                                 l_on_reco_path      varchar2(10);
11531                                 l_start_date        date := null;
11532                                 l_end_date          date := null;
11533 
11534                             BEGIN
11535                                 select on_rec_path
11536                                 into   l_on_reco_path
11537                                 from   wsm_lot_based_jobs
11538                                 where  wip_entity_id = v_wlji_wip_entity_id(v_idx);
11539 l_stmt_num := 985.1;
11540                                 if(l_on_reco_path = 'Y' ) then
11541                                     if(l_scheduling_mode = WIP_CONSTANTS.FORWARDS) then
11542                                         l_start_date := v_wlji_fusd(v_idx);
11543                                         l_end_date   := null;
11544                                     else
11545                                         l_start_date := null;
11546                                         l_end_date   := v_wlji_lucd(v_idx);
11547                                     end if;
11548 
11549                                     wsm_infinite_scheduler_pvt.schedule (
11550                                             p_initmsglist   => fnd_api.g_true,
11551                                             p_enddebug      => fnd_api.g_true,
11552                                             p_orgID         => v_wlji_org_id(v_idx),
11553                                             p_wipEntityID   => v_wlji_wip_entity_id(v_idx),
11554                                             p_scheduleMode  => l_scheduling_mode,
11555                                             p_startDate     => l_start_date,
11556                                             p_endDate       => l_end_date,
11557                                             p_opSeqNum      => null,
11558                                             p_resSeqNum     => null,
11559                                             x_returnstatus  => l_returnstatus,
11560                                             x_errorMsg      => l_error_msg
11561                                     );
11562                                     if(l_returnstatus <> fnd_api.g_ret_sts_success) then
11563                                         handle_error(-1, l_error_msg,  l_stmt_num);
11564                                         l_error_count := l_error_count + 1;
11565                                         goto abort_this_job;
11566                                     end if;
11567                                 end if;
11568                             END;
11569                         end if;
11570                         -- EA 3479666
11571 
11572                     end if; -- load_type 6
11573                     -- ----------------------------------
11574                     -- }}} End: Write for job update
11575                     -- ----------------------------------
11576 
11577 l_stmt_num := 990;
11578                     -- {{ Releasing job, insert period balance
11579                     if (v_wlji_load_type(v_idx) = 6 AND
11580                         v_wlji_status_type(v_idx) = WIP_CONSTANTS.RELEASED and
11581                         l_cur_status_type = WIP_CONSTANTS.UNRELEASED)
11582                     then
11583 
11584                         --
11585                         -- Begin R12Dev Update to set the current_job/rtg_op_seq_num on WLBJ
11586                         --Bug 4748165
11587                       /*  update wsm_lot_based_jobs
11588                         set    (current_job_op_seq_num,
11589                                 current_rtg_op_seq_num)
11590                                =(select wo.operation_seq_num,
11591                                          wo.wsm_op_seq_num
11592                                   from   wip_operations wo
11593                                   where  organization_id = v_wlji_org_id(v_idx)
11594                                   and    wip_entity_id   = v_wlji_wip_entity_id(v_idx)
11595                                   and    wo.quantity_in_queue
11596                                        + wo.quantity_running
11597                                        + wo.quantity_waiting_to_move  > 0)
11598                         where  wip_entity_id   = v_wlji_wip_entity_id(v_idx)
11599                         and    organization_id = v_wlji_org_id(v_idx); */
11600                         --Commented out above query since WSMPVCPY updates both. Bug 4748165
11601                         -- End R12Dev
11602                         --
11603 
11604 
11605                         if ((v_wlji_load_type(v_idx) = 6) AND (v_wlji_class_code(v_idx) is NULL)) then
11606                             v_wlji_class_code(v_idx) := l_cur_class_code;
11607                         end if;
11608                         fnd_file.put_line(fnd_file.log, 'Date Released: '||v_wlji_date_released(v_idx));
11609                         insert_into_period_balances (
11610                             p_wip_entity_id   => v_wlji_wip_entity_id(v_idx),
11611                             p_organization_id => v_wlji_org_id(v_idx),
11612                             p_class_code      => v_wlji_class_code(v_idx),
11613                             p_release_date    => v_wlji_date_released(v_idx),
11614                             p_error_code      => l_error_code,
11615                             p_err_msg         => l_error_msg);
11616 
11617                         if l_error_code <> 0 then
11618                             handle_error(l_error_code, l_error_msg,  l_stmt_num);
11619                             l_error_count := l_error_count + 1;
11620                             GOTO abort_this_job;
11621                         end if;
11622                     end if;
11623                     -- }} Releasing job, insert period balance
11624 
11625             /* LotAttr */
11626                     if l_cur_status_type in (0,1,3,6) THEN  -- Initialized Value,
11627                                         --Unreleased, released, Hold
11628 l_stmt_num := 995;
11629                         IF (lbji_debug='Y') THEN
11630                             fnd_file.put_line(fnd_file.log, 'Before Calling WSM_LotAttr_PVT.create_update_lotattr');
11631                         END IF;
11632                         WSM_LotAttr_PVT.create_update_lotattr(
11633                                 x_err_code          => l_error_code,
11634                                 x_err_msg           => l_error_msg,
11635                                 p_wip_entity_id     => v_wlji_wip_entity_id(v_idx),
11636                                 p_org_id            => v_wlji_org_id(v_idx),
11637                                 p_intf_txn_id       => v_wlji_header_id(v_idx),
11638                                 p_intf_src_code     => 'WSM',
11639                                 p_src_lot_number    => l_src_lot_number,
11640                                 p_src_inv_item_id   => l_src_inv_item_id);
11641                         -- Added for Bug 13833100. Ignore Mandatory Lot Attributes for Jobs created by ASCP.
11642                         IF (l_error_code <> 0 and v_wlji_source_code(v_idx) <> 'MSC' ) THEN
11643                             handle_error(l_error_code, l_error_msg,  l_stmt_num);
11644                             l_error_count := l_error_count + 1;
11645                             GOTO abort_this_job;
11646                         END IF;
11647                         IF (lbji_debug='Y') THEN
11648                            fnd_file.put_line(fnd_file.log, 'WSM_LotAttr_PVT.create_update_lotattr returned Success' || l_error_code);
11649                         END IF;
11650 
11651                     end if; -- l_cur_status_type in (1,3,6)
11652 
11653             -- Invoke the serial code...
11654             -- ST : Serial Support Project ...........................---------------------------------------------------------------
11655             DECLARE
11656 
11657             l_msg_count NUMBER := 0;
11658             l_return_status VARCHAR2(1);
11659             l_err_msg       VARCHAR2(2000);
11660             l_log_level     NUMBER := FND_LOG.G_CURRENT_RUNTIME_LEVEL;
11661             l_param_tbl WSM_Log_PVT.param_tbl_type;
11662             BEGIN
11663 
11664             IF (lbji_debug='Y') THEN
11665                 fnd_file.put_line(fnd_file.log, 'Before WSM_Serial_support_PVT.LBJ_serial_intf_proc');
11666             END IF;
11667             l_stmt_num := 995.1;
11668 
11669             WSM_Serial_support_PVT.LBJ_serial_intf_proc( p_header_id         => v_wlji_header_id(v_idx),
11670                                      p_wip_entity_id         => v_wlji_wip_entity_id(v_idx),
11671                                      p_organization_id           => v_wlji_org_id(v_idx),
11672                                      p_inventory_item_id     => v_wlji_item_id(v_idx),
11673                                      x_return_status         => l_return_status,
11674                                      x_error_count       => l_msg_count,
11675                                      x_error_msg             => l_err_msg
11676                                     );
11677             l_stmt_num := 995.2;
11678 
11679             if l_return_status = FND_API.G_RET_STS_SUCCESS then
11680                 IF (lbji_debug='Y') THEN
11681                     fnd_file.put_line(fnd_file.log, 'WSM_Serial_support_PVT.LBJ_serial_intf_proc returned Success');
11682                 END IF;
11683                 ELSE
11684                 IF (lbji_debug='Y') THEN
11685                     fnd_file.put_line(fnd_file.log, 'WSM_Serial_support_PVT.LBJ_serial_intf_proc returned failure');
11686                 END IF;
11687 
11688                 IF (l_msg_count = 1)  THEN
11689                     handle_error(-1, l_err_msg,  l_stmt_num);
11690                 ELSIF (l_msg_count > 1)  THEN
11691                     FOR i IN 1..l_msg_count LOOP
11692                     l_err_msg := fnd_msg_pub.get( p_msg_index => l_msg_count - i + 1,
11693                                           p_encoded   => FND_API.G_FALSE
11694                                      );
11695                     handle_error(-1, l_err_msg,  l_stmt_num);
11696                     END LOOP;
11697                 ELSE
11698                     l_err_msg := 'WSM_Serial_support_PVT.LBJ_serial_intf_proc returned failure';
11699                     handle_error(-1, l_err_msg,  l_stmt_num);
11700                 END IF;
11701                 l_error_count := l_error_count + 1;
11702                 GOTO abort_this_job;
11703             END IF;
11704 
11705            END;
11706            --------------------------------------------------------------------------------------------------------------------
11707 
11708 -- ==============================================================================================
11709 -- END: WRITING INTO BASE TABLES }}}
11710 -- ==============================================================================================
11711 
11712 l_stmt_num := 1000;
11713                     ---------------------------------------------------
11714                     -- {{{ Begin: material transaction for mode 2 jobs
11715                     ---------------------------------------------------
11716                     if v_wlji_mode_flag(v_idx) = 2 then
11717                         IF WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_ORG_ACC_PERIODS is null OR
11718                            WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_ORG_ACC_PERIODS = 0
11719                         then
11720                             l_error_code := -1;
11721                             handle_errmsg_name('WIP', 'WIP_NO_ACCT_PERIOD', l_stmt_num);
11722                             GOTO skip_mat_trans;
11723                         end if;
11724                         if WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_STK_LOC_CNTRL is null then
11725                             WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_STK_LOC_CNTRL := 1;
11726                         end if;
11727 
11728 l_stmt_num := 1010;
11729                         Begin  -- material transaction
11730                             insert into mtl_material_transactions_temp(
11731                                 last_update_date,
11732                                 creation_date,
11733                                 last_updated_by,
11734                                 created_by,
11735                                 last_update_login,
11736                                 transaction_header_id,
11737                                 transaction_source_id,
11738                                 inventory_item_id,
11739                                 organization_id,
11740                                 revision,
11741                                 subinventory_code,
11742                                 locator_id,
11743                                 transaction_quantity,
11744                                 primary_quantity,
11745                                 transaction_uom,
11746                                 transaction_type_id,
11747                                 transaction_action_id,
11748                                 transaction_source_type_id,
11749                                 transaction_date,
11750                                 acct_period_id,
11751                                 source_code,
11752                                 source_line_id,
11753                                 wip_entity_type,
11754                                 negative_req_flag,
11755                                 operation_seq_num,
11756                                 wip_supply_type,
11757                                 wip_commit_flag,
11758                                 process_flag,
11759                                 posting_flag,
11760                                 transaction_temp_id
11761                             ) values (
11762                                 v_wlji_last_updt_date(v_idx),
11763                                 v_wlji_creation_date(v_idx),
11764                                 v_wlji_last_updt_by(v_idx),
11765                                 v_wlji_created_by(v_idx),
11766                                 v_wlji_last_updt_login(v_idx),
11767                                 l_mmtt_txn_hdr_id,
11768                                 v_wlji_wip_entity_id(v_idx),
11769                                 v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id,
11770                                 v_wlji_org_id(v_idx),
11771                                 v_wsli(v_wlji_src_line_id(v_idx)).revision,
11772                                 v_wsli(v_wlji_src_line_id(v_idx)).subinventory_code,
11773                                 v_wsli(v_wlji_src_line_id(v_idx)).locator_id,
11774                                 -l_quantity_tobe_issued,
11775                                 -l_quantity_tobe_issued,
11776                                 v_wsli(v_wlji_src_line_id(v_idx)).primary_uom_code,
11777                                 35,       -- Transaction_Type_Id
11778                                 1,        -- Transaction_Action_Id
11779                                 5,        -- Transaction_Source_Type_Id
11780                                 SYSDATE,  -- Transaction_Date
11781                                 WSMPLCVA.v_org(v_wlji_org_id(v_idx)).MAX_ORG_ACC_PERIODS,
11782                                 'WSM',    -- Source_Code
11783                                 to_char(v_wlji_src_line_id(v_idx)),
11784                                 5,        -- Wip_Entity_Type
11785                                 1,        -- Neg Req Flag
11786                                 10,       -- Op Seq
11787                                 '',       -- Supply Type
11788                                 'N',      -- Wip_Commit_Flag
11789                                 'Y',      -- Process_Flag
11790                                 'Y',      -- Posting_Flag
11791                                 mtl_material_transactions_s.nextval
11792                             ) RETURNING transaction_temp_id into l_txn_tmp_hdr_id;
11793                             if lbji_debug = 'Y' then
11794                                 fnd_file.put_line(fnd_file.log,
11795                                     'Inserted '||SQL%ROWCOUNT||' rows into mmtt');
11796                             end if;
11797 
11798 l_stmt_num := 1020;
11799                             INSERT INTO MTL_TRANSACTION_LOTS_TEMP (
11800                                 transaction_temp_id,
11801                                 last_update_date,
11802                                 creation_date,
11803                                 last_updated_by,
11804                                 created_by,
11805                                 last_update_login,
11806                                 transaction_quantity,
11807                                 primary_quantity,
11808                                 lot_number
11809                             ) values (
11810                                 l_txn_tmp_hdr_id,
11811                                 v_wlji_last_updt_date(v_idx),
11812                                 v_wlji_creation_date(v_idx),
11813                                 v_wlji_last_updt_by(v_idx),
11814                                 v_wlji_created_by(v_idx),
11815                                 v_wlji_last_updt_login(v_idx),
11816                                 -l_quantity_tobe_issued,
11817                                 -l_quantity_tobe_issued,
11818                                 v_wsli(v_wlji_src_line_id(v_idx)).lot_number);
11819                             if lbji_debug = 'Y' then
11820                                 fnd_file.put_line(fnd_file.log,
11821                                     'Inserted '||SQL%ROWCOUNT||' rows into mtlt');
11822                             end if;
11823 
11824 l_stmt_num := 1030;
11825                             UPDATE WIP_REQUIREMENT_OPERATIONS
11826                             set    wip_supply_type = 1
11827                             where  wip_entity_id = v_wlji_wip_entity_id(v_idx)
11828                             and    operation_seq_num = 10
11829                             and    inventory_item_id = v_wsli(v_wlji_src_line_id(v_idx)).inventory_item_id;
11830 
11831                         Exception  -- material transaction
11832                             when others then
11833                                 l_error_code := SQLCODE;
11834                                 l_error_msg  :=
11835                                     'WSM_LBJ_INTERFACE_PVT.launch_worker: stmt# '||l_stmt_num||' '
11836                                    ||SUBSTR(SQLERRM, 1,1000);
11837                                 handle_error(l_error_code, l_error_msg,  l_stmt_num);
11838                                 GOTO skip_mat_trans;
11839                         End; -- material transaction
11840 
11841 << skip_mat_trans >>
11842 
11843                         if l_error_code <> 0 then
11844                             l_error_count := l_error_count + 1;
11845                             GOTO abort_this_job;
11846                         end if;
11847 
11848                         l_atleast_one_row_in_mmtt := l_atleast_one_row_in_mmtt + 1;
11849 
11850                     end if; -- for mode 2
11851                     ---------------------------------------------------
11852                     -- }}} End: material transaction for mode 2 jobs
11853                     ---------------------------------------------------
11854 
11855                 EXCEPTION
11856                     when others then
11857                         l_error_code := SQLCODE;
11858                         l_error_msg  :=
11859                             'WSM_LBJ_INTERFACE_PVT.launch_worker (stmt# '||l_stmt_num||') '
11860                            ||SUBSTR(SQLERRM, 1,1000);
11861                         handle_error(l_error_code, l_error_msg,  l_stmt_num);
11862                         l_error_count := l_error_count + 1;
11863                 END; -- }}} main block
11864 
11865 
11866 <<abort_this_job>>
11867 
11868 
11869 l_stmt_num := 1040;
11870                 -- {{ write into output file
11871                 -- note that this is a rudimentary piece of commentary on the job created,
11872                 -- or failed to create because there's no customer requirement on this.
11873                 if v_wlji_load_type(v_idx) = 5 then
11874 l_stmt_num := 1040.1;
11875 	-- changed from org_organization_definitions to MTL_PARAMETERS for bug 5051885
11876 	-- Performance issue on org_organization_definitions for full table scan on base tables
11877 	-- SQL id 16641913
11878 
11879                     select organization_code
11880                     into   l_org_code
11881                     from   MTL_PARAMETERS
11882                     where  organization_id = v_wlji_org_id(v_idx);
11883     -- bug 5051885
11884 l_stmt_num := 1040.2;
11885                     select meaning
11886                     into   l_job_type_code
11887                     from   mfg_lookups
11888                     where  lookup_type = 'WIP_DISCRETE_JOB'
11889                     and    lookup_code = v_wlji_job_type(v_idx);
11890 
11891 l_stmt_num := 1040.3;
11892                     begin
11893                         select unique(concatenated_segments)
11894                         into   l_assembly_name
11895                         from   mtl_system_items_kfv
11896                         where  inventory_item_id = v_wlji_item_id(v_idx)
11897                         and    organization_id = v_wlji_org_id(v_idx);
11898                     exception
11899                         when others then
11900                             l_assembly_name := 'Unknown';
11901                     end;
11902                     fnd_file.put_line(fnd_file.output, '-------------------------------------------------------');
11903                     fnd_file.put_line(fnd_file.output, '                 Lot Based Job Creation                ');
11904                     fnd_file.put_line(fnd_file.output, '-------------------------------------------------------');
11905                     fnd_file.put_line(fnd_file.output, '   Organization: ' || l_org_code);
11906                     fnd_file.put_line(fnd_file.output, '       Job_name: ' || v_wlji_job_name(v_idx));
11907                     fnd_file.put_line(fnd_file.output, '       Job Type: ' || l_job_type_code);
11908                     fnd_file.put_line(fnd_file.output, '       Assembly: ' || l_assembly_name);
11909                     fnd_file.put_line(fnd_file.output, '       Quantity: ' || v_wlji_start_quantity(v_idx));
11910                     fnd_file.put_line(fnd_file.output, '     Start Date: ' || v_wlji_fusd(v_idx));
11911                     fnd_file.put_line(fnd_file.output, 'Completion Date: ' || v_wlji_lucd(v_idx));
11912                     fnd_file.put_line(fnd_file.output, '    Kanban Card: ' || v_wlji_kanban_card_id(v_idx));
11913                     if v_wlji_process_status(v_idx) <> 3 then
11914                         fnd_file.put_line(fnd_file.output, 'Process Status: Success');
11915                     else
11916                         fnd_file.put_line(fnd_file.output, 'Process Status: Falied To Create');
11917                     end if;
11918                     if v_wlji_process_status(v_idx) = 3 then
11919                         fnd_file.put_line(fnd_file.output, 'Error Message: ' || v_wlji_err_msg(v_idx));
11920                     end if;
11921                     fnd_file.put_line(fnd_file.output, '-------------------------------------------------------');
11922                 end if; -- load type 5
11923                 -- }} write into output file
11924 
11925 
11926                 -- mark the rows without error to be deleted
11927                 if v_wlji_err_code(v_idx) <> 0 then
11928                     rollback to savepoint_skip_job;
11929                 end if;
11930 
11931 l_stmt_num := 1050;
11932                 if v_wlji_process_status(v_idx) <> 3 then
11933                     v_wlji_process_status(v_idx) := 5; -- 5 : complete without error
11934                     if lbji_debug = 'Y' then
11935                         fnd_file.put_line(fnd_file.log,
11936                             'Everything OK, changing the status of the row to 5..');
11937                     end if;
11938                 else
11939                     -- {{ kanban begin
11940                     if v_wlji_kanban_card_id(v_idx) is not null then
11941                         inv_kanban_pvt.Update_Card_Supply_Status(
11942                                 x_return_status  => l_return_status,
11943                                 p_Kanban_Card_Id => v_wlji_kanban_card_id(v_idx),
11944                                 p_Supply_Status  => inv_kanban_pvt.g_supply_status_Exception);
11945 
11946                         if ( l_return_status <> fnd_api.g_ret_sts_success ) then
11947                             l_error_code := -1;
11948                             fnd_message.set_name('WSM', 'WSM_KNBN_CARD_STS_FAIL');
11949                             select meaning
11950                             into   l_meaning
11951                             from   mfg_lookups
11952                             where  lookup_type = 'MTL_KANBAN_SUPPLY_STATUS'
11953                             and    lookup_code = 7
11954                             and    upper(enabled_flag) = 'Y';
11955 
11956                             fnd_message.set_token('STATUS',l_meaning);
11957                             l_error_msg := fnd_message.get;
11958                             handle_error(l_error_code, l_error_msg,  l_stmt_num);
11959                             l_error_count := l_error_count + 1;
11960                         end if;
11961 
11962                         update wip_discrete_jobs
11963                         set    kanban_card_id = null
11964                         where  wip_entity_id =  v_wlji_wip_entity_id(v_idx);
11965 
11966                     end if;
11967                     -- }} kanban end
11968 
11969                     dummy_err_code := 0;
11970                     dummy_err_msg := NULL;
11971                     wsmputil.WRITE_TO_WIE (
11972                         v_wlji_header_id(v_idx),
11973                         substr(v_wlji_err_msg(v_idx),1,2000),
11974                         v_wlji_request_id(v_idx),
11975                         v_wlji_program_id(v_idx),
11976                         v_wlji_program_appl_id(v_idx),
11977                         1,
11978                         dummy_err_code,
11979                         dummy_err_msg );
11980 
11981                     if dummy_err_code <> 0 then
11982                         fnd_file.put_line(fnd_file.log, 'WARNING ');
11983                         fnd_file.put_line(fnd_file.log,
11984                             'WSM_LBJ_INTERFACE_PVT.launch_worker: '||dummy_err_msg);
11985                         l_error_count := l_error_count + 1;
11986                     end if;
11987                 end if;
11988 
11989                 v_idx := v_wlji_header_id.next(v_idx);
11990 
11991             end loop; -- }}} inner loop
11992 
11993 -- ==============================================================================================
11994 -- END: Processing of data }}}
11995 -- ==============================================================================================
11996 
11997             -- RETCODE return values
11998             --      0: success
11999             --      1: success with warning
12000             --      2: error
12001             -- RETCODE return values
12002 
12003             if l_warning_count <> 0 then
12004                 retcode := 1;
12005                 errbuf := 'The interface process produced at least one warning message';
12006                 fnd_file.put_line(fnd_file.log,errbuf);
12007                 conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12008             end if;
12009 
12010             if l_error_count <> 0 then
12011                 retcode := 1;
12012                 errbuf := 'The interface process marked at least one row as errored';
12013                 fnd_file.put_line(fnd_file.log,errbuf);
12014                 conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12015             end if;
12016 
12017 l_stmt_num := 1060;
12018             -- bulk update wsm_lot_job_interface
12019             forall i in v_wlji_process_status.first..v_wlji_process_status.last
12020             update  wsm_lot_job_interface
12021             set     process_status = v_wlji_process_status(i),
12022                     error_code     = v_wlji_err_code(i),
12023                     error_msg      = v_wlji_err_msg(i),
12024                     request_id     = v_wlji_request_id(i),
12025                     program_id     = v_wlji_program_id(i),
12026                     program_application_id = v_wlji_program_appl_id(i)
12027             where   header_id = v_wlji_header_id(i);
12028 
12029         -- ST : Serial Support Project ...........................----------------------------------------------
12030         -- bulk update WSM_SERIAL_TXN_INTERFACE
12031         forall i in v_wlji_process_status.first..v_wlji_process_status.last
12032             update  WSM_SERIAL_TXN_INTERFACE
12033             set     process_status = v_wlji_process_status(i),
12034                     error_message  = v_wlji_err_msg(i),
12035                     request_id     = v_wlji_request_id(i),
12036                     program_id     = v_wlji_program_id(i),
12037                     program_application_id = v_wlji_program_appl_id(i)
12038             where   header_id = v_wlji_header_id(i);
12039         -- ST : Serial Support Project ...........................----------------------------------------------
12040 
12041 l_stmt_num := 1060.1;
12042 
12043             forall i in v_wlji_process_status.first..v_wlji_process_status.last
12044             update  wsm_lot_job_dtl_interface
12045             set     process_status = v_wlji_process_status(i),
12046                     error_code     = v_wlji_err_code(i),
12047                     error_msg      = decode(v_wlji_process_status(i),   -- BUG3600450
12048                                             3, 'See header (header_id=' ||
12049                                                   v_wlji_header_id(i) || ') for error message',
12050                                             null),
12051                     request_id     = v_wlji_request_id(i),
12052                     program_id     = v_wlji_program_id(i),
12053                     program_application_id = v_wlji_program_appl_id(i)
12054             where   parent_header_id = v_wlji_header_id(i)
12055             and     v_wlji_num_of_children(i) > 0;
12056 
12057 
12058 l_stmt_num := 1070;
12059             -- {{ launch INV worker
12060             if l_atleast_one_row_in_mmtt <>0 THEN
12061                 fnd_file.put_line(fnd_file.log,
12062                     'Invoking Inventory API with header id: '||to_char(l_mmtt_txn_hdr_id));
12063                 --OPTII-PERF:Call Inv API to reduce the waiting time.
12064                 /*
12065                 l_inv_worker_req_id := FND_REQUEST.submit_request (
12066                                     'INV', 'INCTCW', NULL, NULL, FALSE,
12067                                     to_char(l_mmtt_txn_hdr_id),
12068                                     --'1', NULL, NULL); -- bug 3733798
12069                     '4', NULL, NULL);   -- bug 3733798
12070 
12071                 COMMIT; -- CZHDBG: why?
12072 
12073                 fnd_file.put_line(fnd_file.log,
12074                     'Material Transaction temp_header_id is '||to_char(l_mmtt_txn_hdr_id));
12075 
12076                 if l_inv_worker_req_id = 0 then
12077 l_stmt_num := 1080;
12078                     retcode := 1;
12079                     errbuf:= 'Inventory worker returned failure '||
12080                              '(Transaction_header_id=' ||l_mmtt_txn_hdr_id||') : '
12081                              || SUBSTRB(SQLERRM,1,1000);
12082                     fnd_file.put_line(fnd_file.log,errbuf);
12083                     conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12084                     update  wsm_lot_job_interface
12085                     set     process_status = 4,
12086                             error_code = -2,
12087                             error_msg = l_error_msg
12088                     where   mode_flag = 2;
12089 
12090                 else -- req_id <> 0
12091 l_stmt_num := 1090;
12092                     fnd_file.put_line(fnd_file.log,'Inventory Transaction Worker request_id is '
12093                             ||to_char(l_inv_worker_req_id));
12094                     req_wait := FND_CONCURRENT.WAIT_FOR_REQUEST (
12095                                 request_id => l_inv_worker_req_id,
12096                                 interval   => 10, -- 10 seconds interval
12097                                 max_wait   => 36000, -- 10 Hours maximum wait.
12098                                 phase      => req_phase,
12099                                 status     => req_status,
12100                                 dev_phase  => req_devphase,
12101                                 dev_status => req_devstatus,
12102                                 message    => req_message);
12103 
12104                     fnd_file.put_line(fnd_file.log,
12105                         'Inventory Transaction Worker status is '||req_status);
12106                     fnd_file.put_line(fnd_file.log,
12107                         'Inventory Transaction Worker Completion Message: '||req_message);
12108 
12109                     if  req_devphase <> 'COMPLETE' OR  req_devstatus <> 'NORMAL' THEN
12110                         retcode := 1;
12111                         errbuf:= 'Inventory worker returned failure '||
12112                                  '(Transaction_header_id=' ||l_mmtt_txn_hdr_id||') : '||
12113                                  SUBSTRB(SQLERRM,1,1000);
12114                         fnd_file.put_line(fnd_file.log,errbuf);
12115                         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12116                     end if;
12117 
12118                     check_errored_mmtt_records(l_mmtt_txn_hdr_id, l_error_code, l_error_msg);
12119 
12120                     if (l_error_code <> 0) or (l_error_msg is not null ) then
12121                         retcode := 1;
12122                         errbuf:= 'Errored Records in mmtt ' ||
12123                                  '(Transaction_header_id=' ||l_mmtt_txn_hdr_id||') : '||
12124                                  SUBSTRB(SQLERRM,1,1000);
12125                         fnd_file.put_line(fnd_file.log,errbuf);
12126                         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12127                     end if;
12128 
12129                 end if;  -- req_id
12130                 */
12131                 retcode := INV_LPN_TRX_PUB.PROCESS_LPN_TRX(
12132                                      p_trx_hdr_id =>l_mmtt_txn_hdr_id,
12133                                      p_commit => 'F',
12134                                      x_proc_msg => req_message,
12135                                      p_proc_mode => 1,
12136                                      p_process_trx => 'T',
12137                                      p_atomic => 'T',
12138                                      p_business_flow_code =>NULL);
12139                 IF retcode <> 0 THEN
12140                    declare
12141                        x_msgcnt NUMBER;
12142                        x_msg_data VARCHAR2(256);
12143                    begin
12144 
12145                      FND_MSG_PUB.Count_And_Get(
12146                         p_encoded => 'T',
12147                         p_count => x_msgcnt,
12148                         p_data  => x_msg_data);
12149 
12150                        IF x_msgcnt > 0 THEN
12151                              -- There are messages in the stack so display them
12152                              for  x in 1..x_msgcnt LOOP
12153                                x_msg_data  := fnd_msg_pub.get(x, 'F');
12154                                fnd_file.put_line(fnd_file.log,'Inventory API Error at statement number ' || l_stmt_num || ' : ' || x_msg_data);
12155                              end loop;
12156                        ELSE
12157                            fnd_message.set_name(
12158                                                     application => 'INV',
12159                                                     name        => 'INV_PROCESS_FAILED');
12160                            fnd_message.set_token(
12161                                                     token    => 'PROCESS',
12162                                                     value    => 'TRXNS',
12163                                                     translate => TRUE);
12164                            x_msg_data := fnd_message.get;
12165                            fnd_file.put_line(fnd_file.log,'Inventory API Error at statement number ' || l_stmt_num || ' : ' || x_msg_data);
12166                        END IF;
12167                    end;
12168 
12169                 END IF;--End of check on retcode
12170 
12171 
12172             end if; -- l_atleast_one_row_in_mmtt
12173             -- }} launch INV worker
12174 
12175             -- delete marked rows (without error rows) from wlji and wljdi
12176             Begin
12177                 -- bug 3126758
12178                 l_del_int_prof_value := fnd_profile.value('WSM_INTERFACE_HISTORY_DAYS');
12179 
12180                 if l_atleast_one_row_in_mmtt <> 0 then
12181 l_stmt_num := 1100;
12182                     delete from wsm_starting_lots_interface
12183                     where  header_id IN (
12184                             select  wlji.source_line_id
12185                             from    wsm_lot_job_interface wlji
12186                             where   wlji.process_status = 5
12187                             --and     wlji.group_id = l_batch_grp_id
12188                             and     NVL(transaction_date, creation_date)
12189                                         <= decode(l_del_int_prof_value,
12190                                                   null,
12191                                                   NVL(transaction_date, creation_date) -1,
12192                                                   SYSDATE-l_del_int_prof_value)
12193                            );
12194                     if lbji_debug = 'Y' then
12195                         fnd_file.put_line(fnd_file.log,
12196                             'Deleted '||SQL%ROWCOUNT||' rows from wsli');
12197                     end if;
12198                 end if;
12199 
12200 l_stmt_num := 1100.1;
12201                 delete from wsm_lot_job_dtl_interface
12202                 where  parent_header_id IN (
12203                         select  wlji.header_id
12204                         from    wsm_lot_job_interface wlji
12205                         where   wlji.process_status = 5
12206                         --and     wlji.group_id = l_batch_grp_id
12207                         and     NVL(transaction_date, creation_date)
12208                                     <= decode(l_del_int_prof_value,
12209                                               null,
12210                                               NVL(transaction_date, creation_date) -1,
12211                                               SYSDATE-l_del_int_prof_value)
12212                        );
12213 l_stmt_num := 1100.2;
12214 
12215         -- ST : Serial Support Project ...........................----------------------------------------------
12216         -- Delete successful rows from WSM_SERIAL_TXN_INTERFACE
12217         delete from WSM_SERIAL_TXN_INTERFACE
12218         where header_id IN
12219                 (select  wlji.header_id
12220                          from    wsm_lot_job_interface wlji
12221                          where   wlji.process_status = 5
12222                          and     NVL(transaction_date, creation_date)
12223                                     <= decode(l_del_int_prof_value,
12224                                               null,
12225                                               NVL(transaction_date, creation_date) -1,
12226                                               SYSDATE-l_del_int_prof_value)
12227                        );
12228         -- ST : Serial Support Project ...........................----------------------------------------------
12229 
12230 l_stmt_num := 1100.25;
12231 
12232                 delete  from wsm_lot_job_interface
12233                 where   process_status = 5
12234                 --and     group_id = l_batch_grp_id
12235                 and     NVL(transaction_date, creation_date)
12236                             <= decode(l_del_int_prof_value,
12237                                       null,
12238                                       NVL(transaction_date, creation_date) -1,
12239                                       SYSDATE-l_del_int_prof_value);
12240                 if lbji_debug = 'Y' then
12241                     fnd_file.put_line(fnd_file.log,
12242                         'Deleted '||SQL%ROWCOUNT||' rows from wlji');
12243                 end if;
12244 
12245 l_stmt_num := 1100.3;
12246                 -- this is for those without header
12247                 delete  from wsm_lot_job_dtl_interface
12248                 where   process_status = 5
12249                 --and     group_id = l_batch_grp_id
12250                 and     NVL(transaction_date, creation_date)
12251                             <= decode(l_del_int_prof_value,
12252                                       null,
12253                                       NVL(transaction_date, creation_date) -1,
12254                                       SYSDATE-l_del_int_prof_value);
12255                 if lbji_debug = 'Y' then
12256                     fnd_file.put_line(fnd_file.log,
12257                         'Deleted '||SQL%ROWCOUNT||' rows from wljdi');
12258                 end if;
12259 
12260             Exception
12261                 when others then
12262                     retcode := 1;
12263                     errbuf := 'Deletion of successful rows from interface table(s) failed';
12264                     fnd_file.put_line(fnd_file.log,errbuf);
12265                     conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12266             End;
12267 
12268             COMMIT; -- commit a batch
12269 
12270         else    -- }} if csr_wlji%rowcount - prev_rowcount <> 0, one batch
12271             exit;   -- exit the outer loop
12272         end if;
12273 
12274         prev_rowcount := csr_wlji%rowcount;
12275 
12276     END LOOP; -- }}} outer loop
12277     close csr_wlji;
12278 
12279 
12280 -- ==============================================================================================
12281 -- BEGIN: handle WLJDI records with parent_header_id = NULL {{{
12282 -- ==============================================================================================
12283     -- reset savepoint savepoint_top since all the records with header are done.
12284     SAVEPOINT savepoint_top;
12285 
12286     DECLARE
12287         l_v_d_we_id_indp    t_number;
12288         l_v_d_org_id_indp   t_number;
12289         l_d_jobs            number := 0;
12290         l_v_d_we_id_err     t_number;
12291         l_v_d_org_id_err    t_number;
12292         l_v_d_emsg_err      t_errmsg;
12293         l_v_d_ecode_err     t_number;
12294         l_d_idx_err         number := 0;
12295         l_v_d_we_id_ok      t_number;
12296         l_v_d_org_id_ok     t_number;
12297         l_d_idx_ok          number := 0;
12298     BEGIN
12299 l_stmt_num := 1200;
12300         open c_wljdi_jobs_no_parent;
12301         fetch c_wljdi_jobs_no_parent bulk collect into
12302                 l_v_d_we_id_indp,
12303                 l_v_d_org_id_indp;
12304         l_d_jobs := c_wljdi_jobs_no_parent%rowcount;
12305         close c_wljdi_jobs_no_parent;
12306 
12307         if(l_d_jobs > 0) then
12308             if lbji_debug = 'Y' then
12309                 fnd_file.new_line(fnd_file.log, 2);
12310                 fnd_file.put_line(fnd_file.log,
12311                     'Handling WLJDI records with parent_header_id = NULL...');
12312             end if;
12313 
12314             l_d := l_v_d_org_id_indp.first;
12315             l_d_idx_err := 1;
12316             l_d_idx_ok := 1;
12317             while (l_d IS NOT NULL)
12318             loop
12319 
12320                 SAVEPOINT savepoint_skip_job2;
12321 l_stmt_num := 1210;
12322                 import_lot_job_details(
12323                     p_wip_entity_id             => l_v_d_we_id_indp(l_d),
12324                     p_org_id                    => l_v_d_org_id_indp(l_d),
12325                     p_wo_records_exist          => 'Y',            -- p_wo_records_exist
12326                     p_parent_header_id          => null,
12327                     p_job_scheduled_start_date  => null,           -- p_job_scheduled_start_date
12328                     p_job_scheduled_compl_date  => null,           -- p_job_scheduled_compl_date
12329                     p_job_scheduled_quantity    => null,
12330                     p_group_id                  => p_group_id,
12331                     p_last_updt_date            => sysdate,
12332                     p_last_updt_by              => fnd_global.user_id,
12333                     p_last_updt_login           => fnd_global.login_id,
12334                     p_creation_date             => sysdate,
12335                     p_created_by                => fnd_global.user_id,
12336                     p_request_id                => fnd_global.conc_request_id,
12337                     p_program_appl_id           => fnd_global.prog_appl_id,
12338                     p_program_id                => fnd_global.conc_program_id,
12339                     p_prog_updt_date            => sysdate,
12340                     p_error_code                => l_error_code,
12341                     p_error_msg                 => l_error_msg);
12342                 if (l_error_code <> 0) then
12343                     l_error_count := l_error_count + 1;
12344                     l_v_d_we_id_err(l_d_idx_err)  := l_v_d_we_id_indp(l_d);
12345                     l_v_d_org_id_err(l_d_idx_err) := l_v_d_org_id_indp(l_d);
12346                     l_v_d_ecode_err(l_d_idx_err)  := l_error_code;
12347                     l_v_d_emsg_err(l_d_idx_err)   := l_error_msg;
12348                     l_d_idx_err := l_d_idx_err +1;
12349                     ROLLBACK to savepoint_skip_job2;
12350                 else
12351                     l_v_d_we_id_ok(l_d_idx_ok)   := l_v_d_we_id_indp(l_d);
12352                     l_v_d_org_id_ok(l_d_idx_ok)  := l_v_d_org_id_indp(l_d);
12353                     l_d_idx_ok := l_d_idx_ok +1;
12354                     COMMIT;     -- commit each job
12355                 end if;
12356 
12357                 l_d := l_v_d_org_id_indp.next(l_d);
12358             end loop;
12359 
12360 l_stmt_num := 1220;
12361             if(l_d_idx_err > 1) then
12362                 forall i in l_v_d_we_id_err.first..l_v_d_we_id_err.last
12363                     update  wsm_lot_job_dtl_interface
12364                     set     process_status = 3,
12365                             error_code     = l_v_d_ecode_err(i),
12366                             error_msg      = l_v_d_emsg_err(i),
12367                             request_id     = fnd_global.conc_request_id,
12368                             program_id     = fnd_global.conc_program_id,
12369                             program_application_id = fnd_global.prog_appl_id
12370                     where   parent_header_id IS NULL
12371                     and     process_status = WIP_CONSTANTS.RUNNING
12372                     and     wip_entity_id = l_v_d_we_id_err(i)
12373                     and     organization_id = l_v_d_org_id_err(i);
12374 
12375                     errbuf := 'The interface process marked at least one row as errored';
12376                     fnd_file.put_line(fnd_file.log,errbuf);
12377                     conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('WARNING',errbuf);
12378 
12379             end if;
12380 l_stmt_num := 1230;
12381             if(l_d_idx_ok > 1) then
12382                 forall i in l_v_d_we_id_ok.first..l_v_d_we_id_ok.last
12383                     delete  from wsm_lot_job_dtl_interface
12384                     where   parent_header_id IS NULL
12385                     and     process_status = WIP_CONSTANTS.RUNNING
12386                     and     wip_entity_id = l_v_d_we_id_ok(i)
12387                     and     organization_id = l_v_d_org_id_ok(i);
12388             end if;
12389         end if; -- l_d_jobs > 0
12390     END;
12391 
12392 -- ==============================================================================================
12393 -- END: handle WLJDI records with parent_header_id = NULL }}}
12394 -- ==============================================================================================
12395 
12396 
12397 l_stmt_num := 1300;
12398     -- {{ osp begin, launch import requisition
12399     if g_num_of_osp_exists <> 0 then
12400 
12401         /*Added Code to set MOAC parameter for bug 5485653  */
12402 
12403         select to_number(ORG_INFORMATION3) into l_ou_id
12404         from HR_ORGANIZATION_INFORMATION
12405         where ORGANIZATION_ID = l_mo_org_id
12406         and ORG_INFORMATION_CONTEXT = l_org_acct_ctxt;
12407 
12408         FND_REQUEST.SET_ORG_ID (l_ou_id);
12409 
12410         l_req_request_id := fnd_request.submit_request(
12411                 'PO', 'REQIMPORT', NULL, NULL, FALSE,'WIP', NULL, 'ITEM',
12412                 NULL,'N', 'Y', chr(0), NULL, NULL, NULL,
12413                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12414                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12415                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12416                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12417                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12418                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12419                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12420                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
12421                 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
12422         );
12423 
12424         fnd_file.put_line(fnd_file.log,'Concurrent Request for Requisition Inport Submitted');
12425         fnd_file.put_line(fnd_file.log,'Request_id: '||l_req_request_id);
12426     end if;
12427     -- }} osp end
12428 
12429 l_stmt_num := 1310;
12430     -- phantom project
12431     delete from bom_explosion_temp
12432     where  group_id = wsmpwrot.explosion_group_id;
12433     wsmpwrot.explosion_group_id   := null;
12434     wsmpwrot.use_phantom_routings := null;
12435 
12436     if l_error_count = 0 then
12437         retcode := 0;
12438         errbuf := '';
12439         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('SUCCESS',errbuf);
12440     end if;
12441 
12442     COMMIT;
12443 
12444 
12445 EXCEPTION  -- for launch_worker
12446     when e_abort_request then
12447         rollback to savepoint_top;
12448         retcode := 2;
12449         errbuf := 'WSM_LBJ_INTERFACE_PVT.launch_worker (#'||l_stmt_num||') '
12450                   || SUBSTR(SQLERRM, 1,240);
12451         fnd_file.put_line(fnd_file.log,errbuf);
12452         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',errbuf);
12453         wsmputil.WRITE_TO_WIE (
12454             0,
12455             substr(errbuf,1,2000),
12456             fnd_global.conc_request_id,
12457             fnd_global.conc_program_id,
12458             fnd_global.prog_appl_id,
12459             1,
12460             dummy_err_code,
12461             dummy_err_msg );
12462         COMMIT;
12463 
12464     when others then
12465         rollback to savepoint_top;
12466         retcode := 2;
12467         errbuf := 'WSM_LBJ_INTERFACE_PVT.launch_worker (#'||l_stmt_num||') '
12468                   || SUBSTR(SQLERRM, 1,240);
12469         fnd_file.put_line(fnd_file.log,errbuf);
12470         conc_status := FND_CONCURRENT.SET_COMPLETION_STATUS('ERROR',errbuf);
12471 
12472         wsmputil.WRITE_TO_WIE (
12473             0,
12474             substr(errbuf,1,2000),
12475             fnd_global.conc_request_id,
12476             fnd_global.conc_program_id,
12477             fnd_global.prog_appl_id,
12478             1,
12479             dummy_err_code,
12480             dummy_err_msg );
12481         COMMIT;
12482 END launch_worker;
12483 
12484 
12485 
12486 -- **********************************************************************************************
12487 -- PROCEDURE build_job_header_info
12488 -- **********************************************************************************************
12489 
12490 -------------------------------------------------------------------------------------------------
12491 --
12492 -- Note that a new wip_entity_id will be generated
12493 --
12494 -- p_src_client_server: 1     => this procedure is being called from client side,
12495 --                      other => server side call.
12496 --
12497 -- p_po_creation_time: => pass the value of po_creation_time in wip_parameters for the org
12498 -------------------------------------------------------------------------------------------------
12499 
12500 --
12501 -- R12Dev: for secondary quantity
12502 -- Overload the procedure
12503 --
12504 
12505 procedure build_job_header_info(
12506         p_common_routing_sequence_id    in number,
12507         p_common_bill_sequence_id       in number,
12508         p_status_type                   in number,
12509         p_class_code                    in varchar2,
12510         p_org_id                        in number,
12511         p_wip_entity_id                 in out nocopy number,
12512         p_last_updt_date                in date,
12513         p_last_updt_by                  in number,
12514         p_creation_date                 in date,
12515         p_created_by                    in number,
12516         p_last_updt_login               in number,
12517         p_request_id                    in number,
12518         p_program_appl_id               in number,
12519         p_program_id                    in number,
12520         p_prog_updt_date                in date,
12521         p_source_line_id                in number,
12522         p_source_code                   in varchar2,
12523         p_description                   in varchar2,
12524         p_item                          in number,
12525         p_job_type                      in number,
12526         p_bom_reference_id              in number,
12527         p_routing_reference_id          in number,
12528         p_firm_planned_flag             in number,
12529         p_wip_supply_type               in number,
12530         p_job_scheduled_start_date      in date,
12531         p_job_scheduled_compl_date      in date,
12532         p_start_quantity                in number,
12533         p_net_quantity                  in number,
12534         p_coproducts_supply             in number,
12535         p_bom_revision                  in varchar2,
12536         p_routing_revision              in varchar2,
12537         p_bom_revision_date             in date,
12538         p_routing_revision_date         in date,
12539         p_lot_number                    in varchar2,
12540         p_alt_bom_designator            in varchar2,
12541         p_alt_routing_designator        in varchar2,
12542         p_priority                      in number,
12543         p_due_date                      in date,
12544         p_attribute_category            in varchar2,
12545         p_attribute1                    in varchar2,
12546         p_attribute2                    in varchar2,
12547         p_attribute3                    in varchar2,
12548         p_attribute4                    in varchar2,
12549         p_attribute5                    in varchar2,
12550         p_attribute6                    in varchar2,
12551         p_attribute7                    in varchar2,
12552         p_attribute8                    in varchar2,
12553         p_attribute9                    in varchar2,
12554         p_attribute10                   in varchar2,
12555         p_attribute11                   in varchar2,
12556         p_attribute12                   in varchar2,
12557         p_attribute13                   in varchar2,
12558         p_attribute14                   in varchar2,
12559         p_attribute15                   in varchar2,
12560         p_job_name                      in varchar2,
12561         p_completion_subinventory       in varchar2,
12562         p_completion_locator_id         in number,
12563         p_demand_class                  in varchar2,
12564         p_project_id                    in number,
12565         p_task_id                       in number,
12566         p_schedule_group_id             in number,
12567         p_build_sequence                in number,
12568         p_line_id                       in number,
12569         p_kanban_card_id                in number,
12570         p_overcompl_tol_type            in number,
12571         p_overcompl_tol_value           in number,
12572         p_end_item_unit_number          in number,
12573         p_src_client_server             in number,
12574         p_po_creation_time              in number,
12575         p_date_released                 in date,
12576         p_error_code                    out nocopy number,
12577         p_error_msg                     out nocopy varchar2) is
12578 
12579 l_wjsq_uom_code         tbl_wjsq_uom_code;
12580 l_wjsq_start_quantity   tbl_wjsq_start_quantity;
12581 
12582 BEGIN
12583 
12584     l_wjsq_uom_code.delete();
12585     l_wjsq_start_quantity.delete();
12586 
12587     build_job_header_info(
12588         p_common_routing_sequence_id   =>  p_common_routing_sequence_id    ,
12589         p_common_bill_sequence_id      =>  p_common_bill_sequence_id       ,
12590         p_status_type                  =>  p_status_type                   ,
12591         p_class_code                   =>  p_class_code                    ,
12592         p_org_id                       =>  p_org_id                        ,
12593         p_wip_entity_id                =>  p_wip_entity_id                 ,
12594         p_last_updt_date               =>  p_last_updt_date                ,
12595         p_last_updt_by                 =>  p_last_updt_by                  ,
12596         p_creation_date                =>  p_creation_date                 ,
12597         p_created_by                   =>  p_created_by                    ,
12598         p_last_updt_login              =>  p_last_updt_login               ,
12599         p_request_id                   =>  p_request_id                    ,
12600         p_program_appl_id              =>  p_program_appl_id               ,
12601         p_program_id                   =>  p_program_id                    ,
12602         p_prog_updt_date               =>  p_prog_updt_date                ,
12603         p_source_line_id               =>  p_source_line_id                ,
12604         p_source_code                  =>  p_source_code                   ,
12605         p_description                  =>  p_description                   ,
12606         p_item                         =>  p_item                          ,
12607         p_job_type                     =>  p_job_type                      ,
12608         p_bom_reference_id             =>  p_bom_reference_id              ,
12609         p_routing_reference_id         =>  p_routing_reference_id          ,
12610         p_firm_planned_flag            =>  p_firm_planned_flag             ,
12611         p_wip_supply_type              =>  p_wip_supply_type               ,
12612         p_job_scheduled_start_date     =>  p_job_scheduled_start_date      ,
12613         p_job_scheduled_compl_date     =>  p_job_scheduled_compl_date      ,
12614         p_start_quantity               =>  p_start_quantity                ,
12615         p_net_quantity                 =>  p_net_quantity                  ,
12616         p_coproducts_supply            =>  p_coproducts_supply             ,
12617         p_bom_revision                 =>  p_bom_revision                  ,
12618         p_routing_revision             =>  p_routing_revision              ,
12619         p_bom_revision_date            =>  p_bom_revision_date             ,
12620         p_routing_revision_date        =>  p_routing_revision_date         ,
12621         p_lot_number                   =>  p_lot_number                    ,
12622         p_alt_bom_designator           =>  p_alt_bom_designator            ,
12623         p_alt_routing_designator       =>  p_alt_routing_designator        ,
12624         p_priority                     =>  p_priority                      ,
12625         p_due_date                     =>  p_due_date                      ,
12626         p_attribute_category           =>  p_attribute_category            ,
12627         p_attribute1                   =>  p_attribute1                    ,
12628         p_attribute2                   =>  p_attribute2                    ,
12629         p_attribute3                   =>  p_attribute3                    ,
12630         p_attribute4                   =>  p_attribute4                    ,
12631         p_attribute5                   =>  p_attribute5                    ,
12632         p_attribute6                   =>  p_attribute6                    ,
12633         p_attribute7                   =>  p_attribute7                    ,
12634         p_attribute8                   =>  p_attribute8                    ,
12635         p_attribute9                   =>  p_attribute9                    ,
12636         p_attribute10                  =>  p_attribute10                   ,
12637         p_attribute11                  =>  p_attribute11                   ,
12638         p_attribute12                  =>  p_attribute12                   ,
12639         p_attribute13                  =>  p_attribute13                   ,
12640         p_attribute14                  =>  p_attribute14                   ,
12641         p_attribute15                  =>  p_attribute15                   ,
12642         p_job_name                     =>  p_job_name                      ,
12643         p_completion_subinventory      =>  p_completion_subinventory       ,
12644         p_completion_locator_id        =>  p_completion_locator_id         ,
12645         p_demand_class                 =>  p_demand_class                  ,
12646         p_project_id                   =>  p_project_id                    ,
12647         p_task_id                      =>  p_task_id                       ,
12648         p_schedule_group_id            =>  p_schedule_group_id             ,
12649         p_build_sequence               =>  p_build_sequence                ,
12650         p_line_id                      =>  p_line_id                       ,
12651         p_kanban_card_id               =>  p_kanban_card_id                ,
12652         p_overcompl_tol_type           =>  p_overcompl_tol_type            ,
12653         p_overcompl_tol_value          =>  p_overcompl_tol_value           ,
12654         p_end_item_unit_number         =>  p_end_item_unit_number          ,
12655         p_src_client_server            =>  p_src_client_server             ,
12656         p_po_creation_time             =>  p_po_creation_time              ,
12657         p_date_released                =>  p_date_released                 ,
12658         p_wjsq_uom_code                =>  l_wjsq_uom_code                 ,
12659         p_wjsq_start_quantity          =>  l_wjsq_start_quantity           ,
12660         p_error_code                   =>  p_error_code                    ,
12661         p_error_msg                    =>  p_error_msg                     );
12662 
12663 END build_job_header_info;
12664 
12665 
12666 
12667 -- **********************************************************************************************
12668 -- PROCEDURE build_job_copy_info
12669 -- **********************************************************************************************
12670 
12671 procedure build_job_copy_info (
12672         p_common_routing_sequence_id    in number,
12673         p_common_bill_sequence_id       in number,
12674         p_org_id                        in number,
12675         p_wip_entity_id                 in number,
12676         p_last_updt_date                in date,
12677         p_last_updt_by                  in number,
12678         p_creation_date                 in date,
12679         p_created_by                    in number,
12680         p_last_updt_login               in number,
12681         p_request_id                    in number,
12682         p_program_appl_id               in number,
12683         p_program_id                    in number,
12684         p_prog_updt_date                in date,
12685         p_item                          in number,
12686         p_bom_reference_id              in number,
12687         p_routing_reference_id          in number,
12688         p_wip_supply_type               in number,
12689         p_job_scheduled_start_date      in date,
12690         p_job_scheduled_compl_date      in date,
12691         p_start_quantity                in number,
12692         p_bom_revision_date             in date,
12693         p_routing_revision_date         in date,
12694         p_alt_bom_designator            in varchar2,
12695         p_alt_routing_designator        in varchar2,
12696         p_header_id                     in number,      -- header_id in WLJI, pass null if N/A
12697         p_num_of_children               in number,      -- number of children in WLJDI
12698         p_infinite_schedule             in varchar2,    -- call infinite scheduler or not
12699         p_error_code                    out nocopy number,
12700         p_error_msg                     out nocopy varchar2) is
12701 
12702 l_stmt_num              number;
12703 e_build_job_dtl         exception;
12704 
12705 l_sch_mode              number  := null;
12706 l_sch_date              date    := null;
12707 
12708 l_scheduled_start_date  date;
12709 l_scheduled_compl_date  date;
12710 
12711 l_bill_sequence_id      number; -- BUG: 3341350/3286849
12712 l_bill_item_id          number;
12713 l_common_bill_seq_id    number; -- BUG: 3981616
12714 
12715 l_op_seq_id             NUMBER; --Bug 5104459
12716 
12717 BEGIN
12718     p_error_code := 0;
12719     p_error_msg := '';
12720 
12721     l_scheduled_start_date := p_job_scheduled_start_date;
12722     l_scheduled_compl_date := p_job_scheduled_compl_date;
12723 
12724     if(p_infinite_schedule = 'Y') then
12725         if(p_job_scheduled_start_date IS NULL) then
12726             l_sch_mode := WIP_CONSTANTS.BACKWARDS;
12727             l_sch_date := p_job_scheduled_compl_date;
12728         else
12729             l_sch_mode := WIP_CONSTANTS.FORWARDS;
12730             l_sch_date := p_job_scheduled_start_date;
12731         end if;
12732         -- force import_lot_job_details to get scheduled_start_date
12733         -- and scheduled_compl_date from WDJ in such case
12734         l_scheduled_start_date := null;
12735         l_scheduled_compl_date := null;
12736     end if;
12737 
12738     -- BA: BUG: 3341350/3286849
12739 l_stmt_num := 5;
12740     l_bill_sequence_id := WSMPUTIL.GET_JOB_BOM_SEQ_ID(p_wip_entity_id);
12741     if(l_bill_sequence_id = -1) then
12742         p_error_msg  := 'Fail to get bill sequence id (wip_entity_id='||p_wip_entity_id ||')';
12743         p_error_code := -1;
12744         raise e_build_job_dtl;
12745     end if;
12746     -- EA: BUG: 3341350/3286849
12747 
12748    -- BA: BUG 3981616 l_common_bill_seq_id is set to be null if p_common_bill_sequence_id = 0
12749    if(p_common_bill_sequence_id = 0) then
12750       l_common_bill_seq_id := NULL;
12751    else
12752       l_common_bill_seq_id := p_common_bill_sequence_id;
12753    end if;
12754    -- EA: BUG 3981616
12755 
12756     if(p_routing_reference_id IS NULL) then
12757         l_bill_item_id := p_item;
12758     else
12759         l_bill_item_id := p_bom_reference_id;
12760     end if;
12761 
12762 l_stmt_num := 10;
12763     -- Create the job level copy
12764     --OPTII-PERF:Find if phantom exists or not.
12765     begin
12766          select 1 into v_phantom_exists
12767          from  bom_inventory_components
12768          where bill_sequence_id = l_common_bill_seq_id
12769          and   p_bom_revision_date between effectivity_date and nvl(disable_date,p_bom_revision_date+1)
12770          and   wip_supply_type = 6
12771          and   rownum = 1;
12772 
12773          v_phantom_exists := 1;
12774 
12775     exception
12776          when others then
12777             v_phantom_exists := 2;
12778 
12779     end;
12780 
12781     if v_insert_wip = 1 then
12782        --if  v_phantom_exists = 1 then
12783        if  v_phantom_exists = 1 then
12784            v_insert_wip := 2;
12785        end if;
12786        --If job dtls are populated wo should be populated after the
12787        -- copy tables are updated with the data from dtls table.
12788        if  p_num_of_children > 0 then
12789            v_insert_wip := 2;
12790        end if;
12791     end if;
12792     --Start changes for Bug 5104459
12793     l_stmt_num := 11;
12794 
12795     wsmputil.FIND_ROUTING_START(
12796                             p_common_routing_sequence_id,
12797                             p_routing_revision_date,
12798                             l_op_seq_id,
12799                             p_error_code,
12800                             p_error_msg);
12801 
12802      if p_error_code <> 0 then
12803                     raise e_build_job_dtl;
12804      end if;
12805 
12806      l_stmt_num := 12;
12807 
12808      wsmputil.FIND_ROUTING_END(
12809                             p_common_routing_sequence_id,
12810                             p_routing_revision_date,
12811                             l_op_seq_id,
12812                             p_error_code,
12813                             p_error_msg);
12814 
12815      if p_error_code <> 0 then
12816               raise e_build_job_dtl;
12817      end if;
12818 
12819      --End changes for Bug 5104459
12820 
12821     wsm_jobcopies_pvt.create_jobcopies (
12822             x_err_buf               => p_error_msg,
12823             x_err_code              => p_error_code,
12824             p_wip_entity_id         => p_wip_entity_id,
12825             p_org_id                => p_org_id,
12826             p_primary_item_id       => p_item,
12827             p_routing_item_id       => nvl(p_routing_reference_id, p_item),      -- newly added ??
12828             p_alt_rtg_desig         => p_alt_routing_designator,    -- newly added
12829             p_rtg_seq_id            => null,                        -- newly added, not used in API
12830             p_common_rtg_seq_id     => p_common_routing_sequence_id,
12831             p_rtg_rev_date          => p_routing_revision_date,
12832             p_bill_item_id          => l_bill_item_id,              -- newly added ??
12833             p_alt_bom_desig         => p_alt_bom_designator,
12834             p_bill_seq_id           => l_bill_sequence_id,          -- newly added
12835             --p_common_bill_seq_id  => p_common_bill_sequence_id,   -- bug 3981616
12836             p_common_bill_seq_id    => l_common_bill_seq_id,        -- bug 3981616
12837             p_bom_rev_date          => p_bom_revision_date,
12838             p_wip_supply_type       => p_wip_supply_type,
12839             p_last_update_date      => p_last_updt_date,
12840             p_last_updated_by       => p_last_updt_by,
12841             p_last_update_login     => p_last_updt_login,
12842             p_creation_date         => p_creation_date,
12843             p_created_by            => p_created_by,
12844             p_request_id            => p_request_id,
12845             p_program_app_id        => p_program_appl_id,
12846             p_program_id            => p_program_id,
12847             p_program_update_date   => p_prog_updt_date,
12848             p_inf_sch_flag          => p_infinite_schedule,
12849             p_inf_sch_mode          => l_sch_mode,
12850             p_inf_sch_date          => l_sch_date,
12851             --OPTII-PERF:Following new arguments are added.
12852             p_new_job               => v_new_job,
12853             p_insert_wip            => v_insert_wip,
12854             p_phantom_exists        => v_phantom_exists,
12855             p_charges_exist         => 2);
12856 
12857             v_new_job := NULL;
12858             --v_insert_wip := NULL;
12859             --v_phantom_exists := NULL;
12860     -- BC: bug 3400858/3400858
12861     --if (p_error_code <> 0) then
12862 
12863     --if (p_error_code <> 0) OR (p_error_code IS NOT NULL) OR -- No error
12864     --    (p_error_code <> -1)    -- Warning
12865     --then
12866     --    raise e_build_job_dtl;
12867     --end if;
12868 
12869     if (p_error_code = 0) or
12870         (p_error_code is null) or -- no error
12871         (p_error_code = -1)    -- warning
12872     then
12873         p_error_code := 0;
12874         p_error_msg := null;
12875     else
12876         raise e_build_job_dtl;
12877     end if;
12878     -- EC: bug 3303267/3400858
12879 
12880     if lbji_debug = 'Y' then
12881         fnd_file.put_line(fnd_file.log, 'Make job-level BOM/RTG copy ...');
12882     end if;
12883 
12884 l_stmt_num := 20;
12885     -- Mark recommendations on the job level copy
12886     if(p_num_of_children > 0) then
12887         import_lot_job_details (
12888             p_wip_entity_id             => p_wip_entity_id,
12889             p_org_id                    => p_org_id,
12890             p_wo_records_exist          => 'N',                -- p_wo_records_exist
12891             p_parent_header_id          => p_header_id,
12892             p_job_scheduled_start_date  => l_scheduled_start_date,
12893             p_job_scheduled_compl_date  => l_scheduled_compl_date,
12894             p_job_scheduled_quantity    => p_start_quantity,
12895             p_group_id                  => null,
12896             p_last_updt_date            => p_last_updt_date,
12897             p_last_updt_by              => p_last_updt_by,
12898             p_last_updt_login           => p_last_updt_login,
12899             p_creation_date             => p_creation_date,
12900             p_created_by                => p_created_by,
12901             p_request_id                => p_request_id,
12902             p_program_appl_id           => p_program_appl_id,
12903             p_program_id                => p_program_id,
12904             p_prog_updt_date            => p_prog_updt_date,
12905             p_error_code                => p_error_code,
12906             p_error_msg                 => p_error_msg);
12907         if (p_error_code <> 0) then
12908             raise e_build_job_dtl;
12909         end if;
12910     end if;
12911 
12912 EXCEPTION
12913     when e_build_job_dtl then
12914         -- p_error_code/p_error_msg was set
12915         fnd_file.put_line(fnd_file.log,
12916             substr('build_job_copy_info: (#'||l_stmt_num||') '
12917                    ||p_error_msg, 1, 2000));
12918 
12919     when others then
12920         p_error_code := SQLCODE;
12921         p_error_msg := substr('build_job_copy_info: (#'||l_stmt_num||') '
12922                        ||SQLERRM, 1, 2000);
12923         fnd_file.put_line(fnd_file.log, substr(p_error_msg, 1, 2000));
12924 END build_job_copy_info;
12925 
12926 
12927 -- **********************************************************************************************
12928 -- PROCEDURE build_job_detail_info
12929 -- **********************************************************************************************
12930 
12931 -------------------------------------------------------------------------------------------------
12932 -- For job creation, p_rtg_op_seq_num = rtg op_seq_num of the first operation or null
12933 -- For bouns, p_rtg_op_seq_num = rtg op_seq_num of the starting operation on 'the' path
12934 -- This procedure is only called during
12935 --     (1)job creation
12936 --     (2)change bom/rtg alt
12937 --     (3)change rbom/rtg reference
12938 -- in all such cases, WO records are considered as not available
12939 -------------------------------------------------------------------------------------------------
12940 
12941 procedure build_job_detail_info(
12942         p_common_routing_sequence_id    in number,
12943         p_common_bill_sequence_id       in number,
12944         p_status_type                   in number,
12945         p_org_id                        in number,
12946         p_wip_entity_id                 in number,
12947         p_last_updt_date                in date,
12948         p_last_updt_by                  in number,
12949         p_creation_date                 in date,
12950         p_created_by                    in number,
12951         p_last_updt_login               in number,
12952         p_request_id                    in number,
12953         p_program_appl_id               in number,
12954         p_program_id                    in number,
12955         p_prog_updt_date                in date,
12956         p_item                          in number,
12957         p_job_type                      in number,
12958         p_bom_reference_id              in number,
12959         p_routing_reference_id          in number,
12960         p_wip_supply_type               in number,
12961         p_job_scheduled_start_date      in date,        -- not used
12962         p_job_scheduled_compl_date      in date,        -- not used
12963         p_start_quantity                in number,
12964         p_bom_revision_date             in date,
12965         p_routing_revision_date         in date,
12966         p_alt_bom_designator            in varchar2,
12967         p_alt_routing_designator        in varchar2,
12968         p_rtg_op_seq_num                in number,
12969         p_error_code                    out nocopy number,
12970         p_error_msg                     out nocopy varchar2) is
12971 
12972 BEGIN
12973 
12974     build_job_detail_info(
12975             p_common_routing_sequence_id => p_common_routing_sequence_id,
12976             p_common_bill_sequence_id    => p_common_bill_sequence_id,
12977             p_status_type                => p_status_type,
12978             p_org_id                     => p_org_id,
12979             p_wip_entity_id              => p_wip_entity_id,
12980             p_last_updt_date             => p_last_updt_date,
12981             p_last_updt_by               => p_last_updt_by,
12982             p_creation_date              => p_creation_date,
12983             p_created_by                 => p_created_by,
12984             p_last_updt_login            => p_last_updt_login,
12985             p_request_id                 => p_request_id,
12986             p_program_appl_id            => p_program_appl_id,
12987             p_program_id                 => p_program_id,
12988             p_prog_updt_date             => p_prog_updt_date,
12989             p_item                       => p_item,
12990             p_job_type                   => p_job_type,
12991             p_bom_reference_id           => p_bom_reference_id,
12992             p_routing_reference_id       => p_routing_reference_id,
12993             p_wip_supply_type            => p_wip_supply_type,
12994             p_job_scheduled_start_date   => p_job_scheduled_start_date,
12995             p_job_scheduled_compl_date   => p_job_scheduled_compl_date,
12996             p_start_quantity             => p_start_quantity,
12997             p_bom_revision_date          => p_bom_revision_date,
12998             p_routing_revision_date      => p_routing_revision_date,
12999             p_alt_bom_designator         => p_alt_bom_designator,
13000             p_alt_routing_designator     => p_alt_routing_designator,
13001             p_rtg_op_seq_num             => p_rtg_op_seq_num,
13002             p_error_code                 => p_error_code,
13003             p_error_msg                  => p_error_msg,
13004             p_src_client_server          => 0,
13005             p_po_creation_time           => wip_constants.manual_creation); -- default is not to create PO
13006 
13007 END build_job_detail_info;
13008 
13009 
13010 /*******************************************************************************
13011 
13012    Procedure to check if a resource is costed.....
13013 
13014 ********************************************************************************/
13015 
13016 Procedure check_resource_costed (p_wip_entity_id     in         number,
13017                      p_org_id            in         number,
13018                  p_res_op_seq_num    in         number,
13019                  p_op_seq_num        in         number,
13020                  x_error_code        out nocopy number,
13021                  x_error_msg         out nocopy varchar2
13022                 )
13023                 is
13024 
13025 cursor c_lot_wcti_check is
13026       select 1
13027       from wip_cost_txn_interface
13028       where organization_id = p_org_id
13029       and wip_entity_id = p_wip_entity_id
13030       and operation_seq_num = p_op_seq_num
13031       and resource_seq_num = p_res_op_seq_num
13032       and rownum = 1;
13033 
13034 cursor c_lot_wt_check is
13035       select 1
13036       from wip_transactions
13037       where organization_id = p_org_id
13038       and wip_entity_id = p_wip_entity_id
13039       and operation_seq_num = p_op_seq_num
13040       and resource_seq_num = p_res_op_seq_num
13041       and rownum = 1;
13042 
13043 l_stmt_num number;
13044 begin
13045 
13046     x_error_code := 0;
13047     x_error_msg  := null;
13048 
13049     l_stmt_num := 10;
13050 
13051     for c_lot_wcti_check_rec in c_lot_wcti_check loop
13052         x_error_code := -1;  /* costed .. */
13053     end loop;
13054 
13055     l_stmt_num := 20;
13056 
13057     for c_lot_wt_check_rec in c_lot_wt_check loop
13058         x_error_code := -1; /* costed.... */
13059     end loop;
13060 
13061 exception
13062     when others then
13063         /* add the message also.... */
13064 
13065         x_error_code := -1;
13066         x_error_msg  := 'check_resource_costed : ' || l_stmt_num || ' : ' || SQLCODE || substrb(SQLERRM,1,1000);
13067         fnd_file.put_line(fnd_file.log,x_error_msg);
13068 
13069 end check_resource_costed;
13070 
13071 /*******************************************************************************************
13072 
13073     Procedure to delete the setup info of a resource that is being replaced or deleted...
13074 
13075 ********************************************************************************************/
13076 
13077 Procedure delete_setup_info(p_wip_entity_id     in         number,
13078                 p_org_id            in         number,
13079                 p_par_res_seq_num   in     number,
13080                 p_op_seq_num    in         number,
13081                 p_handle_curr_op    in         varchar2,
13082                 x_error_code        out nocopy number,
13083                 x_error_msg         out nocopy varchar2
13084                )
13085                is
13086 
13087 e_res_costed  exception;
13088 
13089 l_setup_exists NUMBER := 0;
13090 l_res_costed   number := 0;
13091 cursor c_setup_res(c_par_res_seq_num NUMBER) is
13092        select resource_id,resource_seq_num,operation_seq_num,applied_resource_units
13093        from wip_operation_resources wor
13094        where  wor.organization_id = p_org_id
13095        and    wor.wip_entity_id   = p_wip_entity_id
13096        and    wor.operation_seq_num   = p_op_seq_num
13097        and    wor.parent_resource_seq  = c_par_res_seq_num;
13098 
13099 l_stmt_num  number;
13100 
13101 begin
13102 
13103     /* open the cursor and loop through the setup resources and check if any of them is costed...
13104       if so return an error....
13105     */
13106     l_stmt_num := 10;
13107 
13108     l_res_costed := 0;
13109     l_setup_exists := 0;
13110 
13111     if (p_handle_curr_op = 'Y') then
13112         IF lbji_debug = 'Y' then
13113                 fnd_file.put_line(fnd_file.log,'delete setup info: WIP table change.... : ');
13114         END IF;
13115 
13116         l_stmt_num := 20;
13117 
13118         for l_setup_res in c_setup_res(p_par_res_seq_num) loop
13119 
13120             l_setup_exists := 1;
13121             /* call to check if costs exost for the particular setup resource... */
13122             if nvl(l_setup_res.applied_resource_units,0) <> 0 then
13123                 /* error out as costed.... */
13124                 l_stmt_num := 30;
13125                 raise e_res_costed;
13126             else
13127                 x_error_code := 0;
13128                 x_error_msg  := null;
13129 
13130                 l_res_costed := 0;
13131 
13132                 l_stmt_num := 35;
13133                 IF lbji_debug = 'Y' then
13134                         fnd_file.put_line(fnd_file.log,'delete setup info:call to check_resource_costed .... : '
13135                                   || ' Resource seq num : ' || l_setup_res.resource_seq_num
13136                                   || ' Operation seq num : ' || p_op_seq_num
13137                                   );
13138                 END IF;
13139 
13140                 check_resource_costed ( p_wip_entity_id     => p_wip_entity_id,
13141                             p_org_id            => p_org_id,
13142                             p_res_op_seq_num    => l_setup_res.resource_seq_num,
13143                             p_op_seq_num        => p_op_seq_num,
13144                             x_error_code        => x_error_code,
13145                             x_error_msg     => x_error_msg
13146                               );
13147 
13148                 l_stmt_num := 40;
13149 
13150                 if x_error_code <> 0 then
13151                     /* error out... */
13152                     l_res_costed := 1;
13153                     exit;
13154                 end if;
13155 
13156             end if;
13157         end loop;
13158 
13159         l_stmt_num := 50;
13160 
13161         if l_res_costed = 0 and l_setup_exists <> 0 then
13162 
13163             l_stmt_num := 55;
13164             /* delete the instance information ... */
13165             delete from wip_op_resource_instances wori
13166             where wori.organization_id  = p_org_id
13167             and    wori.wip_entity_id   = p_wip_entity_id
13168             and    wori.operation_seq_num  = p_op_seq_num
13169             and    wori.RESOURCE_SEQ_NUM  in (select resource_seq_num
13170                               from wip_operation_resources wor
13171                               where wor.organization_id = p_org_id
13172                               and    wor.wip_entity_id   = p_wip_entity_id
13173                               and    wor.operation_seq_num   = p_op_seq_num
13174                               and    wor.parent_resource_seq = p_par_res_seq_num
13175                               );
13176 
13177             l_stmt_num := 60;
13178 
13179             /* delete the usage information .... */
13180             delete from wip_operation_resource_usage woru
13181             where woru.organization_id  = p_org_id
13182             and    woru.wip_entity_id   = p_wip_entity_id
13183             and    woru.operation_seq_num  = p_op_seq_num
13184             and    woru.resource_seq_num  in (select resource_seq_num
13185                               from wip_operation_resources wor
13186                               where wor.organization_id = p_org_id
13187                               and    wor.wip_entity_id   = p_wip_entity_id
13188                               and    wor.operation_seq_num   = p_op_seq_num
13189                               and    wor.parent_resource_seq = p_par_res_seq_num
13190                               );
13191 
13192             l_stmt_num := 70;
13193 
13194             /* delete the setup resources.... */
13195             delete from wip_operation_resources wor
13196             where wor.organization_id = p_org_id
13197             and    wor.wip_entity_id   = p_wip_entity_id
13198             and    wor.operation_seq_num   = p_op_seq_num
13199             and    wor.parent_resource_seq = p_par_res_seq_num;
13200 
13201         elsif l_res_costed <> 0 then
13202             l_stmt_num := 80;
13203             raise e_res_costed;
13204         end if;
13205 
13206 
13207     else
13208 
13209         /* delete the instance information ... */
13210         l_stmt_num := 90;
13211 
13212         l_setup_exists := 0;
13213         select count(*)
13214         into   l_setup_exists
13215         from   wsm_copy_op_resources wcor
13216         where  wcor.organization_id = p_org_id
13217         and    wcor.wip_entity_id   = p_wip_entity_id
13218         and    wcor.operation_seq_num   = p_op_seq_num
13219         and    wcor.parent_resource_seq_num = p_par_res_seq_num
13220         and    rownum = 1;
13221 
13222         IF l_setup_exists <> 0 THEN
13223                 delete from wsm_copy_op_resource_instances wcori
13224                 where wcori.organization_id  = p_org_id
13225                 and    wcori.wip_entity_id   = p_wip_entity_id
13226                 and    wcori.operation_seq_num  = p_op_seq_num
13227                 and    wcori.resource_seq_num  in (select resource_seq_num
13228                                   from wsm_copy_op_resources wcor
13229                                   where wcor.organization_id = p_org_id
13230                                   and    wcor.wip_entity_id   = p_wip_entity_id
13231                                   and    wcor.operation_seq_num   = p_op_seq_num
13232                                   and    wcor.parent_resource_seq_num = p_par_res_seq_num
13233                                   );
13234                 /* delete the usage information .... */
13235                 l_stmt_num := 100;
13236                 delete from wsm_copy_op_resource_usage wcoru
13237                 where wcoru.organization_id  = p_org_id
13238                 and    wcoru.wip_entity_id   = p_wip_entity_id
13239                 and    wcoru.operation_seq_num  = p_op_seq_num
13240                 and    wcoru.resource_seq_num  in (select resource_seq_num
13241                                   from wsm_copy_op_resources wcor
13242                                   where wcor.organization_id = p_org_id
13243                                   and    wcor.wip_entity_id   = p_wip_entity_id
13244                                   and    wcor.operation_seq_num   = p_op_seq_num
13245                                   and    wcor.parent_resource_seq_num = p_par_res_seq_num
13246                                   );
13247                 /* delete the setup resources.... */
13248                 l_stmt_num := 110;
13249 
13250                 delete from wsm_copy_op_resources wcor
13251                 where wcor.organization_id = p_org_id
13252                 and    wcor.wip_entity_id   = p_wip_entity_id
13253                 and    wcor.operation_seq_num   = p_op_seq_num
13254                 and    wcor.parent_resource_seq_num = p_par_res_seq_num;
13255         END IF;
13256 
13257     end if;
13258 
13259     x_error_code := 0;
13260 
13261 exception
13262     when e_res_costed then
13263 
13264         fnd_message.set_name('WSM','WSM_OP_RES_COSTED');
13265         x_error_msg := fnd_message.get;
13266         x_error_code := -1;
13267         fnd_file.put_line(fnd_file.log,'delete_setup_info : ' || l_stmt_num || ' : ' || x_error_msg);
13268 
13269     when others then
13270         x_error_code := -1;
13271         x_error_msg  := 'check_resource_costed : ' || l_stmt_num || ' : ' || SQLCODE || substrb(SQLERRM,1,1000);
13272         fnd_file.put_line(fnd_file.log,x_error_msg);
13273 
13274 end delete_setup_info;
13275 
13276 /**************************************************************************************
13277 
13278 Procedure Added to validate the new resource + its setup resources being added (or)
13279 
13280  validate setup resources being added for a particular resource
13281 
13282 ***************************************************************************************/
13283 
13284 Procedure validate_new_resource(p_wip_entity_id     in               number,
13285                                 p_org_id            in               number,
13286                                 -- p_par_res_index  in               number,
13287                                 p_setup_index_tbl   in               t_number,
13288                                 p_res_recom         in               t_res_recom,
13289                                 p_handle_wip_cur_op in               varchar2,
13290                                 p_job_op_seq_num    in               number,
13291                                 p_rtg_op_seq_num    in               number,
13292                                 p_new_res_tbl       in  out nocopy   new_res_tbl,
13293                                 x_error_code        in  out nocopy   number,
13294                                 x_error_msg         in  out nocopy   varchar2
13295                                 ) is
13296 
13297 
13298 l_par_res_seq_num       number;
13299 l_exists                number;
13300 l_setup_exists          number;
13301 l_dummy                 number;
13302 l_batchable             number;
13303 l_field_name            varchar2(100);
13304 l_error_code            number;
13305 l_error_msg             varchar2(2000);
13306 l_stmt_num              number;
13307 l_index                 number;
13308 
13309 l_max_resource_seq_num  number;
13310 
13311 l_new_res_seq_tbl       t_number;
13312 
13313 l_res_id                number;
13314 l_res_recommended       varchar2(10);
13315 
13316 l_hash_value            number;
13317 l_hash_str              varchar2(500);
13318 
13319 l_res_num1              number;
13320 l_res_num2              number;
13321 
13322 l_res_auto_charge_type  number;
13323 
13324 l_hour_uom              varchar2(50);
13325 l_hour_uom_class        varchar2(200);
13326 l_uom_time_class_flag   boolean;
13327 l_uom_class             varchar2(10);
13328 l_autocharge_type       number;
13329 
13330 e_no_po_move_res        exception;
13331 e_invalid_res_seq_num   exception;
13332 e_invalid_par_seq       exception;
13333 e_invalid_res           exception;
13334 e_invalid_res_dates     exception;
13335 e_invalid_res_seq       exception;
13336 e_invalid_seq           exception;
13337 e_invalid_setup_id      exception;
13338 e_invalid_setup_code    exception;
13339 e_setup_costed          exception;
13340 
13341 e_invalid_field         exception;
13342 
13343 e_invalid_max_assg_units        exception;
13344 e_invalid_setup_max_assg_units  exception;
13345 e_invalid_batch_max_assg_units  exception;
13346 e_res_not_batchable             exception;
13347 
13348 cursor c_setup_res(c_par_res_seq_num NUMBER) is
13349        select resource_id,resource_seq_num,operation_seq_num
13350        from wip_operation_resources wor
13351        where  wor.organization_id = p_org_id
13352        and    wor.wip_entity_id   = p_wip_entity_id
13353        and    wor.operation_seq_num   = p_job_op_seq_num
13354        and    wor.parent_resource_seq  = c_par_res_seq_num;
13355 
13356 begin
13357     if p_handle_wip_cur_op = 'Y' then
13358 
13359         l_stmt_num := 10;
13360         /* get the maximum resource seq num.... for the current operation...*/
13361         begin
13362 
13363             select max(resource_seq_num)
13364             into   l_res_num1
13365             from   wip_operation_resources wor
13366             where  wor.organization_id = p_org_id
13367             and    wor.wip_entity_id   = p_wip_entity_id
13368             and    wor.operation_seq_num   = p_job_op_seq_num;
13369 
13370         exception
13371             when no_data_found then
13372                 null;
13373         end;
13374 
13375         /* get the parent resource seq num */
13376         l_par_res_seq_num := p_new_res_tbl(p_new_res_tbl.first).parent_seq_num;
13377 
13378         /* Indicates that there is no parent resource being added... ..... */
13379         /*
13380             Validations to be done...
13381             i)   check if any parent resource exists ( consider only WIP_OPERATION_RESOURCES )
13382             ii)  Check if any setup resources already exist....
13383             iii) Delete the setup resources...( if no cost incurred )
13384         */
13385 
13386         l_stmt_num := 20;
13387         begin
13388 
13389             l_exists := 0;
13390 
13391             select 1
13392             into   l_exists
13393             from   wip_operation_resources wor
13394             where  wor.organization_id = p_org_id
13395             and    wor.wip_entity_id   = p_wip_entity_id
13396             and    wor.operation_seq_num   = p_job_op_seq_num
13397             and    resource_seq_num    = l_par_res_seq_num;
13398 
13399         exception
13400             when no_data_found then
13401                 raise e_invalid_par_seq;
13402 
13403         end;
13404 
13405         l_stmt_num := 30;
13406         begin
13407             l_setup_exists := 0;
13408 
13409             select 1
13410             into   l_setup_exists
13411             from   wip_operation_resources wor
13412             where  wor.organization_id = p_org_id
13413             and    wor.wip_entity_id   = p_wip_entity_id
13414             and    wor.operation_seq_num   = p_job_op_seq_num
13415             and    wor.parent_resource_seq = l_par_res_seq_num
13416             and    rownum =1 ;
13417 
13418         exception
13419             when no_data_found then
13420                 l_setup_exists := 0;
13421         end;
13422 
13423         l_stmt_num := 40;
13424         IF lbji_debug = 'Y' THEN
13425                 fnd_file.put_line(fnd_file.log,'validate new resource : ' || l_stmt_num || ' l_setup_exists at current op ..... ' || l_setup_exists);
13426         END IF;
13427 
13428         if l_setup_exists = 1  then
13429             /* open the cursor and loop through the setup resources and check if any of them is costed...
13430             if so return an error....*/
13431 
13432             l_error_code := 0;
13433             l_error_msg  := null;
13434 
13435             l_stmt_num := 50;
13436             IF lbji_debug = 'Y' THEN
13437                 fnd_file.put_line(fnd_file.log,'validate new resource : ' || l_stmt_num || ' calling delete setup info ...' );
13438             END IF;
13439 
13440             delete_setup_info( p_wip_entity_id     => p_wip_entity_id,
13441                        p_org_id            => p_org_id,
13442                        p_par_res_seq_num   => l_par_res_seq_num,
13443                        p_op_seq_num        => p_job_op_seq_num,
13444                        p_handle_curr_op    => 'Y',
13445                        x_error_code        => l_error_code,
13446                        x_error_msg         => l_error_msg
13447                      );
13448 
13449             l_stmt_num := 60;
13450 
13451             if l_error_code <> 0 then
13452                 raise e_setup_costed;
13453             end if;
13454             l_stmt_num := 70;
13455 
13456             /* delete the setup resources.... */
13457             delete from wip_operation_resources wor
13458             where wor.organization_id = p_org_id
13459             and    wor.wip_entity_id   = p_wip_entity_id
13460             and    wor.operation_seq_num   = p_job_op_seq_num
13461             and    wor.parent_resource_seq = l_par_res_seq_num;
13462         end if;
13463 
13464         l_stmt_num := 75;
13465         begin
13466             select max(resource_seq_num)
13467             into   l_res_num2
13468             from   wip_sub_operation_resources wsor
13469             where  wsor.organization_id = p_org_id
13470             and    wsor.wip_entity_id   = p_wip_entity_id
13471             and    wsor.operation_seq_num   = p_job_op_seq_num;
13472 
13473         exception
13474             when no_data_found then
13475                 null;
13476         end;
13477 
13478         if l_res_num1 > l_res_num2 then
13479             l_max_resource_seq_num := l_res_num1;
13480         elsif l_res_num2 > l_res_num1 then
13481             l_max_resource_seq_num := l_res_num2;
13482         end if;
13483 
13484         l_max_resource_seq_num := nvl(l_max_resource_seq_num,10);
13485 
13486         l_stmt_num := 78;
13487 
13488         -- Code for debug purpose.... ----
13489         IF lbji_debug = 'Y' THEN
13490                 fnd_file.put_line(fnd_file.log,'validate new resource : ' || l_stmt_num || ' l_max_resource_seq_num ..... '|| l_max_resource_seq_num);
13491         END IF;
13492 
13493     else
13494         l_par_res_seq_num := p_new_res_tbl(p_new_res_tbl.first).parent_seq_num;
13495 
13496         /* Indicates that there is no parent resource being added... ..... */
13497         /*
13498             Validations to be done...
13499             i)   check if any parent resource exists
13500             ii)  Check if any setup resources already exist....
13501             iii) Delete the setup resources...( if no cost incurred )
13502         */
13503         l_stmt_num := 100;
13504 
13505         begin
13506 
13507             l_res_id := 0;
13508 
13509             select wcor.resource_id,wcor.recommended
13510             into   l_res_id,l_res_recommended
13511             from   wsm_copy_op_resources wcor
13512             where  wcor.organization_id = p_org_id
13513             and    wcor.wip_entity_id   = p_wip_entity_id
13514             and    wcor.operation_seq_num    = p_rtg_op_seq_num
13515             and    wcor.resource_seq_num     = l_par_res_seq_num;
13516 
13517             /* sh. i add cond to check if recommeded res.... */
13518 
13519             if l_res_recommended = 'Y' then
13520                 /* Ideally should check here if this resource is getting recommended...*/
13521                 l_hash_str := to_char(p_wip_entity_id) || ':' || to_char(p_rtg_op_seq_num) || ':' || to_char(l_res_id);
13522                 l_hash_value := dbms_utility.get_hash_value(l_hash_str,35,1048576);
13523 
13524                 if (p_res_recom.exists(l_hash_value) and p_res_recom(l_hash_value) = 'N') then
13525                     raise e_invalid_par_seq;
13526                 end if;
13527 
13528             elsif l_res_recommended = 'N' then
13529 
13530                 /* Ideally should check here if this resource is getting recommended...*/
13531                 l_hash_str := to_char(p_wip_entity_id) || ':' || to_char(p_rtg_op_seq_num) || ':' || to_char(l_res_id);
13532                 l_hash_value := dbms_utility.get_hash_value(l_hash_str,35,1048576);
13533 
13534                 if not(p_res_recom.exists(l_hash_value) and p_res_recom(l_hash_value) = 'Y') then
13535                     raise e_invalid_par_seq;
13536                 end if;
13537 
13538             else
13539                 raise e_invalid_par_seq;
13540             end if;
13541         exception
13542             when no_data_found then
13543 
13544                 raise e_invalid_par_seq;
13545 
13546         end;
13547 
13548         l_stmt_num := 110;
13549 
13550         l_error_code := 0;
13551         l_error_msg  := null;
13552         delete_setup_info( p_wip_entity_id     => p_wip_entity_id,
13553                    p_org_id            => p_org_id,
13554                    p_par_res_seq_num   => l_par_res_seq_num,
13555                    p_op_seq_num        => p_rtg_op_seq_num,
13556                    p_handle_curr_op    => 'N',
13557                    x_error_code        => l_error_code,
13558                    x_error_msg         => l_error_msg
13559                  );
13560 
13561         if l_error_code <> 0 then
13562             raise e_setup_costed;
13563         end if;
13564 
13565         l_stmt_num := 120;
13566 
13567         begin
13568             select max(resource_seq_num)
13569             into   l_max_resource_seq_num
13570             from   wsm_copy_op_resources wcor
13571             where  wcor.organization_id = p_org_id
13572             and    wcor.wip_entity_id   = p_wip_entity_id
13573             and    wcor.operation_seq_num   = p_rtg_op_seq_num;
13574 
13575         exception
13576             when no_data_found then
13577                 null;
13578         end;
13579 
13580         l_stmt_num := 122;
13581 
13582         l_max_resource_seq_num := nvl(l_max_resource_seq_num,10);
13583 
13584     end if;
13585 
13586     -- l_new_res_seq_tbl.delete;
13587     l_stmt_num := 125;
13588 
13589     /* get the maximum sequence number and add ten.... */
13590     /* Loop on each record... the validations will be common for all the parent resource and setup resource also....*/
13591     l_index := p_new_res_tbl.first;
13592 
13593     while l_index  is not null loop
13594         /* do all the validations here...... */
13595 
13596         /*  i)   resource seq num not nulll
13597             ii)  start date < completion date
13598             iii) validity of the resource... sh not be disabled..
13599             iv)  autocharge not null
13600             v)   UOM validation
13601             vi)  assigned units > 0
13602             vii) all DS validations....
13603         */
13604 
13605         l_stmt_num := 140.1;
13606         if p_new_res_tbl(l_index).res_seq_num is NULL then
13607 
13608             l_max_resource_seq_num := l_max_resource_seq_num + 10;
13609             p_new_res_tbl(l_index).res_seq_num := l_max_resource_seq_num;
13610 
13611             /* unfortunately have to update the global table also.... */
13612             v_wljdi_resource_seq_num(l_index) := l_max_resource_seq_num;
13613 
13614         elsif p_new_res_tbl(l_index).res_seq_num <=0 then
13615             /* error out... */
13616             l_field_name := 'res_seq_num -- invalid field value';
13617             raise e_invalid_field;
13618         end if;
13619 
13620         l_stmt_num := 140.2;
13621         if l_new_res_seq_tbl.exists(p_new_res_tbl(l_index).res_seq_num) then
13622             /* error out.... */
13623             l_field_name := 'res_seq_num -- duplicate';
13624             raise e_invalid_field;
13625         else
13626             l_new_res_seq_tbl(p_new_res_tbl(l_index).res_seq_num) := (p_new_res_tbl(l_index).res_seq_num);
13627         end if;
13628 
13629         if p_new_res_tbl(l_index).rpl_grp_num is NULL then
13630             /* error out */
13631             l_field_name := 'replacement_grp_num';
13632             raise e_invalid_field;
13633         end if;
13634         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
13635         -- Query up the fields from BR and then validate them...
13636         BEGIN
13637 
13638             select (BR.autocharge_type),
13639                    (BR.unit_of_measure),
13640                    nvl(p_new_res_tbl(l_index).basis_type,BR.default_basis_type),
13641                    uom.uom_class,
13642                    br.batchable,
13643                    nvl(BR.standard_rate_flag,2)
13644             into   p_new_res_tbl(l_index).autocharge_type,
13645                    p_new_res_tbl(l_index).uom_code,
13646                    p_new_res_tbl(l_index).basis_type,
13647                    l_uom_class,
13648                    l_batchable,
13649                    p_new_res_tbl(l_index).standard_rate_flag
13650             from bom_resources BR,
13651                  mtl_units_of_measure_vl uom
13652             where br.resource_id =  p_new_res_tbl(l_index).resource_id
13653             and autocharge_type = nvl(p_new_res_tbl(l_index).autocharge_type,autocharge_type)
13654             and nvl(p_new_res_tbl(l_index).uom_code,br.unit_of_measure) = br.unit_of_measure -- uom.uom_code
13655             and br.unit_of_measure = uom.uom_code
13656             and nvl(BR.standard_rate_flag,-1) = nvl(nvl(p_new_res_tbl(l_index).standard_rate_flag,BR.standard_rate_flag),-1);
13657 
13658         EXCEPTION
13659             WHEN NO_DATA_FOUND THEN
13660                 l_field_name := 'Resource Information';
13661                 raise e_invalid_field;
13662 
13663         END;
13664 
13665         if p_new_res_tbl(l_index).autocharge_type = 4 then
13666             raise e_no_po_move_res;
13667         end if;
13668 
13669 
13670         if nvl(p_new_res_tbl(l_index).assigned_units,-1) <= 0 then
13671             -- error out.... --
13672             l_field_name := 'assigned units';
13673             raise e_invalid_field;
13674         end if;
13675 
13676         if nvl(p_new_res_tbl(l_index).usage_rate_or_amount,-1) <=0 then
13677             -- error out --
13678             l_field_name := 'usage_rate_or_amount';
13679             raise e_invalid_field;
13680         end if;
13681 
13682 
13683         if p_new_res_tbl(l_index).basis_type <> 2 then
13684             -- error out --
13685             l_field_name := 'basis type for setup resource';
13686             raise e_invalid_field;
13687         end if;
13688 
13689 
13690         if p_new_res_tbl(l_index).scheduled_flag is NULL then
13691             -- error out --
13692             l_field_name := 'schedule_flag';
13693             raise e_invalid_field;
13694         else
13695             begin
13696                 select 1
13697                 into l_dummy
13698                 from mfg_lookups
13699                 where LOOKUP_TYPE  = 'BOM_RESOURCE_SCHEDULE_TYPE'
13700                 and   lookup_code = p_new_res_tbl(l_index).scheduled_flag;
13701             exception
13702                 when others then
13703                     -- error out --
13704                     l_field_name := 'schedule_flag';
13705                     raise e_invalid_field;
13706             end;
13707         end if;
13708 
13709         l_stmt_num := 142;
13710 
13711         -- Combined validations for UOM and autocharge... --
13712         l_hour_uom := FND_PROFILE.value('BOM:HOUR_UOM_CODE');
13713         l_hour_uom_class := wip_op_resources_utilities.get_uom_class(l_hour_uom);
13714         l_autocharge_type := p_new_res_tbl(l_index).autocharge_type;
13715 
13716         l_stmt_num := 145;
13717 
13718         if l_hour_uom_class = l_uom_class then
13719             l_uom_time_class_flag := true;
13720         else
13721             l_uom_time_class_flag := false;
13722         end if;
13723 
13724         l_stmt_num := 148;
13725 
13726         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
13727             /* error out... */
13728             l_field_name := 'Invalid usage rate for the autocharge/UOM combination';
13729             raise e_invalid_field;
13730         end if;
13731 
13732         l_stmt_num := 150;
13733         if p_handle_wip_cur_op = 'Y' then
13734 
13735             l_stmt_num := 155;
13736             /* check in WOR... */
13737             begin
13738                 select 1
13739                 into l_dummy
13740                 from wip_operation_resources wor
13741                 where wor.organization_id = p_org_id
13742                 and wor.wip_entity_id  = p_wip_entity_id
13743                 and wor.operation_seq_num = p_job_op_seq_num
13744                 and wor.resource_seq_num = p_new_res_tbl(l_index).res_seq_num;
13745 
13746                 raise e_invalid_res_seq_num;
13747 
13748             exception
13749                 when no_data_found then
13750                     null;
13751             end;
13752 
13753             /* check in WSOR.... */
13754             l_stmt_num := 157;
13755 
13756             begin
13757                 select 1
13758                 into l_dummy
13759                 from wip_sub_operation_resources wsor
13760                 where wsor.organization_id = p_org_id
13761                 and wsor.wip_entity_id  = p_wip_entity_id
13762                 and wsor.operation_seq_num = p_job_op_seq_num
13763                 and wsor.resource_seq_num = p_new_res_tbl(l_index).res_seq_num;
13764 
13765                 raise e_invalid_res_seq_num;
13766 
13767             exception
13768                 when no_data_found then
13769                    null;
13770             end;
13771 
13772             l_stmt_num := 160;
13773 
13774             begin
13775 
13776                 select nvl(p_new_res_tbl(l_index).dept_id,wo.department_id)
13777                 into p_new_res_tbl(l_index).dept_id
13778                 from bom_resources br,bom_department_resources bdr, wip_operations wo
13779                 where br.resource_id = p_new_res_tbl(l_index).resource_id
13780                 and  br.resource_code = p_new_res_tbl(l_index).resource_code
13781                 and (br.disable_date > sysdate  or br.disable_date is null)
13782                 and br.organization_id = p_org_id
13783                 and br.autocharge_type = p_new_res_tbl(l_index).autocharge_type
13784                 and bdr.resource_id = p_new_res_tbl(l_index).resource_id
13785                 and bdr.department_id = nvl(p_new_res_tbl(l_index).dept_id,wo.department_id)
13786                 and wo.organization_id = p_org_id
13787                 and wo.wip_entity_id  = p_wip_entity_id
13788                 and wo.operation_seq_num = p_job_op_seq_num;
13789 
13790                 l_stmt_num := 160.1;
13791 
13792             exception
13793                 when no_data_found then
13794                     /* Not a valid resource error out...*/
13795                     raise e_invalid_res;
13796 
13797                 when too_many_rows then
13798                     /* Ideally not possible */
13799                     /* what to do in this case..... */
13800                     raise e_invalid_res;
13801             end;
13802 
13803             l_stmt_num := 170;
13804             begin
13805                 select 1
13806                 into l_dummy
13807                 from wip_operations wo
13808                 where wo.wip_entity_id = p_wip_entity_id
13809                 and   wo.organization_id = p_org_id
13810                 and   wo.operation_seq_num = p_job_op_seq_num
13811                 and   p_new_res_tbl(l_index).start_date >= wo.first_unit_start_date
13812                 and   p_new_res_tbl(l_index).start_date <=  nvl(last_unit_start_date,p_new_res_tbl(l_index).start_date)
13813                 and   p_new_res_tbl(l_index).completion_date <= nvl(wo.last_unit_completion_date,p_new_res_tbl(l_index).completion_date)
13814                 and   p_new_res_tbl(l_index).completion_date >= nvl(wo.last_unit_start_date,p_new_res_tbl(l_index).completion_date);
13815 
13816             exception
13817                 when no_data_found then
13818                     /* invlaid dates */
13819                     raise e_invalid_res_dates;
13820             end;
13821         else
13822             l_stmt_num := 180;
13823             begin
13824                 select 1
13825                 into l_dummy
13826                 from wsm_copy_op_resources wcor
13827                 where wcor.organization_id = p_org_id
13828                 and wcor.wip_entity_id  = p_wip_entity_id
13829                 and wcor.operation_seq_num = p_rtg_op_seq_num
13830                 and wcor.resource_seq_num = p_new_res_tbl(l_index).res_seq_num;
13831 
13832                 raise e_invalid_res_seq;
13833 
13834             exception
13835                 when no_data_found then
13836                     null;
13837             end;
13838 
13839             l_stmt_num := 185;
13840             -- There are two SQLs for more meaningful messages...
13841             begin
13842 
13843                 select nvl(p_new_res_tbl(l_index).dept_id,wco.department_id)
13844                 into p_new_res_tbl(l_index).dept_id
13845                 from bom_resources br,bom_department_resources bdr, wsm_copy_operations wco
13846                 where br.resource_id = p_new_res_tbl(l_index).resource_id
13847                 and  br.resource_code = p_new_res_tbl(l_index).resource_code
13848                 and (br.disable_date > sysdate  or br.disable_date is null)
13849                 and br.organization_id = p_org_id
13850                 and bdr.resource_id = p_new_res_tbl(l_index).resource_id
13851                 and bdr.department_id = nvl(nvl(p_new_res_tbl(l_index).dept_id,wco.department_id),bdr.department_id)
13852                 and wco.organization_id = p_org_id
13853                 and wco.wip_entity_id  = p_wip_entity_id
13854                 and wco.operation_seq_num = p_rtg_op_seq_num;
13855 
13856             exception
13857                 when no_data_found then
13858                     /* Not a valid resource error out...*/
13859                     raise e_invalid_res;
13860 
13861                 when too_many_rows then
13862                     /* Not possible ideally */
13863                     /* what to do in this case..... */
13864                     raise e_invalid_res;
13865             end;
13866 
13867             l_stmt_num := 190;
13868             begin
13869                 select 1
13870                 into l_dummy
13871                 from wsm_copy_operations wco
13872                 where wco.organization_id = p_org_id
13873                 and   wco.wip_entity_id   = p_wip_entity_id
13874                 and   wco.operation_seq_num = p_rtg_op_seq_num
13875                 and   p_new_res_tbl(l_index).start_date >= wco.reco_start_date
13876                 and   p_new_res_tbl(l_index).start_date <=  nvl(reco_completion_date,p_new_res_tbl(l_index).start_date)
13877                 -- and   nvl(p_new_res_tbl(l_index).completion_date,nvl(reco_completion_date,sysdate)) <= nvl(reco_completion_date,sysdate)
13878                 and  ( (p_new_res_tbl(l_index).completion_date IS NULL and reco_completion_date IS NULL)
13879                        OR
13880                        ( (p_new_res_tbl(l_index).completion_date IS NOT NULL and reco_completion_date IS NOT NULL)
13881                           AND
13882                      (p_new_res_tbl(l_index).completion_date <= reco_completion_date)
13883                        )
13884                        OR
13885                        (p_new_res_tbl(l_index).completion_date IS NOT NULL and reco_completion_date IS NULL)
13886                       )
13887                 and   nvl(p_new_res_tbl(l_index).completion_date,nvl(reco_completion_date,wco.reco_start_date)) >= wco.reco_start_date;
13888 
13889             exception
13890                 when no_data_found then
13891                     /* invlaid dates */
13892                     raise e_invalid_res_dates;
13893             end;
13894 
13895 
13896         end if;
13897 
13898         /* Non-NULL validations */
13899         /* Prposed ones are ..
13900            i) sh we validate UOM code....
13901         */
13902 
13903         /* DS : validations */
13904         l_stmt_num := 200;
13905         if p_new_res_tbl(l_index).setup_id is NULL and p_new_res_tbl(l_index).setup_code is NOT NULL then
13906              begin
13907                     select  bst.setup_id
13908                     into    p_new_res_tbl(l_index).setup_id
13909                     from    bom_setup_types bst
13910                     where   bst.setup_code      = p_new_res_tbl(l_index).setup_code
13911                     and     bst.organization_id = p_org_id;
13912 
13913              exception
13914                 when no_data_found then
13915                         raise e_invalid_setup_code;
13916                     when others then
13917                         null;
13918              end;
13919         end if;
13920         if p_new_res_tbl(l_index).setup_id is not null then
13921 
13922             l_stmt_num := 210;
13923             BEGIN
13924 
13925                 select  1
13926                 into    l_dummy
13927                 from    bom_resource_setups brs
13928                 where   brs.resource_id     = p_new_res_tbl(l_index).resource_id
13929                 and     brs.organization_id = p_org_id
13930                 and     brs.setup_id        = p_new_res_tbl(l_index).setup_id;
13931 
13932             EXCEPTION
13933                 when no_data_found then
13934                     raise e_invalid_setup_id;
13935 
13936             END;
13937         end if;
13938         l_stmt_num := 220;
13939 
13940         if (p_new_res_tbl(l_index).batch_id is not null ) then
13941 
13942             l_stmt_num := 230;
13943 
13944             if l_batchable <> 1 then
13945                 -- error out..
13946                 raise e_res_not_batchable;
13947             end if;
13948 
13949             if (p_new_res_tbl(l_index).max_assg_units <> 1 or p_new_res_tbl(l_index).assigned_units <> 1)
13950             then
13951                  raise e_invalid_batch_max_assg_units;
13952             end if;
13953         end if;
13954 
13955         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
13956             l_stmt_num := 240;
13957             if (p_new_res_tbl(l_index).setup_id is null)
13958             then
13959                  raise e_invalid_seq;
13960             end if;
13961 
13962             if (p_new_res_tbl(l_index).setup_id  is not null)
13963                and
13964                (p_new_res_tbl(l_index).max_assg_units <> 1 or p_new_res_tbl(l_index).assigned_units <> 1)
13965             then
13966                  raise e_invalid_setup_max_assg_units;
13967              end if;
13968 
13969         end if;
13970 
13971         l_stmt_num := 250;
13972 		-- Modified the condition for fixing bug 5471266.
13973 		-- This allows max_assg_units to be passed as null while adding setup resource.
13974         /* if nvl(p_new_res_tbl(l_index).max_assg_units,0) < p_new_res_tbl(l_index).assigned_units then */
13975 		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
13976                 raise e_invalid_max_assg_units;
13977         end if;
13978 
13979         -- ST Fix for bug 4240165
13980         -- Now copy the values derived into the global table also..
13981         v_wljdi_uom_code(l_index)               := p_new_res_tbl(l_index).uom_code;
13982         v_wljdi_basis_type(l_index)             := p_new_res_tbl(l_index).basis_type;
13983         v_wljdi_res_autocharge_type(l_index)    := p_new_res_tbl(l_index).autocharge_type;
13984         v_wljdi_standard_rate_flag(l_index)     := p_new_res_tbl(l_index).standard_rate_flag;
13985         v_wljdi_department_id(l_index)          := p_new_res_tbl(l_index).dept_id;
13986 
13987         l_index := p_new_res_tbl.next(l_index);
13988 
13989     end loop;
13990 
13991     x_error_code := 0;
13992 
13993 exception
13994 
13995     when e_invalid_res_seq_num then
13996 
13997         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
13998         fnd_message.set_token('FLD_NAME', ' resource sequence number ');
13999         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14000 
14001         fnd_file.put_line(fnd_file.log,x_error_msg);
14002 
14003         x_error_code := -1;
14004 
14005     when e_invalid_par_seq  then
14006         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
14007         fnd_message.set_token('FLD_NAME', ' parent resource sequence number ');
14008         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14009         fnd_file.put_line(fnd_file.log,x_error_msg);
14010         x_error_code := -1;
14011 
14012     when e_invalid_res   then
14013         --Bug 4704351:Resource id validation is already done in the prev sql.So
14014         --this exception is used for resource and department mismatch.
14015         --fnd_message.set_name('WSM','WSM_INVALID_FIELD');
14016         --fnd_message.set_token('FLD_NAME', ' resource id ');
14017         fnd_message.set_name('WSM','WSM_INVALID_RES_DEPT');
14018         --Set up Resource should be from the same department as Parent Resource
14019         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14020         fnd_file.put_line(fnd_file.log,x_error_msg);
14021         x_error_code := -1;
14022 
14023     when e_invalid_res_dates  then
14024         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
14025         fnd_message.set_token('FLD_NAME', ' resource start/completion dates ');
14026         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14027         fnd_file.put_line(fnd_file.log,x_error_msg);
14028         x_error_code := -1;
14029 
14030     when e_invalid_res_seq  then
14031 
14032         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
14033         fnd_message.set_token('FLD_NAME', ' resource seq num ');
14034         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14035         fnd_file.put_line(fnd_file.log,x_error_msg);
14036         x_error_code := -1;
14037 
14038     when e_invalid_seq then
14039         fnd_message.set_name('WSM','WSM_INVALID_SEQ');
14040         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get ;
14041         fnd_file.put_line(fnd_file.log,x_error_msg);
14042         x_error_code := -1;
14043 
14044     when e_invalid_setup_id then
14045         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
14046         fnd_message.set_token('FLD_NAME', ' setup id ');
14047         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get ;
14048         fnd_file.put_line(fnd_file.log,x_error_msg);
14049         x_error_code := -1;
14050 
14051     when e_invalid_field  then
14052 	    /*Modified error handling mechanism for bug 5469917. */
14053 	    fnd_message.set_name('WSM','WSM_INVALID_FIELD');
14054         fnd_message.set_token('FLD_NAME', l_field_name);
14055         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14056         fnd_file.put_line(fnd_file.log,x_error_msg);
14057         x_error_code := -1;
14058 
14059     when e_invalid_max_assg_units then
14060 
14061         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
14062         fnd_message.set_token('FLD_NAME', ' maximum assigned units ');
14063         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14064         fnd_file.put_line(fnd_file.log,x_error_msg);
14065         x_error_code := -1;
14066 
14067     when e_invalid_setup_max_assg_units then
14068 
14069         fnd_message.set_name('WSM','WSM_INVALID_SETUP_MAX_ASSG');
14070 
14071         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14072 
14073         fnd_file.put_line(fnd_file.log,x_error_msg);
14074         x_error_code := -1;
14075 
14076     when e_setup_costed then
14077 
14078         fnd_message.set_name('WSM','WSM_INVALID_SETUP_MAX_ASSG');
14079 
14080         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14081         fnd_file.put_line(fnd_file.log,x_error_msg);
14082         x_error_code := -1;
14083 
14084     when e_invalid_batch_max_assg_units then
14085 
14086         fnd_message.set_name('WSM','WSM_INVALID_BATCH_MAX_ASSG');
14087 
14088         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14089         x_error_code := -1;
14090         fnd_file.put_line(fnd_file.log,x_error_msg);
14091 
14092     when e_res_not_batchable then
14093 
14094         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
14095         fnd_message.set_token('FLD_NAME', ' batch id for resource ');
14096 
14097         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14098         fnd_file.put_line(fnd_file.log,x_error_msg);
14099         x_error_code := -1;
14100 
14101     when e_no_po_move_res then
14102         fnd_message.set_name('WSM','WSM_CURR_OP_RES_PO_MOVE');
14103         fnd_message.set_token('FLD_NAME', ' resource ');
14104         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || fnd_message.get;
14105         fnd_file.put_line(fnd_file.log,x_error_msg);
14106         x_error_code := -1;
14107 
14108     when others then
14109         x_error_msg := ' validate_new_resource :  ' || l_stmt_num || ': ' || SQLCODE || ' : ' || substrb(SQLERRM,1,1000) ;
14110         fnd_file.put_line(fnd_file.log,x_error_msg);
14111         x_error_code := -1;
14112 
14113 end validate_new_resource;
14114 
14115 /* ST : Detailed scheduling start */
14116 /***************************************************************************
14117 
14118 Procedure to take care of
14119 
14120 i) setup resources to an existing resource
14121 
14122 All these additions either at current op or future op
14123 
14124 ***************************************************************************/
14125 
14126 Procedure add_resource(p_wip_entity_id      in      number,
14127                        p_org_id             in      number,
14128                        p_job_op_seq_num     in      number,
14129                        p_cur_job_op_seq_num in      number,
14130                        p_rtg_op_seq_num     in      number,
14131                        p_load_type          in      number,
14132                        p_sub_type           in      number,
14133                        p_res_recom          in      t_res_recom,
14134                        p_last_updt_date     in      date,
14135                        p_last_updt_by       in      number,
14136                        p_creation_date      in      date,
14137                        p_created_by         in      number,
14138                        p_last_updt_login    in      number,
14139                        p_request_id         in      number,
14140                        p_program_appl_id    in      number,
14141                        p_program_id         in      number,
14142                        p_prog_updt_date     in      date,
14143                        p_global_index       in out nocopy   number,
14144                        x_error_code         in out nocopy   number,
14145                        x_error_msg          in out nocopy   varchar2
14146                       )
14147                       is
14148 
14149 
14150 l_new_res_tbl           new_res_tbl;
14151 l_setup_index_tbl       t_number;
14152 l_par_res_index         number;
14153 l_temp                  number;
14154 
14155 l_error_code            number;
14156 l_error_msg             varchar2(2000);
14157 l_stmt_num              number := 0;
14158 
14159 
14160 l_index             number;
14161 l_found             number;
14162 
14163 begin
14164 
14165     l_stmt_num := 10;
14166 
14167     x_error_code := 0;
14168     x_error_msg  := null;
14169 
14170     /* 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
14171        its setup resource
14172     ... */
14173 
14174     l_new_res_tbl.delete;
14175     l_par_res_index := null;
14176     l_setup_index_tbl.delete;
14177 
14178     /* ST : Detailed Scheduling : Support adding resources to current op */
14179     /* Update the WIP tables in this case..... */
14180     --Bug 5474321: Setup resource should be imported even for previous operations.
14181     --if ( ((p_job_op_seq_num IS NOT NULL) and (p_job_op_seq_num = p_cur_job_op_seq_num) )
14182     if ( ((p_job_op_seq_num IS NOT NULL) )
14183          OR
14184          ((p_rtg_op_seq_num IS NOT NULL))
14185        )
14186     then
14187 
14188         l_stmt_num := 20;
14189         /*
14190           First clear the validation table of records
14191           First load it into the validation table of records ( index will be the resource_id )
14192           Check if has a setup res ( loop through.... )
14193           if found add that record also to the validation table of records
14194           call the validate_add_new_res procedure
14195           if successful then add the resources in the validation
14196           Load the resources into  local PL/SQL tables and delete them from v_wldji_
14197         */
14198 
14199         if v_wljdi_parent_seq_num(p_global_index) is not null then /* Indicates that it is a setup resource....*/
14200 
14201             l_stmt_num := 30;
14202             /* Search through the v_wldji_ for with similar load type for its parent resource...and also sibling setup resources*/
14203             /* Load this into the validation PL/SQL table... */
14204 
14205             l_temp := p_global_index;
14206             l_setup_index_tbl(l_temp) := p_global_index;
14207 
14208             l_new_res_tbl(l_temp).job_op_seq_num                := v_wljdi_job_op_seq_num(l_temp);
14209             l_new_res_tbl(l_temp).rtg_op_seq_num                := v_wljdi_routing_op_seq_num(l_temp);
14210             l_new_res_tbl(l_temp).resource_id                   := v_wljdi_resource_id_new(l_temp);
14211             l_new_res_tbl(l_temp).resource_code                 := v_wljdi_resource_code_new(l_temp);
14212             l_new_res_tbl(l_temp).dept_id                       := v_wljdi_department_id(l_temp);
14213             l_new_res_tbl(l_temp).dept_code                     := v_wljdi_department_code(l_temp);
14214             l_new_res_tbl(l_temp).res_seq_num                   := v_wljdi_resource_seq_num(l_temp);
14215             l_new_res_tbl(l_temp).sub_grp_num                   := v_wljdi_substitute_group_num(l_temp);
14216             l_new_res_tbl(l_temp).rpl_grp_num                   := v_wljdi_replacement_group_num(l_temp);
14217             l_new_res_tbl(l_temp).activity_id                   := v_wljdi_activity_id(l_temp);
14218             l_new_res_tbl(l_temp).standard_rate_flag            := v_wljdi_standard_rate_flag(l_temp);
14219             l_new_res_tbl(l_temp).assigned_units                := v_wljdi_assigned_units(l_temp);
14220             l_new_res_tbl(l_temp).usage_rate_or_amount          := v_wljdi_usage_rate_or_amt(l_temp);
14221             l_new_res_tbl(l_temp).usage_rate_or_amount_inverse  := 0; /* calculate it shiva ..... */
14222             l_new_res_tbl(l_temp).uom_code                      := v_wljdi_uom_code(l_temp);
14223             l_new_res_tbl(l_temp).basis_type                    := v_wljdi_basis_type(l_temp);
14224             l_new_res_tbl(l_temp).scheduled_flag                := v_wljdi_scheduled_flag(l_temp);
14225             l_new_res_tbl(l_temp).autocharge_type               := v_wljdi_res_autocharge_type(l_temp);
14226             l_new_res_tbl(l_temp).schedule_seq_num              := v_wljdi_schedule_seq_num(l_temp);
14227             l_new_res_tbl(l_temp).setup_id                      := v_wljdi_setup_id(l_temp);
14228             l_new_res_tbl(l_temp).setup_code                    := v_wljdi_setup_code(l_temp);
14229             l_new_res_tbl(l_temp).start_date                    := v_wljdi_start_date(l_temp);
14230             l_new_res_tbl(l_temp).firm_type                     := v_wljdi_firm_type(l_temp);
14231             l_new_res_tbl(l_temp).grp_seq_id                    := v_wljdi_grp_seq_id(l_temp);
14232             l_new_res_tbl(l_temp).grp_seq_num                   := v_wljdi_grp_seq_num(l_temp);
14233             l_new_res_tbl(l_temp).max_assg_units                := v_wljdi_max_assg_units(l_temp);
14234             l_new_res_tbl(l_temp).parent_seq_num                := v_wljdi_parent_seq_num(l_temp);
14235             l_new_res_tbl(l_temp).batch_id                      := v_wljdi_batch_id(l_temp);
14236             l_new_res_tbl(l_temp).completion_date               := v_wljdi_completion_date(l_temp);
14237 
14238             l_temp := v_wljdi_record_id.next(p_global_index);
14239 
14240             l_stmt_num := 35;
14241             /* start the search */
14242             while (l_temp is not null) loop
14243 
14244                    -- Removed the above code (see the previous revision)
14245                    -- Cannot add a parent resource...
14246                    if ( (v_wljdi_parent_seq_num(l_temp) = v_wljdi_parent_seq_num(p_global_index)) and
14247                     (v_wljdi_load_type(l_temp)     = p_load_type) and
14248                     (v_wljdi_substitution_type(l_temp)     = p_sub_type) and
14249                     ((v_wljdi_job_op_seq_num(l_temp)        = p_job_op_seq_num)
14250                        or
14251                           (v_wljdi_routing_op_seq_num(l_temp)   = p_rtg_op_seq_num)
14252                         )
14253                      )
14254                    then
14255                         l_stmt_num := 50;
14256 
14257                         /* indicates a sibling setup resource */
14258                         -- We are using a separate index so that it will be contiguous..
14259                         -- or else we will get an error during the BULK INSERT
14260 
14261                         l_setup_index_tbl(l_temp) := l_temp;
14262 
14263                         l_new_res_tbl(l_temp).job_op_seq_num                := v_wljdi_job_op_seq_num(l_temp);
14264                         l_new_res_tbl(l_temp).rtg_op_seq_num                := v_wljdi_routing_op_seq_num(l_temp);
14265                         l_new_res_tbl(l_temp).resource_id                   := v_wljdi_resource_id_new(l_temp);
14266                         l_new_res_tbl(l_temp).resource_code                 := v_wljdi_resource_code_new(l_temp);
14267                         l_new_res_tbl(l_temp).dept_id                       := v_wljdi_department_id(l_temp);
14268                         l_new_res_tbl(l_temp).dept_code                     := v_wljdi_department_code(l_temp);
14269                         l_new_res_tbl(l_temp).res_seq_num                   := v_wljdi_resource_seq_num(l_temp);
14270                         l_new_res_tbl(l_temp).sub_grp_num                   := v_wljdi_substitute_group_num(l_temp);
14271                         l_new_res_tbl(l_temp).rpl_grp_num                   := v_wljdi_replacement_group_num(l_temp);
14272                         l_new_res_tbl(l_temp).activity_id                   := v_wljdi_activity_id(l_temp);
14273                         l_new_res_tbl(l_temp).standard_rate_flag            := v_wljdi_standard_rate_flag(l_temp);
14274                         l_new_res_tbl(l_temp).assigned_units                := v_wljdi_assigned_units(l_temp);
14275                         l_new_res_tbl(l_temp).usage_rate_or_amount          := v_wljdi_usage_rate_or_amt(l_temp);
14276                         l_new_res_tbl(l_temp).usage_rate_or_amount_inverse  := 0; /* calculate it shiva ..... */
14277                         l_new_res_tbl(l_temp).uom_code                      := v_wljdi_uom_code(l_temp);
14278                         l_new_res_tbl(l_temp).basis_type                    := v_wljdi_basis_type(l_temp);
14279                         l_new_res_tbl(l_temp).scheduled_flag                := v_wljdi_scheduled_flag(l_temp);
14280                         l_new_res_tbl(l_temp).autocharge_type               := v_wljdi_res_autocharge_type(l_temp);
14281                         l_new_res_tbl(l_temp).schedule_seq_num              := v_wljdi_schedule_seq_num(l_temp);
14282                         l_new_res_tbl(l_temp).setup_id                      := v_wljdi_setup_id(l_temp);
14283                         l_new_res_tbl(l_temp).setup_code                    := v_wljdi_setup_code(l_temp);
14284                         l_new_res_tbl(l_temp).start_date                    := v_wljdi_start_date(l_temp);
14285                         l_new_res_tbl(l_temp).firm_type                     := v_wljdi_firm_type(l_temp);
14286                         l_new_res_tbl(l_temp).grp_seq_id                    := v_wljdi_grp_seq_id(l_temp);
14287                         l_new_res_tbl(l_temp).grp_seq_num                   := v_wljdi_grp_seq_num(l_temp);
14288                         l_new_res_tbl(l_temp).max_assg_units                := v_wljdi_max_assg_units(l_temp);
14289                         l_new_res_tbl(l_temp).parent_seq_num                := v_wljdi_parent_seq_num(l_temp);
14290                         l_new_res_tbl(l_temp).batch_id                      := v_wljdi_batch_id(l_temp);
14291                         l_new_res_tbl(l_temp).completion_date               := v_wljdi_completion_date(l_temp);
14292 
14293                    end if;
14294 
14295                    l_temp := v_wljdi_record_id.next(l_temp);
14296 
14297              end loop;
14298 
14299         end if;
14300     else
14301         /* error out.... */
14302         fnd_message.set_name('WSM','WSM_NULL_FIELD');
14303         fnd_message.set_token('FLD_NAME', 'job_op_seq_num/rtg_op_seq_num');
14304         x_error_msg := fnd_message.get;
14305         x_error_code := -1;
14306         return; --raise e_wljdi_common_error;
14307     end if;
14308     l_stmt_num := 80;
14309 
14310     --Bug 5474321: Setup resource should be imported even for previous operations.
14311     --if ( (p_job_op_seq_num IS NOT NULL) and (p_job_op_seq_num = p_cur_job_op_seq_num) ) then
14312     if ( p_job_op_seq_num IS NOT NULL) THEN
14313 
14314 
14315         l_stmt_num := 90;
14316         l_error_code := 0;
14317         l_error_msg  := null;
14318 
14319         l_stmt_num := 100;
14320         /* call the validate procedure now.... */
14321         validate_new_resource(p_wip_entity_id     => p_wip_entity_id,
14322                               p_org_id            => p_org_id,
14323                               p_new_res_tbl       => l_new_res_tbl,
14324                               p_setup_index_tbl   => l_setup_index_tbl,
14325                               p_res_recom         => p_res_recom,
14326                               p_handle_wip_cur_op => 'Y',
14327                               p_job_op_seq_num    => p_job_op_seq_num,
14328                               p_rtg_op_seq_num    => null,
14329                               x_error_code        => l_error_code,
14330                               x_error_msg         => l_error_msg);
14331 
14332         if l_error_code <> 0 then
14333             /* error out validation failed,,,, set p_error_msg to the error message */
14334             x_error_msg  := l_error_msg;
14335             x_error_code := -1;
14336             return; --raise e_wljdi_common_error;
14337         end if;
14338         l_stmt_num := 110;
14339 
14340         forall i in l_setup_index_tbl.first..l_setup_index_tbl.last
14341 
14342             /* issue the insert statement */
14343              insert into wip_operation_resources
14344              (
14345                 wip_entity_id,
14346                 operation_seq_num,
14347                 resource_seq_num,
14348                 organization_id,
14349                 department_id,
14350                 --repetitive_schedule_id,
14351 
14352                 resource_id,
14353                 uom_code,
14354                 basis_type,
14355                 usage_rate_or_amount,
14356 
14357                 activity_id,
14358                 scheduled_flag,
14359                 assigned_units,
14360 
14361                 maximum_assigned_units,
14362                 batch_id,
14363                 firm_flag,
14364                 group_sequence_id,
14365                 group_sequence_number,
14366                 parent_resource_seq,
14367 
14368                 autocharge_type,
14369                 standard_rate_flag,
14370                 applied_resource_units,
14371                 applied_resource_value,
14372                 start_date,
14373                 completion_date,
14374 
14375                 attribute_category,
14376                 attribute1,
14377                 attribute2,
14378                 attribute3,
14379                 attribute4,
14380                 attribute5,
14381                 attribute6,
14382                 attribute7,
14383                 attribute8,
14384                 attribute9,
14385                 attribute10,
14386                 attribute11,
14387                 attribute12,
14388                 attribute13,
14389                 attribute14,
14390                 attribute15,
14391 
14392                 schedule_seq_num,
14393                 substitute_group_num,
14394                 replacement_group_num,
14395                 --principle_flag,
14396                 setup_id,
14397 
14398                 last_update_date,
14399                 last_updated_by,
14400                 creation_date,
14401                 created_by,
14402                 last_update_login,
14403                 request_id,
14404                 program_application_id,
14405                 program_id,
14406                 program_update_date
14407                 )
14408             values
14409             (
14410                p_wip_entity_id,
14411                p_job_op_seq_num,
14412                v_wljdi_resource_seq_num(i),
14413                p_org_id,
14414                v_wljdi_department_id(i),
14415                -- check,
14416 
14417                v_wljdi_resource_id_new(i),
14418                v_wljdi_uom_code(i),
14419                v_wljdi_basis_type(i),
14420                v_wljdi_usage_rate_or_amt(i),
14421 
14422                v_wljdi_activity_id(i),
14423                v_wljdi_scheduled_flag(i),
14424                v_wljdi_assigned_units(i),
14425                /* Modified Query by Adding NVL during insert of max_assg_units. Bug 5471266 */
14426                nvl(v_wljdi_max_assg_units(i),v_wljdi_assigned_units(i)),
14427                v_wljdi_batch_id(i),
14428 			   /* Modified Query by Adding NVL during insert of firm type. Bug 5471266 */
14429                nvl(v_wljdi_firm_type(i),0),
14430                v_wljdi_grp_seq_id(i),
14431                v_wljdi_grp_seq_num(i),
14432                v_wljdi_parent_seq_num(i),
14433 
14434                v_wljdi_res_autocharge_type(i),
14435                v_wljdi_standard_rate_flag(i),
14436                0,
14437                0,
14438                v_wljdi_start_date(i),
14439                v_wljdi_completion_date(i),
14440 
14441                v_wljdi_attribute_category(i),
14442                v_wljdi_attribute1(i),
14443                v_wljdi_attribute2(i),
14444                v_wljdi_attribute3(i),
14445                v_wljdi_attribute4(i),
14446                v_wljdi_attribute5(i),
14447                v_wljdi_attribute6(i),
14448                v_wljdi_attribute7(i),
14449                v_wljdi_attribute8(i),
14450                v_wljdi_attribute9(i),
14451                v_wljdi_attribute10(i),
14452                v_wljdi_attribute11(i),
14453                v_wljdi_attribute12(i),
14454                v_wljdi_attribute13(i),
14455                v_wljdi_attribute14(i),
14456                v_wljdi_attribute15(i),
14457 
14458                v_wljdi_schedule_seq_num(i),
14459                v_wljdi_substitute_group_num(i),
14460                v_wljdi_replacement_group_num(i),
14461                v_wljdi_setup_id(i),
14462 
14463                p_last_updt_date,
14464                p_last_updt_by,
14465                p_creation_date,
14466                p_created_by,
14467                p_last_updt_login,
14468                p_request_id,
14469                p_program_appl_id,
14470                p_program_id,
14471                p_prog_updt_date
14472             );
14473 
14474         l_index := l_setup_index_tbl.first;
14475 
14476         while l_index is not null loop
14477             if v_wljdi_res_autocharge_type(l_index) = 3 then
14478 
14479                 wip_osp.create_requisition(
14480                                  p_wip_entity_id          => p_wip_entity_id,
14481                                  p_organization_id        => p_org_id,
14482                                  p_repetitive_schedule_id => null,
14483                                  p_operation_seq_num      => p_job_op_seq_num,
14484                                  p_resource_seq_num       => v_wljdi_resource_seq_num(l_index),
14485                                  p_run_reqimport          => wip_constants.no
14486                               );
14487 
14488                 g_num_of_osp_exists := nvl(g_num_of_osp_exists,0) + 1;
14489 
14490             end if;
14491 
14492             l_index := l_setup_index_tbl.next(l_index);
14493 
14494         end loop;
14495 
14496     elsif (p_rtg_op_seq_num IS NOT NULL) then
14497 
14498         l_stmt_num := 120;
14499 
14500         l_error_code := 0;
14501         l_error_msg  := null;
14502         /* call the validate procedure now.... */
14503         validate_new_resource(p_wip_entity_id     => p_wip_entity_id,
14504                               p_org_id            => p_org_id,
14505                               p_new_res_tbl       => l_new_res_tbl,
14506                               p_setup_index_tbl   => l_setup_index_tbl,
14507                               p_res_recom         => p_res_recom,
14508                               p_handle_wip_cur_op => 'N',
14509                               p_job_op_seq_num    => null,
14510                               p_rtg_op_seq_num    => p_rtg_op_seq_num,
14511                               x_error_code        => l_error_code,
14512                               x_error_msg         => l_error_msg);
14513 
14514         if l_error_code <> 0 then
14515             /* error out validation failed,,,, set p_error_msg to the error message */
14516             x_error_msg  := l_error_msg;
14517             x_error_code := -1;
14518             return; --raise e_wljdi_common_error;
14519         end if;
14520 
14521         l_stmt_num := 130;
14522         forall i in l_setup_index_tbl.first..l_setup_index_tbl.last
14523 
14524             /* issue the insert statement */
14525              insert into wsm_copy_op_resources
14526              (
14527                 wip_entity_id,
14528                 operation_seq_num,
14529                 resource_seq_num,
14530                 organization_id,
14531                 department_id,
14532                 --repetitive_schedule_id,
14533                 recommended,
14534 
14535                 resource_id,
14536                 resource_code,
14537                 uom_code,
14538                 basis_type,
14539                 usage_rate_or_amount,
14540                 usage_rate_or_amount_inverse,
14541                 activity_id,
14542                 schedule_flag,
14543                 assigned_units,
14544 
14545                 max_assigned_units,
14546                 batch_id,
14547                 firm_type,
14548                 group_sequence_id,
14549                 group_sequence_num,
14550                 parent_resource_seq_num,
14551 
14552                 autocharge_type,
14553                 standard_rate_flag,
14554                 reco_start_date,
14555                 reco_completion_date,
14556 
14557                 attribute_category,
14558                 attribute1,
14559                 attribute2,
14560                 attribute3,
14561                 attribute4,
14562                 attribute5,
14563                 attribute6,
14564                 attribute7,
14565                 attribute8,
14566                 attribute9,
14567                 attribute10,
14568                 attribute11,
14569                 attribute12,
14570                 attribute13,
14571                 attribute14,
14572                 attribute15,
14573 
14574                 schedule_seq_num,
14575                 substitute_group_num,
14576                 replacement_group_num,
14577                 --principle_flag,
14578                 setup_id,
14579 
14580                 last_update_date,
14581                 last_updated_by,
14582                 creation_date,
14583                 created_by,
14584                 last_update_login,
14585                 request_id,
14586                 program_application_id,
14587                 program_id,
14588                 program_update_date
14589                 )
14590             values
14591             (
14592                p_wip_entity_id,
14593                p_rtg_op_seq_num,
14594                v_wljdi_resource_seq_num(i),
14595                p_org_id,
14596                v_wljdi_department_id(i),
14597                -- check,
14598                'Y',
14599 
14600                v_wljdi_resource_id_new(i),
14601                v_wljdi_resource_code_new(i),
14602                v_wljdi_uom_code(i),
14603                v_wljdi_basis_type(i),
14604                v_wljdi_usage_rate_or_amt(i),
14605                round(1/v_wljdi_usage_rate_or_amt(i),6),
14606                v_wljdi_activity_id(i),
14607                v_wljdi_scheduled_flag(i),
14608                v_wljdi_assigned_units(i),
14609                /* Modified Query by Adding NVL during insert of max_assg_units. Bug 5471266 */
14610                nvl(v_wljdi_max_assg_units(i),v_wljdi_assigned_units(i)),
14611                v_wljdi_batch_id(i),
14612 			   /* Modified Query by Adding NVL during insert of firm type. Bug 5471266 */
14613                nvl(v_wljdi_firm_type(i),0),
14614                v_wljdi_grp_seq_id(i),
14615                v_wljdi_grp_seq_num(i),
14616                v_wljdi_parent_seq_num(i),
14617 
14618                v_wljdi_res_autocharge_type(i),
14619                v_wljdi_standard_rate_flag(i),
14620                v_wljdi_start_date(i),
14621                v_wljdi_completion_date(i),
14622 
14623                v_wljdi_attribute_category(i),
14624                v_wljdi_attribute1(i),
14625                v_wljdi_attribute2(i),
14626                v_wljdi_attribute3(i),
14627                v_wljdi_attribute4(i),
14628                v_wljdi_attribute5(i),
14629                v_wljdi_attribute6(i),
14630                v_wljdi_attribute7(i),
14631                v_wljdi_attribute8(i),
14632                v_wljdi_attribute9(i),
14633                v_wljdi_attribute10(i),
14634                v_wljdi_attribute11(i),
14635                v_wljdi_attribute12(i),
14636                v_wljdi_attribute13(i),
14637                v_wljdi_attribute14(i),
14638                v_wljdi_attribute15(i),
14639 
14640                v_wljdi_schedule_seq_num(i),
14641                v_wljdi_substitute_group_num(i),
14642                v_wljdi_replacement_group_num(i),
14643                -- not there...
14644                v_wljdi_setup_id(i),
14645 
14646                p_last_updt_date,
14647                p_last_updt_by,
14648                p_creation_date,
14649                p_created_by,
14650                p_last_updt_login,
14651                p_request_id,
14652                p_program_appl_id,
14653                p_program_id,
14654                p_prog_updt_date
14655             );
14656 
14657     else
14658 
14659        /* error out.... cannt delete previous op resources.... */
14660        fnd_message.set_name('WSM','WSM_NULL_FIELD');
14661        fnd_message.set_token('FLD_NAME', 'job_op_seq_num/rtg_op_seq_num');
14662        x_error_msg := fnd_message.get;
14663        x_error_code := -1;
14664        return; --raise e_wljdi_common_error;
14665 
14666     end if;
14667 
14668     l_stmt_num := 140;
14669 
14670     --Bug 5474321: Setup resource should be imported even for previous operations.
14671     --if ( ((p_job_op_seq_num IS NOT NULL) and (p_job_op_seq_num = p_cur_job_op_seq_num) )
14672     if ( ((p_job_op_seq_num IS NOT NULL) )
14673          OR
14674          ((p_rtg_op_seq_num IS NOT NULL))
14675        )
14676     then
14677 
14678         l_stmt_num := 150;
14679         /* check if this part has to come under an if clause....*/
14680         /* no significance..... */
14681         l_index := p_global_index; /* current position of p_global_index which will be deleted... */
14682         p_global_index := null;
14683 
14684         IF l_setup_index_tbl.exists(l_index) THEN
14685             l_index := v_wljdi_record_id.next(l_index);
14686         END IF;
14687 
14688         -- Try to get a resource record...
14689         while l_index is not null loop
14690 
14691             IF lbji_debug = 'Y' THEN
14692                 fnd_file.put_line(fnd_file.log,'Add Resource : Finding the next one : Current ID : ' || v_wljdi_record_id(l_index));
14693             END IF;
14694 
14695             IF (not(l_setup_index_tbl.exists(l_index))
14696                 and v_wljdi_load_type(l_index) = WSM_LOAD_RES)
14697             Then
14698                 -- ok.. this is for  Resource
14699                 IF lbji_debug = 'Y' THEN
14700                         fnd_file.put_line(fnd_file.log,'Add Resource : Found the next record : ' ||  v_wljdi_record_id(l_index));
14701                 END IF;
14702                 p_global_index := l_index;
14703                 exit;
14704             END IF;
14705 
14706             l_index := v_wljdi_record_id.next(l_index);
14707         end loop;
14708         IF lbji_debug = 'Y' THEN
14709                 fnd_file.put_line(fnd_file.log,'Add Resource : Next Record ID : ' || p_global_index);
14710         END IF;
14711 
14712         /* delete the fetched records... */
14713         l_index := l_setup_index_tbl.first;
14714 
14715         while l_index is not null loop
14716 
14717             v_wljdi_record_id.delete(l_index);
14718             v_wljdi_load_type.delete(l_index);
14719             v_wljdi_substitution_type.delete(l_index);
14720             v_wljdi_job_op_seq_num.delete(l_index);
14721             v_wljdi_routing_op_seq_num.delete(l_index);
14722             v_wljdi_next_rtg_op_seq_num.delete(l_index);
14723             v_wljdi_inventory_item_id_new.delete(l_index);
14724             v_wljdi_inventory_item_new.delete(l_index);
14725             v_wljdi_primary_item.delete(l_index);
14726             v_wljdi_primary_item_id.delete(l_index);
14727             v_wljdi_src_phantom_item.delete(l_index);
14728             v_wljdi_src_phantom_item_id.delete(l_index);
14729             v_wljdi_component_sequence_id.delete(l_index);
14730             v_wljdi_date_required.delete(l_index);
14731             v_wljdi_scheduled_quantity.delete(l_index);
14732             v_wljdi_required_quantity.delete(l_index);
14733             v_wljdi_batch_id.delete(l_index);
14734             v_wljdi_assigned_units.delete(l_index);
14735             v_wljdi_instance_id_new.delete(l_index);
14736             v_wljdi_op_completion_date.delete(l_index);
14737             v_wljdi_op_start_date.delete(l_index);
14738             v_wljdi_op_yield.delete(l_index);
14739             v_wljdi_resource_code_new.delete(l_index);
14740             v_wljdi_resource_id_new.delete(l_index);
14741             v_wljdi_resource_seq_num.delete(l_index);
14742             v_wljdi_substitute_group_num.delete(l_index);
14743             v_wljdi_replacement_group_num.delete(l_index);
14744             v_wljdi_serial_number_new.delete(l_index);
14745             v_wljdi_start_date.delete(l_index);
14746             v_wljdi_completion_date.delete(l_index);
14747 
14748             v_wljdi_firm_type.delete(l_index);
14749             v_wljdi_setup_id.delete(l_index);
14750             v_wljdi_grp_seq_id.delete(l_index);
14751             v_wljdi_grp_seq_num.delete(l_index);
14752             v_wljdi_max_assg_units.delete(l_index);
14753             v_wljdi_parent_seq_num.delete(l_index);
14754             v_wljdi_resource_hours.delete(l_index);
14755 
14756             v_wljdi_department_id.delete(l_index);
14757             v_wljdi_department_code.delete(l_index);
14758             v_wljdi_activity_id.delete(l_index);
14759             v_wljdi_standard_rate_flag.delete(l_index);
14760             v_wljdi_usage_rate_or_amt.delete(l_index);
14761             v_wljdi_basis_type.delete(l_index);
14762             v_wljdi_uom_code.delete(l_index);
14763             v_wljdi_scheduled_flag.delete(l_index);
14764             v_wljdi_res_autocharge_type.delete(l_index);
14765             v_wljdi_schedule_seq_num.delete(l_index);
14766 
14767              v_wljdi_attribute_category.delete(l_index);
14768              v_wljdi_attribute1.delete(l_index);
14769              v_wljdi_attribute2.delete(l_index);
14770              v_wljdi_attribute3.delete(l_index);
14771              v_wljdi_attribute4.delete(l_index);
14772              v_wljdi_attribute5.delete(l_index);
14773              v_wljdi_attribute6.delete(l_index);
14774              v_wljdi_attribute7.delete(l_index);
14775              v_wljdi_attribute8.delete(l_index);
14776              v_wljdi_attribute9.delete(l_index);
14777              v_wljdi_attribute10.delete(l_index);
14778              v_wljdi_attribute11.delete(l_index);
14779              v_wljdi_attribute12.delete(l_index);
14780              v_wljdi_attribute13.delete(l_index);
14781              v_wljdi_attribute14.delete(l_index);
14782              v_wljdi_attribute15.delete(l_index);
14783 
14784              l_index := l_setup_index_tbl.next(l_index);
14785 
14786         end loop;
14787     end if;
14788 
14789     l_stmt_num := 160;
14790 
14791     x_error_code := 0;
14792 
14793 exception
14794 
14795     when others then
14796         x_error_msg  := 'WSM_LBJ_INTERFACE_PVT : add_resource :: ' || l_stmt_num || ' : ' || substrb(SQLERRM,1,2000);
14797         fnd_file.put_line(fnd_file.log,x_error_msg);
14798         x_error_code := -1;
14799 
14800 end add_resource;
14801 
14802 -- **********************************************************************************************
14803 -- PROCEDURE import_lot_job_details
14804 -- **********************************************************************************************
14805 
14806 -- If one line has a fetal error, we will abort all the process
14807 
14808 procedure import_lot_job_details (
14809         p_wip_entity_id                 in number,
14810         p_org_id                        in number,
14811         p_wo_records_exist              in varchar2,    -- 'Y' or 'N'
14812         p_parent_header_id              in number,      -- header_id in WLJI, pass null if N/A
14813         p_job_scheduled_start_date      in date,
14814         p_job_scheduled_compl_date      in date,
14815         p_job_scheduled_quantity        in number,
14816         p_group_id                      in number,
14817         p_last_updt_date                in date,
14818         p_last_updt_by                  in number,
14819         p_creation_date                 in date,
14820         p_created_by                    in number,
14821         p_last_updt_login               in number,
14822         p_request_id                    in number,
14823         p_program_appl_id               in number,
14824         p_program_id                    in number,
14825         p_prog_updt_date                in date,
14826         p_error_code                    out nocopy number,
14827         p_error_msg                     out nocopy varchar2) is
14828 
14829 
14830 l_stmt_num                  number;
14831 l_d                         number;         -- index for WLJDI records
14832 l_d_saved                   number;
14833 l_temp                      number;
14834 l_exists                    number := 0;
14835 l_swap                      number := 0;    -- ADD: bug 3537390
14836 l_in                        number;
14837 l_out                       number;
14838 l_idx_out_phm               number;
14839 l_idx2                      number;
14840 l_status                    VARCHAR2(10);
14841 l_msg_count                 number;
14842 
14843 l_hash_str                  varchar2(200);  -- bug 3373496
14844 l_hash_value                number;         -- bug 3373496
14845 l_v_dup_usage               t_number;       -- bug 3373496
14846 
14847 
14848 
14849 l_res_first                 number  := NULL;
14850 l_comp_first                number  := NULL;
14851 l_op_first                  number  := NULL;
14852 l_res_use_first             number  := NULL;
14853 l_link_first                number  := NULL;
14854 l_op_link_first             number  := NULL;
14855 l_res_ins_first             number  := NULL;
14856 l_change_path               boolean := false;
14857 l_with_discommand           boolean := false;
14858 l_path_changed              boolean := false;
14859 l_job_status                number;
14860 l_job_start_qty             number;
14861 l_job_qty_scrapped          number;
14862 
14863 l_load_type                 number;
14864 l_sub_type                  number;
14865 l_end_op_seq_num            number:= NULL;
14866 l_end_op_seq_id             number:= NULL;
14867 l_start_op_seq_num          number:= NULL;
14868 l_start_op_seq_id           number:= NULL;
14869 l_cur_op_seq_id             number := NULL;
14870 l_cur_rtg_op_seq_num        number := NULL;
14871 l_cur_job_op_seq_num        number := NULL;
14872 l_on_reco_path              varchar2(1) := 'N';
14873 l_cur_job_qty               number := NULL;
14874 l_rtg_op_seq_num            number;
14875 l_job_op_seq_num            number;
14876 l_fm_op                     number;
14877 l_to_op                     number;
14878 l_op_sdate                  date;
14879 l_op_edate                  date;
14880 l_op_qty                    number;
14881 l_op_yield                  number;
14882 l_resource_id               number;
14883 l_res_seq_num               number;
14884 l_sub_grp_num               number;
14885 l_rpl_grp_num               number;
14886 l_instance_id               number;
14887 l_serial_num                varchar2(30);
14888 /*Added for bugfix:7248992 */
14889 l_cum_yield                 number;
14890 l_rev_cum_yield             number;
14891 
14892 l_item_id                   number;
14893 l_primary_item_id           number;
14894 l_src_phm_item_id           number;
14895 l_component_seq_id          number;
14896 l_job_start_date            date;
14897 l_job_compl_date            date;
14898 l_reco_flag                 varchar2(1);
14899 l_use_phantom_routings      number;
14900 
14901 /* ST : Detailed Scheduling */
14902 l_resource_hours            number;
14903 l_index                     number;
14904 l_parent_res_seq_num        number;
14905 l_setup_res_seq_num         number;
14906 
14907 l_v_disco_res_seq_num        t_number;
14908 /* ST : Detailed Scheduling */
14909 
14910 l_v_links                   t_links;
14911 l_v_ops                     t_ops;
14912 l_v_res                     t_op_res;
14913 l_v_res_ins                 t_res_ins;
14914 l_v_res_use                 t_res_use;
14915 l_v_comp                    t_op_comp;
14916 
14917 l_v_path_seq_num            t_number;
14918 l_v_path_ops                t_number;
14919 l_v_fm_ops                  t_number;
14920 l_v_to_ops                  t_number;
14921 
14922 l_v_rtg_op_seq              t_number;
14923 l_v_res_seq_num             t_number;
14924 l_v_instance_id             t_number;
14925 l_v_serial_num              t_serial;
14926 l_v_start_date              t_date;
14927 l_v_compl_date              t_date;
14928 l_v_recoflag                t_recoflag;
14929 
14930 l_v_op_yield                t_number;
14931 l_v_op_qty                  t_number;
14932 
14933 l_v_res_ids                 t_number;
14934 l_v_res_sub_grp             t_number;
14935 l_v_res_rpl_grp             t_number;
14936 l_v_disco_res_ids           t_number;
14937 l_v_disco_res_rpl_grp       t_number;
14938 
14939 -- ST : Detailed Scheduling....
14940 l_v_res_seq_nums            t_number;
14941 
14942 -- These two tables store the RES SEQ NUM and OP SEQ NUM for which setup resources where added..
14943 type t_setup is table of varchar2(1) index by varchar2(100);
14944 
14945 l_v_setup_op_res_num        t_setup;
14946 l_temp_par_res_seq          number;
14947 -- ST : Detailed Scheduling......
14948 
14949 l_v_res_ins_batch_id        t_number;
14950 
14951 l_v_res_use_asgn_units      t_number;
14952 
14953 l_v_comp_item_id            t_number;
14954 l_v_comp_prm_item_id        t_number;
14955 l_v_comp_phm_item_id        t_number;
14956 l_v_comp_comp_seq_id        t_number;
14957 l_v_comp_reqd_date          t_date;
14958 l_v_comp_yield              t_number;
14959 l_v_comp_qpa                t_number; -- ADD bug 3347985
14960 l_v_disco_item_id           t_number;
14961 l_v_disco_comp_seq_id       t_number;
14962 l_v_disco_src_phm_id        t_number;
14963 
14964 /* ST : Detailed Scheduling start */
14965 l_v_res_firm_type       t_number;
14966 l_v_res_setup_id        t_number;
14967 l_v_res_grp_seq_id      t_number;
14968 l_v_res_grp_seq_num     t_number;
14969 l_v_res_max_assg_units      t_number;
14970 l_v_res_parent_seq_num      t_number;
14971 l_v_res_batch_id        t_number;
14972 l_v_res_assg_units      t_number;
14973 
14974 l_v_resource_hours      t_number;
14975 l_v_attribute_category   t_attribute_category;
14976 l_v_attribute1   t_attribute;
14977 l_v_attribute2   t_attribute;
14978 l_v_attribute3   t_attribute;
14979 l_v_attribute4   t_attribute;
14980 l_v_attribute5   t_attribute;
14981 l_v_attribute6   t_attribute;
14982 l_v_attribute7   t_attribute;
14983 l_v_attribute8   t_attribute;
14984 l_v_attribute9   t_attribute;
14985 l_v_attribute10 t_attribute;
14986 l_v_attribute11  t_attribute;
14987 l_v_attribute12  t_attribute;
14988 l_v_attribute13  t_attribute;
14989 l_v_attribute14  t_attribute;
14990 l_v_attribute15  t_attribute;
14991 
14992 l_v_res_recom              t_res_recom;
14993 /* ST : Detailed Scheduling end */
14994 
14995 /*Added for bugfix:7248992 */
14996 l_v_cum_yield                 t_number;
14997 l_v_rev_cum_yield            t_number;
14998 
14999 cursor c_wljdi is
15000     select  record_id,
15001             -- ST Fix for bug 4285032 :
15002             -- APS will be populating 8 for resource instance information
15003             -- and 9 resource instance usage information..
15004             -- Map it to WSM load type 7 --> resource instance
15005             --                         4 --> resource instance usage..
15006             decode(load_type,8,7,
15007                              9,4,
15008                  load_type) load_type,
15009             substitution_type,
15010             job_op_seq_num,
15011             decode(job_op_seq_num,null,routing_op_seq_num,null) routing_op_seq_num,
15012             next_routing_op_seq_num,
15013             inventory_item_id_new,
15014             inventory_item_new,
15015             primary_item,
15016             primary_item_id,
15017             src_phantom_item,
15018             src_phantom_item_id,
15019             component_sequence_id,
15020             date_required,
15021             scheduled_quantity,
15022             required_quantity,
15023             batch_id,
15024             assigned_units,
15025             instance_id_new,
15026             operation_completion_date,
15027             operation_start_date,
15028             operation_yield,
15029             resource_code_new,
15030             resource_id_new,
15031             resource_seq_num,
15032             substitute_group_num,
15033             replacement_group_num,
15034             serial_number_new,
15035             start_date,
15036             completion_date,
15037             /* ST : Detailed Scheduling */
15038             firm_type,
15039             setup_id,
15040             setup_code,
15041             group_sequence_id,
15042             group_sequence_num,
15043             max_assigned_units,
15044             parent_resource_seq_num,
15045             resource_hours,
15046             /* ST : Detailed Scheduling */
15047             /* ST : Detailed Scheduling : Added for resource adding changes */
15048             department_id          ,
15049             department_code        ,
15050             activity_id        ,
15051             standard_rate_flag     ,
15052             usage_rate_or_amount   ,
15053             basis_type          ,
15054             uom_code            ,
15055             scheduled_flag      ,
15056             autocharge_type    ,
15057             schedule_seq_num       ,
15058             attribute_category     ,
15059             attribute1         ,
15060             attribute2         ,
15061             attribute3         ,
15062             attribute4         ,
15063             attribute5         ,
15064             attribute6         ,
15065             attribute7         ,
15066             attribute8         ,
15067             attribute9         ,
15068             attribute10        ,
15069             attribute11        ,
15070             attribute12        ,
15071             attribute13        ,
15072             attribute14        ,
15073             attribute15        ,
15074             /* ST : Detailed Scheduling : Added for resource adding changes : end */
15075             -- Added For LBJ Interface Alcoa Enhancement
15076             recommended,
15077             standard_operation_code,
15078             standard_operation_id,
15079             operation_yield_enabled,
15080             minimum_transfer_quantity,
15081             backflush_flag,
15082             count_point_type,
15083             description,
15084             include_in_rollup,
15085             option_dependent_flag,
15086             inventory_item_id_old,
15087             inventory_item_old,
15088             wip_supply_type,
15089             quantity_per_assembly,
15090             supply_subinventory,
15091             supply_locator_id,
15092             mrp_net_flag,
15093             auto_request_material,
15094             comments,
15095             applied_resource_units,
15096 	    cumulative_yield, /*Added for bugfix:7248992 */
15097 	    reverse_cumulative_yield/*Added for bugfix:7248992 */
15098     from    wsm_lot_job_dtl_interface
15099     where   parent_header_id = p_parent_header_id
15100     and     process_status = WIP_CONSTANTS.RUNNING
15101     order by load_type,
15102              NVL(parent_resource_seq_num,0), -- Detailed Scheduling Code review remark...
15103              -- (Moved parent resource_seq_num before substitution_type so that setup resources are processed after runtime resource
15104              -- ST : Detailed Scheduling Added to grpup the new setup resources for a runtime resource together
15105              -- Or else we'll get a error in the BULK INSERT of add_resource procedure
15106              substitution_type,
15107              NVL(job_op_seq_num, 0), NVL(routing_op_seq_num, 0),
15108              NVL(substitute_group_num, -1), NVL(replacement_group_num, 0),
15109              NVL(resource_seq_num, 0),
15110              NVL(instance_id_new, 0), NVL(serial_number_new, 0),
15111              DECODE(substitution_type,WSM_SUB_DEL,NVL(src_phantom_item_id, 0)) desc,
15112              DECODE(substitution_type,WSM_SUB_ADD,NVL(src_phantom_item_id, 0)
15113                                      ,WSM_SUB_REC,NVL(src_phantom_item_id, 0)
15114                                      ,WSM_SUB_DIS,NVL(src_phantom_item_id, 0)
15115                                      ,WSM_SUB_CHG,NVL(src_phantom_item_id, 0)) asc,
15116              transaction_date,
15117              record_id desc;
15118 
15119 cursor c_wljdi_indp is
15120     select  record_id,
15121             -- ST Fix for bug 4285032 :
15122         -- APS will be populating 8 for resource instance information
15123         -- and 9 resource instance usage information..
15124         -- Map it to WSM load type 7 --> resource instance
15125         --                         4 --> resource instance usage..
15126             decode(load_type,8,7,
15127                          9,4,
15128                  load_type) load_type,
15129             substitution_type,
15130             job_op_seq_num,
15131             decode(job_op_seq_num,null,routing_op_seq_num,null) routing_op_seq_num,
15132             next_routing_op_seq_num,
15133             inventory_item_id_new,
15134             inventory_item_new,
15135             primary_item,
15136             primary_item_id,
15137             src_phantom_item,
15138             src_phantom_item_id,
15139             component_sequence_id,
15140             date_required,
15141             scheduled_quantity,
15142             required_quantity,
15143             batch_id,
15144             assigned_units,
15145             instance_id_new,
15146             operation_completion_date,
15147             operation_start_date,
15148             operation_yield,
15149             resource_code_new,
15150             resource_id_new,
15151             resource_seq_num,
15152             substitute_group_num,
15153             replacement_group_num,
15154             serial_number_new,
15155             start_date,
15156             completion_date,
15157             /* ST : Detailed Scheduling */
15158             firm_type,
15159             setup_id,
15160             setup_code,
15161             group_sequence_id,
15162             group_sequence_num,
15163             max_assigned_units,
15164             parent_resource_seq_num,
15165             resource_hours,
15166             /* ST : Detailed Scheduling */
15167             /* ST : Detailed Scheduling : Added for resource adding changes */
15168             department_id          ,
15169             department_code        ,
15170             activity_id        ,
15171             standard_rate_flag     ,
15172             usage_rate_or_amount   ,
15173             --usage_rate_or_amount_inve ,
15174             basis_type          ,
15175             uom_code            ,
15176             scheduled_flag      ,
15177             --resource_offset_percent   ,
15178             autocharge_type    ,
15179             schedule_seq_num       ,
15180             --principle_flag       ,
15181             --change_notice    ,
15182             --acd_type         ,
15183             --original_system_reference,
15184             attribute_category     ,
15185             attribute1         ,
15186             attribute2         ,
15187             attribute3         ,
15188             attribute4         ,
15189             attribute5         ,
15190             attribute6         ,
15191             attribute7         ,
15192             attribute8         ,
15193             attribute9         ,
15194             attribute10        ,
15195             attribute11        ,
15196             attribute12        ,
15197             attribute13        ,
15198             attribute14        ,
15199             attribute15        ,
15200             /* ST : Detailed Scheduling : Added for resource adding changes : end */
15201             -- Added For LBJ Interface Alcoa Enhancement
15202             recommended,
15203             standard_operation_code,
15204             standard_operation_id,
15205             operation_yield_enabled,
15206             minimum_transfer_quantity,
15207             backflush_flag,
15208             count_point_type,
15209             description,
15210             include_in_rollup,
15211             option_dependent_flag,
15212             inventory_item_id_old,
15213             inventory_item_old,
15214             wip_supply_type,
15215             quantity_per_assembly,
15216             supply_subinventory,
15217             supply_locator_id,
15218             mrp_net_flag,
15219             auto_request_material,
15220             comments,
15221             applied_resource_units,
15222 	    cumulative_yield, /*Added for bugfix:7248992 */
15223 	    reverse_cumulative_yield/*Added for bugfix:7248992 */
15224     from    wsm_lot_job_dtl_interface
15225     where   parent_header_id IS NULL
15226     and     wip_entity_id     = p_wip_entity_id
15227     and     organization_id   = p_org_id
15228     and     process_status    = WIP_CONSTANTS.RUNNING
15229     and     NVL(group_id, -1) = NVL(p_group_id, -1)
15230     order by load_type,
15231              NVL(parent_resource_seq_num,0),
15232              -- Detailed Scheduling Code review remark...
15233              -- (Moved parent resource_seq_num before substitution_type so that setup resources are processed after runtime resource
15234              -- ST : Detailed Scheduling Added to grpup the new setup resources for a runtime resource together
15235              -- Or else we'll get a error in the BULK INSERT of add_resource procedure
15236              substitution_type,
15237              NVL(job_op_seq_num, 0), NVL(routing_op_seq_num, 0),
15238              NVL(substitute_group_num, -1), NVL(replacement_group_num, 0),
15239              NVL(resource_seq_num, 0),
15240              NVL(instance_id_new, 0), NVL(serial_number_new, 0),
15241              DECODE(substitution_type,WSM_SUB_DEL,NVL(src_phantom_item_id, 0)) desc,
15242              DECODE(substitution_type,WSM_SUB_ADD,NVL(src_phantom_item_id, 0)
15243                                      ,WSM_SUB_REC,NVL(src_phantom_item_id, 0)
15244                                      ,WSM_SUB_DIS,NVL(src_phantom_item_id, 0)
15245                                      ,WSM_SUB_CHG,NVL(src_phantom_item_id, 0)) asc,
15246              transaction_date,
15247              record_id desc;
15248 
15249 cursor c_the_path (
15250         c_wip_entity_id     number,
15251         c_end_op_seq_num    number) is
15252     select     from_op_seq_num,
15253                to_op_seq_num
15254     from       wsm_copy_op_networks
15255     start with (to_op_seq_num = c_end_op_seq_num and
15256                 recommended = 'Y' and
15257                 wip_entity_id = c_wip_entity_id)
15258     connect by (to_op_seq_num = prior from_op_seq_num and
15259                 recommended = 'Y' and
15260                 wip_entity_id = c_wip_entity_id);
15261 
15262 cursor c_disco_res (
15263         c_wip_entity_id     number,
15264         c_rtg_op_seq_num    number,
15265         c_sub_grp_num       number,
15266         c_rpl_grp_num       number) is
15267     select   resource_id,
15268              replacement_group_num,
15269              resource_seq_num  /* ST : Detailed Scheduling added.... */
15270     from     wsm_copy_op_resources
15271     where    wip_entity_id          = c_wip_entity_id
15272     and      operation_seq_num      = c_rtg_op_seq_num
15273     and      nvl(substitute_group_num, -1)  = nvl(c_sub_grp_num, -1)
15274     and      nvl(replacement_group_num, 0) <> nvl(c_rpl_grp_num, 0);
15275 
15276 cursor c_disco_comp (
15277         c_wip_entity_id     number,
15278         c_rtg_op_seq_num    number,
15279         c_item_id           number,
15280         c_primary_item_id   number,
15281         c_src_phantom_id    number) is
15282     select   wcro.component_item_id,
15283              wcro.component_sequence_id,
15284              wcro.source_phantom_id
15285     from     wsm_copy_requirement_ops wcro
15286     where    wcro.wip_entity_id     = c_wip_entity_id
15287     and      wcro.operation_seq_num = c_rtg_op_seq_num
15288     and      wcro.component_item_id <> c_item_id
15289     and      wcro.primary_component_id = c_primary_item_id
15290     and      wcro.source_phantom_id = -1 -- IS NULL
15291     union
15292     select   wcro.component_item_id,
15293              wcro.component_sequence_id,
15294              wcro.source_phantom_id
15295     from     wsm_copy_requirement_ops wcro
15296     where    wcro.wip_entity_id        = c_wip_entity_id
15297     and      wcro.operation_seq_num    = c_rtg_op_seq_num
15298     and      wcro.primary_component_id = c_primary_item_id
15299     and      wcro.source_phantom_id <> -1 -- IS NOT NULL
15300     and      wcro.source_phantom_id <> c_src_phantom_id;
15301 
15302 
15303 e_wljdi_common_error        exception;
15304 e_invalid_discommend_path   exception;
15305 e_invalid_recommend_path    exception;
15306 e_invalid_sub_load_type     exception;
15307 e_invalid_op_seq_num        exception;
15308 e_invalid_item_name         exception;
15309 e_invalid_resource_name     exception;
15310 e_null_comp_seq_id          exception;
15311 
15312 -- Added For LBJ Interface Alcoa Enhancement
15313 l_link_exists     number;
15314 l_from_op_seq_id  number;
15315 l_to_op_seq_id    number;
15316 l_validate_network number := -1;
15317 l_scrap_account   number;
15318 l_est_absorption_account number;
15319 l_department_code varchar2(10);
15320 l_op_seq_id       number;
15321 l_item_old_id     number;
15322 x_return_status   varchar2(1);
15323 l_Supply_subinventory     varchar2(20);
15324 l_Supply_locator_id       number;
15325 l_mrp_net_flag    number;
15326 l_valid_comp      boolean;
15327 l_applied_resource_units   number;
15328 l_resource_seq_num         number;
15329 --Modified for Alcoa Enhanement as the exception is not used
15330 --e_no_supply_sub      exception;
15331 
15332 BEGIN
15333 
15334     SAVEPOINT start_import_details;
15335 
15336     p_error_code         := 0;
15337     p_error_msg          := '';
15338     l_cur_job_op_seq_num := null;
15339     l_cur_rtg_op_seq_num := null;
15340     l_cur_op_seq_id      := null;
15341 
15342 l_stmt_num := 5.1;
15343     -- get the op_seq_num of the routing end
15344     find_copy_routing_end(
15345         p_wip_entity_id,
15346         l_end_op_seq_num,
15347         l_end_op_seq_id,
15348         p_error_code,
15349         p_error_msg);
15350     if p_error_code <> 0 then
15351         raise e_wljdi_common_error;
15352     end if;
15353 
15354 l_stmt_num := 5.2;
15355     -- get the op_seq_num of the routing start
15356     find_copy_routing_start(
15357         p_wip_entity_id,
15358         l_start_op_seq_num,
15359         l_start_op_seq_id,
15360         p_error_code,
15361         p_error_msg);
15362     if p_error_code <> 0 then
15363         raise e_wljdi_common_error;
15364     end if;
15365 
15366 
15367     -- if p_wo_records_exist = 'Y', get current operation information here!
15368     if p_wo_records_exist = 'Y' then
15369 
15370 l_stmt_num := 10.1;
15371         select  status_type,
15372                 start_quantity,
15373                 quantity_scrapped
15374         into    l_job_status,
15375                 l_job_start_qty,
15376                 l_job_qty_scrapped
15377         from    wip_discrete_jobs
15378         where   wip_entity_id = p_wip_entity_id;
15379 
15380         if(l_job_status = WIP_CONSTANTS.UNRELEASED) then
15381             l_cur_job_op_seq_num := g_op_seq_incr;
15382             l_cur_rtg_op_seq_num := l_start_op_seq_num;
15383             l_cur_op_seq_id      := l_start_op_seq_id;
15384             l_cur_job_qty        := l_job_start_qty;
15385 
15386             /*For Unreleased jobs insert records for changes in wip tables */
15387             BEGIN
15388 
15389               insert into wsm_lot_job_dtl_interface (
15390                                       record_id,
15391                                       interface_id,
15392                                       group_id,
15393                                       parent_header_id,
15394                                       load_type,
15395                                       substitution_type,
15396                                       process_phase,
15397                                       process_status,
15398                                       job_name,
15399                                       wip_entity_id,
15400                                       organization_id,
15401                                       organization_code,
15402                                       department_id,
15403                                       department_code,
15404                                       description,
15405                                       job_op_seq_num,
15406                                       routing_op_seq_num,
15407                                       next_routing_op_seq_num,
15408                                       resource_seq_num,
15409                                       resource_id_old,
15410                                       resource_id_new,
15411                                       resource_code_old,
15412                                       resource_code_new,
15413                                       usage_rate_or_amount,
15414                                       scheduled_flag,
15415                                       assigned_units,
15416                                       applied_resource_units,
15417                                       applied_resource_value,
15418                                       uom_code,
15419                                       basis_type,
15420                                       activity_id,
15421                                       autocharge_type,
15422                                       standard_rate_flag,
15423                                       start_date,
15424                                       completion_date,
15425                                       schedule_seq_num,
15426                                       substitute_group_num,
15427                                       replacement_group_num,
15428                                       setup_id,
15429                                       batch_id,
15430                                       inventory_item_id_old,
15431                                       inventory_item_id_new,
15432                                       inventory_item_old,
15433                                       inventory_item_new,
15434                                       primary_item_id,
15435                                       primary_item,
15436                                       src_phantom_item_id,
15437                                       src_phantom_item,
15438                                       component_sequence_id,
15439                                       quantity_per_assembly,
15440                                       wip_supply_type,
15441                                       date_required,
15442                                       required_quantity,
15443                                       quantity_issued,
15444                                       supply_subinventory,
15445                                       supply_locator_id,
15446                                       mrp_net_flag,
15447                                       mps_required_quantity,
15448                                       mps_date_required,
15449                                       standard_operation_id,
15450                                       scheduled_quantity,
15451                                       operation_yield,
15452                                       operation_start_date,
15453                                       operation_completion_date,
15454                                       minimum_transfer_quantity,
15455                                       backflush_flag,
15456                                       count_point_type,
15457                                       planning_pct,
15458                                       transaction_date,
15459                                       last_update_date,
15460                                       last_updated_by_name,
15461                                       last_updated_by,
15462                                       creation_date,
15463                                       created_by_name,
15464                                       created_by,
15465                                       last_update_login,
15466                                       request_id,
15467                                       program_application_id,
15468                                       program_id,
15469                                       program_update_date,
15470                                       attribute_category,
15471                                       attribute1,
15472                                       attribute2,
15473                                       attribute3,
15474                                       attribute4,
15475                                       attribute5 ,
15476                                       attribute6,
15477                                       attribute7,
15478                                       attribute8,
15479                                       attribute9,
15480                                       attribute10,
15481                                       attribute11,
15482                                       attribute12,
15483                                       attribute13,
15484                                       attribute14,
15485                                       attribute15,
15486                                       instance_id_new,
15487                                       instance_id_old,
15488                                       serial_number_new,
15489                                       serial_number_old,
15490                                       auto_request_material,
15491                                       comments,
15492                                       error_code,
15493                                       error_msg,
15494                                       firm_type,
15495                                       group_sequence_id,
15496                                       group_sequence_num,
15497                                       max_assigned_units,
15498                                       parent_resource_seq_num,
15499                                       resource_hours,
15500                                       setup_code,
15501                                       cumulative_yield,
15502                                       reverse_cumulative_yield,
15503                                       standard_operation_code,
15504                                       operation_yield_enabled,
15505                                       include_in_rollup,
15506                                       option_dependent_flag,
15507                                       recommended)
15508                             (select   -1 * record_id,
15509                                       interface_id,
15510                                       group_id,
15511                                       parent_header_id,
15512                                       decode(load_type,6,3,load_type),
15513                                       substitution_type,
15514                                       process_phase,
15515                                       process_status,
15516                                       job_name,
15517                                       wip_entity_id,
15518                                       organization_id,
15519                                       organization_code,
15520                                       department_id,
15521                                       department_code,
15522                                       description,
15523                                       null,
15524                                       l_start_op_seq_num,
15525                                       next_routing_op_seq_num,
15526                                       resource_seq_num,
15527                                       resource_id_old,
15528                                       resource_id_new,
15529                                       resource_code_old,
15530                                       resource_code_new,
15531                                       usage_rate_or_amount,
15532                                       scheduled_flag,
15533                                       assigned_units,
15534                                       applied_resource_units,
15535                                       applied_resource_value,
15536                                       uom_code,
15537                                       basis_type,
15538                                       activity_id,
15539                                       autocharge_type,
15540                                       standard_rate_flag,
15541                                       start_date,
15542                                       completion_date,
15543                                       schedule_seq_num,
15544                                       substitute_group_num,
15545                                       replacement_group_num,
15546                                       setup_id,
15547                                       batch_id,
15548                                       inventory_item_id_old,
15549                                       inventory_item_id_new,
15550                                       inventory_item_old,
15551                                       inventory_item_new,
15552                                       primary_item_id,
15553                                       primary_item,
15554                                       src_phantom_item_id,
15555                                       src_phantom_item,
15556                                       component_sequence_id,
15557                                       quantity_per_assembly,
15558                                       wip_supply_type,
15559                                       date_required,
15560                                       required_quantity,
15561                                       quantity_issued,
15562                                       supply_subinventory,
15563                                       supply_locator_id,
15564                                       mrp_net_flag,
15565                                       mps_required_quantity,
15566                                       mps_date_required,
15567                                       standard_operation_id,
15568                                       scheduled_quantity,
15569                                       operation_yield,
15570                                       operation_start_date,
15571                                       operation_completion_date,
15572                                       minimum_transfer_quantity,
15573                                       backflush_flag,
15574                                       count_point_type,
15575                                       planning_pct,
15576                                       transaction_date,
15577                                       last_update_date,
15578                                       last_updated_by_name,
15579                                       last_updated_by,
15580                                       creation_date,
15581                                       created_by_name,
15582                                       created_by,
15583                                       last_update_login,
15584                                       request_id,
15585                                       program_application_id,
15586                                       program_id,
15587                                       program_update_date,
15588                                       attribute_category,
15589                                       attribute1,
15590                                       attribute2,
15591                                       attribute3,
15592                                       attribute4,
15593                                       attribute5 ,
15594                                       attribute6,
15595                                       attribute7,
15596                                       attribute8,
15597                                       attribute9,
15598                                       attribute10,
15599                                       attribute11,
15600                                       attribute12,
15601                                       attribute13,
15602                                       attribute14,
15603                                       attribute15,
15604                                       instance_id_new,
15605                                       instance_id_old,
15606                                       serial_number_new,
15607                                       serial_number_old,
15608                                       auto_request_material,
15609                                       comments,
15610                                       error_code,
15611                                       error_msg,
15612                                       firm_type,
15613                                       group_sequence_id,
15614                                       group_sequence_num,
15615                                       max_assigned_units,
15616                                       parent_resource_seq_num,
15617                                       resource_hours,
15618                                       setup_code,
15619                                       cumulative_yield,
15620                                       reverse_cumulative_yield,
15621                                       standard_operation_code,
15622                                       operation_yield_enabled,
15623                                       include_in_rollup,
15624                                       option_dependent_flag,
15625                                       recommended
15626                                  from wsm_lot_job_dtl_interface
15627                                  where ((parent_header_id = p_parent_header_id)
15628                                         or (parent_header_id IS NULL and wip_entity_id = p_wip_entity_id and organization_id = p_org_id and  NVL(group_id, -1) = NVL(p_group_id, -1)))
15629                                   and   process_status    = WIP_CONSTANTS.RUNNING
15630                                   and   (job_op_seq_num=g_op_seq_incr and routing_op_seq_num=l_start_op_seq_num)
15631                                   and   load_type <> 5
15632                                   and   ((load_type=3 and substitution_type not in (1,2))
15633                                        or (load_type=1 and substitution_type not in (1,2))));
15634 
15635                            EXCEPTION
15636                                  WHEN OTHERS THEN
15637                                   fnd_file.put_line(fnd_file.log, 'Inserting additional records into WLDJI failed');
15638                             END;
15639         else
15640             begin
15641 l_stmt_num := 10.2;
15642                 select  operation_seq_num,
15643                         operation_sequence_id,
15644                         quantity_in_queue + quantity_running + quantity_waiting_to_move
15645                 into    l_cur_job_op_seq_num,
15646                         l_cur_op_seq_id,
15647                         l_cur_job_qty
15648                 from    wip_operations
15649                 where   wip_entity_id = p_wip_entity_id
15650                 and     (quantity_in_queue <> 0 or
15651                          quantity_running <> 0 or
15652                          quantity_waiting_to_move <> 0);
15653                 if(l_cur_op_seq_id IS NOT NULL) then
15654                     begin
15655 l_stmt_num := 10.3;
15656                         select operation_seq_num
15657                         into   l_cur_rtg_op_seq_num
15658                         from   wsm_copy_operations
15659                         where  wip_entity_id = p_wip_entity_id
15660                         and    operation_sequence_id = l_cur_op_seq_id;
15661                     exception
15662                         when no_data_found then
15663                             NULL;
15664                     end;
15665                 end if;
15666             exception
15667                 when no_data_found then
15668                     if (l_job_status IN ( WIP_CONSTANTS.COMP_CHRG,
15669                                           WIP_CONSTANTS.COMP_NOCHRG,
15670                                           WIP_CONSTANTS.CLOSED) )
15671                     then
15672                         l_cur_job_op_seq_num := null;
15673                         l_cur_rtg_op_seq_num := l_end_op_seq_num;
15674                         l_cur_op_seq_id      := l_end_op_seq_id;
15675                         l_cur_job_qty        := l_job_start_qty - l_job_qty_scrapped;
15676                     end if;
15677             end;
15678         end if;
15679 
15680     else -- p_wo_records_exist = 'N'
15681         l_cur_rtg_op_seq_num := l_start_op_seq_num;
15682         l_cur_job_qty        := p_job_scheduled_quantity;
15683     end if;
15684 
15685     -- get job start/compl date if not specified
15686     l_job_start_date := p_job_scheduled_start_date;
15687     l_job_compl_date := p_job_scheduled_compl_date;
15688     if(l_job_start_date IS NULL or l_job_compl_date IS NULL) then
15689 l_stmt_num := 10.4;
15690         select  scheduled_start_date,
15691                 scheduled_completion_date
15692         into    l_job_start_date,
15693                 l_job_compl_date
15694         from    wip_discrete_jobs
15695         where   wip_entity_id = p_wip_entity_id;
15696     end if;
15697 
15698     -----------------------------------------------------
15699     -- default WLJDI values, and validate required fields
15700     -----------------------------------------------------
15701 l_stmt_num := 20;
15702     default_wljdi_fields(
15703         p_wip_entity_id    => p_wip_entity_id,
15704         p_org_id           => p_org_id,
15705         p_parent_header_id => p_parent_header_id,
15706         p_group_id         => p_group_id,
15707         p_wo_records_exist => p_wo_records_exist,
15708         p_error_code       => p_error_code,
15709         p_error_msg        => p_error_msg);
15710     if p_error_code <> 0 then
15711         raise e_wljdi_common_error;
15712     end if;
15713 
15714 
15715 
15716     ---------------------------------------
15717     -- empty v_wljdi and load WLJDI records
15718     ---------------------------------------
15719     -- BA: bug 3313463/3311695 8i compatibility
15720     v_wljdi_record_id               := t_wljdi_record_id();
15721     v_wljdi_load_type               := t_wljdi_load_type();
15722     v_wljdi_substitution_type       := t_wljdi_substitution_type();
15723     v_wljdi_job_op_seq_num          := t_wljdi_job_op_seq_num();
15724     v_wljdi_routing_op_seq_num      := t_wljdi_routing_op_seq_num();
15725     v_wljdi_next_rtg_op_seq_num     := t_wljdi_next_rtg_op_seq_num();
15726 
15727     v_wljdi_inventory_item_id_new   := t_wljdi_inventory_item_id_new();
15728     v_wljdi_inventory_item_new      := t_wljdi_inventory_item_new();
15729     v_wljdi_primary_item            := t_wljdi_primary_item();
15730     v_wljdi_primary_item_id         := t_wljdi_primary_item_id();
15731     v_wljdi_src_phantom_item        := t_wljdi_src_phantom_item();
15732     v_wljdi_src_phantom_item_id     := t_wljdi_src_phantom_item_id();
15733     v_wljdi_component_sequence_id   := t_wljdi_component_sequence_id();
15734     v_wljdi_date_required           := t_wljdi_date_required();
15735     v_wljdi_scheduled_quantity      := t_wljdi_scheduled_quantity();
15736     v_wljdi_required_quantity       := t_wljdi_required_quantity();
15737     v_wljdi_batch_id                := t_wljdi_batch_id();
15738     v_wljdi_assigned_units          := t_wljdi_assigned_units();
15739     v_wljdi_instance_id_new         := t_wljdi_instance_id_new();
15740     v_wljdi_op_completion_date      := t_wljdi_op_completion_date();
15741     v_wljdi_op_start_date           := t_wljdi_op_start_date();
15742     v_wljdi_op_yield                := t_wljdi_op_yield();
15743     v_wljdi_resource_code_new       := t_wljdi_resource_code_new();
15744     v_wljdi_resource_id_new         := t_wljdi_resource_id_new();
15745     v_wljdi_resource_seq_num        := t_wljdi_resource_seq_num();
15746     v_wljdi_substitute_group_num    := t_wljdi_substitute_group_num();
15747     v_wljdi_replacement_group_num   := t_wljdi_replacement_group_num();
15748     v_wljdi_serial_number_new       := t_wljdi_serial_number_new();
15749     v_wljdi_start_date              := t_wljdi_start_date();
15750     v_wljdi_completion_date         := t_wljdi_completion_date();
15751     -- EA: bug 3313463/3311695 8i compatibility
15752 
15753     /* ST : Detailed scheduling */
15754     v_wljdi_firm_type           := t_wljdi_firm_type();
15755     v_wljdi_setup_id            := t_wljdi_setup_id();
15756     v_wljdi_setup_code          := t_wljdi_setup_code();
15757     v_wljdi_grp_seq_id          := t_wljdi_grp_seq_id();
15758     v_wljdi_grp_seq_num         := t_wljdi_grp_seq_num();
15759     v_wljdi_max_assg_units      := t_wljdi_max_assg_units();
15760     v_wljdi_parent_seq_num      := t_wljdi_parent_seq_num();
15761     v_wljdi_resource_hours      := t_wljdi_resource_hours();
15762 
15763     /* ST : Detailed Scheduling : Considered for adding a setup resource.... */
15764     v_wljdi_department_id       := t_wljdi_department_id();
15765     v_wljdi_department_code     := t_wljdi_department_code();
15766 
15767     v_wljdi_activity_id         := t_wljdi_activity_id();
15768     v_wljdi_standard_rate_flag  := t_wljdi_standard_rate_flag();
15769     v_wljdi_usage_rate_or_amt   := t_wljdi_usage_rate_or_amt();
15770     v_wljdi_basis_type          := t_wljdi_basis_type();
15771     v_wljdi_uom_code            := t_wljdi_uom_code();
15772     v_wljdi_scheduled_flag      := t_wljdi_scheduled_flag();
15773     v_wljdi_res_autocharge_type := t_wljdi_res_autocharge_type();
15774     v_wljdi_schedule_seq_num    := t_wljdi_schedule_seq_num();
15775 
15776     v_wljdi_attribute_category  := t_wljdi_attribute_category();
15777     v_wljdi_attribute1          := t_wljdi_attribute1();
15778     v_wljdi_attribute2          := t_wljdi_attribute2();
15779     v_wljdi_attribute3          := t_wljdi_attribute3();
15780     v_wljdi_attribute4          := t_wljdi_attribute4();
15781     v_wljdi_attribute5          := t_wljdi_attribute5();
15782     v_wljdi_attribute6          := t_wljdi_attribute6();
15783     v_wljdi_attribute7          := t_wljdi_attribute7();
15784     v_wljdi_attribute8          := t_wljdi_attribute8();
15785     v_wljdi_attribute9          := t_wljdi_attribute9();
15786     v_wljdi_attribute10         := t_wljdi_attribute10();
15787     v_wljdi_attribute11         := t_wljdi_attribute11();
15788     v_wljdi_attribute12         := t_wljdi_attribute12();
15789     v_wljdi_attribute13         := t_wljdi_attribute13();
15790     v_wljdi_attribute14         := t_wljdi_attribute14();
15791     v_wljdi_attribute15         := t_wljdi_attribute15();
15792     /* ST : Detailed scheduling : end */
15793 
15794     -- Added For LBJ Interface Alcoa Enhancement
15795     v_wljdi_recommended         := t_wljdi_recommended();
15796     v_wljdi_std_op_code         := t_wljdi_std_op_code();
15797     v_wljdi_std_op_id           := t_wljdi_std_op_id();
15798     v_wljdi_op_yield_enable     := t_wljdi_op_yield_enable();
15799     v_wljdi_min_trf_qty         := t_wljdi_min_trf_qty();
15800     v_wljdi_backflush_flag      := t_wljdi_backflush_flag();
15801     v_wljdi_count_point_type    := t_wljdi_count_point_type();
15802     v_wljdi_description         := t_wljdi_description();
15803     v_wljdi_inc_in_rollup       := t_wljdi_inc_in_rollup();
15804     v_wljdi_option_dep_flag     := t_wljdi_option_dep_flag();
15805     v_wljdi_inventory_item_id_old   := t_wljdi_inventory_item_id_old();
15806     v_wljdi_inventory_item_old      := t_wljdi_inventory_item_old();
15807     v_wljdi_wip_supply_type         := t_wljdi_wip_supply_type();
15808     v_wljdi_quantity_per_assembly   := t_wljdi_quantity_per_assembly();
15809     v_wljdi_supply_subinventory     := t_wljdi_supply_subinventory();
15810     v_wljdi_supply_locator_id       := t_wljdi_supply_locator_id();
15811     v_wljdi_mrp_net_flag            := t_wljdi_mrp_net_flag();
15812     v_wljdi_auto_request_material   := t_wljdi_auto_request_material();
15813     v_wljdi_comments                := t_wljdi_comments();
15814     v_wljdi_applied_resource_units  := t_wljdi_applied_resource_units();
15815 
15816     /*Added for bugfix:6857061 */
15817     v_wljdi_cum_yield               := t_wljdi_cum_yield();
15818     v_wljdi_rev_cum_yield           := t_wljdi_rev_cum_yield();
15819 
15820     if(p_parent_header_id IS NULL) then
15821 l_stmt_num := 20.1;
15822         --BC: bug 3313463/3311695
15823         open c_wljdi_indp;
15824         fetch c_wljdi_indp bulk collect into
15825             v_wljdi_record_id,
15826             v_wljdi_load_type,
15827             v_wljdi_substitution_type,
15828             v_wljdi_job_op_seq_num,
15829             v_wljdi_routing_op_seq_num,
15830             v_wljdi_next_rtg_op_seq_num,
15831             v_wljdi_inventory_item_id_new,
15832             v_wljdi_inventory_item_new,
15833             v_wljdi_primary_item,
15834             v_wljdi_primary_item_id,
15835             v_wljdi_src_phantom_item,
15836             v_wljdi_src_phantom_item_id,
15837             v_wljdi_component_sequence_id,
15838             v_wljdi_date_required,
15839             v_wljdi_scheduled_quantity,
15840             v_wljdi_required_quantity,
15841             v_wljdi_batch_id,
15842             v_wljdi_assigned_units,
15843             v_wljdi_instance_id_new,
15844             v_wljdi_op_completion_date,
15845             v_wljdi_op_start_date,
15846             v_wljdi_op_yield,
15847             v_wljdi_resource_code_new,
15848             v_wljdi_resource_id_new,
15849             v_wljdi_resource_seq_num,
15850             v_wljdi_substitute_group_num,
15851             v_wljdi_replacement_group_num,
15852             v_wljdi_serial_number_new,
15853             v_wljdi_start_date,
15854             v_wljdi_completion_date,
15855             /* ST : Detailed Scheduling start */
15856             v_wljdi_firm_type,
15857             v_wljdi_setup_id,
15858             v_wljdi_setup_code,
15859             v_wljdi_grp_seq_id,
15860             v_wljdi_grp_seq_num,
15861             v_wljdi_max_assg_units,
15862             v_wljdi_parent_seq_num,
15863             v_wljdi_resource_hours,
15864             /* ST : Detailed Scheduling end */
15865             /* ST : Detailed Scheduling : Considered for adding a resource */
15866             v_wljdi_department_id      ,
15867             v_wljdi_department_code        ,
15868             v_wljdi_activity_id        ,
15869             v_wljdi_standard_rate_flag     ,
15870             v_wljdi_usage_rate_or_amt      ,
15871             v_wljdi_basis_type         ,
15872             v_wljdi_uom_code           ,
15873             v_wljdi_scheduled_flag     ,
15874             v_wljdi_res_autocharge_type    ,
15875             v_wljdi_schedule_seq_num       ,
15876 
15877             v_wljdi_attribute_category     ,
15878             v_wljdi_attribute1         ,
15879             v_wljdi_attribute2         ,
15880             v_wljdi_attribute3         ,
15881             v_wljdi_attribute4         ,
15882             v_wljdi_attribute5         ,
15883             v_wljdi_attribute6         ,
15884             v_wljdi_attribute7         ,
15885             v_wljdi_attribute8         ,
15886             v_wljdi_attribute9         ,
15887             v_wljdi_attribute10            ,
15888             v_wljdi_attribute11        ,
15889             v_wljdi_attribute12        ,
15890             v_wljdi_attribute13        ,
15891             v_wljdi_attribute14        ,
15892             v_wljdi_attribute15        ,
15893             /* ST : Detailed Scheduling :  end adding a resource changes */
15894             -- Added For LBJ Interface Alcoa Enhancement
15895             v_wljdi_recommended,
15896             v_wljdi_std_op_code,
15897             v_wljdi_std_op_id,
15898             v_wljdi_op_yield_enable,
15899             v_wljdi_min_trf_qty,
15900             v_wljdi_backflush_flag,
15901             v_wljdi_count_point_type,
15902             v_wljdi_description,
15903             v_wljdi_inc_in_rollup,
15904             v_wljdi_option_dep_flag,
15905             v_wljdi_inventory_item_id_old,
15906             v_wljdi_inventory_item_old,
15907             v_wljdi_wip_supply_type,
15908             v_wljdi_quantity_per_assembly,
15909             v_wljdi_supply_subinventory,
15910             v_wljdi_supply_locator_id,
15911             v_wljdi_mrp_net_flag,
15912             v_wljdi_auto_request_material,
15913             v_wljdi_comments,
15914             v_wljdi_applied_resource_units,
15915 	    v_wljdi_cum_yield,
15916 	    v_wljdi_rev_cum_yield
15917         ;
15918         close c_wljdi_indp;
15919         --EC: bug 3313463/3311695
15920     else
15921 l_stmt_num := 20.2;
15922         --BC: bug 3313463/3311695
15923         open c_wljdi;
15924         fetch c_wljdi bulk collect into
15925             v_wljdi_record_id,
15926             v_wljdi_load_type,
15927             v_wljdi_substitution_type,
15928             v_wljdi_job_op_seq_num,
15929             v_wljdi_routing_op_seq_num,
15930             v_wljdi_next_rtg_op_seq_num,
15931             v_wljdi_inventory_item_id_new,
15932             v_wljdi_inventory_item_new,
15933             v_wljdi_primary_item,
15934             v_wljdi_primary_item_id,
15935             v_wljdi_src_phantom_item,
15936             v_wljdi_src_phantom_item_id,
15937             v_wljdi_component_sequence_id,
15938             v_wljdi_date_required,
15939             v_wljdi_scheduled_quantity,
15940             v_wljdi_required_quantity,
15941             v_wljdi_batch_id,
15942             v_wljdi_assigned_units,
15943             v_wljdi_instance_id_new,
15944             v_wljdi_op_completion_date,
15945             v_wljdi_op_start_date,
15946             v_wljdi_op_yield,
15947             v_wljdi_resource_code_new,
15948             v_wljdi_resource_id_new,
15949             v_wljdi_resource_seq_num,
15950             v_wljdi_substitute_group_num,
15951             v_wljdi_replacement_group_num,
15952             v_wljdi_serial_number_new,
15953             v_wljdi_start_date,
15954             v_wljdi_completion_date,
15955             /* ST : Detailed Scheduling start */
15956             v_wljdi_firm_type,
15957             v_wljdi_setup_id,
15958             v_wljdi_setup_code,
15959             v_wljdi_grp_seq_id,
15960             v_wljdi_grp_seq_num,
15961             v_wljdi_max_assg_units,
15962             v_wljdi_parent_seq_num,
15963             v_wljdi_resource_hours,
15964             /* ST : Detailed Scheduling end */
15965             /* ST : Detailed Scheduling : Considered for adding a resource */
15966             v_wljdi_department_id      ,
15967             v_wljdi_department_code        ,
15968             v_wljdi_activity_id        ,
15969             v_wljdi_standard_rate_flag     ,
15970             v_wljdi_usage_rate_or_amt      ,
15971             v_wljdi_basis_type         ,
15972             v_wljdi_uom_code           ,
15973             v_wljdi_scheduled_flag     ,
15974             v_wljdi_res_autocharge_type    ,
15975             v_wljdi_schedule_seq_num       ,
15976 
15977             v_wljdi_attribute_category     ,
15978             v_wljdi_attribute1         ,
15979             v_wljdi_attribute2         ,
15980             v_wljdi_attribute3         ,
15981             v_wljdi_attribute4         ,
15982             v_wljdi_attribute5         ,
15983             v_wljdi_attribute6         ,
15984             v_wljdi_attribute7         ,
15985             v_wljdi_attribute8         ,
15986             v_wljdi_attribute9         ,
15987             v_wljdi_attribute10            ,
15988             v_wljdi_attribute11        ,
15989             v_wljdi_attribute12        ,
15990             v_wljdi_attribute13        ,
15991             v_wljdi_attribute14        ,
15992             v_wljdi_attribute15        ,
15993             /* ST : Detailed Scheduling :  end adding a resource changes */
15994             -- Added For LBJ Interface Alcoa Enhancement
15995             v_wljdi_recommended,
15996             v_wljdi_std_op_code,
15997             v_wljdi_std_op_id,
15998             v_wljdi_op_yield_enable,
15999             v_wljdi_min_trf_qty,
16000             v_wljdi_backflush_flag,
16001             v_wljdi_count_point_type,
16002             v_wljdi_description,
16003             v_wljdi_inc_in_rollup,
16004             v_wljdi_option_dep_flag,
16005             v_wljdi_inventory_item_id_old,
16006             v_wljdi_inventory_item_old,
16007             v_wljdi_wip_supply_type,
16008             v_wljdi_quantity_per_assembly,
16009             v_wljdi_supply_subinventory,
16010             v_wljdi_supply_locator_id,
16011             v_wljdi_mrp_net_flag,
16012             v_wljdi_auto_request_material,
16013             v_wljdi_comments,
16014             v_wljdi_applied_resource_units,
16015 	    v_wljdi_cum_yield,
16016 	    v_wljdi_rev_cum_yield
16017         ;
16018         close c_wljdi;
16019         --EC: bug 3313463/3311695
16020     end if;
16021 
16022     l_d := v_wljdi_record_id.first;
16023     while l_d IS NOT NULL
16024     loop
16025         l_load_type      := v_wljdi_load_type(l_d);
16026         l_sub_type       := v_wljdi_substitution_type(l_d);
16027         l_rtg_op_seq_num := v_wljdi_routing_op_seq_num(l_d);
16028         l_job_op_seq_num := v_wljdi_job_op_seq_num(l_d);
16029 
16030 l_stmt_num := 40;
16031         if (l_load_type = WSM_LOAD_RES) then            -- resource
16032             if (l_res_first IS NULL) then
16033                 l_res_first := l_d;
16034             end if;
16035 
16036         elsif (l_load_type = WSM_LOAD_COMP) then        -- component
16037             if (l_comp_first IS NULL) then
16038                 l_comp_first := l_d;
16039             end if;
16040 
16041         elsif (l_load_type = WSM_LOAD_OP) then          -- operation: load_type 3 and 4 are the same
16042             if (l_op_first IS NULL) then
16043                 l_op_first := l_d;
16044             end if;
16045        ---Added for Alcoa Enhancement
16046             if (l_sub_type = WSM_SUB_ADD) then
16047                 l_validate_network := 0;
16048             end if;
16049 
16050         elsif (l_load_type = WSM_LOAD_RES_USE) then     -- resource usage
16051             if (l_res_use_first IS NULL) then
16052                 l_res_use_first := l_d;
16053             end if;
16054 
16055         elsif (l_load_type = WSM_LOAD_LINK) then        -- link: load_type must be 4 or 5
16056             if (l_sub_type = WSM_SUB_DIS) then
16057                 l_with_discommand := true;
16058             end if;
16059             if (l_link_first IS NULL) then
16060                 l_link_first := l_d;
16061             end if;
16062             -- Added For LBJ Interface Alcoa Enhancement
16063             l_validate_network := 0;
16064 
16065         elsif (l_load_type = WSM_LOAD_LINK_OP) then     -- link or operation
16066             if (l_op_link_first IS NULL) then
16067                 l_op_link_first := l_d;
16068             end if;
16069             -- Added For LBJ Interface Alcoa Enhancement
16070             l_validate_network := 0;
16071         elsif (l_load_type = WSM_LOAD_RES_INS) then     -- resource instance
16072             if (l_res_ins_first IS NULL) then
16073                 l_res_ins_first := l_d;
16074             end if;
16075 
16076         else
16077             fnd_message.set_name('WSM','WSM_INVALID_FIELD');
16078             fnd_message.set_token('FLD_NAME', 'Load_type');
16079             p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
16080                         || fnd_message.get;
16081             p_error_code := -1;
16082             raise e_wljdi_common_error;
16083         end if;
16084         ---------------------------------------------------------------------------
16085         -- }} validete load_type, substitution_type combination and required fields
16086         ---------------------------------------------------------------------------
16087 
16088         l_d := v_wljdi_record_id.next(l_d);
16089     end loop;
16090     ------------------------------------------------
16091     -- Add Operation should precede add operation link.
16092     -- Added For LBJ Interface Alcoa Enhancement
16093     ------------------------------------------------
16094     if NOT(l_op_first IS NULL) then
16095         l_d :=l_op_first;
16096         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) =WSM_LOAD_OP)
16097         loop
16098             if (v_wljdi_substitution_type(l_d) = WSM_SUB_ADD) then
16099 
16100                 select scrap_account,est_absorption_account,department_code
16101                 into l_scrap_account,l_est_absorption_account,l_department_code
16102                 from bom_departments
16103                 where department_id = v_wljdi_department_id(l_d)
16104                 and organization_id = p_org_id;
16105 
16106                 select decode(sign(min(operation_sequence_id)),-1,min(operation_sequence_id)-1,-2),
16107                        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))
16108                 into l_op_seq_id,v_wljdi_routing_op_seq_num(l_d)
16109                 from wsm_copy_operations
16110                 where wip_entity_id = p_wip_entity_id
16111                 and organization_id = p_org_id;
16112 
16113                 if v_wljdi_recommended(l_d) is null then
16114                     v_wljdi_recommended(l_d) :='N';
16115                 end if;
16116 
16117                 if v_wljdi_scheduled_quantity(l_d) IS NOT NULL then
16118                     if v_wljdi_scheduled_quantity(l_d) <= 0 or v_wljdi_scheduled_quantity(l_d) > l_cur_job_qty then
16119                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
16120                         fnd_message.set_token('FLD_NAME', 'scheduled_quantity');
16121                         p_error_msg  := fnd_message.get;
16122                         p_error_code := - 1;
16123                         raise e_wljdi_common_error;
16124                     end if;
16125                 else
16126                     v_wljdi_scheduled_quantity(l_d) := l_cur_job_qty;
16127                 end if;
16128 
16129                 insert into wsm_copy_operations
16130                 (WIP_ENTITY_ID,
16131                  OPERATION_SEQ_NUM,
16132                  RECOMMENDED,
16133                  RECO_PATH_SEQ_NUM,
16134                  NETWORK_START_END,
16135                  RECO_SCHEDULED_QUANTITY,
16136                  RECO_START_DATE,
16137                  RECO_COMPLETION_DATE,
16138                  OPERATION_SEQUENCE_ID,
16139                  ROUTING_SEQUENCE_ID,
16140                  ORGANIZATION_ID,
16141                  STANDARD_OPERATION_ID,
16142                  STANDARD_OPERATION_CODE,
16143                  DEPARTMENT_ID,
16144                  DEPARTMENT_CODE,
16145                  SCRAP_ACCOUNT,
16146                  EST_ABSORPTION_ACCOUNT,
16147                  OPERATION_LEAD_TIME_PERCENT,
16148                  MINIMUM_TRANSFER_QUANTITY,
16149                  COUNT_POINT_TYPE,
16150                  OPERATION_DESCRIPTION,
16151                  EFFECTIVITY_DATE,
16152                  DISABLE_DATE,
16153                  BACKFLUSH_FLAG,
16154                  OPTION_DEPENDENT_FLAG,
16155                  OPERATION_TYPE,
16156                  REFERENCE_FLAG,
16157                  YIELD,
16158                  CUMULATIVE_YIELD,
16159                  REVERSE_CUMULATIVE_YIELD,
16160                  LABOR_TIME_CALC,
16161                  MACHINE_TIME_CALC,
16162                  TOTAL_TIME_CALC,
16163                  LABOR_TIME_USER,
16164                  MACHINE_TIME_USER,
16165                  TOTAL_TIME_USER,
16166                  NET_PLANNING_PERCENT,
16167                  X_COORDINATE,
16168                  Y_COORDINATE,
16169                  INCLUDE_IN_ROLLUP,
16170                  OPERATION_YIELD_ENABLED,
16171                  OLD_OPERATION_SEQUENCE_ID,
16172                  ACD_TYPE,
16173                  REVISED_ITEM_SEQUENCE_ID,
16174                  CHANGE_NOTICE,
16175                  ECO_FOR_PRODUCTION,
16176                  SHUTDOWN_TYPE,
16177                  ACTUAL_IPK,
16178                  CRITICAL_TO_QUALITY,
16179                  VALUE_ADDED,
16180                  LAST_UPDATE_DATE,
16181                  LAST_UPDATED_BY,
16182                  LAST_UPDATE_LOGIN,
16183                  CREATION_DATE,
16184                  CREATED_BY,
16185                  REQUEST_ID,
16186                  PROGRAM_APPLICATION_ID,
16187                  PROGRAM_ID,
16188                  PROGRAM_UPDATE_DATE,
16189                  ATTRIBUTE_CATEGORY,
16190                  ATTRIBUTE1,
16191                  ATTRIBUTE2,
16192                  ATTRIBUTE3,
16193                  ATTRIBUTE4,
16194                  ATTRIBUTE5,
16195                  ATTRIBUTE6,
16196                  ATTRIBUTE7,
16197                  ATTRIBUTE8,
16198                  ATTRIBUTE9,
16199                  ATTRIBUTE10,
16200                  ATTRIBUTE11,
16201                  ATTRIBUTE12,
16202                  ATTRIBUTE13,
16203                  ATTRIBUTE14,
16204                  ATTRIBUTE15,
16205                  ORIGINAL_SYSTEM_REFERENCE)
16206                 values
16207                 (p_wip_entity_id,
16208                  v_wljdi_routing_op_seq_num(l_d),
16209                  v_wljdi_recommended(l_d),        --recommended
16210                  null,
16211                  null,
16212                  v_wljdi_scheduled_quantity(l_d), --reco_scheduled_quantity
16213                  v_wljdi_op_start_date(l_d),      --reco_start_date
16214                  v_wljdi_op_completion_date(l_d), --reco_completion_date
16215                  l_op_seq_id,
16216                  -1,
16217                  p_org_id,
16218                  v_wljdi_std_op_id(l_d),
16219                  v_wljdi_std_op_code(l_d),
16220                  v_wljdi_department_id(l_d),
16221                  l_department_code,
16222                  l_scrap_account,
16223                  l_est_absorption_account,
16224                  null,
16225                  v_wljdi_min_trf_qty(l_d),
16226                  v_wljdi_count_point_type(l_d),
16227                  v_wljdi_description(l_d),
16228                  p_creation_date,
16229                  null,
16230                  v_wljdi_backflush_flag(l_d),
16231                  v_wljdi_option_dep_flag(l_d),
16232                  1,
16233                  2,
16234                  1,
16235                  v_wljdi_cum_yield(l_d),
16236                  v_wljdi_rev_cum_yield(l_d),
16237                  null,
16238                  null,
16239                  null,
16240                  null,
16241                  null,
16242                  null,
16243                  null,
16244                  null,
16245                  null,
16246                  v_wljdi_inc_in_rollup(l_d),
16247                  v_wljdi_op_yield_enable(l_d),
16248                  null,
16249                  null,
16250                  null,
16251                  null,
16252                  null,
16253                  null,
16254                  null,
16255                  null,
16256                  null,
16257                  p_last_updt_date,
16258                  p_last_updt_by,
16259                  p_last_updt_login,
16260                  p_creation_date,
16261                  p_created_by,
16262                  p_request_id,
16263                  p_program_appl_id,
16264                  p_program_id,
16265                  p_prog_updt_date,
16266                  v_wljdi_attribute_category(l_d),
16267                  v_wljdi_attribute1(l_d) ,
16268                  v_wljdi_attribute2(l_d) ,
16269                  v_wljdi_attribute3(l_d) ,
16270                  v_wljdi_attribute4(l_d) ,
16271                  v_wljdi_attribute5(l_d) ,
16272                  v_wljdi_attribute6(l_d) ,
16273                  v_wljdi_attribute7(l_d) ,
16274                  v_wljdi_attribute8(l_d) ,
16275                  v_wljdi_attribute9(l_d) ,
16276                  v_wljdi_attribute10(l_d),
16277                  v_wljdi_attribute11(l_d),
16278                  v_wljdi_attribute12(l_d),
16279                  v_wljdi_attribute13(l_d),
16280                  v_wljdi_attribute14(l_d),
16281                  v_wljdi_attribute15(l_d),
16282                  null);
16283 
16284                 if (v_wljdi_std_op_id(l_d) is not null) then
16285 
16286                     -- Validate if a resource of charge type PO_MOVE is present.
16287                     begin
16288                         l_temp :=0;
16289                         select 1
16290                         into l_temp
16291                         from dual
16292                         where exists ( SELECT 1
16293                                        FROM   bom_std_op_resources  bsor
16294                                        WHERE  bsor.standard_operation_id = v_wljdi_std_op_id(l_d)
16295                                        AND    bsor.autocharge_type = WIP_CONSTANTS.PO_MOVE)
16296                            or exists ( SELECT 1
16297                                        FROM   bom_std_sub_op_resources  bsor
16298                                        WHERE  bsor.standard_operation_id = v_wljdi_std_op_id(l_d)
16299                                        AND    bsor.autocharge_type = WIP_CONSTANTS.PO_MOVE);
16300                         if l_temp=1 then
16301                             fnd_message.set_name('WSM','WSM_OP_PO_MOVE');
16302                             p_error_msg  := fnd_message.get;
16303                             p_error_code := - 1;
16304                             raise e_wljdi_common_error;
16305                         end if;
16306                     exception
16307                         when no_data_found then
16308                             null;
16309                         when others then
16310                             fnd_message.set_name('WSM','WSM_OP_PO_MOVE');
16311                             p_error_msg  := fnd_message.get;
16312                             p_error_code := - 1;
16313                             raise e_wljdi_common_error;
16314                     end;
16315                     l_temp :=0;
16316                     -- This insert statement adds primary resources for standard operation.
16317 
16318                     INSERT INTO WSM_COPY_OP_RESOURCES
16319                     (wip_entity_id,
16320                      operation_seq_num,
16321                      resource_seq_num,
16322                      organization_id,
16323                      substitute_group_num,
16324                      replacement_group_num,
16325                      recommended,
16326                      reco_start_date,
16327                      reco_completion_date,
16328                      resource_id,
16329                      resource_code,
16330                      department_id,
16331                      phantom_flag,
16332                      phantom_op_seq_num,
16333                      phantom_item_id,
16334                      activity_id,
16335                      standard_rate_flag,
16336                      assigned_units,
16337                      max_assigned_units,
16338                      firm_type,
16339                      usage_rate_or_amount,
16340                      usage_rate_or_amount_inverse,
16341                      uom_code,
16342                      basis_type,
16343                      schedule_flag,
16344                      resource_offset_percent,
16345                      autocharge_type,
16346                      schedule_seq_num,
16347                      principle_flag,
16348                      setup_id,
16349                      change_notice,
16350                      acd_type,
16351                      last_update_date,
16352                      last_updated_by,
16353                      last_update_login,
16354                      creation_date,
16355                      created_by,
16356                      request_id,
16357                      program_application_id,
16358                      program_id,
16359                      program_update_date,
16360                      attribute_category,
16361                      attribute1,
16362                      attribute2,
16363                      attribute3,
16364                      attribute4,
16365                      attribute5,
16366                      attribute6,
16367                      attribute7,
16368                      attribute8,
16369                      attribute9,
16370                      attribute10,
16371                      attribute11,
16372                      attribute12,
16373                      attribute13,
16374                      attribute14,
16375                      attribute15,
16376                      original_system_reference
16377                     )
16378                     select
16379                      wco.wip_entity_id,
16380                      wco.operation_seq_num,
16381                      bsor.resource_seq_num,
16382                      wco.organization_id,
16383                      bsor.substitute_group_num,
16384                      0,                              -- repl_grp_no
16385                      'Y',
16386                      v_wljdi_op_start_date(l_d),
16387                      v_wljdi_op_completion_date(l_d),
16388                      bsor.resource_id,
16389                      br.resource_code,
16390                      v_wljdi_department_id(l_d),
16391                      null,
16392                      null,
16393                      null,
16394                      bsor.activity_id,
16395                      bsor.standard_rate_flag,
16396                      bsor.assigned_units,
16397                      bsor.assigned_units,
16398                      0,                      -- Unfirmed
16399                      bsor.usage_rate_or_amount,
16400                      bsor.usage_rate_or_amount_inverse,
16401                      br.unit_of_measure,
16402                      bsor.basis_type,
16403                      bsor.schedule_flag,
16404                      null,
16405                      bsor.autocharge_type,
16406                      bsor.schedule_seq_num,
16407                      bsor.principle_flag,
16408                      null,
16409                      null,
16410                      null,
16411                      p_last_updt_date,
16412                      p_last_updt_by,
16413                      p_last_updt_login,
16414                      p_creation_date,
16415                      p_created_by,
16416                      p_request_id,
16417                      p_program_appl_id,
16418                      p_program_id,
16419                      p_prog_updt_date,
16420                      bsor.attribute_category,
16421                      bsor.attribute1,
16422                      bsor.attribute2,
16423                      bsor.attribute3,
16424                      bsor.attribute4,
16425                      bsor.attribute5,
16426                      bsor.attribute6,
16427                      bsor.attribute7,
16428                      bsor.attribute8,
16429                      bsor.attribute9,
16430                      bsor.attribute10,
16431                      bsor.attribute11,
16432                      bsor.attribute12,
16433                      bsor.attribute13,
16434                      bsor.attribute14,
16435                      bsor.attribute15,
16436                      null
16437                     from   wsm_copy_operations wco,
16438                            bom_resources br,
16439                            bom_std_op_resources bsor
16440                     where  bsor.standard_operation_id = v_wljdi_std_op_id(l_d)
16441                     and    br.resource_id = bsor.resource_id
16442                     and    nvl(br.disable_date, SYSDATE + 1) > SYSDATE
16443                     and    wco.organization_id = p_org_id
16444                     and    wco.wip_entity_id = p_wip_entity_id
16445                     and    wco.operation_seq_num = v_wljdi_routing_op_seq_num(l_d);
16446 
16447                     -- This insert statement adds substitute resources for standard operation.
16448 
16449                     INSERT INTO WSM_COPY_OP_RESOURCES
16450                     (wip_entity_id,
16451                      operation_seq_num,
16452                      resource_seq_num,
16453                      organization_id,
16454                      substitute_group_num,
16455                      replacement_group_num,
16456                      recommended,
16457                      reco_start_date,
16458                      reco_completion_date,
16459                      resource_id,
16460                      resource_code,
16461                      department_id,
16462                      phantom_flag,
16463                      phantom_op_seq_num,
16464                      phantom_item_id,
16465                      activity_id,
16466                      standard_rate_flag,
16467                      assigned_units,
16468                      max_assigned_units,
16469                      firm_type,
16470                      usage_rate_or_amount,
16471                      usage_rate_or_amount_inverse,
16472                      uom_code,
16473                      basis_type,
16474                      schedule_flag,
16475                      resource_offset_percent,
16476                      autocharge_type,
16477                      schedule_seq_num,
16478                      principle_flag,
16479                      setup_id,
16480                      change_notice,
16481                      acd_type,
16482                      last_update_date,
16483                      last_updated_by,
16484                      last_update_login,
16485                      creation_date,
16486                      created_by,
16487                      request_id,
16488                      program_application_id,
16489                      program_id,
16490                      program_update_date,
16491                      attribute_category,
16492                      attribute1,
16493                      attribute2,
16494                      attribute3,
16495                      attribute4,
16496                      attribute5,
16497                      attribute6,
16498                      attribute7,
16499                      attribute8,
16500                      attribute9,
16501                      attribute10,
16502                      attribute11,
16503                      attribute12,
16504                      attribute13,
16505                      attribute14,
16506                      attribute15,
16507                      original_system_reference
16508                     )
16509                     select
16510                      wco.wip_entity_id,
16511                      wco.operation_seq_num,
16512                      wcor.resource_seq_num+rownum,
16513                      wco.organization_id,
16514                      bsor.substitute_group_num,
16515                      bsor.replacement_group_num,
16516                      'N',
16517                      v_wljdi_op_start_date(l_d),
16518                      v_wljdi_op_completion_date(l_d),
16519                      bsor.resource_id,
16520                      br.resource_code,
16521                      v_wljdi_department_id(l_d),
16522                      null,
16523                      null,
16524                      null,
16525                      bsor.activity_id,
16526                      bsor.standard_rate_flag,
16527                      bsor.assigned_units,
16528                      bsor.assigned_units,
16529                      0,                      -- Unfirmed
16530                      bsor.usage_rate_or_amount,
16531                      bsor.usage_rate_or_amount_inverse,
16532                      br.unit_of_measure,
16533                      bsor.basis_type,
16534                      bsor.schedule_flag,
16535                      null,
16536                      bsor.autocharge_type,
16537                      bsor.schedule_seq_num,
16538                      null,
16539                      null,
16540                      null,
16541                      null,
16542                      p_last_updt_date,
16543                      p_last_updt_by,
16544                      p_last_updt_login,
16545                      p_creation_date,
16546                      p_created_by,
16547                      p_request_id,
16548                      p_program_appl_id,
16549                      p_program_id,
16550                      p_prog_updt_date,
16551                      bsor.attribute_category,
16552                      bsor.attribute1,
16553                      bsor.attribute2,
16554                      bsor.attribute3,
16555                      bsor.attribute4,
16556                      bsor.attribute5,
16557                      bsor.attribute6,
16558                      bsor.attribute7,
16559                      bsor.attribute8,
16560                      bsor.attribute9,
16561                      bsor.attribute10,
16562                      bsor.attribute11,
16563                      bsor.attribute12,
16564                      bsor.attribute13,
16565                      bsor.attribute14,
16566                      bsor.attribute15,
16567                      null
16568                     from   wsm_copy_operations wco,
16569                            bom_resources br,
16570                            bom_std_sub_op_resources bsor,
16571                            wsm_copy_op_resources wcor
16572                     where  bsor.standard_operation_id = v_wljdi_std_op_id(l_d)
16573                     and    br.resource_id = bsor.resource_id
16574                     and    nvl(br.disable_date, SYSDATE + 1) > SYSDATE
16575                     and    wco.organization_id = p_org_id
16576                     and    wco.wip_entity_id = p_wip_entity_id
16577                     and    wco.operation_seq_num = v_wljdi_routing_op_seq_num(l_d)
16578                     and    exists (select 1
16579                                    from bom_std_op_resources bsor1
16580                                    where bsor1.standard_operation_id = bsor.standard_operation_id
16581                                    and bsor1.substitute_group_num = bsor.substitute_group_num )
16582                     and    wcor.wip_entity_id = wco.wip_entity_id
16583                     and    wcor.operation_seq_num = wco.operation_seq_num
16584                     and    wcor.resource_seq_num = (select max(resource_seq_num)
16585                                                     from   wsm_copy_op_resources wcor1
16586                                                     where  wip_entity_id = wco.wip_entity_id
16587                                                     and operation_seq_num = wco.operation_seq_num);
16588 
16589                     -- Create default Usages for the added primary resources having schedule flag other than 'No'.
16590                     insert into wsm_copy_op_resource_usage
16591                     (wip_entity_id,
16592                      operation_seq_num,
16593                      resource_seq_num,
16594                      organization_id,
16595                      start_date,
16596                      completion_date,
16597                      assigned_units,
16598                      cumulative_processing_time,
16599                      last_update_date,
16600                      last_updated_by,
16601                      creation_date,
16602                      created_by,
16603                      last_update_login,
16604                      request_id,
16605                      program_application_id,
16606                      program_id,
16607                      program_update_date)
16608                     select
16609                      wcor.wip_entity_id,
16610                      wcor.operation_seq_num,
16611                      wcor.resource_seq_num,
16612                      wcor.organization_id,
16613                      wcor.reco_start_date,
16614                      wcor.reco_completion_date,
16615                      wcor.assigned_units,
16616                      to_number(round((wcor.reco_completion_date - wcor.reco_start_date)*1440)), -- Convert to Minutes.
16617                      p_last_updt_date,
16618                      p_last_updt_by,
16619                      p_creation_date,
16620                      p_created_by,
16621                      p_last_updt_login,
16622                      p_request_id,
16623                      p_program_appl_id,
16624                      p_program_id,
16625                      p_prog_updt_date
16626                     from wsm_copy_op_resources wcor
16627                     where wcor.organization_id = p_org_id
16628                     and wcor.wip_entity_id = p_wip_entity_id
16629                     and wcor.operation_seq_num = v_wljdi_routing_op_seq_num(l_d)
16630                     and wcor.recommended = 'Y'
16631                     and wcor.schedule_flag <> WIP_CONSTANTS.SCHED_NO;
16632 
16633                 end if;
16634             end if;
16635             l_d := v_wljdi_record_id.next(l_d);
16636         end loop;
16637     end if;
16638 
16639     l_d_saved := NULL;  -- hold the first l_d for link dis/recommendation
16640     l_path_changed := false;
16641     if NOT(l_link_first IS NULL ) then
16642         -- delete PL/SQL table
16643         l_v_links.delete;
16644         l_v_fm_ops.delete;
16645         l_v_to_ops.delete;
16646         l_v_path_ops.delete;
16647         l_v_path_seq_num.delete;
16648 
16649         -- Added For LBJ Interface Alcoa Enhancement
16650         -- Added to support Add/Delete links.
16651         l_d := l_link_first;
16652         while (l_d IS NOT NULL and
16653                v_wljdi_load_type(l_d) =WSM_LOAD_LINK and
16654                v_wljdi_substitution_type(l_d) in (WSM_SUB_ADD,WSM_SUB_DEL) )
16655         loop
16656             if v_wljdi_substitution_type(l_d) = WSM_SUB_DEL then
16657 
16658                 delete wsm_copy_op_networks
16659                 where wip_entity_id= p_wip_entity_id
16660                 and from_op_seq_num= v_wljdi_routing_op_seq_num(l_d)
16661                 and to_op_seq_num =  v_wljdi_next_rtg_op_seq_num(l_d);
16662 
16663                 if sql%notfound then
16664                     fnd_message.set_name('WSM','WSM_INV_DEL_LINK');
16665                     p_error_msg  := fnd_message.get;
16666                     p_error_code := - 1;
16667                     raise e_wljdi_common_error;
16668                 end if;
16669             else   /* This is for substitution_type add. */
16670                 begin
16671                     select 1 into l_link_exists
16672                     from wsm_copy_op_networks
16673                     where wip_entity_id = p_wip_entity_id and
16674                     from_op_seq_num = v_wljdi_routing_op_seq_num(l_d) and
16675                     to_op_seq_num = v_wljdi_next_rtg_op_seq_num(l_d);
16676                 exception
16677                     when no_data_found then
16678                         l_link_exists := 0;
16679                     when too_many_rows then
16680                         fnd_message.set_name('WSM','WSM_DUP_NETWORK_LINK');
16681                         p_error_msg  := fnd_message.get;
16682                         p_error_code := - 1;
16683                         raise e_wljdi_common_error;
16684                 end;
16685 
16686                 if l_link_exists <> 0 then
16687                     fnd_message.set_name('WSM','WSM_DUP_NETWORK_LINK');
16688                     p_error_msg  := fnd_message.get;
16689                     p_error_code := - 1;
16690                     raise e_wljdi_common_error;
16691                 end if;
16692 
16693                 begin
16694 
16695                     select operation_sequence_id
16696                     into l_from_op_seq_id
16697                     from wsm_copy_operations
16698                     where wip_entity_id = p_wip_entity_id
16699                     and operation_seq_num = v_wljdi_routing_op_seq_num(l_d);
16700 
16701                     select operation_sequence_id
16702                     into l_to_op_seq_id
16703                     from wsm_copy_operations
16704                     where wip_entity_id = p_wip_entity_id
16705                     and operation_seq_num = v_wljdi_next_rtg_op_seq_num(l_d);
16706 
16707                 exception
16708                     when no_data_found then
16709                         fnd_message.set_name('WSM','WSM_DUP_NETWORK_LINK');
16710                         p_error_msg  := fnd_message.get;
16711                         p_error_code := - 1;
16712                         raise e_wljdi_common_error;
16713                     when others then
16714                         fnd_message.set_name('WSM','WSM_INV_NETWORK_LINK');
16715                         p_error_msg  := fnd_message.get;
16716                         p_error_code := - 1;
16717                         raise e_wljdi_common_error;
16718                 end;
16719 
16720                 insert into wsm_copy_op_networks(
16721                  wip_entity_id,
16722                  from_op_seq_num,
16723                  to_op_seq_num,
16724                  from_op_seq_id,
16725                  to_op_seq_id,
16726                  recommended,
16727                  routing_sequence_id,
16728                  transition_type,
16729                  planning_pct,
16730                  last_update_date,
16731                  last_updated_by,
16732                  last_update_login,
16733                  creation_date,
16734                  created_by,
16735                  request_id,
16736                  program_application_id,
16737                  program_id,
16738                  program_update_date,
16739                  attribute_category,
16740                  attribute1,
16741                  attribute2,
16742                  attribute3,
16743                  attribute4,
16744                  attribute5,
16745                  attribute6,
16746                  attribute7,
16747                  attribute8,
16748                  attribute9,
16749                  attribute10,
16750                  attribute11,
16751                  attribute12,
16752                  attribute13,
16753                  attribute14,
16754                  attribute15,
16755                  original_system_reference)
16756                 values(
16757                  p_wip_entity_id,
16758                  v_wljdi_routing_op_seq_num(l_d),
16759                  v_wljdi_next_rtg_op_seq_num(l_d),
16760                  l_from_op_seq_id,
16761                  l_to_op_seq_id,
16762                  v_wljdi_recommended(l_d),
16763                  -1,
16764                  2,
16765                  null,
16766                  p_last_updt_date,
16767                  p_last_updt_by,
16768                  p_last_updt_login,
16769                  p_creation_date,
16770                  p_created_by,
16771                  p_request_id,
16772                  p_program_appl_id,
16773                  p_program_id,
16774                  p_prog_updt_date,
16775                  v_wljdi_attribute_category(l_d),
16776                  v_wljdi_attribute1(l_d),
16777                  v_wljdi_attribute2(l_d),
16778                  v_wljdi_attribute3(l_d),
16779                  v_wljdi_attribute4(l_d),
16780                  v_wljdi_attribute5(l_d),
16781                  v_wljdi_attribute6(l_d),
16782                  v_wljdi_attribute7(l_d),
16783                  v_wljdi_attribute8(l_d),
16784                  v_wljdi_attribute9(l_d),
16785                  v_wljdi_attribute10(l_d),
16786                  v_wljdi_attribute11(l_d),
16787                  v_wljdi_attribute12(l_d),
16788                  v_wljdi_attribute13(l_d),
16789                  v_wljdi_attribute14(l_d),
16790                  v_wljdi_attribute15(l_d),
16791                  null
16792                  );
16793             end if;
16794             l_d := v_wljdi_record_id.next(l_d);
16795         end loop;
16796 
16797 ---Modified for Alcoa Enhancement
16798 
16799     ------------------------------------------------
16800     -- {{ validete the path when the path is changed
16801     ------------------------------------------------
16802   /*  l_d_saved := NULL;  -- hold the first l_d for link dis/recommendation
16803     l_path_changed := false;
16804     if NOT(l_link_first IS NULL and l_op_link_first IS NULL) then
16805         -- delete PL/SQL table
16806         l_v_links.delete;
16807         l_v_fm_ops.delete;
16808         l_v_to_ops.delete;
16809         l_v_path_ops.delete;
16810         l_v_path_seq_num.delete;
16811      if l_with_discommand then   -- {{ with discommand record(s)
16812 l_stmt_num := 60.1;
16813             -- get recommended path from WCON, and saved to PL/SQL table
16814             open c_the_path (
16815                 p_wip_entity_id,
16816                 l_end_op_seq_num);
16817             loop
16818                 fetch c_the_path
16819                 into  l_fm_op,
16820                       l_to_op;
16821                 exit when c_the_path%notfound;
16822                 l_v_links(l_to_op).from_op_seq_num := l_fm_op;
16823                 l_v_links(l_to_op).to_op_seq_num   := l_to_op;
16824             end loop;
16825             close c_the_path;
16826 
16827 l_stmt_num := 60.2;
16828             -- delete discommended paths from PL/SQL table
16829             l_d := l_link_first;
16830             while (l_d IS NOT NULL and v_wljdi_load_type(l_d) =WSM_LOAD_LINK)
16831             loop
16832                 if(v_wljdi_substitution_type(l_d) = WSM_SUB_DIS) then
16833                     if(l_d_saved IS NULL) then
16834                         l_d_saved := l_d;
16835                     end if;
16836                     -- Fixed Bug 12856280.
16837                     l_fm_op := nvl(v_wljdi_routing_op_seq_num(l_d),v_wljdi_job_op_seq_num(l_d));
16838                     l_to_op := v_wljdi_next_rtg_op_seq_num(l_d);
16839                     if( l_v_links.EXISTS(l_to_op) ) then
16840                         if(l_v_links(l_to_op).from_op_seq_num = l_fm_op) then
16841                             l_v_links.delete(l_to_op);
16842                             l_path_changed := true;
16843                         else
16844                             raise e_invalid_discommend_path;
16845                         end if;
16846                     else
16847                         raise e_invalid_discommend_path;
16848                     end if;
16849                 end if;
16850                 l_d := v_wljdi_record_id.next(l_d);
16851             end loop;
16852         end if;      */               -- }} with discommand record(s)
16853 
16854 l_stmt_num := 60.3;
16855         -- add recommended paths to PL/SQL table
16856         l_d := NVL(l_link_first, l_op_link_first);
16857         while (l_d IS NOT NULL and
16858                v_wljdi_load_type(l_d) IN (WSM_LOAD_LINK, WSM_LOAD_LINK_OP))
16859         loop
16860             l_load_type := v_wljdi_load_type(l_d);
16861             l_sub_type  := v_wljdi_substitution_type(l_d);
16862             -- Fixed Bug 12856280.
16863             l_fm_op     := nvl(v_wljdi_routing_op_seq_num(l_d),v_wljdi_job_op_seq_num(l_d));
16864             l_to_op     := v_wljdi_next_rtg_op_seq_num(l_d);
16865 
16866             if(l_load_type = WSM_LOAD_LINK and l_sub_type in (WSM_SUB_REC,WSM_SUB_DIS)) or
16867               (l_load_type = WSM_LOAD_LINK_OP and l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC) and
16868                (l_fm_op IS NOT NULL) and (l_to_op IS NOT NULL) )
16869             then
16870                ---Modified for Alcoa Enhancement
16871 	      /*	if(l_d_saved IS NULL) then
16872                     l_d_saved := l_d;
16873                 end if;
16874                 if( l_v_links.EXISTS(l_to_op) ) then
16875                     if(l_v_links(l_to_op).from_op_seq_num <> l_fm_op) then
16876                         raise e_invalid_recommend_path;
16877                     end if;
16878                 else
16879                     l_v_links(l_to_op).from_op_seq_num := l_fm_op;
16880                     l_v_links(l_to_op).to_op_seq_num   := l_to_op;
16881                     l_path_changed := true;
16882                 end if;*/
16883                 update  wsm_copy_op_networks
16884                 set     recommended            = decode(l_sub_type,WSM_SUB_DIS,'N',
16885                                                                    WSM_SUB_REC,'Y',
16886                                                                    WSM_SUB_CHG,'Y',recommended),
16887                         last_update_date       = p_last_updt_date,
16888                         last_updated_by        = p_last_updt_by,
16889                         last_update_login      = p_last_updt_login,
16890                         request_id             = p_request_id,
16891                         program_application_id = p_program_appl_id,
16892                         program_id             = p_program_id,
16893                         program_update_date    = p_prog_updt_date,
16894                         attribute_category = nvl(attribute_category,v_wljdi_attribute_category(l_d)),
16895                         attribute1 = nvl(attribute1,v_wljdi_attribute1(l_d)),
16896                         attribute2 = nvl(attribute2,v_wljdi_attribute2(l_d)),
16897                         attribute3 = nvl(attribute3,v_wljdi_attribute3(l_d)),
16898                         attribute4 = nvl(attribute4,v_wljdi_attribute4(l_d)),
16899                         attribute5 = nvl(attribute5,v_wljdi_attribute5(l_d)),
16900                         attribute6 = nvl(attribute6,v_wljdi_attribute6(l_d)),
16901                         attribute7 = nvl(attribute7,v_wljdi_attribute7(l_d)),
16902                         attribute8 = nvl(attribute8,v_wljdi_attribute8(l_d)),
16903                         attribute9 = nvl(attribute9,v_wljdi_attribute9(l_d)),
16904                         attribute10 = nvl(attribute10,v_wljdi_attribute10(l_d)),
16905                         attribute11 = nvl(attribute11,v_wljdi_attribute11(l_d)),
16906                         attribute12 = nvl(attribute12,v_wljdi_attribute12(l_d)),
16907                         attribute13 = nvl(attribute13,v_wljdi_attribute13(l_d)),
16908                         attribute14 = nvl(attribute14,v_wljdi_attribute14(l_d)),
16909                         attribute15 = nvl(attribute15,v_wljdi_attribute15(l_d))
16910                 where   wip_entity_id   = p_wip_entity_id
16911                 and     from_op_seq_num = l_fm_op
16912                 and     to_op_seq_num   = l_to_op;
16913 
16914                 if sql%notfound and l_sub_type = WSM_SUB_DIS then
16915                     raise e_invalid_discommend_path;
16916                 end if;
16917 
16918                 if sql%notfound and l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC) then
16919                     raise e_invalid_recommend_path;
16920                 end if;
16921             else
16922                 if ( (l_fm_op IS NULL) or (l_to_op IS NULL) )then
16923                     fnd_message.set_name('WSM','WSM_INVALID_FIELD');
16924                     fnd_message.set_token('FLD_NAME', 'Routing_op_seq and Next_routing_op_seq Combination.');
16925                     p_error_msg  := 'WLJDI(record_id ='||v_wljdi_record_id(l_d)||') '||fnd_message.get;
16926                     p_error_code := - 1;
16927                     raise e_wljdi_common_error;
16928 		end if;
16929             end if;
16930             l_d := v_wljdi_record_id.next(l_d);
16931         end loop;
16932 
16933 l_stmt_num := 60.4;
16934 ---Modified for Alcoa Enhancement
16935        /* if( l_path_changed = true) then
16936 
16937             -- validate the paths
16938             validate_wljdi_path(
16939                 p_wip_entity_id  => p_wip_entity_id,
16940                 p_end_op_seq_num => l_end_op_seq_num,
16941                 p_links          => l_v_links,
16942                 x_error_code     => p_error_code,
16943                 x_error_msg      => p_error_msg);
16944             if p_error_code <> 0 then
16945                 l_d := l_d_saved;   -- l_d can be set to null
16946                 raise e_wljdi_common_error;
16947             end if;
16948             if lbji_debug = 'Y' then
16949                 fnd_file.put_line(fnd_file.log, 'Mark new recommended path ... OK.');
16950             end if;
16951 
16952 
16953             -- {{ Begin: Update WCO, WCON to mark the recommended operation
16954 l_stmt_num := 70;
16955 
16956             l_on_reco_path := 'N';
16957 
16958             if(l_v_links.count = 0) then    -- Every link is discommended
16959                 l_out := 1;
16960                 l_v_path_ops(l_out)     := l_end_op_seq_num;
16961                 l_v_path_seq_num(l_out) := 1;
16962                 if( nvl(l_cur_rtg_op_seq_num, -1) = l_end_op_seq_num )
16963                 then
16964                     l_on_reco_path := 'Y';
16965                 end if;
16966             else
16967                 l_in := l_end_op_seq_num;
16968                 l_out := 1;
16969                 while ((l_in IS NOT NULL) and (l_v_links.exists(l_in)))
16970                 loop
16971                     l_v_fm_ops(l_out)   := l_v_links(l_in).from_op_seq_num;
16972                     l_v_to_ops(l_out)   := l_v_links(l_in).to_op_seq_num;
16973                     l_v_path_ops(l_out) := l_v_to_ops(l_out);
16974                     if( nvl(l_cur_rtg_op_seq_num, -1) = l_v_to_ops(l_out) )
16975                     then
16976                         l_on_reco_path := 'Y';
16977                     end if;
16978                     l_out := l_out + 1;
16979                     l_in  := l_v_links(l_in).from_op_seq_num;
16980                 end loop;
16981 
16982                 -- l_in is now l_v_links(last).from_op_seq_num;
16983                 if(l_in IS NOT NULL) then
16984                     l_v_path_ops(l_out) := l_in;
16985                     if(nvl(l_cur_rtg_op_seq_num, -1) = l_in )
16986                     then
16987                         l_on_reco_path := 'Y';
16988                     end if;
16989                 end if;
16990 
16991                 -- set the l_v_path_seq_num
16992                 for l_out in 1..l_v_path_ops.count
16993                 loop
16994                     l_v_path_seq_num(l_out) := l_v_path_ops.count +1 - l_out;
16995                 end loop;
16996 
16997             end if;
16998 
16999             -- We should mark WCON and WCO
17000             -- OSFM will not nullify all the dates information when the path is
17001             -- changed, nor will infinite scheduler be called. User can go to
17002             -- Lot Based Job Form to infinte schedule a job if he chooses to
17003 
17004 l_stmt_num := 70.1;
17005             -- reset WCON
17006             update  wsm_copy_op_networks
17007             set     recommended            = 'N',
17008                     last_update_date       = p_last_updt_date,
17009                     last_updated_by        = p_last_updt_by,
17010                     last_update_login      = p_last_updt_login,
17011                     request_id             = p_request_id,
17012                     program_application_id = p_program_appl_id,
17013                     program_id             = p_program_id,
17014                     program_update_date    = p_prog_updt_date
17015             where   wip_entity_id = p_wip_entity_id;
17016 
17017 l_stmt_num := 70.2;
17018             -- update WCON
17019             if(l_v_fm_ops.count <> 0) then
17020                 forall i in l_v_fm_ops.first..l_v_fm_ops.last
17021                     update  wsm_copy_op_networks
17022                     set     recommended            = 'Y',
17023                             last_update_date       = p_last_updt_date,
17024                             last_updated_by        = p_last_updt_by,
17025                             last_update_login      = p_last_updt_login,
17026                             request_id             = p_request_id,
17027                             program_application_id = p_program_appl_id,
17028                             program_id             = p_program_id,
17029                             program_update_date    = p_prog_updt_date
17030                     where   wip_entity_id = p_wip_entity_id
17031                     and     from_op_seq_num = l_v_fm_ops(i)
17032                     and     to_op_seq_num = l_v_to_ops(i);
17033             end if;
17034 
17035 l_stmt_num := 70.3;
17036             -- reset WCO
17037             update  wsm_copy_operations
17038             set     recommended            = 'N',
17039                     reco_path_seq_num      = null,
17040                     last_update_date       = p_last_updt_date,
17041                     last_updated_by        = p_last_updt_by,
17042                     last_update_login      = p_last_updt_login,
17043                     request_id             = p_request_id,
17044                     program_application_id = p_program_appl_id,
17045                     program_id             = p_program_id,
17046                     program_update_date    = p_prog_updt_date
17047             where   wip_entity_id = p_wip_entity_id;
17048 
17049 l_stmt_num := 70.4;
17050             -- mark the recomended operations
17051             forall i in 1..l_v_path_ops.count
17052                 update  wsm_copy_operations
17053                 set     recommended            = 'Y',
17054                         reco_path_seq_num      = l_v_path_seq_num(i),
17055                         last_update_date       = p_last_updt_date,
17056                         last_updated_by        = p_last_updt_by,
17057                         last_update_login      = p_last_updt_login,
17058                         request_id             = p_request_id,
17059                         program_application_id = p_program_appl_id,
17060                         program_id             = p_program_id,
17061                         program_update_date    = p_prog_updt_date
17062                 where   wip_entity_id = p_wip_entity_id
17063                 and     operation_seq_num = l_v_path_ops(i);
17064             -- }} End: Update WCO, WCON to mark the recommended operation
17065 
17066 l_stmt_num := 70.5;
17067             -- mark wlbj.on_rec_path flag
17068             update  wsm_lot_based_jobs
17069             set     on_rec_path = l_on_reco_path
17070             where   wip_entity_id = p_wip_entity_id
17071             and     organization_id = p_org_id;
17072 
17073         end if; -- if( l_path_changed = true)*/
17074 
17075     end if;
17076     ------------------------------------------------
17077     -- }} validete the path when the path is changed
17078     ------------------------------------------------
17079 l_stmt_num := 90;
17080 
17081     ------------------------------------------------
17082     -- {{ validate Network
17083     -- Added For LBJ Interface Alcoa Enhancement
17084     ------------------------------------------------
17085     if l_validate_network=0 then
17086 
17087         l_validate_network := WSMPUTIL.validate_job_network(p_wip_entity_id => p_wip_entity_id,
17088                                                             x_err_code      => p_error_code,
17089                                                             x_err_msg       => p_error_msg);
17090 
17091         if l_validate_network =1 then
17092             raise e_wljdi_common_error;
17093         end if;
17094 
17095         -- delete PL/SQL table
17096         l_v_links.delete;
17097         l_v_fm_ops.delete;
17098         l_v_to_ops.delete;
17099         l_v_path_ops.delete;
17100         l_v_path_seq_num.delete;
17101         l_on_reco_path :='N';
17102 
17103         -- Update WCO based on added or deleted links.
17104 
17105         open c_the_path (p_wip_entity_id,
17106                          l_end_op_seq_num);
17107         loop
17108             fetch c_the_path into  l_fm_op,l_to_op;
17109             exit when c_the_path%notfound;
17110 
17111             l_v_links(l_to_op).from_op_seq_num := l_fm_op;
17112             l_v_links(l_to_op).to_op_seq_num   := l_to_op;
17113 
17114         end loop;
17115         close c_the_path;
17116 
17117         if(l_v_links.count = 0) then    -- Every link is discommended
17118 
17119             l_out := 1;
17120             l_v_path_ops(l_out)     := l_end_op_seq_num;
17121             l_v_path_seq_num(l_out) := 1;
17122             if( nvl(l_cur_rtg_op_seq_num, -1) = l_end_op_seq_num )
17123             then
17124                 l_on_reco_path := 'Y';
17125             end if;
17126 
17127         else
17128 
17129             l_in := l_end_op_seq_num;
17130             l_out := 1;
17131             while ((l_in IS NOT NULL) and (l_v_links.exists(l_in)))
17132             loop
17133                 l_v_fm_ops(l_out)   := l_v_links(l_in).from_op_seq_num;
17134                 l_v_to_ops(l_out)   := l_v_links(l_in).to_op_seq_num;
17135                 l_v_path_ops(l_out) := l_v_to_ops(l_out);
17136                 if( nvl(l_cur_rtg_op_seq_num, -1) = l_v_to_ops(l_out) )
17137                 then
17138                     l_on_reco_path := 'Y';
17139                 end if;
17140                 l_out := l_out + 1;
17141                 l_in  := l_v_links(l_in).from_op_seq_num;
17142             end loop;
17143 
17144             -- l_in should have from_op of last link in recommended path when traversed from last_op.
17145             if(l_in IS NOT NULL) then
17146                 l_v_path_ops(l_out) := l_in;
17147                 if(nvl(l_cur_rtg_op_seq_num, -1) = l_in )
17148                 then
17149                     l_on_reco_path := 'Y';
17150                 end if;
17151             end if;
17152 
17153             -- set path_seq_num
17154             for l_out in 1..l_v_path_ops.count
17155             loop
17156                 l_v_path_seq_num(l_out) := l_v_path_ops.count +1 - l_out;
17157             end loop;
17158 
17159         end if;
17160 
17161         -- reset WCO
17162         update  wsm_copy_operations
17163         set     recommended            = 'N',
17164                 reco_path_seq_num      = null,
17165                 last_update_date       = p_last_updt_date,
17166                 last_updated_by        = p_last_updt_by,
17167                 last_update_login      = p_last_updt_login,
17168                 request_id             = p_request_id,
17169                 program_application_id = p_program_appl_id,
17170                 program_id             = p_program_id,
17171                 program_update_date    = p_prog_updt_date
17172         where   wip_entity_id = p_wip_entity_id;
17173 
17174         -- mark the recomended operations
17175         forall i in 1..l_v_path_ops.count
17176             update  wsm_copy_operations
17177             set     recommended            = 'Y',
17178                     reco_path_seq_num      = l_v_path_seq_num(i),
17179                     last_update_date       = p_last_updt_date,
17180                     last_updated_by        = p_last_updt_by,
17181                     last_update_login      = p_last_updt_login,
17182                     request_id             = p_request_id,
17183                     program_application_id = p_program_appl_id,
17184                     program_id             = p_program_id,
17185                     program_update_date    = p_prog_updt_date
17186             where   wip_entity_id = p_wip_entity_id
17187             and     operation_seq_num = l_v_path_ops(i);
17188 
17189         -- Null out dates for non-recommended operations.
17190         update  wsm_copy_operations
17191         set     reco_start_date      = null,
17192                 reco_completion_date = null
17193         where   wip_entity_id = p_wip_entity_id
17194         and     recommended = 'N';
17195 
17196         -- mark wlbj.on_rec_path flag
17197         update  wsm_lot_based_jobs
17198         set     on_rec_path = l_on_reco_path
17199         where   wip_entity_id = p_wip_entity_id
17200         and     organization_id = p_org_id;
17201 
17202         -- delete PL/SQL table
17203         l_v_links.delete;
17204         l_v_fm_ops.delete;
17205         l_v_to_ops.delete;
17206         l_v_path_ops.delete;
17207         l_v_path_seq_num.delete;
17208         l_on_reco_path :='N';
17209     end if;
17210     ------------------------------------------------
17211     -- }} validate Network
17212     ------------------------------------------------
17213 
17214 l_stmt_num := 100;
17215     ------------------------------------------------
17216     -- {{ validete operation
17217     ------------------------------------------------
17218     -- for load_type 3, sub_type 3 and 4,
17219     -- for load_type 6, sub_type 4,
17220     -- will only update operation_start_date, operation_completion_date,
17221     -- operation_yield, scheduled_quantity
17222 
17223     if NOT(l_op_first IS NULL and l_op_link_first IS NULL) then
17224         -- clear output PL/SQL table
17225         l_out := 0;
17226         l_v_rtg_op_seq.delete;
17227         l_v_op_yield.delete;
17228         l_v_op_qty.delete;
17229         l_v_start_date.delete;
17230         l_v_compl_date.delete;
17231  /*Added for bugfix:7248992 */
17232         l_v_cum_yield.delete;
17233         l_v_rev_cum_yield.delete;
17234         l_v_attribute_category.delete;
17235         l_v_attribute1.delete;
17236         l_v_attribute2.delete;
17237         l_v_attribute3.delete;
17238         l_v_attribute4.delete;
17239         l_v_attribute5.delete;
17240         l_v_attribute6.delete;
17241         l_v_attribute7.delete;
17242         l_v_attribute8.delete;
17243         l_v_attribute9.delete;
17244         l_v_attribute10.delete;
17245         l_v_attribute11.delete;
17246         l_v_attribute12.delete;
17247         l_v_attribute13.delete;
17248         l_v_attribute14.delete;
17249         l_v_attribute15.delete;
17250 
17251 
17252         l_d := NVL(l_op_first, l_op_link_first);
17253         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) IN (WSM_LOAD_OP, WSM_LOAD_LINK_OP))
17254         loop
17255             l_load_type     := v_wljdi_load_type(l_d);
17256             l_sub_type      := v_wljdi_substitution_type(l_d);
17257             l_rtg_op_seq_num:= v_wljdi_routing_op_seq_num(l_d);
17258             l_job_op_seq_num:= v_wljdi_job_op_seq_num(l_d);
17259             l_op_qty        := v_wljdi_scheduled_quantity(l_d);
17260             l_op_yield      := v_wljdi_op_yield(l_d);
17261             l_op_sdate      := v_wljdi_op_start_date(l_d);
17262             l_op_edate      := v_wljdi_op_completion_date(l_d);
17263             /*Added for bugfix:7248992 */
17264             l_cum_yield     := v_wljdi_cum_yield(l_d);
17265             l_rev_cum_yield := v_wljdi_rev_cum_yield(l_d);
17266 
17267 
17268             l_v_ops.delete;
17269             l_v_ops(1).job_op_seq_num     := l_job_op_seq_num;
17270             l_v_ops(1).rtg_op_seq_num     := l_rtg_op_seq_num;
17271             l_v_ops(1).scheduled_quantity := l_op_qty;
17272             l_v_ops(1).yield              := l_op_yield;
17273             l_v_ops(1).start_date         := l_op_sdate;
17274             l_v_ops(1).compltion_date     := l_op_edate;
17275             /*Added for bugfix:7248992 */
17276             l_v_ops(1).cumulative_yield         := l_cum_yield;
17277             l_v_ops(1).reverse_cumulative_yield := l_rev_cum_yield;
17278 
17279             -- {{ Update WIP table
17280  -- Added For LBJ Interface Alcoa Enhancement
17281             if l_sub_type not in (WSM_SUB_DEL, WSM_SUB_ADD) then
17282 		    if(l_job_op_seq_num IS NOT NULL) then
17283 			if(l_job_op_seq_num = l_cur_job_op_seq_num) then
17284 	l_stmt_num := 110.1;
17285 			    validate_wljdi_operation (
17286 				p_wip_entity_id            => p_wip_entity_id,
17287 				p_org_id                   => p_org_id,
17288 				p_v_ops                    => l_v_ops,
17289 				p_job_scheduled_start_date => l_job_start_date,
17290 				p_job_scheduled_compl_date => l_job_compl_date,
17291 				p_job_scheduled_qty        => l_cur_job_qty,
17292 				p_handle_wip_cur_op        => 'Y',
17293 				x_error_code               => p_error_code,
17294 				x_error_msg                => p_error_msg );
17295 			    if p_error_code <> 0 then
17296 				raise e_wljdi_common_error;
17297 			    end if;
17298 	l_stmt_num := 110.2;
17299 			    -- update WO for current operation
17300 			    UPDATE WIP_OPERATIONS
17301 			    set     scheduled_quantity         = NVL(l_op_qty,   scheduled_quantity),
17302 				    first_unit_start_date      = NVL(l_op_sdate, first_unit_start_date),
17303 				    first_unit_completion_date = NVL(l_op_edate, first_unit_completion_date),
17304 				    last_unit_start_date       = NVL(l_op_sdate, last_unit_start_date),
17305 				    last_unit_completion_date  = NVL(l_op_edate, last_unit_completion_date),
17306 				    last_update_date           = p_last_updt_date,
17307 				    last_updated_by            = p_last_updt_by,
17308 				    last_update_login          = p_last_updt_login,
17309 				    request_id                 = p_request_id,
17310 				    program_application_id     = p_program_appl_id,
17311 				    program_id                 = p_program_id,
17312 				    program_update_date        = p_prog_updt_date,
17313         attribute_category = nvl(attribute_category,v_wljdi_attribute_category(l_d)),
17314         attribute1 = nvl(attribute1,v_wljdi_attribute1(l_d)),
17315         attribute2 = nvl(attribute2,v_wljdi_attribute2(l_d)),
17316         attribute3 = nvl(attribute3,v_wljdi_attribute3(l_d)),
17317         attribute4 = nvl(attribute4,v_wljdi_attribute4(l_d)),
17318         attribute5 = nvl(attribute5,v_wljdi_attribute5(l_d)),
17319         attribute6 = nvl(attribute6,v_wljdi_attribute6(l_d)),
17320         attribute7 = nvl(attribute7,v_wljdi_attribute7(l_d)),
17321         attribute8 = nvl(attribute8,v_wljdi_attribute8(l_d)),
17322         attribute9 = nvl(attribute9,v_wljdi_attribute9(l_d)),
17323         attribute10 = nvl(attribute10,v_wljdi_attribute10(l_d)),
17324         attribute11 = nvl(attribute11,v_wljdi_attribute11(l_d)),
17325         attribute12 = nvl(attribute12,v_wljdi_attribute12(l_d)),
17326         attribute13 = nvl(attribute13,v_wljdi_attribute13(l_d)),
17327         attribute14 = nvl(attribute14,v_wljdi_attribute14(l_d)),
17328         attribute15 = nvl(attribute15,v_wljdi_attribute15(l_d))
17329 			    where   wip_entity_id = p_wip_entity_id
17330 			    and     operation_seq_num = l_job_op_seq_num;
17331 
17332 			else    -- l_job_op_seq_num <> l_cur_job_op_seq_num
17333 			    -- this can happen when recommendations arrive after the job
17334 			    -- is moved. should give a warning message
17335 			    fnd_file.put_line(fnd_file.log,
17336 				'Warning: operation recommendations for current operation are ignored ' ||
17337 				'(record_id = ' || l_d || ')' );
17338 			end if;
17339 
17340 		    -- }} Update WIP table
17341 		    -- {{ Update WSM_COPY table
17342 		    elsif(l_rtg_op_seq_num IS NOT NULL) then
17343 	l_stmt_num := 120;
17344 			validate_wljdi_operation (
17345 			    p_wip_entity_id             => p_wip_entity_id,
17346 			    p_org_id                    => p_org_id,
17347 			    p_v_ops                     => l_v_ops,
17348 			    p_job_scheduled_start_date  => l_job_start_date,
17349 			    p_job_scheduled_compl_date  => l_job_compl_date,
17350 			    p_job_scheduled_qty         => l_cur_job_qty,
17351 			    p_handle_wip_cur_op         => 'N',
17352 			    x_error_code                => p_error_code,
17353 			    x_error_msg                 => p_error_msg );
17354 			if p_error_code <> 0 then
17355 			    raise e_wljdi_common_error;
17356 			end if;
17357 
17358 			-- save validated info to an array
17359 			l_v_rtg_op_seq(l_out) := l_rtg_op_seq_num;
17360 			l_v_op_yield(l_out)   := l_op_yield;
17361 			l_v_op_qty(l_out)     := l_op_qty;
17362 			l_v_start_date(l_out) := l_op_sdate;
17363 			l_v_compl_date(l_out) := l_op_edate;
17364 
17365 			/*Added for bugfix:7248992 */
17366 			l_v_cum_yield(l_out)       := l_cum_yield;
17367 			l_v_rev_cum_yield(l_out)   := l_rev_cum_yield;
17368    l_v_attribute_category(l_out) :=v_wljdi_attribute_category(l_d);
17369    l_v_attribute1(l_out)   := v_wljdi_attribute1(l_d) ;
17370    l_v_attribute2(l_out)   := v_wljdi_attribute2(l_d) ;
17371    l_v_attribute3(l_out)   := v_wljdi_attribute3(l_d) ;
17372    l_v_attribute4(l_out)   := v_wljdi_attribute4(l_d) ;
17373    l_v_attribute5(l_out)   := v_wljdi_attribute5(l_d) ;
17374    l_v_attribute6(l_out)   := v_wljdi_attribute6(l_d) ;
17375    l_v_attribute7(l_out)   := v_wljdi_attribute7(l_d) ;
17376    l_v_attribute8(l_out)   := v_wljdi_attribute8(l_d) ;
17377    l_v_attribute9(l_out)   := v_wljdi_attribute9(l_d) ;
17378    l_v_attribute10(l_out) := v_wljdi_attribute10(l_d);
17379    l_v_attribute11(l_out) := v_wljdi_attribute11(l_d);
17380    l_v_attribute12(l_out) := v_wljdi_attribute12(l_d);
17381    l_v_attribute13(l_out) := v_wljdi_attribute13(l_d);
17382    l_v_attribute14(l_out) := v_wljdi_attribute14(l_d);
17383    l_v_attribute15(l_out) := v_wljdi_attribute15(l_d);
17384 
17385 			IF lbji_debug = 'Y' THEN
17386 				 fnd_file.put_line(fnd_file.log,'After validate_wljdi_operation : ' || l_stmt_num ||
17387 						   ' Operation start date : ' || to_char(l_op_sdate,'DD-MON-YYYY HH24:MI:SS') ||
17388 						   ' Operation end date :' || to_char(l_op_edate,'DD-MON-YYYY HH24:MI:SS'));
17389 			END IF;
17390 			l_out := l_out +1;
17391 		    end if;
17392 		    -- }} Update WSM_COPY table
17393             -- Added For LBJ Interface Alcoa Enhancement
17394             else
17395                 if l_sub_type = WSM_SUB_DEL then
17396                     if l_rtg_op_seq_num is null then
17397                         fnd_message.set_name('WSM','WSM_NULL_FIELD');
17398                         fnd_message.set_token('FLD_NAME', 'Routing_op_seq_num');
17399                         p_error_msg := fnd_message.get;
17400                         p_error_code := -1;
17401                         raise e_wljdi_common_error;
17402                     else
17403                         -- Validate for start or end operation.
17404                         l_temp :=0;
17405                         begin
17406                             select 1
17407                             into l_temp
17408                             from wsm_copy_operations wco
17409                             where wco.wip_entity_id = p_wip_entity_id
17410                             and wco.organization_id = p_org_id
17411                             and wco.operation_seq_num = l_rtg_op_seq_num
17412                             and wco.network_start_end is null;
17413                         exception
17414                             when others then
17415                                 fnd_message.set_name('WSM','WSM_NW_START_END_NO_DEL');
17416                                 p_error_msg := fnd_message.get;
17417                                 p_error_code := -1;
17418                                 raise e_wljdi_common_error;
17419                         end;
17420                         -- Validate for any existing links with this operation.
17421                         l_temp :=0;
17422 
17423                         begin
17424 
17425                             select 1
17426                             into l_temp
17427                             from dual
17428                             where exists ( select 1
17429                                            from wsm_copy_op_networks
17430                                            where wip_entity_id = p_wip_entity_id
17431                                            and from_op_seq_num = l_rtg_op_seq_num
17432                                            UNION ALL
17433                                            select 1
17434                                            from wsm_copy_op_networks
17435                                            where wip_entity_id = p_wip_entity_id
17436                                            and to_op_seq_num = l_rtg_op_seq_num);
17437 
17438                             if l_temp=1 THEN
17439 
17440                                 fnd_message.set_name('WSM','WSM_NET_LINK_EXIST');
17441                                 p_error_msg := fnd_message.get;
17442                                 p_error_code := -1;
17443                                 raise e_wljdi_common_error;
17444                             end if;
17445 
17446                         exception
17447                             when no_data_found then
17448                                 null;
17449                             when others then
17450                                 fnd_message.set_name('WSM','WSM_NET_LINK_EXIST');
17451                                 p_error_msg := fnd_message.get;
17452                                 p_error_code := -1;
17453                                 raise e_wljdi_common_error;
17454                         end;
17455                     end if;
17456 
17457                     delete from wsm_copy_operations
17458                     where wip_entity_id = p_wip_entity_id
17459                     and operation_seq_num = l_rtg_op_seq_num;
17460 
17461                     delete from wsm_copy_op_resources
17462                     where wip_entity_id = p_wip_entity_id
17463                     and operation_seq_num = l_rtg_op_seq_num;
17464 
17465                     delete from wsm_copy_op_resource_instances
17466                     where wip_entity_id = p_wip_entity_id
17467                     and operation_seq_num = l_rtg_op_seq_num;
17468 
17469                     delete from wsm_copy_op_resource_usage
17470                     where wip_entity_id = p_wip_entity_id
17471                     and operation_seq_num = l_rtg_op_seq_num;
17472 
17473                     delete from wsm_copy_requirement_ops
17474                     where wip_entity_id = p_wip_entity_id
17475                     and operation_seq_num = l_rtg_op_seq_num;
17476 
17477                     delete from wsm_copy_requirement_ops
17478                     where wip_entity_id = p_wip_entity_id
17479                     and operation_seq_num = (-1)*l_rtg_op_seq_num;
17480 
17481                 end if;
17482             end if;
17483             l_d := v_wljdi_record_id.next(l_d);
17484             if(l_d IS NOT NULL and v_wljdi_load_type(l_d) IN (4, 5)) then
17485                 l_d := l_op_link_first;         -- skip load_type = 4, 5
17486             end if;
17487         end loop;
17488         if lbji_debug = 'Y' then
17489             fnd_file.put_line(fnd_file.log, 'Mark recommended operations ... OK.');
17490         end if;
17491 
17492 l_stmt_num := 110;
17493         -- Update WCO, to mark changes
17494         if(l_out > 0) then
17495             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
17496                 UPDATE  WSM_COPY_OPERATIONS
17497                 set     yield                   = NVL(l_v_op_yield(i), yield),
17498                         reco_scheduled_quantity = NVL(l_v_op_qty(i),   reco_scheduled_quantity),
17499                         --BC: bug 3545531 we will allow user to nullify the dates on WCO
17500                         --reco_start_date         = NVL(l_v_start_date(i), reco_start_date),
17501                         --reco_completion_date    = NVL(l_v_compl_date(i), reco_completion_date),
17502                         cumulative_yield        = NVL(l_v_cum_yield(i), cumulative_yield),/*Added for bugfix:7248992 */
17503                         reverse_cumulative_yield= NVL(l_v_rev_cum_yield(i),reverse_cumulative_yield),/*Added for bugfix:7248992 */
17504                         reco_start_date         = l_v_start_date(i),
17505                         reco_completion_date    = l_v_compl_date(i),
17506                         --EC: bug 3545531
17507                         last_update_date        = p_last_updt_date,
17508                         last_updated_by         = p_last_updt_by,
17509                         last_update_login       = p_last_updt_login,
17510                         request_id              = p_request_id,
17511                         program_application_id  = p_program_appl_id,
17512                         program_id              = p_program_id,
17513                         program_update_date     = p_prog_updt_date,
17514                         attribute_category = nvl(attribute_category,l_v_attribute_category(i)),
17515                         attribute1 = nvl(attribute1,l_v_attribute1(i)),
17516                         attribute2 = nvl(attribute2,l_v_attribute2(i)),
17517                         attribute3 = nvl(attribute3,l_v_attribute3(i)),
17518                         attribute4 = nvl(attribute4,l_v_attribute4(i)),
17519                         attribute5 = nvl(attribute5,l_v_attribute5(i)),
17520                         attribute6 = nvl(attribute6,l_v_attribute6(i)),
17521                         attribute7 = nvl(attribute7,l_v_attribute7(i)),
17522                         attribute8 = nvl(attribute8,l_v_attribute8(i)),
17523                         attribute9 = nvl(attribute9,l_v_attribute9(i)),
17524                         attribute10 = nvl(attribute10,l_v_attribute10(i)),
17525                         attribute11 = nvl(attribute11,l_v_attribute11(i)),
17526                         attribute12 = nvl(attribute12,l_v_attribute12(i)),
17527                         attribute13 = nvl(attribute13,l_v_attribute13(i)),
17528                         attribute14 = nvl(attribute14,l_v_attribute14(i)),
17529                         attribute15 = nvl(attribute15,l_v_attribute15(i))
17530                 where   wip_entity_id = p_wip_entity_id
17531                 and     operation_seq_num = l_v_rtg_op_seq(i);
17532         end if;
17533 
17534     end if;
17535     ------------------------------------------------
17536     -- }} validete operation
17537     ------------------------------------------------
17538 
17539 l_stmt_num := 200;
17540     ------------------------------------------------
17541     -- {{ validete operation resources
17542     ------------------------------------------------
17543     if l_res_first IS NOT NULL then
17544         -- clear output PL/SQL table
17545         l_out := 0;
17546         l_v_rtg_op_seq.delete;
17547         l_v_start_date.delete;
17548         l_v_compl_date.delete;
17549         l_v_recoflag.delete;
17550         l_v_res_ids.delete;
17551         l_v_res_sub_grp.delete;
17552         l_v_res_rpl_grp.delete;
17553 
17554         /* ST : Detailed Scheduling start */
17555         l_v_res_seq_nums.delete;
17556         l_v_res_firm_type.delete;
17557         l_v_res_setup_id.delete;
17558         l_v_res_grp_seq_id.delete;
17559         l_v_res_grp_seq_num.delete;
17560         l_v_res_max_assg_units.delete;
17561         l_v_res_parent_seq_num.delete;
17562         l_v_res_batch_id.delete;
17563         l_v_res_assg_units.delete;
17564 
17565         l_v_attribute_category.delete;
17566         l_v_attribute1.delete;
17567         l_v_attribute2.delete;
17568         l_v_attribute3.delete;
17569         l_v_attribute4.delete;
17570         l_v_attribute5.delete;
17571         l_v_attribute6.delete;
17572         l_v_attribute7.delete;
17573         l_v_attribute8.delete;
17574         l_v_attribute9.delete;
17575         l_v_attribute10.delete;
17576         l_v_attribute11.delete;
17577         l_v_attribute12.delete;
17578         l_v_attribute13.delete;
17579         l_v_attribute14.delete;
17580         l_v_attribute15.delete;
17581 
17582         /* will store the recommendations..... */
17583         l_v_res_recom.delete;
17584 
17585         -- ST : Detailed Scheduling..
17586         -- We'll store the data here quite simple..
17587         -- l_v_setup_op_res_num('Y-10-10') := 'Y';
17588         -- l_v_setup_op_res_num('(handle_curr_op)-(Op seq Num)-(resource seq num)') := 'Y';
17589         -- Indicates that setup resource has been added for the
17590         -- resource seq num 10, operation 10 in (WIP_OPERATIONS) --> handle curr op is Y'
17591         l_v_setup_op_res_num.delete;
17592         -- ST : Detailed Scheduling...
17593 
17594 
17595         l_d := l_res_first;
17596         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) = WSM_LOAD_RES)
17597         loop
17598 
17599         l_stmt_num := 201;
17600 
17601             l_load_type       := v_wljdi_load_type(l_d);
17602             l_sub_type        := v_wljdi_substitution_type(l_d);
17603             l_job_op_seq_num  := v_wljdi_job_op_seq_num(l_d);     -- current op only
17604             l_rtg_op_seq_num  := v_wljdi_routing_op_seq_num(l_d); -- future op only
17605             l_resource_id     := v_wljdi_resource_id_new(l_d);
17606             l_sub_grp_num     := v_wljdi_substitute_group_num(l_d);
17607             l_rpl_grp_num     := nvl(v_wljdi_replacement_group_num(l_d), 0);
17608             --Alcoa Enhancement
17609             l_applied_resource_units := v_wljdi_applied_resource_units(l_d);
17610             l_resource_seq_num       := v_wljdi_resource_seq_num(l_d) ;
17611 
17612 --          Modified for Alcoa Enhancement
17613 --            if l_sub_type = (WSM_SUB_DEL) then
17614 --                fnd_file.put_line(fnd_file.log,
17615 --                                'Warning: deleting resources for current operation '||
17616 --                                'is currently not supported and ignored.' -- CZHDBG_MSG
17617 --                                );
17618 --            end if;
17619 ---Added for Alcoa Enhancement
17620                --Alcoa Enhancement Resource Start : Delete
17621                if l_sub_type = WSM_SUB_DEL THEN
17622                    if (l_job_op_seq_num IS NOT NULL) then  -- Current Operation
17623                        if (l_job_op_seq_num = l_cur_job_op_seq_num) THEN
17624 
17625                            -- pack data into PL/SQL table l_v_res for validation
17626                            l_v_res.delete;
17627                            l_v_res(l_resource_id).job_op_seq_num  := l_job_op_seq_num;
17628                            l_v_res(l_resource_id).resource_id     := l_resource_id;
17629                            l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
17630                            l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
17631                            l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
17632                            l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
17633 
17634                             /* ST : Detailed Scheduling start */
17635                            l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17636                            l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
17637                            l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
17638                            l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
17639                            l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
17640                            l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
17641                            l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
17642                            l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
17643                            l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
17644                            l_v_res(l_resource_id).applied_resource_units  := v_wljdi_applied_resource_units(l_d);
17645 
17646                            /* ST : Detailed Scheduling end */
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 => 'Y',
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                            delete from wip_operation_resources wor
17662                            where  wor.wip_entity_id     = p_wip_entity_id
17663                            and    wor.operation_seq_num = l_job_op_seq_num
17664                            and    wor.resource_seq_num  = l_resource_seq_num
17665                            and    wor.organization_id   = p_org_id;
17666 
17667                            delete from wip_sub_operation_resources wsor
17668                            where  wsor.wip_entity_id     = p_wip_entity_id
17669                            and    wsor.operation_seq_num = l_job_op_seq_num
17670                            and    wsor.resource_seq_num  = l_resource_seq_num
17671                            and    wsor.organization_id   = p_org_id;
17672 
17673                            delete from wip_op_resource_instances wori
17674                            where  wori.wip_entity_id     = p_wip_entity_id
17675                            and    wori.operation_seq_num = l_job_op_seq_num
17676                            and    wori.resource_seq_num  = l_resource_seq_num
17677                            and    wori.organization_id   = p_org_id;
17678 
17679                            delete from wip_operation_resource_usage woru
17680                            where  woru.wip_entity_id     = p_wip_entity_id
17681                            and    woru.operation_seq_num = l_job_op_seq_num
17682                            and    woru.resource_seq_num  = l_resource_seq_num
17683                            and    woru.organization_id   = p_org_id;
17684 
17685                            delete_setup_info( p_wip_entity_id     => p_wip_entity_id,
17686                                               p_org_id            => p_org_id,
17687                                               p_par_res_seq_num   => l_resource_seq_num,
17688                                               p_op_seq_num        => l_job_op_seq_num,
17689                                               p_handle_curr_op    => 'Y',
17690                                               x_error_code        => p_error_code,
17691                                               x_error_msg         => p_error_msg);
17692 
17693                            /* Need to verify if primary resources in a substitution group are completely deleted,
17694                               substitute resources in the same substitution group should also be deleted as they will
17695                               be orphaned records.*/
17696 
17697                            if p_error_code <> 0 then
17698                                raise e_wljdi_common_error;
17699                            end if;
17700 
17701                       else
17702                           fnd_file.put_line(fnd_file.log,
17703                           'Warning: Deleting or Adding resources for executed operations '||
17704                           'are currently not supported and ignored.');
17705                       end if;
17706                    elsif(l_rtg_op_seq_num IS NOT NULL) then  -- Future operation
17707 
17708                        -- pack data into PL/SQL table l_v_res for validation
17709                        l_v_res.delete;
17710                        l_v_res(l_resource_id).rtg_op_seq_num  := l_rtg_op_seq_num;
17711                        l_v_res(l_resource_id).resource_id     := l_resource_id;
17712                        l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
17713                        l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
17714                        l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
17715                        l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
17716                         /* ST : Detailed Scheduling start */
17717                        l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17718                        l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
17719                        l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
17720                        l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
17721                        l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
17722                        l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
17723                        l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
17724                        l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
17725                        l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
17726                        l_v_res(l_resource_id).applied_resource_units  := v_wljdi_applied_resource_units(l_d);
17727 
17728                        /* ST : Detailed Scheduling end */
17729 
17730                        validate_wljdi_op_resource(
17731                              p_wip_entity_id     => p_wip_entity_id,
17732                              p_org_id            => p_org_id,
17733                              p_v_res             => l_v_res,
17734                              p_handle_wip_cur_op => 'N',
17735                              p_substitution_type => l_sub_type,
17736                              x_error_code        => p_error_code,
17737                              x_error_msg         => p_error_msg );
17738 
17739                        if p_error_code <> 0 then
17740                          raise e_wljdi_common_error;
17741                        end if;
17742 
17743                        delete from wsm_copy_op_resource_usage wcoru
17744                        where  wcoru.wip_entity_id     = p_wip_entity_id
17745                        and    wcoru.operation_seq_num = l_rtg_op_seq_num
17746                        and    wcoru.resource_seq_num  = l_resource_seq_num
17747                        and    wcoru.organization_id   = p_org_id ;
17748 
17749                        delete from wsm_copy_op_resources wcor
17750                        where  wcor.wip_entity_id      = p_wip_entity_id
17751                        and    wcor.operation_seq_num  = l_rtg_op_seq_num
17752                        and    wcor.resource_seq_num   = l_resource_seq_num
17753                        and    wcor.organization_id    = p_org_id;
17754 
17755                        delete from wsm_copy_op_resource_instances wcori
17756                        where  wcori.wip_entity_id      = p_wip_entity_id
17757                        and    wcori.operation_seq_num  = l_rtg_op_seq_num
17758                        and    wcori.resource_seq_num   = l_resource_seq_num
17759                        and    wcori.organization_id    = p_org_id;
17760 
17761                        delete_setup_info( p_wip_entity_id     => p_wip_entity_id,
17762                                           p_org_id            => p_org_id,
17763                                           p_par_res_seq_num   => l_resource_seq_num,
17764                                           p_op_seq_num        => l_rtg_op_seq_num,
17765                                           p_handle_curr_op    => 'N',
17766                                           x_error_code        => p_error_code,
17767                                           x_error_msg         => p_error_msg);
17768 
17769                        if p_error_code <> 0 then
17770                            raise e_wljdi_common_error;
17771                        end if;
17772 
17773                    end if;
17774 
17775                end if;
17776  --Alcoa Enhancement Resource End : Delete
17777 
17778                --Alcoa Enhancement Resource Start : Add
17779                if l_sub_type  = (WSM_SUB_ADD) and v_wljdi_parent_seq_num(l_d) IS NULL THEN
17780 l_stmt_num := 201;
17781                    if (l_job_op_seq_num IS NOT NULL) then  -- Current Operation
17782                        l_v_res.delete;
17783                        if (l_job_op_seq_num = l_cur_job_op_seq_num) then
17784 
17785                            l_v_res(l_resource_id).job_op_seq_num  := l_job_op_seq_num;
17786                            l_v_res(l_resource_id).resource_id     := l_resource_id;
17787                            l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
17788                            l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
17789                            l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
17790                            l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
17791 
17792                             /* ST : Detailed Scheduling start */
17793                            l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17794                            l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
17795                            l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
17796                            l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
17797                            l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
17798                            l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
17799                            l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
17800                            l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
17801                            l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
17802                            l_v_res(l_resource_id).applied_resource_units  := v_wljdi_applied_resource_units(l_d);
17803                            l_v_res(l_resource_id).uom_code         := v_wljdi_uom_code(l_d);
17804                            l_v_res(l_resource_id).autocharge_type  := v_wljdi_res_autocharge_type(l_d);
17805                            l_v_res(l_resource_id).usage_rate_or_amount :=v_wljdi_usage_rate_or_amt(l_d);
17806                            l_v_res(l_resource_id).scheduled_flag       := v_wljdi_scheduled_flag(l_d);
17807                            l_v_res(l_resource_id).reco_flag            := v_wljdi_recommended(l_d);
17808                            l_v_res(l_resource_id).basis_type            := v_wljdi_basis_type(l_d);
17809                            l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
17810 
17811                            validate_wljdi_op_resource(
17812                                    p_wip_entity_id     => p_wip_entity_id,
17813                                    p_org_id            => p_org_id,
17814                                    p_v_res             => l_v_res,
17815                                    p_handle_wip_cur_op => 'Y',
17816                                    p_substitution_type => l_sub_type,
17817                                    x_error_code        => p_error_code,
17818                                    x_error_msg         => p_error_msg );
17819 
17820                            if p_error_code <> 0 then
17821                                raise e_wljdi_common_error;
17822                            end if;
17823 
17824                            IF l_v_res(l_resource_id).reco_flag = 'Y' then
17825 
17826                                insert into wip_operation_resources
17827                                (wip_entity_id,
17828                                 operation_seq_num,
17829                                 resource_seq_num,
17830                                 organization_id,
17831                                 department_id,
17832                                 resource_id,
17833                                 uom_code,
17834                                 basis_type,
17835                                 usage_rate_or_amount,
17836                                 activity_id,
17837                                 scheduled_flag,
17838                                 assigned_units,
17839                                 maximum_assigned_units,
17840                                 batch_id,
17841                                 firm_flag,
17842                                 group_sequence_id,
17843                                 group_sequence_number,
17844                                 parent_resource_seq,
17845                                 autocharge_type,
17846                                 standard_rate_flag,
17847                                 applied_resource_units,
17848                                 applied_resource_value,
17849                                 start_date,
17850                                 completion_date,
17851                                 attribute_category,
17852                                 attribute1,
17853                                 attribute2,
17854                                 attribute3,
17855                                 attribute4,
17856                                 attribute5,
17857                                 attribute6,
17858                                 attribute7,
17859                                 attribute8,
17860                                 attribute9,
17861                                 attribute10,
17862                                 attribute11,
17863                                 attribute12,
17864                                 attribute13,
17865                                 attribute14,
17866                                 attribute15,
17867                                 schedule_seq_num,
17868                                 substitute_group_num,
17869                                 replacement_group_num,
17870                                 setup_id,
17871                                 last_update_date,
17872                                 last_updated_by,
17873                                 creation_date,
17874                                 created_by,
17875                                 last_update_login,
17876                                 request_id,
17877                                 program_application_id,
17878                                 program_id,
17879                                 program_update_date
17880                                )
17881                                values
17882                                (p_wip_entity_id,
17883                                 l_job_op_seq_num,
17884                                 l_v_res(l_resource_id).res_seq_num,
17885                                 p_org_id,
17886                                 v_wljdi_department_id(l_d),
17887                                 v_wljdi_resource_id_new(l_d),
17888                                 l_v_res(l_resource_id).uom_code,
17889                                 l_v_res(l_resource_id).basis_type,
17890                                 v_wljdi_usage_rate_or_amt(l_d),
17891                                 l_v_res(l_resource_id).activity_id,
17892                                 l_v_res(l_resource_id).scheduled_flag,
17893                                 v_wljdi_assigned_units(l_d),
17894                                 l_v_res(l_resource_id).max_assg_units,
17895                                 v_wljdi_batch_id(l_d),
17896                                 l_v_res(l_resource_id).firm_type,
17897                                 v_wljdi_grp_seq_id(l_d),
17898                                 v_wljdi_grp_seq_num(l_d),
17899                                 null,
17900                                 l_v_res(l_resource_id).autocharge_type,
17901                                 l_v_res(l_resource_id).standard_rate,
17902                                 0,
17903                                 0,
17904                                 l_v_res(l_resource_id).start_date,
17905                                 l_v_res(l_resource_id).completion_date,
17906                                 v_wljdi_attribute_category(l_d),
17907                                 v_wljdi_attribute1(l_d),
17908                                 v_wljdi_attribute2(l_d),
17909                                 v_wljdi_attribute3(l_d),
17910                                 v_wljdi_attribute4(l_d),
17911                                 v_wljdi_attribute5(l_d),
17912                                 v_wljdi_attribute6(l_d),
17913                                 v_wljdi_attribute7(l_d),
17914                                 v_wljdi_attribute8(l_d),
17915                                 v_wljdi_attribute9(l_d),
17916                                 v_wljdi_attribute10(l_d),
17917                                 v_wljdi_attribute11(l_d),
17918                                 v_wljdi_attribute12(l_d),
17919                                 v_wljdi_attribute13(l_d),
17920                                 v_wljdi_attribute14(l_d),
17921                                 v_wljdi_attribute15(l_d),
17922                                 v_wljdi_schedule_seq_num(l_d),
17923                                 v_wljdi_substitute_group_num(l_d),
17924                                 l_v_res(l_resource_id).rpl_grp_num,
17925                                 l_v_res(l_resource_id).setup_id,
17926                                 p_last_updt_date,
17927                                 p_last_updt_by,
17928                                 p_creation_date,
17929                                 p_created_by,
17930                                 p_last_updt_login,
17931                                 p_request_id,
17932                                 p_program_appl_id,
17933                                 p_program_id,
17934                                 p_prog_updt_date);
17935 
17936                                 -- If resource is PO_RECIPT, we should create requisition.
17937                                 if l_v_res(l_resource_id).autocharge_type = WIP_CONSTANTS.PO_RECEIPT then
17938 
17939                                     wip_osp.create_requisition(
17940                                        p_wip_entity_id          => p_wip_entity_id,
17941                                        p_organization_id        => p_org_id,
17942                                        p_repetitive_schedule_id => null,
17943                                        p_operation_seq_num      => l_job_op_seq_num,
17944                                        p_resource_seq_num       => l_v_res(l_resource_id).res_seq_num,
17945                                        p_run_reqimport          => wip_constants.no);
17946 
17947                                     g_num_of_osp_exists := nvl(g_num_of_osp_exists,0) + 1;
17948 
17949                                 end if;
17950                            ELSE
17951                                insert into wip_sub_operation_resources
17952                                (wip_entity_id,
17953                                 operation_seq_num,
17954                                 resource_seq_num,
17955                                 organization_id,
17956                                 department_id,
17957                                 last_update_date,
17958                                 last_updated_by,
17959                                 creation_date,
17960                                 created_by,
17961                                 last_update_login,
17962                                 resource_id,
17963                                 uom_code,
17964                                 basis_type,
17965                                 usage_rate_or_amount,
17966                                 activity_id,
17967                                 scheduled_flag,
17968                                 assigned_units,
17969                                 maximum_assigned_units,
17970                                 autocharge_type,
17971                                 standard_rate_flag,
17972                                 applied_resource_units,
17973                                 applied_resource_value,
17974                                 attribute_category,
17975                                 attribute1,
17976                                 attribute2,
17977                                 attribute3,
17978                                 attribute4,
17979                                 attribute5,
17980                                 attribute6,
17981                                 attribute7,
17982                                 attribute8,
17983                                 attribute9,
17984                                 attribute10,
17985                                 attribute11,
17986                                 attribute12,
17987                                 attribute13,
17988                                 attribute14,
17989                                 attribute15,
17990                                 completion_date,
17991                                 start_date,
17992                                 schedule_seq_num,
17993                                 substitute_group_num,
17994                                 replacement_group_num,
17995                                 setup_id,
17996                                 request_id,
17997                                 program_application_id,
17998                                 program_id,
17999                                 program_update_date
18000                                 )
18001                                values
18002                                (p_wip_entity_id,
18003                                 l_job_op_seq_num,
18004                                 l_v_res(l_resource_id).res_seq_num,
18005                                 p_org_id,
18006                                 v_wljdi_department_id(l_d),
18007                                 p_last_updt_date,
18008                                 p_last_updt_by,
18009                                 p_creation_date,
18010                                 p_created_by,
18011                                 p_last_updt_login,
18012                                 v_wljdi_resource_id_new(l_d),
18013                                 l_v_res(l_resource_id).uom_code,
18014                                 l_v_res(l_resource_id).basis_type,
18015                                 v_wljdi_usage_rate_or_amt(l_d),
18016                                 l_v_res(l_resource_id).activity_id,
18017                                 l_v_res(l_resource_id).scheduled_flag,
18018                                 v_wljdi_assigned_units(l_d),
18019                                 l_v_res(l_resource_id).max_assg_units,
18020                                 l_v_res(l_resource_id).autocharge_type,
18021                                 l_v_res(l_resource_id).standard_rate,
18022                                 0,
18023                                 0,
18024                                 v_wljdi_attribute_category(l_d),
18025                                 v_wljdi_attribute1(l_d),
18026                                 v_wljdi_attribute2(l_d),
18027                                 v_wljdi_attribute3(l_d),
18028                                 v_wljdi_attribute4(l_d),
18029                                 v_wljdi_attribute5(l_d),
18030                                 v_wljdi_attribute6(l_d),
18031                                 v_wljdi_attribute7(l_d),
18032                                 v_wljdi_attribute8(l_d),
18033                                 v_wljdi_attribute9(l_d),
18034                                 v_wljdi_attribute10(l_d),
18035                                 v_wljdi_attribute11(l_d),
18036                                 v_wljdi_attribute12(l_d),
18037                                 v_wljdi_attribute13(l_d),
18038                                 v_wljdi_attribute14(l_d),
18039                                 v_wljdi_attribute15(l_d),
18040                                 l_v_res(l_resource_id).start_date,
18041                                 l_v_res(l_resource_id).completion_date,
18042                                 v_wljdi_schedule_seq_num(l_d),
18043                                 v_wljdi_substitute_group_num(l_d),
18044                                 l_v_res(l_resource_id).rpl_grp_num,
18045                                 l_v_res(l_resource_id).setup_id,
18046                                 p_request_id,
18047                                 p_program_appl_id,
18048                                 p_program_id,
18049                                 p_prog_updt_date);
18050                           END IF;
18051 
18052                           if l_v_res(l_resource_id).scheduled_flag <> WIP_CONSTANTS.SCHED_NO and
18053                              l_v_res(l_resource_id).reco_flag = 'Y' then
18054 
18055                               insert into wip_operation_resource_usage
18056                               (wip_entity_id,
18057                                operation_seq_num,
18058                                resource_seq_num,
18059                                organization_id,
18060                                start_date,
18061                                completion_date,
18062                                assigned_units,
18063                                last_update_date,
18064                                last_updated_by,
18065                                creation_date,
18066                                created_by,
18067                                last_update_login,
18068                                request_id,
18069                                program_application_id,
18070                                program_id,
18071                                program_update_date,
18072                                cumulative_processing_time)
18073                               values
18074                               (p_wip_entity_id,
18075                                l_job_op_seq_num,
18076                                l_v_res(l_resource_id).res_seq_num,
18077                                p_org_id,
18078                                l_v_res(l_resource_id).start_date,
18079                                l_v_res(l_resource_id).completion_date,
18080                                v_wljdi_assigned_units(l_d),
18081                                p_last_updt_date,
18082                                p_last_updt_by,
18083                                p_creation_date,
18084                                p_created_by,
18085                                p_last_updt_login,
18086                                p_request_id,
18087                                p_program_appl_id,
18088                                p_program_id,
18089                                p_prog_updt_date,
18090                                to_number(round((l_v_res(l_resource_id).completion_date - l_v_res(l_resource_id).start_date)*1440)));
18091                            end if;
18092                       else
18093                           fnd_file.put_line(fnd_file.log,
18094                                  'Warning: Deleting or Adding resources for executed operations '||
18095                                  'is currently not supported and ignored.');
18096                       END if;
18097                   ELSIF(l_rtg_op_seq_num IS NOT NULL) THEN
18098 
18099                       l_v_res.delete;
18100                       l_v_res(l_resource_id).rtg_op_seq_num  := l_rtg_op_seq_num;
18101                       l_v_res(l_resource_id).resource_id     := l_resource_id;
18102                       l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
18103                       l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
18104                       l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
18105                       l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
18106 
18107                        /* ST : Detailed Scheduling start */
18108                       l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
18109                       l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
18110                       l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
18111                       l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
18112                       l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
18113                       l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
18114                       l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
18115                       l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
18116                       l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
18117                       l_v_res(l_resource_id).applied_resource_units  := v_wljdi_applied_resource_units(l_d);
18118                       l_v_res(l_resource_id).uom_code         := v_wljdi_uom_code(l_d);
18119                       l_v_res(l_resource_id).autocharge_type  := v_wljdi_res_autocharge_type(l_d);
18120                       l_v_res(l_resource_id).usage_rate_or_amount :=v_wljdi_usage_rate_or_amt(l_d);
18121                       l_v_res(l_resource_id).scheduled_flag := v_wljdi_scheduled_flag(l_d);
18122                       l_v_res(l_resource_id).reco_flag      := v_wljdi_recommended(l_d);
18123                       l_v_res(l_resource_id).resource_code  := v_wljdi_resource_code_new(l_d);
18124                       l_v_res(l_resource_id).basis_type     := v_wljdi_basis_type(l_d);
18125                       l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
18126 
18127                       validate_wljdi_op_resource(
18128                               p_wip_entity_id     => p_wip_entity_id,
18129                               p_org_id            => p_org_id,
18130                               p_v_res             => l_v_res,
18131                               p_handle_wip_cur_op => 'N',
18132                               p_substitution_type => l_sub_type,
18133                               x_error_code        => p_error_code,
18134                               x_error_msg         => p_error_msg );
18135 
18136                       if p_error_code <> 0 then
18137                           raise e_wljdi_common_error;
18138                       end if;
18139 
18140                       INSERT INTO WSM_COPY_OP_RESOURCES
18141                       (wip_entity_id,
18142                        operation_seq_num,
18143                        resource_seq_num,
18144                        organization_id,
18145                        substitute_group_num,
18146                        replacement_group_num,
18147                        recommended,
18148                        reco_start_date,
18149                        reco_completion_date,
18150                        resource_id,
18151                        resource_code,
18152                        department_id,
18153                        phantom_flag,
18154                        phantom_op_seq_num,
18155                        phantom_item_id,
18156                        activity_id,
18157                        standard_rate_flag,
18158                        assigned_units,
18159                        max_assigned_units,
18160                        firm_type,
18161                        usage_rate_or_amount,
18162                        usage_rate_or_amount_inverse,
18163                        uom_code,
18164                        basis_type,
18165                        schedule_flag,
18166                        resource_offset_percent,
18167                        autocharge_type,
18168                        schedule_seq_num,
18169                        principle_flag,
18170                        setup_id,
18171                        change_notice,
18172                        acd_type,
18173                        last_update_date,
18174                        last_updated_by,
18175                        last_update_login,
18176                        creation_date,
18177                        created_by,
18178                        request_id,
18179                        program_application_id,
18180                        program_id,
18181                        program_update_date,
18182                        attribute_category,
18183                        attribute1,
18184                        attribute2,
18185                        attribute3,
18186                        attribute4,
18187                        attribute5,
18188                        attribute6,
18189                        attribute7,
18190                        attribute8,
18191                        attribute9,
18192                        attribute10,
18193                        attribute11,
18194                        attribute12,
18195                        attribute13,
18196                        attribute14,
18197                        attribute15,
18198                        original_system_reference
18199                        )
18200                        values
18201                        (p_wip_entity_id,
18202                         l_rtg_op_seq_num,
18203                         l_v_res(l_resource_id).res_seq_num,
18204                         p_org_id,
18205                         v_wljdi_substitute_group_num(l_d),
18206                         nvl(v_wljdi_replacement_group_num(l_d),0),
18207                         l_v_res(l_resource_id).reco_flag,
18208                         l_v_res(l_resource_id).start_date,
18209                         l_v_res(l_resource_id).completion_date,
18210                         v_wljdi_resource_id_new(l_d),
18211                         l_v_res(l_resource_id).resource_code,
18212                         v_wljdi_department_id(l_d),
18213                         null,
18214                         null,
18215                         null,
18216                         l_v_res(l_resource_id).activity_id,
18217                         l_v_res(l_resource_id).standard_rate,
18218                         v_wljdi_assigned_units(l_d),
18219                         l_v_res(l_resource_id).max_assg_units,
18220                         l_v_res(l_resource_id).firm_type,
18221                         v_wljdi_usage_rate_or_amt(l_d),
18222                         1/v_wljdi_usage_rate_or_amt(l_d),
18223                         l_v_res(l_resource_id).uom_code,
18224                         l_v_res(l_resource_id).basis_type,
18225                         l_v_res(l_resource_id).scheduled_flag,
18226                         null,
18227                         l_v_res(l_resource_id).autocharge_type,
18228                         v_wljdi_schedule_seq_num(l_d),
18229                         null,
18230                         l_v_res(l_resource_id).setup_id,
18231                         null,
18232                         null,
18233                         p_last_updt_date,
18234                         p_last_updt_by,
18235                         p_last_updt_login,
18236                         p_creation_date,
18237                         p_created_by,
18238                         p_request_id,
18239                         p_program_appl_id,
18240                         p_program_id,
18241                         p_prog_updt_date,
18242                         v_wljdi_attribute_category(l_d),
18243                         v_wljdi_attribute1(l_d),
18244                         v_wljdi_attribute2(l_d),
18245                         v_wljdi_attribute3(l_d),
18246                         v_wljdi_attribute4(l_d),
18247                         v_wljdi_attribute5(l_d),
18248                         v_wljdi_attribute6(l_d),
18249                         v_wljdi_attribute7(l_d),
18250                         v_wljdi_attribute8(l_d),
18251                         v_wljdi_attribute9(l_d),
18252                         v_wljdi_attribute10(l_d),
18253                         v_wljdi_attribute11(l_d),
18254                         v_wljdi_attribute12(l_d),
18255                         v_wljdi_attribute13(l_d),
18256                         v_wljdi_attribute14(l_d),
18257                         v_wljdi_attribute15(l_d),
18258                         null);
18259 
18260                         if l_v_res(l_resource_id).reco_flag='Y' and
18261                            l_v_res(l_resource_id).scheduled_flag <> WIP_CONSTANTS.SCHED_NO then
18262 
18263                             insert into wsm_copy_op_resource_usage
18264                             (wip_entity_id,
18265                              operation_seq_num,
18266                              resource_seq_num,
18267                              organization_id,
18268                              start_date,
18269                              completion_date,
18270                              assigned_units,
18271                              cumulative_processing_time,
18272                              last_update_date,
18273                              last_updated_by,
18274                              creation_date,
18275                              created_by,
18276                              last_update_login,
18277                              request_id,
18278                              program_application_id,
18279                              program_id,
18280                              program_update_date)
18281                             values
18282                             (p_wip_entity_id,
18283                             l_rtg_op_seq_num,
18284                             l_v_res(l_resource_id).res_seq_num,
18285                             p_org_id,
18286                             l_v_res(l_resource_id).start_date,
18287                             l_v_res(l_resource_id).completion_date,
18288                             v_wljdi_assigned_units(l_d),
18289                             to_number(round((l_v_res(l_resource_id).completion_date - l_v_res(l_resource_id).start_date)*1440)),
18290                             p_last_updt_date,
18291                             p_last_updt_by,
18292                             p_creation_date,
18293                             p_created_by,
18294                             p_last_updt_login,
18295                             p_request_id,
18296                             p_program_appl_id,
18297                             p_program_id,
18298                             p_prog_updt_date);
18299 
18300                        end if;
18301 
18302                    END if; --l_job_op_seq_num not null
18303                END if; --wsm_sub_add
18304                --Alcoa Enhancement Resource End : Add
18305 
18306             -- can only a setup resource..
18307             if l_sub_type  = (WSM_SUB_ADD) and v_wljdi_parent_seq_num(l_d) IS NOT NULL then
18308 
18309                 /* 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
18310                 its setup resource
18311                 ... */
18312 
18313                 l_stmt_num := 202;
18314 
18315                 p_error_code := 0;
18316                 p_error_msg  := null;
18317 
18318                 l_temp_par_res_seq := v_wljdi_parent_seq_num(l_d);
18319 
18320                 add_resource (p_wip_entity_id       => p_wip_entity_id,
18321                               p_org_id              => p_org_id,
18322                               p_job_op_seq_num      => l_job_op_seq_num,
18323                               p_cur_job_op_seq_num  => l_cur_job_op_seq_num,
18324                               p_rtg_op_seq_num      => l_rtg_op_seq_num,
18325                               p_load_type           => l_load_type,
18326                               p_sub_type            => l_sub_type,
18327                               p_res_recom           => l_v_res_recom,
18328                               p_last_updt_date      => p_last_updt_date,
18329                               p_last_updt_by        => p_last_updt_by,
18330                               p_creation_date       => p_creation_date,
18331                               p_created_by          => p_created_by,
18332                               p_last_updt_login     => p_last_updt_login,
18333                               p_request_id          => p_request_id,
18334                               p_program_appl_id     => p_program_appl_id,
18335                               p_program_id          => p_program_id,
18336                               p_prog_updt_date      => p_prog_updt_date,
18337                               p_global_index        => l_d,
18338                               x_error_code          => p_error_code,
18339                               x_error_msg           => p_error_msg
18340                              );
18341 
18342                 l_stmt_num := 203;
18343 
18344                 if p_error_code <> 0 then
18345                         raise e_wljdi_common_error;
18346                 end if;
18347 
18348                 -- ST : Detailed Scheduling ...
18349                 -- If setup resources are added for a resource, keep track of it..
18350                 IF l_job_op_seq_num IS NOT NULL THEN
18351 
18352                         -- Added to WIP Operations...
18353                         fnd_file.put_line(fnd_file.log,'Adding : ' || 'Y'||'-'||l_job_op_seq_num||'-'||l_temp_par_res_seq);
18354                         l_v_setup_op_res_num('Y'||'-'||l_job_op_seq_num||'-'||l_temp_par_res_seq) := 'Y';
18355 
18356                 ELSIF l_rtg_op_seq_num IS NOT NULL THEN
18357 
18358                         fnd_file.put_line(fnd_file.log,'Adding : ' || 'N'||'-'||l_rtg_op_seq_num||'-'||l_temp_par_res_seq);
18359                         l_v_setup_op_res_num('N'||'-'||l_rtg_op_seq_num||'-'||l_temp_par_res_seq) := 'Y';
18360 
18361                 END IF;
18362 
18363                 l_stmt_num := 204;
18364 --        Modified for Alcoa Enhancement
18365 --        elsif l_sub_type  = (WSM_SUB_ADD) and v_wljdi_parent_seq_num(l_d) IS NULL then
18366 --                fnd_file.put_line(fnd_file.log,
18367 --                                'Warning: Add a runtime resources is currently not supported and ignored.' -- CZHDBG_MSG
18368 --                                 );
18369 --
18370 --                l_d := v_wljdi_record_id.next(l_d);
18371 
18372         elsif l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC) then
18373                 -- {{ Update WIP table
18374                 if(l_job_op_seq_num IS NOT NULL) then
18375 
18376                     if(l_job_op_seq_num = l_cur_job_op_seq_num) then
18377 l_stmt_num := 250.1;
18378                         -- pack data into PL/SQL table l_v_res for validation
18379                         l_v_res.delete;
18380                         l_v_res(l_resource_id).job_op_seq_num  := l_job_op_seq_num;
18381                         l_v_res(l_resource_id).resource_id     := l_resource_id;
18382                         l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
18383                         l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
18384                         l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
18385                         l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
18386 
18387                         /* ST : Detailed Scheduling start */
18388                         l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
18389                         l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
18390                         l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
18391                         l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
18392                         l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
18393                         l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
18394                         l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
18395                         l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
18396                         l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
18397                         /* ST : Detailed Scheduling end */
18398 
18399                         if(l_sub_type = WSM_SUB_REC) then
18400                             -- get all the resource in the same rpl grp
18401                             -- this local search will change index l_d
18402                             l_temp := v_wljdi_record_id.next(l_d);
18403                             while(l_temp IS NOT NULL and
18404                                   v_wljdi_load_type(l_temp)             = l_load_type and
18405                                   v_wljdi_substitution_type(l_temp)     = l_sub_type and
18406                                   v_wljdi_job_op_seq_num(l_temp)        = l_job_op_seq_num and
18407                                   nvl(v_wljdi_substitute_group_num(l_temp), -1) = nvl(l_sub_grp_num, -1) and
18408                                   nvl(v_wljdi_replacement_group_num(l_temp), 0) = nvl(l_rpl_grp_num, 0) )
18409                             loop
18410                                 l_d := l_temp;
18411                                 l_resource_id := v_wljdi_resource_id_new(l_d);
18412                                 l_v_res(l_resource_id).job_op_seq_num  := l_job_op_seq_num;
18413                                 l_v_res(l_resource_id).resource_id     := l_resource_id;
18414                                 l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
18415                                 l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
18416                                 l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
18417                                 l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
18418 
18419                                 /* ST : Detailed Scheduling start */
18420                                 l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
18421                                 l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
18422                                 l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
18423                                 l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
18424                                 l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
18425                                 l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
18426                                 l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
18427                                 l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
18428                                 l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
18429                                 /* ST : Detailed Scheduling end */
18430 
18431                                 l_temp := v_wljdi_record_id.next(l_d);
18432                             end loop;
18433                         end if;
18434 
18435                         l_stmt_num := 250.2;
18436 
18437                         validate_wljdi_op_resource( p_wip_entity_id     => p_wip_entity_id,
18438                                                     p_org_id            => p_org_id,
18439                                                     p_v_res             => l_v_res,
18440                                                     p_handle_wip_cur_op => 'Y',
18441                                                     p_substitution_type => l_sub_type,
18442                                                     x_error_code        => p_error_code,
18443                                                     x_error_msg         => p_error_msg );
18444                         if p_error_code <> 0 then
18445                             raise e_wljdi_common_error;
18446                         end if;
18447 
18448 
18449                         -- Delete only for resources for which setup resources are not added..
18450                         -- Delete the all the setup resources of the resources which are being updated...
18451                         l_index := l_v_res.first;
18452                         while l_index is not null loop
18453                                 IF not(l_v_setup_op_res_num.exists('Y'||'-'||l_job_op_seq_num||'-'||l_v_res(l_index).res_seq_num)) THEN
18454                                         p_error_code := 0;
18455                                         p_error_msg  := null;
18456                                         IF lbji_debug = 'Y' THEN
18457                                                 fnd_file.put_line(fnd_file.log,'import job details : before calling delete setup resource '
18458                                                                 || ' parent res.... : ' || l_v_res(l_index).res_seq_num );
18459                                         END IF;
18460 
18461                                         -- ST : Detailed Scheduling... should we remove the resources' setup resources that are being replaced....
18462 
18463                                         delete_setup_info( p_wip_entity_id             => p_wip_entity_id,
18464                                                            p_org_id                    => p_org_id,
18465                                                            p_par_res_seq_num           => l_v_res(l_index).res_seq_num,
18466                                                            p_op_seq_num                => l_job_op_seq_num,
18467                                                            p_handle_curr_op            => 'Y',
18468                                                            x_error_code                => p_error_code,
18469                                                            x_error_msg                 => p_error_msg
18470                                                          );
18471 
18472                                          if p_error_code <> 0 then
18473                                                 -- error out...
18474                                                 raise e_wljdi_common_error;
18475                                          end if;
18476                                 END IF;
18477                                 l_index := l_v_res.next(l_index);
18478                         end loop;
18479 
18480                         if(l_sub_type = WSM_SUB_REC) then
18481                             -- swap the records in WOR and WSOR
18482                             -- This wip procdure will take care of applied_resource_units
18483                             -- and WORU/WORI records
18484 
18485                             -- BA: bug 3537390 should check if we need to call REPLACE_RESOURCES
18486                             l_swap := 0;
18487                             begin
18488                                 l_stmt_num := 250.3;
18489                                 select   0
18490                                 into     l_swap
18491                                 from     wip_operation_resources
18492                                 where    wip_entity_id         = p_wip_entity_id
18493                                 and      operation_seq_num     = l_job_op_seq_num
18494                                 and      NVL(substitute_group_num, -1) = NVL(l_sub_grp_num, -1)
18495                                 and      NVL(replacement_group_num, 0) = NVL(l_rpl_grp_num, 0);
18496                             exception
18497                                 when no_data_found then
18498                                     l_swap := 1;
18499                                 when too_many_rows then
18500                                     l_swap := 0;
18501                             end;
18502                             -- EA: bug 3537390
18503 
18504                             if(l_swap = 1) then         -- bug 3537390
18505                                 begin
18506                                     l_stmt_num := 250.4;
18507                                     select  1
18508                                     into    l_exists
18509                                     from    wip_cost_txn_interface
18510                                     where   wip_entity_id = p_wip_entity_id
18511                                     and     transaction_type = 1
18512                                     and     operation_seq_num = l_job_op_seq_num
18513                                     and     organization_id = p_org_id;
18514                                 exception
18515                                     when no_data_found then
18516                                         l_exists := 0;
18517                                     when too_many_rows then
18518                                         l_exists := 1;
18519                                 end;
18520                                 if(l_exists = 1) then
18521                                     fnd_message.set_name(application => 'WIP',
18522                                                          name        => 'WIP_REPLACE_APPLIED_RES');
18523                                     p_error_msg := fnd_message.get;
18524                                     p_error_code := -1;
18525                                     raise e_wljdi_common_error;
18526                                 end if;
18527                                 l_stmt_num := 250.5;
18528 
18529                                 WIP_SUB_OP_RESOURCES_PKG.REPLACE_RESOURCES(
18530                                     l_wip_entity_id           => p_wip_entity_id,
18531                                     l_operation_seq_num       => l_job_op_seq_num,
18532                                     l_substitute_group_num    => l_sub_grp_num,
18533                                     l_replacement_group_num   => l_rpl_grp_num,
18534                                     x_status                  => l_status,
18535                                     x_msg_count               => l_msg_count,
18536                                     x_msg_data                => p_error_msg);
18537 
18538                                 if(l_status <> fnd_api.g_ret_sts_success) then
18539                                     p_error_code := -1;
18540                                     p_error_msg := 'Error: WIP_SUB_OP_RESOURCES_PKG.REPLACE_RESOURCES ' || p_error_msg;
18541                                     raise e_wljdi_common_error;
18542                                 end if;
18543 
18544                             end if; -- l_swap = 1 (bug 3537390)
18545                         end if;
18546 
18547 
18548                         if(l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC)) then
18549                             -- update the records in WOR or WSOR
18550                                 l_resource_id := l_v_res.first;
18551                                 l_stmt_num := 250.6;
18552                                 while(l_resource_id IS NOT NULL)    -- bug 3736602 added loop
18553                                 loop
18554 
18555                                         UPDATE WIP_OPERATION_RESOURCES
18556                                         set     start_date              = NVL(l_v_res(l_resource_id).start_date,
18557                                                                                 start_date),
18558                                                 completion_date         = NVL(l_v_res(l_resource_id).completion_date,
18559                                                                                 completion_date),
18560 
18561                                                 /* ST : Detailed Scheduling start */
18562                                                 firm_flag               =  NVL(l_v_res(l_resource_id).firm_type,firm_flag),
18563                                                 setup_id                =  NVL(l_v_res(l_resource_id).setup_id,setup_id),
18564                                                 group_sequence_id       =  NVL(l_v_res(l_resource_id).grp_seq_id, group_sequence_id),
18565                                                 group_sequence_number   =  NVL(l_v_res(l_resource_id).grp_seq_num, group_sequence_number),
18566                                                 assigned_units          =  NVL(l_v_res(l_resource_id).assigned_units,assigned_units),
18567                                                 maximum_assigned_units  =  NVL(l_v_res(l_resource_id).max_assg_units,maximum_assigned_units),
18568                                                 --parent_resource_seq   =  NVL(l_v_res(l_resource_id).parent_seq_num,parent_resource_seq),
18569                                                 batch_id                =  NVL(l_v_res(l_resource_id).batch_id,batch_id),
18570                                                 /* ST : Detailed Scheduling end */
18571                                                 last_update_date        = p_last_updt_date,
18572                                                 last_updated_by         = p_last_updt_by,
18573                                                 last_update_login       = p_last_updt_login,
18574                                                 creation_date           = p_creation_date,
18575                                                 created_by              = p_created_by,
18576                                                 request_id              = p_request_id,
18577                                                 program_application_id  = p_program_appl_id,
18578                                                 program_id              = p_program_id,
18579                                                 program_update_date     = p_prog_updt_date,
18580                                                 attribute_category = v_wljdi_attribute_category(l_d),
18581                                                 attribute1 = v_wljdi_attribute1(l_d) ,
18582                                                 attribute2 = v_wljdi_attribute2(l_d) ,
18583                                                 attribute3 = v_wljdi_attribute3(l_d) ,
18584                                                 attribute4 = v_wljdi_attribute4(l_d) ,
18585                                                 attribute5 = v_wljdi_attribute5(l_d),
18586                                                 attribute6 = v_wljdi_attribute6(l_d) ,
18587                                                 attribute7 = v_wljdi_attribute7(l_d) ,
18588                                                 attribute8 = v_wljdi_attribute8(l_d),
18589                                                 attribute9 = v_wljdi_attribute9(l_d) ,
18590                                                 attribute10 = v_wljdi_attribute10(l_d),
18591                                                 attribute11 = v_wljdi_attribute11(l_d),
18592                                                 attribute12 = v_wljdi_attribute12(l_d),
18593                                                 attribute13 = v_wljdi_attribute13(l_d),
18594                                                 attribute14 = v_wljdi_attribute14(l_d),
18595                                                 attribute15 = v_wljdi_attribute15(l_d)
18596                                             where   wip_entity_id         = p_wip_entity_id
18597                                             and     organization_id       = p_org_id
18598                                             and     operation_seq_num     = l_job_op_seq_num
18599                                             and     resource_id           = l_resource_id
18600                                             -- ST : Detailed Scheduling .. use resource seq num when possible...
18601                                             and     resource_seq_num      = nvl(l_v_res(l_resource_id).res_seq_num,resource_seq_num)
18602                                             -- ST : Detailed Scheduling end...
18603                                             and     nvl(substitute_group_num, -1) = nvl(l_sub_grp_num, -1)
18604                                             and     nvl(replacement_group_num, 0) = nvl(l_rpl_grp_num, 0);
18605 
18606                                             l_resource_id := l_v_res.next(l_resource_id);
18607                                 end loop;
18608                                 -- bug 3736602 added loop
18609                                 l_stmt_num := 250.7;
18610                                 if(SQL%ROWCOUNT = 0) then
18611                                         -- change should be made in WSOR
18612                                         l_resource_id := l_v_res.first;
18613                                         while(l_resource_id IS NOT NULL)    -- bug 3736602 added loop
18614                                         loop
18615 
18616                                                 UPDATE WIP_SUB_OPERATION_RESOURCES
18617                                                 set     start_date              = NVL(l_v_res(l_resource_id).start_date,
18618                                                                                       start_date),
18619                                                         completion_date         = NVL(l_v_res(l_resource_id).completion_date,
18620                                                                                       completion_date),
18621                                                         /* ST : Detailed Scheduling start */
18622                                                         assigned_units          = NVL(l_v_res(l_resource_id).assigned_units,assigned_units),
18623                                                         maximum_assigned_units  = NVL(l_v_res(l_resource_id).max_assg_units,maximum_assigned_units),
18624                                                         /* ST : Detailed Scheduling end */
18625                                                         last_update_date        = p_last_updt_date,
18626                                                         last_updated_by         = p_last_updt_by,
18627                                                         last_update_login       = p_last_updt_login,
18628                                                         creation_date           = p_creation_date,
18629                                                         created_by              = p_created_by,
18630                                                         request_id              = p_request_id,
18631                                                         program_application_id  = p_program_appl_id,
18632                                                         program_id              = p_program_id,
18633                                                         program_update_date     = p_prog_updt_date,
18634                                                         attribute_category = v_wljdi_attribute_category(l_d),
18635                                                         attribute1 = v_wljdi_attribute1(l_d) ,
18636                                                         attribute2 = v_wljdi_attribute2(l_d) ,
18637                                                         attribute3 = v_wljdi_attribute3(l_d) ,
18638                                                         attribute4 = v_wljdi_attribute4(l_d) ,
18639                                                         attribute5 = v_wljdi_attribute5(l_d),
18640                                                         attribute6 = v_wljdi_attribute6(l_d) ,
18641                                                         attribute7 = v_wljdi_attribute7(l_d) ,
18642                                                         attribute8 = v_wljdi_attribute8(l_d),
18643                                                         attribute9 = v_wljdi_attribute9(l_d) ,
18644                                                         attribute10 = v_wljdi_attribute10(l_d),
18645                                                         attribute11 = v_wljdi_attribute11(l_d),
18646                                                         attribute12 = v_wljdi_attribute12(l_d),
18647                                                         attribute13 = v_wljdi_attribute13(l_d),
18648                                                         attribute14 = v_wljdi_attribute14(l_d),
18649                                                         attribute15 = v_wljdi_attribute15(l_d)
18650                                                 where   wip_entity_id         = p_wip_entity_id
18651                                                 and     organization_id       = p_org_id
18652                                                 and     operation_seq_num     = l_job_op_seq_num
18653                                                 and     resource_id           = l_resource_id
18654                                                 -- ST : Detailed Scheduling .. use resource seq num when possible...
18655                                                 and     resource_seq_num      = nvl(l_v_res(l_resource_id).res_seq_num,resource_seq_num)
18656                                                 -- ST : Detailed Scheduling end...
18657                                                 and     nvl(substitute_group_num, -1) = nvl(l_sub_grp_num, -1)
18658                                                 and     nvl(replacement_group_num, 0) = nvl(l_rpl_grp_num, 0);
18659 
18660                                                 l_resource_id := l_v_res.next(l_resource_id);
18661                                         end loop;                       -- bug 3736602 added loop
18662                                 end if;
18663                         end if;
18664 
18665                     else    -- l_job_op_seq_num <> l_cur_job_op_seq_num
18666                         -- this can happen when recommendations arrive after the job
18667                         -- is moved. should give a warning message
18668                         fnd_file.put_line(fnd_file.log,
18669                             'Warning: resource recommendations for current operation are ignored ' ||
18670                             '(record_id = ' || l_d || ')' );
18671                     end if;
18672                     -- }} Update WIP table
18673 
18674                 -- {{ Update WSM_COPY table
18675                 elsif(l_rtg_op_seq_num IS NOT NULL) then
18676 l_stmt_num := 260.1;
18677                     fnd_file.put_line(fnd_file.log,'import job details : Resource Updation...' || ' Rtg Op seq Num : ' || l_rtg_op_seq_num);
18678 
18679                     -- pack data into PL/SQL table l_v_res for validation
18680                     l_v_res.delete;
18681                     l_v_res(l_resource_id).rtg_op_seq_num  := l_rtg_op_seq_num;
18682                     l_v_res(l_resource_id).resource_id     := l_resource_id;
18683                     l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
18684                     l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
18685                     l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
18686                     l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
18687 
18688                     /* ST : Detailed Scheduling start */
18689                     l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
18690                     l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
18691                     l_v_res(l_resource_id).setup_id        := v_wljdi_setup_id(l_d);
18692                     l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
18693                     l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
18694                     l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
18695                     l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
18696                     l_v_res(l_resource_id).batch_id        := v_wljdi_batch_id(l_d);
18697                     l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
18698                     /* ST : Detailed Scheduling end */
18699 
18700                     if(l_sub_type = WSM_SUB_REC) then
18701                         -- get all the resource in the same rpl grp
18702                         -- this local search will change index l_d
18703                         l_temp := v_wljdi_record_id.next(l_d);
18704                         while(l_temp IS NOT NULL and
18705                               v_wljdi_load_type(l_temp)             = l_load_type and
18706                               v_wljdi_substitution_type(l_temp)     = l_sub_type and
18707                               v_wljdi_routing_op_seq_num(l_temp)    = l_rtg_op_seq_num and
18708                               nvl(v_wljdi_substitute_group_num(l_temp), -1) = nvl(l_sub_grp_num, -1) and
18709                               nvl(v_wljdi_replacement_group_num(l_temp), 0) = nvl(l_rpl_grp_num, 0) )
18710                         loop
18711                             l_d := l_temp;
18712                             l_resource_id := v_wljdi_resource_id_new(l_d);
18713                             l_v_res(l_resource_id).rtg_op_seq_num  := l_rtg_op_seq_num;
18714                             l_v_res(l_resource_id).resource_id     := l_resource_id;
18715                             l_v_res(l_resource_id).sub_grp_num     := l_sub_grp_num;
18716                             l_v_res(l_resource_id).rpl_grp_num     := l_rpl_grp_num;
18717                             l_v_res(l_resource_id).start_date      := v_wljdi_start_date(l_d);
18718                             l_v_res(l_resource_id).completion_date := v_wljdi_completion_date(l_d);
18719 
18720                             /* ST : Detailed Scheduling start */
18721                             l_v_res(l_resource_id).res_seq_num     := v_wljdi_resource_seq_num(l_d);
18722                             l_v_res(l_resource_id).firm_type       := v_wljdi_firm_type(l_d);
18723                             l_v_res(l_resource_id).setup_id    := v_wljdi_setup_id(l_d);
18724                             l_v_res(l_resource_id).grp_seq_id      := v_wljdi_grp_seq_id(l_d);
18725                             l_v_res(l_resource_id).grp_seq_num     := v_wljdi_grp_seq_num(l_d);
18726                             l_v_res(l_resource_id).max_assg_units  := v_wljdi_max_assg_units(l_d);
18727                             l_v_res(l_resource_id).parent_seq_num  := v_wljdi_parent_seq_num(l_d);
18728                             l_v_res(l_resource_id).batch_id    := v_wljdi_batch_id(l_d);
18729                             l_v_res(l_resource_id).assigned_units  := v_wljdi_assigned_units(l_d);
18730                             /* ST : Detailed Scheduling end */
18731 
18732                             IF lbji_debug = 'Y' THEN
18733                                     fnd_file.put_line(fnd_file.log,'import job details : Recomm. the resource..' || ' Rtg Op seq Num : ' || l_rtg_op_seq_num
18734                                             || ' Res ID .... : ' || l_resource_id
18735                                             || ' date : ' || v_wljdi_start_date(l_d) );
18736                             END IF;
18737 
18738                             l_temp := v_wljdi_record_id.next(l_d);
18739                         end loop;
18740                     end if;
18741 
18742                     l_stmt_num := 260.2;
18743 
18744                     validate_wljdi_op_resource(
18745                             p_wip_entity_id     => p_wip_entity_id,
18746                             p_org_id            => p_org_id,
18747                             p_v_res             => l_v_res,
18748                             p_handle_wip_cur_op => 'N',
18749                             p_substitution_type => l_sub_type,
18750                             x_error_code        => p_error_code,
18751                             x_error_msg         => p_error_msg );
18752 
18753                     if p_error_code <> 0 then
18754                         raise e_wljdi_common_error;
18755                     end if;
18756 
18757                     l_stmt_num := 260.21;
18758 
18759 
18760                     -- Delete the setup resources for the resources for whci setup resources where not added in this run...
18761                     -- these resources are recommended one..
18762                     -- Delete the all the setup resources of the resources which are being updated...
18763 
18764                     l_index :=  l_v_res.first;
18765 
18766                     while l_index is not null loop
18767                         --- fnd_file.put_line(fnd_file.log,'import job details : Loop resource counter : ' ||  l_index);
18768                         IF not(l_v_setup_op_res_num.exists('N'||'-'||l_rtg_op_seq_num||'-'||l_v_res(l_index).res_seq_num)) THEN
18769 
18770                             p_error_code := 0;
18771                             p_error_msg  := null;
18772                             l_stmt_num := 260.22;
18773                             -- ST : Detailed Scheduling... should we remove the resources' setup resources that are being replaced....
18774                             delete_setup_info( p_wip_entity_id             => p_wip_entity_id,
18775                                        p_org_id                    => p_org_id,
18776                                        p_par_res_seq_num           => l_v_res(l_index).res_seq_num,
18777                                        p_op_seq_num                => l_rtg_op_seq_num,
18778                                        p_handle_curr_op            => 'N',
18779                                        x_error_code                => p_error_code,
18780                                        x_error_msg                 => p_error_msg
18781                                      );
18782 
18783                             l_stmt_num := 260.23;
18784 
18785                             if p_error_code <> 0 then
18786                                 -- error out...
18787                                 raise e_wljdi_common_error;
18788                             end if;
18789                         END IF;
18790                         l_stmt_num := 260.24;
18791                         l_index := l_v_res.next(l_index);
18792                     end loop;
18793 
18794                     l_stmt_num := 260.3;
18795 
18796                     -- save validated info to an array
18797                     if(l_sub_type = WSM_SUB_CHG) then
18798                         l_resource_id := l_v_res.first;
18799                         l_v_rtg_op_seq(l_out)  := l_rtg_op_seq_num;
18800                         l_v_res_ids(l_out)     := l_resource_id;
18801                         l_v_res_sub_grp(l_out) := l_sub_grp_num;
18802                         l_v_res_rpl_grp(l_out) := l_rpl_grp_num;
18803                         l_v_start_date(l_out)  := l_v_res(l_resource_id).start_date;
18804                         l_v_compl_date(l_out)  := l_v_res(l_resource_id).completion_date;
18805 
18806                         /* ST : Detailed Scheduling start */
18807                         l_v_res_seq_nums(l_out)         := l_v_res(l_resource_id).res_seq_num ;
18808                         l_v_res_firm_type(l_out)        := l_v_res(l_resource_id).firm_type ;
18809                         l_v_res_setup_id(l_out)         := l_v_res(l_resource_id).setup_id ;
18810                         l_v_res_grp_seq_id(l_out)       := l_v_res(l_resource_id).grp_seq_id  ;
18811                         l_v_res_grp_seq_num(l_out)      := l_v_res(l_resource_id).grp_seq_num  ;
18812                         l_v_res_max_assg_units(l_out)   := l_v_res(l_resource_id).max_assg_units ;
18813                         l_v_res_parent_seq_num(l_out)   := l_v_res(l_resource_id).parent_seq_num ;
18814                         l_v_res_batch_id(l_out)         := l_v_res(l_resource_id).batch_id;
18815                         l_v_res_assg_units(l_out)       := l_v_res(l_resource_id).assigned_units;
18816                         /* ST : Detailed Scheduling end */
18817 
18818                         l_v_attribute_category(l_out) :=v_wljdi_attribute_category(l_d);
18819                         l_v_attribute1(l_out)   := v_wljdi_attribute1(l_d) ;
18820                         l_v_attribute2(l_out)   := v_wljdi_attribute2(l_d) ;
18821                         l_v_attribute3(l_out)   := v_wljdi_attribute3(l_d) ;
18822                         l_v_attribute4(l_out)   := v_wljdi_attribute4(l_d) ;
18823                         l_v_attribute5(l_out)   := v_wljdi_attribute5(l_d) ;
18824                         l_v_attribute6(l_out)   := v_wljdi_attribute6(l_d) ;
18825                         l_v_attribute7(l_out)   := v_wljdi_attribute7(l_d) ;
18826                         l_v_attribute8(l_out)   := v_wljdi_attribute8(l_d) ;
18827                         l_v_attribute9(l_out)   := v_wljdi_attribute9(l_d) ;
18828                         l_v_attribute10(l_out) := v_wljdi_attribute10(l_d);
18829                         l_v_attribute11(l_out) := v_wljdi_attribute11(l_d);
18830                         l_v_attribute12(l_out) := v_wljdi_attribute12(l_d);
18831                         l_v_attribute13(l_out) := v_wljdi_attribute13(l_d);
18832                         l_v_attribute14(l_out) := v_wljdi_attribute14(l_d);
18833                         l_v_attribute15(l_out) := v_wljdi_attribute15(l_d);
18834 
18835                         l_v_recoflag(l_out)    := NULL;
18836                         l_out := l_out +1;
18837 
18838                     elsif(l_sub_type = WSM_SUB_REC) then
18839                         l_resource_id := l_v_res.first;
18840                         while (l_resource_id IS NOT NULL)
18841                         loop
18842                             l_v_rtg_op_seq(l_out)  := l_rtg_op_seq_num;
18843                             l_v_res_ids(l_out)     := l_resource_id;
18844                             l_v_res_sub_grp(l_out) := l_sub_grp_num;
18845                             l_v_res_rpl_grp(l_out) := l_rpl_grp_num;
18846                             l_v_start_date(l_out)  := l_v_res(l_resource_id).start_date;
18847                             l_v_compl_date(l_out)  := l_v_res(l_resource_id).completion_date;
18848 
18849                             /* ST : Detailed Scheduling start */
18850                             l_v_res_seq_nums(l_out)   := l_v_res(l_resource_id).res_seq_num ;
18851                             l_v_res_firm_type(l_out)      := l_v_res(l_resource_id).firm_type ;
18852                             l_v_res_setup_id(l_out)       := l_v_res(l_resource_id).setup_id ;
18853                             l_v_res_grp_seq_id(l_out)     := l_v_res(l_resource_id).grp_seq_id  ;
18854                             l_v_res_grp_seq_num(l_out)    := l_v_res(l_resource_id).grp_seq_num  ;
18855                             l_v_res_max_assg_units(l_out) := l_v_res(l_resource_id).max_assg_units ;
18856                             l_v_res_parent_seq_num(l_out) := l_v_res(l_resource_id).parent_seq_num ;
18857                             l_v_res_batch_id(l_out)       := l_v_res(l_resource_id).batch_id;
18858                             l_v_res_assg_units(l_out)     := l_v_res(l_resource_id).assigned_units;
18859                             /* ST : Detailed Scheduling end */
18860 
18861                             l_v_attribute_category(l_out) :=v_wljdi_attribute_category(l_d);
18862                             l_v_attribute1(l_out)   := v_wljdi_attribute1(l_d) ;
18863                             l_v_attribute2(l_out)   := v_wljdi_attribute2(l_d) ;
18864                             l_v_attribute3(l_out)   := v_wljdi_attribute3(l_d) ;
18865                             l_v_attribute4(l_out)   := v_wljdi_attribute4(l_d) ;
18866                             l_v_attribute5(l_out)   := v_wljdi_attribute5(l_d) ;
18867                             l_v_attribute6(l_out)   := v_wljdi_attribute6(l_d) ;
18868                             l_v_attribute7(l_out)   := v_wljdi_attribute7(l_d) ;
18869                             l_v_attribute8(l_out)   := v_wljdi_attribute8(l_d) ;
18870                             l_v_attribute9(l_out)   := v_wljdi_attribute9(l_d) ;
18871                             l_v_attribute10(l_out) := v_wljdi_attribute10(l_d);
18872                             l_v_attribute11(l_out) := v_wljdi_attribute11(l_d);
18873                             l_v_attribute12(l_out) := v_wljdi_attribute12(l_d);
18874                             l_v_attribute13(l_out) := v_wljdi_attribute13(l_d);
18875                             l_v_attribute14(l_out) := v_wljdi_attribute14(l_d);
18876                             l_v_attribute15(l_out) := v_wljdi_attribute15(l_d);
18877 
18878                             IF lbji_debug = 'Y' THEN
18879                                     fnd_file.put_line(fnd_file.log,'import job details : Recomm. the resource..' || ' Rtg Op seq Num : ' || l_rtg_op_seq_num
18880                                                         || ' Res ID .... : ' || l_resource_id
18881                                                         || ' date : ' || l_v_res(l_resource_id).start_date );
18882                             END IF;
18883 
18884                             l_v_recoflag(l_out)    := 'Y';
18885 
18886                             /* here do it..... */
18887                             l_hash_str := to_char(p_wip_entity_id) || ':' || to_char(l_rtg_op_seq_num) || ':' || to_char(l_resource_id);
18888                             l_hash_value := dbms_utility.get_hash_value(l_hash_str,35,1048576); /* 2^20 */
18889                             l_v_res_recom(l_hash_value) := 'Y';
18890                             /* ........................... */
18891 
18892                             l_resource_id := l_v_res.NEXT(l_resource_id);
18893                             l_out := l_out +1;
18894 
18895                         end loop;
18896                         l_stmt_num := 260.4;
18897                         -- get the other resource with different l_rpl_grp_num
18898                         l_v_disco_res_ids.delete;
18899                         l_v_disco_res_rpl_grp.delete;
18900 
18901                         /* ST : Detailed Scheduling added... */
18902                         l_v_disco_res_seq_num.delete;
18903                         /* ST : Detailed Scheduling added... */
18904 
18905                         open c_disco_res (
18906                             p_wip_entity_id,
18907                             l_rtg_op_seq_num,
18908                             l_sub_grp_num,
18909                             l_rpl_grp_num);
18910 
18911                         fetch c_disco_res bulk collect into
18912                             l_v_disco_res_ids,
18913                             l_v_disco_res_rpl_grp,
18914                             l_v_disco_res_seq_num; /* ST : Detailed Scheduling added... */
18915 
18916                          if(c_disco_res%rowcount > 0) then
18917                                 l_idx2 := l_v_disco_res_ids.first;
18918 
18919                                 while(l_idx2 IS NOT NULL)
18920                                 loop
18921                                        l_v_rtg_op_seq(l_out)  := l_rtg_op_seq_num;
18922                                        l_v_res_ids(l_out)     := l_v_disco_res_ids(l_idx2);
18923 
18924                                        l_v_res_sub_grp(l_out) := l_sub_grp_num;
18925                                        l_v_res_rpl_grp(l_out) := l_v_disco_res_rpl_grp(l_idx2);
18926                                        l_v_start_date(l_out)  := NULL;     -- CZHDBG: why nullify?
18927                                        l_v_compl_date(l_out)  := NULL;     -- CZHDBG: why nullify?
18928                                        l_v_recoflag(l_out)    := 'N';
18929 
18930                                        /* ST : Detailed scheduling... if we have to change the DS populated data.. add code here to
18931                                           null the new fields
18932                                        */
18933                                        l_v_res_seq_nums(l_out)     := l_v_disco_res_seq_num(l_idx2);
18934                                        l_v_res_firm_type(l_out)    := 0; /* none */
18935                                        l_v_res_setup_id(l_out)     := null;
18936                                        l_v_res_grp_seq_id(l_out)   := null;
18937                                        l_v_res_grp_seq_id(l_out)   := null;
18938                                        l_v_res_batch_id(l_out)     := null;
18939                                        l_v_res_max_assg_units(l_out)   := null;
18940                                        l_v_res_assg_units(l_out)   := null;
18941                                        l_v_attribute_category(l_out) :=null;
18942                                        l_v_attribute1(l_out)   := null;
18943                                        l_v_attribute2(l_out)   := null;
18944                                        l_v_attribute3(l_out)   := null;
18945                                        l_v_attribute4(l_out)   := null;
18946                                        l_v_attribute5(l_out)   := null;
18947                                        l_v_attribute6(l_out)   := null;
18948                                        l_v_attribute7(l_out)   := null;
18949                                        l_v_attribute8(l_out)   := null;
18950                                        l_v_attribute9(l_out)   := null;
18951                                        l_v_attribute10(l_out) := null;
18952                                        l_v_attribute11(l_out) := null;
18953                                        l_v_attribute12(l_out) := null;
18954                                        l_v_attribute13(l_out) := null;
18955                                        l_v_attribute14(l_out) := null;
18956                                        l_v_attribute15(l_out) := null;
18957 
18958                                         IF lbji_debug = 'Y' THEN
18959                                                fnd_file.put_line(fnd_file.log,'import job details : Replacing the resource..' || ' Rtg Op seq Num : ' || l_rtg_op_seq_num
18960                                                                 || ' parent res.... : ' || l_v_disco_res_seq_num(l_idx2) );
18961                                         END IF;
18962                                         /* here do it..... */
18963                                         /* not required... but check it with maruthi...
18964                                         since
18965                                         i) Add setup resources to resource X
18966                                         ii) Replace resource X.
18967 
18968                                         This shouldnt be a problem only that we will still insert setup resources and then delete it here...
18969                                         */
18970 
18971                                         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));
18972                                         l_hash_value := dbms_utility.get_hash_value(l_hash_str,35,1048576); -- 2^20
18973                                         l_v_res_recom(l_hash_value) := 'N';
18974                                         /* ........................... */
18975                                         /* Add code here to delete the setup resource info.....*/
18976                                         p_error_code := 0;
18977                                         p_error_msg  := null;
18978 
18979                                         /* ST : Detailed Scheduling... should we remove the resources' setup resources that are being replaced.... */
18980 
18981                                         delete_setup_info( p_wip_entity_id             => p_wip_entity_id,
18982                                                            p_org_id                    => p_org_id,
18983                                                            p_par_res_seq_num           => l_v_disco_res_seq_num(l_idx2),
18984                                                            p_op_seq_num                => l_rtg_op_seq_num,
18985                                                            p_handle_curr_op            => 'N',
18986                                                            x_error_code                => p_error_code,
18987                                                            x_error_msg                 => p_error_msg
18988                                                          );
18989 
18990                                         if p_error_code <> 0 then
18991                                                 /* error out... */
18992                                                 raise e_wljdi_common_error;
18993                                         end if;
18994                                         l_out := l_out +1;
18995                                         l_idx2 := l_v_disco_res_ids.next(l_idx2);
18996 
18997                                 end loop;
18998                         end if;
18999                         close c_disco_res;
19000                     end if;  -- l_sub_type = WSM_SUB_REC
19001                 end if;     -- l_rtg_op_seq_num IS NOT NULL
19002                 -- }} Update WSM_COPY table
19003                 ---l_d := v_wljdi_record_id.next(l_d);  Updated for Alcoa Enhancement
19004             end if; -- l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC)
19005 	    l_d := v_wljdi_record_id.next(l_d);    ---Added for Alcoa Enhancement
19006         end loop;
19007 
19008         -- Update WCOR
19009         if(l_v_rtg_op_seq.first IS NOT NULL) then
19010             l_stmt_num := 270.1;
19011             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
19012                 UPDATE  WSM_COPY_OP_RESOURCES
19013                 set     reco_start_date         = l_v_start_date(i),
19014                         reco_completion_date    = l_v_compl_date(i),
19015                         recommended             = NVL(l_v_recoflag(i),  recommended),
19016 
19017                         /* ST : Detailed Scheduling start */
19018 						/* Modified Query by Adding NVL during update for firm_type. bug 5471266
19019 						   Already assig_units, max_assg_units were having nvl in it.*/
19020                         firm_type           = NVL( l_v_res_firm_type(i) , firm_type),
19021                         setup_id            = l_v_res_setup_id(i),
19022                         group_sequence_id   = l_v_res_grp_seq_id(i),
19023                         group_sequence_num  = l_v_res_grp_seq_id(i),
19024                         max_assigned_units  = nvl( l_v_res_max_assg_units(i) , max_assigned_units),
19025                         assigned_units      = nvl( l_v_res_assg_units(i)   , assigned_units),
19026                         batch_id            = l_v_res_batch_id(i),
19027                         /* ST : Detailed Scheduling end */
19028 
19029                         last_update_date        = p_last_updt_date,
19030                         last_updated_by         = p_last_updt_by,
19031                         last_update_login       = p_last_updt_login,
19032                         request_id              = p_request_id,
19033                         program_application_id  = p_program_appl_id,
19034                         program_id              = p_program_id,
19035                         program_update_date     = p_prog_updt_date,
19036                         attribute_category = nvl(attribute_category,l_v_attribute_category(i)),
19037                         attribute1 = nvl(attribute1,l_v_attribute1(i)),
19038                         attribute2 = nvl(attribute2,l_v_attribute2(i)),
19039                         attribute3 = nvl(attribute3,l_v_attribute3(i)),
19040                         attribute4 = nvl(attribute4,l_v_attribute4(i)),
19041                         attribute5 = nvl(attribute5,l_v_attribute5(i)),
19042                         attribute6 = nvl(attribute6,l_v_attribute6(i)),
19043                         attribute7 = nvl(attribute7,l_v_attribute7(i)),
19044                         attribute8 = nvl(attribute8,l_v_attribute8(i)),
19045                         attribute9 = nvl(attribute9,l_v_attribute9(i)),
19046                         attribute10 = nvl(attribute10,l_v_attribute10(i)),
19047                         attribute11 = nvl(attribute11,l_v_attribute11(i)),
19048                         attribute12 = nvl(attribute12,l_v_attribute12(i)),
19049                         attribute13 = nvl(attribute13,l_v_attribute13(i)),
19050                         attribute14 = nvl(attribute14,l_v_attribute14(i)),
19051                         attribute15 = nvl(attribute15,l_v_attribute15(i))
19052                 where   wip_entity_id         = p_wip_entity_id
19053                 and     operation_seq_num     = l_v_rtg_op_seq(i)
19054                 and     resource_id           = l_v_res_ids(i)
19055                 and     resource_seq_num      = nvl(l_v_res_seq_nums(i),resource_seq_num)
19056                 and     nvl(substitute_group_num, -1) = nvl(l_v_res_sub_grp(i), -1)
19057                 and     nvl(replacement_group_num, 0) = nvl(l_v_res_rpl_grp(i), 0);
19058 
19059                 -- will not delete this because resource dates on non-reco operation
19060                 -- will be kept. Gantt will not show and usage/instance on non-reco
19061                 -- path
19062 
19063                 l_stmt_num := 270.2;
19064                 -- delete resource usage/instance records
19065                 --delete  WSM_COPY_OP_RESOURCE_USAGE
19066                 --where   wip_entity_id         = p_wip_entity_id
19067                 --and     (operation_seq_num,
19068                 --         resource_seq_num) in (
19069                 --        select operation_seq_num,
19070                 --               resource_seq_num
19071                 --        from   WSM_COPY_OP_RESOURCES
19072                 --        where  wip_entity_id = p_wip_entity_id
19073                 --        and    recommended <> 'Y' );
19074                 l_stmt_num := 270.3;
19075                 --delete  WSM_COPY_OP_RESOURCE_INSTANCES
19076                 --where   wip_entity_id         = p_wip_entity_id
19077                 --and     (operation_seq_num,
19078                 --         resource_seq_num) in (
19079                 --        select operation_seq_num,
19080                 --               resource_seq_num
19081                 --        from   WSM_COPY_OP_RESOURCES
19082                 --        where  wip_entity_id = p_wip_entity_id
19083                 --        and    recommended <> 'Y' );
19084 
19085         end if;
19086         if lbji_debug = 'Y' then
19087             fnd_file.put_line(fnd_file.log, 'Mark recommended operation resources ... OK.');
19088         end if;
19089 
19090     end if;
19091     ------------------------------------------------
19092     -- }} validete operation resources
19093     ------------------------------------------------
19094 
19095 l_stmt_num := 300;
19096     ------------------------------------------------
19097     -- {{ validete operation resources instances
19098     ------------------------------------------------
19099     if l_res_ins_first IS NOT NULL then
19100         -- clear output PL/SQL table
19101         l_out := 0;
19102         l_v_rtg_op_seq.delete;
19103         l_v_res_seq_num.delete;
19104         l_v_start_date.delete;
19105         l_v_compl_date.delete;
19106         l_v_instance_id.delete;
19107         l_v_serial_num.delete;
19108     l_v_res_ins_batch_id.delete;
19109 
19110         l_d := l_res_ins_first;
19111         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) = WSM_LOAD_RES_INS)
19112         loop
19113             l_load_type      := v_wljdi_load_type(l_d);
19114             l_sub_type       := v_wljdi_substitution_type(l_d);
19115             l_job_op_seq_num := v_wljdi_job_op_seq_num(l_d);     -- current op only
19116             l_rtg_op_seq_num := v_wljdi_routing_op_seq_num(l_d); -- future op only
19117             l_res_seq_num    := v_wljdi_resource_seq_num(l_d);
19118 
19119             -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19120             l_resource_id               := v_wljdi_resource_id_new(l_d);
19121             l_parent_res_seq_num    := v_wljdi_parent_seq_num(l_d);
19122             -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19123 
19124             -- {{ Update WIP table
19125             if(l_job_op_seq_num IS NOT NULL) then
19126 
19127                 if(l_job_op_seq_num = l_cur_job_op_seq_num) then
19128                     l_stmt_num := 310.1;
19129                     -- pack data into PL/SQL table l_v_res_ins for validation
19130                     l_v_res_ins.delete;
19131                     l_in := 1;
19132                     l_v_res_ins(l_in).job_op_seq_num := l_job_op_seq_num;
19133                     l_v_res_ins(l_in).res_seq_num    := l_res_seq_num;
19134                     l_v_res_ins(l_in).instance_id    := v_wljdi_instance_id_new(l_d);
19135                     l_v_res_ins(l_in).serial_number  := v_wljdi_serial_number_new(l_d);
19136                     l_v_res_ins(l_in).start_date     := v_wljdi_start_date(l_d);
19137                     l_v_res_ins(l_in).completion_date:= v_wljdi_completion_date(l_d);
19138                     l_v_res_ins(l_in).batch_id       := v_wljdi_batch_id(l_d);
19139 
19140                     l_stmt_num := 310.2;
19141                     l_temp := v_wljdi_record_id.next(l_d);
19142 
19143                     -- get all records with the same res_seq_num or belonging to the same resource...
19144                     while(l_temp IS NOT NULL and
19145                           v_wljdi_load_type(l_temp)         = l_load_type and
19146                           v_wljdi_substitution_type(l_temp) = l_sub_type and
19147                           v_wljdi_job_op_seq_num(l_temp)    = l_job_op_seq_num and
19148                           ( (v_wljdi_resource_seq_num(l_temp)  = l_res_seq_num)
19149                              -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19150                              OR
19151                              ( nvl(v_wljdi_resource_seq_num(l_temp),-1) = nvl(l_res_seq_num,-1) and
19152                                v_wljdi_parent_seq_num(l_d)              = l_parent_res_seq_num   and
19153                                v_wljdi_resource_id_new(l_d)             = l_resource_id
19154                              )
19155                            -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19156                           )
19157                          )
19158                     loop
19159                         l_d  := l_temp;      -- move index l_d!!
19160                         l_in := l_in+1;
19161                         l_v_res_ins(l_in).job_op_seq_num := l_job_op_seq_num;
19162                         l_v_res_ins(l_in).res_seq_num    := l_res_seq_num;
19163                         l_v_res_ins(l_in).instance_id    := v_wljdi_instance_id_new(l_d);
19164                         l_v_res_ins(l_in).serial_number  := v_wljdi_serial_number_new(l_d);
19165                         l_v_res_ins(l_in).start_date     := v_wljdi_start_date(l_d);
19166                         l_v_res_ins(l_in).completion_date:= v_wljdi_completion_date(l_d);
19167                         l_v_res_ins(l_in).batch_id       := v_wljdi_batch_id(l_d);
19168 
19169                         l_temp := v_wljdi_record_id.next(l_d);
19170                     end loop;
19171 
19172                     l_stmt_num := 310.3;
19173                     -- valid resource usage
19174                     validate_wljdi_res_instance(
19175                         p_wip_entity_id     => p_wip_entity_id,
19176                         p_org_id            => p_org_id,
19177                         p_v_res_ins         => l_v_res_ins,
19178                         p_substitution_type => l_sub_type,
19179                         p_handle_wip_cur_op => 'Y',
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 
19188                     if p_error_code <> 0 then
19189                         raise e_wljdi_common_error;
19190                     end if;
19191 
19192                     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19193                     if l_res_seq_num IS NULL THEN
19194                         l_res_seq_num := l_setup_res_seq_num;
19195                     end if;
19196                     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19197 
19198                     l_stmt_num := 310.4;
19199                     -- delete all existing records for that resource_seq_num
19200                     -- CZHDBG: do we need to check if resource is charged ???
19201                     delete from wip_op_resource_instances
19202                     where  wip_entity_id     = p_wip_entity_id
19203                     and    organization_id   = p_org_id
19204                     and    operation_seq_num = l_job_op_seq_num
19205                     and    resource_seq_num  = l_res_seq_num;
19206 
19207                     l_stmt_num := 310.5;
19208                     delete from wip_operation_resource_usage
19209                     where  wip_entity_id     = p_wip_entity_id
19210                     and    organization_id   = p_org_id
19211                     and    operation_seq_num = l_job_op_seq_num
19212                     and    resource_seq_num  = l_res_seq_num;
19213 
19214                     if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC) then
19215                            l_in := l_v_res_ins.first;
19216 
19217                             while (l_in IS NOT NULL)
19218                             loop
19219                                     l_stmt_num := 310.6;
19220                                     insert into wip_op_resource_instances(
19221                                         wip_entity_id  ,
19222                                         organization_id,
19223                                         operation_seq_num,
19224                                         resource_seq_num,
19225                                         instance_id,
19226                                         serial_number,
19227                                         start_date,
19228                                         completion_date,
19229                                         batch_id ,
19230                                         last_update_date,
19231                                         last_updated_by,
19232                                         creation_date,
19233                                         created_by,
19234                                         last_update_login
19235                                     ) values (
19236                                         p_wip_entity_id,
19237                                         p_org_id,
19238                                         l_job_op_seq_num,
19239                                         l_res_seq_num,
19240                                         l_v_res_ins(l_in).instance_id,
19241                                         l_v_res_ins(l_in).serial_number,
19242                                         l_v_res_ins(l_in).start_date,
19243                                         l_v_res_ins(l_in).completion_date,
19244                                         l_v_res_ins(l_in).batch_id,
19245                                         p_last_updt_date,
19246                                         p_last_updt_by,
19247                                         p_creation_date,
19248                                         p_created_by,
19249                                         p_last_updt_login);
19250 
19251                                     l_in  := l_v_res_ins.next(l_in);
19252                             end loop;
19253                     end if;
19254 
19255                 else    -- l_job_op_seq_num <> l_cur_job_op_seq_num
19256                     -- this can happen when recommendations arrive after the job
19257                     -- is moved. should give a warning message
19258                     fnd_file.put_line(fnd_file.log,
19259                         'Warning: resource instance recommendations for current operation are ignored ' ||
19260                         '(record_id = ' || l_d || ')' );
19261                 end if;
19262                 -- }} Update WIP table
19263 
19264             -- {{ Update WSM_COPY table
19265             elsif(l_rtg_op_seq_num IS NOT NULL) then
19266 
19267                 l_stmt_num := 320.1;
19268                 -- pack data into PL/SQL table l_v_res for validation
19269                 l_v_res_ins.delete;
19270                 l_in := 1;
19271                 l_v_res_ins(l_in).rtg_op_seq_num := l_rtg_op_seq_num;
19272                 l_v_res_ins(l_in).res_seq_num    := l_res_seq_num;
19273                 l_v_res_ins(l_in).instance_id    := v_wljdi_instance_id_new(l_d);
19274                 l_v_res_ins(l_in).serial_number  := v_wljdi_serial_number_new(l_d);
19275                 l_v_res_ins(l_in).start_date     := v_wljdi_start_date(l_d);
19276                 l_v_res_ins(l_in).completion_date:= v_wljdi_completion_date(l_d);
19277                 l_v_res_ins(l_in).batch_id       := v_wljdi_batch_id(l_d);
19278 
19279 
19280                 l_stmt_num := 320.2;
19281                 l_temp := v_wljdi_record_id.next(l_d);
19282                 -- get all records with the same res_seq_num
19283                 while(l_temp IS NOT NULL and
19284                       v_wljdi_load_type(l_temp)          = l_load_type and
19285                       v_wljdi_substitution_type(l_temp)  = l_sub_type and
19286                       v_wljdi_routing_op_seq_num(l_temp) = l_rtg_op_seq_num and
19287                       (  (  v_wljdi_resource_seq_num(l_temp)   = l_res_seq_num)
19288                          -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19289                          OR
19290                          (  (nvl(v_wljdi_resource_seq_num(l_temp),-1) = nvl(l_res_seq_num,-1)   and
19291                             (v_wljdi_parent_seq_num(l_d)              = l_parent_res_seq_num)   and
19292                             (v_wljdi_resource_id_new(l_d)             = l_resource_id))
19293                          -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19294                          )
19295                       )
19296                      )
19297                 loop
19298                     l_d := l_temp;      -- move index l_d!!
19299                     l_in := l_in+1;
19300                     l_v_res_ins(l_in).rtg_op_seq_num := l_rtg_op_seq_num;
19301                     l_v_res_ins(l_in).res_seq_num    := l_res_seq_num;
19302                     l_v_res_ins(l_in).instance_id    := v_wljdi_instance_id_new(l_d);
19303                     l_v_res_ins(l_in).serial_number  := v_wljdi_serial_number_new(l_d);
19304                     l_v_res_ins(l_in).start_date     := v_wljdi_start_date(l_d);
19305                     l_v_res_ins(l_in).completion_date:= v_wljdi_completion_date(l_d);
19306                     l_v_res_ins(l_in).batch_id       := v_wljdi_batch_id(l_d);
19307                     l_temp := v_wljdi_record_id.next(l_d);
19308                 end loop;
19309 
19310 
19311 l_stmt_num := 320.3;
19312 
19313                 -- valid resource usage
19314                 validate_wljdi_res_instance(
19315                     p_wip_entity_id     => p_wip_entity_id,
19316                     p_org_id            => p_org_id,
19317                     p_v_res_ins         => l_v_res_ins,
19318                     p_substitution_type => l_sub_type,
19319                     p_handle_wip_cur_op => 'N',
19320                     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19321                     p_parent_res_seq_num => l_parent_res_seq_num,
19322                     p_resource_id        => l_resource_id,
19323                     x_setup_res_seq_num  => l_setup_res_seq_num,
19324                     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19325                     x_error_code        => p_error_code,
19326                     x_error_msg         => p_error_msg);
19327 
19328                 if p_error_code <> 0 then
19329                     raise e_wljdi_common_error;
19330                 end if;
19331 
19332                 -- l_res_seq_num should always be available now
19333                 l_res_seq_num := l_v_res_ins(1).res_seq_num;
19334 
19335                 -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19336                 if l_res_seq_num IS NULL THEN
19337                         l_res_seq_num := l_setup_res_seq_num;
19338                 end if;
19339                 -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19340 
19341                 l_stmt_num := 320.4;
19342                 -- delete all existing records for that resource_seq_num
19343                 delete from wsm_copy_op_resource_instances
19344                 where  wip_entity_id     = p_wip_entity_id
19345                 and    organization_id   = p_org_id
19346                 and    operation_seq_num = l_rtg_op_seq_num
19347                 and    resource_seq_num  = l_res_seq_num;
19348 l_stmt_num := 320.5;
19349                 delete from wsm_copy_op_resource_usage
19350                 where  wip_entity_id     = p_wip_entity_id
19351                 and    organization_id   = p_org_id
19352                 and    operation_seq_num = l_rtg_op_seq_num
19353                 and    resource_seq_num  = l_res_seq_num;
19354 l_stmt_num := 320.6;
19355                 -- save validated data into an array for balk update
19356                 if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC) then
19357                     l_in := l_v_res_ins.first;
19358                     while (l_in IS NOT NULL)
19359                     loop
19360                         l_v_rtg_op_seq(l_out)       := l_rtg_op_seq_num;
19361                         l_v_res_seq_num(l_out)      := l_res_seq_num;
19362                         l_v_instance_id(l_out)      := l_v_res_ins(l_in).instance_id;
19363                         l_v_serial_num(l_out)       := l_v_res_ins(l_in).serial_number;
19364                         l_v_start_date(l_out)       := l_v_res_ins(l_in).start_date;
19365                         l_v_compl_date(l_out)       := l_v_res_ins(l_in).completion_date;
19366                         l_v_res_ins_batch_id(l_out) := l_v_res_ins(l_in).batch_id;
19367                         l_out := l_out +1;
19368                         l_in  := l_v_res_ins.next(l_in);
19369                     end loop;
19370                 end if;
19371             end if;
19372             -- }} Update WSM_COPY table
19373 
19374             l_d := v_wljdi_record_id.next(l_d);
19375         end loop;
19376 
19377         -- creating WCORI records
19378         if(l_v_rtg_op_seq.first IS NOT NULL) then
19379             l_stmt_num := 330;
19380             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
19381                 insert into wsm_copy_op_resource_instances(
19382                     wip_entity_id  ,
19383                     organization_id,
19384                     operation_seq_num,
19385                     resource_seq_num,
19386                     instance_id,
19387                     serial_number,
19388                     start_date ,
19389                     completion_date,
19390                     batch_id,
19391                     last_update_date,
19392                     last_updated_by,
19393                     creation_date,
19394                     created_by,
19395                     last_update_login,
19396                     request_id,
19397                     program_application_id,
19398                     program_id,
19399                     program_update_date
19400                 ) values (
19401                     p_wip_entity_id,
19402                     p_org_id,
19403                     l_v_rtg_op_seq(i),
19404                     l_v_res_seq_num(i),
19405                     l_v_instance_id(i),
19406                     l_v_serial_num(i),
19407                     l_v_start_date(i),
19408                     l_v_compl_date(i),
19409                     l_v_res_ins_batch_id(i),
19410                     p_last_updt_date,
19411                     p_last_updt_by,
19412                     p_creation_date,
19413                     p_created_by,
19414                     p_last_updt_login,
19415                     p_request_id,
19416                     p_program_appl_id,
19417                     p_program_id,
19418                     p_prog_updt_date);
19419 
19420          if lbji_debug = 'Y' then
19421             fnd_file.put_line(fnd_file.log, 'Inserted resource instance ... OK.');
19422          end if;
19423 
19424         end if;
19425         if lbji_debug = 'Y' then
19426             fnd_file.put_line(fnd_file.log, 'Mark recommended resource instance ... OK.');
19427         end if;
19428 
19429     end if; -- resource instance recommendations exist
19430     ------------------------------------------------
19431     -- }} validete operation resources instances
19432     ------------------------------------------------
19433 
19434 
19435 l_stmt_num := 400;
19436     ------------------------------------------------
19437     -- {{ validete operation resources usage
19438     ------------------------------------------------
19439 
19440     if l_res_use_first IS NOT NULL then
19441         -- clear output PL/SQL table
19442         l_out := 0;
19443         l_v_rtg_op_seq.delete;
19444         l_v_res_seq_num.delete;
19445         l_v_instance_id.delete;
19446         l_v_serial_num.delete;
19447         l_v_start_date.delete;
19448         l_v_compl_date.delete;
19449         l_v_res_use_asgn_units.delete;
19450 
19451         /* ST : Detailed scheduling : start */
19452         l_v_resource_hours.delete;
19453         /* ST : Detailed scheduling : end */
19454 
19455         l_d := l_res_use_first;
19456         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) = WSM_LOAD_RES_USE)
19457         loop
19458             l_load_type      := v_wljdi_load_type(l_d);
19459             l_sub_type       := v_wljdi_substitution_type(l_d);
19460             l_job_op_seq_num := v_wljdi_job_op_seq_num(l_d);     -- current op only
19461             l_rtg_op_seq_num := v_wljdi_routing_op_seq_num(l_d); -- future op only
19462             l_res_seq_num    := v_wljdi_resource_seq_num(l_d);
19463             l_instance_id    := v_wljdi_instance_id_new(l_d);
19464             l_serial_num     := v_wljdi_serial_number_new(l_d);
19465 
19466             /* ST : Detailed scheduling : start */
19467             l_resource_hours := v_wljdi_resource_hours(l_d);
19468             /* ST : Detailed scheduling : end */
19469 
19470             -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19471             l_resource_id               := v_wljdi_resource_id_new(l_d);
19472             l_parent_res_seq_num    := v_wljdi_parent_seq_num(l_d);
19473             -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19474 
19475             if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC, WSM_SUB_DEL) then
19476 
19477                 -- {{ Update WIP table
19478                 if(l_job_op_seq_num IS NOT NULL) then
19479 
19480                     if(l_job_op_seq_num = l_cur_job_op_seq_num) then
19481                         l_stmt_num := 410.1;
19482                         -- pack data into PL/SQL table l_v_res_use for validation
19483                         l_v_res_use.delete;
19484                         l_in := 1;
19485                         l_v_res_use(l_in).job_op_seq_num := l_job_op_seq_num;
19486                         l_v_res_use(l_in).res_seq_num    := l_res_seq_num;
19487                         l_v_res_use(l_in).instance_id    := l_instance_id;
19488                         l_v_res_use(l_in).serial_number  := l_serial_num;
19489                         l_v_res_use(l_in).assigned_units := v_wljdi_assigned_units(l_d);
19490                         l_v_res_use(l_in).start_date     := v_wljdi_start_date(l_d);
19491                         l_v_res_use(l_in).completion_date:= v_wljdi_completion_date(l_d);
19492 
19493                         /* ST : Detailed scheduling : start */
19494                         l_v_res_use(l_in).resource_hours := v_wljdi_resource_hours(l_d);
19495                         /* ST : Detailed scheduling : end */
19496 
19497                         -- BA: bug 3373496 to filter duplicated resource usage records
19498                         l_v_dup_usage.delete;
19499                         l_hash_str   := to_char(l_v_res_use(l_in).start_date, 'DD-MON-YYYY HH24:MI:SS') ||
19500                                         to_char(l_v_res_use(l_in).completion_date, 'DD-MON-YYYY HH24:MI:SS');
19501                         l_hash_value := dbms_utility.get_hash_value(l_hash_str, 37, 1073741824);
19502                         l_v_dup_usage(l_hash_value) := l_hash_value;
19503                         -- EA: bug 3373496
19504 
19505 l_stmt_num := 410.2;
19506                         l_temp := v_wljdi_record_id.next(l_d);
19507                         -- get all records with the same res_seq_num, instance_id and serial_num
19508                         while(l_temp IS NOT NULL and
19509                               v_wljdi_load_type(l_temp)         = l_load_type and
19510                               v_wljdi_substitution_type(l_temp) = l_sub_type and
19511                               v_wljdi_job_op_seq_num(l_temp)    = l_job_op_seq_num and
19512                               ( (v_wljdi_resource_seq_num(l_temp)  = l_res_seq_num)
19513                                 -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19514                                 OR
19515                                 ( nvl(v_wljdi_resource_seq_num(l_temp),-1) = nvl(l_res_seq_num,-1) and
19516                                  (v_wljdi_parent_seq_num(l_d) = l_parent_res_seq_num)              and
19517                                  (v_wljdi_resource_id_new(l_d)     = l_resource_id)
19518                                 )
19519                                 -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19520                               ) and
19521                               NVL(v_wljdi_instance_id_new(l_temp), -1)= NVL(l_instance_id, -1) and
19522                               NVL(v_wljdi_serial_number_new(l_temp), g_nvl_str) = NVL(l_serial_num, g_nvl_str)
19523                              )
19524                         loop
19525                             l_d := l_temp;      -- move index l_d!!
19526 
19527                             -- BA: bug 3373496 to filter duplicated resource usage records
19528                             l_hash_str   := to_char(v_wljdi_start_date(l_d), 'DD-MON-YYYY HH24:MI:SS') ||
19529                                             to_char(v_wljdi_completion_date(l_d), 'DD-MON-YYYY HH24:MI:SS');
19530                             l_hash_value := dbms_utility.get_hash_value(l_hash_str, 37, 1073741824);
19531                             -- EA: bug 3373496
19532 
19533                             -- BC: bug 3373496 to filter duplicated resource usage records
19534                             -- only new value will be added
19535 
19536                             --if(l_v_dup_usage(l_hash_value) IS NULL) then      -- bug 3401727
19537                             if(NOT l_v_dup_usage.exists(l_hash_value)) then     -- bug 3401727
19538                                 l_in := l_in+1;
19539                                 l_v_res_use(l_in).job_op_seq_num := l_job_op_seq_num;
19540                                 l_v_res_use(l_in).res_seq_num    := l_res_seq_num;
19541                                 l_v_res_use(l_in).instance_id    := l_instance_id;
19542                                 l_v_res_use(l_in).serial_number  := l_serial_num;
19543                                 l_v_res_use(l_in).assigned_units := v_wljdi_assigned_units(l_d);
19544                                 l_v_res_use(l_in).start_date     := v_wljdi_start_date(l_d);
19545                                 l_v_res_use(l_in).completion_date:= v_wljdi_completion_date(l_d);
19546 
19547                                 /* ST : Detailed scheduling : start */
19548                                 l_v_res_use(l_in).resource_hours := v_wljdi_resource_hours(l_d);
19549                                 /* ST : Detailed scheduling : end */
19550 
19551                                 l_v_dup_usage(l_hash_value) := l_hash_value;
19552                             else
19553                                 -- dup usage record
19554                                 fnd_file.put_line(fnd_file.log,
19555                                     'Warning: duplicated resource usage recommendations are ignored ' ||
19556                                     '(record_id = ' || l_d || ').');
19557                             end if;
19558                             -- EC: bug 3373496
19559 
19560                             l_temp := v_wljdi_record_id.next(l_d);
19561                         end loop;
19562 
19563 l_stmt_num := 410.3;
19564                         -- valid resource usage
19565                         validate_wljdi_res_usage(
19566                             p_wip_entity_id     => p_wip_entity_id,
19567                             p_org_id            => p_org_id,
19568                             p_v_res_use         => l_v_res_use,
19569                             p_substitution_type => l_sub_type,
19570                             p_handle_wip_cur_op => 'Y',
19571                             -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19572                             p_parent_res_seq_num => l_parent_res_seq_num,
19573                             p_resource_id    => l_resource_id,
19574                             x_setup_res_seq_num  => l_setup_res_seq_num,
19575                             -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19576                             x_error_code        => p_error_code,
19577                             x_error_msg         => p_error_msg);
19578                         if p_error_code <> 0 then
19579                             raise e_wljdi_common_error;
19580                         end if;
19581 
19582                         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19583                         if l_res_seq_num IS NULL THEN
19584                                 l_res_seq_num := l_setup_res_seq_num;
19585                         end if;
19586                         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19587 
19588 l_stmt_num := 410.4;
19589                         -- delete all existing records for that resource_seq_num
19590                         delete from wip_operation_resource_usage
19591                         where  wip_entity_id        = p_wip_entity_id
19592                         and    organization_id      = p_org_id
19593                         and    operation_seq_num    = l_job_op_seq_num
19594                         and    resource_seq_num     = l_res_seq_num
19595                         and    NVL(instance_id, -1) = NVL(l_instance_id, -1)
19596                         and    NVL(serial_number, g_nvl_str) = NVL(l_serial_num, g_nvl_str);
19597 
19598                         if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC) then
19599                             l_in := l_v_res_use.first;
19600                             while (l_in IS NOT NULL)
19601                             loop
19602 l_stmt_num := 410.5;
19603                                 insert into wip_operation_resource_usage(
19604                                     wip_entity_id  ,
19605                                     organization_id,
19606                                     operation_seq_num,
19607                                     resource_seq_num,
19608                                     instance_id,
19609                                     serial_number,
19610                                     start_date ,
19611                                     completion_date,
19612                                     assigned_units ,
19613                                     -- resource_hours,  /* ST : Added for Detailed Scheduling */
19614                                     last_update_date,
19615                                     last_updated_by,
19616                                     creation_date,
19617                                     created_by,
19618                                     last_update_login,
19619                                     request_id,
19620                                     program_application_id,
19621                                     program_id,
19622                                     program_update_date
19623                                 ) values (
19624                                     p_wip_entity_id,
19625                                     p_org_id,
19626                                     l_job_op_seq_num,
19627                                     l_res_seq_num,
19628                                     l_instance_id,
19629                                     l_serial_num,
19630                                     l_v_res_use(l_in).start_date,
19631                                     l_v_res_use(l_in).completion_date,
19632                                     l_v_res_use(l_in).assigned_units,
19633                                     -- l_v_res_use(l_in).resource_hours, /* ST : Added for Detailed Scheduling */
19634                                     p_last_updt_date,
19635                                     p_last_updt_by,
19636                                     p_creation_date,
19637                                     p_created_by,
19638                                     p_last_updt_login,
19639                                     p_request_id,
19640                                     p_program_appl_id,
19641                                     p_program_id,
19642                                     p_prog_updt_date);
19643 
19644                                 update_cumulative_time(
19645                                     p_wip_entity_id      => p_wip_entity_id,
19646                                     p_operation_seq_num  => l_job_op_seq_num,
19647                                     p_resource_seq_num   => l_res_seq_num,
19648                                     p_instance_id        => l_instance_id,
19649                                     p_serial_num         => l_serial_num,
19650                                     p_handle_wip_cur_op  => 'Y' );
19651 
19652                                 l_in  := l_v_res_use.next(l_in);
19653                             end loop;
19654                         end if;
19655 
19656                     else -- l_job_op_seq_num <> l_cur_job_op_seq_num
19657                         -- this can happen when recommendations arrive after the job
19658                         -- is moved. should give a warning message
19659                         fnd_file.put_line(fnd_file.log,
19660                             'Warning: resource usage recommendations for current operation is ignored ' ||
19661                             '(record_id = ' || l_d || ').');
19662                     end if;
19663                 -- }} Update WIP table
19664 
19665                 -- {{ Update WSM_COPY table
19666                 elsif(l_rtg_op_seq_num IS NOT NULL) then
19667                     l_stmt_num := 420.1;
19668                     -- pack data into PL/SQL table l_v_res for validation
19669                     l_v_res_use.delete;
19670                     l_in := 1;
19671                     l_v_res_use(l_in).rtg_op_seq_num := l_rtg_op_seq_num;
19672                     l_v_res_use(l_in).res_seq_num    := l_res_seq_num;
19673                     l_v_res_use(l_in).instance_id    := l_instance_id;
19674                     l_v_res_use(l_in).serial_number  := l_serial_num;
19675                     l_v_res_use(l_in).assigned_units := v_wljdi_assigned_units(l_d);
19676                     l_v_res_use(l_in).start_date     := v_wljdi_start_date(l_d);
19677                     l_v_res_use(l_in).completion_date:= v_wljdi_completion_date(l_d);
19678 
19679                     /* ST : Detailed scheduling : start */
19680                     l_v_res_use(l_in).resource_hours := v_wljdi_resource_hours(l_d);
19681                     /* ST : Detailed scheduling : end */
19682 
19683                     -- BA: bug 3373496 to filter duplicated resource usage records
19684                     l_v_dup_usage.delete;
19685                     l_hash_str   := to_char(l_v_res_use(l_in).start_date, 'DD-MON-YYYY HH24:MI:SS') ||
19686                                     to_char(l_v_res_use(l_in).completion_date, 'DD-MON-YYYY HH24:MI:SS');
19687                     l_hash_value := dbms_utility.get_hash_value(l_hash_str, 37, 1073741824);
19688                     l_v_dup_usage(l_hash_value) := l_hash_value;
19689                     -- EA: bug 3373496
19690 
19691 l_stmt_num := 420.2;
19692                     l_temp := v_wljdi_record_id.next(l_d);
19693                     -- get all records with the same res_seq_num
19694                     while(l_temp IS NOT NULL and
19695                           v_wljdi_load_type(l_temp)          = l_load_type and
19696                           v_wljdi_substitution_type(l_temp)  = l_sub_type and
19697                           v_wljdi_routing_op_seq_num(l_temp) = l_rtg_op_seq_num and
19698                           (  (v_wljdi_resource_seq_num(l_temp)   = l_res_seq_num)
19699                               -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19700                               OR
19701                               ( nvl(v_wljdi_resource_seq_num(l_temp),-1) = nvl(l_res_seq_num,-1)        and
19702                                 (v_wljdi_parent_seq_num(l_d) = l_parent_res_seq_num)                      and
19703                                  (v_wljdi_resource_id_new(l_d)     = l_resource_id)
19704                               )
19705                               -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19706                           )
19707                           and
19708                           NVL(v_wljdi_instance_id_new(l_temp), -1) = NVL(l_instance_id, -1) and
19709                           NVL(v_wljdi_serial_number_new(l_temp), g_nvl_str) = NVL(l_serial_num, g_nvl_str))
19710                     loop
19711                         l_d := l_temp;      -- move index l_d!!
19712 
19713                         -- BA: bug 3373496 to filter duplicated resource usage records
19714                         l_hash_str   := to_char(v_wljdi_start_date(l_d), 'DD-MON-YYYY HH24:MI:SS') ||
19715                                         to_char(v_wljdi_completion_date(l_d), 'DD-MON-YYYY HH24:MI:SS');
19716                         l_hash_value := dbms_utility.get_hash_value(l_hash_str, 37, 1073741824);
19717                         -- EA: bug 3373496
19718 
19719                         -- BC: bug 3373496 to filter duplicated resource usage records
19720                         -- only new value will be added
19721 
19722                         --if(l_v_dup_usage(l_hash_value) IS NULL) then          -- bug 3401727
19723                         if(NOT l_v_dup_usage.exists(l_hash_value)) then         -- bug 3401727
19724                             l_in := l_in+1;
19725                             l_v_res_use(l_in).rtg_op_seq_num := l_rtg_op_seq_num;
19726                             l_v_res_use(l_in).res_seq_num    := l_res_seq_num;
19727                             l_v_res_use(l_in).instance_id    := l_instance_id;
19728                             l_v_res_use(l_in).serial_number  := l_serial_num;
19729                             l_v_res_use(l_in).assigned_units := v_wljdi_assigned_units(l_d);
19730                             l_v_res_use(l_in).start_date     := v_wljdi_start_date(l_d);
19731                             l_v_res_use(l_in).completion_date:= v_wljdi_completion_date(l_d);
19732 
19733                             /* ST : Detailed scheduling : start */
19734                             l_v_res_use(l_in).resource_hours := v_wljdi_resource_hours(l_d);
19735                             /* ST : Detailed scheduling : end */
19736 
19737                             l_v_dup_usage(l_hash_value) := l_hash_value;
19738                         else
19739                             -- dup usage record
19740                             fnd_file.put_line(fnd_file.log,
19741                                 'Warning: duplicated resource usage recommendations are ignored ' ||
19742                                 '(record_id = ' || l_d || ').');
19743                         end if;
19744                         -- EC: bug 3373496
19745 
19746                         l_temp := v_wljdi_record_id.next(l_d);
19747                     end loop;
19748 
19749 l_stmt_num := 420.3;
19750                     -- valid resource usage
19751                     validate_wljdi_res_usage(
19752                         p_wip_entity_id     => p_wip_entity_id,
19753                         p_org_id            => p_org_id,
19754                         p_v_res_use         => l_v_res_use,
19755                         p_substitution_type => l_sub_type,
19756                         p_handle_wip_cur_op => 'N',
19757                         -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19758                         p_parent_res_seq_num => l_parent_res_seq_num,
19759                         p_resource_id        => l_resource_id,
19760                         x_setup_res_seq_num  => l_setup_res_seq_num,
19761                         -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19762                         x_error_code        => p_error_code,
19763                         x_error_msg         => p_error_msg);
19764                     if p_error_code <> 0 then
19765                         raise e_wljdi_common_error;
19766                     end if;
19767 
19768                     -- ST : Bug fix 4240165 : Detailed Scheduling changes start
19769                     if l_res_seq_num IS NULL THEN
19770                         l_res_seq_num := l_setup_res_seq_num;
19771                     end if;
19772                     -- ST : Bug fix 4240165 : Detailed Scheduling changes end
19773 
19774 l_stmt_num := 420.4;
19775                     -- delete all existing records for that resource_seq_num
19776                     delete from wsm_copy_op_resource_usage
19777                     where  wip_entity_id     = p_wip_entity_id
19778                     and    organization_id   = p_org_id
19779                     and    operation_seq_num = l_rtg_op_seq_num
19780                     and    resource_seq_num  = l_res_seq_num;
19781 
19782                     -- save validated data into an array for balk update
19783                     if l_sub_type in (WSM_SUB_ADD, WSM_SUB_REC) then
19784                         l_in := l_v_res_use.first;
19785                         while (l_in IS NOT NULL)
19786                         loop
19787                             l_v_rtg_op_seq(l_out)         := l_rtg_op_seq_num;
19788                             l_v_res_seq_num(l_out)        := l_res_seq_num;
19789                             l_v_instance_id(l_out)        := l_instance_id;
19790                             l_v_serial_num(l_out)         := l_serial_num;
19791                             l_v_start_date(l_out)         := l_v_res_use(l_in).start_date;
19792                             l_v_compl_date(l_out)         := l_v_res_use(l_in).completion_date;
19793                             l_v_res_use_asgn_units(l_out) := l_v_res_use(l_in).assigned_units;
19794 
19795                             /* ST : Detailed scheduling : start */
19796                             l_v_resource_hours(l_out)     := l_v_res_use(l_in).resource_hours;
19797                             /* ST : Detailed scheduling : end */
19798 
19799                             l_out := l_out +1;
19800                             l_in  := l_v_res_use.next(l_in);
19801                         end loop;
19802                     end if;
19803                 end if;
19804                 -- }} Update WSM_COPY table
19805 
19806             end if; -- p_substitution_type in (WSM_SUB_ADD, WSM_SUB_REC, WSM_SUB_DEL)
19807             l_d := v_wljdi_record_id.next(l_d);
19808         end loop;
19809 
19810         -- creating WCORU records
19811         if(l_v_rtg_op_seq.first IS NOT NULL) then
19812 l_stmt_num := 430;
19813             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
19814                 insert into wsm_copy_op_resource_usage(
19815                     wip_entity_id  ,
19816                     organization_id,
19817                     operation_seq_num,
19818                     resource_seq_num,
19819                     instance_id,
19820                     serial_number,
19821                     start_date ,
19822                     completion_date,
19823                     assigned_units ,
19824                     resource_hours,  /* ST : Added for Detailed Scheduling */
19825                     last_update_date,
19826                     last_updated_by,
19827                     creation_date,
19828                     created_by,
19829                     last_update_login,
19830                     request_id,
19831                     program_application_id,
19832                     program_id,
19833                     program_update_date
19834                 ) values (
19835                     p_wip_entity_id,
19836                     p_org_id,
19837                     l_v_rtg_op_seq(i),
19838                     l_v_res_seq_num(i),
19839                     l_v_instance_id(i),
19840                     l_v_serial_num(i),
19841                     l_v_start_date(i),
19842                     l_v_compl_date(i),
19843                     l_v_res_use_asgn_units(i),
19844                     l_v_resource_hours(i),  /* ST : Added for Detailed Scheduling */
19845                     p_last_updt_date,
19846                     p_last_updt_by,
19847                     p_creation_date,
19848                     p_created_by,
19849                     p_last_updt_login,
19850                     p_request_id,
19851                     p_program_appl_id,
19852                     p_program_id,
19853                     p_prog_updt_date);
19854 
19855             for i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
19856             loop
19857                 update_cumulative_time(
19858                     p_wip_entity_id      => p_wip_entity_id,
19859                     p_operation_seq_num  => l_v_rtg_op_seq(i),
19860                     p_resource_seq_num   => l_v_res_seq_num(i),
19861                     p_instance_id        => l_v_instance_id(i),
19862                     p_serial_num         => l_v_serial_num(i),
19863                     p_handle_wip_cur_op  => 'N' );
19864             end loop;
19865 
19866         end if;
19867         if lbji_debug = 'Y' then
19868             fnd_file.put_line(fnd_file.log, 'Mark recommended resource usage ... OK.');
19869         end if;
19870 
19871     end if; -- resource usage recommendations exist
19872     ------------------------------------------------
19873     -- }} validete operation resources usage
19874     ------------------------------------------------
19875 
19876 
19877 l_stmt_num := 500;
19878     ------------------------------------------------
19879     -- {{ validete component
19880     ------------------------------------------------
19881     if l_comp_first IS NOT NULL then
19882         -- clear output PL/SQL table
19883         l_out := 0;
19884         l_v_rtg_op_seq.delete;
19885         l_v_comp_item_id.delete;
19886         l_v_comp_prm_item_id.delete;
19887         l_v_comp_phm_item_id.delete;
19888         l_v_comp_reqd_date.delete;
19889         l_v_recoflag.delete;
19890         l_v_comp_yield.delete;
19891         l_v_comp_qpa.delete;
19892         l_v_attribute_category.delete;
19893         l_v_attribute1.delete;
19894         l_v_attribute2.delete;
19895         l_v_attribute3.delete;
19896         l_v_attribute4.delete;
19897         l_v_attribute5.delete;
19898         l_v_attribute6.delete;
19899         l_v_attribute7.delete;
19900         l_v_attribute8.delete;
19901         l_v_attribute9.delete;
19902         l_v_attribute10.delete;
19903         l_v_attribute11.delete;
19904         l_v_attribute12.delete;
19905         l_v_attribute13.delete;
19906         l_v_attribute14.delete;
19907         l_v_attribute15.delete;
19908         l_d := l_comp_first;
19909         while (l_d IS NOT NULL and v_wljdi_load_type(l_d) = WSM_LOAD_COMP)
19910         loop
19911             l_load_type       := v_wljdi_load_type(l_d);
19912             l_sub_type        := v_wljdi_substitution_type(l_d);
19913             l_job_op_seq_num  := v_wljdi_job_op_seq_num(l_d);         -- current op only
19914             l_rtg_op_seq_num  := v_wljdi_routing_op_seq_num(l_d);     -- future op only
19915             l_item_id         := v_wljdi_inventory_item_id_new(l_d);  -- should not be null
19916             l_primary_item_id := v_wljdi_primary_item_id(l_d);        -- should not be null
19917             l_src_phm_item_id := v_wljdi_src_phantom_item_id(l_d);    -- can be null
19918             l_component_seq_id:= v_wljdi_component_sequence_id(l_d);  -- should not be null
19919             -- Added For LBJ Interface Alcoa Enhancement
19920             l_item_old_id         := v_wljdi_inventory_item_id_old(l_d);
19921 
19922             if l_sub_type in (WSM_SUB_DEL, WSM_SUB_ADD) then
19923 --               Modified for Alcoa Enhancement
19924 --                if(l_job_op_seq_num IS NULL) then
19925 --l_stmt_num := 510;
19926 --                    fnd_message.set_name('WSM','WSM_NULL_FIELD');
19927 --                    fnd_message.set_token('FLD_NAME', 'job_op_seq_num');
19928 --                    p_error_msg := fnd_message.get;
19929 --                    p_error_code := -1;
19930 --                    raise e_wljdi_common_error;
19931 --                else
19932 --                    fnd_file.put_line(fnd_file.log,
19933 --                        'Warning: deleting or adding components for current operation '||
19934 --                        'is currently not supported and ignored.' -- CZHDBG_MSG
19935 --                    );
19936 --                end if;
19937 			l_stmt_num := 510;
19938 			-- Added For LBJ Interface Alcoa Enhancement
19939 			IF(l_job_op_seq_num IS NOT NULL) THEN
19940 			    IF (l_job_op_seq_num=l_cur_job_op_seq_num) THEN
19941 				IF (l_sub_type=WSM_SUB_DEL) THEN
19942 				    l_in := 1;
19943 					l_v_comp.delete;
19944 					l_v_comp(l_in).job_op_seq_num   := l_job_op_seq_num;
19945 					l_v_comp(l_in).item_id          := l_item_id;
19946 					l_v_comp(l_in).item_old_id      := l_item_old_id;
19947 					l_v_comp(l_in).primary_item_id  := l_primary_item_id;
19948 					l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
19949 					l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
19950 	l_stmt_num := 750.2;
19951 					validate_wljdi_op_component(
19952 					    p_wip_entity_id     => p_wip_entity_id,
19953 					    p_org_id            => p_org_id,
19954 					    p_v_comp            => l_v_comp,
19955 					    p_substitution_type => l_sub_type,
19956 					    p_handle_wip_cur_op => 'Y',
19957 					    x_error_code        => p_error_code,
19958 					    x_error_msg         => p_error_msg);
19959 					if p_error_code <> 0 then
19960 					    raise e_wljdi_common_error;
19961 					end if;
19962 
19963 					delete wip_requirement_operations
19964 					where wip_entity_id = p_wip_entity_id
19965 					and organization_id   = p_org_id
19966 					and operation_seq_num = l_job_op_seq_num
19967 					and inventory_item_id = l_item_old_id;
19968 
19969 					wip_picking_pvt.cancel_comp_allocations(
19970 					    p_wip_entity_id     => p_wip_entity_id,
19971 					    p_operation_seq_num => l_job_op_seq_num,
19972 					    p_inventory_item_id => l_item_old_id,
19973 					    p_wip_entity_type   => WIP_CONSTANTS.LOTBASED,
19974 					    x_return_status     => x_return_status,
19975 					    x_msg_data          => p_error_msg);
19976 
19977 					if (x_return_status <> fnd_api.g_ret_sts_success) then
19978 					    raise e_wljdi_common_error;
19979 					end if;
19980 
19981 				ELSIF (l_sub_type=WSM_SUB_ADD) then
19982 	l_stmt_num:=59.9;
19983 				    l_in := 1;
19984 				    l_v_comp.delete;
19985 				    l_v_comp(l_in).job_op_seq_num   := l_job_op_seq_num;
19986 				    l_v_comp(l_in).item_id          := l_item_id;
19987 				    l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
19988 				    l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
19989 				    l_v_comp(l_in).wip_supply_type  := v_wljdi_wip_supply_type(l_d);
19990 				    l_v_comp(l_in).qpa                  := v_wljdi_quantity_per_assembly(l_d);
19991 				    l_v_comp(l_in).supply_subinventory  := v_wljdi_supply_subinventory(l_d);
19992 				    l_v_comp(l_in).supply_locator       := v_wljdi_supply_locator_id(l_d);
19993 				    l_v_comp(l_in).mrp_net_flag         := v_wljdi_mrp_net_flag(l_d);
19994 
19995 				    if v_wljdi_recommended(l_d) = 'N' then
19996 					fnd_file.put_line(fnd_file.log,'Warning: Addition of non-recommended component for current operation are ignored ' || '(record_id = ' || l_d || ').' );
19997 					l_valid_comp := false;
19998 				    else
19999 					l_valid_comp := true;
20000 				    end if;
20001 	l_stmt_num := 750.2;
20002 				    if l_valid_comp then
20003 
20004 					validate_wljdi_op_component(
20005 					p_wip_entity_id     => p_wip_entity_id,
20006 					p_org_id            => p_org_id,
20007 					p_v_comp            => l_v_comp,
20008 					p_substitution_type => l_sub_type,
20009 					p_handle_wip_cur_op => 'Y',
20010 					x_error_code        => p_error_code,
20011 					x_error_msg         => p_error_msg);
20012 
20013 					if p_error_code <> 0 then
20014 					    raise e_wljdi_common_error;
20015 					end if;
20016 
20017 					INSERT INTO WIP_REQUIREMENT_OPERATIONS
20018 					(inventory_item_id,
20019 						organization_id,
20020 						wip_entity_id,
20021 						operation_seq_num,
20022 						repetitive_schedule_id,
20023 						last_update_date,
20024 						last_updated_by,
20025 						creation_date,
20026 						created_by,
20027 						last_update_login,
20028 						component_sequence_id,
20029 						wip_supply_type,
20030 						date_required,
20031 						required_quantity,
20032 						quantity_issued,
20033 						quantity_per_assembly,
20034 						supply_subinventory,
20035 						supply_locator_id,
20036 						mrp_net_flag,
20037 						auto_request_material,
20038 						comments,
20039 						attribute_category,
20040 						attribute1,
20041 						attribute2,
20042 						attribute3,
20043 						attribute4,
20044 						attribute5,
20045 						attribute6,
20046 						attribute7,
20047 						attribute8,
20048 						attribute9,
20049 						attribute10,
20050 						attribute11,
20051 						attribute12,
20052 						attribute13,
20053 						attribute14,
20054 						attribute15,
20055 						request_id,
20056 						program_application_id,
20057 						program_id,
20058 						program_update_date,
20059 						segment1,
20060 						segment2,
20061 						segment3,
20062 						segment4,
20063 						segment5,
20064 						segment6,
20065 						segment7,
20066 						segment8,
20067 						segment9,
20068 						segment10,
20069 						segment11,
20070 						segment12,
20071 						segment13,
20072 						segment14,
20073 						segment15,
20074 						segment16,
20075 						segment17,
20076 						segment18,
20077 						segment19,
20078 						segment20,
20079 						department_id,
20080 						costed_quantity_issued,
20081 						costed_quantity_relieved,
20082 						released_quantity)
20083 					       select
20084 						l_v_comp(l_in).item_id,
20085 						p_org_id,
20086 						p_wip_entity_id,
20087 						l_v_comp(l_in).job_op_seq_num,
20088 						null,
20089 						p_last_updt_date,
20090 						p_last_updt_by,
20091 						p_creation_date,
20092 						p_created_by,
20093 						p_last_updt_login,
20094 						null,
20095 						l_v_comp(l_in).wip_supply_type,
20096 						l_v_comp(l_in).date_required,
20097 						l_v_comp(l_in).required_qty,
20098 						0,
20099 						l_v_comp(l_in).qpa,
20100 						l_v_comp(l_in).supply_subinventory,
20101 						l_v_comp(l_in).supply_locator,
20102 						v_wljdi_mrp_net_flag(l_d),
20103 						v_wljdi_auto_request_material(l_d),
20104 						v_wljdi_comments(l_d),
20105 						v_wljdi_attribute_category(l_d),
20106 						v_wljdi_attribute1(l_d),
20107 						v_wljdi_attribute2(l_d),
20108 						v_wljdi_attribute3(l_d),
20109 						v_wljdi_attribute4(l_d),
20110 						v_wljdi_attribute5(l_d),
20111 						v_wljdi_attribute6(l_d),
20112 						v_wljdi_attribute7(l_d),
20113 						v_wljdi_attribute8(l_d),
20114 						v_wljdi_attribute9(l_d),
20115 						v_wljdi_attribute10(l_d),
20116 						v_wljdi_attribute11(l_d),
20117 						v_wljdi_attribute12(l_d),
20118 						v_wljdi_attribute13(l_d),
20119 						v_wljdi_attribute14(l_d),
20120 						v_wljdi_attribute15(l_d),
20121 						p_request_id,
20122 						p_program_appl_id,
20123 						p_program_id,
20124 						p_prog_updt_date,
20125 						msi.segment1,
20126 						msi.segment2,
20127 						msi.segment3,
20128 						msi.segment4,
20129 						msi.segment5,
20130 						msi.segment6,
20131 						msi.segment7,
20132 						msi.segment8,
20133 						msi.segment9,
20134 						msi.segment10,
20135 						msi.segment11,
20136 						msi.segment12,
20137 						msi.segment13,
20138 						msi.segment14,
20139 						msi.segment15,
20140 						msi.segment16,
20141 						msi.segment17,
20142 						msi.segment18,
20143 						msi.segment19,
20144 						msi.segment20,
20145 						wo.department_id,
20146 						null,
20147 						null,
20148 						round(l_cur_job_qty*l_v_comp(l_in).qpa, 6)
20149 					from    wip_operations wo,
20150 						mtl_system_items msi
20151 					where   wo.operation_seq_num = l_v_comp(l_in).job_op_seq_num
20152 					and     wo.wip_entity_id= p_wip_entity_id
20153 					and     msi.inventory_item_id = l_v_comp(l_in).item_id
20154 					and     msi.organization_id = wo.organization_id
20155 					and     msi.organization_id = p_org_id;
20156 				    end if;
20157 				end if; -- WSM_SUB_DEL AND ADD
20158 			    else
20159 				fnd_file.put_line(fnd_file.log,
20160 				'Warning: Deleting or Adding components for executed operations '||
20161 				'is currently not supported and ignored.');
20162 			    end if;  --current operation
20163 			ELSIF (l_rtg_op_seq_num IS NOT NULL) THEN
20164 			    IF (l_sub_type=WSM_SUB_DEL) THEN
20165 				l_in := 1;
20166 				l_v_comp.delete;
20167 				l_v_comp(l_in).rtg_op_seq_num   := l_rtg_op_seq_num;
20168 				l_v_comp(l_in).item_id          := l_item_id;
20169 				l_v_comp(l_in).item_old_id      := l_item_old_id;
20170 				l_v_comp(l_in).src_phm_item_id :=v_wljdi_src_phantom_item_id(l_d);
20171 				l_v_comp(l_in).wip_supply_type  := v_wljdi_wip_supply_type(l_d);
20172 			      --l_v_comp(l_in).primary_item_id  := l_primary_item_id;
20173 			      --l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
20174 			      --l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
20175 	l_stmt_num := 750.21;
20176 
20177 				validate_wljdi_op_component(
20178 				    p_wip_entity_id     => p_wip_entity_id,
20179 				    p_org_id            => p_org_id,
20180 				    p_v_comp            => l_v_comp,
20181 				    p_substitution_type => l_sub_type,
20182 				    p_handle_wip_cur_op => 'N',
20183 				    x_error_code        => p_error_code,
20184 				    x_error_msg         => p_error_msg);
20185 
20186 				if p_error_code <> 0 then
20187 				    raise e_wljdi_common_error;
20188 				end if;
20189 
20190 				if v_wljdi_src_phantom_item_id(l_d)=0 then
20191 				    delete  wsm_copy_requirement_ops
20192 				    where  operation_seq_num=(-1)*l_rtg_op_seq_num
20193 				    and  wip_entity_id =p_wip_entity_id
20194 				    and  component_item_id =l_item_old_id;
20195 				else
20196 				    delete  wsm_copy_requirement_ops
20197 				    where  operation_seq_num=l_rtg_op_seq_num
20198 				    and  wip_entity_id =p_wip_entity_id
20199 				    and  component_item_id =l_item_old_id;
20200 				end if;
20201 
20202 			ELSIF (l_sub_type=WSM_SUB_ADD) THEN
20203 	l_stmt_num:=79.9;
20204 			    l_in := 1;
20205 			    l_v_comp.delete;
20206 			    l_v_comp(l_in).rtg_op_seq_num   := l_rtg_op_seq_num;
20207 			    l_v_comp(l_in).item_id          := l_item_id;
20208 			    l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
20209 			    l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
20210 			    l_v_comp(l_in).wip_supply_type  := v_wljdi_wip_supply_type(l_d);
20211 			    l_v_comp(l_in).qpa                  := v_wljdi_quantity_per_assembly(l_d);
20212 			    l_v_comp(l_in).supply_subinventory  := v_wljdi_supply_subinventory(l_d);
20213 			    l_v_comp(l_in).supply_locator       := v_wljdi_supply_locator_id(l_d);
20214 			    l_v_comp(l_in).mrp_net_flag         := v_wljdi_mrp_net_flag(l_d);
20215 			    l_v_comp(l_in).src_phm_item_id    := v_wljdi_src_phantom_item_id(l_d);
20216 
20217 	l_stmt_num := 750.2;
20218 			    validate_wljdi_op_component(
20219 				p_wip_entity_id     => p_wip_entity_id,
20220 				p_org_id            => p_org_id,
20221 				p_v_comp            => l_v_comp,
20222 				p_substitution_type => l_sub_type,
20223 				p_handle_wip_cur_op => 'N',
20224 				x_error_code        => p_error_code,
20225 				x_error_msg         => p_error_msg);
20226 			    if p_error_code <> 0 then
20227 				raise e_wljdi_common_error;
20228 			    end if;
20229 
20230 			     INSERT INTO WSM_COPY_REQUIREMENT_OPS
20231 			     (wip_entity_id,
20232 			      operation_seq_num,
20233 			      component_item_id,
20234 			      primary_component_id,
20235 			      component_sequence_id,
20236 			      source_phantom_id,
20237 			      bill_sequence_id,
20238 			      effectivity_date,
20239 			      planning_factor,
20240 			      quantity_related,
20241 			      include_in_cost_rollup,
20242 			      check_atp,
20243 			      bom_item_type,
20244 			      recommended,
20245 			      reco_date_required,
20246 			      department_id,
20247 			      organization_id,
20248 			      wip_supply_type,
20249 			      supply_subinventory,
20250 			      supply_locator_id,
20251 			      quantity_per_assembly,
20252 			      bill_quantity_per_assembly,
20253 			      component_yield_factor,
20254 			      component_remarks,
20255 			      last_update_date,
20256 			      last_updated_by,
20257 			      last_update_login,
20258 			      creation_date,
20259 			      created_by,
20260 			      request_id,
20261 			      program_application_id,
20262 			      program_id,
20263 			      program_update_date,
20264 			      attribute_category,
20265 			      attribute1,
20266 			      attribute2,
20267 			      attribute3,
20268 			      attribute4,
20269 			      attribute5,
20270 			      attribute6,
20271 			      attribute7,
20272 			      attribute8,
20273 			      attribute9,
20274 			      attribute10,
20275 			      attribute11,
20276 			      attribute12,
20277 			      attribute13,
20278 			      attribute14,
20279 			      attribute15)
20280 			      select
20281 			       p_wip_entity_id,
20282 			       decode(l_v_comp(l_in).wip_supply_type, WIP_CONSTANTS.PHANTOM,(-1*l_rtg_op_seq_num),l_rtg_op_seq_num),
20283 			       l_v_comp(l_in).item_id,
20284 			       decode(nvl(v_wljdi_src_phantom_item_id(l_d),-1),-1,l_v_comp(l_in).item_id,
20285 									0,l_v_comp(l_in).item_id, v_wljdi_src_phantom_item_id(l_d)),
20286 			       -1,               -- Since not in bill
20287 			       decode(nvl(v_wljdi_src_phantom_item_id(l_d),-1), 0 , l_v_comp(l_in).item_id,
20288 									       -1, -1, v_wljdi_src_phantom_item_id(l_d)),
20289 			       -1,               -- Since not in bill
20290 			       p_creation_date,  -- Effective as on created date for job
20291 			       0,                -- Since it's not a planned component
20292 			       2,                -- No quantity relation
20293 			       1,                -- Include in cost rollup
20294 			       2,                -- No ATP
20295 			       4,                -- Standard component
20296 			       v_wljdi_recommended(l_d),
20297 			       l_v_comp(l_in).date_required,
20298 			       wco.department_id,
20299 			       p_org_id,
20300 			       l_v_comp(l_in).wip_supply_type,
20301 			       l_v_comp(l_in).supply_subinventory,
20302 			       l_v_comp(l_in).supply_locator,
20303 			       l_v_comp(l_in).qpa,
20304 			       l_v_comp(l_in).qpa,
20305 			       l_v_comp(l_in).component_yield,
20306 			       v_wljdi_comments(l_d),
20307 			       p_last_updt_date,
20308 			       p_last_updt_by,
20309 			       p_last_updt_login,
20310 			       p_creation_date,
20311 			       p_created_by,
20312 			       p_request_id,
20313 			       p_program_appl_id,
20314 			       p_program_id,
20315 			       p_prog_updt_date,
20316 			       v_wljdi_attribute_category(l_d),
20317 			       v_wljdi_attribute1(l_d),
20318 			       v_wljdi_attribute2(l_d),
20319 			       v_wljdi_attribute3(l_d),
20320 			       v_wljdi_attribute4(l_d),
20321 			       v_wljdi_attribute5(l_d),
20322 			       v_wljdi_attribute6(l_d),
20323 			       v_wljdi_attribute7(l_d),
20324 			       v_wljdi_attribute8(l_d),
20325 			       v_wljdi_attribute9(l_d),
20326 			       v_wljdi_attribute10(l_d),
20327 			       v_wljdi_attribute11(l_d),
20328 			       v_wljdi_attribute12(l_d),
20329 			       v_wljdi_attribute13(l_d),
20330 			       v_wljdi_attribute14(l_d),
20331 			       v_wljdi_attribute15(l_d)
20332 			      from
20333 			      wsm_copy_operations wco
20334 			      where wco.wip_entity_id = p_wip_entity_id
20335 			      and wco.organization_id = p_org_id
20336 			      and wco.operation_seq_num= l_rtg_op_seq_num;
20337 
20338 			END IF;
20339 		    END if;
20340             elsif l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC) then
20341 
20342                 -- {{ Update WIP table
20343                 if(l_job_op_seq_num IS NOT NULL) then
20344                     if(l_job_op_seq_num = l_cur_job_op_seq_num) then
20345 l_stmt_num := 550.1;
20346                         -- pack data into PL/SQL table l_v_comp for validation
20347                         -- for current operation src_phantom_item_id is ignored
20348                         -- bacause we insert consolidated componet info into WRO
20349                         -- also, only item in WRO can be updated/recommeded
20350                         l_in := 1;
20351                         l_v_comp.delete;
20352                         l_v_comp(l_in).job_op_seq_num   := l_job_op_seq_num;
20353                         l_v_comp(l_in).item_id          := l_item_id;
20354                         l_v_comp(l_in).primary_item_id  := l_primary_item_id;
20355                         l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
20356                         l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
20357 l_stmt_num := 550.2;
20358                         validate_wljdi_op_component(
20359                             p_wip_entity_id     => p_wip_entity_id,
20360                             p_org_id            => p_org_id,
20361                             p_v_comp            => l_v_comp,
20362                             p_substitution_type => l_sub_type,
20363                             p_handle_wip_cur_op => 'Y',
20364                             x_error_code        => p_error_code,
20365                             x_error_msg         => p_error_msg);
20366                         if p_error_code <> 0 then
20367                             raise e_wljdi_common_error;
20368                         end if;
20369 
20370 l_stmt_num := 550.3;
20371                         UPDATE WIP_REQUIREMENT_OPERATIONS
20372                         set     date_required           = l_v_comp(l_in).date_required,
20373                                 last_update_date        = p_last_updt_date,
20374                                 last_updated_by         = p_last_updt_by,
20375                                 last_update_login       = p_last_updt_login,
20376                                 request_id              = p_request_id,
20377                                 program_application_id  = p_program_appl_id,
20378                                 program_id              = p_program_id,
20379                                 program_update_date     = p_prog_updt_date,
20380                                 attribute_category = v_wljdi_attribute_category(l_d),
20381                                 attribute1 = v_wljdi_attribute1(l_d) ,
20382                                 attribute2 = v_wljdi_attribute2(l_d) ,
20383                                 attribute3 = v_wljdi_attribute3(l_d) ,
20384                                 attribute4 = v_wljdi_attribute4(l_d) ,
20385                                 attribute5 = v_wljdi_attribute5(l_d),
20386                                 attribute6 = v_wljdi_attribute6(l_d) ,
20387                                 attribute7 = v_wljdi_attribute7(l_d) ,
20388                                 attribute8 = v_wljdi_attribute8(l_d),
20389                                 attribute9 = v_wljdi_attribute9(l_d) ,
20390                                 attribute10 = v_wljdi_attribute10(l_d),
20391                                 attribute11 = v_wljdi_attribute11(l_d),
20392                                 attribute12 = v_wljdi_attribute12(l_d),
20393                                 attribute13 = v_wljdi_attribute13(l_d),
20394                                 attribute14 = v_wljdi_attribute14(l_d),
20395                                 attribute15 = v_wljdi_attribute15(l_d)
20396                         where   wip_entity_id     = p_wip_entity_id
20397                         and     organization_id   = p_org_id
20398                         and     operation_seq_num = l_job_op_seq_num
20399                         and     inventory_item_id = l_item_id;
20400 
20401                     else -- l_job_op_seq_num <> l_cur_job_op_seq_num
20402                         -- this can happen when recommendations arrive after the job is moved.
20403                         -- OSFM will give a warning message, and ignore this record
20404                         fnd_file.put_line(fnd_file.log,
20405                             'Warning: component recommendations for current operation are ignored ' ||
20406                             '(record_id = ' || l_d || ').' );
20407                     end if;
20408                 -- }} Update WIP table
20409 
20410                 -- {{ Update WSM_COPY table
20411                 elsif(l_rtg_op_seq_num IS NOT NULL) then
20412 l_stmt_num := 560.1;
20413                     -- pack data into PL/SQL table l_v_comp for validation
20414                     if(l_src_phm_item_id IS NULL) then
20415                         l_component_seq_id := NULL;
20416                     end if;
20417                     l_in := 1;
20418                     l_v_comp.delete;
20419                     l_v_comp(l_in).rtg_op_seq_num   := l_rtg_op_seq_num;
20420                     l_v_comp(l_in).item_id          := l_item_id;
20421                     l_v_comp(l_in).primary_item_id  := l_primary_item_id;
20422                     l_v_comp(l_in).src_phm_item_id  := l_src_phm_item_id;
20423                     l_v_comp(l_in).component_seq_id := l_component_seq_id;
20424                     l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
20425                     l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
20426                     l_v_comp(l_in).component_yield  := NULL;
20427 
20428                     -- handle phantom components, l_component_seq_id is required
20429                     if(l_src_phm_item_id IS NOT NULL) then
20430 
20431 l_stmt_num := 560.2;
20432                         SELECT  nvl(use_phantom_routings, 0)
20433                         INTO    l_use_phantom_routings
20434                         FROM    BOM_PARAMETERS
20435                         WHERE   ORGANIZATION_ID = p_org_id;
20436 
20437                         IF (l_use_phantom_routings = 1 and l_sub_type = WSM_SUB_REC) THEN
20438                             fnd_message.set_name('WSM','WSM_COMP_USE_PHANTOM_RTG');
20439                             p_error_msg := fnd_message.get;
20440                             p_error_code := -1;
20441                             raise e_wljdi_common_error;
20442                         end if;
20443 
20444                         if(l_component_seq_id IS NULL) then
20445                             raise e_null_comp_seq_id;
20446                         end if;
20447 
20448                         -- should send all the component with the same l_src_phm_item_id
20449                         -- hence, a local search should be done
20450                         -- this local search will change index l_d
20451                         l_temp := v_wljdi_record_id.next(l_d);
20452                         while(l_temp IS NOT NULL and
20453                               v_wljdi_load_type(l_temp)           = l_load_type and
20454                               v_wljdi_substitution_type(l_temp)   = l_sub_type and
20455                               v_wljdi_routing_op_seq_num(l_temp)  = l_rtg_op_seq_num and
20456                               v_wljdi_src_phantom_item_id(l_temp) = l_src_phm_item_id)
20457                         loop
20458                             l_d := l_temp;
20459                             l_component_seq_id := v_wljdi_component_sequence_id(l_d);
20460                             if(l_component_seq_id IS NULL) then
20461                                 raise e_null_comp_seq_id;
20462                             end if;
20463                             l_in := l_in +1;
20464                             l_v_comp(l_in).rtg_op_seq_num   := l_rtg_op_seq_num;
20465                             l_v_comp(l_in).item_id          := v_wljdi_inventory_item_id_new(l_d) ;
20466                             l_v_comp(l_in).primary_item_id  := l_primary_item_id;
20467                             l_v_comp(l_in).src_phm_item_id  := l_src_phm_item_id;
20468                             l_v_comp(l_in).component_seq_id := l_component_seq_id;
20469                             l_v_comp(l_in).date_required    := v_wljdi_date_required(l_d);
20470                             l_v_comp(l_in).required_qty     := v_wljdi_required_quantity(l_d);
20471                             l_v_comp(l_in).component_yield  := NULL;
20472                             l_temp := v_wljdi_record_id.next(l_d);
20473                         end loop;
20474                     end if;
20475 
20476 l_stmt_num := 560.3;
20477                     validate_wljdi_op_component(
20478                         p_wip_entity_id     => p_wip_entity_id,
20479                         p_org_id            => p_org_id,
20480                         p_v_comp            => l_v_comp,
20481                         p_substitution_type => l_sub_type,
20482                         p_handle_wip_cur_op => 'N',
20483                         x_error_code        => p_error_code,
20484                         x_error_msg         => p_error_msg);
20485                     if p_error_code <> 0 then
20486                         raise e_wljdi_common_error;
20487                     end if;
20488 
20489 l_stmt_num := 560.4;
20490                     -- save validated data into an array for balk update
20491                     if(l_sub_type = WSM_SUB_CHG) then
20492                         l_reco_flag := NULL;
20493                     elsif(l_sub_type = WSM_SUB_REC) then
20494                         l_reco_flag := 'Y';
20495                     end if;
20496 
20497                     l_in := l_v_comp.first;
20498                     while (l_in IS NOT NULL)
20499                     loop
20500                         l_v_rtg_op_seq (l_out)      := l_v_comp(l_in).rtg_op_seq_num;
20501                         l_v_comp_item_id(l_out)     := l_v_comp(l_in).item_id;
20502                         l_v_comp_prm_item_id(l_out) := l_v_comp(l_in).primary_item_id;
20503                         l_v_comp_phm_item_id(l_out) := l_v_comp(l_in).src_phm_item_id;
20504                         l_v_comp_comp_seq_id(l_out) := l_v_comp(l_in).component_seq_id;
20505                         l_v_comp_reqd_date(l_out)   := l_v_comp(l_in).date_required;
20506                         l_v_comp_yield(l_out)       := l_v_comp(l_in).component_yield;
20507                         l_v_comp_qpa(l_out)         := l_v_comp(l_in).qpa;  -- ADD bug 3347985
20508                         l_v_recoflag(l_out)         := l_reco_flag;
20509                         l_v_attribute_category(l_out) :=v_wljdi_attribute_category(l_d);
20510                         l_v_attribute1(l_out)   := v_wljdi_attribute1(l_d) ;
20511                         l_v_attribute2(l_out)   := v_wljdi_attribute2(l_d) ;
20512                         l_v_attribute3(l_out)   := v_wljdi_attribute3(l_d) ;
20513                         l_v_attribute4(l_out)   := v_wljdi_attribute4(l_d) ;
20514                         l_v_attribute5(l_out)   := v_wljdi_attribute5(l_d) ;
20515                         l_v_attribute6(l_out)   := v_wljdi_attribute6(l_d) ;
20516                         l_v_attribute7(l_out)   := v_wljdi_attribute7(l_d) ;
20517                         l_v_attribute8(l_out)   := v_wljdi_attribute8(l_d) ;
20518                         l_v_attribute9(l_out)   := v_wljdi_attribute9(l_d) ;
20519                         l_v_attribute10(l_out) := v_wljdi_attribute10(l_d);
20520                         l_v_attribute11(l_out) := v_wljdi_attribute11(l_d);
20521                         l_v_attribute12(l_out) := v_wljdi_attribute12(l_d);
20522                         l_v_attribute13(l_out) := v_wljdi_attribute13(l_d);
20523                         l_v_attribute14(l_out) := v_wljdi_attribute14(l_d);
20524                         l_v_attribute15(l_out) := v_wljdi_attribute15(l_d);
20525                         l_out := l_out +1;
20526                         l_in  := l_v_comp.next(l_in);
20527                     end loop;
20528 
20529                     if(l_sub_type = WSM_SUB_REC) then
20530 l_stmt_num := 560.5;
20531                         -- get the discommended components
20532                         l_v_disco_item_id.delete;
20533                         l_v_disco_comp_seq_id.delete;
20534                         l_v_disco_src_phm_id.delete;
20535                         open c_disco_comp(
20536                             p_wip_entity_id,
20537                             l_rtg_op_seq_num,
20538                             l_item_id,
20539                             l_primary_item_id,
20540                             l_src_phm_item_id);
20541                         fetch c_disco_comp bulk collect into
20542                             l_v_disco_item_id,
20543                             l_v_disco_comp_seq_id,
20544                             l_v_disco_src_phm_id;
20545                         if(c_disco_comp%rowcount > 0) then
20546                             l_in := l_v_disco_item_id.first;
20547                             while(l_in IS NOT NULL)
20548                             loop
20549                                 l_v_rtg_op_seq(l_out)       := l_rtg_op_seq_num;
20550                                 l_v_comp_item_id(l_out)     := l_v_disco_item_id(l_in);
20551                                 l_v_comp_prm_item_id(l_out) := l_primary_item_id;
20552                                 l_v_comp_phm_item_id(l_out) := l_v_disco_src_phm_id(l_in);
20553                                 l_v_comp_comp_seq_id(l_out) := l_v_disco_comp_seq_id(l_in);
20554                                 l_v_comp_yield(l_out)       := NULL;
20555                                 l_v_comp_qpa(l_out)         := NULL; -- ADD bug 3347985
20556                                 l_v_comp_reqd_date(l_out)   := NULL;
20557                                 l_v_recoflag(l_out)         := 'N';
20558                                 l_v_attribute_category(l_out) :=NULL;
20559                                 l_v_attribute1(l_out)   := NULL;
20560                                 l_v_attribute2(l_out)   := NULL;
20561                                 l_v_attribute3(l_out)   := NULL;
20562                                 l_v_attribute4(l_out)   := NULL;
20563                                 l_v_attribute5(l_out)   := NULL;
20564                                 l_v_attribute6(l_out)   := NULL;
20565                                 l_v_attribute7(l_out)   := NULL;
20566                                 l_v_attribute8(l_out)   := NULL;
20567                                 l_v_attribute9(l_out)   := NULL;
20568                                 l_v_attribute10(l_out) := NULL;
20569                                 l_v_attribute11(l_out) := NULL;
20570                                 l_v_attribute12(l_out) := NULL;
20571                                 l_v_attribute13(l_out) := NULL;
20572                                 l_v_attribute14(l_out) := NULL;
20573                                 l_v_attribute15(l_out) := NULL;
20574                                 l_out := l_out +1;
20575                                 l_in := l_v_disco_item_id.next(l_in);
20576                             end loop;
20577                         end if;
20578                         close c_disco_comp;
20579                     end if;  -- l_sub_type = WSM_SUB_REC
20580                 end if;     -- l_rtg_op_seq_num IS NOT NULL
20581                 -- }} Update WSM_COPY table
20582 
20583             end if; -- l_sub_type in (WSM_SUB_CHG, WSM_SUB_REC)
20584 
20585             l_d := v_wljdi_record_id.next(l_d);
20586         end loop;
20587 
20588 l_stmt_num := 570;
20589         -- Update WCRO
20590         if(l_v_rtg_op_seq.first IS NOT NULL) then
20591             forall i in l_v_rtg_op_seq.first..l_v_rtg_op_seq.last
20592 
20593                 -- Note: wcro.source_phantom_id is a NOT NULL column now,
20594                 --       l_v_comp_phm_item_id(i) can be null or -1,
20595                 --       should treate them as the same,
20596 
20597                 update  wsm_copy_requirement_ops
20598                 set     reco_date_required      = NVL(l_v_comp_reqd_date(i), reco_date_required),
20599                         component_yield_factor  = nvl(l_v_comp_yield(i), component_yield_factor),
20600                         quantity_per_assembly   = nvl(l_v_comp_qpa(i), quantity_per_assembly), -- ADD bug 3347985
20601                         recommended             = NVL(l_v_recoflag(i), recommended),
20602                         last_update_date        = p_last_updt_date,
20603                         last_updated_by         = p_last_updt_by,
20604                         last_update_login       = p_last_updt_login,
20605                         request_id              = p_request_id,
20606                         program_application_id  = p_program_appl_id,
20607                         program_id              = p_program_id,
20608                         program_update_date     = p_prog_updt_date,
20609                         attribute_category = nvl(attribute_category,l_v_attribute_category(i)),
20610                         attribute1 = nvl(attribute1,l_v_attribute1(i)),
20611                         attribute2 = nvl(attribute2,l_v_attribute2(i)),
20612                         attribute3 = nvl(attribute3,l_v_attribute3(i)),
20613                         attribute4 = nvl(attribute4,l_v_attribute4(i)),
20614                         attribute5 = nvl(attribute5,l_v_attribute5(i)),
20615                         attribute6 = nvl(attribute6,l_v_attribute6(i)),
20616                         attribute7 = nvl(attribute7,l_v_attribute7(i)),
20617                         attribute8 = nvl(attribute8,l_v_attribute8(i)),
20618                         attribute9 = nvl(attribute9,l_v_attribute9(i)),
20619                         attribute10 = nvl(attribute10,l_v_attribute10(i)),
20620                         attribute11 = nvl(attribute11,l_v_attribute11(i)),
20621                         attribute12 = nvl(attribute12,l_v_attribute12(i)),
20622                         attribute13 = nvl(attribute13,l_v_attribute13(i)),
20623                         attribute14 = nvl(attribute14,l_v_attribute14(i)),
20624                         attribute15 = nvl(attribute15,l_v_attribute15(i))
20625                 where   wip_entity_id         = p_wip_entity_id
20626                 and     operation_seq_num     = l_v_rtg_op_seq(i)
20627                 and     component_item_id     = l_v_comp_item_id(i)
20628                 and     primary_component_id  = l_v_comp_prm_item_id(i)
20629                 --and   nvl(source_phantom_id, -1)
20630                 --          = nvl(nvl(l_v_comp_phm_item_id(i), source_phantom_id), -1)
20631                 and     source_phantom_id = nvl(l_v_comp_phm_item_id(i), -1)
20632                 and     component_sequence_id = decode(l_v_comp_phm_item_id(i),
20633                                                        null, component_sequence_id,
20634                                                        -1, component_sequence_id,
20635                                                        l_v_comp_comp_seq_id(i));
20636         end if;
20637         if lbji_debug = 'Y' then
20638             fnd_file.put_line(fnd_file.log, 'Mark recommended operation components ... OK.');
20639         end if;
20640 
20641     end if;
20642     ------------------------------------------------
20643     -- }} validete component
20644     ------------------------------------------------
20645 
20646 
20647 EXCEPTION
20648     when e_invalid_recommend_path then
20649         ROLLBACK to start_import_details;
20650         fnd_message.set_name('WSM','WSM_INVALID_RECO_LINK');
20651         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20652                     || fnd_message.get;
20653         p_error_code := -1;
20654         fnd_file.put_line(fnd_file.log,
20655             substr('import_lot_job_details: (#'||l_stmt_num||') '
20656                    || p_error_msg, 1, 2000));
20657 
20658     when e_invalid_discommend_path then
20659         ROLLBACK to start_import_details;
20660         fnd_message.set_name('WSM','WSM_INVALID_DISCO_LINK');
20661         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20662                        || fnd_message.get;
20663         p_error_code := -1;
20664         fnd_file.put_line(fnd_file.log,
20665             substr('import_lot_job_details: (#'||l_stmt_num||') '
20666                    || p_error_msg, 1, 2000));
20667 
20668 
20669     when e_invalid_sub_load_type then
20670         ROLLBACK to start_import_details;
20671         fnd_message.set_name('WSM','WSM_INVALID_FIELD');
20672         fnd_message.set_token('FLD_NAME', 'load_type and substitution_type');
20673         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20674                        || fnd_message.get;
20675         p_error_code := -1;
20676         fnd_file.put_line(fnd_file.log,
20677             substr('import_lot_job_details: (#'||l_stmt_num||') '
20678                    || p_error_msg, 1, 2000));
20679 
20680     when e_invalid_op_seq_num then
20681         ROLLBACK to start_import_details;
20682         fnd_message.set_name('WSM','WSM_NULL_FIELD');
20683         fnd_message.set_token('FLD_NAME', 'job_op_seq_num and routing_op_seq_num');
20684         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20685                        || fnd_message.get;
20686         p_error_code := -1;
20687         fnd_file.put_line(fnd_file.log,
20688             substr('import_lot_job_details: (#'||l_stmt_num||') '
20689                    || p_error_msg, 1, 2000));
20690 
20691     when e_invalid_resource_name then
20692         ROLLBACK to start_import_details;
20693         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20694                     || 'Error: Cannot get resource_id from resource name';
20695         p_error_code := -1;
20696         fnd_file.put_line(fnd_file.log,
20697             substr('import_lot_job_details: (#'||l_stmt_num||') '
20698                    || p_error_msg, 1, 2000));
20699 
20700     when e_null_comp_seq_id then
20701         ROLLBACK to start_import_details;
20702         fnd_message.set_name('WSM','WSM_NULL_FIELD');
20703         fnd_message.set_token('FLD_NAME', 'component_seq_id');
20704         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20705                        || fnd_message.get;
20706         p_error_code := -1;
20707         fnd_file.put_line(fnd_file.log,
20708             substr('import_lot_job_details: (#'||l_stmt_num||') '
20709                    || p_error_msg, 1, 2000));
20710 
20711     when e_invalid_item_name then
20712         ROLLBACK to start_import_details;
20713         p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20714                        || 'Error: Cannot get item_id from item name';
20715         p_error_code := -1;
20716         fnd_file.put_line(fnd_file.log,
20717             substr('import_lot_job_details: (#'||l_stmt_num||') '
20718                    || p_error_msg, 1, 2000));
20719 
20720     when e_wljdi_common_error then
20721         ROLLBACK to start_import_details;
20722         -- p_error_code/p_error_msg was set
20723         if(l_d IS NOT NULL) then
20724             p_error_msg := 'WLJDI(record_id =' || v_wljdi_record_id(l_d) || ') '
20725                            || p_error_msg;
20726         end if;
20727         fnd_file.put_line(fnd_file.log,
20728             substr('import_lot_job_details: (#'||l_stmt_num||') '
20729                    || p_error_msg, 1, 2000));
20730 
20731     when others then
20732         ROLLBACK to start_import_details;
20733         p_error_code := SQLCODE;
20734         p_error_msg := substr('import_lot_job_details: (#'||l_stmt_num||') '
20735                               ||SQLERRM, 1, 2000);
20736         fnd_file.put_line(fnd_file.log, p_error_msg);
20737 END import_lot_job_details;
20738 
20739 
20740 
20741 
20742 -- **********************************************************************************************
20743 -- PROCEDURE load_wsli_data
20744 -- **********************************************************************************************
20745 
20746 PROCEDURE load_wsli_data (p_group_id IN NUMBER) IS
20747 
20748 -- ==============================================================================================
20749 -- cursors used to bulk bind data from wsli to PL/SQL tables
20750 -- ==============================================================================================
20751 cursor c_wsli_1 is
20752 select
20753         wsli.header_id,
20754         wsli.lot_number,
20755         wsli.inventory_item_id,
20756         wsli.organization_id,
20757         wsli.quantity,
20758         wsli.subinventory_code,
20759         wsli.locator_id,
20760         wsli.revision,
20761         wsli.last_updated_by,
20762         wsli.created_by,
20763         msi.primary_uom_code,
20764         wsli.component_issue_quantity
20765 from    wsm_starting_lots_interface wsli,
20766         wsm_lot_job_interface wlji,
20767         mtl_system_items msi
20768 where   wsli.header_id = wlji.source_line_id
20769 and     wlji.group_id = p_group_id
20770 and     wlji.process_status = 2 -- WIP_CONSTANTS.running
20771 and     wlji.mode_flag = 2
20772 and     msi.inventory_item_id = wsli.inventory_item_id
20773 and     msi.organization_id = wsli.organization_id;
20774 
20775 BEGIN
20776 
20777 -- ==============================================================================================
20778 -- bulk fetching data from wsli to PL/SQL tables
20779 -- ==============================================================================================
20780     open c_wsli_1;
20781     fetch c_wsli_1 bulk collect into
20782         v_wsli_hdr_id,
20783         v_wsli_lot_number,
20784         v_wsli_inventory_item_id,
20785         v_wsli_organization_id,
20786         v_wsli_quantity,
20787         v_wsli_subinventory_code,
20788         v_wsli_locator_id,
20789         v_wsli_revision,
20790         v_wsli_last_updated_by,
20791         v_wsli_created_by,
20792         v_wsli_primary_uom_code,
20793         v_wsli_comp_issue_qty;
20794 
20795 -- ==============================================================================================
20796 -- transfering wsli data into index by PL/SQL table for ease of validation
20797 -- ==============================================================================================
20798     v_wsli_idx := v_wsli_hdr_id.first;
20799     while v_wsli_idx <= v_wsli_hdr_id.last
20800     loop
20801         v_wsli(v_wsli_hdr_id(v_wsli_idx)).lot_number          := v_wsli_lot_number(v_wsli_idx);
20802         v_wsli(v_wsli_hdr_id(v_wsli_idx)).inventory_item_id   := v_wsli_inventory_item_id(v_wsli_idx);
20803         v_wsli(v_wsli_hdr_id(v_wsli_idx)).organization_id     := v_wsli_organization_id(v_wsli_idx);
20804         v_wsli(v_wsli_hdr_id(v_wsli_idx)).quantity            := v_wsli_quantity(v_wsli_idx);
20805         v_wsli(v_wsli_hdr_id(v_wsli_idx)).subinventory_code   := v_wsli_subinventory_code(v_wsli_idx);
20806         v_wsli(v_wsli_hdr_id(v_wsli_idx)).locator_id          := v_wsli_locator_id(v_wsli_idx);
20807         v_wsli(v_wsli_hdr_id(v_wsli_idx)).revision            := v_wsli_revision(v_wsli_idx);
20808         v_wsli(v_wsli_hdr_id(v_wsli_idx)).last_updated_by     := v_wsli_last_updated_by(v_wsli_idx);
20809         v_wsli(v_wsli_hdr_id(v_wsli_idx)).created_by          := v_wsli_created_by(v_wsli_idx);
20810         v_wsli(v_wsli_hdr_id(v_wsli_idx)).primary_uom_code    := v_wsli_primary_uom_code(v_wsli_idx);
20811         v_wsli(v_wsli_hdr_id(v_wsli_idx)).comp_issue_quantity := v_wsli_comp_issue_qty(v_wsli_idx);
20812 
20813         v_wsli_idx := v_wsli_hdr_id.next(v_wsli_idx);
20814     end loop;
20815 
20816     close c_wsli_1;
20817 
20818 END load_wsli_data;
20819 
20820 
20821 
20822 -- **********************************************************************************************
20823 -- PROCEDURE check_errored_mmtt_records
20824 -- **********************************************************************************************
20825 
20826 PROCEDURE check_errored_mmtt_records (
20827         p_header_id         in number,
20828         x_err_code          out nocopy number,
20829         x_err_msg           out nocopy varchar2) is
20830 l_transaction_temp_id   NUMBER;
20831 l_wsli_header_id        NUMBER;
20832 l_wlji_header_id        NUMBER;
20833 l_interface_id          NUMBER;
20834 l_err_code              VARCHAR2(240);
20835 l_err_explanation       VARCHAR2(240);
20836 l_stmt_num              NUMBER;
20837 
20838 cursor recs is
20839         select  mmtt.transaction_temp_id,
20840                 mmtt.error_code,
20841                 mmtt.error_explanation,
20842                 wsli.header_id,
20843                 wlji.header_id
20844         from    mtl_material_transactions_temp mmtt,
20845                 wsm_starting_lots_interface wsli,
20846                 wsm_lot_job_interface wlji
20847         where   mmtt.transaction_header_id = p_header_id
20848         and     mmtt.source_line_id = wsli.header_id
20849         and     wsli.header_id = wlji.source_line_id;
20850 BEGIN
20851 
20852     open recs;
20853     loop
20854         fetch recs
20855         into    l_transaction_temp_id,
20856                 l_err_code,
20857                 l_err_explanation,
20858                 l_wsli_header_id,
20859                 l_wlji_header_id;
20860 
20861         exit when recs%notfound;
20862 
20863         update  wsm_lot_job_interface wljia
20864         set     wljia.process_status = 4,
20865                 wljia.error_code = -2,
20866                 wljia.error_msg = substr(l_err_explanation,1,240)
20867         where   wljia.header_id = l_wlji_header_id;
20868 
20869         x_err_code := -2;
20870     close recs;
20871     END LOOP;
20872 
20873 EXCEPTION
20874 
20875     when others then
20876         x_err_code := SQLCODE;
20877         x_err_msg := 'check_errored_mmtt_records' ||
20878                      '(#'||l_stmt_num||'), (Header_Id=' || l_wlji_header_id||') '
20879                      || SUBSTRB(SQLERRM,1,1000);
20880 END check_errored_mmtt_records;
20881 
20882 
20883 
20884 
20885 
20886 
20887 -- **********************************************************************************************
20888 -- FUNCTION DISCRETE_CHARGES_EXIST
20889 -- **********************************************************************************************
20890 
20891 
20892 FUNCTION discrete_charges_exist(
20893         p_wip_entity_id         in  number,
20894         p_organization_id       in  number,
20895         p_check_mode            in  number )
20896 return boolean is
20897 
20898 retnvalue       boolean;
20899 charges_exist   varchar2(2);
20900 l_stmt_num      number;
20901 
20902 cursor check_discrete_charges is
20903         select  distinct 'x'
20904         from    wip_discrete_jobs   wdj,
20905                 wip_period_balances wpb
20906         where   wdj.wip_entity_id = wpb.wip_entity_id
20907                 and wdj.organization_id = wpb.organization_id
20908                 and wdj.wip_entity_id = p_wip_entity_id
20909                 and wdj.organization_id = p_organization_id
20910                 and (  wdj.quantity_completed <> 0
20911                     or wdj.quantity_scrapped <> 0
20912                     or wpb.tl_resource_in <> 0
20913                     or wpb.tl_overhead_in <> 0
20914                     or wpb.tl_outside_processing_in <> 0
20915                     or wpb.pl_material_in <> 0
20916                     or wpb.pl_material_overhead_in <> 0
20917                     or wpb.pl_resource_in <> 0
20918                     or wpb.pl_overhead_in <> 0
20919                     or wpb.pl_outside_processing_in <> 0
20920                     or wpb.tl_material_out <> 0
20921                     or wpb.tl_resource_out <> 0
20922                     or wpb.tl_overhead_out <> 0
20923                     or wpb.tl_outside_processing_out <> 0
20924                     or wpb.pl_material_out <> 0
20925                     or wpb.pl_material_overhead_out <> 0
20926                     or wpb.pl_resource_out <> 0
20927                     or wpb.pl_overhead_out <> 0
20928                     or wpb.pl_outside_processing_out <> 0
20929                     or exists (
20930                         select 'x'
20931                         from   wip_requirement_operations
20932                         where  organization_id = p_organization_id
20933                         and    wip_entity_id = p_wip_entity_id
20934                         and    quantity_issued <> 0)
20935                     or exists (
20936                         select 'x'
20937                         from   wip_move_txn_interface
20938                         where  organization_id = p_organization_id
20939                         and    wip_entity_id = p_wip_entity_id)
20940                     or exists (
20941                         select 'x'
20942                         from   wsm_lot_move_txn_interface
20943                         where  organization_id = p_organization_id
20944                         and    wip_entity_id = p_wip_entity_id)
20945                     or exists (
20946                         select 'x'
20947                         from   wip_cost_txn_interface
20948                         where  organization_id = p_organization_id
20949                         and    wip_entity_id = p_wip_entity_id)
20950                     or exists (
20951                         select 'x'
20952                         from   mtl_material_transactions_temp
20953                         where  organization_id = p_organization_id
20954                         and    transaction_source_type_id = 5
20955                         and    transaction_source_id = p_wip_entity_id)
20956                     or exists (
20957                         select 'x'
20958                         from   wip_move_transactions
20959                         where  organization_id = p_organization_id
20960                         and    wip_entity_id = p_wip_entity_id)
20961                     /* ST bug fix 3493984 : commented this code */
20962             /*or exists (
20963                         select 'x'
20964                         from   wsm_sm_resulting_jobs
20965                         where  organization_id = p_organization_id
20966                         and    wip_entity_id = p_wip_entity_id) */
20967             /* ST bug fix 3493984 : Added code to consider only successful WIP lot transactions */
20968             or exists (
20969                 select 'x'
20970                         from  wsm_sm_resulting_jobs wsrj
20971                         where  wsrj.organization_id = p_organization_id
20972                         and    wsrj.wip_entity_id = p_wip_entity_id
20973             and    exists (select transaction_id
20974                        from wsm_split_merge_transactions wsmt
20975                        where wsmt.transaction_id = wsrj.transaction_id
20976                        and wsmt.status = 4 )
20977                 )
20978            /* ST bug fix 3493984 : end */
20979            or exists (
20980                         select 'x'
20981                         from   wip_operation_resources
20982                         where  organization_id = p_organization_id
20983                         and    wip_entity_id = p_wip_entity_id
20984                         and    applied_resource_units <> 0)
20985 		   /* for bug fix 16327585 */
20986 		   or exists (
20987 		                select 'x'
20988 						from mtl_material_transactions
20989 						where  organization_id = p_organization_id
20990 						and    transaction_source_type_id = 5
20991 						and    transaction_source_id = p_wip_entity_id
20992 						and    costed_flag in ('N', 'E')));
20993 
20994 
20995 --check for only shop floor transactions
20996 
20997 cursor check_discrete_charges_1 is
20998         select  distinct 'x'
20999         from    wip_discrete_jobs   wdj,
21000                 wip_period_balances wpb
21001         where   wdj.wip_entity_id = wpb.wip_entity_id
21002                 and wdj.organization_id = wpb.organization_id
21003                 and wdj.wip_entity_id = p_wip_entity_id
21004                 and wdj.organization_id = p_organization_id
21005                 and (  wdj.quantity_completed <> 0
21006                     or wdj.quantity_scrapped <> 0
21007                     or wpb.tl_resource_in <> 0
21008                     or wpb.tl_overhead_in <> 0
21009                     or wpb.tl_outside_processing_in <> 0
21010                     or wpb.pl_resource_in <> 0
21011                     or wpb.pl_overhead_in <> 0
21012                     or wpb.pl_outside_processing_in <> 0
21013                     or wpb.tl_resource_out <> 0
21014                     or wpb.tl_overhead_out <> 0
21015                     or wpb.tl_outside_processing_out <> 0
21016                     or wpb.pl_resource_out <> 0
21017                     or wpb.pl_overhead_out <> 0
21018                     or wpb.pl_outside_processing_out <> 0
21019                     or exists (
21020                         select 'x'
21021                         from   wip_move_txn_interface
21022                         where  organization_id = p_organization_id
21023                         and    wip_entity_id = p_wip_entity_id)
21024                     or exists (
21025                         select 'x'
21026                         from   wsm_lot_move_txn_interface
21027                         where  organization_id = p_organization_id
21028                         and    wip_entity_id = p_wip_entity_id)
21029                     or exists (
21030                         select 'x'
21031                         from   wip_cost_txn_interface
21032                         where  organization_id = p_organization_id
21033                         and    wip_entity_id = p_wip_entity_id)
21034                     or exists (
21035                         select 'x'
21036                         from   wip_move_transactions
21037                         where  organization_id = p_organization_id
21038                         and    wip_entity_id = p_wip_entity_id)
21039                     or exists (
21040                         select 'x'
21041                         from   wsm_sm_resulting_jobs
21042                         where  organization_id = p_organization_id
21043                         and    wip_entity_id = p_wip_entity_id)
21044                     or exists (
21045                         select 'x'
21046                         from   wip_operation_resources
21047                         where  organization_id = p_organization_id
21048                         and    wip_entity_id = p_wip_entity_id
21049                         and    applied_resource_units <> 0));
21050 BEGIN
21051 
21052     retnvalue := FALSE;
21053 
21054     if (p_check_mode = 1) then
21055         open check_discrete_charges_1;
21056         fetch check_discrete_charges_1 into charges_exist;
21057 
21058         IF (check_discrete_charges_1%FOUND) THEN
21059             retnvalue := TRUE;
21060             close  check_discrete_charges_1;
21061             RETURN retnvalue;
21062         ELSE
21063             close  check_discrete_charges_1;
21064         END IF;
21065 
21066     else
21067         open check_discrete_charges;
21068         fetch check_discrete_charges into charges_exist;
21069 
21070         IF (check_discrete_charges%FOUND) THEN
21071             retnvalue := TRUE;
21072             close  check_discrete_charges;
21073             RETURN retnvalue;
21074         ELSE
21075             close  check_discrete_charges;
21076         END IF;
21077 
21078     end if;
21079 
21080     RETURN retnvalue;
21081 
21082 END discrete_charges_exist;
21083 
21084 
21085 
21086 -- **********************************************************************************************
21087 -- PROCEDURE insert_into_period_balances
21088 -- **********************************************************************************************
21089 
21090 PROCEDURE insert_into_period_balances (
21091         p_wip_entity_id         in number,
21092         p_organization_id       in number,
21093         p_class_code            in varchar2,
21094         p_release_date          in date,
21095         p_error_code            out nocopy number,
21096         p_err_msg               out nocopy varchar2 ) is
21097 
21098 x_user_id       number := fnd_global.user_id;
21099 x_login_id      number := fnd_global.login_id;
21100 --l_inv_period_id number;   -- BUG3126650
21101 l_cnt           number;     -- bug 3571360
21102 
21103 BEGIN
21104 
21105     -- BD: bugfix 3299811, this is a regression of bugfix 3126650
21106     --l_inv_period_id := wsmputil.get_inv_acct_period (
21107     --        x_err_code         => p_error_code,
21108     --        x_err_msg          => p_err_msg,
21109     --        p_organization_id  => p_organization_id,
21110     --        p_date             => trunc(nvl(p_release_date, sysdate)) );
21111     --if(p_error_code <> 0) then
21112     --    p_error_code := -1;
21113     --    fnd_message.set_name('WIP', 'WIP_NO_ACCT_PERIOD');
21114     --    fnd_message.set_token('FLD_NAME','Wip Accounting Period');
21115     --    p_err_msg := fnd_message.get;
21116     --    return;
21117     --end if;
21118     -- ED: bugfix 3299811
21119 
21120     insert into wip_period_balances (
21121             acct_period_id,
21122             wip_entity_id,
21123             last_update_date,
21124             last_updated_by,
21125             creation_date,
21126             created_by,
21127             last_update_login,
21128             organization_id,
21129             class_type,
21130             tl_resource_in,
21131             tl_overhead_in,
21132             tl_outside_processing_in,
21133             pl_material_in,
21134             pl_material_overhead_in,
21135             pl_resource_in,
21136             pl_overhead_in,
21137             pl_outside_processing_in,
21138             tl_material_out,
21139             tl_resource_out,
21140             tl_overhead_out,
21141             tl_outside_processing_out,
21142             pl_material_out,
21143             pl_material_overhead_out,
21144             pl_resource_out,
21145             pl_overhead_out,
21146             pl_outside_processing_out,
21147             pl_material_overhead_var,
21148             pl_material_var,
21149             pl_outside_processing_var,
21150             pl_overhead_var,
21151             pl_resource_var,
21152             tl_material_var,
21153             tl_outside_processing_var,
21154             tl_overhead_var,
21155             tl_resource_var,
21156             tl_material_overhead_out,
21157             tl_material_overhead_var)
21158     select  oap.acct_period_id,
21159             p_wip_entity_id,
21160             sysdate, x_user_id,
21161             sysdate, x_user_id, x_login_id,
21162             p_organization_id, wc.class_type,
21163             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21164             0, 0, 0, 0, 0, 0, 0, 0
21165      from   org_acct_periods oap,
21166             wip_accounting_classes wc
21167      where  wc.class_code = p_class_code
21168      and    wc.organization_id = p_organization_id
21169      and    oap.organization_id = p_organization_id
21170      and    oap.schedule_close_date >=
21171                  trunc(inv_le_timezone_pub.get_le_day_for_inv_org(
21172                         nvl(p_release_date, sysdate),
21173                         p_organization_id))
21174      and    oap.period_close_date is null
21175      and    not exists (
21176                  select 'balance record already there'
21177                  from   wip_period_balances wpb
21178                  where  wpb.wip_entity_id = p_wip_entity_id
21179                  and    wpb.acct_period_id = oap.acct_period_id
21180                  and    wpb.organization_id = oap.organization_id);
21181 
21182     l_cnt := SQL%ROWCOUNT;      -- bug 3571360
21183 
21184     -- BD: bugfix 3299811, this is a regression of bugfix 3126650
21185     --select  l_inv_period_id,
21186     --        p_wip_entity_id,
21187     --        sysdate, x_user_id,
21188     --        sysdate, x_user_id, x_login_id,
21189     --        p_organization_id,
21190     --        wc.class_type,
21191     --        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21192     --        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21193     --        0, 0, 0, 0, 0, 0, 0, 0
21194     --from    wip_accounting_classes wc
21195     --where   wc.class_code = p_class_code
21196     --and     wc.organization_id = p_organization_id
21197     --and     not exists (
21198     --             select 'balance record already there'
21199     --             from   wip_period_balances wpb
21200     --             where  wpb.wip_entity_id = p_wip_entity_id
21201     --             and    wpb.acct_period_id = l_inv_period_id
21202     --             and    wpb.organization_id = p_organization_id);
21203     -- ED: bugfix 3299811
21204 
21205 
21206     if lbji_debug = 'Y' then
21207         fnd_file.put_line(fnd_file.log, 'Inserted '||l_cnt||' rows into wip_period_balances');
21208     end if;
21209 
21210     --if SQL%NOTFOUND then      -- bug 3571360
21211     if l_cnt <= 0 then          -- bug 3571360
21212         p_error_code := -1;
21213         fnd_message.set_name('WIP', 'WIP_NO_ACCT_PERIOD');
21214         fnd_message.set_token('FLD_NAME','Wip Accounting Period');
21215         p_err_msg := fnd_message.get;
21216     end if;
21217 
21218 exception
21219     when others then
21220         p_err_msg := 'insert_into_period_balances: '|| substr(SQLERRM,1,200);
21221         p_error_code := SQLCODE;
21222 
21223 end insert_into_period_balances;
21224 
21225 
21226 
21227 -- **********************************************************************************************
21228 -- PROCEDURE build_job_detail_info (overloaded)
21229 -- **********************************************************************************************
21230 
21231 procedure build_job_detail_info(
21232         p_common_routing_sequence_id    in number,
21233         p_common_bill_sequence_id       in number,
21234         p_status_type                   in number,
21235         p_org_id                        in number,
21236         p_wip_entity_id                 in number,
21237         p_last_updt_date                in date,
21238         p_last_updt_by                  in number,
21239         p_creation_date                 in date,
21240         p_created_by                    in number,
21241         p_last_updt_login               in number,
21242         p_request_id                    in number,
21243         p_program_appl_id               in number,
21244         p_program_id                    in number,
21245         p_prog_updt_date                in date,
21246         p_item                          in number,      -- not used
21247         p_job_type                      in number,
21248         p_bom_reference_id              in number,      -- not used
21249         p_routing_reference_id          in number,      -- not used
21250         p_wip_supply_type               in number,
21251         p_job_scheduled_start_date      in date,        -- not used
21252         p_job_scheduled_compl_date      in date,        -- not used
21253         p_start_quantity                in number,
21254         p_bom_revision_date             in date,
21255         p_routing_revision_date         in date,
21256         p_alt_bom_designator            in varchar2,
21257         p_alt_routing_designator        in varchar2,
21258         p_rtg_op_seq_num                in number,
21259         p_error_code                    out nocopy number,
21260         p_error_msg                     out nocopy varchar2,
21261         p_src_client_server             in number,      -- bug 3311985 new parameter
21262         p_po_creation_time              in number) IS   -- bug 3311985 new parameter
21263 
21264 l_stmt_num              number;
21265 l_job_op_seq_num        number;
21266 l_rtg_op_seq_num0       number;
21267 l_rtg_op_seq_num        number;
21268 l_start_op_seq_num      number;
21269 l_start_op_seq_id       number;
21270 l_op_seq_id             number;
21271 l_temp_num              number;
21272 l_max_op_seq_num        number;
21273 e_build_job_dtl         exception;
21274 
21275 cursor the_path_cur is (
21276     select     from_op_seq_num,
21277                from_op_seq_id,
21278                level
21279     from       wsm_copy_op_networks
21280     start with (to_op_seq_num = l_rtg_op_seq_num0 and
21281                 recommended = 'Y' and
21282                 wip_entity_id = p_wip_entity_id)
21283     connect by (to_op_seq_num = prior from_op_seq_num and
21284                 recommended = 'Y' and
21285                 wip_entity_id = p_wip_entity_id)
21286     union
21287     select     l_rtg_op_seq_num0 "from_op_seq_num",
21288                l_op_seq_id,
21289                -1
21290     from       dual
21291 ) order by 2 desc;
21292 
21293 BEGIN
21294     p_error_code := 0;
21295     p_error_msg := '';
21296 
21297 l_stmt_num := 10;
21298     -- The following two call should be called when job copy is available
21299     find_copy_routing_start(
21300         p_wip_entity_id,
21301         l_start_op_seq_num,
21302         l_start_op_seq_id,
21303         p_error_code,
21304         p_error_msg);
21305     if p_error_code <> 0 then
21306         raise e_build_job_dtl;
21307     end if;
21308 
21309 l_stmt_num := 20;
21310     if p_rtg_op_seq_num is null then -- first operation
21311         l_rtg_op_seq_num0 := l_start_op_seq_num;
21312     else
21313         l_rtg_op_seq_num0 := p_rtg_op_seq_num;
21314     end if;
21315 
21316     find_copy_op_seq_id (
21317         p_wip_entity_id,
21318         l_rtg_op_seq_num0,
21319         l_op_seq_id,
21320         p_error_code,
21321         p_error_msg);
21322     if p_error_code <> 0 then
21323         raise e_build_job_dtl;
21324     end if;
21325 
21326 l_stmt_num := 30;
21327     -- osp begin
21328     if(l_op_seq_id <> -1) then
21329         if wsmputil.check_po_move (
21330                 p_sequence_id      => l_op_seq_id,
21331                 p_sequence_id_type => 'O',
21332                 p_routing_rev_date => p_routing_revision_date,
21333                 x_err_code         => p_error_code,
21334                 x_err_msg          => p_error_msg)
21335         then
21336             fnd_message.set_name('WSM','WSM_FIRST_OP_PO_MOVE');
21337             p_error_code := -1;
21338             p_error_msg := fnd_message.get;
21339             raise e_build_job_dtl;
21340         end if;
21341     end if;
21342     --osp end
21343 
21344     --l_job_op_seq_num := 0; This initialization has been moved inside the if loop for bug 4647901
21345 
21346     IF v_insert_wip is NULL or v_insert_wip  = 2 THEN
21347 
21348 	l_job_op_seq_num := 0;
21349     --OPTII-PERF: For jobs without phantom requirement,
21350     --WIP Tables are updated while creating the job copies
21351 l_stmt_num := 40;
21352         OPEN the_path_cur;
21353         LOOP
21354             FETCH the_path_cur
21355             INTO  l_rtg_op_seq_num,
21356                   l_op_seq_id,
21357                   l_temp_num;
21358             EXIT when the_path_cur%NOTFOUND;
21359 
21360             l_job_op_seq_num := l_job_op_seq_num + g_op_seq_incr;
21361 
21362 l_stmt_num := 50;
21363             -- will call new procedure to insert WO, WOR, WRO, WOY, WSOR
21364             WSMPOPRN.copy_plan_to_execution(
21365                     x_error_code                => p_error_code,
21366                     x_error_msg                 => p_error_msg,
21367                     p_org_id                    => p_org_id,
21368                     p_wip_entity_id             => p_wip_entity_id,
21369                     p_to_job_op_seq_num         => l_job_op_seq_num,
21370                     p_to_rtg_op_seq_num         => l_rtg_op_seq_num,
21371                     p_to_op_seq_id              => l_op_seq_id,
21372                     p_reco_op_flag              => 'Y',
21373                     p_txn_quantity              => p_start_quantity,
21374                     p_txn_date                  => p_last_updt_date,
21375                     p_user                      => p_last_updt_by,
21376                     p_login                     => p_last_updt_login,
21377                     p_request_id                => p_request_id,
21378                     p_program_application_id    => p_program_appl_id,
21379                     p_program_id                => p_program_id,
21380                     p_dup_val_ignore            => 'N',
21381                     p_start_quantity            => p_start_quantity);
21382 
21383             if p_error_code <> 0 then
21384                 raise e_build_job_dtl;
21385             end if;
21386 
21387         END LOOP;
21388         CLOSE the_path_cur;
21389 
21390         -- {{ Update quantity
21391 l_stmt_num := 80;
21392         select max(operation_seq_num)
21393         into   l_max_op_seq_num
21394         from   wip_operations
21395         where  wip_entity_id = p_wip_entity_id;
21396 
21397 l_stmt_num := 90;
21398         update  wip_operations wo
21399         set     wo.previous_operation_seq_num = (
21400                     select max(operation_seq_num)
21401                     from   wip_operations
21402                     where  wip_entity_id = p_wip_entity_id
21403                     and    operation_seq_num < wo.operation_seq_num),
21404                 wo.next_operation_seq_num = (
21405                     select min(operation_seq_num)
21406                     from   wip_operations
21407                     where  wip_entity_id = p_wip_entity_id
21408                     and    operation_seq_num > wo.operation_seq_num),
21409                 wo.quantity_in_queue =
21410                     decode(operation_seq_num,
21411                            l_max_op_seq_num,
21412                            (decode(p_status_type,
21413                                    3, round(p_start_quantity,
21414                                             wip_constants.max_displayed_precision),
21415                                    0)),
21416                            0)
21417         where   wo.wip_entity_id = p_wip_entity_id;
21418         if lbji_debug = 'Y' then
21419             fnd_file.put_line(fnd_file.log,'Update Quantity, Prev/Next Op in wo(l_stmt_num='
21420                                            ||l_stmt_num||'): '|| SQL%ROWCOUNT);
21421         end if;
21422 
21423 l_stmt_num := 100;
21424         /*This SQL is commneted as per bug 5125991
21425 		update wip_operations
21426         set    quantity_completed = p_start_quantity
21427         where  wip_entity_id = p_wip_entity_id
21428         and    operation_seq_num  < l_max_op_seq_num;
21429         if lbji_debug = 'Y' then
21430             fnd_file.put_line(fnd_file.log,'Update Quantity_Completed in wo(l_stmt_num='
21431                                             ||l_stmt_num||'): '|| SQL%ROWCOUNT);
21432         end if;   */
21433 
21434 l_stmt_num := 110;
21435         update wip_requirement_operations
21436         set    quantity_issued = required_quantity
21437         where  wip_entity_id = p_wip_entity_id
21438         and    operation_seq_num < l_max_op_seq_num
21439         and    wip_supply_type not in (2, 4, 5, 6); -- Fix for bug #2685463
21440         if lbji_debug = 'Y' then
21441             fnd_file.put_line(fnd_file.log,'Update Quantity_Issued in wro(l_stmt_num='
21442                                            ||l_stmt_num||'): '|| SQL%ROWCOUNT);
21443         end if;
21444         -- }} Update quantity
21445         if (p_status_type = 3) then
21446                 update wsm_lot_based_jobs
21447                 set    (current_job_op_seq_num,
21448                         current_rtg_op_seq_num)
21449                        =(select wo.operation_seq_num,
21450                                  wo.wsm_op_seq_num
21451                           from   wip_operations wo
21452                           where  organization_id = p_org_id
21453                           and    wip_entity_id   = p_wip_entity_id
21454                           and    wo.quantity_in_queue
21455                                + wo.quantity_running
21456                                + wo.quantity_waiting_to_move  > 0)
21457                 where  wip_entity_id   = p_wip_entity_id
21458                 and    organization_id = p_org_id;
21459        end if;
21460     END IF;--Check on v_insert_wip
21461             v_insert_wip := NULL;
21462             v_phantom_exists := NULL;
21463 
21464 --OPTII-PERF: Updating current job and routing op seq num is moved inside the
21465 --check on v_insert_wip
21466     --
21467     -- Begin R12Dev Update to set the current_job/rtg_op_seq_num on WLBJ
21468     --
21469     /*
21470     if (p_status_type = 3) then
21471 
21472         update wsm_lot_based_jobs
21473         set    (current_job_op_seq_num,
21474                 current_rtg_op_seq_num)
21475                =(select wo.operation_seq_num,
21476                          wo.wsm_op_seq_num
21477                   from   wip_operations wo
21478                   where  organization_id = p_org_id
21479                   and    wip_entity_id   = p_wip_entity_id
21480                   and    wo.quantity_in_queue
21481                        + wo.quantity_running
21482                        + wo.quantity_waiting_to_move  > 0)
21483         where  wip_entity_id   = p_wip_entity_id
21484         and    organization_id = p_org_id;
21485     end if;
21486 */
21487     --
21488     -- End R12Dev
21489     --
21490 
21491     -- bug 3311985: osp code moved from build_job_header_info
21492     -- osp begin -- this has to be put after insertion into wip_entities
21493     if p_status_type = 3 and p_po_creation_time <> wip_constants.manual_creation then
21494         if p_src_client_server = 1 then
21495 l_stmt_num := 130;
21496             if wsmputil.check_osp_operation(p_wip_entity_id, l_job_op_seq_num, p_org_id) then
21497                 g_num_of_osp_exists := g_num_of_osp_exists + 1;
21498                 wip_osp.create_requisition(
21499                         p_wip_entity_id          => p_wip_entity_id,
21500                         p_organization_id        => p_org_id,
21501                         p_repetitive_schedule_id => null,
21502                         p_operation_seq_num      => l_job_op_seq_num,
21503                         p_resource_seq_num       => null,
21504                         p_run_reqimport          => wip_constants.yes);
21505             end if; -- check_osp_operation
21506         else
21507 l_stmt_num := 140;
21508             -- if build_job_info is called from form, only then P_Run_ReqImport should be YES, i.e.
21509             -- the requisition import concurrent request should be launched immediately.
21510             if wsmputil.check_osp_operation(p_wip_entity_id, l_job_op_seq_num, p_org_id) then
21511                 g_num_of_osp_exists := g_num_of_osp_exists + 1;
21512                 wip_osp.create_requisition(
21513                         p_wip_entity_id          => p_wip_entity_id,
21514                         p_organization_id        => p_org_id,
21515                         p_repetitive_schedule_id => null,
21516                         p_operation_seq_num      => l_job_op_seq_num,
21517                         p_resource_seq_num       => null,
21518                         p_run_reqimport          => wip_constants.no);
21519             end if; -- check_osp_operation
21520         end if; --  p_src_client_server
21521     end if; --  p_status_type = 3
21522     -- osp end
21523 
21524 EXCEPTION
21525     when e_build_job_dtl then
21526         -- p_error_code/p_error_msg was set
21527         fnd_file.put_line(fnd_file.log,
21528             substr('build_job_detail_info (#'||l_stmt_num||') '
21529                    ||p_error_msg, 1, 2000));
21530 
21531     when others then
21532         p_error_code := SQLCODE;
21533         p_error_msg := substr('build_job_detail_info (#'||l_stmt_num||') '
21534                               || SQLERRM, 1, 2000);
21535         fnd_file.put_line(fnd_file.log, substr(p_error_msg, 1, 2000));
21536 END build_job_detail_info;
21537 
21538 
21539 -- **********************************************************************************************
21540 -- PROCEDURE build_job_header_info (overloaded)
21541 -- **********************************************************************************************
21542 
21543 procedure build_job_header_info(
21544         p_common_routing_sequence_id    in number,
21545         p_common_bill_sequence_id       in number,
21546         p_status_type                   in number,
21547         p_class_code                    in varchar2,
21548         p_org_id                        in number,
21549         p_wip_entity_id                 in out nocopy number,
21550         p_last_updt_date                in date,
21551         p_last_updt_by                  in number,
21552         p_creation_date                 in date,
21553         p_created_by                    in number,
21554         p_last_updt_login               in number,
21555         p_request_id                    in number,
21556         p_program_appl_id               in number,
21557         p_program_id                    in number,
21558         p_prog_updt_date                in date,
21559         p_source_line_id                in number,
21560         p_source_code                   in varchar2,
21561         p_description                   in varchar2,
21562         p_item                          in number,
21563         p_job_type                      in number,
21564         p_bom_reference_id              in number,
21565         p_routing_reference_id          in number,
21566         p_firm_planned_flag             in number,
21567         p_wip_supply_type               in number,
21568         p_job_scheduled_start_date      in date,
21569         p_job_scheduled_compl_date      in date,
21570         p_start_quantity                in number,
21571         p_net_quantity                  in number,
21572         p_coproducts_supply             in number,
21573         p_bom_revision                  in varchar2,
21574         p_routing_revision              in varchar2,
21575         p_bom_revision_date             in date,
21576         p_routing_revision_date         in date,
21577         p_lot_number                    in varchar2,
21578         p_alt_bom_designator            in varchar2,
21579         p_alt_routing_designator        in varchar2,
21580         p_priority                      in number,
21581         p_due_date                      in date,
21582         p_attribute_category            in varchar2,
21583         p_attribute1                    in varchar2,
21584         p_attribute2                    in varchar2,
21585         p_attribute3                    in varchar2,
21586         p_attribute4                    in varchar2,
21587         p_attribute5                    in varchar2,
21588         p_attribute6                    in varchar2,
21589         p_attribute7                    in varchar2,
21590         p_attribute8                    in varchar2,
21591         p_attribute9                    in varchar2,
21592         p_attribute10                   in varchar2,
21593         p_attribute11                   in varchar2,
21594         p_attribute12                   in varchar2,
21595         p_attribute13                   in varchar2,
21596         p_attribute14                   in varchar2,
21597         p_attribute15                   in varchar2,
21598         p_job_name                      in varchar2,
21599         p_completion_subinventory       in varchar2,
21600         p_completion_locator_id         in number,
21601         p_demand_class                  in varchar2,
21602         p_project_id                    in number,
21603         p_task_id                       in number,
21604         p_schedule_group_id             in number,
21605         p_build_sequence                in number,
21606         p_line_id                       in number,
21607         p_kanban_card_id                in number,
21608         p_overcompl_tol_type            in number,
21609         p_overcompl_tol_value           in number,
21610         p_end_item_unit_number          in number,
21611         p_src_client_server             in number,
21612         p_po_creation_time              in number,
21613         p_date_released                 in date,
21614         p_wjsq_uom_code                 in tbl_wjsq_uom_code,
21615         p_wjsq_start_quantity           in tbl_wjsq_start_quantity,
21616         p_error_code                    out nocopy number,
21617         p_error_msg                     out nocopy varchar2) is
21618 
21619 
21620 l_stmt_num                      number;
21621 l_material_account              number;
21622 l_material_overhead_account     number;
21623 l_resource_account              number;
21624 l_outside_processing_account    number;
21625 l_material_variance_account     number;
21626 l_resource_variance_account     number;
21627 l_outside_proc_var_acc          number;
21628 l_std_cost_adjustment_account   number;
21629 l_overhead_account              number;
21630 l_overhead_variance_account     number;
21631 l_po_creation_time              number;
21632 l_est_scrap_account             number;
21633 l_est_scrap_var_account         number;
21634 l_job_op_seq_num                number := NULL;
21635 l_return_status                 varchar2(1);
21636 l_translated_meaning            varchar2(240);
21637 
21638 e_build_job                     exception;
21639 
21640 
21641 BEGIN
21642     p_error_code := 0;
21643     p_error_msg := '';
21644 
21645 
21646 l_stmt_num := 100;
21647     -- get a new wip entity id
21648     select wip_entities_s.nextval
21649     into   p_wip_entity_id
21650     from   dual;
21651 
21652 
21653 l_stmt_num := 110;
21654     -- get wip accounting class information
21655     select  wac.material_account,
21656             wac.material_overhead_account,
21657             wac.resource_account,
21658             wac.outside_processing_account,
21659             wac.material_variance_account,
21660             wac.resource_variance_account,
21661             wac.outside_proc_variance_account,
21662             wac.std_cost_adjustment_account,
21663             wac.overhead_account,
21664             wac.overhead_variance_account,
21665             params.po_creation_time,
21666             wac.est_scrap_account,
21667             wac.est_scrap_var_account
21668     into    l_material_account,
21669             l_material_overhead_account,
21670             l_resource_account,
21671             l_outside_processing_account,
21672             l_material_variance_account,
21673             l_resource_variance_account,
21674             l_outside_proc_var_acc,
21675             l_std_cost_adjustment_account,
21676             l_overhead_account,
21677             l_overhead_variance_account,
21678             l_po_creation_time,
21679             l_est_scrap_account,
21680             l_est_scrap_var_account
21681     from    wip_accounting_classes wac,
21682             wip_parameters params
21683     where   wac.class_code(+)= p_class_code
21684     and     wac.organization_id(+)= p_org_id
21685     and     params.organization_id = p_org_id;
21686 
21687 
21688 l_stmt_num := 120;
21689     -- check scrap account
21690     if wsmputil.wsm_esa_enabled(
21691             p_wip_entity_id => null,
21692             err_code        => p_error_code,
21693             err_msg         => p_error_msg,
21694             p_org_id        => p_org_id,
21695             p_job_type      => p_job_type) = 1 and
21696        (l_est_scrap_account is null or l_est_scrap_var_account is null)
21697     then
21698         fnd_message.set_name('WSM','WSM_NO_WAC_SCRAP_ACC');
21699         fnd_message.set_token('CC',p_class_code);
21700         p_error_code := -1;
21701         p_error_msg := fnd_message.get;
21702         raise e_build_job;
21703     end if;
21704 
21705 
21706 l_stmt_num := 130;
21707     -- insert into WDJ
21708     -- Modified for Bug 13073424.
21709     insert into wip_discrete_jobs (
21710         wip_entity_id,
21711         organization_id,
21712         last_update_date,
21713         last_updated_by,
21714         creation_date,
21715         created_by,
21716         last_update_login,
21717         request_id,
21718         program_application_id,
21719         program_id,
21720         program_update_date,
21721         source_line_id,
21722         source_code,
21723         description,
21724         status_type,
21725         date_released,
21726         primary_item_id,
21727         bom_reference_id,
21728         routing_reference_id,
21729         firm_planned_flag,
21730         job_type,
21731         wip_supply_type,
21732         class_code,
21733         material_account,
21734         material_overhead_account,
21735         resource_account,
21736         outside_processing_account,
21737         material_variance_account,
21738         resource_variance_account,
21739         outside_proc_variance_account,
21740         std_cost_adjustment_account,
21741         overhead_account,
21742         overhead_variance_account,
21743         scheduled_start_date,
21744         scheduled_completion_date,
21745         start_quantity,
21746         quantity_completed,
21747         quantity_scrapped,
21748         net_quantity,
21749         common_bom_sequence_id,
21750         common_routing_sequence_id,
21751         bom_revision,
21752         routing_revision,
21753         bom_revision_date,
21754         routing_revision_date,
21755         lot_number,
21756         alternate_bom_designator,
21757         alternate_routing_designator,
21758         completion_subinventory,
21759         completion_locator_id,
21760         demand_class,
21761         project_id,
21762         task_id,
21763         schedule_group_id,
21764         build_sequence,
21765         line_id,
21766         kanban_card_id,
21767         overcompletion_tolerance_type,
21768         overcompletion_tolerance_value,
21769         end_item_unit_number,
21770         po_creation_time,
21771         priority,
21772         due_date,
21773         attribute_category,
21774         attribute1,
21775         attribute2,
21776         attribute3,
21777         attribute4,
21778         attribute5,
21779         attribute6,
21780         attribute7,
21781         attribute8,
21782         attribute9,
21783         attribute10,
21784         attribute11,
21785         attribute12,
21786         attribute13,
21787         attribute14,
21788         attribute15,
21789         est_scrap_account,
21790         est_scrap_var_account,
21791         coproducts_supply
21792     ) values (
21793         p_wip_entity_id,
21794         p_org_id,
21795         p_last_updt_date,
21796         p_last_updt_by,
21797         p_creation_date,
21798         p_created_by,
21799         p_last_updt_login,
21800         p_request_id,
21801         p_program_appl_id,
21802         p_program_id,
21803         p_prog_updt_date,
21804         p_source_line_id,
21805         p_source_code,
21806         p_description,
21807         p_status_type,
21808         -- BC: bug 3382472
21809         --NVL(p_date_released, decode(p_status_type, wip_constants.unreleased, null, sysdate)),
21810         NVL(p_date_released, decode(p_status_type,
21811                                     wip_constants.released, sysdate,
21812                                     null)),
21813         -- EC: bug 3382472
21814         p_item,
21815         decode(p_job_type, 3, p_bom_reference_id, null),
21816         decode(p_job_type, 3, p_routing_reference_id, null),
21817         p_firm_planned_flag,
21818         decode(p_job_type, 3, wip_constants.nonstandard, wip_constants.standard),
21819         p_wip_supply_type,
21820         p_class_code,
21821         l_material_account,
21822         l_material_overhead_account,
21823         l_resource_account,
21824         l_outside_processing_account,
21825         l_material_variance_account,
21826         l_resource_variance_account,
21827         l_outside_proc_var_acc,
21828         l_std_cost_adjustment_account,
21829         l_overhead_account,
21830         l_overhead_variance_account,
21831         trunc(p_job_scheduled_start_date,'mi'),     --round(p_job_scheduled_start_date,'mi'),
21832         trunc(p_job_scheduled_compl_date,'mi'),     --round(p_job_scheduled_compl_date,'mi'),
21833         round(p_start_quantity, wip_constants.max_displayed_precision),
21834         0, -- quantity_completed
21835         0, -- quantity_scrapped
21836         round(p_net_quantity, wip_constants.max_displayed_precision),
21837         p_common_bill_sequence_id,
21838         p_common_routing_sequence_id,
21839         p_bom_revision,
21840         p_routing_revision,
21841         p_bom_revision_date,
21842         p_routing_revision_date,
21843         nvl(p_lot_number,p_job_name),
21844         p_alt_bom_designator,
21845         p_alt_routing_designator,
21846         p_completion_subinventory,
21847         p_completion_locator_id,
21848         p_demand_class,
21849         p_project_id,
21850         p_task_id,
21851         p_schedule_group_id,
21852         p_build_sequence,
21853         p_line_id,
21854         p_kanban_card_id,
21855         p_overcompl_tol_type,
21856         p_overcompl_tol_value,
21857         p_end_item_unit_number,
21858         l_po_creation_time,
21859         p_priority,
21860         p_due_date,
21861         p_attribute_category,
21862         p_attribute1,
21863         p_attribute2,
21864         p_attribute3,
21865         p_attribute4,
21866         p_attribute5,
21867         p_attribute6,
21868         p_attribute7,
21869         p_attribute8,
21870         p_attribute9,
21871         p_attribute10,
21872         p_attribute11,
21873         p_attribute12,
21874         p_attribute13,
21875         p_attribute14,
21876         p_attribute15,
21877         l_est_scrap_account,
21878         l_est_scrap_var_account,
21879         p_coproducts_supply
21880     );
21881     if lbji_debug = 'Y' then
21882         fnd_file.put_line(fnd_file.log, 'Inserted '||SQL%ROWCOUNT||' rows into wdj');
21883     end if;
21884 
21885 
21886 l_stmt_num := 135;
21887     -- insert into WLBJ
21888     if g_skip_wlbj <> 1 THEN --OPTII-PERF: This will be created in create_jobcopies
21889         insert into wsm_lot_based_jobs (
21890         wip_entity_id,
21891         organization_id,
21892         on_rec_path,
21893         internal_copy_type,
21894         last_update_date,
21895         last_updated_by,
21896         creation_date,
21897         created_by,
21898         last_update_login,
21899         request_id,
21900         program_application_id,
21901         program_id,
21902         program_update_date
21903         ) values (
21904         p_wip_entity_id,
21905         p_org_id,
21906         'Y',
21907         1,
21908         p_last_updt_date,
21909         p_last_updt_by,
21910         p_creation_date,
21911         p_created_by,
21912         p_last_updt_login,
21913         p_request_id,
21914         p_program_appl_id,
21915         p_program_id,
21916         p_prog_updt_date
21917         );
21918         if lbji_debug = 'Y' then
21919         fnd_file.put_line(fnd_file.log, 'Inserted '||SQL%ROWCOUNT||' rows into wlbj');
21920         end if;
21921     END IF;--Check on g_skip_wlbj
21922    g_skip_wlbj := 0; --OPTII-PERF
21923 l_stmt_num := 140;
21924     -- insert into WE
21925    /* Added for bug 6861458.*/
21926    begin
21927     insert into wip_entities (
21928         wip_entity_id,
21929         organization_id,
21930         last_update_date,
21931         last_updated_by,
21932         creation_date,
21933         created_by,
21934         last_update_login,
21935         request_id,
21936         program_application_id,
21937         program_id,
21938         program_update_date,
21939         wip_entity_name,
21940         entity_type,
21941         description,
21942         primary_item_id,
21943         gen_object_id
21944     ) values (
21945         p_wip_entity_id,
21946         p_org_id,
21947         p_last_updt_date,
21948         p_last_updt_by,
21949         p_creation_date,
21950         p_created_by,
21951         p_last_updt_login,
21952         p_request_id,
21953         p_program_appl_id,
21954         p_program_id,
21955         p_prog_updt_date,
21956         p_job_name,
21957         5,
21958         p_description,
21959         p_item,
21960         mtl_gen_object_id_s.nextval
21961     );
21962    exception
21963    when others then
21964        fnd_message.set_name('WSM', 'WSM_UNIQUE_LOT_CREATE');
21965        p_error_code := -1;
21966        p_error_msg := fnd_message.get;
21967        raise e_build_job;
21968    end;
21969     if lbji_debug = 'Y' then
21970         fnd_file.put_line(fnd_file.log, 'Inserted '||SQL%ROWCOUNT||' rows into we');
21971     end if;
21972 
21973 
21974 l_stmt_num := 150;
21975     -- call update_card_supply_status to set the status of the kanban card to InProcess,
21976     -- if a card reference exists
21977     if ( p_kanban_card_id is not null ) then
21978         inv_kanban_pvt.update_card_supply_status(
21979                 x_return_status      => l_return_status,
21980                 p_kanban_card_id     => p_kanban_card_id,
21981                 p_supply_status      => inv_kanban_pvt.g_supply_status_InProcess,
21982                 p_document_type      => inv_kanban_pvt.G_Doc_type_lot_job,
21983                 p_document_header_id => p_wip_entity_id,
21984                 p_Document_detail_Id => null,
21985                 p_replenish_quantity => p_start_quantity);
21986 
21987         if ( l_return_status <> fnd_api.g_ret_sts_success ) then
21988             select meaning
21989             into   l_translated_meaning
21990             from   mfg_lookups
21991             where  lookup_type = 'MTL_KANBAN_SUPPLY_STATUS'
21992             and    lookup_code = 5
21993             and    upper(enabled_flag) = 'Y';
21994 
21995             fnd_message.set_name('WSM', 'WSM_KNBN_CARD_STS_FAIL');
21996             fnd_message.set_token('STATUS',l_translated_meaning);
21997             p_error_code := -1;
21998             p_error_msg := fnd_message.get;
21999             raise e_build_job;
22000         end if;
22001     end if;
22002 
22003 l_stmt_num := 160;
22004     if p_status_type = WIP_CONSTANTS.RELEASED then
22005         insert_into_period_balances (
22006                 p_wip_entity_id   => p_wip_entity_id,
22007                 p_organization_id => p_org_id,
22008                 p_class_code      => p_class_code,
22009                 p_release_date    => p_date_released,
22010                 p_error_code      => p_error_code,
22011                 p_err_msg         => p_error_msg );
22012         if p_error_code <> 0 then
22013             raise e_build_job;
22014         end if;
22015     end if;
22016 
22017     -- bug 3311985: osp code moved to build_job_detail_info
22018 
22019 
22020 --
22021 -- Begin R12Dev: for secondary quantity
22022 --
22023 l_stmt_num := 170;
22024     if(p_wjsq_uom_code.COUNT = 0) then -- No secondary quantities are passed
22025         insert INTO WSM_JOB_SECONDARY_QUANTITIES (
22026                WIP_ENTITY_ID,
22027                ORGANIZATION_ID,
22028                UOM_CODE,
22029                START_QUANTITY,
22030                CURRENT_QUANTITY,
22031                CURRENTLY_ACTIVE,
22032                LAST_UPDATE_DATE,
22033                LAST_UPDATED_BY,
22034                LAST_UPDATE_LOGIN,
22035                CREATION_DATE,
22036                CREATED_BY
22037         )
22038         select
22039                p_wip_entity_id,
22040                p_org_id,
22041                wsu.UOM_CODE,
22042                null,
22043                null,
22044                1, -- CURRENTLY_ACTIVE = Yes
22045                p_last_updt_date,
22046                p_last_updt_by,
22047                p_last_updt_login,
22048                p_creation_date,
22049                p_created_by
22050         from   WSM_SECONDARY_UOMS wsu
22051         where  INVENTORY_ITEM_ID = p_item
22052         and    ORGANIZATION_ID   = p_org_id;
22053     else
22054         forall i in p_wjsq_uom_code.first..p_wjsq_uom_code.last
22055             insert into WSM_JOB_SECONDARY_QUANTITIES
22056             (
22057                WIP_ENTITY_ID,
22058                ORGANIZATION_ID,
22059                UOM_CODE,
22060                START_QUANTITY,
22061                CURRENT_QUANTITY,
22062                CURRENTLY_ACTIVE,
22063                LAST_UPDATE_DATE,
22064                LAST_UPDATED_BY,
22065                LAST_UPDATE_LOGIN,
22066                CREATION_DATE,
22067                CREATED_BY
22068             ) values (
22069                p_wip_entity_id,
22070                p_org_id,
22071                p_wjsq_uom_code(i),
22072                p_wjsq_start_quantity(i),
22073                null,
22074                1, -- CURRENTLY_ACTIVE = Yes
22075                p_last_updt_date,
22076                p_last_updt_by,
22077                p_last_updt_login,
22078                p_creation_date,
22079                p_created_by
22080             );
22081     end if;
22082 --
22083 -- End R12Dev: for secondary quantity
22084 --
22085 
22086 EXCEPTION
22087 
22088     when e_build_job then
22089         -- p_error_code and p_error_msg were set
22090         fnd_file.put_line(fnd_file.log,substr('build_job_header_info: (#'||l_stmt_num||') '
22091                           ||p_error_msg, 1, 2000));
22092 
22093 
22094     when others then
22095         p_error_code := SQLCODE;
22096         p_error_msg := substr('build_job_header_info: (#'||l_stmt_num||') '
22097                               ||SQLERRM, 1, 2000);
22098         fnd_file.put_line(fnd_file.log, substr(p_error_msg, 1, 2000));
22099 
22100 END build_job_header_info;
22101 
22102 
22103 END WSM_LBJ_INTERFACE_PVT;