DBA Data[Home] [Help]

PACKAGE BODY: APPS.MSC_CL_PRE_PROCESS

Source


1 PACKAGE BODY MSC_CL_PRE_PROCESS AS -- body
2 /* $Header: MSCCLPPB.pls 120.27.12010000.5 2008/07/10 11:03:56 arusubra ship $ */
3 
4   -- ========= Global Parameters ===========
5 
6    --Instance --
7 
8    v_apps_ver                  	NUMBER;
9    v_supply_id_c                NUMBER ;
10    v_supply_id_n               	NUMBER ;
11    v_supply_id                  NUMBER ;
12    v_source_organization_id    	NUMBER ;
13    v_source_sr_instance_id      NUMBER ;
14    v_discrete_flag              NUMBER:= SYS_NO;
15    v_process_flag               NUMBER:= SYS_NO;
16    --v_debug			BOOLEAN := FALSE;
17    v_req_data                   varchar2(10);
18    v_cal_build                  boolean := False;
19    v_MSC_CONFIGURATION 		VARCHAR2(10) := nvl(fnd_profile.value('MSC_X_CONFIGURATION'), 1);
20    v_my_company                 VARCHAR2(1000) := MSC_ST_UTIL.GET_MY_COMPANY;
21 
22 
23    -- User Environment --
24    -- v_current_date               DATE := sysdate;
25    -- v_current_user               NUMBER;
26    v_applsys_schema             VARCHAR2(32);
27    prec                         MSC_UTIL.CollParamREC;
28 
29    -- Pre Process Program --
30    --v_batch_size               NUMBER := 1000;
31    v_last_collection_id         NUMBER;
32    v_is_complete_refresh        BOOLEAN;
33    v_is_incremental_refresh     BOOLEAN;
34    v_is_so_complete_refresh     BOOLEAN;
35    v_is_so_incremental_refresh  BOOLEAN;
36    v_cp_enabled                 NUMBER;
37    v_recalc_nra                 NUMBER;
38    v_recalc_sh                  NUMBER;
39    v_exchange_mode              NUMBER:= SYS_NO;  -- default to SYS_NO
40    v_so_exchange_mode           NUMBER:= SYS_NO;  -- default to SYS_NO
41    v_monitor_request_id         NUMBER;
42    v_error_no_begin             PLS_INTEGER;
43    v_error_no_end               PLS_INTEGER;
44    v_sce_installed              BOOLEAN;
45 
46    v_res_dmd_enabled            NUMBER;
47    v_mat_dmd_enabled            NUMBER;
48    v_reserve_enabled            NUMBER;
49    v_mat_sup_enabled            NUMBER;
50    v_rtg_enabled                NUMBER;
51    v_bom_enabled                NUMBER;
52    v_resources_enabled          NUMBER;
53    v_sourcing_enabled           NUMBER;
54    v_ctg_enabled                NUMBER;
55    v_sup_cap_enabled            NUMBER;
56    v_safety_stk_enabled         NUMBER;
57    v_item_enabled               NUMBER;
58    v_tp_enabled                 NUMBER;
59    v_dmd_class_enabled          NUMBER;
60    v_uom_enabled                NUMBER;
61    v_desig_enabled              NUMBER;
62    v_project_enabled            NUMBER;
63    v_cal_enabled                NUMBER;
64    v_item_cat_enabled           NUMBER;
65    v_uom_class_enabled          NUMBER;
66    v_ship_mthd_enabled          NUMBER;
67    v_item_cst_enabled           NUMBER;
68    v_parent_request_id          PLS_INTEGER;
69    v_request_id                 PLS_INTEGER; -- added to identify the set of records loaded at a time.
70    v_second_pass                BOOLEAN := FALSE;
71    v_fiscal_cal_enabled         NUMBER;
72    v_comp_cal_enabled           NUMBER;
73    v_setup_enabled              NUMBER;
74    v_item_rollup                NUMBER;
75    v_level_value_enabled        NUMBER;
76    v_level_assoc_enabled        NUMBER;
77    v_booking_enabled            NUMBER;
78    v_shipment_enabled           NUMBER;
79    v_mfg_fct_enabled            NUMBER;
80    v_list_price_enabled         NUMBER;
81    v_cs_data_enabled            NUMBER;
82    v_cs_refresh                 NUMBER;
83    v_curr_conv_enabled          NUMBER;
84    v_uom_conv_enabled           NUMBER;
85    v_comp_users_enabled         NUMBER;
86    v_item_substitute_enabled    NUMBER;
87    v_planners_enabled           NUMBER;
88    v_profile_enabled            NUMBER;
89    v_cal_assignment_enabled     NUMBER;
90     v_iro_enabled               NUMBER;
91     v_ero_enabled               NUMBER;
92   /*  v_cs_batches_launched       NUMBER := 0; S_OP*/
93 
94    --  status --
95    v_warning_flag               NUMBER:= SYS_NO;
96 
97    -- Task Control --
98    v_pipe_task_que              VARCHAR2(32);
99    v_pipe_wm                    VARCHAR2(32);
100    v_pipe_mw                    VARCHAR2(32);
101    v_pipe_status                VARCHAR2(32);
102 
103    -- Misc --
104 
105    PBS                          NUMBER;   -- purge batch size
106    v_sourcing                   NUMBER;   -- sourcing rule flag
107    v_cal_worker                 NumTblTyp:= NumTblTyp(0);-- Calendar Build
108    v_total_worker_num           Number;
109 
110   -- ODP(Oracle Demand Planning) Changes-----------
111   v_install_msd                 BOOLEAN;
112   v_install_msc                 BOOLEAN;
113 -----------------------------
114   -- ========== Declare Local Procedures ==============
115 
116   PROCEDURE LOAD_SUPPLIER_CAPACITY;
117   PROCEDURE LOAD_SAFETY_STOCK;
118   PROCEDURE LOAD_ITEM (p_batch_id  NUMBER);
119   PROCEDURE LOAD_ITEM_SUBSTITUTES; -- Product item substitute
120   PROCEDURE LOAD_CATEGORIES_SOURCING;
121   PROCEDURE LOAD_DEPT_RESOURCES;
122   PROCEDURE LOAD_MATERIAL_SUPPLY(p_batch_id  NUMBER);
123   PROCEDURE LOAD_MATERIAL_DEMAND(p_batch_id  NUMBER);
124   PROCEDURE LOAD_SO_DEMAND(p_batch_id  NUMBER);
125   PROCEDURE LOAD_RESERVATION;
126   PROCEDURE LOAD_BOM_ROUTING;
127   PROCEDURE LOAD_RESOURCE_DEMAND;
128   PROCEDURE LOAD_ITEM_CST;
129 -- Added for the bug fix 2191021
130   PROCEDURE LOAD_ITEM_ABC_CLASSES;
131   PROCEDURE LOAD_COMPANY_USER;
132   PROCEDURE LOAD_ITEM_SUBSTITUTE;
133   PROCEDURE LOAD_CAL_ASSIGNMENTS;
134 
135   -- ========= Declare DP local Procedure ===============
136   PROCEDURE  LOAD_LEVEL_VALUE (p_batch_id   NUMBER );
137   PROCEDURE  LOAD_LEVEL_ASSOCIATION (p_batch_id   NUMBER );
138   PROCEDURE  LOAD_BOOKING_DATA (p_batch_id   NUMBER );
139   PROCEDURE  LOAD_SHIPMENT_DATA (p_batch_id   NUMBER );
140   PROCEDURE  LOAD_MFG_FORECAST (p_batch_id   NUMBER );
141   PROCEDURE  LOAD_PRICE_LIST(p_batch_id   NUMBER );
142   -- PROCEDURE  LOAD_ITEM_LIST_PRICE(p_batch_id   NUMBER );
143   PROCEDURE  LOAD_CS_DATA (p_batch_id NUMBER);
144   PROCEDURE  LOAD_CURR_CONV (p_batch_id   NUMBER );
145   PROCEDURE  LOAD_UOM_CONV (p_batch_id   NUMBER );
146   PROCEDURE  LOAD_SETUP_PARAMETER;
147   PROCEDURE  LOAD_FISCAL_TIME;
148   PROCEDURE  LOAD_COMPOSITE_TIME;
149   PROCEDURE  LOAD_IRO_SUPPLY_DEMAND;
150   PROCEDURE  LOAD_ERO_SUPPLY_DEMAND;
151 
152 
153   --==Send Error/ Warning Email for XML Transactions having Errors/ Warnings ==
154 
155 /*
156   PROCEDURE SEND_ERROR
157   IS
158 	TYPE MailCurTyp IS REF CURSOR;
159 	c_email                 MailCurTyp;
160 	lv_errbuf               varchar2(4000):='';
161 	lv_protocol_type        varchar2(30) := 'SMTP';
162 	lv_protocol_address     varchar2(4000);
163 	lv_clob                 CLOB;
164 	lv_message              system.ecxevtmsg;
165 	lv_messageproperties    dbms_aq.message_properties_t;
166 	lv_msgid                raw(16);
167 	lv_enqueueoptions       dbms_aq.enqueue_options_t;
168 	lv_amount               pls_integer;
169 	lv_pointer              pls_integer;
170 	lv_party_id             pls_integer;
171 
172 	cursor c_err is select
173 	ed.document_number,
174 	ed.transaction_type,
175 	ed.transaction_subtype,
176 	ed.party_site_id,
177 	err.error_id,
178 	err.error_text,
179 	err.severity,
180 	err.table_name,
181 	err.rrow
182 	from msc_errors err,
183 	ecx_doclogs ed
184 	where err.message_id = ed.internal_control_number
185 	and err.instance_code = v_instance_code
186 	and nvl(err.message_sent,SYS_NO) <> SYS_YES;
187 
188   BEGIN
189         FOR c in c_err loop
190 
191         lv_errbuf:= '';
192 
193         lv_pointer:= 1;
194 	  OPEN c_email FOR
195 		'select hdr.company_admin_email, hdr.tp_header_id '
196               ||' from ecx_tp_headers hdr, ecx_tp_details det'
197               ||' where hdr.tp_header_id = det.tp_header_id'
198               ||' and hdr.company_admin_email IS NOT NULL'
199               ||' and det.source_tp_location_code = :loc' USING c.party_site_id;
200                                  -- party_site_id actually contains the loc code
201 	  FETCH c_email into lv_protocol_address, lv_party_id;
202         CLOSE c_email;
203 
204         lv_pointer:= 2;
205         IF lv_protocol_address IS NULL THEN
206                fnd_message.set_name ('ECX', 'ECX_TP_ADMIN_EMAIL_NOT_FOUND');
207                fnd_message.set_token ('TP_ID', c.party_site_id);
208                lv_errbuf := fnd_message.get;
209                LOG_MESSAGE( lv_errbuf);
210                fnd_profile.get('ECX_SYS_ADMIN_EMAIL',lv_protocol_address);
211         END IF;
212 
213         lv_pointer:= 3;
214         IF lv_protocol_address IS NULL THEN
215                fnd_message.set_name ('ECX', 'ECX_SYS_ADMIN_EMAIL_NOT_FOUND');
216                LOG_MESSAGE(fnd_message.get);
217         ELSE
218                IF v_debug THEN
219                    LOG_MESSAGE('lv_protocol_address: '||lv_protocol_address);
220                END IF;
221                IF c.severity = 1 THEN
222                    fnd_message.set_name ('MSC', 'MSC_PP_ERROR_EMAIL');
223                ELSE
224                    fnd_message.set_name ('MSC', 'MSC_PP_WARN_EMAIL');
225                END IF;
226 
227                lv_pointer:= 4;
228                fnd_message.set_token ('TRANSACTION_TYPE', c.transaction_type);
229                fnd_message.set_token ('TRANSACTION_SUB_TYPE', c.transaction_subtype);
230                fnd_message.set_token ('DOCUMENT_NUMBER', c.document_number);
231                lv_errbuf := lv_errbuf ||fnd_message.get;
232                lv_errbuf := lv_errbuf ||c.error_text;
233 
234                IF v_debug THEN
235                    LOG_MESSAGE('lv_errbuf : '||lv_errbuf );
236                END IF;
237 
238                lv_pointer:= 5;
239                lv_amount := length(lv_errbuf);
240                dbms_lob.createtemporary(lv_clob,TRUE,DBMS_LOB.SESSION);
241                dbms_lob.write(lv_clob,lv_amount,1,lv_errbuf);
242 
243                lv_pointer:= 6;
244                lv_message := system.ecxevtmsg( c.document_number,
245                                                lv_party_id,
246                                                c.transaction_type,
247                                                lv_clob,
248                                                'USER',
249                                                'ERROR',
250                                                lv_protocol_address,
251                                                null,
252                                                null,
253                                                null);
254 
255                lv_pointer:= 7;
256                -- Enqueue
257                dbms_aq.enqueue
258                (
259                queue_name=>'ECXEVENTQ',
260                enqueue_options=>lv_enqueueoptions,
261                message_properties=>lv_messageproperties,
262                payload=>lv_message,
263                msgid=>lv_msgid
264                );
265 
266                lv_pointer:= 8;
267                -- Start the Workflow process
268                wf_xmlnotify.startprocess(lv_msgid);
269 
270                lv_pointer:= 9;
271                update msc_errors set message_sent = SYS_YES
272                where error_id = c.error_id;
273 
274                END IF;
275                END LOOP;
276 
277   EXCEPTION
278   when others then
279   LOG_MESSAGE('MSC_CL_PRE_PROCESS.SEND_ERROR ('||lv_pointer||'): '||SQLERRM);
280   END;
281 */
282 
283 -- WRITE_ERROR_SUMM_LOG ===========
284 PROCEDURE WRITE_ERROR_SUMM_LOG (p_parent_req_id number DEFAULT -1 ) IS
285   CURSOR   c_error IS
286     SELECT table_name, error_text, count(*) occur
287     FROM   msc_errors
288     WHERE  instance_code = v_instance_code
289     AND    error_id between (v_error_no_begin + 1) and v_error_no_end
290     AND    severity = 1
291     GROUP BY table_name, error_text;
292 
293   CURSOR   c_error_by_req(p_request_id number) IS
294     SELECT table_name, error_text, count(*) occur
295     FROM   msc_errors
296     WHERE  instance_code = v_instance_code
297     AND    request_id = p_request_id
298     AND    severity = 1
299     GROUP BY table_name, error_text;
300 
301   CURSOR   c_warning IS
302     SELECT table_name, error_text, count(*) occur
303     FROM   msc_errors
304     WHERE  instance_code = v_instance_code
305     AND    error_id between (v_error_no_begin +1) and v_error_no_end
306     AND    severity = 2
307     GROUP BY table_name, error_text;
308 
309   CURSOR   c_warning_by_req(p_request_id number) IS
310     SELECT table_name, error_text, count(*) occur
311     FROM   msc_errors
312     WHERE  instance_code = v_instance_code
313     AND    request_id = p_request_id
314     AND    severity = 2
315     GROUP BY table_name, error_text;
316 
317   lv_count     PLS_INTEGER := 0;
318 
319 BEGIN
320 
321 IF nvl(p_parent_req_id,-1) < 1 THEN
322   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'*************** DATA ERRORS DURING PRE-PROCESSING ****************');
323 
324   FOR cur in c_error LOOP
325 
326     lv_count:= lv_count + 1;
327 
328     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_count||'. Table Name: '||cur.table_name||', Number of Occurences: '|| cur.occur||', Error: '|| cur.error_text);
329 
330   END LOOP;
331 
332   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'                 **********************');
333   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,' ');
334 
335 MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'*************** WARNINGS DURING PRE-PROCESSING ****************');
336 
337     lv_count := 0;
338 
339     FOR cur in c_warning LOOP
340 
341       lv_count:= lv_count + 1;
342 
343       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_count||'. Table Name: '||cur.table_name||', Number of Occurences: '|| cur.occur||', Warning: '|| cur.error_text);
344 
345     END LOOP;
346 
347   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'                 **********************');
348   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,' ');
349 
350 ELSE
351   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'*************** DATA ERRORS DURING PRE-PROCESSING ****************');
352 
353   FOR cur in c_error_by_req(p_parent_req_id) LOOP
354 
355     lv_count:= lv_count + 1;
356 
357     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_count||'. Table Name: '||cur.table_name||', Number of Occurences: '|| cur.occur||', Error: '|| cur.error_text);
358 
359   END LOOP;
360 
361   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'                 **********************');
362   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,' ');
363 
364   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'*************** WARNINGS DURING PRE-PROCESSING ****************');
365 
366     lv_count := 0;
367 
368     FOR cur in c_warning_by_req(p_parent_req_id) LOOP
369 
370       lv_count:= lv_count + 1;
371 
372       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_count||'. Table Name: '||cur.table_name||', Number of Occurences: '|| cur.occur||', Warning: '|| cur.error_text);
373 
374     END LOOP;
375 
376   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'                 **********************');
377   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,' ');
378 
379 END IF;
380 
381 EXCEPTION
382 when others then
383  MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error in WRITE_ERROR_SUMM_LOG: '||dbms_utility.format_error_stack );
384 
385 END;
386 
387 --
388 
389 --Launch DP Pull -------------
390    --Launch DP Pull -------------
391   PROCEDURE LAUNCH_DP_PULL (ERRBUF   OUT NOCOPY VARCHAR2,
392                             RETCODE  OUT NOCOPY NUMBER)
393   IS
394   lv_direct_load_profile       BOOLEAN ;
395   lv_launch_lvalue     NUMBER := SYS_NO;
396   lv_launch_time       NUMBER := SYS_NO;
397   lv_sql_stmt          VARCHAR2(4000);
398   lv_error_text        VARCHAR2(250);
399 
400   BEGIN
401 
402   IF (v_install_msd) THEN
403 
404      lv_direct_load_profile := (fnd_profile.value('MSD_ONE_STEP_COLLECTION')='Y');
405 
406    --IF ((v_level_assoc_enabled = SYS_YES) OR (v_item_rollup = SYS_YES) ) THEN
407      IF ((v_level_assoc_enabled = SYS_YES) OR (v_level_value_enabled = SYS_YES) OR (v_item_rollup = SYS_YES) ) THEN
408          lv_launch_lvalue := SYS_YES;
409      END IF;
410 
411 
412      IF ((v_cal_enabled = SYS_YES)  OR (v_fiscal_cal_enabled = SYS_YES) OR (v_comp_cal_enabled=SYS_YES)) THEN
413        lv_launch_time  := SYS_YES ;
414      END IF ;
415 
416      IF(lv_direct_load_profile) THEN
417          lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
418                        ||' retcode  number ;'
419                        ||' BEGIN '
420                        ||' MSD_CL_PRE_PROCESS.LAUNCH_PULL_PROGRAM'
421                        ||' (ERRBUF             =>  errbuf ,'
422                        ||'  RETCODE            =>  retcode ,'
423                        ||'  p_instance_id      => :v_instance_id,'
424                        ||'  p_request_id       => :v_request_id,'
425                        ||'  p_launch_lvalue    => :lv_launch_lvalue,'
426                        ||'  p_launch_booking   => :v_booking_enabled,'
427                        ||'  p_launch_shipment  => :v_shipment_enabled,'
428                        ||'  p_launch_forecast  => :v_mfg_fct_enabled,'
429                        ||'  p_launch_time      => :lv_launch_time,'
430                        ||'  p_launch_pricing   => :v_list_price_enabled,'
431                        ||'  p_launch_curr_conv => :v_curr_conv_enabled ,'
432                        ||'  p_launch_uom_conv  => :v_uom_conv_enabled,'
433                        ||'  p_launch_cs_data   => :v_cs_data_enabled,'
434                        ||'  p_cs_refresh       => :v_cs_refresh);'
435                        ||'  END ;';
436 
437       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_sql_stmt);
438 
439         EXECUTE IMMEDIATE lv_sql_stmt
440                 USING     v_instance_id,
441                           v_request_id,
442                           lv_launch_lvalue,
443                           v_booking_enabled,
444                           v_shipment_enabled,
445                           v_mfg_fct_enabled,
446                           lv_launch_time,
447                           v_list_price_enabled,
448                           v_curr_conv_enabled ,
449                           v_uom_conv_enabled,
450                           v_cs_data_enabled,
451                           v_cs_refresh ;
452 
453       END IF; -- One step collection
454    END IF ;-- v_install_msd
455 
456   EXCEPTION
457 
458    WHEN OTHERS THEN
459     lv_error_text := substr('MSC_CL_PRE_PROCESS.LAUNCH_DP_PULL'||SQLERRM,1,240);
460     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
461     RETCODE := G_WARNING;
462     ERRBUF  := lv_error_text;
463 
464   END LAUNCH_DP_PULL ;
465 
466   PROCEDURE LAUNCH_DELETE_DUPLICATES (ERRBUF   OUT NOCOPY VARCHAR2,
467                             RETCODE  OUT NOCOPY NUMBER)
468   IS
469 
470   lv_sql_stmt          VARCHAR2(4000);
471   lv_error_text        VARCHAR2(250);
472 
473   BEGIN
474 
475   IF (v_install_msd) THEN
476 
477        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
478                        ||' retcode  number ;'
479                        ||' BEGIN '
480                        ||' MSD_CL_PRE_PROCESS.LAUNCH_DELETE_DUPLICATES'
481                        ||' (ERRBUF             =>  errbuf ,'
482                        ||'  RETCODE            =>  retcode ,'
483                        ||'  p_instance_id      => :v_instance_id);'
484                        ||'  END ;';
485 
486         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_sql_stmt);
487 
488         EXECUTE IMMEDIATE lv_sql_stmt
489                 USING     v_instance_id;
490 
491 
492    END IF ;-- v_install_msd
493 
494   EXCEPTION
495 
496    WHEN OTHERS THEN
497     lv_error_text := substr('MSC_CL_PRE_PROCESS.LAUNCH_DELETE_DUPLICATES'||SQLERRM,1,240);
498     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
499     RETCODE := G_WARNING;
500     ERRBUF  := lv_error_text;
501 
502   END LAUNCH_DELETE_DUPLICATES ;
503 
504 -- =====Local Procedures =========
505    FUNCTION GET_MY_COMPANY return VARCHAR2 IS
506        p_my_company    msc_companies.company_name%TYPE;
507    BEGIN
508 
509       /* Get the name of the own Company */
510       /* This name is seeded with company_is = 1 in msc_companies */
511       BEGIN
512          select company_name into p_my_company
513          from msc_companies
514          where company_id = 1;
515       EXCEPTION
516          WHEN OTHERS THEN
517          return 'My Company';
518       END;
519 
520       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'The name in GET_MY_COMPANY :'||p_my_company);
521       return p_my_company;
522 
523    END GET_MY_COMPANY;
524 
525 -- ===== CREATE_INDEXES ==========
526 PROCEDURE CREATE_INDEXES (p_batch_index   VARCHAR2 DEFAULT NULL) IS
527 BEGIN
528 
529 IF p_batch_index IS NULL THEN
530 
531   IF v_item_enabled = SYS_YES THEN
532 
533     BEGIN
534 
535     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
536                            application_short_name => 'MSC',
537                            statement_type => AD_DDL.CREATE_INDEX,
538                            statement =>
539                  'create index MSC_ST_SYSTEM_ITEMS_N1_'||v_instance_code
540               ||' on MSC_ST_SYSTEM_ITEMS '
541               ||'(item_name, sr_instance_code, organization_code, company_name) '
542               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
543                            object_name =>'MSC_ST_SYSTEM_ITEMS_N1_'||v_instance_code);
544 
545     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Created Index MSC_ST_SYSTEM_ITEMS_N1_'||v_instance_code);
546 
547 
548     EXCEPTION
549     WHEN OTHERS THEN
550          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SYSTEM_ITEMS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
551     END;
552 
553     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SYSTEM_ITEMS', v_instance_id, -1);
554 
555   END IF;
556 
557   IF v_mat_sup_enabled = SYS_YES THEN
558 
559     BEGIN
560     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
561                            application_short_name => 'MSC',
562                            statement_type => AD_DDL.CREATE_INDEX,
563                            statement =>
564                  'create index MSC_ST_SUPPLIES_N1_'||v_instance_code
565               ||' on MSC_ST_SUPPLIES '
566               ||'(SR_INSTANCE_CODE, ORDER_NUMBER, PURCH_LINE_NUM, ORDER_TYPE, COMPANY_NAME) '
567               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
568                            object_name =>'MSC_ST_SUPPLIES_N1_'||v_instance_code);
569         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Created Index MSC_ST_SUPPLIES_N1_'||v_instance_code);
570 
571     EXCEPTION
572     WHEN OTHERS THEN
573          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Error creating Index MSC_ST_SUPPLIES_N1_'||v_instance_code ||'. Error:'||substr(SQLERRM,1,240));
574     END;
575 
576     BEGIN
577     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
578                            application_short_name => 'MSC',
579                            statement_type => AD_DDL.CREATE_INDEX,
580                            statement =>
581                  'create index MSC_ST_SUPPLIES_N2_'||v_instance_code
582               ||' on MSC_ST_SUPPLIES '
583               ||'(sr_instance_code, schedule_line_num, schedule_designator, organization_code, order_type, company_name) '
584               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
585                            object_name =>'MSC_ST_SUPPLIES_N2_'||v_instance_code);
586           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Created Index MSC_ST_SUPPLIES_N2_'||v_instance_code);
587 
588     EXCEPTION
589     WHEN OTHERS THEN
590          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Error creating Index MSC_ST_SUPPLIES_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
591     END;
592 
593     BEGIN
594     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
595                            application_short_name => 'MSC',
596                            statement_type => AD_DDL.CREATE_INDEX,
597                            statement =>
598                  'create index MSC_ST_SUPPLIES_N3_'||v_instance_code
599               ||' on MSC_ST_SUPPLIES '
600               ||'(sr_instance_code, item_name, organization_code, order_type, company_name) '
601               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
602                            object_name =>'MSC_ST_SUPPLIES_N3_'||v_instance_code);
603 
604     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SUPPLIES_N3_'||v_instance_code);
605 
606 
607     EXCEPTION
608     WHEN OTHERS THEN
609          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SUPPLIES_N3_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
610     END;
611 
612     BEGIN
613     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
614                            application_short_name => 'MSC',
615                            statement_type => AD_DDL.CREATE_INDEX,
616                            statement =>
617                  'create index MSC_ST_SUPPLIES_N4_'||v_instance_code
618               ||' on MSC_ST_SUPPLIES '
619               ||'(sr_instance_code,wip_entity_name, organization_code,order_type,  company_name) '
620               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
621                            object_name =>'MSC_ST_SUPPLIES_N4_'||v_instance_code);
622     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SUPPLIES_N4_'||v_instance_code);
623 
624 
625     EXCEPTION
626     WHEN OTHERS THEN
627          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SUPPLIES_N4_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
628     END;
629 
630     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SUPPLIES', v_instance_id, -1);
631 
632   END IF;
633 
634   IF v_mat_dmd_enabled = SYS_YES THEN
635 
636     BEGIN
637     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
638                            application_short_name => 'MSC',
639                            statement_type => AD_DDL.CREATE_INDEX,
640                            statement =>
641                  'create index MSC_ST_DEMANDS_N1_'||v_instance_code
642               ||' on MSC_ST_DEMANDS '
643               ||'(sr_instance_code, schedule_date, item_name, organization_code, forecast_designator, company_name) '
644               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
645                            object_name =>'MSC_ST_DEMANDS_N1_'||v_instance_code);
646     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_DEMANDS_N1_'||v_instance_code);
647 
648 
649     EXCEPTION
650     WHEN OTHERS THEN
651          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_DEMANDS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
652     END;
653 
654     BEGIN
655     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
656                            application_short_name => 'MSC',
657                            statement_type => AD_DDL.CREATE_INDEX,
658                            statement =>
659                  'create index MSC_ST_DEMANDS_N2_'||v_instance_code
660               ||' on MSC_ST_DEMANDS '
661               ||'(SR_INSTANCE_CODE, ITEM_NAME,WIP_ENTITY_NAME, ORGANIZATION_CODE, ORIGINATION_TYPE, OPERATION_SEQ_CODE, COMPANY_NAME) '
662               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
663                            object_name =>'MSC_ST_DEMANDS_N2_'||v_instance_code);
664 
665          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_DEMANDS_N2_'||v_instance_code);
666 
667     EXCEPTION
668     WHEN OTHERS THEN
669          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_DEMANDS_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
670     END;
671 
672     BEGIN
673     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
674                            application_short_name => 'MSC',
675                            statement_type => AD_DDL.CREATE_INDEX,
676                            statement =>
677                  'create index MSC_ST_SO_N1_'||v_instance_code
678               ||' on MSC_ST_SALES_ORDERS '
679               ||'(sr_instance_code, sales_order_number, line_num, organization_code, company_name) '
680               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
681                            object_name =>'MSC_ST_SO_N1_'||v_instance_code);
682 
683          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SO_N1_'||v_instance_code);
684 
685     EXCEPTION
686     WHEN OTHERS THEN
687          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SO_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
688     END;
689 
690     msc_analyse_tables_pk.analyse_table( 'MSC_ST_DEMANDS', v_instance_id, -1);
691     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SALES_ORDERS', v_instance_id, -1);
692 
693   END IF;
694 
695    IF v_reserve_enabled = SYS_YES THEN
696 
697     BEGIN
698     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
699                            application_short_name => 'MSC',
700                            statement_type => AD_DDL.CREATE_INDEX,
701                            statement =>
702                  'create index MSC_ST_RESERVE_N1_'||v_instance_code
703               ||' on MSC_ST_RESERVATIONS '
704               ||'(sr_instance_code,sales_order_number, line_num,organization_code,company_name ) '
705               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
706                            object_name =>'MSC_ST_RESERVE_N1_'||v_instance_code);
707 
708 
709          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_RESERVE_N1_'||v_instance_code);
710 
711     EXCEPTION
712     WHEN OTHERS THEN
713          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_RESERVE_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
714     END;
715 
716     msc_analyse_tables_pk.analyse_table( 'MSC_ST_RESERVATIONS', v_instance_id, -1);
717 
718   END IF;
719 
720 
721   IF v_bom_enabled = SYS_YES THEN
722 
723     BEGIN
724     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
725                            application_short_name => 'MSC',
726                            statement_type => AD_DDL.CREATE_INDEX,
727                            statement =>
728                  'create index MSC_ST_BOMS_N1_'||v_instance_code
729               ||' on MSC_ST_BOMS '
730               ||'(sr_instance_code, assembly_name, organization_code, company_name, bom_name, alternate_bom_designator) '
731               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
732                            object_name =>'MSC_ST_BOMS_N1_'||v_instance_code);
733          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_BOMS_N1_'||v_instance_code);
734 
735     EXCEPTION
736     WHEN OTHERS THEN
737          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_BOMS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
738     END;
739 
740     BEGIN
741     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
742                            application_short_name => 'MSC',
743                            statement_type => AD_DDL.CREATE_INDEX,
744                            statement =>
745                  'create index MSC_ST_BOM_COMP_N1_'||v_instance_code
746               ||' on MSC_ST_BOM_COMPONENTS '
747               ||'(sr_instance_code, assembly_name, component_name, effectivity_date, operation_seq_code, organization_code, company_name, bom_name, alternate_bom_designator) '
748               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
749                            object_name =>'MSC_ST_BOM_COMP_N1_'||v_instance_code);
750          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_BOM_COMP_N1_'||v_instance_code);
751 
752     EXCEPTION
753     WHEN OTHERS THEN
754          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_BOM_COMP_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
755     END;
756 
757     BEGIN
758     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
759                            application_short_name => 'MSC',
760                            statement_type => AD_DDL.CREATE_INDEX,
761                            statement =>
762                  'create index MSC_ST_COMP_SUB_N1_'||v_instance_code
763               ||' on MSC_ST_COMPONENT_SUBSTITUTES '
764               ||'(sr_instance_code, assembly_name, component_name, sub_item_name, effectivity_date, operation_seq_code, organization_code,company_name, bom_name, alternate_bom_designator) '
765               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
766                            object_name =>'MSC_ST_COMP_SUB_N1_'||v_instance_code);
767 
768          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_COMP_SUB_N1_'||v_instance_code);
769 
770     EXCEPTION
771     WHEN OTHERS THEN
772          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_COMP_SUB_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
773     END;
774 
775      -- Added for Co Products (OSFM Integration) --
776     BEGIN
777     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
778                            application_short_name => 'MSC',
779                            statement_type => AD_DDL.CREATE_INDEX,
780                            statement =>
781                  'create index MSC_ST_CO_PRODUCTS_N1_'||v_instance_code
782               ||' on MSC_ST_CO_PRODUCTS '
783               ||'(co_product_name,component_name,sr_instance_code,company_name ) '
784               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
785                            object_name =>'MSC_ST_CO_PRODUCTS_N1_'||v_instance_code);
786          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_CO_PRODUCTS_N1_'||v_instance_code);
787 
788     EXCEPTION
789     WHEN OTHERS THEN
790          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_CO_PRODUCTS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
791     END;
792 
793     msc_analyse_tables_pk.analyse_table( 'MSC_ST_BOMS', v_instance_id, -1);
794     msc_analyse_tables_pk.analyse_table( 'MSC_ST_BOM_COMPONENTS', v_instance_id, -1);
795     msc_analyse_tables_pk.analyse_table( 'MSC_ST_COMPONENT_SUBSTITUTES', v_instance_id, -1);
796     msc_analyse_tables_pk.analyse_table( 'MSC_ST_CO_PRODUCTS', v_instance_id, -1);
797 
798 
799   END IF;
800 
801   IF v_rtg_enabled = SYS_YES THEN
802 
803     BEGIN
804     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
805                            application_short_name => 'MSC',
806                            statement_type => AD_DDL.CREATE_INDEX,
807                            statement =>
808                  'create index MSC_ST_ROUTINGS_N1_'||v_instance_code
809               ||' on MSC_ST_ROUTINGS '
810               ||'(sr_instance_code, assembly_name, organization_code,  company_name, routing_name, alternate_routing_designator) '
811               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
812                            object_name =>'MSC_ST_ROUTINGS_N1_'||v_instance_code);
813 
814          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ROUTINGS_N1_'||v_instance_code);
815 
816     EXCEPTION
817     WHEN OTHERS THEN
818          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ROUTINGS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
819     END;
820 
821     BEGIN
822     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
823                            application_short_name => 'MSC',
824                            statement_type => AD_DDL.CREATE_INDEX,
825                            statement =>
826                  'create index MSC_ST_ROUTING_OPER_N1_'||v_instance_code
827               ||' on MSC_ST_ROUTING_OPERATIONS '
828               ||'( sr_instance_code, assembly_name, operation_seq_code, organization_code, company_name, routing_name, alternate_routing_designator) '
829               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
830                            object_name =>'MSC_ST_ROUTING_OPER_N1_'||v_instance_code);
831 
832          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ROUTING_OPER_N1_'||v_instance_code);
833 
834 
835     EXCEPTION
836     WHEN OTHERS THEN
837          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ROUTING_OPER_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
838     END;
839 
840     BEGIN
841     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
842                            application_short_name => 'MSC',
843                            statement_type => AD_DDL.CREATE_INDEX,
844                            statement =>
845                  'create index MSC_ST_OPER_RES_N1_'||v_instance_code
846               ||' on MSC_ST_OPERATION_RESOURCES '
847               ||'(sr_instance_code,  assembly_name, resource_code, organization_code, company_name, alternate_number, operation_seq_code, resource_seq_code, routing_name, alternate_routing_designator) '
848               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
849                            object_name =>'MSC_ST_OPER_RES_N1_'||v_instance_code);
850 
851          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_OPER_RES_N1_'||v_instance_code);
852 
853     EXCEPTION
854     WHEN OTHERS THEN
855          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_OPER_RES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
856     END;
857 
858     -- Added for Operation Networks (OSFM Integration )
859     BEGIN
860     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
861                            application_short_name => 'MSC',
862                            statement_type => AD_DDL.CREATE_INDEX,
863                            statement =>
864                  'create index MSC_ST_OPER_NTWK_N1_'||v_instance_code
865               ||' on MSC_ST_OPERATION_NETWORKS '
866               ||'(assembly_name, organization_code,FROM_OPERATION_SEQ_CODE, TO_OPERATION_SEQ_CODE,sr_instance_code, company_name) '
867               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
868                            object_name =>'MSC_ST_OPER_NTWK_N1_'||v_instance_code);
869 
870          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_OPER_NTWK_N1_'||v_instance_code);
871 
872     EXCEPTION
873     WHEN OTHERS THEN
874          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_OPER_NTWK_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
875     END;
876 
877     msc_analyse_tables_pk.analyse_table( 'MSC_ST_ROUTINGS', v_instance_id, -1);
878     msc_analyse_tables_pk.analyse_table( 'MSC_ST_OPERATION_RESOURCES', v_instance_id, -1);
879     msc_analyse_tables_pk.analyse_table( 'MSC_ST_ROUTING_OPERATIONS', v_instance_id, -1);
880     msc_analyse_tables_pk.analyse_table( 'MSC_ST_OPERATION_NETWORKS', v_instance_id, -1);
881 
882 
883   END IF;
884 
885   IF v_item_cat_enabled = SYS_YES THEN
886 
887     BEGIN
888     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
889                            application_short_name => 'MSC',
890                            statement_type => AD_DDL.CREATE_INDEX,
891                            statement =>
892                  'create index MSC_ST_ITEM_CAT_N1_'||v_instance_code
893               ||' on MSC_ST_ITEM_CATEGORIES '
894               ||'(sr_instance_code,item_name, category_name, category_set_name, organization_code, company_name) '
895               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
896                            object_name =>'MSC_ST_ITEM_CAT_N1_'||v_instance_code);
897 
898          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ITEM_CAT_N1_'||v_instance_code);
899 
900     EXCEPTION
901     WHEN OTHERS THEN
902          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ITEM_CAT_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
903     END;
904 
905     BEGIN
906     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
907                            application_short_name => 'MSC',
908                            statement_type => AD_DDL.CREATE_INDEX,
909                            statement =>
910                  'create index MSC_ST_ITEM_CAT_N2_'||v_instance_code
911               ||' on MSC_ST_ITEM_CATEGORIES '
912               ||'(sr_instance_code,batch_id) '
913               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
914                            object_name =>'MSC_ST_ITEM_CAT_N2_'||v_instance_code);
915 
916          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ITEM_CAT_N2_'||v_instance_code);
917 
918     EXCEPTION
919     WHEN OTHERS THEN
920          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ITEM_CAT_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
921     END;
922 
923     msc_analyse_tables_pk.analyse_table( 'MSC_ST_ITEM_CATEGORIES', v_instance_id, -1);
924 
925   END IF;
926 
927   IF v_resources_enabled = SYS_YES THEN
928     BEGIN
929 
930     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
931                            application_short_name => 'MSC',
932                            statement_type => AD_DDL.CREATE_INDEX,
933                            statement =>
934                  'create index MSC_ST_RESOURCE_GROUPS_N1_'||v_instance_code
935               ||' on MSC_ST_RESOURCE_GROUPS '
936               ||'(sr_instance_code, group_code, company_name) '
937               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
938                            object_name =>'MSC_ST_RESOURCE_GROUPS_N1_'||v_instance_code);
939 
940          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_RESOURCE_GROUPS_N1_'||v_instance_code);
941 
942     EXCEPTION
943     WHEN OTHERS THEN
944          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_RESOURCE_GROUPS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
945     END;
946 
947     BEGIN
948     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
949                            application_short_name => 'MSC',
950                            statement_type => AD_DDL.CREATE_INDEX,
951                            statement =>
952                  'create index MSC_ST_DEPT_RES_N1_'||v_instance_code
953               ||' on MSC_ST_DEPARTMENT_RESOURCES '
954               ||'(sr_instance_code, resource_code, department_code, organization_code,  company_name) '
955               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
956                            object_name =>'MSC_ST_DEPT_RES_N1_'||v_instance_code);
957 
958          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_DEPT_RES_N1_'||v_instance_code);
959 
960     EXCEPTION
961     WHEN OTHERS THEN
962          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_DEPT_RES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
963     END;
964 
965     BEGIN
966     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
967                            application_short_name => 'MSC',
968                            statement_type => AD_DDL.CREATE_INDEX,
969                            statement =>
970                  'create index MSC_ST_RES_SHIFTS_N1_'||v_instance_code
971               ||' on MSC_ST_RESOURCE_SHIFTS '
972               ||'(sr_instance_code, resource_code, department_code, shift_name,  organization_code, company_name) '
973               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
974                            object_name =>'MSC_ST_RES_SHIFTS_N1_'||v_instance_code);
975 
976          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_RES_SHIFTS_N1_'||v_instance_code);
977 
978     EXCEPTION
979     WHEN OTHERS THEN
980          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_RES_SHIFTS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
981     END;
982 
983     BEGIN
984     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
985                            application_short_name => 'MSC',
986                            statement_type => AD_DDL.CREATE_INDEX,
987                            statement =>
988                  'create index MSC_ST_RES_CHANGES_N1_'||v_instance_code
989               ||' on MSC_ST_RESOURCE_CHANGES '
990               ||'(sr_instance_code, simulation_set, resource_code, department_code, from_date, shift_name,  organization_code, company_name) '
991               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
992                            object_name =>'MSC_ST_RES_CHANGES_N1_'||v_instance_code);
993 
994          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_RES_CHANGES_N1_'||v_instance_code);
995 
996     EXCEPTION
997     WHEN OTHERS THEN
998          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_RES_CHANGES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
999     END;
1000 
1001     msc_analyse_tables_pk.analyse_table( 'MSC_ST_RESOURCE_GROUPS', v_instance_id, -1);
1002     msc_analyse_tables_pk.analyse_table( 'MSC_ST_DEPARTMENT_RESOURCES', v_instance_id, -1);
1003     msc_analyse_tables_pk.analyse_table( 'MSC_ST_RESOURCE_SHIFTS', v_instance_id, -1);
1004     msc_analyse_tables_pk.analyse_table( 'MSC_ST_RESOURCE_CHANGES', v_instance_id, -1);
1005 
1006   END IF;
1007 
1008   IF v_project_enabled = SYS_YES THEN
1009 
1010     BEGIN
1011     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1012                            application_short_name => 'MSC',
1013                            statement_type => AD_DDL.CREATE_INDEX,
1014                            statement =>
1015                  'create index MSC_ST_PROJECT_TASKS_N1_'||v_instance_code
1016               ||' on MSC_ST_PROJECT_TASKS '
1017               ||'(sr_instance_code, project_number, task_number, organization_code,  company_name) '
1018               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1019                            object_name =>'MSC_ST_PROJECT_TASKS_N1_'||v_instance_code);
1020 
1021          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_PROJECT_TASKS_N1_'||v_instance_code);
1022 
1023     EXCEPTION
1024     WHEN OTHERS THEN
1025          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_PROJECT_TASKS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1026     END;
1027 
1028     msc_analyse_tables_pk.analyse_table( 'MSC_ST_PROJECT_TASKS', v_instance_id, -1);
1029 
1030   END IF;
1031 
1032   IF v_dmd_class_enabled = SYS_YES THEN
1033 
1034     BEGIN
1035     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1036                            application_short_name => 'MSC',
1037                            statement_type => AD_DDL.CREATE_INDEX,
1038                            statement =>
1039                  'create index MSC_ST_DMD_CLASS_N1_'||v_instance_code
1040               ||' on MSC_ST_DEMAND_CLASSES '
1041               ||'(sr_instance_code,demand_class,company_name) '
1042               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1043                            object_name =>'MSC_ST_DMD_CLASS_N1_'||v_instance_code);
1044 
1045          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_DMD_CLASS_N1_'||v_instance_code);
1046 
1047     EXCEPTION
1048     WHEN OTHERS THEN
1049          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_DMD_CLASS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1050     END;
1051 
1052     msc_analyse_tables_pk.analyse_table( 'MSC_ST_DEMAND_CLASSES', v_instance_id, -1);
1053 
1054   END IF;
1055 
1056   IF v_tp_enabled = SYS_YES THEN
1057 
1058     BEGIN
1059     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1060                            application_short_name => 'MSC',
1061                            statement_type => AD_DDL.CREATE_INDEX,
1062                            statement =>
1063                  'create index MSC_ST_TRADING_PART_N1_'||v_instance_code
1064               ||' on MSC_ST_TRADING_PARTNERS '
1065               ||'(sr_instance_code, partner_name, partner_type, company_name) '
1066               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1067                            object_name =>'MSC_ST_TRADING_PART_N1_'||v_instance_code);
1068 
1069          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_TRADING_PART_N1_'||v_instance_code);
1070 
1071     EXCEPTION
1072     WHEN OTHERS THEN
1073          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_TRADING_PART_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1074     END;
1075 
1076     BEGIN
1077     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1078                            application_short_name => 'MSC',
1079                            statement_type => AD_DDL.CREATE_INDEX,
1080                            statement =>
1081                  'create index MSC_ST_TRADING_PART_N2_'||v_instance_code
1082               ||' on MSC_ST_TRADING_PARTNERS '
1083               ||'(sr_instance_code, organization_code, partner_type, company_name) '
1084               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1085                            object_name =>'MSC_ST_TRADING_PART_N2_'||v_instance_code);
1086 
1087          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_TRADING_PART_N2_'||v_instance_code);
1088 
1089     EXCEPTION
1090     WHEN OTHERS THEN
1091          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_TRADING_PART_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1092     END;
1093 
1094     BEGIN
1095     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1096                            application_short_name => 'MSC',
1097                            statement_type => AD_DDL.CREATE_INDEX,
1098                            statement =>
1099                  'create index MSC_ST_TRD_PART_SITES_N1_'||v_instance_code
1100               ||' on MSC_ST_TRADING_PARTNER_SITES '
1101               ||'(sr_instance_code ,tp_site_code, partner_name, partner_type) '
1102               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1103                            object_name =>'MSC_ST_TRD_PART_SITES_N1_'||v_instance_code);
1104 
1105          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_TRD_PART_SITES_N1_'||v_instance_code);
1106 
1107     EXCEPTION
1108     WHEN OTHERS THEN
1109          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_TRD_PART_SITES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1110     END;
1111 
1112     BEGIN
1113     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1114                            application_short_name => 'MSC',
1115                            statement_type => AD_DDL.CREATE_INDEX,
1116                            statement =>
1117                  'create index MSC_ST_SUB_INV_N1_'||v_instance_code
1118               ||' on MSC_ST_SUB_INVENTORIES '
1119               ||'( sr_instance_code, sub_inventory_code, organization_code ) '
1120               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1121                            object_name =>'MSC_ST_SUB_INV_N1_'||v_instance_code);
1122 
1123          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SUB_INV_N1_'||v_instance_code);
1124 
1125     EXCEPTION
1126     WHEN OTHERS THEN
1127          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SUB_INV_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1128     END;
1129 
1130     BEGIN
1131     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1132                            application_short_name => 'MSC',
1133                            statement_type => AD_DDL.CREATE_INDEX,
1134                            statement =>
1135                  'create index MSC_ST_LOC_ASC_N1_'||v_instance_code
1136               ||' on MSC_ST_LOCATION_ASSOCIATIONS '
1137               ||'(sr_instance_code, location_code, tp_site_code, partner_name, partner_type,  company_name) '
1138               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1139                            object_name =>'MSC_ST_LOC_ASC_N1_'||v_instance_code);
1140 
1141          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_LOC_ASC_N1_'||v_instance_code);
1142 
1143     EXCEPTION
1144     WHEN OTHERS THEN
1145          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_LOC_ASC_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1146     END;
1147 
1148     BEGIN
1149     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1150                            application_short_name => 'MSC',
1151                            statement_type => AD_DDL.CREATE_INDEX,
1152                            statement =>
1153                  'create index MSC_ST_PART_CONT_N1_'||v_instance_code
1154               ||' on MSC_ST_PARTNER_CONTACTS '
1155               ||'(sr_instance_code,tp_site_code, partner_name, partner_type, company_name) '
1156               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1157                            object_name =>'MSC_ST_PART_CONT_N1_'||v_instance_code);
1158 
1159          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_PART_CONT_N1_'||v_instance_code);
1160 
1161     EXCEPTION
1162     WHEN OTHERS THEN
1163          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_PART_CONT_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1164     END;
1165 
1166     --For Aeroexchange
1167     --Added for MSC_ST_GROUPS
1168     BEGIN
1169       ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1170                            application_short_name => 'MSC',
1171                            statement_type => AD_DDL.CREATE_INDEX,
1172                            statement =>
1173                  'create index MSC_ST_GROUPS_N1_'||v_instance_code
1174               ||' on MSC_ST_GROUPS '
1175               ||'(GROUP_NAME) '
1176               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1177                            object_name =>'MSC_ST_GROUPS_N1_'||v_instance_code);
1178 
1179          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_GROUPS_N1_'||v_instance_code);
1180 
1181     msc_analyse_tables_pk.analyse_table( 'MSC_ST_GROUPS', v_instance_id, -1);
1182 
1183     EXCEPTION
1184     WHEN OTHERS THEN
1185          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_GROUPS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1186     END;
1187 
1188     BEGIN
1189     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1190                            application_short_name => 'MSC',
1191                            statement_type => AD_DDL.CREATE_INDEX,
1192                            statement =>
1193                  'create index MSC_ST_GROUP_COMPANIES_N1_'||v_instance_code
1194               ||' on MSC_ST_GROUP_COMPANIES '
1195               ||'(group_name,company_name) '
1196               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1197                            object_name =>'MSC_ST_GROUP_COMPANIES_N1_'||v_instance_code);
1198 
1199          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_GROUP_COMPANIES_N1_'||v_instance_code);
1200 
1201     EXCEPTION
1202     WHEN OTHERS THEN
1203          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_GROUP_COMPANIES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1204     END;
1205 
1206 
1207 
1208     msc_analyse_tables_pk.analyse_table( 'MSC_ST_TRADING_PARTNERS', v_instance_id, -1);
1209     msc_analyse_tables_pk.analyse_table( 'MSC_ST_TRADING_PARTNER_SITES', v_instance_id, -1);
1210     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SUB_INVENTORIES', v_instance_id, -1);
1211     msc_analyse_tables_pk.analyse_table( 'MSC_ST_LOCATION_ASSOCIATIONS', v_instance_id, -1);
1212     msc_analyse_tables_pk.analyse_table( 'MSC_ST_PARTNER_CONTACTS', v_instance_id, -1);
1213 
1214   END IF;
1215 
1216   IF v_item_cst_enabled = SYS_YES THEN
1217 
1218     BEGIN
1219 
1220     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1221                            application_short_name => 'MSC',
1222                            statement_type => AD_DDL.CREATE_INDEX,
1223                            statement =>
1224                  'create index MSC_ST_ITEM_CUSTOMERS_N1_'||v_instance_code
1225               ||' on MSC_ST_ITEM_CUSTOMERS '
1226               ||'(ITEM_NAME, CUSTOMER_NAME, SR_INSTANCE_CODE, COMPANY_NAME, CUSTOMER_SITE_NAME) '
1227               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1228                            object_name =>'MSC_ST_ITEM_CUSTOMERS_N1_'||v_instance_code);
1229 
1230          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ITEM_CUSTOMERS_N1_'||v_instance_code);
1231 
1232     EXCEPTION
1233     WHEN OTHERS THEN
1234          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ITEM_CUSTOMERS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1235     END;
1236 
1237     BEGIN
1238 
1239     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1240                            application_short_name => 'MSC',
1241                            statement_type => AD_DDL.CREATE_INDEX,
1242                            statement =>
1243                  'create index MSC_ST_ITEM_CUSTOMERS_N2_'||v_instance_code
1244               ||' on MSC_ST_ITEM_CUSTOMERS '
1245               ||'(CUSTOMER_ITEM_NAME, CUSTOMER_NAME, SR_INSTANCE_CODE, COMPANY_NAME, CUSTOMER_SITE_NAME) '
1246               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1247                            object_name =>'MSC_ST_ITEM_CUSTOMERS_N2_'||v_instance_code);
1248 
1249          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ITEM_CUSTOMERS_N2_'||v_instance_code);
1250 
1251     EXCEPTION
1252     WHEN OTHERS THEN
1253          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ITEM_CUSTOMERS_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1254     END;
1255 
1256     msc_analyse_tables_pk.analyse_table( 'MSC_ST_ITEM_CUSTOMERS', v_instance_id, -1);
1257 
1258   END IF;
1259 
1260   IF v_ship_mthd_enabled = SYS_YES THEN
1261 
1262     BEGIN
1263     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1264                            application_short_name => 'MSC',
1265                            statement_type => AD_DDL.CREATE_INDEX,
1266                            statement =>
1267                  'create index MSC_ST_INTERORG_SHIP_N1_'||v_instance_code
1268               ||' on MSC_ST_INTERORG_SHIP_METHODS '
1269               ||'(sr_instance_code,from_organization_code,to_organization_code,ship_method,company_name) '
1270               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1271                            object_name =>'MSC_ST_INTERORG_SHIP_N1_'||v_instance_code);
1272 
1273          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_INTERORG_SHIP_N1_'||v_instance_code);
1274 
1275     EXCEPTION
1276     WHEN OTHERS THEN
1277          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_INTERORG_SHIP_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1278     END;
1279 
1280     msc_analyse_tables_pk.analyse_table( 'MSC_ST_INTERORG_SHIP_METHODS', v_instance_id, -1);
1281 
1282    END IF;
1283 
1284   IF v_sourcing_enabled = SYS_YES THEN
1285 
1286    BEGIN
1287 
1288     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1289                            application_short_name => 'MSC',
1290                            statement_type => AD_DDL.CREATE_INDEX,
1291                            statement =>
1292                  'create index MSC_ST_ITEM_SRC_N1_'||v_instance_code
1293               ||' on MSC_ST_ITEM_SOURCING '
1294               ||'(sr_instance_code, sourcing_rule_name, assignment_set_name, organization_code, company_name, receipt_organization_code, source_organization_code) '
1295               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1296                            object_name =>'MSC_ST_ITEM_SRC_N1_'||v_instance_code);
1297 
1298          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ITEM_SRC_N1_'||v_instance_code);
1299 
1300     EXCEPTION
1301     WHEN OTHERS THEN
1302          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ITEM_SRC_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1303     END;
1304 
1305     BEGIN
1306 
1307     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1308                            application_short_name => 'MSC',
1309                            statement_type => AD_DDL.CREATE_INDEX,
1310                            statement =>
1311                  'create index MSC_ST_REGIONS_N1_'||v_instance_code
1312               ||' on MSC_ST_REGIONS '
1313               ||'(sr_instance_code, country, state, city, postal_code_from, postal_code_to, zone ) '
1314               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1315                            object_name =>'MSC_ST_REGIONS_N1_'||v_instance_code);
1316 
1317          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_REGIONS_N1_'||v_instance_code);
1318 
1319     EXCEPTION
1320     WHEN OTHERS THEN
1321          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_REGIONS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1322     END;
1323 
1324     BEGIN
1325 
1326     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1327                            application_short_name => 'MSC',
1328                            statement_type => AD_DDL.CREATE_INDEX,
1329                            statement =>
1330                  'create index MSC_ST_ZONE_REGIONS_N1_'||v_instance_code
1331               ||' on MSC_ST_ZONE_REGIONS '
1332               ||'(sr_instance_code, country, state, city, postal_code_from, postal_code_to, zone ) '
1333               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1334                            object_name =>'MSC_ST_ZONE_REGIONS_N1_'||v_instance_code);
1335 
1336          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ZONE_REGIONS_N1_'||v_instance_code);
1337 
1338     EXCEPTION
1339     WHEN OTHERS THEN
1340          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ZONE_REGIONS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1341     END;
1342 
1343    BEGIN
1344     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1345                            application_short_name => 'MSC',
1346                            statement_type => AD_DDL.CREATE_INDEX,
1347                            statement =>
1348                  'create index MSC_ST_REGION_LOC_N1_'||v_instance_code
1349               ||' on MSC_ST_REGION_LOCATIONS '
1350               ||'(sr_instance_code, country, state, city, postal_code_from, postal_code_to, location_code ) '
1351               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1352                            object_name =>'MSC_ST_REGION_LOC_N1_'||v_instance_code);
1353 
1354          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_REGION_LOC_N1_'||v_instance_code);
1355 
1356     EXCEPTION
1357     WHEN OTHERS THEN
1358          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_REGION_LOC_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1359     END;
1360 
1361     BEGIN
1362     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1363                            application_short_name => 'MSC',
1364                            statement_type => AD_DDL.CREATE_INDEX,
1365                            statement =>
1366                  'create index MSC_ST_REGION_SITE_N1_'||v_instance_code
1367               ||' on MSC_ST_REGION_SITES '
1368               ||'(sr_instance_code, country, state, city, postal_code_from, postal_code_to, vendor_name, vendor_site_code ) '
1369               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1370                            object_name =>'MSC_ST_REGION_SITE_N1_'||v_instance_code);
1371 
1372          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_REGION_SITE_N1_'||v_instance_code);
1373 
1374     EXCEPTION
1375     WHEN OTHERS THEN
1376          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_REGION_SITE_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1377     END;
1378 
1379     msc_analyse_tables_pk.analyse_table( 'MSC_ST_ITEM_SOURCING', v_instance_id, -1);
1380     msc_analyse_tables_pk.analyse_table( 'MSC_ST_REGIONS', v_instance_id, -1);
1381     msc_analyse_tables_pk.analyse_table( 'MSC_ST_ZONE_REGIONS', v_instance_id, -1);
1382     msc_analyse_tables_pk.analyse_table( 'MSC_ST_REGION_LOCATIONS', v_instance_id, -1);
1383     msc_analyse_tables_pk.analyse_table( 'MSC_ST_REGION_SITES', v_instance_id, -1);
1384 
1385   END IF;
1386 
1387   IF v_res_dmd_enabled = SYS_YES THEN
1388 
1389    BEGIN
1390 
1391     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1392                            application_short_name => 'MSC',
1393                            statement_type => AD_DDL.CREATE_INDEX,
1394                            statement =>
1395                  'create index MSC_ST_RES_REQ_N1_'||v_instance_code
1396               ||' on MSC_ST_RESOURCE_REQUIREMENTS '
1397               ||'(sr_instance_code, resource_code, wip_entity_name, operation_seq_code, organization_code, company_name, resource_seq_code) '
1398               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1399                            object_name =>'MSC_ST_RES_REQ_N1_'||v_instance_code);
1400 
1401          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_RES_REQ_N1_'||v_instance_code);
1402 
1403     EXCEPTION
1404     WHEN OTHERS THEN
1405          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_RES_REQ_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1406     END;
1407 
1408     msc_analyse_tables_pk.analyse_table( 'MSC_ST_RESOURCE_REQUIREMENTS', v_instance_id, -1);
1409 
1410   END IF;
1411 
1412   IF v_uom_class_enabled = SYS_YES THEN
1413 
1414    BEGIN
1415 
1416     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1417                            application_short_name => 'MSC',
1418                            statement_type => AD_DDL.CREATE_INDEX,
1419                            statement =>
1420                  'create index MSC_ST_UOM_CLS_CONV_N1_'||v_instance_code
1421               ||' on MSC_ST_UOM_CLASS_CONVERSIONS '
1422               ||'( from_uom_code, to_uom_code, item_name, company_name) '
1423               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1424                            object_name =>'MSC_ST_UOM_CLS_CONV_N1_'||v_instance_code);
1425 
1426          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_UOM_CLS_CONV_N1_'||v_instance_code);
1427 
1428     EXCEPTION
1429     WHEN OTHERS THEN
1430          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_UOM_CLS_CONV_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1431     END;
1432 
1433     msc_analyse_tables_pk.analyse_table( 'MSC_ST_UOM_CLASS_CONVERSIONS', v_instance_id, -1);
1434 
1435   END IF;
1436 
1437   IF v_sup_cap_enabled = SYS_YES THEN
1438 
1439      BEGIN
1440      ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1441                            application_short_name => 'MSC',
1442                            statement_type => AD_DDL.CREATE_INDEX,
1443                            statement =>
1444                  'create index MSC_ST_ITEM_SUP_N1_'||v_instance_code
1445               ||' on MSC_ST_ITEM_SUPPLIERS '
1446               ||'(sr_instance_code, item_name, vendor_name, organization_code, company_name, vendor_site_code) '
1447               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1448                            object_name =>'MSC_ST_ITEM_SUP_N1_'||v_instance_code);
1449 
1450 
1451          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ITEM_SUP_N1_'||v_instance_code);
1452 
1453     EXCEPTION
1454     WHEN OTHERS THEN
1455          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ITEM_SUP_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1456     END;
1457 
1458     BEGIN
1459     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1460                            application_short_name => 'MSC',
1461                            statement_type => AD_DDL.CREATE_INDEX,
1462                            statement =>
1463                  'create index MSC_ST_SUP_CAP_N1_'||v_instance_code
1464               ||' on MSC_ST_SUPPLIER_CAPACITIES '
1465               ||'(sr_instance_code, item_name, vendor_name, from_date,organization_code,company_name,vendor_site_code) '
1466               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1467                            object_name =>'MSC_ST_SUP_CAP_N1_'||v_instance_code);
1468 
1469          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SUP_CAP_N1_'||v_instance_code);
1470 
1471     EXCEPTION
1472     WHEN OTHERS THEN
1473          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SUP_CAP_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1474     END;
1475 
1476     BEGIN
1477     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1478                            application_short_name => 'MSC',
1479                            statement_type => AD_DDL.CREATE_INDEX,
1480                            statement =>
1481                  'create index MSC_ST_SUP_FENCES_N1_'||v_instance_code
1482               ||' on MSC_ST_SUPPLIER_FLEX_FENCES '
1483               ||'(sr_instance_code, item_name,vendor_name,fence_days,organization_code,company_name,vendor_site_code) '
1484               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1485                            object_name =>'MSC_ST_SUP_FENCES_N1_'||v_instance_code);
1486 
1487          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SUP_FENCES_N1_'||v_instance_code);
1488 
1489     EXCEPTION
1490     WHEN OTHERS THEN
1491          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SUP_FENCES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1492     END;
1493 
1494     msc_analyse_tables_pk.analyse_table( 'MSC_ST_ITEM_SUPPLIERS', v_instance_id, -1);
1495     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SUPPLIER_CAPACITIES', v_instance_id, -1);
1496     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SUPPLIER_FLEX_FENCES', v_instance_id, -1);
1497 
1498   END IF;
1499 
1500   IF v_safety_stk_enabled = SYS_YES THEN
1501 
1502     BEGIN
1503 
1504     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1505                            application_short_name => 'MSC',
1506                            statement_type => AD_DDL.CREATE_INDEX,
1507                            statement =>
1508                  'create index MSC_ST_SAFETY_STOCKS_N1_'||v_instance_code
1509               ||' on MSC_ST_SAFETY_STOCKS '
1510               ||'(sr_instance_code,organization_code,item_name,period_start_date,company_name) '
1511               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1512                            object_name =>'MSC_ST_SAFETY_STOCKS_N1_'||v_instance_code);
1513 
1514          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SAFETY_STOCKS_N1_'||v_instance_code);
1515 
1516     EXCEPTION
1517     WHEN OTHERS THEN
1518          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SAFETY_STOCKS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1519     END;
1520   END IF;
1521 
1522   IF v_item_substitute_enabled = SYS_YES THEN
1523 
1524     BEGIN
1525 
1526     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1527                            application_short_name => 'MSC',
1528                            statement_type => AD_DDL.CREATE_INDEX,
1529                            statement =>
1530                  'create index MSC_ST_ITEM_SUBSTITUTES_N1_'||v_instance_code
1531               ||' on MSC_ST_ITEM_SUBSTITUTES '
1532               ||' (sr_instance_code,item_name,substitute_item_name,customer_name,'
1533               ||' customer_site_code,substitution_set,company_name) '
1534               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1535                            object_name =>'MSC_ST_SAFETY_STOCKS_N1_'||v_instance_code);
1536 
1537          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_ITEM_SUBSTITUTES_N1_'||v_instance_code);
1538 
1539     EXCEPTION
1540     WHEN OTHERS THEN
1541          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_ITEM_SUBSTITUTES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1542     END;
1543 
1544     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SAFETY_STOCKS', v_instance_id, -1);
1545     msc_analyse_tables_pk.analyse_table( 'MSC_ST_ITEM_SUBSTITUTES', v_instance_id, -1);
1546 
1547   END IF;
1548 
1549   IF v_planners_enabled = SYS_YES THEN
1550 
1551     BEGIN
1552 
1553     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1554                            application_short_name => 'MSC',
1555                            statement_type => AD_DDL.CREATE_INDEX,
1556                            statement =>
1557                  'create index MSC_ST_PLANNERS_N1_'||v_instance_code
1558               ||' on MSC_ST_PLANNERS '
1559               ||'(sr_instance_code,organization_code,planner_code,company_name) '
1560               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1561                            object_name =>'MSC_ST_PLANNERS_N1_'||v_instance_code);
1562 
1563          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_PLANNERS_N1_'||v_instance_code);
1564 
1565     EXCEPTION
1566     WHEN OTHERS THEN
1567          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_PLANNERS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1568     END;
1569 
1570     msc_analyse_tables_pk.analyse_table( 'MSC_ST_PLANNERS', v_instance_id, -1);
1571 
1572   END IF;
1573 
1574   IF v_mat_sup_enabled = SYS_YES THEN
1575 
1576     BEGIN
1577     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1578                            application_short_name => 'MSC',
1579                            statement_type => AD_DDL.CREATE_INDEX,
1580                            statement =>
1581                  'create index MSC_ST_JOB_OP_NETWORKS_N1_'||v_instance_code
1582               ||' on MSC_ST_JOB_OPERATION_NETWORKS '
1583               ||'(SR_INSTANCE_CODE, WIP_ENTITY_NAME, FROM_OPERATION_SEQ_CODE, TO_OPERATION_SEQ_CODE) '
1584               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1585                            object_name =>'MSC_ST_JOB_OP_NETWORKS_N1_'||v_instance_code);
1586          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_JOB_OP_NETWORKS_N1_'||v_instance_code);
1587 
1588     EXCEPTION
1589     WHEN OTHERS THEN
1590          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_JOB_OP_NETWORKS_N1_'||v_instance_code ||'. Error:'||substr(SQLERRM,1,240));
1591     END;
1592 
1593     BEGIN
1594     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1595                            application_short_name => 'MSC',
1596                            statement_type => AD_DDL.CREATE_INDEX,
1597                            statement =>
1598                  'create index MSC_ST_JOB_OPERATIONS_N1_'||v_instance_code
1599               ||' on MSC_ST_JOB_OPERATIONS '
1600               ||'(SR_INSTANCE_CODE, ORGANIZATION_CODE, WIP_ENTITY_NAME, OPERATION_SEQ_CODE ) '
1601               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1602                            object_name =>'MSC_ST_JOB_OPERATIONS_N1_'||v_instance_code);
1603          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_JOB_OPERATIONS_N1_'||v_instance_code);
1604 
1605     EXCEPTION
1606     WHEN OTHERS THEN
1607          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_JOB_OPERATIONS_N1_'||v_instance_code ||'. Error:'||substr(SQLERRM,1,240));
1608     END;
1609 
1610     BEGIN
1611     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1612                            application_short_name => 'MSC',
1613                            statement_type => AD_DDL.CREATE_INDEX,
1614                            statement =>
1615                  'create index MSC_ST_JOB_REQ_OPS_N1_'||v_instance_code
1616               ||' on MSC_ST_JOB_REQUIREMENT_OPS '
1617               ||'(SR_INSTANCE_CODE, ORGANIZATION_CODE, WIP_ENTITY_NAME, OPERATION_SEQ_CODE, COMPONENT_NAME, PRIMARY_COMPONENT_NAME ) '
1618               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1619                            object_name =>'MSC_ST_JOB_REQ_OPS_N1_'||v_instance_code);
1620          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_JOB_REQ_OPS_N1_'||v_instance_code);
1621 
1622     EXCEPTION
1623     WHEN OTHERS THEN
1624          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_JOB_REQ_OPS_N1_'||v_instance_code ||'. Error:'||substr(SQLERRM,1,240));
1625     END;
1626 
1627     BEGIN
1628     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1629                            application_short_name => 'MSC',
1630                            statement_type => AD_DDL.CREATE_INDEX,
1631                            statement =>
1632                  'create index MSC_ST_JOB_OP_RESOURCES_N1_'||v_instance_code
1633               ||' on MSC_ST_JOB_OP_RESOURCES '
1634               ||'(SR_INSTANCE_CODE, ORGANIZATION_CODE, WIP_ENTITY_NAME, OPERATION_SEQ_CODE, RESOURCE_SEQ_CODE, ALTERNATE_NUM, RESOURCE_CODE ) '
1635               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1636                            object_name =>'MSC_ST_JOB_OP_RESOURCES_N1_'||v_instance_code);
1637          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_JOB_OP_RESOURCES_N1_'||v_instance_code);
1638 
1639     EXCEPTION
1640     WHEN OTHERS THEN
1641          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_JOB_OP_RESOURCES_N1_'||v_instance_code ||'. Error:'||substr(SQLERRM,1,240));
1642     END;
1643 
1644   END IF;
1645 
1646   IF v_cal_assignment_enabled = SYS_YES THEN
1647 
1648     BEGIN
1649 
1650     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1651                            application_short_name => 'MSC',
1652                            statement_type => AD_DDL.CREATE_INDEX,
1653                            statement =>
1654                  'create index MSC_ST_CAL_ASSIGNMENTS_N1_'||v_instance_code
1655               ||' on MSC_ST_CALENDAR_ASSIGNMENTS '
1656               ||'(sr_instance_code,association_type, calendar_code, calendar_type, partner_name, partner_site_code, organization_code, ship_method_code) '
1657               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1658                            object_name =>'MSC_ST_CAL_ASSIGNMENTS_N1_'||v_instance_code);
1659 
1660          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_CAL_ASSIGNMENTS_N1_'||v_instance_code);
1661 
1662     EXCEPTION
1663     WHEN OTHERS THEN
1664          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_CAL_ASSIGNMENTS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1665     END;
1666 
1667     msc_analyse_tables_pk.analyse_table( 'MSC_ST_CALENDAR_ASSIGNMENTS', v_instance_id, -1);
1668 
1669   END IF;
1670 
1671 
1672 ELSIF p_batch_index = 'ITEM' THEN
1673 
1674     BEGIN
1675 
1676     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1677                            application_short_name => 'MSC',
1678                            statement_type => AD_DDL.CREATE_INDEX,
1679                            statement =>
1680                  'create index MSC_ST_SYSTEM_ITEMS_N2_'||v_instance_code
1681               ||' on MSC_ST_SYSTEM_ITEMS '
1682               ||' (sr_instance_code,batch_id,sr_inventory_item_id, deleted_flag) '
1683               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1684                            object_name => 'MSC_ST_SYSTEM_ITEMS_N2_'||v_instance_code);
1685 
1686 
1687          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SYSTEM_ITEMS_N2_'||v_instance_code);
1688 
1689         EXCEPTION
1690     WHEN OTHERS THEN
1691          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SYSTEM_ITEMS_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1692     END;
1693 
1694     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SYSTEM_ITEMS', v_instance_id, -1);
1695 
1696 ELSIF p_batch_index = 'SUPPLY' THEN
1697 
1698     BEGIN
1699 
1700     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1701                            application_short_name => 'MSC',
1702                            statement_type => AD_DDL.CREATE_INDEX,
1703                            statement =>
1704                  'create index MSC_ST_SUPPLIES_N5_'||v_instance_code
1705               ||' on MSC_ST_SUPPLIES '
1706               ||' (batch_id, order_type, sr_instance_code, deleted_flag) '
1707               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1708                            object_name => 'MSC_ST_SUPPLIES_N5_'||v_instance_code);
1709 
1710          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SUPPLIES_N5_'||v_instance_code);
1711 
1712     EXCEPTION
1713     WHEN OTHERS THEN
1714          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SUPPLIES_N5_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1715     END;
1716 
1717     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SUPPLIES', v_instance_id, -1);
1718 
1719 ELSIF p_batch_index = 'DEMAND' THEN
1720 
1721     BEGIN
1722 
1723     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1724                            application_short_name => 'MSC',
1725                            statement_type => AD_DDL.CREATE_INDEX,
1726                            statement =>
1727                  'create index MSC_ST_DEMANDS_N3_'||v_instance_code
1728               ||' on MSC_ST_DEMANDS '
1729               ||' (batch_id, origination_type, sr_instance_code, deleted_flag) '
1730               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1731                            object_name => 'MSC_ST_DEMANDS_N3_'||v_instance_code);
1732 
1733          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_DEMANDS_N3_'||v_instance_code);
1734 
1735     EXCEPTION
1736     WHEN OTHERS THEN
1737          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_DEMANDS_N3_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1738     END;
1739 
1740     msc_analyse_tables_pk.analyse_table( 'MSC_ST_DEMANDS', v_instance_id, -1);
1741 
1742 ELSIF p_batch_index = 'SO' THEN
1743 
1744     BEGIN
1745     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1746                            application_short_name => 'MSC',
1747                            statement_type => AD_DDL.CREATE_INDEX,
1748                            statement =>
1749                  'create index MSC_ST_SO_N2_'||v_instance_code
1750               ||' on MSC_ST_SALES_ORDERS '
1751               ||' (sales_order_number, batch_id ,sr_instance_code, deleted_flag) '
1752               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1753                            object_name => 'MSC_ST_SO_N2_'||v_instance_code);
1754 
1755          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SO_N2_'||v_instance_code);
1756 
1757     EXCEPTION
1758     WHEN OTHERS THEN
1759          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SO_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1760     END;
1761 
1762     BEGIN
1763     ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1764                            application_short_name => 'MSC',
1765                            statement_type => AD_DDL.CREATE_INDEX,
1766                            statement =>
1767                  'create index MSC_ST_SO_N3_'||v_instance_code
1768               ||' on MSC_ST_SALES_ORDERS '
1769               ||' (sr_instance_code, batch_id , deleted_flag) '
1770               ||' STORAGE (INITIAL 100K NEXT 1M PCTINCREASE 0) ',
1771                            object_name => 'MSC_ST_SO_N3_'||v_instance_code);
1772 
1773          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Created Index MSC_ST_SO_N3_'||v_instance_code);
1774 
1775     EXCEPTION
1776     WHEN OTHERS THEN
1777          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error creating Index MSC_ST_SO_N3_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1778     END;
1779 
1780     msc_analyse_tables_pk.analyse_table( 'MSC_ST_SALES_ORDERS', v_instance_id, -1);
1781 
1782 END IF;
1783 
1784 EXCEPTION
1785 when others then
1786 MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'CREATE_INDEXES: '||dbms_utility.format_error_stack );
1787 END CREATE_INDEXES;
1788 
1789 -- ===== DROP_INDEXES================
1790 PROCEDURE DROP_INDEXES IS
1791 BEGIN
1792 
1793   IF v_item_enabled = SYS_YES THEN
1794 
1795     BEGIN
1796           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1797                          application_short_name => 'MSC',
1798                          statement_type => AD_DDL.DROP_INDEX,
1799                          statement =>
1800                 'drop index MSC_ST_SYSTEM_ITEMS_N1_'||v_instance_code,
1801                          object_name => 'MSC_ST_SYSTEM_ITEMS_N1_'||v_instance_code);
1802 
1803                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SYSTEM_ITEMS_N1_'||v_instance_code);
1804 
1805     EXCEPTION
1806     WHEN OTHERS THEN
1807          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SYSTEM_ITEMS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1808     END;
1809 
1810     BEGIN
1811           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1812                          application_short_name => 'MSC',
1813                          statement_type => AD_DDL.DROP_INDEX,
1814                          statement =>
1815                 'drop index MSC_ST_SYSTEM_ITEMS_N2_'||v_instance_code,
1816                          object_name => 'MSC_ST_SYSTEM_ITEMS_N2_'||v_instance_code);
1817 
1818                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SYSTEM_ITEMS_N2_'||v_instance_code);
1819 
1820     EXCEPTION
1821     WHEN OTHERS THEN
1822          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SYSTEM_ITEMS_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1823     END;
1824 
1825     msc_analyse_tables_pk.analyse_table( 'MSC_LOCAL_ID_ITEM', v_instance_id, -1);
1826 
1827   END IF;
1828 
1829   IF v_mat_sup_enabled = SYS_YES THEN
1830 
1831     BEGIN
1832           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1833                          application_short_name => 'MSC',
1834                          statement_type => AD_DDL.DROP_INDEX,
1835                          statement =>
1836                 'drop index MSC_ST_SUPPLIES_N1_'||v_instance_code,
1837                          object_name => 'MSC_ST_SUPPLIES_N1_'||v_instance_code);
1838 
1839                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SUPPLIES_N1_'||v_instance_code);
1840 
1841     EXCEPTION
1842     WHEN OTHERS THEN
1843          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SUPPLIES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1844     END;
1845 
1846     BEGIN
1847           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1848                          application_short_name => 'MSC',
1849                          statement_type => AD_DDL.DROP_INDEX,
1850                          statement =>
1851                 'drop index MSC_ST_SUPPLIES_N2_'||v_instance_code,
1852                          object_name => 'MSC_ST_SUPPLIES_N2_'||v_instance_code);
1853 
1854                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SUPPLIES_N2_'||v_instance_code);
1855     EXCEPTION
1856     WHEN OTHERS THEN
1857          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SUPPLIES_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1858     END;
1859 
1860     BEGIN
1861           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1862                          application_short_name => 'MSC',
1863                          statement_type => AD_DDL.DROP_INDEX,
1864                          statement =>
1865                 'drop index MSC_ST_SUPPLIES_N3_'||v_instance_code,
1866                          object_name => 'MSC_ST_SUPPLIES_N3_'||v_instance_code);
1867 
1868                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SUPPLIES_N3_'||v_instance_code);
1869     EXCEPTION
1870     WHEN OTHERS THEN
1871          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SUPPLIES_N3_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1872     END;
1873 
1874     BEGIN
1875           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1876                          application_short_name => 'MSC',
1877                          statement_type => AD_DDL.DROP_INDEX,
1878                          statement =>
1879                 'drop index MSC_ST_SUPPLIES_N4_'||v_instance_code,
1880                          object_name => 'MSC_ST_SUPPLIES_N4_'||v_instance_code);
1881 
1882                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SUPPLIES_N4_'||v_instance_code);
1883     EXCEPTION
1884     WHEN OTHERS THEN
1885          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SUPPLIES_N4_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1886     END;
1887 
1888     BEGIN
1889           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1890                          application_short_name => 'MSC',
1891                          statement_type => AD_DDL.DROP_INDEX,
1892                          statement =>
1893                 'drop index MSC_ST_SUPPLIES_N5_'||v_instance_code,
1894                          object_name => 'MSC_ST_SUPPLIES_N5_'||v_instance_code);
1895 
1896                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SUPPLIES_N5_'||v_instance_code);
1897 
1898     EXCEPTION
1899     WHEN OTHERS THEN
1900          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SUPPLIES_N5_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1901     END;
1902 
1903      msc_analyse_tables_pk.analyse_table( 'MSC_LOCAL_ID_SUPPLY', v_instance_id, -1);
1904 
1905   END IF;
1906 
1907   IF v_mat_dmd_enabled = SYS_YES THEN
1908 
1909     BEGIN
1910           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1911                          application_short_name => 'MSC',
1912                          statement_type => AD_DDL.DROP_INDEX,
1913                          statement =>
1914                 'drop index MSC_ST_DEMANDS_N1_'||v_instance_code,
1915                          object_name => 'MSC_ST_DEMANDS_N1_'||v_instance_code);
1916 
1917                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_DEMANDS_N1_'||v_instance_code);
1918     EXCEPTION
1919     WHEN OTHERS THEN
1920          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_DEMANDS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1921     END;
1922 
1923     BEGIN
1924           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1925                          application_short_name => 'MSC',
1926                          statement_type => AD_DDL.DROP_INDEX,
1927                          statement =>
1928                 'drop index MSC_ST_DEMANDS_N2_'||v_instance_code,
1929                          object_name => 'MSC_ST_DEMANDS_N2_'||v_instance_code);
1930 
1931                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_DEMANDS_N2_'||v_instance_code);
1932     EXCEPTION
1933     WHEN OTHERS THEN
1934          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_DEMANDS_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1935     END;
1936 
1937     BEGIN
1938           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1939                          application_short_name => 'MSC',
1940                          statement_type => AD_DDL.DROP_INDEX,
1941                          statement =>
1942                 'drop index MSC_ST_DEMANDS_N3_'||v_instance_code,
1943                          object_name => 'MSC_ST_DEMANDS_N3_'||v_instance_code);
1944 
1945                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_DEMANDS_N3_'||v_instance_code);
1946 
1947     EXCEPTION
1948     WHEN OTHERS THEN
1949          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_DEMANDS_N3_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1950     END;
1951 
1952     BEGIN
1953           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1954                          application_short_name => 'MSC',
1955                          statement_type => AD_DDL.DROP_INDEX,
1956                          statement =>
1957                 'drop index MSC_ST_SO_N1_'||v_instance_code,
1958                          object_name => 'MSC_ST_SO_N1_'||v_instance_code);
1959 
1960                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SO_N1_'||v_instance_code);
1961     EXCEPTION
1962     WHEN OTHERS THEN
1963          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SO_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1964     END;
1965 
1966     BEGIN
1967           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1968                          application_short_name => 'MSC',
1969                          statement_type => AD_DDL.DROP_INDEX,
1970                          statement =>
1971                 'drop index MSC_ST_SO_N2_'||v_instance_code,
1972                          object_name => 'MSC_ST_SO_N2_'||v_instance_code);
1973 
1974                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SO_N2_'||v_instance_code);
1975     EXCEPTION
1976     WHEN OTHERS THEN
1977          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SO_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1978     END;
1979 
1980     BEGIN
1981           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
1982                          application_short_name => 'MSC',
1983                          statement_type => AD_DDL.DROP_INDEX,
1984                          statement =>
1985                 'drop index MSC_ST_SO_N3_'||v_instance_code,
1986                          object_name => 'MSC_ST_SO_N3_'||v_instance_code);
1987 
1988                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SO_N3_'||v_instance_code);
1989 
1990     EXCEPTION
1991     WHEN OTHERS THEN
1992          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SO_N3_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
1993     END;
1994 
1995     msc_analyse_tables_pk.analyse_table( 'MSC_LOCAL_ID_DEMAND', v_instance_id, -1);
1996 
1997   END IF;
1998 
1999  IF v_reserve_enabled = SYS_YES THEN
2000     BEGIN
2001           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2002                          application_short_name => 'MSC',
2003                          statement_type => AD_DDL.DROP_INDEX,
2004                          statement =>
2005                 'drop index MSC_ST_RESERVE_N1_'||v_instance_code,
2006                          object_name => 'MSC_ST_RESERVE_N1_'||v_instance_code);
2007 
2008                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_RESERVE_N1_'||v_instance_code);
2009     EXCEPTION
2010     WHEN OTHERS THEN
2011          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_RESERVE_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2012     END;
2013   END IF;
2014 
2015   IF v_bom_enabled = SYS_YES THEN
2016 
2017     BEGIN
2018           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2019                          application_short_name => 'MSC',
2020                          statement_type => AD_DDL.DROP_INDEX,
2021                          statement =>
2022                 'drop index MSC_ST_BOMS_N1_'||v_instance_code,
2023                          object_name => 'MSC_ST_BOMS_N1_'||v_instance_code);
2024 
2025                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_BOMS_N1_'||v_instance_code);
2026     EXCEPTION
2027     WHEN OTHERS THEN
2028          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_BOMS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2029     END;
2030 
2031     BEGIN
2032           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2033                          application_short_name => 'MSC',
2034                          statement_type => AD_DDL.DROP_INDEX,
2035                          statement =>
2036                 'drop index MSC_ST_BOM_COMP_N1_'||v_instance_code,
2037                          object_name => 'MSC_ST_BOM_COMP_N1_'||v_instance_code);
2038 
2039                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_BOM_COMP_N1_'||v_instance_code);
2040     EXCEPTION
2041     WHEN OTHERS THEN
2042          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_BOM_COMP_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2043     END;
2044 
2045     BEGIN
2046           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2047                          application_short_name => 'MSC',
2048                          statement_type => AD_DDL.DROP_INDEX,
2049                          statement =>
2050                 'drop index MSC_ST_COMP_SUB_N1_'||v_instance_code,
2051                          object_name => 'MSC_ST_COMP_SUB_N1_'||v_instance_code);
2052 
2053                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_COMP_SUB_N1_'||v_instance_code);
2054 
2055     EXCEPTION
2056     WHEN OTHERS THEN
2057          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_COMP_SUB_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2058     END;
2059 
2060      --  Added for Co Products (OSFM Integration --
2061     BEGIN
2062           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2063                          application_short_name => 'MSC',
2064                          statement_type => AD_DDL.DROP_INDEX,
2065                          statement =>
2066                 'drop index MSC_ST_CO_PRODUCTS_N1_'||v_instance_code,
2067                          object_name => 'MSC_ST_CO_PRODUCTS_N1_'||v_instance_code);
2068 
2069                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_CO_PRODUCTS_N1_'||v_instance_code);
2070     EXCEPTION
2071     WHEN OTHERS THEN
2072          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_CO_PRODUCTS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2073     END;
2074 
2075   END IF;
2076 
2077   IF v_rtg_enabled = SYS_YES THEN
2078 
2079     BEGIN
2080           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2081                          application_short_name => 'MSC',
2082                          statement_type => AD_DDL.DROP_INDEX,
2083                          statement =>
2084                 'drop index MSC_ST_ROUTINGS_N1_'||v_instance_code,
2085                          object_name => 'MSC_ST_ROUTINGS_N1_'||v_instance_code);
2086 
2087                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ROUTINGS_N1_'||v_instance_code);
2088     EXCEPTION
2089     WHEN OTHERS THEN
2090          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ROUTINGS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2091     END;
2092 
2093     BEGIN
2094           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2095                          application_short_name => 'MSC',
2096                          statement_type => AD_DDL.DROP_INDEX,
2097                          statement =>
2098                 'drop index MSC_ST_ROUTING_OPER_N1_'||v_instance_code,
2099                          object_name => 'MSC_ST_ROUTING_OPER_N1_'||v_instance_code);
2100 
2101                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ROUTING_OPER_N1_'||v_instance_code);
2102 
2103     EXCEPTION
2104     WHEN OTHERS THEN
2105          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ROUTING_OPER_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2106     END;
2107 
2108     BEGIN
2109           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2110                          application_short_name => 'MSC',
2111                          statement_type => AD_DDL.DROP_INDEX,
2112                          statement =>
2113                 'drop index MSC_ST_OPER_RES_N1_'||v_instance_code,
2114                          object_name => 'MSC_ST_OPER_RES_N1_'||v_instance_code);
2115 
2116                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_OPER_RES_N1_'||v_instance_code);
2117 
2118     EXCEPTION
2119     WHEN OTHERS THEN
2120          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_OPER_RES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2121     END;
2122 
2123      -- Added for Operation Networks (OSFM Integration)--
2124     BEGIN
2125           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2126                          application_short_name => 'MSC',
2127                          statement_type => AD_DDL.DROP_INDEX,
2128                          statement =>
2129                 'drop index MSC_ST_OPER_NTWK_N1_'||v_instance_code,
2130                          object_name => 'MSC_ST_OPER_NTWK_N1_'||v_instance_code);
2131 
2132 
2133                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_OPER_NTWK_N1_'||v_instance_code);
2134 
2135     EXCEPTION
2136     WHEN OTHERS THEN
2137          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_OPER_NTWK_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2138     END;
2139 
2140   END IF;
2141 
2142   IF v_item_cat_enabled = SYS_YES THEN
2143 
2144     BEGIN
2145           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2146                          application_short_name => 'MSC',
2147                          statement_type => AD_DDL.DROP_INDEX,
2148                          statement =>
2149                 'drop index MSC_ST_ITEM_CAT_N1_'||v_instance_code,
2150                          object_name => 'MSC_ST_ITEM_CAT_N1_'||v_instance_code);
2151 
2152                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ITEM_CAT_N1_'||v_instance_code);
2153 
2154     EXCEPTION
2155     WHEN OTHERS THEN
2156          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ITEM_CAT_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2157     END;
2158 
2159     BEGIN
2160           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2161                          application_short_name => 'MSC',
2162                          statement_type => AD_DDL.DROP_INDEX,
2163                          statement =>
2164                 'drop index MSC_ST_ITEM_CAT_N2_'||v_instance_code,
2165                          object_name => 'MSC_ST_ITEM_CAT_N2_'||v_instance_code);
2166 
2167                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ITEM_CAT_N2_'||v_instance_code);
2168 
2169     EXCEPTION
2170     WHEN OTHERS THEN
2171          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ITEM_CAT_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2172     END;
2173   END IF;
2174 
2175 
2176   IF v_resources_enabled = SYS_YES THEN
2177 
2178     BEGIN
2179           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2180                          application_short_name => 'MSC',
2181                          statement_type => AD_DDL.DROP_INDEX,
2182                          statement =>
2183                 'drop index MSC_ST_RESOURCE_GROUPS_N1_'||v_instance_code,
2184                          object_name => 'MSC_ST_RESOURCE_GROUPS_N1_'||v_instance_code);
2185 
2186                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_RESOURCE_GROUPS_N1_'||v_instance_code);
2187 
2188     EXCEPTION
2189     WHEN OTHERS THEN
2190          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_RESOURCE_GROUPS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2191     END;
2192 
2193     BEGIN
2194           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2195                          application_short_name => 'MSC',
2196                          statement_type => AD_DDL.DROP_INDEX,
2197                          statement =>
2198                 'drop index MSC_ST_DEPT_RES_N1_'||v_instance_code,
2199                          object_name => 'MSC_ST_DEPT_RES_N1_'||v_instance_code);
2200 
2201                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_DEPT_RES_N1_'||v_instance_code);
2202 
2203     EXCEPTION
2204     WHEN OTHERS THEN
2205          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_DEPT_RES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2206     END;
2207 
2208     BEGIN
2209           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2210                          application_short_name => 'MSC',
2211                          statement_type => AD_DDL.DROP_INDEX,
2212                          statement =>
2213                 'drop index MSC_ST_RES_SHIFTS_N1_'||v_instance_code,
2214                          object_name => 'MSC_ST_RES_SHIFTS_N1_'||v_instance_code);
2215 
2216                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_RES_SHIFTS_N1_'||v_instance_code);
2217 
2218     EXCEPTION
2219     WHEN OTHERS THEN
2220          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_RES_SHIFTS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2221     END;
2222 
2223     BEGIN
2224           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2225                          application_short_name => 'MSC',
2226                          statement_type => AD_DDL.DROP_INDEX,
2227                          statement =>
2228                 'drop index MSC_ST_RES_CHANGES_N1_'||v_instance_code,
2229                          object_name => 'MSC_ST_RES_CHANGES_N1_'||v_instance_code);
2230 
2231                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_RES_CHANGES_N1_'||v_instance_code);
2232 
2233 
2234     EXCEPTION
2235     WHEN OTHERS THEN
2236          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_RES_CHANGES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2237     END;
2238 
2239   END IF;
2240 
2241 
2242   IF v_project_enabled = SYS_YES THEN
2243 
2244     BEGIN
2245 
2246           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2247                          application_short_name => 'MSC',
2248                          statement_type => AD_DDL.DROP_INDEX,
2249                          statement =>
2250                 'drop index MSC_ST_PROJECT_TASKS_N1_'||v_instance_code,
2251                          object_name => 'MSC_ST_PROJECT_TASKS_N1_'||v_instance_code);
2252 
2253                  MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_PROJECT_TASKS_N1_'||v_instance_code);
2254 
2255 
2256     EXCEPTION
2257     WHEN OTHERS THEN
2258          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_PROJECT_TASKS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2259     END;
2260 
2261   END IF;
2262 
2263   IF v_dmd_class_enabled = SYS_YES THEN
2264 
2265     BEGIN
2266           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2267                          application_short_name => 'MSC',
2268                          statement_type => AD_DDL.DROP_INDEX,
2269                          statement =>
2270                 'drop index MSC_ST_DMD_CLASS_N1_'||v_instance_code,
2271                          object_name => 'MSC_ST_DMD_CLASS_N1_'||v_instance_code);
2272 
2273                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_DMD_CLASS_N1_'||v_instance_code);
2274 
2275     EXCEPTION
2276     WHEN OTHERS THEN
2277          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_DMD_CLASS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2278     END;
2279   END IF;
2280 
2281   IF v_tp_enabled = SYS_YES THEN
2282 
2283     BEGIN
2284           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2285                          application_short_name => 'MSC',
2286                          statement_type => AD_DDL.DROP_INDEX,
2287                          statement =>
2288                 'drop index MSC_ST_TRADING_PART_N1_'||v_instance_code,
2289                          object_name => 'MSC_ST_TRADING_PART_N1_'||v_instance_code);
2290 
2291                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_TRADING_PART_N1_'||v_instance_code);
2292 
2293     EXCEPTION
2294     WHEN OTHERS THEN
2295          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_TRADING_PART_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2296     END;
2297 
2298     BEGIN
2299           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2300                          application_short_name => 'MSC',
2301                          statement_type => AD_DDL.DROP_INDEX,
2302                          statement =>
2303                 'drop index MSC_ST_TRADING_PART_N2_'||v_instance_code,
2304                          object_name => 'MSC_ST_TRADING_PART_N2_'||v_instance_code);
2305 
2306                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_TRADING_PART_N2_'||v_instance_code);
2307 
2308     EXCEPTION
2309     WHEN OTHERS THEN
2310          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_TRADING_PART_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2311     END;
2312 
2313     BEGIN
2314           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2315                          application_short_name => 'MSC',
2316                          statement_type => AD_DDL.DROP_INDEX,
2317                          statement =>
2318                 'drop index MSC_ST_TRD_PART_SITES_N1_'||v_instance_code,
2319                          object_name => 'MSC_ST_TRD_PART_SITES_N1_'||v_instance_code);
2320 
2321                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_TRD_PART_SITES_N1_'||v_instance_code);
2322 
2323     EXCEPTION
2324     WHEN OTHERS THEN
2325          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_TRD_PART_SITES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2326     END;
2327 
2328     BEGIN
2329           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2330                          application_short_name => 'MSC',
2331                          statement_type => AD_DDL.DROP_INDEX,
2332                          statement =>
2333                 'drop index MSC_ST_SUB_INV_N1_'||v_instance_code,
2334                          object_name => 'MSC_ST_SUB_INV_N1_'||v_instance_code);
2335 
2336                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SUB_INV_N1_'||v_instance_code);
2337 
2338     EXCEPTION
2339     WHEN OTHERS THEN
2340          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SUB_INV_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2341     END;
2342 
2343     BEGIN
2344           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2345                          application_short_name => 'MSC',
2346                          statement_type => AD_DDL.DROP_INDEX,
2347                          statement =>
2348                 'drop index MSC_ST_LOC_ASC_N1_'||v_instance_code,
2349                          object_name => 'MSC_ST_LOC_ASC_N1_'||v_instance_code);
2350 
2351                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_LOC_ASC_N1_'||v_instance_code);
2352 
2353     EXCEPTION
2354     WHEN OTHERS THEN
2355          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_LOC_ASC_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2356     END;
2357 
2358     BEGIN
2359           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2360                          application_short_name => 'MSC',
2361                          statement_type => AD_DDL.DROP_INDEX,
2362                          statement =>
2363                 'drop index MSC_ST_PART_CONT_N1_'||v_instance_code,
2364                          object_name => 'MSC_ST_PART_CONT_N1_'||v_instance_code);
2365 
2366                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_PART_CONT_N1_'||v_instance_code);
2367     EXCEPTION
2368     WHEN OTHERS THEN
2369          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_PART_CONT_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2370     END;
2371 
2372     -- For Aeroexchange
2373     -- Drop index of MSC_ST_GROUP_COMPANIES
2374 
2375     BEGIN
2376           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2377                          application_short_name => 'MSC',
2378                          statement_type => AD_DDL.DROP_INDEX,
2379                          statement =>
2380                 'drop index MSC_ST_GROUP_COMPANIES_N1_'||v_instance_code,
2381                          object_name => 'MSC_ST_GROUP_COMPANIES_N1_'||v_instance_code);
2382 
2383                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_GROUP_COMPANIES_N1_'||v_instance_code);
2384 
2385     EXCEPTION
2386     WHEN OTHERS THEN
2387          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_GROUP_COMPANIES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2388     END;
2389 
2390 
2391     ---- Drop index of MSC_ST_GROUPS
2392     BEGIN
2393           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2394                          application_short_name => 'MSC',
2395                          statement_type => AD_DDL.DROP_INDEX,
2396                          statement =>
2397                 'drop index MSC_ST_GROUPS_N1_'||v_instance_code,
2398                          object_name => 'MSC_ST_GROUPS_N1_'||v_instance_code);
2399 
2400                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_GROUPS_N1_'||v_instance_code);
2401 
2402     EXCEPTION
2403     WHEN OTHERS THEN
2404          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_GROUPS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2405     END;
2406 
2407 
2408   END IF;
2409 
2410   -- For bug fix 2379391, dropping the indexes for MSC_ST_ITEM_CUSTOMERS
2411   IF v_item_cst_enabled = SYS_YES THEN
2412 
2413     BEGIN
2414           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2415                          application_short_name => 'MSC',
2416                          statement_type => AD_DDL.DROP_INDEX,
2417                          statement =>
2418                 'drop index MSC_ST_ITEM_CUSTOMERS_N1_'||v_instance_code,
2419                          object_name => 'MSC_ST_ITEM_CUSTOMERS_N1_'||v_instance_code);
2420 
2421                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ITEM_CUSTOMERS_N1_'||v_instance_code);
2422 
2423     EXCEPTION
2424     WHEN OTHERS THEN
2425          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ITEM_CUSTOMERS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2426     END;
2427 
2428     BEGIN
2429           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2430                          application_short_name => 'MSC',
2431                          statement_type => AD_DDL.DROP_INDEX,
2432                          statement =>
2433                 'drop index MSC_ST_ITEM_CUSTOMERS_N2_'||v_instance_code,
2434                          object_name => 'MSC_ST_ITEM_CUSTOMERS_N2_'||v_instance_code);
2435 
2436                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ITEM_CUSTOMERS_N2_'||v_instance_code);
2437 
2438     EXCEPTION
2439     WHEN OTHERS THEN
2440          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ITEM_CUSTOMERS_N2_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2441     END;
2442 
2443     msc_analyse_tables_pk.analyse_table( 'MSC_ST_ITEM_CUSTOMERS', v_instance_id, -1);
2444 
2445   END IF;
2446 
2447   IF v_ship_mthd_enabled = SYS_YES THEN
2448 
2449     BEGIN
2450           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2451                          application_short_name => 'MSC',
2452                          statement_type => AD_DDL.DROP_INDEX,
2453                          statement =>
2454                 'drop index MSC_ST_INTERORG_SHIP_N1_'||v_instance_code,
2455                          object_name => 'MSC_ST_INTERORG_SHIP_N1_'||v_instance_code);
2456 
2457                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_INTERORG_SHIP_N1_'||v_instance_code);
2458 
2459     EXCEPTION
2460     WHEN OTHERS THEN
2461          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_INTERORG_SHIP_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2462     END;
2463 END IF;
2464 
2465   IF v_sourcing_enabled = SYS_YES THEN
2466 
2467     BEGIN
2468           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2469                          application_short_name => 'MSC',
2470                          statement_type => AD_DDL.DROP_INDEX,
2471                          statement =>
2472                 'drop index MSC_ST_ITEM_SRC_N1_'||v_instance_code,
2473                          object_name => 'MSC_ST_ITEM_SRC_N1_'||v_instance_code);
2474 
2475                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ITEM_SRC_N1_'||v_instance_code);
2476 
2477     EXCEPTION
2478     WHEN OTHERS THEN
2479          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ITEM_SRC_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2480     END;
2481 
2482     BEGIN
2483           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2484                          application_short_name => 'MSC',
2485                          statement_type => AD_DDL.DROP_INDEX,
2486                          statement =>
2487                 'drop index MSC_ST_REGIONS_N1_'||v_instance_code,
2488                          object_name => 'MSC_ST_REGIONS_N1_'||v_instance_code);
2489 
2490                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_REGIONS_N1_'||v_instance_code);
2491 
2492     EXCEPTION
2493     WHEN OTHERS THEN
2494          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_REGIONS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2495     END;
2496 
2497     BEGIN
2498           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2499                          application_short_name => 'MSC',
2500                          statement_type => AD_DDL.DROP_INDEX,
2501                          statement =>
2502                 'drop index MSC_ST_REGIONS_N1_'||v_instance_code,
2503                          object_name => 'MSC_ST_ZONE_REGIONS_N1_'||v_instance_code);
2504 
2505                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ZONE_REGIONS_N1_'||v_instance_code);
2506 
2507     EXCEPTION
2508     WHEN OTHERS THEN
2509          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ZONE_REGIONS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2510     END;
2511 
2512     BEGIN
2513           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2514                          application_short_name => 'MSC',
2515                          statement_type => AD_DDL.DROP_INDEX,
2516                          statement =>
2517                 'drop index MSC_ST_REGION_LOC_N1_'||v_instance_code,
2518                          object_name => 'MSC_ST_REGION_LOC_N1_'||v_instance_code);
2519 
2520                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_REGION_LOC_N1_'||v_instance_code);
2521 
2522     EXCEPTION
2523     WHEN OTHERS THEN
2524          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_REGION_LOC_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2525     END;
2526 
2527     BEGIN
2528           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2529                          application_short_name => 'MSC',
2530                          statement_type => AD_DDL.DROP_INDEX,
2531                          statement =>
2532                 'drop index MSC_ST_REGION_SITE_N1_'||v_instance_code,
2533                          object_name => 'MSC_ST_REGION_SITE_N1_'||v_instance_code);
2534 
2535                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_REGION_SITE_N1_'||v_instance_code);
2536 
2537     EXCEPTION
2538     WHEN OTHERS THEN
2539          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_REGION_SITE_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2540     END;
2541 
2542   END IF;
2543 
2544   IF v_res_dmd_enabled = SYS_YES THEN
2545 
2546     BEGIN
2547           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2548                          application_short_name => 'MSC',
2549                          statement_type => AD_DDL.DROP_INDEX,
2550                          statement =>
2551                 'drop index MSC_ST_RES_REQ_N1_'||v_instance_code,
2552                          object_name => 'MSC_ST_RES_REQ_N1_'||v_instance_code);
2553 
2554                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_RES_REQ_N1_'||v_instance_code);
2555 
2556     EXCEPTION
2557     WHEN OTHERS THEN
2558          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_RES_REQ_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2559     END;
2560   END IF;
2561 
2562   IF v_sup_cap_enabled = SYS_YES THEN
2563 
2564     BEGIN
2565           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2566                          application_short_name => 'MSC',
2567                          statement_type => AD_DDL.DROP_INDEX,
2568                          statement =>
2569                 'drop index MSC_ST_ITEM_SUP_N1_'||v_instance_code,
2570                          object_name => 'MSC_ST_ITEM_SUP_N1_'||v_instance_code);
2571 
2572                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ITEM_SUP_N1_'||v_instance_code);
2573 
2574     EXCEPTION
2575     WHEN OTHERS THEN
2576          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ITEM_SUP_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2577     END;
2578 
2579     BEGIN
2580           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2581                          application_short_name => 'MSC',
2582                          statement_type => AD_DDL.DROP_INDEX,
2583                          statement =>
2584                 'drop index MSC_ST_SUP_CAP_N1_'||v_instance_code,
2585                          object_name => 'MSC_ST_SUP_CAP_N1_'||v_instance_code);
2586 
2587                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SUP_CAP_N1_'||v_instance_code);
2588 
2589     EXCEPTION
2590     WHEN OTHERS THEN
2591          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SUP_CAP_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2592     END;
2593 
2594     BEGIN
2595           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2596                          application_short_name => 'MSC',
2597                          statement_type => AD_DDL.DROP_INDEX,
2598                          statement =>
2599                 'drop index MSC_ST_SUP_FENCES_N1_'||v_instance_code,
2600                          object_name => 'MSC_ST_SUP_FENCES_N1_'||v_instance_code);
2601 
2602 
2603                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SUP_FENCES_N1_'||v_instance_code);
2604 
2605     EXCEPTION
2606     WHEN OTHERS THEN
2607          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SUP_FENCES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2608     END;
2609 
2610   END IF;
2611 
2612   IF v_safety_stk_enabled = SYS_YES THEN
2613 
2614     BEGIN
2615           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2616                          application_short_name => 'MSC',
2617                          statement_type => AD_DDL.DROP_INDEX,
2618                          statement =>
2619                 'drop index MSC_ST_SAFETY_STOCKS_N1_'||v_instance_code,
2620                          object_name => 'MSC_ST_SAFETY_STOCKS_N1_'||v_instance_code);
2621 
2622 
2623                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_SAFETY_STOCKS_N1_'||v_instance_code);
2624 
2625     EXCEPTION
2626     WHEN OTHERS THEN
2627          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_SAFETY_STOCKS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2628     END;
2629 
2630   END IF;
2631 
2632   IF v_item_substitute_enabled = SYS_YES THEN
2633 
2634     BEGIN
2635           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2636                          application_short_name => 'MSC',
2637                          statement_type => AD_DDL.DROP_INDEX,
2638                          statement =>
2639                 'drop index MSC_ST_ITEM_SUBSTITUTES_N1_'||v_instance_code,
2640                          object_name => 'MSC_ST_ITEM_SUBSTITUTES_N1_'||v_instance_code);
2641 
2642                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_ITEM_SUBSTITUTES_N1_'||v_instance_code);
2643 
2644     EXCEPTION
2645     WHEN OTHERS THEN
2646          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_ITEM_SUBSTITUTES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2647     END;
2648 
2649   END IF;
2650 
2651   IF v_planners_enabled = SYS_YES THEN
2652 
2653     BEGIN
2654           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2655                          application_short_name => 'MSC',
2656                          statement_type => AD_DDL.DROP_INDEX,
2657                          statement =>
2658                 'drop index MSC_ST_PLANNERS_N1_'||v_instance_code,
2659                          object_name => 'MSC_ST_PLANNERS_N1_'||v_instance_code);
2660 
2661                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_PLANNERS_N1_'||v_instance_code);
2662 
2663     EXCEPTION
2664     WHEN OTHERS THEN
2665          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_PLANNERS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2666     END;
2667 
2668   END IF;
2669 
2670   IF v_uom_class_enabled = SYS_YES THEN
2671 
2672     BEGIN
2673           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2674                          application_short_name => 'MSC',
2675                          statement_type => AD_DDL.DROP_INDEX,
2676                          statement =>
2677                 'drop index MSC_ST_UOM_CLS_CONV_N1_'||v_instance_code,
2678                          object_name => 'MSC_ST_UOM_CLS_CONV_N1_'||v_instance_code);
2679 
2680                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_UOM_CLS_CONV_N1_'||v_instance_code);
2681 
2682     EXCEPTION
2683     WHEN OTHERS THEN
2684          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_UOM_CLS_CONV_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2685     END;
2686 
2687   END IF;
2688 
2689   IF v_mat_sup_enabled = SYS_YES THEN
2690 
2691     BEGIN
2692           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2693                          application_short_name => 'MSC',
2694                          statement_type => AD_DDL.DROP_INDEX,
2695                          statement =>
2696                 'drop index MSC_ST_JOB_OP_NETWORKS_N1_'||v_instance_code,
2697                          object_name => 'MSC_ST_JOB_OP_NETWORKS_N1_'||v_instance_code);
2698 
2699                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_JOB_OP_NETWORKS_N1_'||v_instance_code);
2700 
2701     EXCEPTION
2702     WHEN OTHERS THEN
2703          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_JOB_OP_NETWORKS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2704     END;
2705 
2706     BEGIN
2707           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2708                          application_short_name => 'MSC',
2709                          statement_type => AD_DDL.DROP_INDEX,
2710                          statement =>
2711                 'drop index MSC_ST_JOB_OPERATIONS_N1_'||v_instance_code,
2712                          object_name => 'MSC_ST_JOB_OPERATIONS_N1_'||v_instance_code);
2713 
2714                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_JOB_OPERATIONS_N1_'||v_instance_code);
2715 
2716     EXCEPTION
2717     WHEN OTHERS THEN
2718          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_JOB_OPERATIONS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2719     END;
2720 
2721     BEGIN
2722           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2723                          application_short_name => 'MSC',
2724                          statement_type => AD_DDL.DROP_INDEX,
2725                          statement =>
2726                 'drop index MSC_ST_JOB_REQ_OPS_N1_'||v_instance_code,
2727                          object_name => 'MSC_ST_JOB_REQ_OPS_N1_'||v_instance_code);
2728 
2729                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_JOB_REQ_OPS_N1_'||v_instance_code);
2730 
2731     EXCEPTION
2732     WHEN OTHERS THEN
2733          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_JOB_REQ_OPS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2734     END;
2735 
2736     BEGIN
2737           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2738                          application_short_name => 'MSC',
2739                          statement_type => AD_DDL.DROP_INDEX,
2740                          statement =>
2741                 'drop index MSC_ST_JOB_OP_RESOURCES_N1_'||v_instance_code,
2742                          object_name => 'MSC_ST_JOB_OP_RESOURCES_N1_'||v_instance_code);
2743 
2744                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_JOB_OP_RESOURCES_N1_'||v_instance_code);
2745 
2746     EXCEPTION
2747     WHEN OTHERS THEN
2748          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_JOB_OP_RESOURCES_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2749     END;
2750 
2751   END IF;
2752 
2753   IF v_cal_assignment_enabled = SYS_YES THEN
2754 
2755     BEGIN
2756           ad_ddl.do_ddl( applsys_schema => v_applsys_schema,
2757                          application_short_name => 'MSC',
2758                          statement_type => AD_DDL.DROP_INDEX,
2759                          statement =>
2760                 'drop index MSC_ST_CAL_ASSIGNMENTS_N1_'||v_instance_code,
2761                          object_name => 'MSC_ST_CAL_ASSIGNMENTS_N1_'||v_instance_code);
2762 
2763                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Dropped Index MSC_ST_CAL_ASSIGNMENTS_N1_'||v_instance_code);
2764 
2765     EXCEPTION
2766     WHEN OTHERS THEN
2767          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error dropping Index MSC_ST_CAL_ASSIGNMENTS_N1_'||v_instance_code||'. Error:'||substr(SQLERRM,1,240));
2768     END;
2769 
2770   END IF;
2771 
2772   msc_analyse_tables_pk.analyse_table( 'MSC_LOCAL_ID_SETUP', v_instance_id, -1);
2773   msc_analyse_tables_pk.analyse_table( 'MSC_LOCAL_ID_MISC', v_instance_id, -1);
2774 
2775 
2776 EXCEPTION
2777   when others then
2778   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'DROP_INDEXES: '||sqlerrm);
2779 END DROP_INDEXES;
2780 
2781 
2782 --========= Get Task Number =================
2783    FUNCTION GET_TASK_NUM(p_message VARCHAR2) RETURN NUMBER
2784    IS
2785 	lv_comma_pos       number:= 0;
2786 	lv_length                 number:= 0;
2787 	lv_task_num           number:= 0;
2788    BEGIN
2789       -- The message in the pipe could be of the form 'task_num,batch_id'
2790       -- or just 'task_num'. The comma should be in 2nd place or higher if present.
2791       -- to get the task num we find the length till the comma and then
2792       -- extract the task number
2793 
2794 	lv_comma_pos:= INSTR(p_message,',');
2795 	IF lv_comma_pos > 1 THEN
2796 		lv_length       := lv_comma_pos - 1;
2797 		lv_task_num := to_number(substr(p_message,1,lv_length));
2798 	ELSE
2799 		lv_task_num := to_number(p_message);
2800 	END IF;
2801 
2802 	RETURN lv_task_num;
2803 
2804    exception
2805    when others then
2806 	RETURN -1;
2807    end;
2808 
2809 
2810    FUNCTION GET_BATCH_ID(p_message VARCHAR2) RETURN NUMBER
2811    IS
2812 	lv_comma_pos   number := 0;
2813 	lv_start_pos        number := 0;
2814 	lv_batch_id         number := 0;
2815    BEGIN
2816       -- The message in the pipe would be of the form 'task_num,batch_id'
2817       -- or just 'task_num'. The comma should be in 2nd place or higher if present.
2818       -- to get the batch id we find the postion of  the comma and then
2819       -- extract the batch id
2820 
2821 	lv_comma_pos:= INSTR(p_message,',');
2822 	IF lv_comma_pos > 1 THEN
2823 		lv_start_pos:= lv_comma_pos + 1;
2824 		lv_batch_id := to_number(substr(p_message,lv_start_pos));
2825 	ELSE
2826 		lv_batch_id := -1;
2827 	END IF;
2828 
2829 	RETURN lv_batch_id;
2830 
2831    exception
2832    when others then
2833 	RETURN -1;
2834    end;
2835 
2836    PROCEDURE INITIALIZE( p_instance_id NUMBER)
2837    IS
2838       lv_last_refresh_type                VARCHAR2(1)  := '';
2839       lv_so_last_refresh_type             VARCHAR2(1)  := '';
2840       lv_retval                           BOOLEAN;
2841       lv_dummy1                           VARCHAR2(32) := '';
2842       lv_dummy2                           VARCHAR2(32) := '';
2843       lv_msc_config                       NUMBER := 1;
2844    BEGIN
2845 
2846       SELECT LRTYPE,
2847              SO_LRTYPE,
2848              APPS_VER,
2849              SYSDATE,
2850              SYSDATE,
2851              LRID,                     -- gets current refresh id
2852              FND_GLOBAL.USER_ID,
2853              SYSDATE,
2854              FND_GLOBAL.USER_ID,
2855              INSTANCE_CODE,
2856              INSTANCE_TYPE             -- OPM
2857         INTO lv_last_refresh_type,
2858              lv_so_last_refresh_type,
2859              v_apps_ver,
2860              START_TIME,
2861              v_current_date,
2862              v_refresh_id,
2863              v_current_user,
2864              v_current_date,
2865              v_current_user,
2866              v_instance_code,
2867              v_instance_type           -- OPM
2868         FROM MSC_APPS_INSTANCES
2869        WHERE INSTANCE_ID= p_instance_id;
2870 
2871 
2872       -- check whether SCE is installed
2873       BEGIN
2874          lv_msc_config := FND_PROFILE.VALUE('MSC_X_CONFIGURATION');
2875 
2876       EXCEPTION
2877          when others then
2878             lv_msc_config := 1;
2879       END;
2880 
2881       IF lv_msc_config IN (2,3) THEN
2882          v_sce_installed := true;
2883       ELSIF lv_msc_config = 1 THEN
2884          v_sce_installed := false;
2885       END IF;
2886 
2887 /*
2888       IF lv_last_refresh_type= 'C' THEN
2889          v_is_complete_refresh       := TRUE;
2890          v_is_incremental_refresh    := FALSE;
2891       ELSE
2892          v_is_complete_refresh       := FALSE;
2893          v_is_incremental_refresh    := TRUE;
2894       END IF;
2895 
2896       IF lv_so_last_refresh_type= 'C' THEN
2897          v_is_so_complete_refresh    := TRUE;
2898          v_is_so_incremental_refresh := FALSE;
2899       ELSE
2900          v_is_so_complete_refresh    := FALSE;
2901          v_is_so_incremental_refresh := TRUE;
2902       END IF;
2903 */
2904 
2905     v_pipe_task_que    := 'MSC_CL_PRE_TQ'||TO_CHAR(p_instance_id);
2906     v_pipe_wm          := 'MSC_CL_PRE_WM'||TO_CHAR(p_instance_id);
2907     v_pipe_mw          := 'MSC_CL_PRE_MW'||TO_CHAR(p_instance_id);
2908     v_pipe_status      := 'MSC_CL_PRE_ST'||TO_CHAR(p_instance_id);
2909     v_instance_id      :=  p_instance_id;
2910 
2911     MSC_ST_UTIL.v_instance_type := v_instance_type;
2912     MSC_ST_UTIL.v_instance_id := v_instance_id;
2913 
2914     --------------  Get current ERROR_ID from MSC_ERRORS ---
2915     -- SELECT max(ERROR_ID) INTO v_error_no_begin FROM MSC_ERRORS;
2916 
2917     ---------------- Set Flags -----------------------------
2918     -- set the flags as to whether discrete and/or process
2919     -- manufacturing are being used in the same instance
2920 
2921     v_discrete_flag  := SYS_NO;
2922     v_process_flag   := SYS_NO;
2923 
2924     IF v_instance_type = G_INS_DISCRETE OR
2925        v_instance_type = G_INS_MIXED    THEN
2926        v_discrete_flag := SYS_YES;
2927     END IF;
2928 
2929     IF v_instance_type = G_INS_PROCESS OR
2930        v_instance_type = G_INS_MIXED   THEN
2931        v_process_flag  := SYS_YES;
2932     END IF;
2933 
2934    --------- Get the Application Information ---------------------
2935     -- Get the aplication info, whether MSD(ODP) is installed or not
2936 
2937     lv_retval := FND_INSTALLATION.GET_APP_INFO(
2938                    'MSD', lv_dummy1,lv_dummy2, v_applsys_schema);
2939 
2940     IF (lv_dummy1 = 'I' OR lv_dummy1 = 'S')THEN
2941             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Oracle Demand Planning Installed');
2942             v_install_msd := TRUE ;
2943     END IF;
2944 
2945    -- Get the aplication info, whether MSC(ASCP) is installed or not
2946     lv_retval := FND_INSTALLATION.GET_APP_INFO(
2947                    'MSC', lv_dummy1,lv_dummy2, v_applsys_schema);
2948 
2949     IF (lv_dummy1 = 'I' OR lv_dummy1 = 'S')THEN
2950             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Oracle ASCP Installed');
2951             v_install_msc := TRUE ;
2952     END IF;
2953 
2954     -- set thr v_applsys_schema properly
2955 
2956     lv_retval := FND_INSTALLATION.GET_APP_INFO(
2957                    'FND', lv_dummy1,lv_dummy2, v_applsys_schema);
2958 
2959    EXCEPTION
2960    WHEN NO_DATA_FOUND THEN
2961      FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_INVALID_INSTANCE_ID');
2962      FND_MESSAGE.SET_TOKEN('INSTANCE_ID', v_pipe_wm);
2963      raise_application_error(-20101,FND_MESSAGE.GET);
2964    END INITIALIZE;
2965 
2966 
2967    PROCEDURE FINAL
2968    IS
2969      lv_param_rec_count           PLS_INTEGER; -- for L flow
2970    BEGIN
2971 
2972       UPDATE MSC_APPS_INSTANCES mai
2973          SET LAST_UPDATE_DATE = v_current_date,
2974              LAST_UPDATED_BY  = v_current_user,
2975              LRID= v_refresh_id,
2976              LRTYPE= 'L',
2977              SO_LRTYPE= 'I',
2978              CLEANSED_FLAG= SYS_NO,
2979              REQUEST_ID = FND_GLOBAL.CONC_REQUEST_ID
2980        WHERE mai.INSTANCE_ID = v_instance_id;
2981 
2982        -- Legacy Net Change Perf L flow changes
2983 
2984           SELECT count(*)
2985             INTO lv_param_rec_count
2986              FROM msc_coll_parameters
2987           WHERE instance_id = v_instance_id;
2988 
2989           IF lv_param_rec_count > 0 THEN
2990              DELETE FROM msc_coll_parameters
2991               WHERE instance_id = v_instance_id;
2992           END IF;
2993 
2994           INSERT INTO msc_coll_parameters
2995              (INSTANCE_ID, delete_ods_data, supplier_capacity, atp_rules,
2996               bom, bor, calendar_check, demand_class, forecast, item,
2997               kpi_targets_bis, mds, mps, oh, parameter, planners,
2998               item_substitutes, projects, po, reservations, nra, safety_stock,
2999               sales_order, sourcing_history, sourcing, sub_inventories,
3000               customer, supplier, unit_numbers, uom, user_supply_demand, wip, user_comp_association,
3001               INTERNAL_REPAIR,EXTERNAL_REPAIR,LAST_UPDATE_DATE, LAST_UPDATED_BY, CREATION_DATE, CREATED_BY)
3002            VALUES (v_instance_id, prec.purge_ods_flag, prec.app_supp_cap_flag,
3003               prec.atp_rules_flag, prec.bom_flag,
3004               prec.bor_flag, prec.calendar_flag,
3005               prec.demand_class_flag, prec.forecast_flag,
3006               prec.item_flag, prec.kpi_bis_flag,
3007               prec.mds_flag, prec.mps_flag,
3008               prec.oh_flag, prec.parameter_flag,
3009               prec.planner_flag,prec.item_subst_flag, prec.project_flag,
3010               prec.po_flag, prec.reserves_flag,
3011               prec.resource_nra_flag, prec.saf_stock_flag,
3012               prec.sales_order_flag, prec.source_hist_flag,
3013               prec.sourcing_rule_flag, prec.sub_inventory_flag,
3014               prec.tp_customer_flag, prec.tp_vendor_flag,
3015               prec.unit_number_flag, prec.uom_flag,
3016               prec.user_supply_demand_flag, prec.wip_flag, prec.user_company_flag,
3017               prec.internal_repair_flag,prec.external_repair_flag,sysdate, v_current_user, sysdate, v_current_user);
3018 
3019           -- End L Flow changes
3020 
3021    END FINAL;
3022 
3023    FUNCTION is_monitor_status_running RETURN NUMBER
3024    IS
3025       l_call_status      boolean;
3026       l_phase            varchar2(80);
3027       l_status           varchar2(80);
3028       l_dev_phase        varchar2(80);
3029       l_dev_status       varchar2(80);
3030       l_message          varchar2(2048);
3031 
3032    BEGIN
3033 
3034       IF v_cp_enabled= SYS_NO THEN
3035          RETURN SYS_YES;
3036       END IF;
3037 
3038        l_call_status:= FND_CONCURRENT.GET_REQUEST_STATUS
3039                                 ( v_monitor_request_id,
3040                                 NULL,
3041                                 NULL,
3042                                 l_phase,
3043                                 l_status,
3044                                 l_dev_phase,
3045                                 l_dev_status,
3046                                 l_message);
3047 
3048        IF l_call_status=FALSE THEN
3049           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, l_message);
3050           RETURN SYS_NO;
3051        END IF;
3052 
3053        IF l_dev_phase='RUNNING' THEN
3054           RETURN SYS_YES;
3055        ELSE
3056           RETURN SYS_NO;
3057        END IF;
3058 
3059    END is_monitor_status_running;
3060 
3061    FUNCTION is_request_status_running RETURN NUMBER
3062    IS
3063       l_call_status           boolean;
3064       l_phase                 varchar2(80);
3065       l_status                varchar2(80);
3066       l_dev_phase             varchar2(80);
3067       l_dev_status            varchar2(80);
3068       l_message               varchar2(2048);
3069 
3070       l_request_id            NUMBER;
3071 
3072    BEGIN
3073 
3074       IF v_cp_enabled= SYS_NO THEN
3075          RETURN SYS_YES;
3076       END IF;
3077 
3078       l_request_id := FND_GLOBAL.CONC_REQUEST_ID;
3079 
3080       l_call_status:= FND_CONCURRENT.GET_REQUEST_STATUS
3081                               ( l_request_id,
3082                                 NULL,
3083                                 NULL,
3084                                 l_phase,
3085                                 l_status,
3086                                 l_dev_phase,
3087                                 l_dev_status,
3088                                 l_message);
3089 
3090       IF l_call_status=FALSE THEN
3091          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, l_message);
3092          RETURN SYS_NO;
3093       END IF;
3094 
3095       IF l_dev_phase='RUNNING' THEN
3096          RETURN SYS_YES;
3097       ELSE
3098          RETURN SYS_NO;
3099       END IF;
3100 
3101    END is_request_status_running;
3102 
3103    FUNCTION is_worker_status_valid( ps_request_id      IN NumTblTyp)
3104      RETURN NUMBER
3105    IS
3106       l_call_status      boolean;
3107       l_phase            varchar2(80);
3108       l_status           varchar2(80);
3109       l_dev_phase        varchar2(80);
3110       l_dev_status       varchar2(80);
3111       l_message          varchar2(2048);
3112 
3113       l_request_id       NUMBER;
3114    BEGIN
3115 
3116       IF v_cp_enabled= SYS_NO THEN
3117          RETURN SYS_YES;
3118       END IF;
3119 
3120       FOR lc_i IN 1..(ps_request_id.COUNT-1) LOOP
3121 
3122           l_request_id := ps_request_id(lc_i);
3123 
3124           l_call_status:= FND_CONCURRENT.GET_REQUEST_STATUS
3125                               ( l_request_id,
3126                                 NULL,
3127                                 NULL,
3128                                 l_phase,
3129                                 l_status,
3130                                 l_dev_phase,
3131                                 l_dev_status,
3132                                 l_message);
3133 
3134            IF l_call_status=FALSE THEN
3135               MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, l_message);
3136               RETURN SYS_NO;
3137            END IF;
3138 
3139            IF l_dev_phase NOT IN ( 'PENDING','RUNNING') THEN
3140               RETURN SYS_NO;
3141            END IF;
3142 
3143        END LOOP;
3144 
3145        RETURN SYS_YES;
3146 
3147    END is_worker_status_valid;
3148 
3149 
3150    -- Fix for 2788719.
3151    -- Returns 1 if bis periods has to be updated else 2.
3152    FUNCTION is_bis_periods_load_reqd RETURN NUMBER
3153    IS
3154       lv_rec_count_cal   NUMBER:=0;
3155       lv_rec_count_mtp   NUMBER:=0;
3156   BEGIN
3157 
3158     BEGIN
3159       SELECT 	1
3160       INTO      lv_rec_count_cal
3161       FROM      dual
3162       WHERE     EXISTS(SELECT 1
3163                          FROM 	msc_st_calendars
3164                          WHERE  sr_instance_id = v_instance_id
3165                          AND    process_flag = G_VALID);
3166 
3167     EXCEPTION
3168       WHEN NO_DATA_FOUND THEN
3169         lv_rec_count_cal := 0;
3170     END;
3171 
3172     BEGIN
3173       SELECT 	1
3174       INTO      lv_rec_count_mtp
3175       FROM      dual
3176       WHERE     EXISTS(SELECT 1
3177                          FROM 	msc_st_trading_partners
3178                          WHERE  sr_instance_id = v_instance_id
3179                          AND    process_flag = G_VALID);
3180 
3181     EXCEPTION
3182       WHEN NO_DATA_FOUND THEN
3183         lv_rec_count_mtp := 0;
3184     END;
3185     IF (lv_rec_count_cal > 0 OR lv_rec_count_mtp > 0) THEN
3186         RETURN SYS_YES;
3187     ELSE
3188         RETURN SYS_NO;
3189 
3190     END IF;
3191 
3192    END is_bis_periods_load_reqd;
3193 
3194   ----- ============== BO VALIDATIONS START HERE ========
3195 
3196   --+===========================================================================+
3197   --| DESCRIPTION  : This Procedure derives the modeled partner id and          |
3198   --|                site id for those partners and partner sites which is      |
3199   --|                modeled as inventory organization.                         |
3200   --+===========================================================================+
3201 
3202   PROCEDURE MODEL_ORG_SUPPLIER_CUSTOMER IS
3203   lv_error_text         VARCHAR2(250);
3204   lv_return             NUMBER;
3205   lv_message_text       msc_errors.error_text%TYPE;
3206   ex_logging_err        EXCEPTION;
3207 
3208   BEGIN
3209 
3210     v_sql_stmt := 1;
3211     UPDATE msc_st_trading_partners
3212     SET    modeled_customer_id =
3213           (SELECT local_id
3214           FROM    msc_local_id_setup
3215           WHERE  char1                = sr_instance_code
3216           AND    NVL(char2,NULL_CHAR) = NVL(company_name,NULL_CHAR)
3217           AND    char3                = modeled_customer_name
3218           AND    number1              = G_CUSTOMER
3219           AND    entity_name          = 'SR_TP_ID' )
3220     WHERE  sr_instance_code = v_instance_code
3221     AND    modeled_customer_name IS NOT NULL
3222     AND    process_flag     = G_VALID
3223     AND    partner_type     = G_ORGANIZATION;
3224 
3225     v_sql_stmt := 2;
3226     UPDATE msc_st_trading_partners
3227     SET    modeled_supplier_id =
3228            (SELECT local_id
3229            FROM    msc_local_id_setup
3230            WHERE  char1                = sr_instance_code
3231            AND    NVL(char2,NULL_CHAR) = NVL(company_name,NULL_CHAR)
3232            AND    char3                = modeled_supplier_name
3233            AND    number1              = G_VENDOR
3234            AND    entity_name          = 'SR_TP_ID' )
3235     WHERE  sr_instance_code = v_instance_code
3236     AND    modeled_supplier_name IS NOT NULL
3237     AND    process_flag     = G_VALID
3238     AND    partner_type     = G_ORGANIZATION;
3239 
3240     v_sql_stmt := 3;
3241     UPDATE msc_st_trading_partners
3242     SET    modeled_customer_site_id =
3243            (SELECT local_id
3244            FROM    msc_local_id_setup
3245            WHERE   char1                = sr_instance_code
3246            AND     NVL(char2,NULL_CHAR) = NVL(company_name,NULL_CHAR)
3247            AND     char3                = modeled_customer_name
3248            AND     char4                = modeled_customer_site_code
3249            AND     number1              = G_CUSTOMER
3250            AND     entity_name          = 'SR_TP_SITE_ID')
3251     WHERE  sr_instance_code = v_instance_code
3252     AND    modeled_customer_site_code IS NOT NULL
3253     AND    process_flag     = G_VALID
3254     AND    partner_type     = G_ORGANIZATION;
3255 
3256     v_sql_stmt := 4;
3257     UPDATE msc_st_trading_partners
3258     SET    modeled_supplier_site_id =
3259            (SELECT local_id
3260            FROM    msc_local_id_setup
3261            WHERE   char1                = sr_instance_code
3262            AND     NVL(char2,NULL_CHAR) = NVL(company_name,NULL_CHAR)
3263            AND     char3                = modeled_supplier_name
3264            AND     char4                = modeled_supplier_site_code
3265            AND     number1              = G_VENDOR
3266            AND     entity_name          = 'SR_TP_SITE_ID')
3267     WHERE  sr_instance_code = v_instance_code
3268     AND    modeled_supplier_site_code  IS NOT NULL
3269     AND    process_flag     = G_VALID
3270     AND    partner_type     = G_ORGANIZATION;
3271 
3272     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
3273                      (p_app_short_name    => 'MSC',
3274                       p_error_code        => 'MSC_PP_INVALID_VALUE',
3275                       p_message_text      => lv_message_text,
3276                       p_error_text        => lv_error_text,
3277                       p_token1            => 'COLUMN_NAME',
3278                       p_token_value1      => 'MODELED_CUSTOMER_NAME OR MODELED_SUPPLIER_NAME OR '
3279                                              ||'MODELED_CUSTOMER_SITE_CODE OR MODELED_SUPPLIER_SITE_CODE');
3280 
3281     IF lv_return <> 0 THEN
3282       RAISE ex_logging_err;
3283     END IF;
3284 
3285     v_sql_stmt := 5;
3286     UPDATE msc_st_trading_partners
3287     SET    error_text       = lv_message_text
3288     WHERE  sr_instance_code = v_instance_code
3289     AND    partner_type     = G_ORGANIZATION
3290     AND    process_flag     = G_VALID
3291     AND    ((modeled_customer_id IS NULL
3292            AND    modeled_customer_name IS NOT NULL)
3293     OR     (modeled_supplier_id  IS NULL
3294            AND    modeled_supplier_name IS NOT NULL)
3295     OR     (modeled_customer_site_id IS NULL
3296            AND    modeled_customer_site_code IS NOT NULL)
3297     OR     (modeled_supplier_site_id IS NULL
3298            AND    modeled_supplier_site_code IS NOT NULL));
3299 
3300    v_sql_stmt := 6;
3301   -- bug 2774016 (validation for master organization code)
3302 
3303    UPDATE msc_st_trading_partners
3304     SET   master_organization =
3305           (SELECT local_id
3306           FROM   msc_local_id_setup
3307           WHERE  char1                = sr_instance_code
3308           AND    NVL(char2,NULL_CHAR) = NVL(company_name,NULL_CHAR)
3309           AND    char3                = master_organization_code
3310           AND    number1              = G_ORGANIZATION
3311           AND    entity_name          = 'SR_TP_ID' )
3312     WHERE  sr_instance_code = v_instance_code
3313     AND    master_organization_code IS NOT NULL
3314     AND    process_flag     = G_VALID
3315     AND    partner_type     = G_ORGANIZATION;
3316 
3317    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
3318                      (p_app_short_name    => 'MSC',
3319                       p_error_code        => 'MSC_PP_INVALID_VALUE',
3320                       p_message_text      => lv_message_text,
3321                       p_error_text        => lv_error_text,
3322                       p_token1            => 'COLUMN_NAME',
3323                       p_token_value1      => 'MASTER_ORGANIZATION_CODE');
3324 
3325       IF lv_return <> 0 THEN
3326         RAISE ex_logging_err;
3327       END IF;
3328 
3329 
3330   INSERT INTO msc_errors
3331     ( error_id,
3332      transaction_id,
3333      message_id,
3334      instance_code,
3335      table_name,
3336      propagated,
3337      source,
3338      rrow,
3339      severity,
3340      message_sent,
3341      last_update_date,
3342      last_updated_by,
3343      creation_date,
3344      created_by,
3345      last_update_login,
3346      request_id,
3347      program_application_id,
3348      program_id,
3349      program_update_date,
3350       error_text)
3351      SELECT
3352      msc_errors_s.NEXTVAL,
3353      st_transaction_id,
3354      message_id,
3355      sr_instance_code,
3356     'MSC_ST_TRADING_PARTNERS',
3357     'N',
3358     data_source_type,
3359     'MASTER_ORGANIZATION_CODE',
3360     G_SEV_WARNING,
3361     SYS_NO,
3362     last_update_date,
3363     last_updated_by,
3364     creation_date,
3365     created_by,
3366     last_update_login,
3367     request_id,
3368     program_application_id,
3369     program_id,
3370     program_update_date,
3371     lv_message_text
3372     FROM
3373     MSC_ST_TRADING_PARTNERS
3374     WHERE  sr_instance_code = v_instance_code
3375     AND    partner_type     = G_ORGANIZATION
3376     AND    process_flag     = G_VALID
3377     AND    (master_organization IS NULL
3378            AND  master_organization_code IS NOT NULL);
3379 
3380 
3381     commit;
3382 
3383   EXCEPTION
3384     WHEN OTHERS THEN
3385       lv_error_text    := substr('MSC_CL_PRE_PROCESS.MODEL_ORG_SUPPLIER_CUSTOMER'||'('
3386                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
3387      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
3388       ROLLBACK;
3389 
3390   END MODEL_ORG_SUPPLIER_CUSTOMER;
3391 
3392 
3393 /*==========================================================================+
3394 | DESCRIPTION  : This function errors out the record for which the          |
3395 |                validation failed and inserts a record into the error      |
3396 |                table. And then this error is propagated to the remaining  |
3397 |                calendar summary tables(with the process_flag = 4)         |
3398 +==========================================================================*/
3399   FUNCTION PROPAGATE_CAL_ERROR
3400            (p_table_name           VARCHAR2,
3401             p_transaction_id       NUMBER,
3402             p_calendar_code        VARCHAR2,
3403             p_message_text         VARCHAR2,
3404             p_column_names         VARCHAR2,
3405             p_error_text     OUT NOCOPY  VARCHAR2)
3406   RETURN NUMBER IS
3407   lv_sql_stmt           VARCHAR2(5000);
3408   lv_status             NUMBER := 0;
3409   lv_column_names       VARCHAR2(5000);       --stores concatenated column names
3410   lv_error_type         NUMBER;
3411 
3412   BEGIN
3413 
3414     IF    p_table_name = 'MSC_ST_CALENDARS' THEN
3415       v_sql_stmt  := 1;
3416       lv_sql_stmt :=
3417       'UPDATE   msc_st_calendars'
3418       ||' set   process_flag      = '||G_ERROR_FLG||','
3419       ||'       error_text        = :p_message_text'
3420       ||' WHERE st_transaction_id = :p_transaction_id'
3421       ||' AND   sr_instance_code  = :instance_code'
3422       ||' AND   process_flag      = '||G_IN_PROCESS
3423       ||' AND   calendar_code     = :calendar_code';
3424 
3425         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3426 
3427       EXECUTE IMMEDIATE lv_sql_stmt
3428             USING       p_message_text,
3429                         p_transaction_id,
3430                         v_instance_code,
3431                         p_calendar_code;
3432 
3433     ELSIF p_table_name = 'MSC_ST_WORKDAY_PATTERNS' THEN
3434 
3435       v_sql_stmt  := 2;
3436       lv_sql_stmt :=
3437       'UPDATE   msc_st_workday_patterns'
3438       ||' set   process_flag      = '||G_ERROR_FLG||','
3439       ||'       error_text        = :p_message_text'
3440       ||' WHERE st_transaction_id = :p_transaction_id'
3441       ||' AND   sr_instance_code  = :instance_code'
3442       ||' AND   process_flag      = '||G_IN_PROCESS
3443       ||' AND   calendar_code     = :calendar_code';
3444 
3445         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3446 
3447       EXECUTE IMMEDIATE lv_sql_stmt
3448               USING     p_message_text,
3449                         p_transaction_id,
3450                         v_instance_code,
3451                         p_calendar_code;
3452 
3453     ELSIF p_table_name = 'MSC_ST_SHIFT_TIMES' THEN
3454 
3455       v_sql_stmt  := 3;
3456       lv_sql_stmt :=
3457       'UPDATE   msc_st_shift_times'
3458       ||' set   process_flag      = '||G_ERROR_FLG||','
3459       ||'       error_text        = :p_message_text'
3460       ||' WHERE st_transaction_id = :p_transaction_id'
3461       ||' AND   sr_instance_code  = :instance_code'
3462       ||' AND   process_flag      = '||G_IN_PROCESS
3463       ||' AND   calendar_code     = :calendar_code';
3464 
3465         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3466 
3467       EXECUTE IMMEDIATE lv_sql_stmt
3468             USING       p_message_text,
3469                         p_transaction_id,
3470                         v_instance_code,
3471                         p_calendar_code;
3472 
3473 
3474     ELSIF p_table_name = 'MSC_ST_CALENDAR_EXCEPTIONS' THEN
3475 
3476       v_sql_stmt  := 4;
3477       lv_sql_stmt :=
3478       'UPDATE   msc_st_calendar_exceptions'
3479       ||' set   process_flag      = '||G_ERROR_FLG||','
3480       ||'       error_text        = :p_message_text'
3481       ||' WHERE st_transaction_id = :p_transaction_id'
3482       ||' AND   sr_instance_code  = :instance_code'
3483       ||' AND   process_flag      = '||G_IN_PROCESS
3484       ||' AND   calendar_code     = :calendar_code';
3485 
3486         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3487 
3488       EXECUTE IMMEDIATE lv_sql_stmt
3489             USING       p_message_text,
3490                         p_transaction_id,
3491                         v_instance_code,
3492                         p_calendar_code;
3493 
3494     ELSIF p_table_name = 'MSC_ST_SHIFT_EXCEPTIONS' THEN
3495 
3496       v_sql_stmt  := 5;
3497       lv_sql_stmt :=
3498       'UPDATE   msc_st_shift_exceptions'
3499       ||' set   process_flag      = '||G_ERROR_FLG||','
3500       ||'       error_text        = :p_message_text'
3501       ||' WHERE st_transaction_id = :p_transaction_id'
3502       ||' AND   sr_instance_code  = :instance_code'
3503       ||' AND   process_flag      = '||G_IN_PROCESS
3504       ||' AND   calendar_code     = :calendar_code';
3505 
3506         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3507 
3508       EXECUTE IMMEDIATE   lv_sql_stmt
3509               USING       p_message_text,
3510                           p_transaction_id,
3511                           v_instance_code,
3512                           p_calendar_code;
3513 
3514     END IF;
3515 
3516     IF    p_table_name <> 'MSC_ST_CALENDARS' THEN
3517       v_sql_stmt  := 6;
3518       lv_sql_stmt :=
3519       'UPDATE   msc_st_calendars'
3520       ||' set   process_flag = '||G_PROPAGATION
3521       ||' WHERE sr_instance_code = :instance_code'
3522       ||' AND   process_flag     = '||G_IN_PROCESS
3523       ||' AND   calendar_code    = :calendar_code';
3524 
3525         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3526 
3527       EXECUTE IMMEDIATE lv_sql_stmt
3528               USING     v_instance_code,
3529                         p_calendar_code;
3530 
3531     END IF;
3532 
3533     v_sql_stmt  := 7;
3534     lv_sql_stmt :=
3535     'UPDATE   msc_st_workday_patterns'
3536     ||' set   process_flag = '||G_PROPAGATION
3537     ||' WHERE sr_instance_code = :instance_code'
3538     ||' AND   process_flag     = '||G_IN_PROCESS
3539     ||' AND   calendar_code    = :calendar_code';
3540 
3541       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3542 
3543     EXECUTE IMMEDIATE lv_sql_stmt
3544             USING     v_instance_code,
3545                       p_calendar_code;
3546 
3547     v_sql_stmt  := 8;
3548     lv_sql_stmt :=
3549     'UPDATE   msc_st_calendar_shifts'
3550     ||' set   process_flag = '||G_PROPAGATION
3551     ||' WHERE sr_instance_code = :instance_code'
3552     ||' AND   process_flag     = '||G_IN_PROCESS
3553     ||' AND   calendar_code    = :calendar_code';
3554 
3555       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3556 
3557     EXECUTE IMMEDIATE lv_sql_stmt
3558             USING     v_instance_code,
3559                       p_calendar_code;
3560 
3561     v_sql_stmt  := 9;
3562     lv_sql_stmt :=
3563     'UPDATE   msc_st_shift_times'
3564     ||' set   process_flag = '||G_PROPAGATION
3565     ||' WHERE sr_instance_code = :instance_code'
3566     ||' AND   process_flag     = '||G_IN_PROCESS
3567     ||' AND   calendar_code    = :calendar_code';
3568 
3569       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3570 
3571     EXECUTE IMMEDIATE lv_sql_stmt
3572             USING     v_instance_code,
3573                       p_calendar_code;
3574 
3575 
3576     v_sql_stmt  := 10;
3577     lv_sql_stmt :=
3578     'UPDATE   msc_st_calendar_exceptions'
3579     ||' set   process_flag = '||G_PROPAGATION
3580     ||' WHERE sr_instance_code = :instance_code'
3581     ||' AND   process_flag     = '||G_IN_PROCESS
3582     ||' AND   calendar_code    = :calendar_code';
3583 
3584       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3585 
3586     EXECUTE IMMEDIATE lv_sql_stmt
3587             USING     v_instance_code,
3588                       p_calendar_code;
3589 
3590     v_sql_stmt  := 11;
3591     lv_sql_stmt :=
3592     'UPDATE   msc_st_shift_exceptions'
3593     ||' set   process_flag = '||G_PROPAGATION
3594     ||' WHERE sr_instance_code = :instance_code'
3595     ||' AND   process_flag     = '||G_IN_PROCESS
3596     ||' AND   calendar_code    = :calendar_code';
3597 
3598       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3599 
3600     EXECUTE IMMEDIATE lv_sql_stmt
3601             USING     v_instance_code,
3602                       p_calendar_code;
3603 
3604 
3605     lv_status := MSC_ST_UTIL.LOG_ERROR
3606                  (p_table_name        => p_table_name,
3607                   p_instance_code     => v_instance_code,
3608                   p_row               => p_column_names,
3609                   p_severity          => G_SEV_ERROR,
3610                   p_message_text      => NULL,
3611                   p_error_text        => p_error_text,
3612                   p_debug             => v_debug,
3613                   p_propagated        => 'Y');
3614 
3615     RETURN(lv_status);
3616   EXCEPTION
3617     WHEN OTHERS THEN
3618       p_error_text := substr('MSC_CL_PRE_PROCESS.PROPAGATE_CAL_ERROR'
3619                             ||'('||v_sql_stmt||')'|| SQLERRM, 1, 240);
3620       return(SQLCODE);
3621 
3622   END PROPAGATE_CAL_ERROR;
3623 
3624 /*==========================================================================+
3625 | DESCRIPTION  : This function inserts a record into the LID table for the  |
3626 |                new shifts, derives the shift information from the         |
3627 |                workday pattern table and updates the shift_num in the     |
3628 |                tables msc_st_shift_exceptions and msc_st_shift_times      |
3629 +==========================================================================*/
3630   FUNCTION derive_shift_details
3631            (p_transaction_id     NUMBER,
3632             p_company_name       VARCHAR2,
3633             p_calendar_code      VARCHAR2,
3634             p_shift_name         VARCHAR2,
3635             p_error_text     OUT NOCOPY VARCHAR2,
3636             P_shift_num          NUMBER DEFAULT NULL_VALUE,
3637             p_shift_exist        NUMBER DEFAULT NULL_VALUE)
3638   RETURN NUMBER IS
3639   lv_sql_stmt             VARCHAR2(5000);
3640   lv_shift_num msc_st_calendar_shifts.shift_num%TYPE;
3641 
3642   BEGIN
3643     lv_shift_num := p_shift_num;
3644 
3645     IF p_shift_exist = SYS_NO THEN
3646       v_sql_stmt  := 2;
3647       lv_sql_stmt :=
3648       'INSERT INTO msc_local_id_setup'
3649       ||'  (local_id, '
3650       ||'   st_transaction_id,'
3651       ||'   instance_id,'
3652       ||'   entity_name,'
3653       ||'   char1,'
3654       ||'   char2,'
3655       ||'   char3,'
3656       ||'   char4,'
3657       ||'   last_update_date,'
3658       ||'   last_updated_by,'
3659       ||'   creation_date,'
3660       ||'   created_by)'
3661       ||' VALUES'
3662       ||'   (:lv_shift_num,'
3663       ||'   :p_transaction_id,'
3664       ||'   :v_instance_id,'
3665       ||'   ''SHIFT_NUM'','
3666       ||'   :v_instance_code,'
3667       ||'   :company_name,'
3668       ||'   :p_calendar_code,'
3669       ||'   :p_shift_name,'
3670       ||'   :v_current_date,'
3671       ||'   :v_current_user,'
3672       ||'   :v_current_date,'
3673       ||'   :v_current_user)';
3674 
3675         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3676       EXECUTE IMMEDIATE lv_sql_stmt
3677               USING     lv_shift_num,
3678                         p_transaction_id,
3679                         v_instance_id,
3680                         v_instance_code,
3681                         p_company_name,
3682                         p_calendar_code,
3683                         p_shift_name,
3684                         v_current_date,
3685                         v_current_user,
3686                         v_current_date,
3687                         v_current_user;
3688     END IF;
3689 
3690     v_sql_stmt  := 3;
3691     lv_sql_stmt :=
3692     'UPDATE msc_st_workday_patterns'
3693     ||' SET    shift_num         = :lv_shift_num'
3694     ||' WHERE  st_transaction_id = :p_transaction_id'
3695     ||' AND    process_flag      = '||G_IN_PROCESS
3696     ||' AND    sr_instance_code  = :v_instance_code';
3697 
3698       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3699     EXECUTE IMMEDIATE lv_sql_stmt
3700             USING     lv_shift_num,
3701                       p_transaction_id,
3702                       v_instance_code;
3703 
3704     v_sql_stmt  := 4;
3705     lv_sql_stmt :=
3706     'UPDATE msc_st_shift_times '
3707     ||' SET    shift_num        = :lv_shift_num'
3708     ||' WHERE  calendar_code    = :p_calendar_code'
3709     ||' AND    shift_name       = :p_shift_name'
3710     ||' AND    process_flag     = '||G_IN_PROCESS
3711     ||' AND    sr_instance_code = :v_instance_code';
3712 
3713       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3714     EXECUTE IMMEDIATE lv_sql_stmt
3715             USING     lv_shift_num,
3716                       p_calendar_code,
3717                       p_shift_name,
3718                       v_instance_code;
3719 
3720     v_sql_stmt  := 5;
3721     lv_sql_stmt :=
3722     'UPDATE msc_st_shift_exceptions '
3723     ||' SET    shift_num        = :lv_shift_num'
3724     ||' WHERE  calendar_code    = :p_calendar_code'
3725     ||' AND    shift_name       = :p_shift_name'
3726     ||' AND    process_flag     = '||G_IN_PROCESS
3727     ||' AND    sr_instance_code = :v_instance_code';
3728 
3729       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3730     EXECUTE IMMEDIATE lv_sql_stmt
3731             USING     lv_shift_num,
3732                       p_calendar_code,
3733                       p_shift_name,
3734                       v_instance_code;
3735 
3736     v_sql_stmt  := 6;
3737     lv_sql_stmt :=
3738     'INSERT INTO msc_st_calendar_shifts '
3739     ||'   (calendar_code,'
3740     ||'   shift_name,'
3741     ||'   shift_num,'
3742     ||'   sr_instance_code,'
3743     ||'   company_name,'
3744     ||'   sr_instance_id,'
3745     ||'   last_update_date,'
3746     ||'   last_updated_by,'
3747     ||'   creation_date,'
3748     ||'   created_by,'
3749     ||'   message_id,'
3750     ||'   process_flag,'
3751     ||'   data_source_type)'
3752     ||' SELECT'
3753     ||'   calendar_code,'
3754     ||'   shift_name,'
3755     ||'   shift_num,'
3756     ||'   :v_instance_code,'
3757     ||'   company_name,'
3758     ||    0||','
3759     ||'   last_update_date,'
3760     ||'   last_updated_by,'
3761     ||'   creation_date,'
3762     ||'   created_by,'
3763     ||'   message_id,'
3764     ||'   process_flag,'
3765     ||'   data_source_type'
3766     ||' FROM   msc_st_workday_patterns mwp'
3767     ||' WHERE  st_transaction_id = :p_transaction_id'
3768     ||' AND    process_flag      = '||G_IN_PROCESS
3769     ||' AND    sr_instance_code  = :instance_code'
3770     ||' AND    NOT EXISTS( SELECT 1 '
3771     ||'                    FROM   MSC_ST_CALENDAR_SHIFTS mcs'
3772     ||'                    where  mcs.calendar_code  =  mwp.calendar_code'
3773     ||'                    AND    mcs.shift_name     =  mwp.shift_name'
3774     ||'                    AND    mcs.shift_num      =  mwp.shift_num'
3775     ||'                    AND    mcs.sr_instance_code = :v_instance_code'
3776     ||'        AND   NVL(mwp.company_name,  '||''''||NULL_CHAR||''''||') = '
3777     ||'              NVL(mcs.company_name,  '||''''||NULL_CHAR||''''||'))';
3778 
3779 
3780 
3781       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3782     EXECUTE IMMEDIATE lv_sql_stmt
3783             USING     v_instance_code,
3784                       p_transaction_id,
3785                       v_instance_code,
3786                       v_instance_code;
3787     RETURN(0);
3788   EXCEPTION
3789     WHEN OTHERS THEN
3790 	p_error_text := substr('MSC_CL_PRE_PROCESS.DERIVE_SHIFT_DETAILS'
3791                             ||'('||v_sql_stmt||')'|| SQLERRM, 1, 240);
3792 	return(SQLCODE);
3793 
3794   END derive_shift_details;
3795 
3796 /*==========================================================================+
3797 | DESCRIPTION  : This procedure validates the summary information from the  |
3798 |                legacy systems, inserts/updates the validated information  |
3799 |                into the ODS tables and spawns the calendar build program  |
3800 |                which populates the calendar details into the ODS tables.  |
3801 +==========================================================================*/
3802   PROCEDURE LOAD_CALENDAR IS
3803   lv_rec_no               NUMBER;
3804   lv_return               NUMBER;
3805   lv_request_id           Number;
3806   lv_shift_num_gen        NUMBER;
3807   lv_shift_dup            NUMBER;
3808   lv_error_text           VARCHAR2(250);
3809   lv_sql_stmt             VARCHAR2(5000);
3810   lv_where_str            VARCHAR2(5000);
3811   lv_table_name           VARCHAR2(32);
3812   lv_column_names         VARCHAR2(5000);       --stores concatenated column names
3813 
3814   lv_calendar_code        msc_st_calendars.calendar_code%TYPE;
3815   lv_transaction_id       msc_st_calendars.st_transaction_id%TYPE;
3816   lv_calendar_start_date  msc_st_calendars.calendar_start_date%TYPE;
3817   lv_calendar_end_date    msc_st_calendars.calendar_end_date%TYPE;
3818   lv_seq_num              msc_st_workday_patterns.seq_num%TYPE := 0;
3819   lv_message_text         msc_errors.error_text%TYPE;
3820   lv_shift_num            msc_st_calendar_shifts.shift_num%TYPE;
3821 
3822   ex_logging_err          EXCEPTION;
3823   ex_critical_err         EXCEPTION;
3824 
3825   CURSOR   c1 IS
3826     SELECT rowid,
3827            calendar_code,
3828            calendar_start_date,
3829            calendar_end_date,
3830            quarterly_calendar_type,
3831            week_start_day,
3832            overwrite_flag,
3833            deleted_flag
3834     FROM   msc_st_calendars
3835     WHERE  sr_instance_code = v_instance_code
3836     AND    process_flag     = G_IN_PROCESS;
3837 
3838   CURSOR c2(p_calendar_code VARCHAR2) IS
3839     SELECT rowid,
3840            company_name,
3841            shift_name,
3842            seq_num,
3843            days_on,
3844            days_off,
3845            deleted_flag
3846     FROM   msc_st_workday_patterns
3847     WHERE  sr_instance_code = v_instance_code
3848     AND    calendar_code    = p_calendar_code
3849     AND    process_flag     = G_IN_PROCESS;
3850 
3851   CURSOR c3(p_calendar_code VARCHAR2) IS
3852     SELECT rowid,
3853            shift_name,
3854            shift_num,
3855            from_time,
3856            to_time,
3857            deleted_flag
3858     FROM   msc_st_shift_times
3859     WHERE  sr_instance_code = v_instance_code
3860     AND    calendar_code    = p_calendar_code
3861     AND    process_flag     = G_IN_PROCESS;
3862 
3863   CURSOR c4(p_calendar_code VARCHAR2) IS
3864     SELECT rowid,
3865            exception_date,
3866            exception_type,
3867            deleted_flag
3868     FROM   msc_st_calendar_exceptions
3869     WHERE  sr_instance_code = v_instance_code
3870     AND    calendar_code    = p_calendar_code
3871     AND    process_flag     = G_IN_PROCESS;
3872 
3873   CURSOR c5(p_calendar_code VARCHAR2) IS
3874     SELECT rowid,
3875            shift_name,
3876            exception_date,
3877            exception_type,
3878            deleted_flag
3879     FROM   msc_st_shift_exceptions
3880     WHERE  sr_instance_code = v_instance_code
3881     AND    calendar_code    = p_calendar_code
3882     AND    process_flag     = G_IN_PROCESS;
3883 
3884   BEGIN
3885     --Calendar summary information is processed calendar by calendar.
3886     v_cal_build := False;
3887 
3888     --Added to fix the bug#2748859
3889     --Getting the error message text
3890     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
3891                  (p_app_short_name    => 'MSC',
3892                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
3893                   p_message_text      => lv_message_text,
3894                   p_error_text        => lv_error_text);
3895 
3896     IF lv_return <> 0 THEN
3897       RAISE ex_logging_err;
3898     END IF;
3899 
3900     --Duplicate records check for the records whose source is XML
3901 
3902     UPDATE  msc_st_calendars mc1
3903     SET     process_flag = G_ERROR_FLG,
3904             error_text   = lv_message_text
3905     WHERE   message_id <  (SELECT MAX(message_id)
3906                            FROM   msc_st_calendars mc2
3907                            WHERE  mc2.sr_instance_code  = mc1.sr_instance_code
3908                            AND    mc2.calendar_code     = mc1.calendar_code
3909                            AND    mc2.process_flag      = G_IN_PROCESS
3910                            AND    NVL(mc2.message_id,NULL_VALUE) <>NULL_VALUE)
3911     AND     mc1.process_flag             = G_IN_PROCESS
3912     AND     mc1.sr_instance_code         = v_instance_code
3913     AND     NVL(mc1.message_id,NULL_VALUE) <> NULL_VALUE;
3914 
3915 
3916     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
3917                  (p_app_short_name    => 'MSC',
3918                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
3919                   p_message_text      => lv_message_text,
3920                   p_error_text        => lv_error_text);
3921 
3922     IF lv_return <> 0 THEN
3923       RAISE ex_logging_err;
3924     END IF;
3925 
3926     --Duplicate records check for the records whose source is other than XML
3927     --Different SQL is used because in XML we can identify the latest records
3928     --whereas in batch load we cannot.
3929 
3930     UPDATE  msc_st_calendars mc1
3931     SET     process_flag = G_ERROR_FLG,
3932             error_text   = lv_message_text
3933     WHERE   EXISTS( SELECT 1
3934                     FROM   msc_st_calendars mc2
3935                     WHERE  mc2.sr_instance_code  = mc1.sr_instance_code
3936                     AND    mc2.calendar_code     = mc1.calendar_code
3937                     AND    mc2.process_flag      = G_IN_PROCESS
3938                     AND    NVL(mc2.message_id,NULL_VALUE) = NULL_VALUE
3939                     GROUP BY sr_instance_code,company_name,calendar_code
3940                     HAVING COUNT(*) > 1)
3941     AND     mc1.process_flag             = G_IN_PROCESS
3942     AND     mc1.sr_instance_code         = v_instance_code
3943     AND     NVL(mc1.message_id,NULL_VALUE) = NULL_VALUE;
3944     --Added to fix the bug#2748859
3945 
3946     FOR rec1 IN c1
3947     LOOP
3948      BEGIN
3949 
3950       lv_calendar_code := rec1.calendar_code;--used for error propagation
3951       lv_table_name    := 'MSC_ST_CALENDARS';--used for error propagation
3952 
3953       lv_column_names :=
3954       'CALENDAR_CODE            ||''~''||'
3955       ||'CALENDAR_START_DATE    ||''~''||'
3956       ||'CALENDAR_END_DATE      ||''~''||'
3957       ||'QUARTERLY_CALENDAR_TYPE||''~''||'
3958       ||'SR_INSTANCE_CODE       ||''~''||'
3959       ||'DESCRIPTION            ||''~''||'
3960       ||'WEEK_START_DAY         ||''~''||'
3961       ||'OVERWRITE_FLAG         ||''~''||'
3962       ||'COMPANY_NAME           ||''~''||'
3963       ||'DAYS_ON                ||''~''||'
3964       ||'DAYS_OFF               ||''~''||'
3965       ||'DELETED_FLAG';
3966 
3967       v_sql_stmt  := 1;
3968       SELECT msc_st_calendars_s.NEXTVAL
3969       INTO   lv_transaction_id
3970       FROM   dual;
3971 
3972         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,'Refresh ID: '||v_refresh_id);
3973 
3974       --Update the transaction_id and the who columns
3975       v_sql_stmt  := 2;
3976       lv_sql_stmt :=
3977       'UPDATE   msc_st_calendars '
3978       ||' SET   st_transaction_id = :lv_transaction_id,'
3979       ||'       refresh_id        = '||v_refresh_id ||','
3980       ||'       last_update_date  = :v_current_date,'
3981       ||'       last_updated_by   = :v_current_user,'
3982       ||'       creation_date     = :v_current_date,'
3983       ||'       created_by        = :v_current_user'
3984       ||' WHERE nvl(calendar_code, '||''''||NULL_CHAR||''''||') ='
3985       ||'       nvl(:calendar_code,'||''''||NULL_CHAR||''''||') '
3986       ||' AND   sr_instance_code  = :v_instance_code';
3987 
3988         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
3989 
3990       EXECUTE IMMEDIATE lv_sql_stmt
3991               USING     lv_transaction_id,
3992                         v_current_date,
3993                         v_current_user,
3994                         v_current_date,
3995                         v_current_user,
3996                         rec1.calendar_code,
3997                         v_instance_code;
3998 
3999       IF (rec1.calendar_code       IS NULL OR
4000           rec1.calendar_start_date IS NULL OR
4001           rec1.calendar_end_date   IS NULL) THEN
4002 
4003         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4004                        (p_app_short_name    => 'MSC',
4005                         p_error_code        => 'MSC_PP_COL_VAL_NULL',
4006                         p_message_text      => lv_message_text,
4007                         p_error_text        => lv_error_text,
4008                         p_token1            => 'COLUMN_NAME',
4009                         p_token_value1      => 'CALENDAR_CODE OR '
4010                                                ||'CALENDAR_START_DATE OR '
4011                                                ||'CALENDAR_END_DATE ');
4012 
4013         IF lv_return <> 0 THEN
4014           RAISE ex_logging_err;
4015         END IF;
4016 
4017         RAISE ex_critical_err;
4018 
4019       ELSIF rec1.deleted_flag = SYS_YES THEN
4020 
4021         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4022                        (p_app_short_name    => 'MSC',
4023                         p_error_code        => 'MSC_PP_NO_DELETION',
4024                         p_message_text      => lv_message_text,
4025                         p_error_text        => lv_error_text,
4026                         p_token1            => 'TABLE_NAME',
4027                         p_token_value1      => 'MSC_ST_CALENDARS');
4028 
4029         IF lv_return <> 0 THEN
4030           RAISE ex_logging_err;
4031         END IF;
4032         RAISE ex_critical_err;
4033 
4034       ELSIF (rec1.week_start_day           NOT IN(1,2)      OR
4035              rec1.quarterly_calendar_type  NOT IN(1,2,3,4)  OR
4036              NVL(rec1.overwrite_flag,'N')  NOT IN('Y','N')) THEN
4037 
4038         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4039                        (p_app_short_name    => 'MSC',
4040                         p_error_code        => 'MSC_PP_INVALID_VALUE',
4041                         p_message_text      => lv_message_text,
4042                         p_error_text        => lv_error_text,
4043                         p_token1            => 'COLUMN_NAME',
4044                         p_token_value1      => 'WEEK_START_DAY OR'
4045                                                ||' QUARTERLY_CALENDAR_TYPE OR'
4046                                                ||' OVERWRITE_FLAG');
4047 
4048         IF lv_return <> 0 THEN
4049           RAISE ex_logging_err;
4050         END IF;
4051         RAISE ex_critical_err;
4052 
4053       ELSIF rec1.calendar_end_date <= rec1.calendar_start_date THEN
4054 
4055         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4056                        (p_app_short_name    => 'MSC',
4057                         p_error_code        => 'MSC_PP_END_GT_ST_DATE',
4058                         p_message_text      => lv_message_text,
4059                         p_error_text        => lv_error_text);
4060 
4061         IF lv_return <> 0 THEN
4062           RAISE ex_logging_err;
4063         END IF;
4064         RAISE ex_critical_err;
4065 
4066       END IF;
4067 
4068       --Calendar is a global entity. Hence checking for the similar calendar
4069       --existence in other instances. Check is done in msc_calendar_dates
4070       --because 'ERP collections' does'nt collect the calendar header informations
4071       --like msc_Calendars, msc_workday_patterns etc.,
4072       v_sql_stmt  := 2;  /*Bug 2424200*/
4073       lv_sql_stmt :=
4074       'SELECT   count(*)'
4075       ||' FROM  msc_calendar_dates'
4076       ||' WHERE calendar_code   =  :calendar_code'
4077       ||' AND   sr_instance_id  <> :instance_id';
4078 
4079 
4080         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
4081       EXECUTE IMMEDIATE lv_sql_stmt
4082               INTO      lv_rec_no
4083               USING     rec1.calendar_code,
4084                         v_instance_id;
4085 
4086       IF lv_rec_no > 0 THEN
4087 
4088         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4089                      (p_app_short_name    => 'MSC',
4090                       p_error_code        => 'MSC_PP_CAL_ALREADY_EXISTS',
4091                       p_message_text      => lv_message_text,
4092                       p_error_text        => lv_error_text);
4093 
4094         IF lv_return <> 0 THEN
4095           RAISE ex_logging_err;
4096         END IF;
4097         RAISE ex_critical_err;
4098       END IF;
4099 
4100       --If the overwrite_flag is 'N' and the calendar is already existing
4101       --in the ODS table then the calendar will be errored out and also if
4102       --if the overwrite_flag is 'Y' and if the calendar  does'nt exist
4103       --in the ODS table then the calendar will be errored out. And also, if
4104       --all the existing shifts(in ODS) is not provided, then the calendar
4105       --will be errored out.
4106       BEGIN
4107         v_sql_stmt  := 3;  /*Bug 2172537*/
4108         lv_sql_stmt :=
4109         'SELECT   calendar_start_date,'
4110         ||'       calendar_end_date'
4111         ||' FROM  msc_calendars'
4112         ||' WHERE calendar_code   =  :calendar_code'
4113         ||' AND   sr_instance_id   = :instance_id'
4114         ||' FOR   UPDATE NOWAIT';
4115 
4116           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
4117         EXECUTE IMMEDIATE lv_sql_stmt
4118                 INTO      lv_calendar_start_date,
4119                           lv_calendar_end_date
4120                 USING     rec1.calendar_code,
4121                           v_instance_id;
4122 
4123         IF NVL(rec1.overwrite_flag,'N')     = 'N' THEN
4124 
4125           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4126                        (p_app_short_name    => 'MSC',
4127                         p_error_code        => 'MSC_PP_CAL_ALREADY_EXISTS',
4128                         p_message_text      => lv_message_text,
4129                         p_error_text        => lv_error_text);
4130 
4131           IF lv_return <> 0 THEN
4132             RAISE ex_logging_err;
4133           END IF;
4134           RAISE ex_critical_err;
4135 
4136         ELSIF  NVL(rec1.overwrite_flag,'N') = 'Y' AND
4137               (rec1.calendar_start_date > lv_calendar_start_date OR
4138                rec1.calendar_end_date   < lv_calendar_end_date) THEN
4139 
4140            lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4141                         (p_app_short_name    => 'MSC',
4142                          p_error_code        => 'MSC_PP_CAL_PERIOD_INVALID',
4143                          p_message_text      => lv_message_text,
4144                          p_error_text        => lv_error_text);
4145 
4146            IF lv_return <> 0 THEN
4147              RAISE ex_logging_err;
4148            END IF;
4149            RAISE ex_critical_err;
4150 
4151         END IF;
4152 
4153       EXCEPTION
4154         WHEN no_data_found THEN
4155           IF NVL(rec1.overwrite_flag,'N') = 'Y' THEN
4156 
4157              lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4158                           (p_app_short_name    => 'MSC',
4159                            p_error_code        => 'MSC_PP_CAL_UPDATE_NOT_ALLOWED',
4160                            p_message_text      => lv_message_text,
4161                            p_error_text        => lv_error_text);
4162 
4163              IF lv_return <> 0 THEN
4164                RAISE ex_logging_err;
4165              END IF;
4166              RAISE ex_critical_err;
4167           END IF;
4168      END;
4169 
4170       IF NVL(rec1.overwrite_flag,'N') = 'Y' THEN
4171         BEGIN
4172           v_sql_stmt  := 4;  /*Bug 2172537*/
4173           lv_sql_stmt :=
4174           'SELECT 1'
4175           ||' FROM   msc_calendar_shifts mcs, msc_apps_instances mai '
4176           ||' WHERE  NOT EXISTS(SELECT 1 '
4177           ||'        FROM  msc_st_workday_patterns mwp '
4178           ||'        WHERE mwp.shift_name       = mcs.shift_name'
4179           ||'        AND   mcs.calendar_code =    mwp.calendar_code'
4180           ||'        AND   mwp.sr_instance_code = mai.instance_code'
4181           ||'        AND   mwp.process_flag     = '||G_IN_PROCESS||')'
4182           ||' AND    calendar_code    =  :calendar_code'
4183           ||' AND    mai.instance_code = :instance_code'
4184           ||' AND    mcs.sr_instance_id = mai.instance_id'
4185           ||' FOR   UPDATE NOWAIT';
4186 
4187             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
4188           EXECUTE IMMEDIATE lv_sql_stmt
4189                   INTO      lv_rec_no
4190                   USING     rec1.calendar_code,
4191                             v_instance_code;
4192 
4193           IF lv_rec_no =1 THEN
4194 
4195              lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4196                           (p_app_short_name    => 'MSC',
4197                            p_error_code        => 'MSC_PP_SHIFT_NOT_EXIST',
4198                            p_message_text      => lv_message_text,
4199                            p_error_text        => lv_error_text);
4200 
4201              IF lv_return <> 0 THEN
4202                RAISE ex_logging_err;
4203              END IF;
4204              RAISE ex_critical_err;
4205           END IF;
4206         EXCEPTION
4207           WHEN no_data_found THEN
4208             NULL;
4209 
4210           WHEN too_many_rows THEN
4211              lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4212                           (p_app_short_name    => 'MSC',
4213                            p_error_code        => 'MSC_PP_SHIFT_NOT_EXIST',
4214                            p_message_text      => lv_message_text,
4215                            p_error_text        => lv_error_text);
4216 
4217              IF lv_return <> 0 THEN
4218                RAISE ex_logging_err;
4219              END IF;
4220             RAISE ex_critical_err;
4221         END;
4222       END IF;
4223 
4224       v_sql_stmt  := 7;
4225       lv_sql_stmt :=
4226       'SELECT   nvl(SUM(nvl(days_on,0) + nvl(days_off,0)),0)'
4227       ||' FROM  msc_st_workday_patterns'
4228       ||' WHERE calendar_code = :calendar_code'
4229       ||' AND   NVL(shift_name,'||''''||NULL_CHAR||''''||')'
4230       ||'       =              '||''''||NULL_CHAR||''''
4231       ||' AND   deleted_flag <> '||SYS_YES
4232       ||' AND   process_flag = '||G_IN_PROCESS
4233       ||' AND   sr_instance_code = :instance_code';
4234 
4235         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
4236       EXECUTE IMMEDIATE   lv_sql_stmt
4237                 INTO      lv_rec_no
4238                 USING     rec1.calendar_code,
4239                           v_instance_code;
4240 
4241       IF lv_rec_no <= 0 THEN
4242         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4243                      (p_app_short_name    => 'MSC',
4244                       p_error_code        => 'MSC_PP_WORKDAY_PAT_INVALID',
4245                       p_message_text      => lv_message_text,
4246                       p_error_text        => lv_error_text);
4247 
4248         IF lv_return <> 0 THEN
4249           RAISE ex_logging_err;
4250         END IF;
4251 
4252         RAISE ex_critical_err;
4253       END IF;
4254 
4255       --Call to customised validation.
4256       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
4257         (ERRBUF         => lv_error_text,
4258          RETCODE        => lv_return,
4259          pBatchID       => null,
4260          pInstanceCode  => v_instance_code,
4261          pEntityName    => 'MSC_ST_CALENDARS',
4262          pInstanceID    => v_instance_id);
4263 
4264       IF lv_return <> 0 THEN
4265         RAISE ex_logging_err;
4266       END IF;
4267 
4268       IF rec1.deleted_flag NOT IN(1,2) THEN
4269         lv_where_str :=   ' AND   calendar_code      = '||''''||rec1.calendar_code||''''
4270                         ||' AND     sr_instance_code = '||''''||v_instance_code   ||'''';
4271 
4272         --Logging a warning for those records where the deleted_flag value
4273         --is other than Yes/No.
4274         lv_return := MSC_ST_UTIL.LOG_ERROR
4275                        (p_table_name        => 'MSC_ST_CALENDARS',
4276                         p_instance_code     => v_instance_code,
4277                         p_row               => lv_column_names,
4278                         p_severity          => G_SEV_WARNING,
4279                         p_message_text      => lv_message_text,
4280                         p_error_text        => lv_error_text,
4281                         p_where_str         => lv_where_str,
4282                         p_col_name          => 'DELETED_FLAG',
4283                         p_debug             => v_debug,
4284                         p_default_value     => SYS_NO);
4285 
4286         IF lv_return <> 0 THEN
4287           RAISE ex_logging_err;
4288         END IF;
4289       END IF;
4290 
4291       SELECT MIN(to_number(decode(length(rtrim(shift_name,'0123456789')),
4292       NULL,shift_name,'0')))
4293       INTO   lv_shift_num_gen
4294       FROM   msc_st_workday_patterns
4295       WHERE  sr_instance_code = v_instance_code
4296       AND    calendar_code    = rec1.calendar_code
4297       AND    process_flag     = G_IN_PROCESS
4298       AND    shift_name IS NOT NULL;
4299 
4300       --Workday Pattern is validated for a calendar.
4301       FOR rec2 IN c2(rec1.calendar_code)
4302       LOOP
4303         lv_table_name := 'MSC_ST_WORKDAY_PATTERNS';
4304         lv_seq_num    := lv_seq_num + 1;
4305 
4306         lv_column_names :=
4307         'CALENDAR_CODE      ||''~''||'
4308         ||'DAYS_ON          ||''~''||'
4309         ||'DAYS_OFF         ||''~''||'
4310         ||'SR_INSTANCE_CODE ||''~''||'
4311         ||'SHIFT_NUM        ||''~''||'
4312         ||'SHIFT_NAME       ||''~''||'
4313         ||'DESCRIPTION      ||''~''||'
4314         ||'COMPANY_NAME     ||''~''||'
4315         ||'DELETED_FLAG';
4316 
4317         v_sql_stmt  := 8;
4318         SELECT msc_st_workday_patterns_s.NEXTVAL
4319         INTO   lv_transaction_id
4320         FROM   dual;
4321 
4322         v_sql_stmt  := 9;
4323         UPDATE   msc_st_workday_patterns
4324         SET      st_transaction_id  = lv_transaction_id,
4325                  refresh_id         = v_refresh_id,
4326                  seq_num            = lv_seq_num,
4327                  last_update_date   = v_current_date,
4328                  last_updated_by    = v_current_user,
4329                  creation_date      = v_current_date,
4330                  created_by         = v_current_user
4331         WHERE    rowid              = rec2.rowid;
4332 
4333         IF rec2.deleted_flag = SYS_YES THEN
4334 
4335           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4336                        (p_app_short_name    => 'MSC',
4337                         p_error_code        => 'MSC_PP_NO_DELETION',
4338                         p_message_text      => lv_message_text,
4339                         p_error_text        => lv_error_text,
4340                         p_token1            => 'TABLE_NAME',
4341                         p_token_value1      => 'MSC_ST_WORKDAY_PATTERNS');
4342 
4343           IF lv_return <> 0 THEN
4344             RAISE ex_logging_err;
4345           END IF;
4346           RAISE ex_critical_err;
4347 
4348         ELSIF (rec2.days_on  < 0 OR
4349                rec2.days_off < 0) THEN
4350 
4351           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4352                        (p_app_short_name    => 'MSC',
4353                         p_error_code        => 'MSC_PP_CONV_RT_NEG',
4354                         p_message_text      => lv_message_text,
4355                         p_error_text        => lv_error_text,
4356                         p_token1            => 'COLUMN_NAME',
4357                         p_token_value1      => 'DAYS_ON AND DAYS_OFF');
4358 
4359           IF lv_return <> 0 THEN
4360             RAISE ex_logging_err;
4361           END IF;
4362           RAISE ex_critical_err;
4363 
4364         END IF;
4365 
4366         IF rec2.deleted_flag NOT IN(1,2) THEN
4367 
4368            lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4369                        (p_app_short_name    => 'MSC',
4370                         p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
4371                         p_message_text      => lv_message_text,
4372                         p_error_text        => lv_error_text,
4373                         p_token1            => 'COLUMN_NAME',
4374                         p_token_value1      => 'DELETED_FLAG',
4375                         p_token2            => 'DEFAULT_VALUE',
4376                         p_token_value2      => SYS_NO);
4377 
4378           lv_where_str := ' AND st_transaction_id = '||lv_transaction_id;
4379 
4380           --Logging a warning for those records where the deleted_flag value
4381           --is other than Yes/No.
4382           lv_return := MSC_ST_UTIL.LOG_ERROR
4383                        (p_table_name        => 'MSC_ST_WORKDAY_PATTERNS',
4384                         p_instance_code     => v_instance_code,
4385                         p_row               => lv_column_names,
4386                         p_severity          => G_SEV_WARNING,
4387                         p_message_text      => lv_message_text,
4388                         p_error_text        => lv_error_text,
4389                         p_where_str         => lv_where_str,
4390                         p_col_name          => 'DELETED_FLAG',
4391                         p_debug             => v_debug,
4392                         p_default_value     => SYS_NO);
4393 
4394         IF lv_return <> 0 THEN
4395           RAISE ex_logging_err;
4396         END IF;
4397       END IF;
4398 
4399         --Call to customised validation.
4400         MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
4401           (ERRBUF         => lv_error_text,
4402            RETCODE        => lv_return,
4403            pBatchID       => null,
4404            pInstanceCode  => v_instance_code,
4405            pEntityName    => 'MSC_ST_WORKDAY_PATTERNS',
4406            pInstanceID    => v_instance_id);
4407 
4408         IF lv_return <> 0 THEN
4409           RAISE ex_logging_err;
4410         END IF;
4411 
4412       --If the workday pattern definition is for the shifts, then the
4413       --shift details are derived here.
4414       IF rec2.shift_name IS NOT NULL THEN
4415 
4416         BEGIN
4417             v_sql_stmt  := 12;
4418             lv_sql_stmt :=
4419             'SELECT    local_id '
4420             ||' FROM   msc_local_id_setup'
4421             ||' WHERE  char1 = :v_instance_code'
4422             ||' AND    NVL(char2,       '||''''||NULL_CHAR||''''||') = '
4423             ||'        NVL(:company_name,'||''''||NULL_CHAR||''''||') '
4424             ||' AND    char3 = :calendar_code'
4425             ||' AND    char4 = :shift_name'
4426             ||' AND    entity_name = ''SHIFT_NUM'' ';
4427 
4428               MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
4429             EXECUTE IMMEDIATE lv_sql_stmt
4430                     INTO      lv_shift_num
4431                     USING     v_instance_code,
4432                               rec2.company_name,
4433                               rec1.calendar_code,
4434                               rec2.shift_name;
4435 
4436             lv_return := derive_shift_details
4437 	                 (p_transaction_id => lv_transaction_id,
4438                           p_company_name   => rec2.company_name,
4439                           p_calendar_code  => rec1.calendar_code,
4440                           p_shift_name     => rec2.shift_name,
4441                           p_error_text     => lv_error_text,
4442                           p_shift_num      => lv_shift_num,
4443                           p_shift_exist    => SYS_YES);
4444 
4445             IF lv_return <> 0 THEN
4446               RAISE ex_logging_err;
4447             END IF;
4448 
4449           EXCEPTION
4450             WHEN no_data_found THEN
4451 
4452               IF lv_shift_num_gen > 0 THEN
4453                  lv_shift_num := rec2.shift_name;
4454               ELSE
4455                  SELECT msc_st_shift_num_s.NEXTVAL
4456                  INTO   lv_shift_num
4457                  FROM   dual;
4458               END IF;
4459 
4460               lv_return := derive_shift_details
4461                           (p_transaction_id => lv_transaction_id,
4462                            p_company_name   => rec2.company_name,
4463                            p_calendar_code  => rec1.calendar_code,
4464                            p_shift_name     => rec2.shift_name,
4465                            p_error_text     => lv_error_text,
4466                            p_shift_num      => lv_shift_num,
4467                            p_shift_exist    => SYS_NO);
4468 
4469               IF lv_return <> 0 THEN
4470                 RAISE ex_logging_err;
4471               END IF;
4472           END;
4473         END IF;
4474 
4475       END LOOP;
4476 
4477      --Validation for shift times table.
4478       FOR rec3 IN c3(rec1.calendar_code)
4479       LOOP
4480         lv_table_name    := 'MSC_ST_SHIFT_TIMES';
4481         lv_column_names :=
4482         'CALENDAR_CODE      ||''~''||'
4483         ||'SHIFT_NAME       ||''~''||'
4484         ||'SHIFT_NUM        ||''~''||'
4485         ||'SR_INSTANCE_CODE ||''~''||'
4486         ||'FROM_TIME        ||''~''||'
4487         ||'TO_TIME          ||''~''||'
4488         ||'COMPANY_NAME     ||''~''||'
4489         ||'DELETED_FLAG';
4490 
4491         v_sql_stmt  := 13;
4492         SELECT msc_st_shift_times_s.NEXTVAL
4493         INTO   lv_transaction_id
4494         FROM   dual;
4495 
4496         v_sql_stmt  := 14;
4497         UPDATE msc_st_shift_times
4498         SET    st_transaction_id = lv_transaction_id,
4499                refresh_id        = v_refresh_id,
4500                last_update_date  = v_current_date,
4501                last_updated_by   = v_current_user,
4502                creation_date     = v_current_date,
4503                created_by        = v_current_user
4504         WHERE  rowid             = rec3.rowid;
4505 
4506         IF    (rec3.shift_name IS NULL OR
4507                rec3.shift_num  IS NULL) THEN
4508 
4509           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4510                        (p_app_short_name    => 'MSC',
4511                         p_error_code        => 'MSC_PP_COL_VAL_NULL',
4512                         p_message_text      => lv_message_text,
4513                         p_error_text        => lv_error_text,
4514                         p_token1            => 'COLUMN_NAME',
4515                         p_token_value1      => 'SHIFT_NAME OR SHIFT_NUM ');
4516           RAISE ex_critical_err;
4517 
4518         ELSIF rec3.deleted_flag = SYS_YES THEN
4519 
4520           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4521                        (p_app_short_name    => 'MSC',
4522                         p_error_code        => 'MSC_PP_NO_DELETION',
4523                         p_message_text      => lv_message_text,
4524                         p_error_text        => lv_error_text,
4525                         p_token1            => 'TABLE_NAME',
4526                         p_token_value1      => lv_table_name);
4527 
4528           IF lv_return <> 0 THEN
4529             RAISE ex_logging_err;
4530           END IF;
4531           RAISE ex_critical_err;
4532 
4533         END IF;
4534 
4535         IF rec3.deleted_flag NOT IN(1,2) THEN
4536           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4537                          (p_app_short_name    => 'MSC',
4538                           p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
4539                           p_message_text      => lv_message_text,
4540                           p_error_text        => lv_error_text,
4541                           p_token1            => 'COLUMN_NAME',
4542                           p_token_value1      => 'DELETED_FLAG',
4543                           p_token2            => 'DEFAULT_VALUE',
4544                           p_token_value2      => SYS_NO);
4545 
4546           lv_where_str := ' AND st_transaction_id = '||lv_transaction_id;
4547 
4548           --Logging a warning for those records where the deleted_flag value
4549           --is other than Yes/No.
4550           lv_return := MSC_ST_UTIL.LOG_ERROR
4551                        (p_table_name        => 'MSC_ST_SHIFT_TIMES',
4552                         p_instance_code     => v_instance_code,
4553                         p_row               => lv_column_names,
4554                         p_severity          => G_SEV_WARNING,
4555                         p_message_text      => lv_message_text,
4556                         p_error_text        => lv_error_text,
4557                         p_where_str         => lv_where_str,
4558                         p_col_name          => 'DELETED_FLAG',
4559                         p_debug             => v_debug,
4560                         p_default_value     => SYS_NO);
4561 
4562           IF lv_return <> 0 THEN
4563             RAISE ex_logging_err;
4564           END IF;
4565         END IF;
4566 
4567         BEGIN
4568           v_sql_stmt  := 17; /*Bug 2172537-Modified the satement*/
4569           lv_sql_stmt :=
4570           'SELECT 1'
4571           ||' FROM   DUAL'
4572           ||' WHERE  NOT EXISTS(SELECT 1'
4573           ||'        FROM  msc_st_shift_times'
4574           ||'        WHERE sr_instance_code  =  :sr_instance_code'
4575           ||'        AND   process_flag      =  '||G_IN_PROCESS
4576           ||'        AND   deleted_flag      =  '||SYS_NO
4577           ||'        AND   calendar_code     =  :calendar_code'
4578           ||'        AND   shift_num         =  :shift_name'
4579           ||'        AND   from_time         =  :from_time'
4580           ||'        AND   to_time           =  :to_time'
4581           ||'        AND   st_transaction_id <> :lv_transaction_id'
4582           ||'        UNION'
4583           ||'        SELECT 1'
4584           ||'        FROM   msc_st_shift_times'
4585           ||'        WHERE sr_instance_code  =  :sr_instance_code'
4586           ||'        AND   process_flag      =  '||G_IN_PROCESS
4587           ||'        AND   deleted_flag      =  '||SYS_NO
4588           ||'        AND   calendar_code     =  :calendar_code'
4589           ||'        AND   shift_num         =  :shift_name'
4590           ||'        AND   from_time         >  :from_time'
4591           ||'        AND   from_time         <  :to_time'
4592           ||'        AND   st_transaction_id <> :lv_transaction_id'
4593           ||'        UNION'
4594           ||'        SELECT 1'
4595           ||'        FROM   msc_st_shift_times'
4596           ||'        WHERE sr_instance_code  =  :sr_instance_code'
4597           ||'        AND   process_flag      =  '||G_IN_PROCESS
4598           ||'        AND   deleted_flag      =  '||SYS_NO
4599           ||'        AND   calendar_code     =  :calendar_code'
4600           ||'        AND   shift_num         =  :shift_name'
4601           ||'        AND   to_time            > :from_time'
4602           ||'        AND   to_time           <  :to_time'
4603           ||'        AND   st_transaction_id <> :lv_transaction_id)';
4604 
4605             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
4606           EXECUTE IMMEDIATE lv_sql_stmt
4607                   INTO      lv_rec_no
4608                   USING     v_instance_code,
4609                             rec1.calendar_code,
4610                             rec3.shift_name,
4611                             rec3.from_time,
4612                             rec3.to_time,
4613                             lv_transaction_id,
4614                             v_instance_code,
4615                             rec1.calendar_code,
4616                             rec3.shift_name,
4617                             rec3.from_time,
4618                             rec3.to_time,
4619                             lv_transaction_id,
4620                             v_instance_code,
4621                             rec1.calendar_code,
4622                             rec3.shift_name,
4623                             rec3.from_time,
4624                             rec3.to_time,
4625                             lv_transaction_id;
4626 
4627         EXCEPTION
4628           WHEN NO_DATA_FOUND THEN
4629             lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4630                          (p_app_short_name    => 'MSC',
4631                           p_error_code        => 'MSC_PP_SHIFT_TIME_OVERLAP',
4632                           p_message_text      => lv_message_text,
4633                           p_error_text        => lv_error_text);
4634 
4635             IF lv_return <> 0 THEN
4636               RAISE ex_logging_err;
4637             END IF;
4638             RAISE ex_critical_err;
4639         END;
4640 
4641         --Call to customised validation.
4642         MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
4643           (ERRBUF         => lv_error_text,
4644            RETCODE        => lv_return,
4645            pBatchID       => null,
4646            pInstanceCode  => v_instance_code,
4647            pEntityName    => 'MSC_ST_SHIFT_TIMES',
4648            pInstanceID    => v_instance_id);
4649 
4650         IF lv_return <> 0 THEN
4651           RAISE ex_logging_err;
4652         END IF;
4653 
4654       END LOOP;
4655 
4656       --validation for the table msc_st_Calendar_exceptions.
4657       --Added to fix the bug#2748859
4658 
4659       --Getting the error message text
4660       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4661                    (p_app_short_name    => 'MSC',
4662                     p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
4663                     p_message_text      => lv_message_text,
4664                     p_error_text        => lv_error_text);
4665 
4666       IF lv_return <> 0 THEN
4667         RAISE ex_logging_err;
4668       END IF;
4669 
4670       --Duplicate records check for the records whose source is XML
4671 
4672       UPDATE  msc_st_calendar_exceptions mc1
4673       SET     process_flag = G_ERROR_FLG,
4674               error_text   = lv_message_text
4675       WHERE   message_id <  (SELECT MAX(message_id)
4676                              FROM   msc_st_calendar_exceptions mc2
4677                              WHERE  mc2.sr_instance_code  = mc1.sr_instance_code
4678                              AND    mc2.calendar_code     = mc1.calendar_code
4679                              AND    mc2.exception_date    = mc1.exception_date
4680                              AND    mc2.process_flag      = G_IN_PROCESS
4681                              AND    NVL(mc2.message_id,NULL_VALUE) <>NULL_VALUE)
4682       AND     mc1.calendar_code            = rec1.calendar_code
4683       AND     mc1.process_flag             = G_IN_PROCESS
4684       AND     mc1.sr_instance_code         = v_instance_code
4685       AND     NVL(mc1.message_id,NULL_VALUE) <> NULL_VALUE;
4686 
4687 
4688       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4689                    (p_app_short_name    => 'MSC',
4690                     p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
4691                     p_message_text      => lv_message_text,
4692                     p_error_text        => lv_error_text);
4693 
4694       IF lv_return <> 0 THEN
4695         RAISE ex_logging_err;
4696       END IF;
4697 
4698       --Duplicate records check for the records whose source is other than XML
4699       --Different SQL is used because in XML we can identify the latest records
4700       --whereas in batch load we cannot.
4701 
4702       UPDATE  msc_st_calendar_exceptions mc1
4703       SET     process_flag = G_ERROR_FLG,
4704               error_text   = lv_message_text
4705       WHERE   EXISTS( SELECT 1
4706                       FROM   msc_st_calendar_exceptions mc2
4707                       WHERE  mc2.sr_instance_code  = mc1.sr_instance_code
4708                       AND    mc2.calendar_code     = mc1.calendar_code
4709                       AND    mc2.exception_date    = mc1.exception_date
4710                       AND    mc2.process_flag      = G_IN_PROCESS
4711                       AND    NVL(mc2.message_id,NULL_VALUE) = NULL_VALUE
4712                       GROUP BY sr_instance_code,company_name,calendar_code,exception_date
4713                       HAVING COUNT(*) > 1)
4714       AND     mc1.calendar_code            = rec1.calendar_code
4715       AND     mc1.process_flag             = G_IN_PROCESS
4716       AND     mc1.sr_instance_code         = v_instance_code
4717       AND     NVL(mc1.message_id,NULL_VALUE) = NULL_VALUE;
4718 
4719       --Added to fix the bug#2748859
4720 
4721       FOR rec4 IN c4(rec1.calendar_code)
4722       LOOP
4723         lv_table_name    := 'MSC_ST_CALENDAR_EXCEPTIONS';
4724         lv_column_names :=
4725         'CALENDAR_CODE      ||''~''||'
4726         ||'EXCEPTION_DATE   ||''~''||'
4727         ||'EXCEPTION_TYPE   ||''~''||'
4728         ||'SR_INSTANCE_CODE ||''~''||'
4729         ||'COMPANY_NAME     ||''~''||'
4730         ||'EXCEPTION_SET_ID ||''~''||'
4731         ||'DELETED_FLAG';
4732 
4733         v_sql_stmt  := 18;
4734         SELECT msc_st_calendar_exceptions_s.NEXTVAL
4735         INTO   lv_transaction_id
4736         FROM   dual;
4737 
4738         v_sql_stmt  := 19;
4739         UPDATE  msc_st_calendar_exceptions
4740         SET     st_transaction_id = lv_transaction_id,
4741                 refresh_id        = v_refresh_id,
4742                 exception_set_id  = -1,
4743                 last_update_date  = v_current_date,
4744                 last_updated_by   = v_current_user,
4745                 creation_date     = v_current_date,
4746                 created_by        = v_current_user
4747         WHERE   rowid             = rec4.rowid;
4748 
4749         IF rec4.deleted_flag = SYS_YES THEN
4750           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4751                          (p_app_short_name    => 'MSC',
4752                           p_error_code        => 'MSC_PP_NO_DELETION',
4753                           p_message_text      => lv_message_text,
4754                           p_error_text        => lv_error_text,
4755                           p_token1            => 'TABLE_NAME',
4756                           p_token_value1      => lv_table_name);
4757 
4758           IF lv_return <> 0 THEN
4759             RAISE ex_logging_err;
4760           END IF;
4761           RAISE ex_critical_err;
4762 
4763         ELSIF rec4.exception_date IS NULL THEN
4764 
4765           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4766                          (p_app_short_name    => 'MSC',
4767                           p_error_code        => 'MSC_PP_COL_VAL_NULL',
4768                           p_message_text      => lv_message_text,
4769                           p_error_text        => lv_error_text,
4770                           p_token1            => 'COLUMN_NAME',
4771                           p_token_value1      => 'EXCEPTION_DATE ');
4772 
4773           IF lv_return <> 0 THEN
4774             RAISE ex_logging_err;
4775           END IF;
4776           RAISE ex_critical_err;
4777 
4778         ELSIF (rec4.exception_date < rec1.calendar_start_date OR
4779                rec4.exception_date > rec1.calendar_end_date) THEN
4780           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4781                          (p_app_short_name    => 'MSC',
4782                           p_error_code        => 'MSC_PP_EXCEP_DATE_INVALID',
4783                           p_message_text      => lv_message_text,
4784                           p_error_text        => lv_error_text);
4785 
4786           IF lv_return <> 0 THEN
4787             RAISE ex_logging_err;
4788           END IF;
4789           RAISE ex_critical_err;
4790 
4791         ELSIF rec4.exception_type NOT IN(1,2) THEN
4792           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4793                          (p_app_short_name    => 'MSC',
4794                           p_error_code        => 'MSC_PP_INVALID_VALUE',
4795                           p_message_text      => lv_message_text,
4796                           p_error_text        => lv_error_text,
4797                           p_token1            => 'COLUMN_NAME',
4798                           p_token_value1      => 'EXCEPTION_TYPE');
4799 
4800           IF lv_return <> 0 THEN
4801             RAISE ex_logging_err;
4802           END IF;
4803           RAISE ex_critical_err;
4804         END IF;
4805 
4806         IF v_instance_type = G_SCE then
4807           v_sql_stmt  := 20;
4808           lv_sql_stmt :=
4809           '   UPDATE  msc_st_calendar_exceptions mc'
4810           ||' SET     company_id = (SELECT party_id '
4811           ||'         FROM   hz_parties hp'
4812           ||'         WHERE  hp.party_name = mc.company_name)'
4813           ||' WHERE   st_transaction_id    = :lv_transaction_id';
4814 
4815             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
4816           EXECUTE IMMEDIATE lv_sql_stmt
4817                   USING     lv_transaction_id;
4818 
4819           IF SQL%NOTFOUND THEN
4820             lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4821                            (p_app_short_name    => 'MSC',
4822                             p_error_code        => 'MSC_PP_INVALID_VALUE',
4823                             p_message_text      => lv_message_text,
4824                             p_error_text        => lv_error_text,
4825                             p_token1            => 'COLUMN_NAME',
4826                             p_token_value1      => 'COMPANY_NAME');
4827 
4828             IF lv_return <> 0 THEN
4829               RAISE ex_logging_err;
4830             END IF;
4831             RAISE ex_critical_err;
4832           END IF;
4833         ELSE
4834           v_sql_stmt  := 21;
4835           lv_sql_stmt :=
4836           '   UPDATE msc_st_calendar_exceptions '
4837           ||' SET    company_id        = '||G_COMPANY_ID
4838           ||' WHERE  st_transaction_id = :lv_transaction_id';
4839 
4840             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
4841           EXECUTE IMMEDIATE lv_sql_stmt
4842                   USING     lv_transaction_id;
4843 
4844         END IF;
4845 
4846         IF rec4.deleted_flag NOT IN(1,2) THEN
4847           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4848                          (p_app_short_name    => 'MSC',
4849                           p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
4850                           p_message_text      => lv_message_text,
4851                           p_error_text        => lv_error_text,
4852                           p_token1            => 'COLUMN_NAME',
4853                           p_token_value1      => 'DELETED_FLAG',
4854                           p_token2            => 'DEFAULT_VALUE',
4855                           p_token_value2      => SYS_NO);
4856 
4857           lv_where_str := ' AND st_transaction_id = '||lv_transaction_id;
4858 
4859           --Logging a warning for those records where the deleted_flag value
4860           --is other than Yes/No.
4861           lv_return := MSC_ST_UTIL.LOG_ERROR
4862                         (p_table_name        => 'MSC_ST_CALENDAR_EXCEPTIONS',
4863                          p_instance_code     => v_instance_code,
4864                          p_row               => lv_column_names,
4865                          p_severity          => G_SEV_WARNING,
4866                          p_message_text      => lv_message_text,
4867                          p_error_text        => lv_error_text,
4868                          p_where_str         => lv_where_str,
4869                          p_col_name          => 'DELETED_FLAG',
4870                          p_debug             => v_debug,
4871                          p_default_value     => SYS_NO);
4872 
4873           IF lv_return <> 0 THEN
4874             RAISE ex_logging_err;
4875           END IF;
4876         END IF;
4877 
4878         --Call to customised validation.
4879         MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
4880           (ERRBUF         => lv_error_text,
4881            RETCODE        => lv_return,
4882            pBatchID       => null,
4883            pInstanceCode  => v_instance_code,
4884            pEntityName    => 'MSC_ST_CALENDAR_EXCEPTIONS',
4885            pInstanceID    => v_instance_id);
4886 
4887         IF lv_return <> 0 THEN
4888           RAISE ex_logging_err;
4889         END IF;
4890 
4891       END LOOP;
4892 
4893       --validation for the table msc_st_shift_exceptions.
4894       --Added to fix the bug#2748859
4895 
4896       --Getting the error message text
4897       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4898                  (p_app_short_name    => 'MSC',
4899                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
4900                   p_message_text      => lv_message_text,
4901                   p_error_text        => lv_error_text);
4902 
4903       IF lv_return <> 0 THEN
4904         RAISE ex_logging_err;
4905       END IF;
4906 
4907       --Duplicate records check for the records whose source is XML
4908 
4909       UPDATE  msc_st_shift_exceptions mc1
4910       SET     process_flag = G_ERROR_FLG,
4911               error_text   = lv_message_text
4912       WHERE   message_id <  (SELECT MAX(message_id)
4913                            FROM   msc_st_shift_exceptions mc2
4914                            WHERE  mc2.sr_instance_code          = mc1.sr_instance_code
4915                            AND    mc2.calendar_code             = mc1.calendar_code
4916                            AND    mc2.exception_date            = mc1.exception_date
4917                            AND    nvl(mc2.shift_name,NULL_CHAR) = nvl(mc1.shift_name,NULL_CHAR)
4918                            AND    nvl(mc2.shift_num,NULL_VALUE) = nvl(mc1.shift_num,NULL_VALUE)
4919                            AND    mc2.process_flag              = G_IN_PROCESS
4920                            AND    NVL(mc2.message_id,NULL_VALUE) <>NULL_VALUE)
4921       AND     mc1.calendar_code            = rec1.calendar_code
4922       AND     mc1.process_flag             = G_IN_PROCESS
4923       AND     mc1.sr_instance_code         = v_instance_code
4924       AND     NVL(mc1.message_id,NULL_VALUE) <> NULL_VALUE;
4925 
4926 
4927       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4928                  (p_app_short_name    => 'MSC',
4929                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
4930                   p_message_text      => lv_message_text,
4931                   p_error_text        => lv_error_text);
4932 
4933       IF lv_return <> 0 THEN
4934         RAISE ex_logging_err;
4935       END IF;
4936 
4937       --Duplicate records check for the records whose source is other than XML
4938       --Different SQL is used because in XML we can identify the latest records
4939       --whereas in batch load we cannot.
4940 
4941       UPDATE  msc_st_shift_exceptions mc1
4942       SET     process_flag = G_ERROR_FLG,
4943               error_text   = lv_message_text
4944       WHERE   EXISTS( SELECT 1
4945                     FROM   msc_st_shift_exceptions mc2
4946                     WHERE  mc2.sr_instance_code           = mc1.sr_instance_code
4947                     AND    mc2.calendar_code              = mc1.calendar_code
4948                     AND    mc2.exception_date             = mc1.exception_date
4949                     AND    nvl(mc2.shift_name,NULL_CHAR)  = nvl(mc1.shift_name,NULL_CHAR)
4950                     AND    nvl(mc2.shift_num,NULL_VALUE)  = nvl(mc1.shift_num,NULL_VALUE)
4951                     AND    mc2.process_flag               = G_IN_PROCESS
4952                     AND    NVL(mc2.message_id,NULL_VALUE) = NULL_VALUE
4953                     GROUP BY sr_instance_code,company_name,calendar_code,exception_date
4954                     HAVING COUNT(*) > 1)
4955       AND     mc1.calendar_code            = rec1.calendar_code
4956       AND     mc1.process_flag             = G_IN_PROCESS
4957       AND     mc1.sr_instance_code         = v_instance_code
4958       AND     NVL(mc1.message_id,NULL_VALUE) = NULL_VALUE;
4959       --Added to fix the bug#2748859
4960 
4961       FOR rec5 IN c5(rec1.calendar_code)
4962       LOOP
4963         lv_table_name    := 'MSC_ST_SHIFT_EXCEPTIONS';
4964         lv_column_names :=
4965         'CALENDAR_CODE     ||''~''||'
4966         ||'EXCEPTION_DATE  ||''~''||'
4967         ||'EXCEPTION_TYPE  ||''~''||'
4968         ||'SR_INSTANCE_CODE||''~''||'
4969         ||'SHIFT_NUM       ||''~''||'
4970         ||'SHIFT_NAME      ||''~''||'
4971         ||'COMPANY_NAME    ||''~''||'
4972         ||'EXCEPTION_SET_ID||''~''||'
4973         ||'DELETED_FLAG';
4974 
4975         v_sql_stmt  := 22;
4976         SELECT msc_st_shift_exceptions_s.NEXTVAL
4977         INTO   lv_transaction_id
4978         FROM   dual;
4979 
4980         v_sql_stmt  := 23;
4981         UPDATE  msc_st_shift_exceptions
4982         SET     st_transaction_id = lv_transaction_id,
4983                 refresh_id        = v_refresh_id,
4984                 exception_set_id  = -1,
4985                 last_update_date  = v_current_date,
4986                 last_updated_by   = v_current_user,
4987                 creation_date     = v_current_date,
4988                 created_by        = v_current_user
4989         WHERE   rowid             = rec5.rowid;
4990 
4991         IF rec5.deleted_flag = SYS_YES THEN
4992           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
4993                          (p_app_short_name    => 'MSC',
4994                           p_error_code        => 'MSC_PP_NO_DELETION',
4995                           p_message_text      => lv_message_text,
4996                           p_error_text        => lv_error_text,
4997                           p_token1            => 'TABLE_NAME',
4998                           p_token_value1      => 'MSC_ST_SHIFT_EXCEPTIONS');
4999 
5000           IF lv_return <> 0 THEN
5001             RAISE ex_logging_err;
5002           END IF;
5003           RAISE ex_critical_err;
5004 
5005         ELSIF rec5.exception_date IS NULL THEN
5006           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5007                          (p_app_short_name    => 'MSC',
5008                           p_error_code        => 'MSC_PP_COL_VAL_NULL',
5009                           p_message_text      => lv_message_text,
5010                           p_error_text        => lv_error_text,
5011                           p_token1            => 'COLUMN_NAME',
5012                           p_token_value1      => 'EXCEPTION_DATE ');
5013 
5014           IF lv_return <> 0 THEN
5015             RAISE ex_logging_err;
5016           END IF;
5017           RAISE ex_critical_err;
5018 
5019         ELSIF (rec5.exception_date < rec1.calendar_start_date OR
5020                rec5.exception_date > rec1.calendar_end_date) THEN
5021           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5022                         (p_app_short_name    => 'MSC',
5023                          p_error_code        => 'MSC_PP_EXCEP_DATE_INVALID',
5024                          p_message_text      => lv_message_text,
5025                          p_error_text        => lv_error_text);
5026 
5027           IF lv_return <> 0 THEN
5028             RAISE ex_logging_err;
5029           END IF;
5030           RAISE ex_critical_err;
5031 
5032         ELSIF rec5.exception_type NOT IN(1,2) THEN
5033           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5034                          (p_app_short_name    => 'MSC',
5035                           p_error_code        => 'MSC_PP_INVALID_VALUE',
5036                           p_message_text      => lv_message_text,
5037                           p_error_text        => lv_error_text,
5038                           p_token1            => 'COLUMN_NAME',
5039                           p_token_value1      => 'EXCEPTION_TYPE');
5040 
5041           IF lv_return <> 0 THEN
5042             RAISE ex_logging_err;
5043           END IF;
5044           RAISE ex_critical_err;
5045 
5046         ELSIF rec5.shift_name IS NULL THEN
5047           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5048                          (p_app_short_name    => 'MSC',
5049                           p_error_code        => 'MSC_PP_COL_VAL_NULL',
5050                           p_message_text      => lv_message_text,
5051                           p_error_text        => lv_error_text,
5052                           p_token1            => 'COLUMN_NAME',
5053                           p_token_value1      => 'SHIFT_NAME');
5054 
5055            IF lv_return <> 0 THEN
5056              RAISE ex_logging_err;
5057            END IF;
5058            RAISE ex_critical_err;
5059 
5060         END IF;
5061 
5062         IF v_instance_type = G_SCE then
5063           v_sql_stmt  := 24;
5064           lv_sql_stmt :=
5065           '   UPDATE  msc_st_shift_exceptions mc'
5066           ||' SET     company_id = (SELECT party_id '
5067           ||'         FROM   hz_parties hp'
5068           ||'         WHERE  hp.party_name = mc.company_name)'
5069           ||' WHERE st_transaction_id = :lv_transaction_id ';
5070 
5071             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5072           EXECUTE IMMEDIATE lv_sql_stmt
5073                   USING     lv_transaction_id;
5074 
5075           IF SQL%NOTFOUND THEN
5076             lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5077                            (p_app_short_name    => 'MSC',
5078                             p_error_code        => 'MSC_PP_INVALID_VALUE',
5079                             p_message_text      => lv_message_text,
5080                             p_error_text        => lv_error_text,
5081                             p_token1            => 'COLUMN_NAME',
5082                             p_token_value1      => 'COMPANY_NAME');
5083 
5084             IF lv_return <> 0 THEN
5085               RAISE ex_logging_err;
5086             END IF;
5087             RAISE ex_critical_err;
5088           END IF;
5089         ELSE
5090           v_sql_stmt  := 25;
5091           lv_sql_stmt :=
5092           '   UPDATE msc_st_shift_exceptions'
5093           ||' SET    company_id = '||G_COMPANY_ID
5094           ||' WHERE  st_transaction_id  = :lv_transaction_id ';
5095 
5096             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5097           EXECUTE IMMEDIATE lv_sql_stmt
5098                   USING     lv_transaction_id;
5099 
5100         END IF;
5101 
5102         IF rec5.deleted_flag NOT IN(1,2) THEN
5103           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5104                          (p_app_short_name    => 'MSC',
5105                           p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
5106                           p_message_text      => lv_message_text,
5107                           p_error_text        => lv_error_text,
5108                           p_token1            => 'COLUMN_NAME',
5109                           p_token_value1      => 'DELETED_FLAG',
5110                           p_token2            => 'DEFAULT_VALUE',
5111                           p_token_value2      => SYS_NO);
5112 
5113           lv_where_str := ' AND st_transaction_id = '||lv_transaction_id;
5114 
5115           --Logging a warning for those records where the deleted_flag value
5116           --is other than Yes/No.
5117           lv_return := MSC_ST_UTIL.LOG_ERROR
5118                         (p_table_name        => 'MSC_ST_SHIFT_EXCEPTIONS',
5119                          p_instance_code     => v_instance_code,
5120                          p_row               => lv_column_names,
5121                          p_severity          => G_SEV_WARNING,
5122                          p_message_text      => lv_message_text,
5123                          p_error_text        => lv_error_text,
5124                          p_where_str         => lv_where_str,
5125                          p_col_name          => 'DELETED_FLAG',
5126                          p_debug             => v_debug,
5127                          p_default_value     => SYS_NO);
5128 
5129           IF lv_return <> 0 THEN
5130             RAISE ex_logging_err;
5131           END IF;
5132         END IF;
5133 
5134         --Call to customised validation.
5135         MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
5136           (ERRBUF         => lv_error_text,
5137            RETCODE        => lv_return,
5138            pBatchID       => null,
5139            pInstanceCode  => v_instance_code,
5140            pEntityName    => 'MSC_ST_SHIFT_EXCEPTIONS',
5141            pInstanceID    => v_instance_id);
5142 
5143         IF lv_return <> 0 THEN
5144           RAISE ex_logging_err;
5145         END IF;
5146 
5147       END LOOP;
5148 
5149       lv_where_str :=
5150       ' AND   calendar_code      = '||''''||rec1.calendar_code||'''';
5151       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
5152                      (p_table_name     => 'MSC_ST_CALENDARS',
5153                       p_instance_id    => v_instance_id,
5154                       p_instance_code  => v_instance_code,
5155                       p_process_flag   => G_VALID,
5156                       p_where_str      => lv_where_str,
5157                       p_debug          => v_debug,
5158                       p_error_text     => lv_error_text);
5159 
5160       IF lv_return <> 0 THEN
5161         RAISE ex_logging_err;
5162       END IF;
5163 
5164       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
5165                      (p_table_name     => 'MSC_ST_WORKDAY_PATTERNS',
5166                       p_instance_id    => v_instance_id,
5167                       p_instance_code  => v_instance_code,
5168                       p_process_flag   => G_VALID,
5169                       p_where_str      => lv_where_str,
5170                       p_debug          => v_debug,
5171                       p_error_text     => lv_error_text);
5172 
5173       IF lv_return <> 0 THEN
5174         RAISE ex_logging_err;
5175       END IF;
5176 
5177 
5178       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
5179                    (p_table_name     => 'MSC_ST_CALENDAR_SHIFTS',
5180                     p_instance_id    => v_instance_id,
5181                     p_instance_code  => v_instance_code,
5182                     p_process_flag   => G_VALID,
5183                     p_where_str      => lv_where_str,
5184                     p_debug          => v_debug,
5185                     p_error_text     => lv_error_text);
5186 
5187       IF lv_return <> 0 THEN
5188         RAISE ex_logging_err;
5189       END IF;
5190 
5191       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
5192                    (p_table_name     => 'MSC_ST_SHIFT_TIMES',
5193                     p_instance_id    => v_instance_id,
5194                     p_instance_code  => v_instance_code,
5195                     p_process_flag   => G_VALID,
5196                     p_where_str      => lv_where_str,
5197                     p_debug          => v_debug,
5198                     p_error_text     => lv_error_text);
5199 
5200       IF lv_return <> 0 THEN
5201         RAISE ex_logging_err;
5202       END IF;
5203 
5204       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
5205                      (p_table_name     => 'MSC_ST_CALENDAR_EXCEPTIONS',
5206                       p_instance_id    => v_instance_id,
5207                       p_instance_code  => v_instance_code,
5208                       p_process_flag   => G_VALID,
5209                       p_where_str      => lv_where_str,
5210                       p_debug          => v_debug,
5211                       p_error_text     => lv_error_text);
5212 
5213       IF lv_return <> 0 THEN
5214         RAISE ex_logging_err;
5215       END IF;
5216 
5217       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
5218                      (p_table_name     => 'MSC_ST_SHIFT_EXCEPTIONS',
5219                       p_instance_id    => v_instance_id,
5220                       p_instance_code  => v_instance_code,
5221                       p_process_flag   => G_VALID,
5222                       p_where_str      => lv_where_str,
5223                       p_debug          => v_debug,
5224                       p_error_text     => lv_error_text);
5225 
5226       IF lv_return <> 0 THEN
5227         RAISE ex_logging_err;
5228       END IF;
5229 
5230       IF rec1.overwrite_flag = 'Y' THEN
5231         v_sql_stmt  := 26;  /*Bug 2172537*/
5232         lv_sql_stmt :=
5233         'UPDATE    msc_calendars '
5234         ||' SET    calendar_start_date     = :calendar_start_date,'
5235         ||'        calendar_end_date       = :calendar_end_date,'
5236         ||'        quarterly_calendar_type = :quarterly_calendar_type,'
5237         ||'        week_start_day          = :week_start_day,'
5238         ||'        creation_date           = :v_current_date,'
5239         ||'        created_by              = :v_current_user,'
5240         ||'        last_update_date        = :v_current_date,'
5241         ||'        last_updated_by         = :v_current_user'
5242         ||' WHERE  calendar_code           = :calendar_code'
5243         ||' AND    sr_instance_id          = :instance_id';
5244 
5245           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5246         EXECUTE IMMEDIATE lv_sql_stmt
5247                 USING     rec1.calendar_start_date,
5248                           rec1.calendar_end_date,
5249                           rec1.quarterly_calendar_type,
5250                           rec1.week_start_day,
5251                           v_current_date,
5252                           v_current_user,
5253                           v_current_date,
5254                           v_current_user,
5255                           rec1.calendar_code,
5256                           v_instance_id;
5257 
5258       ELSE
5259         v_sql_stmt  := 27;
5260         lv_sql_stmt :=
5261         'DELETE FROM msc_calendars '
5262         ||' WHERE calendar_code    = :calendar_code';
5263 
5264           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5265         EXECUTE IMMEDIATE lv_sql_stmt
5266                 USING     rec1.calendar_code;
5267 
5268         v_sql_stmt  := 28;   /*Bug 2172537*/
5269         lv_sql_stmt :=
5270         'INSERT INTO msc_calendars '
5271         ||' (calendar_code,'
5272         ||'  description,'
5273         ||'  days_on,'
5274         ||'  days_off,'
5275         ||'  calendar_start_date,'
5276         ||'  calendar_end_date,'
5277         ||'  quarterly_calendar_type,'
5278         ||'  sr_instance_id,'
5279         ||'  last_update_date,'
5280         ||'  last_updated_by,'
5281         ||'  creation_date,'
5282         ||'  created_by,'
5283         ||'  last_update_login,'
5284         ||'  request_id,'
5285         ||'  program_application_id,'
5286         ||'  program_id,'
5287         ||'  program_update_date,'
5288         ||'  refresh_id,'
5289         ||'  overwrite_flag,'
5290         ||'  week_start_day)'
5291         ||'  SELECT '
5292         ||'  calendar_code,'
5293         ||'  description,'
5294         ||'  days_on,'
5295         ||'  days_off,'
5296         ||'  calendar_start_date,'
5297         ||'  calendar_end_date,'
5298         ||'  quarterly_calendar_type,'
5299         ||'  sr_instance_id,'
5300         ||'  last_update_date,'
5301         ||'  last_updated_by,'
5302         ||'  creation_date,'
5303         ||'  created_by,'
5304         ||'  last_update_login,'
5305         ||'  request_id,'
5306         ||'  program_application_id,'
5307         ||'  program_id,'
5308         ||'  program_update_date,'
5309         ||'  refresh_id,'
5310         ||'  overwrite_flag,'
5311         ||'  week_start_day'
5312         ||' FROM  msc_st_calendars'
5313         ||' WHERE calendar_code    = :calendar_code'
5314         ||' AND   sr_instance_code = :sr_instance_code'
5315         ||' AND   process_flag      = '||G_VALID;
5316 
5317 
5318           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5319         EXECUTE IMMEDIATE lv_sql_stmt
5320                 USING     rec1.calendar_code,
5321                           v_instance_code;
5322       END IF;
5323 
5324       v_sql_stmt  := 29; /*Bug 2172537*/
5325       lv_sql_stmt :=
5326       'DELETE FROM msc_workday_patterns '
5327       ||' WHERE calendar_code    = :calendar_code';
5328 
5329         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5330       EXECUTE IMMEDIATE lv_sql_stmt
5331               USING     rec1.calendar_code;
5332 
5333       v_sql_stmt  := 30; /*Bug 2172537*/
5334       lv_sql_stmt :=
5335       'INSERT INTO msc_workday_patterns '
5336       ||' (calendar_code,'
5337       ||'  description,'
5338       ||'  sr_instance_id,'
5339       ||'  shift_num,'
5340       ||'  seq_num,'
5341       ||'  last_update_date,'
5342       ||'  last_updated_by,'
5343       ||'  creation_date,'
5344       ||'  created_by,'
5345       ||'  last_update_login,'
5346       ||'  days_on,'
5347       ||'  days_off,'
5348       ||'  request_id,'
5349       ||'  program_application_id,'
5350       ||'  program_id,'
5351       ||'  program_update_date,'
5352       ||'  refresh_id)'
5353       ||' SELECT '
5354       ||' calendar_code,'
5355       ||'  description,'
5356       ||'  sr_instance_id,'
5357       ||'  shift_num,'
5358       ||'  seq_num,'
5359       ||'  last_update_date,'
5360       ||'  last_updated_by,'
5361       ||'  creation_date,'
5362       ||'  created_by,'
5363       ||'  last_update_login,'
5364       ||'  days_on,'
5365       ||'  days_off,'
5366       ||'  request_id,'
5367       ||'  program_application_id,'
5368       ||'  program_id,'
5369       ||'  program_update_date,'
5370       ||'  refresh_id'
5371       ||' FROM  msc_st_workday_patterns'
5372       ||' WHERE calendar_code    = :calendar_code'
5373       ||' AND   sr_instance_code = :sr_instance_code'
5374       ||' AND   process_flag      = '||G_VALID;
5375 
5376         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5377       EXECUTE IMMEDIATE lv_sql_stmt
5378               USING     rec1.calendar_code,
5379                         v_instance_code;
5380 
5381       v_sql_stmt  := 31; /*Bug 2172537*/
5382       lv_sql_stmt :=
5383       'DELETE FROM msc_calendar_exceptions '
5384       ||' WHERE calendar_code    = :calendar_code';
5385 
5386         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5387       EXECUTE IMMEDIATE lv_sql_stmt
5388               USING     rec1.calendar_code;
5389 
5390 
5391       v_sql_stmt  := 32; /*Bug 2172537*/
5392       lv_sql_stmt :=
5393       'INSERT INTO msc_calendar_exceptions'
5394       ||' (calendar_code,'
5395       ||'  exception_date,'
5396       ||'  exception_type,'
5397       ||'  sr_instance_id,'
5398       ||'  last_update_date,'
5399       ||'  last_updated_by,'
5400       ||'  creation_date,'
5401       ||'  created_by,'
5402       ||'  last_update_login,'
5403       ||'  request_id,'
5404       ||'  program_application_id,'
5405       ||'  program_id,'
5406       ||'  program_update_date,'
5407       ||'  refresh_id,'
5408       ||'  exception_set_id)'
5409       ||' SELECT '
5410       ||'  calendar_code,'
5411       ||'  exception_date,'
5412       ||'  exception_type,'
5413       ||'  sr_instance_id,'
5414       ||'  last_update_date,'
5415       ||'  last_updated_by,'
5416       ||'  creation_date,'
5417       ||'  created_by,'
5418       ||'  last_update_login,'
5419       ||'  request_id,'
5420       ||'  program_application_id,'
5421       ||'  program_id,'
5422       ||'  program_update_date,'
5423       ||'  refresh_id,'
5424       ||'  exception_set_id'
5425       ||' FROM  msc_st_calendar_exceptions'
5426       ||' WHERE calendar_code    = :calendar_code'
5427       ||' AND   sr_instance_code = :sr_instance_code'
5428       ||' AND   process_flag      = '||G_VALID ;
5429 
5430         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5431       EXECUTE IMMEDIATE lv_sql_stmt
5432               USING     rec1.calendar_code,
5433                         v_instance_code;
5434 
5435       v_sql_stmt  := 33;  /*Bug 2172537*/
5436       lv_sql_stmt :=
5437       'DELETE FROM msc_calendar_shifts '
5438       ||' WHERE calendar_code  = :calendar_code';
5439 
5440 
5441         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5442       EXECUTE IMMEDIATE lv_sql_stmt
5443               USING     rec1.calendar_code;
5444 
5445       v_sql_stmt  := 34;  /*Bug 2172537*/
5446       lv_sql_stmt :=
5447       'INSERT INTO msc_calendar_shifts '
5448       ||' (calendar_code,'
5449       ||'  sr_instance_id,'
5450       ||'  shift_name,'
5451       ||'  shift_num,'
5452       ||'  days_on,'
5453       ||'  days_off,'
5454       ||'  description,'
5455       ||'  refresh_number,'
5456       ||'  last_update_date,'
5457       ||'  last_updated_by,'
5458       ||'  last_update_login,'
5459       ||'  creation_date,'
5460       ||'  created_by,'
5461       ||'  request_id,'
5462       ||'  program_application_id,'
5463       ||'  program_id,'
5464       ||'  program_update_date)'
5465       ||' SELECT '
5466       ||'  calendar_code,'
5467       ||'  sr_instance_id,'
5468       ||'  shift_name,'
5469       ||'  shift_num, '
5470       ||'  days_on,'
5471       ||'  days_off,'
5472       ||'  description,'
5473       ||'  refresh_id,'
5474       ||'  last_update_date,'
5475       ||'  last_updated_by,'
5476       ||'  last_update_login,'
5477       ||'  creation_date,'
5478       ||'  created_by,'
5479       ||'  request_id,'
5480       ||'  program_application_id,'
5481       ||'  program_id,'
5482       ||'  program_update_date'
5483       ||' FROM  msc_st_calendar_shifts '
5484       ||' WHERE calendar_code    = :calendar_code'
5485       ||' AND   sr_instance_code = :sr_instance_code'
5486       ||' AND   process_flag      = '||G_VALID;
5487 
5488         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5489       EXECUTE IMMEDIATE lv_sql_stmt
5490               USING     rec1.calendar_code,
5491                         v_instance_code;
5492 
5493       v_sql_stmt  := 35;/*Bug 2172537*/
5494       lv_sql_stmt :=
5495       'DELETE FROM msc_shift_times '
5496       ||' WHERE calendar_code    = :calendar_code';
5497 
5498         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5499       EXECUTE IMMEDIATE lv_sql_stmt
5500               USING     rec1.calendar_code;
5501 
5502 
5503       v_sql_stmt  := 36; /*Bug 2172537*/
5504       lv_sql_stmt :=
5505       'INSERT INTO msc_shift_times '
5506       ||' (calendar_code,'
5507       ||'  sr_instance_id,'
5508       ||'  shift_num,'
5509       ||'  from_time,'
5510       ||'  to_time,'
5511       ||'  refresh_number,'
5512       ||'  last_update_date,'
5513       ||'  last_updated_by,'
5514       ||'  last_update_login,'
5515       ||'  creation_date,'
5516       ||'  created_by,'
5517       ||'  request_id,'
5518       ||'  program_application_id,'
5519       ||'  program_id,'
5520       ||'  program_update_date)'
5521       ||' SELECT '
5522       ||'  calendar_code,'
5523       ||'  sr_instance_id,'
5524       ||'  shift_num,'
5525       ||'  from_time,'
5526       ||'  to_time,'
5527       ||'  refresh_id,'
5528       ||'  last_update_date,'
5529       ||'  last_updated_by,'
5530       ||'  last_update_login,'
5531       ||'  creation_date,'
5532       ||'  created_by,'
5533       ||'  request_id,'
5534       ||'  program_application_id,'
5535       ||'  program_id,'
5536       ||'  program_update_date'
5537       ||' FROM  msc_st_shift_times '
5538       ||' WHERE calendar_code    = :calendar_code'
5539       ||' AND   sr_instance_code = :sr_instance_code'
5540       ||' AND   process_flag      = '||G_VALID;
5541 
5542         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5543       EXECUTE IMMEDIATE lv_sql_stmt
5544               USING     rec1.calendar_code,
5545                         v_instance_code;
5546 
5547       v_sql_stmt  := 37;/*Bug 2172537*/
5548       lv_sql_stmt :=
5549       'DELETE FROM msc_shift_exceptions '
5550       ||' WHERE calendar_code  = :calendar_code';
5551 
5552         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5553       EXECUTE IMMEDIATE lv_sql_stmt
5554               USING     rec1.calendar_code;
5555 
5556 
5557       v_sql_stmt  := 38; /*Bug 2172537*/
5558       lv_sql_stmt :=
5559       'INSERT INTO msc_shift_exceptions'
5560       ||' (calendar_code,'
5561       ||'  shift_num,'
5562       ||'  exception_date,'
5563       ||'  exception_type,'
5564       ||'  sr_instance_id,'
5565       ||'  last_update_date,'
5566       ||'  last_updated_by,'
5567       ||'  creation_date,'
5568       ||'  created_by,'
5569       ||'  last_update_login,'
5570       ||'  request_id,'
5571       ||'  program_application_id,'
5572       ||'  program_id,'
5573       ||'  program_update_date,'
5574       ||'  refresh_number,'
5575       ||'  exception_set_id)'
5576       ||' SELECT '
5577       ||'  calendar_code,'
5578       ||'  shift_num,'
5579       ||'  exception_date,'
5580       ||'  exception_type,'
5581       ||'  sr_instance_id,'
5582       ||'  last_update_date,'
5583       ||'  last_updated_by,'
5584       ||'  creation_date,'
5585       ||'  created_by,'
5586       ||'  last_update_login,'
5587       ||'  request_id,'
5588       ||'  program_application_id,'
5589       ||'  program_id,'
5590       ||'  program_update_date,'
5591       ||'  refresh_id,'
5592       ||'  exception_set_id'
5593       ||' FROM  msc_st_shift_exceptions'
5594       ||' WHERE calendar_code    = :calendar_code'
5595       ||' AND   sr_instance_code = :sr_instance_code'
5596       ||' AND   process_flag      = '||G_VALID;
5597 
5598         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5599       EXECUTE IMMEDIATE lv_sql_stmt
5600               USING     rec1.calendar_code,
5601                         v_instance_code;
5602 
5603   -- Delete those tables that is collected by collections
5604       v_sql_stmt  := 39;
5605 
5606      lv_sql_stmt :=
5607       ' DELETE FROM msc_st_calendar_shifts '
5608       ||' WHERE calendar_code    = :calendar_code'
5609       ||' AND   sr_instance_code = :sr_instance_code'
5610       ||' AND   process_flag      = '||G_VALID;
5611 
5612 
5613         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5614 
5615       EXECUTE IMMEDIATE lv_sql_stmt
5616               USING     rec1.calendar_code,
5617                         v_instance_code;
5618 
5619       v_sql_stmt  := 40;
5620 
5621      lv_sql_stmt :=
5622       ' DELETE FROM msc_st_shift_times '
5623       ||' WHERE calendar_code    = :calendar_code'
5624       ||' AND   sr_instance_code = :sr_instance_code'
5625       ||' AND   process_flag      = '||G_VALID;
5626 
5627 
5628         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5629 
5630       EXECUTE IMMEDIATE lv_sql_stmt
5631               USING     rec1.calendar_code,
5632                         v_instance_code;
5633 
5634       v_sql_stmt  := 41;
5635 
5636      lv_sql_stmt :=
5637       ' DELETE FROM msc_st_shift_exceptions '
5638       ||' WHERE calendar_code    = :calendar_code'
5639       ||' AND   sr_instance_code = :sr_instance_code'
5640       ||' AND   process_flag      = '||G_VALID;
5641 
5642 
5643         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5644 
5645       EXECUTE IMMEDIATE lv_sql_stmt
5646               USING     rec1.calendar_code,
5647                         v_instance_code;
5648 
5649 
5650 
5651       COMMIT;
5652       lv_request_id := fnd_request.submit_request('MSC','MSCPCL',
5653                        '','',TRUE,
5654                        rec1.calendar_code,
5655                        v_instance_id);
5656       v_cal_build := true;
5657       IF lv_request_id <> 0 THEN
5658         fnd_conc_global.set_req_globals(conc_status => 'PAUSED',
5659                                      request_data => to_char(G_CAL_REQ_DATA)) ;
5660         FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_WORKER_REQUEST_ID');
5661         FND_MESSAGE.SET_TOKEN('REQUEST_ID', lv_request_id);
5662         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
5663       END IF;
5664 
5665      EXCEPTION
5666        WHEN too_many_rows THEN
5667         lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_CALENDAR'||'('
5668                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
5669         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
5670         ROLLBACK;
5671 
5672       WHEN ex_logging_err THEN
5673         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
5674         ROLLBACK;
5675 
5676       WHEN ex_critical_err THEN
5677         lv_return := PROPAGATE_CAL_ERROR
5678                      (p_table_name      => lv_table_name,
5679                       p_transaction_id  => lv_transaction_id,
5680                       p_calendar_code   => lv_calendar_code,
5681                       p_message_text    => lv_message_text,
5682                       p_column_names    => lv_column_names,
5683                       p_error_text      => lv_error_text);
5684 
5685         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
5686         IF lv_return <> 0 THEN
5687           ROLLBACK;
5688         ELSE
5689           COMMIT;
5690        END IF;
5691 
5692       WHEN OTHERS THEN
5693         lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_CALENDAR'||'('
5694                          ||v_sql_stmt||')'|| SQLERRM, 1, 240);
5695         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
5696 
5697         ROLLBACK;
5698      END;
5699 
5700     END LOOP;
5701   EXCEPTION
5702     WHEN OTHERS THEN
5703       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_CALENDAR'||'('
5704                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
5705         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
5706 
5707       ROLLBACK;
5708 
5709   END LOAD_CALENDAR;
5710 /*==========================================================================+
5711 | DESCRIPTION  : This function performs the validation and loads the data   |
5712 |                for business object demand class                           |
5713 +==========================================================================*/
5714   PROCEDURE  LOAD_DEMAND_CLASS  IS
5715   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
5716   lb_rowid          RowidTab; --bulk collects rowid
5717   lv_return         NUMBER;
5718   lv_batch_id       NUMBER;
5719   lv_error_text     VARCHAR2(250);
5720   lv_where_str      VARCHAR2(5000);
5721   lv_sql_stmt       VARCHAR2(5000);
5722   lv_column_names   VARCHAR2(5000);     --stores concatenated column names
5723   lv_message_text   msc_errors.error_text%TYPE;
5724 
5725   ex_logging_err        EXCEPTION;
5726 
5727   CURSOR c1(p_batch_id NUMBER) IS
5728     SELECT rowid
5729     FROM   msc_st_demand_classes
5730     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
5731     AND    sr_instance_code          = v_instance_code
5732     AND    batch_id                  = p_batch_id;
5733 
5734   BEGIN
5735 
5736     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5737                  (p_app_short_name    => 'MSC',
5738                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
5739                   p_message_text      => lv_message_text,
5740                   p_error_text        => lv_error_text);
5741 
5742     IF lv_return <> 0 THEN
5743       RAISE ex_logging_err;
5744     END IF;
5745 
5746     --Validation check for the table MSC_ST_DEMAND_CLASSES
5747 
5748     --Duplicate records check for the records whose source is other than XML
5749     --Different SQL is used because in XML we can identify the latest records
5750     --whereas in batch load we cannot.
5751 
5752     v_sql_stmt := 01;
5753     lv_sql_stmt :=
5754     'UPDATE    msc_st_demand_classes msd1 '
5755     ||' SET    process_flag = '||G_ERROR_FLG||','
5756     ||'         error_text   = '||''''||lv_message_text||''''
5757     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_demand_classes msd2 '
5758     ||'        WHERE msd2.sr_instance_code  = msd1.sr_instance_code'
5759     ||'        AND   msd2.demand_class      = msd1.demand_class'
5760     ||'        AND   NVL(msd2.company_name,  '||''''||NULL_CHAR||''''||') = '
5761     ||'              NVL(msd1.company_name,  '||''''||NULL_CHAR||''''||')'
5762     ||'        AND   msd2.process_flag      = msd1.process_flag'
5763     ||'        AND   NVL(msd2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
5764     ||'        GROUP BY sr_instance_code,demand_class,company_name '
5765     ||'        HAVING COUNT(*) >1 )'
5766     ||' AND    msd1.process_flag              = '||G_IN_PROCESS
5767     ||' AND    msd1.sr_instance_code          = :v_instance_code'
5768     ||' AND    NVL(msd1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
5769 
5770 
5771       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5772 
5773 
5774     EXECUTE IMMEDIATE lv_sql_stmt
5775             USING     v_instance_code;
5776 
5777 
5778     --Duplicate records check for the records whose source is XML
5779     v_sql_stmt := 02;
5780     lv_sql_stmt :=
5781     'UPDATE   msc_st_demand_classes msd1 '
5782     ||' SET   process_flag = '||G_ERROR_FLG||','
5783     ||'       error_text   = '||''''||lv_message_text||''''
5784     ||' WHERE message_id   < ( SELECT max(message_id) '
5785     ||'       FROM  msc_st_demand_classes msd2'
5786     ||'       WHERE msd2.sr_instance_code  = msd1.sr_instance_code'
5787     ||'        AND   msd2.demand_class      = msd1.demand_class'
5788     ||'       AND   NVL(msd2.company_name,  '||''''||NULL_CHAR||''''||') = '
5789     ||'             NVL(msd1.company_name,  '||''''||NULL_CHAR||''''||')'
5790     ||'       AND   msd2.process_flag      = msd1.process_flag'
5791     ||'       AND   NVL(msd2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
5792     ||' AND   msd1.process_flag            = '||G_IN_PROCESS
5793     ||' AND   msd1.sr_instance_code        = :v_instance_code '
5794     ||' AND   NVL(msd1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
5795 
5796 
5797       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5798 
5799 
5800     EXECUTE IMMEDIATE lv_sql_stmt
5801             USING     v_instance_code;
5802 
5803 
5804     lv_column_names :=
5805     '   DEMAND_CLASS      ||''~''||'
5806     ||' MEANING           ||''~''||'
5807     ||' DESCRIPTION       ||''~''||'
5808     ||' FROM_DATE         ||''~''||'
5809     ||' TO_DATE           ||''~''||'
5810     ||' ENABLED_FLAG      ||''~''||'
5811     ||' SR_INSTANCE_CODE  ||''~''||'
5812     ||' COMPANY_NAME      ||''~''||'
5813     ||' DELETED_FLAG ';
5814 
5815 
5816     -- Dividing records in batches based on batchsize
5817     LOOP
5818       v_sql_stmt := 03;
5819       SELECT       msc_st_batch_id_s.NEXTVAL
5820       INTO         lv_batch_id
5821       FROM         DUAL;
5822 
5823       v_sql_stmt := 04;
5824       lv_sql_stmt :=
5825       'UPDATE   msc_st_demand_classes '
5826       ||' SET   batch_id                       = :lv_batch_id '
5827       ||' WHERE process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
5828       ||' AND   sr_instance_code               = :v_instance_code'
5829       ||' AND   NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
5830       ||' AND    rownum                        <= '||v_batch_size;
5831 
5832 
5833         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5834 
5835 
5836       EXECUTE IMMEDIATE lv_sql_stmt
5837               USING     lv_batch_id,
5838                         v_instance_code;
5839 
5840       EXIT WHEN SQL%NOTFOUND ;
5841 
5842       OPEN  c1(lv_batch_id);
5843       FETCH c1 BULK COLLECT INTO lb_rowid;
5844       CLOSE c1;
5845 
5846       v_sql_stmt := 05;
5847       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
5848       UPDATE msc_st_demand_classes
5849       SET  st_transaction_id = msc_st_demand_classes_s.NEXTVAL,
5850            refresh_id        = v_refresh_id,
5851            last_update_date  = v_current_date,
5852            last_updated_by   = v_current_user,
5853            creation_date     = v_current_date,
5854            created_by        = v_current_user
5855       WHERE  rowid           = lb_rowid(j);
5856 
5857 
5858       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5859                      (p_app_short_name    => 'MSC',
5860                       p_error_code        => 'MSC_PP_NO_DELETION',
5861                       p_message_text      => lv_message_text,
5862                       p_error_text        => lv_error_text,
5863                       p_token1            => 'TABLE_NAME',
5864                       p_token_value1      => 'MSC_ST_DEMAND_CLASSES');
5865 
5866       -- Deletion is not allowed for this table
5867       v_sql_stmt := 06;
5868       lv_sql_stmt :=
5869       'UPDATE      msc_st_demand_classes '
5870       ||' SET      process_flag     = '||G_ERROR_FLG||','
5871       ||'          error_text       = '||''''||lv_message_text||''''
5872       ||' WHERE    deleted_flag     = '||SYS_YES
5873       ||' AND      process_flag     = '||G_IN_PROCESS
5874       ||' AND      batch_id         = :lv_batch_id'
5875       ||' AND      sr_instance_code = :v_instance_code';
5876 
5877 
5878         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5879 
5880 
5881       EXECUTE IMMEDIATE lv_sql_stmt
5882               USING     lv_batch_id,
5883                         v_instance_code;
5884 
5885 
5886       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5887                      (p_app_short_name    => 'MSC',
5888                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
5889                       p_message_text      => lv_message_text,
5890                       p_error_text        => lv_error_text,
5891                       p_token1            => 'COLUMN_NAME',
5892                       p_token_value1      => 'DEMAND_CLASS OR MEANING');
5893 
5894       IF lv_return <> 0 THEN
5895         RAISE ex_logging_err;
5896       END IF;
5897 
5898       -- Error out records where demand_class or meaning  is NULL
5899 
5900       v_sql_stmt := 07;
5901       lv_sql_stmt :=
5902       'UPDATE    msc_st_demand_classes'
5903       ||' SET    process_flag            = '||G_ERROR_FLG||','
5904       ||'        error_text      = '||''''||lv_message_text||''''
5905       ||' WHERE  (NVL(demand_class, '||''''||NULL_CHAR||''''||') '
5906       ||'        =                 '||''''||NULL_CHAR||''''
5907       ||' OR    NVL(meaning, '||''''||NULL_CHAR||''''||') '
5908       ||'        =                 '||''''||NULL_CHAR||''''||') '
5909       ||' AND    batch_id                = :lv_batch_id'
5910       ||' AND    process_flag            = '||G_IN_PROCESS
5911       ||' AND    sr_instance_code        = :v_instance_code';
5912 
5913 
5914         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
5915 
5916       EXECUTE IMMEDIATE lv_sql_stmt
5917               USING     lv_batch_id,
5918                         v_instance_code;
5919 
5920 
5921       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5922                      (p_app_short_name    => 'MSC',
5923                       p_error_code        => 'MSC_PP_NO_UPDATION',
5924                       p_message_text      => lv_message_text,
5925                       p_error_text        => lv_error_text,
5926                       p_token1            => 'TABLE_NAME',
5927                       p_token_value1      => 'MSC_ST_DEMAND_CLASSES');
5928 
5929 
5930     -- Error out the record if same demand class already exists in ODS
5931     -- As we do not allow demand_class incremental update
5932     -- It has to be purged before laoded again
5933 
5934      -- bug# 3807591
5935       /* v_sql_stmt := 08;
5936       lv_sql_stmt :=
5937       'UPDATE      msc_st_demand_classes mstd'
5938       ||' SET      mstd.process_flag     = '||G_ERROR_FLG||','
5939       ||'          mstd.error_text       = '||''''||lv_message_text||''''
5940       ||' WHERE    mstd.batch_id         = :lv_batch_id'
5941       ||' AND      mstd.sr_instance_code = :v_instance_code'
5942       ||' AND      EXISTS ( SELECT 1'
5943       ||'          FROM     msc_demand_classes msd'
5944       ||'          WHERE    mstd.demand_class = msd.demand_class'
5945       ||'          AND      msd.sr_instance_id = :v_instance_id)';
5946       IF V_DEBUG THEN
5947         msc_st_util.log_message(lv_sql_stmt);
5948       END IF;
5949 
5950       EXECUTE IMMEDIATE lv_sql_stmt
5951               USING     lv_batch_id,
5952                         v_instance_code,
5953                         v_instance_id; */
5954 
5955 
5956       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5957                      (p_app_short_name    => 'MSC',
5958                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
5959                       p_message_text      => lv_message_text,
5960                       p_error_text        => lv_error_text,
5961                       p_token1            => 'COLUMN_NAME',
5962                       p_token_value1      => 'DELETED_FLAG',
5963                       p_token2            => 'DEFAULT_VALUE',
5964                       p_token_value2      => SYS_NO);
5965 
5966       IF lv_return <> 0 THEN
5967         RAISE ex_logging_err;
5968       END IF;
5969 
5970       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
5971       -- Log error procedure inserts warning and also defaults the delete flag
5972 
5973       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
5974                       ||' NOT IN(1,2)';
5975 
5976       lv_return := MSC_ST_UTIL.LOG_ERROR
5977                         (p_table_name        => 'MSC_ST_DEMAND_CLASSES',
5978                          p_instance_code     => v_instance_code,
5979                          p_row               => lv_column_names,
5980                          p_severity          => G_SEV_WARNING,
5981                          p_message_text      => lv_message_text,
5982                          p_error_text        => lv_error_text,
5983                          p_batch_id          => lv_batch_id,
5984                          p_where_str         => lv_where_str,
5985                          p_col_name          => 'DELETED_FLAG',
5986                          p_debug             => v_debug,
5987                          p_default_value     => SYS_NO);
5988 
5989       IF lv_return <> 0 THEN
5990         RAISE ex_logging_err;
5991       END IF;
5992 
5993       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
5994                      (p_app_short_name    => 'MSC',
5995                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
5996                       p_message_text      => lv_message_text,
5997                       p_error_text        => lv_error_text,
5998                       p_token1            => 'COLUMN_NAME',
5999                       p_token_value1      => 'ENABLED_FLAG',
6000                       p_token2            => 'DEFAULT_VALUE',
6001                       p_token_value2      => SYS_NO);
6002 
6003       IF lv_return <> 0 THEN
6004         RAISE ex_logging_err;
6005       END IF;
6006 
6007       -- Check ENABLED_FLAG column has valid value or not and if not set it to SYS_NO
6008       -- Log error procedure inserts warning and also defaults the delete flag
6009 
6010       lv_where_str := ' AND NVL(enabled_flag,'||NULL_VALUE||') '
6011                       ||' NOT IN(1,2)';
6012 
6013       lv_return := MSC_ST_UTIL.LOG_ERROR
6014                         (p_table_name        => 'MSC_ST_DEMAND_CLASSES',
6015                          p_instance_code     => v_instance_code,
6016                          p_row               => lv_column_names,
6017                          p_severity          => G_SEV_WARNING,
6018                          p_message_text      => lv_message_text,
6019                          p_error_text        => lv_error_text,
6020                          p_batch_id          => lv_batch_id,
6021                          p_where_str         => lv_where_str,
6022                          p_col_name          => 'ENABLED_FLAG',
6023                          p_debug             => v_debug,
6024                          p_default_value     => SYS_YES);
6025 
6026       IF lv_return <> 0 THEN
6027         RAISE ex_logging_err;
6028       END IF;
6029 
6030         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6031                        (p_app_short_name    => 'MSC',
6032                         p_error_code        => 'MSC_PP_INVALID_VALUE',
6033                         p_message_text      => lv_message_text,
6034                         p_error_text        => lv_error_text,
6035                         p_token1            => 'COLUMN_NAME',
6036                         p_token_value1      => 'FROM_DATE OR'
6037                                                ||' TO_DATE');
6038 
6039         IF lv_return <> 0 THEN
6040           RAISE ex_logging_err;
6041         END IF;
6042 
6043 
6044      -- Validate that from_date is greater than to_date
6045 
6046       v_sql_stmt := 08;
6047 
6048       lv_sql_stmt :=
6049       'UPDATE    msc_st_demand_classes'
6050       ||' SET    process_flag            = '||G_ERROR_FLG||','
6051       ||'        error_text      = '||''''||lv_message_text||''''
6052       ||' WHERE  NVL(from_date,SYSDATE-365000 ) '
6053       ||'  >    NVL(to_date, SYSDATE+365000) '
6054       ||' AND    batch_id                = :lv_batch_id'
6055       ||' AND    process_flag            = '||G_IN_PROCESS
6056       ||' AND    sr_instance_code        = :v_instance_code';
6057 
6058 
6059         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6060 
6061       EXECUTE IMMEDIATE lv_sql_stmt
6062               USING     lv_batch_id,
6063                         v_instance_code;
6064 
6065 
6066       --Call to customised validation.
6067       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
6068         (ERRBUF         => lv_error_text,
6069          RETCODE        => lv_return,
6070          pBatchID       => lv_batch_id,
6071          pInstanceCode  => v_instance_code,
6072          pEntityName    => 'MSC_ST_TRADING_PARTNERS',
6073          pInstanceID    => v_instance_id);
6074 
6075       IF NVL(lv_return,0) <> 0 THEN
6076         RAISE ex_logging_err;
6077       END IF;
6078 
6079 
6080       -- Set the process flag as Valid and populate instance_id
6081       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
6082                         (p_table_name   => 'MSC_ST_DEMAND_CLASSES',
6083                          p_instance_id    => v_instance_id,
6084                          p_instance_code  => v_instance_code,
6085                          p_process_flag   => G_VALID,
6086                          p_error_text     => lv_error_text,
6087                          p_debug          => v_debug,
6088                          p_batch_id       => lv_batch_id);
6089 
6090       IF lv_return <> 0 THEN
6091         RAISE ex_logging_err;
6092       END IF;
6093 
6094       -- At the end calling  the LOG_ERROR for logging all
6095       -- errored out records.
6096 
6097       lv_return := MSC_ST_UTIL.LOG_ERROR
6098                         (p_table_name    => 'MSC_ST_DEMAND_CLASSES',
6099                          p_instance_code => v_instance_code,
6100                          p_row           => lv_column_names,
6101                          p_severity      => G_SEV_ERROR,
6102                          p_message_text  => NULL,
6103                          p_error_text    => lv_error_text,
6104                          p_debug         => v_debug,
6105                          p_batch_id      => lv_batch_id  );
6106 
6107       IF lv_return <> 0 THEN
6108         RAISE ex_logging_err;
6109       END IF;
6110 
6111      v_sql_stmt := 09;
6112 
6113      -- bug# 3807591 (code is moved to MSCCLBAB.pls)
6114     /* lv_sql_stmt :=
6115        ' INSERT INTO MSC_DEMAND_CLASSES '
6116        ||' ( DEMAND_CLASS, '
6117        ||' MEANING, '
6118        ||' DESCRIPTION, '
6119        ||' FROM_DATE, '
6120        ||' TO_DATE, '
6121        ||' ENABLED_FLAG, '
6122        ||' SR_INSTANCE_ID, '
6123        ||' LAST_UPDATE_DATE, '
6124        ||' LAST_UPDATED_BY, '
6125        ||' CREATION_DATE, '
6126        ||' CREATED_BY) '
6127        ||' SELECT distinct '
6128        ||' mstd.demand_class,'
6129        ||' mstd.meaning,'
6130        ||' mstd.description,'
6131        ||' mstd.from_date,'
6132        ||' mstd.to_date,'
6133        ||' mstd.enabled_flag,'
6134        ||  v_instance_id ||','
6135        ||' mstd.last_update_date,'
6136        ||' mstd.last_updated_by,'
6137        ||' mstd.creation_date,'
6138        ||' mstd.created_by '
6139        ||' FROM   MSC_ST_DEMAND_CLASSES mstd'
6140        ||' WHERE  mstd.batch_id         = :lv_batch_id'
6141        ||' AND    mstd.sr_instance_code = :v_instance_code'
6142        ||' AND    mstd.process_flag    = '||G_VALID ;
6143 
6144       IF V_DEBUG THEN
6145         msc_st_util.log_message(lv_sql_stmt);
6146       END IF;
6147 
6148       EXECUTE IMMEDIATE lv_sql_stmt
6149               USING     lv_batch_id,
6150                         v_instance_code; */
6151 
6152      COMMIT;
6153 
6154       -- ODP change
6155       -- Call the procedure to insert categories into msd_st_level_values
6156       IF(v_install_msd) THEN
6157 
6158         lv_sql_stmt := ' DECLARE errbuf varchar2(250);'
6159                  ||' retcode  number ;'
6160                  || ' BEGIN '
6161                  ||'  MSD_CL_PRE_PROCESS.LOAD_DEMAND_CLASS'
6162                  ||'  (ERRBUF         =>  errbuf ,'
6163                  ||'  RETCODE        =>  retcode ,'
6164                  ||'  p_instance_code => :v_instance_code, '
6165                  ||'  p_instance_id => :v_instance_id,'
6166                  ||'  p_batch_id    => :lv_batch_id);'
6167                  ||'  IF ( retcode = '||G_WARNING||') THEN'
6168                  ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
6169                  ||'  END IF;'
6170                  ||'  END ;';
6171 
6172 
6173             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6174 
6175 
6176         EXECUTE IMMEDIATE lv_sql_stmt
6177                 USING     v_instance_code,
6178                           v_instance_id,
6179                           lv_batch_id;
6180 
6181       END IF ;
6182 
6183      COMMIT;
6184 
6185     END LOOP;
6186 
6187   EXCEPTION
6188 
6189     WHEN ex_logging_err THEN
6190         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
6191 
6192       ROLLBACK;
6193 
6194     WHEN OTHERS THEN
6195       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_DEMAND_CLASS '||'('
6196                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
6197         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
6198 
6199       ROLLBACK;
6200 
6201   END LOAD_DEMAND_CLASS ;
6202 
6203 
6204 /*==========================================================================+
6205 | DESCRIPTION  : This function performs the validation and loads the data   |
6206 |                for business object trading partners                       |
6207 +==========================================================================*/
6208   PROCEDURE  LOAD_TRADING_PARTNERS  IS
6209   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
6210   lb_rowid          RowidTab; --bulk collects rowid
6211   lv_return         NUMBER;
6212   lv_batch_id       NUMBER;
6213   lv_error_text     VARCHAR2(250);
6214   lv_where_str      VARCHAR2(5000);
6215   lv_sql_stmt       VARCHAR2(5000);
6216   lv_column_names   VARCHAR2(5000);     --stores concatenated column names
6217   lv_message_text   msc_errors.error_text%TYPE;
6218   lv_my_company     msc_companies.company_name%TYPE;
6219   lv_user_id        NUMBER;
6220 
6221   TYPE CharTblTyp IS TABLE OF VARCHAR2(240);
6222   TYPE NumTblTyp  IS TABLE OF NUMBER;
6223   TYPE DateTblTyp IS TABLE OF DATE;
6224   lb_GROUP_ID           NumTblTyp;
6225   lb_GROUP_NAME         CharTblTyp;
6226   lb_DESCRIPTION        CharTblTyp;
6227   lb_GROUP_TYPE         NumTblTyp;
6228   lb_GROUP_OWNER_ID     NumTblTyp;
6229   lb_CONTACT_USER_NAME  CharTblTyp;
6230   lb_CONTACT_USER_ID    NumTblTyp;
6231   lb_CONTRACT_DOC_URL   CharTblTyp;
6232   lb_EFFECTIVE_DATE     DateTblTyp;
6233   lb_DISABLE_DATE       DateTblTyp;
6234   lb_CONTEXT            CharTblTyp;
6235   lb_ATTRIBUTE1         CharTblTyp;
6236   lb_ATTRIBUTE2         CharTblTyp;
6237   lb_ATTRIBUTE3         CharTblTyp;
6238   lb_ATTRIBUTE4         CharTblTyp;
6239   lb_ATTRIBUTE5         CharTblTyp;
6240   lb_ATTRIBUTE6         CharTblTyp;
6241   lb_ATTRIBUTE7         CharTblTyp;
6242   lb_ATTRIBUTE8         CharTblTyp;
6243   lb_ATTRIBUTE9         CharTblTyp;
6244   lb_ATTRIBUTE10        CharTblTyp;
6245   lb_ATTRIBUTE11        CharTblTyp;
6246   lb_ATTRIBUTE12        CharTblTyp;
6247   lb_ATTRIBUTE13        CharTblTyp;
6248   lb_ATTRIBUTE14        CharTblTyp;
6249   lb_ATTRIBUTE15        CharTblTyp;
6250   lb_OWNER_CONTACT      CharTblTyp;
6251   lb_LAST_UPDATE_DATE   DateTblTyp;
6252   lb_LAST_UPDATED_BY    CharTblTyp;
6253   lb_LAST_UPDATE_LOGIN  CharTblTyp;
6254   lb_COMPANY_ID		NumTblTyp;
6255 
6256 
6257   ex_logging_err        EXCEPTION;
6258 
6259   CURSOR c1(p_batch_id NUMBER) IS
6260     SELECT rowid
6261     FROM   msc_st_trading_partners
6262     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
6263     AND    sr_instance_code          = v_instance_code
6264     AND    batch_id                  = p_batch_id;
6265 
6266   CURSOR c2(p_batch_id NUMBER) IS
6267     SELECT rowid
6268     FROM   msc_st_trading_partners
6269     WHERE  process_flag             = G_IN_PROCESS
6270     AND    sr_instance_code         = v_instance_code
6271     AND    batch_id                 = p_batch_id
6272     AND    NVL(sr_tp_id,NULL_VALUE) = NULL_VALUE
6273     AND    deleted_flag             = SYS_NO;
6274 
6275   CURSOR c3(p_batch_id NUMBER) IS
6276     SELECT rowid
6277     FROM   msc_st_trading_partner_sites
6278     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
6279     AND    sr_instance_code         = v_instance_code
6280     AND    batch_id                 = p_batch_id;
6281 
6282   CURSOR c4(p_batch_id NUMBER) IS
6283     SELECT rowid
6284     FROM   msc_st_trading_partner_sites
6285     WHERE  process_flag                  = G_IN_PROCESS
6286     AND    sr_instance_code              = v_instance_code
6287     AND    batch_id                      = p_batch_id
6288     AND    NVL(sr_tp_site_id,NULL_VALUE) = NULL_VALUE
6289     AND    partner_type  IN (1,2)
6290     AND    deleted_flag                  = SYS_NO;
6291 
6292   CURSOR c5(p_batch_id NUMBER) IS
6293     SELECT rowid
6294     FROM   msc_st_sub_inventories
6295     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
6296     AND    sr_instance_code  = v_instance_code
6297     AND    batch_id          = p_batch_id;
6298 
6299   CURSOR c6(p_batch_id NUMBER) IS
6300     SELECT rowid
6301     FROM   msc_st_location_associations
6302     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
6303     AND    sr_instance_code  = v_instance_code
6304     AND    batch_id          = p_batch_id;
6305 
6306   CURSOR c7(p_batch_id NUMBER) IS
6307     SELECT max(rowid)
6308     FROM   msc_st_trading_partner_sites
6309     WHERE  process_flag                = G_IN_PROCESS
6310     AND    deleted_flag                = SYS_NO
6311     AND    NVL(sr_tp_site_id,NULL_VALUE) = NULL_VALUE
6312     AND    partner_type                = 3
6313     AND    sr_instance_code            = v_instance_code
6314     AND    batch_id                    = p_batch_id
6315     GROUP BY location;
6316 
6317   CURSOR c8(p_batch_id NUMBER) IS
6318     SELECT rowid
6319     FROM   msc_st_partner_contacts
6320     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
6321     AND    sr_instance_code  = v_instance_code
6322     AND    batch_id          = p_batch_id;
6323 
6324    CURSOR c9(p_batch_id NUMBER) IS
6325     SELECT rowid
6326     FROM   MSC_ST_GROUPS
6327     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
6328     AND    batch_id         = p_batch_id
6329     AND    sr_instance_code = v_instance_code;
6330 
6331    CURSOR c10(p_batch_id NUMBER) IS
6332     SELECT rowid
6333     FROM   MSC_ST_GROUPS
6334     WHERE  process_flag     = G_IN_PROCESS
6335     AND    sr_instance_code = v_instance_code
6336     AND    batch_id         = p_batch_id
6337     AND    NVL(group_id,NULL_VALUE) = NULL_VALUE;
6338 
6339    CURSOR c11(p_batch_id NUMBER) IS
6340     SELECT
6341     	   msg.GROUP_ID,
6342 	   msg.GROUP_NAME,
6343     	   msg.DESCRIPTION,
6344            msg.GROUP_TYPE,
6345            msg.GROUP_OWNER_ID,
6346            msg.CONTACT_USER_ID,
6347            msg.CONTRACT_DOC_URL,
6348            msg.EFFECTIVE_DATE,
6349            msg.DISABLE_DATE,
6350            msg.LAST_UPDATE_DATE,
6351            msg.LAST_UPDATED_BY,
6352            msg.LAST_UPDATE_LOGIN,
6353            msg.CONTEXT,
6354            msg.ATTRIBUTE1,
6355            msg.ATTRIBUTE2,
6356            msg.ATTRIBUTE3,
6357            msg.ATTRIBUTE4,
6358            msg.ATTRIBUTE5,
6359            msg.ATTRIBUTE6,
6360            msg.ATTRIBUTE7,
6361            msg.ATTRIBUTE8,
6362            msg.ATTRIBUTE9,
6363            msg.ATTRIBUTE10,
6364            msg.ATTRIBUTE11,
6365            msg.ATTRIBUTE12,
6366            msg.ATTRIBUTE13,
6367            msg.ATTRIBUTE14,
6368            msg.ATTRIBUTE15
6369     FROM   msc_st_groups msg,msc_groups mg
6370     WHERE  msg.process_flag      = G_VALID
6371     AND    msg.sr_instance_code  = v_instance_code
6372     AND    msg.batch_id          = p_batch_id
6373     AND    msg.group_id          = mg.group_id;
6374 
6375     --------
6376     CURSOR c12(p_batch_id NUMBER) IS
6377     SELECT rowid
6378     FROM   msc_st_group_companies
6379     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
6380     AND    sr_instance_code          = v_instance_code
6381     AND    batch_id                  = p_batch_id;
6382 
6383    CURSOR c13(p_batch_id NUMBER) IS
6384     SELECT msg.group_id,
6385            msg.company_id,
6386            msg.CONTACT_USER_ID,
6387            msg.EFFECTIVE_DATE,
6388            msg.DISABLE_DATE,
6389            msg.CONTEXT,
6390            msg.ATTRIBUTE1,
6391            msg.ATTRIBUTE2,
6392            msg.ATTRIBUTE3,
6393            msg.ATTRIBUTE4,
6394            msg.ATTRIBUTE5,
6395            msg.ATTRIBUTE6,
6396            msg.ATTRIBUTE7,
6397            msg.ATTRIBUTE8,
6398            msg.ATTRIBUTE9,
6399            msg.ATTRIBUTE10,
6400            msg.ATTRIBUTE11,
6401            msg.ATTRIBUTE12,
6402            msg.ATTRIBUTE13,
6403            msg.ATTRIBUTE14,
6404            msg.ATTRIBUTE15
6405     FROM   msc_st_group_companies msg,msc_group_companies mg
6406     WHERE  msg.process_flag      = G_VALID
6407     AND    msg.sr_instance_code  = v_instance_code
6408     AND    msg.batch_id          = p_batch_id
6409     AND    msg.group_id          = mg.group_id
6410     AND    msg.company_id        = mg.company_id;
6411 
6412     ----
6413 
6414     CURSOR c14(p_batch_id NUMBER) IS
6415     SELECT max(rowid)
6416     FROM   msc_st_location_associations
6417     WHERE  process_flag                = G_IN_PROCESS
6418     AND    deleted_flag                = SYS_NO
6419     AND    NVL(location_id,NULL_VALUE) = NULL_VALUE
6420     AND    sr_instance_code            = v_instance_code
6421     AND    batch_id                    = p_batch_id
6422     GROUP BY location_code;
6423 
6424   BEGIN
6425 
6426     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6427                  (p_app_short_name    => 'MSC',
6428                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
6429                   p_message_text      => lv_message_text,
6430                   p_error_text        => lv_error_text);
6431 
6432     IF lv_return <> 0 THEN
6433       RAISE ex_logging_err;
6434     END IF;
6435 
6436     --Validation check for the table MSC_ST_TRADING_PARTNERS
6437 
6438     --Duplicate records check for the records whose source is other than XML
6439     --Different SQL is used because in XML we can identify the latest records
6440     --whereas in batch load we cannot.
6441 
6442     v_sql_stmt := 01;
6443     lv_sql_stmt :=
6444     'UPDATE    msc_st_trading_partners mstp1 '
6445     ||' SET    process_flag = '||G_ERROR_FLG||','
6446     ||'         error_text   = '||''''||lv_message_text||''''
6447     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_trading_partners mstp2 '
6448     ||'        WHERE mstp2.sr_instance_code  = mstp1.sr_instance_code'
6449     ||'        AND   NVL(mstp2.company_name,  '||''''||NULL_CHAR||''''||') = '
6450     ||'              NVL(mstp1.company_name,  '||''''||NULL_CHAR||''''||')'
6451     ||'        AND   mstp2.partner_name      = mstp1.partner_name'
6452     ||'        AND   mstp2.partner_type      = mstp1.partner_type'
6453     ||'        AND   mstp2.process_flag      = mstp1.process_flag'
6454     ||'        AND   NVL(mstp2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
6455     ||'        GROUP BY sr_instance_code,company_name,partner_name,partner_type '
6456     ||'        HAVING COUNT(*) >1 )'
6457     ||' AND    mstp1.process_flag              = '||G_IN_PROCESS
6458     -- ssurendr - bug 2647423: partner name should be unique for orgs as well.
6459     --||' AND    mstp1.partner_type              <>'||G_ORGANIZATION
6460     ||' AND    mstp1.sr_instance_code          = :v_instance_code'
6461     ||' AND    NVL(mstp1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
6462 
6463 
6464       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6465 
6466 
6467     EXECUTE IMMEDIATE lv_sql_stmt
6468             USING     v_instance_code;
6469 
6470 
6471     IF v_MSC_CONFIGURATION = 3 THEN            -- For SCE Standalone add company in udk
6472     v_sql_stmt := 02;
6473     lv_sql_stmt :=
6474     'UPDATE    msc_st_trading_partners mstp1 '
6475     ||' SET    process_flag = '||G_ERROR_FLG||','
6476     ||'        error_text   = '||''''||lv_message_text||''''
6477     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_trading_partners mstp2 '
6478     ||'        WHERE mstp2.sr_instance_code  = mstp1.sr_instance_code'
6479     ||'        AND   NVL(mstp2.company_name,  '||''''||NULL_CHAR||''''||') = '
6480     ||'              NVL(mstp1.company_name,  '||''''||NULL_CHAR||''''||')'
6481     ||'        AND   mstp2.organization_code      = mstp1.organization_code'
6482     ||'        AND   mstp2.partner_type      = mstp1.partner_type'
6483     ||'        AND   mstp2.process_flag      = mstp1.process_flag'
6484     ||'        AND   NVL(mstp2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
6485     ||'        GROUP BY sr_instance_code,company_name,organization_code,partner_type '
6486     -- ||'        GROUP BY sr_instance_code,organization_code,partner_type '
6487     ||'        HAVING COUNT(*) >1 )'
6488     ||' AND    mstp1.process_flag              = '||G_IN_PROCESS
6489     ||' AND    mstp1.partner_type              = '||G_ORGANIZATION
6490     ||' AND    mstp1.sr_instance_code          = :v_instance_code'
6491     ||' AND    NVL(mstp1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
6492 
6493     ELSE
6494      v_sql_stmt := 02;
6495     lv_sql_stmt :=
6496     'UPDATE    msc_st_trading_partners mstp1 '
6497     ||' SET    process_flag = '||G_ERROR_FLG||','
6498     ||'        error_text   = '||''''||lv_message_text||''''
6499     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_trading_partners mstp2 '
6500     ||'        WHERE mstp2.sr_instance_code  = mstp1.sr_instance_code'
6501     -- ||'        AND   NVL(mstp2.company_name,  '||''''||NULL_CHAR||''''||') = '
6502     -- ||'              NVL(mstp1.company_name,  '||''''||NULL_CHAR||''''||')'
6503     ||'        AND   mstp2.organization_code      = mstp1.organization_code'
6504     ||'        AND   mstp2.partner_type      = mstp1.partner_type'
6505     ||'        AND   mstp2.process_flag      = mstp1.process_flag'
6506     ||'        AND   NVL(mstp2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
6507     -- ||'        GROUP BY sr_instance_code,company_name,organization_code,partner_type '
6508     ||'        GROUP BY sr_instance_code,organization_code,partner_type '
6509     ||'        HAVING COUNT(*) >1 )'
6510     ||' AND    mstp1.process_flag              = '||G_IN_PROCESS
6511     ||' AND    mstp1.partner_type              = '||G_ORGANIZATION
6512     ||' AND    mstp1.sr_instance_code          = :v_instance_code'
6513     ||' AND    NVL(mstp1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
6514     END IF;
6515 
6516 
6517       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6518 
6519 
6520     EXECUTE IMMEDIATE lv_sql_stmt
6521             USING     v_instance_code;
6522 
6523 
6524 
6525     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6526                  (p_app_short_name    => 'MSC',
6527                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
6528                   p_message_text      => lv_message_text,
6529                   p_error_text        => lv_error_text);
6530 
6531     IF lv_return <> 0 THEN
6532       RAISE ex_logging_err;
6533     END IF;
6534 
6535     --Duplicate records check for the records whose source is XML
6536     v_sql_stmt := 03;
6537     lv_sql_stmt :=
6538     'UPDATE   msc_st_trading_partners mstp1 '
6539     ||' SET   process_flag = '||G_ERROR_FLG||','
6540     ||'         error_text   = '||''''||lv_message_text||''''
6541     ||' WHERE message_id   < ( SELECT max(message_id) '
6542     ||'       FROM  msc_st_trading_partners mstp2'
6543     ||'       WHERE mstp2.sr_instance_code  = mstp1.sr_instance_code'
6544     ||'       AND   NVL(mstp2.company_name,  '||''''||NULL_CHAR||''''||') = '
6545     ||'             NVL(mstp1.company_name,  '||''''||NULL_CHAR||''''||')'
6546     ||'       AND   mstp2.partner_name      = mstp1.partner_name'
6547     ||'       AND   mstp2.partner_type      = mstp1.partner_type'
6548     ||'       AND   mstp2.process_flag      = mstp1.process_flag'
6549     ||'       AND   mstp2.process_flag      = '||G_IN_PROCESS
6550     ||'       AND   NVL(mstp2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
6551     ||' AND   mstp1.process_flag            = '||G_IN_PROCESS
6552     -- ssurendr - bug 2647423: partner name should be unique for orgs as well.
6553     --||' AND   mstp1.partner_type            <> '||G_ORGANIZATION
6554     ||' AND   mstp1.sr_instance_code        = :v_instance_code '
6555     ||' AND   NVL(mstp1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
6556 
6557 
6558       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6559 
6560 
6561     EXECUTE IMMEDIATE lv_sql_stmt
6562             USING     v_instance_code;
6563 
6564     v_sql_stmt := 04;
6565     lv_sql_stmt :=
6566     'UPDATE   msc_st_trading_partners mstp1 '
6567     ||' SET   process_flag = '||G_ERROR_FLG||','
6568     ||'         error_text   = '||''''||lv_message_text||''''
6569     ||' WHERE message_id   < ( SELECT max(message_id) '
6570     ||'       FROM  msc_st_trading_partners mstp2'
6571     ||'       WHERE mstp2.sr_instance_code  = mstp1.sr_instance_code'
6572     -- ||'       AND   NVL(mstp2.company_name,  '||''''||NULL_CHAR||''''||') = '
6573     -- ||'             NVL(mstp1.company_name,  '||''''||NULL_CHAR||''''||')'
6574     ||'       AND   mstp2.organization_code      = mstp1.organization_code'
6575     ||'       AND   mstp2.partner_type      = mstp1.partner_type'
6576     ||'       AND   mstp2.process_flag      = mstp1.process_flag'
6577     ||'       AND   mstp2.process_flag      = '||G_IN_PROCESS
6578     ||'       AND   NVL(mstp2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
6579     ||' AND   mstp1.process_flag            = '||G_IN_PROCESS
6580     ||' AND   mstp1.partner_type            = '||G_ORGANIZATION
6581     ||' AND   mstp1.sr_instance_code        = :v_instance_code '
6582     ||' AND   NVL(mstp1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
6583 
6584 
6585       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6586 
6587 
6588     EXECUTE IMMEDIATE lv_sql_stmt
6589             USING     v_instance_code;
6590 
6591     lv_column_names :=
6592     'PARTNER_TYPE                  ||''~''||'
6593     ||'PARTNER_NAME                ||''~''||'
6594     ||'SR_INSTANCE_CODE            ||''~''||'
6595     ||'ORGANIZATION_CODE           ||''~''||'
6596     ||'DISABLE_DATE                ||''~''||'
6597     ||'STATUS                      ||''~''||'
6598     ||'MASTER_ORGANIZATION         ||''~''||'
6599     ||'PARTNER_NUMBER              ||''~''||'
6600     ||'CALENDAR_CODE               ||''~''||'
6601     ||'MAXIMUM_WEIGHT              ||''~''||'
6602     ||'MAXIMUM_VOLUME              ||''~''||'
6603     ||'WEIGHT_UOM                  ||''~''||'
6604     ||'VOLUME_UOM                  ||''~''||'
6605     ||'PROJECT_REFERENCE_ENABLED   ||''~''||'
6606     ||'PROJECT_CONTROL_LEVEL       ||''~''||'
6607     ||'DEMAND_LATENESS_COST        ||''~''||'
6608     ||'SUPPLIER_CAP_OVERUTIL_COST  ||''~''||'
6609     ||'RESOURCE_CAP_OVERUTIL_COST  ||''~''||'
6610     ||'TRANSPORT_CAP_OVER_UTIL_COST||''~''||'
6611     ||'USE_PHANTOM_ROUTINGS        ||''~''||'
6612     ||'INHERIT_PHANTOM_OP_SEQ      ||''~''||'
6613     ||'DEFAULT_ATP_RULE_NAME       ||''~''||'
6614     ||'DEFAULT_DEMAND_CLASS        ||''~''||'
6615     ||'MATERIAL_ACCOUNT            ||''~''||'
6616     ||'EXPENSE_ACCOUNT             ||''~''||'
6617     ||'ORGANIZATION_TYPE           ||''~''||'
6618     ||'SERVICE_LEVEL               ||''~''||'
6619     ||'COMPANY_NAME                ||''~''||'
6620     ||'CALENDAR_EXCEPTION_SET_ID   ||''~''||'
6621     ||'DELETED_FLAG';
6622 
6623     -- Dividing records in batches based on batchsize
6624     LOOP
6625       v_sql_stmt := 05;
6626       SELECT       msc_st_batch_id_s.NEXTVAL
6627       INTO         lv_batch_id
6628       FROM         DUAL;
6629 
6630       v_sql_stmt := 06;
6631       lv_sql_stmt :=
6632       'UPDATE   msc_st_trading_partners '
6633       ||' SET   batch_id                       = :lv_batch_id '
6634       ||' WHERE process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
6635       ||' AND   sr_instance_code               = :v_instance_code'
6636       ||' AND   NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
6637       ||' AND    rownum                        <= '||v_batch_size;
6638 
6639 
6640         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6641 
6642 
6643       EXECUTE IMMEDIATE lv_sql_stmt
6644               USING     lv_batch_id,
6645                         v_instance_code;
6646 
6647       EXIT WHEN SQL%NOTFOUND ;
6648 
6649       OPEN  c1(lv_batch_id);
6650       FETCH c1 BULK COLLECT INTO lb_rowid;
6651       CLOSE c1;
6652 
6653       v_sql_stmt := 07;
6654       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
6655       UPDATE msc_st_trading_partners
6656       SET  st_transaction_id = msc_st_trading_partners_s.NEXTVAL,
6657            refresh_id        = v_refresh_id,
6658            last_update_date  = v_current_date,
6659            last_updated_by   = v_current_user,
6660            creation_date     = v_current_date,
6661            created_by        = v_current_user
6662       WHERE  rowid           = lb_rowid(j);
6663 
6664       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6665                      (p_app_short_name    => 'MSC',
6666                       p_error_code        => 'MSC_PP_NO_DELETION',
6667                       p_message_text      => lv_message_text,
6668                       p_error_text        => lv_error_text,
6669                       p_token1            => 'TABLE_NAME',
6670                       p_token_value1      => 'MSC_ST_TRADING_PARTNERS');
6671 
6672       -- Deletion is not allowed for this table
6673       v_sql_stmt := 08;
6674       lv_sql_stmt :=
6675       'UPDATE      msc_st_trading_partners '
6676       ||' SET      process_flag     = '||G_ERROR_FLG||','
6677       ||'          error_text       = '||''''||lv_message_text||''''
6678       ||' WHERE    deleted_flag     = '||SYS_YES
6679       ||' AND      process_flag     = '||G_IN_PROCESS
6680       ||' AND      batch_id         = :lv_batch_id'
6681       ||' AND      sr_instance_code = :v_instance_code';
6682 
6683 
6684         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6685 
6686 
6687       EXECUTE IMMEDIATE lv_sql_stmt
6688               USING     lv_batch_id,
6689                         v_instance_code;
6690 
6691       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6692                      (p_app_short_name    => 'MSC',
6693                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
6694                       p_message_text      => lv_message_text,
6695                       p_error_text        => lv_error_text,
6696                       p_token1            => 'COLUMN_NAME',
6697                       p_token_value1      => 'DELETED_FLAG',
6698                       p_token2            => 'DEFAULT_VALUE',
6699                       p_token_value2      => SYS_NO);
6700 
6701       IF lv_return <> 0 THEN
6702         RAISE ex_logging_err;
6703       END IF;
6704 
6705       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
6706       -- Log error procedure inserts warning and also defaults the delete flag
6707 
6708       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
6709                       ||' NOT IN(1,2)';
6710 
6711       lv_return := MSC_ST_UTIL.LOG_ERROR
6712                         (p_table_name        => 'MSC_ST_TRADING_PARTNERS',
6713                          p_instance_code     => v_instance_code,
6714                          p_row               => lv_column_names,
6715                          p_severity          => G_SEV_WARNING,
6716                          p_message_text      => lv_message_text,
6717                          p_error_text        => lv_error_text,
6718                          p_batch_id          => lv_batch_id,
6719                          p_where_str         => lv_where_str,
6720                          p_col_name          => 'DELETED_FLAG',
6721                          p_debug             => v_debug,
6722                          p_default_value     => SYS_NO);
6723 
6724       IF lv_return <> 0 THEN
6725         RAISE ex_logging_err;
6726       END IF;
6727 
6728       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6729                      (p_app_short_name    => 'MSC',
6730                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
6731                       p_message_text      => lv_message_text,
6732                       p_error_text        => lv_error_text,
6733                       p_token1            => 'COLUMN_NAME',
6734                       p_token_value1      => 'PARTNER_NAME');
6735 
6736       IF lv_return <> 0 THEN
6737         RAISE ex_logging_err;
6738       END IF;
6739 
6740       -- Error out records where partner_name is NULL
6741       v_sql_stmt := 09;
6742       lv_sql_stmt :=
6743       'UPDATE    msc_st_trading_partners'
6744       ||' SET    process_flag            = '||G_ERROR_FLG||','
6745       ||'        error_text      = '||''''||lv_message_text||''''
6746       ||' WHERE  NVL(partner_name, '||''''||NULL_CHAR||''''||') '
6747       ||'        =                 '||''''||NULL_CHAR||''''
6748       ||' AND    batch_id                = :lv_batch_id'
6749       ||' AND    process_flag            = '||G_IN_PROCESS
6750       ||' AND    sr_instance_code        = :v_instance_code';
6751 
6752 
6753         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6754 
6755       EXECUTE IMMEDIATE lv_sql_stmt
6756               USING     lv_batch_id,
6757                         v_instance_code;
6758 
6759 
6760       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6761                      (p_app_short_name    => 'MSC',
6762                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
6763                       p_message_text      => lv_message_text,
6764                       p_error_text        => lv_error_text,
6765                       p_token1            => 'COLUMN_NAME',
6766                       p_token_value1      => 'PARTNER_TYPE');
6767 
6768       IF lv_return <> 0 THEN
6769         RAISE ex_logging_err;
6770       END IF;
6771 
6772       -- Error out records where partner_type NOT in (1,2,3,4)
6773       v_sql_stmt := 10;
6774       lv_sql_stmt :=
6775       'UPDATE    msc_st_trading_partners'
6776       ||' SET    process_flag       = '||G_ERROR_FLG||','
6777       ||'        error_text         = '||''''||lv_message_text||''''
6778       ||' WHERE  sr_instance_code   = :v_instance_code'
6779       ||' AND    batch_id           = :lv_batch_id'
6780       ||' AND    process_flag       = '||G_IN_PROCESS
6781       ||' AND    partner_type NOT IN (1,2,3,4)' ;
6782 
6783 
6784         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6785 
6786       EXECUTE IMMEDIATE lv_sql_stmt
6787               USING     v_instance_code,
6788                         lv_batch_id;
6789 
6790 
6791       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6792                      (p_app_short_name    => 'MSC',
6793                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
6794                       p_message_text      => lv_message_text,
6795                       p_error_text        => lv_error_text,
6796                       p_token1            => 'COLUMN_NAME',
6797                       p_token_value1      => 'ORGANIZATION_CODE');
6798 
6799       IF lv_return <> 0 THEN
6800         RAISE ex_logging_err;
6801       END IF;
6802 
6803       -- Error out records where organization_code is NULL for partner_type = 3
6804       v_sql_stmt := 11;
6805       lv_sql_stmt :=
6806       'UPDATE    msc_st_trading_partners '
6807       ||' SET    process_flag                  = '||G_ERROR_FLG||','
6808       ||'         error_text   = '||''''||lv_message_text||''''
6809       ||' WHERE  sr_instance_code              = :v_instance_code'
6810       ||' AND    batch_id                      = :lv_batch_id'
6811       ||' AND    process_flag                  = '||G_IN_PROCESS
6812       ||' AND    NVL(organization_code,    '||''''||NULL_CHAR||''''||')'
6813       ||'        =                      '||''''||NULL_CHAR||''''
6814       ||' AND    partner_type                  ='||G_ORGANIZATION;
6815 
6816 
6817         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6818 
6819       EXECUTE IMMEDIATE lv_sql_stmt
6820               USING     v_instance_code,
6821                         lv_batch_id;
6822 
6823 --- changes for SRP
6824 
6825       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6826                      (p_app_short_name    => 'MSC',
6827                       p_error_code        => 'MSC_PP_INVALID_VALUE',
6828                       p_message_text      => lv_message_text,
6829                       p_error_text        => lv_error_text,
6830                       p_token1            => 'COLUMN_NAME',
6831                       p_token_value1      => 'RESOURCE_TYPE');
6832 
6833       -- Resource type validation
6834       v_sql_stmt := 11.1;
6835       lv_sql_stmt :=
6836       'UPDATE      msc_st_trading_partners '
6837       ||' SET      process_flag     = '||G_ERROR_FLG||','
6838       ||'          error_text       = '||''''||lv_message_text||''''
6839       ||' WHERE    process_flag     = '||G_IN_PROCESS
6840       ||' AND      batch_id         = :lv_batch_id'
6841       ||' AND      sr_instance_code = :v_instance_code'
6842       ||' AND      partner_type     =  2'
6843       ||' AND      (resource_type IS NOT NULL AND resource_type <>1)';
6844 
6845 
6846         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6847 
6848 
6849       EXECUTE IMMEDIATE lv_sql_stmt
6850               USING     lv_batch_id,
6851                         v_instance_code;
6852 
6853       v_sql_stmt := 11.2;
6854       lv_sql_stmt :=
6855       'UPDATE      msc_st_trading_partners '
6856       	||' SET    RESOURCE_TYPE ='''''
6857       	||' WHERE  process_flag       = '||G_IN_PROCESS
6858       	||' AND    batch_id           = :lv_batch_id'
6859       	||' AND    sr_instance_code   = :v_instance_code'
6860         ||' AND    PARTNER_TYPE       in (1,3,4) '
6861         ||' AND (resource_type is not null and resource_type <>1)';
6862 
6863 
6864         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6865 
6866 
6867       EXECUTE IMMEDIATE lv_sql_stmt
6868               USING     lv_batch_id,
6869                         v_instance_code;
6870 
6871         -- bug# 2385805: dont allow dup orgs to go through
6872         -- unless these belong to this company - to allow updation
6873         -- Bypass this check if it is SCE Standalone
6874 
6875         IF v_MSC_CONFIGURATION <> 3 THEN
6876         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6877                       (p_app_short_name    => 'MSC',
6878                       p_error_code        => 'MSC_PP_INVALID_VALUE',
6879                       p_message_text      => lv_message_text,
6880                       p_error_text        => lv_error_text,
6881                       p_token1            => 'COLUMN_NAME',
6882                       p_token_value1      => 'ORGANIZATION_CODE');
6883 
6884         IF lv_return <> 0 THEN
6885           RAISE ex_logging_err;
6886         END IF;
6887 
6888         UPDATE msc_st_trading_partners mstp
6889         SET    process_flag = G_ERROR_FLG,
6890                error_text   = lv_message_text
6891         WHERE  mstp.process_flag      = G_IN_PROCESS
6892         AND    mstp.sr_instance_code  = v_instance_code
6893         AND    mstp.batch_id          = lv_batch_id
6894         AND    mstp.partner_type           = G_ORGANIZATION
6895         AND    EXISTS -- check against those orgs which do not belong to this company
6896                (SELECT mtp.organization_code from msc_trading_partners mtp
6897                 WHERE  mstp.organization_code = mtp.organization_code
6898                 AND    mtp.sr_instance_id = v_instance_id
6899                 AND    mtp.partner_type = G_ORGANIZATION
6900                 MINUS
6901                 SELECT mlis.char3 from msc_local_id_setup mlis
6902                 WHERE  mlis.entity_name = 'SR_TP_ID'
6903                 AND    mlis.char1       =  mstp.sr_instance_code
6904                 AND    mlis.number1     =  mstp.partner_type
6905                 AND    nvl(mlis.char2, NULL_CHAR)       = nvl(mstp.company_name,NULL_CHAR));
6906         END IF;
6907 
6908 
6909       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6910                      (p_app_short_name    => 'MSC',
6911                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
6912                       p_message_text      => lv_message_text,
6913                       p_error_text        => lv_error_text,
6914                       p_token1            => 'COLUMN_NAME',
6915                       p_token_value1      => 'CALENDAR_CODE');
6916 
6917       IF lv_return <> 0 THEN
6918         RAISE ex_logging_err;
6919       END IF;
6920 
6921       -- Error out records where calendar_code is NULL for partner_type = 3
6922       -- This validation is not required if MSC not installed(ODP standalone)
6923 
6924       IF (v_install_msc) THEN
6925       v_sql_stmt := 11;
6926       lv_sql_stmt :=
6927       'UPDATE    msc_st_trading_partners '
6928       ||' SET    process_flag                  = '||G_ERROR_FLG||','
6929       ||'         error_text   = '||''''||lv_message_text||''''
6930       ||' WHERE  sr_instance_code              = :v_instance_code'
6931       ||' AND    batch_id                      = :lv_batch_id'
6932       ||' AND    process_flag                  = '||G_IN_PROCESS
6933       ||' AND    NVL(calendar_code,    '||''''||NULL_CHAR||''''||')'
6934       ||'        =                      '||''''||NULL_CHAR||''''
6935       ||' AND    partner_type                  ='||G_ORGANIZATION;
6936 
6937 
6938         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6939 
6940       EXECUTE IMMEDIATE lv_sql_stmt
6941               USING     v_instance_code,
6942                         lv_batch_id;
6943 
6944 
6945      /* Bug 2172537 - Do not prefix the instance code
6946       -- Update  all partner_name and calendar_code as instance_code concatenated with name
6947       -- Where partner_type = 3 (organization)
6948       v_sql_stmt := 12;
6949       lv_sql_stmt :=
6950       'UPDATE   msc_st_trading_partners '
6951       ||' SET   partner_name      = sr_instance_code'||'||'':''||'||'partner_name,'
6952       ||'       calendar_code     = sr_instance_code'||'||'':''||'||'calendar_code'
6953       ||' WHERE partner_type      = '||G_ORGANIZATION
6954       ||' AND   sr_instance_code  = :v_instance_code'
6955       ||' AND   batch_id          = :lv_batch_id'
6956       ||' AND   process_flag      = '||G_IN_PROCESS;
6957 
6958       IF V_DEBUG THEN
6959         msc_st_util.log_message(lv_sql_stmt);
6960       END IF;
6961       EXECUTE IMMEDIATE lv_sql_stmt
6962               USING     v_instance_code,
6963                         lv_batch_id;               */
6964 
6965       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
6966                      (p_app_short_name    => 'MSC',
6967                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
6968                       p_message_text      => lv_message_text,
6969                       p_error_text        => lv_error_text,
6970                       p_token1            => 'COLUMN_NAMES',
6971                       p_token_value1      => 'SR_INSTANCE_ID AND CALENDAR_CODE',
6972                       p_token2            => 'MASTER_TABLE',
6973                       p_token_value2      => 'MSC_CALENDARS',
6974                       p_token3            => 'CHILD_TABLE',
6975                       p_token_value3      => 'MSC_ST_TRADING_PARTNERS');
6976       IF lv_return <> 0 THEN
6977         RAISE ex_logging_err;
6978       END IF;
6979 
6980       -- Validate the calendar code for org
6981       v_sql_stmt := 14;
6982       lv_sql_stmt :=
6983       'UPDATE    msc_st_trading_partners   mstp'
6984       ||' SET    process_flag             =  '||G_ERROR_FLG||','
6985       ||'         error_text   = '||''''||lv_message_text||''''
6986       ||' WHERE  NOT EXISTS ( SELECT 1 '
6987       ||'        FROM   msc_calendars mc '
6988       ||'        WHERE  mc.calendar_code  = mstp.calendar_code '
6989       ||'        AND    mc.sr_instance_id = :v_instance_id )'
6990       ||' AND    partner_type             = '||G_ORGANIZATION
6991       ||' AND    sr_instance_code         = :v_instance_code'
6992       ||' AND    batch_id                 = :lv_batch_id'
6993       ||' AND    process_flag             = '||G_IN_PROCESS;
6994 
6995 
6996 
6997         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
6998 
6999       EXECUTE IMMEDIATE lv_sql_stmt
7000               USING     v_instance_id,
7001                         v_instance_code,
7002                         lv_batch_id;
7003 
7004       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7005                      (p_app_short_name    => 'MSC',
7006                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
7007                       p_message_text      => lv_message_text,
7008                       p_error_text        => lv_error_text,
7009                       p_token1            => 'COLUMN_NAME',
7010                       p_token_value1      => 'CALENDAR_EXCEPTION_SET_ID');
7011 
7012       IF lv_return <> 0 THEN
7013         RAISE ex_logging_err;
7014       END IF;
7015 
7016       -- Default calendar exception set id as -1 always for legacy
7017       lv_where_str :=
7018       ' AND   calendar_exception_set_id <> -1 '
7019       ||' AND partner_type = '||G_ORGANIZATION ;
7020 
7021       lv_return := MSC_ST_UTIL.LOG_ERROR
7022                     (p_table_name        => 'MSC_ST_TRADING_PARTNERS',
7023                      p_instance_code     => v_instance_code,
7024                      p_row               => lv_column_names,
7025                      p_severity          => G_SEV_WARNING,
7026                      p_message_text      => lv_message_text,
7027                      p_error_text        => lv_error_text,
7028                      p_batch_id          => lv_batch_id,
7029                      p_where_str         => lv_where_str,
7030                      p_col_name          => 'CALENDAR_EXCEPTION_SET_ID',
7031                      p_debug             => v_debug,
7032                      p_default_value     => G_CAL_EXCEP_SET_ID);
7033 
7034       IF lv_return <> 0 THEN
7035         RAISE ex_logging_err;
7036       END IF;
7037 
7038 
7039       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7040                      (p_app_short_name    => 'MSC',
7041                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
7042                       p_message_text      => lv_message_text,
7043                       p_error_text        => lv_error_text,
7044                       p_token1            => 'COLUMN_NAMES',
7045                       p_token_value1      => 'SR_INSTANCE_CODE OR WEIGHT_UOM',
7046                       p_token2            => 'MASTER_TABLE',
7047                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
7048                       p_token3            => 'CHILD_TABLE',
7049                       p_token_value3      => 'MSC_ST_TRADING_PARTNERS');
7050       IF lv_return <> 0 THEN
7051         RAISE ex_logging_err;
7052       END IF;
7053 
7054       -- Validate all UOM code  both Weight/Volume
7055       v_sql_stmt := 15;
7056       lv_sql_stmt :=
7057       'UPDATE   msc_st_trading_partners mstp'
7058       ||' SET   process_flag = '||G_ERROR_FLG||','
7059       ||'         error_text   = '||''''||lv_message_text||''''
7060       ||' WHERE NOT EXISTS(SELECT 1 '
7061       ||'       FROM msc_units_of_measure muom '
7062       ||'       WHERE NVL(muom.uom_code,  '||''''||NULL_CHAR||''''||') = '
7063       ||'             NVL(mstp.weight_uom,'||''''||NULL_CHAR||''''||')'
7064       ||'       UNION '
7065       ||'       SELECT 1 FROM msc_st_units_of_measure msuom'
7066       ||'       WHERE NVL(msuom.uom_code, '||''''||NULL_CHAR||''''||') = '
7067       ||'             NVL(mstp.weight_uom,'||''''||NULL_CHAR||''''||')'
7068       ||'       AND   msuom.sr_instance_id = :v_instance_id'
7069       ||'       AND   msuom.process_flag   = '||G_VALID||' ) '
7070       ||' AND NVL(mstp.weight_uom,'||''''||NULL_CHAR||''''||') '
7071       ||'     <> '||''''||NULL_CHAR||''''
7072       ||' AND partner_type                 = '||G_ORGANIZATION
7073       ||' AND mstp.sr_instance_code        = :v_instance_code'
7074       ||' AND mstp.batch_id                = :lv_batch_id'
7075       ||' AND mstp.process_flag            = '||G_IN_PROCESS ;
7076 
7077 
7078 
7079         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7080 
7081 
7082       EXECUTE IMMEDIATE lv_sql_stmt
7083               USING     v_instance_id,
7084                         v_instance_code,
7085                         lv_batch_id;
7086 
7087       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7088                      (p_app_short_name    => 'MSC',
7089                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
7090                       p_message_text      => lv_message_text,
7091                       p_error_text        => lv_error_text,
7092                       p_token1            => 'COLUMN_NAMES',
7093                       p_token_value1      => 'SR_INSTANCE_CODE OR VOLUME_UOM',
7094                       p_token2            => 'MASTER_TABLE',
7095                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
7096                       p_token3            => 'CHILD_TABLE',
7097                       p_token_value3      => 'MSC_ST_TRADING_PARTNERS');
7098       IF lv_return <> 0 THEN
7099         RAISE ex_logging_err;
7100       END IF;
7101 
7102       v_sql_stmt := 16;
7103       lv_sql_stmt :=
7104       'UPDATE   msc_st_trading_partners mstp'
7105       ||' SET   process_flag                   = '||G_ERROR_FLG||','
7106       ||'         error_text   = '||''''||lv_message_text||''''
7107       ||' WHERE NOT EXISTS(SELECT 1 '
7108       ||'       FROM  msc_units_of_measure muom'
7109       ||'       WHERE NVL(muom.uom_code,  '||''''||NULL_CHAR||''''||') = '
7110       ||'             NVL(mstp.volume_uom,'||''''||NULL_CHAR||''''||')'
7111       ||'       UNION '
7112       ||'       SELECT 1 FROM msc_st_units_of_measure msuom'
7113       ||'       WHERE NVL(msuom.uom_code,' ||''''||NULL_CHAR||''''||') = '
7114       ||'             NVL(mstp.volume_uom,'||''''||NULL_CHAR||''''||')'
7115       ||'       AND   msuom.sr_instance_id     = :v_instance_id'
7116       ||'       AND   msuom.process_flag       = '||G_VALID||' )'
7117       ||' AND   NVL(mstp.volume_uom,'||''''||NULL_CHAR||''''||')'
7118       ||'       <> '||''''||NULL_CHAR||''''
7119       ||' AND   mstp.partner_type              ='||G_ORGANIZATION
7120       ||' AND   mstp.sr_instance_code          = :v_instance_code'
7121       ||' AND   mstp.batch_id                  = :lv_batch_id'
7122       ||' AND   mstp.process_flag              = '||G_IN_PROCESS ;
7123 
7124 
7125         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7126 
7127 
7128       EXECUTE IMMEDIATE lv_sql_stmt
7129               USING     v_instance_id,
7130                         v_instance_code,
7131                         lv_batch_id;
7132 
7133 
7134       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7135                      (p_app_short_name    => 'MSC',
7136                       p_error_code        => 'MSC_PP_INVALID_VALUE',
7137                       p_message_text      => lv_message_text,
7138                       p_error_text        => lv_error_text,
7139                       p_token1            => 'COLUMN_NAME',
7140                       p_token_value1      => 'COMPANY_NAME');
7141 
7142       IF lv_return <> 0 THEN
7143         RAISE ex_logging_err;
7144       END IF;
7145 
7146     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7147                      (p_app_short_name    => 'MSC',
7148                       p_error_code        => 'MSC_PP_INVALID_VALUE',
7149                       p_message_text      => lv_message_text,
7150                       p_error_text        => lv_error_text,
7151                       p_token1            => 'COLUMN_NAME',
7152                       p_token_value1      => 'DEFAULT_DEMAND_CLASS');
7153 
7154       IF lv_return <> 0 THEN
7155         RAISE ex_logging_err;
7156       END IF;
7157 
7158 
7159       -- Validate Demand Class, if value provided it should exists
7160       -- in ODS or staging table
7161 
7162      lv_return :=
7163      MSC_ST_UTIL.VALIDATE_DMD_CLASS(p_table_name       => 'MSC_ST_TRADING_PARTNERS',
7164                                     p_dmd_class_column => 'DEFAULT_DEMAND_CLASS',
7165                                     p_instance_id      => v_instance_id,
7166                                     p_instance_code    => v_instance_code,
7167                                     p_severity         => G_SEV3_ERROR,
7168                                     p_message_text     => lv_message_text,
7169                                     p_batch_id         => lv_batch_id,
7170                                     p_debug            => v_debug,
7171                                     p_error_text       => lv_error_text);
7172 
7173      if(lv_return <> 0) then
7174         RAISE ex_logging_err;
7175      end if;
7176 
7177 
7178     IF v_sce_installed THEN
7179      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7180                       (p_app_short_name    => 'MSC',
7181                       p_error_code        => 'MSC_PP_INVALID_VALUE',
7182                       p_message_text      => lv_message_text,
7183                       p_error_text        => lv_error_text,
7184                       p_token1            => 'COLUMN_NAME',
7185                       p_token_value1      => 'COMPANY_NAME');
7186 
7187      IF lv_return <> 0 THEN
7188           RAISE ex_logging_err;
7189      END IF;
7190 
7191       --Derive company_id
7192       lv_return := MSC_ST_UTIL.DERIVE_COMPANY_ID
7193                         (p_table_name     => 'MSC_ST_TRADING_PARTNERS',
7194                          p_company_name   => 'COMPANY_NAME',
7195                          p_company_id     => 'COMPANY_ID',
7196                          p_instance_code  => v_instance_code,
7197                          p_error_text     => lv_error_text,
7198                          p_batch_id       => lv_batch_id,
7199                          p_message_text   => lv_message_text,
7200                          p_debug          => v_debug);
7201 
7202 
7203       IF lv_return <> 0 THEN
7204         RAISE ex_logging_err;
7205       END IF;
7206 
7207     END IF;
7208 
7209 /*          Moved the code to MSCSUTLB.pls
7210 
7211     --Modified to fix the bug#2336975
7212 
7213     IF v_sce_installed THEN
7214       lv_my_company := GET_MY_COMPANY;
7215       -- if record is for OEM's partners, company name will be null or 'My Company'
7216 
7217       UPDATE msc_st_trading_partners mscu
7218       SET    company_id = -1
7219       WHERE  exists( SELECT 1
7220                      FROM   msc_companies mc
7221                      WHERE  mc.company_name                = nvl(mscu.company_name,lv_my_company)
7222                      AND    mc.company_id                  = 1
7223                      AND    NVL(mc.disable_date,sysdate+1) > sysdate)
7224       AND    mscu.process_flag      = G_IN_PROCESS
7225       AND    mscu.sr_instance_code  = v_instance_code
7226       AND    mscu.batch_id          = lv_batch_id;
7227 
7228       -- if not 'My Company' then get the first occurence of sr_tp_id for this company_name
7229 
7230       UPDATE msc_st_trading_partners mscu
7231       SET    company_id = (SELECT local_id from MSC_LOCAL_ID_SETUP
7232               WHERE char1           = mscu.sr_instance_code
7233               and   NVL(char3,NULL_CHAR) = NVL(mscu.company_name,NULL_CHAR)
7234               and   entity_name     = 'SR_TP_ID'
7235               and   rownum          = 1) -- we need the first occurence of sr_tp_id
7236       WHERE   mscu.company_id        IS NULL
7237       AND    mscu.process_flag      = G_IN_PROCESS
7238       AND    mscu.sr_instance_code  = v_instance_code
7239       AND    mscu.batch_id          = lv_batch_id;
7240 
7241         -- Set the  message
7242 
7243         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7244                       (p_app_short_name    => 'MSC',
7245                       p_error_code        => 'MSC_PP_INVALID_VALUE',
7246                       p_message_text      => lv_message_text,
7247                       p_error_text        => lv_error_text,
7248                       p_token1            => 'COLUMN_NAME',
7249                       p_token_value1      => 'COMPANY_NAME');
7250 
7251         IF lv_return <> 0 THEN
7252           RAISE ex_logging_err;
7253         END IF;
7254 
7255         UPDATE msc_st_trading_partners mscu
7256         SET    process_flag = G_ERROR_FLG,
7257                error_text   = lv_message_text
7258         WHERE  mscu.company_id        IS NULL
7259         AND    mscu.process_flag      = G_IN_PROCESS
7260         AND    mscu.sr_instance_code  = v_instance_code
7261         AND    mscu.batch_id          = lv_batch_id;
7262 
7263       END IF;
7264 */
7265 
7266       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7267                      (p_app_short_name    => 'MSC',
7268                       p_error_code        => 'MSC_PP_INVALID_VALUE',
7269                       p_message_text      => lv_message_text,
7270                       p_error_text        => lv_error_text,
7271                       p_token1            => 'COLUMN_NAME',
7272                       p_token_value1      => 'SOURCE_ORG_CODE');
7273 
7274       IF lv_return <> 0 THEN
7275         RAISE ex_logging_err;
7276       END IF;
7277 
7278       -- Derive source_org_id
7279       lv_return :=msc_st_util.derive_partner_org_id
7280                        (p_table_name        => 'MSC_ST_TRADING_PARTNERS',
7281                         p_org_partner_name  => 'SOURCE_ORG_CODE',
7282                         p_org_partner_id    => 'SOURCE_ORG_ID',
7283                         p_instance_code     => v_instance_code,
7284                         p_partner_type      => G_ORGANIZATION,
7285                         p_error_text        => lv_error_text,
7286                         p_batch_id          => lv_batch_id,
7287                         p_severity          => G_SEV3_ERROR,
7288                         p_debug             => v_debug,
7289                         p_message_text      => lv_message_text);
7290 
7291       IF lv_return <> 0 THEN
7292         RAISE ex_logging_err;
7293       END IF;
7294    END IF; --v_msc_installed
7295 
7296     -- ssurendr - bug 2647423: check for partner name - org code mismatch with respect to ODS - begin.
7297     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7298                  (p_app_short_name    => 'MSC',
7299                  p_error_code        => 'MSC_PP_INVALID_VALUE',
7300                  p_message_text      => lv_message_text,
7301                  p_error_text        => lv_error_text,
7302                  p_token1            => 'COLUMN_NAME',
7303                  p_token_value1      => 'PARTNER_NAME');
7304 
7305     IF lv_return <> 0 THEN
7306     RAISE ex_logging_err;
7307     END IF;
7308 
7309     v_sql_stmt := 16.1;
7310     lv_sql_stmt :=
7311     'UPDATE  msc_st_trading_partners mstp'
7312     ||' SET  process_flag                  = '||G_ERROR_FLG||','
7313     ||'      error_text  = '||''''||lv_message_text||''''
7314     ||' WHERE EXISTS(SELECT 1 '
7315     ||'      FROM  msc_trading_partners mtp,'
7316     ||'            msc_company_id_lid mcil'
7317     ||'      WHERE mtp.partner_name = mstp.partner_name'
7318     ||'      AND  mtp.partner_type = mstp.partner_type'
7319     ||'      AND  mtp.organization_code <> mstp.organization_code'
7320     ||'      AND  mcil.sr_company_id = mstp.company_id'
7321     ||'      AND  mcil.partner_type = mstp.partner_type'
7322     ||'      AND  mcil.company_id = mtp.company_id'
7323     ||'      AND  mcil.sr_instance_id = :v_instance_id)'
7324     ||' AND  mstp.partner_type              ='||G_ORGANIZATION
7325     ||' AND  nvl(mstp.company_id,-1)        <> -1'
7326     ||' AND  mstp.sr_instance_code          = :v_instance_code'
7327     ||' AND  mstp.batch_id                  = :lv_batch_id'
7328     ||' AND  mstp.process_flag              = '||G_IN_PROCESS ;
7329 
7330 
7331         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7332 
7333 
7334     EXECUTE IMMEDIATE lv_sql_stmt
7335         USING   v_instance_id,
7336                 v_instance_code,
7337                 lv_batch_id;
7338 
7339     v_sql_stmt := 16.2;
7340     lv_sql_stmt :=
7341     'UPDATE  msc_st_trading_partners mstp'
7342     ||' SET  process_flag                  = '||G_ERROR_FLG||','
7343     ||'      error_text  = '||''''||lv_message_text||''''
7344     ||' WHERE EXISTS(SELECT 1 '
7345     ||'      FROM  msc_trading_partners mtp'
7346     ||'      WHERE mtp.partner_name = mstp.partner_name'
7347     ||'      AND  mtp.partner_type = mstp.partner_type'
7348     ||'      AND  mtp.organization_code <> mstp.organization_code'
7349     ||'      AND  nvl(mtp.company_id,1) = 1)'
7350     ||' AND  mstp.partner_type              ='||G_ORGANIZATION
7351     ||' AND  nvl(mstp.company_id,-1)        = -1'
7352     ||' AND  mstp.sr_instance_code          = :v_instance_code'
7353     ||' AND  mstp.batch_id                  = :lv_batch_id'
7354     ||' AND  mstp.process_flag              = '||G_IN_PROCESS ;
7355 
7356 
7357     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7358 
7359 
7360     EXECUTE IMMEDIATE lv_sql_stmt
7361          USING    v_instance_code,
7362                    lv_batch_id;
7363     -- ssurendr - bug 2647423: check for partner name - org code mismatch with respect to ODS - end.
7364 
7365       -- Derive sr_tp_id(Organization)
7366       lv_return :=msc_st_util.derive_partner_org_id
7367                        (p_table_name        => 'MSC_ST_TRADING_PARTNERS',
7368                         p_org_partner_name  => 'ORGANIZATION_CODE',
7369                         p_org_partner_id    => 'SR_TP_ID',
7370                         p_instance_code     => v_instance_code,
7371                         p_partner_type      => G_ORGANIZATION,
7372                         p_error_text        => lv_error_text,
7373                         p_debug             => v_debug,
7374                         p_batch_id          => lv_batch_id);
7375 
7376       IF lv_return <> 0 THEN
7377         RAISE ex_logging_err;
7378       END IF;
7379 
7380       -- Derive sr_tp_id(Customer)
7381       lv_return :=msc_st_util.derive_partner_org_id
7382                        (p_table_name        => 'MSC_ST_TRADING_PARTNERS',
7383                         p_org_partner_name  => 'PARTNER_NAME',
7384                         p_org_partner_id    => 'SR_TP_ID',
7385                         p_instance_code     => v_instance_code,
7386                         p_partner_type      => G_CUSTOMER,
7387                         p_error_text        => lv_error_text,
7388                         p_debug             => v_debug,
7389                         p_batch_id          => lv_batch_id);
7390 
7391       IF lv_return <> 0 THEN
7392         RAISE ex_logging_err;
7393       END IF;
7394 
7395       -- Derive sr_tp_id(Supplier).
7396       lv_return :=msc_st_util.derive_partner_org_id
7397                        (p_table_name        => 'MSC_ST_TRADING_PARTNERS',
7398                         p_org_partner_name  => 'PARTNER_NAME',
7399                         p_org_partner_id    => 'SR_TP_ID',
7400                         p_instance_code     => v_instance_code,
7401                         p_partner_type      => G_VENDOR,
7402                         p_error_text        => lv_error_text,
7403                         p_debug             => v_debug,
7404                         p_batch_id          => lv_batch_id);
7405 
7406       IF lv_return <> 0 THEN
7407         RAISE ex_logging_err;
7408       END IF;
7409 
7410        -- Derive sr_tp_id(Carrier).
7411       lv_return :=msc_st_util.derive_partner_org_id
7412                        (p_table_name        => 'MSC_ST_TRADING_PARTNERS',
7413                         p_org_partner_name  => 'PARTNER_NAME',
7414                         p_org_partner_id    => 'SR_TP_ID',
7415                         p_instance_code     => v_instance_code,
7416                         p_partner_type      => G_CARRIER,
7417                         p_error_text        => lv_error_text,
7418                         p_debug             => v_debug,
7419                         p_batch_id          => lv_batch_id);
7420 
7421       IF lv_return <> 0 THEN
7422         RAISE ex_logging_err;
7423       END IF;
7424 
7425 
7426       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7427                      (p_app_short_name    => 'MSC',
7428                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
7429                       p_message_text      => lv_message_text,
7430                       p_error_text        => lv_error_text,
7431                       p_token1            => 'COLUMN_NAME',
7432                       p_token_value1      => 'INHERIT_OC_OP_SEQ_NUM',
7433                       p_token2            => 'DEFAULT_VALUE',
7434                       p_token_value2      => 2);
7435 
7436       IF lv_return <> 0 THEN
7437         RAISE ex_logging_err;
7438       END IF;
7439 
7440       -- Check INHERIT_OC_OP_SEQ_NUM column has valid value or not and if not set it to 2
7441       -- Log error procedure inserts warning and also defaults the INHERIT_OC_OP_SEQ_NUM
7442 
7443       lv_where_str := ' AND NVL(INHERIT_OC_OP_SEQ_NUM,'||NULL_VALUE||') '
7444                       ||' NOT IN(1,2)'
7445                       ||' AND partner_type = '||G_ORGANIZATION ;
7446 
7447       lv_return := MSC_ST_UTIL.LOG_ERROR
7448                         (p_table_name        => 'MSC_ST_TRADING_PARTNERS',
7449                          p_instance_code     => v_instance_code,
7450                          p_row               => lv_column_names,
7451                          p_severity          => G_SEV_WARNING,
7452                          p_message_text      => lv_message_text,
7453                          p_error_text        => lv_error_text,
7454                          p_batch_id          => lv_batch_id,
7455                          p_where_str         => lv_where_str,
7456                          p_col_name          => 'INHERIT_OC_OP_SEQ_NUM',
7457                          p_debug             => v_debug,
7458                          p_default_value     => 2);
7459 
7460       IF lv_return <> 0 THEN
7461         RAISE ex_logging_err;
7462       END IF;
7463 
7464       --Call to customised validation.
7465       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
7466         (ERRBUF         => lv_error_text,
7467          RETCODE        => lv_return,
7468          pBatchID       => lv_batch_id,
7469          pInstanceCode  => v_instance_code,
7470          pEntityName    => 'MSC_ST_TRADING_PARTNERS',
7471          pInstanceID    => v_instance_id);
7472 
7473       IF NVL(lv_return,0) <> 0 THEN
7474         RAISE ex_logging_err;
7475       END IF;
7476 
7477 
7478       OPEN  c2(lv_batch_id);
7479       FETCH c2 BULK COLLECT INTO lb_rowid ;
7480 
7481       IF c2%ROWCOUNT > 0  THEN
7482         v_sql_stmt := 17;
7483         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
7484         UPDATE msc_st_trading_partners
7485         SET    sr_tp_id  =  msc_st_sr_tp_id_s.NEXTVAL
7486         WHERE  rowid     =  lb_rowid(j);
7487 
7488         -- Insert into the LID table this new partner with the
7489         -- user defined key
7490 
7491         v_sql_stmt := 18;
7492         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
7493         INSERT INTO  msc_local_id_setup
7494           (local_id,
7495            st_transaction_id,
7496            instance_id,
7497            entity_name,
7498            data_source_type,
7499            char1,
7500            char2,
7501            char3,
7502            number1,
7503            SOURCE_SOURCE_ORG_ID,
7504            SOURCE_SR_TP_ID,
7505            last_update_date,
7506            last_updated_by,
7507            creation_date,
7508            created_by  )
7509         SELECT
7510            sr_tp_id,
7511            st_transaction_id,
7512            v_instance_id,
7513            'SR_TP_ID',
7514            data_source_type,
7515            v_instance_code,
7516            company_name,
7517            DECODE(partner_type,G_ORGANIZATION,organization_code,partner_name),
7518            partner_type,
7519            SOURCE_SOURCE_ORG_ID,
7520            SOURCE_SR_TP_ID,
7521            v_current_date,
7522            v_current_user,
7523            v_current_date,
7524            v_current_user
7525         FROM  msc_st_trading_partners
7526         WHERE rowid  = lb_rowid(j);
7527 
7528       END IF;
7529       CLOSE c2 ;
7530      /* Removed this prefixing of instance to org code bug 2172537
7531       -- Update  all organization_code as instance_code concatenated with org_code
7532       -- Where partner_type = 3 (organization)
7533       v_sql_stmt := 19;
7534       lv_sql_stmt :=
7535       'UPDATE   msc_st_trading_partners '
7536       ||' SET   organization_code   = sr_instance_code'||'||'':''||'||'organization_code,'
7537       ||'       master_organization = sr_tp_id'
7538       ||' WHERE partner_type        = '||G_ORGANIZATION
7539       ||' AND   sr_instance_code    = :v_instance_code'
7540       ||' AND   batch_id            = :lv_batch_id'
7541       ||' AND   process_flag        = '||G_IN_PROCESS;
7542 
7543       IF V_DEBUG THEN
7544         msc_st_util.log_message(lv_sql_stmt);
7545       END IF;
7546       EXECUTE IMMEDIATE lv_sql_stmt
7547               USING     v_instance_code,
7548                         lv_batch_id;        */
7549 
7550       -- Set the process flag as Valid and populate instance_id
7551       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
7552                         (p_table_name   => 'MSC_ST_TRADING_PARTNERS',
7553                          p_instance_id    => v_instance_id,
7554                          p_instance_code  => v_instance_code,
7555                          p_process_flag   => G_VALID,
7556                          p_error_text     => lv_error_text,
7557                          p_debug          => v_debug,
7558                          p_batch_id       => lv_batch_id);
7559 
7560       IF lv_return <> 0 THEN
7561         RAISE ex_logging_err;
7562       END IF;
7563 
7564       -- At the end calling  the LOG_ERROR for logging all
7565       -- errored out records.
7566 
7567       lv_return := MSC_ST_UTIL.LOG_ERROR
7568                         (p_table_name    => 'MSC_ST_TRADING_PARTNERS',
7569                          p_instance_code => v_instance_code,
7570                          p_row           => lv_column_names,
7571                          p_severity      => G_SEV_ERROR,
7572                          p_message_text  => NULL,
7573                          p_error_text    => lv_error_text,
7574                          p_debug         => v_debug,
7575                          p_batch_id      => lv_batch_id  );
7576 
7577       IF lv_return <> 0 THEN
7578         RAISE ex_logging_err;
7579       END IF;
7580     COMMIT;
7581 
7582       -- ODP change
7583       -- Call the procedure to insert all org/customer into the msd_st_level_values
7584       IF(v_install_msd) THEN
7585         lv_sql_stmt := 'DECLARE errbuf varchar2(250);'
7586                  ||' retcode number;'
7587                  ||' BEGIN '
7588                  ||' MSD_CL_PRE_PROCESS.LOAD_ORG_CUST'
7589                  ||' (ERRBUF        => errbuf,'
7590                  ||'  RETCODE       => retcode,'
7591                  ||'  p_instance_id => :v_instance_id,'
7592                  ||'  p_batch_id    => :lv_batch_id);'
7593                  ||'  IF ( retcode = '||G_WARNING||') THEN '
7594                  ||' update msc_apps_instances'
7595                  ||' set dbs_ver = '||SYS_YES
7596                  ||' where instance_id='||v_instance_id||';'
7597                  ||'  END IF;'
7598                  ||'  END ;';
7599 
7600         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7601 
7602         EXECUTE IMMEDIATE lv_sql_stmt
7603                 USING     v_instance_id,
7604                           lv_batch_id ;
7605       END IF ;
7606       COMMIT;
7607     END LOOP;
7608 
7609     --Validation for MSC_ST_TRADING_PARTNER_SITES
7610 
7611     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7612                  (p_app_short_name    => 'MSC',
7613                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
7614                   p_message_text      => lv_message_text,
7615                   p_error_text        => lv_error_text);
7616 
7617     IF lv_return <> 0 THEN
7618       RAISE ex_logging_err;
7619     END IF;
7620 /*
7621     -- UDK validations
7622     v_sql_stmt := 19;
7623     lv_sql_stmt :=
7624     '   UPDATE msc_st_trading_partner_sites mstp1'
7625     ||' SET    process_flag = '||G_ERROR_FLG||', '
7626     ||'         error_text   = '||''''||lv_message_text||''''
7627     ||' WHERE  EXISTS ( SELECT 1 '
7628     ||'        FROM  msc_st_trading_partner_sites mstp2 '
7629     ||'        WHERE mstp2.sr_instance_code  = mstp1.sr_instance_code '
7630     ||'        AND    NVL(mstp2.company_name,  '||''''||NULL_CHAR||''''||') = '
7631     ||'               NVL(mstp1.company_name,  '||''''||NULL_CHAR||''''||')'
7632     ||'        AND   mstp2.partner_name      = mstp1.partner_name  '
7633     ||'        AND   NVL(mstp2.tp_site_code,mstp2.location)   = NVL(mstp1.tp_site_code,mstp1.location) '
7634     ||'        AND   mstp2.partner_type      = mstp1.partner_type  '
7635     ||'        AND   mstp2.process_flag      = mstp1.process_flag  '
7636     ||'        AND   NVL(mstp2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
7637     ||'        GROUP BY sr_instance_code,partner_name,tp_site_code,'
7638     ||'                 partner_type '
7639     ||'        HAVING COUNT(*) >1 )'
7640     ||' AND    mstp1.process_flag            = '||G_IN_PROCESS
7641     ||' AND    mstp1.sr_instance_code        = :v_instance_code '
7642     ||' AND    NVL(mstp1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
7643 
7644     IF V_DEBUG THEN
7645       msc_st_util.log_message(lv_sql_stmt);
7646     END IF;
7647 
7648     EXECUTE IMMEDIATE lv_sql_stmt
7649             USING     v_instance_code;
7650 */
7651 
7652     -- UDK validations for locations
7653     v_sql_stmt := 19;
7654     lv_sql_stmt :=
7655     '   UPDATE msc_st_trading_partner_sites mstp1'
7656     ||' SET    process_flag = '||G_ERROR_FLG||', '
7657     ||'         error_text   = '||''''||lv_message_text||''''
7658     ||' WHERE  EXISTS ( SELECT 1 '
7659     ||'        FROM  msc_st_trading_partner_sites mstp2 '
7660     ||'        WHERE mstp2.sr_instance_code  = mstp1.sr_instance_code '
7661     ||'        AND    NVL(mstp2.company_name,  '||''''||NULL_CHAR||''''||') = '
7662     ||'               NVL(mstp1.company_name,  '||''''||NULL_CHAR||''''||')'
7663     ||'        AND   mstp2.partner_name      = mstp1.partner_name  '
7664     ||'        AND   mstp2.partner_type      = mstp1.partner_type  '
7665     ||'        AND   mstp2.process_flag      = mstp1.process_flag  '
7666     ||'        AND   NVL(mstp2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
7667     ||'        GROUP BY sr_instance_code,partner_name,company_name,'
7668     ||'                 partner_type '
7669     ||'        HAVING COUNT(*) >1 )'
7670     ||' AND    mstp1.process_flag            = '||G_IN_PROCESS
7671     ||' AND    mstp1.sr_instance_code        = :v_instance_code '
7672     ||' AND    mstp1.partner_type = 3'
7673     ||' AND    NVL(mstp1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
7674 
7675 
7676       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7677 
7678 
7679     EXECUTE IMMEDIATE lv_sql_stmt
7680             USING     v_instance_code;
7681 
7682 
7683     -- UDK validations for tp sites
7684     v_sql_stmt := 19;
7685     lv_sql_stmt :=
7686     '   UPDATE msc_st_trading_partner_sites mstp1'
7687     ||' SET    process_flag = '||G_ERROR_FLG||', '
7688     ||'         error_text   = '||''''||lv_message_text||''''
7689     ||' WHERE  EXISTS ( SELECT 1 '
7690     ||'        FROM  msc_st_trading_partner_sites mstp2 '
7691     ||'        WHERE mstp2.sr_instance_code  = mstp1.sr_instance_code '
7692     ||'        AND    NVL(mstp2.company_name,  '||''''||NULL_CHAR||''''||') = '
7693     ||'               NVL(mstp1.company_name,  '||''''||NULL_CHAR||''''||')'
7694     ||'        AND   mstp2.partner_name      = mstp1.partner_name  '
7695     ||'        AND   mstp2.tp_site_code      = mstp1.tp_site_code  '
7696     ||'        AND   mstp2.partner_type      = mstp1.partner_type  '
7697     ||'        AND   mstp2.process_flag      = mstp1.process_flag  '
7698     ||'        AND   NVL(mstp2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
7699     ||'        GROUP BY sr_instance_code,partner_name,tp_site_code,'
7700     ||'                 partner_type '
7701     ||'        HAVING COUNT(*) >1 )'
7702     ||' AND    mstp1.process_flag            = '||G_IN_PROCESS
7703     ||' AND    mstp1.sr_instance_code        = :v_instance_code '
7704     ||' AND    mstp1.partner_type            IN (1,2) '
7705     ||' AND    NVL(mstp1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
7706 
7707 
7708       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7709 
7710 
7711     EXECUTE IMMEDIATE lv_sql_stmt
7712             USING     v_instance_code;
7713     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7714                  (p_app_short_name    => 'MSC',
7715                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
7716                   p_message_text      => lv_message_text,
7717                   p_error_text        => lv_error_text);
7718 
7719     IF lv_return <> 0 THEN
7720       RAISE ex_logging_err;
7721     END IF;
7722 
7723     -- Duplicate UDK validation for XML message
7724     v_sql_stmt := 20;
7725     lv_sql_stmt :=
7726     '   UPDATE msc_st_trading_partner_sites mstp1'
7727     ||' SET    process_flag  = '||G_ERROR_FLG||', '
7728     ||'         error_text   = '||''''||lv_message_text||''''
7729     ||' WHERE  message_id    < ( SELECT max(message_id) '
7730     ||'        FROM  msc_st_trading_partner_sites mstp2 '
7731     ||'        WHERE mstp2.sr_instance_code  = mstp1.sr_instance_code '
7732     ||'        AND   NVL(mstp2.company_name,  '||''''||NULL_CHAR||''''||') = '
7733     ||'              NVL(mstp1.company_name,  '||''''||NULL_CHAR||''''||')'
7734     ||'        AND   mstp2.partner_name      = mstp1.partner_name '
7735     ||'        AND   mstp2.partner_type      = mstp1.partner_type '
7736     ||'        AND   mstp2.process_flag      = mstp1.process_flag '
7737     ||'        AND   mstp2.process_flag      = '||G_IN_PROCESS
7738     ||'        AND   NVL(mstp2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
7739     ||' AND    mstp1.process_flag            = '||G_IN_PROCESS
7740     ||' AND    mstp1.sr_instance_code        = :v_instance_code '
7741     ||' AND    mstp1.partner_type = 3'
7742     ||' AND    NVL(mstp1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
7743 
7744 
7745       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7746 
7747 
7748     EXECUTE IMMEDIATE lv_sql_stmt
7749             USING     v_instance_code;
7750 
7751     lv_column_names :=
7752     'PARTNER_NAME            ||''~''||'
7753     ||'TP_SITE_CODE          ||''~''||'
7754     ||'PARTNER_TYPE          ||''~''||'
7755     ||'PARTNER_ADDRESS       ||''~''||'
7756     ||'LOCATION              ||''~''||'
7757     ||'LONGITUDE             ||''~''||'
7758     ||'LATITUDE              ||''~''||'
7759     ||'SR_INSTANCE_CODE      ||''~''||'
7760     ||'COMPANY_NAME          ||''~''||'
7761     ||'DELETED_FLAG';
7762 
7763     -- Dividing records in batches based on batchsize
7764 
7765     LOOP
7766       v_sql_stmt := 21;
7767       SELECT msc_st_batch_id_s.NEXTVAL
7768       INTO   lv_batch_id
7769       FROM   DUAL;
7770 
7771       v_sql_stmt := 22;
7772       lv_sql_stmt :=
7773       'UPDATE    msc_st_trading_partner_sites '
7774       ||' SET    batch_id                       = :lv_batch_id'
7775       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
7776       ||' AND    sr_instance_code               = :v_instance_code'
7777       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
7778       ||' AND    rownum                        <= '||v_batch_size;
7779 
7780 
7781         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7782 
7783 
7784       EXECUTE IMMEDIATE lv_sql_stmt
7785               USING     lv_batch_id,
7786                         v_instance_code;
7787 
7788       EXIT WHEN SQL%NOTFOUND ;
7789 
7790       OPEN c3(lv_batch_id);
7791       FETCH c3 BULK COLLECT INTO lb_rowid;
7792       CLOSE c3;
7793 
7794       -- Populate the st_transaction_id and WHO columns
7795       v_sql_stmt := 23;
7796       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
7797       UPDATE   msc_st_trading_partner_sites
7798       SET      st_transaction_id =  msc_st_trading_partner_sites_s.NEXTVAL ,
7799                refresh_id        = v_refresh_id,
7800                last_update_date  = v_current_date,
7801                last_updated_by   = v_current_user,
7802                creation_date     = v_current_date,
7803                created_by        = v_current_user
7804       WHERE  rowid               = lb_rowid(j);
7805 
7806       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7807                      (p_app_short_name    => 'MSC',
7808                       p_error_code        => 'MSC_PP_NO_DELETION',
7809                       p_message_text      => lv_message_text,
7810                       p_error_text        => lv_error_text,
7811                       p_token1            => 'TABLE_NAME',
7812                       p_token_value1      => 'MSC_ST_TRADING_PARTNER_SITES');
7813 
7814       IF lv_return <> 0 THEN
7815         RAISE ex_logging_err;
7816       END IF;
7817 
7818       -- Deletion is not allowed for this table
7819       v_sql_stmt := 24;
7820       lv_sql_stmt :=
7821       'UPDATE      msc_st_trading_partner_sites '
7822       ||' SET      process_flag     = '||G_ERROR_FLG||','
7823       ||'         error_text   = '||''''||lv_message_text||''''
7824       ||' WHERE    deleted_flag     = '||SYS_YES
7825       ||' AND      process_flag     = '||G_IN_PROCESS
7826       ||' AND      batch_id         =  :lv_batch_id'
7827       ||' AND      sr_instance_code = :v_instance_code';
7828 
7829 
7830         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7831 
7832 
7833       EXECUTE IMMEDIATE lv_sql_stmt
7834               USING     lv_batch_id,
7835                         v_instance_code;
7836 
7837 
7838       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7839                      (p_app_short_name    => 'MSC',
7840                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
7841                       p_message_text      => lv_message_text,
7842                       p_error_text        => lv_error_text,
7843                       p_token1            => 'COLUMN_NAME',
7844                       p_token_value1      => 'DELETED_FLAG',
7845                       p_token2            => 'DEFAULT_VALUE',
7846                       p_token_value2      => SYS_NO);
7847 
7848       IF lv_return <> 0 THEN
7849         RAISE ex_logging_err;
7850       END IF;
7851 
7852       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
7853       -- Log error procedure inserts warning and also defaults the delete flag
7854 
7855       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
7856                       ||' NOT IN(1,2)';
7857 
7858       lv_return := MSC_ST_UTIL.LOG_ERROR
7859                        (p_table_name        => 'MSC_ST_TRADING_PARTNER_SITES',
7860                         p_instance_code     => v_instance_code,
7861                         p_row               => lv_column_names,
7862                         p_severity          => G_SEV_WARNING,
7863                         p_message_text      => lv_message_text,
7864                         p_error_text        => lv_error_text,
7865                         p_batch_id          => lv_batch_id,
7866                         p_where_str         => lv_where_str,
7867                         p_col_name          => 'DELETED_FLAG',
7868                         p_debug             => v_debug,
7869                         p_default_value     => SYS_NO);
7870 
7871 
7872       IF lv_return <> 0 THEN
7873         RAISE ex_logging_err;
7874       END IF;
7875 
7876       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7877                      (p_app_short_name    => 'MSC',
7878                       p_error_code        => 'MSC_PP_INVALID_VALUE',
7879                       p_message_text      => lv_message_text,
7880                       p_error_text        => lv_error_text,
7881                       p_token1            => 'COLUMN_NAME',
7882                       p_token_value1      => 'PARTNER_TYPE');
7883 
7884       IF lv_return <> 0 THEN
7885         RAISE ex_logging_err;
7886       END IF;
7887 
7888       -- Error out records where partner_type NOT in (1,2,3)
7889       v_sql_stmt := 25;
7890       lv_sql_stmt :=
7891       'UPDATE      msc_st_trading_partner_sites '
7892       ||' SET   process_flag        = '||G_ERROR_FLG||', '
7893       ||'         error_text   = '||''''||lv_message_text||''''
7894       ||' WHERE sr_instance_code    =  :v_instance_code'
7895       ||' AND   batch_id            =  :lv_batch_id'
7896       ||' AND   process_flag        = '||G_IN_PROCESS
7897       ||' AND   NVL(partner_type,'||NULL_VALUE||') NOT IN (1,2,3)';
7898 
7899 
7900         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7901 
7902 
7903       EXECUTE IMMEDIATE lv_sql_stmt
7904               USING     v_instance_code,
7905                         lv_batch_id;
7906 
7907     -- ODP changes , error out the record if only DP is installed
7908     -- then only partner_type = 2 (customer ) should come in
7909 
7910     IF (NOT v_install_msc) THEN
7911             lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7912                      (p_app_short_name    => 'MSC',
7913                       p_error_code        => 'MSC_PP_INVALID_VALUE',
7914                       p_message_text      => lv_message_text,
7915                       p_error_text        => lv_error_text,
7916                       p_token1            => 'COLUMN_NAME',
7917                       p_token_value1      => 'PARTNER_TYPE');
7918 
7919         IF lv_return <> 0 THEN
7920             RAISE ex_logging_err;
7921         END IF;
7922 
7923           -- Error out records where partner_type NOT in (2)
7924           v_sql_stmt := 25;
7925          lv_sql_stmt :=
7926          'UPDATE      msc_st_trading_partner_sites '
7927          ||' SET   process_flag        = '||G_ERROR_FLG||', '
7928          ||'         error_text   = '||''''||lv_message_text||''''
7929          ||' WHERE sr_instance_code    =  :v_instance_code'
7930          ||' AND   batch_id            =  :lv_batch_id'
7931          ||' AND   process_flag        = '||G_IN_PROCESS
7932         ||' AND   NVL(partner_type,'||NULL_VALUE||') <> 2';
7933 
7934 
7935            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7936 
7937 
7938       EXECUTE IMMEDIATE lv_sql_stmt
7939               USING     v_instance_code,
7940                         lv_batch_id;
7941       END IF ; --v_msc_install
7942 
7943 
7944 
7945       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7946                      (p_app_short_name    => 'MSC',
7947                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
7948                       p_message_text      => lv_message_text,
7949                       p_error_text        => lv_error_text,
7950                       p_token1            => 'COLUMN_NAME',
7951                       p_token_value1      => 'PARTNER_NAME OR TP_SITE_CODE');
7952 
7953       IF lv_return <> 0 THEN
7954         RAISE ex_logging_err;
7955       END IF;
7956 
7957       -- Error out records where partner_name/tp_site_code is NULL
7958       -- For partner type 1 and 2
7959       v_sql_stmt := 26;
7960       lv_sql_stmt :=
7961       'UPDATE    msc_st_trading_partner_sites '
7962       ||' SET    process_flag     = '||G_ERROR_FLG||','
7963       ||'        error_text   = '||''''||lv_message_text||''''
7964       ||' WHERE  sr_instance_code =  :v_instance_code'
7965       ||' AND    batch_id         =  :lv_batch_id'
7966       ||' AND    process_flag     = '||G_IN_PROCESS
7967       ||' AND    partner_type IN (1,2)'
7968       ||' AND    (NVL(partner_name,'||''''||NULL_CHAR||''''||')'
7969       ||'        = '||''''||NULL_CHAR||''''
7970       ||' OR     NVL(tp_site_code,'||''''||NULL_CHAR||''''||') '
7971       ||'        = '||''''||NULL_CHAR||''''||')';
7972 
7973 
7974         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
7975 
7976 
7977       EXECUTE IMMEDIATE lv_sql_stmt
7978               USING     v_instance_code,
7979                         lv_batch_id;
7980 
7981     IF (v_install_msc) THEN
7982         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
7983                      (p_app_short_name    => 'MSC',
7984                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
7985                       p_message_text      => lv_message_text,
7986                       p_error_text        => lv_error_text,
7987                       p_token1            => 'COLUMN_NAME',
7988                       p_token_value1      => 'PARTNER_NAME OR LOCATION');
7989 
7990       IF lv_return <> 0 THEN
7991         RAISE ex_logging_err;
7992       END IF;
7993 
7994       -- Error out records where partner_name/location is NULL
7995       -- For partner type 3
7996       v_sql_stmt := 27;
7997       lv_sql_stmt :=
7998       'UPDATE    msc_st_trading_partner_sites '
7999       ||' SET    process_flag     = '||G_ERROR_FLG||','
8000       ||'        error_text   = '||''''||lv_message_text||''''
8001       ||' WHERE  sr_instance_code =  :v_instance_code'
8002       ||' AND    batch_id         =  :lv_batch_id'
8003       ||' AND    process_flag     = '||G_IN_PROCESS
8004       ||' AND    partner_type  = 3'
8005       ||' AND    (NVL(partner_name,'||''''||NULL_CHAR||''''||')'
8006       ||'        = '||''''||NULL_CHAR||''''
8007       ||' OR     NVL(location,'||''''||NULL_CHAR||''''||') '
8008       ||'        = '||''''||NULL_CHAR||''''||')';
8009 
8010 
8011         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8012 
8013 
8014       EXECUTE IMMEDIATE lv_sql_stmt
8015               USING     v_instance_code,
8016                         lv_batch_id;
8017 
8018       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8019                      (p_app_short_name    => 'MSC',
8020                       p_error_code        => 'MSC_PP_INVALID_VALUE',
8021                       p_message_text      => lv_message_text,
8022                       p_error_text        => lv_error_text,
8023                       p_token1            => 'COLUMN_NAME',
8024                       p_token_value1      => 'PARTNER_NAME');
8025 
8026       IF lv_return <> 0 THEN
8027         RAISE ex_logging_err;
8028       END IF;
8029 
8030       -- Update sr_tp_id(customer)
8031      lv_return :=msc_st_util.derive_partner_org_id
8032                       (p_table_name        =>'MSC_ST_TRADING_PARTNER_SITES',
8033                        p_org_partner_name  =>'PARTNER_NAME',
8034                        p_org_partner_id    =>'SR_TP_ID',
8035                        p_instance_code     => v_instance_code,
8036                        p_partner_type      => G_CUSTOMER,
8037                        p_batch_id          =>lv_batch_id,
8038                        p_debug             => v_debug,
8039                        p_error_text        =>lv_error_text);
8040 
8041       IF lv_return <> 0 THEN
8042         RAISE ex_logging_err;
8043       END IF;
8044 
8045       -- Update  sr_tp_id(Organization)
8046       lv_return :=msc_st_util.derive_partner_org_id
8047                        (p_table_name        => 'MSC_ST_TRADING_PARTNER_SITES',
8048                         p_org_partner_name  => 'PARTNER_NAME',
8049                         p_org_partner_id    => 'SR_TP_ID',
8050                         p_instance_code     => v_instance_code,
8051                         p_partner_type      => G_ORGANIZATION,
8052                         p_error_text        => lv_error_text,
8053                         p_debug             => v_debug,
8054                         p_batch_id          => lv_batch_id);
8055 
8056       IF lv_return <> 0 THEN
8057         RAISE ex_logging_err;
8058       END IF;
8059     END IF ; --v_install_msc
8060 
8061       -- Update sr_tp_id(supplier)
8062      lv_return :=msc_st_util.derive_partner_org_id
8063                       (p_table_name        =>'MSC_ST_TRADING_PARTNER_SITES',
8064                        p_org_partner_name  =>'PARTNER_NAME',
8065                        p_org_partner_id    =>'SR_TP_ID',
8066                        p_instance_code     => v_instance_code,
8067                        p_partner_type      => G_VENDOR,
8068                        p_message_text      => lv_message_text,
8069                        p_batch_id          =>lv_batch_id,
8070                        p_debug             => v_debug,
8071                        p_error_text        =>lv_error_text);
8072 
8073       IF lv_return <> 0 THEN
8074         RAISE ex_logging_err;
8075       END IF;
8076 
8077 
8078    -- Error out the record if sr_tp_id is NULL
8079        v_sql_stmt := 28;
8080       lv_sql_stmt :=
8081       'UPDATE    msc_st_trading_partner_sites'
8082       ||' SET    process_flag            = '||G_ERROR_FLG||','
8083       ||'        error_text   =   '||''''||lv_message_text||''''
8084       ||' WHERE  NVL(sr_tp_id,'||NULL_VALUE||')= '||NULL_VALUE
8085       ||' AND    batch_id                = :lv_batch_id'
8086       ||' AND    process_flag            = '||G_IN_PROCESS
8087       ||' AND    sr_instance_code        = :v_instance_code';
8088 
8089 
8090         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8091 
8092       EXECUTE IMMEDIATE lv_sql_stmt
8093               USING     lv_batch_id,
8094                         v_instance_code;
8095 
8096       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8097                      (p_app_short_name    => 'MSC',
8098                       p_error_code        => 'MSC_PP_INVALID_VALUE',
8099                       p_message_text      => lv_message_text,
8100                       p_error_text        => lv_error_text,
8101                       p_token1            => 'COLUMN_NAME',
8102                       p_token_value1      => 'TP_SITE_CODE');
8103 
8104       IF lv_return <> 0 THEN
8105         RAISE ex_logging_err;
8106       END IF;
8107 
8108       -- Update sr_tp_site_id(Vendor)
8109       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
8110                        (p_table_name        => 'MSC_ST_TRADING_PARTNER_SITES',
8111                         p_partner_name      => 'PARTNER_NAME',
8112                         p_partner_site_code => 'TP_SITE_CODE',
8113                         p_partner_site_id   => 'SR_TP_SITE_ID',
8114                         p_instance_code     => v_instance_code,
8115                         p_partner_type      => G_VENDOR,
8116                         p_error_text        => lv_error_text,
8117                         p_batch_id          => lv_batch_id,
8118                         p_debug             => v_debug,
8119                         p_row               => lv_column_names);
8120 
8121       IF lv_return <> 0 THEN
8122         RAISE ex_logging_err;
8123       END IF;
8124 
8125     IF(v_install_msc) THEN
8126        -- Update sr_tp_site_id(Customer)
8127        lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
8128                        (p_table_name        => 'MSC_ST_TRADING_PARTNER_SITES',
8129                         p_partner_name      => 'PARTNER_NAME',
8130                         p_partner_site_code => 'TP_SITE_CODE',
8131                         p_partner_site_id   => 'SR_TP_SITE_ID',
8132                         p_instance_code     => v_instance_code,
8133                         p_partner_type      => G_CUSTOMER,
8134                         p_error_text        => lv_error_text,
8135                         p_batch_id          => lv_batch_id,
8136                         p_debug             => v_debug,
8137                         p_row               => lv_column_names);
8138 
8139       IF lv_return <> 0 THEN
8140         RAISE ex_logging_err;
8141       END IF;
8142 
8143       -- Update sr_tp_site_id(Organization)
8144       -- location_id goes into sr_tp_site_id
8145       v_sql_stmt := 28;
8146       lv_sql_stmt :=
8147       'UPDATE msc_st_trading_partner_sites mts'
8148       ||' SET sr_tp_site_id              = (SELECT local_id'
8149       ||'       FROM   msc_local_id_setup mls'
8150       ||'       WHERE  mls.char1       = mts.sr_instance_code'
8151       ||'       AND    mls.char3       = mts.location'
8152       ||'       AND    mls.entity_name = ''LOCATION_ID'' )'
8153       ||' WHERE  process_flag                   = '||G_IN_PROCESS
8154       ||' AND    partner_type                   = 3'
8155       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
8156       ||' AND    sr_instance_code               = :v_instance_code';
8157 
8158 
8159         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8160 
8161 
8162       EXECUTE IMMEDIATE lv_sql_stmt
8163               USING     lv_batch_id,
8164                         v_instance_code;
8165 
8166      /* not required anymore for bug fix 2748525. locations are not specific to orgs
8167 
8168      -- If we get a location code for the same org
8169      -- We will update the location_code in LID and won't generate a new location_id
8170      -- This will ensure that we will have just one location for the organization
8171 
8172       v_sql_stmt := 29;
8173       v_sql_stmt := 29;
8174       lv_sql_stmt :=
8175       'UPDATE msc_local_id_setup mls'
8176       ||' SET mls.char4    =  NVL( (select mts.location '
8177       ||'     FROM msc_st_trading_partner_sites mts'
8178       ||'     WHERE mls.char1        = mts.sr_instance_code'
8179       ||'     AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
8180       ||'            NVL(mts.company_name,'||''''||NULL_CHAR||''''||') '
8181       ||'     AND   mls.char3       = mts.partner_name'
8182       ||'     AND   mts.process_flag                   = '||G_IN_PROCESS
8183       ||'     AND   mts.partner_type                   = '||G_ORGANIZATION
8184       ||'     AND   NVL(mts.batch_id,'||NULL_VALUE||') = :p_batch_id'
8185       ||'     AND   mts.sr_instance_code               = :v_instance_code),mls.char4)'
8186       ||'     WHERE   mls.entity_name = ''LOCATION_ID'' '
8187       ||'     AND   mls.instance_id = :v_instance_id' ;
8188 
8189       IF V_DEBUG THEN
8190         msc_st_util.log_message(lv_sql_stmt);
8191       END IF;
8192 
8193       EXECUTE IMMEDIATE lv_sql_stmt
8194               USING     lv_batch_id,
8195                         v_instance_code,v_instance_id;
8196     */
8197 
8198     END IF ; -- v_install_msc
8199 
8200       --Call to customised validation.
8201       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
8202         (ERRBUF         => lv_error_text,
8203          RETCODE        => lv_return,
8204          pBatchID       => lv_batch_id,
8205          pInstanceCode  => v_instance_code,
8206          pEntityName    => 'MSC_ST_TRADING_PARTNER_SITES',
8207          pInstanceID    => v_instance_id);
8208 
8209       IF NVL(lv_return,0) <> 0 THEN
8210         RAISE ex_logging_err;
8211       END IF;
8212 
8213       OPEN  c4(lv_batch_id);
8214       FETCH c4 BULK COLLECT INTO lb_rowid ;
8215 
8216       IF c4%ROWCOUNT > 0  THEN
8217         v_sql_stmt := 29;
8218         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
8219         UPDATE msc_st_trading_partner_sites
8220         SET    sr_tp_site_id  = msc_st_sr_tp_site_id_s.NEXTVAL
8221         WHERE  rowid          = lb_rowid(j);
8222 
8223         -- Insert record in LID
8224         v_sql_stmt := 30;
8225         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
8226         INSERT INTO msc_local_id_setup
8227          (local_id,
8228           st_transaction_id,
8229           instance_id,
8230           entity_name,
8231           data_source_type,
8232           char1,
8233           char2,
8234           char3,
8235           char4,
8236           number1,
8237           SOURCE_SR_TP_ID,
8238           SOURCE_SR_TP_SITE_ID,
8239           last_update_date,
8240           last_updated_by,
8241           creation_date,
8242           created_by)
8243         SELECT
8244           sr_tp_site_id,
8245           st_transaction_id,
8246           v_instance_id,
8247           'SR_TP_SITE_ID',
8248           data_source_type,
8249           v_instance_code,
8250           company_name,
8251           partner_name,
8252           tp_site_code,
8253           partner_type,
8254           SOURCE_SR_TP_ID,
8255           SOURCE_SR_TP_SITE_ID,
8256           v_current_date,
8257           v_current_user,
8258           v_current_date,
8259           v_current_user
8260         FROM  msc_st_trading_partner_sites
8261         WHERE rowid            = lb_rowid(j);
8262       END IF;
8263       CLOSE C4;
8264 
8265     IF (v_install_msc) THEN   -- IF MSC installed then only do this
8266       OPEN  c7(lv_batch_id);
8267       FETCH c7 BULK COLLECT INTO lb_rowid ;
8268 
8269       IF c7%ROWCOUNT > 0  THEN
8270         v_sql_stmt := 31;
8271         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
8272         UPDATE msc_st_trading_partner_sites
8273         SET    sr_tp_site_id  = msc_st_location_id_s.NEXTVAL
8274         WHERE  rowid          = lb_rowid(j);
8275 
8276         -- Insert record in LID
8277         v_sql_stmt := 32;
8278         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
8279         INSERT INTO msc_local_id_setup
8280          (local_id,
8281           st_transaction_id,
8282           instance_id,
8283           entity_name,
8284           data_source_type,
8285           char1,
8286           char3,
8287           SOURCE_SR_TP_ID,
8288 	  SOURCE_SR_TP_SITE_ID,
8289           last_update_date,
8290           last_updated_by,
8291           creation_date,
8292           created_by)
8293         SELECT
8294           sr_tp_site_id,
8295           st_transaction_id,
8296           v_instance_id,
8297           'LOCATION_ID',
8298           data_source_type,
8299           v_instance_code,
8300           location,
8301           SOURCE_SR_TP_ID,
8302 	  SOURCE_SR_TP_SITE_ID,
8303           v_current_date,
8304           v_current_user,
8305           v_current_date,
8306           v_current_user
8307         FROM  msc_st_trading_partner_sites
8308         WHERE rowid            = lb_rowid(j);
8309       END IF;
8310       CLOSE C7;
8311 
8312       --Deriving location_id
8313       v_sql_stmt := 32.1;
8314       lv_sql_stmt :=
8315       'UPDATE msc_st_trading_partner_sites mts'
8316       ||' SET sr_tp_site_id              = (SELECT local_id'
8317       ||'       FROM   msc_local_id_setup mls'
8318       ||'       WHERE  mls.char1       = mts.sr_instance_code'
8319       ||'       AND    mls.char3       = mts.location'
8320       ||'       AND    mls.entity_name = ''LOCATION_ID'' )'
8321       ||' WHERE  process_flag                   = '||G_IN_PROCESS
8322       ||' AND    partner_type                   = 3'
8323       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
8324       ||' AND    sr_instance_code               = :v_instance_code';
8325 
8326 
8327         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8328 
8329 
8330       EXECUTE IMMEDIATE lv_sql_stmt
8331               USING     lv_batch_id,
8332                         v_instance_code;
8333 
8334     END IF ; -- v_install_msc
8335 
8336 
8337       -- fix for 2409667
8338       -- Populating TP_SITE_CODE into LOCATION for Customer sites because in case
8339       -- of Customer Sites SCE would pick LOCATION as the COMPANY_SITE_NAME for MSC_COMPANY_SITES.
8340 
8341       v_sql_stmt := 33;
8342       lv_sql_stmt :=
8343       'UPDATE msc_st_trading_partner_sites '
8344       ||' SET LOCATION                          =  TP_SITE_CODE '
8345       ||' WHERE  process_flag                   = '|| G_IN_PROCESS
8346       ||' AND    partner_type                   = '|| G_CUSTOMER
8347       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
8348       ||' AND    sr_instance_code               = :v_instance_code';
8349 
8350 
8351         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8352 
8353 
8354       EXECUTE IMMEDIATE lv_sql_stmt
8355               USING     lv_batch_id,
8356                         v_instance_code;
8357 
8358 
8359 
8360       -- Set the process flag as Valid and poulate instance_id
8361       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
8362                         (p_table_name     => 'MSC_ST_TRADING_PARTNER_SITES',
8363                          p_instance_id    => v_instance_id,
8364                          p_instance_code  => v_instance_code,
8365                          p_process_flag   => G_VALID,
8366                          p_error_text     => lv_error_text,
8367                          p_debug          => v_debug,
8368                          p_batch_id       => lv_batch_id);
8369 
8370       IF lv_return < 0 THEN
8371         RAISE ex_logging_err;
8372       END IF;
8373 
8374       -- At the end calling  the LOG_ERROR for logging all errored out records
8375 
8376       lv_return := MSC_ST_UTIL.LOG_ERROR
8377                         (p_table_name     => 'MSC_ST_TRADING_PARTNER_SITES',
8378                          p_instance_code  => v_instance_code,
8379                          p_row            => lv_column_names,
8380                          p_severity       => G_SEV_ERROR,
8381                          p_message_text   => NULL,
8382                          p_error_text     => lv_error_text,
8383                          p_debug          => v_debug,
8384                          p_batch_id       => lv_batch_id  );
8385 
8386       IF lv_return < 0 THEN
8387         RAISE ex_logging_err;
8388       END IF;
8389       COMMIT;
8390 
8391 
8392       -- ODP change
8393       -- Call the procedure to insert site into the msd_st_level_values
8394       IF(v_install_msd) THEN
8395         lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
8396                  ||' retcode  number ;'
8397                  ||' BEGIN '
8398                  ||' MSD_CL_PRE_PROCESS.LOAD_SITE'
8399                  ||' (ERRBUF         =>  errbuf ,'
8400                  ||'  RETCODE        =>  retcode ,'
8401                  ||'  p_instance_id => :v_instance_id,'
8402                  ||'  p_batch_id    => :lv_batch_id);'
8403                  ||'  IF ( retcode = '||G_WARNING||') THEN '
8404                  ||'  update msc_apps_instances'
8405                  ||'  set dbs_ver = '||SYS_YES
8406                  ||'  where instance_id='||v_instance_id||';'
8407                  ||'  END IF;'
8408                  ||'  END ;';
8409 
8410         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8411 
8412         EXECUTE IMMEDIATE lv_sql_stmt
8413                 USING     v_instance_id,
8414                           lv_batch_id ;
8415      END IF ; -- v_install_msd
8416       COMMIT;
8417    END LOOP;
8418 
8419     --Validation for MSC_ST_SUB_INVENTORIES
8420 
8421 
8422     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8423                  (p_app_short_name    => 'MSC',
8424                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
8425                   p_message_text      => lv_message_text,
8426                   p_error_text        => lv_error_text);
8427 
8428     IF lv_return <> 0 THEN
8429       RAISE ex_logging_err;
8430     END IF;
8431 
8432     -- UDK validations
8433     v_sql_stmt := 29;
8434     lv_sql_stmt :=
8435     'UPDATE   msc_st_sub_inventories mssi1 '
8436     ||' SET   process_flag  = '||G_ERROR_FLG||','
8437     ||'         error_text   = '||''''||lv_message_text||''''
8438     ||' WHERE EXISTS( SELECT 1 '
8439     ||'       FROM  msc_st_sub_inventories mssi2 '
8440     ||'       WHERE mssi2.sr_instance_code   = mssi1.sr_instance_code'
8441     ||'       AND   NVL(mssi2.company_name,  '||''''||NULL_CHAR||''''||') = '
8442     ||'             NVL(mssi1.company_name,  '||''''||NULL_CHAR||''''||')'
8443     ||'       AND   mssi2.organization_code  = mssi1.organization_code'
8444     ||'       AND   mssi2.sub_inventory_code = mssi1.sub_inventory_code'
8445     ||'       AND   NVL(mssi2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
8446     ||'       GROUP BY sr_instance_code,organization_code,sub_inventory_code'
8447     ||'       HAVING COUNT(*) >1 )'
8448     ||' AND   mssi1.process_flag           = '||G_IN_PROCESS
8449     ||' AND   mssi1.sr_instance_code       = :v_instance_code'
8450     ||' AND   NVL(mssi1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
8451 
8452 
8453       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8454 
8455     EXECUTE IMMEDIATE lv_sql_stmt
8456             USING     v_instance_code;
8457 
8458 
8459     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8460                  (p_app_short_name    => 'MSC',
8461                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
8462                   p_message_text      => lv_message_text,
8463                   p_error_text        => lv_error_text);
8464 
8465     IF lv_return <> 0 THEN
8466       RAISE ex_logging_err;
8467     END IF;
8468 
8469     -- Duplicate UDK validation for XML message
8470     v_sql_stmt := 28;
8471     lv_sql_stmt :=
8472     'UPDATE    msc_st_sub_inventories mssi1 '
8473     ||' SET    process_flag  = '||G_ERROR_FLG||','
8474     ||'         error_text   = '||''''||lv_message_text||''''
8475     ||' WHERE  message_id    < (SELECT MAX(message_id)'
8476     ||'        FROM  msc_st_sub_inventories mssi2'
8477     ||'        WHERE mssi2.sr_instance_code   = mssi1.sr_instance_code '
8478     ||'        AND   NVL(mssi2.company_name,  '||''''||NULL_CHAR||''''||') = '
8479     ||'              NVL(mssi1.company_name,  '||''''||NULL_CHAR||''''||')'
8480     ||'        AND   mssi2.organization_code  = mssi1.organization_code'
8481     ||'        AND   mssi2.sub_inventory_code = mssi1.sub_inventory_code'
8482     ||'        AND   mssi2.process_flag       = '||G_IN_PROCESS
8483     ||'        AND   NVL(mssi2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
8484     ||' AND    mssi1.process_flag      = '||G_IN_PROCESS
8485     ||' AND    mssi1.sr_instance_code  = :v_instance_code'
8486     ||' AND    NVL(mssi1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
8487 
8488 
8489       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8490 
8491     EXECUTE IMMEDIATE lv_sql_stmt
8492             USING     v_instance_code;
8493 
8494 
8495     lv_column_names :=
8496     'ORGANIZATION_CODE   ||''~''||'
8497     ||'SUB_INVENTORY_CODE||''~''||'
8498     ||'SR_INSTANCE_CODE  ||''~''||'
8499     ||'DESCRIPTION       ||''~''||'
8500     ||'DISABLE_DATE      ||''~''||'
8501     ||'NETTING_TYPE      ||''~''||'
8502     ||'DEMAND_CLASS      ||''~''||'
8503     ||'PROJECT_NUMBER    ||''~''||'
8504     ||'TASK_NUMBER       ||''~''||'
8505     ||'INVENTORY_ATP_CODE||''~''||'
8506     ||'COMPANY_NAME      ||''~''||'
8507     ||'DELETED_FLAG';
8508 
8509 
8510     -- Dividing records in batches based on batchsize
8511 
8512     LOOP
8513       v_sql_stmt := 29;
8514       SELECT msc_st_batch_id_s.NEXTVAL
8515       INTO   lv_batch_id
8516       FROM   DUAL;
8517 
8518       v_sql_stmt := 30;
8519       lv_sql_stmt :=
8520       'UPDATE msc_st_sub_inventories '
8521       ||' SET    batch_id                       = :lv_batch_id'
8522       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
8523       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
8524       ||' AND    sr_instance_code               = :v_instance_code'
8525       ||' AND    rownum                        <= '||v_batch_size;
8526 
8527 
8528         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8529 
8530       EXECUTE IMMEDIATE lv_sql_stmt
8531               USING     lv_batch_id,
8532                         v_instance_code;
8533 
8534       EXIT WHEN SQL%NOTFOUND;
8535 
8536       OPEN  c5(lv_batch_id);
8537       FETCH c5 BULK COLLECT INTO lb_rowid;
8538       CLOSE c5;
8539 
8540       v_sql_stmt := 31;
8541       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
8542         UPDATE msc_st_sub_inventories
8543         SET    st_transaction_id =  msc_st_sub_inventories_s.NEXTVAL ,
8544                refresh_id        = v_refresh_id,
8545                last_update_date  = v_current_date,
8546                last_updated_by   = v_current_user,
8547                creation_date     = v_current_date,
8548                created_by        = v_current_user
8549         WHERE  rowid             = lb_rowid(j);
8550 
8551 
8552       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8553                      (p_app_short_name    => 'MSC',
8554                       p_error_code        => 'MSC_PP_NO_DELETION',
8555                       p_message_text      => lv_message_text,
8556                       p_error_text        => lv_error_text,
8557                       p_token1            => 'TABLE_NAME',
8558                       p_token_value1      => 'MSC_ST_SUB_INVENTORIES');
8559 
8560       IF lv_return <> 0 THEN
8561         RAISE ex_logging_err;
8562       END IF;
8563 
8564       -- Deletion is not allowed for this table
8565       v_sql_stmt := 32;
8566       lv_sql_stmt :=
8567       'UPDATE       msc_st_sub_inventories '
8568       ||' SET      process_flag     = '||G_ERROR_FLG||','
8569       ||'         error_text   = '||''''||lv_message_text||''''
8570       ||' WHERE    deleted_flag     = '||SYS_YES
8571       ||' AND      process_flag     = '||G_IN_PROCESS
8572       ||' AND      batch_id         =  :lv_batch_id'
8573       ||' AND      sr_instance_code = :v_instance_code';
8574 
8575 
8576 
8577         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8578 
8579       EXECUTE IMMEDIATE lv_sql_stmt
8580               USING     lv_batch_id,
8581                         v_instance_code;
8582 
8583       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8584                      (p_app_short_name    => 'MSC',
8585                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
8586                       p_message_text      => lv_message_text,
8587                       p_error_text        => lv_error_text,
8588                       p_token1            => 'COLUMN_NAME',
8589                       p_token_value1      => 'DELETED_FLAG',
8590                       p_token2            => 'DEFAULT_VALUE',
8591                       p_token_value2      => SYS_NO);
8592 
8593       IF lv_return <> 0 THEN
8594         RAISE ex_logging_err;
8595       END IF;
8596 
8597       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
8598       -- Log error procedure inserts warning and also defaults the delete flag
8599 
8600       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
8601                       ||' NOT IN(1,2)';
8602 
8603       lv_return := MSC_ST_UTIL.LOG_ERROR
8604                         (p_table_name        => 'MSC_ST_SUB_INVENTORIES',
8605                          p_instance_code     => v_instance_code,
8606                          p_row               => lv_column_names,
8607                          p_severity          => G_SEV_WARNING,
8608                          p_message_text      => lv_message_text,
8609                          p_error_text        => lv_error_text,
8610                          p_batch_id          => lv_batch_id,
8611                          p_where_str         => lv_where_str,
8612                          p_col_name          => 'DELETED_FLAG',
8613                          p_debug             => v_debug,
8614                          p_default_value     => SYS_NO);
8615 
8616       IF lv_return <> 0 THEN
8617         RAISE ex_logging_err;
8618       END IF;
8619 
8620       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8621                      (p_app_short_name    => 'MSC',
8622                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
8623                       p_message_text      => lv_message_text,
8624                       p_error_text        => lv_error_text,
8625                       p_token1            => 'COLUMN_NAME',
8626                       p_token_value1      => 'SUB_INVENTORY_CODE');
8627 
8628       IF lv_return <> 0 THEN
8629         RAISE ex_logging_err;
8630       END IF;
8631 
8632       -- Error out records where subinventory_code is NULL
8633       v_sql_stmt := 33;
8634       lv_sql_stmt :=
8635       'UPDATE    msc_st_sub_inventories '
8636       ||' SET    process_flag      = '||G_ERROR_FLG||','
8637       ||'         error_text   = '||''''||lv_message_text||''''
8638       ||' WHERE  sr_instance_code  = :v_instance_code'
8639       ||' AND    batch_id          = :lv_batch_id'
8640       ||' AND    process_flag      = '||G_IN_PROCESS
8641       ||' AND    NVL(sub_inventory_code,'||''''||NULL_CHAR||''''||')'
8642       ||'        = '||''''||NULL_CHAR||'''';
8643 
8644 
8645 
8646         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8647       EXECUTE IMMEDIATE lv_sql_stmt
8648               USING     v_instance_code,
8649                         lv_batch_id;
8650 
8651       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8652                      (p_app_short_name    => 'MSC',
8653                       p_error_code        => 'MSC_PP_INVALID_VALUE',
8654                       p_message_text      => lv_message_text,
8655                       p_error_text        => lv_error_text,
8656                       p_token1            => 'COLUMN_NAME',
8657                       p_token_value1      => 'ORGANIZATION_CODE');
8658 
8659       IF lv_return <> 0 THEN
8660         RAISE ex_logging_err;
8661       END IF;
8662 
8663       -- Update organization_id
8664       lv_return := msc_st_util.derive_partner_org_id
8665                         (p_table_name        => 'MSC_ST_SUB_INVENTORIES',
8666                          p_org_partner_name  => 'ORGANIZATION_CODE',
8667                          p_org_partner_id    => 'ORGANIZATION_ID',
8668                          p_instance_code     => v_instance_code,
8669                          p_partner_type      => G_ORGANIZATION,
8670                          p_error_text        => lv_error_text,
8671                          p_batch_id          => lv_batch_id,
8672                          p_severity          => G_SEV_ERROR,
8673                          p_message_text      => lv_message_text,
8674                          p_debug             => v_debug,
8675                          p_row               => lv_column_names);
8676 
8677       if(lv_return <> 0) then
8678         RAISE ex_logging_err;
8679       end if;
8680 
8681       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8682                      (p_app_short_name    => 'MSC',
8683                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
8684                       p_message_text      => lv_message_text,
8685                       p_error_text        => lv_error_text,
8686                       p_token1            => 'COLUMN_NAME',
8687                       p_token_value1      => 'NETTING_TYPE',
8688                       p_token2            => 'DEFAULT_VALUE',
8689                       p_token_value2      => 1);
8690 
8691       IF lv_return <> 0 THEN
8692         RAISE ex_logging_err;
8693       END IF;
8694 
8695       -- Default netting_type  as 2 if NOT IN (1,2)
8696       lv_where_str := ' AND nvl(netting_type,'||NULL_VALUE||') NOT IN (1,2)';
8697       lv_return := MSC_ST_UTIL.LOG_ERROR
8698                         (p_table_name        => 'MSC_ST_SUB_INVENTORIES',
8699                          p_instance_code     => v_instance_code,
8700                          p_row               => lv_column_names,
8701                          p_severity          => G_SEV_WARNING,
8702                          p_message_text      => lv_message_text,
8703                          p_error_text        => lv_error_text,
8704                          p_batch_id          => lv_batch_id,
8705                          p_where_str         => lv_where_str,
8706                          p_col_name          => 'NETTING_TYPE',
8707                          p_debug             => v_debug,
8708                          p_default_value     => 1);
8709 
8710       IF lv_return <> 0 THEN
8711         RAISE ex_logging_err;
8712       END IF;
8713 
8714       --Call to customised validation.
8715       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
8716         (ERRBUF         => lv_error_text,
8717          RETCODE        => lv_return,
8718          pBatchID       => lv_batch_id,
8719          pInstanceCode  => v_instance_code,
8720          pEntityName    => 'MSC_ST_SUB_INVENTORIES',
8721          pInstanceID    => v_instance_id);
8722 
8723       IF NVL(lv_return,0) <> 0 THEN
8724         RAISE ex_logging_err;
8725       END IF;
8726 
8727       -- Set the processf flag as Valid and poulate instance_id
8728       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
8729                         (p_table_name     => 'MSC_ST_SUB_INVENTORIES',
8730                          p_instance_id    => v_instance_id,
8731                          p_instance_code  => v_instance_code,
8732                          p_process_flag   => G_VALID,
8733                          p_error_text     => lv_error_text,
8734                          p_debug          => v_debug,
8735                          p_batch_id       => lv_batch_id);
8736 
8737       IF lv_return < 0 THEN
8738         RAISE ex_logging_err;
8739       END IF;
8740 
8741       -- At the end calling  the LOG_ERROR for logging all errored out records
8742 
8743       lv_return := MSC_ST_UTIL.LOG_ERROR
8744                         (p_table_name    =>'MSC_ST_SUB_INVENTORIES',
8745                          p_instance_code =>v_instance_code,
8746                          p_row           =>lv_column_names,
8747                          p_severity      =>G_SEV_ERROR,
8748                          p_message_text  => NULL,
8749                          p_error_text    =>lv_error_text,
8750                          p_debug         => v_debug,
8751                          p_batch_id      => lv_batch_id  );
8752 
8753       IF(lv_return <> 0) then
8754         RAISE ex_logging_err;
8755       END IF;
8756       COMMIT;
8757     END LOOP;
8758 
8759 
8760     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8761                  (p_app_short_name    => 'MSC',
8762                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
8763                   p_message_text      => lv_message_text,
8764                   p_error_text        => lv_error_text);
8765 
8766     IF lv_return <> 0 THEN
8767       RAISE ex_logging_err;
8768     END IF;
8769 
8770     --Validation check for the table MSC_ST_LOCATION_ASSOCIATIONS
8771 
8772     --Duplicate records check for the records whose source is other than XML
8773     --Different SQL is used because in XML we can identify the latest records
8774     --whereas in batch load we cannot.
8775 
8776     v_sql_stmt := 34;
8777     lv_sql_stmt :=
8778     'UPDATE    msc_st_location_associations mla1 '
8779     ||' SET    process_flag = '||G_ERROR_FLG||','
8780     ||'         error_text   = '||''''||lv_message_text||''''
8781     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_location_associations mla2 '
8782     ||'        WHERE mla2.sr_instance_code  = mla1.sr_instance_code'
8783     ||'        AND   NVL(mla2.company_name,  '||''''||NULL_CHAR||''''||') = '
8784     ||'              NVL(mla1.company_name,  '||''''||NULL_CHAR||''''||')'
8785     ||'        AND   mla2.partner_name     = mla1.partner_name  '
8786     ||'        AND   mla2.partner_type     = mla1.partner_type  '
8787     ||'        AND   mla2.tp_site_code     = mla1.tp_site_code  '
8788     ||'        AND   mla2.location_code    = mla1.location_code'
8789     ||'        AND   mla2.organization_code = mla1.organization_code'
8790     ||'        AND   mla2.process_flag     = mla1.process_flag  '
8791     ||'        AND   NVL(mla2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
8792     ||'        GROUP BY sr_instance_code,company_name,partner_name,'
8793     ||'              tp_site_code,location_code,partner_type,organization_code '
8794     ||'        HAVING COUNT(*) >1 )'
8795     ||' AND    mla1.process_flag              = '||G_IN_PROCESS
8796     ||' AND    mla1.sr_instance_code          = :v_instance_code'
8797     ||' AND    NVL(mla1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
8798 
8799 
8800       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8801 
8802     EXECUTE IMMEDIATE lv_sql_stmt
8803             USING     v_instance_code;
8804 
8805 
8806     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8807                  (p_app_short_name    => 'MSC',
8808                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
8809                   p_message_text      => lv_message_text,
8810                   p_error_text        => lv_error_text);
8811 
8812     IF lv_return <> 0 THEN
8813       RAISE ex_logging_err;
8814     END IF;
8815 
8816     --Duplicate records check for the records whose source is XML
8817     v_sql_stmt := 35;
8818     lv_sql_stmt :=
8819     'UPDATE    msc_st_location_associations mla1 '
8820     ||' SET    process_flag = '||G_ERROR_FLG||','
8821     ||'        error_text   = '||''''||lv_message_text||''''
8822     ||' WHERE  message_id   < ( SELECT max(message_id) '
8823     ||'        FROM msc_st_location_associations mla2 '
8824     ||'        WHERE mla2.sr_instance_code  = mla1.sr_instance_code'
8825     ||'        AND   NVL(mla2.company_name,  '||''''||NULL_CHAR||''''||') = '
8826     ||'              NVL(mla1.company_name,  '||''''||NULL_CHAR||''''||')'
8827     ||'        AND   mla2.partner_name     = mla1.partner_name  '
8828     ||'        AND   mla2.partner_type     = mla1.partner_type  '
8829     ||'        AND   mla2.tp_site_code     = mla1.tp_site_code  '
8830     ||'        AND   mla2.location_code    = mla1.location_code'
8831     ||'        AND   mla2.organization_code = mla1.organization_code'
8832     ||'        AND   mla2.process_flag     = mla1.process_flag  '
8833     ||'        AND   NVL(mla2.message_id,'||NULL_VALUE||') <> '||NULL_VALUE||')'
8834     ||' AND   mla1.process_flag            = '||G_IN_PROCESS
8835     ||' AND   mla1.sr_instance_code        = :v_instance_code '
8836     ||' AND   NVL(mla1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
8837 
8838 
8839       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8840 
8841     EXECUTE IMMEDIATE lv_sql_stmt
8842             USING     v_instance_code;
8843 
8844     lv_column_names :=
8845     'LOCATION_CODE       ||''~''||'
8846     ||'PARTNER_NAME      ||''~''||'
8847     ||'TP_SITE_CODE      ||''~''||'
8848     ||'PARTNER_TYPE      ||''~''||'
8849     ||'SR_INSTANCE_CODE  ||''~''||'
8850     ||'ORGANIZATION_CODE ||''~''||'
8851     ||'DELETED_FLAG      ||''~''||'
8852     ||'COMPANY_NAME ';
8853 
8854 
8855     -- Dividing records in batches based on batchsize
8856     LOOP
8857       v_sql_stmt := 36;
8858       SELECT       msc_st_batch_id_s.NEXTVAL
8859       INTO         lv_batch_id
8860       FROM         DUAL;
8861 
8862       v_sql_stmt := 37;
8863       lv_sql_stmt :=
8864       'UPDATE   msc_st_location_associations '
8865       ||' SET   batch_id                       = :lv_batch_id '
8866       ||' WHERE process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
8867       ||' AND   sr_instance_code               = :v_instance_code'
8868       ||' AND   NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
8869       ||' AND    rownum                        <= '||v_batch_size;
8870 
8871 
8872         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8873 
8874       EXECUTE IMMEDIATE lv_sql_stmt
8875               USING     lv_batch_id,
8876                         v_instance_code;
8877 
8878       EXIT WHEN SQL%NOTFOUND ;
8879 
8880       OPEN  c6(lv_batch_id);
8881       FETCH c6 BULK COLLECT INTO lb_rowid;
8882       CLOSE c6;
8883 
8884       v_sql_stmt := 38;
8885       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
8886       UPDATE msc_st_location_associations
8887       SET  st_transaction_id = msc_st_location_associations_s.NEXTVAL,
8888            refresh_id        = v_refresh_id,
8889            last_update_date  = v_current_date,
8890            last_updated_by   = v_current_user,
8891            creation_date     = v_current_date,
8892            created_by        = v_current_user
8893       WHERE  rowid           = lb_rowid(j);
8894 
8895       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8896                      (p_app_short_name    => 'MSC',
8897                       p_error_code        => 'MSC_PP_NO_DELETION',
8898                       p_message_text      => lv_message_text,
8899                       p_error_text        => lv_error_text,
8900                       p_token1            => 'TABLE_NAME',
8901                       p_token_value1      => 'MSC_ST_LOCATION_ASSOCIATIONS');
8902 
8903       -- Deletion is not allowed for this table
8904       v_sql_stmt := 39;
8905       lv_sql_stmt :=
8906       'UPDATE      msc_st_location_associations '
8907       ||' SET      process_flag     = '||G_ERROR_FLG||','
8908       ||'          error_text       = '||''''||lv_message_text||''''
8909       ||' WHERE    deleted_flag     = '||SYS_YES
8910       ||' AND      process_flag     = '||G_IN_PROCESS
8911       ||' AND      batch_id         =  :lv_batch_id'
8912       ||' AND      sr_instance_code =  :v_instance_code';
8913 
8914 
8915         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8916 
8917       EXECUTE IMMEDIATE lv_sql_stmt
8918               USING     lv_batch_id,
8919                         v_instance_code;
8920 
8921 
8922       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8923                      (p_app_short_name    => 'MSC',
8924                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
8925                       p_message_text      => lv_message_text,
8926                       p_error_text        => lv_error_text,
8927                       p_token1            => 'COLUMN_NAME',
8928                       p_token_value1      => 'DELETED_FLAG',
8929                       p_token2            => 'DEFAULT_VALUE',
8930                       p_token_value2      => SYS_NO);
8931 
8932       IF lv_return <> 0 THEN
8933         RAISE ex_logging_err;
8934       END IF;
8935 
8936       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
8937       -- Log error procedure inserts warning and also defaults the delete flag
8938 
8939       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
8940                       ||' NOT IN(1,2)';
8941 
8942       lv_return := MSC_ST_UTIL.LOG_ERROR
8943                         (p_table_name        => 'MSC_ST_LOCATION_ASSOCIATIONS',
8944                          p_instance_code     => v_instance_code,
8945                          p_row               => lv_column_names,
8946                          p_severity          => G_SEV_WARNING,
8947                          p_message_text      => lv_message_text,
8948                          p_error_text        => lv_error_text,
8949                          p_batch_id          => lv_batch_id,
8950                          p_where_str         => lv_where_str,
8951                          p_col_name          => 'DELETED_FLAG',
8952                          p_debug             => v_debug,
8953                          p_default_value     => SYS_NO);
8954 
8955       IF lv_return <> 0 THEN
8956         RAISE ex_logging_err;
8957       END IF;
8958 
8959       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8960                      (p_app_short_name    => 'MSC',
8961                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
8962                       p_message_text      => lv_message_text,
8963                       p_error_text        => lv_error_text,
8964                       p_token1            => 'COLUMN_NAME',
8965                       p_token_value1      => 'PARTNER_NAME OR TP_SITE_CODE OR LOCATION_CODE');
8966 
8967       IF lv_return <> 0 THEN
8968         RAISE ex_logging_err;
8969       END IF;
8970 
8971       -- Error out records where partner_name/tp_site_code/location_code is NULL
8972       v_sql_stmt := 40;
8973       lv_sql_stmt :=
8974       'UPDATE    msc_st_location_associations'
8975       ||' SET    process_flag            = '||G_ERROR_FLG||','
8976       ||'         error_text   =   '||''''||lv_message_text||''''
8977       ||' WHERE  (NVL(partner_name,'||''''||NULL_CHAR||''''||') '
8978       ||'        =                 '||''''||NULL_CHAR||''''
8979       ||' OR     (partner_type <> 3 AND NVL(tp_site_code, '||''''||NULL_CHAR||''''||') '
8980       ||'        =                 '||''''||NULL_CHAR||''''||' ) '
8981       ||' OR     NVL(location_code, '||''''||NULL_CHAR||''''||') '
8982       ||'        =                 '||''''||NULL_CHAR||''''||' ) '
8983       ||' AND    batch_id                = :lv_batch_id'
8984       ||' AND    process_flag            = '||G_IN_PROCESS
8985       ||' AND    deleted_flag            = '||SYS_NO
8986       ||' AND    sr_instance_code        = :v_instance_code';
8987 
8988 
8989         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
8990       EXECUTE IMMEDIATE lv_sql_stmt
8991               USING     lv_batch_id,
8992                         v_instance_code;
8993 
8994 
8995       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
8996                      (p_app_short_name    => 'MSC',
8997                       p_error_code        => 'MSC_PP_INVALID_VALUE',
8998                       p_message_text      => lv_message_text,
8999                       p_error_text        => lv_error_text,
9000                       p_token1            => 'COLUMN_NAME',
9001                       p_token_value1      => 'PARTNER_TYPE');
9002 
9003       IF lv_return <> 0 THEN
9004         RAISE ex_logging_err;
9005       END IF;
9006 
9007       -- Error out records where partner_type NOT in (1,2,3)
9008       v_sql_stmt := 41;
9009       lv_sql_stmt :=
9010       'UPDATE    msc_st_location_associations'
9011       ||' SET    process_flag       = '||G_ERROR_FLG||','
9012       ||'        error_text   = '||''''||lv_message_text||''''
9013       ||' WHERE  sr_instance_code   = :v_instance_code'
9014       ||' AND    batch_id           = :lv_batch_id'
9015       ||' AND    process_flag       = '||G_IN_PROCESS
9016       ||' AND    partner_type NOT IN (1,2,3)' ;
9017 
9018 
9019         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9020       EXECUTE IMMEDIATE lv_sql_stmt
9021               USING     v_instance_code,
9022                         lv_batch_id;
9023 
9024       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9025                      (p_app_short_name    => 'MSC',
9026                       p_error_code        => 'MSC_PP_INVALID_VALUE',
9027                       p_message_text      => lv_message_text,
9028                       p_error_text        => lv_error_text,
9029                       p_token1            => 'COLUMN_NAME',
9030                       p_token_value1      => 'ORGANIZATION_CODE');
9031 
9032       IF lv_return <> 0 THEN
9033         RAISE ex_logging_err;
9034       END IF;
9035 
9036       -- Update organization_id
9037      lv_return :=msc_st_util.derive_partner_org_id
9038                       (p_table_name        =>'MSC_ST_LOCATION_ASSOCIATIONS',
9039                        p_org_partner_name  =>'ORGANIZATION_CODE',
9040                        p_org_partner_id    =>'ORGANIZATION_ID',
9041                        p_instance_code     => v_instance_code,
9042                        p_partner_type      => G_ORGANIZATION,
9043                        p_severity          => G_SEV_ERROR,
9044                        p_message_text      => lv_message_text,
9045                        p_batch_id          =>lv_batch_id,
9046                        p_debug             => v_debug,
9047                        p_error_text        =>lv_error_text);
9048 
9049       IF lv_return <> 0 THEN
9050         RAISE ex_logging_err;
9051       END IF;
9052 
9053      --Update sr_tp_id (organizations)
9054       v_sql_stmt := 41.1;
9055       lv_sql_stmt :=
9056       'UPDATE msc_st_location_associations mla'
9057       ||' SET    SR_TP_ID                       = ORGANIZATION_ID'
9058       ||' WHERE  process_flag                   = '||G_IN_PROCESS
9059       ||' AND    partner_type                   = '||G_ORGANIZATION
9060       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
9061       ||' AND    sr_instance_code               = :v_instance_code';
9062 
9063 
9064         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9065 
9066       EXECUTE IMMEDIATE lv_sql_stmt
9067               USING     lv_batch_id,
9068                         v_instance_code;
9069 
9070 
9071 
9072 
9073 
9074       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9075                      (p_app_short_name    => 'MSC',
9076                       p_error_code        => 'MSC_PP_INVALID_VALUE',
9077                       p_message_text      => lv_message_text,
9078                       p_error_text        => lv_error_text,
9079                       p_token1            => 'COLUMN_NAME',
9080                       p_token_value1      => 'PARTNER_NAME');
9081 
9082       IF lv_return <> 0 THEN
9083         RAISE ex_logging_err;
9084       END IF;
9085 
9086       -- Update sr_tp_id(customer)
9087      lv_return :=msc_st_util.derive_partner_org_id
9088                       (p_table_name        =>'MSC_ST_LOCATION_ASSOCIATIONS',
9089                        p_org_partner_name  =>'PARTNER_NAME',
9090                        p_org_partner_id    =>'SR_TP_ID',
9091                        p_instance_code     => v_instance_code,
9092                        p_partner_type      => G_CUSTOMER,
9093                        p_batch_id          =>lv_batch_id,
9094                        p_debug             => v_debug,
9095                        p_error_text        =>lv_error_text);
9096 
9097       IF lv_return <> 0 THEN
9098         RAISE ex_logging_err;
9099       END IF;
9100 
9101       -- Update sr_tp_id(supplier)
9102      lv_return :=msc_st_util.derive_partner_org_id
9103                       (p_table_name        =>'MSC_ST_LOCATION_ASSOCIATIONS',
9104                        p_org_partner_name  =>'PARTNER_NAME',
9105                        p_org_partner_id    =>'SR_TP_ID',
9106                        p_instance_code     => v_instance_code,
9107                        p_partner_type      => G_VENDOR,
9108                        p_severity          => G_SEV_ERROR,
9109                        p_message_text      => lv_message_text,
9110                        p_batch_id          => lv_batch_id,
9111                        p_debug             => v_debug,
9112                        p_error_text        => lv_error_text);
9113 
9114       IF lv_return <> 0 THEN
9115         RAISE ex_logging_err;
9116       END IF;
9117 
9118 
9119       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9120                      (p_app_short_name    => 'MSC',
9121                       p_error_code        => 'MSC_PP_INVALID_VALUE',
9122                       p_message_text      => lv_message_text,
9123                       p_error_text        => lv_error_text,
9124                       p_token1            => 'COLUMN_NAME',
9125                       p_token_value1      => 'TP_SITE_CODE');
9126 
9127       IF lv_return <> 0 THEN
9128         RAISE ex_logging_err;
9129       END IF;
9130 
9131       -- Derive sr_tp_site_id(Customer)
9132       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
9133                        (p_table_name        => 'MSC_ST_LOCATION_ASSOCIATIONS',
9134                         p_partner_name      => 'PARTNER_NAME',
9135                         p_partner_site_code => 'TP_SITE_CODE',
9136                         p_partner_site_id   => 'SR_TP_SITE_ID',
9137                         p_instance_code     => v_instance_code,
9138                         p_partner_type      => G_CUSTOMER,
9139                         p_severity          => G_SEV_ERROR,
9140                         p_message_text      => lv_message_text,
9141                         p_error_text        => lv_error_text,
9142                         p_batch_id          => lv_batch_id,
9143                         p_debug             => v_debug,
9144                         p_row               => lv_column_names);
9145 
9146       IF lv_return <> 0 THEN
9147         RAISE ex_logging_err;
9148       END IF;
9149 
9150       -- Derive sr_tp_site_id(Supplier)
9151       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
9152                        (p_table_name        => 'MSC_ST_LOCATION_ASSOCIATIONS',
9153                         p_partner_name      => 'PARTNER_NAME',
9154                         p_partner_site_code => 'TP_SITE_CODE',
9155                         p_partner_site_id   => 'SR_TP_SITE_ID',
9156                         p_instance_code     => v_instance_code,
9157                         p_partner_type      => G_VENDOR,
9158                         p_severity          => G_SEV_ERROR,
9159                         p_message_text      => lv_message_text,
9160                         p_error_text        => lv_error_text,
9161                         p_batch_id          => lv_batch_id,
9162                         p_debug             => v_debug,
9163                         p_row               => lv_column_names);
9164 
9165       IF lv_return <> 0 THEN
9166         RAISE ex_logging_err;
9167       END IF;
9168 
9169 
9170       --Deriving location_id
9171       v_sql_stmt := 42;
9172       lv_sql_stmt :=
9173       'UPDATE msc_st_location_associations mla'
9174       ||' SET   location_id              = (SELECT local_id'
9175       ||'       FROM   msc_local_id_setup mls'
9176       ||'       WHERE  mls.char1       = mla.sr_instance_code'
9177       ||'       AND    mls.char3       = mla.location_code'
9178       ||'       AND    mls.entity_name = ''LOCATION_ID'' )'
9179       ||' WHERE  process_flag                   = '||G_IN_PROCESS
9180       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
9181       ||' AND    sr_instance_code               = :v_instance_code';
9182 
9183 
9184         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9185 
9186       EXECUTE IMMEDIATE lv_sql_stmt
9187               USING     lv_batch_id,
9188                         v_instance_code;
9189 
9190 /*
9191       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9192                      (p_app_short_name    => 'MSC',
9193                       p_error_code        => 'MSC_PP_INVALID_VALUE',
9194                       p_message_text      => lv_message_text,
9195                       p_error_text        => lv_error_text,
9196                       p_token1            => 'COLUMN_NAME',
9197                       p_token_value1      => 'ORGANIZATION_CODE AND LOCATION_CODE');
9198 
9199       IF lv_return <> 0 THEN
9200         RAISE ex_logging_err;
9201       END IF;
9202 
9203    -- Error out the record if location_id is NULL
9204        v_sql_stmt := 43;
9205       lv_sql_stmt :=
9206       'UPDATE    msc_st_location_associations'
9207       ||' SET    process_flag            = '||G_ERROR_FLG||','
9208       ||'        error_text   =   '||''''||lv_message_text||''''
9209       ||' WHERE  NVL(location_id,'||NULL_VALUE||')= '||NULL_VALUE
9210       ||' AND    batch_id                = :lv_batch_id'
9211       ||' AND    process_flag            = '||G_IN_PROCESS
9212       ||' AND    sr_instance_code        = :v_instance_code';
9213 
9214       IF V_DEBUG THEN
9215         msc_st_util.log_message(lv_sql_stmt);
9216       END IF;
9217       EXECUTE IMMEDIATE lv_sql_stmt
9218               USING     lv_batch_id,
9219                         v_instance_code;
9220 
9221   */
9222 
9223 
9224 
9225 
9226 
9227       --Call to customised validation.
9228       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
9229         (ERRBUF         => lv_error_text,
9230          RETCODE        => lv_return,
9231          pBatchID       => lv_batch_id,
9232          pInstanceCode  => v_instance_code,
9233          pEntityName    => 'MSC_ST_LOCATION_ASSOCIATIONS',
9234          pInstanceID    => v_instance_id);
9235 
9236       IF NVL(lv_return,0) <> 0 THEN
9237         RAISE ex_logging_err;
9238       END IF;
9239 
9240       -- generation location id's for new locations
9241 
9242       IF (v_install_msc) THEN   -- IF MSC installed then only do this
9243       OPEN  c14(lv_batch_id);
9244       FETCH c14 BULK COLLECT INTO lb_rowid ;
9245 
9246       IF c14%ROWCOUNT > 0  THEN
9247         v_sql_stmt := 43.1;
9248         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
9249         UPDATE msc_st_location_associations
9250         SET    location_id  = msc_st_location_id_s.NEXTVAL
9251         WHERE  rowid          = lb_rowid(j);
9252 
9253         -- Insert record in LID
9254         v_sql_stmt := 43.2;
9255         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
9256         INSERT INTO msc_local_id_setup
9257          (local_id,
9258           st_transaction_id,
9259           instance_id,
9260           entity_name,
9261           data_source_type,
9262           char1,
9263           char3,
9264           SOURCE_SR_TP_ID,
9265 	  SOURCE_SR_TP_SITE_ID,
9266           last_update_date,
9267           last_updated_by,
9268           creation_date,
9269           created_by)
9270         SELECT
9271           location_id,
9272           st_transaction_id,
9273           v_instance_id,
9274           'LOCATION_ID',
9275           data_source_type,
9276           v_instance_code,
9277           location_code,
9278           SOURCE_SR_TP_ID,
9279 	  SOURCE_SR_TP_SITE_ID,
9280           v_current_date,
9281           v_current_user,
9282           v_current_date,
9283           v_current_user
9284         FROM  msc_st_location_associations
9285         WHERE rowid            = lb_rowid(j);
9286       END IF;
9287       CLOSE C14;
9288 
9289       --Deriving location_id
9290       v_sql_stmt := 43.3;
9291       lv_sql_stmt :=
9292       'UPDATE msc_st_location_associations mla'
9293       ||' SET   location_id              = (SELECT local_id'
9294       ||'       FROM   msc_local_id_setup mls'
9295       ||'       WHERE  mls.char1       = mla.sr_instance_code'
9296       ||'       AND    mls.char3       = mla.location_code'
9297       ||'       AND    mls.entity_name = ''LOCATION_ID'' )'
9298       ||' WHERE  process_flag                   = '||G_IN_PROCESS
9299       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
9300       ||' AND    sr_instance_code               = :v_instance_code';
9301 
9302 
9303         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9304 
9305       EXECUTE IMMEDIATE lv_sql_stmt
9306               USING     lv_batch_id,
9307                         v_instance_code;
9308 
9309     END IF ; -- v_install_msc
9310 
9311 
9312       -- Update partner_site_id with location id for orgs
9313        v_sql_stmt := 43.4;
9314       lv_sql_stmt :=
9315       'UPDATE    msc_st_location_associations'
9316       ||' SET    partner_site_id            =  location_id'
9317       ||' WHERE  NVL(partner_site_id,'||NULL_VALUE||')= '||NULL_VALUE
9318       ||' AND    batch_id                = :lv_batch_id'
9319       ||' AND    partner_type            = '||G_ORGANIZATION
9320       ||' AND    process_flag            = '||G_IN_PROCESS
9321       ||' AND    sr_instance_code        = :v_instance_code';
9322 
9323 
9324         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9325       EXECUTE IMMEDIATE lv_sql_stmt
9326               USING     lv_batch_id,
9327                         v_instance_code;
9328 
9329 
9330       -- Set the process flag as Valid and populate instance_id
9331       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
9332                         (p_table_name     => 'MSC_ST_LOCATION_ASSOCIATIONS',
9333                          p_instance_id    => v_instance_id,
9334                          p_instance_code  => v_instance_code,
9335                          p_process_flag   => G_VALID,
9336                          p_error_text     => lv_error_text,
9337                          p_debug          => v_debug,
9338                          p_batch_id       => lv_batch_id);
9339 
9340       IF lv_return <> 0 THEN
9341         RAISE ex_logging_err;
9342       END IF;
9343 
9344       -- At the end calling  the LOG_ERROR for logging all
9345       -- errored out records.
9346       lv_return := MSC_ST_UTIL.LOG_ERROR
9347                         (p_table_name     => 'MSC_ST_LOCATION_ASSOCIATIONS',
9348                          p_instance_code  => v_instance_code,
9349                          p_row            => lv_column_names,
9350                          p_severity       => G_SEV_ERROR,
9351                          p_message_text   => NULL,
9352                          p_error_text     => lv_error_text,
9353                          p_debug          => v_debug,
9354                          p_batch_id       => lv_batch_id  );
9355 
9356       IF lv_return <> 0 THEN
9357         RAISE ex_logging_err;
9358       END IF;
9359 
9360       COMMIT;
9361     END LOOP;
9362 
9363 
9364     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9365                  (p_app_short_name    => 'MSC',
9366                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
9367                   p_message_text      => lv_message_text,
9368                   p_error_text        => lv_error_text);
9369 
9370     IF lv_return <> 0 THEN
9371       RAISE ex_logging_err;
9372     END IF;
9373 
9374     --Validation check for the table MSC_ST_PARTNER_CONTACTS
9375 
9376     --Duplicate records check for the records whose source is other than XML
9377     --Different SQL is used because in XML we can identify the latest records
9378     --whereas in batch load we cannot.
9379 
9380     v_sql_stmt := 45;
9381     lv_sql_stmt :=
9382     'UPDATE    msc_st_partner_contacts mpc1 '
9383     ||' SET    process_flag = '||G_ERROR_FLG||','
9384     ||'        error_text   = '||''''||lv_message_text||''''
9385     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_partner_contacts mpc2 '
9386     ||'        WHERE mpc2.sr_instance_code  = mpc1.sr_instance_code'
9387     ||'        AND   NVL(mpc2.company_name,  '||''''||NULL_CHAR||''''||') = '
9388     ||'              NVL(mpc1.company_name,  '||''''||NULL_CHAR||''''||')'
9389     ||'        AND   mpc2.partner_name     = mpc1.partner_name  '
9390     ||'        AND   mpc2.partner_type     = mpc1.partner_type  '
9391     ||'        AND   mpc2.tp_site_code     = mpc1.tp_site_code  '
9392     ||'        AND   mpc2.process_flag     = mpc1.process_flag  '
9393     ||'        AND   NVL(mpc2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
9394     ||'        GROUP BY sr_instance_code,company_name,partner_name,'
9395     ||'              tp_site_code,partner_type '
9396     ||'        HAVING COUNT(*) >1 )'
9397     ||' AND    mpc1.process_flag              = '||G_IN_PROCESS
9398     ||' AND    mpc1.sr_instance_code          = :v_instance_code'
9399     ||' AND    NVL(mpc1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
9400 
9401 
9402       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9403 
9404     EXECUTE IMMEDIATE lv_sql_stmt
9405             USING     v_instance_code;
9406 
9407 
9408     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9409                  (p_app_short_name    => 'MSC',
9410                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
9411                   p_message_text      => lv_message_text,
9412                   p_error_text        => lv_error_text);
9413 
9414     IF lv_return <> 0 THEN
9415       RAISE ex_logging_err;
9416     END IF;
9417 
9418     --Duplicate records check for the records whose source is XML
9419     v_sql_stmt := 46;
9420     lv_sql_stmt :=
9421     'UPDATE    msc_st_partner_contacts mpc1 '
9422     ||' SET    process_flag = '||G_ERROR_FLG||','
9423     ||'        error_text   = '||''''||lv_message_text||''''
9424     ||' WHERE  message_id   < ( SELECT max(message_id) '
9425     ||'        FROM msc_st_partner_contacts mpc2 '
9426     ||'        WHERE mpc2.sr_instance_code  = mpc1.sr_instance_code'
9427     ||'        AND   NVL(mpc2.company_name,  '||''''||NULL_CHAR||''''||') = '
9428     ||'              NVL(mpc1.company_name,  '||''''||NULL_CHAR||''''||')'
9429     ||'        AND   mpc2.partner_name     = mpc1.partner_name  '
9430     ||'        AND   mpc2.partner_type     = mpc1.partner_type  '
9431     ||'        AND   mpc2.tp_site_code     = mpc1.tp_site_code  '
9432     ||'        AND   mpc2.process_flag     = mpc1.process_flag  '
9433     ||'        AND   NVL(mpc2.message_id,'||NULL_VALUE||') <> '||NULL_VALUE||')'
9434     ||' AND   mpc1.process_flag            = '||G_IN_PROCESS
9435     ||' AND   mpc1.sr_instance_code        = :v_instance_code '
9436     ||' AND   NVL(mpc1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
9437 
9438 
9439       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9440 
9441     EXECUTE IMMEDIATE lv_sql_stmt
9442             USING     v_instance_code;
9443 
9444     lv_column_names :=
9445     'PARTNER_NAME       ||''~''||'
9446     ||'TP_SITE_CODE     ||''~''||'
9447     ||'PARTNER_TYPE     ||''~''||'
9448     ||'NAME             ||''~''||'
9449     ||'SR_INSTANCE_CODE ||''~''||'
9450     ||'DISPLAY_NAME     ||''~''||'
9451     ||'EMAIL            ||''~''||'
9452     ||'FAX              ||''~''||'
9453     ||'DELETED_FLAG     ||''~''||'
9454     ||'COMPANY_NAME     ||''~''||'
9455     ||'ENABLED_FLAG ';
9456 
9457 
9458     -- Dividing records in batches based on batchsize
9459     LOOP
9460       v_sql_stmt := 47;
9461       SELECT       msc_st_batch_id_s.NEXTVAL
9462       INTO         lv_batch_id
9463       FROM         DUAL;
9464 
9465       v_sql_stmt := 48;
9466       lv_sql_stmt :=
9467       'UPDATE   msc_st_partner_contacts '
9468       ||' SET   batch_id                       = :lv_batch_id '
9469       ||' WHERE process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
9470       ||' AND   sr_instance_code               = :v_instance_code'
9471       ||' AND   NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
9472       ||' AND   rownum                        <= '||v_batch_size;
9473 
9474 
9475         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9476 
9477       EXECUTE IMMEDIATE lv_sql_stmt
9478               USING     lv_batch_id,
9479                         v_instance_code;
9480 
9481       EXIT WHEN SQL%NOTFOUND ;
9482 
9483       OPEN  c8(lv_batch_id);
9484       FETCH c8 BULK COLLECT INTO lb_rowid;
9485       CLOSE c8;
9486 
9487       v_sql_stmt := 49;
9488       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
9489       UPDATE msc_st_partner_contacts
9490       SET  st_transaction_id = msc_st_partner_contacts_s.NEXTVAL,
9491            refresh_id        = v_refresh_id,
9492            last_update_date  = v_current_date,
9493            last_updated_by   = v_current_user,
9494            creation_date     = v_current_date,
9495            created_by        = v_current_user
9496       WHERE  rowid           = lb_rowid(j);
9497 
9498 /*
9499       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9500                      (p_app_short_name    => 'MSC',
9501                       p_error_code        => 'MSC_PP_NO_DELETION',
9502                       p_message_text      => lv_message_text,
9503                       p_error_text        => lv_error_text,
9504                       p_token1            => 'TABLE_NAME',
9505                       p_token_value1      => 'MSC_ST_PARTNER_CONTACTS');
9506 
9507       -- Deletion is not allowed for this table
9508       v_sql_stmt := 50;
9509       lv_sql_stmt :=
9510       'UPDATE      msc_st_partner_contacts '
9511       ||' SET      process_flag     = '||G_ERROR_FLG||','
9512       ||'          error_text       = '||''''||lv_message_text||''''
9513       ||' WHERE    deleted_flag     = '||SYS_YES
9514       ||' AND      process_flag     = '||G_IN_PROCESS
9515       ||' AND      batch_id         =  :lv_batch_id'
9516       ||' AND      sr_instance_code =  :v_instance_code';
9517 
9518       IF V_DEBUG THEN
9519         msc_st_util.log_message(lv_sql_stmt);
9520       END IF;
9521 
9522       EXECUTE IMMEDIATE lv_sql_stmt
9523               USING     lv_batch_id,
9524                         v_instance_code;
9525 
9526   */
9527 
9528 
9529       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9530                      (p_app_short_name    => 'MSC',
9531                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
9532                       p_message_text      => lv_message_text,
9533                       p_error_text        => lv_error_text,
9534                       p_token1            => 'COLUMN_NAME',
9535                       p_token_value1      => 'DELETED_FLAG',
9536                       p_token2            => 'DEFAULT_VALUE',
9537                       p_token_value2      => SYS_NO);
9538 
9539       IF lv_return <> 0 THEN
9540         RAISE ex_logging_err;
9541       END IF;
9542 
9543       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
9544       -- Log error procedure inserts warning and also defaults the delete flag
9545 
9546       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
9547                       ||' NOT IN(1,2)';
9548 
9549       lv_return := MSC_ST_UTIL.LOG_ERROR
9550                         (p_table_name        => 'MSC_ST_PARTNER_CONTACTS',
9551                          p_instance_code     => v_instance_code,
9552                          p_row               => lv_column_names,
9553                          p_severity          => G_SEV_WARNING,
9554                          p_message_text      => lv_message_text,
9555                          p_error_text        => lv_error_text,
9556                          p_batch_id          => lv_batch_id,
9557                          p_where_str         => lv_where_str,
9558                          p_col_name          => 'DELETED_FLAG',
9559                          p_debug             => v_debug,
9560                          p_default_value     => SYS_NO);
9561 
9562       IF lv_return <> 0 THEN
9563         RAISE ex_logging_err;
9564       END IF;
9565 
9566       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9567                      (p_app_short_name    => 'MSC',
9568                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
9569                       p_message_text      => lv_message_text,
9570                       p_error_text        => lv_error_text,
9571                       p_token1            => 'COLUMN_NAME',
9572                       p_token_value1      => 'PARTNER_NAME OR '
9573                                              ||'TP_SITE_CODE OR '
9574                                              ||'NAME ');
9575 
9576       IF lv_return <> 0 THEN
9577         RAISE ex_logging_err;
9578       END IF;
9579 
9580       -- Error out records where partner_name/tp_site_code/name is NULL
9581       v_sql_stmt := 51;
9582       lv_sql_stmt :=
9583       'UPDATE    msc_st_partner_contacts'
9584       ||' SET    process_flag            = '||G_ERROR_FLG||','
9585       ||'         error_text   =   '||''''||lv_message_text||''''
9586       ||' WHERE  (NVL(partner_name,'||''''||NULL_CHAR||''''||') '
9587       ||'        =                 '||''''||NULL_CHAR||''''
9588       ||' OR     NVL(tp_site_code, '||''''||NULL_CHAR||''''||') '
9589       ||'        =                 '||''''||NULL_CHAR||''''
9590       ||' OR     NVL(name,         '||''''||NULL_CHAR||''''||') '
9591       ||'        =                 '||''''||NULL_CHAR||''''||') '
9592       ||' AND    batch_id                = :lv_batch_id'
9593       ||' AND    process_flag            = '||G_IN_PROCESS
9594       ||' AND    deleted_flag            = '||SYS_NO
9595       ||' AND    sr_instance_code        = :v_instance_code';
9596 
9597 
9598         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9599       EXECUTE IMMEDIATE lv_sql_stmt
9600               USING     lv_batch_id,
9601                         v_instance_code;
9602 
9603 
9604       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9605                      (p_app_short_name    => 'MSC',
9606                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
9607                       p_message_text      => lv_message_text,
9608                       p_error_text        => lv_error_text,
9609                       p_token1            => 'COLUMN_NAME',
9610                       p_token_value1      => 'PARTNER_TYPE');
9611 
9612       IF lv_return <> 0 THEN
9613         RAISE ex_logging_err;
9614       END IF;
9615 
9616       -- Error out records where partner_type NOT in (1,2)
9617       v_sql_stmt := 52;
9618       lv_sql_stmt :=
9619       'UPDATE    msc_st_partner_contacts'
9620       ||' SET    process_flag       = '||G_ERROR_FLG||','
9621       ||'        error_text   = '||''''||lv_message_text||''''
9622       ||' WHERE  sr_instance_code   = :v_instance_code'
9623       ||' AND    batch_id           = :lv_batch_id'
9624       ||' AND    process_flag       = '||G_IN_PROCESS
9625       ||' AND    partner_type NOT IN (1,2)' ;
9626 
9627 
9628         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9629       EXECUTE IMMEDIATE lv_sql_stmt
9630               USING     v_instance_code,
9631                         lv_batch_id;
9632 
9633       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9634                      (p_app_short_name    => 'MSC',
9635                       p_error_code        => 'MSC_PP_INVALID_VALUE',
9636                       p_message_text      => lv_message_text,
9637                       p_error_text        => lv_error_text,
9638                       p_token1            => 'COLUMN_NAME',
9639                       p_token_value1      => 'PARTNER_NAME');
9640 
9641       IF lv_return <> 0 THEN
9642         RAISE ex_logging_err;
9643       END IF;
9644 
9645       -- Update sr_tp_id(customer)
9646      lv_return :=msc_st_util.derive_partner_org_id
9647                       (p_table_name        =>'MSC_ST_PARTNER_CONTACTS',
9648                        p_org_partner_name  =>'PARTNER_NAME',
9649                        p_org_partner_id    =>'PARTNER_ID',
9650                        p_instance_code     => v_instance_code,
9651                        p_partner_type      => G_CUSTOMER,
9652                        p_batch_id          =>lv_batch_id,
9653                        p_debug             => v_debug,
9654                        p_error_text        =>lv_error_text);
9655 
9656       IF lv_return <> 0 THEN
9657         RAISE ex_logging_err;
9658       END IF;
9659 
9660       -- Update sr_tp_id(supplier)
9661      lv_return :=msc_st_util.derive_partner_org_id
9662                       (p_table_name        =>'MSC_ST_PARTNER_CONTACTS',
9663                        p_org_partner_name  =>'PARTNER_NAME',
9664                        p_org_partner_id    =>'PARTNER_ID',
9665                        p_instance_code     => v_instance_code,
9666                        p_partner_type      => G_VENDOR,
9667                        p_severity          => G_SEV_ERROR,
9668                        p_message_text      => lv_message_text,
9669                        p_batch_id          => lv_batch_id,
9670                        p_debug             => v_debug,
9671                        p_error_text        => lv_error_text);
9672 
9673       IF lv_return <> 0 THEN
9674         RAISE ex_logging_err;
9675       END IF;
9676 
9677       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9678                      (p_app_short_name    => 'MSC',
9679                       p_error_code        => 'MSC_PP_INVALID_VALUE',
9680                       p_message_text      => lv_message_text,
9681                       p_error_text        => lv_error_text,
9682                       p_token1            => 'COLUMN_NAME',
9683                       p_token_value1      => 'TP_SITE_CODE');
9684 
9685       IF lv_return <> 0 THEN
9686         RAISE ex_logging_err;
9687       END IF;
9688 
9689       -- Derive sr_tp_site_id(Customer)
9690       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
9691                        (p_table_name        => 'MSC_ST_PARTNER_CONTACTS',
9692                         p_partner_name      => 'PARTNER_NAME',
9693                         p_partner_site_code => 'TP_SITE_CODE',
9694                         p_partner_site_id   => 'PARTNER_SITE_ID',
9695                         p_instance_code     => v_instance_code,
9696                         p_partner_type      => G_CUSTOMER,
9697                         p_error_text        => lv_error_text,
9698                         p_batch_id          => lv_batch_id,
9699                         p_debug             => v_debug,
9700                         p_row               => lv_column_names);
9701 
9702       IF lv_return <> 0 THEN
9703         RAISE ex_logging_err;
9704       END IF;
9705 
9706       -- Derive sr_tp_site_id(Supplier)
9707       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
9708                        (p_table_name        => 'MSC_ST_PARTNER_CONTACTS',
9709                         p_partner_name      => 'PARTNER_NAME',
9710                         p_partner_site_code => 'TP_SITE_CODE',
9711                         p_partner_site_id   => 'PARTNER_SITE_ID',
9712                         p_instance_code     => v_instance_code,
9713                         p_partner_type      => G_VENDOR,
9714                         p_severity          => G_SEV_ERROR,
9715                         p_message_text      => lv_message_text,
9716                         p_error_text        => lv_error_text,
9717                         p_batch_id          => lv_batch_id,
9718                         p_debug             => v_debug,
9719                         p_row               => lv_column_names);
9720 
9721       IF lv_return <> 0 THEN
9722         RAISE ex_logging_err;
9723       END IF;
9724 
9725       --Call to customised validation.
9726       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
9727         (ERRBUF         => lv_error_text,
9728          RETCODE        => lv_return,
9729          pBatchID       => lv_batch_id,
9730          pInstanceCode  => v_instance_code,
9731          pEntityName    => 'MSC_ST_PARTNER_CONTACTS',
9732          pInstanceID    => v_instance_id);
9733 
9734       IF NVL(lv_return,0) <> 0 THEN
9735         RAISE ex_logging_err;
9736       END IF;
9737 
9738       -- Set the process flag as Valid and populate instance_id
9739       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
9740                         (p_table_name     => 'MSC_ST_PARTNER_CONTACTS',
9741                          p_instance_id    => v_instance_id,
9742                          p_instance_code  => v_instance_code,
9743                          p_process_flag   => G_VALID,
9744                          p_error_text     => lv_error_text,
9745                          p_debug          => v_debug,
9746                          p_batch_id       => lv_batch_id);
9747 
9748       IF lv_return <> 0 THEN
9749         RAISE ex_logging_err;
9750       END IF;
9751 
9752       -- At the end calling  the LOG_ERROR for logging all
9753       -- errored out records.
9754       lv_return := MSC_ST_UTIL.LOG_ERROR
9755                         (p_table_name     => 'MSC_ST_PARTNER_CONTACTS',
9756                          p_instance_code  => v_instance_code,
9757                          p_row            => lv_column_names,
9758                          p_severity       => G_SEV_ERROR,
9759                          p_message_text   => NULL,
9760                          p_error_text     => lv_error_text,
9761                          p_debug          => v_debug,
9762                          p_batch_id       => lv_batch_id  );
9763 
9764       IF lv_return <> 0 THEN
9765         RAISE ex_logging_err;
9766       END IF;
9767 
9768       COMMIT;
9769     END LOOP;
9770 
9771 
9772     --Validation for GROUPS IS
9773 
9774     lv_return := MSC_ST_util.GET_ERROR_MESSAGE
9775                  (p_app_short_name    => 'MSC',
9776                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
9777                   p_message_text      => lv_message_text,
9778                   p_error_text        => lv_error_text);
9779 
9780     IF lv_return <> 0 THEN
9781       RAISE ex_logging_err;
9782     END IF;
9783 
9784     --Duplicate records check for the records whose source is XML
9785     v_sql_stmt := 52;
9786     lv_sql_stmt :=
9787     ' UPDATE   MSC_ST_GROUPS msu1'
9788     ||' SET    process_flag = '||G_ERROR_FLG||','
9789     ||'        error_text   = '||''''||lv_message_text||''''
9790     ||' WHERE  message_id <  (SELECT MAX(message_id)'
9791     ||'        FROM  MSC_ST_GROUPS msu2'
9792     ||'        where msu2.group_name        = msu1.group_name '
9793     ||'        AND   msu2.process_flag      <> '||G_ERROR_FLG
9794     ||'        AND   NVL(msu2.message_id,'||NULL_VALUE||')<>'||NULL_VALUE||')'
9795     ||' AND    msu1.process_flag     = '||G_IN_PROCESS
9796     ||' AND    msu1.sr_instance_code = :v_instance_code'
9797     ||' AND    NVL(msu1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
9798 
9799 
9800       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9801 
9802     EXECUTE IMMEDIATE lv_sql_stmt
9803             USING     v_instance_code;
9804 
9805     lv_return := MSC_ST_util.GET_ERROR_MESSAGE
9806                  (p_app_short_name    => 'MSC',
9807                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
9808                   p_message_text      => lv_message_text,
9809                   p_error_text        => lv_error_text);
9810 
9811     IF lv_return <> 0 THEN
9812       RAISE ex_logging_err;
9813     END IF;
9814 
9815     --Duplicate records check for the records whose source is other than XML
9816     --Different SQL is used because in XML we can identify the latest records
9817     --whereas in batch load we cannot.
9818 
9819     v_sql_stmt := 53;
9820     lv_sql_stmt :=
9821     ' UPDATE   MSC_ST_GROUPS msu1'
9822     ||' SET    process_flag = '||G_ERROR_FLG||','
9823     ||'        error_text   = '||''''||lv_message_text||''''
9824     ||' WHERE  EXISTS( SELECT 1 '
9825     ||'        FROM   MSC_ST_GROUPS msu2'
9826     ||'        WHERE  msu2.group_name                       =  msu1.group_name'
9827     ||'        AND    msu2.process_flag                     <> '||G_ERROR_FLG
9828     ||'        AND    NVL(msu2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
9829     ||'        AND    msu2.rowid                            <> msu1.rowid)'
9830     ||' AND    msu1.process_flag     = '||G_IN_PROCESS
9831     ||' AND    msu1.sr_instance_code = :v_instance_code '
9832     ||' AND    NVL(msu1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
9833 
9834 
9835       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9836 
9837     EXECUTE IMMEDIATE lv_sql_stmt
9838             USING     v_instance_code;
9839 
9840     lv_column_names :=
9841     'GROUP_NAME	                ||''~''||'
9842     ||'	DESCRIPTION             ||''~''||'
9843     ||'	GROUP_TYPE              ||''~''||'
9844     ||'	CONTACT_USER_NAME       ||''~''||'
9845     ||'	CONTRACT_DOC_URL        ||''~''||'
9846     ||'	EFFECTIVE_DATE	        ||''~''||'
9847     ||'	DISABLE_DATE	        ||''~''||'
9848     ||'	CONTEXT		        ||''~''||'
9849     ||'	ATTRIBUTE1              ||''~''||'
9850     ||'	ATTRIBUTE2              ||''~''||'
9851     ||'	ATTRIBUTE3              ||''~''||'
9852     ||'	ATTRIBUTE4              ||''~''||'
9853     ||'	ATTRIBUTE5              ||''~''||'
9854     ||'	ATTRIBUTE6              ||''~''||'
9855     ||'	ATTRIBUTE7              ||''~''||'
9856     ||'	ATTRIBUTE8              ||''~''||'
9857     ||'	ATTRIBUTE9              ||''~''||'
9858     ||'	ATTRIBUTE10             ||''~''||'
9859     ||'	ATTRIBUTE11             ||''~''||'
9860     ||'	ATTRIBUTE12             ||''~''||'
9861     ||'	ATTRIBUTE13             ||''~''||'
9862     ||'	ATTRIBUTE14             ||''~''||'
9863     ||'	ATTRIBUTE15             ||''~''||'
9864     ||'	SR_INSTANCE_CODE        ||''~''||'
9865     ||'	DELETED_FLAG  '          ;
9866 
9867 
9868     -- Dividing records in batches based on batchsize
9869     LOOP
9870       v_sql_stmt := 54;
9871       SELECT msc_st_batch_id_s.NEXTVAL
9872       INTO   lv_batch_id
9873       FROM   dual;
9874 
9875       v_sql_stmt := 55;
9876       lv_sql_stmt :=
9877       ' UPDATE   MSC_ST_GROUPS '
9878       ||' SET    batch_id  = :lv_batch_id'
9879       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
9880       ||' AND    sr_instance_code               = :v_instance_code'
9881       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
9882       ||' AND    rownum                        <= '||v_batch_size;
9883 
9884 
9885         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9886 
9887       EXECUTE IMMEDIATE lv_sql_stmt
9888               USING     lv_batch_id,
9889                         v_instance_code;
9890 
9891       EXIT WHEN SQL%NOTFOUND;
9892 
9893 	-- updation of WHO columns
9894 
9895       OPEN  c9(lv_batch_id);
9896       FETCH c9 BULK COLLECT INTO lb_rowid;
9897       CLOSE c9;
9898 
9899       v_sql_stmt := 56;
9900       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
9901       UPDATE MSC_ST_GROUPS
9902       SET  st_transaction_id = msc_st_groups_s.NEXTVAL,
9903            last_update_date  = v_current_date,
9904            last_updated_by   = v_current_user,
9905            creation_date     = v_current_date,
9906            created_by        = v_current_user
9907       WHERE  rowid           = lb_rowid(j);
9908 
9909       -- Set the  message
9910       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
9911                      (p_app_short_name    => 'MSC',
9912                       p_error_code        => 'MSC_PP_NO_DELETION',
9913                       p_message_text      => lv_message_text,
9914                       p_error_text        => lv_error_text,
9915                       p_token1            => 'TABLE_NAME',
9916                       p_token_value1      => 'MSC_ST_GROUPS');
9917 
9918       IF lv_return <> 0 THEN
9919         RAISE ex_logging_err;
9920        END IF;
9921 
9922       --Deletion is not allowed on this table.
9923       v_sql_stmt := 57;
9924       lv_sql_stmt :=
9925       ' UPDATE     msc_st_groups '
9926       ||' SET      process_flag     ='||G_ERROR_FLG||','
9927       ||'          error_text       = '||''''||lv_message_text||''''
9928       ||' WHERE    deleted_flag     = '||SYS_YES
9929       ||' AND      process_flag     = '||G_IN_PROCESS
9930       ||' AND      batch_id         =  :lv_batch_id'
9931       ||' AND      sr_instance_code = :v_instance_code';
9932 
9933 
9934             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
9935 
9936       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
9937 
9938       -- Set the message for deleted flag
9939 
9940       lv_return :=    MSC_ST_util.GET_ERROR_MESSAGE
9941                      (p_app_short_name    => 'MSC',
9942                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
9943                       p_message_text      => lv_message_text,
9944                       p_error_text        => lv_error_text,
9945                       p_token1            => 'COLUMN_NAME',
9946                       p_token_value1      => 'DELETED_FLAG',
9947                       p_token2            => 'DEFAULT_VALUE',
9948                       p_token_value2      =>  SYS_NO);
9949 
9950       IF lv_return <> 0 THEN
9951         RAISE ex_logging_err;
9952       END IF;
9953 
9954       -- Check DELETED_FLAG column has valid value or not and
9955       -- if not set it to SYS_NO
9956 
9957       lv_where_str :=
9958       ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
9959 
9960       lv_return := MSC_ST_util.LOG_ERROR
9961                     (p_table_name        => 'MSC_ST_GROUPS',
9962                      p_instance_code     => v_instance_code,
9963                      p_row               => lv_column_names,
9964                      p_severity          => G_SEV_WARNING,
9965                      p_message_text      => lv_message_text,
9966                      p_error_text        => lv_error_text,
9967                      p_batch_id          => lv_batch_id,
9968                      p_where_str         => lv_where_str,
9969                      p_col_name          => 'DELETED_FLAG',
9970                      p_debug             => v_debug,
9971                      p_default_value     => SYS_NO);
9972 
9973       IF lv_return <> 0 THEN
9974         RAISE ex_logging_err;
9975       END IF;
9976 
9977       -- Set the error message for invalid group type
9978 
9979       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
9980                      (p_app_short_name    => 'MSC',
9981                       p_error_code        => 'MSC_PP_INVALID_VALUE',
9982                       p_message_text      => lv_message_text,
9983                       p_error_text        => lv_error_text,
9984                       p_token1            => 'COLUMN_NAME',
9985                       p_token_value1      => 'GROUP_TYPE');
9986 
9987       IF lv_return <> 0 THEN
9988         RAISE ex_logging_err;
9989       END IF;
9990 
9991 
9992       -- error out record with GROUP_TYPE NOT IN (1,2)
9993 
9994       v_sql_stmt := 58;
9995 
9996       lv_sql_Stmt:=
9997       'UPDATE    msc_st_groups '
9998       ||' SET    process_flag        ='||G_ERROR_FLG||','
9999       ||'        error_text          = '||''''||lv_message_text||''''
10000       ||' WHERE  NVL(group_type,'||NULL_VALUE||')  NOT IN (1,2) '
10001       ||' AND    sr_instance_code    = :v_instance_code'
10002       ||' AND    process_flag        ='|| G_IN_PROCESS
10003       ||' AND    batch_id            = :lv_batch_id';
10004 
10005 
10006            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10007 
10008       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
10009 
10010 
10011       -- The following values are mandatory in MSC_GROUPS
10012       -- 1.Group name
10013       -- 2.Effective Date
10014       -- 3.Contact Doc URL
10015       -- 4.Group Owner Id.
10016 
10017       lv_return :=
10018       MSC_ST_util.GET_ERROR_MESSAGE (p_app_short_name    => 'MSC',
10019                                          p_error_code        => 'MSC_PP_COL_VAL_NULL',
10020                	                         p_message_text      => lv_message_text,
10021                                    	 p_error_text        => lv_error_text,
10022                       	                 p_token1            => 'COLUMN_NAME',
10023                                          p_token_value1      => 'GROUP_NAME OR '
10024                                                              ||' EFFECTIVE_DATE OR '
10025                                                              ||' CONTRACT_DOC_URL OR '
10026                                                              ||' GROUP_OWNER_ID ');
10027 
10028        IF lv_return <> 0 THEN
10029          RAISE ex_logging_err;
10030        END IF;
10031 
10032 
10033       v_sql_stmt := 59;
10034 
10035       lv_sql_stmt :=
10036       ' UPDATE  MSC_ST_GROUPS '
10037       ||' SET   process_flag        = '||G_ERROR_FLG||','
10038       ||'       error_text          = '||''''||lv_message_text||''''
10039       ||' WHERE (effective_date IS NULL  '
10040       ||' OR    group_name      IS NULL  '
10041       ||' OR    contract_doc_url IS NULL  '
10042       ||' OR    group_owner_id  IS NULL) '
10043       ||' AND   batch_id            = :lv_batch_id'
10044       ||' AND   process_flag        = '||G_IN_PROCESS
10045       ||' AND   sr_instance_code    = :v_instance_code';
10046 
10047 
10048         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10049 
10050       EXECUTE IMMEDIATE lv_sql_stmt
10051               USING     lv_batch_id,
10052                         v_instance_code;
10053 
10054       -- Set the error message for  DISABLE_DATE should be greater than EFFECTIVE_DATE
10055 
10056       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10057                      (p_app_short_name    => 'MSC',
10058                       p_error_code        => 'MSC_PP_END_GT_ST_DATE',
10059                       p_message_text      => lv_message_text,
10060                       p_error_text        => lv_error_text);
10061 
10062       IF lv_return <> 0 THEN
10063         RAISE ex_logging_err;
10064       END IF;
10065 
10066      v_sql_stmt := 60;
10067 
10068      lv_sql_stmt :=
10069       ' UPDATE  MSC_ST_GROUPS '
10070       ||' SET   process_flag        = '||G_ERROR_FLG||','
10071       ||'       error_text          = '||''''||lv_message_text||''''
10072       ||' WHERE effective_date      > disable_date'
10073       ||' AND   disable_date        IS NOT NULL'
10074       ||' AND   batch_id            = :lv_batch_id'
10075       ||' AND   process_flag        = '||G_IN_PROCESS
10076       ||' AND   sr_instance_code    = :v_instance_code';
10077 
10078 
10079         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10080 
10081       EXECUTE IMMEDIATE lv_sql_stmt
10082               USING     lv_batch_id,
10083                         v_instance_code;
10084 
10085 
10086 
10087     --Derive contact_user_id
10088 
10089       v_sql_stmt := 61;
10090       lv_sql_stmt :=
10091       'UPDATE    msc_st_groups msg'
10092       ||' SET    contact_user_id  = (select user_id '
10093       ||'                        from   fnd_user fu'
10094       ||'                        where  UPPER(fu.user_name) = UPPER(msg.contact_user_name)) '
10095       ||' WHERE  contact_user_name IS NOT NULL'
10096       ||' AND    batch_id         =  :lv_batch_id'
10097       ||' AND    sr_instance_code =  :v_instance_code'
10098       ||' AND    process_flag     = '||G_IN_PROCESS;
10099 
10100 
10101         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10102 
10103       EXECUTE IMMEDIATE lv_sql_stmt
10104               USING     lv_batch_id,
10105                         v_instance_code;
10106 
10107 
10108       -- Error out the invalid Contact User Name
10109 
10110       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10111                      (p_app_short_name    => 'MSC',
10112                       p_error_code        => 'MSC_PP_INVALID_VALUE',
10113                       p_message_text      => lv_message_text,
10114                       p_error_text        => lv_error_text,
10115                       p_token1            => 'COLUMN_NAME',
10116                       p_token_value1      => 'CONTACT_USER_NAME');
10117 
10118       IF lv_return <> 0 THEN
10119         RAISE ex_logging_err;
10120       END IF;
10121 
10122       -- Update the record as errored out record
10123 
10124       v_sql_stmt := 62;
10125       lv_sql_stmt :=
10126       'UPDATE    MSC_ST_GROUPS '
10127       ||' SET    process_flag  = '||G_ERROR_FLG||','
10128       ||'        error_text    = '||''''||lv_message_text||''''
10129       ||' WHERE  contact_user_name IS NOT NULL '
10130       ||' AND    contact_user_id   IS NULL '
10131       ||' AND    batch_id         =  :lv_batch_id'
10132       ||' AND    sr_instance_code =  :v_instance_code'
10133       ||' AND    process_flag     = '||G_IN_PROCESS;
10134 
10135 
10136         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10137 
10138       EXECUTE IMMEDIATE lv_sql_stmt
10139               USING     lv_batch_id,
10140                         v_instance_code;
10141 
10142       --DERIVE GROUP_ID
10143 
10144       lv_return :=  MSC_ST_util.DERIVE_GROUP_ID
10145                      (p_table_name    =>'MSC_ST_GROUPS',
10146                       p_grp_col_name  => 'GROUP_NAME',
10147                       p_grp_col_id   => 'GROUP_ID',
10148                       p_instance_code => v_instance_code,
10149                       p_instance_id   => v_instance_id,
10150                       p_batch_id      => lv_batch_id,
10151                       p_debug         => v_debug,
10152                       p_severity      => G_SEV_ERROR,
10153                       p_error_text    => lv_error_text,
10154                       p_row           => lv_column_names);
10155 
10156       IF lv_return <> 0 THEN
10157         RAISE ex_logging_err;
10158       END IF;
10159 
10160       select fnd_global.user_id into lv_user_id from dual;
10161 
10162     IF NOT MSC_ST_util.IS_OPERATOR(lv_user_id) THEN
10163       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10164                      (p_app_short_name    => 'MSC',
10165                       p_error_code        => 'MSC_PP_INVALID_VALUE',
10166                       p_message_text      => lv_message_text,
10167                       p_error_text        => lv_error_text,
10168                       p_token1            => 'COLUMN_NAME',
10169                       p_token_value1      => 'GROUP_OWNER_ID');
10170 
10171       IF lv_return <> 0 THEN
10172         RAISE ex_logging_err;
10173       END IF;
10174 
10175       v_sql_stmt := 63;
10176       lv_sql_stmt :=
10177       'UPDATE    MSC_ST_GROUPS msg'
10178       ||' SET    process_flag  = '||G_ERROR_FLG||','
10179       ||'        error_text    = '||''''||lv_message_text||''''
10180       ||' WHERE  EXISTS(SELECT 1 '
10181       ||'               FROM MSC_GROUPS mg'
10182       ||'               where  mg.GROUP_OWNER_ID <> msg.GROUP_OWNER_ID '
10183       ||'               AND    mg.group_id       =  msg.group_id )'
10184       ||' AND    group_id IS NOT NULL'
10185       ||' AND    batch_id         =  :lv_batch_id'
10186       ||' AND    sr_instance_code =  :v_instance_code'
10187       ||' AND    process_flag     = '||G_IN_PROCESS;
10188 
10189 
10190         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10191 
10192 
10193       EXECUTE IMMEDIATE lv_sql_stmt
10194               USING     lv_batch_id,
10195                         v_instance_code;
10196     END IF;
10197 
10198       --Call to customised validation.
10199 
10200       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
10201         (ERRBUF         => lv_error_text,
10202          RETCODE        => lv_return,
10203          pBatchID       => lv_batch_id,
10204          pInstanceCode  => v_instance_code,
10205          pEntityName    => 'MSC_ST_GROUPS',
10206          pInstanceID    => v_instance_id);
10207 
10208       IF NVL(lv_return,0) <> 0 THEN
10209         RAISE ex_logging_err;
10210       END IF;
10211 
10212       -- Generate group_id for the first time
10213 
10214       OPEN c10(lv_batch_id);
10215       FETCH c10 BULK COLLECT INTO lb_rowid ;
10216 
10217       IF c10%ROWCOUNT > 0  THEN
10218         v_sql_stmt := 64;
10219         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
10220         UPDATE MSC_ST_GROUPS
10221         SET    group_id    = msc_st_group_id_s.NEXTVAL
10222         WHERE  rowid        = lb_rowid(j);
10223 
10224         v_sql_stmt := 65;
10225         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
10226         INSERT INTO msc_local_id_setup
10227         (local_id,
10228          st_transaction_id,
10229          instance_id,
10230          entity_name,
10231          data_source_type,
10232          char1,
10233          last_update_date,
10234          last_updated_by,
10235          creation_date,
10236          created_by)
10237         SELECT
10238          group_id,
10239          st_transaction_id,
10240          v_instance_id,
10241          'GROUP_ID',
10242          data_source_type,
10243          group_name,
10244          v_current_date,
10245          v_current_user,
10246          v_current_date,
10247          v_current_user
10248         FROM  MSC_ST_GROUPS
10249         WHERE rowid = lb_rowid(j);
10250 
10251       END IF;
10252       CLOSE c10 ;
10253 
10254 
10255       -- Set the process flag as Valid and derive sr_instance_id
10256 
10257       lv_return := MSC_ST_util.SET_PROCESS_FLAG
10258                   (p_table_name     => 'MSC_ST_GROUPS',
10259                    p_instance_id    => v_instance_id,
10260                    p_instance_code  => v_instance_code,
10261                    p_process_flag   => G_VALID,
10262                    p_error_text     => lv_error_text,
10263                    p_debug          => v_debug,
10264                    p_batch_id       => lv_batch_id);
10265 
10266       IF lv_return <> 0 THEN
10267         RAISE ex_logging_err;
10268       END IF;
10269 
10270       -- Inserting all the errored out records into MSC_ERRORS
10271 
10272       lv_return := MSC_ST_util.LOG_ERROR
10273                    (p_table_name        => 'MSC_ST_GROUPS',
10274                     p_instance_code     => v_instance_code,
10275                     p_row               => lv_column_names,
10276                     p_severity          => G_SEV_ERROR,
10277                     p_error_text        => lv_error_text,
10278                     p_message_text      => NULL,
10279                     p_batch_id          => lv_batch_id);
10280 
10281 
10282       IF lv_return <> 0 THEN
10283         RAISE ex_logging_err;
10284       END IF;
10285 
10286       OPEN  c11(lv_batch_id);
10287       FETCH c11 BULK COLLECT INTO
10288                   lb_GROUP_ID,
10289                   lb_GROUP_NAME,
10290                   lb_DESCRIPTION,
10291                   lb_GROUP_TYPE,
10292                   lb_GROUP_OWNER_ID,
10293                   lb_CONTACT_USER_ID,
10294                   lb_CONTRACT_DOC_URL,
10295                   lb_EFFECTIVE_DATE,
10296                   lb_DISABLE_DATE,
10297                   lb_LAST_UPDATE_DATE,
10298                   lb_LAST_UPDATED_BY,
10299                   lb_LAST_UPDATE_LOGIN,
10300                   lb_CONTEXT,
10301                   lb_ATTRIBUTE1,
10302                   lb_ATTRIBUTE2,
10303                   lb_ATTRIBUTE3,
10304                   lb_ATTRIBUTE4,
10305                   lb_ATTRIBUTE5,
10306                   lb_ATTRIBUTE6,
10307                   lb_ATTRIBUTE7,
10308                   lb_ATTRIBUTE8,
10309                   lb_ATTRIBUTE9,
10310                   lb_ATTRIBUTE10,
10311                   lb_ATTRIBUTE11,
10312                   lb_ATTRIBUTE12,
10313                   lb_ATTRIBUTE13,
10314                   lb_ATTRIBUTE14,
10315                   lb_ATTRIBUTE15;
10316 
10317 
10318 
10319       IF c11%ROWCOUNT > 0  THEN
10320         v_sql_stmt := 66;
10321 
10322       FORALL j IN lb_GROUP_ID.FIRST..lb_GROUP_ID.LAST
10323         UPDATE msc_groups
10324         SET    description      = lb_description(j),
10325                group_type = lb_group_type(j),
10326                owner_contact	= lb_contact_user_id(j),
10327                contract_doc_url = lb_contract_doc_url(j),
10328                effective_date = lb_effective_date(j),
10329                disable_date	= lb_disable_date(j),
10330                last_update_date = v_current_date,
10331                last_updated_by	= v_current_user,
10332                context	        = lb_context(j),
10333                attribute1       = lb_attribute1(j),
10334                attribute2       = lb_attribute2(j),
10335                attribute3       = lb_attribute3(j),
10336                attribute4       = lb_attribute4(j),
10337                attribute5       = lb_attribute5(j),
10338                attribute6       = lb_attribute6(j),
10339                attribute7       = lb_attribute7(j),
10340                attribute8       = lb_attribute8(j),
10341                attribute9       = lb_attribute9(j),
10342                attribute10      = lb_attribute10(j),
10343                attribute11      = lb_attribute11(j),
10344                attribute12      = lb_attribute12(j),
10345                attribute13      = lb_attribute13(j),
10346                attribute14      = lb_attribute14(j),
10347                attribute15      = lb_attribute15(j)
10348         WHERE  group_id         = lb_group_id(j);
10349 	end if;
10350 CLOSE c11;
10351 
10352     --Inserts the valid records into the ODS table, Since the collection program
10353      --doesn't collect this entity incrementally and whereras post-processor always
10354      --runs in the incremental mode.
10355 
10356      v_sql_stmt := 67;
10357      lv_sql_stmt :=
10358 		     'INSERT INTO MSC_GROUPS'
10359 		||'(	GROUP_ID	,'
10360 		||'	GROUP_NAME	,'
10361 		||'	DESCRIPTION	,'
10362 		||'	GROUP_TYPE	,'
10363 		||'	GROUP_OWNER_ID	,'
10364 		||'	OWNER_CONTACT	,'
10365 		||'	CONTRACT_DOC_URL,'
10366 		||'	EFFECTIVE_DATE	,'
10367 		||'	DISABLE_DATE	,'
10368 		||'	CREATION_DATE	,'
10369 		||'	CREATED_BY	,'
10370 		||'	LAST_UPDATE_DATE,'
10371 		||'	LAST_UPDATED_BY	,'
10372 		||'	LAST_UPDATE_LOGIN,'
10373 		||'	CONTEXT	,'
10374 		||'	ATTRIBUTE1	,'
10375 		||'	ATTRIBUTE2	,'
10376 		||'	ATTRIBUTE3	,'
10377 		||'	ATTRIBUTE4	,'
10378 		||'	ATTRIBUTE5	,'
10379 		||'	ATTRIBUTE6	,'
10380 		||'	ATTRIBUTE7	,'
10381 		||'	ATTRIBUTE8	,'
10382 		||'	ATTRIBUTE9	,'
10383 		||'	ATTRIBUTE10	,'
10384 		||'	ATTRIBUTE11	,'
10385 		||'	ATTRIBUTE12	,'
10386 		||'	ATTRIBUTE13	,'
10387 		||'	ATTRIBUTE14	,'
10388 		||'	ATTRIBUTE15	)'
10389 		||' SELECT'
10390 		||'	GROUP_ID	,'
10391 		||'	GROUP_NAME	,'
10392 		||'	DESCRIPTION	,'
10393 		||'	GROUP_TYPE	,'
10394 		||'	GROUP_OWNER_ID	,'
10395 		--||'	CONTACT_USER_NAME,'
10396 		||'	CONTACT_USER_ID	,'
10397 		||'	CONTRACT_DOC_URL,'
10398 		||'	EFFECTIVE_DATE	,'
10399 		||'	DISABLE_DATE	,'
10400 		||'	CREATION_DATE	,'
10401 		||'	CREATED_BY	,'
10402 		||'	LAST_UPDATE_DATE,'
10403 		||'	LAST_UPDATED_BY	,'
10404 		||'	LAST_UPDATE_LOGIN,'
10405 		||'	CONTEXT	,'
10406 		||'	ATTRIBUTE1	,'
10407 		||'	ATTRIBUTE2	,'
10408 		||'	ATTRIBUTE3	,'
10409 		||'	ATTRIBUTE4	,'
10410 		||'	ATTRIBUTE5	,'
10411 		||'	ATTRIBUTE6	,'
10412 		||'	ATTRIBUTE7	,'
10413 		||'	ATTRIBUTE8	,'
10414 		||'	ATTRIBUTE9	,'
10415 		||'	ATTRIBUTE10	,'
10416 		||'	ATTRIBUTE11	,'
10417 		||'	ATTRIBUTE12	,'
10418 		||'	ATTRIBUTE13	,'
10419 		||'	ATTRIBUTE14	,'
10420 		||'	ATTRIBUTE15	'
10421 		||' FROM   MSC_ST_GROUPS msg'
10422 		||' WHERE  sr_instance_code = :sr_instance_code'
10423 		||' AND    batch_id         = :lv_batch_id'
10424 		||' AND    process_flag     = '||G_VALID
10425                 ||' AND    NOT EXISTS( SELECT 1 '
10426                 ||'                    FROM   MSC_GROUPS mg'
10427                 ||'                    WHERE  mg.group_id = msg.group_id)';
10428 
10429 
10430         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10431 
10432       EXECUTE IMMEDIATE lv_sql_stmt
10433               USING     v_instance_code,
10434                         lv_batch_id;
10435       COMMIT;
10436     END LOOP;
10437 
10438         --Validation for GROUP COMPANIES IS
10439 
10440     lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10441                  (p_app_short_name    => 'MSC',
10442                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
10443                   p_message_text      => lv_message_text,
10444                   p_error_text        => lv_error_text);
10445 
10446     IF lv_return <> 0 THEN
10447       RAISE ex_logging_err;
10448     END IF;
10449 
10450     --Duplicate records check for the records whose source is XML
10451     v_sql_stmt := 68;
10452     lv_sql_stmt :=
10453     ' UPDATE   msc_st_group_companies mgc1'
10454     ||' SET    process_flag = '||G_ERROR_FLG||','
10455     ||'        error_text   = '||''''||lv_message_text||''''
10456     ||' WHERE  message_id <  (SELECT MAX(message_id)'
10457     ||'                       FROM   msc_st_group_companies mgc2'
10458     ||'                       WHERE  mgc2.group_name    = mgc1.group_name '
10459     ||'                       AND    mgc2.company_name  = mgc1.company_name '
10460     ||'                       AND    mgc2.process_flag  <> '||G_ERROR_FLG
10461     ||'                       AND    NVL(mgc2.message_id,'||NULL_VALUE||')<>'||NULL_VALUE||')'
10462     ||' AND    mgc1.process_flag                     =  '||G_IN_PROCESS
10463     ||' AND    mgc1.sr_instance_code                 = :v_instance_code'
10464     ||' AND    NVL(mgc1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
10465 
10466 
10467       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10468 
10469     EXECUTE IMMEDIATE lv_sql_stmt
10470             USING     v_instance_code;
10471 
10472     lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10473                  (p_app_short_name    => 'MSC',
10474                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
10475                   p_message_text      => lv_message_text,
10476                   p_error_text        => lv_error_text);
10477 
10478     IF lv_return <> 0 THEN
10479       RAISE ex_logging_err;
10480     END IF;
10481 
10482     --Duplicate records check for the records whose source is other than XML
10483     --Different SQL is used because in XML we can identify the latest records
10484     --whereas in batch load we cannot.
10485 
10486     v_sql_stmt := 69;
10487 
10488     lv_sql_stmt :=
10489     ' UPDATE   msc_st_group_companies mgc1'
10490     ||' SET    process_flag = '||G_ERROR_FLG||','
10491     ||'        error_text   = '||''''||lv_message_text||''''
10492     ||' WHERE  EXISTS( SELECT 1 '
10493     ||'        FROM   msc_st_group_companies mgc2'
10494     ||'        WHERE  mgc2.group_name    = mgc1.group_name '
10495     ||'        AND    mgc2.company_name  = mgc1.company_name '
10496     ||'        AND    mgc2.process_flag  <> '||G_ERROR_FLG
10497     ||'        AND    NVL(mgc2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
10498     ||'        AND    mgc2.rowid         <> mgc1.rowid)'
10499     ||' AND    mgc1.process_flag                     = '||G_IN_PROCESS
10500     ||' AND    mgc1.sr_instance_code                 = :v_instance_code '
10501     ||' AND    NVL(mgc1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
10502 
10503 
10504       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10505 
10506     EXECUTE IMMEDIATE lv_sql_stmt
10507             USING     v_instance_code;
10508 
10509     lv_column_names :=
10510     '	GROUP_NAME            ||''~''||'
10511     ||'	 COMPANY_NAME         ||''~''||'
10512     ||'	 EFFECTIVE_DATE       ||''~''||'
10513     ||'	 DISABLE_DATE         ||''~''||'
10514     ||'	 CONTACT_USER_NAME    ||''~''||'
10515     ||'	 CONTEXT              ||''~''||'
10516     ||'	 ATTRIBUTE1           ||''~''||'
10517     ||'	 ATTRIBUTE2           ||''~''||'
10518     ||'	 ATTRIBUTE3           ||''~''||'
10519     ||'	 ATTRIBUTE4           ||''~''||'
10520     ||'	 ATTRIBUTE5           ||''~''||'
10521     ||'	 ATTRIBUTE6           ||''~''||'
10522     ||'	 ATTRIBUTE7           ||''~''||'
10523     ||'	 ATTRIBUTE8           ||''~''||'
10524     ||'	 ATTRIBUTE9           ||''~''||'
10525     ||'	 ATTRIBUTE10          ||''~''||'
10526     ||'	 ATTRIBUTE11          ||''~''||'
10527     ||'	 ATTRIBUTE12          ||''~''||'
10528     ||'	 ATTRIBUTE13          ||''~''||'
10529     ||'	 ATTRIBUTE14          ||''~''||'
10530     ||'	 ATTRIBUTE15          ||''~''||'
10531     ||'	 SR_INSTANCE_CODE';
10532 
10533     -- Dividing records in batches based on batchsize
10534 
10535     LOOP
10536       v_sql_stmt := 70;
10537       SELECT msc_st_batch_id_s.NEXTVAL
10538       INTO   lv_batch_id
10539       FROM   DUAL;
10540 
10541       v_sql_stmt := 71;
10542       lv_sql_stmt :=
10543       'UPDATE    msc_st_group_companies '
10544       ||' SET    batch_id                       = :lv_batch_id'
10545       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
10546       ||' AND    sr_instance_code               = :v_instance_code'
10547       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
10548       ||' AND    rownum                        <= '||v_batch_size;
10549 
10550 
10551         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10552 
10553       EXECUTE IMMEDIATE lv_sql_stmt
10554               USING     lv_batch_id,
10555                         v_instance_code;
10556 
10557       EXIT WHEN SQL%NOTFOUND ;
10558 
10559       -- Updation of WHO columns
10560 
10561       OPEN  c12(lv_batch_id);
10562       FETCH c12 BULK COLLECT INTO lb_rowid;
10563       CLOSE c12;
10564 
10565       v_sql_stmt := 72;
10566       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
10567       UPDATE msc_st_group_companies
10568       SET  st_transaction_id = msc_st_group_companies_s.NEXTVAL,
10569            last_update_date  = v_current_date,
10570            last_updated_by   = v_current_user,
10571            creation_date     = v_current_date,
10572            created_by        = v_current_user
10573       WHERE  rowid           = lb_rowid(j);
10574 
10575 
10576       -- Set the  message for no deletion is  allowed.
10577 
10578       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10579                      (p_app_short_name    => 'MSC',
10580                       p_error_code        => 'MSC_PP_NO_DELETION',
10581                       p_message_text      => lv_message_text,
10582                       p_error_text        => lv_error_text,
10583                       p_token1            => 'TABLE_NAME',
10584                       p_token_value1      => 'MSC_ST_GROUP_COMPANIES');
10585 
10586       IF lv_return <> 0 THEN
10587         RAISE ex_logging_err;
10588        END IF;
10589 
10590       --Deletion is not allowed on this table.
10591       v_sql_stmt := 73;
10592       lv_sql_stmt :=
10593       ' UPDATE     msc_st_group_companies '
10594       ||' SET      process_flag     ='||G_ERROR_FLG||','
10595       ||'          error_text       = '||''''||lv_message_text||''''
10596       ||' WHERE    deleted_flag     = '||SYS_YES
10597       ||' AND      process_flag     = '||G_IN_PROCESS
10598       ||' AND      batch_id         =  :lv_batch_id'
10599       ||' AND      sr_instance_code = :v_instance_code';
10600 
10601 
10602             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10603 
10604       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
10605 
10606       -- Set the message for deleted flag
10607 
10608       lv_return :=    MSC_ST_util.GET_ERROR_MESSAGE
10609                      (p_app_short_name    => 'MSC',
10610                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
10611                       p_message_text      => lv_message_text,
10612                       p_error_text        => lv_error_text,
10613                       p_token1            => 'COLUMN_NAME',
10614                       p_token_value1      => 'DELETED_FLAG',
10615                       p_token2            => 'DEFAULT_VALUE',
10616                       p_token_value2      =>  SYS_NO);
10617 
10618       IF lv_return <> 0 THEN
10619         RAISE ex_logging_err;
10620       END IF;
10621 
10622       -- Check DELETED_FLAG column has valid value or not and
10623       -- if not set it to SYS_NO
10624 
10625       lv_where_str :=
10626       ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
10627 
10628       lv_return := MSC_ST_util.LOG_ERROR
10629                     (p_table_name        => 'MSC_ST_GROUP_COMPANIES',
10630                      p_instance_code     => v_instance_code,
10631                      p_row               => lv_column_names,
10632                      p_severity          => G_SEV_WARNING,
10633                      p_message_text      => lv_message_text,
10634                      p_error_text        => lv_error_text,
10635                      p_batch_id          => lv_batch_id,
10636                      p_where_str         => lv_where_str,
10637                      p_col_name          => 'DELETED_FLAG',
10638                      p_debug             => v_debug,
10639                      p_default_value     => SYS_NO);
10640 
10641       IF lv_return <> 0 THEN
10642         RAISE ex_logging_err;
10643       END IF;
10644 
10645       -- Effective Date is a mandatory column in MSC_GROUP_COMPANIES , set error message
10646       lv_return :=
10647       MSC_ST_util.GET_ERROR_MESSAGE (p_app_short_name    => 'MSC',
10648                                          p_error_code        => 'MSC_PP_COL_VAL_NULL',
10649                	                         p_message_text      => lv_message_text,
10650                                    	 p_error_text        => lv_error_text,
10651                       	                 p_token1            => 'COLUMN_NAME',
10652                                          p_token_value1      => 'EFFECTIVE_DATE');
10653 
10654        IF lv_return <> 0 THEN
10655          RAISE ex_logging_err;
10656        END IF;
10657 
10658 
10659       v_sql_stmt := 74;
10660 
10661       lv_sql_stmt :=
10662       ' UPDATE  msc_st_group_companies '
10663       ||' SET   process_flag        = '||G_ERROR_FLG||','
10664       ||'       error_text          = '||''''||lv_message_text||''''
10665       ||' WHERE effective_date IS NULL  '
10666       ||' AND   batch_id            = :lv_batch_id'
10667       ||' AND   process_flag        = '||G_IN_PROCESS
10668       ||' AND   sr_instance_code    = :v_instance_code';
10669 
10670 
10671         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10672 
10673       EXECUTE IMMEDIATE lv_sql_stmt
10674               USING     lv_batch_id,
10675                         v_instance_code;
10676 
10677       -- Set the error message for  DISABLE_DATE should be greater than EFFECTIVE_DATE
10678 
10679       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10680                      (p_app_short_name    => 'MSC',
10681                       p_error_code        => 'MSC_PP_END_GT_ST_DATE',
10682                       p_message_text      => lv_message_text,
10683                       p_error_text        => lv_error_text);
10684 
10685       IF lv_return <> 0 THEN
10686         RAISE ex_logging_err;
10687       END IF;
10688 
10689       v_sql_stmt := 75;
10690 
10691       lv_sql_stmt :=
10692       ' UPDATE  msc_st_group_companies '
10693       ||' SET   process_flag        = '||G_ERROR_FLG||','
10694       ||'       error_text          = '||''''||lv_message_text||''''
10695       ||' WHERE effective_date      > disable_date'
10696       ||' AND   disable_date        IS NOT NULL'
10697       ||' AND   batch_id            = :lv_batch_id'
10698       ||' AND   process_flag        = '||G_IN_PROCESS
10699       ||' AND   sr_instance_code    = :v_instance_code';
10700 
10701 
10702         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10703 
10704 
10705       EXECUTE IMMEDIATE lv_sql_stmt
10706               USING     lv_batch_id,
10707                         v_instance_code;
10708 
10709       --DERIVE GROUP_ID
10710 
10711       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10712                      (p_app_short_name    => 'MSC',
10713                       p_error_code        => 'MSC_PP_INVALID_VALUE',
10714                       p_message_text      => lv_message_text,
10715                       p_error_text        => lv_error_text,
10716                       p_token1            => 'COLUMN_NAME',
10717                       p_token_value1      => 'GROUP_NAME');
10718 
10719 
10720 	 lv_return :=  MSC_ST_util.DERIVE_GROUP_ID
10721 	                     (p_table_name    =>'MSC_ST_GROUP_COMPANIES',
10722 	                      p_grp_col_name  => 'GROUP_NAME',
10723 	                      p_grp_col_id   => 'GROUP_ID',
10724 	                      p_instance_code => v_instance_code,
10725 	                      p_instance_id   => v_instance_id,
10726 	                      p_batch_id      => lv_batch_id,
10727 	                      p_debug         => v_debug,
10728 	                      p_severity      => G_SEV_ERROR,
10729 	                      p_error_text    => lv_error_text,
10730 	                      p_row           => lv_column_names);
10731 
10732 	    IF lv_return <> 0 THEN
10733 	        RAISE ex_logging_err;
10734 	    END IF;
10735 
10736 
10737 
10738       -- Only group owner or operator can update
10739 
10740      select fnd_global.user_id into lv_user_id from dual;
10741 
10742       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10743                      (p_app_short_name    => 'MSC',
10744                       p_error_code        => 'MSC_PP_INVALID_VALUE',
10745                       p_message_text      => lv_message_text,
10746                       p_error_text        => lv_error_text,
10747                       p_token1            => 'COLUMN_NAME',
10748                       p_token_value1      => 'Posting_party_id');
10749 
10750       IF lv_return <> 0 THEN
10751         RAISE ex_logging_err;
10752       END IF;
10753 
10754 -- changes
10755 
10756     IF NOT MSC_ST_util.IS_OPERATOR(lv_user_id) THEN
10757 
10758       v_sql_stmt := 76;
10759       lv_sql_stmt :=
10760       'UPDATE    msc_st_group_companies msg'
10761       ||' SET    process_flag  = '||G_ERROR_FLG||','
10762       ||'        error_text    = '||''''||lv_message_text||''''
10763       ||' WHERE  NOT EXISTS(SELECT 1 '
10764       ||'               FROM msc_groups mg'
10765       ||'               where  mg.group_owner_id = msg.posting_party_id '
10766       ||'               AND    mg.group_id       =  msg.group_id '
10767       ||'               UNION '
10768       ||'               SELECT 1 '
10769       ||'               FROM   msc_st_groups ms'
10770       ||'               where  ms.group_owner_id = msg.posting_party_id '
10771       ||'               AND    ms.group_id       =  msg.group_id '
10772       ||'               AND    ms.process_flag   = '||G_VALID ||')'
10773       ||' AND    batch_id         =  :lv_batch_id'
10774       ||' AND    sr_instance_code =  :v_instance_code'
10775       ||' AND    process_flag     = '||G_IN_PROCESS;
10776 
10777 
10778         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10779 
10780       EXECUTE IMMEDIATE lv_sql_stmt
10781               USING     lv_batch_id,
10782                         v_instance_code;
10783   END IF;
10784     --Derive company_id
10785 
10786       v_sql_stmt := 77;
10787       lv_sql_stmt :=
10788       'UPDATE msc_st_group_companies msg '
10789       ||' SET    company_id = ( SELECT mc.company_id '
10790       ||'        FROM   msc_companies mc '
10791       ||'        WHERE  mc.company_name = msg.company_name  '
10792       ||'        AND    NVL(mc.disable_date,sysdate+1) > sysdate) '
10793       ||' WHERE  msg.sr_instance_code  = :v_instance_code'
10794       ||' AND    msg.batch_id          = :lv_batch_id'
10795       ||' AND    msg.process_flag      = '||G_IN_PROCESS;
10796 
10797 
10798         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10799 
10800       EXECUTE IMMEDIATE lv_sql_stmt
10801               USING     v_instance_code,
10802                         lv_batch_id;
10803 
10804       -- Error out the invalid company Name
10805 
10806       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10807                      (p_app_short_name    => 'MSC',
10808                       p_error_code        => 'MSC_PP_INVALID_VALUE',
10809                       p_message_text      => lv_message_text,
10810                       p_error_text        => lv_error_text,
10811                       p_token1            => 'COLUMN_NAME',
10812                       p_token_value1      => 'COMPANY_NAME');
10813 
10814       IF lv_return <> 0 THEN
10815         RAISE ex_logging_err;
10816       END IF;
10817 
10818     --Derive contact_user_id
10819 
10820       v_sql_stmt := 78;
10821       lv_sql_stmt :=
10822       'UPDATE    msc_st_group_companies msg'
10823       ||' SET    contact_user_id  = (select user_id '
10824       ||'                        from   fnd_user fu'
10825       ||'                        where  UPPER(fu.user_name) =UPPER( msg.contact_user_name)) '
10826       ||' WHERE  batch_id         =  :lv_batch_id'
10827       ||' AND    sr_instance_code =  :v_instance_code'
10828       ||' AND    process_flag     = '||G_IN_PROCESS;
10829 
10830 
10831         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10832 
10833       EXECUTE IMMEDIATE lv_sql_stmt
10834               USING     lv_batch_id,
10835                         v_instance_code;
10836 
10837 -- Error out the invalid Contact User Name
10838 
10839       lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10840                      (p_app_short_name    => 'MSC',
10841                       p_error_code        => 'MSC_PP_INVALID_VALUE',
10842                       p_message_text      => lv_message_text,
10843                       p_error_text        => lv_error_text,
10844                       p_token1            => 'COLUMN_NAME',
10845                       p_token_value1      => 'CONTACT_USER_NAME');
10846 
10847       IF lv_return <> 0 THEN
10848         RAISE ex_logging_err;
10849       END IF;
10850 
10851       -- Update the record as errored out record
10852 
10853 	      v_sql_stmt := 79;
10854 	      lv_sql_stmt :=
10855 	      'UPDATE    msc_st_group_companies '
10856 	      ||' SET    process_flag  = '||G_ERROR_FLG||','
10857 	      ||'        error_text    = '||''''||lv_message_text||''''
10858 	      ||' WHERE  contact_user_id   IS NULL '
10859 	      ||' AND    batch_id         =  :lv_batch_id'
10860 	      ||' AND    sr_instance_code =  :v_instance_code'
10861 	      ||' AND    process_flag     = '||G_IN_PROCESS;
10862 
10863  lv_return := MSC_ST_util.GET_ERROR_MESSAGE
10864                      (p_app_short_name    => 'MSC',
10865                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
10866                       p_message_text      => lv_message_text,
10867                       p_error_text        => lv_error_text,
10868                       p_token1            => 'COLUMN_NAMES',
10869                       p_token_value1      => 'CONTACT_USER_NAME,COMPANY_NAME',
10870                       p_token2            => 'MASTER_TABLE',
10871                       p_token_value2      => 'MSC_COMPANY_USERS',
10872                       p_token3            => 'CHILD_TABLE',
10873                       p_token_value3      => 'MSC_ST_GROUP_COMPANIES');
10874 
10875     IF lv_return <> 0 THEN
10876         RAISE ex_logging_err;
10877     END IF;
10878 
10879     -- Populating valid Contact user id
10880 
10881 	    v_sql_stmt := 80;
10882 	    lv_sql_stmt :=
10883 	    '   UPDATE      msc_st_group_companies mgc'
10884 	    ||' SET         process_flag = '||G_ERROR_FLG||','
10885 	    ||'             error_text   = '||''''||lv_message_text||''''
10886 	    ||' WHERE       NOT EXISTS (SELECT 1 '
10887 	    ||'             FROM  msc_company_users mcu '
10888 	    ||'             WHERE mcu.company_id  = mgc.company_id'
10889 	    ||'             AND   mcu.user_id     = mgc.contact_user_id)'
10890 	    ||' AND mgc.sr_instance_code   = :v_instance_code'
10891 	    ||' AND mgc.batch_id           = :lv_batch_id'
10892 	    ||' AND mgc.process_flag       = '||G_IN_PROCESS ;
10893 
10894 
10895           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
10896 
10897     EXECUTE IMMEDIATE lv_sql_stmt
10898             USING     v_instance_code,
10899                       lv_batch_id;
10900 
10901       --Call to customised validation.
10902 
10903       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
10904         (ERRBUF         => lv_error_text,
10905          RETCODE        => lv_return,
10906          pBatchID       => lv_batch_id,
10907          pInstanceCode  => v_instance_code,
10908          pEntityName    => 'MSC_ST_GROUP_COMPANIES',
10909          pInstanceID    => v_instance_id);
10910 
10911       IF NVL(lv_return,0) <> 0 THEN
10912         RAISE ex_logging_err;
10913       END IF;
10914 
10915       -- Set the process flag as Valid and derive sr_instance_id
10916 
10917       lv_return := MSC_ST_util.SET_PROCESS_FLAG
10918                   (p_table_name     => 'MSC_ST_GROUP_COMPANIES',
10919                    p_instance_id    => v_instance_id,
10920                    p_instance_code  => v_instance_code,
10921                    p_process_flag   => G_VALID,
10922                    p_error_text     => lv_error_text,
10923                    p_debug          => v_debug,
10924                    p_batch_id       => lv_batch_id);
10925 
10926       IF lv_return <> 0 THEN
10927         RAISE ex_logging_err;
10928       END IF;
10929 
10930       -- Inserting all the errored out records into MSC_ERRORS
10931 
10932       lv_return := MSC_ST_util.LOG_ERROR
10933                    (p_table_name        => 'MSC_ST_GROUP_COMPANIES',
10934                     p_instance_code     => v_instance_code,
10935                     p_row               => lv_column_names,
10936                     p_severity          => G_SEV_ERROR,
10937                     p_error_text        => lv_error_text,
10938                     p_message_text      => NULL,
10939                     p_batch_id          => lv_batch_id);
10940 
10941 
10942       IF lv_return <> 0 THEN
10943         RAISE ex_logging_err;
10944       END IF;
10945 
10946       -- Updation of the record in msc_group_companies
10947       OPEN  c13(lv_batch_id);
10948       FETCH c13 BULK COLLECT INTO
10949                   lb_group_id,
10950                   lb_company_id,
10951                   lb_CONTACT_USER_ID,
10952                   lb_EFFECTIVE_DATE,
10953                   lb_DISABLE_DATE,
10954                   lb_CONTEXT,
10955                   lb_ATTRIBUTE1,
10956                   lb_ATTRIBUTE2,
10957                   lb_ATTRIBUTE3,
10958                   lb_ATTRIBUTE4,
10959                   lb_ATTRIBUTE5,
10960                   lb_ATTRIBUTE6,
10961                   lb_ATTRIBUTE7,
10962                   lb_ATTRIBUTE8,
10963                   lb_ATTRIBUTE9,
10964                   lb_ATTRIBUTE10,
10965                   lb_ATTRIBUTE11,
10966                   lb_ATTRIBUTE12,
10967                   lb_ATTRIBUTE13,
10968                   lb_ATTRIBUTE14,
10969                   lb_ATTRIBUTE15;
10970 
10971       IF c13%ROWCOUNT > 0  THEN
10972       v_sql_stmt := 81;
10973       FORALL j IN lb_GROUP_ID.FIRST..lb_GROUP_ID.LAST
10974 
10975         UPDATE msc_group_companies
10976         SET    contact_user	= lb_contact_user_id(j),
10977                effective_date	= lb_effective_date(j),
10978                disable_date	= lb_disable_date(j),
10979                last_update_date = v_current_date,
10980                last_updated_by	= v_current_user,
10981                context	        = lb_context(j),
10982                attribute1       = lb_attribute1(j),
10983                attribute2       = lb_attribute2(j),
10984                attribute3       = lb_attribute3(j),
10985                attribute4       = lb_attribute4(j),
10986                attribute5       = lb_attribute5(j),
10987                attribute6       = lb_attribute6(j),
10988                attribute7       = lb_attribute7(j),
10989                attribute8       = lb_attribute8(j),
10990                attribute9       = lb_attribute9(j),
10991                attribute10      = lb_attribute10(j),
10992                attribute11      = lb_attribute11(j),
10993                attribute12      = lb_attribute12(j),
10994                attribute13      = lb_attribute13(j),
10995                attribute14      = lb_attribute14(j),
10996                attribute15      = lb_attribute15(j)
10997         WHERE  group_id         = lb_group_id(J)
10998         AND    company_id       = lb_company_id(J);
10999 	end if;
11000 	CLOSE c13;
11001 
11002       --Inserts the valid records into the ODS table, Since the collection program
11003       --doesn't collect this entity incrementally and whereras post-processor always
11004       --runs in the incremental mode.
11005 
11006       v_sql_stmt := 82;
11007       lv_sql_stmt :=
11008 		'INSERT INTO MSC_GROUP_COMPANIES'
11009 		||'(	GROUP_ID	,'
11010 		||'	COMPANY_ID	,'
11011 		||'	EFFECTIVE_DATE	,'
11012 		||'	DISABLE_DATE	,'
11013 		||'	CONTACT_USER	,'
11014 		||'	CREATION_DATE	,'
11015 		||'	CREATED_BY	,'
11016 		||'	LAST_UPDATE_DATE,'
11017 		||'	LAST_UPDATED_BY	,'
11018 		||'	LAST_UPDATE_LOGIN,'
11019 		||'	CONTEXT	,'
11020 		||'	ATTRIBUTE1	,'
11021 		||'	ATTRIBUTE2	,'
11022 		||'	ATTRIBUTE3	,'
11023 		||'	ATTRIBUTE4	,'
11024 		||'	ATTRIBUTE5	,'
11025 		||'	ATTRIBUTE6	,'
11026 		||'	ATTRIBUTE7	,'
11027 		||'	ATTRIBUTE8	,'
11028 		||'	ATTRIBUTE9	,'
11029 		||'	ATTRIBUTE10	,'
11030 		||'	ATTRIBUTE11	,'
11031 		||'	ATTRIBUTE12	,'
11032 		||'	ATTRIBUTE13	,'
11033 		||'	ATTRIBUTE14	,'
11034 		||'	ATTRIBUTE15	)'
11035 		||'     SELECT'
11036 		||'	GROUP_ID	,'
11037 		||'	COMPANY_ID	,'
11038 		||'	EFFECTIVE_DATE	,'
11039 		||'	DISABLE_DATE	,'
11040 		||'	CONTACT_USER_id ,'
11041 		||'	CREATION_DATE	,'
11042 		||'	CREATED_BY	,'
11043 		||'	LAST_UPDATE_DATE,'
11044 		||'	LAST_UPDATED_BY	,'
11045 		||'	LAST_UPDATE_LOGIN,'
11046 		||'	CONTEXT	,'
11047 		||'	ATTRIBUTE1	,'
11048 		||'	ATTRIBUTE2	,'
11049 		||'	ATTRIBUTE3	,'
11050 		||'	ATTRIBUTE4	,'
11051 		||'	ATTRIBUTE5	,'
11052 		||'	ATTRIBUTE6	,'
11053 		||'	ATTRIBUTE7	,'
11054 		||'	ATTRIBUTE8	,'
11055 		||'	ATTRIBUTE9	,'
11056 		||'	ATTRIBUTE10	,'
11057 		||'	ATTRIBUTE11	,'
11058 		||'	ATTRIBUTE12	,'
11059 		||'	ATTRIBUTE13	,'
11060 		||'	ATTRIBUTE14	,'
11061 		||'	ATTRIBUTE15	'
11062 		||' FROM   msc_st_group_companies msg'
11063 		||' WHERE  sr_instance_code = :sr_instance_code'
11064 		||' AND    batch_id         = :lv_batch_id'
11065 		||' AND    process_flag     = '||G_VALID
11066                 ||' AND    NOT EXISTS( SELECT 1 '
11067                 ||'                    FROM   msc_group_companies mgc'
11068                 ||'                    WHERE  mgc.group_id   = msg.group_id '
11069                 ||'                    AND    mgc.company_id = msg.company_id)';
11070 
11071 
11072         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11073 
11074       EXECUTE IMMEDIATE lv_sql_stmt
11075               USING     v_instance_code,
11076                         lv_batch_id;
11077 
11078       COMMIT;
11079    END LOOP;
11080 
11081    MODEL_ORG_SUPPLIER_CUSTOMER;
11082 
11083   EXCEPTION
11084     WHEN too_many_rows THEN
11085       lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_TRADING_PARTNERS '||'('
11086                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
11087         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
11088 
11089       ROLLBACK;
11090 
11091     WHEN ex_logging_err THEN
11092         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
11093 
11094       ROLLBACK;
11095 
11096     WHEN OTHERS THEN
11097       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_TRADING_PARTNERS '||'('
11098                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
11099         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
11100 
11101       ROLLBACK;
11102 
11103   END LOAD_TRADING_PARTNERS ;
11104 
11105    /*==========================================================================+
11106   | DESCRIPTION  : This procedure performs validation and loads data into the |
11107   |                table msc_st_calendar_assignments.                         |
11108   +==========================================================================*/
11109 
11110 
11111   PROCEDURE LOAD_CALENDAR_ASSIGNMENTS IS
11112   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
11113   lb_rowid           RowidTab;
11114   lv_return          NUMBER;
11115   lv_error_text      VARCHAR2(250);
11116   lv_where_str       VARCHAR2(5000);
11117   lv_sql_stmt        VARCHAR2(5000);
11118   lv_column_names    VARCHAR2(5000);
11119   lv_batch_id        msc_st_calendar_assignments.batch_id%TYPE;
11120   lv_message_text    msc_errors.error_text%TYPE;
11121 
11122   ex_logging_err     EXCEPTION;
11123 
11124   CURSOR    c1(p_batch_id NUMBER) IS
11125     SELECT  rowid
11126     FROM    msc_st_calendar_assignments
11127     WHERE   process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
11128     AND     batch_id         = p_batch_id
11129     AND     sr_instance_code = v_instance_code;
11130 
11131  BEGIN
11132 
11133     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11134                  (p_app_short_name    => 'MSC',
11135                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
11136                   p_message_text      => lv_message_text,
11137                   p_error_text        => lv_error_text);
11138 
11139     IF lv_return <> 0 THEN
11140       RAISE ex_logging_err;
11141     END IF;
11142 
11143 
11144     --Duplicate records check for the records whose source is XML
11145     v_sql_stmt := 01;
11146     lv_sql_stmt :=
11147     ' UPDATE   msc_st_calendar_assignments msp1'
11148     ||' SET    process_flag = '||G_ERROR_FLG||','
11149     ||'        error_text   = '||''''||lv_message_text||''''
11150     ||' WHERE  message_id <  (SELECT MAX(message_id)'
11151     ||'        FROM  msc_st_calendar_assignments msp2'
11152     ||'        WHERE msp2.sr_instance_code  = msp1.sr_instance_code'
11153     ||'        AND   NVL(msp2.company_name,  '   ||''''||NULL_CHAR||''''||') = '
11154     ||'              NVL(msp1.company_name,  '   ||''''||NULL_CHAR||''''||')'
11155     ||'        AND   NVL(msp2.organization_code,  '   ||''''||NULL_CHAR||''''||') = '
11156     ||'              NVL(msp1.organization_code,  '   ||''''||NULL_CHAR||''''||')'
11157     ||'        AND   NVL(msp2.partner_name,  '   ||''''||NULL_CHAR||''''||') = '
11158     ||'              NVL(msp1.partner_name,  '   ||''''||NULL_CHAR||''''||')'
11159     ||'        AND   NVL(msp2.partner_site_code,  '   ||''''||NULL_CHAR||''''||') = '
11160     ||'              NVL(msp1.partner_site_code,  '   ||''''||NULL_CHAR||''''||')'
11161     ||'        AND   NVL(msp2.carrier_partner_code,  '   ||''''||NULL_CHAR||''''||') = '
11162     ||'              NVL(msp1.carrier_partner_code,  '   ||''''||NULL_CHAR||''''||')'
11163     ||'        AND   NVL(msp2.ship_method_code,  '   ||''''||NULL_CHAR||''''||') = '
11164     ||'              NVL(msp1.ship_method_code,  '   ||''''||NULL_CHAR||''''||')'
11165     ||'        AND   msp2.calendar_type = msp1.calendar_type'
11166     ||'        AND   msp2.calendar_code = msp1.calendar_code'
11167     ||'        AND   NVL(msp2.partner_type,'||NULL_VALUE||') = NVL(msp1.partner_type,'||NULL_VALUE||')'
11168     ||'        AND   msp2.association_type = msp1.association_type'
11169     ||'        AND   msp2.process_flag = '||G_IN_PROCESS
11170     ||'        AND   NVL(msp2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
11171     ||' AND    msp1.process_flag     = '||G_IN_PROCESS
11172     ||' AND    msp1.sr_instance_code = :v_instance_code'
11173     ||' AND    NVL(msp1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
11174 
11175 
11176 
11177       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11178 
11179     EXECUTE IMMEDIATE lv_sql_stmt
11180             USING     v_instance_code;
11181 
11182    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11183                  (p_app_short_name    => 'MSC',
11184                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
11185                   p_message_text      => lv_message_text,
11186                   p_error_text        => lv_error_text);
11187 
11188     IF lv_return <> 0 THEN
11189       RAISE ex_logging_err;
11190     END IF;
11191 
11192     --Duplicate records check for the records whose source is other than XML
11193     --Different SQL is used because in XML we can identify the latest records
11194     --whereas in batch load we cannot.
11195     v_sql_stmt := 02;
11196     lv_sql_stmt :=
11197     ' UPDATE  msc_st_calendar_assignments msp1'
11198     ||' SET   process_flag  = '||G_ERROR_FLG||','
11199     ||'       error_text    = '||''''||lv_message_text||''''
11200     ||' WHERE EXISTS( SELECT 1 '
11201     ||'         FROM  msc_st_calendar_assignments msp2'
11202     ||'         WHERE msp2.sr_instance_code  = msp1.sr_instance_code'
11203     ||'        AND   NVL(msp2.company_name,  '   ||''''||NULL_CHAR||''''||') = '
11204     ||'               NVL(msp1.company_name,  '   ||''''||NULL_CHAR||''''||')'
11205     ||'        AND   NVL(msp2.organization_code,  '   ||''''||NULL_CHAR||''''||') = '
11206     ||'              NVL(msp1.organization_code,  '   ||''''||NULL_CHAR||''''||')'
11207     ||'        AND   NVL(msp2.partner_name,  '   ||''''||NULL_CHAR||''''||') = '
11208     ||'              NVL(msp1.partner_name,  '   ||''''||NULL_CHAR||''''||')'
11209     ||'        AND   NVL(msp2.partner_site_code,  '   ||''''||NULL_CHAR||''''||') = '
11210     ||'              NVL(msp1.partner_site_code,  '   ||''''||NULL_CHAR||''''||')'
11211     ||'        AND   NVL(msp2.carrier_partner_code,  '   ||''''||NULL_CHAR||''''||') = '
11212     ||'              NVL(msp1.carrier_partner_code,  '   ||''''||NULL_CHAR||''''||')'
11213     ||'        AND   NVL(msp2.ship_method_code,  '   ||''''||NULL_CHAR||''''||') = '
11214     ||'              NVL(msp1.ship_method_code,  '   ||''''||NULL_CHAR||''''||')'
11215     ||'        AND   msp2.calendar_type = msp1.calendar_type'
11216     ||'        AND   msp2.calendar_code = msp1.calendar_code'
11217     ||'        AND   NVL(msp2.partner_type,'||NULL_VALUE||') = NVL(msp1.partner_type,'||NULL_VALUE||')'
11218     ||'        AND   msp2.association_type = msp1.association_type'
11219     ||'        AND   msp2.process_flag = '||G_IN_PROCESS
11220     ||'        AND   NVL(msp2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
11221     ||'        AND   msp2.rowid <> msp1.rowid)'
11222     ||' AND   msp1.process_flag     = '||G_IN_PROCESS
11223     ||' AND   msp1.sr_instance_code = :v_instance_code'
11224     ||' AND   NVL(msp1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
11225 
11226 
11227 
11228       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11229 
11230     EXECUTE IMMEDIATE lv_sql_stmt
11231             USING     v_instance_code;
11232 
11233     lv_column_names :=
11234     'CALENDAR_CODE             ||''~''||'
11235     ||'ORGANIZATION_CODE       ||''~''||'
11236     ||'ASSOCIATION_TYPE        ||''~''||'
11237     ||'CALENDAR_TYPE           ||''~''||'
11238     ||'PARTNER_NAME            ||''~''||'
11239     ||'PARTNER_SITE_CODE       ||''~''||'
11240     ||'CARRIER_PARTNER_CODE    ||''~''||'
11241     ||'SR_INSTANCE_CODE        ||''~''||'
11242     ||'COMPANY_NAME';
11243 
11244     LOOP
11245       v_sql_stmt := 03;
11246       SELECT msc_st_batch_id_s.NEXTVAL
11247       INTO   lv_batch_id
11248       FROM   dual;
11249 
11250       v_sql_stmt := 04;
11251       lv_sql_stmt :=
11252       'UPDATE msc_st_calendar_assignments '
11253       ||' SET    batch_id  = :lv_batch_id'
11254       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
11255       ||' AND    sr_instance_code               = :v_instance_code'
11256       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
11257       ||' AND    rownum                        <= '||v_batch_size;
11258 
11259 
11260         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11261 
11262       EXECUTE IMMEDIATE lv_sql_stmt
11263               USING     lv_batch_id,
11264                         v_instance_code;
11265 
11266       EXIT WHEN SQL%NOTFOUND;
11267 
11268       OPEN c1(lv_batch_id);
11269       FETCH c1 BULK COLLECT INTO lb_rowid;
11270       CLOSE c1;
11271 
11272       v_sql_stmt := 05;
11273       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
11274       UPDATE msc_st_calendar_assignments
11275       SET    st_transaction_id   = msc_st_calendar_assignments_s.NEXTVAL,
11276              refresh_id          = v_refresh_id,
11277              last_update_date    = v_current_date,
11278              last_updated_by     = v_current_user,
11279              creation_date       = v_current_date,
11280              created_by          = v_current_user
11281       WHERE  rowid           = lb_rowid(j);
11282 
11283       -- set the error message
11284 
11285       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11286                      (p_app_short_name    => 'MSC',
11287                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
11288                       p_message_text      => lv_message_text,
11289                       p_error_text        => lv_error_text,
11290                       p_token1            => 'COLUMN_NAME',
11291                       p_token_value1      => 'DELETED_FLAG',
11292                       p_token2            => 'DEFAULT_VALUE',
11293                       p_token_value2      => SYS_NO);
11294 
11295       IF lv_return <> 0 THEN
11296         RAISE ex_logging_err;
11297       END IF;
11298 
11299       lv_where_str :=
11300       ' AND NVL(deleted_flag,'||NULL_VALUE||') '
11301       ||' NOT IN(1,2)';
11302       --Log a warning for those records where the deleted_flag has a value other
11303       --than SYS_NO
11304       lv_return := MSC_ST_UTIL.LOG_ERROR
11305                      (p_table_name        => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11306                       p_instance_code     => v_instance_code,
11307                       p_row               => lv_column_names,
11308                       p_severity          => G_SEV_WARNING,
11309                       p_message_text      => lv_message_text,
11310                       p_error_text        => lv_error_text,
11311                       p_batch_id          => lv_batch_id,
11312                       p_where_str         => lv_where_str,
11313                       p_col_name          => 'DELETED_FLAG',
11314                       p_debug             => v_debug,
11315                       p_default_value     => SYS_NO);
11316 
11317       IF lv_return <> 0 THEN
11318         RAISE ex_logging_err;
11319       END IF;
11320 
11321       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11322                      (p_app_short_name    => 'MSC',
11323                       p_error_code        => 'MSC_PP_INVALID_VALUE',
11324                       p_message_text      => lv_message_text,
11325                       p_error_text        => lv_error_text,
11326                       p_token1            => 'COLUMN_NAME',
11327                       p_token_value1      => 'CALENDAR_TYPE');
11328 
11329       IF lv_return <> 0 THEN
11330         RAISE ex_logging_err;
11331       END IF;
11332 
11333       lv_sql_stmt :=
11334       'UPDATE    msc_st_calendar_assignments '
11335       ||' SET    process_flag  = '||G_ERROR_FLG||','
11336       ||'        error_text    = '||''''||lv_message_text||''''
11337       ||' WHERE  NVL(calendar_type,'||''''||NULL_CHAR||''''||') '
11338       ||'        NOT IN(''RECEIVING'',''SHIPPING'', ''CARRIER'' )'
11339       ||' AND    batch_id         =  :lv_batch_id'
11340       ||' AND    sr_instance_code =  :v_instance_code'
11341       ||' AND    process_flag     = '||G_IN_PROCESS;
11342 
11343 
11344         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11345 
11346       EXECUTE IMMEDIATE lv_sql_stmt
11347                USING     lv_batch_id,
11348                         v_instance_code;
11349 
11350 
11351     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11352                      (p_app_short_name    => 'MSC',
11353                       p_error_code        => 'MSC_PP_INVALID_VALUE',
11354                       p_message_text      => lv_message_text,
11355                       p_error_text        => lv_error_text,
11356                       p_token1            => 'COLUMN_NAME',
11357                       p_token_value1      => 'ASSOCIATION_TYPE');
11358 
11359       IF lv_return <> 0 THEN
11360         RAISE ex_logging_err;
11361       END IF;
11362 
11363       -- error out the records where association_type is not correct
11364 
11365       lv_sql_stmt :=
11366       'UPDATE    msc_st_calendar_assignments '
11367       ||' SET    process_flag  = '||G_ERROR_FLG||','
11368       ||'        error_text    = '||''''||lv_message_text||''''
11369       ||' WHERE  association_type '
11370       ||'        NOT IN (SELECT lookup_code FROM mfg_lookups where lookup_type = ''MSC_UI_ASSIGNMENT_TYPE'')'
11371       ||' AND    batch_id         =  :lv_batch_id'
11372       ||' AND    sr_instance_code =  :v_instance_code'
11373       ||' AND    process_flag     = '||G_IN_PROCESS;
11374 
11375 
11376         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11377 
11378       EXECUTE IMMEDIATE lv_sql_stmt
11379                USING     lv_batch_id,
11380                         v_instance_code;
11381 
11382 
11383      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11384                      (p_app_short_name    => 'MSC',
11385                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
11386                       p_message_text      => lv_message_text,
11387                       p_error_text        => lv_error_text,
11388                       p_token1            => 'COLUMN_NAME',
11389                       p_token_value1      => 'PARTNER_TYPE');
11390 
11391       IF lv_return <> 0 THEN
11392         RAISE ex_logging_err;
11393       END IF;
11394 
11395       -- Error out records where partner_type NOT in (1,2,3,4)
11396       v_sql_stmt := 10;
11397       lv_sql_stmt :=
11398       'UPDATE    msc_st_calendar_assignments'
11399       ||' SET    process_flag       = '||G_ERROR_FLG||','
11400       ||'        error_text         = '||''''||lv_message_text||''''
11401       ||' WHERE  sr_instance_code   = :v_instance_code'
11402       ||' AND    batch_id           = :lv_batch_id'
11403       ||' AND    process_flag       = '||G_IN_PROCESS
11404       ||' AND    partner_type NOT IN (1,2,3,4)' ;
11405 
11406 
11407         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11408       EXECUTE IMMEDIATE lv_sql_stmt
11409               USING     v_instance_code,
11410                         lv_batch_id;
11411 
11412     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11413                      (p_app_short_name    => 'MSC',
11414                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
11415                       p_message_text      => lv_message_text,
11416                       p_error_text        => lv_error_text,
11417                       p_token1            => 'COLUMN_NAME',
11418                       p_token_value1      => 'ASSOCIATION_LEVEL');
11419 
11420       IF lv_return <> 0 THEN
11421         RAISE ex_logging_err;
11422       END IF;
11423 
11424       -- Error out records where ASSOCIATION_LEVEL NOT in (1,2,3,4)
11425       v_sql_stmt := 11;
11426       lv_sql_stmt :=
11427       'UPDATE    msc_st_calendar_assignments'
11428       ||' SET    process_flag       = '||G_ERROR_FLG||','
11429       ||'        error_text         = '||''''||lv_message_text||''''
11430       ||' WHERE  sr_instance_code   = :v_instance_code'
11431       ||' AND    batch_id           = :lv_batch_id'
11432       ||' AND    process_flag       = '||G_IN_PROCESS
11433       ||' AND    association_level NOT IN (1,2,3,4)' ;
11434 
11435 
11436         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11437       EXECUTE IMMEDIATE lv_sql_stmt
11438               USING     v_instance_code,
11439                         lv_batch_id;
11440 
11441 
11442   lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11443                      (p_app_short_name    => 'MSC',
11444                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
11445                       p_message_text      => lv_message_text,
11446                       p_error_text        => lv_error_text,
11447                       p_token1            => 'COLUMN_NAME',
11448                       p_token_value1      => 'CALENDAR_CODE');
11449 
11450       IF lv_return <> 0 THEN
11451         RAISE ex_logging_err;
11452       END IF;
11453 
11454       -- Error out records where calendar_code is NULL
11455 
11456       lv_sql_stmt :=
11457       'UPDATE    msc_st_calendar_assignments '
11458       ||' SET    process_flag                  = '||G_ERROR_FLG||','
11459       ||'         error_text   = '||''''||lv_message_text||''''
11460       ||' WHERE  sr_instance_code              = :v_instance_code'
11461       ||' AND    batch_id                      = :lv_batch_id'
11462       ||' AND    process_flag                  = '||G_IN_PROCESS
11463       ||' AND    NVL(calendar_code,    '||''''||NULL_CHAR||''''||')'
11464       ||'        =                      '||''''||NULL_CHAR||'''' ;
11465 
11466 
11467         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11468       EXECUTE IMMEDIATE lv_sql_stmt
11469               USING     v_instance_code,
11470                         lv_batch_id;
11471 
11472 
11473    -- We need to check correct association_type is attached to that particular record.
11474    -- For example if association_type is CARRIER_VENDOR_SITE, then for that particular record partner_name, partner_site_code and carrier_code should not be null
11475 
11476 
11477    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11478                      (p_app_short_name    => 'MSC',
11479                       p_error_code        => 'MSC_PP_INVAL_ASSO_COMB',
11480                       p_message_text      => lv_message_text,
11481                       p_error_text        => lv_error_text,
11482                       p_token1            => 'COLUMN_NAME',
11483                       p_token_value1      => 'PARTNER_NAME OR PARTNER_SITE_CODE OR'
11484                                              ||'CARRIER_PARTNER_CODE');
11485 
11486 
11487       IF lv_return <> 0 THEN
11488         RAISE ex_logging_err;
11489       END IF;
11490 
11491       lv_sql_stmt :=
11492       'UPDATE    msc_st_calendar_assignments '
11493       ||' SET    process_flag            = '||G_ERROR_FLG||','
11494       ||'        error_text   = '||''''||lv_message_text||''''
11495       ||' WHERE  sr_instance_code              = :v_instance_code'
11496       ||' AND    batch_id                      = :lv_batch_id'
11497       ||' AND    process_flag                  = '||G_IN_PROCESS
11498       ||' AND   association_type IN (10,11)'
11499       ||' AND   (NVL(PARTNER_NAME, '||''''||NULL_CHAR||''''||') '
11500       ||'        =                 '||''''||NULL_CHAR||''''
11501       ||' OR     NVL(PARTNER_SITE_CODE, '||''''||NULL_CHAR||''''||') '
11502       ||'        =                 '||''''||NULL_CHAR||''''
11503       ||' OR    NVL(CARRIER_PARTNER_CODE, '||''''||NULL_CHAR||''''||') '
11504       ||'        =                 '||''''||NULL_CHAR||''''||') ';
11505 
11506 
11507             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11508 
11509          EXECUTE IMMEDIATE lv_sql_stmt
11510          USING     v_instance_code,
11511                    lv_batch_id;
11512 
11513 
11514 
11515    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11516                      (p_app_short_name    => 'MSC',
11517                       p_error_code        => 'MSC_PP_INVAL_ASSO_COMB',
11518                       p_message_text      => lv_message_text,
11519                       p_error_text        => lv_error_text,
11520                       p_token1            => 'COLUMN_NAME',
11521                       p_token_value1      => 'PARTNER_NAME OR CARRIER_PARTNER_CODE');
11522 
11523 
11524 
11525       IF lv_return <> 0 THEN
11526         RAISE ex_logging_err;
11527       END IF;
11528 
11529       lv_sql_stmt :=
11530       'UPDATE    msc_st_calendar_assignments '
11531       ||' SET    process_flag            = '||G_ERROR_FLG||','
11532       ||'        error_text   = '||''''||lv_message_text||''''
11533       ||' WHERE  sr_instance_code              = :v_instance_code'
11534       ||' AND    batch_id                      = :lv_batch_id'
11535       ||' AND    process_flag                  = '||G_IN_PROCESS
11536       ||' AND    association_type IN (7,8)'
11537       ||' AND   (NVL(PARTNER_NAME, '||''''||NULL_CHAR||''''||') '
11538       ||'        =                 '||''''||NULL_CHAR||''''
11539       ||' OR    NVL(CARRIER_PARTNER_CODE, '||''''||NULL_CHAR||''''||') '
11540       ||'        =                 '||''''||NULL_CHAR||''''||') ';
11541 
11542 
11543             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11544 
11545       EXECUTE IMMEDIATE lv_sql_stmt
11546       USING     v_instance_code,
11547                 lv_batch_id;
11548 
11549 
11550       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11551                      (p_app_short_name    => 'MSC',
11552                       p_error_code        => 'MSC_PP_INVAL_ASSO_COMB',
11553                       p_message_text      => lv_message_text,
11554                       p_error_text        => lv_error_text,
11555                       p_token1            => 'COLUMN_NAME',
11556                       p_token_value1      => 'ORGANIZATION_CODE OR CARRIER_PARTNER_CODE');
11557 
11558 
11559       IF lv_return <> 0 THEN
11560         RAISE ex_logging_err;
11561       END IF;
11562 
11563       lv_sql_stmt :=
11564       'UPDATE    msc_st_calendar_assignments '
11565       ||' SET    process_flag            = '||G_ERROR_FLG||','
11566       ||'        error_text   = '||''''||lv_message_text||''''
11567       ||' WHERE  sr_instance_code              = :v_instance_code'
11568       ||' AND    batch_id                      = :lv_batch_id'
11569       ||' AND    process_flag                  = '||G_IN_PROCESS
11570       ||' AND    association_type = 9 '
11571       ||' AND   (NVL(ORGANIZATION_CODE, '||''''||NULL_CHAR||''''||') '
11572       ||'        =                 '||''''||NULL_CHAR||''''
11573       ||' OR    NVL(CARRIER_PARTNER_CODE, '||''''||NULL_CHAR||''''||') '
11574       ||'        =                 '||''''||NULL_CHAR||''''||') ';
11575 
11576 
11577             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11578 
11579       EXECUTE IMMEDIATE lv_sql_stmt
11580       USING     v_instance_code,
11581                 lv_batch_id;
11582 
11583 
11584      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11585                      (p_app_short_name    => 'MSC',
11586                       p_error_code        => 'MSC_PP_INVAL_ASSO_COMB',
11587                       p_message_text      => lv_message_text,
11588                       p_error_text        => lv_error_text,
11589                       p_token1            => 'COLUMN_NAME',
11590                       p_token_value1      => 'PARTNER_NAME OR PARTNER_SITE_CODE');
11591 
11592 
11593       IF lv_return <> 0 THEN
11594         RAISE ex_logging_err;
11595       END IF;
11596 
11597       lv_sql_stmt :=
11598       'UPDATE    msc_st_calendar_assignments '
11599       ||' SET    process_flag            = '||G_ERROR_FLG||','
11600       ||'        error_text   = '||''''||lv_message_text||''''
11601       ||' WHERE  sr_instance_code              = :v_instance_code'
11602       ||' AND    batch_id                      = :lv_batch_id'
11603       ||' AND    process_flag                  = '||G_IN_PROCESS
11604       ||' AND    association_type IN( 5,6 )'
11605       ||' AND   (NVL(PARTNER_NAME, '||''''||NULL_CHAR||''''||') '
11606       ||'        =                 '||''''||NULL_CHAR||''''
11607       ||' OR    NVL(PARTNER_SITE_CODE, '||''''||NULL_CHAR||''''||') '
11608       ||'        =                 '||''''||NULL_CHAR||''''||') ';
11609 
11610 
11611             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11612 
11613       EXECUTE IMMEDIATE lv_sql_stmt
11614       USING     v_instance_code,
11615                 lv_batch_id;
11616 
11617 
11618       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11619                      (p_app_short_name    => 'MSC',
11620                       p_error_code        => 'MSC_PP_INVAL_ASSO_COMB',
11621                       p_message_text      => lv_message_text,
11622                       p_error_text        => lv_error_text,
11623                       p_token1            => 'COLUMN_NAME',
11624                       p_token_value1      => 'PARTNER_NAME OR ORGANIZATION_CODE OR CARRIER_PARTNER_CODE');
11625 
11626 
11627       IF lv_return <> 0 THEN
11628         RAISE ex_logging_err;
11629       END IF;
11630 
11631       lv_sql_stmt :=
11632       'UPDATE    msc_st_calendar_assignments '
11633       ||' SET    process_flag            = '||G_ERROR_FLG||','
11634       ||'        error_text   = '||''''||lv_message_text||''''
11635       ||' WHERE  sr_instance_code              = :v_instance_code'
11636       ||' AND    batch_id                      = :lv_batch_id'
11637       ||' AND    process_flag                  = '||G_IN_PROCESS
11638       ||' AND    ((association_type IN( 1,2 )'
11639       ||' AND    NVL(PARTNER_NAME, '||''''||NULL_CHAR||''''||') '
11640       ||'        =                 '||''''||NULL_CHAR||''''||') '
11641       ||' OR    (association_type =3 '
11642       ||' AND    NVL(ORGANIZATION_CODE, '||''''||NULL_CHAR||''''||') '
11643       ||'        =                 '||''''||NULL_CHAR||''''||') '
11644       ||' OR    (association_type =4 '
11645       ||' AND    NVL(CARRIER_PARTNER_CODE, '||''''||NULL_CHAR||''''||') '
11646       ||'        =                 '||''''||NULL_CHAR||''''||')) '   ;
11647 
11648 
11649             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11650 
11651       EXECUTE IMMEDIATE lv_sql_stmt
11652       USING     v_instance_code,
11653                 lv_batch_id;
11654 
11655 
11656 
11657       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11658                      (p_app_short_name    => 'MSC',
11659                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
11660                       p_message_text      => lv_message_text,
11661                       p_error_text        => lv_error_text,
11662                       p_token1            => 'COLUMN_NAMES',
11663                       p_token_value1      => 'CALENDAR_CODE',
11664                       p_token2            => 'MASTER_TABLE',
11665                       p_token_value2      => 'MSC_CALENDARS',
11666                       p_token3            => 'CHILD_TABLE',
11667                       p_token_value3      => 'MSC_ST_CALENDAR_ASSIGNMENTS');
11668       IF lv_return <> 0 THEN
11669         RAISE ex_logging_err;
11670       END IF;
11671 
11672       lv_sql_stmt :=
11673       'UPDATE    msc_st_calendar_assignments   msca'
11674       ||' SET    process_flag             =  '||G_ERROR_FLG||','
11675       ||'         error_text   = '||''''||lv_message_text||''''
11676       ||' WHERE  NOT EXISTS ( SELECT 1 '
11677       ||'        FROM   msc_calendars mc '
11678       ||'        WHERE  mc.calendar_code  = msca.calendar_code '
11679       ||'        AND    mc.sr_instance_id = :v_instance_id )'
11680       ||' AND    sr_instance_code         = :v_instance_code'
11681       ||' AND    batch_id                 = :lv_batch_id'
11682       ||' AND    process_flag             = '||G_IN_PROCESS;
11683 
11684 
11685 
11686         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
11687       EXECUTE IMMEDIATE lv_sql_stmt
11688               USING     v_instance_id,
11689                         v_instance_code,
11690                         lv_batch_id;
11691 
11692 
11693      -- Derive partner_id (supplier)
11694 
11695      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11696                      (p_app_short_name    => 'MSC',
11697                       p_error_code        => 'MSC_PP_INVALID_VALUE',
11698                       p_message_text      => lv_message_text,
11699                       p_error_text        => lv_error_text,
11700                       p_token1            => 'COLUMN_NAME',
11701                       p_token_value1      => 'PARTNER_NAME');
11702 
11703       IF lv_return <> 0 THEN
11704         RAISE ex_logging_err;
11705       END IF;
11706 
11707       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
11708                      (p_table_name       => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11709                       p_org_partner_name => 'PARTNER_NAME',
11710                       p_org_partner_id   => 'PARTNER_ID',
11711                       p_instance_code    => v_instance_code,
11712                       p_partner_type     => G_VENDOR,
11713                       p_error_text       => lv_error_text,
11714                       p_batch_id         => lv_batch_id,
11715                       p_severity         => G_SEV_ERROR,
11716                       p_message_text     => lv_message_text,
11717                       p_debug            => v_debug,
11718                       p_row              => lv_column_names);
11719 
11720       IF lv_return <> 0 THEN
11721         RAISE ex_logging_err;
11722       END IF;
11723 
11724   -- Derive partner_id (Customer)
11725 
11726      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11727                      (p_app_short_name    => 'MSC',
11728                       p_error_code        => 'MSC_PP_INVALID_VALUE',
11729                       p_message_text      => lv_message_text,
11730                       p_error_text        => lv_error_text,
11731                       p_token1            => 'COLUMN_NAME',
11732                       p_token_value1      => 'PARTNER_NAME');
11733 
11734       IF lv_return <> 0 THEN
11735         RAISE ex_logging_err;
11736       END IF;
11737 
11738       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
11739                      (p_table_name       => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11740                       p_org_partner_name => 'PARTNER_NAME',
11741                       p_org_partner_id   => 'PARTNER_ID',
11742                       p_instance_code    => v_instance_code,
11743                       p_partner_type     => G_CUSTOMER,
11744                       p_error_text       => lv_error_text,
11745                       p_batch_id         => lv_batch_id,
11746                       p_severity         => G_SEV_ERROR,
11747                       p_message_text     => lv_message_text,
11748                       p_debug            => v_debug,
11749                       p_row              => lv_column_names);
11750 
11751       IF lv_return <> 0 THEN
11752         RAISE ex_logging_err;
11753       END IF;
11754 
11755       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11756                      (p_app_short_name    => 'MSC',
11757                       p_error_code        => 'MSC_PP_INVALID_VALUE',
11758                       p_message_text      => lv_message_text,
11759                       p_error_text        => lv_error_text,
11760                       p_token1            => 'COLUMN_NAME',
11761                       p_token_value1      => 'ORGANIZATION_CODE');
11762 
11763       IF lv_return <> 0 THEN
11764         RAISE ex_logging_err;
11765       END IF;
11766 
11767     --Derive Organization_id
11768       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
11769                      (p_table_name       => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11770                       p_org_partner_name => 'ORGANIZATION_CODE',
11771                       p_org_partner_id   => 'ORGANIZATION_ID',
11772                       p_instance_code    => v_instance_code,
11773                       p_partner_type     => G_ORGANIZATION,
11774                       p_error_text       => lv_error_text,
11775                       p_batch_id         => lv_batch_id,
11776                       p_severity         => G_SEV3_ERROR,
11777                       p_message_text     => lv_message_text,
11778                       p_debug            => v_debug,
11779                       p_row              => lv_column_names);
11780 
11781       IF lv_return <> 0 THEN
11782         RAISE ex_logging_err;
11783       END IF;
11784 
11785       -- Derive carrier_partner_id (Carrier)
11786 
11787      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11788                      (p_app_short_name    => 'MSC',
11789                       p_error_code        => 'MSC_PP_INVALID_VALUE',
11790                       p_message_text      => lv_message_text,
11791                       p_error_text        => lv_error_text,
11792                       p_token1            => 'COLUMN_NAME',
11793                       p_token_value1      => 'CARRIER_PARTNER_CODE');
11794 
11795       IF lv_return <> 0 THEN
11796         RAISE ex_logging_err;
11797       END IF;
11798 
11799       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
11800                      (p_table_name       => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11801                       p_org_partner_name => 'CARRIER_PARTNER_CODE',
11802                       p_org_partner_id   => 'CARRIER_PARTNER_ID',
11803                       p_instance_code    => v_instance_code,
11804                       p_partner_type     => G_CARRIER,
11805                       p_error_text       => lv_error_text,
11806                       p_batch_id         => lv_batch_id,
11807                       p_severity         => G_SEV3_ERROR,
11808                       p_message_text     => lv_message_text,
11809                       p_debug            => v_debug,
11810                       p_row              => lv_column_names);
11811 
11812       IF lv_return <> 0 THEN
11813         RAISE ex_logging_err;
11814       END IF;
11815 
11816       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11817                      (p_app_short_name    => 'MSC',
11818                       p_error_code        => 'MSC_PP_INVALID_VALUE',
11819                       p_message_text      => lv_message_text,
11820                       p_error_text        => lv_error_text,
11821                       p_token1            => 'COLUMN_NAME',
11822                       p_token_value1      => 'PARTNER_SITE_CODE');
11823 
11824       IF lv_return <> 0 THEN
11825         RAISE ex_logging_err;
11826       END IF;
11827 
11828       --Derive partner_site_id (Supplier)
11829        lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
11830                      (p_table_name        => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11831                       p_partner_name      => 'PARTNER_NAME',
11832                       p_partner_site_code => 'PARTNER_SITE_CODE',
11833                       p_partner_site_id   => 'PARTNER_SITE_ID',
11834                       p_instance_code     => v_instance_code,
11835                       p_partner_type      => G_VENDOR,
11836                       p_error_text        => lv_error_text,
11837                       p_batch_id          => lv_batch_id,
11838                       p_severity          => G_SEV3_ERROR,
11839                       p_message_text      => lv_message_text,
11840                       p_debug             => v_debug,
11841                       p_row               => lv_column_names);
11842 
11843       IF lv_return <> 0 THEN
11844         RAISE ex_logging_err;
11845       END IF;
11846 
11847    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11848                      (p_app_short_name    => 'MSC',
11849                       p_error_code        => 'MSC_PP_INVALID_VALUE',
11850                       p_message_text      => lv_message_text,
11851                       p_error_text        => lv_error_text,
11852                       p_token1            => 'COLUMN_NAME',
11853                       p_token_value1      => 'PARTNER_SITE_CODE');
11854 
11855       IF lv_return <> 0 THEN
11856         RAISE ex_logging_err;
11857       END IF;
11858 
11859       --Derive partner_site_id (Customer)
11860        lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
11861                      (p_table_name        => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11862                       p_partner_name      => 'PARTNER_NAME',
11863                       p_partner_site_code => 'PARTNER_SITE_CODE',
11864                       p_partner_site_id   => 'PARTNER_SITE_ID',
11865                       p_instance_code     => v_instance_code,
11866                       p_partner_type      => G_CUSTOMER,
11867                       p_error_text        => lv_error_text,
11868                       p_batch_id          => lv_batch_id,
11869                       p_severity          => G_SEV3_ERROR,
11870                       p_message_text      => lv_message_text,
11871                       p_debug             => v_debug,
11872                       p_row               => lv_column_names);
11873 
11874       IF lv_return <> 0 THEN
11875         RAISE ex_logging_err;
11876       END IF;
11877 
11878       --Call to customised validation.
11879       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
11880         (ERRBUF         => lv_error_text,
11881          RETCODE        => lv_return,
11882          pBatchID       => lv_batch_id,
11883          pInstanceCode  => v_instance_code,
11884          pEntityName    => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11885          pInstanceID    => v_instance_id);
11886 
11887       IF NVL(lv_return,0) <> 0 THEN
11888         RAISE ex_logging_err;
11889       END IF;
11890 
11891       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
11892                   (p_table_name     => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11893                    p_instance_id    => v_instance_id,
11894                    p_instance_code  => v_instance_code,
11895                    p_process_flag   => G_VALID,
11896                    p_error_text     => lv_error_text,
11897                    p_debug          => v_debug,
11898                    p_batch_id       => lv_batch_id);
11899 
11900       IF lv_return <> 0 THEN
11901         RAISE ex_logging_err;
11902       END IF;
11903 
11904       lv_return := MSC_ST_UTIL.LOG_ERROR
11905                    (p_table_name        => 'MSC_ST_CALENDAR_ASSIGNMENTS',
11906                     p_instance_code     => v_instance_code,
11907                     p_row               => lv_column_names,
11908                     p_severity          => G_SEV_ERROR,
11909                     p_message_text      => NULL,
11910                     p_error_text        => lv_error_text,
11911                     p_debug             => v_debug,
11912                     p_batch_id          => lv_batch_id);
11913 
11914       IF lv_return <> 0 THEN
11915         RAISE ex_logging_err;
11916       END IF;
11917       COMMIT;
11918     END LOOP;
11919 
11920    EXCEPTION
11921 
11922     WHEN too_many_rows THEN
11923       lv_error_text := substr('MSC_CL_PRE_PROCESS.MSC_ST_CALENDAR_ASSIGNMENTS'||'('
11924                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
11925       ROLLBACK ;
11926 
11927     WHEN ex_logging_err THEN
11928         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
11929       ROLLBACK;
11930 
11931     WHEN OTHERS THEN
11932       lv_error_text    := substr('MSC_CL_PRE_PROCESS.MSC_ST_CALENDAR_ASSIGNMENTS '||'('
11933                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
11934         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
11935       ROLLBACK;
11936 
11937   END LOAD_CALENDAR_ASSIGNMENTS;
11938 
11939 
11940   /*==========================================================================+
11941   | DESCRIPTION  : This procedure performs validation and loads data into the |
11942   |                table msc_st_planners.                                     |
11943   +==========================================================================*/
11944   PROCEDURE LOAD_planners IS
11945   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
11946   lb_rowid           RowidTab;
11947   lv_return          NUMBER;
11948   lv_error_text      VARCHAR2(250);
11949   lv_where_str       VARCHAR2(5000);
11950   lv_sql_stmt        VARCHAR2(5000);
11951   lv_column_names    VARCHAR2(5000);
11952   lv_batch_id        msc_st_planners.batch_id%TYPE;
11953   lv_message_text    msc_errors.error_text%TYPE;
11954 
11955   ex_logging_err     EXCEPTION;
11956 
11957   CURSOR    c1(p_batch_id NUMBER) IS
11958     SELECT  rowid
11959     FROM    msc_st_planners
11960     WHERE   process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
11961     AND     batch_id         = p_batch_id
11962     AND     sr_instance_code = v_instance_code;
11963 
11964     CURSOR c2(p_batch_id NUMBER)IS
11965 	   SELECT
11966 	   x.PLANNER_CODE,
11967 	   x.ORGANIZATION_ID,
11968 	   x.DESCRIPTION,
11969 	   x.DISABLE_DATE,
11970 	   x.ELECTRONIC_MAIL_ADDRESS,
11971 	   x.EMPLOYEE_ID,
11972 	   x.CURRENT_EMPLOYEE_FLAG,
11973 	   x.USER_NAME,
11974 	   x.sr_instance_id,
11975 	   x.LAST_UPDATE_DATE,
11976 	   x.LAST_UPDATED_BY,
11977 	   x.CREATION_DATE,
11978 	   x.CREATED_BY,
11979 	   x.deleted_flag
11980 	  FROM MSC_ST_PLANNERS x
11981 	 WHERE x.sr_instance_code = v_instance_code
11982 	 and   x.batch_id         =p_batch_id
11983 	 and   x.process_flag     = G_VALID;
11984 
11985   BEGIN
11986 
11987     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
11988                  (p_app_short_name    => 'MSC',
11989                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
11990                   p_message_text      => lv_message_text,
11991                   p_error_text        => lv_error_text);
11992 
11993     IF lv_return <> 0 THEN
11994       RAISE ex_logging_err;
11995     END IF;
11996 
11997     --Duplicate records check for the records whose source is XML
11998     v_sql_stmt := 01;
11999     lv_sql_stmt :=
12000     ' UPDATE   msc_st_planners msp1'
12001     ||' SET    process_flag = '||G_ERROR_FLG||','
12002     ||'        error_text   = '||''''||lv_message_text||''''
12003     ||' WHERE  message_id <  (SELECT MAX(message_id)'
12004     ||'        FROM  msc_st_planners msp2'
12005     ||'        WHERE msp2.sr_instance_code  = msp1.sr_instance_code'
12006     ||'        AND   NVL(msp2.company_name,  '   ||''''||NULL_CHAR||''''||') = '
12007     ||'              NVL(msp1.company_name,  '   ||''''||NULL_CHAR||''''||')'
12008     ||'        AND   msp2.organization_code = msp1.organization_code'
12009     ||'        AND   msp2.planner_code = msp1.planner_code'
12010     ||'        AND   msp2.process_flag = '||G_IN_PROCESS
12011     ||'        AND   NVL(msp2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
12012     ||' AND    msp1.process_flag     = '||G_IN_PROCESS
12013     ||' AND    msp1.sr_instance_code = :v_instance_code'
12014     ||' AND    NVL(msp1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
12015 
12016 
12017 
12018       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12019 
12020     EXECUTE IMMEDIATE lv_sql_stmt
12021             USING     v_instance_code;
12022 
12023     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12024                  (p_app_short_name    => 'MSC',
12025                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
12026                   p_message_text      => lv_message_text,
12027                   p_error_text        => lv_error_text);
12028 
12029     IF lv_return <> 0 THEN
12030       RAISE ex_logging_err;
12031     END IF;
12032 
12033     --Duplicate records check for the records whose source is other than XML
12034     --Different SQL is used because in XML we can identify the latest records
12035     --whereas in batch load we cannot.
12036     v_sql_stmt := 02;
12037     lv_sql_stmt :=
12038     ' UPDATE  msc_st_planners msp1'
12039     ||' SET   process_flag  = '||G_ERROR_FLG||','
12040     ||'       error_text    = '||''''||lv_message_text||''''
12041     ||' WHERE EXISTS( SELECT 1 '
12042     ||'         FROM  msc_st_planners msp2'
12043     ||'         WHERE msp2.sr_instance_code  = msp1.sr_instance_code'
12044     ||'         AND   NVL(msp2.company_name,  '   ||''''||NULL_CHAR||''''||') = '
12045     ||'               NVL(msp1.company_name,  '   ||''''||NULL_CHAR||''''||')'
12046     ||'         AND   msp2.organization_code = msp1.organization_code'
12047     ||'         AND   msp2.planner_code = msp1.planner_code'
12048     ||'         AND   msp2.process_flag = '||G_IN_PROCESS
12049     ||'         AND   NVL(msp2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
12050     ||'         AND   msp2.rowid <> msp1.rowid)'
12051     ||' AND   msp1.process_flag     = '||G_IN_PROCESS
12052     ||' AND   msp1.sr_instance_code = :v_instance_code'
12053     ||' AND   NVL(msp1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
12054 
12055 
12056 
12057       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12058 
12059     EXECUTE IMMEDIATE lv_sql_stmt
12060             USING     v_instance_code;
12061 
12062     lv_column_names :=
12063     'PLANNER_CODE              ||''~''||'
12064     ||'ORGANIZATION_CODE       ||''~''||'
12065     ||'DESCRIPTION             ||''~''||'
12066     ||'DISABLE_DATE            ||''~''||'
12067     ||'ELECTRONIC_MAIL_ADDRESS ||''~''||'
12068     ||'CURRENT_EMPLOYEE_FLAG   ||''~''||'
12069     ||'USER_NAME               ||''~''||'
12070     ||'SR_INSTANCE_CODE        ||''~''||'
12071     ||'COMPANY_NAME';
12072 
12073 
12074     LOOP
12075       v_sql_stmt := 03;
12076       SELECT msc_st_batch_id_s.NEXTVAL
12077       INTO   lv_batch_id
12078       FROM   dual;
12079 
12080       v_sql_stmt := 04;
12081       lv_sql_stmt :=
12082       'UPDATE msc_st_planners '
12083       ||' SET    batch_id  = :lv_batch_id'
12084       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
12085       ||' AND    sr_instance_code               = :v_instance_code'
12086       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
12087       ||' AND    rownum                        <= '||v_batch_size;
12088 
12089 
12090         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12091 
12092       EXECUTE IMMEDIATE lv_sql_stmt
12093               USING     lv_batch_id,
12094                         v_instance_code;
12095 
12096       EXIT WHEN SQL%NOTFOUND;
12097 
12098       OPEN c1(lv_batch_id);
12099       FETCH c1 BULK COLLECT INTO lb_rowid;
12100       CLOSE c1;
12101 
12102       v_sql_stmt := 05;
12103       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
12104       UPDATE msc_st_planners
12105       SET    st_transaction_id   = msc_st_planners_s.NEXTVAL,
12106              refresh_id          = v_refresh_id,
12107              last_update_date    = v_current_date,
12108              last_updated_by     = v_current_user,
12109              creation_date       = v_current_date,
12110              created_by          = v_current_user
12111       WHERE  rowid           = lb_rowid(j);
12112 
12113  /*     --Deletion is not allowed on this table.
12114       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12115                      (p_app_short_name    => 'MSC',
12116                       p_error_code        => 'MSC_PP_NO_DELETION',
12117                       p_message_text      => lv_message_text,
12118                       p_error_text        => lv_error_text,
12119                       p_token1            => 'TABLE_NAME',
12120                       p_token_value1      => 'MSC_ST_PLANNERS');
12121 
12122       IF lv_return <> 0 THEN
12123         RAISE ex_logging_err;
12124       END IF;
12125 
12126       v_sql_stmt := 06;
12127       lv_sql_stmt :=
12128       'UPDATE    msc_st_planners '
12129       ||' SET    process_flag        = '||G_ERROR_FLG||','
12130       ||'        error_text          = '||''''||lv_message_text||''''
12131       ||' WHERE  deleted_flag        = '||SYS_YES
12132       ||' AND    process_flag        = '||G_IN_PROCESS
12133       ||' AND    batch_id            =  :lv_batch_id'
12134       ||' AND    sr_instance_code    = :v_instance_code';
12135 
12136 
12137         msc_st_util.log_message(lv_sql_stmt);
12138       END IF;
12139 
12140       EXECUTE IMMEDIATE lv_sql_stmt
12141               USING     lv_batch_id,
12142                         v_instance_code;
12143   */
12144 
12145       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12146                      (p_app_short_name    => 'MSC',
12147                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
12148                       p_message_text      => lv_message_text,
12149                       p_error_text        => lv_error_text,
12150                       p_token1            => 'COLUMN_NAME',
12151                       p_token_value1      => 'DELETED_FLAG',
12152                       p_token2            => 'DEFAULT_VALUE',
12153                       p_token_value2      => SYS_NO);
12154 
12155       IF lv_return <> 0 THEN
12156         RAISE ex_logging_err;
12157       END IF;
12158 
12159       lv_where_str :=
12160       ' AND NVL(deleted_flag,'||NULL_VALUE||') '
12161       ||' NOT IN(1,2)';
12162       --Log a warning for those records where the deleted_flag has a value other
12163       --than SYS_NO
12164       lv_return := MSC_ST_UTIL.LOG_ERROR
12165                      (p_table_name        => 'MSC_ST_PLANNERS',
12166                       p_instance_code     => v_instance_code,
12167                       p_row               => lv_column_names,
12168                       p_severity          => G_SEV_WARNING,
12169                       p_message_text      => lv_message_text,
12170                       p_error_text        => lv_error_text,
12171                       p_batch_id          => lv_batch_id,
12172                       p_where_str         => lv_where_str,
12173                       p_col_name          => 'DELETED_FLAG',
12174                       p_debug             => v_debug,
12175                       p_default_value     => SYS_NO);
12176 
12177       IF lv_return <> 0 THEN
12178         RAISE ex_logging_err;
12179       END IF;
12180 
12181       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12182                      (p_app_short_name    => 'MSC',
12183                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
12184                       p_message_text      => lv_message_text,
12185                       p_error_text        => lv_error_text,
12186                       p_token1            => 'COLUMN_NAME',
12187                       p_token_value1      => 'PLANNER_CODE');
12188 
12189       IF lv_return <> 0 THEN
12190         RAISE ex_logging_err;
12191       END IF;
12192 
12193       v_sql_stmt := 07;
12194       lv_sql_stmt :=
12195       'UPDATE    msc_st_planners '
12196       ||' SET    process_flag  = '||G_ERROR_FLG||','
12197       ||'        error_text    = '||''''||lv_message_text||''''
12198       ||' WHERE  NVL(planner_code,  '||''''||NULL_CHAR||''''||') '
12199       ||'        =               '||''''||NULL_CHAR||''''
12200       ||' AND    batch_id         =  :lv_batch_id'
12201       ||' AND    sr_instance_code =  :v_instance_code'
12202       ||' AND    process_flag     = '||G_IN_PROCESS;
12203 
12204 
12205         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12206 
12207       EXECUTE IMMEDIATE lv_sql_stmt
12208               USING     lv_batch_id,
12209                         v_instance_code;
12210 
12211       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12212                      (p_app_short_name    => 'MSC',
12213                       p_error_code        => 'MSC_PP_INVALID_VALUE',
12214                       p_message_text      => lv_message_text,
12215                       p_error_text        => lv_error_text,
12216                       p_token1            => 'COLUMN_NAME',
12217                       p_token_value1      => 'CURRENT_EMPLOYEE_FLAG');
12218 
12219       IF lv_return <> 0 THEN
12220         RAISE ex_logging_err;
12221       END IF;
12222 
12223       v_sql_stmt := 07;
12224       lv_sql_stmt :=
12225       'UPDATE    msc_st_planners '
12226       ||' SET    process_flag  = '||G_ERROR_FLG||','
12227       ||'        error_text    = '||''''||lv_message_text||''''
12228       ||' WHERE  NVL(current_employee_flag,'||NULL_VALUE||') NOT IN(1,2) '
12229       ||' AND    batch_id         =  :lv_batch_id'
12230       ||' AND    sr_instance_code =  :v_instance_code'
12231       ||' AND    process_flag     = '||G_IN_PROCESS;
12232 
12233 
12234         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12235 
12236       EXECUTE IMMEDIATE lv_sql_stmt
12237                USING     lv_batch_id,
12238                         v_instance_code;
12239 
12240 
12241       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12242                      (p_app_short_name    => 'MSC',
12243                       p_error_code        => 'MSC_PP_INVALID_VALUE',
12244                       p_message_text      => lv_message_text,
12245                       p_error_text        => lv_error_text,
12246                       p_token1            => 'COLUMN_NAME',
12247                       p_token_value1      => 'USER_NAME');
12248 
12249       IF lv_return <> 0 THEN
12250         RAISE ex_logging_err;
12251       END IF;
12252 
12253       v_sql_stmt := 07;
12254       lv_sql_stmt :=
12255       'UPDATE    msc_st_planners msp'
12256       ||' SET    process_flag  = '||G_ERROR_FLG||','
12257       ||'        error_text    = '||''''||lv_message_text||''''
12258       ||' WHERE  NOT EXISTS(SELECT 1 '
12259       ||'        FROM fnd_user fu'
12260       ||'        where  fu.user_name =UPPER(msp.user_name))'
12261       ||' AND    batch_id         =  :lv_batch_id'
12262       ||' AND    sr_instance_code =  :v_instance_code'
12263       ||' AND    process_flag     = '||G_IN_PROCESS;
12264 
12265 
12266         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12267 
12268 
12269       EXECUTE IMMEDIATE lv_sql_stmt
12270               USING     lv_batch_id,
12271                         v_instance_code;
12272 
12273       -- Set the message
12274 
12275      lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
12276                       (p_app_short_name    => 'MSC',
12277                        p_error_code        => 'MSC_PP_INVALID_VALUE',
12278                        p_message_text      => lv_message_text,
12279                        p_error_text        => lv_error_text,
12280                        p_token1            => 'COLUMN_NAME',
12281                        p_token_value1      => 'ORGANIZATION_CODE');
12282 
12283       IF lv_return <> 0 THEN
12284         RAISE ex_logging_err;
12285       END IF;
12286 
12287      -- Derive organization_id
12288 
12289     lv_return :=  MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
12290                      (p_table_name       => 'MSC_ST_PLANNERS',
12291                       p_org_partner_name  => 'ORGANIZATION_CODE',
12292                       p_org_partner_id    => 'ORGANIZATION_ID',
12293                       p_instance_code     => v_instance_code,
12294                       p_partner_type      => G_ORGANIZATION,
12295                       p_severity          => G_SEV_ERROR,
12296                       p_error_text        => lv_error_text,
12297                       p_batch_id          => lv_batch_id,
12298                       p_debug             => v_debug,
12299                       p_message_text      => lv_message_text);
12300 
12301     IF lv_return <> 0 THEN
12302         RAISE ex_logging_err;
12303     END IF;
12304 /*
12305       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12306                      (p_app_short_name    => 'MSC',
12307                       p_error_code        => 'MSC_PP_NO_UPDATION',
12308                       p_message_text      => lv_message_text,
12309                       p_error_text        => lv_error_text,
12310                       p_token1            => 'TABLE_NAME',
12311                       p_token_value1      => 'MSC_ST_PLANNERS');
12312 
12313       IF lv_return <> 0 THEN
12314         RAISE ex_logging_err;
12315       END IF;
12316 
12317       --Updation is not allowed on this entity(this is done to make the
12318       --Post-Processor in sync with the collection program).
12319       v_sql_stmt := 09;
12320       lv_sql_stmt :=
12321       ' UPDATE   msc_st_planners msp1'
12322       ||' SET    process_flag     = '||G_ERROR_FLG||','
12323       ||'        error_text       = '||''''||lv_message_text||''''
12324       ||' WHERE  process_flag     = '||G_IN_PROCESS
12325       ||' AND    batch_id         =  :lv_batch_id'
12326       ||' AND    sr_instance_code =  :v_instance_code'
12327       ||' AND    EXISTS'
12328       ||'        (SELECT 1 FROM msc_planners msp2'
12329       ||'        WHERE   msp2.organization_id      = msp1.organization_id'
12330       ||'        AND     msp2.planner_code         = msp1.planner_code'
12331       ||'        AND     msp2.sr_instance_id       = ' ||v_instance_id ||')';
12332 
12333 
12334         msc_st_util.log_message(lv_sql_stmt);
12335       END IF;
12336 
12337       EXECUTE IMMEDIATE lv_sql_stmt
12338               USING     lv_batch_id,
12339                         v_instance_code;
12340 
12341 */
12342     --Derive employee_id
12343 
12344       v_sql_stmt := 07;
12345       lv_sql_stmt :=
12346       'UPDATE    msc_st_planners msp'
12347       ||' SET    employee_id  = (select employee_id '
12348       ||'                        from   fnd_user fu'
12349       ||'                        where  fu.user_name = UPPER(msp.user_name)) '
12350       ||' WHERE  batch_id         =  :lv_batch_id'
12351       ||' AND    sr_instance_code =  :v_instance_code'
12352       ||' AND    process_flag     = '||G_IN_PROCESS;
12353 
12354 
12355         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12356 
12357       EXECUTE IMMEDIATE lv_sql_stmt
12358               USING     lv_batch_id,
12359                         v_instance_code;
12360 
12361       --Call to customised validation.
12362       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
12363         (ERRBUF         => lv_error_text,
12364          RETCODE        => lv_return,
12365          pBatchID       => lv_batch_id,
12366          pInstanceCode  => v_instance_code,
12367          pEntityName    => 'MSC_ST_PLANNERS',
12368          pInstanceID    => v_instance_id);
12369 
12370       IF NVL(lv_return,0) <> 0 THEN
12371         RAISE ex_logging_err;
12372       END IF;
12373 
12374       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
12375                   (p_table_name     => 'MSC_ST_PLANNERS',
12376                    p_instance_id    => v_instance_id,
12377                    p_instance_code  => v_instance_code,
12378                    p_process_flag   => G_VALID,
12379                    p_error_text     => lv_error_text,
12380                    p_debug          => v_debug,
12381                    p_batch_id       => lv_batch_id);
12382 
12383       IF lv_return <> 0 THEN
12384         RAISE ex_logging_err;
12385       END IF;
12386 
12387       lv_return := MSC_ST_UTIL.LOG_ERROR
12388                    (p_table_name        => 'MSC_ST_PLANNERS',
12389                     p_instance_code     => v_instance_code,
12390                     p_row               => lv_column_names,
12391                     p_severity          => G_SEV_ERROR,
12392                     p_message_text      => NULL,
12393                     p_error_text        => lv_error_text,
12394                     p_debug             => v_debug,
12395                     p_batch_id          => lv_batch_id);
12396 
12397       IF lv_return <> 0 THEN
12398         RAISE ex_logging_err;
12399       END IF;
12400 
12401      --Inserts the valid records into the ODS table, Since the collection program
12402      --doesn't collect this entity incrementally and whereras post-processor always
12403      --runs in the incremental mode.
12404 
12405      v_sql_stmt := 10;
12406     FOR c_rec IN c2(lv_batch_id) LOOP
12407 
12408   BEGIN
12409 
12410   if (c_rec.deleted_flag= SYS_YES) then
12411     DELETE from MSC_PLANNERS x where
12412     x.PLANNER_CODE  = c_rec.PLANNER_CODE
12413     AND x.ORGANIZATION_ID = c_rec.ORGANIZATION_ID
12414     AND x.SR_INSTANCE_ID = v_instance_id
12415     AND c_rec.deleted_flag= SYS_YES;
12416 
12417 
12418   else
12419 
12420      UPDATE MSC_PLANNERS x set
12421      DESCRIPTION =c_rec.DESCRIPTION,
12422      DISABLE_DATE =c_rec.DISABLE_DATE,
12423      ELECTRONIC_MAIL_ADDRESS =c_rec.ELECTRONIC_MAIL_ADDRESS,
12424      EMPLOYEE_ID =c_rec.EMPLOYEE_ID,
12425      CURRENT_EMPLOYEE_FLAG =c_rec.CURRENT_EMPLOYEE_FLAG,
12426      USER_NAME =c_rec.USER_NAME,
12427      LAST_UPDATE_DATE = c_rec.LAST_UPDATE_DATE,
12428      LAST_UPDATED_BY = c_rec.LAST_UPDATED_BY,
12429      CREATION_DATE = c_rec.CREATION_DATE,
12430      CREATED_BY = c_rec.CREATED_BY
12431      where x.PLANNER_CODE  = c_rec.PLANNER_CODE
12432      and x.ORGANIZATION_ID = c_rec.ORGANIZATION_ID
12433      and x.SR_INSTANCE_ID = v_instance_id;
12434 
12435 
12436    IF SQL%NOTFOUND THEN
12437 
12438      INSERT INTO MSC_PLANNERS
12439      ( PLANNER_CODE,
12440        ORGANIZATION_ID,
12441        DESCRIPTION,
12442        DISABLE_DATE,
12443        ELECTRONIC_MAIL_ADDRESS,
12444        EMPLOYEE_ID,
12445        CURRENT_EMPLOYEE_FLAG,
12446        USER_NAME,
12447        SR_INSTANCE_ID,
12448        LAST_UPDATE_DATE,
12449        LAST_UPDATED_BY,
12450        CREATION_DATE,
12451        CREATED_BY)
12452        VALUES
12453        (
12454        c_rec.PLANNER_CODE,
12455        c_rec.ORGANIZATION_ID,
12456        c_rec.DESCRIPTION,
12457        c_rec.DISABLE_DATE,
12458        c_rec.ELECTRONIC_MAIL_ADDRESS,
12459        c_rec.EMPLOYEE_ID,
12460        c_rec.CURRENT_EMPLOYEE_FLAG,
12461        c_rec.USER_NAME,
12462        v_instance_id,
12463        c_rec.LAST_UPDATE_DATE,
12464        c_rec.LAST_UPDATED_BY,
12465        c_rec.CREATION_DATE,
12466        c_rec.CREATED_BY
12467        );
12468    END IF;
12469 end if;
12470 
12471   EXCEPTION
12472     WHEN OTHERS THEN
12473     lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_PLANNERS'||
12474                        'Error in Inserting or Updating into MSC_PLANNERS'|| SQLERRM, 1, 240);
12475         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
12476       ROLLBACK;
12477   END;
12478 END LOOP;
12479 
12480       UPDATE FND_USER y
12481       SET    EMAIL_ADDRESS = (select  ELECTRONIC_MAIL_ADDRESS
12482                               from    msc_st_planners z
12483                                WHERE   UPPER(z.USER_NAME) = y.USER_NAME
12484                               AND     z.process_flag     = G_VALID
12485                               AND     z.CURRENT_EMPLOYEE_FLAG = 1
12486                               AND     z.EMPLOYEE_ID IS NOT NULL
12487                               AND     z.ELECTRONIC_MAIL_ADDRESS IS NOT NULL
12488                               AND     z.SR_INSTANCE_ID = v_instance_id
12489                               AND     rownum=1)
12490       WHERE  exists (SELECT 1
12491              from   msc_st_planners x
12492              WHERE  x.USER_NAME = y.USER_NAME
12493              AND    x.CURRENT_EMPLOYEE_FLAG = 1
12494              AND    x.EMPLOYEE_ID IS NOT NULL
12495              AND    x.ELECTRONIC_MAIL_ADDRESS IS NOT NULL
12496              AND    x.SR_INSTANCE_ID = v_instance_id
12497              AND    x.batch_id       = lv_batch_id);
12498 
12499 
12500       COMMIT;
12501      END LOOP;
12502 
12503   EXCEPTION
12504     WHEN ex_logging_err THEN
12505         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
12506       ROLLBACK;
12507 
12508     WHEN OTHERS THEN
12509       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_PLANNERS'||'('
12510                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
12511         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
12512       ROLLBACK;
12513 
12514   END LOAD_PLANNERS;
12515 
12516 
12517 /*==========================================================================+
12518 | DESCRIPTION  : This procedure validates the data for inter org shipping   |
12519 |                methods for table MSC_ST_INTERORG_SHIP_METHODS             |
12520 +==========================================================================*/
12521   PROCEDURE LOAD_SHIP_METHODS IS
12522 
12523   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
12524   lb_rowid          RowidTab; --bulk collects rowid
12525   lv_return         NUMBER;
12526   lv_batch_id       NUMBER;
12527   lv_error_text     VARCHAR2(250);
12528   lv_where_str      VARCHAR2(5000);
12529   lv_sql_stmt       VARCHAR2(5000);
12530   lv_column_names   VARCHAR2(5000);     --stores concatenated column names
12531   lv_message_text   msc_errors.error_text%TYPE;
12532 
12533   ex_logging_err        EXCEPTION;
12534 
12535   CURSOR c1(p_batch_id NUMBER) IS
12536     SELECT rowid
12537     FROM   msc_st_interorg_ship_methods
12538     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
12539     AND    sr_instance_code          = v_instance_code
12540     AND    batch_id                  = p_batch_id;
12541 
12542   BEGIN
12543 
12544     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12545                  (p_app_short_name    => 'MSC',
12546                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
12547                   p_message_text      => lv_message_text,
12548                   p_error_text        => lv_error_text);
12549 
12550     IF lv_return <> 0 THEN
12551       RAISE ex_logging_err;
12552     END IF;
12553 
12554 
12555     -- duplicate key validation changed for the bug fix 2748525.
12556     --Duplicate records check for the records whose source is other than XML
12557     --Different SQL is used because in XML we can identify the latest records
12558     --whereas in batch load we cannot.
12559 
12560     v_sql_stmt := 01;
12561     lv_sql_stmt :=
12562     'UPDATE    msc_st_interorg_ship_methods msis1 '
12563     ||' SET    process_flag = '||G_ERROR_FLG||','
12564     ||'         error_text   = '||''''||lv_message_text||''''
12565     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_interorg_ship_methods msis2 '
12566     ||'        WHERE msis2.sr_instance_code  = msis1.sr_instance_code'
12567     ||'        AND   NVL(msis2.company_name,  '||''''||NULL_CHAR||''''||') = '
12568     ||'              NVL(msis1.company_name,  '||''''||NULL_CHAR||''''||')'
12569     ||'        AND   NVL(msis2.from_organization_code,  '||''''||NULL_CHAR||''''||') = '
12570     ||'              NVL(msis1.from_organization_code,  '||''''||NULL_CHAR||''''||')'
12571     ||'        AND   NVL(msis2.to_organization_code,  '||''''||NULL_CHAR||''''||') = '
12572     ||'              NVL(msis1.to_organization_code,  '||''''||NULL_CHAR||''''||')'
12573     ||'        AND   NVL(msis2.from_location_code,  '||''''||NULL_CHAR||''''||') = '
12574     ||'              NVL(msis1.from_location_code,  '||''''||NULL_CHAR||''''||')'
12575     ||'        AND   NVL(msis2.to_location_code,  '||''''||NULL_CHAR||''''||') = '
12576     ||'              NVL(msis1.to_location_code,  '||''''||NULL_CHAR||''''||')'
12577     ||'        AND   msis2.ship_method       = msis1.ship_method'
12578     ||'        AND   msis2.process_flag      = msis1.process_flag'
12579     ||'        AND   NVL(msis2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
12580     ||'        AND    NVL(msis2.to_region_type, '||NULL_VALUE||')= NVL(msis1.to_region_type, '||NULL_VALUE||')'
12581     ||'        AND    NVL(msis2.to_country,  '||''''||NULL_CHAR||''''||') = '
12582     ||'               NVL(msis1.to_country,  '||''''||NULL_CHAR||''''||')'
12583     ||'        AND    NVL(msis2.to_country_code,  '||''''||NULL_CHAR||''''||') = '
12584     ||'               NVL(msis1.to_country_code,  '||''''||NULL_CHAR||''''||')'
12585     ||'        AND    NVL(msis2.to_state,  '||''''||NULL_CHAR||''''||') = '
12586     ||'               NVL(msis1.to_state,  '||''''||NULL_CHAR||''''||')'
12587     ||'        AND    NVL(msis2.to_state_code,  '||''''||NULL_CHAR||''''||') = '
12588     ||'               NVL(msis1.to_state_code,  '||''''||NULL_CHAR||''''||')'
12589     ||'        AND    NVL(msis2.to_city,  '||''''||NULL_CHAR||''''||') = '
12590     ||'               NVL(msis1.to_city,  '||''''||NULL_CHAR||''''||')'
12591     ||'        AND    NVL(msis2.to_city_code,  '||''''||NULL_CHAR||''''||') = '
12592     ||'               NVL(msis1.to_city_code,  '||''''||NULL_CHAR||''''||')'
12593     ||'        AND    NVL(msis2.to_postal_code_from,  '||''''||NULL_CHAR||''''||') = '
12594     ||'               NVL(msis1.to_postal_code_from,  '||''''||NULL_CHAR||''''||')'
12595     ||'        AND    NVL(msis2.to_postal_code_to,  '||''''||NULL_CHAR||''''||') = '
12596     ||'               NVL(msis1.to_postal_code_to,  '||''''||NULL_CHAR||''''||')'
12597     ||'        AND    NVL(msis2.to_zone,  '||''''||NULL_CHAR||''''||') = '
12598     ||'               NVL(msis1.to_zone,  '||''''||NULL_CHAR||''''||')'
12599     ||'        AND    NVL(msis2.from_region_type, '||NULL_VALUE||')= NVL(msis1.from_region_type, '||NULL_VALUE||')'
12600     ||'        AND    NVL(msis2.from_country,  '||''''||NULL_CHAR||''''||') = '
12601     ||'               NVL(msis1.from_country,  '||''''||NULL_CHAR||''''||')'
12602     ||'        AND    NVL(msis2.from_country_code,  '||''''||NULL_CHAR||''''||') = '
12603     ||'                NVL(msis1.from_country_code,  '||''''||NULL_CHAR||''''||')'
12604     ||'        AND    NVL(msis2.from_state,  '||''''||NULL_CHAR||''''||') = '
12605     ||'               NVL(msis1.from_state,  '||''''||NULL_CHAR||''''||')'
12606     ||'        AND    NVL(msis2.from_state_code,  '||''''||NULL_CHAR||''''||') = '
12607     ||'               NVL(msis1.from_state_code,  '||''''||NULL_CHAR||''''||')'
12608     ||'        AND    NVL(msis2.from_city,  '||''''||NULL_CHAR||''''||') = '
12609     ||'               NVL(msis1.from_city,  '||''''||NULL_CHAR||''''||')'
12610     ||'        AND    NVL(msis2.from_city_code,  '||''''||NULL_CHAR||''''||') = '
12611     ||'               NVL(msis1.from_city_code,  '||''''||NULL_CHAR||''''||')'
12612     ||'        AND    NVL(msis2.from_postal_code_from,  '||''''||NULL_CHAR||''''||') = '
12613     ||'               NVL(msis1.from_postal_code_from,  '||''''||NULL_CHAR||''''||')'
12614     ||'        AND    NVL(msis2.from_postal_code_to,  '||''''||NULL_CHAR||''''||') = '
12615     ||'               NVL(msis1.from_postal_code_to,  '||''''||NULL_CHAR||''''||')'
12616     ||'        AND    NVL(msis2.from_zone,  '||''''||NULL_CHAR||''''||') = '
12617     ||'               NVL(msis1.from_zone,  '||''''||NULL_CHAR||''''||')'
12618     ||'        AND   msis2.rowid <> msis1.rowid)'
12619     ||' AND    msis1.process_flag              = '||G_IN_PROCESS
12620     ||' AND    msis1.sr_instance_code          = :v_instance_code'
12621     ||' AND    NVL(msis1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
12622 
12623 
12624       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12625 
12626     EXECUTE IMMEDIATE lv_sql_stmt
12627             USING     v_instance_code;
12628 
12629     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12630                  (p_app_short_name    => 'MSC',
12631                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
12632                   p_message_text      => lv_message_text,
12633                   p_error_text        => lv_error_text);
12634 
12635     IF lv_return <> 0 THEN
12636       RAISE ex_logging_err;
12637     END IF;
12638 
12639     --Duplicate records check for the records whose source is XML
12640     v_sql_stmt := 02;
12641     lv_sql_stmt :=
12642     'UPDATE    msc_st_interorg_ship_methods msis1 '
12643     ||' SET    process_flag = '||G_ERROR_FLG||','
12644     ||'         error_text   = '||''''||lv_message_text||''''
12645     ||' WHERE message_id   < ( SELECT max(message_id) '
12646     ||'        FROM msc_st_interorg_ship_methods msis2 '
12647     ||'        WHERE msis2.sr_instance_code  = msis1.sr_instance_code'
12648     ||'        AND   NVL(msis2.company_name,  '||''''||NULL_CHAR||''''||') = '
12649     ||'              NVL(msis1.company_name,  '||''''||NULL_CHAR||''''||')'
12650     ||'        AND   NVL(msis2.from_organization_code,  '||''''||NULL_CHAR||''''||') = '
12651     ||'              NVL(msis1.from_organization_code,  '||''''||NULL_CHAR||''''||')'
12652     ||'        AND   NVL(msis2.to_organization_code,  '||''''||NULL_CHAR||''''||') = '
12653     ||'              NVL(msis1.to_organization_code,  '||''''||NULL_CHAR||''''||')'
12654     ||'        AND   NVL(msis2.from_location_code,  '||''''||NULL_CHAR||''''||') = '
12655     ||'              NVL(msis1.from_location_code,  '||''''||NULL_CHAR||''''||')'
12656     ||'        AND   NVL(msis2.to_location_code,  '||''''||NULL_CHAR||''''||') = '
12657     ||'              NVL(msis1.to_location_code,  '||''''||NULL_CHAR||''''||')'
12658     ||'        AND   msis2.ship_method       = msis1.ship_method'
12659     ||'        AND    NVL(msis2.to_region_type, '||NULL_VALUE||')= NVL(msis1.to_region_type, '||NULL_VALUE||')'
12660     ||'        AND    NVL(msis2.to_country,  '||''''||NULL_CHAR||''''||') = '
12661     ||'               NVL(msis1.to_country,  '||''''||NULL_CHAR||''''||')'
12662     ||'        AND    NVL(msis2.to_country_code,  '||''''||NULL_CHAR||''''||') = '
12663     ||'               NVL(msis1.to_country_code,  '||''''||NULL_CHAR||''''||')'
12664     ||'        AND    NVL(msis2.to_state,  '||''''||NULL_CHAR||''''||') = '
12665     ||'               NVL(msis1.to_state,  '||''''||NULL_CHAR||''''||')'
12666     ||'        AND    NVL(msis2.to_state_code,  '||''''||NULL_CHAR||''''||') = '
12667     ||'               NVL(msis1.to_state_code,  '||''''||NULL_CHAR||''''||')'
12668     ||'        AND    NVL(msis2.to_city,  '||''''||NULL_CHAR||''''||') = '
12669     ||'               NVL(msis1.to_city,  '||''''||NULL_CHAR||''''||')'
12670     ||'        AND    NVL(msis2.to_city_code,  '||''''||NULL_CHAR||''''||') = '
12671     ||'               NVL(msis1.to_city_code,  '||''''||NULL_CHAR||''''||')'
12672     ||'        AND    NVL(msis2.to_postal_code_from,  '||''''||NULL_CHAR||''''||') = '
12673     ||'               NVL(msis1.to_postal_code_from,  '||''''||NULL_CHAR||''''||')'
12674     ||'        AND    NVL(msis2.to_postal_code_to,  '||''''||NULL_CHAR||''''||') = '
12675     ||'               NVL(msis1.to_postal_code_to,  '||''''||NULL_CHAR||''''||')'
12676     ||'        AND    NVL(msis2.to_zone,  '||''''||NULL_CHAR||''''||') = '
12677     ||'               NVL(msis1.to_zone,  '||''''||NULL_CHAR||''''||')'
12678     ||'        AND    NVL(msis2.from_region_type, '||NULL_VALUE||')= NVL(msis1.from_region_type, '||NULL_VALUE||')'
12679     ||'        AND    NVL(msis2.from_country,  '||''''||NULL_CHAR||''''||') = '
12680     ||'               NVL(msis1.from_country,  '||''''||NULL_CHAR||''''||')'
12681     ||'        AND    NVL(msis2.from_country_code,  '||''''||NULL_CHAR||''''||') = '
12682     ||'                NVL(msis1.from_country_code,  '||''''||NULL_CHAR||''''||')'
12683     ||'        AND    NVL(msis2.from_state,  '||''''||NULL_CHAR||''''||') = '
12684     ||'               NVL(msis1.from_state,  '||''''||NULL_CHAR||''''||')'
12685     ||'        AND    NVL(msis2.from_state_code,  '||''''||NULL_CHAR||''''||') = '
12686     ||'               NVL(msis1.from_state_code,  '||''''||NULL_CHAR||''''||')'
12687     ||'        AND    NVL(msis2.from_city,  '||''''||NULL_CHAR||''''||') = '
12688     ||'               NVL(msis1.from_city,  '||''''||NULL_CHAR||''''||')'
12689     ||'        AND    NVL(msis2.from_city_code,  '||''''||NULL_CHAR||''''||') = '
12690     ||'               NVL(msis1.from_city_code,  '||''''||NULL_CHAR||''''||')'
12691     ||'        AND    NVL(msis2.from_postal_code_from,  '||''''||NULL_CHAR||''''||') = '
12692     ||'               NVL(msis1.from_postal_code_from,  '||''''||NULL_CHAR||''''||')'
12693     ||'        AND    NVL(msis2.from_postal_code_to,  '||''''||NULL_CHAR||''''||') = '
12694     ||'               NVL(msis1.from_postal_code_to,  '||''''||NULL_CHAR||''''||')'
12695     ||'        AND    NVL(msis2.from_zone,  '||''''||NULL_CHAR||''''||') = '
12696     ||'               NVL(msis1.from_zone,  '||''''||NULL_CHAR||''''||')'
12697     ||'        AND   msis2.process_flag      = msis1.process_flag'
12698     ||'        AND   NVL(msis2.message_id,'||NULL_VALUE||') <> '||NULL_VALUE||')'
12699     ||' AND    msis1.process_flag              = '||G_IN_PROCESS
12700     ||' AND    msis1.sr_instance_code          = :v_instance_code'
12701     ||' AND    NVL(msis1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
12702 
12703 
12704       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12705 
12706     EXECUTE IMMEDIATE lv_sql_stmt
12707             USING     v_instance_code;
12708 
12709   lv_column_names :=
12710   'FROM_ORGANIZATION_CODE       ||''~''||'
12711   ||'TO_ORGANIZATION_CODE          ||''~''||'
12712   ||'SR_INSTANCE_CODE              ||''~''||'
12713   ||'SHIP_METHOD                   ||''~''||'
12714   ||'DEFAULT_FLAG                  ||''~''||'
12715   ||'INTRANSIT_TIME               ||''~''||'
12716   ||'TIME_UOM_CODE                 ||''~''||'
12717   ||'WEIGHT_CAPACITY               ||''~''||'
12718   ||'WEIGHT_UOM                    ||''~''||'
12719   ||'VOLUME_CAPACITY               ||''~''||'
12720   ||'VOLUME_UOM                    ||''~''||'
12721   ||'COST_PER_WEIGHT_UNIT          ||''~''||'
12722   ||'COST_PER_VOLUME_UNIT          ||''~''||'
12723   ||'TRANSPORT_CAP_OVER_UTIL_COST  ||''~''||'
12724   ||'COMPANY_NAME                  ||''~''||'
12725   ||'FROM_LOCATION_CODE            ||''~''||'
12726   ||'TO_LOCATION_CODE              ||''~''||'
12727   ||'DELETED_FLAG' ;
12728 
12729     -- Dividing records in batches based on batchsize
12730     LOOP
12731       v_sql_stmt := 03;
12732       SELECT       msc_st_batch_id_s.NEXTVAL
12733       INTO         lv_batch_id
12734       FROM         DUAL;
12735 
12736     v_sql_stmt := 04;
12737       lv_sql_stmt :=
12738       'UPDATE   msc_st_interorg_ship_methods '
12739       ||' SET   batch_id                       = :lv_batch_id '
12740       ||' WHERE process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
12741       ||' AND   sr_instance_code               = :v_instance_code'
12742       ||' AND   NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
12743       ||' AND    rownum                        <= '||v_batch_size;
12744 
12745 
12746         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12747 
12748       EXECUTE IMMEDIATE lv_sql_stmt
12749               USING     lv_batch_id,
12750                         v_instance_code;
12751 
12752       EXIT WHEN SQL%NOTFOUND ;
12753 
12754       OPEN  c1(lv_batch_id);
12755       FETCH c1 BULK COLLECT INTO lb_rowid;
12756       CLOSE c1;
12757 
12758       v_sql_stmt := 05;
12759       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
12760       UPDATE msc_st_interorg_ship_methods
12761       SET  st_transaction_id = msc_st_interorg_ship_methods_s.NEXTVAL,
12762            refresh_id        = v_refresh_id,
12763            last_update_date  = v_current_date,
12764            last_updated_by   = v_current_user,
12765            creation_date     = v_current_date,
12766            created_by        = v_current_user
12767       WHERE  rowid           = lb_rowid(j);
12768 
12769       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12770                      (p_app_short_name    => 'MSC',
12771                       p_error_code        => 'MSC_PP_NO_DELETION',
12772                       p_message_text      => lv_message_text,
12773                       p_error_text        => lv_error_text,
12774                       p_token1            => 'TABLE_NAME',
12775                       p_token_value1      => 'MSC_ST_INTERORG_SHIP_METHODS');
12776 
12777 
12778       -- Deletion is not allowed for this table
12779       v_sql_stmt := 06;
12780       lv_sql_stmt :=
12781       'UPDATE      msc_st_interorg_ship_methods '
12782       ||' SET      process_flag     = '||G_ERROR_FLG||','
12783       ||'          error_text       = '||''''||lv_message_text||''''
12784       ||' WHERE    deleted_flag     = '||SYS_YES
12785       ||' AND      process_flag     = '||G_IN_PROCESS
12786       ||' AND      batch_id         = :lv_batch_id'
12787       ||' AND      sr_instance_code = :v_instance_code';
12788 
12789 
12790         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12791 
12792       EXECUTE IMMEDIATE lv_sql_stmt
12793               USING     lv_batch_id,
12794                         v_instance_code;
12795 
12796      -- set the message
12797 
12798       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12799                      (p_app_short_name    => 'MSC',
12800                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
12801                       p_message_text      => lv_message_text,
12802                       p_error_text        => lv_error_text,
12803                       p_token1            => 'COLUMN_NAME',
12804                       p_token_value1      => 'DELETED_FLAG',
12805                       p_token2            => 'DEFAULT_VALUE',
12806                       p_token_value2      => SYS_NO);
12807 
12808       IF lv_return <> 0 THEN
12809         RAISE ex_logging_err;
12810       END IF;
12811 
12812       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
12813       -- Log error procedure inserts warning and also defaults the delete flag
12814 
12815       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
12816                       ||' NOT IN(1,2)';
12817 
12818       lv_return := MSC_ST_UTIL.LOG_ERROR
12819                         (p_table_name        => 'MSC_ST_INTERORG_SHIP_METHODS',
12820                          p_instance_code     => v_instance_code,
12821                          p_row               => lv_column_names,
12822                          p_severity          => G_SEV_WARNING,
12823                          p_message_text      => lv_message_text,
12824                          p_error_text        => lv_error_text,
12825                          p_batch_id          => lv_batch_id,
12826                          p_where_str         => lv_where_str,
12827                          p_col_name          => 'DELETED_FLAG',
12828                          p_debug             => v_debug,
12829                          p_default_value     => SYS_NO);
12830 
12831       IF lv_return <> 0 THEN
12832         RAISE ex_logging_err;
12833       END IF;
12834 
12835       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
12836                      (p_app_short_name    => 'MSC',
12837                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
12838                       p_message_text      => lv_message_text,
12839                       p_error_text        => lv_error_text,
12840                       p_token1            => 'COLUMN_NAME',
12841                       p_token_value1      => 'SHIP_METHOD');
12842 
12843       IF lv_return <> 0 THEN
12844         RAISE ex_logging_err;
12845       END IF;
12846 
12847       -- Error out records where ship_method is NULL
12848       v_sql_stmt := 07;
12849       lv_sql_stmt :=
12850       'UPDATE    msc_st_interorg_ship_methods'
12851       ||' SET    process_flag            = '||G_ERROR_FLG||','
12852       ||'        error_text      = '||''''||lv_message_text||''''
12853       ||' WHERE  NVL(ship_method, '||''''||NULL_CHAR||''''||') '
12854       ||'        =                 '||''''||NULL_CHAR||''''
12855       ||' AND    batch_id                = :lv_batch_id'
12856       ||' AND    process_flag            = '||G_IN_PROCESS
12857       ||' AND    sr_instance_code        = :v_instance_code';
12858 
12859 
12860         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12861       EXECUTE IMMEDIATE lv_sql_stmt
12862               USING     lv_batch_id,
12863                         v_instance_code;
12864 
12865       -- Set the message
12866 
12867      lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
12868                       (p_app_short_name    => 'MSC',
12869                        p_error_code        => 'MSC_PP_INVALID_VALUE',
12870                        p_message_text      => lv_message_text,
12871                        p_error_text        => lv_error_text,
12872                        p_token1            => 'COLUMN_NAME',
12873                        p_token_value1      => 'FROM_ORGANIZATION_CODE');
12874 
12875       IF lv_return <> 0 THEN
12876         RAISE ex_logging_err;
12877       END IF;
12878 
12879      -- Derive from_organization_id
12880 
12881     lv_return :=  MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
12882                      (p_table_name       => 'MSC_ST_INTERORG_SHIP_METHODS',
12883                       p_org_partner_name  => 'FROM_ORGANIZATION_CODE',
12884                       p_org_partner_id    => 'FROM_ORGANIZATION_ID',
12885                       p_instance_code     => v_instance_code,
12886                       p_partner_type      => G_ORGANIZATION,
12887                       p_severity          => G_SEV3_ERROR,
12888                       p_error_text        => lv_error_text,
12889                       p_batch_id          => lv_batch_id,
12890                       p_debug             => v_debug,
12891                       p_message_text      => lv_message_text);
12892 
12893     IF lv_return <> 0 THEN
12894         RAISE ex_logging_err;
12895     END IF;
12896 
12897     lv_sql_stmt :=
12898       'UPDATE msc_st_interorg_ship_methods '
12899       ||' SET   FROM_ORGANIZATION_ID = -1 '
12900       ||' WHERE NVL(FROM_ORGANIZATION_ID,'||NULL_VALUE||') = '||NULL_VALUE
12901       ||' AND   NVL(FROM_ORGANIZATION_CODE,'||''''||NULL_CHAR||''''||')'
12902       ||'       = '||''''||NULL_CHAR||''''
12903       ||' AND   process_flag                   = '||G_IN_PROCESS
12904       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
12905       ||' AND    sr_instance_code               = :v_instance_code';
12906 
12907 
12908         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12909 
12910 
12911       EXECUTE IMMEDIATE lv_sql_stmt
12912               USING     lv_batch_id,
12913                         v_instance_code;
12914 
12915       -- Set the message
12916 
12917      lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
12918                       (p_app_short_name    => 'MSC',
12919                        p_error_code        => 'MSC_PP_INVALID_VALUE',
12920                        p_message_text      => lv_message_text,
12921                        p_error_text        => lv_error_text,
12922                        p_token1            => 'COLUMN_NAME',
12923                        p_token_value1      => 'TO_ORGANIZATION_CODE');
12924 
12925       IF lv_return <> 0 THEN
12926         RAISE ex_logging_err;
12927       END IF;
12928 
12929      -- Derive to_organization_id
12930 
12931     lv_return :=  MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
12932                      (p_table_name       => 'MSC_ST_INTERORG_SHIP_METHODS',
12933                       p_org_partner_name  => 'TO_ORGANIZATION_CODE',
12934                       p_org_partner_id    => 'TO_ORGANIZATION_ID',
12935                       p_instance_code     => v_instance_code,
12936                       p_partner_type      => G_ORGANIZATION,
12937                       p_severity          => G_SEV3_ERROR,
12938                       p_error_text        => lv_error_text,
12939                       p_batch_id          => lv_batch_id,
12940                       p_debug             => v_debug,
12941                       p_message_text      => lv_message_text);
12942 
12943     IF lv_return <> 0 THEN
12944         RAISE ex_logging_err;
12945     END IF;
12946 
12947     lv_sql_stmt :=
12948       'UPDATE msc_st_interorg_ship_methods '
12949       ||' SET   TO_ORGANIZATION_ID = -1 '
12950       ||' WHERE NVL(TO_ORGANIZATION_ID,'||NULL_VALUE||') = '||NULL_VALUE
12951       ||' AND   NVL(TO_ORGANIZATION_CODE,'||''''||NULL_CHAR||''''||')'
12952       ||'       = '||''''||NULL_CHAR||''''
12953       ||' AND   process_flag                   = '||G_IN_PROCESS
12954       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
12955       ||' AND    sr_instance_code               = :v_instance_code';
12956 
12957 
12958         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12959 
12960 
12961       EXECUTE IMMEDIATE lv_sql_stmt
12962               USING     lv_batch_id,
12963                         v_instance_code;
12964 
12965     -- Update FROM_LOCATION_ID
12966       v_sql_stmt  := 08;
12967       lv_sql_stmt :=
12968       'UPDATE msc_st_interorg_ship_methods msis'
12969       ||' SET   from_location_id              = (SELECT local_id'
12970       ||'       FROM   msc_local_id_setup mls'
12971       ||'       WHERE  mls.char1       = msis.sr_instance_code'
12972       ||'       AND    mls.char3       = msis.from_location_code'
12973       ||'       AND    mls.entity_name = ''LOCATION_ID'' )'
12974       ||' WHERE  process_flag                   = '||G_IN_PROCESS
12975       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
12976       ||' AND    sr_instance_code               = :v_instance_code';
12977 
12978 
12979         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
12980 
12981       EXECUTE IMMEDIATE lv_sql_stmt
12982               USING     lv_batch_id,
12983                         v_instance_code;
12984 
12985 
12986     -- Update TO_LOCATION_ID
12987 
12988       v_sql_stmt  := 09;
12989       lv_sql_stmt :=
12990       'UPDATE msc_st_interorg_ship_methods msis'
12991       ||' SET   to_location_id              = (SELECT local_id'
12992       ||'       FROM   msc_local_id_setup mls'
12993       ||'       WHERE  mls.char1       = msis.sr_instance_code'
12994       ||'       AND    mls.char3       = msis.to_location_code'
12995       ||'       AND    mls.entity_name = ''LOCATION_ID'' )'
12996       ||' WHERE  process_flag                   = '||G_IN_PROCESS
12997       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
12998       ||' AND    sr_instance_code               = :v_instance_code';
12999 
13000 
13001 
13002         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13003 
13004       EXECUTE IMMEDIATE lv_sql_stmt
13005               USING     lv_batch_id,
13006                         v_instance_code;
13007 
13008 
13009    -- if from or to location code was provided, error out if doesn't exists in local id.
13010 
13011       lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
13012                       (p_app_short_name    => 'MSC',
13013                        p_error_code        => 'MSC_PP_INVALID_VALUE',
13014                        p_message_text      => lv_message_text,
13015                        p_error_text        => lv_error_text,
13016                        p_token1            => 'COLUMN_NAME',
13017                        p_token_value1      => 'TO_LOCATION_CODE');
13018 
13019       IF lv_return <> 0 THEN
13020         RAISE ex_logging_err;
13021       END IF;
13022 
13023       v_sql_stmt  := 9.1;
13024       lv_sql_stmt :=
13025       'UPDATE msc_st_interorg_ship_methods '
13026       ||' SET   error_text   = '||''''||lv_error_text||''''||','
13027       ||'       process_flag = '||G_ERROR
13028       ||' WHERE NVL(to_location_id,'||NULL_VALUE||') = '||NULL_VALUE
13029       ||' AND   NVL(to_location_code,'||''''||NULL_CHAR||''''||')'
13030       ||'       <> '||''''||NULL_CHAR||''''
13031       ||' AND   process_flag                   = '||G_IN_PROCESS
13032       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
13033       ||' AND    sr_instance_code               = :v_instance_code';
13034 
13035 
13036         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13037 
13038 
13039       EXECUTE IMMEDIATE lv_sql_stmt
13040               USING     lv_batch_id,
13041                         v_instance_code;
13042 
13043 
13044       lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
13045                       (p_app_short_name    => 'MSC',
13046                        p_error_code        => 'MSC_PP_INVALID_VALUE',
13047                        p_message_text      => lv_message_text,
13048                        p_error_text        => lv_error_text,
13049                        p_token1            => 'COLUMN_NAME',
13050                        p_token_value1      => 'FROM_LOCATION_CODE');
13051 
13052       IF lv_return <> 0 THEN
13053         RAISE ex_logging_err;
13054       END IF;
13055 
13056       v_sql_stmt  := 9.2;
13057       lv_sql_stmt :=
13058       'UPDATE msc_st_interorg_ship_methods '
13059       ||' SET   error_text   = '||''''||lv_error_text||''''||','
13060       ||'       process_flag = '||G_ERROR
13061       ||' WHERE NVL(from_location_id,'||NULL_VALUE||') = '||NULL_VALUE
13062       ||' AND   NVL(from_location_code,'||''''||NULL_CHAR||''''||')'
13063       ||'       <> '||''''||NULL_CHAR||''''
13064       ||' AND   process_flag                   = '||G_IN_PROCESS
13065       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
13066       ||' AND    sr_instance_code               = :v_instance_code';
13067 
13068 
13069         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13070 
13071 
13072       EXECUTE IMMEDIATE lv_sql_stmt
13073               USING     lv_batch_id,
13074                         v_instance_code;
13075 
13076       lv_sql_stmt :=
13077       'UPDATE msc_st_interorg_ship_methods '
13078       ||' SET   from_location_id = -1'
13079       ||' WHERE NVL(from_location_id,'||NULL_VALUE||') = '||NULL_VALUE
13080       ||' AND   NVL(from_location_code,'||''''||NULL_CHAR||''''||')'
13081       ||'       = '||''''||NULL_CHAR||''''
13082       ||' AND   process_flag                   = '||G_IN_PROCESS
13083       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
13084       ||' AND    sr_instance_code               = :v_instance_code';
13085 
13086 
13087         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13088 
13089 
13090       EXECUTE IMMEDIATE lv_sql_stmt
13091               USING     lv_batch_id,
13092                         v_instance_code;
13093 
13094    lv_sql_stmt :=
13095       'UPDATE msc_st_interorg_ship_methods '
13096       ||' SET   to_location_id = -1'
13097       ||' WHERE NVL(to_location_id,'||NULL_VALUE||') = '||NULL_VALUE
13098       ||' AND   NVL(to_location_code,'||''''||NULL_CHAR||''''||')'
13099       ||'       = '||''''||NULL_CHAR||''''
13100       ||' AND   process_flag                   = '||G_IN_PROCESS
13101       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
13102       ||' AND    sr_instance_code               = :v_instance_code';
13103 
13104 
13105         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13106 
13107 
13108       EXECUTE IMMEDIATE lv_sql_stmt
13109               USING     lv_batch_id,
13110                         v_instance_code;
13111 
13112      -- Derive from_region_id
13113 
13114       lv_sql_stmt :=
13115       'UPDATE msc_st_interorg_ship_methods msr'
13116       ||' SET from_region_id     = (SELECT local_id'
13117       ||'       FROM   msc_local_id_setup mls'
13118       ||'       WHERE  mls.char1 = msr.sr_instance_code'
13119       ||'       AND    mls.number1 = nvl(msr.from_region_type,'||NULL_VALUE||')'
13120       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
13121       ||'              NVL(msr.from_country,     '||''''||NULL_CHAR||''''||') '
13122       ||'       AND    NVL(mls.char3,       '||''''||NULL_CHAR||''''||') = '
13123       ||'              NVL(msr.from_country_code,    '||''''||NULL_CHAR||''''||') '
13124       ||'       AND    NVL(mls.char4,       '||''''||NULL_CHAR||''''||') = '
13125       ||'              NVL(msr.from_state,     '||''''||NULL_CHAR||''''||') '
13126       ||'       AND    NVL(mls.char5,       '||''''||NULL_CHAR||''''||') = '
13127       ||'              NVL(msr.from_state_code,     '||''''||NULL_CHAR||''''||') '
13128       ||'       AND    NVL(mls.char6,       '||''''||NULL_CHAR||''''||') = '
13129       ||'              NVL(msr.from_city,     '||''''||NULL_CHAR||''''||') '
13130       ||'       AND    NVL(mls.char7,       '||''''||NULL_CHAR||''''||') = '
13131       ||'              NVL(msr.from_city_code,     '||''''||NULL_CHAR||''''||') '
13132       ||'       AND    NVL(mls.char8,       '||''''||NULL_CHAR||''''||') = '
13133       ||'              NVL(msr.from_postal_code_from,     '||''''||NULL_CHAR||''''||') '
13134       ||'       AND    NVL(mls.char9,       '||''''||NULL_CHAR||''''||') = '
13135       ||'              NVL(msr.from_postal_code_to,     '||''''||NULL_CHAR||''''||') '
13136       ||'       AND    NVL(mls.char10,       '||''''||NULL_CHAR||''''||') = '
13137       ||'              NVL(msr.from_zone,     '||''''||NULL_CHAR||''''||') '
13138       ||'       AND    mls.entity_name = ''REGION_ID'' )'
13139       ||' WHERE  process_flag                   = '||G_IN_PROCESS
13140       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
13141       ||' AND    sr_instance_code               = :v_instance_code';
13142 
13143 
13144         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13145 
13146 
13147       EXECUTE IMMEDIATE lv_sql_stmt
13148               USING     lv_batch_id,
13149                         v_instance_code;
13150 
13151     -- Derive to_region_id
13152 
13153       lv_sql_stmt :=
13154       'UPDATE msc_st_interorg_ship_methods msr'
13155       ||' SET   to_region_id     = (SELECT local_id'
13156       ||'       FROM   msc_local_id_setup mls'
13157       ||'       WHERE  mls.char1 = msr.sr_instance_code'
13158       ||'       AND    mls.number1 = nvl(msr.to_region_type,'||NULL_VALUE||')'
13159       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
13160       ||'              NVL(msr.to_country,     '||''''||NULL_CHAR||''''||') '
13161       ||'       AND    NVL(mls.char3,       '||''''||NULL_CHAR||''''||') = '
13162       ||'              NVL(msr.to_country_code,    '||''''||NULL_CHAR||''''||') '
13163       ||'       AND    NVL(mls.char4,       '||''''||NULL_CHAR||''''||') = '
13164       ||'              NVL(msr.to_state,     '||''''||NULL_CHAR||''''||') '
13165       ||'       AND    NVL(mls.char5,       '||''''||NULL_CHAR||''''||') = '
13166       ||'              NVL(msr.to_state_code,     '||''''||NULL_CHAR||''''||') '
13167       ||'       AND    NVL(mls.char6,       '||''''||NULL_CHAR||''''||') = '
13168       ||'              NVL(msr.to_city,     '||''''||NULL_CHAR||''''||') '
13169       ||'       AND    NVL(mls.char7,       '||''''||NULL_CHAR||''''||') = '
13170       ||'              NVL(msr.to_city_code,     '||''''||NULL_CHAR||''''||') '
13171       ||'       AND    NVL(mls.char8,       '||''''||NULL_CHAR||''''||') = '
13172       ||'              NVL(msr.to_postal_code_from,     '||''''||NULL_CHAR||''''||') '
13173       ||'       AND    NVL(mls.char9,       '||''''||NULL_CHAR||''''||') = '
13174       ||'              NVL(msr.to_postal_code_to,     '||''''||NULL_CHAR||''''||') '
13175       ||'       AND    NVL(mls.char10,       '||''''||NULL_CHAR||''''||') = '
13176       ||'              NVL(msr.to_zone,     '||''''||NULL_CHAR||''''||') '
13177       ||'       AND    mls.entity_name = ''REGION_ID'' )'
13178       ||' WHERE  process_flag                   = '||G_IN_PROCESS
13179       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
13180       ||' AND    sr_instance_code               = :v_instance_code';
13181 
13182 
13183         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13184 
13185 
13186       EXECUTE IMMEDIATE lv_sql_stmt
13187               USING     lv_batch_id,
13188                         v_instance_code;
13189 
13190 
13191    -- records should either have organization id's populated or location id's populated or both.
13192 
13193    -- Set the message
13194 
13195      lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
13196                       (p_app_short_name    => 'MSC',
13197                        p_error_code        => 'MSC_PP_INVALID_VALUE',
13198                        p_message_text      => lv_message_text,
13199                        p_error_text        => lv_error_text,
13200                        p_token1            => 'COLUMN_NAME',
13201                        p_token_value1      =>  'TO_ORGANIZATION_CODE OR '
13202                                              ||'FROM_ORGANIZATION_CODE OR '
13203                                              ||'TO_LOCATION_CODE OR '
13204                                              ||'FROM_LOCATION_CODE OR '
13205                                              ||'TO_REGION_CODE OR '
13206                                              ||'FROM_REGION_CODE ');
13207 
13208       IF lv_return <> 0 THEN
13209         RAISE ex_logging_err;
13210       END IF;
13211 
13212 
13213       v_sql_stmt := 9.1;
13214       lv_sql_stmt :=
13215       'UPDATE     msc_st_interorg_ship_methods msis1'
13216       ||' SET     process_flag     = '||G_ERROR_FLG||','
13217       ||'         error_text       = '||''''||lv_message_text||''''
13218       ||' WHERE   (msis1.to_location_id is NULL OR msis1.from_location_id is NULL) '
13219       ||' AND     (msis1.to_organization_id is NULL OR msis1.from_organization_id is NULL) '
13220       ||' AND     (msis1.to_region_id is NULL OR msis1.from_region_id is NULL) '
13221       ||' AND     msis1.batch_id                 = :lv_batch_id'
13222       ||' AND     msis1.process_flag             = '||G_IN_PROCESS
13223       ||' AND     msis1.sr_instance_code         = :v_instance_code' ;
13224 
13225 
13226         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13227 
13228 
13229       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,
13230                                           v_instance_code;
13231 
13232 
13233    -- Set the message
13234           lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13235                          (p_app_short_name    => 'MSC',
13236                           p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
13237                           p_message_text      => lv_message_text,
13238                           p_error_text        => lv_error_text,
13239                           p_token1            => 'COLUMN_NAME',
13240                           p_token_value1      => 'DEFAULT_FLAG',
13241                           p_token2            => 'DEFAULT_VALUE',
13242                           p_token_value2      => SYS_NO);
13243       -- Check DEFAULT_FLAG column has valid value or not and if not set it to SYS_NO
13244 
13245       lv_where_str := ' AND NVL(default_flag,'||NULL_VALUE||') '
13246                       ||' NOT IN(1,2)';
13247 
13248       lv_return := MSC_ST_UTIL.LOG_ERROR
13249                         (p_table_name        => 'MSC_ST_INTERORG_SHIP_METHODS',
13250                          p_instance_code     => v_instance_code,
13251                          p_row               => lv_column_names,
13252                          p_severity          => G_SEV_WARNING,
13253                          p_message_text      => lv_message_text,
13254                          p_error_text        => lv_error_text,
13255                          p_batch_id          => lv_batch_id,
13256                          p_where_str         => lv_where_str,
13257                          p_col_name          => 'DEFAULT_FLAG',
13258                          p_debug             => v_debug,
13259                          p_default_value     => SYS_NO);
13260 
13261       IF lv_return <> 0 THEN
13262         RAISE ex_logging_err;
13263       END IF;
13264 
13265 
13266       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13267                      (p_app_short_name    => 'MSC',
13268                       p_error_code        => 'MSC_PP_DEFAULT_SHIP_METHOD',
13269                       p_message_text      => lv_message_text,
13270                       p_error_text        => lv_error_text,
13271                       p_token1            => 'FROM_ORGANIZATION',
13272                       p_token_value1      => 'FROM_ORGANIZATION_CODE, FROM_LOCATION_CODE, FROM_REGION_CODE',
13273                       p_token2            => 'TO_ORGANIZATION',
13274                       p_token_value2      => 'TO_ORGANIZATION, TO_LOCATION_CODE, TO_REGION_CODE');
13275 
13276       IF lv_return <> 0 THEN
13277         RAISE ex_logging_err;
13278       END IF;
13279 
13280     -- Check that only one ship method has default flag set as yes
13281     -- in ODS as well as staging
13282     -- forward ported 2767559
13283 
13284       v_sql_stmt := 12;
13285       lv_sql_stmt :=
13286       'UPDATE     msc_st_interorg_ship_methods msis1'
13287       ||' SET     process_flag     = '||G_ERROR_FLG||','
13288       ||'         error_text       = '||''''||lv_message_text||''''
13289       ||' WHERE   EXISTS( SELECT 1 '
13290       ||'         FROM    msc_st_interorg_ship_methods msis2'
13291       ||'         WHERE   msis2.sr_instance_code = msis1.sr_instance_code'
13292       ||'         AND     nvl(msis2.from_organization_code, '||''''||NULL_CHAR||''''||') = '
13293       ||'		  nvl(msis1.from_organization_code, '||''''||NULL_CHAR||''''||')'
13294       ||'         AND     nvl(msis2.to_organization_code, '||''''||NULL_CHAR||''''||') = '
13295       ||'		  nvl(msis1.to_organization_code, '||''''||NULL_CHAR||''''||')'
13296       ||'         AND     nvl(msis2.from_location_code, '||''''||NULL_CHAR||''''||') = '
13297       ||'		  nvl(msis1.from_location_code, '||''''||NULL_CHAR||''''||')'
13298       ||'         AND     nvl(msis2.to_location_code, '||''''||NULL_CHAR||''''||') = '
13299       ||'		  nvl(msis1.to_location_code, '||''''||NULL_CHAR||''''||')'
13300       ||'         AND     nvl(msis2.from_region_id, '||NULL_VALUE||') = '
13301       ||'		  nvl(msis1.from_region_id, '||NULL_VALUE||')'
13302       ||'         AND     nvl(msis2.to_region_id, '||NULL_VALUE||') = '
13303       ||'		  nvl(msis1.to_region_id, '||NULL_VALUE||')'
13304       ||'         AND     msis2.process_flag IN (2,5)'
13305       ||'         AND     msis2.default_flag = '||SYS_YES
13306       ||'         AND     msis2.rowid <> msis1.rowid'
13307       ||'         UNION'
13308       ||'         SELECT 1 '
13309       ||'         FROM   msc_interorg_ship_methods msis3 '
13310       ||'         WHERE  msis3.sr_instance_id = :v_instance_id'
13311       ||'         AND    nvl(msis3.from_organization_id, '||''''||NULL_CHAR||''''||') = '
13312       ||'		 nvl(msis1.from_organization_id, '||''''||NULL_CHAR||''''||')'
13313       ||'         AND    nvl(msis3.to_organization_id, '||''''||NULL_CHAR||''''||') = '
13314       ||'		 nvl(msis1.to_organization_id, '||''''||NULL_CHAR||''''||')'
13315       ||'         AND    nvl(msis3.from_location_id, '||''''||NULL_CHAR||''''||') = '
13316       ||'		 nvl(msis1.from_location_id, '||''''||NULL_CHAR||''''||')'
13317       ||'         AND    nvl(msis3.to_location_id, '||''''||NULL_CHAR||''''||') = '
13318       ||'		 nvl(msis1.to_location_id, '||''''||NULL_CHAR||''''||')'
13319       ||'         AND    nvl(msis3.from_region_id, '||''''||NULL_CHAR||''''||') = '
13320       ||'		 nvl(msis1.from_region_id, '||''''||NULL_CHAR||''''||')'
13321       ||'         AND    nvl(msis3.to_region_id, '||''''||NULL_CHAR||''''||') = '
13322       ||'		 nvl(msis1.to_region_id, '||''''||NULL_CHAR||''''||')'
13323       ||'         AND    msis3.plan_id  = -1 '
13324       ||'         AND    msis3.default_flag = '||SYS_YES||')'
13325       ||' AND     msis1.default_flag             = '||SYS_YES
13326       ||' AND     msis1.batch_id                 = :lv_batch_id'
13327       ||' AND     msis1.process_flag             = '||G_IN_PROCESS
13328       ||' AND     msis1.sr_instance_code         = :v_instance_code' ;
13329 
13330 
13331         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13332 
13333 
13334       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_id,
13335                                           lv_batch_id,
13336                                           v_instance_code;
13337 
13338     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13339                      (p_app_short_name    => 'MSC',
13340                       p_error_code        => 'MSC_PP_INVALID_VALUE',
13341                       p_message_text      => lv_message_text,
13342                       p_error_text        => lv_error_text,
13343                       p_token1            => 'COLUMN_NAME',
13344                       p_token_value1      => 'VOLUME_UOM');
13345 
13346       IF lv_return <> 0 THEN
13347         RAISE ex_logging_err;
13348       END IF;
13349 
13350       -- Validate Volume UOM
13351 
13352      lv_return :=
13353      MSC_ST_UTIL.VALIDATE_UOM      (p_table_name     => 'MSC_ST_INTERORG_SHIP_METHODS',
13354                                     p_uom_col_name   => 'VOLUME_UOM',
13355                                     p_instance_id    => v_instance_id,
13356                                     p_instance_code  => v_instance_code,
13357                                     p_severity       => G_SEV3_ERROR,
13358                                     p_message_text   => lv_message_text,
13359                                     p_batch_id       => lv_batch_id,
13360                                     p_debug          => v_debug,
13361                                     p_error_text     => lv_error_text);
13362 
13363      if(lv_return <> 0) then
13364         RAISE ex_logging_err;
13365      end if;
13366 
13367     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13368                      (p_app_short_name    => 'MSC',
13369                       p_error_code        => 'MSC_PP_INVALID_VALUE',
13370                       p_message_text      => lv_message_text,
13371                       p_error_text        => lv_error_text,
13372                       p_token1            => 'COLUMN_NAME',
13373                       p_token_value1      => 'WEIGHT_UOM');
13374 
13375       IF lv_return <> 0 THEN
13376         RAISE ex_logging_err;
13377       END IF;
13378 
13379       -- Validate Weight UOM
13380 
13381      lv_return :=
13382      MSC_ST_UTIL.VALIDATE_UOM      (p_table_name     => 'MSC_ST_INTERORG_SHIP_METHODS',
13383                                     p_uom_col_name   => 'WEIGHT_UOM',
13384                                     p_instance_id    => v_instance_id,
13385                                     p_instance_code  => v_instance_code,
13386                                     p_severity       => G_SEV3_ERROR,
13387                                     p_message_text   => lv_message_text,
13388                                     p_batch_id       => lv_batch_id,
13389                                     p_debug          => v_debug,
13390                                     p_error_text     => lv_error_text);
13391 
13392      if(lv_return <> 0) then
13393         RAISE ex_logging_err;
13394      end if;
13395 
13396 
13397       --Call to customised validation.
13398       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
13399         (ERRBUF         => lv_error_text,
13400          RETCODE        => lv_return,
13401          pBatchID       => lv_batch_id,
13402          pInstanceCode  => v_instance_code,
13403          pEntityName    => 'MSC_ST_INTERORG_SHIP_METHODS',
13404          pInstanceID    => v_instance_id);
13405 
13406       IF NVL(lv_return,0) <> 0 THEN
13407         RAISE ex_logging_err;
13408      END IF;
13409 
13410 
13411       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
13412                      (p_table_name     => 'MSC_ST_INTERORG_SHIP_METHODS',
13413                       p_instance_id    => v_instance_id,
13414                       p_instance_code  => v_instance_code,
13415                       p_process_flag   => G_VALID,
13416                       p_error_text     => lv_error_text,
13417                       p_debug          => v_debug,
13418                       p_batch_id       => lv_batch_id);
13419 
13420       IF lv_return <> 0 THEN
13421         RAISE ex_logging_err;
13422       END IF;
13423 
13424   v_sql_stmt := 13;
13425    lv_sql_stmt :=
13426    'UPDATE      msc_st_interorg_ship_methods'
13427    ||' SET      sr_instance_id2 = '||v_instance_id
13428    ||' WHERE    sr_instance_code    = :v_instance_code'
13429    ||' AND      process_flag        ='|| G_VALID
13430    ||' AND      batch_id            = :lv_batch_id' ;
13431 
13432 
13433          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13434 
13435 
13436    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
13437 
13438       lv_return := MSC_ST_UTIL.LOG_ERROR
13439                      (p_table_name        => 'MSC_ST_INTERORG_SHIP_METHODS',
13440                       p_instance_code     => v_instance_code,
13441                       p_row               => lv_column_names,
13442                       p_severity          => G_SEV_ERROR,
13443                       p_message_text      => NULL,
13444                       p_error_text        => lv_error_text,
13445                       p_debug             => v_debug,
13446                       p_batch_id          => lv_batch_id);
13447       IF lv_return <> 0 THEN
13448         RAISE ex_logging_err;
13449       END IF;
13450       COMMIT;
13451     END LOOP;
13452 
13453   EXCEPTION
13454     WHEN too_many_rows THEN
13455       lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_SHIP_METHODS'||'('
13456                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
13457         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
13458       ROLLBACK;
13459 
13460     WHEN ex_logging_err THEN
13461         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
13462       ROLLBACK;
13463 
13464     WHEN OTHERS THEN
13465       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_SHIP_METHODS'||'('
13466                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
13467         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
13468       ROLLBACK;
13469 
13470   END LOAD_SHIP_METHODS ;
13471 
13472 
13473 /*=============================================================================+
13474 | DESCRIPTION  : This procedure validates the data for REGIONS, ZONES, Region   |
13475 |                locations and region sites                                     |
13476 +==============================================================================*/
13477 
13478   PROCEDURE LOAD_REGIONS IS
13479   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
13480   TYPE CurTyp       IS REF CURSOR;
13481   c1                CurTyp;
13482   lb_rowid              RowidTab;   --bulk collects rowid
13483   lv_return             NUMBER;
13484   lv_error_text         VARCHAR2(250);
13485   lv_where_str          VARCHAR2(5000);
13486   lv_sql_stmt           VARCHAR2(5000);
13487   lv_column_names       VARCHAR2(5000);       --stores concatenated column names
13488   lv_batch_id           msc_st_regions.batch_id%TYPE;
13489   lv_message_text       msc_errors.error_text%TYPE;
13490   lv_cursor_stmt        VARCHAR2(5000);
13491 
13492   ex_logging_err        EXCEPTION;
13493 
13494   CURSOR c2(p_batch_id NUMBER) IS
13495     SELECT rowid
13496     FROM   msc_st_regions
13497     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
13498     AND    sr_instance_code = v_instance_code
13499     AND    batch_id         = p_batch_id;
13500 
13501 
13502  CURSOR c3(p_batch_id NUMBER) IS
13503     SELECT rowid
13504     FROM   msc_st_regions
13505     WHERE  NVL(region_id,NULL_VALUE) = NULL_VALUE
13506     AND    process_flag                  = G_IN_PROCESS
13507     AND    NVL(batch_id,NULL_VALUE)      =p_batch_id
13508     AND    sr_instance_code              = v_instance_code;
13509 
13510  CURSOR c4(p_batch_id NUMBER) IS
13511     SELECT rowid
13512     FROM   msc_st_zone_regions
13513     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
13514     AND    sr_instance_code = v_instance_code
13515     AND    batch_id         = p_batch_id;
13516 
13517  CURSOR c5(p_batch_id NUMBER) IS
13518     SELECT rowid
13519     FROM   msc_st_zone_regions
13520     WHERE  NVL(zone_region_id,NULL_VALUE) = NULL_VALUE
13521     AND    process_flag                  = G_IN_PROCESS
13522     AND    NVL(batch_id,NULL_VALUE)      =p_batch_id
13523     AND    sr_instance_code              = v_instance_code;
13524 
13525  CURSOR c6(p_batch_id NUMBER) IS
13526     SELECT rowid
13527     FROM   msc_st_region_locations
13528     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
13529     AND    sr_instance_code = v_instance_code
13530     AND    batch_id         = p_batch_id;
13531 
13532  CURSOR c7(p_batch_id NUMBER) IS
13533     SELECT max(rowid)
13534     FROM   msc_st_region_locations
13535     WHERE  process_flag                = G_IN_PROCESS
13536     AND    deleted_flag                = SYS_NO
13537     AND    NVL(location_id,NULL_VALUE) = NULL_VALUE
13538     AND    sr_instance_code            = v_instance_code
13539     AND    batch_id                    = p_batch_id
13540     GROUP BY location_code;
13541 
13542  CURSOR c8(p_batch_id NUMBER) IS
13543     SELECT rowid
13544     FROM   msc_st_region_sites
13545     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
13546     AND    sr_instance_code = v_instance_code
13547     AND    batch_id         = p_batch_id;
13548 
13549   BEGIN
13550 
13551     --Getting the error message text
13552     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13553                  (p_app_short_name    => 'MSC',
13554                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
13555                   p_message_text      => lv_message_text,
13556                   p_error_text        => lv_error_text);
13557 
13558     IF lv_return <> 0 THEN
13559       RAISE ex_logging_err;
13560     END IF;
13561 
13562     --Duplicate records check for the records whose source is XML
13563     v_sql_stmt := 01;
13564     lv_sql_stmt :=
13565     'UPDATE     msc_st_regions msr1 '
13566     ||' SET     process_flag = '||G_ERROR_FLG||','
13567     ||'         error_text   = '||''''||lv_message_text||''''
13568     ||' WHERE   message_id <  (SELECT MAX(message_id)'
13569     ||'         FROM   msc_st_regions msr2'
13570     ||'         WHERE  msr2.sr_instance_code  = msr1.sr_instance_code'
13571     ||'         AND    msr2.region_type = msr1.region_type'
13572     ||'         AND    NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
13573     ||'                NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
13574     ||'         AND    NVL(msr2.country,  '||''''||NULL_CHAR||''''||') = '
13575     ||'                NVL(msr1.country,  '||''''||NULL_CHAR||''''||')'
13576     ||'         AND    NVL(msr2.country_code,  '||''''||NULL_CHAR||''''||') = '
13577     ||'                NVL(msr1.country_code,  '||''''||NULL_CHAR||''''||')'
13578     ||'         AND    NVL(msr2.state,  '||''''||NULL_CHAR||''''||') = '
13579     ||'                NVL(msr1.state,  '||''''||NULL_CHAR||''''||')'
13580     ||'         AND    NVL(msr2.state_code,  '||''''||NULL_CHAR||''''||') = '
13581     ||'                NVL(msr1.state_code,  '||''''||NULL_CHAR||''''||')'
13582     ||'         AND    NVL(msr2.city,  '||''''||NULL_CHAR||''''||') = '
13583     ||'                NVL(msr1.city,  '||''''||NULL_CHAR||''''||')'
13584     ||'         AND    NVL(msr2.city_code,  '||''''||NULL_CHAR||''''||') = '
13585     ||'                NVL(msr1.city_code,  '||''''||NULL_CHAR||''''||')'
13586     ||'         AND    NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||') = '
13587     ||'                NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
13588     ||'         AND    NVL(msr2.postal_code_to,  '||''''||NULL_CHAR||''''||') = '
13589     ||'                NVL(msr1.postal_code_to,  '||''''||NULL_CHAR||''''||')'
13590     ||'         AND    NVL(msr2.zone,  '||''''||NULL_CHAR||''''||') = '
13591     ||'                NVL(msr1.zone,  '||''''||NULL_CHAR||''''||')'
13592     ||'         AND    msr2.process_flag      ='|| G_IN_PROCESS
13593     ||'         AND    NVL(msr2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
13594     ||' AND     msr1.process_flag             = '||G_IN_PROCESS
13595     ||' AND     msr1.sr_instance_code         = :v_instance_code'
13596     ||' AND     NVL(msr1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
13597 
13598 
13599       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13600 
13601 
13602     EXECUTE IMMEDIATE lv_sql_stmt
13603             USING     v_instance_code;
13604 
13605 
13606    /* v_sql_stmt := 1.1;
13607     lv_sql_stmt :=
13608     'UPDATE     msc_st_regions msr1 '
13609     ||' SET     process_flag = '||G_ERROR_FLG||','
13610     ||'         error_text   = '||''''||lv_message_text||''''
13611     ||' WHERE   message_id <  (SELECT MAX(message_id)'
13612     ||'         FROM   msc_st_regions msr2'
13613     ||'         WHERE  msr2.sr_instance_code  = msr1.sr_instance_code'
13614     ||'         AND    msr2.region_type = msr1.region_type'
13615     ||'         AND    NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
13616     ||'                NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
13617     ||'         AND    NVL(msr2.zone,  '||''''||NULL_CHAR||''''||') = '
13618     ||'                NVL(msr1.zone,  '||''''||NULL_CHAR||''''||')'
13619     ||'         AND    msr2.process_flag      ='|| G_IN_PROCESS
13620     ||'         AND    msr2.region_type in  (10,11) '
13621     ||'         AND    NVL(msr2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
13622     ||' AND     msr1.process_flag             = '||G_IN_PROCESS
13623     ||' AND     msr1.sr_instance_code         = :v_instance_code'
13624     ||' AND    msr1.region_type in  (10,11) '
13625     ||' AND     NVL(msr1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
13626 
13627     IF V_DEBUG THEN
13628       msc_st_util.log_message(lv_sql_stmt);
13629     END IF;
13630 
13631     EXECUTE IMMEDIATE lv_sql_stmt
13632             USING     v_instance_code; */
13633 
13634 
13635     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13636                  (p_app_short_name    => 'MSC',
13637                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
13638                   p_message_text      => lv_message_text,
13639                   p_error_text        => lv_error_text);
13640 
13641     IF lv_return <> 0 THEN
13642       RAISE ex_logging_err;
13643     END IF;
13644 
13645     --Duplicate records check for the records whose source is other than XML
13646     --Different SQL is used because in XML we can identify the latest records
13647     --whereas in batch load we cannot.
13648     v_sql_stmt := 02;
13649     lv_sql_stmt :=
13650     'UPDATE  msc_st_regions msr1'
13651     ||' SET     process_flag  = '||G_ERROR_FLG||','
13652     ||'         error_text   = '||''''||lv_message_text||''''
13653     ||' WHERE   EXISTS( SELECT 1 '
13654     ||'         FROM   msc_st_regions msr2'
13655     ||'         WHERE  msr2.sr_instance_code           = msr1.sr_instance_code'
13656     ||'         AND    msr2.region_type = msr1.region_type'
13657     ||'         AND    NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
13658     ||'                NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
13659     ||'         AND    NVL(msr2.country,  '||''''||NULL_CHAR||''''||') = '
13660     ||'                NVL(msr1.country,  '||''''||NULL_CHAR||''''||')'
13661     ||'         AND    NVL(msr2.country_code,  '||''''||NULL_CHAR||''''||') = '
13662     ||'                NVL(msr1.country_code,  '||''''||NULL_CHAR||''''||')'
13663     ||'         AND    NVL(msr2.state,  '||''''||NULL_CHAR||''''||') = '
13664     ||'                NVL(msr1.state,  '||''''||NULL_CHAR||''''||')'
13665     ||'         AND    NVL(msr2.state_code,  '||''''||NULL_CHAR||''''||') = '
13666     ||'                NVL(msr1.state_code,  '||''''||NULL_CHAR||''''||')'
13667     ||'         AND    NVL(msr2.city,  '||''''||NULL_CHAR||''''||') = '
13668     ||'                NVL(msr1.city,  '||''''||NULL_CHAR||''''||')'
13669     ||'         AND    NVL(msr2.city_code,  '||''''||NULL_CHAR||''''||') = '
13670     ||'                NVL(msr1.city_code,  '||''''||NULL_CHAR||''''||')'
13671     ||'         AND    NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||') = '
13672     ||'                NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
13673     ||'         AND    NVL(msr2.postal_code_to,  '||''''||NULL_CHAR||''''||') = '
13674     ||'                NVL(msr1.postal_code_to,  '||''''||NULL_CHAR||''''||')'
13675     ||'         AND    NVL(msr2.zone,  '||''''||NULL_CHAR||''''||') = '
13676     ||'                NVL(msr1.zone,  '||''''||NULL_CHAR||''''||')'
13677     ||'         AND    msr2.process_flag               = '||G_IN_PROCESS
13678     ||'         GROUP BY sr_instance_code, region_type, company_name,country,country_code, state, state_code, city, city_code, postal_code_from, postal_code_to, zone'
13679     ||'         HAVING COUNT(*) > 1)'
13680     ||' AND     msr1.process_flag     = '||G_IN_PROCESS
13681     ||' AND     msr1.sr_instance_code = :v_instance_code'
13682     ||' AND     NVL(msr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
13683 
13684 
13685       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13686 
13687 
13688     EXECUTE IMMEDIATE lv_sql_stmt
13689             USING     v_instance_code;
13690 
13691 
13692 
13693     lv_column_names :=
13694   'REGION_TYPE                    ||''~''||'
13695   ||' COUNTRY                     ||''~''||'
13696   ||' COUNTRY_CODE                ||''~''||'
13697   ||' STATE_CODE            	  ||''~''||'
13698   ||' SR_INSTANCE_CODE            ||''~''||'
13699   ||' STATE                       ||''~''||'
13700   ||' CITY              	  ||''~''||'
13701   ||' CITY_CODE 		  ||''~''||'
13702   ||' POSTAL_CODE_FROM            ||''~''||'
13703   ||' POSTAL_CODE_TO              ||''~''||'
13704   ||' ZONE                        ||''~''||'
13705   ||' DELETED_FLAG                ||''~''||'
13706   ||' COMPANY_NAME ' ;
13707 
13708   -- Now processing by batch
13709    LOOP
13710       v_sql_stmt := 03;
13711       lv_cursor_stmt :=
13712       ' SELECT msc_st_batch_id_s.NEXTVAL '
13713       ||' FROM   dual';
13714 
13715       OPEN  c1 FOR lv_cursor_stmt;
13716       FETCH c1 INTO lv_batch_id;
13717       CLOSE c1;
13718 
13719       v_sql_stmt := 04;
13720       lv_sql_stmt :=
13721       ' UPDATE msc_st_regions '
13722       ||' SET    batch_id  = :lv_batch_id'
13723       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
13724       ||' AND    sr_instance_code               = :v_instance_code'
13725       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
13726       ||' AND    rownum                        <= '||v_batch_size;
13727 
13728 
13729          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13730 
13731 
13732       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
13733 
13734       EXIT WHEN SQL%NOTFOUND;
13735 
13736       OPEN c2(lv_batch_id);
13737       FETCH c2 BULK COLLECT INTO lb_rowid;
13738       CLOSE c2;
13739 
13740       v_sql_stmt := 05;
13741       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
13742       UPDATE msc_st_regions
13743       SET  st_transaction_id = msc_st_regions_s.NEXTVAL,
13744            refresh_number    = v_refresh_id,
13745            last_update_date  = v_current_date,
13746            last_updated_by   = v_current_user,
13747            creation_date     = v_current_date,
13748            created_by        = v_current_user
13749       WHERE  rowid           = lb_rowid(j);
13750 
13751       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13752                      (p_app_short_name    => 'MSC',
13753                       p_error_code        => 'MSC_PP_NO_DELETION',
13754                       p_message_text      => lv_message_text,
13755                       p_error_text        => lv_error_text,
13756                       p_token1            => 'TABLE_NAME',
13757                       p_token_value1      => 'MSC_ST_REGIONS');
13758 
13759 
13760       -- Deletion is not allowed for this table
13761       v_sql_stmt := 06;
13762       lv_sql_stmt :=
13763       'UPDATE      msc_st_regions '
13764       ||' SET      process_flag     = '||G_ERROR_FLG||','
13765       ||'          error_text       = '||''''||lv_message_text||''''
13766       ||' WHERE    deleted_flag     = '||SYS_YES
13767       ||' AND      process_flag     = '||G_IN_PROCESS
13768       ||' AND      batch_id         = :lv_batch_id'
13769       ||' AND      sr_instance_code = :v_instance_code';
13770 
13771 
13772         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13773 
13774 
13775       EXECUTE IMMEDIATE lv_sql_stmt
13776               USING     lv_batch_id,
13777                         v_instance_code;
13778 
13779      -- set the message
13780 
13781       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13782                      (p_app_short_name    => 'MSC',
13783                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
13784                       p_message_text      => lv_message_text,
13785                       p_error_text        => lv_error_text,
13786                       p_token1            => 'COLUMN_NAME',
13787                       p_token_value1      => 'DELETED_FLAG',
13788                       p_token2            => 'DEFAULT_VALUE',
13789                       p_token_value2      => SYS_NO);
13790 
13791       IF lv_return <> 0 THEN
13792         RAISE ex_logging_err;
13793       END IF;
13794 
13795       -- Check DELETED_FLAG column has valid value or not and if not set it to SYS_NO
13796       -- Log error procedure inserts warning and also defaults the deleted flag
13797 
13798       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
13799                       ||' NOT IN(1,2)';
13800 
13801       lv_return := MSC_ST_UTIL.LOG_ERROR
13802                         (p_table_name        => 'MSC_ST_REGIONS',
13803                          p_instance_code     => v_instance_code,
13804                          p_row               => lv_column_names,
13805                          p_severity          => G_SEV_WARNING,
13806                          p_message_text      => lv_message_text,
13807                          p_error_text        => lv_error_text,
13808                          p_batch_id          => lv_batch_id,
13809                          p_where_str         => lv_where_str,
13810                          p_col_name          => 'DELETED_FLAG',
13811                          p_debug             => v_debug,
13812                          p_default_value     => SYS_NO);
13813 
13814       IF lv_return <> 0 THEN
13815         RAISE ex_logging_err;
13816       END IF;
13817 
13818 
13819      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13820                      (p_app_short_name    => 'MSC',
13821                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
13822                       p_message_text      => lv_message_text,
13823                       p_error_text        => lv_error_text,
13824                       p_token1            => 'COLUMN_NAME',
13825                       p_token_value1      => 'REGION_TYPE');
13826 
13827       IF lv_return <> 0 THEN
13828         RAISE ex_logging_err;
13829       END IF;
13830 
13831     lv_sql_stmt :=
13832     'UPDATE    msc_st_regions'
13833     ||' SET    process_flag     ='||G_ERROR_FLG||','
13834     ||'        error_text   = '||''''||lv_message_text||''''
13835     ||' WHERE  region_type   NOT IN (0,1,2,3,10) '
13836     ||' AND    sr_instance_code  = :v_instance_code'
13837     ||' AND    process_flag      = '||G_IN_PROCESS
13838     ||' AND    batch_id          = :lv_batch_id';
13839 
13840 
13841          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13842 
13843     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
13844 
13845 
13846     lv_sql_stmt :=
13847       ' UPDATE msc_st_regions '
13848       ||' SET    parent_region_id  = -1'
13849       ||' WHERE  process_flag   = '||G_IN_PROCESS
13850       ||' AND    sr_instance_code  = :v_instance_code'
13851       ||' AND    region_type in (0,10) '
13852       ||' AND    batch_id          = :lv_batch_id';
13853 
13854 
13855          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13856 
13857 
13858       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code, lv_batch_id;
13859 
13860 
13861     -- write message text here
13862 
13863     /* lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13864                          (p_app_short_name    => 'MSC',
13865                           p_error_code        => 'MSC_PP_POSTAL_CODE_OVERLAP',
13866                           p_message_text      => lv_message_text,
13867                           p_error_text        => lv_error_text);
13868 
13869             IF lv_return <> 0 THEN
13870               RAISE ex_logging_err;
13871             END IF;
13872 
13873     lv_sql_stmt :=
13874     'UPDATE  msc_st_regions msr1'
13875     ||' SET     process_flag  = '||G_ERROR_FLG||','
13876     ||'         error_text   = '||''''||lv_message_text||''''
13877     ||' WHERE   EXISTS( SELECT 1 '
13878     ||'         FROM   msc_st_regions msr2'
13879     ||'         WHERE  msr2.sr_instance_code           = msr1.sr_instance_code'
13880     ||'         AND    msr2.region_type = msr1.region_type'
13881     ||'         AND    NVL(msr2.country,  '||''''||NULL_CHAR||''''||') = '
13882     ||'                NVL(msr1.country,  '||''''||NULL_CHAR||''''||')'
13883     ||'         AND    NVL(msr2.country_code,  '||''''||NULL_CHAR||''''||') = '
13884     ||'                NVL(msr1.country_code,  '||''''||NULL_CHAR||''''||')'
13885     ||'         AND    ((NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||') = '
13886     ||'                NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
13887     ||'                AND    NVL(msr2.postal_code_to,  '||''''||NULL_CHAR||''''||') = '
13888     ||'                NVL(msr1.postal_code_to,  '||''''||NULL_CHAR||''''||'))'
13889     ||'            OR (NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||') > '
13890     ||'                NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
13891     ||'                AND    NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||') < '
13892     ||'                NVL(msr1.postal_code_to,  '||''''||NULL_CHAR||''''||'))'
13893     ||'            OR (NVL(msr2.postal_code_to,  '||''''||NULL_CHAR||''''||') > '
13894     ||'                NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
13895     ||'                AND    NVL(msr2.postal_code_to,  '||''''||NULL_CHAR||''''||') < '
13896     ||'                NVL(msr1.postal_code_to,  '||''''||NULL_CHAR||''''||')))'
13897     ||'         AND    msr2.process_flag               = '||G_IN_PROCESS
13898     ||'         AND    NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||')'
13899     ||'                <> '||''''||NULL_CHAR||''''
13900     ||'         AND    NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
13901     ||'                <> '||''''||NULL_CHAR||''''
13902     ||'         AND    msr2.region_type in  (0,1,2,3) '
13903     ||' AND     msr1.rowid <> msr2.rowid)'
13904     ||' AND     msr1.process_flag     = '||G_IN_PROCESS
13905     ||' AND     msr1.sr_instance_code = :v_instance_code'
13906     ||' AND     msr1.region_type in  (0,1,2,3) ';
13907 
13908 
13909 
13910       msc_st_util.log_message(lv_sql_stmt);
13911 
13912 
13913     EXECUTE IMMEDIATE lv_sql_stmt
13914             USING     v_instance_code;
13915 
13916     */
13917 
13918      --Deriving region_id
13919 
13920       lv_sql_stmt :=
13921       'UPDATE msc_st_regions msr'
13922       ||' SET region_id     = (SELECT local_id'
13923       ||'       FROM   msc_local_id_setup mls'
13924       ||'       WHERE  mls.char1 = msr.sr_instance_code'
13925       ||'       AND    mls.number1 = msr.region_type'
13926       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
13927       ||'              NVL(msr.country,     '||''''||NULL_CHAR||''''||') '
13928       ||'       AND    NVL(mls.char3,       '||''''||NULL_CHAR||''''||') = '
13929       ||'              NVL(msr.country_code,    '||''''||NULL_CHAR||''''||') '
13930       ||'       AND    NVL(mls.char4,       '||''''||NULL_CHAR||''''||') = '
13931       ||'              NVL(msr.state,     '||''''||NULL_CHAR||''''||') '
13932       ||'       AND    NVL(mls.char5,       '||''''||NULL_CHAR||''''||') = '
13933       ||'              NVL(msr.state_code,     '||''''||NULL_CHAR||''''||') '
13934       ||'       AND    NVL(mls.char6,       '||''''||NULL_CHAR||''''||') = '
13935       ||'              NVL(msr.city,     '||''''||NULL_CHAR||''''||') '
13936       ||'       AND    NVL(mls.char7,       '||''''||NULL_CHAR||''''||') = '
13937       ||'              NVL(msr.city_code,     '||''''||NULL_CHAR||''''||') '
13938       ||'       AND    NVL(mls.char8,       '||''''||NULL_CHAR||''''||') = '
13939       ||'              NVL(msr.postal_code_from,     '||''''||NULL_CHAR||''''||') '
13940       ||'       AND    NVL(mls.char9,       '||''''||NULL_CHAR||''''||') = '
13941       ||'              NVL(msr.postal_code_to,     '||''''||NULL_CHAR||''''||') '
13942       ||'       AND    NVL(mls.char10,       '||''''||NULL_CHAR||''''||') = '
13943       ||'              NVL(msr.zone,     '||''''||NULL_CHAR||''''||') '
13944       ||'       AND    mls.entity_name = ''REGION_ID'' )'
13945       ||' WHERE  process_flag                   = '||G_IN_PROCESS
13946       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
13947       ||' AND    sr_instance_code               = :v_instance_code';
13948 
13949 
13950         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13951 
13952 
13953       EXECUTE IMMEDIATE lv_sql_stmt
13954               USING     lv_batch_id,
13955                         v_instance_code;
13956 
13957 
13958       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
13959                      (p_app_short_name    => 'MSC',
13960                       p_error_code        => 'MSC_PP_NO_UPDATION',
13961                       p_message_text      => lv_message_text,
13962                       p_error_text        => lv_error_text,
13963                       p_token1            => 'TABLE_NAME',
13964                       p_token_value1      => 'MSC_ST_REGIONS');
13965 
13966 
13967     -- Error out the record if same region already exists in ODS
13968     -- As we do not allow regions incremental update
13969     -- It has to be purged before loaded again
13970 
13971       lv_sql_stmt :=
13972       'UPDATE      msc_st_regions msr'
13973       ||' SET      msr.process_flag     = '||G_ERROR_FLG||','
13974       ||'          msr.error_text       = '||''''||lv_message_text||''''
13975       ||' WHERE    msr.batch_id         = :lv_batch_id'
13976       ||' AND      msr.sr_instance_code = :v_instance_code'
13977       ||' AND      EXISTS ( SELECT 1'
13978       ||'          FROM     msc_regions mr'
13979       ||'          WHERE    nvl(msr.region_id,'||NULL_VALUE||') = mr.region_id'
13980       ||'          AND      mr.sr_instance_id = :v_instance_id)';
13981 
13982         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
13983 
13984 
13985       EXECUTE IMMEDIATE lv_sql_stmt
13986               USING     lv_batch_id,
13987                         v_instance_code,
13988                         v_instance_id;
13989 
13990 
13991      --Call to customised validation.
13992       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
13993         (ERRBUF         => lv_error_text,
13994          RETCODE        => lv_return,
13995          pBatchID       => lv_batch_id,
13996          pInstanceCode  => v_instance_code,
13997          pEntityName    => 'MSC_ST_REGIONS',
13998          pInstanceID    => v_instance_id);
13999 
14000       IF NVL(lv_return,0) <> 0 THEN
14001         RAISE ex_logging_err;
14002       END IF;
14003 
14004       --Generation of region_id
14005       OPEN  c3(lv_batch_id);
14006       FETCH c3 BULK COLLECT INTO lb_rowid ;
14007 
14008       IF c3%ROWCOUNT > 0  THEN
14009         v_sql_stmt := 12;
14010         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
14011         UPDATE  msc_st_regions
14012         SET     region_id = msc_st_region_id_s.NEXTVAL
14013         WHERE rowid           = lb_rowid(j);
14014 
14015       v_sql_stmt := 13;
14016         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
14017         INSERT INTO msc_local_id_setup
14018           (local_id,
14019            st_transaction_id,
14020            instance_id,
14021            entity_name,
14022            data_source_type,
14023            char1,
14024            char2,
14025            char3,
14026            char4,
14027            char5,
14028            char6,
14029            char7,
14030            char8,
14031            char9,
14032            char10,
14033            number1,
14034            last_update_date,
14035            last_updated_by,
14036            creation_date,
14037            created_by)
14038         SELECT
14039             region_id,
14040             st_transaction_id,
14041             v_instance_id,
14042             'REGION_ID',
14043             data_source_type,
14044             v_instance_code,
14045             country,
14046             country_code,
14047             state,
14048             state_code,
14049             city,
14050             city_code,
14051             postal_code_from,
14052             postal_code_to,
14053             zone,
14054             region_type,
14055             v_current_date,
14056             v_current_user,
14057             v_current_date,
14058             v_current_user
14059         FROM msc_st_regions
14060         WHERE  rowid            = lb_rowid(j);
14061 
14062       END IF;
14063       CLOSE c3 ;
14064 
14065       lv_sql_stmt :=
14066       ' UPDATE msc_st_regions msr1 '
14067       ||' SET    parent_region_id  = (SELECT msr2.region_id'
14068       ||' FROM   msc_st_regions msr2'
14069       ||' WHERE  msr2.sr_instance_code = msr1.sr_instance_code '
14070       ||' AND    msr2.region_type      = msr1.region_type - 1 '
14071       ||' AND    NVL(msr2.country,   NVL(msr1.country,    '||''''||NULL_CHAR||''''||')) = '
14072       ||'              NVL(msr1.country,     '||''''||NULL_CHAR||''''||') '
14073       ||' AND    NVL(msr2.country_code,  NVL(msr1.country_code,   '||''''||NULL_CHAR||''''||')) = '
14074       ||'             NVL(msr1.country_code,    '||''''||NULL_CHAR||''''||') '
14075       ||' AND    NVL(msr2.state,  NVL(msr1.state,  '||''''||NULL_CHAR||''''||')) = '
14076       ||'            NVL(msr1.state,     '||''''||NULL_CHAR||''''||') '
14077       ||' AND    NVL(msr2.state_code, NVL(msr1.state_code,  '||''''||NULL_CHAR||''''||')) = '
14078       ||'              NVL(msr1.state_code,     '||''''||NULL_CHAR||''''||') '
14079       ||' AND    NVL(msr2.city,  NVL(msr1.city,  '||''''||NULL_CHAR||''''||')) = '
14080       ||'              NVL(msr1.city,     '||''''||NULL_CHAR||''''||') '
14081       ||' AND    NVL(msr2.city_code, NVL(msr1.city_code,  '||''''||NULL_CHAR||''''||')) = '
14082       ||'              NVL(msr1.city_code,     '||''''||NULL_CHAR||''''||') '
14083       ||' AND    msr2.process_flag   = '||G_IN_PROCESS
14084       ||' AND    msr2.region_type in (0,1,2)) '
14085       ||' WHERE  msr1.process_flag   = '||G_IN_PROCESS
14086       ||' AND    sr_instance_code  = :v_instance_code'
14087       ||' AND    region_type in (1,2,3) '
14088       ||' AND    NVL(parent_region_id, '||NULL_VALUE||')= '|| NULL_VALUE
14089       ||' AND    batch_id          = :lv_batch_id';
14090 
14091 
14092          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14093 
14094 
14095       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code, lv_batch_id;
14096 
14097       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
14098                   (p_table_name     => 'MSC_ST_REGIONS',
14099                    p_instance_id    => v_instance_id,
14100                    p_instance_code  => v_instance_code,
14101                    p_process_flag   => G_VALID,
14102                    p_error_text     => lv_error_text,
14103                    p_debug          => v_debug,
14104                    p_batch_id       => lv_batch_id);
14105       IF lv_return <> 0 THEN
14106         RAISE ex_logging_err;
14107       END IF;
14108 
14109       lv_return := MSC_ST_UTIL.LOG_ERROR
14110                    (p_table_name        => 'MSC_ST_REGIONS',
14111                     p_instance_code     => v_instance_code,
14112                     p_row               => lv_column_names,
14113                     p_severity          => G_SEV_ERROR,
14114                     p_message_text      => NULL,
14115                     p_error_text        => lv_error_text,
14116                     p_debug             => v_debug,
14117                     p_batch_id          => lv_batch_id);
14118 
14119       IF lv_return <> 0 THEN
14120         RAISE ex_logging_err;
14121       END IF;
14122 
14123    lv_sql_stmt :=
14124    ' INSERT INTO MSC_REGIONS '
14125   ||' (REGION_ID,'
14126   ||' REGION_TYPE,'
14127   ||' PARENT_REGION_ID,'
14128   ||' COUNTRY_CODE,'
14129   ||' COUNTRY_REGION_CODE,'
14130   ||' STATE_CODE,'
14131   ||' CITY_CODE,'
14132   ||' PORT_FLAG,'
14133   ||' AIRPORT_FLAG,'
14134   ||' ROAD_TERMINAL_FLAG,'
14135   ||' RAIL_TERMINAL_FLAG,'
14136   ||' LONGITUDE,'
14137   ||' LATITUDE,'
14138   ||' TIMEZONE,'
14139   ||' CONTINENT,'
14140   ||' COUNTRY,'
14141   ||' COUNTRY_REGION,'
14142   ||' STATE,'
14143   ||' CITY,'
14144   ||' ZONE,'
14145   ||' ZONE_LEVEL,'
14146   ||' POSTAL_CODE_FROM,'
14147   ||' POSTAL_CODE_TO,'
14148   ||' ALTERNATE_NAME,'
14149   ||' COUNTY,'
14150   ||' SR_INSTANCE_ID,'
14151   ||' ZONE_USAGE,'
14152   ||' CREATED_BY,'
14153   ||' CREATION_DATE,'
14154   ||' LAST_UPDATED_BY,'
14155   ||' LAST_UPDATE_DATE)'
14156   ||' SELECT distinct '
14157   ||' msr.REGION_ID,'
14158   ||' msr.REGION_TYPE,'
14159   ||' msr.PARENT_REGION_ID,'
14160   ||' msr.COUNTRY_CODE,'
14161   ||' msr.COUNTRY_REGION_CODE,'
14162   ||' msr.STATE_CODE,'
14163   ||' msr.CITY_CODE,'
14164   ||' msr.PORT_FLAG,'
14165   ||' msr.AIRPORT_FLAG,'
14166   ||' msr.ROAD_TERMINAL_FLAG,'
14167   ||' msr.RAIL_TERMINAL_FLAG,'
14168   ||' msr.LONGITUDE,'
14169   ||' msr.LATITUDE,'
14170   ||' msr.TIMEZONE,'
14171   ||' msr.CONTINENT,'
14172   ||' msr.COUNTRY,'
14173   ||' msr.COUNTRY_REGION,'
14174   ||' msr.STATE,'
14175   ||' msr.CITY,'
14176   ||' msr.ZONE,'
14177   ||' msr.ZONE_LEVEL,'
14178   ||' msr.POSTAL_CODE_FROM,'
14179   ||' msr.POSTAL_CODE_TO,'
14180   ||' msr.ALTERNATE_NAME,'
14181   ||' msr.COUNTY,'
14182   ||' msr.SR_INSTANCE_ID,'
14183   ||' msr.ZONE_USAGE,'
14184   ||' msr.CREATED_BY,'
14185   ||' msr.CREATION_DATE,'
14186   ||' msr.LAST_UPDATED_BY,'
14187   ||' msr.LAST_UPDATE_DATE'
14188   ||' FROM   MSC_ST_REGIONS msr'
14189   ||' WHERE  msr.batch_id         = :lv_batch_id'
14190   ||' AND    msr.sr_instance_code = :v_instance_code'
14191   ||' AND    msr.process_flag    = '||G_VALID ;
14192 
14193 
14194         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14195 
14196 
14197       EXECUTE IMMEDIATE lv_sql_stmt
14198               USING     lv_batch_id,
14199                         v_instance_code;
14200 
14201 
14202    COMMIT;
14203   END LOOP;
14204 
14205   -- Validation for Zone Regions
14206 
14207   --Getting the error message text
14208     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14209                  (p_app_short_name    => 'MSC',
14210                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
14211                   p_message_text      => lv_message_text,
14212                   p_error_text        => lv_error_text);
14213 
14214     IF lv_return <> 0 THEN
14215       RAISE ex_logging_err;
14216     END IF;
14217 
14218     --Duplicate records check for the records whose source is XML
14219     v_sql_stmt := 01;
14220     lv_sql_stmt :=
14221     'UPDATE     msc_st_zone_regions mszr1 '
14222     ||' SET     process_flag = '||G_ERROR_FLG||','
14223     ||'         error_text   = '||''''||lv_message_text||''''
14224     ||' WHERE   message_id <  (SELECT MAX(message_id)'
14225     ||'         FROM   msc_st_zone_regions mszr2'
14226     ||'         WHERE  mszr2.sr_instance_code  = mszr1.sr_instance_code'
14227     ||'         AND    mszr2.zone = mszr1.zone'
14228     ||'         AND    NVL(mszr2.country, NVL(mszr1.country, '||''''||NULL_CHAR||''''||')) = '
14229     ||'                NVL(mszr1.country, NVL(mszr2.country, '||''''||NULL_CHAR||''''||'))'
14230     ||'         AND    NVL(mszr2.country_code, NVL(mszr1.country_code, '||''''||NULL_CHAR||''''||')) = '
14231     ||'                NVL(mszr1.country_code, NVL(mszr2.country_code, '||''''||NULL_CHAR||''''||'))'
14232     ||'         AND    NVL(mszr2.state, NVL(mszr1.state, '||''''||NULL_CHAR||''''||')) = '
14233     ||'                NVL(mszr1.state, NVL(mszr2.state, '||''''||NULL_CHAR||''''||'))'
14234     ||'         AND    NVL(mszr2.state_code, NVL(mszr1.state_code, '||''''||NULL_CHAR||''''||')) = '
14235     ||'                NVL(mszr1.state_code, NVL(mszr2.state_code, '||''''||NULL_CHAR||''''||'))'
14236     ||'         AND    NVL(mszr2.city, NVL(mszr1.city, '||''''||NULL_CHAR||''''||')) = '
14237     ||'                NVL(mszr1.city, NVL(mszr2.city, '||''''||NULL_CHAR||''''||'))'
14238     ||'         AND    NVL(mszr2.city_code, NVL(mszr1.city_code, '||''''||NULL_CHAR||''''||')) = '
14239     ||'                NVL(mszr1.city_code, NVL(mszr2.city_code, '||''''||NULL_CHAR||''''||'))'
14240     ||'         AND    NVL(mszr2.postal_code_from, NVL(mszr1.postal_code_from, '||''''||NULL_CHAR||''''||')) = '
14241     ||'                NVL(mszr1.postal_code_from, NVL(mszr2.postal_code_from, '||''''||NULL_CHAR||''''||'))'
14242     ||'         AND    NVL(mszr2.postal_code_to, NVL(mszr1.postal_code_to, '||''''||NULL_CHAR||''''||')) = '
14243     ||'                NVL(mszr1.postal_code_to, NVL(mszr2.postal_code_to, '||''''||NULL_CHAR||''''||'))'
14244     ||'         AND    mszr2.process_flag      ='|| G_IN_PROCESS
14245     ||'         AND    NVL(mszr2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
14246     ||' AND     mszr1.process_flag             = '||G_IN_PROCESS
14247     ||' AND     mszr1.sr_instance_code         = :v_instance_code'
14248     ||' AND     NVL(mszr1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
14249 
14250 
14251       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14252 
14253     EXECUTE IMMEDIATE lv_sql_stmt
14254             USING     v_instance_code;
14255 
14256    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14257                  (p_app_short_name    => 'MSC',
14258                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
14259                   p_message_text      => lv_message_text,
14260                   p_error_text        => lv_error_text);
14261 
14262     IF lv_return <> 0 THEN
14263       RAISE ex_logging_err;
14264     END IF;
14265 
14266     --Duplicate records check for the records whose source is other than XML
14267     --Different SQL is used because in XML we can identify the latest records
14268     --whereas in batch load we cannot.
14269     v_sql_stmt := 02;
14270     lv_sql_stmt :=
14271     'UPDATE  msc_st_zone_regions mszr1'
14272     ||' SET     process_flag  = '||G_ERROR_FLG||','
14273     ||'         error_text   = '||''''||lv_message_text||''''
14274     ||' WHERE   EXISTS( SELECT 1 '
14275     ||'         FROM   msc_st_zone_regions mszr2'
14276     ||'         WHERE  mszr2.sr_instance_code  = mszr1.sr_instance_code'
14277     ||'         AND    mszr2.zone = mszr1.zone'
14278     ||'         AND    NVL(mszr2.country, NVL(mszr1.country, '||''''||NULL_CHAR||''''||')) = '
14279     ||'                NVL(mszr1.country, NVL(mszr2.country, '||''''||NULL_CHAR||''''||'))'
14280     ||'         AND    NVL(mszr2.country_code, NVL(mszr1.country_code, '||''''||NULL_CHAR||''''||')) = '
14281     ||'                NVL(mszr1.country_code, NVL(mszr2.country_code, '||''''||NULL_CHAR||''''||'))'
14282     ||'         AND    NVL(mszr2.state, NVL(mszr1.state, '||''''||NULL_CHAR||''''||')) = '
14283     ||'                NVL(mszr1.state, NVL(mszr2.state, '||''''||NULL_CHAR||''''||'))'
14284     ||'         AND    NVL(mszr2.state_code, NVL(mszr1.state_code, '||''''||NULL_CHAR||''''||')) = '
14285     ||'                NVL(mszr1.state_code, NVL(mszr2.state_code, '||''''||NULL_CHAR||''''||'))'
14286     ||'         AND    NVL(mszr2.city, NVL(mszr1.city, '||''''||NULL_CHAR||''''||')) = '
14287     ||'                NVL(mszr1.city, NVL(mszr2.city, '||''''||NULL_CHAR||''''||'))'
14288     ||'         AND    NVL(mszr2.city_code, NVL(mszr1.city_code, '||''''||NULL_CHAR||''''||')) = '
14289     ||'                NVL(mszr1.city_code, NVL(mszr2.city_code, '||''''||NULL_CHAR||''''||'))'
14290     ||'         AND    NVL(mszr2.postal_code_from, NVL(mszr1.postal_code_from, '||''''||NULL_CHAR||''''||')) = '
14291     ||'                NVL(mszr1.postal_code_from, NVL(mszr2.postal_code_from, '||''''||NULL_CHAR||''''||'))'
14292     ||'         AND    NVL(mszr2.postal_code_to, NVL(mszr1.postal_code_to, '||''''||NULL_CHAR||''''||')) = '
14293     ||'                NVL(mszr1.postal_code_to, NVL(mszr2.postal_code_to, '||''''||NULL_CHAR||''''||'))'
14294     ||'         AND    mszr2.process_flag               = '||G_IN_PROCESS
14295     ||'         GROUP BY sr_instance_code, zone, country,country_code, state, state_code, city, city_code, postal_code_from, postal_code_to'
14296     ||'         HAVING COUNT(*) > 1)'
14297     ||' AND     mszr1.process_flag     = '||G_IN_PROCESS
14298     ||' AND     mszr1.sr_instance_code = :v_instance_code'
14299     ||' AND     NVL(mszr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
14300 
14301 
14302       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14303 
14304     EXECUTE IMMEDIATE lv_sql_stmt
14305             USING     v_instance_code;
14306 
14307   lv_column_names :=
14308   'ZONE                           ||''~''||'
14309   ||' COUNTRY                     ||''~''||'
14310   ||' COUNTRY_CODE                ||''~''||'
14311   ||' STATE_CODE            	  ||''~''||'
14312   ||' SR_INSTANCE_CODE            ||''~''||'
14313   ||' STATE                       ||''~''||'
14314   ||' CITY              	  ||''~''||'
14315   ||' CITY_CODE 		  ||''~''||'
14316   ||' POSTAL_CODE_FROM            ||''~''||'
14317   ||' POSTAL_CODE_TO              ||''~''||'
14318   ||'DELETED_FLAG';
14319 
14320   LOOP
14321 
14322       SELECT msc_st_batch_id_s.NEXTVAL
14323       INTO   lv_batch_id
14324       FROM   DUAL;
14325 
14326       lv_sql_stmt :=
14327       'UPDATE    msc_st_zone_regions '
14328       ||' SET    batch_id                       = :lv_batch_id'
14329       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
14330       ||' AND    sr_instance_code               = :v_instance_code'
14331       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
14332       ||' AND    rownum                        <= '||v_batch_size;
14333 
14334 
14335         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14336 
14337       EXECUTE IMMEDIATE lv_sql_stmt
14338               USING     lv_batch_id,
14339                         v_instance_code;
14340 
14341       EXIT WHEN SQL%NOTFOUND ;
14342 
14343 
14344       OPEN c4(lv_batch_id);
14345       FETCH c4 BULK COLLECT INTO lb_rowid;
14346       CLOSE c4;
14347 
14348       v_sql_stmt := 05;
14349       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
14350       UPDATE msc_st_zone_regions
14351       SET  st_transaction_id = msc_st_zone_regions_s.NEXTVAL,
14352            refresh_number    = v_refresh_id,
14353            last_update_date  = v_current_date,
14354            last_updated_by   = v_current_user,
14355            creation_date     = v_current_date,
14356            created_by        = v_current_user
14357       WHERE  rowid           = lb_rowid(j);
14358 
14359       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14360                      (p_app_short_name    => 'MSC',
14361                       p_error_code        => 'MSC_PP_NO_DELETION',
14362                       p_message_text      => lv_message_text,
14363                       p_error_text        => lv_error_text,
14364                       p_token1            => 'TABLE_NAME',
14365                       p_token_value1      => 'MSC_ST_ZONE_REGIONS');
14366 
14367 
14368       -- Deletion is not allowed for this table
14369       v_sql_stmt := 06;
14370       lv_sql_stmt :=
14371       'UPDATE      msc_st_zone_regions '
14372       ||' SET      process_flag     = '||G_ERROR_FLG||','
14373       ||'          error_text       = '||''''||lv_message_text||''''
14374       ||' WHERE    deleted_flag     = '||SYS_YES
14375       ||' AND      process_flag     = '||G_IN_PROCESS
14376       ||' AND      batch_id         = :lv_batch_id'
14377       ||' AND      sr_instance_code = :v_instance_code';
14378 
14379 
14380         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14381 
14382       EXECUTE IMMEDIATE lv_sql_stmt
14383               USING     lv_batch_id,
14384                         v_instance_code;
14385 
14386      -- set the message
14387 
14388       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14389                      (p_app_short_name    => 'MSC',
14390                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
14391                       p_message_text      => lv_message_text,
14392                       p_error_text        => lv_error_text,
14393                       p_token1            => 'COLUMN_NAME',
14394                       p_token_value1      => 'DELETED_FLAG',
14395                       p_token2            => 'DEFAULT_VALUE',
14396                       p_token_value2      => SYS_NO);
14397 
14398       IF lv_return <> 0 THEN
14399         RAISE ex_logging_err;
14400       END IF;
14401 
14402       -- Check DELETED_FLAG column has valid value or not and if not set it to SYS_NO
14403       -- Log error procedure inserts warning and also defaults the deleted flag
14404 
14405       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
14406                       ||' NOT IN(1,2)';
14407 
14408       lv_return := MSC_ST_UTIL.LOG_ERROR
14409                         (p_table_name        => 'MSC_ST_ZONE_REGIONS',
14410                          p_instance_code     => v_instance_code,
14411                          p_row               => lv_column_names,
14412                          p_severity          => G_SEV_WARNING,
14413                          p_message_text      => lv_message_text,
14414                          p_error_text        => lv_error_text,
14415                          p_batch_id          => lv_batch_id,
14416                          p_where_str         => lv_where_str,
14417                          p_col_name          => 'DELETED_FLAG',
14418                          p_debug             => v_debug,
14419                          p_default_value     => SYS_NO);
14420 
14421       IF lv_return <> 0 THEN
14422         RAISE ex_logging_err;
14423       END IF;
14424 
14425       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14426                      (p_app_short_name    => 'MSC',
14427                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
14428                       p_message_text      => lv_message_text,
14429                       p_error_text        => lv_error_text,
14430                       p_token1            => 'COLUMN_NAME',
14431                       p_token_value1      => 'ZONE OR COUNTRY OR COUNTRY_CODE');
14432 
14433       IF lv_return <> 0 THEN
14434         RAISE ex_logging_err;
14435       END IF;
14436 
14437       -- Error out records where zone/country/country_code is NULL
14438 
14439       lv_sql_stmt :=
14440       'UPDATE    msc_st_zone_regions '
14441       ||' SET    process_flag     = '||G_ERROR_FLG||','
14442       ||'        error_text   = '||''''||lv_message_text||''''
14443       ||' WHERE  sr_instance_code =  :v_instance_code'
14444       ||' AND    batch_id         =  :lv_batch_id'
14445       ||' AND    process_flag     = '||G_IN_PROCESS
14446       ||' AND    (NVL(zone,'||''''||NULL_CHAR||''''||')'
14447       ||'        = '||''''||NULL_CHAR||''''
14448       ||' OR     NVL(country,'||''''||NULL_CHAR||''''||') '
14449       ||'        = '||''''||NULL_CHAR||''''
14450       ||' OR     NVL(country_code,'||''''||NULL_CHAR||''''||') '
14451       ||'        = '||''''||NULL_CHAR||''''||')';
14452 
14453 
14454         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14455 
14456       EXECUTE IMMEDIATE lv_sql_stmt
14457               USING     v_instance_code,
14458                         lv_batch_id;
14459 
14460 
14461 
14462      lv_sql_stmt :=
14463       'UPDATE msc_st_zone_regions mszr'
14464       ||' SET region_id     = (SELECT local_id'
14465       ||'       FROM   msc_local_id_setup mls'
14466       ||'       WHERE  mls.char1 = mszr.sr_instance_code'
14467       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
14468       ||'              NVL(mszr.country,     '||''''||NULL_CHAR||''''||') '
14469       ||'       AND    NVL(mls.char3,       '||''''||NULL_CHAR||''''||') = '
14470       ||'              NVL(mszr.country_code,    '||''''||NULL_CHAR||''''||') '
14471       ||'       AND    NVL(mls.char4,       '||''''||NULL_CHAR||''''||') = '
14472       ||'              NVL(mszr.state,     '||''''||NULL_CHAR||''''||') '
14473       ||'       AND    NVL(mls.char5,       '||''''||NULL_CHAR||''''||') = '
14474       ||'              NVL(mszr.state_code,     '||''''||NULL_CHAR||''''||') '
14475       ||'       AND    NVL(mls.char6,       '||''''||NULL_CHAR||''''||') = '
14476       ||'              NVL(mszr.city,     '||''''||NULL_CHAR||''''||') '
14477       ||'       AND    NVL(mls.char7,       '||''''||NULL_CHAR||''''||') = '
14478       ||'              NVL(mszr.city_code,     '||''''||NULL_CHAR||''''||') '
14479       ||'       AND    NVL(mls.char8,       '||''''||NULL_CHAR||''''||') = '
14480       ||'              NVL(mszr.postal_code_from,     '||''''||NULL_CHAR||''''||') '
14481       ||'       AND    NVL(mls.char9,       '||''''||NULL_CHAR||''''||') = '
14482       ||'              NVL(mszr.postal_code_to,     '||''''||NULL_CHAR||''''||') '
14483       ||'       AND    mls.number1 in (0,1,2,3) '
14484       ||'       AND    mls.entity_name = ''REGION_ID'' )'
14485       ||' WHERE  process_flag                   = '||G_IN_PROCESS
14486       ||' AND    NVL(region_id,'||NULL_VALUE||')= '||NULL_VALUE
14487       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
14488       ||' AND    sr_instance_code               = :v_instance_code';
14489 
14490 
14491         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14492 
14493       EXECUTE IMMEDIATE lv_sql_stmt
14494               USING     lv_batch_id,
14495                         v_instance_code;
14496 
14497       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14498                      (p_app_short_name    => 'MSC',
14499                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
14500                       p_message_text      => lv_message_text,
14501                       p_error_text        => lv_error_text,
14502                       p_token1            => 'COLUMN_NAME',
14503                       p_token_value1      => 'REGION_ID');
14504 
14505       IF lv_return <> 0 THEN
14506         RAISE ex_logging_err;
14507       END IF;
14508 
14509       -- Error out records where region_id is null
14510 
14511       lv_sql_stmt :=
14512       'UPDATE    msc_st_zone_regions '
14513       ||' SET    process_flag     = '||G_ERROR_FLG||','
14514       ||'        error_text   = '||''''||lv_message_text||''''
14515       ||' WHERE  sr_instance_code =  :v_instance_code'
14516       ||' AND    batch_id         =  :lv_batch_id'
14517       ||' AND    process_flag     = '||G_IN_PROCESS
14518       ||' AND    NVL(region_id,'||NULL_VALUE||')= '||NULL_VALUE  ;
14519 
14520 
14521         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14522 
14523       EXECUTE IMMEDIATE lv_sql_stmt
14524               USING     v_instance_code,
14525                         lv_batch_id;
14526 
14527     lv_sql_stmt :=
14528       'UPDATE msc_st_zone_regions mszr'
14529       ||' SET parent_region_id     = (SELECT local_id'
14530       ||'       FROM   msc_local_id_setup mls'
14531       ||'       WHERE  mls.char1 = mszr.sr_instance_code'
14532       ||'       AND    NVL(mls.char10,       '||''''||NULL_CHAR||''''||') = '
14533       ||'              NVL(mszr.zone,     '||''''||NULL_CHAR||''''||') '
14534       ||'       AND    mls.number1 = 10 '
14535       ||'       AND    mls.entity_name = ''REGION_ID'' )'
14536       ||' WHERE  process_flag                   = '||G_IN_PROCESS
14537       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
14538       ||' AND    NVL(parent_region_id,'||NULL_VALUE||')= '||NULL_VALUE
14539       ||' AND    sr_instance_code               = :v_instance_code';
14540 
14541 
14542         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14543 
14544       EXECUTE IMMEDIATE lv_sql_stmt
14545               USING     lv_batch_id,
14546                         v_instance_code;
14547 
14548       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14549                      (p_app_short_name    => 'MSC',
14550                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
14551                       p_message_text      => lv_message_text,
14552                       p_error_text        => lv_error_text,
14553                       p_token1            => 'COLUMN_NAME',
14554                       p_token_value1      => 'PARENT_REGION_ID');
14555 
14556       IF lv_return <> 0 THEN
14557         RAISE ex_logging_err;
14558       END IF;
14559 
14560       -- Error out records where parent_region_id is null
14561 
14562       lv_sql_stmt :=
14563       'UPDATE    msc_st_zone_regions '
14564       ||' SET    process_flag     = '||G_ERROR_FLG||','
14565       ||'        error_text   = '||''''||lv_message_text||''''
14566       ||' WHERE  sr_instance_code =  :v_instance_code'
14567       ||' AND    batch_id         =  :lv_batch_id'
14568       ||' AND    process_flag     = '||G_IN_PROCESS
14569       ||' AND    NVL(parent_region_id,'||NULL_VALUE||')= '||NULL_VALUE  ;
14570 
14571 
14572         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14573 
14574       EXECUTE IMMEDIATE lv_sql_stmt
14575               USING     v_instance_code,
14576                         lv_batch_id;
14577 
14578      -- set the default value of party_id to -1
14579 
14580      lv_sql_stmt :=
14581       'UPDATE    msc_st_zone_regions '
14582       ||' SET    party_id     = -1 '
14583       ||' WHERE  sr_instance_code =  :v_instance_code'
14584       ||' AND    batch_id         =  :lv_batch_id'
14585       ||' AND    process_flag     = '||G_IN_PROCESS
14586       ||' AND    NVL(party_id,'||NULL_VALUE||')= '||NULL_VALUE  ;
14587 
14588 
14589         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14590 
14591       EXECUTE IMMEDIATE lv_sql_stmt
14592               USING     v_instance_code,
14593                         lv_batch_id;
14594 
14595 
14596     -- Deriving zone_region_id
14597 
14598       lv_sql_stmt :=
14599       'UPDATE msc_st_zone_regions mszr'
14600       ||' SET zone_region_id     = (SELECT local_id'
14601       ||'       FROM   msc_local_id_setup mls'
14602       ||'       WHERE  mls.char1 = mszr.sr_instance_code'
14603       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
14604       ||'              NVL(mszr.country,     '||''''||NULL_CHAR||''''||') '
14605       ||'       AND    NVL(mls.char3,       '||''''||NULL_CHAR||''''||') = '
14606       ||'              NVL(mszr.country_code,    '||''''||NULL_CHAR||''''||') '
14607       ||'       AND    NVL(mls.char4,       '||''''||NULL_CHAR||''''||') = '
14608       ||'              NVL(mszr.state,     '||''''||NULL_CHAR||''''||') '
14609       ||'       AND    NVL(mls.char5,       '||''''||NULL_CHAR||''''||') = '
14610       ||'              NVL(mszr.state_code,     '||''''||NULL_CHAR||''''||') '
14611       ||'       AND    NVL(mls.char6,       '||''''||NULL_CHAR||''''||') = '
14612       ||'              NVL(mszr.city,     '||''''||NULL_CHAR||''''||') '
14613       ||'       AND    NVL(mls.char7,       '||''''||NULL_CHAR||''''||') = '
14614       ||'              NVL(mszr.city_code,     '||''''||NULL_CHAR||''''||') '
14615       ||'       AND    NVL(mls.char8,       '||''''||NULL_CHAR||''''||') = '
14616       ||'              NVL(mszr.postal_code_from,     '||''''||NULL_CHAR||''''||') '
14617       ||'       AND    NVL(mls.char9,       '||''''||NULL_CHAR||''''||') = '
14618       ||'              NVL(mszr.postal_code_to,     '||''''||NULL_CHAR||''''||') '
14619       ||'       AND    NVL(mls.char10,       '||''''||NULL_CHAR||''''||') = '
14620       ||'              NVL(mszr.zone,     '||''''||NULL_CHAR||''''||') '
14621       ||'       AND    mls.entity_name = ''ZONE_REGION_ID'' )'
14622       ||' WHERE  process_flag                   = '||G_IN_PROCESS
14623       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
14624       ||' AND    sr_instance_code               = :v_instance_code';
14625 
14626 
14627         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14628 
14629       EXECUTE IMMEDIATE lv_sql_stmt
14630               USING     lv_batch_id,
14631                         v_instance_code;
14632 
14633 
14634      --Call to customised validation.
14635       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
14636         (ERRBUF         => lv_error_text,
14637          RETCODE        => lv_return,
14638          pBatchID       => lv_batch_id,
14639          pInstanceCode  => v_instance_code,
14640          pEntityName    => 'MSC_ST_ZONE_REGIONS',
14641          pInstanceID    => v_instance_id);
14642 
14643       IF NVL(lv_return,0) <> 0 THEN
14644         RAISE ex_logging_err;
14645       END IF;
14646 
14647       --Generation of zone_region_id
14648       OPEN  c5(lv_batch_id);
14649       FETCH c5 BULK COLLECT INTO lb_rowid ;
14650 
14651       IF c5%ROWCOUNT > 0  THEN
14652         v_sql_stmt := 12;
14653         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
14654         UPDATE  msc_st_zone_regions
14655         SET     zone_region_id = msc_st_zone_region_id_s.NEXTVAL
14656         WHERE rowid           = lb_rowid(j);
14657 
14658       v_sql_stmt := 13;
14659         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
14660         INSERT INTO msc_local_id_setup
14661           (local_id,
14662            st_transaction_id,
14663            instance_id,
14664            entity_name,
14665            data_source_type,
14666            char1,
14667            char2,
14668            char3,
14669            char4,
14670            char5,
14671            char6,
14672            char7,
14673            char8,
14674            char9,
14675            char10,
14676            last_update_date,
14677            last_updated_by,
14678            creation_date,
14679            created_by)
14680         SELECT
14681             zone_region_id,
14682             st_transaction_id,
14683             v_instance_id,
14684             'ZONE_REGION_ID',
14685             data_source_type,
14686             v_instance_code,
14687             country,
14688             country_code,
14689             state,
14690             state_code,
14691             city,
14692             city_code,
14693             postal_code_from,
14694             postal_code_to,
14695             zone,
14696             v_current_date,
14697             v_current_user,
14698             v_current_date,
14699             v_current_user
14700         FROM msc_st_zone_regions
14701         WHERE  rowid            = lb_rowid(j);
14702 
14703       END IF;
14704       CLOSE c5 ;
14705 
14706       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
14707                   (p_table_name     => 'MSC_ST_ZONE_REGIONS',
14708                    p_instance_id    => v_instance_id,
14709                    p_instance_code  => v_instance_code,
14710                    p_process_flag   => G_VALID,
14711                    p_error_text     => lv_error_text,
14712                    p_debug          => v_debug,
14713                    p_batch_id       => lv_batch_id);
14714       IF lv_return <> 0 THEN
14715         RAISE ex_logging_err;
14716       END IF;
14717 
14718       lv_return := MSC_ST_UTIL.LOG_ERROR
14719                    (p_table_name        => 'MSC_ST_ZONE_REGIONS',
14720                     p_instance_code     => v_instance_code,
14721                     p_row               => lv_column_names,
14722                     p_severity          => G_SEV_ERROR,
14723                     p_message_text      => NULL,
14724                     p_error_text        => lv_error_text,
14725                     p_debug             => v_debug,
14726                     p_batch_id          => lv_batch_id);
14727 
14728       IF lv_return <> 0 THEN
14729         RAISE ex_logging_err;
14730       END IF;
14731 
14732       lv_sql_stmt :=
14733    ' INSERT INTO MSC_ZONE_REGIONS '
14734   ||' (ZONE_REGION_ID,'
14735   ||' REGION_ID,'
14736   ||' PARENT_REGION_ID,'
14737   ||' PARTY_ID,'
14738   ||' SR_INSTANCE_ID,'
14739   ||' CREATED_BY,'
14740   ||' CREATION_DATE,'
14741   ||' LAST_UPDATED_BY,'
14742   ||' LAST_UPDATE_DATE)'
14743   ||' SELECT distinct '
14744   ||' msr.ZONE_REGION_ID,'
14745   ||' msr.REGION_ID,'
14746   ||' msr.PARENT_REGION_ID,'
14747   ||' msr.PARTY_ID, '
14748   ||' msr.SR_INSTANCE_ID,'
14749   ||' msr.CREATED_BY,'
14750   ||' msr.CREATION_DATE,'
14751   ||' msr.LAST_UPDATED_BY,'
14752   ||' msr.LAST_UPDATE_DATE'
14753   ||' FROM   MSC_ST_ZONE_REGIONS msr'
14754   ||' WHERE  msr.batch_id         = :lv_batch_id'
14755   ||' AND    msr.sr_instance_code = :v_instance_code'
14756   ||' AND    msr.process_flag    = '||G_VALID
14757   ||' AND    NOT EXISTS'
14758   ||'            ( SELECT 1 FROM MSC_ZONE_REGIONS mzr'
14759   ||'              WHERE  mzr.zone_region_id = msr.zone_region_id'
14760   ||'              AND    mzr.sr_instance_id = ' ||v_instance_id ||')';
14761 
14762 
14763         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14764 
14765 
14766       EXECUTE IMMEDIATE lv_sql_stmt
14767               USING     lv_batch_id,
14768                         v_instance_code;
14769 
14770    COMMIT;
14771    END LOOP;
14772 
14773    -- validation for Region Locations
14774 
14775     --Getting the error message text
14776     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14777                  (p_app_short_name    => 'MSC',
14778                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
14779                   p_message_text      => lv_message_text,
14780                   p_error_text        => lv_error_text);
14781 
14782     IF lv_return <> 0 THEN
14783       RAISE ex_logging_err;
14784     END IF;
14785 
14786     --Duplicate records check for the records whose source is XML
14787     v_sql_stmt := 01;
14788     lv_sql_stmt :=
14789     'UPDATE     msc_st_region_locations msr1 '
14790     ||' SET     process_flag = '||G_ERROR_FLG||','
14791     ||'         error_text   = '||''''||lv_message_text||''''
14792     ||' WHERE   message_id <  (SELECT MAX(message_id)'
14793     ||'         FROM   msc_st_region_locations msr2'
14794     ||'         WHERE  msr2.sr_instance_code  = msr1.sr_instance_code'
14795     ||'         AND    NVL(msr2.region_type, '||NULL_VALUE||') = NVL(msr1.region_type, '||NULL_VALUE||')'
14796     ||'         AND    NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
14797     ||'                NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
14798     ||'         AND    NVL(msr2.country,  '||''''||NULL_CHAR||''''||') = '
14799     ||'                NVL(msr1.country,  '||''''||NULL_CHAR||''''||')'
14800     ||'         AND    NVL(msr2.country_code,  '||''''||NULL_CHAR||''''||') = '
14801     ||'                NVL(msr1.country_code,  '||''''||NULL_CHAR||''''||')'
14802     ||'         AND    NVL(msr2.state,  '||''''||NULL_CHAR||''''||') = '
14803     ||'                NVL(msr1.state,  '||''''||NULL_CHAR||''''||')'
14804     ||'         AND    NVL(msr2.state_code,  '||''''||NULL_CHAR||''''||') = '
14805     ||'                NVL(msr1.state_code,  '||''''||NULL_CHAR||''''||')'
14806     ||'         AND    NVL(msr2.city,  '||''''||NULL_CHAR||''''||') = '
14807     ||'                NVL(msr1.city,  '||''''||NULL_CHAR||''''||')'
14808     ||'         AND    NVL(msr2.city_code,  '||''''||NULL_CHAR||''''||') = '
14809     ||'                NVL(msr1.city_code,  '||''''||NULL_CHAR||''''||')'
14810     ||'         AND    NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||') = '
14811     ||'                NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
14812     ||'         AND    NVL(msr2.postal_code_to,  '||''''||NULL_CHAR||''''||') = '
14813     ||'                NVL(msr1.postal_code_to,  '||''''||NULL_CHAR||''''||')'
14814     ||'         AND    NVL(msr2.location_code,  '||''''||NULL_CHAR||''''||') = '
14815     ||'                NVL(msr1.location_code,  '||''''||NULL_CHAR||''''||')'
14816     ||'         AND    msr2.process_flag      ='|| G_IN_PROCESS
14817     ||'         AND    NVL(msr2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
14818     ||' AND     msr1.process_flag             = '||G_IN_PROCESS
14819     ||' AND     msr1.sr_instance_code         = :v_instance_code'
14820     ||' AND     NVL(msr1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
14821 
14822 
14823       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14824 
14825     EXECUTE IMMEDIATE lv_sql_stmt
14826             USING     v_instance_code;
14827 
14828 
14829    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14830                  (p_app_short_name    => 'MSC',
14831                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
14832                   p_message_text      => lv_message_text,
14833                   p_error_text        => lv_error_text);
14834 
14835     IF lv_return <> 0 THEN
14836       RAISE ex_logging_err;
14837     END IF;
14838 
14839     --Duplicate records check for the records whose source is other than XML
14840     --Different SQL is used because in XML we can identify the latest records
14841     --whereas in batch load we cannot.
14842     v_sql_stmt := 02;
14843     lv_sql_stmt :=
14844     'UPDATE  msc_st_region_locations msr1'
14845     ||' SET     process_flag  = '||G_ERROR_FLG||','
14846     ||'         error_text   = '||''''||lv_message_text||''''
14847     ||' WHERE   EXISTS( SELECT 1 '
14848     ||'         FROM   msc_st_region_locations msr2'
14849     ||'         WHERE  msr2.sr_instance_code           = msr1.sr_instance_code'
14850     ||'         AND    NVL(msr2.region_type, '||NULL_VALUE||') = NVL(msr1.region_type,'||NULL_VALUE||')'
14851     ||'         AND    NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
14852     ||'                NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
14853     ||'         AND    NVL(msr2.country,  '||''''||NULL_CHAR||''''||') = '
14854     ||'                NVL(msr1.country,  '||''''||NULL_CHAR||''''||')'
14855     ||'         AND    NVL(msr2.country_code,  '||''''||NULL_CHAR||''''||') = '
14856     ||'                NVL(msr1.country_code,  '||''''||NULL_CHAR||''''||')'
14857     ||'         AND    NVL(msr2.state,  '||''''||NULL_CHAR||''''||') = '
14858     ||'                NVL(msr1.state,  '||''''||NULL_CHAR||''''||')'
14859     ||'         AND    NVL(msr2.state_code,  '||''''||NULL_CHAR||''''||') = '
14860     ||'                NVL(msr1.state_code,  '||''''||NULL_CHAR||''''||')'
14861     ||'         AND    NVL(msr2.city,  '||''''||NULL_CHAR||''''||') = '
14862     ||'                NVL(msr1.city,  '||''''||NULL_CHAR||''''||')'
14863     ||'         AND    NVL(msr2.city_code,  '||''''||NULL_CHAR||''''||') = '
14864     ||'                NVL(msr1.city_code,  '||''''||NULL_CHAR||''''||')'
14865     ||'         AND    NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||') = '
14866     ||'                NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
14867     ||'         AND    NVL(msr2.postal_code_to,  '||''''||NULL_CHAR||''''||') = '
14868     ||'                NVL(msr1.postal_code_to,  '||''''||NULL_CHAR||''''||')'
14869     ||'         AND    NVL(msr2.location_code,  '||''''||NULL_CHAR||''''||') = '
14870     ||'                NVL(msr1.location_code,  '||''''||NULL_CHAR||''''||')'
14871     ||'         AND    msr2.process_flag               = '||G_IN_PROCESS
14872     ||'         GROUP BY sr_instance_code, region_type, company_name,country,country_code, state, state_code, city, city_code, postal_code_from, postal_code_to, location_code'
14873     ||'         HAVING COUNT(*) > 1)'
14874     ||' AND     msr1.process_flag     = '||G_IN_PROCESS
14875     ||' AND     msr1.sr_instance_code = :v_instance_code'
14876     ||' AND     NVL(msr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
14877 
14878 
14879       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14880 
14881     EXECUTE IMMEDIATE lv_sql_stmt
14882             USING     v_instance_code;
14883 
14884 
14885 
14886     lv_column_names :=
14887   'REGION_TYPE                    ||''~''||'
14888   ||' COUNTRY                     ||''~''||'
14889   ||' COUNTRY_CODE                ||''~''||'
14890   ||' STATE_CODE            	  ||''~''||'
14891   ||' SR_INSTANCE_CODE            ||''~''||'
14892   ||' STATE                       ||''~''||'
14893   ||' CITY              	  ||''~''||'
14894   ||' CITY_CODE 		  ||''~''||'
14895   ||' POSTAL_CODE_FROM            ||''~''||'
14896   ||' POSTAL_CODE_TO              ||''~''||'
14897   ||' LOCATION_CODE               ||''~''||'
14898   ||' DELETED_FLAG                ||''~''||'
14899   ||' COMPANY_NAME ' ;
14900 
14901   -- Now processing by batch
14902    LOOP
14903       v_sql_stmt := 03;
14904       lv_cursor_stmt :=
14905       ' SELECT msc_st_batch_id_s.NEXTVAL '
14906       ||' FROM   dual';
14907 
14908       OPEN  c1 FOR lv_cursor_stmt;
14909       FETCH c1 INTO lv_batch_id;
14910       CLOSE c1;
14911 
14912       lv_sql_stmt :=
14913       ' UPDATE msc_st_region_locations '
14914       ||' SET    batch_id  = :lv_batch_id'
14915       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
14916       ||' AND    sr_instance_code               = :v_instance_code'
14917       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
14918       ||' AND    rownum                        <= '||v_batch_size;
14919 
14920 
14921          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14922 
14923       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
14924 
14925       EXIT WHEN SQL%NOTFOUND;
14926 
14927       OPEN c6(lv_batch_id);
14928       FETCH c6 BULK COLLECT INTO lb_rowid;
14929       CLOSE c6;
14930 
14931 
14932       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
14933       UPDATE msc_st_region_locations
14934       SET  st_transaction_id = msc_st_region_locations_s.NEXTVAL,
14935            refresh_number    = v_refresh_id,
14936            last_update_date  = v_current_date,
14937            last_updated_by   = v_current_user,
14938            creation_date     = v_current_date,
14939            created_by        = v_current_user
14940       WHERE  rowid           = lb_rowid(j);
14941 
14942       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14943                      (p_app_short_name    => 'MSC',
14944                       p_error_code        => 'MSC_PP_NO_DELETION',
14945                       p_message_text      => lv_message_text,
14946                       p_error_text        => lv_error_text,
14947                       p_token1            => 'TABLE_NAME',
14948                       p_token_value1      => 'MSC_ST_REGION_LOCATIONS');
14949 
14950 
14951       -- Deletion is not allowed for this table
14952       v_sql_stmt := 06;
14953       lv_sql_stmt :=
14954       'UPDATE      msc_st_region_locations '
14955       ||' SET      process_flag     = '||G_ERROR_FLG||','
14956       ||'          error_text       = '||''''||lv_message_text||''''
14957       ||' WHERE    deleted_flag     = '||SYS_YES
14958       ||' AND      process_flag     = '||G_IN_PROCESS
14959       ||' AND      batch_id         = :lv_batch_id'
14960       ||' AND      sr_instance_code = :v_instance_code';
14961 
14962 
14963         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
14964 
14965       EXECUTE IMMEDIATE lv_sql_stmt
14966               USING     lv_batch_id,
14967                         v_instance_code;
14968 
14969      -- set the message
14970 
14971       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
14972                      (p_app_short_name    => 'MSC',
14973                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
14974                       p_message_text      => lv_message_text,
14975                       p_error_text        => lv_error_text,
14976                       p_token1            => 'COLUMN_NAME',
14977                       p_token_value1      => 'DELETED_FLAG',
14978                       p_token2            => 'DEFAULT_VALUE',
14979                       p_token_value2      => SYS_NO);
14980 
14981       IF lv_return <> 0 THEN
14982         RAISE ex_logging_err;
14983       END IF;
14984 
14985       -- Check DELETED_FLAG column has valid value or not and if not set it to SYS_NO
14986       -- Log error procedure inserts warning and also defaults the deleted flag
14987 
14988       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
14989                       ||' NOT IN(1,2)';
14990 
14991       lv_return := MSC_ST_UTIL.LOG_ERROR
14992                         (p_table_name        => 'MSC_ST_REGION_LOCATIONS',
14993                          p_instance_code     => v_instance_code,
14994                          p_row               => lv_column_names,
14995                          p_severity          => G_SEV_WARNING,
14996                          p_message_text      => lv_message_text,
14997                          p_error_text        => lv_error_text,
14998                          p_batch_id          => lv_batch_id,
14999                          p_where_str         => lv_where_str,
15000                          p_col_name          => 'DELETED_FLAG',
15001                          p_debug             => v_debug,
15002                          p_default_value     => SYS_NO);
15003 
15004       IF lv_return <> 0 THEN
15005         RAISE ex_logging_err;
15006       END IF;
15007 
15008       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15009                      (p_app_short_name    => 'MSC',
15010                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
15011                       p_message_text      => lv_message_text,
15012                       p_error_text        => lv_error_text,
15013                       p_token1            => 'COLUMN_NAME',
15014                       p_token_value1      => 'LOCATION_CODE');
15015 
15016       IF lv_return <> 0 THEN
15017         RAISE ex_logging_err;
15018       END IF;
15019 
15020       -- Error out records where location_code is NULL
15021 
15022       lv_sql_stmt :=
15023       'UPDATE    msc_st_region_locations '
15024       ||' SET    process_flag     = '||G_ERROR_FLG||','
15025       ||'        error_text   = '||''''||lv_message_text||''''
15026       ||' WHERE  sr_instance_code =  :v_instance_code'
15027       ||' AND    batch_id         =  :lv_batch_id'
15028       ||' AND    process_flag     = '||G_IN_PROCESS
15029       ||' AND    NVL(location_code,'||''''||NULL_CHAR||''''||') '
15030       ||'        =  '||''''||NULL_CHAR||'''';
15031 
15032 
15033         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15034 
15035       EXECUTE IMMEDIATE lv_sql_stmt
15036               USING     v_instance_code,
15037                         lv_batch_id;
15038 
15039 
15040     -- Derive region_id
15041 
15042     lv_sql_stmt :=
15043       'UPDATE msc_st_region_locations msr'
15044       ||' SET region_id     = (SELECT local_id'
15045       ||'       FROM   msc_local_id_setup mls'
15046       ||'       WHERE  mls.char1 = msr.sr_instance_code'
15047       ||'       AND    NVL(mls.number1, '||NULL_VALUE||') = NVL(msr.region_type,'||NULL_VALUE||')'
15048       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
15049       ||'              NVL(msr.country,     '||''''||NULL_CHAR||''''||') '
15050       ||'       AND    NVL(mls.char3,       '||''''||NULL_CHAR||''''||') = '
15051       ||'              NVL(msr.country_code,    '||''''||NULL_CHAR||''''||') '
15052       ||'       AND    NVL(mls.char4,       '||''''||NULL_CHAR||''''||') = '
15053       ||'              NVL(msr.state,     '||''''||NULL_CHAR||''''||') '
15054       ||'       AND    NVL(mls.char5,       '||''''||NULL_CHAR||''''||') = '
15055       ||'              NVL(msr.state_code,     '||''''||NULL_CHAR||''''||') '
15056       ||'       AND    NVL(mls.char6,       '||''''||NULL_CHAR||''''||') = '
15057       ||'              NVL(msr.city,     '||''''||NULL_CHAR||''''||') '
15058       ||'       AND    NVL(mls.char7,       '||''''||NULL_CHAR||''''||') = '
15059       ||'              NVL(msr.city_code,     '||''''||NULL_CHAR||''''||') '
15060       ||'       AND    NVL(mls.char8,       '||''''||NULL_CHAR||''''||') = '
15061       ||'              NVL(msr.postal_code_from,     '||''''||NULL_CHAR||''''||') '
15062       ||'       AND    NVL(mls.char9,       '||''''||NULL_CHAR||''''||') = '
15063       ||'              NVL(msr.postal_code_to,     '||''''||NULL_CHAR||''''||') '
15064       ||'       AND    mls.number1 in (0,1,2,3) '
15065       ||'       AND    mls.entity_name = ''REGION_ID'' )'
15066       ||' WHERE  process_flag                   = '||G_IN_PROCESS
15067       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
15068       ||' AND    msr.region_type in (0,1,2,3) '
15069       ||' AND    sr_instance_code               = :v_instance_code';
15070 
15071 
15072         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15073 
15074       EXECUTE IMMEDIATE lv_sql_stmt
15075               USING     lv_batch_id,
15076                         v_instance_code;
15077 
15078       --Deriving location_id
15079       lv_sql_stmt :=
15080       'UPDATE msc_st_region_locations msr'
15081       ||' SET   location_id              = (SELECT local_id'
15082       ||'       FROM   msc_local_id_setup mls'
15083       ||'       WHERE  mls.char1       = msr.sr_instance_code'
15084       ||'       AND    mls.char3       = msr.location_code'
15085       ||'       AND    mls.entity_name = ''LOCATION_ID'' )'
15086       ||' WHERE  process_flag                   = '||G_IN_PROCESS
15087       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
15088       ||' AND    sr_instance_code               = :v_instance_code';
15089 
15090 
15091         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15092 
15093       EXECUTE IMMEDIATE lv_sql_stmt
15094               USING     lv_batch_id,
15095                         v_instance_code;
15096 
15097 
15098       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15099                      (p_app_short_name    => 'MSC',
15100                       p_error_code        => 'MSC_PP_NO_UPDATION',
15101                       p_message_text      => lv_message_text,
15102                       p_error_text        => lv_error_text,
15103                       p_token1            => 'TABLE_NAME',
15104                       p_token_value1      => 'MSC_ST_REGION_LOCATIONS');
15105 
15106 
15107     -- Error out the record if same region locations already exists in ODS
15108     -- As we do not allow region locations incremental update
15109     -- It has to be purged before laoded again
15110 
15111       lv_sql_stmt :=
15112       'UPDATE      msc_st_region_locations msrl'
15113       ||' SET      msrl.process_flag     = '||G_ERROR_FLG||','
15114       ||'          msrl.error_text       = '||''''||lv_message_text||''''
15115       ||' WHERE    msrl.batch_id         = :lv_batch_id'
15116       ||' AND      msrl.sr_instance_code = :v_instance_code'
15117       ||' AND      EXISTS ( SELECT 1'
15118       ||'          FROM     msc_region_locations mrl'
15119       ||'          WHERE    mrl.location_id = nvl(msrl.location_id ,'||NULL_VALUE||') '
15120       ||'          AND      nvl(mrl.region_id, '||NULL_VALUE||') = nvl(msrl.region_id, '||NULL_VALUE||')'
15121       ||'          AND      mrl.sr_instance_id = :v_instance_id)';
15122 
15123         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15124 
15125       EXECUTE IMMEDIATE lv_sql_stmt
15126               USING     lv_batch_id,
15127                         v_instance_code,
15128                         v_instance_id;
15129 
15130        --Call to customised validation.
15131       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
15132         (ERRBUF         => lv_error_text,
15133          RETCODE        => lv_return,
15134          pBatchID       => lv_batch_id,
15135          pInstanceCode  => v_instance_code,
15136          pEntityName    => 'MSC_ST_REGION_LOCATIONS',
15137          pInstanceID    => v_instance_id);
15138 
15139       IF NVL(lv_return,0) <> 0 THEN
15140         RAISE ex_logging_err;
15141       END IF;
15142 
15143       OPEN  c7(lv_batch_id);
15144       FETCH c7 BULK COLLECT INTO lb_rowid ;
15145 
15146       IF c7%ROWCOUNT > 0  THEN
15147         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
15148         UPDATE msc_st_region_locations
15149         SET    location_id  = msc_st_location_id_s.NEXTVAL
15150         WHERE  rowid          = lb_rowid(j);
15151 
15152         -- Insert record in LID
15153         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
15154         INSERT INTO msc_local_id_setup
15155          (local_id,
15156           st_transaction_id,
15157           instance_id,
15158           entity_name,
15159           data_source_type,
15160           char1,
15161           char3,
15162           last_update_date,
15163           last_updated_by,
15164           creation_date,
15165           created_by)
15166         SELECT
15167           location_id,
15168           st_transaction_id,
15169           v_instance_id,
15170           'LOCATION_ID',
15171           data_source_type,
15172           v_instance_code,
15173           location_code,
15174           v_current_date,
15175           v_current_user,
15176           v_current_date,
15177           v_current_user
15178         FROM  msc_st_region_locations
15179         WHERE rowid            = lb_rowid(j);
15180       END IF;
15181       CLOSE c7;
15182 
15183       --Deriving location_id
15184 
15185       lv_sql_stmt :=
15186       'UPDATE msc_st_region_locations msr'
15187       ||' SET   location_id              = (SELECT local_id'
15188       ||'       FROM   msc_local_id_setup mls'
15189       ||'       WHERE  mls.char1       = msr.sr_instance_code'
15190       ||'       AND    mls.char3       = msr.location_code'
15191       ||'       AND    mls.entity_name = ''LOCATION_ID'' )'
15192       ||' WHERE  process_flag                   = '||G_IN_PROCESS
15193       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
15194       ||' AND    sr_instance_code               = :v_instance_code';
15195 
15196 
15197         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15198 
15199       EXECUTE IMMEDIATE lv_sql_stmt
15200               USING     lv_batch_id,
15201                         v_instance_code;
15202 
15203        -- Set the process flag as Valid and populate instance_id
15204       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
15205                         (p_table_name     => 'MSC_ST_REGION_LOCATIONS',
15206                          p_instance_id    => v_instance_id,
15207                          p_instance_code  => v_instance_code,
15208                          p_process_flag   => G_VALID,
15209                          p_error_text     => lv_error_text,
15210                          p_debug          => v_debug,
15211                          p_batch_id       => lv_batch_id);
15212 
15213       IF lv_return <> 0 THEN
15214         RAISE ex_logging_err;
15215       END IF;
15216 
15217       -- At the end calling  the LOG_ERROR for logging all
15218       -- errored out records.
15219       lv_return := MSC_ST_UTIL.LOG_ERROR
15220                         (p_table_name     => 'MSC_ST_REGION_LOCATIONS',
15221                          p_instance_code  => v_instance_code,
15222                          p_row            => lv_column_names,
15223                          p_severity       => G_SEV_ERROR,
15224                          p_message_text   => NULL,
15225                          p_error_text     => lv_error_text,
15226                          p_debug          => v_debug,
15227                          p_batch_id       => lv_batch_id  );
15228 
15229       IF lv_return <> 0 THEN
15230         RAISE ex_logging_err;
15231       END IF;
15232 
15233    lv_sql_stmt :=
15234    ' INSERT INTO MSC_REGION_LOCATIONS '
15235   ||' (REGION_ID,'
15236   ||' LOCATION_ID,'
15237   ||' REGION_TYPE,'
15238   ||' PARENT_REGION_FLAG,'
15239   ||' LOCATION_SOURCE,'
15240   ||' EXCEPTION_TYPE,'
15241   ||' SR_INSTANCE_ID,'
15242   ||' CREATED_BY,'
15243   ||' CREATION_DATE,'
15244   ||' LAST_UPDATED_BY,'
15245   ||' LAST_UPDATE_DATE,'
15246   ||' LAST_UPDATE_LOGIN)'
15247   ||' SELECT distinct '
15248   ||' REGION_ID,'
15249   ||' LOCATION_ID,'
15250   ||' REGION_TYPE,'
15251   ||' PARENT_REGION_FLAG,'
15252   ||' LOCATION_SOURCE,'
15253   ||' EXCEPTION_TYPE,'
15254   ||' SR_INSTANCE_ID,'
15255   ||' CREATED_BY,'
15256   ||' CREATION_DATE,'
15257   ||' LAST_UPDATED_BY,'
15258   ||' LAST_UPDATE_DATE,'
15259   ||' LAST_UPDATE_LOGIN'
15260   ||' FROM   MSC_ST_REGION_LOCATIONS msrl'
15261   ||' WHERE  msrl.batch_id         = :lv_batch_id'
15262   ||' AND    msrl.sr_instance_code = :v_instance_code'
15263   ||' AND    msrl.process_flag    = '||G_VALID ;
15264 
15265 
15266         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15267 
15268       EXECUTE IMMEDIATE lv_sql_stmt
15269               USING     lv_batch_id,
15270                         v_instance_code;
15271 
15272 
15273       COMMIT;
15274     END LOOP;
15275 
15276     -- validation for region sites
15277 
15278      --Getting the error message text
15279     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15280                  (p_app_short_name    => 'MSC',
15281                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
15282                   p_message_text      => lv_message_text,
15283                   p_error_text        => lv_error_text);
15284 
15285     IF lv_return <> 0 THEN
15286       RAISE ex_logging_err;
15287     END IF;
15288 
15289     --Duplicate records check for the records whose source is XML
15290 
15291     lv_sql_stmt :=
15292     'UPDATE     msc_st_region_sites msr1 '
15293     ||' SET     process_flag = '||G_ERROR_FLG||','
15294     ||'         error_text   = '||''''||lv_message_text||''''
15295     ||' WHERE   message_id <  (SELECT MAX(message_id)'
15296     ||'         FROM   msc_st_region_sites msr2'
15297     ||'         WHERE  msr2.sr_instance_code  = msr1.sr_instance_code'
15298     ||'         AND    NVL(msr2.country,  '||''''||NULL_CHAR||''''||') = '
15299     ||'                NVL(msr1.country,  '||''''||NULL_CHAR||''''||')'
15300     ||'         AND    NVL(msr2.country_code,  '||''''||NULL_CHAR||''''||') = '
15301     ||'                NVL(msr1.country_code,  '||''''||NULL_CHAR||''''||')'
15302     ||'         AND    NVL(msr2.state,  '||''''||NULL_CHAR||''''||') = '
15303     ||'                NVL(msr1.state,  '||''''||NULL_CHAR||''''||')'
15304     ||'         AND    NVL(msr2.state_code,  '||''''||NULL_CHAR||''''||') = '
15305     ||'                NVL(msr1.state_code,  '||''''||NULL_CHAR||''''||')'
15306     ||'         AND    NVL(msr2.city,  '||''''||NULL_CHAR||''''||') = '
15307     ||'                NVL(msr1.city,  '||''''||NULL_CHAR||''''||')'
15308     ||'         AND    NVL(msr2.city_code,  '||''''||NULL_CHAR||''''||') = '
15309     ||'                NVL(msr1.city_code,  '||''''||NULL_CHAR||''''||')'
15310     ||'         AND    NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||') = '
15311     ||'                NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
15312     ||'         AND    NVL(msr2.postal_code_to,  '||''''||NULL_CHAR||''''||') = '
15313     ||'                NVL(msr1.postal_code_to,  '||''''||NULL_CHAR||''''||')'
15314     ||'         AND    NVL(msr2.zone,   '||''''||NULL_CHAR||''''||') = '
15315     ||'                NVL(msr1.zone,   '||''''||NULL_CHAR||''''||')'
15316     ||'         AND    NVL(msr2.vendor_name,  '||''''||NULL_CHAR||''''||') = '
15317     ||'                NVL(msr1.vendor_name,  '||''''||NULL_CHAR||''''||')'
15318     ||'         AND    NVL(msr2.vendor_site_code,  '||''''||NULL_CHAR||''''||') = '
15319     ||'                NVL(msr1.vendor_site_code,  '||''''||NULL_CHAR||''''||')'
15320     ||'         AND    msr2.process_flag      ='|| G_IN_PROCESS
15321     ||'         AND    NVL(msr2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
15322     ||' AND     msr1.process_flag             = '||G_IN_PROCESS
15323     ||' AND     msr1.sr_instance_code         = :v_instance_code'
15324     ||' AND     NVL(msr1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
15325 
15326 
15327       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15328 
15329     EXECUTE IMMEDIATE lv_sql_stmt
15330             USING     v_instance_code;
15331 
15332     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15333                  (p_app_short_name    => 'MSC',
15334                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
15335                   p_message_text      => lv_message_text,
15336                   p_error_text        => lv_error_text);
15337 
15338     IF lv_return <> 0 THEN
15339       RAISE ex_logging_err;
15340     END IF;
15341 
15342     --Duplicate records check for the records whose source is other than XML
15343     --Different SQL is used because in XML we can identify the latest records
15344     --whereas in batch load we cannot.
15345 
15346     lv_sql_stmt :=
15347     'UPDATE  msc_st_region_sites msr1'
15348     ||' SET     process_flag  = '||G_ERROR_FLG||','
15349     ||'         error_text   = '||''''||lv_message_text||''''
15350     ||' WHERE   EXISTS( SELECT 1 '
15351     ||'         FROM   msc_st_region_sites msr2'
15352     ||'         WHERE  msr2.sr_instance_code           = msr1.sr_instance_code'
15353     ||'         AND    NVL(msr2.country,  '||''''||NULL_CHAR||''''||') = '
15354     ||'                NVL(msr1.country,  '||''''||NULL_CHAR||''''||')'
15355     ||'         AND    NVL(msr2.country_code,  '||''''||NULL_CHAR||''''||') = '
15356     ||'                NVL(msr1.country_code,  '||''''||NULL_CHAR||''''||')'
15357     ||'         AND    NVL(msr2.state,  '||''''||NULL_CHAR||''''||') = '
15358     ||'                NVL(msr1.state,  '||''''||NULL_CHAR||''''||')'
15359     ||'         AND    NVL(msr2.state_code,  '||''''||NULL_CHAR||''''||') = '
15360     ||'                NVL(msr1.state_code,  '||''''||NULL_CHAR||''''||')'
15361     ||'         AND    NVL(msr2.city,  '||''''||NULL_CHAR||''''||') = '
15362     ||'                NVL(msr1.city,  '||''''||NULL_CHAR||''''||')'
15363     ||'         AND    NVL(msr2.city_code,  '||''''||NULL_CHAR||''''||') = '
15364     ||'                NVL(msr1.city_code,  '||''''||NULL_CHAR||''''||')'
15365     ||'         AND    NVL(msr2.postal_code_from,  '||''''||NULL_CHAR||''''||') = '
15366     ||'                NVL(msr1.postal_code_from,  '||''''||NULL_CHAR||''''||')'
15367     ||'         AND    NVL(msr2.postal_code_to,  '||''''||NULL_CHAR||''''||') = '
15368     ||'                NVL(msr1.postal_code_to,  '||''''||NULL_CHAR||''''||')'
15369     ||'         AND    NVL(msr2.zone,  '||''''||NULL_CHAR||''''||') = '
15370     ||'                NVL(msr1.zone,  '||''''||NULL_CHAR||''''||')'
15371     ||'         AND    NVL(msr2.vendor_name,  '||''''||NULL_CHAR||''''||') = '
15372     ||'                NVL(msr1.vendor_name,  '||''''||NULL_CHAR||''''||')'
15373     ||'         AND    NVL(msr2.vendor_site_code,  '||''''||NULL_CHAR||''''||') = '
15374     ||'                NVL(msr1.vendor_site_code,  '||''''||NULL_CHAR||''''||')'
15375     ||'         AND    msr2.process_flag               = '||G_IN_PROCESS
15376     ||'         GROUP BY sr_instance_code,country,country_code, state, state_code, city, city_code, postal_code_from, postal_code_to, zone, vendor_name, vendor_site_code'
15377     ||'         HAVING COUNT(*) > 1)'
15378     ||' AND     msr1.process_flag     = '||G_IN_PROCESS
15379     ||' AND     msr1.sr_instance_code = :v_instance_code'
15380     ||' AND     NVL(msr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
15381 
15382 
15383       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15384 
15385     EXECUTE IMMEDIATE lv_sql_stmt
15386             USING     v_instance_code;
15387 
15388 
15389 
15390     lv_column_names :=
15391   'REGION_TYPE                    ||''~''||'
15392   ||' COUNTRY                     ||''~''||'
15393   ||' COUNTRY_CODE                ||''~''||'
15394   ||' STATE_CODE            	  ||''~''||'
15395   ||' SR_INSTANCE_CODE            ||''~''||'
15396   ||' STATE                       ||''~''||'
15397   ||' CITY              	  ||''~''||'
15398   ||' CITY_CODE 		  ||''~''||'
15399   ||' POSTAL_CODE_FROM            ||''~''||'
15400   ||' POSTAL_CODE_TO              ||''~''||'
15401   ||' ZONE                        ||''~''||'
15402   ||' DELETED_FLAG                ||''~''||'
15403   ||' VENDOR_NAME ' ;
15404 
15405   -- Now processing by batch
15406    LOOP
15407 
15408       lv_cursor_stmt :=
15409       ' SELECT msc_st_batch_id_s.NEXTVAL '
15410       ||' FROM   dual';
15411 
15412       OPEN  c1 FOR lv_cursor_stmt;
15413       FETCH c1 INTO lv_batch_id;
15414       CLOSE c1;
15415 
15416       lv_sql_stmt :=
15417       ' UPDATE msc_st_region_sites '
15418       ||' SET    batch_id  = :lv_batch_id'
15419       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
15420       ||' AND    sr_instance_code               = :v_instance_code'
15421       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
15422       ||' AND    rownum                        <= '||v_batch_size;
15423 
15424 
15425          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15426 
15427       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
15428 
15429       EXIT WHEN SQL%NOTFOUND;
15430 
15431       OPEN c8(lv_batch_id);
15432       FETCH c8 BULK COLLECT INTO lb_rowid;
15433       CLOSE c8;
15434 
15435       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
15436       UPDATE msc_st_region_sites
15437       SET  st_transaction_id = msc_st_region_sites_s.NEXTVAL,
15438            refresh_id        = v_refresh_id,
15439            last_update_date  = v_current_date,
15440            last_updated_by   = v_current_user,
15441            creation_date     = v_current_date,
15442            created_by        = v_current_user
15443       WHERE  rowid           = lb_rowid(j);
15444 
15445       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15446                      (p_app_short_name    => 'MSC',
15447                       p_error_code        => 'MSC_PP_NO_DELETION',
15448                       p_message_text      => lv_message_text,
15449                       p_error_text        => lv_error_text,
15450                       p_token1            => 'TABLE_NAME',
15451                       p_token_value1      => 'MSC_ST_REGION_SITES');
15452 
15453 
15454       -- Deletion is not allowed for this table
15455       v_sql_stmt := 06;
15456       lv_sql_stmt :=
15457       'UPDATE      msc_st_region_sites '
15458       ||' SET      process_flag     = '||G_ERROR_FLG||','
15459       ||'          error_text       = '||''''||lv_message_text||''''
15460       ||' WHERE    deleted_flag     = '||SYS_YES
15461       ||' AND      process_flag     = '||G_IN_PROCESS
15462       ||' AND      batch_id         = :lv_batch_id'
15463       ||' AND      sr_instance_code = :v_instance_code';
15464 
15465 
15466         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15467 
15468       EXECUTE IMMEDIATE lv_sql_stmt
15469               USING     lv_batch_id,
15470                         v_instance_code;
15471 
15472      -- set the message
15473 
15474       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15475                      (p_app_short_name    => 'MSC',
15476                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
15477                       p_message_text      => lv_message_text,
15478                       p_error_text        => lv_error_text,
15479                       p_token1            => 'COLUMN_NAME',
15480                       p_token_value1      => 'DELETED_FLAG',
15481                       p_token2            => 'DEFAULT_VALUE',
15482                       p_token_value2      => SYS_NO);
15483 
15484       IF lv_return <> 0 THEN
15485         RAISE ex_logging_err;
15486       END IF;
15487 
15488       -- Check DELETED_FLAG column has valid value or not and if not set it to SYS_NO
15489       -- Log error procedure inserts warning and also defaults the deleted flag
15490 
15491       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
15492                       ||' NOT IN(1,2)';
15493 
15494       lv_return := MSC_ST_UTIL.LOG_ERROR
15495                         (p_table_name        => 'MSC_ST_REGION_SITES',
15496                          p_instance_code     => v_instance_code,
15497                          p_row               => lv_column_names,
15498                          p_severity          => G_SEV_WARNING,
15499                          p_message_text      => lv_message_text,
15500                          p_error_text        => lv_error_text,
15501                          p_batch_id          => lv_batch_id,
15502                          p_where_str         => lv_where_str,
15503                          p_col_name          => 'DELETED_FLAG',
15504                          p_debug             => v_debug,
15505                          p_default_value     => SYS_NO);
15506 
15507       IF lv_return <> 0 THEN
15508         RAISE ex_logging_err;
15509       END IF;
15510 
15511       --Deriving region_id
15512 
15513       lv_sql_stmt :=
15514       'UPDATE msc_st_region_sites msr'
15515       ||' SET region_id     = (SELECT local_id'
15516       ||'       FROM   msc_local_id_setup mls'
15517       ||'       WHERE  mls.char1 = msr.sr_instance_code'
15518       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
15519       ||'              NVL(msr.country,     '||''''||NULL_CHAR||''''||') '
15520       ||'       AND    NVL(mls.char3,       '||''''||NULL_CHAR||''''||') = '
15521       ||'              NVL(msr.country_code,    '||''''||NULL_CHAR||''''||') '
15522       ||'       AND    NVL(mls.char4,       '||''''||NULL_CHAR||''''||') = '
15523       ||'              NVL(msr.state,     '||''''||NULL_CHAR||''''||') '
15524       ||'       AND    NVL(mls.char5,       '||''''||NULL_CHAR||''''||') = '
15525       ||'              NVL(msr.state_code,     '||''''||NULL_CHAR||''''||') '
15526       ||'       AND    NVL(mls.char6,       '||''''||NULL_CHAR||''''||') = '
15527       ||'              NVL(msr.city,     '||''''||NULL_CHAR||''''||') '
15528       ||'       AND    NVL(mls.char7,       '||''''||NULL_CHAR||''''||') = '
15529       ||'              NVL(msr.city_code,     '||''''||NULL_CHAR||''''||') '
15530       ||'       AND    NVL(mls.char8,       '||''''||NULL_CHAR||''''||') = '
15531       ||'              NVL(msr.postal_code_from,     '||''''||NULL_CHAR||''''||') '
15532       ||'       AND    NVL(mls.char9,       '||''''||NULL_CHAR||''''||') = '
15533       ||'              NVL(msr.postal_code_to,     '||''''||NULL_CHAR||''''||') '
15534       ||'       AND    NVL(mls.char10,       '||''''||NULL_CHAR||''''||') = '
15535       ||'              NVL(msr.zone,     '||''''||NULL_CHAR||''''||') '
15536       ||'       AND    mls.entity_name = ''REGION_ID'' )'
15537       ||' WHERE  process_flag                   = '||G_IN_PROCESS
15538       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
15539       ||' AND    sr_instance_code               = :v_instance_code';
15540 
15541 
15542         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15543 
15544       EXECUTE IMMEDIATE lv_sql_stmt
15545               USING     lv_batch_id,
15546                         v_instance_code;
15547 
15548     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15549                      (p_app_short_name    => 'MSC',
15550                       p_error_code        => 'MSC_PP_INVALID_VALUE',
15551                       p_message_text      => lv_message_text,
15552                       p_error_text        => lv_error_text,
15553                       p_token1            => 'COLUMN_NAME',
15554                       p_token_value1      => 'VENDOR_SITE_CODE');
15555 
15556       IF lv_return <> 0 THEN
15557         RAISE ex_logging_err;
15558       END IF;
15559 
15560       --Derive vendor_site_id
15561        lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
15562                      (p_table_name        => 'MSC_ST_REGION_SITES',
15563                       p_partner_name      => 'VENDOR_NAME',
15564                       p_partner_site_code => 'VENDOR_SITE_CODE',
15565                       p_partner_site_id   => 'VENDOR_SITE_ID',
15566                       p_instance_code     => v_instance_code,
15567                       p_partner_type      => G_VENDOR,
15568                       p_error_text        => lv_error_text,
15569                       p_batch_id          => lv_batch_id,
15570                       p_severity          => G_SEV3_ERROR,
15571                       p_message_text      => lv_message_text,
15572                       p_debug             => v_debug,
15573                       p_row               => lv_column_names);
15574 
15575       IF lv_return <> 0 THEN
15576         RAISE ex_logging_err;
15577       END IF;
15578 
15579       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15580                      (p_app_short_name    => 'MSC',
15581                       p_error_code        => 'MSC_PP_NO_UPDATION',
15582                       p_message_text      => lv_message_text,
15583                       p_error_text        => lv_error_text,
15584                       p_token1            => 'TABLE_NAME',
15585                       p_token_value1      => 'MSC_ST_REGION_SITES');
15586 
15587 
15588     -- Error out the record if same region_site already exists in ODS
15589     -- As we do not allow region_site incremental update
15590     -- It has to be purged before loaded again
15591 
15592       lv_sql_stmt :=
15593       'UPDATE      msc_st_region_sites msrs'
15594       ||' SET      msrs.process_flag     = '||G_ERROR_FLG||','
15595       ||'          msrs.error_text       = '||''''||lv_message_text||''''
15596       ||' WHERE    msrs.batch_id         = :lv_batch_id'
15597       ||' AND      msrs.sr_instance_code = :v_instance_code'
15598       ||' AND      EXISTS ( SELECT 1'
15599       ||'          FROM     msc_region_sites mrs, MSC_TP_SITE_ID_LID mtsil'
15600       ||'          WHERE    msrs.vendor_site_id = mtsil.SR_TP_SITE_ID'
15601       ||'          AND      mtsil.sr_instance_id = :v_instance_id '
15602       ||'          AND      mtsil.Partner_Type = 1'
15603       ||'          AND      mtsil.TP_SITE_ID = mrs.vendor_site_id '
15604       ||'          AND      msrs.region_id = mrs.region_id'
15605       ||'          AND      mrs.sr_instance_id = :v_instance_id)';
15606 
15607         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15608 
15609       EXECUTE IMMEDIATE lv_sql_stmt
15610               USING     lv_batch_id,
15611                         v_instance_code,
15612                         v_instance_id,
15613                         v_instance_id;
15614 
15615        --Call to customised validation.
15616       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
15617         (ERRBUF         => lv_error_text,
15618          RETCODE        => lv_return,
15619          pBatchID       => lv_batch_id,
15620          pInstanceCode  => v_instance_code,
15621          pEntityName    => 'MSC_ST_REGION_SITES',
15622          pInstanceID    => v_instance_id);
15623 
15624       IF NVL(lv_return,0) <> 0 THEN
15625         RAISE ex_logging_err;
15626       END IF;
15627 
15628        -- Set the process flag as Valid and populate instance_id
15629       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
15630                         (p_table_name     => 'MSC_ST_REGION_SITES',
15631                          p_instance_id    => v_instance_id,
15632                          p_instance_code  => v_instance_code,
15633                          p_process_flag   => G_VALID,
15634                          p_error_text     => lv_error_text,
15635                          p_debug          => v_debug,
15636                          p_batch_id       => lv_batch_id);
15637 
15638       IF lv_return <> 0 THEN
15639         RAISE ex_logging_err;
15640       END IF;
15641 
15642       -- At the end calling  the LOG_ERROR for logging all
15643       -- errored out records.
15644       lv_return := MSC_ST_UTIL.LOG_ERROR
15645                         (p_table_name     => 'MSC_ST_REGION_SITES',
15646                          p_instance_code  => v_instance_code,
15647                          p_row            => lv_column_names,
15648                          p_severity       => G_SEV_ERROR,
15649                          p_message_text   => NULL,
15650                          p_error_text     => lv_error_text,
15651                          p_debug          => v_debug,
15652                          p_batch_id       => lv_batch_id  );
15653 
15654       IF lv_return <> 0 THEN
15655         RAISE ex_logging_err;
15656       END IF;
15657 
15658     lv_sql_stmt :=
15659     ' INSERT INTO MSC_REGION_SITES '
15660   ||' (REGION_ID, '
15661   ||' VENDOR_SITE_ID,'
15662   ||' REGION_TYPE,'
15663   ||' ZONE_LEVEL,'
15664   ||' SR_INSTANCE_ID,'
15665   ||' CREATED_BY,'
15666   ||' CREATION_DATE,'
15667   ||' LAST_UPDATED_BY,'
15668   ||' LAST_UPDATE_DATE,'
15669   ||' LAST_UPDATE_LOGIN)'
15670   ||' SELECT distinct '
15671   ||' msrs.REGION_ID,'
15672   ||' mtsil.TP_SITE_ID,'
15673   ||' msrs.REGION_TYPE,'
15674   ||' msrs.ZONE_LEVEL,'
15675   ||' msrs.SR_INSTANCE_ID,'
15676   ||' msrs.CREATED_BY,'
15677   ||' msrs.CREATION_DATE,'
15678   ||' msrs.LAST_UPDATED_BY,'
15679   ||' msrs.LAST_UPDATE_DATE,'
15680   ||' msrs.LAST_UPDATE_LOGIN'
15681   ||' FROM  MSC_TP_SITE_ID_LID mtsil, MSC_ST_REGION_SITES msrs'
15682   ||' WHERE  msrs.batch_id         = :lv_batch_id'
15683   ||' AND    mtsil.SR_TP_SITE_ID = msrs.VENDOR_SITE_ID'
15684   ||' AND    mtsil.SR_Instance_ID = msrs.SR_INSTANCE_ID'
15685   ||' AND    mtsil.Partner_Type = 1 '
15686   ||' AND    msrs.sr_instance_code = :v_instance_code'
15687   ||' AND    msrs.process_flag    = '||G_VALID ;
15688 
15689 
15690         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15691 
15692       EXECUTE IMMEDIATE lv_sql_stmt
15693               USING     lv_batch_id,
15694                         v_instance_code;
15695 
15696       COMMIT;
15697     END LOOP;
15698 
15699     EXCEPTION
15700     WHEN too_many_rows THEN
15701       lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_REGIONS'||'('
15702                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
15703         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
15704       ROLLBACK;
15705 
15706     WHEN ex_logging_err THEN
15707         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
15708       ROLLBACK;
15709 
15710     WHEN OTHERS THEN
15711       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_REGIONS'||'('
15712                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
15713         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
15714       ROLLBACK;
15715 
15716 
15717   END LOAD_REGIONS;
15718 
15719 
15720 /*==========================================================================+
15721 | DESCRIPTION  : This procedure validates the data in MSC_ST_CATEGORY_SETS  |
15722 |                table.                                                     |
15723 +==========================================================================*/
15724   PROCEDURE LOAD_CATEGORY_SET IS
15725   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
15726   lb_rowid              RowidTab;   --bulk collects rowid
15727   lv_return             NUMBER;
15728   lv_error_text         VARCHAR2(250);
15729   lv_where_str          VARCHAR2(5000);
15730   lv_sql_stmt           VARCHAR2(5000);
15731   lv_column_names       VARCHAR2(5000);       --stores concatenated column names
15732   lv_batch_id           msc_st_category_sets.batch_id%TYPE;
15733   lv_message_text       msc_errors.error_text%TYPE;
15734   ex_logging_err        EXCEPTION;
15735 
15736   CURSOR c1(p_batch_id NUMBER) IS
15737     SELECT rowid
15738     FROM   msc_st_category_sets
15739     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
15740     AND    batch_id         = p_batch_id
15741     AND    sr_instance_code = v_instance_code;
15742 
15743   CURSOR c2(p_batch_id NUMBER) IS
15744     SELECT rowid
15745     FROM   msc_st_category_sets
15746     WHERE  NVL(sr_category_set_id,NULL_VALUE) = NULL_VALUE
15747     AND    process_flag          = G_IN_PROCESS
15748     AND    batch_id              = p_batch_id
15749     AND    sr_instance_code      = v_instance_code;
15750 
15751   BEGIN
15752 
15753     --Getting the error message text
15754     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15755                  (p_app_short_name    => 'MSC',
15756                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
15757                   p_message_text      => lv_message_text,
15758                   p_error_text        => lv_error_text);
15759 
15760     IF lv_return <> 0 THEN
15761       RAISE ex_logging_err;
15762     END IF;
15763 
15764     --Duplicate records check for the records whose source is XML
15765     v_sql_stmt := 01;
15766     lv_sql_stmt :=
15767     'UPDATE     msc_st_category_sets mcs1 '
15768     ||' SET     process_flag = '||G_ERROR_FLG||','
15769     ||'         error_text   = '||''''||lv_message_text||''''
15770     ||' WHERE   message_id <  (SELECT MAX(message_id)'
15771     ||'         FROM   msc_st_category_sets mcs2'
15772     ||'         WHERE  mcs2.sr_instance_code  = mcs1.sr_instance_code'
15773     ||'         AND    NVL(mcs2.company_name,  '||''''||NULL_CHAR||''''||') = '
15774     ||'                NVL(mcs1.company_name,  '||''''||NULL_CHAR||''''||')'
15775     ||'         AND    mcs2.category_set_name = mcs1.category_set_name'
15776     ||'         AND    mcs2.process_flag      ='|| G_IN_PROCESS
15777     ||'         AND    NVL(mcs2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
15778     ||' AND     mcs1.process_flag             = '||G_IN_PROCESS
15779     ||' AND     mcs1.sr_instance_code         = :v_instance_code'
15780     ||' AND     NVL(mcs1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
15781 
15782 
15783       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15784 
15785     EXECUTE IMMEDIATE lv_sql_stmt
15786             USING     v_instance_code;
15787 
15788 
15789     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15790                  (p_app_short_name    => 'MSC',
15791                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
15792                   p_message_text      => lv_message_text,
15793                   p_error_text        => lv_error_text);
15794 
15795     IF lv_return <> 0 THEN
15796       RAISE ex_logging_err;
15797     END IF;
15798 
15799     --Duplicate records check for the records whose source is other than XML
15800     --Different SQL is used because in XML we can identify the latest records
15801     --whereas in batch load we cannot.
15802     v_sql_stmt := 02;
15803     lv_sql_stmt :=
15804     'UPDATE  msc_st_category_sets mcs1'
15805     ||' SET     process_flag  = '||G_ERROR_FLG||','
15806     ||'         error_text   = '||''''||lv_message_text||''''
15807     ||' WHERE   EXISTS( SELECT 1 '
15808     ||'         FROM   msc_st_category_sets mcs2'
15809     ||'         WHERE  mcs2.sr_instance_code           = mcs1.sr_instance_code'
15810     ||'         AND    NVL(mcs2.company_name,  '||''''||NULL_CHAR||''''||') = '
15811     ||'                NVL(mcs1.company_name,  '||''''||NULL_CHAR||''''||')'
15812     ||'         AND    mcs2.category_set_name          = mcs1.category_set_name'
15813     ||'         AND    mcs2.process_flag               = '||G_IN_PROCESS
15814     ||'         AND    NVL(mcs2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
15815     ||'         GROUP BY sr_instance_code,company_name,category_set_name'
15816     ||'         HAVING COUNT(*) > 1)'
15817     ||' AND     mcs1.process_flag     = '||G_IN_PROCESS
15818     ||' AND     mcs1.sr_instance_code = :v_instance_code'
15819     ||' AND     NVL(mcs1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
15820 
15821 
15822       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15823 
15824     EXECUTE IMMEDIATE lv_sql_stmt
15825             USING     v_instance_code;
15826 
15827     lv_column_names :=
15828     'CATEGORY_SET_NAME ||''~''||'
15829     ||'CONTROL_LEVEL   ||''~''||'
15830     ||'SR_INSTANCE_CODE||''~''||'
15831     ||'DESCRIPTION     ||''~''||'
15832     ||'DEFAULT_FLAG    ||''~''||'
15833     ||'COMPANY_NAME    ||''~''||'
15834     ||'DELETED_FLAG ';
15835 
15836     LOOP
15837       v_sql_stmt := 03;
15838       SELECT msc_st_batch_id_s.NEXTVAL
15839       INTO   lv_batch_id
15840       FROM   dual;
15841 
15842       v_sql_stmt := 04;
15843       lv_sql_stmt :=
15844       ' UPDATE   msc_st_category_sets '
15845       ||' SET    batch_id  = :lv_batch_id'
15846       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
15847       ||' AND    sr_instance_code               = :v_instance_code'
15848       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
15849       ||' AND    rownum                        <= '||v_batch_size;
15850 
15851 
15852         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15853 
15854       EXECUTE IMMEDIATE lv_sql_stmt
15855               USING     lv_batch_id,
15856                         v_instance_code;
15857 
15858       EXIT WHEN SQL%NOTFOUND;
15859 
15860       OPEN c1(lv_batch_id);
15861       FETCH c1 BULK COLLECT INTO lb_rowid;
15862       CLOSE c1;
15863 
15864       v_sql_stmt := 05;
15865       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
15866       UPDATE msc_st_category_sets
15867       SET  st_transaction_id = msc_st_category_sets_s.NEXTVAL,
15868            refresh_id        = v_refresh_id,
15869            last_update_date  = v_current_date,
15870            last_updated_by   = v_current_user,
15871            creation_date     = v_current_date,
15872            created_by        = v_current_user
15873       WHERE  rowid           = lb_rowid(j);
15874 
15875       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15876                      (p_app_short_name    => 'MSC',
15877                       p_error_code        => 'MSC_PP_NO_DELETION',
15878                       p_message_text      => lv_message_text,
15879                       p_error_text        => lv_error_text,
15880                       p_token1            => 'TABLE_NAME',
15881                       p_token_value1      => 'MSC_ST_CATEGORY_SETS');
15882 
15883       IF lv_return <> 0 THEN
15884         RAISE ex_logging_err;
15885       END IF;
15886 
15887       --Deletion is not allowed for this entity.
15888       v_sql_stmt := 06;
15889       lv_sql_stmt :=
15890       ' UPDATE     msc_st_category_sets '
15891       ||' SET      process_flag     = '||G_ERROR_FLG||','
15892       ||'          error_text       = '||''''||lv_message_text||''''
15893       ||' WHERE    deleted_flag     = '||SYS_YES
15894       ||' AND      process_flag     = '||G_IN_PROCESS
15895       ||' AND      batch_id         =  :lv_batch_id'
15896       ||' AND      sr_instance_code = :v_instance_code';
15897 
15898 
15899         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15900 
15901       EXECUTE IMMEDIATE lv_sql_stmt
15902               USING     lv_batch_id,
15903                         v_instance_code;
15904 
15905       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15906                      (p_app_short_name    => 'MSC',
15907                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
15908                       p_message_text      => lv_message_text,
15909                       p_error_text        => lv_error_text,
15910                       p_token1            => 'COLUMN_NAME',
15911                       p_token_value1      => 'DELETED_FLAG',
15912                       p_token2            => 'DEFAULT_VALUE',
15913                       p_token_value2      => SYS_NO);
15914 
15915       IF lv_return <> 0 THEN
15916         RAISE ex_logging_err;
15917       END IF;
15918 
15919       lv_where_str :=
15920       ' AND NVL(deleted_flag,'||NULL_VALUE||') '
15921       ||' NOT IN(1,2)';
15922 
15923       --Logging a warning for those records where the deleted_flag value
15924       --is other than Yes/No.
15925       lv_return := MSC_ST_UTIL.LOG_ERROR
15926                      (p_table_name        => 'MSC_ST_CATEGORY_SETS',
15927                       p_instance_code     => v_instance_code,
15928                       p_row               => lv_column_names,
15929                       p_severity          => G_SEV_WARNING,
15930                       p_message_text      => lv_message_text,
15931                       p_error_text        => lv_error_text,
15932                       p_batch_id          => lv_batch_id,
15933                       p_where_str         => lv_where_str,
15934                       p_col_name          => 'DELETED_FLAG',
15935                       p_debug             => v_debug,
15936                       p_default_value     => SYS_NO);
15937 
15938       IF lv_return <> 0 THEN
15939         RAISE ex_logging_err;
15940       END IF;
15941 
15942       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15943                      (p_app_short_name    => 'MSC',
15944                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
15945                       p_message_text      => lv_message_text,
15946                       p_error_text        => lv_error_text,
15947                       p_token1            => 'COLUMN_NAME',
15948                       p_token_value1      => 'CATEGORY_SET_NAME');
15949 
15950       IF lv_return <> 0 THEN
15951         RAISE ex_logging_err;
15952       END IF;
15953 
15954       v_sql_stmt := 07;
15955       lv_sql_stmt :=
15956       ' UPDATE  msc_st_category_sets '
15957       ||' SET   process_flag        = '||G_ERROR_FLG||','
15958       ||'       error_text          = '||''''||lv_message_text||''''
15959       ||' WHERE NVL(category_set_name,'||''''||NULL_CHAR||''''||')'
15960       ||'       =                     '||''''||NULL_CHAR||''''
15961       ||' AND   batch_id            = :lv_batch_id'
15962       ||' AND   process_flag        = '||G_IN_PROCESS
15963       ||' AND   sr_instance_code    = :v_instance_code';
15964 
15965 
15966         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15967       EXECUTE IMMEDIATE lv_sql_stmt
15968               USING     lv_batch_id,
15969                         v_instance_code;
15970 
15971 
15972       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
15973                      (p_app_short_name    => 'MSC',
15974                       p_error_code        => 'MSC_PP_INVALID_VALUE',
15975                       p_message_text      => lv_message_text,
15976                       p_error_text        => lv_error_text,
15977                       p_token1            => 'COLUMN_NAME',
15978                       p_token_value1      => 'CONTROL_LEVEL OR DEFAULT_FLAG');
15979 
15980       IF lv_return <> 0 THEN
15981         RAISE ex_logging_err;
15982       END IF;
15983 
15984       v_sql_stmt := 08;
15985       lv_sql_stmt :=
15986       ' UPDATE  msc_st_category_sets '
15987       ||' SET   process_flag     = '||G_ERROR_FLG||','
15988       ||'       error_text       = '||''''||lv_message_text||''''
15989       ||' WHERE (NVL(control_level,'||NULL_VALUE||')  NOT IN(1,2)'
15990       ||' OR    NVL(default_flag,'  ||NULL_VALUE||')  NOT IN(1,2))'
15991       ||' AND   batch_id         = :lv_batch_id'
15992       ||' AND   process_flag     = '||G_IN_PROCESS
15993       ||' AND   sr_instance_code = :v_instance_code';
15994 
15995 
15996         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
15997       EXECUTE IMMEDIATE lv_sql_stmt
15998               USING     lv_batch_id,
15999                         v_instance_code;
16000 
16001 
16002       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16003                      (p_app_short_name    => 'MSC',
16004                       p_error_code        => 'MSC_PP_DEFAULT_FLG_INVALID',
16005                       p_message_text      => lv_message_text,
16006                       p_error_text        => lv_error_text);
16007 
16008       IF lv_return <> 0 THEN
16009         RAISE ex_logging_err;
16010       END IF;
16011 
16012       --There can be only one record in the staging/ODS with the
16013       --default_flag = 'YES'
16014       v_sql_stmt := 09;
16015       lv_sql_stmt :=
16016       'UPDATE     msc_st_category_sets mcs1'
16017       ||' SET     process_flag     = '||G_ERROR_FLG||','
16018       ||'         error_text       = '||''''||lv_message_text||''''
16019       ||' WHERE   EXISTS( SELECT 1 '
16020       ||'         FROM   msc_st_category_sets mcs2'
16021       ||'         WHERE  mcs2.sr_instance_code  = mcs1.sr_instance_code'
16022       ||'         AND    mcs2.process_flag      IN(2,5)'
16023       ||'         AND    mcs2.default_flag      = '||SYS_YES
16024       ||'         GROUP BY sr_instance_code'
16025       ||'         HAVING COUNT(*) > 1'
16026       ||'         UNION'
16027       ||'         SELECT 1 '
16028       ||'         FROM   msc_category_sets mcs3'
16029       ||'         WHERE  mcs3.sr_instance_id    =  :v_instance_id'
16030       ||'         AND    mcs3.category_set_name <> mcs1.category_set_name'
16031       ||'         AND    mcs3.default_flag      = '||SYS_YES||')'
16032       ||' AND     mcs1.default_flag             = '||SYS_YES
16033       ||' AND     mcs1.batch_id                 = :lv_batch_id'
16034       ||' AND     mcs1.process_flag             = '||G_IN_PROCESS
16035       ||' AND     mcs1.sr_instance_code         = :v_instance_code'
16036       ||' AND     NVL(mcs1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
16037 
16038 
16039         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16040 
16041       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_id,
16042                                           lv_batch_id,
16043                                           v_instance_code;
16044 
16045 
16046       --Derive sr_category_set_id.
16047       v_sql_stmt := 10;
16048       lv_sql_stmt :=
16049       ' UPDATE  msc_st_category_sets'
16050       ||' SET   sr_category_set_id = (SELECT local_id'
16051       ||'       FROM   msc_local_id_misc'
16052       ||'       WHERE  char1 =   sr_instance_code'
16053       ||'       AND    NVL(char2,       '||''''||NULL_CHAR||''''||')='
16054       ||'              NVL(company_name,'||''''||NULL_CHAR||''''||')'
16055       ||'       AND    char3 =   category_set_name'
16056       ||'       AND    entity_name = ''SR_CATEGORY_SET_ID'' )'
16057       ||' WHERE batch_id         = :lv_batch_id'
16058       ||' AND   process_flag     = '||G_IN_PROCESS
16059       ||' AND   sr_instance_code = :v_instance_code';
16060 
16061 
16062         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16063       EXECUTE IMMEDIATE lv_sql_stmt
16064               USING     lv_batch_id,
16065                         v_instance_code;
16066 
16067       --Call to customised validation.
16068       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
16069         (ERRBUF         => lv_error_text,
16070          RETCODE        => lv_return,
16071          pBatchID       => lv_batch_id,
16072          pInstanceCode  => v_instance_code,
16073          pEntityName    => 'MSC_ST_CATEGORY_SETS',
16074          pInstanceID    => v_instance_id);
16075 
16076       IF NVL(lv_return,0) <> 0 THEN
16077         RAISE ex_logging_err;
16078       END IF;
16079 
16080       OPEN c2(lv_batch_id);
16081       FETCH c2 BULK COLLECT INTO lb_rowid ;
16082 
16083       IF c2%ROWCOUNT > 0  THEN
16084         v_sql_stmt := 11;
16085         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
16086         UPDATE msc_st_category_sets
16087         SET   sr_category_set_id = msc_st_sr_category_set_id_s.NEXTVAL
16088         WHERE rowid              = lb_rowid(j);
16089 
16090         v_sql_stmt := 12;
16091         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
16092         INSERT INTO msc_local_id_misc
16093         (local_id,
16094          st_transaction_id,
16095          instance_id,
16096          entity_name,
16097          data_source_type,
16098          char1,
16099          char2,
16100          char3,
16101          SOURCE_SR_CATEGORY_SET_ID,
16102          last_update_date,
16103          last_updated_by,
16104          creation_date,
16105          created_by)
16106         SELECT
16107          sr_category_set_id,
16108          st_transaction_id,
16109          v_instance_id,
16110          'SR_CATEGORY_SET_ID',
16111          data_source_type,
16112          v_instance_code,
16113          company_name,
16114          category_set_name,
16115          SOURCE_SR_CATEGORY_SET_ID,
16116          v_current_date,
16117          v_current_user,
16118          v_current_date,
16119          v_current_user
16120          FROM msc_st_category_sets
16121          WHERE  rowid            = lb_rowid(j);
16122 
16123       END IF;
16124       CLOSE c2 ;
16125 
16126       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
16127                      (p_table_name     => 'MSC_ST_CATEGORY_SETS',
16128                       p_instance_id    => v_instance_id,
16129                       p_instance_code  => v_instance_code,
16130                       p_process_flag   => G_VALID,
16131                       p_error_text     => lv_error_text,
16132                       p_debug          => v_debug,
16133                       p_batch_id       => lv_batch_id);
16134 
16135       IF lv_return <> 0 THEN
16136         RAISE ex_logging_err;
16137       END IF;
16138 
16139       lv_return := MSC_ST_UTIL.LOG_ERROR
16140                      (p_table_name        => 'MSC_ST_CATEGORY_SETS',
16141                       p_instance_code     => v_instance_code,
16142                       p_row               => lv_column_names,
16143                       p_severity          => G_SEV_ERROR,
16144                       p_message_text      => NULL,
16145                       p_error_text        => lv_error_text,
16146                       p_debug             => v_debug,
16147                       p_batch_id          => lv_batch_id);
16148       IF lv_return <> 0 THEN
16149         RAISE ex_logging_err;
16150       END IF;
16151       COMMIT;
16152     END LOOP;
16153 
16154   EXCEPTION
16155     WHEN too_many_rows THEN
16156       lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_CATEGORY_SET'||'('
16157                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
16158         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
16159       ROLLBACK;
16160 
16161     WHEN ex_logging_err THEN
16162         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
16163       ROLLBACK;
16164 
16165     WHEN OTHERS THEN
16166       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_CATEGORY_SET'||'('
16167                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
16168         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
16169       ROLLBACK;
16170 
16171   END LOAD_CATEGORY_SET;
16172 
16173 /*==========================================================================+
16174 | DESCRIPTION  : This procedure performs validation and loads data into the |
16175 |                table msc_st_units_of_measure.                             |
16176 +==========================================================================*/
16177   PROCEDURE LOAD_UOM IS
16178   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
16179   lb_rowid           RowidTab;
16180   lv_return          NUMBER;
16181   lv_error_text      VARCHAR2(250);
16182   lv_where_str       VARCHAR2(5000);
16183   lv_sql_stmt        VARCHAR2(5000);
16184   lv_column_names    VARCHAR2(5000);
16185   lv_batch_id        msc_st_units_of_measure.batch_id%TYPE;
16186   lv_message_text    msc_errors.error_text%TYPE;
16187 
16188   ex_logging_err     EXCEPTION;
16189 
16190   CURSOR    c1(p_batch_id NUMBER) IS
16191     SELECT  rowid
16192     FROM    msc_st_units_of_measure
16193     WHERE   process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
16194     AND     batch_id         = p_batch_id
16195     AND     sr_instance_code = v_instance_code;
16196 
16197   BEGIN
16198     --*********Validation for msc_st_units_of_measure***********
16199     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16200                  (p_app_short_name    => 'MSC',
16201                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
16202                   p_message_text      => lv_message_text,
16203                   p_error_text        => lv_error_text);
16204 
16205     IF lv_return <> 0 THEN
16206       RAISE ex_logging_err;
16207     END IF;
16208 
16209     --Duplicate records check for the records whose source is XML
16210     v_sql_stmt := 01;
16211     lv_sql_stmt :=
16212     ' UPDATE   msc_st_units_of_measure mum1'
16213     ||' SET    process_flag = '||G_ERROR_FLG||','
16214     ||'        error_text   = '||''''||lv_message_text||''''
16215     ||' WHERE  message_id <  (SELECT MAX(message_id)'
16216     ||'        FROM  msc_st_units_of_measure mum2'
16217     ||'        WHERE NVL(mum2.unit_of_measure,'  ||''''||NULL_CHAR||''''||') ='
16218     ||'              NVL(mum1.unit_of_measure,'  ||''''||NULL_CHAR||''''||') '
16219     ||'        AND   NVL(mum2.company_name,  '   ||''''||NULL_CHAR||''''||') = '
16220     ||'              NVL(mum1.company_name,  '   ||''''||NULL_CHAR||''''||')'
16221     ||'        AND   mum2.uom_code = mum1.uom_code'
16222     ||'        AND   mum2.process_flag = '||G_IN_PROCESS
16223     ||'        AND   NVL(mum2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
16224     ||' AND    mum1.process_flag     = '||G_IN_PROCESS
16225     ||' AND    mum1.sr_instance_code = :v_instance_code'
16226     ||' AND    NVL(mum1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
16227 
16228 
16229 
16230       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16231 
16232     EXECUTE IMMEDIATE lv_sql_stmt
16233             USING     v_instance_code;
16234 
16235     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16236                  (p_app_short_name    => 'MSC',
16237                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
16238                   p_message_text      => lv_message_text,
16239                   p_error_text        => lv_error_text);
16240 
16241     IF lv_return <> 0 THEN
16242       RAISE ex_logging_err;
16243     END IF;
16244 
16245     --Duplicate records check for the records whose source is other than XML
16246     --Different SQL is used because in XML we can identify the latest records
16247     --whereas in batch load we cannot.
16248     v_sql_stmt := 02;
16249     lv_sql_stmt :=
16250     ' UPDATE  msc_st_units_of_measure mum1'
16251     ||' SET   process_flag  = '||G_ERROR_FLG||','
16252     ||'       error_text    = '||''''||lv_message_text||''''
16253     ||' WHERE EXISTS( SELECT 1 '
16254     ||'         FROM   msc_st_units_of_measure mum2'
16255     ||'         WHERE  NVL(mum2.unit_of_measure,   '||''''||NULL_CHAR||''''||')'
16256     ||'                 = NVL(mum1.unit_of_measure,'||''''||NULL_CHAR||''''||')'
16257     ||'         AND    NVL(mum2.company_name,  '||''''||NULL_CHAR||''''||') = '
16258     ||'                NVL(mum1.company_name,  '||''''||NULL_CHAR||''''||')'
16259     ||'         AND    mum2.uom_code = mum1.uom_code'
16260     ||'         AND    mum2.process_flag     ='|| G_IN_PROCESS
16261     ||'         AND    NVL(mum2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
16262     ||'         GROUP BY uom_code,company_name,unit_of_measure'
16263     ||'         HAVING COUNT(*) > 1)'
16264     ||' AND   mum1.process_flag     = '||G_IN_PROCESS
16265     ||' AND   mum1.sr_instance_code = :v_instance_code'
16266     ||' AND   NVL(mum1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
16267 
16268 
16269 
16270       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16271 
16272     EXECUTE IMMEDIATE lv_sql_stmt
16273             USING     v_instance_code;
16274 
16275     lv_column_names :=
16276     'UNIT_OF_MEASURE    ||''~''||'
16277     ||'UOM_CODE         ||''~''||'
16278     ||'UOM_CLASS        ||''~''||'
16279     ||'BASE_UOM_FLAG    ||''~''||'
16280     ||'SR_INSTANCE_CODE ||''~''||'
16281     ||'DISABLE_DATE     ||''~''||'
16282     ||'DESCRIPTION      ||''~''||'
16283     ||'COMPANY_NAME     ||''~''||'
16284     ||'DELETED_FLAG';
16285 
16286     LOOP
16287       v_sql_stmt := 03;
16288       SELECT msc_st_batch_id_s.NEXTVAL
16289       INTO   lv_batch_id
16290       FROM   dual;
16291 
16292       v_sql_stmt := 04;
16293       lv_sql_stmt :=
16294       'UPDATE msc_st_units_of_measure '
16295       ||' SET    batch_id  = :lv_batch_id'
16296       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
16297       ||' AND    sr_instance_code               = :v_instance_code'
16298       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
16299       ||' AND    rownum                        <= '||v_batch_size;
16300 
16301 
16302         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16303 
16304       EXECUTE IMMEDIATE lv_sql_stmt
16305               USING     lv_batch_id,
16306                         v_instance_code;
16307 
16308       EXIT WHEN SQL%NOTFOUND;
16309 
16310       OPEN c1(lv_batch_id);
16311       FETCH c1 BULK COLLECT INTO lb_rowid;
16312       CLOSE c1;
16313 
16314       v_sql_stmt := 05;
16315       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
16316       UPDATE msc_st_units_of_measure
16317       SET    st_transaction_id   = msc_st_units_of_measure_s.NEXTVAL,
16318              refresh_id          = v_refresh_id,
16319              last_update_date    = v_current_date,
16320              last_updated_by     = v_current_user,
16321              creation_date       = v_current_date,
16322              created_by          = v_current_user
16323       WHERE  rowid           = lb_rowid(j);
16324 
16325       --Deletion is not allowed on this table.
16326       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16327                      (p_app_short_name    => 'MSC',
16328                       p_error_code        => 'MSC_PP_NO_DELETION',
16329                       p_message_text      => lv_message_text,
16330                       p_error_text        => lv_error_text,
16331                       p_token1            => 'TABLE_NAME',
16332                       p_token_value1      => 'MSC_ST_UNITS_OF_MEASURE');
16333 
16334       IF lv_return <> 0 THEN
16335         RAISE ex_logging_err;
16336       END IF;
16337 
16338       v_sql_stmt := 06;
16339       lv_sql_stmt :=
16340       'UPDATE    msc_st_units_of_measure '
16341       ||' SET    process_flag     = '||G_ERROR_FLG||','
16342       ||'        error_text       = '||''''||lv_message_text||''''
16343       ||' WHERE  deleted_flag     = '||SYS_YES
16344       ||' AND    process_flag     = '||G_IN_PROCESS
16345       ||' AND    batch_id         =  :lv_batch_id'
16346       ||' AND    sr_instance_code = :v_instance_code';
16347 
16348 
16349         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16350 
16351       EXECUTE IMMEDIATE lv_sql_stmt
16352               USING     lv_batch_id,
16353                         v_instance_code;
16354 
16355       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16356                      (p_app_short_name    => 'MSC',
16357                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
16358                       p_message_text      => lv_message_text,
16359                       p_error_text        => lv_error_text,
16360                       p_token1            => 'COLUMN_NAME',
16361                       p_token_value1      => 'DELETED_FLAG',
16362                       p_token2            => 'DEFAULT_VALUE',
16363                       p_token_value2      => SYS_NO);
16364 
16365       IF lv_return <> 0 THEN
16366         RAISE ex_logging_err;
16367       END IF;
16368 
16369       lv_where_str :=
16370       ' AND NVL(deleted_flag,'||NULL_VALUE||') '
16371       ||' NOT IN(1,2)';
16372       --Log a warning for those records where the deleted_flag has a value other
16373       --than SYS_NO
16374       lv_return := MSC_ST_UTIL.LOG_ERROR
16375                      (p_table_name        => 'MSC_ST_UNITS_OF_MEASURE',
16376                       p_instance_code     => v_instance_code,
16377                       p_row               => lv_column_names,
16378                       p_severity          => G_SEV_WARNING,
16379                       p_message_text      => lv_message_text,
16380                       p_error_text        => lv_error_text,
16381                       p_batch_id          => lv_batch_id,
16382                       p_where_str         => lv_where_str,
16383                       p_col_name          => 'DELETED_FLAG',
16384                       p_debug             => v_debug,
16385                       p_default_value     => SYS_NO);
16386 
16387       IF lv_return <> 0 THEN
16388         RAISE ex_logging_err;
16389       END IF;
16390 
16391       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16392                      (p_app_short_name    => 'MSC',
16393                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
16394                       p_message_text      => lv_message_text,
16395                       p_error_text        => lv_error_text,
16396                       p_token1            => 'COLUMN_NAME',
16397                       p_token_value1      => 'UOM_CODE OR UOM_CLASS');
16398 
16399       IF lv_return <> 0 THEN
16400         RAISE ex_logging_err;
16401       END IF;
16402 
16403       v_sql_stmt := 07;
16404       lv_sql_stmt :=
16405       'UPDATE    msc_st_units_of_measure '
16406       ||' SET    process_flag  = '||G_ERROR_FLG||','
16407       ||'        error_text    = '||''''||lv_message_text||''''
16408       ||' WHERE  (NVL(uom_code,  '||''''||NULL_CHAR||''''||') '
16409       ||'        =               '||''''||NULL_CHAR||''''
16410       ||' OR     NVL(uom_class,  '||''''||NULL_CHAR||''''||')'
16411       ||'        =               '||''''||NULL_CHAR||''''
16412       ||' AND    batch_id         =  :lv_batch_id'
16413       ||' AND    sr_instance_code =  :v_instance_code'
16414       ||' AND    process_flag     = '||G_IN_PROCESS||')';
16415 
16416 
16417         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16418 
16419       EXECUTE IMMEDIATE lv_sql_stmt
16420               USING     lv_batch_id,
16421                         v_instance_code;
16422 
16423       v_sql_stmt := 08;
16424       lv_sql_stmt :=
16425       'UPDATE    msc_st_units_of_measure '
16426       ||' SET    unit_of_measure  = uom_code'
16427       ||' WHERE  NVL(unit_of_measure,'||''''||NULL_CHAR||''''||') '
16428       ||'        = '||''''||NULL_CHAR||''''
16429       ||' AND    batch_id          =  :lv_batch_id'
16430       ||' AND    sr_instance_code  = :v_instance_code'
16431       ||' AND    process_flag      = '||G_IN_PROCESS;
16432 
16433 
16434 
16435         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16436 
16437       EXECUTE IMMEDIATE lv_sql_stmt
16438               USING     lv_batch_id,
16439                         v_instance_code;
16440 
16441       --A UOM class can have one and only one base UOM.
16442 
16443       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16444                      (p_app_short_name    => 'MSC',
16445                       p_error_code        => 'MSC_PP_BASE_UOM_INVALID',
16446                       p_message_text      => lv_message_text,
16447                       p_error_text        => lv_error_text);
16448 
16449       IF lv_return <> 0 THEN
16450         RAISE ex_logging_err;
16451       END IF;
16452 
16453       v_sql_stmt := 09;
16454       lv_sql_stmt :=
16455       'UPDATE   msc_st_units_of_measure mum1 '
16456       ||' SET   process_flag  = '||G_ERROR_FLG||','
16457       ||'       error_text    = '||''''||lv_message_text||''''
16458       ||' WHERE EXISTS(SELECT 1'
16459       ||'       FROM   msc_units_of_measure mum2'
16460       ||'       WHERE  mum2.unit_of_measure <> '
16461       ||'              NVL(mum1.unit_of_measure,'||''''||NULL_CHAR||''''||' )'
16462       ||'       AND    mum2.uom_code        <> '
16463       ||'              NVL(mum1.uom_code,       '||''''||NULL_CHAR||''''||' )'
16464       ||'       AND    mum2.uom_class        = '
16465       ||'              NVL(mum1.uom_class,       '||''''||NULL_CHAR||''''||' )'
16466       ||'       AND    mum2.base_uom_flag   = ''Y'' '
16467       ||'       UNION'
16468       ||'       SELECT 1'
16469       ||'       FROM   msc_st_units_of_measure mum3'
16470       ||'       WHERE  NVL(mum3.unit_of_measure,'||''''||NULL_CHAR||''''||') <> '
16471       ||'              NVL(mum1.unit_of_measure,'||''''||NULL_CHAR||''''||')'
16472       ||'       AND    NVL(mum3.uom_code,       '||''''||NULL_CHAR||''''||') <> '
16473       ||'              NVL(mum1.uom_code,       '||''''||NULL_CHAR||''''||')'
16474       ||'       AND    NVL(mum3.uom_class,       '||''''||NULL_CHAR||''''||') = '
16475       ||'              NVL(mum1.uom_class,       '||''''||NULL_CHAR||''''||')'
16476       ||'       AND    mum3.base_uom_flag     = ''Y'' '
16477       ||'       AND    mum3.process_flag      IN(2,5))'
16478       ||' AND   mum1.base_uom_flag   = ''Y'' '
16479       ||' AND   process_flag     = '||G_IN_PROCESS
16480       ||' AND    batch_id          =  :lv_batch_id'
16481       ||' AND   sr_instance_code = :v_instance_code';
16482 
16483 
16484         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16485 
16486       EXECUTE IMMEDIATE lv_sql_stmt
16487               USING     lv_batch_id,
16488                         v_instance_code;
16489 
16490       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16491                      (p_app_short_name    => 'MSC',
16492                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
16493                       p_message_text      => lv_message_text,
16494                       p_error_text        => lv_error_text,
16495                       p_token1            => 'COLUMN_NAME',
16496                       p_token_value1      => 'BASE_UOM_FLAG');
16497 
16498       IF lv_return <> 0 THEN
16499         RAISE ex_logging_err;
16500       END IF;
16501 
16502       lv_where_str :=
16503       '   AND NVL(base_uom_flag,'||''''||NULL_CHAR||''''||') '
16504       ||' NOT IN(''Y'',''N'' )';
16505       --Log a warning for those records where the base_uom_flag has  value
16506       --other than('Y','N')
16507 
16508       lv_return := MSC_ST_UTIL.LOG_ERROR
16509                      (p_table_name        => 'MSC_ST_UNITS_OF_MEASURE',
16510                       p_instance_code     => v_instance_code,
16511                       p_row               => lv_column_names,
16512                       p_severity          => G_SEV_WARNING,
16513                       p_message_text      => lv_message_text,
16514                       p_error_text        => lv_error_text,
16515                       p_batch_id          => lv_batch_id,
16516                       p_where_str         => lv_where_str,
16517                       p_col_name          => 'BASE_UOM_FLAG',
16518                       p_debug             => v_debug,
16519                       p_default_value     => '''N''');
16520 
16521       IF lv_return <> 0 THEN
16522         RAISE ex_logging_err;
16523       END IF;
16524 
16525       --Call to customised validation.
16526       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
16527         (ERRBUF         => lv_error_text,
16528          RETCODE        => lv_return,
16529          pBatchID       => lv_batch_id,
16530          pInstanceCode  => v_instance_code,
16531          pEntityName    => 'MSC_ST_UNTIS_OF_MEASURE',
16532          pInstanceID    => v_instance_id);
16533 
16534       IF NVL(lv_return,0) <> 0 THEN
16535         RAISE ex_logging_err;
16536       END IF;
16537 
16538       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
16539                   (p_table_name     => 'MSC_ST_UNITS_OF_MEASURE',
16540                    p_instance_id    => v_instance_id,
16541                    p_instance_code  => v_instance_code,
16542                    p_process_flag   => G_VALID,
16543                    p_error_text     => lv_error_text,
16544                    p_debug          => v_debug,
16545                    p_batch_id       => lv_batch_id);
16546 
16547       IF lv_return <> 0 THEN
16548         RAISE ex_logging_err;
16549       END IF;
16550 
16551       lv_return := MSC_ST_UTIL.LOG_ERROR
16552                    (p_table_name        => 'MSC_ST_UNITS_OF_MEASURE',
16553                     p_instance_code     => v_instance_code,
16554                     p_row               => lv_column_names,
16555                     p_severity          => G_SEV_ERROR,
16556                     p_message_text      => NULL,
16557                     p_error_text        => lv_error_text,
16558                     p_debug             => v_debug,
16559                     p_batch_id          => lv_batch_id);
16560 
16561       IF lv_return <> 0 THEN
16562         RAISE ex_logging_err;
16563       END IF;
16564       COMMIT;
16565      END LOOP;
16566 
16567   EXCEPTION
16568     WHEN ex_logging_err THEN
16569         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
16570       ROLLBACK;
16571 
16572     WHEN OTHERS THEN
16573       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_UOM'||'('
16574                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
16575         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
16576       ROLLBACK;
16577 
16578   END LOAD_UOM;
16579 
16580 /*==========================================================================+
16581 | DESCRIPTION  : This procedure performs validation and loads data into the |
16582 |                tables msc_st_uom_class_conversions and                    |
16583 |                msc_st_uom_conversions.                                    |
16584 +==========================================================================*/
16585   PROCEDURE LOAD_UOM_CLASS_CONV IS
16586   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
16587   TYPE CharTblTyp IS TABLE OF VARCHAR2(1000);
16588   lb_rowid           RowidTab;
16589   lb_item            CharTblTyp;
16590   lb_from_uom_code   CharTblTyp;
16591   lb_to_uom_code     CharTblTyp;
16592   lb_org             CharTblTyp;
16593   lb_comp            CharTblTyp;
16594   lv_return          NUMBER;
16595   lv_error_text      VARCHAR2(250);
16596   lv_where_str       VARCHAR2(5000);
16597   lv_sql_stmt        VARCHAR2(5000);
16598   lv_column_names    VARCHAR2(5000);
16599   lv_batch_id        msc_st_uom_class_conversions.batch_id%TYPE;
16600   lv_message_text    msc_errors.error_text%TYPE;
16601 
16602   ex_logging_err     EXCEPTION;
16603 
16604   CURSOR    c_udk1 is
16605      SELECT from_uom_code,  to_uom_code, item_name, company_name
16606      FROM   msc_st_uom_class_conversions mcc2
16607      WHERE  mcc2.process_flag        = G_IN_PROCESS
16608      AND    item_name is not null
16609      AND    sr_instance_code = v_instance_code
16610      AND    NVL(mcc2.message_id, NULL_VALUE) = NULL_VALUE
16611      GROUP  BY from_uom_code,  to_uom_code, item_name, company_name
16612      HAVING COUNT(*) > 1;
16613 
16614   CURSOR    c_udk2 is select  from_uom_code,  to_uom_code, item_name, company_name
16615      FROM   msc_st_uom_class_conversions mcc2
16616      WHERE  mcc2.process_flag        = G_IN_PROCESS
16617      AND    item_name is null
16618      AND    sr_instance_code = v_instance_code
16619      AND    NVL(mcc2.message_id, NULL_VALUE) = NULL_VALUE
16620      GROUP  BY from_uom_code,  to_uom_code, item_name, company_name
16621      HAVING COUNT(*) > 1;
16622 
16623   CURSOR    c2(p_batch_id NUMBER) IS
16624     SELECT  rowid
16625     FROM    msc_st_uom_class_conversions
16626     WHERE   process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
16627     AND     batch_id         = p_batch_id
16628     AND     sr_instance_code = v_instance_code;
16629 
16630   CURSOR    c3(p_batch_id NUMBER) IS
16631     SELECT  rowid
16632     FROM    msc_st_uom_conversions
16633     WHERE   process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
16634     AND     batch_id         = p_batch_id
16635     AND     sr_instance_code = v_instance_code;
16636 
16637   BEGIN
16638     --*****Validation for the table msc_st_uom_class_conversions.*****
16639 
16640     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16641                  (p_app_short_name    => 'MSC',
16642                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
16643                   p_message_text      => lv_message_text,
16644                   p_error_text        => lv_error_text);
16645 
16646     IF lv_return <> 0 THEN
16647       RAISE ex_logging_err;
16648     END IF;
16649 
16650 /*  --Remove organization code from records which have item name null
16651 
16652     UPDATE  msc_st_uom_class_conversions
16653     SET     organization_code = ''
16654     WHERE   sr_instance_code = v_instance_code
16655     AND     process_flag     = G_IN_PROCESS
16656     AND     item_name is null;*/
16657 
16658     --Duplicate records check for the records whose source is XML.
16659     v_sql_stmt := 10;
16660     lv_sql_stmt :=
16661     'UPDATE  msc_st_uom_class_conversions mcc1 '
16662     ||' SET     process_flag = '||G_ERROR_FLG||','
16663     ||'         error_text   = '||''''||lv_message_text||''''
16664     ||' WHERE   message_id <  (SELECT MAX(message_id)'
16665     ||'         FROM   msc_st_uom_class_conversions mcc2'
16666     ||'         WHERE  NVL(mcc2.item_name,           '||''''||NULL_CHAR||''''||') = '
16667     ||'                NVL(mcc1.item_name,           '||''''||NULL_CHAR||''''||')'
16668     ||'         AND    NVL(mcc2.from_uom_code,       '||''''||NULL_CHAR||''''||') = '
16669     ||'                NVL(mcc1.from_uom_code,       '||''''||NULL_CHAR||''''||')'
16670     ||'         AND    NVL(mcc2.to_uom_code,         '||''''||NULL_CHAR||''''||') = '
16671     ||'                NVL(mcc1.to_uom_code,         '||''''||NULL_CHAR||''''||')'
16672     ||'         AND    NVL(mcc2.company_name,        '||''''||NULL_CHAR||''''||') = '
16673     ||'                NVL(mcc1.company_name,        '||''''||NULL_CHAR||''''||')'
16674     ||'         AND    mcc2.process_flag      = '||G_IN_PROCESS
16675     ||'         AND    NVL(mcc2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
16676     ||' AND mcc1.process_flag                 = '||G_IN_PROCESS
16677     ||' AND mcc1.sr_instance_code             = :v_instance_code'
16678     ||' AND NVL(mcc1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
16679 
16680 
16681 
16682       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16683 
16684     EXECUTE IMMEDIATE lv_sql_stmt
16685             USING     v_instance_code;
16686 
16687 
16688     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16689                  (p_app_short_name    => 'MSC',
16690                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
16691                   p_message_text      => lv_message_text,
16692                   p_error_text        => lv_error_text);
16693 
16694     IF lv_return <> 0 THEN
16695       RAISE ex_logging_err;
16696     END IF;
16697 
16698     --Duplicate records check for the records whose source is other than XML
16699     --Different SQL is used because in XML we can identify the latest records
16700     --whereas in batch load we cannot.
16701     v_sql_stmt := 11;
16702 /*
16703     lv_sql_stmt :=
16704     'UPDATE     msc_st_uom_class_conversions mcc1 '
16705     ||' SET     process_flag  = '||G_ERROR_FLG||','
16706     ||'         error_text    = '||''''||lv_message_text||''''
16707     ||' WHERE   EXISTS( SELECT 1 '
16708     ||'         FROM   msc_st_uom_class_conversions mcc2'
16709     ||'         WHERE  NVL(mcc2.item_name,           '||''''||NULL_CHAR||''''||') = '
16710     ||'                NVL(mcc1.item_name,           '||''''||NULL_CHAR||''''||')'
16711     ||'         AND    NVL(mcc2.organization_code,   '||''''||NULL_CHAR||''''||') = '
16712     ||'                NVL(mcc1.organization_code,   '||''''||NULL_CHAR||''''||')'
16713     ||'         AND    NVL(mcc2.from_uom_code,       '||''''||NULL_CHAR||''''||') = '
16714     ||'                NVL(mcc1.from_uom_code,       '||''''||NULL_CHAR||''''||')'
16715     ||'         AND    NVL(mcc2.to_uom_code,         '||''''||NULL_CHAR||''''||') = '
16716     ||'                NVL(mcc1.to_uom_code,         '||''''||NULL_CHAR||''''||')'
16717     ||'         AND    NVL(mcc2.company_name,        '||''''||NULL_CHAR||''''||') = '
16718     ||'                NVL(mcc1.company_name,        '||''''||NULL_CHAR||''''||')'
16719     ||'         AND    mcc2.process_flag      = '||G_IN_PROCESS
16720     ||'         AND    NVL(mcc2.message_id,          '||NULL_VALUE||') = '||NULL_VALUE
16721     ||' GROUP BY item_name,organization_code,company_name,'
16722     ||'          from_uom_code,to_uom_code'
16723     ||' HAVING COUNT(*) > 1)'
16724     ||' AND    mcc1.process_flag            = '||G_IN_PROCESS
16725     ||' AND    mcc1.sr_instance_code        = :v_instance_code'
16726     ||' AND     NVL(mcc1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
16727 
16728 
16729       msc_st_util.log_message(lv_sql_stmt);
16730 
16731     EXECUTE IMMEDIATE lv_sql_stmt
16732             USING     v_instance_code;
16733 */
16734 
16735    BEGIN
16736 
16737      BEGIN
16738         OPEN c_udk1;
16739          FETCH c_udk1 BULK COLLECT INTO lb_from_uom_code,lb_to_uom_code, lb_item,lb_comp;
16740         CLOSE c_udk1;
16741 
16742         FORALL j IN lb_item.FIRST..lb_item.LAST
16743         UPDATE msc_st_uom_class_conversions  SET process_flag  = G_ERROR_FLG, error_text =lv_message_text
16744         WHERE  from_uom_code= lb_from_uom_code(j)
16745         AND    to_uom_code = lb_to_uom_code(j)
16746         AND    item_name= lb_item(j)
16747         AND    NVL(company_name,   NULL_CHAR) = NVL(lb_comp(j),   NULL_CHAR)
16748         AND    process_flag        = G_IN_PROCESS
16749         AND    sr_instance_code        = v_instance_code
16750         AND    NVL(message_id,  NULL_VALUE) = NULL_VALUE ;
16751      EXCEPTION
16752         when others then
16753         null;
16754      END;
16755 
16756      BEGIN
16757         lb_item.DELETE;
16758         lb_from_uom_code.DELETE;
16759         lb_to_uom_code.DELETE;
16760         lb_comp.DELETE;
16761         OPEN c_udk2;
16762          FETCH c_udk2 BULK COLLECT INTO lb_from_uom_code,lb_to_uom_code, lb_item,lb_comp;
16763         CLOSE c_udk2;
16764 
16765         FORALL j IN lb_item.FIRST..lb_item.LAST
16766         UPDATE msc_st_uom_class_conversions  SET  process_flag  = G_ERROR_FLG, error_text =lv_message_text
16767         WHERE  from_uom_code= lb_from_uom_code(j)
16768         AND    to_uom_code = lb_to_uom_code(j)
16769         AND    NVL(company_name,    NULL_CHAR) = NVL(lb_comp(j),   NULL_CHAR)
16770         AND    item_name is null
16771         AND    process_flag        = G_IN_PROCESS
16772         AND    sr_instance_code        = v_instance_code
16773         AND    NVL(message_id,    NULL_VALUE) = NULL_VALUE ;
16774      EXCEPTION
16775         when others then
16776         null;
16777      END;
16778 
16779     END;
16780 
16781     lv_column_names :=
16782     'FROM_UNIT_OF_MEASURE||''~''||'
16783     ||'FROM_UOM_CODE     ||''~''||'
16784     ||'FROM_UOM_CLASS    ||''~''||'
16785     ||'TO_UNIT_OF_MEASURE||''~''||'
16786     ||'TO_UOM_CODE       ||''~''||'
16787     ||'TO_UOM_CLASS      ||''~''||'
16788     ||'CONVERSION_RATE   ||''~''||'
16789     ||'SR_INSTANCE_CODE  ||''~''||'
16790     ||'DISABLE_DATE      ||''~''||'
16791     ||'ITEM_NAME         ||''~''||'
16792     ||'ORGANIZATION_CODE ||''~''||'
16793     ||'COMPANY_NAME      ||''~''||'
16794     ||'DELETED_FLAG';
16795 
16796     LOOP
16797       v_sql_stmt := 12;
16798       SELECT msc_st_batch_id_s.NEXTVAL
16799       INTO   lv_batch_id
16800       FROM   dual;
16801 
16802       v_sql_stmt := 13;
16803       lv_sql_stmt :=
16804       'UPDATE msc_st_uom_class_conversions '
16805       ||' SET    batch_id  = :lv_batch_id'
16806       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
16807       ||' AND    sr_instance_code               = :v_instance_code'
16808       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
16809       ||' AND    rownum                        <= '||v_batch_size;
16810 
16811 
16812         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16813 
16814       EXECUTE IMMEDIATE lv_sql_stmt
16815               USING     lv_batch_id,
16816                         v_instance_code;
16817 
16818       EXIT WHEN SQL%NOTFOUND;
16819 
16820       OPEN  c2(lv_batch_id);
16821       FETCH c2 BULK COLLECT INTO lb_rowid;
16822       CLOSE c2;
16823 
16824       v_sql_stmt := 14;
16825       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
16826       UPDATE msc_st_uom_class_conversions
16827       SET    st_transaction_id   = msc_st_uom_class_conversions_s.NEXTVAL,
16828              refresh_id          = v_refresh_id,
16829              last_update_date    = v_current_date,
16830              last_updated_by     = v_current_user,
16831              creation_date       = v_current_date,
16832              created_by          = v_current_user
16833       WHERE  rowid               = lb_rowid(j);
16834 
16835       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16836                      (p_app_short_name    => 'MSC',
16837                       p_error_code        => 'MSC_PP_NO_DELETION',
16838                       p_message_text      => lv_message_text,
16839                       p_error_text        => lv_error_text,
16840                       p_token1            => 'TABLE_NAME',
16841                       p_token_value1      => 'MSC_ST_UOM_CLASS_CONVERSIONS');
16842 
16843       IF lv_return <> 0 THEN
16844         RAISE ex_logging_err;
16845       END IF;
16846 
16847       --Deletion is not allowed on this table.
16848       v_sql_stmt := 15;
16849       lv_sql_stmt :=
16850       'UPDATE      msc_st_uom_class_conversions '
16851       ||' SET      process_flag     = '||G_ERROR_FLG||','
16852       ||'          error_text       = '||''''||lv_message_text||''''
16853       ||' WHERE    deleted_flag     = '||SYS_YES
16854       ||' AND      process_flag     = '||G_IN_PROCESS
16855       ||' AND      batch_id         =  :lv_batch_id'
16856       ||' AND      sr_instance_code = :v_instance_code';
16857 
16858 
16859         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16860 
16861       EXECUTE IMMEDIATE lv_sql_stmt
16862               USING     lv_batch_id,
16863                         v_instance_code;
16864 
16865       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16866                      (p_app_short_name    => 'MSC',
16867                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
16868                       p_message_text      => lv_message_text,
16869                       p_error_text        => lv_error_text,
16870                       p_token1            => 'COLUMN_NAME',
16871                       p_token_value1      => 'DELETED_FLAG',
16872                       p_token2            => 'DEFAULT_VALUE',
16873                       p_token_value2      => SYS_NO);
16874 
16875       IF lv_return <> 0 THEN
16876         RAISE ex_logging_err;
16877       END IF;
16878 
16879       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
16880                       ||' NOT IN(1,2)';
16881 
16882       --Log a warning for those records where the deleted_flag has a value other
16883       --than SYS_NO
16884       lv_return := MSC_ST_UTIL.LOG_ERROR
16885                      (p_table_name        => 'MSC_ST_UOM_CLASS_CONVERSIONS',
16886                       p_instance_code     => v_instance_code,
16887                       p_row               => lv_column_names,
16888                       p_severity          => G_SEV_WARNING,
16889                       p_message_text      => lv_message_text,
16890                       p_error_text        => lv_error_text,
16891                       p_batch_id          => lv_batch_id,
16892                       p_where_str         => lv_where_str,
16893                       p_col_name          => 'DELETED_FLAG',
16894                       p_debug             => v_debug,
16895                       p_default_value     => SYS_NO);
16896 
16897       IF lv_return <> 0 THEN
16898         RAISE ex_logging_err;
16899       END IF;
16900 
16901       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16902                      (p_app_short_name    => 'MSC',
16903                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
16904                       p_message_text      => lv_message_text,
16905                       p_error_text        => lv_error_text,
16906                       p_token1            => 'COLUMN_NAME',
16907                       p_token_value1      => 'FROM_UOM_CODE OR FROM_UOM_CLASS OR'
16908                                              ||' TO_UOM_CODE OR TO_UOM_CLASS');
16909 
16910       IF lv_return <> 0 THEN
16911         RAISE ex_logging_err;
16912       END IF;
16913 
16914       v_sql_stmt := 16;
16915       lv_sql_stmt :=
16916       'UPDATE msc_st_uom_class_conversions'
16917       ||' SET process_flag           = '||G_ERROR_FLG||','
16918       ||'         error_text         = '||''''||lv_message_text||''''
16919       ||' WHERE (NVL(from_uom_code,'||''''||NULL_CHAR||''''||') '
16920       ||'       =                  '||''''||NULL_CHAR||''''
16921       ||' OR    NVL(from_uom_class,'||''''||NULL_CHAR||''''||') '
16922       ||'       =                  '||''''||NULL_CHAR||''''
16923       ||' OR    NVL(to_uom_code,   '||''''||NULL_CHAR||''''||') '
16924       ||'       =                  '||''''||NULL_CHAR||''''
16925       ||' OR    NVL(to_uom_class,  '||''''||NULL_CHAR||''''||') '
16926       ||'       =                  '||''''||NULL_CHAR||''''||') '
16927       ||' AND   process_flag        = '||G_IN_PROCESS
16928       ||' AND   batch_id            = :lv_batch_id'
16929       ||' AND   sr_instance_code    = :v_instance_code';
16930 
16931 
16932 
16933         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16934 
16935       EXECUTE IMMEDIATE lv_sql_stmt
16936               USING     lv_batch_id,
16937                         v_instance_code;
16938 
16939       v_sql_stmt := 17;
16940       lv_sql_stmt :=
16941       'UPDATE   msc_st_uom_class_conversions'
16942       ||' SET   from_unit_of_measure          = from_uom_code'
16943       ||' WHERE NVL(from_unit_of_measure,'||''''||NULL_CHAR||''''||')'
16944       ||'       =                        '||''''||NULL_CHAR||''''
16945       ||' AND   process_flag                  = '||G_IN_PROCESS
16946       ||' AND   batch_id                      = :lv_batch_id'
16947       ||' AND   sr_instance_code              = :v_instance_code';
16948 
16949 
16950         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16951 
16952       EXECUTE IMMEDIATE lv_sql_stmt
16953               USING     lv_batch_id,
16954                         v_instance_code;
16955 
16956       v_sql_stmt := 18;
16957       lv_sql_stmt :=
16958       'UPDATE   msc_st_uom_class_conversions '
16959       ||' SET   to_unit_of_measure  = to_uom_code'
16960       ||' WHERE NVL(to_unit_of_measure,'||''''||NULL_CHAR||''''||')'
16961       ||'       =                      '||''''||NULL_CHAR||''''
16962       ||' AND   process_flag        = '||G_IN_PROCESS
16963       ||' AND   batch_id            = :lv_batch_id'
16964       ||' AND   sr_instance_code    = :v_instance_code';
16965 
16966 
16967         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
16968 
16969       EXECUTE IMMEDIATE lv_sql_stmt
16970               USING     lv_batch_id,
16971                         v_instance_code;
16972 
16973       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
16974                      (p_app_short_name    => 'MSC',
16975                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
16976                       p_message_text      => lv_message_text,
16977                       p_error_text        => lv_error_text,
16978                       p_token1            => 'COLUMN_NAMES',
16979                       p_token_value1      => 'FROM_UOM_CODE, FROM_UOM_CLASS AND FROM_UNIT_OF_MEASURE',
16980                       p_token2            => 'MASTER_TABLE',
16981                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
16982                       p_token3            => 'MASTER_TABLE',
16983                       p_token_value3      => 'MSC_ST_UNITS_OF_MEASURE',
16984                       p_token4            => 'CHILD_TABLE',
16985                       p_token_value4      => 'MSC_ST_UOM_CLASS_CONVERSIONS');
16986       IF lv_return <> 0 THEN
16987         RAISE ex_logging_err;
16988       END IF;
16989 
16990       v_sql_stmt := 19;
16991       lv_sql_stmt :=
16992       'UPDATE    msc_st_uom_class_conversions mcc '
16993       ||' SET    process_flag  = '||G_ERROR_FLG||','
16994       ||'        error_text    = '||''''||lv_message_text||''''
16995       ||' WHERE  NOT EXISTS(SELECT 1'
16996       ||'        FROM   msc_units_of_measure mum'
16997       ||'        WHERE  mum.uom_code        = mcc.from_uom_code'
16998       ||'        AND    mum.uom_class       = mcc.from_uom_class'
16999       ||'        AND    mum.unit_of_measure = mcc.from_unit_of_measure'
17000       ||'        AND    mum.base_uom_flag   = ''Y'' '
17001       ||'        UNION'
17002       ||'        SELECT 1'
17003       ||'        FROM   msc_st_units_of_measure mum'
17004       ||'        WHERE  mum.uom_code          = mcc.from_uom_code'
17005       ||'        AND    mum.uom_class         = mcc.from_uom_class'
17006       ||'        AND    mum.unit_of_measure = mcc.from_unit_of_measure'
17007       ||'        AND    mum.base_uom_flag   = ''Y'' '
17008       ||'        AND    mum.process_flag      = '||G_VALID||')'
17009       ||' AND    mcc.batch_id         = :lv_batch_id'
17010       ||' AND    mcc.sr_instance_code = :v_instance_code'
17011       ||' AND    mcc.process_flag     = 2';
17012 
17013 
17014 
17015         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17016 
17017       EXECUTE IMMEDIATE lv_sql_stmt
17018               USING     lv_batch_id,
17019                         v_instance_code;
17020 
17021 
17022       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17023                      (p_app_short_name    => 'MSC',
17024                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
17025                       p_message_text      => lv_message_text,
17026                       p_error_text        => lv_error_text,
17027                       p_token1            => 'COLUMN_NAMES',
17028                       p_token_value1      => 'TO_UOM_CODE, TO_UOM_CLASS'
17029                                              ||' AND TO_UNIT_OF_MEASURE',
17030                       p_token2            => 'MASTER_TABLE',
17031                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
17032                       p_token3            => 'CHILD_TABLE',
17033                       p_token_value3      => 'MSC_ST_UOM_CLASS_CONVERSIONS');
17034       IF lv_return <> 0 THEN
17035         RAISE ex_logging_err;
17036       END IF;
17037 
17038       v_sql_stmt := 20;
17039       lv_sql_stmt :=
17040       'UPDATE msc_st_uom_class_conversions mcc '
17041       ||' SET    process_flag  = '||G_ERROR_FLG||','
17042       ||'        error_text   = '||''''||lv_message_text||''''
17043       ||' WHERE  NOT EXISTS(SELECT 1'
17044       ||'        FROM   msc_units_of_measure mum '
17045       ||'        WHERE  mum.uom_code          = mcc.to_uom_code'
17046       ||'        AND    mum.uom_class         = mcc.to_uom_class'
17047       ||'        AND    mum.unit_of_measure   = mcc.to_unit_of_measure'
17048       ||'        AND    mum.base_uom_flag   = ''Y'' '
17049       ||'        UNION'
17050       ||'        SELECT 1'
17051       ||'        FROM   msc_st_units_of_measure mum'
17052       ||'        WHERE  mum.uom_code          = mcc.to_uom_code'
17053       ||'        AND    mum.uom_class         = mcc.to_uom_class'
17054       ||'        AND    mum.unit_of_measure   = mcc.to_unit_of_measure'
17055       ||'        AND    mum.base_uom_flag   = ''Y'' '
17056       ||'        AND    mum.process_flag      = '||G_VALID||') '
17057       ||' AND    mcc.batch_id         = :lv_batch_id'
17058       ||' AND    mcc.sr_instance_code = :v_instance_code'
17059       ||' AND    mcc.process_flag     = 2';
17060 
17061 
17062         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17063 
17064       EXECUTE IMMEDIATE lv_sql_stmt
17065               USING     lv_batch_id,
17066                         v_instance_code;
17067 
17068 
17069       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17070                      (p_app_short_name    => 'MSC',
17071                       p_error_code        => 'MSC_PP_CONV_RT_NEG',
17072                       p_message_text      => lv_message_text,
17073                       p_error_text        => lv_error_text,
17074                       p_token1            => 'COLUMN_NAME',
17075                       p_token_value1      => 'CONVERSION_RATE');
17076 
17077       IF lv_return <> 0 THEN
17078         RAISE ex_logging_err;
17079       END IF;
17080 
17081       v_sql_stmt := 21;
17082       lv_sql_stmt :=
17083       ' UPDATE msc_st_uom_class_conversions'
17084       ||' SET    process_flag     = '||G_ERROR_FLG||', '
17085       ||'        error_text   = '||''''||lv_message_text||''''
17086       ||' WHERE  NVL(conversion_rate,'||NULL_VALUE||') <= 0'
17087       ||' AND    process_flag     = '||G_IN_PROCESS
17088       ||' AND    batch_id         = :lv_batch_id'
17089       ||' AND    sr_instance_code = :v_instance_code';
17090 
17091 
17092         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17093 
17094       EXECUTE IMMEDIATE lv_sql_stmt
17095               USING     lv_batch_id,
17096                         v_instance_code;
17097 
17098 
17099 /*      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17100                      (p_app_short_name    => 'MSC',
17101                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
17102                       p_message_text      => lv_message_text,
17103                       p_error_text        => lv_error_text,
17104                       p_token1            => 'COLUMN_NAME',
17105                       p_token_value1      => 'ORGANIZATION_CODE');
17106 
17107       IF lv_return <> 0 THEN
17108         RAISE ex_logging_err;
17109       END IF;
17110 
17111       --Org code is mandatory where item name is not null
17112       v_sql_stmt := 22;
17113       lv_sql_stmt :=
17114       ' UPDATE  msc_st_uom_class_conversions'
17115       ||' SET   process_flag   =      '||G_ERROR_FLG||', '
17116       ||'       error_text     =      '||''''||lv_message_text||''''
17117       ||' WHERE NVL(item_name,        '||''''||NULL_CHAR||''''||')'
17118       ||'       =                     '||''''||NULL_CHAR||''''
17119       ||' AND   NVL(ORGANIZATION_CODE,'||''''||NULL_CHAR||''''||') '
17120       ||'       =                     '||''''||NULL_CHAR||''''
17121       ||' AND   process_flag                  =  '||G_IN_PROCESS
17122       ||' AND   batch_id                      =  :lv_batch_id'
17123       ||' AND   sr_instance_code              =  :v_instance_code';
17124 
17125 
17126         msc_st_util.log_message(lv_sql_stmt);
17127 
17128       EXECUTE IMMEDIATE lv_sql_stmt
17129               USING     lv_batch_id,
17130                         v_instance_code;
17131 
17132       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17133                      (p_app_short_name    => 'MSC',
17134                       p_error_code        => 'MSC_PP_INVALID_VALUE',
17135                       p_message_text      => lv_message_text,
17136                       p_error_text        => lv_error_text,
17137                       p_token1            => 'COLUMN_NAME',
17138                       p_token_value1      => 'ORGANIZATION_CODE');
17139 
17140       IF lv_return <> 0 THEN
17141         RAISE ex_logging_err;
17142       END IF;
17143 
17144       --Derive Organization_id
17145       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
17146                      (p_table_name       => 'MSC_ST_UOM_CLASS_CONVERSIONS',
17147                       p_org_partner_name => 'ORGANIZATION_CODE',
17148                       p_org_partner_id   => 'ORGANIZATION_ID',
17149                       p_instance_code    => v_instance_code,
17150                       p_partner_type     => G_ORGANIZATION,
17151                       p_error_text       => lv_error_text,
17152                       p_batch_id         => lv_batch_id,
17153                       p_severity         => G_SEV3_ERROR,
17154                       p_message_text     => lv_message_text,
17155                       p_debug            => v_debug,
17156                       p_row              => lv_column_names);
17157 
17158       IF lv_return <> 0 THEN
17159         RAISE ex_logging_err;
17160       END IF; */
17161       v_sql_stmt := 23;
17162       --Update inventory_item_id
17163       lv_sql_stmt :=
17164       'UPDATE msc_st_uom_class_conversions mcc '
17165       ||' SET inventory_item_id = (SELECT local_id'
17166       ||'     FROM   msc_local_id_item mli'
17167       ||'     WHERE  mli.char1         = mcc.sr_instance_code '
17168       ||'     AND    NVL(mli.char2,       '||''''||NULL_CHAR||''''||')='
17169       ||'            NVL(mcc.company_name,'||''''||NULL_CHAR||''''||')'
17170       ||'     AND    mli.char4         = mcc.item_name'
17171       ||'     AND    mli.entity_name   = ''SR_INVENTORY_ITEM_ID'' '
17172       ||'     AND    mli.instance_id   = :v_instance_id '
17173       ||'     AND    rownum < 2) '
17174       ||' WHERE  item_name IS NOT NULL'
17175       ||' AND    sr_instance_code      = :v_instance_code'
17176       ||' AND    batch_id              = :lv_batch_id'
17177       ||' AND    process_flag          = '||G_IN_PROCESS;
17178 
17179 
17180          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17181 
17182     EXECUTE IMMEDIATE lv_sql_stmt
17183             USING     v_instance_id,
17184                       v_instance_code,
17185                       lv_batch_id;
17186 
17187 
17188       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17189                      (p_app_short_name    => 'MSC',
17190                       p_error_code        => 'MSC_PP_INVALID_VALUE',
17191                       p_message_text      => lv_message_text,
17192                       p_error_text        => lv_error_text,
17193                       p_token1            => 'COLUMN_NAME',
17194                       p_token_value1      => 'ITEM_NAME');
17195 
17196       IF lv_return <> 0 THEN
17197         RAISE ex_logging_err;
17198       END IF;
17199       v_sql_stmt := 24;
17200       lv_sql_stmt :=
17201       'UPDATE msc_st_uom_class_conversions '
17202       ||' SET   error_text   = '||''''||lv_message_text||''''||','
17203       ||'       process_flag = '||g_error
17204       ||' WHERE inventory_item_id IS NULL '
17205       ||' AND   item_name IS NOT NULL '
17206       ||' AND   sr_instance_code       = :v_instance_code'
17207       ||' AND   batch_id               = :lv_batch_id'
17208       ||' AND   process_flag           = ' ||G_IN_PROCESS;
17209 
17210 
17211         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_sql_stmt);
17212 
17213       EXECUTE IMMEDIATE lv_sql_stmt
17214               USING     v_instance_code,
17215                         lv_batch_id;
17216 
17217 
17218       --Call to customised validation.
17219       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
17220         (ERRBUF         => lv_error_text,
17221          RETCODE        => lv_return,
17222          pBatchID       => lv_batch_id,
17223          pInstanceCode  => v_instance_code,
17224          pEntityName    => 'MSC_ST_UOM_CLASS_CONVERSIONS',
17225          pInstanceID    => v_instance_id);
17226 
17227       IF NVL(lv_return,0) <> 0 THEN
17228         RAISE ex_logging_err;
17229       END IF;
17230 
17231       v_sql_stmt := 25;
17232       --Fixed against the bug#2734588.
17233 
17234       UPDATE msc_st_uom_class_conversions
17235       SET    inventory_item_id   = 0
17236       WHERE  item_name IS NULL
17237       AND    process_flag        = G_IN_PROCESS
17238       AND    batch_id            = lv_batch_id
17239       AND    sr_instance_code    = v_instance_code;
17240 
17241       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
17242                   (p_table_name     => 'MSC_ST_UOM_CLASS_CONVERSIONS',
17243                    p_instance_id    => v_instance_id,
17244                    p_instance_code  => v_instance_code,
17245                    p_process_flag   => G_VALID,
17246                    p_error_text     => lv_error_text,
17247                    p_debug          => v_debug,
17248                    p_batch_id       => lv_batch_id);
17249 
17250       IF lv_return <> 0 THEN
17251         RAISE ex_logging_err;
17252       END IF;
17253 
17254       lv_return := MSC_ST_UTIL.LOG_ERROR
17255                    (p_table_name        => 'MSC_ST_UOM_CLASS_CONVERSIONS',
17256                     p_instance_code     => v_instance_code,
17257                     p_row               => lv_column_names,
17258                     p_severity          => G_SEV_ERROR,
17259                     p_message_text      => NULL,
17260                     p_error_text        => lv_error_text,
17261                     p_debug             => v_debug,
17262                     p_batch_id          => lv_batch_id);
17263 
17264       IF lv_return <> 0 THEN
17265         RAISE ex_logging_err;
17266       END IF;
17267 
17268       COMMIT;
17269     END LOOP;
17270 
17271     --Validation check for the table msc_st_uom_class_conversions.
17272 
17273     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17274                  (p_app_short_name    => 'MSC',
17275                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
17276                   p_message_text      => lv_message_text,
17277                   p_error_text        => lv_error_text);
17278 
17279     IF lv_return <> 0 THEN
17280       RAISE ex_logging_err;
17281     END IF;
17282 
17283     --Duplicate records check for the records whose source is XML.
17284     v_sql_stmt := 23;
17285     lv_sql_stmt :=
17286     'UPDATE     msc_st_uom_conversions mcc1 '
17287     ||' SET     process_flag = '||G_ERROR_FLG||','
17288     ||'         error_text   = '||''''||lv_message_text||''''
17289     ||' WHERE   message_id <  (SELECT MAX(message_id) '
17290     ||'         FROM   msc_st_uom_conversions mcc2 '
17291     ||'         WHERE  NVL(mcc2.item_name,        '||''''||NULL_CHAR||''''||') = '
17292     ||'                NVL(mcc1.item_name,        '||''''||NULL_CHAR||''''||')'
17293     ||'         AND    NVL(mcc2.unit_of_measure,  '||''''||NULL_CHAR||''''||') = '
17294     ||'                NVL(mcc1.unit_of_measure,  '||''''||NULL_CHAR||''''||')'
17295     ||'         AND    NVL(mcc2.company_name,     '||''''||NULL_CHAR||''''||') = '
17296     ||'                NVL(mcc1.company_name,     '||''''||NULL_CHAR||''''||')'
17297     ||'         AND    mcc2.process_flag =        '||G_IN_PROCESS
17298     ||'         AND    NVL(mcc2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
17299     ||' AND    mcc1.process_flag     = '||G_IN_PROCESS
17300     ||' AND    mcc1.sr_instance_code = :v_instance_code'
17301     ||' AND    NVL(mcc1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
17302 
17303 
17304 
17305       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17306 
17307     EXECUTE IMMEDIATE lv_sql_stmt
17308             USING     v_instance_code;
17309 
17310 
17311     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17312                  (p_app_short_name    => 'MSC',
17313                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
17314                   p_message_text      => lv_message_text,
17315                   p_error_text        => lv_error_text);
17316 
17317     IF lv_return <> 0 THEN
17318       RAISE ex_logging_err;
17319     END IF;
17320 
17321     --Duplicate records check for the records whose source is other than XML
17322     --Different SQL is used because in XML we can identify the latest records
17323     --whereas in batch load we cannot.
17324     v_sql_stmt := 24;
17325     lv_sql_stmt :=
17326     'UPDATE     msc_st_uom_conversions mcc1 '
17327     ||' SET     process_flag   = '||G_ERROR_FLG||','
17328     ||'         error_text     = '||''''||lv_message_text||''''
17329     ||' WHERE   EXISTS( SELECT 1 '
17330     ||'         FROM   msc_st_uom_conversions mcc2'
17331     ||'         WHERE  NVL(mcc2.item_name,         '||''''||NULL_CHAR||''''||') = '
17332     ||'                NVL(mcc1.item_name,         '||''''||NULL_CHAR||''''||')'
17333     ||'         AND    NVL(mcc2.unit_of_measure,   '||''''||NULL_CHAR||''''||') = '
17334     ||'                NVL(mcc1.unit_of_measure,   '||''''||NULL_CHAR||''''||')'
17335     ||'         AND    NVL(mcc2.company_name,      '||''''||NULL_CHAR||''''||') = '
17336     ||'                NVL(mcc1.company_name,      '||''''||NULL_CHAR||''''||')'
17337     ||'         AND    mcc2.process_flag      =     '||G_IN_PROCESS
17338     ||'         AND    NVL(mcc2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
17339     ||'         GROUP BY item_name,company_name,'
17340     ||'                unit_of_measure'
17341     ||'         HAVING COUNT(*) > 1)'
17342     ||' AND     mcc1.process_flag          = '||G_IN_PROCESS
17343     ||' AND     mcc1.sr_instance_code      = :v_instance_code'
17344     ||' AND     NVL(mcc1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
17345 
17346 
17347       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17348 
17349     EXECUTE IMMEDIATE lv_sql_stmt
17350             USING     v_instance_code;
17351 
17352     lv_column_names :=
17353     'UNIT_OF_MEASURE          ||''~''||'
17354     ||'UOM_CODE                ||''~''||'
17355     ||'UOM_CLASS               ||''~''||'
17356     ||'INVENTORY_ITEM_ID       ||''~''||'
17357     ||'CONVERSION_RATE         ||''~''||'
17358     ||'DEFAULT_CONVERSION_FLAG ||''~''||'
17359     ||'SR_INSTANCE_CODE        ||''~''||'
17360     ||'DISABLE_DATE            ||''~''||'
17361     ||'ITEM_NAME               ||''~''||'
17362     ||'ORGANIZATION_CODE       ||''~''||'
17363     ||'COMPANY_NAME            ||''~''||'
17364     ||'DELETED_FLAG';
17365 
17366     LOOP
17367       v_sql_stmt := 25;
17368       SELECT msc_st_batch_id_s.NEXTVAL
17369       INTO   lv_batch_id
17370       FROM   dual;
17371 
17372       v_sql_stmt := 26;
17373       lv_sql_stmt :=
17374       'UPDATE msc_st_uom_conversions '
17375       ||' SET    batch_id  = :lv_batch_id'
17376       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
17377       ||' AND    sr_instance_code               = :v_instance_code'
17378       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
17379       ||' AND    rownum                        <= '||v_batch_size;
17380 
17381 
17382         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17383 
17384       EXECUTE IMMEDIATE lv_sql_stmt
17385               USING     lv_batch_id,
17386                         v_instance_code;
17387 
17388       EXIT WHEN SQL%NOTFOUND;
17389 
17390       OPEN c3(lv_batch_id);
17391       FETCH c3 BULK COLLECT INTO lb_rowid;
17392       CLOSE c3;
17393 
17394       v_sql_stmt := 27;
17395       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
17396       UPDATE msc_st_uom_conversions
17397       SET    st_transaction_id = msc_st_uom_conversions_s.NEXTVAL,
17398              refresh_id        = v_refresh_id,
17399              last_update_date  = v_current_date,
17400              last_updated_by   = v_current_user,
17401              creation_date     = v_current_date,
17402              created_by        = v_current_user
17403       WHERE  rowid             = lb_rowid(j);
17404 
17405       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17406                      (p_app_short_name    => 'MSC',
17407                       p_error_code        => 'MSC_PP_NO_DELETION',
17408                       p_message_text      => lv_message_text,
17409                       p_error_text        => lv_error_text,
17410                       p_token1            => 'TABLE_NAME',
17411                       p_token_value1      => 'MSC_ST_UOM_CONVERSIONS');
17412 
17413       IF lv_return <> 0 THEN
17414         RAISE ex_logging_err;
17415       END IF;
17416 
17417       --Deletion is not allowed on this table.
17418       v_sql_stmt := 28;
17419       lv_sql_stmt :=
17420       'UPDATE      msc_st_uom_conversions'
17421       ||' SET      process_flag     = '||G_ERROR_FLG||','
17422       ||'          error_text       = '||''''||lv_message_text||''''
17423       ||' WHERE    deleted_flag     = '||SYS_YES
17424       ||' AND      process_flag     = '||G_IN_PROCESS
17425       ||' AND      batch_id         = :lv_batch_id'
17426       ||' AND      sr_instance_code = :v_instance_code';
17427 
17428 
17429         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17430 
17431       EXECUTE IMMEDIATE lv_sql_stmt
17432               USING     lv_batch_id,
17433                         v_instance_code;
17434 
17435       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17436                      (p_app_short_name    => 'MSC',
17437                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
17438                       p_message_text      => lv_message_text,
17439                       p_error_text        => lv_error_text,
17440                       p_token1            => 'COLUMN_NAME',
17441                       p_token_value1      => 'ITEM_NAME');
17442 
17443       IF lv_return <> 0 THEN
17444         RAISE ex_logging_err;
17445       END IF;
17446 
17447       --Item Name is mandatory if the Default Conversion Flag is set to 'Y' (fix for 2747791)
17448       v_sql_stmt := 29;
17449       lv_sql_stmt :=
17450       'UPDATE      msc_st_uom_conversions'
17451       ||' SET      process_flag     = '||G_ERROR_FLG||','
17452       ||'          error_text       = '||''''||lv_message_text||''''
17453       ||' WHERE    item_name IS NULL '
17454       ||' AND      default_conversion_flag     = ''Y'' '
17455       ||' AND      process_flag     = '||G_IN_PROCESS
17456       ||' AND      batch_id         = :lv_batch_id'
17457       ||' AND      sr_instance_code = :v_instance_code';
17458 
17459 
17460         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17461 
17462       EXECUTE IMMEDIATE lv_sql_stmt
17463               USING     lv_batch_id,
17464                         v_instance_code;
17465 
17466 
17467       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17468                      (p_app_short_name    => 'MSC',
17469                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
17470                       p_message_text      => lv_message_text,
17471                       p_error_text        => lv_error_text,
17472                       p_token1            => 'COLUMN_NAME',
17473                       p_token_value1      => 'DELETED_FLAG',
17474                       p_token2            => 'DEFAULT_VALUE',
17475                       p_token_value2      => SYS_NO);
17476 
17477       IF lv_return <> 0 THEN
17478         RAISE ex_logging_err;
17479       END IF;
17480 
17481       lv_where_str :=
17482       ' AND NVL(deleted_flag,'||NULL_VALUE||') '
17483       ||' NOT IN(1,2)';
17484 
17485       --Log a warning for those records where the deleted_flag has a value other
17486       --than SYS_NO
17487       lv_return := MSC_ST_UTIL.LOG_ERROR
17488                      (p_table_name        => 'MSC_ST_UOM_CONVERSIONS',
17489                       p_instance_code     => v_instance_code,
17490                       p_row               => lv_column_names,
17491                       p_severity          => G_SEV_WARNING,
17492                       p_message_text      => lv_message_text,
17493                       p_error_text        => lv_error_text,
17494                       p_batch_id          => lv_batch_id,
17495                       p_where_str         => lv_where_str,
17496                       p_col_name          => 'DELETED_FLAG',
17497                       p_debug             => v_debug,
17498                       p_default_value     => SYS_NO);
17499 
17500       IF lv_return <> 0 THEN
17501         RAISE ex_logging_err;
17502       END IF;
17503 
17504       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17505                      (p_app_short_name    => 'MSC',
17506                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
17507                       p_message_text      => lv_message_text,
17508                       p_error_text        => lv_error_text,
17509                       p_token1            => 'COLUMN_NAME',
17510                       p_token_value1      => 'UOM_CODE OR UOM_CLASS');
17511 
17512       IF lv_return <> 0 THEN
17513         RAISE ex_logging_err;
17514       END IF;
17515 
17516       v_sql_stmt := 30;
17517       lv_sql_stmt :=
17518       'UPDATE   msc_st_uom_conversions'
17519       ||' SET   process_flag     = '||G_ERROR_FLG||','
17520       ||'         error_text     = '||''''||lv_message_text||''''
17521       ||' WHERE (NVL(uom_code,     '||''''||NULL_CHAR||''''||') '
17522       ||'       =                  '||''''||NULL_CHAR||''''
17523       ||' OR    NVL(uom_class,     '||''''||NULL_CHAR||''''||') '
17524       ||'       =                  '||''''||NULL_CHAR||''''||') '
17525       ||' AND   process_flag        = '||G_IN_PROCESS
17526       ||' AND   batch_id            = :lv_batch_id'
17527       ||' AND   sr_instance_code    = :v_instance_code';
17528 
17529 
17530         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17531       EXECUTE IMMEDIATE lv_sql_stmt
17532               USING     lv_batch_id,
17533                         v_instance_code;
17534 
17535       v_sql_stmt := 30;
17536       lv_sql_stmt :=
17537       'UPDATE   msc_st_uom_conversions'
17538       ||' SET   unit_of_measure     = uom_code'
17539       ||' WHERE NVL(unit_of_measure,'||''''||NULL_CHAR||''''||')'
17540       ||'        =                  '||''''||NULL_CHAR||''''
17541       ||' AND   process_flag               = '||G_IN_PROCESS
17542       ||' AND   batch_id                   = :lv_batch_id'
17543       ||' AND   sr_instance_code           = :v_instance_code';
17544 
17545 
17546         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17547 
17548       EXECUTE IMMEDIATE lv_sql_stmt
17549               USING     lv_batch_id,
17550                         v_instance_code;
17551 
17552       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17553                      (p_app_short_name    => 'MSC',
17554                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
17555                       p_message_text      => lv_message_text,
17556                       p_error_text        => lv_error_text,
17557                       p_token1            => 'COLUMN_NAMES',
17558                       p_token_value1      => 'UOM_CODE, UOM_CLASS AND UNIT_OF_MEASURE',
17559                       p_token2            => 'MASTER_TABLE',
17560                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
17561                       p_token3            => 'CHILD_TABLE',
17562                       p_token_value3      => 'MSC_ST_UOM_CONVERSIONS');
17563       IF lv_return <> 0 THEN
17564         RAISE ex_logging_err;
17565       END IF;
17566 
17567       v_sql_stmt := 31;
17568       lv_sql_stmt :=
17569       ' UPDATE   msc_st_uom_conversions mcc'
17570       ||' SET    process_flag  = '||G_ERROR_FLG||','
17571       ||'        error_text    = '||''''||lv_message_text||''''
17572       ||' WHERE  NOT EXISTS(SELECT 1'
17573       ||'        FROM   msc_units_of_measure mum'
17574       ||'        WHERE  mum.uom_code        = mcc.uom_code'
17575       ||'        AND    mum.uom_class       = mcc.uom_class'
17576       ||'        AND    mum.unit_of_measure = mcc.unit_of_measure'
17577       ||'        UNION'
17578       ||'        SELECT 1'
17579       ||'        FROM   msc_st_units_of_measure mum'
17580       ||'        WHERE  mum.uom_code        = mcc.uom_code'
17581       ||'        AND    mum.uom_class       = mcc.uom_class'
17582       ||'        AND    mum.unit_of_measure = mcc.unit_of_measure'
17583       ||'        AND    mum.process_flag    = '||G_VALID||')'
17584       ||' AND   batch_id                    = :lv_batch_id'
17585       ||' AND    mcc.sr_instance_code       = :v_instance_code'
17586       ||' AND    mcc.process_flag           = '||G_IN_PROCESS;
17587 
17588 
17589         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17590 
17591       EXECUTE IMMEDIATE lv_sql_stmt
17592               USING     lv_batch_id,
17593                         v_instance_code;
17594 
17595 
17596       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17597                      (p_app_short_name    => 'MSC',
17598                       p_error_code        => 'MSC_PP_CONV_RT_NEG',
17599                       p_message_text      => lv_message_text,
17600                       p_error_text        => lv_error_text,
17601                       p_token1            => 'COLUMN_NAME',
17602                       p_token_value1      => 'CONVERSION_RATE');
17603 
17604       IF lv_return <> 0 THEN
17605         RAISE ex_logging_err;
17606       END IF;
17607 
17608       v_sql_stmt := 32;
17609       lv_sql_stmt :=
17610       'UPDATE    msc_st_uom_conversions'
17611       ||' SET    process_flag     = '||G_ERROR_FLG||','
17612       ||'        error_text       = '||''''||lv_message_text||''''
17613       ||' WHERE  NVL(conversion_rate,'||NULL_VALUE||')  <= 0'
17614       ||' AND    process_flag     = '||G_IN_PROCESS
17615       ||' AND    batch_id         = :lv_batch_id'
17616       ||' AND    sr_instance_code = :v_instance_code';
17617 
17618 
17619         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17620 
17621       EXECUTE IMMEDIATE lv_sql_stmt
17622               USING     lv_batch_id,
17623                         v_instance_code;
17624 
17625       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17626                      (p_app_short_name    => 'MSC',
17627                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
17628                       p_message_text      => lv_message_text,
17629                       p_error_text        => lv_error_text,
17630                       p_token1            => 'COLUMN_NAME',
17631                       p_token_value1      => 'ORGANIZATION_CODE');
17632 
17633       IF lv_return <> 0 THEN
17634         RAISE ex_logging_err;
17635       END IF;
17636 
17637 /*    --Org code is mandatory where item name is not null
17638       v_sql_stmt := 33;
17639       lv_sql_stmt :=
17640       'UPDATE    msc_st_uom_conversions'
17641       ||' SET    process_flag          = '||G_ERROR_FLG||','
17642       ||'        error_text            = '||''''||lv_message_text||''''
17643       ||' WHERE  NVL(item_name,          '||''''||NULL_CHAR||''''||') '
17644       ||'        <>                      '||''''||NULL_CHAR||''''
17645       ||' AND    NVL(ORGANIZATION_CODE,  '||''''||NULL_CHAR||''''||') '
17646       ||'        =                       '||''''||NULL_CHAR||''''
17647       ||' AND    process_flag        =   '||G_IN_PROCESS
17648       ||' AND    batch_id            = :lv_batch_id'
17649       ||' AND    sr_instance_code    = :v_instance_code';
17650 
17651 
17652         msc_st_util.log_message(lv_sql_stmt);
17653 
17654       EXECUTE IMMEDIATE lv_sql_stmt
17655               USING     lv_batch_id,
17656                         v_instance_code;
17657 
17658 
17659       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17660                      (p_app_short_name    => 'MSC',
17661                       p_error_code        => 'MSC_PP_INVALID_VALUE',
17662                       p_message_text      => lv_message_text,
17663                       p_error_text        => lv_error_text,
17664                       p_token1            => 'COLUMN_NAME',
17665                       p_token_value1      => 'ORGANIZATION_CODE');
17666 
17667       IF lv_return <> 0 THEN
17668         RAISE ex_logging_err;
17669       END IF;
17670 
17671       --Derive Organization_id
17672       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
17673                      (p_table_name       => 'MSC_ST_UOM_CONVERSIONS',
17674                       p_org_partner_name => 'ORGANIZATION_CODE',
17675                       p_org_partner_id   => 'ORGANIZATION_ID',
17676                       p_instance_code    => v_instance_code,
17677                       p_partner_type     => G_ORGANIZATION,
17678                       p_error_text       => lv_error_text,
17679                       p_batch_id         => lv_batch_id,
17680                       p_severity         => G_SEV3_ERROR,
17681                       p_message_text     => lv_message_text,
17682                       p_debug            => v_debug,
17683                       p_row              => lv_column_names);
17684 
17685       IF lv_return <> 0 THEN
17686         RAISE ex_logging_err;
17687       END IF; */
17688 
17689       --Update inventory_item_id
17690       lv_sql_stmt :=
17691       'UPDATE msc_st_uom_conversions mcc '
17692       ||' SET inventory_item_id = (SELECT local_id'
17693       ||'     FROM   msc_local_id_item mli'
17694       ||'     WHERE  mli.char1         = mcc.sr_instance_code '
17695       ||'     AND    NVL(mli.char2,       '||''''||NULL_CHAR||''''||')='
17696       ||'            NVL(mcc.company_name,'||''''||NULL_CHAR||''''||')'
17697       ||'     AND    mli.char4         = mcc.item_name'
17698       ||'     AND    mli.entity_name   = ''SR_INVENTORY_ITEM_ID'' '
17699       ||'     AND    mli.instance_id   = :v_instance_id '
17700       ||'     AND    rownum < 2 )'
17701       ||' WHERE  item_name IS NOT NULL'
17702       ||' AND    sr_instance_code      = :v_instance_code'
17703       ||' AND    batch_id              = :lv_batch_id'
17704       ||' AND    process_flag          = '||G_IN_PROCESS;
17705 
17706 
17707          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17708 
17709     EXECUTE IMMEDIATE lv_sql_stmt
17710             USING     v_instance_id,
17711                       v_instance_code,
17712                       lv_batch_id;
17713 
17714 
17715       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17716                      (p_app_short_name    => 'MSC',
17717                       p_error_code        => 'MSC_PP_INVALID_VALUE',
17718                       p_message_text      => lv_message_text,
17719                       p_error_text        => lv_error_text,
17720                       p_token1            => 'COLUMN_NAME',
17721                       p_token_value1      => 'ITEM_NAME');
17722 
17723       IF lv_return <> 0 THEN
17724         RAISE ex_logging_err;
17725       END IF;
17726 
17727       lv_sql_stmt :=
17728       'UPDATE msc_st_uom_class_conversions '
17729       ||' SET   error_text   = '||''''||lv_message_text||''''||','
17730       ||'       process_flag = '||g_error
17731       ||' WHERE inventory_item_id IS NULL '
17732       ||' AND   item_name IS NOT NULL '
17733       ||' AND   sr_instance_code       = :v_instance_code'
17734       ||' AND   batch_id               = :lv_batch_id'
17735       ||' AND   process_flag           = ' ||G_IN_PROCESS;
17736 
17737 
17738         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_sql_stmt);
17739 
17740       EXECUTE IMMEDIATE lv_sql_stmt
17741               USING     v_instance_code,
17742                         lv_batch_id;
17743 
17744       --Item id is set to 0 if the inventory_item_id is NULL
17745       v_sql_stmt := 34;
17746       lv_sql_stmt :=
17747       'UPDATE      msc_st_uom_conversions '
17748       ||' SET      inventory_item_id     = 0 '
17749       ||' WHERE    process_flag     = '||G_IN_PROCESS
17750       ||' AND      batch_id         = :lv_batch_id'
17751       ||' AND      sr_instance_code = :v_instance_code'
17752       ||' AND      inventory_item_id IS NULL ';
17753 
17754 
17755         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17756 
17757       EXECUTE IMMEDIATE lv_sql_stmt
17758               USING     lv_batch_id,
17759                         v_instance_code;
17760 
17761       --Call to customised validation.
17762       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
17763         (ERRBUF         => lv_error_text,
17764          RETCODE        => lv_return,
17765          pBatchID       => lv_batch_id,
17766          pInstanceCode  => v_instance_code,
17767          pEntityName    => 'MSC_ST_UOM_CONVERSIONS',
17768          pInstanceID    => v_instance_id);
17769 
17770       IF NVL(lv_return,0) <> 0 THEN
17771         RAISE ex_logging_err;
17772       END IF;
17773 
17774       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
17775                   (p_table_name     => 'MSC_ST_UOM_CONVERSIONS',
17776                    p_instance_id    => v_instance_id,
17777                    p_instance_code  => v_instance_code,
17778                    p_process_flag   => G_VALID,
17779                    p_error_text     => lv_error_text,
17780                    p_debug          => v_debug,
17781                    p_batch_id       => lv_batch_id);
17782 
17783       IF lv_return <> 0 THEN
17784         RAISE ex_logging_err;
17785       END IF;
17786 
17787       lv_return := MSC_ST_UTIL.LOG_ERROR
17788                    (p_table_name        => 'MSC_ST_UOM_CONVERSIONS',
17789                     p_instance_code     => v_instance_code,
17790                     p_row               => lv_column_names,
17791                     p_severity          => G_SEV_ERROR,
17792                     p_message_text      => NULL,
17793                     p_error_text        => lv_error_text,
17794                     p_debug             => v_debug,
17795                     p_batch_id          => lv_batch_id);
17796 
17797       IF lv_return <> 0 THEN
17798         RAISE ex_logging_err;
17799       END IF;
17800 
17801       COMMIT;
17802     END LOOP;
17803 
17804   EXCEPTION
17805     WHEN ex_logging_err THEN
17806         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
17807       ROLLBACK;
17808 
17809     WHEN OTHERS THEN
17810       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_UOM_CLASS_CONV'||'('
17811                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
17812         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
17813       ROLLBACK;
17814 
17815   END LOAD_UOM_CLASS_CONV;
17816 
17817 /*==========================================================================+
17818 | DESCRIPTION  : This procedure validates the records and updates the table |
17819 |                msc_st_designators.                                        |
17820 |                                                                           |
17821 |                Designator Types                    Type Code              |
17822 |                ---------------------------------------------------------- |
17823 |                Manual MDS                          1                      |
17824 |                Plan Orders                         2                      |
17825 |                Forecast                            6                      |
17826 +==========================================================================*/
17827   PROCEDURE LOAD_DESIGNATOR IS
17828   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
17829   lb_rowid          RowidTab;
17830   lv_error_text     VARCHAR2(250);
17831   lv_column_names   VARCHAR2(5000);--stores concatenated column names
17832   lv_where_str      VARCHAR2(5000);
17833   lv_sql_stmt       VARCHAR2(5000);
17834   lv_return         NUMBER;
17835 
17836   lv_batch_id       msc_st_designators.batch_id%TYPE;
17837   lv_message_text   msc_errors.error_text%TYPE;
17838   ex_logging_err    EXCEPTION;
17839 
17840   CURSOR c1(lv_batch_id NUMBER) IS
17841     SELECT rowid
17842     FROM   msc_st_designators
17843     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
17844     AND    sr_instance_code = v_instance_code
17845     AND    batch_id         = lv_batch_id;
17846 
17847   BEGIN
17848 
17849     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17850                  (p_app_short_name    => 'MSC',
17851                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
17852                   p_message_text      => lv_message_text,
17853                   p_error_text        => lv_error_text);
17854 
17855     IF lv_return <> 0 THEN
17856       RAISE ex_logging_err;
17857     END IF;
17858 
17859     --Duplicate records check for the records whose source is XML
17860     --Based on bug#2736771 UDK has been changed.
17861     v_sql_stmt := 01;
17862     lv_sql_stmt :=
17863     'UPDATE     msc_st_designators msd1 '
17864     ||' SET     process_flag = '||G_ERROR_FLG||','
17865     ||'         error_text   = '||''''||lv_message_text||''''
17866     ||' WHERE   message_id <  (SELECT MAX(message_id)'
17867     ||'         FROM   msc_st_designators msd2'
17868     ||'         WHERE  msd2.sr_instance_code      = msd1.sr_instance_code'
17869     ||'         AND    NVL(msd2.company_name,  '||''''||NULL_CHAR||''''||') = '
17870     ||'                NVL(msd1.company_name,  '||''''||NULL_CHAR||''''||')'
17871     ||'         AND    msd2.organization_code     = msd1.organization_code'
17872     ||'         AND    msd2.designator            = msd1.designator'
17873     ||'         AND    msd2.process_flag          = '||G_IN_PROCESS
17874     ||'         AND    msd2.sr_instance_code      = msd1.sr_instance_code'
17875     ||'         AND    NVL(msd2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
17876     ||' AND    msd1.process_flag     = '||G_IN_PROCESS
17877     ||' AND    msd1.sr_instance_code = :v_instance_code'
17878     ||' AND    NVL(msd1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
17879 
17880 
17881       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17882 
17883     EXECUTE IMMEDIATE lv_sql_stmt
17884             USING     v_instance_code;
17885 
17886     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17887                  (p_app_short_name    => 'MSC',
17888                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
17889                   p_message_text      => lv_message_text,
17890                   p_error_text        => lv_error_text);
17891 
17892     IF lv_return <> 0 THEN
17893       RAISE ex_logging_err;
17894     END IF;
17895 
17896     --Duplicate records check for the records whose source is other than XML
17897     --Different SQL is used because in XML we can identify the latest records
17898     --whereas in batch load we cannot.
17899     v_sql_stmt := 02;
17900     lv_sql_stmt :=
17901     'UPDATE  msc_st_designators msd1'
17902     ||' SET     process_flag  = '||G_ERROR_FLG||','
17903     ||'      error_text   = '||''''||lv_message_text||''''
17904     ||' WHERE   EXISTS( SELECT 1 '
17905     ||'         FROM   msc_st_designators msd2'
17906     ||'         WHERE  msd2.sr_instance_code      = msd1.sr_instance_code'
17907     ||'         AND    NVL(msd2.company_name,  '||''''||NULL_CHAR||''''||') = '
17908     ||'                NVL(msd1.company_name,  '||''''||NULL_CHAR||''''||')'
17909     ||'         AND    msd2.organization_code     = msd1.organization_code'
17910     ||'         AND    msd2.designator            = msd1.designator'
17911     ||'         AND    msd2.process_flag          = '||G_IN_PROCESS
17912     ||'         AND    msd2.sr_instance_code      = msd1.sr_instance_code'
17913     ||'         AND    NVL(msd2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
17914     ||'         GROUP BY sr_instance_code,organization_code,company_name,'
17915     ||'                designator'
17916     ||'         HAVING COUNT(*) > 1)'
17917     ||' AND     msd1.process_flag = '||G_IN_PROCESS
17918     ||' AND     msd1.sr_instance_code = :v_instance_code'
17919     ||' AND     NVL(msd1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
17920 
17921 
17922       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17923 
17924     EXECUTE IMMEDIATE lv_sql_stmt
17925             USING     v_instance_code;
17926 
17927     lv_column_names :=
17928     'DESIGNATOR                    ||''~''||'
17929     ||'ORGANIZATION_CODE           ||''~''||'
17930     ||'MPS_RELIEF                  ||''~''||'
17931     ||'INVENTORY_ATP_FLAG          ||''~''||'
17932     ||'DESCRIPTION                 ||''~''||'
17933     ||'DISABLE_DATE                ||''~''||'
17934     ||'DEMAND_CLASS                ||''~''||'
17935     ||'ORGANIZATION_SELECTION      ||''~''||'
17936     ||'PRODUCTION                  ||''~''||'
17937     ||'SR_INSTANCE_CODE            ||''~''||'
17938     ||'DELETED_FLAG                ||''~''||'
17939     ||'BUCKET_TYPE                 ||''~''||'
17940     ||'RECOMMENDATION_RELEASE      ||''~''||'
17941     ||'CONSUME_FORECAST            ||''~''||'
17942     ||'UPDATE_TYPE                 ||''~''||'
17943     ||'FORWARD_UPDATE_TIME_FENCE   ||''~''||'
17944     ||'BACKWARD_UPDATE_TIME_FENCE  ||''~''||'
17945     ||'OUTLIER_UPDATE_PERCENTAGE   ||''~''||'
17946     ||'CUSTOMER_NAME               ||''~''||'
17947     ||'SHIP_TO_SITE_CODE           ||''~''||'
17948     ||'BILL_TO_SITE_CODE           ||''~''||'
17949     ||'FORECAST_SET                ||''~''||'
17950     ||'COMPANY_NAME                ||''~''||'
17951     ||'DESIGNATOR_TYPE             ||''~''||'
17952     ||'PROBABILITY';
17953 
17954     LOOP
17955       v_sql_stmt := 03;
17956 
17957       SELECT msc_st_batch_id_s.NEXTVAL
17958       INTO   lv_batch_id
17959       FROM   dual;
17960 
17961       v_sql_stmt := 04;
17962       lv_sql_stmt :=
17963       'UPDATE msc_st_designators '
17964       ||' SET    batch_id  = :lv_batch_id'
17965       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
17966       ||' AND    sr_instance_code               = :v_instance_code'
17967       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
17968       ||' AND    rownum                        <= '||v_batch_size;
17969 
17970 
17971         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
17972 
17973       EXECUTE IMMEDIATE lv_sql_stmt
17974               USING     lv_batch_id,
17975                         v_instance_code;
17976 
17977       EXIT WHEN SQL%NOTFOUND;
17978 
17979       OPEN c1(lv_batch_id);
17980       FETCH c1 BULK COLLECT INTO lb_rowid;
17981       CLOSE c1;
17982 
17983       v_sql_stmt := 05;
17984       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
17985       UPDATE msc_st_designators
17986       SET    st_transaction_id   = msc_st_designators_s.NEXTVAL,
17987              refresh_id          = v_refresh_id,
17988              last_update_date    = v_current_date,
17989              last_updated_by     = v_current_user,
17990              creation_date       = v_current_date,
17991              created_by          =  v_current_user
17992       WHERE  rowid               = lb_rowid(j);
17993 
17994       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
17995                      (p_app_short_name    => 'MSC',
17996                       p_error_code        => 'MSC_PP_INVALID_VALUE',
17997                       p_message_text      => lv_message_text,
17998                       p_error_text        => lv_error_text,
17999                       p_token1            => 'COLUMN_NAME',
18000                       p_token_value1      => 'DESIGNATOR_TYPE');
18001 
18002       IF lv_return <> 0 THEN
18003         RAISE ex_logging_err;
18004       END IF;
18005 
18006       v_sql_stmt := 06;
18007       lv_sql_stmt :=
18008       'UPDATE   msc_st_designators '
18009       ||' SET   error_text   = '||''''||lv_message_text||''''||','
18010       ||'       process_flag = '||G_ERROR_FLG
18011       ||' WHERE nvl(designator_type,'||NULL_VALUE||') NOT IN(1,2,6)'
18012       ||' AND   deleted_flag     =  '||SYS_NO
18013       ||' AND   process_flag     =  '||G_IN_PROCESS
18014       ||' AND   batch_id         =  :lv_batch_id'
18015       ||' AND   sr_instance_code =  :v_instance_code';
18016 
18017 
18018         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18019 
18020       EXECUTE IMMEDIATE lv_sql_stmt
18021               USING     lv_batch_id,
18022                         v_instance_code;
18023 
18024      -- Set message
18025 
18026       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18027                      (p_app_short_name    => 'MSC',
18028                       p_error_code        => 'MSC_PP_NO_DELETION',
18029                       p_message_text      => lv_message_text,
18030                       p_error_text        => lv_error_text,
18031                       p_token1            => 'TABLE_NAME',
18032                       p_token_value1      => 'MSC_ST_DESIGNATORS');
18033 
18034       IF lv_return <> 0 THEN
18035         RAISE ex_logging_err;
18036       END IF;
18037 
18038       -- Deletion is not allowed for this table
18039       v_sql_stmt := 07;
18040       lv_sql_stmt :=
18041       'UPDATE       msc_st_designators '
18042       ||' SET      process_flag     = '||G_ERROR_FLG||','
18043       ||'          error_text   = '||''''||lv_message_text||''''
18044       ||' WHERE    deleted_flag     = '||SYS_YES
18045       ||' AND      process_flag     = '||G_IN_PROCESS
18046       ||' AND      batch_id         =  :lv_batch_id'
18047       ||' AND      sr_instance_code = :v_instance_code';
18048 
18049 
18050 
18051         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18052       EXECUTE IMMEDIATE lv_sql_stmt
18053               USING     lv_batch_id,
18054                         v_instance_code;
18055 
18056 
18057 
18058       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18059                      (p_app_short_name    => 'MSC',
18060                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
18061                       p_message_text      => lv_message_text,
18062                       p_error_text        => lv_error_text,
18063                       p_token1            => 'COLUMN_NAME',
18064                       p_token_value1      => 'DELETED_FLAG',
18065                       p_token2            => 'DEFAULT_VALUE',
18066                       p_token_value2      => SYS_NO);
18067 
18068       IF lv_return <> 0 THEN
18069         RAISE ex_logging_err;
18070       END IF;
18071 
18072       lv_where_str :=
18073       ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
18074 
18075       --Log a warning for those records where the deleted_flag has a value other
18076       --SYS_NO
18077       lv_return := MSC_ST_UTIL.LOG_ERROR
18078                      (p_table_name        => 'MSC_ST_DESIGNATORS',
18079                       p_instance_code     => v_instance_code,
18080                       p_row               => lv_column_names,
18081                       p_severity          => G_SEV_WARNING,
18082                       p_message_text      => lv_message_text,
18083                       p_error_text        => lv_error_text,
18084                       p_batch_id          => lv_batch_id,
18085                       p_where_str         => lv_where_str,
18086                       p_col_name          => 'DELETED_FLAG',
18087                       p_debug             => v_debug,
18088                       p_default_value     => SYS_NO);
18089 
18090       IF lv_return <> 0 THEN
18091         RAISE ex_logging_err;
18092       END IF;
18093 
18094       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18095                      (p_app_short_name    => 'MSC',
18096                       p_error_code        => 'MSC_PP_INVALID_VALUE',
18097                       p_message_text      => lv_message_text,
18098                       p_error_text        => lv_error_text,
18099                       p_token1            => 'COLUMN_NAME',
18100                       p_token_value1      => 'ORGANIZATION_CODE');
18101 
18102       IF lv_return <> 0 THEN
18103         RAISE ex_logging_err;
18104       END IF;
18105 
18106       --Derive Organization_id
18107       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
18108                      (p_table_name       => 'MSC_ST_DESIGNATORS',
18109                       p_org_partner_name => 'ORGANIZATION_CODE',
18110                       p_org_partner_id   => 'ORGANIZATION_ID',
18111                       p_instance_code    => v_instance_code,
18112                       p_partner_type     => G_ORGANIZATION,
18113                       p_error_text       => lv_error_text,
18114                       p_batch_id         => lv_batch_id,
18115                       p_severity         => G_SEV_ERROR,
18116                       p_message_text     => lv_message_text,
18117                       p_debug            => v_debug,
18118                       p_row              => lv_column_names);
18119 
18120       IF lv_return <> 0 THEN
18121         RAISE ex_logging_err;
18122       END IF;
18123 
18124       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18125                      (p_app_short_name    => 'MSC',
18126                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
18127                       p_message_text      => lv_message_text,
18128                       p_error_text        => lv_error_text,
18129                       p_token1            => 'COLUMN_NAME',
18130                       p_token_value1      => 'DESIGNATOR');
18131 
18132       IF lv_return <> 0 THEN
18133         RAISE ex_logging_err;
18134       END IF;
18135 
18136       v_sql_stmt := 08;
18137       lv_sql_stmt :=
18138       'UPDATE   msc_st_designators '
18139       ||' SET   error_text   = '||''''||lv_message_text||''''||','
18140       ||'       process_flag = '||G_ERROR_FLG
18141       ||' WHERE NVL(designator,'||''''||NULL_CHAR||''''||')'
18142       ||'       = '||''''||NULL_CHAR||''''
18143       ||' AND   deleted_flag      = '||SYS_NO
18144       ||' AND   process_flag      = '||G_IN_PROCESS
18145       ||' AND   batch_id          = :lv_batch_id'
18146       ||' AND   sr_instance_code  = :v_instance_code';
18147 
18148 
18149         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18150 
18151       EXECUTE IMMEDIATE lv_sql_stmt
18152               USING     lv_batch_id,
18153                         v_instance_code;
18154 
18155     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18156                      (p_app_short_name    => 'MSC',
18157                       p_error_code        => 'MSC_PP_INVALID_VALUE',
18158                       p_message_text      => lv_message_text,
18159                       p_error_text        => lv_error_text,
18160                       p_token1            => 'COLUMN_NAME',
18161                       p_token_value1      => 'DEMAND_CLASS');
18162 
18163       IF lv_return <> 0 THEN
18164         RAISE ex_logging_err;
18165       END IF;
18166 
18167       -- Validate Demand Class, if value provided it should exists
18168       -- in ODS or staging table
18169      lv_return :=
18170      MSC_ST_UTIL.VALIDATE_DMD_CLASS(p_table_name       => 'MSC_ST_DESIGNATORS',
18171                                     p_dmd_class_column => 'DEMAND_CLASS',
18172                                     p_instance_id      => v_instance_id,
18173                                     p_instance_code    => v_instance_code,
18174                                     p_severity         => G_SEV3_ERROR,
18175                                     p_message_text     => lv_message_text,
18176                                     p_batch_id         => lv_batch_id,
18177                                     p_debug            => v_debug,
18178                                     p_error_text       => lv_error_text);
18179 
18180      if(lv_return <> 0) then
18181         RAISE ex_logging_err;
18182      end if;
18183 
18184 
18185       v_sql_stmt := 10;
18186       lv_sql_stmt :=
18187       'UPDATE msc_st_designators '
18188       ||' SET inventory_atp_flag = '||G_FOR_INV_ATP_FLAG
18189       --||'     mps_relief         = '||G_FOR_MPS_RELIEF   ||','
18190       --||'     consume_forecast   = '||G_CONSUME_FORECAST ||','
18191       --||'     update_type        = '||G_UPDATE_TYPE
18192       ||' WHERE nvl(inventory_atp_flag,'||NULL_VALUE||') NOT IN(1,2)'
18193       ||' AND   designator_type        ='||G_FORECAST_DESIGNATOR
18194       ||' AND   process_flag           = '||G_IN_PROCESS
18195       ||' AND   batch_id               = :lv_batch_id'
18196       ||' AND   sr_instance_code       = :v_instance_code';
18197 
18198 
18199         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18200 
18201       EXECUTE IMMEDIATE lv_sql_stmt
18202               USING     lv_batch_id,
18203                         v_instance_code;
18204 
18205       v_sql_stmt := 11;
18206       lv_sql_stmt :=
18207       'UPDATE   msc_st_designators '
18208       ||' SET   inventory_atp_flag = '||G_INV_ATP_FLAG
18209       ||' WHERE NVL(inventory_atp_flag,'||NULL_VALUE||') NOT IN(1,2)'
18210       ||' AND   NVL(designator_type,'||NULL_VALUE||')        IN(1,2)'
18211       ||' AND   process_flag           = '||G_IN_PROCESS
18212       ||' AND   batch_id               = :lv_batch_id'
18213       ||' AND   sr_instance_code       = :v_instance_code';
18214 
18215 
18216         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18217 
18218       EXECUTE IMMEDIATE lv_sql_stmt
18219               USING     lv_batch_id,
18220                         v_instance_code;
18221 
18222       v_sql_stmt := 12;
18223       lv_sql_stmt :=
18224       'UPDATE   msc_st_designators '
18225       ||' SET   mps_relief             = '||G_MPS_RELIEF
18226       ||' WHERE designator_type        IN(1,2)'
18227       ||' AND   process_flag           ='|| G_IN_PROCESS
18228       ||' AND   batch_id               = :lv_batch_id'
18229       ||' AND   sr_instance_code       = :v_instance_code';
18230 
18231 
18232         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18233 
18234       EXECUTE IMMEDIATE lv_sql_stmt
18235               USING     lv_batch_id,
18236                         v_instance_code;
18237 
18238       v_sql_stmt := 13;
18239       lv_sql_stmt :=
18240       ' UPDATE  msc_st_designators '
18241       ||' SET   bucket_type       = '||G_BUCKET_TYPE
18242       ||' WHERE NVL(bucket_type,'||NULL_VALUE||') NOT IN(1,2,3,'||NULL_VALUE||')'
18243       ||' AND   designator_type   = 6'
18244       ||' AND   process_flag      = '||G_IN_PROCESS
18245       ||' AND   batch_id          = :lv_batch_id'
18246       ||' AND   sr_instance_code  = :v_instance_code';
18247 
18248 
18249         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18250 
18251       EXECUTE IMMEDIATE lv_sql_stmt
18252               USING     lv_batch_id,
18253                         v_instance_code;
18254 
18255 
18256       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18257                      (p_app_short_name    => 'MSC',
18258                       p_error_code        => 'MSC_PP_INVALID_VALUE',
18259                       p_message_text      => lv_message_text,
18260                       p_error_text        => lv_error_text,
18261                       p_token1            => 'COLUMN_NAME',
18262                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME and CUSTOMER_NAME');
18263 
18264       IF lv_return <> 0 THEN
18265         RAISE ex_logging_err;
18266       END IF;
18267 
18268       --Derive Customer_id
18269       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
18270                      (p_table_name       => 'MSC_ST_DESIGNATORS',
18271                       p_org_partner_name => 'CUSTOMER_NAME',
18272                       p_org_partner_id   => 'CUSTOMER_ID',
18273                       p_instance_code    => v_instance_code,
18274                       p_partner_type     => G_CUSTOMER,
18275                       p_error_text       => lv_error_text,
18276                       p_batch_id         => lv_batch_id,
18277                       p_severity         => G_SEV_WARNING,
18278                       p_message_text     => lv_message_text,
18279                       p_debug            => v_debug,
18280                       p_row              => lv_column_names);
18281 
18282       IF lv_return <> 0 THEN
18283         RAISE ex_logging_err;
18284       END IF;
18285 
18286       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18287                      (p_app_short_name    => 'MSC',
18288                       p_error_code        => 'MSC_PP_INVALID_VALUE',
18289                       p_message_text      => lv_message_text,
18290                       p_error_text        => lv_error_text,
18291                       p_token1            => 'COLUMN_NAME',
18292                       p_token_value1      => 'SR_INSTANCE_CODE, COMPANY_NAME, CUSTOMER_NAME and BILL_TO_SITE_CODE');
18293 
18294       IF lv_return <> 0 THEN
18295         RAISE ex_logging_err;
18296       END IF;
18297 
18298       --Derive Bill_id
18299       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
18300                      (p_table_name        => 'MSC_ST_DESIGNATORS',
18301                       p_partner_name      => 'CUSTOMER_NAME',
18302                       p_partner_site_code => 'BILL_TO_SITE_CODE',
18303                       p_partner_site_id   => 'BILL_ID',
18304                       p_instance_code     => v_instance_code,
18305                       p_partner_type      => G_CUSTOMER,
18306                       p_error_text        => lv_error_text,
18307                       p_batch_id          => lv_batch_id,
18308                       p_severity          => G_SEV_WARNING,
18309                       p_message_text      => lv_message_text,
18310                       p_debug             => v_debug,
18311                       p_row               => lv_column_names);
18312 
18313       IF lv_return <> 0 THEN
18314         RAISE ex_logging_err;
18315       END IF;
18316 
18317       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18318                      (p_app_short_name    => 'MSC',
18319                       p_error_code        => 'MSC_PP_INVALID_VALUE',
18320                       p_message_text      => lv_message_text,
18321                       p_error_text        => lv_error_text,
18322                       p_token1            => 'COLUMN_NAME',
18323                       p_token_value1      => 'SR_INSTANCE_CODE, COMPANY_NAME, CUSTOMER_NAME and SHIP_TO_SITE_CODE');
18324 
18325       IF lv_return <> 0 THEN
18326         RAISE ex_logging_err;
18327       END IF;
18328 
18329       --Derive Ship_id
18330       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
18331                      (p_table_name        => 'MSC_ST_DESIGNATORS',
18332                       p_partner_name      => 'CUSTOMER_NAME',
18333                       p_partner_site_code => 'SHIP_TO_SITE_CODE',
18334                       p_partner_site_id   => 'SHIP_ID',
18335                       p_instance_code     => v_instance_code,
18336                       p_partner_type      => G_CUSTOMER,
18337                       p_error_text        => lv_error_text,
18338                       p_batch_id          => lv_batch_id,
18339                       p_severity          => G_SEV_WARNING,
18340                       p_message_text      => lv_message_text,
18341                       p_debug             => v_debug,
18342                       p_row               => lv_column_names);
18343 
18344       IF lv_return <> 0 THEN
18345         RAISE ex_logging_err;
18346       END IF;
18347 
18348       --Call to customised validation.
18349       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
18350         (ERRBUF         => lv_error_text,
18351          RETCODE        => lv_return,
18352          pBatchID       => lv_batch_id,
18353          pInstanceCode  => v_instance_code,
18354          pEntityName    => 'MSC_ST_DESIGNATORS',
18355          pInstanceID    => v_instance_id);
18356 
18357       IF NVL(lv_return,0) <> 0 THEN
18358         RAISE ex_logging_err;
18359       END IF;
18360 
18361 
18362      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18363                      (p_app_short_name    => 'MSC',
18364                       p_error_code        => 'MSC_PP_COL_VAL_EXISTS',
18365                       p_message_text      => lv_message_text,
18366                       p_error_text        => lv_error_text,
18367                       p_token1            => 'COLUMN_NAME1',
18368                       p_token_value1      => 'DESIGNATOR',
18369                       p_token2            => 'COLUMN_NAME2',
18370                       p_token_value2      => 'DESIGNATOR_TYPE',
18371                       p_token3            => 'ODS_TABLE_NAME',
18372                       p_token_value3      => 'MSC_DESIGNATORS');
18373 
18374       IF lv_return <> 0 THEN
18375         RAISE ex_logging_err;
18376       END IF;
18377 
18378 
18379       lv_sql_stmt :=
18380       'UPDATE   msc_st_designators msd'
18381       ||' SET   error_text   = '||''''||lv_message_text||''''||','
18382       ||'       process_flag = '||G_ERROR_FLG
18383       ||' WHERE EXISTS (SELECT 1 '
18384       ||' FROM  msc_designators md'
18385       ||' WHERE md.designator = msd.designator'
18386       ||' AND   md.sr_instance_id = :v_instance_id'
18387       ||' AND   md.organization_id= msd.organization_id'
18388       ||' AND   md.designator_type <> msd.designator_type)'
18389       ||' AND   msd.deleted_flag      = '||SYS_NO
18390       ||' AND   msd.process_flag      = '||G_IN_PROCESS
18391       ||' AND   msd.batch_id          = :lv_batch_id'
18392       ||' AND   msd.sr_instance_code  = :v_instance_code';
18393 
18394 
18395         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18396 
18397       EXECUTE IMMEDIATE lv_sql_stmt
18398               USING     v_instance_id,
18399                         lv_batch_id,
18400                         v_instance_code;
18401 
18402 
18403 
18404     -- For forecast set we check in ODS if it does not exist then
18405     -- Insert record in staging for forecast set
18406 
18407     v_sql_stmt := 14;
18408     lv_sql_stmt :=
18409     'insert into MSC_ST_DESIGNATORS'
18410     ||'  ( DESIGNATOR,'
18411     ||'    FORECAST_SET,'
18412     ||'    COMPANY_ID,'
18413     ||'    ORGANIZATION_ID,'
18414     ||'    MPS_RELIEF,'
18415     ||'    INVENTORY_ATP_FLAG,'
18416     ||'    DESCRIPTION,'
18417     ||'    DISABLE_DATE,'
18418     ||'    DEMAND_CLASS,'
18419     ||'    CONSUME_FORECAST,'
18420     ||'    UPDATE_TYPE,'
18421     ||'    FORWARD_UPDATE_TIME_FENCE,'
18422     ||'    BACKWARD_UPDATE_TIME_FENCE,'
18423     ||'    OUTLIER_UPDATE_PERCENTAGE,'
18424     ||'    CUSTOMER_ID,'
18425     ||'    SHIP_ID,'
18426     ||'    BILL_ID,'
18427     ||'    BUCKET_TYPE,'
18428     ||'    DESIGNATOR_TYPE,'
18429     ||'    DELETED_FLAG,'
18430     ||'    REFRESH_ID,'
18431     ||'    ST_TRANSACTION_ID,'
18432     ||'    BATCH_ID,'
18433     ||'    MESSAGE_ID,'
18434     ||'    DATA_SOURCE_TYPE,'
18435     ||'    PROCESS_FLAG,'
18436     ||'    SR_INSTANCE_CODE)'
18437     ||'  select'
18438     ||'  FORECAST_SET,'
18439     ||'  NULL,'
18440     ||'  COMPANY_ID,'
18441     ||'  ORGANIZATION_ID,'
18442     ||'  MPS_RELIEF,'
18443     ||'  INVENTORY_ATP_FLAG,'
18444     ||'  DESCRIPTION,'
18445     ||'  DISABLE_DATE,'
18446     ||'  DEMAND_CLASS,'
18447     ||'  CONSUME_FORECAST,'
18448     ||'  UPDATE_TYPE,'
18449     ||'  FORWARD_UPDATE_TIME_FENCE,'
18450     ||'  BACKWARD_UPDATE_TIME_FENCE,'
18451     ||'  OUTLIER_UPDATE_PERCENTAGE,'
18452     ||'  CUSTOMER_ID,'
18453     ||'  SHIP_ID,'
18454     ||'  BILL_ID,'
18455     ||'  BUCKET_TYPE,'
18456     ||'  DESIGNATOR_TYPE,'
18457     ||'  DELETED_FLAG,'
18458     ||   v_refresh_id ||' , '
18459     ||'  ST_TRANSACTION_ID,'
18460     ||'  BATCH_ID,'
18461     ||'  MESSAGE_ID,'
18462     ||'  DATA_SOURCE_TYPE,'
18463     ||   G_IN_PROCESS||','
18464     ||'  SR_INSTANCE_CODE'
18465     ||'  from MSC_ST_DESIGNATORS msd'
18466     ||'  WHERE NOT EXISTS ('
18467     ||'  SELECT 1 FROM MSC_DESIGNATORS md'
18468     ||'  WHERE md.designator = msd.forecast_set'
18469     ||'  AND NVL(md.forecast_set_id,'||NULL_VALUE||') = '||NULL_VALUE
18470     ||'  AND md.organization_id   = msd.organization_id'
18471     ||'  AND md.sr_instance_id    = :v_instance_id'
18472     ||'  UNION'
18473     ||'  SELECT 1'
18474     ||'  FROM   msc_st_designators msd1'
18475     ||'  WHERE  msd.forecast_set = msd1.designator'
18476     ||'  AND    msd.organization_code = msd1.organization_code'
18477     ||'  AND    msd1.sr_instance_code = :v_instance_code'
18478     ||'  AND    msd1.designator_type = 6'
18479     ||'  AND    NVL(msd1.forecast_set,'||''''||NULL_CHAR||''''||')'
18480     ||'        =               '||''''||NULL_CHAR||''''
18481     ||'  AND    msd1.process_flag IN ('||G_IN_PROCESS||','||G_VALID||')'
18482     ||'  AND    msd.rowid  <> msd1.rowid)'
18483     ||'  AND NVL(msd.forecast_set,'||''''||NULL_CHAR||''''||')'
18484     ||'        <>               '||''''||NULL_CHAR||''''
18485     ||'  AND msd.designator_type  = 6'
18486     ||'  AND msd.batch_id         = :lv_batch_id'
18487     ||'  AND msd.process_flag     ='||G_IN_PROCESS
18488     ||'  AND msd.sr_instance_code = :v_instance_code' ;
18489 
18490 
18491 
18492         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18493 
18494       EXECUTE IMMEDIATE lv_sql_stmt
18495               USING     v_instance_id,
18496                         v_instance_code,
18497                         lv_batch_id,
18498                         v_instance_code;
18499 
18500 
18501 
18502       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
18503                      (p_table_name     => 'MSC_ST_DESIGNATORS',
18504                       p_instance_id    => v_instance_id,
18505                       p_instance_code  => v_instance_code,
18506                       p_process_flag   => G_VALID,
18507                       p_error_text     => lv_error_text,
18508                       p_debug          => v_debug,
18509                       p_batch_id       => lv_batch_id);
18510 
18511       IF lv_return <> 0 THEN
18512         RAISE ex_logging_err;
18513       END IF;
18514 
18515       lv_return := MSC_ST_UTIL.LOG_ERROR
18516                      (p_table_name        => 'MSC_ST_DESIGNATORS',
18517                       p_instance_code     => v_instance_code,
18518                       p_row               => lv_column_names,
18519                       p_severity          => G_SEV_ERROR,
18520                       p_message_text      => NULL,
18521                       p_error_text        => lv_error_text,
18522                       p_debug             => v_debug,
18523                       p_batch_id          => lv_batch_id);
18524       IF lv_return <> 0 THEN
18525         RAISE ex_logging_err;
18526       END IF;
18527       COMMIT;
18528     END LOOP;
18529 
18530   EXCEPTION
18531     WHEN ex_logging_err THEN
18532         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
18533       ROLLBACK;
18534 
18535     WHEN OTHERS THEN
18536       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_DESIGNATOR'||'('
18537                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
18538         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
18539       ROLLBACK;
18540 
18541   END LOAD_DESIGNATOR;
18542 
18543   /*==========================================================================+
18544   | DESCRIPTION  : This function performs the validation and loads the data   |
18545   |                for business object item                                   |
18546   +==========================================================================*/
18547   PROCEDURE  LOAD_ITEMS(p_batch_id NUMBER)  IS
18548 
18549   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
18550   lb_rowid          RowidTab;
18551   lb_rowid_abc      RowidTab;
18552 
18553   lv_column_names       VARCHAR2(5000);     -- Stores cocatenated column names
18554   lv_return             NUMBER;
18555   lv_error_text         VARCHAR2(250);
18556   lv_where_str          VARCHAR2(5000);
18557   lv_sql_stmt           VARCHAR2(5000);
18558   lv_batch_id           msc_st_system_items.batch_id%TYPE;
18559   lv_message_text       msc_errors.error_text%TYPE;
18560 
18561   ex_logging_err    EXCEPTION;
18562   busy              EXCEPTION;
18563   PRAGMA EXCEPTION_INIT(busy, -54);
18564 
18565   CURSOR c1(p_batch_id NUMBER) IS
18566     SELECT rowid
18567     FROM   msc_st_system_items
18568     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
18569     AND    sr_instance_code = v_instance_code
18570     AND    batch_id         = p_batch_id;
18571 
18572   CURSOR c2(p_batch_id NUMBER) IS
18573     SELECT max(ROWID)
18574     FROM   msc_st_system_items
18575     WHERE  NVL(sr_inventory_item_id,NULL_VALUE) = NULL_VALUE
18576     AND    process_flag     = G_IN_PROCESS
18577     AND    sr_instance_code = v_instance_code
18578     AND    batch_id         = p_batch_id
18579     GROUP BY item_name, sr_instance_code,company_name;
18580 
18581 /**** Don't need this cursor anymore -- bug fix 2191021
18582   CURSOR c3(p_batch_id NUMBER) IS
18583     SELECT max(ROWID)
18584     FROM   msc_st_system_items
18585     WHERE  ABC_CLASS_ID IS NULL
18586     AND    ABC_CLASS_NAME IS NOT NULL
18587     AND    process_flag     = G_IN_PROCESS
18588     AND    sr_instance_code = v_instance_code
18589     AND    batch_id         = p_batch_id
18590     GROUP BY ABC_CLASS_NAME, organization_code, sr_instance_code, company_name;
18591 ******/
18592 
18593   CURSOR c_lock(p_batch_id NUMBER)  IS
18594     SELECT  'X'
18595     FROM    msc_st_system_items msi1,
18596             msc_st_system_items msi2
18597     WHERE   msi1.batch_id         =  p_batch_id
18598     AND     msi1.batch_id        <>  nvl(msi2.batch_id,NULL_VALUE)
18599     AND     msi1.sr_inventory_item_id IS NULL
18600     AND     msi2.sr_inventory_item_id IS NULL
18601     AND     msi1.sr_instance_code =  msi2.sr_instance_code
18602     AND     msi1.item_name        =  msi2.item_name
18603     AND     NVL(msi1.company_name,NULL_CHAR)= NVL(msi2.company_name,NULL_CHAR)
18604     FOR UPDATE NOWAIT   ;
18605 
18606 /**** Don't need this lock anymore -- bug fix 2191021
18607   CURSOR c_lock_abc(p_batch_id NUMBER)  IS
18608     SELECT  'X'
18609     FROM    msc_st_system_items msi1 ,
18610             msc_st_system_items msi2
18611     WHERE   msi1.batch_id                    =  p_batch_id
18612     AND     msi1.batch_id                   <>  nvl(msi2.batch_id,NULL_VALUE)
18613     AND     msi1.abc_class_id IS NULL
18614     AND     msi2.abc_class_id IS NULL
18615     AND     msi1.sr_instance_code            =  msi2.sr_instance_code
18616     AND     msi1.abc_class_name              =  msi2.abc_class_name
18617     AND     msi1.organization_code           =  msi2.organization_code
18618     AND     NVL(msi1.company_name,NULL_CHAR) = NVL(msi2.company_name,NULL_CHAR)
18619     FOR UPDATE NOWAIT   ;
18620 *******/
18621 
18622 
18623   BEGIN
18624 
18625     -- Validation of table msc_st_system_items
18626 
18627     lv_column_names :=
18628     'ORGANIZATION_CODE             ||''~''||'
18629     ||'ITEM_NAME                   ||''~''||'
18630     ||'LOT_CONTROL_CODE            ||''~''||'
18631     ||'ROUNDING_CONTROL_TYPE       ||''~''||'
18632     ||'IN_SOURCE_PLAN              ||''~''||'
18633     ||'MRP_PLANNING_CODE           ||''~''||'
18634     ||'FULL_LEAD_TIME              ||''~''||'
18635     ||'UOM_CODE                    ||''~''||'
18636     ||'MRP_CALCULATE_ATP_FLAG      ||''~''||'
18637     ||'ATP_COMPONENTS_FLAG         ||''~''||'
18638     ||'BUILT_IN_WIP_FLAG           ||''~''||'
18639     ||'PURCHASING_ENABLED_FLAG     ||''~''||'
18640     ||'PLANNING_MAKE_BUY_CODE      ||''~''||'
18641     ||'REPETITIVE_TYPE             ||''~''||'
18642     ||'ENGINEERING_ITEM_FLAG       ||''~''||'
18643     ||'MRP_SAFETY_STOCK_CODE       ||''~''||'
18644     ||'EFFECTIVITY_CONTROL         ||''~''||'
18645     ||'INVENTORY_PLANNING_CODE     ||''~''||'
18646     ||'ATP_FLAG                    ||''~''||'
18647     ||'SR_INSTANCE_CODE            ||''~''||'
18648     ||'DESCRIPTION                 ||''~''||'
18649     ||'LOTS_EXPIRATION             ||''~''||'
18650     ||'SHRINKAGE_RATE              ||''~''||'
18651     ||'FIXED_DAYS_SUPPLY           ||''~''||'
18652     ||'FIXED_ORDER_QUANTITY        ||''~''||'
18653     ||'FIXED_LOT_MULTIPLIER        ||''~''||'
18654     ||'MINIMUM_ORDER_QUANTITY      ||''~''||'
18655     ||'MAXIMUM_ORDER_QUANTITY      ||''~''||'
18656     ||'PLANNING_TIME_FENCE_DAYS    ||''~''||'
18657     ||'DEMAND_TIME_FENCE_DAYS      ||''~''||'
18658     ||'RELEASE_TIME_FENCE_CODE     ||''~''||'
18659     ||'RELEASE_TIME_FENCE_DAYS     ||''~''||'
18660     ||'REVISION                    ||''~''||'
18661     ||'CATEGORY_NAME               ||''~''||'
18662     ||'ABC_CLASS_NAME              ||''~''||'
18663     ||'FIXED_LEAD_TIME             ||''~''||'
18664     ||'VARIABLE_LEAD_TIME          ||''~''||'
18665     ||'PREPROCESSING_LEAD_TIME     ||''~''||'
18666     ||'POSTPROCESSING_LEAD_TIME    ||''~''||'
18667     ||'CUMULATIVE_TOTAL_LEAD_TIME  ||''~''||'
18668     ||'CUM_MANUFACTURING_LEAD_TIME ||''~''||'
18669     ||'UNIT_WEIGHT                 ||''~''||'
18670     ||'UNIT_VOLUME                 ||''~''||'
18671     ||'WEIGHT_UOM                  ||''~''||'
18672     ||'VOLUME_UOM                  ||''~''||'
18673     ||'PRODUCT_FAMILY_ID           ||''~''||'
18674     ||'ATP_RULE_CODE               ||''~''||'
18675     ||'STANDARD_COST               ||''~''||'
18676     ||'CARRYING_COST               ||''~''||'
18677     ||'ORDER_COST                  ||''~''||'
18678     ||'DMD_LATENESS_COST           ||''~''||'
18679     ||'SS_PENALTY_COST             ||''~''||'
18680     ||'SUPPLIER_CAP_OVERUTIL_COST  ||''~''||'
18681     ||'LIST_PRICE                  ||''~''||'
18682     ||'AVERAGE_DISCOUNT            ||''~''||'
18683     ||'END_ASSEMBLY_PEGGING_FLAG   ||''~''||'
18684     ||'END_ASSEMBLY_PEGGING        ||''~''||'
18685     ||'FULL_PEGGING                ||''~''||'
18686     ||'WIP_SUPPLY_TYPE             ||''~''||'
18687     ||'MRP_SAFETY_STOCK_PERCENT    ||''~''||'
18688     ||'SAFETY_STOCK_BUCKET_DAYS    ||''~''||'
18689     ||'INVENTORY_USE_UP_DATE       ||''~''||'
18690     ||'BUYER_NAME                  ||''~''||'
18691     ||'PLANNER_CODE                ||''~''||'
18692     ||'PLANNING_EXCEPTION_SET      ||''~''||'
18693     ||'EXCESS_QUANTITY             ||''~''||'
18694     ||'EXCEPTION_SHORTAGE_DAYS     ||''~''||'
18695     ||'EXCEPTION_EXCESS_DAYS       ||''~''||'
18696     ||'EXCEPTION_OVERPROMISED_DAYS ||''~''||'
18697     ||'REPETITIVE_VARIANCE_DAYS    ||''~''||'
18698     ||'BASE_ITEM_NAME              ||''~''||'
18699     ||'BOM_ITEM_TYPE               ||''~''||'
18700     ||'ATO_FORECAST_CONTROL        ||''~''||'
18701     ||'ACCEPTABLE_EARLY_DELIVERY   ||''~''||'
18702     ||'ACCEPTABLE_RATE_INCREASE    ||''~''||'
18703     ||'ACCEPTABLE_RATE_DECREASE    ||''~''||'
18704     ||'PRIMARY_SUPPLIER_NAME       ||''~''||'
18705     ||'INVENTORY_ITEM_FLAG         ||''~''||'
18706     ||'REVISION_QTY_CONTROL_CODE   ||''~''||'
18707     ||'EXPENSE_ACCOUNT             ||''~''||'
18708     ||'INVENTORY_ASSET_FLAG        ||''~''||'
18709     ||'MATERIAL_COST               ||''~''||'
18710     ||'RESOURCE_COST               ||''~''||'
18711     ||'SOURCE_ORG_CODE             ||''~''||'
18712     ||'PICK_COMPONENTS_FLAG        ||''~''||'
18713     ||'ALLOWED_UNITS_LOOKUP_CODE   ||''~''||'
18714     ||'SERVICE_LEVEL               ||''~''||'
18715     ||'REPLENISH_TO_ORDER_FLAG     ||''~''||'
18716     ||'COMPANY_NAME                ||''~''||'
18717     ||'SERIAL_NUMBER_CONTROL_CODE  ||''~''||'
18718     ||'REPAIR_LEAD_TIME            ||''~''||'
18719     ||'PREPOSITION_POINT           ||''~''||'
18720     ||'REPAIR_YIELD                ||''~''||'
18721     ||'REPAIR_PROGRAM              ||''~''||'
18722     ||'DELETED_FLAG';
18723 
18724 
18725 
18726 
18727     lv_batch_id := p_batch_id;
18728 
18729     LOOP
18730       BEGIN
18731           v_sql_stmt := 01;
18732           OPEN c_lock(p_batch_id) ;
18733           EXIT;
18734        EXCEPTION
18735         WHEN busy THEN
18736            lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_ITEM'||'(' ||v_sql_stmt||')'|| SQLERRM, 1, 240);
18737             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
18738             dbms_lock.sleep(5);
18739         WHEN OTHERS THEN
18740          lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_ITEM'||'(' ||v_sql_stmt||')'|| SQLERRM, 1, 240);
18741         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
18742 
18743        v_sql_stmt := 02;
18744        lv_sql_stmt :=
18745        'UPDATE msc_st_system_items '
18746        ||' SET process_flag = 1, '
18747        ||' batch_id = NULL'
18748        ||' WHERE batch_id        = :p_batch_id'
18749        ||' AND process_flag      = '||G_IN_PROCESS
18750        ||' AND sr_instance_code  = :v_instance_code';
18751 
18752 
18753          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18754 
18755       EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
18756       COMMIT;
18757       RETURN;
18758       END ;
18759     END LOOP ;
18760 
18761 /************ No need to acquire this lock anymore -- bug fix for 2191021
18762     LOOP
18763       BEGIN
18764           v_sql_stmt := 01;
18765           OPEN c_lock_abc(p_batch_id) ;
18766           EXIT;
18767       EXCEPTION
18768         WHEN busy THEN
18769            lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_ITEM'||'(' ||v_sql_stmt||')'|| SQLERRM, 1, 240);
18770             msc_st_util.log_message(lv_error_text);
18771             dbms_lock.sleep(5);
18772         WHEN OTHERS THEN
18773           lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_ITEM'||'(' ||v_sql_stmt||')'|| SQLERRM, 1, 240);
18774           msc_st_util.log_message(lv_error_text);
18775 
18776           v_sql_stmt := 03;
18777           lv_sql_stmt :=
18778           'UPDATE msc_st_system_items '
18779           ||' SET process_flag = 1, '
18780           ||' batch_id = NULL'
18781           ||' WHERE batch_id        = :p_batch_id'
18782           ||' AND process_flag      = '||G_IN_PROCESS
18783           ||' AND sr_instance_code  = :v_instance_code';
18784 
18785 
18786             msc_st_util.log_message(lv_sql_stmt);
18787 
18788           EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
18789           COMMIT;
18790           RETURN;
18791       END ;
18792     END LOOP ;
18793 ******/
18794 
18795     -- Update transaction id and WHO columns
18796 
18797     OPEN c1(lv_batch_id);
18798 
18799     v_sql_stmt := 01;
18800 
18801     FETCH c1 BULK COLLECT INTO lb_rowid;
18802 
18803        v_sql_stmt := 04;
18804 
18805     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
18806       UPDATE   msc_st_system_items
18807       SET      st_transaction_id = msc_st_system_items_s.NEXTVAL,
18808                refresh_id        = v_refresh_id,
18809                last_update_date  = v_current_date,
18810                last_updated_by   = v_current_user,
18811                creation_date     = v_current_date,
18812                created_by        = v_current_user
18813       WHERE    rowid             = lb_rowid(j);
18814 
18815 
18816     -- Set the error message
18817 
18818     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18819                      (p_app_short_name    => 'MSC',
18820                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
18821                       p_message_text      => lv_message_text,
18822                       p_error_text        => lv_error_text,
18823                       p_token1            => 'COLUMN_NAME',
18824                       p_token_value1      => 'ITEM_NAME');
18825 
18826       IF lv_return <> 0 THEN
18827         RAISE ex_logging_err;
18828       END IF;
18829 
18830     -- Error out records where item_name  is NULL
18831 
18832      v_sql_stmt := 06;
18833 
18834      lv_sql_stmt :=
18835       ' UPDATE  msc_st_system_items '
18836       ||' SET   process_flag        = '||G_ERROR_FLG||','
18837       ||'       error_text          = '||''''||lv_message_text||''''
18838       ||' WHERE NVL(item_name,'||''''||NULL_CHAR||''''||')'
18839       ||'                   = '||''''||NULL_CHAR||''''
18840       ||' AND   batch_id         = :lv_batch_id'
18841       ||' AND   process_flag     = '||G_IN_PROCESS
18842       ||' AND   sr_instance_code = :v_instance_code';
18843 
18844 
18845         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18846 
18847 
18848       EXECUTE IMMEDIATE lv_sql_stmt
18849               USING     lv_batch_id,
18850                         v_instance_code;
18851 
18852     -- Set the error message
18853 
18854     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18855                      (p_app_short_name    => 'MSC',
18856                       p_error_code        => 'MSC_PP_NO_DELETION',
18857                       p_message_text      => lv_message_text,
18858                       p_error_text        => lv_error_text,
18859                       p_token1            => 'TABLE_NAME',
18860                       p_token_value1      => 'MSC_ST_SYSTEM_ITEMS');
18861 
18862     IF lv_return <> 0 THEN
18863         RAISE ex_logging_err;
18864     END IF;
18865 
18866      --Deletion is not allowed on this table.
18867 
18868      v_sql_stmt := 07;
18869      lv_sql_stmt :=
18870       ' UPDATE   msc_st_system_items '
18871       ||' SET      process_flag     = '||G_ERROR_FLG||','
18872       ||'          error_text       = '||''''||lv_message_text||''''
18873       ||' WHERE    deleted_flag     = '||SYS_YES
18874       ||' AND      process_flag     = '||G_IN_PROCESS
18875       ||' AND      batch_id         = :lv_batch_id'
18876       ||' AND      sr_instance_code = :v_instance_code';
18877 
18878 
18879          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18880 
18881     EXECUTE IMMEDIATE   lv_sql_stmt
18882             USING       lv_batch_id,
18883                         v_instance_code;
18884 
18885     -- Set the error message , PIP FIX
18886 
18887      lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
18888                       (p_app_short_name    => 'MSC',
18889                        p_error_code        => 'MSC_PP_INVALID_VALUE',
18890                        p_message_text      => lv_message_text,
18891                        p_error_text        => lv_error_text,
18892                        p_token1            => 'COLUMN_NAME',
18893                        p_token_value1      => 'PIP_FLAG');
18894 
18895       IF lv_return <> 0 THEN
18896         RAISE ex_logging_err;
18897       END IF;
18898 
18899      --Valid values for PIP_flag are 1 and 2.
18900 
18901      v_sql_stmt := 07;
18902      lv_sql_stmt :=
18903       ' UPDATE   msc_st_system_items '
18904       ||' SET      process_flag     = '||G_ERROR_FLG||','
18905       ||'          error_text       = '||''''||lv_message_text||''''
18906       ||' WHERE    nvl(pip_flag,1)  not in(1,2)'
18907       ||' AND      process_flag     = '||G_IN_PROCESS
18908       ||' AND      batch_id         = :lv_batch_id'
18909       ||' AND      sr_instance_code = :v_instance_code';
18910 
18911 
18912          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18913 
18914     EXECUTE IMMEDIATE   lv_sql_stmt
18915             USING       lv_batch_id,
18916                         v_instance_code;
18917 
18918     -- create supply flag validation
18919     -- Set the error message
18920 
18921     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
18922                      (p_app_short_name    => 'MSC',
18923                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
18924                       p_message_text      => lv_message_text,
18925                       p_error_text        => lv_error_text,
18926                       p_token1            => 'COLUMN_NAME',
18927                       p_token_value1      => 'CREATE_SUPPLY_FLAG');
18928 
18929       IF lv_return <> 0 THEN
18930         RAISE ex_logging_err;
18931       END IF;
18932 
18933     -- Valid value for CREATE_SUPPLY_FLAG are (1,2)
18934 
18935     v_sql_stmt := 97;
18936     lv_sql_stmt :=
18937         ' UPDATE   MSC_ST_SYSTEM_ITEMS '
18938       ||' SET      process_flag     = '||G_ERROR_FLG||','
18939       ||'          error_text       = '||''''||lv_message_text||''''
18940       ||' WHERE    nvl(CREATE_SUPPLY_FLAG,1)  not in(1,2)'
18941       ||' AND      process_flag     = '||G_IN_PROCESS
18942       ||' AND      batch_id         = :lv_batch_id'
18943       ||' AND      sr_instance_code = :v_instance_code';
18944 
18945 
18946          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
18947 
18948     EXECUTE IMMEDIATE   lv_sql_stmt
18949             USING       lv_batch_id,
18950                         v_instance_code;
18951 
18952       -- Set the message
18953 
18954      lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
18955                       (p_app_short_name    => 'MSC',
18956                        p_error_code        => 'MSC_PP_INVALID_VALUE',
18957                        p_message_text      => lv_message_text,
18958                        p_error_text        => lv_error_text,
18959                        p_token1            => 'COLUMN_NAME',
18960                        p_token_value1      => 'ORGANIZATION_CODE');
18961 
18962       IF lv_return <> 0 THEN
18963         RAISE ex_logging_err;
18964       END IF;
18965 
18966 
18967     -- Derive organization id
18968     lv_return :=  MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
18969                      (p_table_name       => 'MSC_ST_SYSTEM_ITEMS',
18970                       p_org_partner_name  => 'ORGANIZATION_CODE',
18971                       p_org_partner_id    => 'ORGANIZATION_ID',
18972                       p_instance_code     => v_instance_code,
18973                       p_partner_type      => G_ORGANIZATION,
18974                       p_severity          => G_SEV_ERROR,
18975                       p_error_text        => lv_error_text,
18976                       p_batch_id          => lv_batch_id,
18977                       p_debug             => v_debug,
18978                       p_message_text      => lv_message_text);
18979 
18980     IF lv_return <> 0 THEN
18981         RAISE ex_logging_err;
18982     END IF;
18983 
18984    --Update sr_inventory_item_id if already exists
18985     v_sql_stmt := 08;
18986     lv_sql_stmt :=
18987     'UPDATE msc_st_system_items msi'
18988     ||' SET sr_inventory_item_id = (SELECT local_id'
18989     ||'     FROM   msc_local_id_item mli'
18990     ||'     WHERE  mli.char1         = msi.sr_instance_code '
18991     ||'     AND    NVL(mli.char2,       '||''''||NULL_CHAR||''''||')='
18992     ||'            NVL(msi.company_name,'||''''||NULL_CHAR||''''||')'
18993     ||'     AND    mli.char4         = msi.item_name'
18994     ||'     AND    mli.entity_name   = ''SR_INVENTORY_ITEM_ID'' '
18995     ||'     AND    mli.instance_id   = :v_instance_id'
18996     ||'     AND    rownum            < 2)'
18997     ||' WHERE  sr_instance_code      = :v_instance_code'
18998     ||' AND    batch_id              = :lv_batch_id'
18999     ||' AND    process_flag          = '||G_IN_PROCESS;
19000 
19001 
19002          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19003 
19004     EXECUTE IMMEDIATE lv_sql_stmt
19005             USING      v_instance_id,
19006                        v_instance_code,
19007                       lv_batch_id;
19008 
19009 ---- Changes for SRP legacy collections -----
19010 
19011    --	Set the error message
19012 	lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19013                      (p_app_short_name    => 'MSC',
19014                       p_error_code        => 'MSC_PP_INVALID_VALUE',
19015                       p_message_text      => lv_message_text,
19016                       p_error_text        => lv_error_text,
19017                       p_token1            => 'COLUMN_NAME',
19018                       p_token_value1      => 'PREPOSITION_POINT');
19019 
19020 
19021       IF lv_return <> 0 THEN
19022         RAISE ex_logging_err;
19023       END IF;
19024 
19025      --Valid values for Preposition_point are Y and N
19026 
19027      v_sql_stmt := 101;
19028      lv_sql_stmt :=
19029       ' UPDATE   msc_st_system_items '
19030       ||' SET      process_flag     = '||G_ERROR_FLG||','
19031       ||'          error_text       = '||''''||lv_message_text||''''
19032       ||' WHERE    preposition_point  not in(''Y'',''N'')'
19033       ||' AND      process_flag     = '||G_IN_PROCESS
19034       ||' AND      batch_id         = :lv_batch_id'
19035       ||' AND      sr_instance_code = :v_instance_code';
19036 
19037 
19038          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19039 
19040 
19041     EXECUTE IMMEDIATE   lv_sql_stmt
19042             USING       lv_batch_id,
19043                         v_instance_code;
19044 
19045   --	Set the error message
19046 	lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19047                      (p_app_short_name    => 'MSC',
19048                       p_error_code        => 'MSC_PP_INVALID_VALUE',
19049                       p_message_text      => lv_message_text,
19050                       p_error_text        => lv_error_text,
19051                       p_token1            => 'COLUMN_NAME',
19052                       p_token_value1      => 'REPAIR_PROGRAM');
19053 
19054 
19055 
19056       IF lv_return <> 0 THEN
19057         RAISE ex_logging_err;
19058       END IF;
19059 
19060      --Valid values for repair_program are (1,2,3))
19061 
19062      v_sql_stmt := 102;
19063      lv_sql_stmt :=
19064       ' UPDATE   MSC_ST_SYSTEM_ITEMS '
19065         ||' SET      process_flag     = '||G_ERROR_FLG||','
19066     	  ||'          error_text       = '||''''||lv_message_text||''''
19067     	  ||' WHERE    REPAIR_PROGRAM  not in(1,2,3)'
19068      	  ||' AND      process_flag     = '||G_IN_PROCESS
19069     	  ||' AND      batch_id         = :lv_batch_id'
19070         ||' AND      sr_instance_code = :v_instance_code';
19071 
19072 
19073 
19074          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19075 
19076     EXECUTE IMMEDIATE   lv_sql_stmt
19077             USING       lv_batch_id,
19078                         v_instance_code;
19079 
19080 
19081   IF (v_install_msc) THEN
19082 
19083     -- Now check for UOM code if it is Invalid error out
19084     -- Set the error message
19085 
19086     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19087                      (p_app_short_name    => 'MSC',
19088                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
19089                       p_message_text      => lv_message_text,
19090                       p_error_text        => lv_error_text,
19091                       p_token1            => 'COLUMN_NAMES',
19092                       p_token_value1      => 'SR_INSTANCE_CODE ,UOM_CODE',
19093                       p_token2            => 'MASTER_TABLE',
19094                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
19095                       p_token3            => 'CHILD_TABLE',
19096                       p_token_value3      => 'MSC_ST_SYSTEM_ITEMS');
19097     IF lv_return <> 0 THEN
19098         RAISE ex_logging_err;
19099     END IF;
19100 
19101     v_sql_stmt := 09;
19102 
19103     lv_sql_stmt :=
19104     '   UPDATE      msc_st_system_items mssi'
19105     ||' SET         process_flag = '||G_ERROR_FLG||','
19106     ||'             error_text   = '||''''||lv_message_text||''''
19107     ||' WHERE       NOT EXISTS (SELECT 1 '
19108     ||'             FROM msc_units_of_measure muom'
19109     ||'             WHERE muom.uom_code       = mssi.uom_code'
19110     ||'             UNION'
19111     ||'             SELECT 1 FROM msc_st_units_of_measure msuom'
19112     ||'             WHERE msuom.uom_code       = mssi.uom_code'
19113     ||'             AND   msuom.sr_instance_id = :v_instance_id'
19114     ||'             AND   msuom.process_flag   = '||G_VALID||')'
19115     ||' AND mssi.sr_instance_code   = :v_instance_code'
19116     ||' AND mssi.batch_id           = :lv_batch_id'
19117     ||' AND mssi.process_flag       = '||G_IN_PROCESS ;
19118 
19119 
19120           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19121 
19122     EXECUTE IMMEDIATE lv_sql_stmt
19123             USING     v_instance_id,
19124                       v_instance_code,
19125                       lv_batch_id;
19126 
19127     -- Set the message
19128 
19129     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19130                      (p_app_short_name    => 'MSC',
19131                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
19132                       p_message_text      => lv_message_text,
19133                       p_error_text        => lv_error_text,
19134                       p_token1            => 'COLUMN_NAMES',
19135                       p_token_value1      => 'SR_INSTANCE_CODE OR VOLUME_UOM',
19136                       p_token2            => 'MASTER_TABLE',
19137                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
19138                       p_token3            => 'CHILD_TABLE',
19139                       p_token_value3      => 'MSC_ST_SYSTEM_ITEMS');
19140     IF lv_return <> 0 THEN
19141       RAISE ex_logging_err;
19142     END IF;
19143 
19144     -- Validate volume uom if provided
19145     v_sql_stmt := 10;
19146 
19147     lv_sql_stmt :=
19148     '   UPDATE     msc_st_system_items mssi'
19149     ||' SET        process_flag = '||G_ERROR_FLG||','
19150     ||'            error_text   = '||''''||lv_message_text||''''
19151     ||' WHERE      NOT EXISTS( SELECT 1 '
19152     ||'            FROM    msc_units_of_measure muom'
19153     ||'            WHERE   muom.uom_code       = mssi.volume_uom'
19154     ||'            UNION'
19155     ||'            SELECT 1 '
19156     ||'            FROM   msc_st_units_of_measure msuom'
19157     ||'            WHERE  msuom.uom_code       = mssi.volume_uom'
19158     ||'            AND    msuom.sr_instance_id = :v_instance_id'
19159     ||'            AND    msuom.process_flag   = '||G_VALID||')'
19160     ||' AND  NVL(mssi.volume_uom,'||''''||NULL_CHAR||''''||') '
19161     ||'                       <> '||''''||NULL_CHAR||''''
19162     ||' AND mssi.sr_instance_code   = :v_instance_code'
19163     ||' AND mssi.batch_id           = :lv_batch_id'
19164     ||' AND mssi.process_flag       = '||G_IN_PROCESS ;
19165 
19166 
19167           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19168 
19169 
19170     EXECUTE IMMEDIATE lv_sql_stmt
19171             USING     v_instance_id,
19172                       v_instance_code,
19173                       lv_batch_id;
19174 
19175     -- Set the message
19176 
19177     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19178                      (p_app_short_name    => 'MSC',
19179                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
19180                       p_message_text      => lv_message_text,
19181                       p_error_text        => lv_error_text,
19182                       p_token1            => 'COLUMN_NAMES',
19183                       p_token_value1      => 'SR_INSTANCE_CODE OR WEIGHT_UOM',
19184                       p_token2            => 'MASTER_TABLE',
19185                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
19186                       p_token3            => 'CHILD_TABLE',
19187                       p_token_value3      => 'MSC_ST_SYSTEM_ITEMS');
19188       IF lv_return <> 0 THEN
19189         RAISE ex_logging_err;
19190       END IF;
19191 
19192     -- Validate weight uom if provided
19193 
19194     v_sql_stmt := 12;
19195 
19196     lv_sql_stmt :=
19197     '   UPDATE     msc_st_system_items mssi'
19198     ||' SET        process_flag = '||G_ERROR_FLG||','
19199     ||'            error_text   = '||''''||lv_message_text||''''
19200     ||' WHERE      NOT EXISTS( SELECT 1 '
19201     ||'            FROM   msc_units_of_measure muom'
19202     ||'            WHERE  muom.uom_code       = mssi.weight_uom'
19203     ||'            UNION'
19204     ||'            SELECT 1 FROM msc_st_units_of_measure msuom'
19205     ||'            WHERE  msuom.uom_code       = mssi.weight_uom'
19206     ||'            AND    msuom.sr_instance_id = :v_instance_id'
19207     ||'            AND    msuom.process_flag   = '||G_VALID||')'
19208     ||' AND NVL(mssi.weight_uom,'||''''||NULL_CHAR||''''||') '
19209     ||'     <> '||''''||NULL_CHAR||''''
19210     ||' AND mssi.sr_instance_code   = :v_instance_code'
19211     ||' AND mssi.batch_id           = :lv_batch_id'
19212     ||' AND mssi.process_flag       = '||G_IN_PROCESS ;
19213 
19214 
19215           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19216 
19217       EXECUTE IMMEDIATE lv_sql_stmt
19218               USING     v_instance_id,
19219                         v_instance_code,
19220                         lv_batch_id;
19221 
19222     -- Set the error message
19223     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19224                      (p_app_short_name    => 'MSC',
19225                       p_error_code        => 'MSC_PP_INVALID_VALUE',
19226                       p_message_text      => lv_message_text,
19227                       p_error_text        => lv_error_text,
19228                       p_token1            => 'COLUMN_NAME',
19229                       p_token_value1      => 'SOURCE_ORG_CODE');
19230 
19231 
19232     IF lv_return <> 0 THEN
19233             RAISE ex_logging_err;
19234     END IF;
19235 
19236     -- Update Source org id
19237 
19238     lv_return :=
19239      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_SYSTEM_ITEMS',
19240                                     p_org_partner_name  =>'SOURCE_ORG_CODE',
19241                                     p_org_partner_id    =>'SOURCE_ORG_ID',
19242                                     p_instance_code     => v_instance_code,
19243                                     p_partner_type      => G_ORGANIZATION,
19244                                     p_severity          => G_SEV3_ERROR,
19245                                     p_message_text      => lv_message_text,
19246                                     p_batch_id          =>lv_batch_id,
19247                                     p_debug             => v_debug,
19248                                     p_error_text        =>lv_error_text);
19249     IF lv_return <> 0 THEN
19250             RAISE ex_logging_err;
19251     END IF;
19252 
19253 
19254     -- Set the error message
19255     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19256                      (p_app_short_name    => 'MSC',
19257                       p_error_code        => 'MSC_PP_INVALID_VALUE',
19258                       p_message_text      => lv_message_text,
19259                       p_error_text        => lv_error_text,
19260                       p_token1            => 'COLUMN_NAME',
19261                       p_token_value1      => 'PRIMARY_SUPPLIER_NAME');
19262 
19263 
19264     IF lv_return <> 0 THEN
19265             RAISE ex_logging_err;
19266     END IF;
19267 
19268     -- Update primary_supplier_name
19269 
19270     lv_return :=
19271      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_SYSTEM_ITEMS',
19272                                     p_org_partner_name  =>'PRIMARY_SUPPLIER_NAME',
19273                                     p_org_partner_id    =>'PRIMARY_SUPPLIER_ID',
19274                                     p_instance_code     => v_instance_code,
19275                                     p_partner_type      => G_VENDOR,
19276                                     p_severity          => G_SEV3_ERROR,
19277                                     p_message_text      => lv_message_text,
19278                                     p_batch_id          =>lv_batch_id,
19279                                     p_debug             => v_debug,
19280                                     p_error_text        =>lv_error_text);
19281     IF lv_return <> 0 THEN
19282             RAISE ex_logging_err;
19283     END IF;
19284 
19285       -- Set the message
19286 
19287      lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
19288                       (p_app_short_name    => 'MSC',
19289                        p_error_code        => 'MSC_PP_INVALID_VALUE',
19290                        p_message_text      => lv_message_text,
19291                        p_error_text        => lv_error_text,
19292                        p_token1            => 'COLUMN_NAME',
19293                        p_token_value1      => 'ATP_FLAG and MRP_PLANNING_CODE'
19294                                                ||' and ATP_COMPONENTS_FLAG');
19295 
19296       IF lv_return <> 0 THEN
19297         RAISE ex_logging_err;
19298       END IF;
19299 
19300     -- ERP collection does not handle
19301     -- Error out the record where atp_flag or atp_components_flag is N and
19302     -- MRP_PLANNING_CODE is 6 (not planned)
19303 
19304      v_sql_stmt := 13;
19305 
19306      lv_sql_stmt :=
19307       ' UPDATE  msc_st_system_items '
19308       ||' SET   process_flag        = '||G_ERROR_FLG||','
19309       ||'       error_text          = '||''''||lv_message_text||''''
19310       ||' WHERE mrp_planning_code = 6 '
19311       ||' AND   atp_flag = ''N'' '
19312       ||' AND   atp_components_flag = ''N'' '
19313       ||' AND   pick_components_flag <> ''Y'' '
19314       ||' AND   bom_item_type        <> 3 '
19315       ||' AND   batch_id         = :lv_batch_id'
19316       ||' AND   process_flag     = '||G_IN_PROCESS
19317       ||' AND   sr_instance_code = :v_instance_code';
19318 
19319 
19320         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19321 
19322       EXECUTE IMMEDIATE lv_sql_stmt
19323               USING     lv_batch_id,
19324                         v_instance_code;
19325 
19326 
19327     END IF ; --v_install_msc
19328 
19329     --Call to customised validation.
19330 
19331       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
19332         (ERRBUF         => lv_error_text,
19333          RETCODE        => lv_return,
19334          pBatchID       => lv_batch_id,
19335          pInstanceCode  => v_instance_code,
19336          pEntityName    => 'MSC_ST_SYSTEM_ITEMS',
19337          pInstanceID    => v_instance_id);
19338 
19339       IF NVL(lv_return,0) <> 0 THEN
19340         RAISE ex_logging_err;
19341       END IF;
19342 /**** Transfering this piece of code to the Monitor -- bug fix 2191021
19343   IF(v_install_msc) THEN
19344     -- update ABC_CLASS_ID where it exists in the planning system
19345      v_sql_stmt := 14;
19346      lv_sql_stmt :=
19347       ' UPDATE  msc_st_system_items msi'
19348       ||' SET   abc_class_id        = (SELECT local_id from MSC_LOCAL_ID_MISC mac '
19349       ||'                              WHERE  mac.char1 = msi.sr_instance_code'
19350       ||'                              AND    mac.char3  = msi.organization_code  '
19351       ||'                              AND    mac.char4 = msi.abc_class_name '
19352       ||'                              AND    mac.ENTITY_NAME = ''ABC_CLASS_ID'''
19353       ||'                              AND    NVL(mac.char2,'||''''||NULL_CHAR||''''||') '
19354       ||'                                     =    NVL(msi.company_name,'||''''||NULL_CHAR||''''||')) '
19355       ||' WHERE batch_id         = :lv_batch_id'
19356       ||' AND   process_flag     = '||G_IN_PROCESS
19357       ||' AND   sr_instance_code = :v_instance_code'
19358       ||' AND   abc_class_name is not null ';
19359 
19360 
19361         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19362 
19363       EXECUTE IMMEDIATE lv_sql_stmt
19364               USING     lv_batch_id,
19365                         v_instance_code;
19366 
19367      OPEN c3(lv_batch_id);
19368      FETCH c3 BULK COLLECT INTO lb_rowid_abc ;
19369 
19370      IF c3%ROWCOUNT > 0  THEN
19371 
19372         FORALL j IN lb_rowid_abc.FIRST..lb_rowid_abc.LAST
19373         UPDATE msc_st_system_items msi
19374         SET    abc_class_id   = MSC_ST_ABC_CLASS_ID_S.nextval
19375         WHERE  rowid   = lb_rowid_abc(j);
19376 
19377 
19378        FORALL j IN lb_rowid_abc.FIRST..lb_rowid_abc.LAST
19379        INSERT INTO  msc_local_id_misc
19380        (local_id,
19381        st_transaction_id,
19382        instance_id,
19383        entity_name,
19384        data_source_type,
19385        char1,
19386        char2,
19387        char3,
19388        char4,
19389        last_update_date,
19390        last_updated_by,
19391        creation_date,
19392        created_by  )
19393        SELECT
19394        abc_class_id,
19395        st_transaction_id,
19396        v_instance_id,
19397        'ABC_CLASS_ID',
19398        data_source_type,
19399        sr_instance_code,
19400        company_name,
19401        organization_code,
19402        ABC_CLASS_NAME,
19403        v_current_date,
19404        v_current_user,
19405        v_current_date,
19406        v_current_user
19407       FROM msc_st_system_items
19408       WHERE  rowid            = lb_rowid_abc(j);
19409 
19410      END IF;
19411    CLOSE c3;
19412 
19413      -- update ABC_CLASS_ID for ABC_CLASS_NAME for which we have just populated the local id
19414      v_sql_stmt := 15;
19415      lv_sql_stmt :=
19416       ' UPDATE  msc_st_system_items msi'
19417       ||' SET   abc_class_id        = (SELECT local_id from MSC_LOCAL_ID_MISC mac '
19418       ||'                              WHERE  mac.char1 = msi.sr_instance_code'
19419       ||'                              AND    mac.char3  = msi.organization_code  '
19420       ||'                              AND    mac.char4 = msi.abc_class_name '
19421       ||'                              AND    mac.ENTITY_NAME = ''ABC_CLASS_ID'''
19422       ||'                              AND    NVL(mac.char2,'||''''||NULL_CHAR||''''||') '
19423       ||'                                     =    NVL(msi.company_name,'||''''||NULL_CHAR||''''||')) '
19424       ||' WHERE batch_id         = :lv_batch_id'
19425       ||' AND   process_flag     = '||G_IN_PROCESS
19426       ||' AND   sr_instance_code = :v_instance_code'
19427       ||' AND   abc_class_id  IS NULL'
19428       ||' AND   abc_class_name is not null ';
19429 
19430 
19431         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19432 
19433       EXECUTE IMMEDIATE lv_sql_stmt
19434               USING     lv_batch_id,
19435                         v_instance_code;
19436 
19437      v_sql_stmt := 16;
19438      lv_sql_stmt :=
19439        ' INSERT INTO MSC_ABC_CLASSES '
19440        ||' ( ABC_CLASS_ID, '
19441        ||' ABC_CLASS_NAME, '
19442        ||' ORGANIZATION_ID, '
19443        ||' SR_INSTANCE_ID, '
19444        ||' LAST_UPDATE_DATE, '
19445        ||' LAST_UPDATED_BY, '
19446        ||' CREATION_DATE, '
19447        ||' CREATED_BY) '
19448        ||' SELECT distinct '
19449        ||' msi.abc_class_id,'
19450        ||' msi.abc_class_name,'
19451        ||' msi.organization_id,'
19452        ||  v_instance_id ||','
19453        ||' last_update_date,'
19454        ||' last_updated_by,'
19455        ||' creation_date,'
19456        ||' created_by '
19457        ||' FROM   MSC_ST_SYSTEM_ITEMS msi'
19458        ||' WHERE  batch_id         = :lv_batch_id'
19459        ||' AND    sr_instance_code = :v_instance_code'
19460        ||' AND    process_flag    = '||G_IN_PROCESS
19461        ||' AND    msi.abc_class_id is not null'
19462        ||' AND    msi.abc_class_name is not null'
19463        ||' AND    NOT EXISTS'
19464        ||'        ( SELECT 1 FROM MSC_ABC_CLASSES abc'
19465        ||'        WHERE  abc.abc_class_id = msi.abc_class_id'
19466        ||'        AND    abc.abc_class_name = msi.abc_class_name'
19467        ||'        AND    abc.organization_id = msi.organization_id'
19468        ||'        AND    abc.sr_instance_id = ' ||v_instance_id ||')';
19469 
19470       IF V_DEBUG THEN
19471         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19472       END IF;
19473 
19474       EXECUTE IMMEDIATE lv_sql_stmt
19475               USING     lv_batch_id,
19476                         v_instance_code;
19477   END IF; --v_install_msc
19478 
19479 *******************/
19480      OPEN c2(lv_batch_id);
19481      FETCH c2 BULK COLLECT INTO lb_rowid ;
19482 
19483        IF c2%ROWCOUNT > 0  THEN
19484 
19485     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
19486 
19487         UPDATE msc_st_system_items
19488         SET    sr_inventory_item_id   = msc_st_sr_inventory_item_id_s.NEXTVAL
19489         WHERE  rowid                  = lb_rowid(j);
19490 
19491     v_sql_stmt := 10;
19492 
19493      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
19494    INSERT INTO  msc_local_id_item
19495        (local_id,
19496        st_transaction_id,
19497        instance_id,
19498        entity_name,
19499        data_source_type,
19500        char1,
19501        char2,
19502        char3,
19503        char4,
19504        SOURCE_SR_INVENTORY_ITEM_ID,
19505        SOURCE_SOURCE_ORG_ID,
19506        SOURCE_PRIMARY_SUPPLIER_ID,
19507        SOURCE_BASE_ITEM_ID,
19508        last_update_date,
19509        last_updated_by,
19510        creation_date,
19511        created_by  )
19512        SELECT
19513        sr_inventory_item_id,
19514        st_transaction_id,
19515        v_instance_id,
19516        'SR_INVENTORY_ITEM_ID',
19517        data_source_type,
19518        sr_instance_code,
19519        company_name,
19520        organization_code,
19521        item_name,
19522        SOURCE_SR_INVENTORY_ITEM_ID,
19523        SOURCE_SOURCE_ORG_ID,
19524        SOURCE_PRIMARY_SUPPLIER_ID,
19525        SOURCE_BASE_ITEM_ID,
19526        v_current_date,
19527        v_current_user,
19528        v_current_date,
19529        v_current_user
19530       FROM msc_st_system_items
19531       WHERE  rowid            = lb_rowid(j);
19532 
19533    END IF;
19534    CLOSE c2;
19535 
19536     v_sql_stmt := 17;
19537     lv_sql_stmt :=
19538     'UPDATE msc_st_system_items msi'
19539     ||' SET sr_inventory_item_id = (SELECT local_id'
19540     ||'     FROM   msc_local_id_item mli'
19541     ||'     WHERE  mli.char1         = msi.sr_instance_code '
19542     ||'     AND    NVL(mli.char2,       '||''''||NULL_CHAR||''''||')='
19543     ||'            NVL(msi.company_name,'||''''||NULL_CHAR||''''||')'
19544     ||'     AND    mli.char4         = msi.item_name'
19545     ||'     AND    mli.entity_name   = ''SR_INVENTORY_ITEM_ID'' '
19546     ||'     AND    mli.instance_id   = :v_instance_id'
19547     ||'     AND    rownum            < 2 )'
19548     ||' WHERE  sr_inventory_item_id IS NULL'
19549     ||' AND    sr_instance_code      = :v_instance_code'
19550     ||' AND    batch_id              = :lv_batch_id'
19551     ||' AND    process_flag          = '||G_IN_PROCESS;
19552 
19553 
19554          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19555 
19556     EXECUTE IMMEDIATE lv_sql_stmt
19557             USING      v_instance_id,
19558                        v_instance_code,
19559                       lv_batch_id;
19560 
19561     v_sql_stmt := 18;
19562     lv_sql_stmt :=
19563     'INSERT INTO  msc_local_id_item '
19564     ||' (local_id,'
19565     ||' st_transaction_id,'
19566     ||' instance_id,'
19567     ||' entity_name,'
19568     ||' data_source_type,'
19569     ||' char1,'
19570     ||' char2,'
19571     ||' char3,'
19572     ||' char4,'
19573     ||' SOURCE_SR_INVENTORY_ITEM_ID,'
19574     ||' SOURCE_SOURCE_ORG_ID,'
19575     ||' SOURCE_PRIMARY_SUPPLIER_ID,'
19576     ||' SOURCE_BASE_ITEM_ID,'
19577     ||' last_update_date,'
19578     ||' last_updated_by,'
19579     ||' creation_date,'
19580     ||' created_by  )'
19581     ||' SELECT '
19582     ||' sr_inventory_item_id,'
19583     ||' st_transaction_id,'
19584     ||  v_instance_id||','
19585     ||' ''SR_INVENTORY_ITEM_ID'','
19586     ||' data_source_type,'
19587     ||' sr_instance_code,'
19588     ||' company_name,'
19589     ||' organization_code,'
19590     ||' item_name,'
19591     ||' SOURCE_SR_INVENTORY_ITEM_ID,'
19592     ||' SOURCE_SOURCE_ORG_ID,'
19593     ||' SOURCE_PRIMARY_SUPPLIER_ID,'
19594     ||' SOURCE_BASE_ITEM_ID,'
19595     ||' last_update_date,'
19596     ||' last_updated_by,'
19597     ||' creation_date,'
19598     ||' created_by '
19599     ||' FROM  msc_st_system_items'
19600     ||' WHERE NOT EXISTS( SELECT 1'
19601     ||'       FROM   msc_local_id_item'
19602     ||'       WHERE  char1         = sr_instance_code '
19603     ||'       AND    NVL(char2,       '||''''||NULL_CHAR||''''||')='
19604     ||'              NVL(company_name,'||''''||NULL_CHAR||''''||')'
19605     ||'       AND    char3          = organization_code'
19606     ||'       AND    char4          = item_name'
19607     ||'       AND    entity_name    = ''SR_INVENTORY_ITEM_ID'' )'
19608     ||' AND   sr_instance_code      = :v_instance_code'
19609     ||' AND   batch_id              = :lv_batch_id'
19610     ||' AND   process_flag          = '||G_IN_PROCESS;
19611 
19612 
19613          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
19614 
19615     EXECUTE IMMEDIATE lv_sql_stmt
19616             USING     v_instance_code,
19617                       lv_batch_id;
19618 
19619    IF(v_install_msc) THEN
19620       -- Set the message
19621 
19622    /*  lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19623                      (p_app_short_name    => 'MSC',
19624                       p_error_code        => 'MSC_PP_INVALID_VALUE',
19625                       p_message_text      => lv_message_text,
19626                       p_error_text        => lv_error_text,
19627                       p_token1            => 'COLUMN_NAME',
19628                       p_token_value1      => 'BASE_ITEM_NAME');
19629 
19630       IF lv_return <> 0 THEN
19631         RAISE ex_logging_err;
19632       END IF;
19633 
19634       -- Poplulate base item id
19635      lv_return :=
19636      MSC_ST_UTIL.DERIVE_ITEM_ID(p_table_name => 'MSC_ST_SYSTEM_ITEMS',
19637                              p_item_col_name => 'BASE_ITEM_NAME',
19638                              p_item_col_id   =>'BASE_ITEM_ID',
19639                              p_instance_code => v_instance_code,
19640                              p_instance_id   => v_instance_id,
19641                              p_severity      => G_SEV3_ERROR,
19642                              p_message_text  => lv_message_text,
19643                              p_batch_id      =>lv_batch_id,
19644                              p_debug          => v_debug,
19645                              p_error_text    =>lv_error_text);
19646 
19647 
19648     if(lv_return <> 0) then
19649          RAISE ex_logging_err;
19650     end if; */
19651 
19652    COMMIT;
19653 
19654     -- Set the message
19655 
19656     lv_return :=    MSC_ST_UTIL.GET_ERROR_MESSAGE
19657                      (p_app_short_name    => 'MSC',
19658                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
19659                       p_message_text      => lv_message_text,
19660                       p_error_text        => lv_error_text,
19661                       p_token1            => 'COLUMN_NAME',
19662                       p_token_value1      => 'DELETED_FLAG',
19663                       p_token2            => 'DEFAULT_VALUE',
19664                       p_token_value2      =>  SYS_NO);
19665 
19666       IF lv_return <> 0 THEN
19667         RAISE ex_logging_err;
19668       END IF;
19669 
19670     -- Check DELETE_FLAG column has valid value or not and
19671     -- if not set it to SYS_NO
19672 
19673     lv_where_str :=
19674     ' AND NVL(deleted_flag,'||NULL_VALUE||') <>'||SYS_NO;
19675 
19676     lv_return := MSC_ST_UTIL.LOG_ERROR
19677                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
19678                      p_instance_code     => v_instance_code,
19679                      p_row               => lv_column_names,
19680                      p_severity          => G_SEV_WARNING,
19681                      p_message_text      => lv_message_text,
19682                      p_error_text        => lv_error_text,
19683                      p_batch_id          => lv_batch_id,
19684                      p_where_str         => lv_where_str,
19685                      p_col_name          => 'DELETED_FLAG',
19686                      p_debug             => v_debug,
19687                      p_default_value     => SYS_NO);
19688 
19689     IF lv_return <> 0 THEN
19690         RAISE ex_logging_err;
19691     END IF;
19692 
19693 
19694 
19695     -- Set the error message
19696     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19697                      (p_app_short_name    => 'MSC',
19698                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
19699                       p_message_text      => lv_message_text,
19700                       p_error_text        => lv_error_text,
19701                       p_token1            => 'COLUMN_NAME',
19702                       p_token_value1      => 'LOT_CONTROL_CODE',
19703                       p_token2            => 'DEFAULT_VALUE',
19704                       p_token_value2      =>  G_LOT_CONTROL_CODE  );
19705 
19706     IF lv_return <> 0 THEN
19707         RAISE ex_logging_err;
19708     END IF;
19709 
19710     -- Valid value for Lot control code is 1,2  default it to 2
19711 
19712     lv_where_str :=
19713     '   AND   NVL(lot_control_code,'||NULL_VALUE||')'
19714     ||' NOT IN (1,2)';
19715 
19716     lv_return := MSC_ST_UTIL.LOG_ERROR
19717                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
19718                      p_instance_code     => v_instance_code,
19719                      p_row               => lv_column_names,
19720                      p_severity          => G_SEV_WARNING,
19721                      p_message_text      => lv_message_text,
19722                      p_error_text        => lv_error_text,
19723                      p_batch_id          => lv_batch_id,
19724                      p_where_str         => lv_where_str,
19725                      p_col_name          => 'LOT_CONTROL_CODE',
19726                      p_debug             => v_debug,
19727                      p_default_value     => G_LOT_CONTROL_CODE);
19728 
19729     IF lv_return <> 0 THEN
19730          RAISE ex_logging_err;
19731     END IF;
19732 
19733     -- Set the error message
19734     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19735                      (p_app_short_name    => 'MSC',
19736                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
19737                       p_message_text      => lv_message_text,
19738                       p_error_text        => lv_error_text,
19739                       p_token1            => 'COLUMN_NAME',
19740                       p_token_value1      => 'ROUNDING_CONTROL_TYPE',
19741                       p_token2            => 'DEFAULT_VALUE',
19742                       p_token_value2      => G_ROUNDING_CONTROL_TYPE );
19743 
19744       IF lv_return <> 0 THEN
19745         RAISE ex_logging_err;
19746       END IF;
19747 
19748     -- Valid value for rounding_control_type  is 1,2  default it to 2
19749 
19750     lv_where_str :=
19751     '   AND NVL(rounding_control_type,'||NULL_VALUE||')'
19752     ||' NOT IN (1,2)';
19753 
19754     lv_return := MSC_ST_UTIL.LOG_ERROR
19755                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
19756                      p_instance_code     => v_instance_code,
19757                      p_row               => lv_column_names,
19758                      p_severity          => G_SEV_WARNING,
19759                      p_message_text      => lv_message_text,
19760                      p_error_text        => lv_error_text,
19761                      p_batch_id          => lv_batch_id,
19762                      p_where_str         => lv_where_str,
19763                      p_col_name          => 'ROUNDING_CONTROL_TYPE',
19764                      p_debug             => v_debug,
19765                      p_default_value     => G_ROUNDING_CONTROL_TYPE);
19766 
19767      IF lv_return <> 0 THEN
19768         RAISE ex_logging_err;
19769     END IF;
19770 
19771     -- Set the error message
19772     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19773                      (p_app_short_name    => 'MSC',
19774                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
19775                       p_message_text      => lv_message_text,
19776                       p_error_text        => lv_error_text,
19777                       p_token1            => 'COLUMN_NAME',
19778                       p_token_value1      => 'IN_SOURCE_PLAN',
19779                       p_token2            => 'DEFAULT_VALUE',
19780                       p_token_value2      => G_IN_SOURCE_PLAN );
19781 
19782       IF lv_return <> 0 THEN
19783         RAISE ex_logging_err;
19784       END IF;
19785 
19786     -- Valid value for in_source_plan  is 1,2  default it to 2
19787 
19788     lv_where_str :=
19789     '   AND NVL(in_source_plan,'||NULL_VALUE||')'
19790     ||' NOT IN (1,2)';
19791 
19792     lv_return := MSC_ST_UTIL.LOG_ERROR
19793                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
19794                      p_instance_code     => v_instance_code,
19795                      p_row               => lv_column_names,
19796                      p_severity          => G_SEV_WARNING,
19797                      p_message_text      => lv_message_text,
19798                      p_error_text        => lv_error_text,
19799                      p_batch_id          => lv_batch_id,
19800                      p_where_str         => lv_where_str,
19801                      p_col_name          => 'IN_SOURCE_PLAN',
19802                      p_debug             => v_debug,
19803                      p_default_value     => G_IN_SOURCE_PLAN);
19804 
19805      IF lv_return <> 0 THEN
19806         RAISE ex_logging_err;
19807     END IF;
19808 
19809     -- Set the error message
19810     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19811                      (p_app_short_name    => 'MSC',
19812                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
19813                       p_message_text      => lv_message_text,
19814                       p_error_text        => lv_error_text,
19815                       p_token1            => 'COLUMN_NAME',
19816                       p_token_value1      => 'MRP_PLANNING_CODE',
19817                       p_token2            => 'DEFAULT_VALUE',
19818                       p_token_value2      => G_MRP_PLANNING_CODE );
19819 
19820       IF lv_return <> 0 THEN
19821         RAISE ex_logging_err;
19822       END IF;
19823 
19824     -- Valid value for mrp_planning_code are (3,4,6,7,8,9),default it to 7
19825 
19826     lv_where_str :=
19827     '   AND NVL(mrp_planning_code,'||NULL_VALUE||')'
19828     ||' NOT IN (3,4,6,7,8,9)';
19829 
19830 
19831     lv_return := MSC_ST_UTIL.LOG_ERROR
19832                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
19833                      p_instance_code     => v_instance_code,
19834                      p_row               => lv_column_names,
19835                      p_severity          => G_SEV_WARNING,
19836                      p_message_text      => lv_message_text,
19837                      p_error_text        => lv_error_text,
19838                      p_batch_id          => lv_batch_id,
19839                      p_where_str         => lv_where_str,
19840                      p_col_name          => 'mrp_planning_code',
19841                      p_debug             => v_debug,
19842                      p_default_value     => G_MRP_PLANNING_CODE);
19843 
19844     IF lv_return <> 0 THEN
19845          RAISE ex_logging_err;
19846     END IF;
19847 
19848     -- Set the error message
19849     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19850                      (p_app_short_name    => 'MSC',
19851                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
19852                       p_message_text      => lv_message_text,
19853                       p_error_text        => lv_error_text,
19854                       p_token1            => 'COLUMN_NAME',
19855                       p_token_value1      => 'MRP_CALCULATE_ATP_FLAG',
19856                       p_token2            => 'DEFAULT_VALUE',
19857                       p_token_value2      => G_MRP_CALCULATE_ATP_FLAG );
19858 
19859       IF lv_return <> 0 THEN
19860         RAISE ex_logging_err;
19861       END IF;
19862 
19863    -- Valid value for mrp_calculate_atp_flag are (1,2),default it to 2
19864 
19865     lv_where_str :=
19866     '   AND NVL(mrp_calculate_atp_flag,'||NULL_VALUE||')'
19867     ||' NOT IN (1,2)';
19868 
19869     lv_return := MSC_ST_UTIL.LOG_ERROR
19870                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
19871                      p_instance_code     => v_instance_code,
19872                      p_row               => lv_column_names,
19873                      p_severity          => G_SEV_WARNING,
19874                      p_message_text      => lv_message_text,
19875                      p_error_text        => lv_error_text,
19876                      p_batch_id          => lv_batch_id,
19877                      p_where_str         => lv_where_str,
19878                      p_col_name          => 'MRP_CALCULATE_ATP_FLAG',
19879                      p_debug             => v_debug,
19880                      p_default_value     => G_MRP_CALCULATE_ATP_FLAG);
19881 
19882     IF lv_return <> 0 THEN
19883         RAISE ex_logging_err;
19884     END IF;
19885 
19886     -- Set the error message
19887     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19888                      (p_app_short_name    => 'MSC',
19889                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
19890                       p_message_text      => lv_message_text,
19891                       p_error_text        => lv_error_text,
19892                       p_token1            => 'COLUMN_NAME',
19893                       p_token_value1      => 'ATP_COMPONENTS_FLAG',
19894                       p_token2            => 'DEFAULT_VALUE',
19895                       p_token_value2      =>  G_ATP_COMPONENTS_FLAG );
19896 
19897       IF lv_return <> 0 THEN
19898         RAISE ex_logging_err;
19899       END IF;
19900 
19901     -- Valid value for atp_components_flag are (Y,N),default it to 2
19902 
19903     lv_where_str :=
19904     '    AND NVL(atp_components_flag  ,'||''''||NULL_CHAR||''''||')'
19905     ||'  NOT IN (''Y'', ''N'')';
19906 
19907     lv_return := MSC_ST_UTIL.LOG_ERROR
19908                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
19909                      p_instance_code     => v_instance_code,
19910                      p_row               => lv_column_names,
19911                      p_severity          => G_SEV_WARNING,
19912                      p_message_text      => lv_message_text,
19913                      p_error_text        => lv_error_text,
19914                      p_batch_id          => lv_batch_id,
19915                      p_where_str         => lv_where_str,
19916                      p_col_name          => 'ATP_COMPONENTS_FLAG',
19917                      p_debug             => v_debug,
19918                      p_default_value     => '''N''');
19919 
19920     IF lv_return <> 0 THEN
19921         RAISE ex_logging_err;
19922     END IF;
19923 
19924     -- Set the error message
19925     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19926                      (p_app_short_name    => 'MSC',
19927                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
19928                       p_message_text      => lv_message_text,
19929                       p_error_text        => lv_error_text,
19930                       p_token1            => 'COLUMN_NAME',
19931                       p_token_value1      => 'BUILT_IN_WIP_FLAG',
19932                       p_token2           => 'DEFAULT_VALUE',
19933                       p_token_value2     => G_BUILT_IN_WIP_FLAG );
19934 
19935       IF lv_return <> 0 THEN
19936         RAISE ex_logging_err;
19937       END IF;
19938 
19939     -- Valid value for built_in_wip_flag are (1,2),default it to 1
19940 
19941     lv_where_str :=
19942     ' AND NVL(built_in_wip_flag ,'||NULL_VALUE||') NOT IN (1,2)';
19943 
19944     lv_return := MSC_ST_UTIL.LOG_ERROR
19945                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
19946                      p_instance_code     => v_instance_code,
19947                      p_row               => lv_column_names,
19948                      p_severity          => G_SEV_WARNING,
19949                      p_message_text      => lv_message_text,
19950                      p_error_text        => lv_error_text,
19951                      p_batch_id          => lv_batch_id,
19952                      p_where_str         => lv_where_str,
19953                      p_col_name          => 'BUILT_IN_WIP_FLAG',
19954                      p_debug             => v_debug,
19955                      p_default_value     => G_BUILT_IN_WIP_FLAG);
19956 
19957      IF lv_return <> 0 THEN
19958         RAISE ex_logging_err;
19959      END IF;
19960 
19961     -- Set the error message
19962     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
19963                      (p_app_short_name    => 'MSC',
19964                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
19965                       p_message_text      => lv_message_text,
19966                       p_error_text        => lv_error_text,
19967                       p_token1            => 'COLUMN_NAME',
19968                       p_token_value1      => 'PURCHASING_ENABLED_FLAG',
19969                       p_token2           => 'DEFAULT_VALUE',
19970                       p_token_value2     => G_PURCHASING_ENABLED_FLAG );
19971 
19972       IF lv_return <> 0 THEN
19973         RAISE ex_logging_err;
19974       END IF;
19975 
19976     -- Valid value for purchasing_enabled_flag are (1,2),default it to 2
19977 
19978     lv_where_str :=
19979     ' AND NVL(purchasing_enabled_flag,'||NULL_VALUE||') NOT IN (1,2) ';
19980 
19981     lv_return := MSC_ST_UTIL.LOG_ERROR
19982                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
19983                      p_instance_code     => v_instance_code,
19984                      p_row               => lv_column_names,
19985                      p_severity          => G_SEV_WARNING,
19986                      p_message_text      => lv_message_text,
19987                      p_error_text        => lv_error_text,
19988                      p_batch_id          => lv_batch_id,
19989                      p_where_str         => lv_where_str,
19990                      p_col_name          => 'PURCHASING_ENABLED_FLAG',
19991                      p_debug             => v_debug,
19992                      p_default_value     => G_PURCHASING_ENABLED_FLAG);
19993 
19994     IF lv_return <> 0 THEN
19995             RAISE ex_logging_err;
19996     END IF;
19997 
19998     -- Set the error message
19999     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20000                      (p_app_short_name    => 'MSC',
20001                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20002                       p_message_text      => lv_message_text,
20003                       p_error_text        => lv_error_text,
20004                       p_token1            => 'COLUMN_NAME',
20005                       p_token_value1      => 'PLANNING_MAKE_BUY_CODE',
20006                       p_token2           => 'DEFAULT_VALUE',
20007                       p_token_value2     => G_PLANNING_MAKE_BUY_CODE );
20008 
20009       IF lv_return <> 0 THEN
20010         RAISE ex_logging_err;
20011       END IF;
20012 
20013 
20014   -- Valid value for planning_make_buy_code are (1,2),default it to 2
20015 
20016     lv_where_str :=
20017     ' AND NVL(planning_make_buy_code,'||NULL_VALUE||') NOT IN (1,2)';
20018 
20019     lv_return := MSC_ST_UTIL.LOG_ERROR
20020                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20021                      p_instance_code     => v_instance_code,
20022                      p_row               => lv_column_names,
20023                      p_severity          => G_SEV_WARNING,
20024                      p_message_text      => lv_message_text,
20025                      p_error_text        => lv_error_text,
20026                      p_batch_id          => lv_batch_id,
20027                      p_where_str         => lv_where_str,
20028                      p_col_name          => 'PLANNING_MAKE_BUY_CODE',
20029                      p_debug             => v_debug,
20030                      p_default_value     => G_PLANNING_MAKE_BUY_CODE );
20031 
20032     IF lv_return <> 0 THEN
20033             RAISE ex_logging_err;
20034     END IF;
20035 
20036     -- Set the error message
20037     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20038                      (p_app_short_name    => 'MSC',
20039                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20040                       p_message_text      => lv_message_text,
20041                       p_error_text        => lv_error_text,
20042                       p_token1            => 'COLUMN_NAME',
20043                       p_token_value1      => 'INVENTORY_PLANNING_CODE',
20044                       p_token2           => 'DEFAULT_VALUE',
20045                       p_token_value2     => G_INVENTORY_PLANNING_CODE );
20046 
20047     IF lv_return <> 0 THEN
20048         RAISE ex_logging_err;
20049     END IF;
20050 
20051     -- Valid value for inventory_planning_code are (1,2,6),default it to 6
20052 
20053     lv_where_str :=
20054     ' AND NVL(inventory_planning_code,'||NULL_VALUE||') NOT IN (1,2,6)';
20055 
20056     lv_return := MSC_ST_UTIL.LOG_ERROR
20057                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20058                      p_instance_code     => v_instance_code,
20059                      p_row               => lv_column_names,
20060                      p_severity          => G_SEV_WARNING,
20061                      p_message_text      => lv_message_text,
20062                      p_error_text        => lv_error_text,
20063                      p_batch_id          => lv_batch_id,
20064                      p_where_str         => lv_where_str,
20065                      p_col_name          => 'INVENTORY_PLANNING_CODE',
20066                      p_debug             => v_debug,
20067                      p_default_value     => G_INVENTORY_PLANNING_CODE);
20068 
20069     IF lv_return <> 0 THEN
20070             RAISE ex_logging_err;
20071     END IF;
20072 
20073     -- Set the error message
20074     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20075                      (p_app_short_name    => 'MSC',
20076                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20077                       p_message_text      => lv_message_text,
20078                       p_error_text        => lv_error_text,
20079                       p_token1            => 'COLUMN_NAME',
20080                       p_token_value1      => 'REPETITIVE_TYPE',
20081                       p_token2           => 'DEFAULT_VALUE',
20082                       p_token_value2     => G_REPETITIVE_TYPE);
20083 
20084       IF lv_return <> 0 THEN
20085         RAISE ex_logging_err;
20086       END IF;
20087 
20088   -- Repetiitve type always default to 1
20089 
20090    lv_where_str :=
20091    ' AND NVL(repetitive_type,'||NULL_VALUE||') NOT IN (1,2)';
20092 
20093     lv_return := MSC_ST_UTIL.LOG_ERROR
20094                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20095                      p_instance_code     => v_instance_code,
20096                      p_row               => lv_column_names,
20097                      p_severity          => G_SEV_WARNING,
20098                      p_message_text      => lv_message_text,
20099                      p_error_text        => lv_error_text,
20100                      p_batch_id          => lv_batch_id,
20101                      p_where_str         => lv_where_str,
20102                      p_col_name          => 'repetitive_type',
20103                      p_debug             => v_debug,
20104                      p_default_value     => G_REPETITIVE_TYPE);
20105 
20106     IF lv_return <> 0 THEN
20107             RAISE ex_logging_err;
20108     END IF;
20109 
20110     -- Set the error message
20111     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20112                      (p_app_short_name    => 'MSC',
20113                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20114                       p_message_text      => lv_message_text,
20115                       p_error_text        => lv_error_text,
20116                       p_token1            => 'COLUMN_NAME',
20117                       p_token_value1      => 'ENGINEERING_ITEM_FLAG',
20118                       p_token2           => 'DEFAULT_VALUE',
20119                       p_token_value2     => G_ENGINEERING_ITEM_FLAG);
20120 
20121       IF lv_return <> 0 THEN
20122         RAISE ex_logging_err;
20123       END IF;
20124 
20125     -- Valid value for engineering_item_flag are (1,2),default it to 2
20126     lv_where_str :=
20127     ' AND NVL(engineering_item_flag ,'||NULL_VALUE||') NOT IN (1,2)';
20128 
20129     lv_return := MSC_ST_UTIL.LOG_ERROR
20130                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20131                      p_instance_code     => v_instance_code,
20132                      p_row               => lv_column_names,
20133                      p_severity          => G_SEV_WARNING,
20134                      p_message_text      => lv_message_text,
20135                      p_error_text        => lv_error_text,
20136                      p_batch_id          => lv_batch_id,
20137                      p_where_str         => lv_where_str,
20138                      p_col_name          => 'ENGINEERING_ITEM_FLAG' ,
20139                      p_debug             => v_debug,
20140                      p_default_value     => G_ENGINEERING_ITEM_FLAG);
20141 
20142     IF lv_return <> 0 THEN
20143             RAISE ex_logging_err;
20144     END IF;
20145 
20146     -- Set the error message
20147     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20148                      (p_app_short_name    => 'MSC',
20149                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20150                       p_message_text      => lv_message_text,
20151                       p_error_text        => lv_error_text,
20152                       p_token1            => 'COLUMN_NAME',
20153                       p_token_value1      => 'MRP_SAFETY_STOCK_CODE',
20154                       p_token2           => 'DEFAULT_VALUE',
20155                       p_token_value2     => 1);
20156 
20157     IF lv_return <> 0 THEN
20158         RAISE ex_logging_err;
20159     END IF;
20160 
20161     --   Valid value for mrp_safety_stock_code are (1,2),default it based on
20162     --   whether mrp_safety_stock_percent is NULL or Not
20163 
20164     lv_where_str :=
20165     '   AND NVL(mrp_safety_stock_code,'||NULL_VALUE||') <>'||' 1 AND'
20166     ||' NVL(mrp_safety_stock_percent,'||NULL_VALUE||')='||NULL_VALUE;
20167 
20168     lv_return := MSC_ST_UTIL.LOG_ERROR
20169                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20170                      p_instance_code     => v_instance_code,
20171                      p_row               => lv_column_names,
20172                      p_severity          => G_SEV_WARNING,
20173                      p_message_text      => lv_message_text,
20174                      p_error_text        => lv_error_text,
20175                      p_batch_id          => lv_batch_id,
20176                      p_where_str         => lv_where_str,
20177                      p_col_name          => 'mrp_safety_stock_code' ,
20178                      p_debug             => v_debug,
20179                      p_default_value     => 1);
20180 
20181     IF lv_return <> 0 THEN
20182             RAISE ex_logging_err;
20183     END IF;
20184 
20185     -- Set the error message
20186     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20187                      (p_app_short_name    => 'MSC',
20188                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20189                       p_message_text      => lv_message_text,
20190                       p_error_text        => lv_error_text,
20191                       p_token1            => 'COLUMN_NAME',
20192                       p_token_value1      => 'MRP_SAFETY_STOCK_CODE',
20193                       p_token2           => 'DEFAULT_VALUE',
20194                       p_token_value2     => 2);
20195 
20196     IF lv_return <> 0 THEN
20197         RAISE ex_logging_err;
20198     END IF;
20199 
20200     --   Valid value for mrp_safety_stock_code are (1,2),default it based on
20201     --   whether mrp_safety_stock_percent is NULL or Not
20202 
20203     lv_where_str :=
20204     '   AND NVL(mrp_safety_stock_code,'||NULL_VALUE||') <>'||' 2 AND'
20205     ||' NVL(mrp_safety_stock_percent,'||NULL_VALUE||')<>'||NULL_VALUE;
20206 
20207     lv_return := MSC_ST_UTIL.LOG_ERROR
20208                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20209                      p_instance_code     => v_instance_code,
20210                      p_row               => lv_column_names,
20211                      p_severity          => G_SEV_WARNING,
20212                      p_message_text      => lv_message_text,
20213                      p_error_text        => lv_error_text,
20214                      p_batch_id          => lv_batch_id,
20215                      p_where_str         => lv_where_str,
20216                      p_col_name          => 'mrp_safety_stock_code' ,
20217                      p_debug             => v_debug,
20218                      p_default_value     => 1);
20219 
20220     IF lv_return <> 0 THEN
20221             RAISE ex_logging_err;
20222     END IF;
20223 
20224     -- Set the error message
20225     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20226                      (p_app_short_name    => 'MSC',
20227                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20228                       p_message_text      => lv_message_text,
20229                       p_error_text        => lv_error_text,
20230                       p_token1            => 'COLUMN_NAME',
20231                       p_token_value1      => 'ATP_FLAG',
20232                       p_token2           => 'DEFAULT_VALUE',
20233                       p_token_value2     =>  G_ATP_FLAG);
20234 
20235     IF lv_return <> 0 THEN
20236         RAISE ex_logging_err;
20237     END IF;
20238 
20239     -- Valid value for atp_flag are (Y,N,C),default it to N
20240 
20241     lv_where_str :=
20242     ' AND NVL(atp_flag  ,'||''''||NULL_CHAR||''''||')'
20243     ||'  NOT IN (''Y'', ''N'', ''C'')';
20244 
20245     lv_return := MSC_ST_UTIL.LOG_ERROR
20246                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20247                      p_instance_code     => v_instance_code,
20248                      p_row               => lv_column_names,
20249                      p_severity          => G_SEV_WARNING,
20250                      p_message_text      => lv_message_text,
20251                      p_error_text        => lv_error_text,
20252                      p_batch_id          => lv_batch_id,
20253                      p_where_str         => lv_where_str,
20254                      p_col_name          => 'ATP_FLAG' ,
20255                      p_debug             => v_debug,
20256                      p_default_value     => '''N''');
20257 
20258     IF lv_return <> 0 THEN
20259             RAISE ex_logging_err;
20260     END IF;
20261 
20262     -- Set the error message
20263     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20264                      (p_app_short_name    => 'MSC',
20265                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20266                       p_message_text      => lv_message_text,
20267                       p_error_text        => lv_error_text,
20268                       p_token1            => 'COLUMN_NAME',
20269                       p_token_value1      => 'INVENTORY_ASSET_FLAG',
20270                       p_token2           => 'DEFAULT_VALUE',
20271                       p_token_value2     =>  G_INVENTORY_ASSET_FLAG);
20272 
20273      IF lv_return <> 0 THEN
20274         RAISE ex_logging_err;
20275      END IF;
20276 
20277     -- Valid value for inventory_asset_flag are (Y,N),default it to N
20278 
20279     lv_where_str :=
20280     ' AND NVL(inventory_asset_flag,'||''''||NULL_CHAR||''''||')'
20281     ||' NOT IN (''Y'', ''N'')';
20282 
20283     lv_return := MSC_ST_UTIL.LOG_ERROR
20284                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20285                      p_instance_code     => v_instance_code,
20286                      p_row               => lv_column_names,
20287                      p_severity          => G_SEV_WARNING,
20288                      p_message_text      => lv_message_text,
20289                      p_error_text        => lv_error_text,
20290                      p_batch_id          => lv_batch_id,
20291                      p_where_str         => lv_where_str,
20292                      p_col_name          => 'INVENTORY_ASSET_FLAG' ,
20293                      p_debug             => v_debug,
20294                      p_default_value     => '''Y''');
20295 
20296     IF lv_return <> 0 THEN
20297             RAISE ex_logging_err;
20298     END IF;
20299 
20300     -- Set the error message
20301     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20302                      (p_app_short_name    => 'MSC',
20303                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20304                       p_message_text      => lv_message_text,
20305                       p_error_text        => lv_error_text,
20306                       p_token1            => 'COLUMN_NAME',
20307                       p_token_value1      => 'END_ASSEMBLY_PEGGING_FLAG',
20308                       p_token2           => 'DEFAULT_VALUE',
20309                       p_token_value2     =>  G_END_ASSEMBLY_PEGGING_FLAG);
20310 
20311       IF lv_return <> 0 THEN
20312         RAISE ex_logging_err;
20313       END IF;
20314 
20315 
20316   --      Valid value for end_assembly_pegging_flag are
20317   --      ('A','B','I','N','X','Y'),default it to N
20318 
20319     lv_where_str :=
20320     ' AND end_assembly_pegging_flag NOT IN (''A'',''B'',''X'',''I'',''N'',''Y'')';
20321     lv_return := MSC_ST_UTIL.LOG_ERROR
20322                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20323                      p_instance_code     => v_instance_code,
20324                      p_row               => lv_column_names,
20325                      p_severity          => G_SEV_WARNING,
20326                      p_message_text      => lv_message_text,
20327                      p_error_text        => lv_error_text,
20328                      p_batch_id          => lv_batch_id,
20329                      p_where_str         => lv_where_str,
20330                      p_col_name          => 'END_ASSEMBLY_PEGGING_FLAG' ,
20331                      p_debug             => v_debug,
20332                      p_default_value     => '''N''');
20333 
20334     IF lv_return <> 0 THEN
20335             RAISE ex_logging_err;
20336     END IF;
20337 
20338     -- Set the error message
20339     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20340                      (p_app_short_name    => 'MSC',
20341                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20342                       p_message_text      => lv_message_text,
20343                       p_error_text        => lv_error_text,
20344                       p_token1            => 'COLUMN_NAME',
20345                       p_token_value1      => 'WIP_SUPPLY_TYPE',
20346                       p_token2           => 'DEFAULT_VALUE',
20347                       p_token_value2     =>  G_WIP_SUPPLY_TYPE);
20348 
20349       IF lv_return <> 0 THEN
20350         RAISE ex_logging_err;
20351       END IF;
20352 
20353 
20354    -- Valid value for wip_supply_type are (1,2,3,4,5,6,7),default it to 1
20355 
20356     lv_where_str := ' AND wip_supply_type NOT IN (1,2,3,4,5,6,7) ';
20357 
20358     lv_return := MSC_ST_UTIL.LOG_ERROR
20359                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20360                      p_instance_code     => v_instance_code,
20361                      p_row               => lv_column_names,
20362                      p_severity          => G_SEV_WARNING,
20363                      p_message_text      => lv_message_text,
20364                      p_error_text        => lv_error_text,
20365                      p_batch_id          => lv_batch_id,
20366                      p_where_str         => lv_where_str,
20367                      p_col_name          => 'WIP_SUPPLY_TYPE' ,
20368                      p_debug             => v_debug,
20369                      p_default_value     => G_WIP_SUPPLY_TYPE);
20370 
20371     IF lv_return <> 0 THEN
20372             RAISE ex_logging_err;
20373     END IF;
20374 
20375 
20376     -- Set the error message
20377     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20378                      (p_app_short_name    => 'MSC',
20379                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20380                       p_message_text      => lv_message_text,
20381                       p_error_text        => lv_error_text,
20382                       p_token1            => 'COLUMN_NAME',
20383                       p_token_value1      => 'ATO_FORECAST_CONTROL',
20384                       p_token2           => 'DEFAULT_VALUE',
20385                       p_token_value2     =>  G_ATO_FORECAST_CONTROL);
20386 
20387       IF lv_return <> 0 THEN
20388         RAISE ex_logging_err;
20389       END IF;
20390 
20391     -- Default ato_forecast_control to 3 if NOT IN (1,2,3)
20392 
20393     lv_where_str := ' AND ato_forecast_control NOT IN (1,2,3) ';
20394 
20395     lv_return := MSC_ST_UTIL.LOG_ERROR
20396                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20397                      p_instance_code     => v_instance_code,
20398                      p_row               => lv_column_names,
20399                      p_severity          => G_SEV_WARNING,
20400                      p_message_text      => lv_message_text,
20401                      p_error_text        => lv_error_text,
20402                      p_batch_id          => lv_batch_id,
20403                      p_where_str         => lv_where_str,
20404                      p_col_name          => 'ATO_FORECAST_CONTROL' ,
20405                      p_debug             => v_debug,
20406                      p_default_value     => G_ATO_FORECAST_CONTROL);
20407 
20408     IF lv_return <> 0 THEN
20409             RAISE ex_logging_err;
20410     END IF;
20411 
20412     -- Set the error message
20413     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20414                      (p_app_short_name    => 'MSC',
20415                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20416                       p_message_text      => lv_message_text,
20417                       p_error_text        => lv_error_text,
20418                       p_token1            => 'COLUMN_NAME',
20419                       p_token_value1      => 'INVENTORY_ITEM_FLAG',
20420                       p_token2            => 'DEFAULT_VALUE',
20421                       p_token_value2      =>  G_INVENTORY_ITEM_FLAG);
20422 
20423       IF lv_return <> 0 THEN
20424         RAISE ex_logging_err;
20425       END IF;
20426 
20427     -- Default inventory_item_flag  to 1 if NOT IN (1,2)
20428 
20429     lv_where_str := ' AND inventory_item_flag NOT IN (1,2) ';
20430 
20431     lv_return := MSC_ST_UTIL.LOG_ERROR
20432                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20433                      p_instance_code     => v_instance_code,
20434                      p_row               => lv_column_names,
20435                      p_severity          => G_SEV_WARNING,
20436                      p_message_text      => lv_message_text,
20437                      p_error_text        => lv_error_text,
20438                      p_batch_id          => lv_batch_id,
20439                      p_where_str         => lv_where_str,
20440                      p_col_name          => 'INVENTORY_ITEM_FLAG' ,
20441                      p_debug             => v_debug,
20442                      p_default_value     => G_INVENTORY_ITEM_FLAG);
20443 
20444     IF lv_return <> 0 THEN
20445             RAISE ex_logging_err;
20446     END IF;
20447 
20448     -- Set the error message
20449     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20450                      (p_app_short_name    => 'MSC',
20451                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20452                       p_message_text      => lv_message_text,
20453                       p_error_text        => lv_error_text,
20454                       p_token1            => 'COLUMN_NAME',
20455                       p_token_value1      => 'INVENTORY_TYPE',
20456                       p_token2            => 'DEFAULT_VALUE',
20457                       p_token_value2      =>  G_INVENTORY_TYPE);
20458 
20459       IF lv_return <> 0 THEN
20460         RAISE ex_logging_err;
20461       END IF;
20462 
20463     -- Default inventory_item_flag  to 1 if NOT IN (1,2)
20464 
20465     lv_where_str := ' AND inventory_type NOT IN (1,2,3,4,5,6,7,8) ';
20466 
20467     lv_return := MSC_ST_UTIL.LOG_ERROR
20468                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20469                      p_instance_code     => v_instance_code,
20470                      p_row               => lv_column_names,
20471                      p_severity          => G_SEV_WARNING,
20472                      p_message_text      => lv_message_text,
20473                      p_error_text        => lv_error_text,
20474                      p_batch_id          => lv_batch_id,
20475                      p_where_str         => lv_where_str,
20476                      p_col_name          => 'INVENTORY_TYPE' ,
20477                      p_debug             => v_debug,
20478                      p_default_value     => G_INVENTORY_TYPE);
20479 
20480     IF lv_return <> 0 THEN
20481             RAISE ex_logging_err;
20482     END IF;
20483   END IF ; --v_install_msc
20484     -- Set the error message
20485     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20486                      (p_app_short_name    => 'MSC',
20487                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20488                       p_message_text      => lv_message_text,
20489                       p_error_text        => lv_error_text,
20490                       p_token1            => 'COLUMN_NAME',
20491                       p_token_value1      => 'BOM_ITEM_TYPE',
20492                       p_token2           => 'DEFAULT_VALUE',
20493                       p_token_value2     =>  G_BOM_ITEM_TYPE);
20494 
20495       IF lv_return <> 0 THEN
20496         RAISE ex_logging_err;
20497       END IF;
20498 
20499     -- Default BOM Item Type to 4 if NOT IN (1,2,3,4,5)
20500 
20501     lv_where_str := ' AND bom_item_type NOT IN (1,2,3,4,5) ';
20502 
20503     lv_return := MSC_ST_UTIL.LOG_ERROR
20504                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20505                      p_instance_code     => v_instance_code,
20506                      p_row               => lv_column_names,
20507                      p_severity          => G_SEV_WARNING,
20508                      p_message_text      => lv_message_text,
20509                      p_error_text        => lv_error_text,
20510                      p_batch_id          => lv_batch_id,
20511                      p_where_str         => lv_where_str,
20512                      p_col_name          => 'bom_item_type' ,
20513                      p_debug             => v_debug,
20514                      p_default_value     => G_BOM_ITEM_TYPE);
20515 
20516     IF lv_return <> 0 THEN
20517             RAISE ex_logging_err;
20518     END IF;
20519 
20520 
20521   -- Set the error message for DRP_PLANNED flag
20522     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20523                      (p_app_short_name    => 'MSC',
20524                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20525                       p_message_text      => lv_message_text,
20526                       p_error_text        => lv_error_text,
20527                       p_token1            => 'COLUMN_NAME',
20528                       p_token_value1      => 'DRP_PLANNED',
20529                       p_token2            => 'DEFAULT_VALUE',
20530                       p_token_value2      => G_DRP_PLANNED);
20531 
20532       IF lv_return <> 0 THEN
20533         RAISE ex_logging_err;
20534       END IF;
20535 
20536     -- Valid value for drp_planned is 1,2  default it to 2
20537 
20538     lv_where_str :=
20539     '   AND NVL(drp_planned,'||NULL_VALUE||')'
20540     ||' NOT IN (1,2)';
20541 
20542     lv_return := MSC_ST_UTIL.LOG_ERROR
20543                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20544                      p_instance_code     => v_instance_code,
20545                      p_row               => lv_column_names,
20546                      p_severity          => G_SEV_WARNING,
20547                      p_message_text      => lv_message_text,
20548                      p_error_text        => lv_error_text,
20549                      p_batch_id          => lv_batch_id,
20550                      p_where_str         => lv_where_str,
20551                      p_col_name          => 'DRP_PLANNED',
20552                      p_debug             => v_debug,
20553                      p_default_value     => G_DRP_PLANNED);
20554 
20555      IF lv_return <> 0 THEN
20556         RAISE ex_logging_err;
20557     END IF;
20558 
20559 
20560 -- Set the error message for SERIAL_NUMBER_CONTROL_CODE
20561 
20562     lv_return := MSC_ST_util.GET_ERROR_MESSAGE
20563                      (p_app_short_name    => 'MSC',
20564                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
20565                       p_message_text      => lv_message_text,
20566                       p_error_text        => lv_error_text,
20567                       p_token1            => 'COLUMN_NAME',
20568                       p_token_value1      => 'SERIAL_NUMBER_CONTROL_CODE',
20569                       p_token2            => 'DEFAULT_VALUE',
20570                       p_token_value2      =>  G_SERIAL_NUMBER_CONTROL_CODE  );
20571 
20572     IF lv_return <> 0 THEN
20573         RAISE ex_logging_err;
20574     END IF;
20575 
20576     -- Valid values for SERIAL_NUMBER_CONTROL_CODE are 1,2  default it to 2
20577 
20578     lv_where_str :=
20579     '   AND   NVL(SERIAL_NUMBER_CONTROL_CODE,2)'
20580     ||' NOT IN (1,2)';
20581 
20582     lv_return := MSC_ST_util.LOG_ERROR
20583                     (p_table_name        => 'MSC_ST_SYSTEM_ITEMS',
20584                      p_instance_code     => v_instance_code,
20585                      p_row               => lv_column_names,
20586                      p_severity          => G_SEV_WARNING,
20587                      p_message_text      => lv_message_text,
20588                      p_error_text        => lv_error_text,
20589                      p_batch_id          => lv_batch_id,
20590                      p_where_str         => lv_where_str,
20591                      p_col_name          => 'SERIAL_NUMBER_CONTROL_CODE',
20592                      p_debug             => v_debug,
20593                      p_default_value     => G_SERIAL_NUMBER_CONTROL_CODE);
20594 
20595     IF lv_return <> 0 THEN
20596          RAISE ex_logging_err;
20597     END IF;
20598 
20599     -- If the Serial Number Validation is enabled.
20600     IF NVL(FND_PROFILE.VALUE('MSC_DEFAULT_SERIAL_CONTROL'),'Y') ='Y' THEN
20601 
20602     -- Assumes the org_id,inventory_item_id and sr_instance_id is derrived by now.
20603     -- Set the error message
20604 
20605     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20606                      (p_app_short_name    => 'MSC',
20607                       p_error_code        => 'MSC_PP_SERIAL_FLAG_NOT_CHANGED',
20608                       p_message_text      => lv_message_text,
20609                       p_error_text        => lv_error_text);
20610 
20611       IF lv_return <> 0 THEN
20612         RAISE ex_logging_err;
20613       END IF;
20614 
20615     -- Error out record if supply/demand daa exists and serial_number_control_code flag is being set to no(2)
20616 
20617      v_sql_stmt := 100;
20618 
20619      lv_sql_stmt :=
20620       ' UPDATE  msc_st_system_items mssi '
20621       ||' SET   process_flag        = '||G_ERROR_FLG||','
20622       ||'       error_text          = '||''''||lv_message_text||''''
20623       ||' WHERE nvl(serial_number_control_code,2) = 2 '
20624       ||' AND   EXISTS (SELECT serial_number_control_code
20625                          FROM msc_system_items msi,
20626                               msc_item_id_lid miil,
20627                               msc_apps_instances mai
20628                         WHERE msi.plan_id = -1
20629                           AND msi.organization_id   = mssi.organization_id
20630                           AND msi.sr_instance_id    = mai.instance_id
20631                           AND msi.inventory_item_id = miil.inventory_item_id
20632                           AND miil.sr_instance_id   = mai.instance_id
20633                           AND mai.instance_code     = mssi.sr_instance_code
20634                           AND miil.sr_inventory_item_id = mssi.sr_inventory_item_id
20635                           AND msi.serial_number_control_code = 1
20636                       )'
20637       ||' AND   EXISTS (SELECT msn.serial_txn_id
20638                          FROM msc_serial_numbers msn,
20639                               msc_sup_dem_entries msde,
20640                               msc_trading_partner_maps mtpm,
20641                               msc_trading_partners mtp,
20642                               msc_item_id_lid miil,
20643                               msc_apps_instances mai
20644                         WHERE msde.inventory_item_id = miil.inventory_item_id
20645                           AND miil.sr_inventory_item_id = mssi.sr_inventory_item_id
20646                           AND miil.sr_instance_id = mai.instance_id
20647                           AND mai.instance_code     = mssi.sr_instance_code
20648                           AND msde.plan_id = -1
20649                           AND msde.publisher_site_id = mtpm.company_key
20650                           AND mtpm.map_type = 2
20651                           AND mtpm.tp_key = mtp.partner_id
20652                           AND mtp.partner_type = 3
20653                           AND mtp.sr_tp_id = mssi.organization_id
20654                           AND mtp.sr_instance_id = mai.instance_id
20655                           AND msde.transaction_id = msn.serial_txn_id
20656                           AND NVL(msn.disable_date,sysdate+1)>sysdate
20657                        )'
20658       ||' AND   batch_id         = :lv_batch_id'
20659       ||' AND   process_flag     = '||G_IN_PROCESS
20660       ||' AND   sr_instance_code = :v_instance_code';
20661 
20662 
20663         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
20664 
20665       EXECUTE IMMEDIATE lv_sql_stmt
20666               USING     lv_batch_id,
20667                         v_instance_code;
20668       END IF;
20669 
20670 
20671 
20672   /* Bug 2172537 , do not prefix instance code before org code
20673      --Concatenating organization_code with instance_code
20674    v_sql_stmt := 19;
20675    lv_sql_stmt:=
20676    'UPDATE   msc_st_system_items '
20677       ||' SET      organization_code= sr_instance_code'||'||'':''||'||'organization_code'
20678       ||' WHERE    process_flag     = '||G_IN_PROCESS
20679       ||' AND      batch_id         =  :lv_batch_id'
20680       ||' AND      sr_instance_code = :v_instance_code';
20681 
20682 
20683             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
20684 
20685     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code; */
20686 
20687 /*** Setting the process_flag to valid in LOAD_ITEM_ABC_CLASSES -- fix for the bug 2191021
20688     -- Set the process flag as Valid and populate instance_id */
20689 
20690     lv_return :=
20691     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_SYSTEM_ITEMS',
20692                                p_instance_id    => v_instance_id,
20693                                p_instance_code  => v_instance_code,
20694                                p_process_flag   => G_VALID,
20695                                p_error_text     => lv_error_text,
20696                                p_debug          => v_debug,
20697                                p_batch_id       => lv_batch_id);
20698 
20699       IF lv_return <> 0 THEN
20700         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
20701       END IF;
20702 
20703 
20704     -- At the end calling  the LOG_ERROR for logging all errored out records
20705 
20706    lv_return :=
20707    MSC_ST_UTIL.LOG_ERROR(p_table_name    =>'MSC_ST_SYSTEM_ITEMS',
20708                          p_instance_code =>v_instance_code,
20709                          p_row           =>lv_column_names,
20710                          p_severity      =>G_SEV_ERROR,
20711                          p_message_text  =>NULL,
20712                          p_error_text    =>lv_error_text,
20713                          p_debug         => v_debug,
20714                          p_batch_id      => lv_batch_id  );
20715 
20716   IF(lv_return <> 0) THEN
20717       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
20718   END IF;
20719    COMMIT;
20720 
20721 
20722   IF c_lock%ISOPEN THEN
20723     CLOSE c_lock;
20724   END IF;
20725 
20726       -- ODP change
20727       -- Call the procedure to insert all items into the msd_st_level_values
20728       IF(v_install_msd) THEN
20729         lv_sql_stmt := ' DECLARE errbuf varchar2(250);'
20730                  ||' retcode  number ;'
20731                  ||' BEGIN '
20732                  ||' MSD_CL_PRE_PROCESS.LOAD_ITEMS'
20733                  ||' (ERRBUF         =>  errbuf ,'
20734                  ||'  RETCODE        =>  retcode ,'
20735                  ||'  p_instance_id => :v_instance_id,'
20736                  ||'  p_batch_id    => :lv_batch_id);'
20737                  ||'  IF ( retcode = '||G_WARNING||') THEN'
20738                  ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
20739                  ||'  END IF;'
20740                  ||'  END ;';
20741 
20742         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
20743         EXECUTE IMMEDIATE lv_sql_stmt
20744                 USING     v_instance_id,
20745                           lv_batch_id ;
20746       END IF ;
20747     COMMIT;
20748 
20749 
20750   EXCEPTION
20751     WHEN too_many_rows THEN
20752       lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_ITEMS'||'('
20753                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
20754         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
20755       ROLLBACK;
20756       IF c_lock%ISOPEN THEN
20757         CLOSE c_lock;
20758       END IF;
20759     WHEN ex_logging_err THEN
20760         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
20761       ROLLBACK;
20762       IF c_lock%ISOPEN THEN
20763         CLOSE c_lock;
20764       END IF;
20765     WHEN OTHERS THEN
20766       lv_error_text :=substr('MSC_CL_PRE_PROCESS.LOAD_ITEMS'||'('
20767                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
20768         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
20769       ROLLBACK;
20770       IF c_lock%ISOPEN THEN
20771         CLOSE c_lock;
20772       END IF;
20773   END LOAD_ITEMS;
20774 
20775 
20776   /*===========================================================================+
20777   | DESCRIPTION  : This procedure validates the data in MSC_ST_ITEM_SUBSTITUTES|
20778   |                table and derives the id's from the local id's table.       |
20779   +===========================================================================*/
20780   PROCEDURE LOAD_ITEM_SUBSTITUTES IS
20781 
20782   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
20783   lb_rowid              RowidTab;
20784   lv_return             NUMBER;
20785   lv_error_text         VARCHAR2(250);
20786   lv_where_str          VARCHAR2(5000);
20787   lv_sql_stmt           VARCHAR2(5000);
20788   lv_column_names       VARCHAR2(5000);          --stores concatenated column names
20789   lv_batch_id           msc_st_item_substitutes.batch_id%TYPE;
20790   lv_message_text       msc_errors.error_text%TYPE;
20791 
20792   ex_logging_err        EXCEPTION;
20793 
20794   CURSOR c1(p_batch_id NUMBER) IS
20795     SELECT rowid
20796     FROM   msc_st_item_substitutes
20797     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
20798     AND    batch_id         = p_batch_id
20799     AND    sr_instance_code = v_instance_code;
20800 
20801   BEGIN
20802 
20803     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20804                  (p_app_short_name    => 'MSC',
20805                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
20806                   p_message_text      => lv_message_text,
20807                   p_error_text        => lv_error_text);
20808 
20809     IF lv_return <> 0 THEN
20810       RAISE ex_logging_err;
20811     END IF;
20812 
20813     --Duplicate records check for the records whose source is XML
20814     v_sql_stmt := 01;
20815     lv_sql_stmt :=
20816     ' UPDATE   msc_st_item_substitutes msu1'
20817     ||' SET    process_flag = '||G_ERROR_FLG||','
20818     ||'        error_text   = '||''''||lv_message_text||''''
20819     ||' WHERE  message_id <  (SELECT MAX(message_id)'
20820     ||'        FROM  msc_st_item_substitutes msu2'
20821     ||'        WHERE  msu2.sr_instance_code  = msu1.sr_instance_code'
20822     ||'        AND    NVL(msu2.RELATIONSHIP_TYPE,       '||''''||NULL_CHAR||''''||') = '
20823     ||'               NVL(msu1.RELATIONSHIP_TYPE,       '||''''||NULL_CHAR||''''||')'
20824     ||'        AND    msu2.ITEM_NAME = msu1.ITEM_NAME'
20825     ||'        AND    msu2.SUBSTITUTE_ITEM_NAME         = msu1.SUBSTITUTE_ITEM_NAME'
20826     ||'        AND    NVL(msu2.CUSTOMER_NAME,       '||''''||NULL_CHAR||''''||') = '
20827     ||'               NVL(msu1.CUSTOMER_NAME,       '||''''||NULL_CHAR||''''||')'
20828     ||'        AND    NVL(msu2.CUSTOMER_SITE_CODE,  '||''''||NULL_CHAR||''''||') = '
20829     ||'               NVL(msu1.CUSTOMER_SITE_CODE,  '||''''||NULL_CHAR||''''||')'
20830     ||'        AND    NVL(msu2.SUBSTITUTION_SET,    '||''''||NULL_CHAR||''''||') = '
20831     ||'               NVL(msu1.SUBSTITUTION_SET,    '||''''||NULL_CHAR||''''||')'
20832     ||'        AND    NVL(msu2.EFFECTIVE_DATE,sysdate) = NVL(msu1.EFFECTIVE_DATE,sysdate) '
20833     ||'        AND    NVL(msu2.DISABLE_DATE,sysdate)   = NVL(msu1.DISABLE_DATE,sysdate) '
20834     ||'        AND    NVL(msu2.company_name,  '||''''||NULL_CHAR||''''||') = '
20835     ||'               NVL(msu1.company_name,  '||''''||NULL_CHAR||''''||')'
20836     ||'        AND   msu2.process_flag = msu1.process_flag'
20837     ||'        AND   NVL(msu2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
20838     ||' AND    msu1.process_flag     = '||G_IN_PROCESS
20839     ||' AND    msu1.sr_instance_code = :v_instance_code'
20840     ||' AND    NVL(msu1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
20841 
20842 
20843       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
20844 
20845     EXECUTE IMMEDIATE lv_sql_stmt
20846             USING     v_instance_code;
20847 
20848     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20849                  (p_app_short_name    => 'MSC',
20850                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
20851                   p_message_text      => lv_message_text,
20852                   p_error_text        => lv_error_text);
20853 
20854     IF lv_return <> 0 THEN
20855       RAISE ex_logging_err;
20856     END IF;
20857 
20858     --Duplicate records check for the records whose source is other than XML
20859     --Different SQL is used because in XML we can identify the latest records
20860     --whereas in batch load we cannot.
20861 
20862     v_sql_stmt := 02;
20863     lv_sql_stmt :=
20864     ' UPDATE   msc_st_item_substitutes msu1'
20865     ||' SET    process_flag = '||G_ERROR_FLG||','
20866     ||'        error_text   = '||''''||lv_message_text||''''
20867     ||' WHERE  EXISTS( SELECT 1 '
20868     ||'        FROM   msc_st_item_substitutes msu2'
20869     ||'        WHERE  msu2.sr_instance_code  = msu1.sr_instance_code'
20870     ||'        AND    NVL(msu2.RELATIONSHIP_TYPE,       '||''''||NULL_CHAR||''''||') = '
20871     ||'               NVL(msu1.RELATIONSHIP_TYPE,       '||''''||NULL_CHAR||''''||')'
20872     ||'        AND    msu2.ITEM_NAME = msu1.ITEM_NAME'
20873     ||'        AND    msu2.SUBSTITUTE_ITEM_NAME         = msu1.SUBSTITUTE_ITEM_NAME'
20874     ||'        AND    NVL(msu2.CUSTOMER_NAME,       '||''''||NULL_CHAR||''''||') = '
20875     ||'               NVL(msu1.CUSTOMER_NAME,       '||''''||NULL_CHAR||''''||')'
20876     ||'        AND    NVL(msu2.CUSTOMER_SITE_CODE,  '||''''||NULL_CHAR||''''||') = '
20877     ||'               NVL(msu1.CUSTOMER_SITE_CODE,  '||''''||NULL_CHAR||''''||')'
20878     ||'        AND    NVL(msu2.SUBSTITUTION_SET,    '||''''||NULL_CHAR||''''||') = '
20879     ||'               NVL(msu1.SUBSTITUTION_SET,    '||''''||NULL_CHAR||''''||')'
20880     ||'        AND    NVL(msu2.EFFECTIVE_DATE,sysdate) = NVL(msu1.EFFECTIVE_DATE,sysdate) '
20881     ||'        AND    NVL(msu2.DISABLE_DATE,sysdate)   = NVL(msu1.DISABLE_DATE,sysdate) '
20882     ||'        AND    NVL(msu2.company_name,  '||''''||NULL_CHAR||''''||') = '
20883     ||'               NVL(msu1.company_name,  '||''''||NULL_CHAR||''''||')'
20884     ||'        AND    msu2.process_flag = msu1.process_flag'
20885     ||'        AND    NVL(msu2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
20886     ||'        AND    msu2.rowid <> msu1.rowid)'
20887     ||' AND     msu1.process_flag     = '||G_IN_PROCESS
20888     ||' AND     msu1.sr_instance_code = :v_instance_code '
20889     ||' AND     NVL(msu1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
20890 
20891 
20892       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
20893 
20894     EXECUTE IMMEDIATE lv_sql_stmt
20895             USING     v_instance_code;
20896 
20897     lv_column_names :=
20898     'LOWER_ITEM_ID 	    		||''~''||'
20899     ||'HIGHER_ITEM_ID           	||''~''||'
20900     ||'HIGHEST_ITEM_ID       		||''~''||'
20901     ||'EFFECTIVE_DATE   		||''~''||'
20902     ||'DISABLE_DATE       		||''~''||'
20903     ||'RELATIONSHIP_TYPE     		||''~''||'
20904     ||'RECIPROCAL_FLAG     		||''~''||'
20905     ||'SUBSTITUTION_SET    		||''~''||'
20906     ||'PARTIAL_FULFILLMENT_FLAG         ||''~''||'
20907     ||'SUBSTITUTE_ITEM_NAME     	||''~''||'
20908     ||'ITEM_NAME      			||''~''||'
20909     ||'CUSTOMER_NAME        		||''~''||'
20910     ||'CUSTOMER_SITE_CODE     		||''~''||'
20911     ||'DELETED_FLAG ';
20912 
20913     LOOP
20914       v_sql_stmt := 03;
20915       SELECT msc_st_batch_id_s.NEXTVAL
20916       INTO   lv_batch_id
20917       FROM   dual;
20918 
20919       v_sql_stmt := 04;
20920       lv_sql_stmt :=
20921       ' UPDATE   msc_st_item_substitutes '
20922       ||' SET    batch_id  = :lv_batch_id'
20923       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
20924       ||' AND    sr_instance_code               = :v_instance_code'
20925       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
20926       ||' AND    rownum                        <= '||v_batch_size;
20927 
20928 
20929         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
20930 
20931       EXECUTE IMMEDIATE lv_sql_stmt
20932               USING     lv_batch_id,
20933                         v_instance_code;
20934 
20935       EXIT WHEN SQL%NOTFOUND;
20936 
20937       OPEN c1(lv_batch_id);
20938       FETCH c1 BULK COLLECT INTO lb_rowid;
20939       CLOSE c1;
20940 
20941       v_sql_stmt := 05;
20942       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
20943       UPDATE msc_st_item_substitutes
20944       SET  st_transaction_id = msc_st_item_substitute_s.NEXTVAL,
20945            last_update_date  = v_current_date,
20946            last_updated_by   = v_current_user,
20947            creation_date     = v_current_date,
20948            created_by        = v_current_user
20949       WHERE  rowid           = lb_rowid(j);
20950 
20951  /*     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
20952                      (p_app_short_name    => 'MSC',
20953                       p_error_code        => 'MSC_PP_NO_DELETION',
20954                       p_message_text      => lv_message_text,
20955                       p_error_text        => lv_error_text,
20956                       p_token1            => 'TABLE_NAME',
20957                       p_token_value1      => 'MSC_ST_ITEM_SUBSTITUTES');
20958 
20959       IF lv_return <> 0 THEN
20960         RAISE ex_logging_err;
20961       END IF;
20962 
20963       --Deletion is not allowed for this entity.
20964       v_sql_stmt := 06;
20965       lv_sql_stmt :=
20966       ' UPDATE     msc_st_item_substitutes '
20967       ||' SET      process_flag     = '||G_ERROR_FLG||','
20968       ||'          error_text       = '||''''||lv_message_text||''''
20969       ||' WHERE    deleted_flag     = '||SYS_YES
20970       ||' AND      process_flag     = '||G_IN_PROCESS
20971       ||' AND      batch_id         =  :lv_batch_id'
20972       ||' AND      sr_instance_code = :v_instance_code';
20973 
20974 
20975         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
20976 
20977       EXECUTE IMMEDIATE lv_sql_stmt
20978               USING     lv_batch_id,
20979                         v_instance_code;
20980 
20981 */
20982 
20983     -- RELATIONSHIP_TYPE default to 2
20984 
20985     lv_where_str :=    ' AND RELATIONSHIP_TYPE IS NULL';
20986     lv_return := MSC_ST_UTIL.LOG_ERROR
20987                     (p_table_name        => 'msc_st_item_substitutes',
20988                      p_instance_code     => v_instance_code,
20989                      p_row               => lv_column_names,
20990                      p_severity          => G_SEV_WARNING,
20991                      p_message_text      => lv_message_text,
20992                      p_error_text        => lv_error_text,
20993                      p_batch_id          => lv_batch_id,
20994                      p_where_str         => lv_where_str,
20995                      p_col_name          => 'RELATIONSHIP_TYPE',
20996                      p_debug             => v_debug,
20997                      p_default_value     => G_RELATIONSHIP_TYPE);
20998 
20999     IF lv_return <> 0 THEN
21000             RAISE ex_logging_err;
21001     END IF;
21002 
21003     -- Set the error message
21004     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21005                      (p_app_short_name    => 'MSC',
21006                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21007                       p_message_text      => lv_message_text,
21008                       p_error_text        => lv_error_text,
21009                       p_token1            => 'COLUMN_NAME',
21010                       p_token_value1      => 'RECIPROCAL_FLAG');
21011 
21012       IF lv_return <> 0 THEN
21013         RAISE ex_logging_err;
21014       END IF;
21015 
21016 
21017     -- Valid value for RECIPROCAL_FLAG  is 1 and 2
21018 
21019      v_sql_stmt := 99;
21020      lv_sql_stmt :=
21021 
21022         ' UPDATE   msc_st_item_substitutes '
21023       ||' SET      process_flag     = '||G_ERROR_FLG||','
21024       ||'          error_text       = '||''''||lv_message_text||''''
21025       ||' WHERE    nvl(RECIPROCAL_FLAG,'||NULL_VALUE||')  not in(1,2)'
21026       ||' AND      process_flag     = '||G_IN_PROCESS
21027       ||' AND      batch_id         = :lv_batch_id'
21028       ||' AND      sr_instance_code = :v_instance_code';
21029 
21030 
21031          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21032 
21033     EXECUTE IMMEDIATE   lv_sql_stmt
21034             USING       lv_batch_id,
21035                         v_instance_code;
21036 
21037     -- Set the error message
21038     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21039                      (p_app_short_name    => 'MSC',
21040                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21041                       p_message_text      => lv_message_text,
21042                       p_error_text        => lv_error_text,
21043                       p_token1            => 'COLUMN_NAME',
21044                       p_token_value1      => 'PARTIAL_FULFILLMENT_FLAG');
21045 
21046       IF lv_return <> 0 THEN
21047         RAISE ex_logging_err;
21048       END IF;
21049 
21050 
21051     -- Valid value for PARTIAL_FULFILLMENT_FLAG  is 1,2
21052 
21053     v_sql_stmt := 98;
21054      lv_sql_stmt :=
21055 
21056         ' UPDATE   msc_st_item_substitutes '
21057       ||' SET      process_flag     = '||G_ERROR_FLG||','
21058       ||'          error_text       = '||''''||lv_message_text||''''
21059       ||' WHERE    nvl(PARTIAL_FULFILLMENT_FLAG,'||NULL_VALUE||')  not in(1,2)'
21060       ||' AND      relationship_type = 2 '
21061       ||' AND      process_flag     = '||G_IN_PROCESS
21062       ||' AND      batch_id         = :lv_batch_id'
21063       ||' AND      sr_instance_code =  :v_instance_code';
21064 
21065 
21066          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21067 
21068     EXECUTE IMMEDIATE   lv_sql_stmt
21069             USING       lv_batch_id,
21070                         v_instance_code;
21071 
21072       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21073                      (p_app_short_name    => 'MSC',
21074                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21075                       p_message_text      => lv_message_text,
21076                       p_error_text        => lv_error_text,
21077                       p_token1            => 'COLUMN_NAME',
21078                       p_token_value1      => 'ORGANIZATION_CODE');
21079 
21080       IF lv_return <> 0 THEN
21081         RAISE ex_logging_err;
21082       END IF;
21083 
21084       -- Derive organization_id
21085       lv_return :=msc_st_util.derive_partner_org_id
21086                        (p_table_name        => 'MSC_ST_ITEM_SUBSTITUTES',
21087                         p_org_partner_name  => 'ORGANIZATION_CODE',
21088                         p_org_partner_id    => 'ORGANIZATION_ID',
21089                         p_instance_code     => v_instance_code,
21090                         p_partner_type      => G_ORGANIZATION,
21091                         p_error_text        => lv_error_text,
21092                         p_debug             => v_debug,
21093                         p_batch_id          => lv_batch_id);
21094 
21095       IF lv_return <> 0 THEN
21096         RAISE ex_logging_err;
21097       END IF;
21098 
21099       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21100                      (p_app_short_name    => 'MSC',
21101                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21102                       p_message_text      => lv_message_text,
21103                       p_error_text        => lv_error_text,
21104                       p_token1            => 'COLUMN_NAME',
21105                       p_token_value1      => 'CUSTOMER_NAME');
21106 
21107       IF lv_return <> 0 THEN
21108         RAISE ex_logging_err;
21109       END IF;
21110 
21111      lv_where_str :=   ' AND RELATIONSHIP_TYPE = 2';
21112 
21113     -- Derive sr_tp_id(Customer)
21114       lv_return :=msc_st_util.derive_partner_org_id
21115                        (p_table_name        => 'MSC_ST_ITEM_SUBSTITUTES',
21116                         p_org_partner_name  => 'CUSTOMER_NAME',
21117                         p_org_partner_id    => 'CUSTOMER_ID',
21118                         p_instance_code     => v_instance_code,
21119                         p_partner_type      => G_CUSTOMER,
21120                         p_error_text        => lv_error_text,
21121                         p_debug             => v_debug,
21122                         p_severity          => G_SEV3_ERROR,
21123                         p_message_text      => lv_message_text,
21124                         p_row               => lv_column_names,
21125                         p_batch_id          => lv_batch_id,
21126                         p_where_str         => lv_where_str);
21127 
21128       IF lv_return <> 0 THEN
21129         RAISE ex_logging_err;
21130       END IF;
21131 
21132 
21133       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21134                      (p_app_short_name    => 'MSC',
21135                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21136                       p_message_text      => lv_message_text,
21137                       p_error_text        => lv_error_text,
21138                       p_token1            => 'COLUMN_NAME',
21139                       p_token_value1      => 'CUSTOMER_SITE_CODE');
21140 
21141       lv_where_str :=   ' AND RELATIONSHIP_TYPE = 2';
21142     -- Derive sr_tp_site_id(Customer)
21143       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
21144                        (p_table_name        => 'MSC_ST_ITEM_SUBSTITUTES',
21145                         p_partner_name      => 'CUSTOMER_NAME',
21146                         p_partner_site_code => 'CUSTOMER_SITE_CODE',
21147                         p_partner_site_id   => 'CUSTOMER_SITE_ID',
21148                         p_instance_code     => v_instance_code,
21149                         p_partner_type      => G_CUSTOMER,
21150                         p_error_text        => lv_error_text,
21151                         p_batch_id          => lv_batch_id,
21152                         p_severity          => G_SEV3_ERROR,
21153                         p_debug             => v_debug,
21154                         p_row               => lv_column_names,
21155                         p_where_str         => lv_where_str);
21156 
21157       IF lv_return <> 0 THEN
21158         RAISE ex_logging_err;
21159       END IF;
21160 
21161     --DERIVE LOWER_ITEM_ID
21162 
21163       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21164                      (p_app_short_name    => 'MSC',
21165                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21166                       p_message_text      => lv_message_text,
21167                       p_error_text        => lv_error_text,
21168                       p_token1            => 'COLUMN_NAME',
21169                       p_token_value1      => 'ITEM_NAME');
21170 
21171 
21172    lv_return :=  MSC_ST_UTIL.DERIVE_ITEM_ID
21173                      (p_table_name =>'MSC_ST_ITEM_SUBSTITUTES',
21174                       p_item_col_name =>'ITEM_NAME',
21175                       p_item_col_id   =>'LOWER_ITEM_ID',
21176                       p_instance_code => v_instance_code,
21177                       p_instance_id   => v_instance_id,
21178                       p_batch_id      =>lv_batch_id,
21179                       p_debug         => v_debug,
21180                       p_severity      => G_SEV_ERROR,
21181                       p_message_text  =>lv_message_text,
21182                       p_error_text    =>lv_error_text,
21183                       p_row           => lv_column_names,
21184                       p_check_org        => FALSE);
21185 
21186     IF lv_return <> 0 THEN
21187         RAISE ex_logging_err;
21188     END IF;
21189 
21190 
21191     --DERIVE HIGHER_ITEM_ID
21192 
21193       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21194                      (p_app_short_name    => 'MSC',
21195                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21196                       p_message_text      => lv_message_text,
21197                       p_error_text        => lv_error_text,
21198                       p_token1            => 'COLUMN_NAME',
21199                       p_token_value1      => 'SUBSTITUTE_ITEM_NAME');
21200 
21201 
21202    lv_return :=  MSC_ST_UTIL.DERIVE_ITEM_ID
21203                      (p_table_name =>'msc_st_item_substitutes',
21204                       p_item_col_name =>'SUBSTITUTE_ITEM_NAME',
21205                       p_item_col_id   =>'HIGHER_ITEM_ID',
21206                       p_instance_code => v_instance_code,
21207                       p_instance_id   => v_instance_id,
21208                       p_batch_id      =>lv_batch_id,
21209                       p_debug         => v_debug,
21210                       p_severity      => G_SEV_ERROR,
21211                       p_message_text  =>lv_message_text,
21212                       p_error_text    =>lv_error_text,
21213                       p_row           => lv_column_names,
21214                       p_check_org        => FALSE);
21215 
21216     IF lv_return <> 0 THEN
21217         RAISE ex_logging_err;
21218     END IF;
21219 
21220     -- SUBSTITUTION_SET always default to 'DEFAULT'
21221 
21222     lv_where_str :=
21223       ' AND NVL(SUBSTITUTION_SET,'||''''||NULL_CHAR||''''||') = '||''''||NULL_CHAR||'''';
21224     lv_return := MSC_ST_UTIL.LOG_ERROR
21225                     (p_table_name        => 'msc_st_item_substitutes',
21226                      p_instance_code     => v_instance_code,
21227                      p_row               => lv_column_names,
21228                      p_severity          => G_SEV_WARNING,
21229                      p_message_text      => lv_message_text,
21230                      p_error_text        => lv_error_text,
21231                      p_batch_id          => lv_batch_id,
21232                      p_where_str         => lv_where_str,
21233                      p_col_name          => 'SUBSTITUTION_SET',
21234                      p_default_value     => '''DEFAULT''',
21235                      p_debug             => v_debug);
21236 
21237     IF lv_return <> 0 THEN
21238             RAISE ex_logging_err;
21239     END IF;
21240 
21241 /* removed for SRP
21242     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21243                      (p_app_short_name    => 'MSC',
21244                       p_error_code        => 'MSC_PP_NO_UPDATION',
21245                       p_message_text      => lv_message_text,
21246                       p_error_text        => lv_error_text,
21247                       p_token1            => 'TABLE_NAME',
21248                       p_token_value1      => 'MSC_ST_ITEM_SUBSTITUTES');
21249 
21250     IF lv_return <> 0 THEN
21251       RAISE ex_logging_err;
21252     END IF;
21253 
21254     --Updation is not allowed on this entity(this is done to make the
21255     --Post-Processor in sync with the collection program).
21256 
21257     v_sql_stmt := 09;
21258     lv_sql_stmt :=
21259     ' UPDATE   msc_st_item_substitutes mis1'
21260       ||' SET    process_flag     = '||G_ERROR_FLG||','
21261       ||'        error_text       = '||''''||lv_message_text||''''
21262       ||' WHERE  process_flag     = '||G_IN_PROCESS
21263       ||' AND    batch_id         =  :lv_batch_id'
21264       ||' AND    sr_instance_code =  :v_instance_code'
21265       ||' AND    EXISTS'
21266       ||'        (SELECT 1 FROM msc_item_substitutes mis2'
21267       ||'        WHERE   mis2.higher_item_id        = mis1.higher_item_id'
21268       ||'        AND     mis2.lower_item_id         = mis1.lower_item_id'
21269       ||'        AND     nvl(mis2.customer_id, '||NULL_VALUE||') = nvl(mis1.customer_id,'||NULL_VALUE||')'
21270       ||'        AND     nvl(mis2.customer_site_id, '||NULL_VALUE||')  = nvl(mis1.customer_site_id,'||NULL_VALUE||')'
21271       ||'        AND     mis2.substitution_set      = mis1.substitution_set'
21272       ||'        AND     trunc(nvl(mis2.EFFECTIVE_DATE,SYSDATE+36500)) = trunc(nvl(mis1.EFFECTIVE_DATE, SYSDATE+36500))'
21273       ||'        AND     trunc(nvl(mis2.DISABLE_DATE, SYSDATE+36500))   = trunc(nvl(mis1.DISABLE_DATE, SYSDATE+36500))'
21274       ||'        AND     mis2.sr_instance_id        = ' ||v_instance_id ||')';
21275 
21276 
21277         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21278 
21279       EXECUTE IMMEDIATE lv_sql_stmt
21280               USING     lv_batch_id,
21281                         v_instance_code;
21282 */
21283 ------ Changes for SRP legacy collections
21284 
21285   ---set message
21286 	lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21287                      (p_app_short_name    => 'MSC',
21288                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21289                       p_message_text      => lv_message_text,
21290                       p_error_text        => lv_error_text,
21291                       p_token1            => 'COLUMN_NAME',
21292                       p_token_value1      => 'RELATIONSHIP_TYPE');
21293 
21294     IF lv_return <> 0 THEN
21295       RAISE ex_logging_err;
21296     END IF;
21297 
21298 -- check if relationship_type not in 2,5,8,18
21299 
21300     v_sql_stmt := 12;
21301 	  lv_sql_stmt :=
21302 	' UPDATE   msc_st_item_substitutes '
21303    	   ||' SET      process_flag     = '||G_ERROR_FLG||','
21304   	   ||'          error_text       = '||''''||lv_message_text||''''
21305      	 ||' WHERE   RELATIONSHIP_TYPE  not in(2,5,8,18)'
21306        ||' AND      process_flag     = '||G_IN_PROCESS
21307        ||' AND      batch_id         = :lv_batch_id'
21308        ||' AND      sr_instance_code =  :v_instance_code';
21309 
21310 
21311 
21312         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21313 
21314       EXECUTE IMMEDIATE lv_sql_stmt
21315               USING     lv_batch_id,
21316                         v_instance_code;
21317 
21318 
21319       --Call to customised validation.
21320       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
21321         (ERRBUF         => lv_error_text,
21322          RETCODE        => lv_return,
21323          pBatchID       => lv_batch_id,
21324          pInstanceCode  => v_instance_code,
21325          pEntityName    => 'MSC_ST_ITEM_SUBSTITUTES',
21326          pInstanceID    => v_instance_id);
21327 
21328       IF NVL(lv_return,0) <> 0 THEN
21329         RAISE ex_logging_err;
21330       END IF;
21331 
21332     -- Set the process flag as Valid and derive sr_instance_id
21333 
21334       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
21335                   (p_table_name     => 'MSC_ST_ITEM_SUBSTITUTES',
21336                    p_instance_id    => v_instance_id,
21337                    p_instance_code  => v_instance_code,
21338                    p_process_flag   => G_VALID,
21339                    p_error_text     => lv_error_text,
21340                    p_debug          => v_debug,
21341                    p_batch_id       => lv_batch_id);
21342 
21343       IF lv_return <> 0 THEN
21344         RAISE ex_logging_err;
21345       END IF;
21346 
21347     -- Inserting all the errored out records into MSC_ERRORS
21348 
21349       lv_return := MSC_ST_UTIL.LOG_ERROR
21350                    (p_table_name        => 'MSC_ST_ITEM_SUBSTITUTES',
21351                     p_instance_code     => v_instance_code,
21352                     p_row               => lv_column_names,
21353                     p_severity          => G_SEV_ERROR,
21354                     p_error_text        => lv_error_text,
21355                     p_message_text      => NULL,
21356                     p_batch_id          => lv_batch_id);
21357 
21358 
21359       IF lv_return <> 0 THEN
21360         RAISE ex_logging_err;
21361       END IF;
21362 
21363 
21364  /*
21365     --MSCCLBAB is modified to handle the legacy collection of item substitutes
21366     --Bug#2489334
21367 
21368     --Inserts the valid records into the ODS table, Since the collection program
21369      --doesn't collect this entity incrementally and whereras post-processor always
21370      --runs in the incremental mode.
21371 
21372      v_sql_stmt := 10;
21373      lv_sql_stmt :=
21374       'INSERT INTO msc_item_substitutes'
21375       ||'( PLAN_ID,'
21376       ||'  HIGHER_ITEM_ID,'
21377       ||'  LOWER_ITEM_ID,'
21378       ||'  RECIPROCAL_FLAG,'
21379       ||'  SUBSTITUTION_SET,'
21380       ||'  CUSTOMER_ID,'
21381       ||'  CUSTOMER_SITE_ID,'
21382       ||'  EFFECTIVE_DATE,'
21383       ||'  DISABLE_DATE,'
21384       ||'  RELATIONSHIP_TYPE,'
21385       ||'  PARTIAL_FULFILLMENT_FLAG,'
21386       ||'  SR_INSTANCE_ID,'
21387       ||'  LAST_UPDATE_DATE,'
21388       ||'  LAST_UPDATED_BY,'
21389       ||'  LAST_UPDATE_LOGIN,'
21390       ||'  CREATION_DATE,'
21391       ||'  CREATED_BY)'
21392       ||' SELECT'
21393       ||'   -1,'
21394       ||'  HIGHER_ITEM_ID,'
21395       ||'  LOWER_ITEM_ID,'
21396       ||'  RECIPROCAL_FLAG,'
21397       ||'  SUBSTITUTION_SET,'
21398       ||'  CUSTOMER_ID,'
21399       ||'  CUSTOMER_SITE_ID,'
21400       ||'  EFFECTIVE_DATE,'
21401       ||'  DISABLE_DATE,'
21402       ||'  RELATIONSHIP_TYPE,'
21403       ||'  PARTIAL_FULFILLMENT_FLAG,'
21404       ||'  SR_INSTANCE_ID,'
21405       ||'  LAST_UPDATE_DATE,'
21406       ||'  LAST_UPDATED_BY,'
21407       ||'  LAST_UPDATE_LOGIN,'
21408       ||'  CREATION_DATE,'
21409       ||'  CREATED_BY '
21410       ||' FROM   msc_st_item_substitutes'
21411       ||' WHERE  sr_instance_code = :sr_instance_code'
21412       ||' AND    batch_id         = :lv_batch_id'
21413       ||' AND    process_flag     = '||G_VALID;
21414 
21415 
21416         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21417 
21418       EXECUTE IMMEDIATE lv_sql_stmt
21419               USING     v_instance_code,
21420                         lv_batch_id;
21421 
21422       */
21423 
21424       COMMIT;
21425     END LOOP;
21426 
21427   EXCEPTION
21428 
21429     WHEN too_many_rows THEN
21430       lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_ITEM_SUBSTITUTES'||'('
21431                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
21432         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
21433       ROLLBACK;
21434 
21435     WHEN ex_logging_err THEN
21436         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
21437       ROLLBACK;
21438 
21439     WHEN OTHERS THEN
21440       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_ITEM_SUBSTITUTES'||'('
21441                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
21442         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
21443       ROLLBACK;
21444 
21445   END LOAD_ITEM_SUBSTITUTES;
21446 
21447 
21448 /*==========================================================================+
21449 | DESCRIPTION  : This procedure validates the data in MSC_ST_ITEM_SUPPLIERS,|
21450 |                MSC_ST_SUPPLIER_CAPACITIES and MSC_ST_SUPPLIER_FLEX_FENCES |
21451 |                table.                                                     |
21452 +==========================================================================*/
21453   PROCEDURE LOAD_SUPP_CAPACITY IS
21454   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
21455   lb_rowid          RowidTab;
21456   lv_error_text     VARCHAR2(250);
21457   lv_column_names   VARCHAR2(5000);--stores concatenated column names
21458   lv_where_str      VARCHAR2(5000);
21459   lv_sql_stmt       VARCHAR2(5000);
21460   lv_return         NUMBER;
21461   lv_batch_id       msc_st_item_suppliers.batch_id%TYPE;
21462   lv_message_text   msc_errors.error_text%TYPE;
21463 
21464   ex_logging_err    EXCEPTION;
21465 
21466 
21467   CURSOR c1(p_batch_id NUMBER) IS
21468     SELECT rowid
21469     FROM   msc_st_item_suppliers
21470     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
21471     AND    sr_instance_code = v_instance_code
21472     AND    batch_id         = p_batch_id;
21473 
21474   CURSOR c2(p_batch_id NUMBER) IS
21475     SELECT rowid
21476     FROM   msc_st_supplier_capacities
21477     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
21478     AND    sr_instance_code = v_instance_code
21479     AND    batch_id         = p_batch_id;
21480 
21481   CURSOR c3(p_batch_id NUMBER) IS
21482     SELECT rowid
21483     FROM   msc_st_supplier_flex_fences
21484     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
21485     AND    sr_instance_code = v_instance_code
21486     AND    batch_id         = p_batch_id;
21487 
21488   BEGIN
21489 
21490    --For local ASL's the organization code and the using organization code
21491    --will be one and the same
21492 
21493     v_sql_stmt := 00;
21494 
21495     lv_sql_stmt :=
21496       'UPDATE      msc_st_item_suppliers '
21497       ||' SET      using_organization_code = organization_code'
21498       ||' WHERE    asl_level        = 2'
21499       ||' AND      process_flag     = '||G_IN_PROCESS
21500       ||' AND      sr_instance_code = :v_instance_code';
21501 
21502 
21503         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21504 
21505       EXECUTE IMMEDIATE lv_sql_stmt
21506               USING     v_instance_code;
21507 
21508 
21509     --Validation check fot the table MSC_ST_ITEM_SUPPLIERS.
21510     --Duplicate records check for the records whose source is XML
21511 
21512     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21513                  (p_app_short_name    => 'MSC',
21514                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
21515                   p_message_text      => lv_message_text,
21516                   p_error_text        => lv_error_text);
21517 
21518     IF lv_return <> 0 THEN
21519       RAISE ex_logging_err;
21520     END IF;
21521 
21522     v_sql_stmt := 01;
21523     lv_sql_stmt :=
21524     'UPDATE msc_st_item_suppliers mis1 '
21525     ||' SET process_flag = '||G_ERROR_FLG||','
21526     ||'      error_text   = '||''''||lv_message_text||''''
21527     ||' WHERE   message_id <  (SELECT MAX(message_id) '
21528     ||'         FROM   msc_st_item_suppliers mis2 '
21529     ||'         WHERE  mis2.sr_instance_code  = mis1.sr_instance_code'
21530     ||'         AND    mis2.vendor_name       = mis1.vendor_name'
21531     ||'         AND    mis2.organization_code  = mis1.organization_code'
21532     ||'         AND    mis2.vendor_site_code  = mis1.vendor_site_code'
21533     ||'         AND    mis2.item_name         = mis1.item_name'
21534     ||'         AND    NVL(mis2.company_name,  '||''''||NULL_CHAR||''''||') = '
21535     ||'                NVL(mis1.company_name,  '||''''||NULL_CHAR||''''||')'
21536     ||'         AND    NVL(mis2.using_organization_code,  '||''''||NULL_CHAR||''''||') = '
21537     ||'                NVL(mis1.using_organization_code,  '||''''||NULL_CHAR||''''||')'
21538     ||'         AND    mis2.process_flag      = '||G_IN_PROCESS
21539     ||'         AND    mis2.sr_instance_code  = mis1.sr_instance_code'
21540     ||'         AND    NVL(mis2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
21541     ||' AND     mis1.process_flag             ='||G_IN_PROCESS
21542     ||' AND     mis1.sr_instance_code         = :v_instance_code'
21543     ||' AND     NVL(mis1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
21544 
21545 
21546       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21547 
21548     EXECUTE IMMEDIATE lv_sql_stmt
21549             USING     v_instance_code;
21550 
21551     --Duplicate records check for the records whose source is other than XML
21552     --Different SQL is used because in XML we can identify the latest records
21553     --whereas in batch load we cannot.
21554 
21555     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21556                  (p_app_short_name    => 'MSC',
21557                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
21558                   p_message_text      => lv_message_text,
21559                   p_error_text        => lv_error_text);
21560 
21561     IF lv_return <> 0 THEN
21562       RAISE ex_logging_err;
21563     END IF;
21564 
21565     v_sql_stmt := 02;
21566     lv_sql_stmt :=
21567     'UPDATE  msc_st_item_suppliers mis1 '
21568     ||' SET  process_flag  = '||G_ERROR_FLG||','
21569     ||'      error_text   = '||''''||lv_message_text||''''
21570     ||' WHERE EXISTS( SELECT 1 '
21571     ||'       FROM   msc_st_item_suppliers mis2'
21572     ||'       WHERE  mis2.sr_instance_code         = mis1.sr_instance_code'
21573     ||'       AND    mis2.vendor_name                = mis1.vendor_name'
21574     ||'       AND    mis2.organization_code          = mis1.organization_code'
21575     ||'       AND    mis2.vendor_site_code           = mis1.vendor_site_code'
21576     ||'       AND    mis2.item_name                  = mis1.item_name'
21577     ||'       AND    NVL(mis2.company_name,  '||''''||NULL_CHAR||''''||') = '
21578     ||'              NVL(mis1.company_name,  '||''''||NULL_CHAR||''''||')'
21579     ||'       AND    NVL(mis2.using_organization_code,  '||''''||NULL_CHAR||''''||') = '
21580     ||'              NVL(mis1.using_organization_code,  '||''''||NULL_CHAR||''''||')'
21581     ||'       AND    mis2.process_flag = '||G_IN_PROCESS
21582     ||'       AND    mis2.sr_instance_code           = mis1.sr_instance_code'
21583     ||'       AND    NVL(mis2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
21584     ||'       GROUP BY sr_instance_code,organization_code,company_name,'
21585     ||'              item_name,vendor_name,vendor_site_code,using_organization_code'
21586     ||'       HAVING COUNT(*) > 1)'
21587     ||' AND   mis1.process_flag = '||G_IN_PROCESS
21588     ||' AND   mis1.sr_instance_code = :v_instance_code'
21589     ||' AND   NVL(mis1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
21590 
21591 
21592 
21593       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21594 
21595     EXECUTE IMMEDIATE lv_sql_stmt
21596             USING     v_instance_code;
21597 
21598     lv_column_names :=
21599     'ITEM_NAME                     ||''~''||'
21600     ||'ORGANIZATION_CODE           ||''~''||'
21601     ||'VENDOR_NAME                 ||''~''||'
21602     ||'SR_INSTANCE_CODE            ||''~''||'
21603     ||'PROCESSING_LEAD_TIME        ||''~''||'
21604     ||'MINIMUM_ORDER_QUANTITY      ||''~''||'
21605     ||'FIXED_LOT_MULTIPLE          ||''~''||'
21606     ||'DELIVERY_CALENDAR_CODE      ||''~''||'
21607     ||'VENDOR_SITE_CODE            ||''~''||'
21608     ||'SUPPLIER_CAP_OVER_UTIL_COST ||''~''||'
21609     ||'COMPANY_NAME                ||''~''||'
21610     ||'PURCHASING_UNIT_OF_MEASURE  ||''~''||'
21611     ||'USING_ORGANIZATION_ID       ||''~''||'
21612     ||'DELETED_FLAG';
21613 
21614     LOOP
21615       v_sql_stmt := 03;
21616       SELECT msc_st_batch_id_s.NEXTVAL
21617       INTO   lv_batch_id
21618       FROM   dual;
21619 
21620       v_sql_stmt := 04;
21621       lv_sql_stmt :=
21622       'UPDATE msc_st_item_suppliers '
21623       ||' SET    batch_id         = :lv_batch_id'
21624       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
21625       ||' AND    sr_instance_code               = :v_instance_code'
21626       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
21627       ||' AND    rownum                        <= '||v_batch_size;
21628 
21629 
21630         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21631 
21632       EXECUTE IMMEDIATE lv_sql_stmt
21633               USING     lv_batch_id,
21634                         v_instance_code;
21635 
21636       EXIT WHEN SQL%NOTFOUND;
21637 
21638       OPEN c1(lv_batch_id);
21639       FETCH c1 BULK COLLECT INTO lb_rowid;
21640       CLOSE c1;
21641 
21642       v_sql_stmt := 05;
21643       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
21644       UPDATE msc_st_item_suppliers
21645       SET    st_transaction_id   = msc_st_item_suppliers_s.NEXTVAL,
21646              last_update_date    = v_current_date,
21647              last_updated_by     = v_current_user,
21648              creation_date       = v_current_date,
21649              created_by          = v_current_user
21650       WHERE  rowid           = lb_rowid(j);
21651 
21652       --Deletion is not allowed on this table.
21653 
21654       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21655                      (p_app_short_name    => 'MSC',
21656                       p_error_code        => 'MSC_PP_NO_DELETION',
21657                       p_message_text      => lv_message_text,
21658                       p_error_text        => lv_error_text,
21659                       p_token1            => 'TABLE_NAME',
21660                       p_token_value1      => 'MSC_ST_ITEM_SUPPLIERS');
21661 
21662       IF lv_return <> 0 THEN
21663         RAISE ex_logging_err;
21664       END IF;
21665 
21666       v_sql_stmt := 06;
21667       lv_sql_stmt :=
21668       'UPDATE      msc_st_item_suppliers '
21669       ||' SET      process_flag = '||G_ERROR_FLG||','
21670       ||'      error_text   = '||''''||lv_message_text||''''
21671       ||' WHERE    deleted_flag     = '||SYS_YES
21672       ||' AND      process_flag     = '||G_IN_PROCESS
21673       ||' AND      batch_id         =  :lv_batch_id'
21674       ||' AND      sr_instance_code = :v_instance_code';
21675 
21676 
21677         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21678 
21679       EXECUTE IMMEDIATE lv_sql_stmt
21680               USING     lv_batch_id,
21681                         v_instance_code;
21682 
21683       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21684                      (p_app_short_name    => 'MSC',
21685                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21686                       p_message_text      => lv_message_text,
21687                       p_error_text        => lv_error_text,
21688                       p_token1            => 'COLUMN_NAME',
21689                       p_token_value1      => 'ASL_LEVEL');
21690 
21691       IF lv_return <> 0 THEN
21692         RAISE ex_logging_err;
21693       END IF;
21694 
21695       v_sql_stmt := 06;
21696 
21697       lv_sql_stmt :=
21698       'UPDATE      msc_st_item_suppliers '
21699       ||' SET      process_flag     = '||G_ERROR_FLG||','
21700       ||'      error_text           = '||''''||lv_message_text||''''
21701       ||' WHERE    asl_level        not in(1,2)'
21702       ||' AND      process_flag     = '||G_IN_PROCESS
21703       ||' AND      batch_id         =  :lv_batch_id'
21704       ||' AND      sr_instance_code = :v_instance_code';
21705 
21706 
21707         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21708 
21709       EXECUTE IMMEDIATE lv_sql_stmt
21710               USING     lv_batch_id,
21711                         v_instance_code;
21712 
21713 
21714       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21715                      (p_app_short_name    => 'MSC',
21716                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
21717                       p_message_text      => lv_message_text,
21718                       p_error_text        => lv_error_text,
21719                       p_token1            => 'COLUMN_NAMES',
21720                       p_token_value1      => 'SR_INSTANCE_CODE OR'
21721                                              ||' PURCHASING_UNIT_OF_MEASURE',
21722                       p_token2            => 'MASTER_TABLE',
21723                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
21724                       p_token3            => 'CHILD_TABLE',
21725                       p_token_value3      => 'MSC_ST_ITEM_SUPPLIERS');
21726 
21727       IF lv_return <> 0 THEN
21728         RAISE ex_logging_err;
21729       END IF;
21730 
21731       -- Validate all UOM code.
21732       v_sql_stmt := 07;
21733       lv_sql_stmt :=
21734       'UPDATE   msc_st_item_suppliers mstp'
21735       ||' SET   process_flag     = '||G_ERROR_FLG||','
21736       ||'       error_text       = '||''''||lv_message_text||''''
21737       ||' WHERE NOT EXISTS(SELECT 1 '
21738       ||'       FROM msc_units_of_measure muom '
21739       ||'       WHERE NVL(muom.unit_of_measure,           '||''''||NULL_CHAR||''''||') = '
21740       ||'             NVL(mstp.purchasing_unit_of_measure,'||''''||NULL_CHAR||''''||')'
21741       ||'       UNION '
21742       ||'       SELECT 1 FROM msc_st_units_of_measure msuom'
21743       ||'       WHERE NVL(msuom.unit_of_measure,          '||''''||NULL_CHAR||''''||') = '
21744       ||'             NVL(mstp.purchasing_unit_of_measure,'||''''||NULL_CHAR||''''||')'
21745       ||'       AND   msuom.sr_instance_id = :v_instance_id'
21746       ||'       AND   msuom.process_flag   = '||G_VALID||' ) '
21747       ||' AND NVL(mstp.purchasing_unit_of_measure,        '||''''||NULL_CHAR||''''||')'
21748       ||'     <> '||''''||NULL_CHAR||''''
21749       ||' AND mstp.sr_instance_code        = :v_instance_code'
21750       ||' AND mstp.batch_id                = :lv_batch_id'
21751       ||' AND mstp.process_flag            = '||G_IN_PROCESS ;
21752 
21753 
21754 
21755         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21756 
21757       EXECUTE IMMEDIATE lv_sql_stmt
21758               USING     v_instance_id,
21759                         v_instance_code,
21760                         lv_batch_id;
21761 
21762       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21763                      (p_app_short_name    => 'MSC',
21764                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
21765                       p_message_text      => lv_message_text,
21766                       p_error_text        => lv_error_text,
21767                       p_token1            => 'COLUMN_NAME',
21768                       p_token_value1      => 'DELETED_FLAG',
21769                       p_token2            => 'DEFAULT_VALUE',
21770                       p_token_value2      => SYS_NO);
21771 
21772       IF lv_return <> 0 THEN
21773         RAISE ex_logging_err;
21774       END IF;
21775 
21776       lv_where_str :=
21777       ' AND NVL(deleted_flag,'||NULL_VALUE||') '
21778       ||' NOT IN(1,2)';
21779       --Log a warning for those records where the deleted_flag has a value other
21780       --than SYS_NO
21781 
21782       lv_return := MSC_ST_UTIL.LOG_ERROR
21783                      (p_table_name        => 'MSC_ST_ITEM_SUPPLIERS',
21784                       p_instance_code     => v_instance_code,
21785                       p_row               => lv_column_names,
21786                       p_severity          => G_SEV_WARNING,
21787                       p_message_text      => lv_message_text,
21788                       p_error_text        => lv_error_text,
21789                       p_batch_id          => lv_batch_id,
21790                       p_where_str         => lv_where_str,
21791                       p_col_name          => 'DELETED_FLAG',
21792                       p_debug             => v_debug,
21793                       p_default_value     => SYS_NO);
21794 
21795       IF lv_return <> 0 THEN
21796         RAISE ex_logging_err;
21797       END IF;
21798 
21799       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21800                      (p_app_short_name    => 'MSC',
21801                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21802                       p_message_text      => lv_message_text,
21803                       p_error_text        => lv_error_text,
21804                       p_token1            => 'COLUMN_NAME',
21805                       p_token_value1      => 'ORGANIZATION_CODE');
21806 
21807       IF lv_return <> 0 THEN
21808         RAISE ex_logging_err;
21809       END IF;
21810 
21811       --Derive Organization_id
21812       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
21813                      (p_table_name       => 'MSC_ST_ITEM_SUPPLIERS',
21814                       p_org_partner_name => 'ORGANIZATION_CODE',
21815                       p_org_partner_id   => 'ORGANIZATION_ID',
21816                       p_instance_code    => v_instance_code,
21817                       p_partner_type     => G_ORGANIZATION,
21818                       p_error_text       => lv_error_text,
21819                       p_batch_id         => lv_batch_id,
21820                       p_severity         => G_SEV_ERROR,
21821                       p_message_text     => lv_message_text,
21822                       p_debug            => v_debug,
21823                       p_row              => lv_column_names);
21824 
21825       IF lv_return <> 0 THEN
21826         RAISE ex_logging_err;
21827       END IF;
21828 
21829       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21830                      (p_app_short_name    => 'MSC',
21831                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21832                       p_message_text      => lv_message_text,
21833                       p_error_text        => lv_error_text,
21834                       p_token1            => 'COLUMN_NAME',
21835                       p_token_value1      => 'ITEM_NAME');
21836 
21837       --Derive Inventory_item_id
21838       lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
21839                      (p_table_name       => 'MSC_ST_ITEM_SUPPLIERS',
21840                       p_item_col_name    => 'ITEM_NAME',
21841                       p_item_col_id      => 'INVENTORY_ITEM_ID',
21842                       p_instance_id      => v_instance_id,
21843                       p_instance_code    => v_instance_code,
21844                       p_error_text       => lv_error_text,
21845                       p_batch_id         => lv_batch_id,
21846                       p_severity         => G_SEV_ERROR,
21847                       p_message_text     => lv_message_text,
21848                       p_debug            => v_debug,
21849                       p_row              => lv_column_names);
21850 
21851       IF lv_return <> 0 THEN
21852         RAISE ex_logging_err;
21853       END IF;
21854 
21855       --Derive Supplier_id
21856       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21857                      (p_app_short_name    => 'MSC',
21858                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21859                       p_message_text      => lv_message_text,
21860                       p_error_text        => lv_error_text,
21861                       p_token1            => 'COLUMN_NAME',
21862                       p_token_value1      => 'VENDOR_NAME');
21863 
21864       IF lv_return <> 0 THEN
21865         RAISE ex_logging_err;
21866       END IF;
21867 
21868       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
21869                      (p_table_name       => 'MSC_ST_ITEM_SUPPLIERS',
21870                       p_org_partner_name => 'VENDOR_NAME',
21871                       p_org_partner_id   => 'SUPPLIER_ID',
21872                       p_instance_code    => v_instance_code,
21873                       p_partner_type     => G_VENDOR,
21874                       p_error_text       => lv_error_text,
21875                       p_batch_id         => lv_batch_id,
21876                       p_severity         => G_SEV_ERROR,
21877                       p_message_text     => lv_message_text,
21878                       p_debug            => v_debug,
21879                       p_row              => lv_column_names);
21880 
21881       IF lv_return <> 0 THEN
21882         RAISE ex_logging_err;
21883       END IF;
21884 
21885       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21886                      (p_app_short_name    => 'MSC',
21887                       p_error_code        => 'MSC_PP_INVALID_VALUE',
21888                       p_message_text      => lv_message_text,
21889                       p_error_text        => lv_error_text,
21890                       p_token1            => 'COLUMN_NAME',
21891                       p_token_value1      => 'VENDOR_SITE_CODE');
21892 
21893       IF lv_return <> 0 THEN
21894         RAISE ex_logging_err;
21895       END IF;
21896 
21897       --Derive Supplier_site_id
21898        lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
21899                      (p_table_name        => 'MSC_ST_ITEM_SUPPLIERS',
21900                       p_partner_name      => 'VENDOR_NAME',
21901                       p_partner_site_code => 'VENDOR_SITE_CODE',
21902                       p_partner_site_id   => 'SUPPLIER_SITE_ID',
21903                       p_instance_code     => v_instance_code,
21904                       p_partner_type      => G_VENDOR,
21905                       p_error_text        => lv_error_text,
21906                       p_batch_id          => lv_batch_id,
21907                       p_severity          => G_SEV3_ERROR,
21908                       p_message_text      => lv_message_text,
21909                       p_debug             => v_debug,
21910                       p_row               => lv_column_names);
21911 
21912       IF lv_return <> 0 THEN
21913         RAISE ex_logging_err;
21914       END IF;
21915      /* Bug 2172537
21916       -- Update  all calendar_code as instance_code concatenated with name
21917       v_sql_stmt := 12;
21918       lv_sql_stmt :=
21919       'UPDATE   msc_st_item_suppliers '
21920       ||' SET   delivery_calendar_code     = sr_instance_code'||'||'':''||'||'delivery_calendar_code'
21921       ||' WHERE delivery_calendar_code IS NOT NULL'
21922       ||' AND   sr_instance_code  = :v_instance_code'
21923       ||' AND   batch_id          = :lv_batch_id'
21924       ||' AND   process_flag      = '||G_IN_PROCESS;
21925 
21926 
21927         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21928       EXECUTE IMMEDIATE lv_sql_stmt
21929               USING     v_instance_code,
21930                         lv_batch_id;  */
21931 
21932 
21933       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21934                      (p_app_short_name    => 'MSC',
21935                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
21936                       p_message_text      => lv_message_text,
21937                       p_error_text        => lv_error_text,
21938                       p_token1            => 'COLUMN_NAMES',
21939                       p_token_value1      => 'SR_INSTANCE_ID AND CALENDAR_CODE',
21940                       p_token2            => 'MASTER_TABLE',
21941                       p_token_value2      => 'MSC_CALENDARS',
21942                       p_token3            => 'CHILD_TABLE',
21943                       p_token_value3      => 'MSC_ST_ITEM_SUPPLIERS');
21944       IF lv_return <> 0 THEN
21945         RAISE ex_logging_err;
21946       END IF;
21947 
21948       -- Validate the calendar code for org
21949       v_sql_stmt := 14;
21950       lv_sql_stmt :=
21951       'UPDATE    msc_st_item_suppliers   mis'
21952       ||' SET    process_flag             =  '||G_ERROR_FLG||','
21953       ||'         error_text   = '||''''||lv_message_text||''''
21954       ||' WHERE  NOT EXISTS ( SELECT 1 '
21955       ||'        FROM   msc_calendars mc '
21956       ||'        WHERE  mc.calendar_code  = mis.delivery_calendar_code '
21957       ||'        AND    mc.sr_instance_id = :v_instance_id )'
21958       ||' AND    delivery_calendar_code IS NOT NULL '
21959       ||' AND    sr_instance_code         = :v_instance_code'
21960       ||' AND    batch_id                 = :lv_batch_id'
21961       ||' AND    process_flag             = '||G_IN_PROCESS;
21962 
21963 
21964         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21965 
21966       EXECUTE IMMEDIATE lv_sql_stmt
21967               USING     v_instance_id,
21968                         v_instance_code,
21969                         lv_batch_id;
21970 
21971       --Setting using_organization_id to the default value -1 which is for the
21972       --globally valid supplier rules and to organization_id for the local ASL's.
21973       --Value of sr_instance_id2 should be same as sr_instance_id, Since, we are
21974       --always considering using_organization_id =-1.
21975 
21976 
21977       v_sql_stmt := 08;
21978 
21979       lv_sql_stmt :=
21980       'UPDATE  msc_st_item_suppliers mis '
21981       ||' SET  using_organization_id = decode(asl_level,1,'||G_USING_ORG_ID||',organization_id),'
21982       ||'      sr_instance_id2   = :v_instance_id'
21983       ||' WHERE sr_instance_code = :v_instance_code'
21984       ||' AND   batch_id         = :lv_batch_id'
21985       ||' AND   process_flag     = '||G_IN_PROCESS;
21986 
21987 
21988         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
21989 
21990       EXECUTE IMMEDIATE lv_sql_stmt
21991               USING     v_instance_id,
21992                         v_instance_code,
21993                         lv_batch_id;
21994 
21995      -- validation for REPLENISHMENT_METHOD
21996      -- Set the error message
21997     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
21998                      (p_app_short_name    => 'MSC',
21999                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
22000                       p_message_text      => lv_message_text,
22001                       p_error_text        => lv_error_text,
22002                       p_token1            => 'COLUMN_NAME',
22003                       p_token_value1      => 'REPLENISHMENT_METHOD',
22004                       p_token2            => 'DEFAULT_VALUE',
22005                       p_token_value2      =>  1  );
22006 
22007       IF lv_return <> 0 THEN
22008         RAISE ex_logging_err;
22009       END IF;
22010 
22011      -- Check whether REPLENISHMENT_METHOD value is in (1,2,3,4),if not default to 1 - MIN-MAX - QUANTITIES
22012 
22013    lv_where_str :=
22014    ' AND NVL(REPLENISHMENT_METHOD,'||NULL_VALUE||') NOT IN(1,2,3,4)';
22015 
22016      lv_return := MSC_ST_UTIL.LOG_ERROR
22017                     (p_table_name        => 'MSC_ST_ITEM_SUPPLIERS',
22018                      p_instance_code     => v_instance_code,
22019                      p_row               => lv_column_names,
22020                      p_severity          => G_SEV_WARNING,
22021                      p_message_text      => lv_message_text,
22022                      p_error_text        => lv_error_text,
22023                      p_batch_id          => lv_batch_id,
22024                      p_where_str         => lv_where_str,
22025                      p_col_name          => 'REPLENISHMENT_METHOD',
22026                      p_debug             => v_debug,
22027                      p_default_value     => 1);
22028 
22029       IF lv_return <> 0 THEN
22030         RAISE ex_logging_err;
22031       END IF;
22032 
22033 
22034 
22035       --Call to customised validation.
22036       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
22037         (ERRBUF         => lv_error_text,
22038          RETCODE        => lv_return,
22039          pBatchID       => lv_batch_id,
22040          pInstanceCode  => v_instance_code,
22041          pEntityName    => 'MSC_ST_ITEM_SUPPLIERS',
22042          pInstanceID    => v_instance_id);
22043 
22044       IF NVL(lv_return,0) <> 0 THEN
22045         RAISE ex_logging_err;
22046       END IF;
22047 
22048      -- SCE Changes Starts here
22049 
22050    IF v_sce_installed THEN
22051 
22052       -- company_id derivation
22053       -- derive the company_id . if not found then error out record
22054 
22055       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22056                      (p_app_short_name    => 'MSC',
22057                       p_error_code        => 'MSC_PP_INVALID_VALUE',
22058                       p_message_text      => lv_message_text,
22059                       p_error_text        => lv_error_text,
22060                       p_token1            => 'COLUMN_NAME',
22061                       p_token_value1      => 'COMPANY_NAME');
22062 
22063       IF lv_return <> 0 THEN
22064         RAISE ex_logging_err;
22065       END IF;
22066 
22067       lv_return := MSC_ST_UTIL.DERIVE_COMPANY_ID
22068                         (p_table_name     => 'MSC_ST_ITEM_SUPPLIERS',
22069                          p_company_name   => 'COMPANY_NAME',
22070                          p_company_id     => 'COMPANY_ID',
22071                          p_instance_code  => v_instance_code,
22072                          p_error_text     => lv_error_text,
22073                          p_batch_id       => lv_batch_id,
22074                          p_message_text   => lv_message_text,
22075                          p_debug          => v_debug);
22076 
22077       IF lv_return <> 0 THEN
22078         RAISE ex_logging_err;
22079       END IF;
22080 
22081 /*
22082       lv_sql_stmt :=
22083       ' UPDATE  MSC_ST_ITEM_SUPPLIERS mic '
22084       ||' SET   company_id        =   '
22085       ||'       (select company_id from msc_companies mc'
22086       ||'        where mic.company_name = mc.company_name) '
22087       ||' WHERE NVL(COMPANY_NAME,''-1'')   <> ''-1'''
22088       ||' AND   batch_id         = :lv_batch_id'
22089       ||' AND   process_flag     = '||G_IN_PROCESS
22090       ||' AND   sr_instance_code = :v_instance_code';
22091 
22092 
22093         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22094 
22095       EXECUTE IMMEDIATE lv_sql_stmt
22096               USING     lv_batch_id,
22097                         v_instance_code;
22098 
22099       lv_sql_stmt :=
22100       ' UPDATE  MSC_ST_ITEM_SUPPLIERS '
22101       ||' SET   process_flag        = '||G_ERROR_FLG||','
22102       ||'       error_text          = '||''''||lv_message_text||''''
22103       ||' WHERE NVL(COMPANY_NAME,''-1'')   <> ''-1'''
22104       ||' AND   COMPANY_ID IS NULL '
22105       ||' AND   batch_id         = :lv_batch_id'
22106       ||' AND   process_flag     = '||G_IN_PROCESS
22107       ||' AND   sr_instance_code = :v_instance_code';
22108 
22109 
22110         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22111 
22112       EXECUTE IMMEDIATE lv_sql_stmt
22113               USING     lv_batch_id,
22114                         v_instance_code;
22115 */
22116 
22117    END IF; -- SCE installed
22118 
22119    -- SCE Changes end here
22120 
22121       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
22122                   (p_table_name     => 'MSC_ST_ITEM_SUPPLIERS',
22123                    p_instance_id    => v_instance_id,
22124                    p_instance_code  => v_instance_code,
22125                    p_process_flag   => G_VALID,
22126                    p_error_text     => lv_error_text,
22127                    p_debug          => v_debug,
22128                    p_batch_id       => lv_batch_id);
22129 
22130       IF lv_return <> 0 THEN
22131         RAISE ex_logging_err;
22132       END IF;
22133 
22134       lv_return := MSC_ST_UTIL.LOG_ERROR
22135                    (p_table_name        => 'MSC_ST_ITEM_SUPPLIERS',
22136                     p_instance_code     => v_instance_code,
22137                     p_row               => lv_column_names,
22138                     p_severity          => G_SEV_ERROR,
22139                     p_message_text      => NULL,
22140                     p_error_text        => lv_error_text,
22141                     p_debug             => v_debug,
22142                     p_batch_id          => lv_batch_id);
22143 
22144       IF lv_return <> 0 THEN
22145         RAISE ex_logging_err;
22146       END IF;
22147 
22148       COMMIT;
22149     END LOOP;
22150 
22151     --******Validation for MSC_ST_SUPPLIER_CAPACITIES***********
22152 
22153     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22154                  (p_app_short_name    => 'MSC',
22155                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
22156                   p_message_text      => lv_message_text,
22157                   p_error_text        => lv_error_text);
22158 
22159     IF lv_return <> 0 THEN
22160       RAISE ex_logging_err;
22161     END IF;
22162     --Duplicate records check for the records whose source is XML
22163     v_sql_stmt := 09;
22164     lv_sql_stmt :=
22165     'UPDATE   msc_st_supplier_capacities msc1 '
22166     ||' SET   process_flag = '||G_ERROR_FLG||','
22167     ||'      error_text   = '||''''||lv_message_text||''''
22168     ||' WHERE message_id <  (SELECT MAX(message_id)'
22169     ||'       FROM   msc_st_supplier_capacities msc2'
22170     ||'       WHERE  msc2.sr_instance_code  = msc1.sr_instance_code'
22171     ||'       AND    msc2.organization_code = msc1.organization_code'
22172     ||'       AND    msc2.vendor_name       = msc1.vendor_name'
22173     ||'       AND    msc2.vendor_site_code  = msc1.vendor_site_code'
22174     ||'       AND    msc2.item_name         = msc1.item_name'
22175     ||'       AND    msc2.from_date         = msc1.from_date'
22176     ||'       AND    NVL(msc2.company_name,  '||''''||NULL_CHAR||''''||') = '
22177     ||'              NVL(msc1.company_name,  '||''''||NULL_CHAR||''''||')'
22178     ||'       AND    msc2.process_flag      = '||G_IN_PROCESS
22179     ||'       AND    msc2.sr_instance_code  = msc1.sr_instance_code'
22180     ||'       AND    NVL(msc2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
22181     ||' AND   msc1.process_flag             = '||G_IN_PROCESS
22182     ||' AND   msc1.sr_instance_code         = :v_instance_code'
22183     ||' AND   NVL(msc1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
22184 
22185 
22186 
22187       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22188 
22189     EXECUTE IMMEDIATE lv_sql_stmt
22190             USING     v_instance_code;
22191 
22192     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22193                  (p_app_short_name    => 'MSC',
22194                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
22195                   p_message_text      => lv_message_text,
22196                   p_error_text        => lv_error_text);
22197 
22198     IF lv_return <> 0 THEN
22199       RAISE ex_logging_err;
22200     END IF;
22201 
22202     --Duplicate records check for the records whose source is other than XML
22203     --Different SQL is used because in XML we can identify the latest records
22204     --whereas in batch load we cannot.
22205 
22206     v_sql_stmt := 10;
22207     lv_sql_stmt :=
22208     ' UPDATE    msc_st_supplier_capacities msc1'
22209     ||' SET     process_flag  = '||G_ERROR_FLG||','
22210     ||'         error_text   = '||''''||lv_message_text||''''
22211     ||' WHERE   EXISTS( SELECT 1 '
22212     ||'         FROM   msc_st_supplier_capacities msc2'
22213     ||'         WHERE  msc2.sr_instance_code      = msc1.sr_instance_code'
22214     ||'         AND    msc2.organization_code     = msc1.organization_code'
22215     ||'         AND    msc2.vendor_name           = msc1.vendor_name'
22216     ||'         AND    msc2.vendor_site_code      = msc1.vendor_site_code'
22217     ||'         AND    msc2.item_name             = msc1.item_name'
22218     ||'         AND    msc2.from_date             = msc1.from_date'
22219     ||'         AND    NVL(msc2.company_name,  '||''''||NULL_CHAR||''''||') = '
22220     ||'                NVL(msc1.company_name,  '||''''||NULL_CHAR||''''||')'
22221     ||'         AND    msc2.process_flag      = '||G_IN_PROCESS
22222     ||'         AND    NVL(msc2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
22223     ||'         GROUP BY sr_instance_code,organization_code,company_name,'
22224     ||'                item_name,vendor_name,vendor_site_code,from_date'
22225     ||'         HAVING COUNT(*) > 1)'
22226     ||' AND     msc1.process_flag = '||G_IN_PROCESS
22227     ||' AND     msc1.sr_instance_code = :v_instance_code'
22228     ||' AND     NVL(msc1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
22229 
22230 
22231       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22232 
22233     EXECUTE IMMEDIATE lv_sql_stmt
22234             USING     v_instance_code;
22235 
22236     lv_column_names :=
22237     'ORGANIZATION_CODE       ||''~''||'
22238     ||'ITEM_NAME             ||''~''||'
22239     ||'VENDOR_NAME           ||''~''||'
22240     ||'FROM_DATE             ||''~''||'
22241     ||'CAPACITY              ||''~''||'
22242     ||'SR_INSTANCE_CODE      ||''~''||'
22243     ||'VENDOR_SITE_CODE      ||''~''||'
22244     ||'TO_DATE               ||''~''||'
22245     ||'DELETED_FLAG          ||''~''||'
22246     ||'COMPANY_NAME          ||''~''||'
22247     ||'USING_ORGANIZATION_ID';
22248 
22249     LOOP
22250       v_sql_stmt := 11;
22251       SELECT msc_st_batch_id_s.NEXTVAL
22252       INTO   lv_batch_id
22253       FROM   dual;
22254 
22255       v_sql_stmt := 12;
22256       lv_sql_stmt :=
22257       'UPDATE msc_st_supplier_capacities '
22258       ||' SET    batch_id  = :lv_batch_id'
22259       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
22260       ||' AND    sr_instance_code               = :v_instance_code'
22261       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
22262       ||' AND    rownum                        <= '||v_batch_size;
22263 
22264 
22265         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22266 
22267       EXECUTE IMMEDIATE lv_sql_stmt
22268               USING     lv_batch_id,
22269                         v_instance_code;
22270 
22271       EXIT WHEN SQL%NOTFOUND;
22272 
22273       OPEN c2(lv_batch_id);
22274       FETCH c2 BULK COLLECT INTO lb_rowid;
22275       CLOSE c2;
22276 
22277       v_sql_stmt := 13;
22278       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
22279       UPDATE msc_st_supplier_capacities
22280       SET    st_transaction_id   = msc_st_supplier_capacities_s.NEXTVAL,
22281              refresh_id          = v_refresh_id,
22282              last_update_date    = v_current_date,
22283              last_updated_by     = v_current_user,
22284              creation_date       = v_current_date,
22285              created_by          = v_current_user
22286       WHERE  rowid           = lb_rowid(j);
22287 
22288 
22289       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22290                      (p_app_short_name    => 'MSC',
22291                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
22292                       p_message_text      => lv_message_text,
22293                       p_error_text        => lv_error_text,
22294                       p_token1            => 'COLUMN_NAME',
22295                       p_token_value1      => 'DELETED_FLAG',
22296                       p_token2            => 'DEFAULT_VALUE',
22297                       p_token_value2      => SYS_NO);
22298 
22299       IF lv_return <> 0 THEN
22300         RAISE ex_logging_err;
22301       END IF;
22302 
22303       lv_where_str :=
22304       ' AND NVL(deleted_flag,'||NULL_VALUE||') '
22305       ||' NOT IN(1,2)';
22306       --Log a warning for those records where the deleted_flag has a value other
22307       --than SYS_NO
22308       lv_return := MSC_ST_UTIL.LOG_ERROR
22309                      (p_table_name        => 'MSC_ST_SUPPLIER_CAPACITIES',
22310                       p_instance_code     => v_instance_code,
22311                       p_row               => lv_column_names,
22312                       p_severity          => G_SEV_WARNING,
22313                       p_message_text      => lv_message_text,
22314                       p_error_text        => lv_error_text,
22315                       p_batch_id          => lv_batch_id,
22316                       p_where_str         => lv_where_str,
22317                       p_col_name          => 'DELETED_FLAG',
22318                       p_debug             => v_debug,
22319                       p_default_value     => SYS_NO);
22320 
22321       IF lv_return <> 0 THEN
22322         RAISE ex_logging_err;
22323       END IF;
22324 
22325       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22326                      (p_app_short_name    => 'MSC',
22327                       p_error_code        => 'MSC_PP_INVALID_VALUE',
22328                       p_message_text      => lv_message_text,
22329                       p_error_text        => lv_error_text,
22330                       p_token1            => 'COLUMN_NAME',
22331                       p_token_value1      => 'ORGANIZATION_CODE');
22332 
22333       --Derive Organization_id
22334       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
22335                      (p_table_name       => 'MSC_ST_SUPPLIER_CAPACITIES',
22336                       p_org_partner_name => 'ORGANIZATION_CODE',
22337                       p_org_partner_id   => 'ORGANIZATION_ID',
22338                       p_instance_code    => v_instance_code,
22339                       p_partner_type     => G_ORGANIZATION,
22340                       p_error_text       => lv_error_text,
22341                       p_batch_id         => lv_batch_id,
22342                       p_severity         => G_SEV_ERROR,
22343                       p_message_text     => lv_message_text,
22344                       p_debug            => v_debug,
22345                       p_row              => lv_column_names);
22346 
22347       IF lv_return <> 0 THEN
22348         RAISE ex_logging_err;
22349       END IF;
22350 
22351       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22352                      (p_app_short_name    => 'MSC',
22353                       p_error_code        => 'MSC_PP_INVALID_VALUE',
22354                       p_message_text      => lv_message_text,
22355                       p_error_text        => lv_error_text,
22356                       p_token1            => 'COLUMN_NAME',
22357                       p_token_value1      => 'ITEM_NAME');
22358 
22359       IF lv_return <> 0 THEN
22360         RAISE ex_logging_err;
22361       END IF;
22362 
22363       --Derive Inventory_item_id
22364       lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
22365                      (p_table_name       => 'MSC_ST_SUPPLIER_CAPACITIES',
22366                       p_item_col_name    => 'ITEM_NAME',
22367                       p_item_col_id      => 'INVENTORY_ITEM_ID',
22368                       p_instance_id      => v_instance_id,
22369                       p_instance_code    => v_instance_code,
22370                       p_error_text       => lv_error_text,
22371                       p_batch_id         => lv_batch_id,
22372                       p_severity         => G_SEV_ERROR,
22373                       p_message_text     => lv_message_text,
22374                       p_debug            => v_debug,
22375                       p_row              => lv_column_names);
22376 
22377       IF lv_return <> 0 THEN
22378         RAISE ex_logging_err;
22379       END IF;
22380 
22381       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22382                      (p_app_short_name    => 'MSC',
22383                       p_error_code        => 'MSC_PP_INVALID_VALUE',
22384                       p_message_text      => lv_message_text,
22385                       p_error_text        => lv_error_text,
22386                       p_token1            => 'COLUMN_NAME',
22387                       p_token_value1      => 'VENDOR_NAME');
22388 
22389       IF lv_return <> 0 THEN
22390         RAISE ex_logging_err;
22391       END IF;
22392 
22393       --Derive Supplier_id
22394       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
22395                      (p_table_name       => 'MSC_ST_SUPPLIER_CAPACITIES',
22396                       p_org_partner_name => 'VENDOR_NAME',
22397                       p_org_partner_id   => 'SUPPLIER_ID',
22398                       p_instance_code    => v_instance_code,
22399                       p_partner_type     => G_VENDOR,
22400                       p_error_text       => lv_error_text,
22401                       p_batch_id         => lv_batch_id,
22402                       p_severity         => G_SEV_ERROR,
22403                       p_message_text     => lv_message_text,
22404                       p_debug            => v_debug,
22405                       p_row              => lv_column_names);
22406 
22407       IF lv_return <> 0 THEN
22408         RAISE ex_logging_err;
22409       END IF;
22410 
22411       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22412                      (p_app_short_name    => 'MSC',
22413                       p_error_code        => 'MSC_PP_INVALID_VALUE',
22414                       p_message_text      => lv_message_text,
22415                       p_error_text        => lv_error_text,
22416                       p_token1            => 'COLUMN_NAME',
22417                       p_token_value1      => 'VENDOR_SITE_CODE');
22418 
22419       IF lv_return <> 0 THEN
22420         RAISE ex_logging_err;
22421       END IF;
22422 
22423      --Derive Supplier_site_id
22424       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
22425                      (p_table_name        => 'MSC_ST_SUPPLIER_CAPACITIES',
22426                       p_partner_name      => 'VENDOR_NAME',
22427                       p_partner_site_code => 'VENDOR_SITE_CODE',
22428                       p_partner_site_id   => 'SUPPLIER_SITE_ID',
22429                       p_instance_code     => v_instance_code,
22430                       p_partner_type      => G_VENDOR,
22431                       p_error_text        => lv_error_text,
22432                       p_batch_id          => lv_batch_id,
22433                       p_severity          => G_SEV_ERROR,
22434                       p_message_text      => lv_message_text,
22435                       p_debug             => v_debug,
22436                       p_row               => lv_column_names);
22437 
22438       IF lv_return <> 0 THEN
22439         RAISE ex_logging_err;
22440       END IF;
22441 
22442 
22443       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22444                      (p_app_short_name    => 'MSC',
22445                       p_error_code        => 'MSC_PP_SUPP_NOT_EXIST',
22446                       p_message_text      => lv_message_text,
22447                       p_error_text        => lv_error_text,
22448                       p_token1            => 'COLUMN_NAMES',
22449                       p_token_value1      => 'SR_INSTANCE_CODE, COMPANY_NAME, ORGANIZATION_CODE,'
22450                                              ||' ITEM_NAME, VENDOR_NAME AND VENDOR_SITE_CODE',
22451                       p_token2            => 'MASTER_TABLE',
22452                       p_token_value2      => 'MSC_ST_ITEM_SUPPLIERS',
22453                       p_token3            => 'CHILD_TABLE',
22454                       p_token_value3      => 'MSC_ST_SUPPLIER_CAPACITIES',
22455                       p_token4            => 'VALUE',
22456                       p_token_value4      => 'ASL_LEVEL = 1');
22457       IF lv_return <> 0 THEN
22458         RAISE ex_logging_err;
22459       END IF;
22460 
22461       --Checking for the referential integrity.
22462       v_sql_stmt := 14;
22463       lv_sql_stmt :=
22464       'UPDATE msc_st_supplier_capacities mis1 '
22465       ||' SET   error_text   = '||''''||lv_message_text||''''||','
22466       ||'     process_flag = '||G_ERROR_FLG
22467       ||' WHERE NOT EXISTS(SELECT 1'
22468       ||'       FROM   msc_item_suppliers mis2, MSC_TP_ID_LID mtil, MSC_TP_SITE_ID_LID mtsil, MSC_ITEM_ID_LID mil'
22469       ||'       WHERE  mtil.SR_TP_ID  = mis1.supplier_id'
22470       ||'       AND    mtil.TP_ID     = mis2.supplier_id'
22471       ||'       AND    mtil.sr_instance_id       = mis2.sr_instance_id'
22472       ||'       AND    mtil.PARTNER_TYPE         = 1'
22473       ||'       AND    mtsil.SR_TP_SITE_ID       = mis1.supplier_site_id'
22474       ||'       AND    mtsil.TP_SITE_ID          = mis2.supplier_site_id'
22475       ||'       AND    mtsil.sr_instance_id      = mis2.sr_instance_id'
22476       ||'       AND    mtsil.partner_type        = 1'
22477       ||'       AND    mil.sr_inventory_item_id  = mis1.inventory_item_id'
22478       ||'       AND    mil.inventory_item_id     = mis2.inventory_item_id'
22479       ||'       AND    mil.sr_instance_id        = mis2.sr_instance_id'
22480       ||'       AND    mis2.sr_instance_id       = :v_instance_id'
22481       ||'       AND    mis2.organization_id      = mis1.organization_id'
22482       ||'       AND    mis2.using_organization_id = -1'
22483       ||'       AND    mis2.plan_id              = -1'
22484       ||'       UNION'
22485       ||'       SELECT 1'
22486       ||'       FROM   msc_st_item_suppliers mis3'
22487       ||'       WHERE  mis3.vendor_name       = mis1.vendor_name'
22488       ||'       AND    mis3.vendor_site_code  = mis1.vendor_site_code'
22489       ||'       AND    mis3.item_name         = mis1.item_name'
22490       ||'       AND    mis3.sr_instance_code  = mis1.sr_instance_code'
22491       ||'       AND    NVL(mis3.company_name,'||''''||NULL_CHAR||''''||') = '
22492       ||'              NVL(mis1.company_name,'||''''||NULL_CHAR||''''||') '
22493       ||'       AND    mis3.organization_code = mis1.organization_code'
22494       ||'       AND    mis3.using_organization_id = -1'
22495       ||'       AND    mis3.process_flag      = '||G_VALID||')'
22496       ||' AND    sr_instance_code       = :v_instance_code'
22497       ||' AND    batch_id               = :lv_batch_id'
22498       ||' AND    process_flag           = '||G_IN_PROCESS;
22499 
22500 
22501 
22502         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22503 
22504       EXECUTE IMMEDIATE lv_sql_stmt
22505               USING     v_instance_id,
22506                         v_instance_code,
22507                         lv_batch_id;
22508 
22509       --Setting using_organization_id to the default value -1 which is for the
22510       --globally valid supplier rules.
22511       v_sql_stmt := 15;
22512 
22513       lv_sql_stmt :=
22514       'UPDATE  msc_st_supplier_capacities mis '
22515       ||' SET     using_organization_id  = '||G_USING_ORG_ID
22516       ||' WHERE   sr_instance_code       = :v_instance_code'
22517       ||' AND     batch_id               = :lv_batch_id'
22518       ||' AND     process_flag           = '||G_IN_PROCESS
22519       ||' AND     deleted_flag           = '||SYS_NO;
22520 
22521 
22522         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22523 
22524       EXECUTE IMMEDIATE lv_sql_stmt
22525               USING     v_instance_code,
22526                         lv_batch_id;
22527 
22528       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22529                      (p_app_short_name    => 'MSC',
22530                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
22531                       p_message_text      => lv_message_text,
22532                       p_error_text        => lv_error_text,
22533                       p_token1            => 'COLUMN_NAME',
22534                       p_token_value1      => 'FROM_DATE');
22535 
22536       IF lv_return <> 0 THEN
22537         RAISE ex_logging_err;
22538       END IF;
22539 
22540       --From_date cannot be null.
22541       v_sql_stmt := 16;
22542 
22543       lv_sql_stmt :=
22544       'UPDATE  msc_st_supplier_capacities mis '
22545       ||' SET   error_text   = '||''''||lv_message_text||''''||','
22546       ||'      process_flag      = '||G_ERROR_FLG
22547       ||' WHERE NVL(from_date,(sysdate-36500)) = (sysdate-36500)'
22548       ||' AND   deleted_flag     = '||SYS_NO
22549       ||' AND   process_flag     = '||G_IN_PROCESS
22550       ||' AND   batch_id         = :lv_batch_id'
22551       ||' AND   sr_instance_code = :v_instance_code';
22552 
22553 
22554         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22555 
22556       EXECUTE IMMEDIATE lv_sql_stmt
22557               USING     lv_batch_id,
22558                         v_instance_code;
22559 
22560 
22561       --Call to customised validation.
22562       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
22563         (ERRBUF         => lv_error_text,
22564          RETCODE        => lv_return,
22565          pBatchID       => lv_batch_id,
22566          pInstanceCode  => v_instance_code,
22567          pEntityName    => 'MSC_ST_SUPPLIER_CAPACITIES',
22568          pInstanceID    => v_instance_id);
22569 
22570       IF NVL(lv_return,0) <> 0 THEN
22571         RAISE ex_logging_err;
22572       END IF;
22573 
22574       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
22575                   (p_table_name     => 'MSC_ST_SUPPLIER_CAPACITIES',
22576                    p_instance_id    => v_instance_id,
22577                    p_instance_code  => v_instance_code,
22578                    p_process_flag   => G_VALID,
22579                    p_error_text     => lv_error_text,
22580                    p_debug          => v_debug,
22581                    p_batch_id       => lv_batch_id);
22582 
22583       IF lv_return <> 0 THEN
22584         RAISE ex_logging_err;
22585       END IF;
22586 
22587       lv_return := MSC_ST_UTIL.LOG_ERROR
22588                    (p_table_name        => 'MSC_ST_SUPPLIER_CAPACITIES',
22589                     p_instance_code     => v_instance_code,
22590                     p_row               => lv_column_names,
22591                     p_severity          => G_SEV_ERROR,
22592                     p_message_text      => NULL,
22593                     p_error_text        => lv_error_text,
22594                     p_debug             => v_debug,
22595                     p_batch_id          => lv_batch_id);
22596 
22597       IF lv_return <> 0 THEN
22598         RAISE ex_logging_err;
22599       END IF;
22600       COMMIT;
22601     END LOOP;
22602 
22603     --*********Validation for MSC_ST_SUPPLIER_FLEX_FENCES***********
22604 
22605     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22606                  (p_app_short_name    => 'MSC',
22607                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
22608                   p_message_text      => lv_message_text,
22609                   p_error_text        => lv_error_text);
22610 
22611     IF lv_return <> 0 THEN
22612       RAISE ex_logging_err;
22613     END IF;
22614 
22615     --Duplicate records check for the records whose source is XML
22616     v_sql_stmt := 17;
22617     lv_sql_stmt :=
22618     'UPDATE  msc_st_supplier_flex_fences msf1 '
22619     ||' SET     process_flag = '||G_ERROR_FLG||','
22620     ||'         error_text   = '||''''||lv_message_text||''''
22621     ||' WHERE   message_id <  (SELECT MAX(message_id)'
22622     ||'         FROM   msc_st_supplier_flex_fences msf2'
22623     ||'         WHERE  msf2.sr_instance_code  = msf1.sr_instance_code'
22624     ||'         AND    msf2.organization_code = msf1.organization_code'
22625     ||'         AND    NVL(msf2.company_name,  '||''''||NULL_CHAR||''''||') = '
22626     ||'                NVL(msf1.company_name,  '||''''||NULL_CHAR||''''||')'
22627     ||'         AND    msf2.vendor_name       = msf1.vendor_name'
22628     ||'         AND    msf2.vendor_site_code  = msf1.vendor_site_code'
22629     ||'         AND    msf2.item_name         = msf1.item_name'
22630     ||'         AND    msf2.fence_days        = msf1.fence_days'
22631     ||'         AND    msf2.process_flag      = '||G_IN_PROCESS
22632     ||'         AND    NVL(msf2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
22633     ||' AND     msf1.process_flag             = '||G_IN_PROCESS
22634     ||' AND     msf1.sr_instance_code         = :v_instance_code'
22635     ||' AND     NVL(msf1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
22636 
22637 
22638       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22639 
22640     EXECUTE IMMEDIATE lv_sql_stmt
22641             USING     v_instance_code;
22642 
22643     --Duplicate records check for the records whose source is other than XML
22644     --Different SQL is used because in XML we can identify the latest records
22645     --whereas in batch load we cannot.
22646 
22647     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22648                  (p_app_short_name    => 'MSC',
22649                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
22650                   p_message_text      => lv_message_text,
22651                   p_error_text        => lv_error_text);
22652 
22653     IF lv_return <> 0 THEN
22654       RAISE ex_logging_err;
22655     END IF;
22656 
22657     v_sql_stmt := 18;
22658     lv_sql_stmt :=
22659     'UPDATE   msc_st_supplier_flex_fences msf1 '
22660     ||' SET   process_flag  = '||G_ERROR_FLG||','
22661     ||'         error_text   = '||''''||lv_message_text||''''
22662     ||' WHERE EXISTS( SELECT 1 '
22663     ||'       FROM   msc_st_supplier_flex_fences msf2'
22664     ||'       WHERE  msf2.sr_instance_code    = msf1.sr_instance_code'
22665     ||'       AND    msf2.organization_code   = msf1.organization_code'
22666     ||'       AND    NVL(msf2.company_name,  '||''''||NULL_CHAR||''''||') = '
22667     ||'              NVL(msf1.company_name,  '||''''||NULL_CHAR||''''||')'
22668     ||'       AND    msf2.vendor_name         = msf1.vendor_name'
22669     ||'       AND    msf2.vendor_site_code    = msf1.vendor_site_code'
22670     ||'       AND    msf2.item_name           = msf1.item_name'
22671     ||'       AND    msf2.fence_days          = msf1.fence_days'
22672     ||'       AND    msf2.process_flag      = '||G_IN_PROCESS
22673     ||'       AND    NVL(msf2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
22674     ||'       GROUP BY sr_instance_code,organization_code,company_name,'
22675     ||'              item_name,vendor_name,vendor_site_code,fence_days'
22676     ||'       HAVING COUNT(*) > 1)'
22677     ||' AND   msf1.process_flag             ='|| G_IN_PROCESS
22678     ||' AND   msf1.sr_instance_code         = :v_instance_code'
22679     ||' AND   NVL(msf1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
22680 
22681 
22682       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22683 
22684     EXECUTE IMMEDIATE lv_sql_stmt
22685             USING     v_instance_code;
22686 
22687     lv_column_names :=
22688     'ORGANIZATION_CODE      ||''~''||'
22689     ||'VENDOR_NAME          ||''~''||'
22690     ||'FENCE_DAYS           ||''~''||'
22691     ||'SR_INSTANCE_CODE     ||''~''||'
22692     ||'ITEM_NAME            ||''~''||'
22693     ||'VENDOR_SITE_CODE     ||''~''||'
22694     ||'TOLERANCE_PERCENTAGE ||''~''||'
22695     ||'DELETED_FLAG         ||''~''||'
22696     ||'COMPANY_NAME         ||''~''||'
22697     ||'USING_ORGANIZATION_ID';
22698 
22699     LOOP
22700       v_sql_stmt := 19;
22701       SELECT msc_st_batch_id_s.NEXTVAL
22702       INTO   lv_batch_id
22703       FROM   dual;
22704 
22705       v_sql_stmt := 20;
22706       lv_sql_stmt :=
22707       'UPDATE msc_st_supplier_flex_fences '
22708       ||' SET    batch_id  = :lv_batch_id'
22709       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
22710       ||' AND    sr_instance_code               = :v_instance_code'
22711       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
22712       ||' AND    rownum                        <= '||v_batch_size;
22713 
22714 
22715         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22716 
22717       EXECUTE IMMEDIATE lv_sql_stmt
22718               USING     lv_batch_id,
22719                         v_instance_code;
22720 
22721       EXIT WHEN SQL%NOTFOUND;
22722 
22723       OPEN c3(lv_batch_id);
22724       FETCH c3 BULK COLLECT INTO lb_rowid;
22725       CLOSE c3;
22726 
22727       v_sql_stmt := 21;
22728       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
22729       UPDATE msc_st_supplier_flex_fences
22730       SET    st_transaction_id   = msc_st_supplier_flex_fences_s.NEXTVAL,
22731              refresh_id          = v_refresh_id,
22732              last_update_date    = v_current_date,
22733              last_updated_by     = v_current_user,
22734              creation_date       = v_current_date,
22735              created_by          = v_current_user
22736       WHERE  rowid           = lb_rowid(j);
22737 
22738 
22739       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22740                      (p_app_short_name    => 'MSC',
22741                       p_error_code        => 'MSC_PP_NO_DELETION',
22742                       p_message_text      => lv_message_text,
22743                       p_error_text        => lv_error_text,
22744                       p_token1            => 'TABLE_NAME',
22745                       p_token_value1      => 'MSC_ST_SUPPLIER_FLEX_FENCES');
22746 
22747       IF lv_return <> 0 THEN
22748         RAISE ex_logging_err;
22749       END IF;
22750 
22751       --Deletion is not allowed on this table.
22752       v_sql_stmt := 22;
22753       lv_sql_stmt :=
22754       'UPDATE    msc_st_supplier_flex_fences'
22755       ||' SET    process_flag      = '||G_ERROR_FLG||','
22756       ||'         error_text   = '||''''||lv_message_text||''''
22757       ||' WHERE  deleted_flag     = '||SYS_YES
22758       ||' AND    process_flag     = '||G_IN_PROCESS
22759       ||' AND    batch_id         =  :lv_batch_id'
22760       ||' AND    sr_instance_code = :v_instance_code';
22761 
22762 
22763         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22764 
22765       EXECUTE IMMEDIATE lv_sql_stmt
22766               USING     lv_batch_id,
22767                         v_instance_code;
22768 
22769       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22770                      (p_app_short_name    => 'MSC',
22771                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
22772                       p_message_text      => lv_message_text,
22773                       p_error_text        => lv_error_text,
22774                       p_token1            => 'COLUMN_NAME',
22775                       p_token_value1      => 'DELETED_FLAG',
22776                       p_token2            => 'DEFAULT_VALUE',
22777                       p_token_value2      => SYS_NO);
22778 
22779       IF lv_return <> 0 THEN
22780         RAISE ex_logging_err;
22781       END IF;
22782 
22783       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
22784                       ||' NOT IN(1,2)';
22785 
22786       --Log a warning for those records where the deleted_flag has a value other
22787       --than SYS_NO
22788       lv_return := MSC_ST_UTIL.LOG_ERROR
22789                      (p_table_name        => 'MSC_ST_SUPPLIER_FLEX_FENCES',
22790                       p_instance_code     => v_instance_code,
22791                       p_row               => lv_column_names,
22792                       p_severity          => G_SEV_WARNING,
22793                       p_message_text      => lv_message_text,
22794                       p_error_text        => lv_error_text,
22795                       p_batch_id          => lv_batch_id,
22796                       p_where_str         => lv_where_str,
22797                       p_col_name          => 'deleted_FLAG',
22798                       p_debug             => v_debug,
22799                       p_default_value     => SYS_NO);
22800 
22801       IF lv_return <> 0 THEN
22802         RAISE ex_logging_err;
22803       END IF;
22804 
22805       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22806                      (p_app_short_name    => 'MSC',
22807                       p_error_code        => 'MSC_PP_INVALID_VALUE',
22808                       p_message_text      => lv_message_text,
22809                       p_error_text        => lv_error_text,
22810                       p_token1            => 'COLUMN_NAME',
22811                       p_token_value1      => 'ORGANIZATION_CODE');
22812 
22813       IF lv_return <> 0 THEN
22814         RAISE ex_logging_err;
22815       END IF;
22816 
22817       --Derive Organization_id
22818       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
22819                      (p_table_name       => 'MSC_ST_SUPPLIER_FLEX_FENCES',
22820                       p_org_partner_name => 'ORGANIZATION_CODE',
22821                       p_org_partner_id   => 'ORGANIZATION_ID',
22822                       p_instance_code    => v_instance_code,
22823                       p_partner_type     => G_ORGANIZATION,
22824                       p_error_text       => lv_error_text,
22825                       p_batch_id         => lv_batch_id,
22826                       p_severity         => G_SEV_ERROR,
22827                       p_message_text     => lv_message_text,
22828                       p_debug            => v_debug,
22829                       p_row              => lv_column_names);
22830 
22831       IF lv_return <> 0 THEN
22832         RAISE ex_logging_err;
22833       END IF;
22834 
22835       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22836                      (p_app_short_name    => 'MSC',
22837                       p_error_code        => 'MSC_PP_INVALID_VALUE',
22838                       p_message_text      => lv_message_text,
22839                       p_error_text        => lv_error_text,
22840                       p_token1            => 'COLUMN_NAME',
22841                       p_token_value1      => 'ITEM_NAME');
22842 
22843       IF lv_return <> 0 THEN
22844         RAISE ex_logging_err;
22845       END IF;
22846 
22847       --Derive Inventory_item_id
22848       lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
22849                      (p_table_name       => 'MSC_ST_SUPPLIER_FLEX_FENCES',
22850                       p_item_col_name    => 'ITEM_NAME',
22851                       p_item_col_id      => 'INVENTORY_ITEM_ID',
22852                       p_instance_id      => v_instance_id,
22853                       p_instance_code    => v_instance_code,
22854                       p_error_text       => lv_error_text,
22855                       p_batch_id         => lv_batch_id,
22856                       p_severity         => G_SEV_ERROR,
22857                       p_message_text     => lv_message_text,
22858                       p_debug            => v_debug,
22859                       p_row              => lv_column_names);
22860 
22861       IF lv_return <> 0 THEN
22862         RAISE ex_logging_err;
22863       END IF;
22864 
22865       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22866                      (p_app_short_name    => 'MSC',
22867                       p_error_code        => 'MSC_PP_INVALID_VALUE',
22868                       p_message_text      => lv_message_text,
22869                       p_error_text        => lv_error_text,
22870                       p_token1            => 'COLUMN_NAME',
22871                       p_token_value1      => 'VENDOR_NAME');
22872 
22873       IF lv_return <> 0 THEN
22874         RAISE ex_logging_err;
22875       END IF;
22876 
22877       --Derive Supplier_id
22878       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
22879                      (p_table_name       => 'MSC_ST_SUPPLIER_FLEX_FENCES',
22880                       p_org_partner_name => 'VENDOR_NAME',
22881                       p_org_partner_id   => 'SUPPLIER_ID',
22882                       p_instance_code    => v_instance_code,
22883                       p_partner_type     => G_VENDOR,
22884                       p_error_text       => lv_error_text,
22885                       p_batch_id         => lv_batch_id,
22886                       p_severity         => G_SEV_ERROR,
22887                       p_message_text     => lv_message_text,
22888                       p_debug            => v_debug,
22889                       p_row              => lv_column_names);
22890 
22891       IF lv_return <> 0 THEN
22892         RAISE ex_logging_err;
22893       END IF;
22894 
22895       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22896                      (p_app_short_name    => 'MSC',
22897                       p_error_code        => 'MSC_PP_INVALID_VALUE',
22898                       p_message_text      => lv_message_text,
22899                       p_error_text        => lv_error_text,
22900                       p_token1            => 'COLUMN_NAME',
22901                       p_token_value1      => 'VENDOR_SITE_CODE');
22902 
22903       IF lv_return <> 0 THEN
22904         RAISE ex_logging_err;
22905       END IF;
22906 
22907       --Derive Supplier_site_id
22908       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
22909                      (p_table_name       => 'MSC_ST_SUPPLIER_FLEX_FENCES',
22910                       p_partner_name     => 'VENDOR_NAME',
22911                       p_partner_site_code=> 'VENDOR_SITE_CODE',
22912                       p_partner_site_id  => 'SUPPLIER_SITE_ID',
22913                       p_instance_code    => v_instance_code,
22914                       p_partner_type     => G_VENDOR,
22915                       p_error_text       => lv_error_text,
22916                       p_batch_id         => lv_batch_id,
22917                       p_severity         => G_SEV_ERROR,
22918                       p_message_text     => lv_message_text,
22919                       p_debug            => v_debug,
22920                       p_row              => lv_column_names);
22921 
22922       IF lv_return <> 0 THEN
22923         RAISE ex_logging_err;
22924       END IF;
22925 
22926       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
22927                      (p_app_short_name    => 'MSC',
22928                       p_error_code        => 'MSC_PP_SUPP_NOT_EXIST',
22929                       p_message_text      => lv_message_text,
22930                       p_error_text        => lv_error_text,
22931                       p_token1            => 'COLUMN_NAMES',
22932                       p_token_value1      => 'SR_INSTANCE_CODE, COMPANY_NAME, ORGANIZATION_CODE,'
22933                                              ||' ITEM_NAME, VENDOR_NAME AND VENDOR_SITE_CODE',
22934                       p_token2            => 'MASTER_TABLE',
22935                       p_token_value2      => 'MSC_ST_ITEM_SUPPLIERS',
22936                       p_token3            => 'CHILD_TABLE',
22937                       p_token_value3      => 'MSC_ST_SUPPLIER_FLEX_FENCES ',
22938                       p_token4            => 'VALUE',
22939                       p_token_value4      => 'ASL_LEVEL = 1');
22940       IF lv_return <> 0 THEN
22941         RAISE ex_logging_err;
22942       END IF;
22943 
22944       --Checking for the referential integrity.
22945       v_sql_stmt := 23;
22946       lv_sql_stmt :=
22947       'UPDATE  msc_st_supplier_flex_fences msf1'
22948       ||' SET   error_text   = '||''''||lv_message_text||''''||','
22949       ||'      process_flag = '||G_ERROR_FLG
22950       ||' WHERE NOT EXISTS(SELECT 1'
22951       ||'       FROM   msc_item_suppliers msf2, MSC_TP_ID_LID mtil, MSC_TP_SITE_ID_LID mtsil, MSC_ITEM_ID_LID mil'
22952       ||'       WHERE  mtil.SR_TP_ID  = msf1.supplier_id'
22953       ||'       AND    mtil.TP_ID     = msf2.supplier_id'
22954       ||'       AND    mtil.sr_instance_id       = msf2.sr_instance_id'
22955       ||'       AND    mtil.PARTNER_TYPE         = 1'
22956       ||'       AND    mtsil.SR_TP_SITE_ID       = msf1.supplier_site_id'
22957       ||'       AND    mtsil.TP_SITE_ID          = msf2.supplier_site_id'
22958       ||'       AND    mtsil.sr_instance_id      = msf2.sr_instance_id'
22959       ||'       AND    mtsil.partner_type        = 1'
22960       ||'       AND    mil.sr_inventory_item_id  = msf1.inventory_item_id'
22961       ||'       AND    mil.inventory_item_id     = msf2.inventory_item_id'
22962       ||'       AND    mil.sr_instance_id        = msf2.sr_instance_id'
22963       ||'       AND    msf2.sr_instance_id       = :v_instance_id'
22964       ||'       AND    msf2.organization_id      = msf1.organization_id'
22965       ||'       AND    msf2.using_organization_id = -1'
22966       ||'       AND    msf2.plan_id              = -1'
22967       ||'       UNION'
22968       ||'       SELECT 1'
22969       ||'       FROM   msc_st_item_suppliers msf3'
22970       ||'       WHERE  msf3.vendor_name        = msf1.vendor_name'
22971       ||'       AND    msf3.vendor_site_code   = msf1.vendor_site_code'
22972       ||'       AND    msf3.item_name          = msf1.item_name'
22973       ||'       AND    msf3.sr_instance_code   = msf1.sr_instance_code'
22974       ||'       AND    NVL(msf3.company_name,'||G_COMPANY_ID||') = '
22975       ||'              NVL(msf1.company_name,'||G_COMPANY_ID||')'
22976       ||'       AND    msf3.organization_code  = msf1.organization_code'
22977       ||'       AND    msf3.using_organization_id = -1'
22978       ||'       AND    msf3.process_flag       = '||G_VALID||')'
22979       ||' AND   msf1.sr_instance_code          = :v_instance_code'
22980       ||' AND   msf1.batch_id                  = :lv_batch_id'
22981       ||' AND   msf1.process_flag              = '||G_IN_PROCESS;
22982 
22983 
22984         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
22985 
22986       EXECUTE IMMEDIATE lv_sql_stmt
22987               USING     v_instance_id,
22988                         v_instance_code,
22989                         lv_batch_id;
22990 
22991       --Setting using_organization_id to the default value -1 which is for the
22992       --globally valid supplier rules.
22993       v_sql_stmt := 24;
22994       lv_sql_stmt :=
22995       'UPDATE    msc_st_supplier_flex_fences mis '
22996       ||' SET    using_organization_id  = '||G_USING_ORG_ID
22997       ||' WHERE  deleted_flag           = '||SYS_NO
22998       ||' AND    process_flag           = '||G_IN_PROCESS
22999       ||' AND    batch_id               = :lv_batch_id'
23000       ||' AND    sr_instance_code       = :v_instance_code';
23001 
23002 
23003         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23004 
23005       EXECUTE IMMEDIATE lv_sql_stmt
23006               USING     lv_batch_id,
23007                         v_instance_code;
23008 
23009       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23010                      (p_app_short_name    => 'MSC',
23011                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
23012                       p_message_text      => lv_message_text,
23013                       p_error_text        => lv_error_text,
23014                       p_token1            => 'COLUMN_NAME',
23015                       p_token_value1      => 'FENCE_DAYS');
23016 
23017       IF lv_return <> 0 THEN
23018         RAISE ex_logging_err;
23019       END IF;
23020 
23021       --Fence days should not be null.
23022       v_sql_stmt := 25;
23023       lv_sql_stmt :=
23024       'UPDATE msc_st_supplier_flex_fences '
23025       ||' SET   error_text   = '||''''||lv_message_text||''''||','
23026       ||'     process_flag  = '||G_ERROR_FLG
23027       ||' WHERE  NVL(fence_days,'||NULL_VALUE||' )= '||NULL_VALUE
23028       ||' AND    deleted_flag     ='|| SYS_NO
23029       ||' AND    process_flag     ='|| G_IN_PROCESS
23030       ||' AND    batch_id         = :lv_batch_id'
23031       ||' AND    sr_instance_code = :v_instance_code';
23032 
23033 
23034         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23035 
23036       EXECUTE IMMEDIATE lv_sql_stmt
23037               USING     lv_batch_id,
23038                         v_instance_code;
23039 
23040       --Call to customised validation.
23041       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
23042         (ERRBUF         => lv_error_text,
23043          RETCODE        => lv_return,
23044          pBatchID       => lv_batch_id,
23045          pInstanceCode  => v_instance_code,
23046          pEntityName    => 'MSC_ST_SUPPLIER_FLEX_FENCES',
23047          pInstanceID    => v_instance_id);
23048 
23049       IF NVL(lv_return,0) <> 0 THEN
23050         RAISE ex_logging_err;
23051       END IF;
23052 
23053       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
23054                   (p_table_name     => 'MSC_ST_SUPPLIER_FLEX_FENCES',
23055                    p_instance_id    => v_instance_id,
23056                    p_instance_code  => v_instance_code,
23057                    p_process_flag   => G_VALID,
23058                    p_error_text     => lv_error_text,
23059                    p_debug          => v_debug,
23060                    p_batch_id       => lv_batch_id);
23061 
23062       IF lv_return <> 0 THEN
23063         RAISE ex_logging_err;
23064       END IF;
23065 
23066       lv_return := MSC_ST_UTIL.LOG_ERROR
23067                    (p_table_name        => 'MSC_ST_SUPPLIER_FLEX_FENCES',
23068                     p_instance_code     => v_instance_code,
23069                     p_row               => lv_column_names,
23070                     p_severity          => G_ERROR_FLG,
23071                     p_message_text      => NULL,
23072                     p_error_text        => lv_error_text,
23073                     p_debug             => v_debug,
23074                     p_batch_id          => lv_batch_id);
23075 
23076       IF lv_return <> 0 THEN
23077         RAISE ex_logging_err;
23078       END IF;
23079       COMMIT;
23080     END LOOP;
23081   EXCEPTION
23082     WHEN ex_logging_err THEN
23083         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
23084       ROLLBACK;
23085 
23086     WHEN OTHERS THEN
23087       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_SUPP_CAPACITY'||'('
23088                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
23089         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
23090       ROLLBACK;
23091 
23092   END LOAD_SUPP_CAPACITY;
23093 
23094 /*==========================================================================+
23095 | DESCRIPTION  : This procedure validates the data in MSC_ST_SAFETY_STOCKS  |
23096 |                table and derives the id's from the local id's table.      |
23097 +==========================================================================*/
23098   PROCEDURE LOAD_SAFETY_STOCKS IS
23099   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
23100   lb_rowid              RowidTab;
23101   lv_return             NUMBER;
23102   lv_error_text         VARCHAR2(250);
23103   lv_where_str          VARCHAR2(5000);
23104   lv_sql_stmt           VARCHAR2(5000);
23105   lv_column_names       VARCHAR2(5000);          --stores concatenated column names
23106   lv_batch_id           msc_st_safety_stocks.batch_id%TYPE;
23107   lv_message_text       msc_errors.error_text%TYPE;
23108 
23109   ex_logging_err        EXCEPTION;
23110 
23111   CURSOR c1(p_batch_id NUMBER) IS
23112     SELECT rowid
23113     FROM   msc_st_safety_stocks
23114     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
23115     AND    batch_id         = p_batch_id
23116     AND    sr_instance_code = v_instance_code;
23117 
23118   BEGIN
23119 
23120     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23121                  (p_app_short_name    => 'MSC',
23122                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
23123                   p_message_text      => lv_message_text,
23124                   p_error_text        => lv_error_text);
23125 
23126     IF lv_return <> 0 THEN
23127       RAISE ex_logging_err;
23128     END IF;
23129 
23130     --Duplicate records check for the records whose source is XML
23131     v_sql_stmt := 01;
23132     lv_sql_stmt :=
23133     ' UPDATE   msc_st_safety_stocks mss1'
23134     ||' SET    process_flag = '||G_ERROR_FLG||','
23135     ||'        error_text   = '||''''||lv_message_text||''''
23136     ||' WHERE  message_id <  (SELECT MAX(message_id)'
23137     ||'        FROM  msc_st_safety_stocks mss2'
23138     ||'        WHERE  mss2.sr_instance_code  = mss1.sr_instance_code'
23139     ||'        AND    mss2.organization_code = mss1.organization_code'
23140     ||'        AND    mss2.item_name         = mss1.item_name'
23141     ||'        AND    mss2.period_start_date = mss1.period_start_date'
23142     ||'        AND   NVL(mss2.company_name,  '   ||''''||NULL_CHAR||''''||') = '
23143     ||'              NVL(mss1.company_name,  '   ||''''||NULL_CHAR||''''||')'
23144     ||'        AND   mss2.process_flag = mss1.process_flag'
23145     ||'        AND   NVL(mss2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
23146     ||' AND    mss1.process_flag     = '||G_IN_PROCESS
23147     ||' AND    mss1.sr_instance_code = :v_instance_code'
23148     ||' AND    NVL(mss1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
23149 
23150 
23151       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23152 
23153     EXECUTE IMMEDIATE lv_sql_stmt
23154             USING     v_instance_code;
23155 
23156     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23157                  (p_app_short_name    => 'MSC',
23158                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
23159                   p_message_text      => lv_message_text,
23160                   p_error_text        => lv_error_text);
23161 
23162     IF lv_return <> 0 THEN
23163       RAISE ex_logging_err;
23164     END IF;
23165 
23166     --Duplicate records check for the records whose source is other than XML
23167     --Different SQL is used because in XML we can identify the latest records
23168     --whereas in batch load we cannot.
23169     v_sql_stmt := 02;
23170     lv_sql_stmt :=
23171     ' UPDATE   msc_st_safety_stocks mss1'
23172     ||' SET    process_flag = '||G_ERROR_FLG||','
23173     ||'        error_text   = '||''''||lv_message_text||''''
23174     ||' WHERE  EXISTS( SELECT 1 '
23175     ||'        FROM   msc_st_safety_stocks mss2'
23176     ||'        WHERE  mss2.sr_instance_code  = mss1.sr_instance_code'
23177     ||'        AND    mss2.organization_code = mss1.organization_code'
23178     ||'        AND    mss2.item_name         = mss1.item_name'
23179     ||'        AND    mss2.period_start_date = mss1.period_start_date'
23180     ||'        AND    NVL(mss2.company_name,  '   ||''''||NULL_CHAR||''''||') = '
23181     ||'               NVL(mss1.company_name,  '   ||''''||NULL_CHAR||''''||')'
23182     ||'        AND    mss2.process_flag = mss1.process_flag'
23183     ||'        AND    NVL(mss2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
23184     ||'        GROUP  BY sr_instance_code,organization_code,item_name,'
23185     ||'               period_start_date,company_name'
23186     ||'         HAVING COUNT(*) > 1)'
23187     ||' AND     mss1.process_flag     = '||G_IN_PROCESS
23188     ||' AND     mss1.sr_instance_code = :v_instance_code'
23189     ||' AND     NVL(mss1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
23190 
23191 
23192       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23193 
23194     EXECUTE IMMEDIATE lv_sql_stmt
23195             USING     v_instance_code;
23196 
23197     lv_column_names :=
23198     'ORGANIZATION_CODE       ||''~''||'
23199     ||'ITEM_NAME             ||''~''||'
23200     ||'PERIOD_START_DATE     ||''~''||'
23201     ||'SAFETY_STOCK_QUANTITY ||''~''||'
23202     ||'COMPANY_NAME          ||''~''||'
23203     ||'SR_INSTANCE_CODE      ||''~''||'
23204     ||'DELETED_FLAG ';
23205 
23206     LOOP
23207       v_sql_stmt := 03;
23208       SELECT msc_st_batch_id_s.NEXTVAL
23209       INTO   lv_batch_id
23210       FROM   dual;
23211 
23212       v_sql_stmt := 04;
23213       lv_sql_stmt :=
23214       ' UPDATE   msc_st_safety_stocks '
23215       ||' SET    batch_id  = :lv_batch_id'
23216       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
23217       ||' AND    sr_instance_code               = :v_instance_code'
23218       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
23219       ||' AND    rownum                        <= '||v_batch_size;
23220 
23221 
23222         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23223 
23224       EXECUTE IMMEDIATE lv_sql_stmt
23225               USING     lv_batch_id,
23226                         v_instance_code;
23227 
23228       EXIT WHEN SQL%NOTFOUND;
23229 
23230       OPEN c1(lv_batch_id);
23231       FETCH c1 BULK COLLECT INTO lb_rowid;
23232       CLOSE c1;
23233 
23234       v_sql_stmt := 05;
23235       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
23236       UPDATE msc_st_safety_stocks
23237       SET  st_transaction_id = msc_st_safety_stocks_s.NEXTVAL,
23238            refresh_id        = v_refresh_id,
23239            last_update_date  = v_current_date,
23240            last_updated_by   = v_current_user,
23241            creation_date     = v_current_date,
23242            created_by        = v_current_user
23243       WHERE  rowid           = lb_rowid(j);
23244 
23245       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23246                      (p_app_short_name    => 'MSC',
23247                       p_error_code        => 'MSC_PP_NO_DELETION',
23248                       p_message_text      => lv_message_text,
23249                       p_error_text        => lv_error_text,
23250                       p_token1            => 'TABLE_NAME',
23251                       p_token_value1      => 'MSC_ST_SAFETY_STOCKS');
23252 
23253       IF lv_return <> 0 THEN
23254         RAISE ex_logging_err;
23255       END IF;
23256 
23257       --Deletion is not allowed for this entity.
23258       v_sql_stmt := 06;
23259       lv_sql_stmt :=
23260       ' UPDATE     msc_st_safety_stocks '
23261       ||' SET      process_flag     = '||G_ERROR_FLG||','
23262       ||'          error_text       = '||''''||lv_message_text||''''
23263       ||' WHERE    deleted_flag     = '||SYS_YES
23264       ||' AND      process_flag     = '||G_IN_PROCESS
23265       ||' AND      batch_id         =  :lv_batch_id'
23266       ||' AND      sr_instance_code = :v_instance_code';
23267 
23268 
23269         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23270 
23271       EXECUTE IMMEDIATE lv_sql_stmt
23272               USING     lv_batch_id,
23273                         v_instance_code;
23274 
23275       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23276                      (p_app_short_name    => 'MSC',
23277                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
23278                       p_message_text      => lv_message_text,
23279                       p_error_text        => lv_error_text,
23280                       p_token1            => 'COLUMN_NAME',
23281                       p_token_value1      => 'SAFETY_STOCK_QUANTITY OR PERIOD_START_DATE');
23282 
23283       IF lv_return <> 0 THEN
23284         RAISE ex_logging_err;
23285       END IF;
23286 
23287       v_sql_stmt := 07;
23288       lv_sql_stmt :=
23289       ' UPDATE  msc_st_safety_stocks '
23290       ||' SET   process_flag        = '||G_ERROR_FLG||','
23291       ||'       error_text          = '||''''||lv_message_text||''''
23292       ||' WHERE (safety_stock_quantity IS NULL '
23293       ||' OR    period_start_date IS NULL) '
23294       ||' AND   batch_id            = :lv_batch_id'
23295       ||' AND   process_flag        = '||G_IN_PROCESS
23296       ||' AND   sr_instance_code    = :v_instance_code';
23297 
23298 
23299         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23300       EXECUTE IMMEDIATE lv_sql_stmt
23301               USING     lv_batch_id,
23302                         v_instance_code;
23303 
23304       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23305                      (p_app_short_name    => 'MSC',
23306                       p_error_code        => 'MSC_PP_INVALID_VALUE',
23307                       p_message_text      => lv_message_text,
23308                       p_error_text        => lv_error_text,
23309                       p_token1            => 'COLUMN_NAME',
23310                       p_token_value1      => 'ORGANIZATION_CODE');
23311 
23312       IF lv_return <> 0 THEN
23313         RAISE ex_logging_err;
23314       END IF;
23315 
23316       --Derive Organization_id
23317       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
23318                      (p_table_name       => 'MSC_ST_SAFETY_STOCKS',
23319                       p_org_partner_name => 'ORGANIZATION_CODE',
23320                       p_org_partner_id   => 'ORGANIZATION_ID',
23321                       p_instance_code    => v_instance_code,
23322                       p_partner_type     => G_ORGANIZATION,
23323                       p_error_text       => lv_error_text,
23324                       p_batch_id         => lv_batch_id,
23325                       p_severity         => G_SEV_ERROR,
23326                       p_message_text     => lv_message_text,
23327                       p_debug            => v_debug,
23328                       p_row              => lv_column_names);
23329 
23330       IF lv_return <> 0 THEN
23331         RAISE ex_logging_err;
23332       END IF;
23333 
23334       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23335                      (p_app_short_name    => 'MSC',
23336                       p_error_code        => 'MSC_PP_INVALID_VALUE',
23337                       p_message_text      => lv_message_text,
23338                       p_error_text        => lv_error_text,
23339                       p_token1            => 'COLUMN_NAME',
23340                       p_token_value1      => 'ITEM_NAME');
23341 
23342       IF lv_return <> 0 THEN
23343         RAISE ex_logging_err;
23344       END IF;
23345 
23346       --Derive Inventory_item_id
23347       lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
23348                      (p_table_name       => 'MSC_ST_SAFETY_STOCKS',
23349                       p_item_col_name    => 'ITEM_NAME',
23350                       p_item_col_id      => 'INVENTORY_ITEM_ID',
23351                       p_instance_id      => v_instance_id,
23352                       p_instance_code    => v_instance_code,
23353                       p_error_text       => lv_error_text,
23354                       p_batch_id         => lv_batch_id,
23355                       p_severity         => G_SEV_ERROR,
23356                       p_message_text     => lv_message_text,
23357                       p_debug            => v_debug,
23358                       p_row              => lv_column_names);
23359 
23360       IF lv_return <> 0 THEN
23361         RAISE ex_logging_err;
23362       END IF;
23363 
23364     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23365                    (p_app_short_name    => 'MSC',
23366                     p_error_code        => 'MSC_PP_SAFETY_STOCK_INVALID',
23367                     p_message_text      => lv_message_text,
23368                     p_error_text        => lv_error_text);
23369 
23370     IF lv_return <> 0 THEN
23371       RAISE ex_logging_err;
23372     END IF;
23373 
23374 
23375     v_sql_stmt := 08;
23376     lv_sql_stmt :=
23377 
23378     'UPDATE    msc_st_safety_stocks mss'
23379     ||' SET    process_flag        = '||G_ERROR_FLG||','
23380     ||'        error_text   = '||''''||lv_message_text||''''
23381     ||' WHERE  EXISTS (SELECT 1'
23382     ||'        FROM   msc_st_system_items msi'
23383     ||'        WHERE  msi.sr_instance_code  = mss.sr_instance_code '
23384     ||'        AND    msi.organization_code = mss.organization_code'
23385     ||'        AND    msi.item_name         = mss.item_name'
23386     ||'        AND   NVL(msi.company_name,'||''''||NULL_CHAR||''''||') '
23387     ||'             =    NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
23388     ||'        AND    msi.mrp_safety_stock_code <> 1'
23389     ||'        AND    msi.process_flag      = '||G_VALID
23390     ||'        UNION'
23391     ||'        SELECT 1'
23392     ||'        FROM   msc_system_items itm'
23393     ||'        WHERE  itm.sr_instance_id  = '||v_instance_id
23394     ||'        AND    itm.organization_id = mss.organization_id '
23395     ||'        AND    itm.item_name       = mss.item_name'
23396     ||'        AND    itm.safety_stock_code <> 1'
23397     ||'        AND    itm.plan_id         = -1 )'
23398     ||' AND deleted_flag  = '||SYS_NO
23399     ||' AND process_flag  = '||G_IN_PROCESS
23400     ||' AND  batch_id     = :lv_batch_id'
23401     ||' AND sr_instance_code = :v_instance_code';
23402 
23403 
23404         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23405 
23406       EXECUTE IMMEDIATE lv_sql_stmt
23407               USING     lv_batch_id,
23408                         v_instance_code;
23409 
23410 
23411     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23412                    (p_app_short_name    => 'MSC',
23413                     p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
23414                     p_message_text      => lv_message_text,
23415                     p_error_text        => lv_error_text,
23416                     p_token1            => 'COLUMN_NAMES',
23417                     p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
23418                                            ||' ORGANIZATION_CODE AND PROJECT_NUMBER',
23419                     p_token2            => 'MASTER_TABLE',
23420                     p_token_value2      => 'MSC_ST_PROJECT_TASKS');
23421 
23422     IF lv_return <> 0 THEN
23423       RAISE ex_logging_err;
23424     END IF;
23425 
23426     --Derive Project Id.
23427     lv_return := MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
23428                            (p_table_name          => 'MSC_ST_SAFETY_STOCKS',
23429                             p_proj_col_name       => 'PROJECT_NUMBER',
23430                             p_proj_task_col_id    => 'PROJECT_ID',
23431                             p_instance_code       => v_instance_code,
23432                             p_entity_name         => 'PROJECT_ID',
23433                             p_error_text          => lv_error_text,
23434                             p_batch_id            => lv_batch_id,
23435                             p_severity            => G_SEV_WARNING,
23436                             p_message_text        => lv_message_text,
23437                             p_debug               => v_debug,
23438                             p_row                 => lv_column_names);
23439     IF lv_return <> 0 THEN
23440       RAISE ex_logging_err;
23441     END IF;
23442 
23443     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23444                    (p_app_short_name    => 'MSC',
23445                     p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
23446                     p_message_text      => lv_message_text,
23447                     p_error_text        => lv_error_text,
23448                     p_token1            => 'COLUMN_NAMES',
23449                     p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
23450                                            ||' ORGANIZATION_CODE, PROJECT_NUMBER,'
23451                                            ||' TASK_NUMBER',
23452                     p_token2            => 'MASTER_TABLE',
23453                     p_token_value2      => 'MSC_ST_PROJECT_TASKS');
23454 
23455     IF lv_return <> 0 THEN
23456       RAISE ex_logging_err;
23457     END IF;
23458 
23459     --Derive Task Id.
23460     lv_return := MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
23461                            (p_table_name          => 'MSC_ST_SAFETY_STOCKS',
23462                             p_proj_col_name       => 'PROJECT_NUMBER',
23463                             p_proj_task_col_id    => 'TASK_ID',
23464                             p_instance_code       => v_instance_code,
23465                             p_entity_name         => 'TASK_ID',
23466                             p_error_text          => lv_error_text,
23467                             p_task_col_name       => 'TASK_NUMBER',
23468                             p_batch_id            => lv_batch_id,
23469                             p_severity            => G_SEV_WARNING,
23470                             p_message_text        => lv_message_text,
23471                             p_debug               => v_debug,
23472                             p_row                 => lv_column_names);
23473 
23474     IF lv_return <> 0 THEN
23475       RAISE ex_logging_err;
23476     END IF;
23477 
23478       --Call to customised validation.
23479       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
23480         (ERRBUF         => lv_error_text,
23481          RETCODE        => lv_return,
23482          pBatchID       => lv_batch_id,
23483          pInstanceCode  => v_instance_code,
23484          pEntityName    => 'MSC_ST_SAFETY_STOCKS',
23485          pInstanceID    => v_instance_id);
23486 
23487       IF NVL(lv_return,0) <> 0 THEN
23488         RAISE ex_logging_err;
23489       END IF;
23490 
23491       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
23492                   (p_table_name     => 'MSC_ST_SAFETY_STOCKS',
23493                    p_instance_id    => v_instance_id,
23494                    p_instance_code  => v_instance_code,
23495                    p_process_flag   => G_VALID,
23496                    p_error_text     => lv_error_text,
23497                    p_debug          => v_debug,
23498                    p_batch_id       => lv_batch_id);
23499 
23500       IF lv_return <> 0 THEN
23501         RAISE ex_logging_err;
23502       END IF;
23503 
23504       lv_return := MSC_ST_UTIL.LOG_ERROR
23505                    (p_table_name        => 'MSC_ST_SAFETY_STOCKS',
23506                     p_instance_code     => v_instance_code,
23507                     p_row               => lv_column_names,
23508                     p_severity          => G_SEV_ERROR,
23509                     p_message_text      => NULL,
23510                     p_error_text        => lv_error_text,
23511                     p_debug             => v_debug,
23512                     p_batch_id          => lv_batch_id);
23513 
23514       IF lv_return <> 0 THEN
23515         RAISE ex_logging_err;
23516       END IF;
23517 
23518       COMMIT;
23519     END LOOP;
23520 
23521   EXCEPTION
23522     WHEN too_many_rows THEN
23523       lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_SAFETY_STOCKS'||'('
23524                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
23525         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
23526       ROLLBACK;
23527 
23528     WHEN ex_logging_err THEN
23529         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
23530       ROLLBACK;
23531 
23532     WHEN OTHERS THEN
23533       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_SAFETY_STOCKS'||'('
23534                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
23535         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
23536       ROLLBACK;
23537 
23538   END LOAD_SAFETY_STOCKS;
23539 
23540 /*==========================================================================+
23541 | DESCRIPTION  : This procedure validates the data in MSC_ST_ITEM_CATEGORIES|
23542 |                table.                                                     |
23543 +==========================================================================*/
23544   PROCEDURE LOAD_CATEGORY IS
23545   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
23546   lb_rowid              RowidTab;
23547   lv_return             NUMBER;
23548   lv_error_text         VARCHAR2(250);
23549   lv_where_str          VARCHAR2(5000);
23550   lv_sql_stmt           VARCHAR2(5000);
23551   lv_column_names       VARCHAR2(5000);          --stores concatenated column names
23552   lv_batch_id           msc_st_item_categories.batch_id%TYPE;
23553   lv_message_text       msc_errors.error_text%TYPE;
23554 
23555   ex_logging_err        EXCEPTION;
23556 
23557 
23558   TYPE CharTblTyp IS TABLE OF VARCHAR2(1000);
23559   lb_item    CharTblTyp;
23560   lb_cat     CharTblTyp;
23561   lb_cat_set CharTblTyp;
23562   lb_org     CharTblTyp;
23563   lb_comp    CharTblTyp;
23564 
23565   CURSOR c_udk is
23566     SELECT item_name, category_name, category_set_name, organization_code, company_name
23567     FROM   msc_st_item_categories
23568     WHERE sr_instance_code = v_instance_code
23569     AND   process_flag= G_IN_PROCESS
23570     AND   NVL(message_id,NULL_VALUE) = NULL_VALUE
23571     GROUP BY sr_instance_code, item_name, category_name, category_set_name, organization_code, company_name
23572     HAVING COUNT(*) > 1;
23573 
23574   CURSOR c1(p_batch_id NUMBER) IS
23575     SELECT rowid
23576     FROM   msc_st_item_categories
23577     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
23578     AND    batch_id         = p_batch_id
23579     AND    sr_instance_code = v_instance_code;
23580 
23581   CURSOR c2(p_batch_id NUMBER) IS
23582     SELECT max(rowid)
23583     FROM   msc_st_item_categories
23584     WHERE  NVL(sr_category_id,NULL_VALUE)    = NULL_VALUE
23585     AND    process_flag                   = G_IN_PROCESS
23586     AND    batch_id                       = p_batch_id
23587     AND    sr_instance_code               = v_instance_code
23588     GROUP BY sr_instance_code,company_name,category_name ;
23589 
23590   BEGIN
23591 
23592     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23593                  (p_app_short_name    => 'MSC',
23594                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
23595                   p_message_text      => lv_message_text,
23596                   p_error_text        => lv_error_text);
23597 
23598     IF lv_return <> 0 THEN
23599       RAISE ex_logging_err;
23600     END IF;
23601 
23602     --Duplicate records check for the records whose source is XML
23603     v_sql_stmt := 01;
23604     lv_sql_stmt :=
23605     'UPDATE     msc_st_item_categories mic1 '
23606     ||' SET     process_flag = '||G_ERROR_FLG||','
23607     ||'         error_text   = '||''''||lv_message_text||''''
23608     ||' WHERE   message_id <  (SELECT MAX(message_id)'
23609     ||'         FROM   msc_st_item_categories mic2'
23610     ||'         WHERE  mic2.sr_instance_code    = mic1.sr_instance_code'
23611     ||'         AND    mic2.organization_code   = mic1.organization_code'
23612     ||'         AND    NVL(mic2.company_name,  '||''''||NULL_CHAR||''''||') = '
23613     ||'                NVL(mic1.company_name,  '||''''||NULL_CHAR||''''||')'
23614     ||'         AND    mic2.category_set_name   = mic1.category_set_name'
23615     ||'         AND    mic2.category_name       = mic1.category_name'
23616     ||'         AND    mic2.item_name = mic1.item_name '
23617     ||'         AND    mic2.process_flag        = '||G_IN_PROCESS
23618     ||'         AND    mic2.sr_instance_code    = mic1.sr_instance_code'
23619     ||'         AND    NVL(mic2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
23620     ||' AND  mic1.process_flag        = '||G_IN_PROCESS
23621     ||' AND  mic1.sr_instance_code    = :v_instance_code'
23622     ||' AND  NVL(mic1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
23623 
23624 
23625 
23626       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23627     EXECUTE IMMEDIATE lv_sql_stmt
23628             USING     v_instance_code;
23629 
23630     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23631                  (p_app_short_name    => 'MSC',
23632                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
23633                   p_message_text      => lv_message_text,
23634                   p_error_text        => lv_error_text);
23635 
23636     IF lv_return <> 0 THEN
23637       RAISE ex_logging_err;
23638     END IF;
23639 
23640     --Duplicate records check for the records whose source is other than XML
23641     --Different SQL is used because in XML we can identify the latest records
23642     --whereas in batch load we cannot.
23643 /*
23644     v_sql_stmt := 02;
23645     lv_sql_stmt :=
23646     'UPDATE   msc_st_item_categories mic1 '
23647     ||' SET   process_flag  = '||G_ERROR_FLG||','
23648     ||'         error_text   = '||''''||lv_message_text||''''
23649     ||' WHERE EXISTS( SELECT 1 '
23650     ||'       FROM   msc_st_item_categories mic2'
23651     ||'       WHERE  mic2.sr_instance_code    = mic1.sr_instance_code'
23652     ||'       AND    mic2.organization_code   = mic1.organization_code'
23653     ||'       AND    NVL(mic2.company_name,  '||''''||NULL_CHAR||''''||') = '
23654     ||'              NVL(mic1.company_name,  '||''''||NULL_CHAR||''''||')'
23655     ||'       AND    mic2.category_set_name   = mic1.category_set_name'
23656     ||'       AND    mic2.category_name       = mic1.category_name'
23657     ||'       AND    nvl(mic2.item_name,     '||''''||NULL_CHAR||''''||') = '
23658     ||'              nvl(mic1.item_name,     '||''''||NULL_CHAR||''''||') '
23659     ||'       AND    mic2.sr_instance_code    = mic1.sr_instance_code'
23660     ||'       AND    NVL(mic2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
23661     ||'       GROUP BY sr_instance_code,organization_code,category_set_name,'
23662     ||'              company_name,category_name,item_name'
23663     ||'       HAVING COUNT(*) > 1)'
23664     ||' AND   mic1.process_flag     = '||G_IN_PROCESS
23665     ||' AND   mic1.sr_instance_code = :v_instance_code'
23666     ||' AND   NVL(mic1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
23667 
23668 
23669       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23670     EXECUTE IMMEDIATE lv_sql_stmt
23671             USING     v_instance_code;
23672 */
23673 
23674     BEGIN
23675        OPEN c_udk;
23676          FETCH c_udk BULK COLLECT INTO lb_item, lb_cat, lb_cat_set, lb_org, lb_comp;
23677        CLOSE c_udk;
23678 
23679        FORALL j IN lb_item.FIRST..lb_item.LAST
23680        UPDATE msc_st_item_categories
23681        SET process_flag = G_ERROR_FLG, error_text   = lv_message_text
23682        WHERE  sr_instance_code = v_instance_code
23683        AND    item_name =   lb_item(j)
23684        AND    category_name       = lb_cat(j)
23685        AND    category_set_name   = lb_cat_set(j)
23686        AND    organization_code   = lb_org(j)
23687        AND    NVL(company_name,  NULL_CHAR) =   NVL(lb_comp(j),  NULL_CHAR)
23688        AND    NVL(message_id,NULL_VALUE) = NULL_VALUE
23689        AND    process_flag= G_IN_PROCESS;
23690 
23691     EXCEPTION
23692        WHEN OTHERS THEN
23693         null;
23694     END;
23695 
23696     lv_column_names :=
23697     'CATEGORY_NAME      ||''~''||'
23698     ||'SR_INSTANCE_CODE ||''~''||'
23699     ||'DESCRIPTION      ||''~''||'
23700     ||'DISABLE_DATE     ||''~''||'
23701     ||'START_DATE_ACTIVE||''~''||'
23702     ||'END_DATE_ACTIVE  ||''~''||'
23703     ||'ITEM_NAME        ||''~''||'
23704     ||'ORGANIZATION_CODE||''~''||'
23705     ||'CATEGORY_SET_NAME||''~''||'
23706     ||'COMPANY_NAME     ||''~''||'
23707     ||'SUMMARY_FLAG     ||''~''||'
23708     ||'ENABLED_FLAG     ||''~''||'
23709     ||'DELETED_FLAG';
23710 
23711 
23712     LOOP
23713       v_sql_stmt := 03;
23714       SELECT msc_st_batch_id_s.NEXTVAL
23715       INTO   lv_batch_id
23716       FROM   dual;
23717 
23718       v_sql_stmt := 04;
23719       lv_sql_stmt :=
23720       'UPDATE msc_st_item_categories '
23721       ||' SET    batch_id     = :lv_batch_id'
23722       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
23723       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
23724       ||' AND    sr_instance_code         = :v_instance_code'
23725       ||' AND    rownum                  <= '||v_batch_size;
23726 
23727 
23728         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23729       EXECUTE IMMEDIATE lv_sql_stmt
23730               USING     lv_batch_id,
23731                         v_instance_code;
23732 
23733       EXIT WHEN SQL%NOTFOUND;
23734 
23735       OPEN c1(lv_batch_id);
23736       FETCH c1 BULK COLLECT INTO lb_rowid;
23737       CLOSE c1;
23738 
23739       v_sql_stmt := 05;
23740       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
23741       UPDATE msc_st_item_categories
23742       SET    st_transaction_id = msc_st_item_categories_s.NEXTVAL,
23743              refresh_id        = v_refresh_id,
23744              last_update_date  = v_current_date,
23745              last_updated_by   = v_current_user,
23746              creation_date     = v_current_date,
23747              created_by        = v_current_user
23748       WHERE  rowid             = lb_rowid(j);
23749 
23750       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23751                      (p_app_short_name    => 'MSC',
23752                       p_error_code        => 'MSC_PP_NO_DELETION',
23753                       p_message_text      => lv_message_text,
23754                       p_error_text        => lv_error_text,
23755                       p_token1            => 'TABLE_NAME',
23756                       p_token_value1      => 'MSC_ST_ITEM_CATEGORIES');
23757 
23758       IF lv_return <> 0 THEN
23759         RAISE ex_logging_err;
23760       END IF;
23761 
23762       --Deletion is not allowed on this table.
23763       v_sql_stmt := 06;
23764       lv_sql_stmt :=
23765       'UPDATE   msc_st_item_categories '
23766       ||' SET      process_flag     = '||G_ERROR_FLG||','
23767       ||'         error_text   = '||''''||lv_message_text||''''
23768       ||' WHERE    deleted_flag     = '||SYS_YES
23769       ||' AND      process_flag     = '||G_IN_PROCESS
23770       ||' AND      batch_id         =  :lv_batch_id'
23771       ||' AND      sr_instance_code = :v_instance_code';
23772 
23773 
23774 
23775         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23776       EXECUTE IMMEDIATE lv_sql_stmt
23777               USING     lv_batch_id,
23778                         v_instance_code;
23779 
23780       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23781                      (p_app_short_name    => 'MSC',
23782                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
23783                       p_message_text      => lv_message_text,
23784                       p_error_text        => lv_error_text,
23785                       p_token1            => 'COLUMN_NAME',
23786                       p_token_value1      => 'DELETED_FLAG',
23787                       p_token2            => 'DEFAULT_VALUE',
23788                       p_token_value2      => SYS_NO);
23789 
23790       IF lv_return <> 0 THEN
23791         RAISE ex_logging_err;
23792       END IF;
23793 
23794       lv_where_str :=
23795       ' AND NVL(deleted_flag,'||NULL_VALUE||') '
23796       ||' NOT IN(1,2)';
23797 
23798       --Log a warning for those records where the deleted_flag has a value other
23799       --SYS_NO
23800       lv_return := MSC_ST_UTIL.LOG_ERROR
23801                      (p_table_name        => 'MSC_ST_ITEM_CATEGORIES',
23802                       p_instance_code     => v_instance_code,
23803                       p_row               => lv_column_names,
23804                       p_severity          => G_SEV_WARNING,
23805                       p_message_text      => lv_message_text,
23806                       p_error_text        => lv_error_text,
23807                       p_batch_id          => lv_batch_id,
23808                       p_where_str         => lv_where_str,
23809                       p_col_name          => 'DELETED_FLAG',
23810                       p_debug             => v_debug,
23811                       p_default_value     => SYS_NO);
23812 
23813       IF lv_return <> 0 THEN
23814         RAISE ex_logging_err;
23815       END IF;
23816 
23817       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23818                      (p_app_short_name    => 'MSC',
23819                       p_error_code        => 'MSC_PP_INVALID_VALUE',
23820                       p_message_text      => lv_message_text,
23821                       p_error_text        => lv_error_text,
23822                       p_token1            => 'COLUMN_NAME',
23823                       p_token_value1      => 'ORGANIZATION_CODE');
23824 
23825       IF lv_return <> 0 THEN
23826         RAISE ex_logging_err;
23827       END IF;
23828 
23829       --Derive Organization_id
23830       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
23831                      (p_table_name       => 'MSC_ST_ITEM_CATEGORIES',
23832                       p_org_partner_name => 'ORGANIZATION_CODE',
23833                       p_org_partner_id   => 'ORGANIZATION_ID',
23834                       p_instance_code    => v_instance_code,
23835                       p_partner_type     => G_ORGANIZATION,
23836                       p_error_text       => lv_error_text,
23837                       p_batch_id         => lv_batch_id,
23838                       p_severity         => G_SEV_ERROR,
23839                       p_message_text     => lv_message_text,
23840                       p_debug            => v_debug,
23841                       p_row              => lv_column_names);
23842 
23843       IF lv_return <> 0 THEN
23844         RAISE ex_logging_err;
23845       END IF;
23846 
23847       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23848                      (p_app_short_name    => 'MSC',
23849                       p_error_code        => 'MSC_PP_INVALID_VALUE',
23850                       p_message_text      => lv_message_text,
23851                       p_error_text        => lv_error_text,
23852                       p_token1            => 'COLUMN_NAME',
23853                       p_token_value1      => 'ITEM_NAME');
23854 
23855       IF lv_return <> 0 THEN
23856         RAISE ex_logging_err;
23857       END IF;
23858 
23859       --Derive Inventory_item_id
23860       lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
23861                      (p_table_name       => 'MSC_ST_ITEM_CATEGORIES',
23862                       p_item_col_name    => 'ITEM_NAME',
23863                       p_item_col_id      => 'INVENTORY_ITEM_ID',
23864                       p_instance_id      => v_instance_id,
23865                       p_instance_code    => v_instance_code,
23866                       p_error_text       => lv_error_text,
23867                       p_batch_id         => lv_batch_id,
23868                       p_severity         => G_SEV_ERROR,
23869                       p_message_text     => lv_message_text,
23870                       p_debug            => v_debug,
23871                       p_row              => lv_column_names);
23872 
23873       IF lv_return <> 0 THEN
23874         RAISE ex_logging_err;
23875       END IF;
23876 
23877 
23878       --Derive sr_category_set_id
23879       v_sql_stmt := 07;
23880       lv_sql_stmt :=
23881       'UPDATE  msc_st_item_categories '
23882       ||' SET  sr_category_set_id = (SELECT local_id'
23883       ||'      FROM   msc_local_id_misc'
23884       ||'      WHERE  char1 =   sr_instance_code'
23885       ||'      AND    NVL(char2,       '||''''||NULL_CHAR||''''||') = '
23886       ||'             NVL(company_name,'||''''||NULL_CHAR||''''||') '
23887       ||'      AND    char3 =   category_set_name'
23888       ||'      AND    entity_name = ''SR_CATEGORY_SET_ID'' )'
23889       ||' WHERE sr_instance_code = :v_instance_code'
23890       ||' AND   batch_id         = :lv_batch_id'
23891       ||' AND   process_flag     = '||G_IN_PROCESS;
23892 
23893 
23894 
23895         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23896       EXECUTE IMMEDIATE lv_sql_stmt
23897               USING     v_instance_code,
23898                         lv_batch_id;
23899 
23900       if(v_install_msc) THEN  -- category set should exists
23901       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
23902                      (p_app_short_name    => 'MSC',
23903                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
23904                       p_message_text      => lv_message_text,
23905                       p_error_text        => lv_error_text,
23906                       p_token1            => 'COLUMN_NAME',
23907                       p_token_value1      => 'CATEGORY_SET_NAME');
23908 
23909       IF lv_return <> 0 THEN
23910         RAISE ex_logging_err;
23911       END IF;
23912 
23913       v_sql_stmt := 08;
23914       lv_sql_stmt :=
23915       'UPDATE msc_st_item_categories '
23916       ||' SET process_flag     = '||G_ERROR_FLG||','
23917       ||'         error_text   = '||''''||lv_message_text||''''
23918       ||' WHERE NVL(sr_category_set_id,'||NULL_VALUE||') = '||NULL_VALUE
23919       ||' AND   process_flag   = '||G_IN_PROCESS
23920       ||' AND   batch_id       = :lv_batch_id'
23921       ||' AND   sr_instance_code = :v_instance_code';
23922 
23923 
23924         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23925       EXECUTE IMMEDIATE lv_sql_stmt
23926               USING     lv_batch_id,
23927                         v_instance_code;
23928       END IF ; -- v_install_msc
23929 
23930       --Derive sr_category_id
23931       v_sql_stmt := 09;
23932       lv_sql_stmt :=
23933       'UPDATE msc_st_item_categories '
23934       ||' SET sr_category_id = (SELECT local_id '
23935       ||'     FROM   msc_local_id_misc'
23936       ||'     WHERE  char1 =   sr_instance_code'
23937       ||'     AND    NVL(char2,       '||''''||NULL_CHAR||''''||') = '
23938       ||'            NVL(company_name,'||''''||NULL_CHAR||''''||') '
23939       ||'     AND    char3 =   category_name'
23940       ||'     AND    entity_name = ''SR_CATEGORY_ID'' )'
23941       ||' WHERE sr_instance_code = :v_instance_code'
23942       ||' AND   batch_id         = :lv_batch_id'
23943       ||' AND   process_flag     = '||G_IN_PROCESS;
23944 
23945 
23946         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
23947       EXECUTE IMMEDIATE lv_sql_stmt
23948               USING     v_instance_code,
23949                         lv_batch_id;
23950 
23951       --Call to customised validation.
23952       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
23953         (ERRBUF         => lv_error_text,
23954          RETCODE        => lv_return,
23955          pBatchID       => lv_batch_id,
23956          pInstanceCode  => v_instance_code,
23957          pEntityName    => 'MSC_ST_ITEM_CATEGORIES',
23958          pInstanceID    => v_instance_id);
23959 
23960       IF NVL(lv_return,0) <> 0 THEN
23961         RAISE ex_logging_err;
23962       END IF;
23963 
23964       OPEN c2(lv_batch_id);
23965       FETCH c2 BULK COLLECT INTO lb_rowid ;
23966 
23967       IF c2%ROWCOUNT > 0  THEN
23968 
23969         v_sql_stmt := 10;
23970         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
23971         UPDATE msc_st_item_categories
23972         SET    sr_category_id = msc_st_sr_category_id_s.NEXTVAL
23973         WHERE  rowid          = lb_rowid(j);
23974 
23975         v_sql_stmt := 11;
23976         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
23977         INSERT INTO msc_local_id_misc
23978         (local_id,
23979          st_transaction_id,
23980          instance_id,
23981          entity_name,
23982          data_source_type,
23983          char1,
23984          char2,
23985          char3,
23986          SOURCE_ORG_ID,
23987          SOURCE_INVENTORY_ITEM_ID,
23988          SOURCE_CATEGORY_SET_ID,
23989          SOURCE_CATEGORY_ID,
23990          last_update_date,
23991          last_updated_by,
23992          creation_date,
23993          created_by)
23994         SELECT
23995          sr_category_id,
23996          st_transaction_id,
23997          v_instance_id,
23998          'SR_CATEGORY_ID',
23999          data_source_type,
24000          v_instance_code,
24001          company_name,
24002          category_name,
24003          SOURCE_ORG_ID,
24004  	 SOURCE_INVENTORY_ITEM_ID,
24005          SOURCE_SR_CATEGORY_SET_ID,
24006          SOURCE_SR_CATEGORY_ID,
24007          v_current_date,
24008          v_current_user,
24009          v_current_date,
24010          v_current_user
24011         FROM msc_st_item_categories
24012         WHERE  rowid            = lb_rowid(j);
24013 
24014       END IF;
24015       CLOSE c2 ;
24016 
24017       --Derive sr_category_id
24018       v_sql_stmt := 09;
24019       lv_sql_stmt :=
24020       'UPDATE msc_st_item_categories '
24021       ||' SET sr_category_id = (SELECT local_id '
24022       ||'     FROM   msc_local_id_misc'
24023       ||'     WHERE  char1 =   sr_instance_code'
24024       ||'     AND    NVL(char2,       '||''''||NULL_CHAR||''''||') = '
24025       ||'            NVL(company_name,'||''''||NULL_CHAR||''''||') '
24026       ||'     AND    char3 =   category_name'
24027       ||'     AND    entity_name = ''SR_CATEGORY_ID'' )'
24028       ||' WHERE sr_instance_code = :v_instance_code'
24029       ||' AND    NVL(sr_category_id,'||NULL_VALUE||') = '||NULL_VALUE
24030       ||' AND   batch_id         = :lv_batch_id'
24031       ||' AND   process_flag     = '||G_IN_PROCESS;
24032 
24033 
24034         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24035       EXECUTE IMMEDIATE lv_sql_stmt
24036               USING     v_instance_code,
24037                         lv_batch_id;
24038 
24039 
24040       v_sql_stmt := 12;
24041       lv_sql_stmt :=
24042       'UPDATE   msc_st_item_categories '
24043       ||' SET   summary_flag = ''N'' '
24044       ||' WHERE process_flag = '||G_IN_PROCESS
24045       ||' AND   batch_id     = :lv_batch_id'
24046       ||' AND   sr_instance_code = :v_instance_code';
24047 
24048 
24049         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24050       EXECUTE IMMEDIATE lv_sql_stmt
24051               USING     lv_batch_id,
24052                         v_instance_code;
24053 
24054       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
24055                   (p_table_name     => 'MSC_ST_ITEM_CATEGORIES',
24056                    p_instance_id    => v_instance_id,
24057                    p_instance_code  => v_instance_code,
24058                    p_process_flag   => G_VALID,
24059                    p_error_text     => lv_error_text,
24060                    p_debug          => v_debug,
24061                    p_batch_id       => lv_batch_id);
24062 
24063       IF lv_return <> 0 THEN
24064         RAISE ex_logging_err;
24065       END IF;
24066 
24067       lv_return := MSC_ST_UTIL.LOG_ERROR
24068                    (p_table_name        => 'MSC_ST_ITEM_CATEGORIES',
24069                     p_instance_code     => v_instance_code,
24070                     p_row               => lv_column_names,
24071                     p_severity          => G_SEV_ERROR,
24072                     p_message_text      => NULL,
24073                     p_error_text        => lv_error_text,
24074                     p_debug             => v_debug,
24075                     p_batch_id          => lv_batch_id);
24076 
24077       IF lv_return <> 0 THEN
24078         RAISE ex_logging_err;
24079       END IF;
24080       COMMIT;
24081 
24082       -- ODP change
24083       -- Call the procedure to insert categories into msd_st_level_values
24084       IF(v_install_msd) THEN
24085         lv_sql_stmt := ' DECLARE errbuf varchar2(250);'
24086                  ||' retcode  number ;'
24087                  || ' BEGIN '
24088                  ||' MSD_CL_PRE_PROCESS.LOAD_CATEGORY'
24089                  ||' (ERRBUF         =>  errbuf ,'
24090                  ||'  RETCODE        =>  retcode ,'
24091                  ||'  p_instance_id => :v_instance_id,'
24092                  ||'  p_batch_id    => :lv_batch_id ,'
24093                  ||'  p_link        => :p_item_rollup);'
24094                  ||'  IF ( retcode = '||G_WARNING||') THEN'
24095                  ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
24096                  ||'  END IF;'
24097                  ||'  END ;';
24098 
24099         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24100 
24101         EXECUTE IMMEDIATE lv_sql_stmt
24102                 USING     v_instance_id,
24103                           lv_batch_id,
24104                           v_item_rollup;
24105       END IF ;
24106       COMMIT;
24107 
24108 
24109     END LOOP;
24110 
24111   EXCEPTION
24112     WHEN too_many_rows THEN
24113       lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_CATEGORY'||'('
24114                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
24115         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
24116       ROLLBACK;
24117 
24118     WHEN ex_logging_err THEN
24119         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
24120       ROLLBACK;
24121 
24122     WHEN OTHERS THEN
24123       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_CATEGORY'||'('
24124                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
24125         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
24126       ROLLBACK;
24127 
24128   END LOAD_CATEGORY;
24129 
24130 
24131  /*==========================================================================+
24132   | DESCRIPTION  : This function performs the validation and loads the data   |
24133   |                for business object Sourcing Rules.                        |
24134   +==========================================================================*/
24135   PROCEDURE  LOAD_SOURCING_RULE IS
24136 
24137  TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
24138   TYPE CurTyp           IS REF CURSOR;
24139   c1                    CurTyp;
24140   lb_rowid              RowidTab;
24141 
24142   lv_column_names       VARCHAR2(5000);     -- Stores cocatenated column names
24143   lv_return             NUMBER;
24144   lv_error_text         VARCHAR2(250);
24145   lv_where_str          VARCHAR2(5000);
24146   lv_sql_stmt           VARCHAR2(5000);
24147   lv_cursor_stmt        VARCHAR2(5000);
24148 
24149   lv_default_sourcing_rule  msc_st_item_sourcing.sourcing_rule_name%TYPE
24150       := v_instance_code||':'||'SRULE' ;
24151   lv_default_assignment_set msc_st_item_sourcing.sourcing_rule_name%TYPE
24152       := 'ASET' ;
24153   lv_default_category_set  msc_st_category_sets.category_set_name%TYPE := NULL_CHAR ;
24154   lv_batch_id              msc_st_item_sourcing.batch_id%TYPE;
24155   lv_message_text          msc_errors.error_text%TYPE;
24156 
24157    ex_logging_err       EXCEPTION;
24158 
24159 
24160  CURSOR c2(p_batch_id NUMBER) IS
24161     SELECT rowid
24162     FROM   msc_st_item_sourcing
24163     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
24164     AND    sr_instance_code = v_instance_code
24165     AND    batch_id         = p_batch_id;
24166 
24167  CURSOR c3(p_batch_id NUMBER) IS
24168     SELECT max(rowid)
24169     FROM   msc_st_item_sourcing
24170     WHERE  process_flag     = G_IN_PROCESS
24171     AND    sr_instance_code = v_instance_code
24172     AND    batch_id         = p_batch_id
24173     AND    NVL(sourcing_rule_id,NULL_VALUE) = NULL_VALUE
24174     AND    deleted_flag      = SYS_NO
24175    GROUP BY sr_instance_code,company_name,
24176             organization_code,sourcing_rule_name;
24177 
24178  CURSOR c4(p_batch_id NUMBER) IS
24179     SELECT max(rowid)
24180     FROM   msc_st_item_sourcing
24181     WHERE  process_flag     = G_IN_PROCESS
24182     AND    sr_instance_code = v_instance_code
24183     AND    batch_id         = p_batch_id
24184     AND    NVL(assignment_set_id,NULL_VALUE) = NULL_VALUE
24185     AND    deleted_flag      = SYS_NO
24186     GROUP BY  sr_instance_code,company_name,
24187              assignment_set_name;
24188 
24189  CURSOR c5(p_batch_id NUMBER) IS
24190     SELECT max(rowid)
24191     FROM   msc_st_item_sourcing
24192     WHERE  process_flag     = G_IN_PROCESS
24193     AND    sr_instance_code = v_instance_code
24194     AND    batch_id         = p_batch_id
24195     AND    NVL(sr_receipt_id,NULL_VALUE) = NULL_VALUE
24196     AND    deleted_flag      = SYS_NO
24197     GROUP BY  sr_instance_code,company_name,
24198            organization_code, sourcing_rule_name,
24199            receipt_organization_code,receipt_org_instance_code,effective_date;
24200 
24201  CURSOR c6(p_batch_id NUMBER) IS
24202     SELECT max(rowid)
24203     FROM   msc_st_item_sourcing
24204     WHERE  process_flag     = G_IN_PROCESS
24205     AND    sr_instance_code = v_instance_code
24206     AND    batch_id         = p_batch_id
24207     AND    NVL(sr_source_id,NULL_VALUE) = NULL_VALUE
24208     AND    deleted_flag      = SYS_NO
24209     GROUP BY  sr_instance_code,company_name,
24210            organization_code, sourcing_rule_name,
24211            source_organization_code, source_org_instance_code,
24212            source_partner_site_code,source_partner_name,
24213            receipt_organization_code,receipt_org_instance_code,
24214            effective_date;
24215 
24216  CURSOR c7(p_batch_id NUMBER) IS
24217     SELECT max(rowid)
24218     FROM   msc_st_item_sourcing
24219     WHERE  process_flag     = G_IN_PROCESS
24220     AND    sr_instance_code = v_instance_code
24221     AND    batch_id         = p_batch_id
24222     AND    NVL(assignment_id,NULL_VALUE) = NULL_VALUE
24223     AND    deleted_flag      = SYS_NO
24224     GROUP BY sr_instance_code,company_name,
24225              organization_code,assignment_set_name,
24226              item_name, category_name ;
24227 
24228 
24229   BEGIN
24230 
24231   --Duplicate records check for the records whose source is XML
24232 
24233     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24234                  (p_app_short_name    => 'MSC',
24235                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
24236                   p_message_text      => lv_message_text,
24237                   p_error_text        => lv_error_text);
24238 
24239     IF lv_return <> 0 THEN
24240       RAISE ex_logging_err;
24241     END IF;
24242 
24243   v_sql_stmt := 01;
24244 
24245   lv_sql_stmt :=
24246   'UPDATE   msc_st_item_sourcing msis1'
24247   ||' SET      process_flag ='||G_ERROR_FLG||','
24248   ||'         error_text   = '||''''||lv_message_text||''''
24249   ||' WHERE     message_id < ( SELECT max(message_id) FROM msc_st_item_sourcing msis2'
24250   ||'                 WHERE msis2.sr_instance_code  = msis1.sr_instance_code'
24251   ||'         AND   NVL(msis2.organization_code,'||''''||NULL_CHAR||''''||') '
24252   ||'               = NVL(msis1.organization_code,'||''''||NULL_CHAR||''''||') '
24253   ||'         AND   NVL(msis2.sourcing_rule_name,'||''''||NULL_CHAR||''''||') '
24254   ||'               =  NVL(msis1.sourcing_rule_name,'||''''||NULL_CHAR||''''||') '
24255   ||'         AND   NVL(msis2.assignment_set_name,'||''''||NULL_CHAR||''''||') '
24256   ||'               = NVL(msis1.assignment_set_name,'||''''||NULL_CHAR||''''||') '
24257   ||'         AND   NVL(msis2.item_name,'||''''||NULL_CHAR||''''||') '
24258   ||'                = NVL(msis1.item_name,'||''''||NULL_CHAR||''''||') '
24259   ||'         AND   NVL(msis2.category_name,'||''''||NULL_CHAR||''''||') '
24260   ||'                = NVL(msis1.category_name,'||''''||NULL_CHAR||''''||') '
24261   ||'         AND   NVL(msis2.receipt_organization_code,'||''''||NULL_CHAR||''''||') '
24262   ||'               = NVL(msis1.receipt_organization_code,'||''''||NULL_CHAR||''''||') '
24263   ||'         AND   NVL(msis2.source_organization_code,'||''''||NULL_CHAR||''''||') '
24264   ||'               = NVL(msis1.source_organization_code,'||''''||NULL_CHAR||''''||') '
24265   ||'         AND   NVL(msis2.source_partner_name,'||''''||NULL_CHAR||''''||') '
24266   ||'               = NVL(msis1.source_partner_name,'||''''||NULL_CHAR||''''||') '
24267   ||'         AND   NVL(msis2.source_partner_site_code,'||''''||NULL_CHAR||''''||') '
24268   ||'               = NVL(msis1.source_partner_site_code,'||''''||NULL_CHAR||''''||') '
24269   ||'         AND   msis2.effective_date     = msis1.effective_date'
24270   ||'         AND   msis2.process_flag      = '||G_IN_PROCESS
24271   ||'         AND    NVL(msis2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
24272   ||'  AND       msis1.process_flag     = '||G_IN_PROCESS
24273   ||'  AND       msis1.sr_instance_code = :v_instance_code'
24274   ||'  AND     NVL(msis1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
24275 
24276 
24277          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24278 
24279   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
24280 
24281   --Duplicate records check for the records whose source is batch load
24282 
24283     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24284                  (p_app_short_name    => 'MSC',
24285                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
24286                   p_message_text      => lv_message_text,
24287                   p_error_text        => lv_error_text);
24288 
24289     IF lv_return <> 0 THEN
24290       RAISE ex_logging_err;
24291     END IF;
24292 
24293   v_sql_stmt := 02;
24294 
24295   lv_sql_stmt :=
24296   'UPDATE   msc_st_item_sourcing msis1'
24297   ||' SET      process_flag ='||G_ERROR_FLG||','
24298   ||'         error_text   = '||''''||lv_message_text||''''
24299   ||'  WHERE   EXISTS ( SELECT 1 FROM msc_st_item_sourcing msis2'
24300   ||'                 WHERE msis2.sr_instance_code  = msis1.sr_instance_code'
24301   ||'         AND   NVL(msis2.organization_code,'||''''||NULL_CHAR||''''||') '
24302   ||'               = NVL(msis1.organization_code,'||''''||NULL_CHAR||''''||') '
24303   ||'         AND   NVL(msis2.sourcing_rule_name,'||''''||NULL_CHAR||''''||') '
24304   ||'               =  NVL(msis1.sourcing_rule_name,'||''''||NULL_CHAR||''''||') '
24305   ||'         AND   NVL(msis2.assignment_set_name,'||''''||NULL_CHAR||''''||') '
24306   ||'               = NVL(msis1.assignment_set_name,'||''''||NULL_CHAR||''''||') '
24307   ||'         AND   NVL(msis2.item_name,'||''''||NULL_CHAR||''''||') '
24308   ||'                = NVL(msis1.item_name,'||''''||NULL_CHAR||''''||') '
24309   ||'         AND   NVL(msis2.category_name,'||''''||NULL_CHAR||''''||') '
24310   ||'                = NVL(msis1.category_name,'||''''||NULL_CHAR||''''||') '
24311   ||'         AND   NVL(msis2.receipt_organization_code,'||''''||NULL_CHAR||''''||') '
24312   ||'               = NVL(msis1.receipt_organization_code,'||''''||NULL_CHAR||''''||') '
24313   ||'         AND   NVL(msis2.source_organization_code,'||''''||NULL_CHAR||''''||') '
24314   ||'               = NVL(msis1.source_organization_code,'||''''||NULL_CHAR||''''||') '
24315   ||'         AND   NVL(msis2.source_partner_name,'||''''||NULL_CHAR||''''||') '
24316   ||'               = NVL(msis1.source_partner_name,'||''''||NULL_CHAR||''''||') '
24317   ||'         AND   NVL(msis2.source_partner_site_code,'||''''||NULL_CHAR||''''||') '
24318   ||'               = NVL(msis1.source_partner_site_code,'||''''||NULL_CHAR||''''||') '
24319   ||'         AND   msis2.effective_date     = msis1.effective_date'
24320   ||'         AND   msis2.process_flag      = '||G_IN_PROCESS
24321   ||'         AND NVL(msis2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
24322   ||'         GROUP BY sr_instance_code,company_name,organization_code,sourcing_rule_name,'
24323   ||'         assignment_set_name,item_name, category_name,receipt_organization_code,'
24324   ||'         source_organization_code, source_partner_name, source_partner_site_code,effective_date'
24325   ||'         HAVING COUNT(*) >1 )'
24326   ||'  AND     msis1.process_flag              ='||G_IN_PROCESS
24327   ||'  AND     msis1.sr_instance_code          = :v_instance_code'
24328   ||'  AND     NVL(msis1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
24329 
24330 
24331          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24332 
24333   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
24334 
24335 
24336   lv_column_names :=
24337   'SOURCING_RULE_NAME              ||''~''||'
24338   ||'EFFECTIVE_DATE                ||''~''||'
24339   ||'ALLOCATION_PERCENT            ||''~''||'
24340   ||'SOURCE_TYPE                   ||''~''||'
24341   ||'PLANNING_ACTIVE               ||''~''||'
24342   ||'ASSIGNMENT_TYPE               ||''~''||'
24343   ||'ORGANIZATION_CODE             ||''~''||'
24344   ||'SR_INSTANCE_CODE              ||''~''||'
24345   ||'DISABLE_DATE                  ||''~''||'
24346   ||'RANK                          ||''~''||'
24347   ||'DESCRIPTION                   ||''~''||'
24348   ||'SHIP_METHOD                   ||''~''||'
24349   ||'ITEM_NAME                     ||''~''||'
24350   ||'CATEGORY_NAME                 ||''~''||'
24351   ||'RECEIPT_ORGANIZATION_CODE     ||''~''||'
24352   ||'RECEIPT_ORG_INSTANCE_CODE     ||''~''||'
24353   ||'SOURCE_ORGANIZATION_CODE      ||''~''||'
24354   ||'SOURCE_ORG_INSTANCE_CODE      ||''~''||'
24355   ||'SOURCE_PARTNER_NAME           ||''~''||'
24356   ||'SOURCE_PARTNER_SITE_CODE      ||''~''||'
24357   ||'COMPANY_NAME                  ||''~''||'
24358   ||'ASSIGNMENT_SET_NAME           ||''~''||'
24359   ||'DELETED_FLAG' ;
24360 
24361   BEGIN
24362      -- Get the default category set
24363        lv_sql_stmt := 01;
24364        lv_sql_stmt :=
24365        'SELECT     category_set_name'
24366        ||' FROM    msc_category_sets '
24367        ||' WHERE   sr_instance_id = :v_instance_id'
24368        ||' AND     default_flag   = 1' ;
24369 
24370 
24371             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24372 
24373        EXECUTE IMMEDIATE lv_sql_stmt
24374                INTO      lv_default_category_set
24375                USING     v_instance_id;
24376        EXCEPTION
24377            WHEN NO_DATA_FOUND THEN
24378          BEGIN
24379 
24380          lv_sql_stmt := 13;
24381          lv_sql_stmt :=
24382          'SELECT     category_set_name '
24383          ||' FROM    msc_st_category_sets '
24384          ||' WHERE   sr_instance_id = :v_instance_id'
24385          ||' AND     default_flag   = 1'
24386          ||' AND     deleted_flag   = '||SYS_NO
24387          ||' AND     process_flag   ='|| G_VALID ;
24388 
24389 
24390              MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24391 
24392          EXECUTE IMMEDIATE lv_sql_stmt
24393            INTO      lv_default_category_set
24394            USING     v_instance_id;
24395          EXCEPTION
24396            WHEN NO_DATA_FOUND THEN
24397            -- Set the  message
24398            lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24399                      (p_app_short_name    => 'MSC',
24400                       p_error_code        => 'MSC_PP_DEF_CAT_NOT_EXISTS',
24401                       p_message_text      => lv_message_text,
24402                       p_error_text        => lv_error_text);
24403 
24404            IF lv_return <> 0 THEN
24405                 RAISE ex_logging_err;
24406            END IF;
24407            WHEN OTHERS THEN
24408            lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_SOURCING_RULE'||'('
24409                              ||v_sql_stmt||')'|| SQLERRM, 1, 240);
24410         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
24411 
24412           END ;
24413     WHEN OTHERS THEN
24414       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_CATEGORY'||'('
24415                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
24416      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
24417 
24418      END;
24419 
24420 
24421 
24422  -- Now processing by batch
24423 
24424   LOOP
24425       v_sql_stmt := 03;
24426       lv_cursor_stmt :=
24427       ' SELECT msc_st_batch_id_s.NEXTVAL '
24428       ||' FROM   dual';
24429 
24430       OPEN  c1 FOR lv_cursor_stmt;
24431       FETCH c1 INTO lv_batch_id;
24432       CLOSE c1;
24433 
24434       v_sql_stmt := 04;
24435       lv_sql_stmt :=
24436       ' UPDATE msc_st_item_sourcing '
24437       ||' SET    batch_id  = :lv_batch_id'
24438       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
24439       ||' AND    sr_instance_code               = :v_instance_code'
24440       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
24441       ||' AND    rownum                        <= '||v_batch_size;
24442 
24443 
24444          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24445 
24446       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
24447 
24448       EXIT WHEN SQL%NOTFOUND;
24449 
24450       OPEN c2(lv_batch_id);
24451       FETCH c2 BULK COLLECT INTO lb_rowid;
24452       CLOSE c2;
24453 
24454       v_sql_stmt := 05;
24455       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
24456       UPDATE msc_st_item_sourcing
24457       SET  st_transaction_id = msc_st_item_sourcing_s.NEXTVAL,
24458            refresh_id        = v_refresh_id,
24459            last_update_date  = v_current_date,
24460            last_updated_by   = v_current_user,
24461            creation_date     = v_current_date,
24462            created_by        = v_current_user
24463      WHERE  rowid           = lb_rowid(j);
24464 
24465     -- Set the error message
24466 
24467     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24468                      (p_app_short_name    => 'MSC',
24469                       p_error_code        => 'MSC_PP_NO_DELETION',
24470                       p_message_text      => lv_message_text,
24471                       p_error_text        => lv_error_text,
24472                       p_token1            => 'TABLE_NAME',
24473                       p_token_value1      => 'MSC_ST_ITEM_SOURCING');
24474 
24475     IF lv_return <> 0 THEN
24476         RAISE ex_logging_err;
24477     END IF;
24478 
24479      --Deletion is not allowed on this table.
24480 
24481      v_sql_stmt := 06;
24482      lv_sql_stmt :=
24483       ' UPDATE   msc_st_item_sourcing '
24484       ||' SET      process_flag     = '||G_ERROR_FLG||','
24485       ||'          error_text       = '||''''||lv_message_text||''''
24486       ||' WHERE    deleted_flag     = '||SYS_YES
24487       ||' AND      process_flag     = '||G_IN_PROCESS
24488       ||' AND      batch_id         = :lv_batch_id'
24489       ||' AND      sr_instance_code = :v_instance_code';
24490 
24491 
24492          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24493 
24494     EXECUTE IMMEDIATE   lv_sql_stmt
24495             USING       lv_batch_id,
24496                         v_instance_code;
24497 
24498     -- Set the error message
24499     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24500                      (p_app_short_name    => 'MSC',
24501                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
24502                       p_message_text      => lv_message_text,
24503                       p_error_text        => lv_error_text,
24504                       p_token1            => 'COLUMN_NAME',
24505                       p_token_value1      => 'DELETED_FLAG',
24506                       p_token2            => 'DEFAULT_VALUE',
24507                       p_token_value2      =>  SYS_NO  );
24508 
24509       IF lv_return <> 0 THEN
24510         RAISE ex_logging_err;
24511       END IF;
24512 
24513    -- Check whether delete flag value is in (SYS_YES,SYS_NO),if not default to SYS_NO
24514 
24515      lv_where_str :=
24516      ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
24517 
24518      lv_return := MSC_ST_UTIL.LOG_ERROR
24519                     (p_table_name        => 'MSC_ST_ITEM_SOURCING',
24520                      p_instance_code     => v_instance_code,
24521                      p_row               => lv_column_names,
24522                      p_severity          => G_SEV_WARNING,
24523                      p_message_text      => lv_message_text,
24524                      p_error_text        => lv_error_text,
24525                      p_batch_id          => lv_batch_id,
24526                      p_where_str         => lv_where_str,
24527                      p_col_name          => 'DELETED_FLAG',
24528                      p_debug             => v_debug,
24529                      p_default_value     => SYS_NO);
24530 
24531 
24532       IF lv_return <> 0 THEN
24533           RAISE ex_logging_err;
24534       END IF;
24535 
24536      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24537                      (p_app_short_name    => 'MSC',
24538                       p_error_code        => 'MSC_PP_INVALID_VALUE',
24539                       p_message_text      => lv_message_text,
24540                       p_error_text        => lv_error_text,
24541                       p_token1            => 'COLUMN_NAME',
24542                       p_token_value1      => 'ASSIGNMENT_TYPE');
24543 
24544       IF lv_return <> 0 THEN
24545         RAISE ex_logging_err;
24546       END IF;
24547 
24548     -- Error out record where assignment_type is NOT IN (1,2,3,4,5,6)
24549      v_sql_stmt := 07;
24550      lv_sql_stmt:=
24551      'UPDATE          msc_st_item_sourcing'
24552      ||' SET          process_flag          = '||G_ERROR_FLG||','
24553      ||'              error_text   = '||''''||lv_message_text||''''
24554      ||' WHERE        sr_instance_code      = :v_instance_code'
24555      ||' AND          batch_id              = :lv_batch_id'
24556      ||' AND          process_flag          = '||G_IN_PROCESS
24557      ||' AND          NVL(assignment_type,'||NULL_VALUE||') NOT IN (1,2,3,4,5,6)' ;
24558 
24559 
24560             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24561 
24562       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24563 
24564      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24565                      (p_app_short_name    => 'MSC',
24566                       p_error_code        => 'MSC_PP_INVALID_VALUE',
24567                       p_message_text      => lv_message_text,
24568                       p_error_text        => lv_error_text,
24569                       p_token1            => 'COLUMN_NAME',
24570                       p_token_value1      => 'SOURCE_TYPE');
24571 
24572       IF lv_return <> 0 THEN
24573         RAISE ex_logging_err;
24574       END IF;
24575 
24576 
24577     -- Error out record where source_type is NOT IN (1,2,3)
24578 
24579      v_sql_stmt := 08;
24580      lv_sql_stmt:=
24581      'UPDATE          msc_st_item_sourcing'
24582      ||' SET          process_flag          = '||G_ERROR_FLG||','
24583      ||'              error_text   = '||''''||lv_message_text||''''
24584      ||' WHERE        sr_instance_code      = :v_instance_code'
24585      ||' AND          batch_id              = :lv_batch_id'
24586      ||' AND          process_flag          = '||G_IN_PROCESS
24587      ||' AND          NVL(source_type,'||NULL_VALUE||') NOT IN (1,2,3)' ;
24588 
24589 
24590             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24591 
24592      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24593 
24594 
24595       -- Set the  message
24596 
24597      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24598                      (p_app_short_name    => 'MSC',
24599                       p_error_code        => 'MSC_PP_INVALID_VALUE',
24600                       p_message_text      => lv_message_text,
24601                       p_error_text        => lv_error_text,
24602                       p_token1            => 'COLUMN_NAME',
24603                       p_token_value1      => 'ORGANIZATION_CODE');
24604 
24605       IF lv_return <> 0 THEN
24606         RAISE ex_logging_err;
24607       END IF;
24608 
24609     -- Udpate organization_id for all the records
24610 
24611      lv_return :=
24612      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_ITEM_SOURCING',
24613                                     p_org_partner_name  =>'ORGANIZATION_CODE',
24614                                     p_org_partner_id    =>'ORGANIZATION_ID',
24615                                     p_instance_code     => v_instance_code,
24616                                     p_partner_type      => G_ORGANIZATION,
24617                                     p_severity          => G_SEV3_ERROR,
24618                                     p_message_text      => lv_message_text,
24619                                     p_batch_id          =>lv_batch_id,
24620                                     p_debug             => v_debug,
24621                                     p_error_text        =>lv_error_text);
24622 
24623      IF(lv_return <> 0) then
24624         RAISE ex_logging_err;
24625      END IF;
24626 
24627      -- Default sourcing rule
24628 
24629      v_sql_stmt := 09;
24630 
24631      lv_sql_stmt:=
24632      'UPDATE       msc_st_item_sourcing'
24633      ||' SET          sourcing_rule_name    = '||''''||lv_default_sourcing_rule||''''
24634      ||' WHERE        sr_instance_code      = :v_instance_code'
24635      ||' AND          batch_id              = :lv_batch_id'
24636      ||' AND          process_flag          ='||G_IN_PROCESS
24637      ||' AND          NVL(sourcing_rule_name,'||''''||NULL_CHAR||''''||') '
24638      ||'              =  '||''''||NULL_CHAR||''''                  ;
24639 
24640 
24641             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24642 
24643      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24644 
24645 
24646     -- Default assignment set name
24647 
24648      v_sql_stmt := 10;
24649 
24650      lv_sql_stmt:=
24651      'UPDATE          msc_st_item_sourcing'
24652      ||' SET          assignment_set_name    = '||''''||lv_default_assignment_set||''''
24653      ||' WHERE        sr_instance_code      = :v_instance_code'
24654      ||' AND          batch_id              = :lv_batch_id'
24655      ||' AND          process_flag          ='||G_IN_PROCESS
24656      ||' AND          NVL(assignment_set_name,'||''''||NULL_CHAR||''''||')'
24657      ||'              = '||''''||NULL_CHAR||''''  ;
24658 
24659 
24660             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24661 
24662      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24663 
24664 
24665     -- Set the error message
24666     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24667                      (p_app_short_name    => 'MSC',
24668                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
24669                       p_message_text      => lv_message_text,
24670                       p_error_text        => lv_error_text,
24671                       p_token1            => 'COLUMN_NAME',
24672                       p_token_value1      => 'PLANNING_ACTIVE',
24673                       p_token2            => 'DEFAULT_VALUE',
24674                       p_token_value2      =>  SYS_YES  );
24675 
24676       IF lv_return <> 0 THEN
24677         RAISE ex_logging_err;
24678       END IF;
24679 
24680 
24681      -- Default planning_active as 1
24682 
24683      lv_where_str := ' AND NVL(planning_active,'||NULL_VALUE||') NOT IN (1,2)';
24684 
24685      lv_return := MSC_ST_UTIL.LOG_ERROR
24686                     (p_table_name        => 'MSC_ST_ITEM_SOURCING',
24687                      p_instance_code     => v_instance_code,
24688                      p_row               => lv_column_names,
24689                      p_severity          => G_SEV_WARNING,
24690                      p_message_text      => lv_message_text,
24691                      p_error_text        => lv_error_text,
24692                      p_batch_id          => lv_batch_id,
24693                      p_where_str         => lv_where_str,
24694                      p_col_name          => 'PLANNING_ACTIVE',
24695                      p_debug             => v_debug,
24696                      p_default_value     => SYS_YES );
24697 
24698 
24699      IF(lv_return <> 0) then
24700         RAISE ex_logging_err;
24701      END IF;
24702 
24703       -- Set the  message
24704 
24705      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24706                      (p_app_short_name    => 'MSC',
24707                       p_error_code        => 'MSC_PP_INVALID_VALUE',
24708                       p_message_text      => lv_message_text,
24709                       p_error_text        => lv_error_text,
24710                       p_token1            => 'COLUMN_NAME',
24711                       p_token_value1      => 'RECEIPT_ORGANIZATION_CODE');
24712 
24713       IF lv_return <> 0 THEN
24714         RAISE ex_logging_err;
24715       END IF;
24716 
24717      -- Update receipt_organization_id
24718      -- As of now our assumption is that receipt_org_instance_code is same as
24719      -- v_instance_code ,
24720 
24721      lv_return :=
24722      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_ITEM_SOURCING',
24723                                     p_org_partner_name  =>'RECEIPT_ORGANIZATION_CODE',
24724                                     p_org_partner_id    =>'RECEIPT_ORGANIZATION_ID',
24725                                     p_instance_code     => v_instance_code,
24726                                     p_partner_type      => G_ORGANIZATION,
24727                                     p_severity          => G_SEV3_ERROR,
24728                                     p_message_text      => lv_message_text,
24729                                     p_batch_id          =>lv_batch_id,
24730                                     p_debug             => v_debug,
24731                                     p_error_text        =>lv_error_text);
24732 
24733      IF(lv_return <> 0) then
24734         RAISE ex_logging_err;
24735      END IF;
24736 
24737 
24738    /*  -- Update sr_receipt_org_instance_id
24739       v_sql_stmt := 09;
24740 
24741      lv_sql_stmt:=
24742      'UPDATE          msc_st_item_sourcing  msis'
24743      ||' SET          receipt_org_instance_id = ( SELECT instance_id FROM msc_apps_instances mai'
24744      ||'                                          WHERE mai.instance_code =  msis.sr_instance_code)'
24745      ||' WHERE        sr_instance_code      = :v_instance_code'
24746      ||' AND          batch_id              = :lv_batch_id'
24747      ||' AND          process_flag          ='||G_IN_PROCESS
24748      ||' AND          NVL(receipt_org_instance_code,'||''''||NULL_CHAR||''''||')'
24749      ||'             <> '||''''||NULL_CHAR||'''' ;
24750 
24751 
24752             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24753 
24754      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;  */
24755 
24756      v_sql_stmt := 11;
24757 
24758      lv_sql_stmt:=
24759      'UPDATE          msc_st_item_sourcing  msis'
24760      ||' SET          receipt_org_instance_code  = :v_instance_code ,'
24761      ||'              source_org_instance_code   = :v_instance_code'
24762      ||' WHERE        sr_instance_code      = :v_instance_code'
24763      ||' AND          batch_id              = :lv_batch_id'
24764      ||' AND          process_flag          ='||G_IN_PROCESS  ;
24765 
24766 
24767             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24768 
24769      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,v_instance_code,
24770                                          v_instance_code,lv_batch_id;
24771 
24772       -- Set the  message
24773 
24774      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24775                      (p_app_short_name    => 'MSC',
24776                       p_error_code        => 'MSC_PP_INVALID_VALUE',
24777                       p_message_text      => lv_message_text,
24778                       p_error_text        => lv_error_text,
24779                       p_token1            => 'COLUMN_NAME',
24780                       p_token_value1      => 'ITEM_NAME');
24781 
24782       IF lv_return <> 0 THEN
24783         RAISE ex_logging_err;
24784       END IF;
24785 
24786 
24787    -- Derive inventory_item_id
24788 
24789      lv_return :=
24790      msc_st_util.derive_item_id(p_table_name => 'MSC_ST_ITEM_SOURCING',
24791                              p_item_col_name => 'ITEM_NAME',
24792                              p_item_col_id   => 'INVENTORY_ITEM_ID',
24793                              p_instance_code => v_instance_code,
24794                              p_instance_id   => v_instance_id,
24795                              p_severity      => G_SEV3_ERROR,
24796                              p_message_text  => lv_message_text,
24797                              p_batch_id      => lv_batch_id,
24798                              p_debug         => v_debug,
24799                              p_error_text    => lv_error_text,
24800                              p_check_org     => FALSE);
24801 
24802      IF(lv_return <> 0) then
24803         RAISE ex_logging_err;
24804      END IF;
24805 
24806 
24807    -- If assignment_type = 3 or 6 ITEM_NAME should be valid
24808 
24809     v_sql_stmt:= 12;
24810     lv_sql_stmt:=
24811     'UPDATE     msc_st_item_sourcing'
24812     ||'  SET    process_flag     = '||G_ERROR_FLG||','
24813     ||'         error_text   = '||''''||lv_message_text||''''
24814     ||'  WHERE  (assignment_type IN (3,6)  '
24815     ||'  AND      NVL(inventory_item_id,'||NULL_VALUE||')'
24816     ||'           = '||NULL_VALUE||')'
24817     ||'  AND      sr_instance_code = :v_instance_code'
24818     ||'  AND      process_flag     = '||G_IN_PROCESS
24819     ||'  AND      batch_id         = :lv_batch_id';
24820 
24821 
24822             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24823 
24824      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24825 
24826       -- Set the  message
24827 
24828      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24829                      (p_app_short_name    => 'MSC',
24830                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
24831                       p_message_text      => lv_message_text,
24832                       p_error_text        => lv_error_text,
24833                       p_token1            => 'COLUMN_NAME',
24834                       p_token_value1      => 'CATEGORY_NAME');
24835 
24836       IF lv_return <> 0 THEN
24837         RAISE ex_logging_err;
24838       END IF;
24839 
24840     -- If assignment_type = 2 and 5 then category_name should not be NULL
24841     v_sql_stmt := 13;
24842     lv_sql_stmt :=
24843     'UPDATE      msc_st_item_sourcing '
24844     ||'  SET     process_flag     = '||G_ERROR_FLG||','
24845     ||'          error_text   = '||''''||lv_message_text||''''
24846     ||'  WHERE   NVL(category_name,'||''''||NULL_CHAR||''''||') '
24847     ||'          = '||''''||NULL_CHAR||''''
24848     ||'  AND     assignment_type IN (2,5) '
24849     ||'  AND     sr_instance_code = :v_instance_code '
24850     ||'  AND     process_flag     ='||G_IN_PROCESS
24851     ||'  AND     batch_id         = :lv_batch_id';
24852 
24853 
24854             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24855 
24856     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24857 
24858  -- Update categroy_set_id
24859 
24860   v_sql_stmt :=14;
24861 
24862   lv_sql_stmt :=
24863   'UPDATE          MSC_ST_ITEM_SOURCING  msis'
24864   ||' SET           category_set_id = ( SELECT  local_id FROM  msc_local_id_misc mlim'
24865   ||'                              WHERE    entity_name = ''SR_CATEGORY_SET_ID'' '
24866   ||'                              AND      instance_id = '||v_instance_id
24867   ||'                              AND      mlim.char1 =  msis.sr_instance_code'
24868   ||'              AND   NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
24869   ||'              =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
24870   ||'              AND      mlim.char3 = '||''''||lv_default_category_set||''''||')'
24871   ||' WHERE        msis.assignment_type IN (2,5)'
24872   ||' AND          sr_instance_code = :v_instance_code'
24873   ||' AND          process_flag     = '||G_IN_PROCESS
24874   ||' AND          batch_id         = :lv_batch_id';
24875 
24876 
24877             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24878 
24879   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24880 
24881 
24882  -- Now derive sr_category_id from LID table
24883 
24884   v_sql_stmt :=15;
24885 
24886   lv_sql_stmt :=
24887   'UPDATE          MSC_ST_ITEM_SOURCING  msis'
24888   ||' SET           category_id = ( SELECT  local_id FROM  msc_local_id_misc mlim'
24889   ||'                              WHERE    entity_name = ''SR_CATEGORY_ID'' '
24890   ||'                              AND      instance_id = '||v_instance_id
24891   ||'                              AND      mlim.char1 =  msis.sr_instance_code'
24892   ||'              AND   NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
24893   ||'              =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
24894   ||'              AND      mlim.char3 = msis.category_name )'
24895   ||' WHERE        msis.assignment_type IN (2,5)'
24896   ||' AND          NVL(category_name,'||''''||NULL_CHAR||''''||') '
24897   ||'                   <> '||''''||NULL_CHAR||''''
24898   ||' AND          sr_instance_code = :v_instance_code'
24899   ||' AND          process_flag     = '||G_IN_PROCESS
24900   ||' AND          batch_id         = :lv_batch_id';
24901 
24902 
24903             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24904 
24905   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24906 
24907    -- Set the message
24908 
24909      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24910                      (p_app_short_name    => 'MSC',
24911                       p_error_code        => 'MSC_PP_INVALID_VALUE',
24912                       p_message_text      => lv_message_text,
24913                       p_error_text        => lv_error_text,
24914                       p_token1            => 'COLUMN_NAME',
24915                       p_token_value1      => 'CATEGORY_NAME');
24916 
24917       IF lv_return <> 0 THEN
24918         RAISE ex_logging_err;
24919       END IF;
24920 
24921    -- Error our records if category_id/category_set_id  is NULL
24922    lv_sql_stmt :=
24923     'UPDATE     msc_st_item_sourcing '
24924     ||'  SET    process_flag     = '||G_ERROR_FLG||','
24925     ||'         error_text   = '||''''||lv_message_text||''''
24926     ||'  WHERE  (NVL(category_id,'||NULL_VALUE||') = '||NULL_VALUE
24927     ||'  OR     NVL(category_set_id,'||NULL_VALUE||') = '||NULL_VALUE||')'
24928     ||'  AND    assignment_type IN (2,5) '
24929     ||'  AND    sr_instance_code = :v_instance_code '
24930     ||'  AND    process_flag     ='||G_IN_PROCESS
24931     ||'  AND    batch_id         = :lv_batch_id';
24932 
24933 
24934             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24935 
24936  EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24937 
24938      -- Set the message
24939 
24940      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24941                      (p_app_short_name    => 'MSC',
24942                       p_error_code        => 'MSC_PP_INVALID_VALUE',
24943                       p_message_text      => lv_message_text,
24944                       p_error_text        => lv_error_text,
24945                       p_token1            => 'COLUMN_NAME',
24946                       p_token_value1      => 'SOURCE_ORGANIZATION_CODE');
24947 
24948       IF lv_return <> 0 THEN
24949         RAISE ex_logging_err;
24950       END IF;
24951 
24952 
24953    -- If source_type = 2(make at) then source_partner_name should be NULL
24954    -- And source_organization_code should be same as organization_code
24955 
24956    v_sql_stmt :=16;
24957 
24958    lv_sql_stmt :=
24959    'UPDATE        MSC_ST_ITEM_SOURCING  msis'
24960    ||'  SET       process_flag     ='||G_ERROR_FLG||','
24961    ||'            error_text   = '||''''||lv_message_text||''''
24962    ||'  WHERE     NVL(source_partner_name,'||''''||NULL_CHAR||''''||') '
24963    ||'            <> '||''''||NULL_CHAR||''''
24964    ||'  AND       NVL(source_organization_code,'||''''||NULL_CHAR||''''||') '
24965    ||'            <> NVL(organization_code,'||''''||NULL_CHAR||''''||') '
24966    ||'  AND       source_type      = 2 '
24967    ||'  AND       sr_instance_code = :v_instance_code '
24968    ||'  AND       process_flag     = '||G_IN_PROCESS
24969    ||'  AND       batch_id         = :lv_batch_id';
24970 
24971 
24972             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
24973 
24974   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
24975 
24976    -- Set the message
24977 
24978      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
24979                      (p_app_short_name    => 'MSC',
24980                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
24981                       p_message_text      => lv_message_text,
24982                       p_error_text        => lv_error_text,
24983                       p_token1            => 'COLUMN_NAME',
24984                       p_token_value1      => 'SOURCE_PARTNER_NAME');
24985 
24986       IF lv_return <> 0 THEN
24987         RAISE ex_logging_err;
24988       END IF;
24989 
24990 
24991    -- If source_type = 3 then , source_partner_name should not be NULL
24992 
24993    v_sql_stmt :=17;
24994 
24995    lv_sql_stmt :=
24996    'UPDATE        MSC_ST_ITEM_SOURCING  msis'
24997    ||'  SET       process_flag     ='||G_ERROR_FLG||','
24998    ||'            error_text   = '||''''||lv_message_text||''''
24999    ||'  WHERE    NVL(source_partner_name,'||''''||NULL_CHAR||''''||') '
25000    ||'         = '||''''||NULL_CHAR||''''
25001    ||'  AND      source_type      = 3 '
25002    ||'  AND      sr_instance_code = :v_instance_code '
25003    ||'  AND      process_flag     = '||G_IN_PROCESS
25004    ||'  AND      batch_id         = :lv_batch_id';
25005 
25006 
25007             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25008 
25009   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25010 
25011  -- Set the message
25012 
25013      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
25014                      (p_app_short_name    => 'MSC',
25015                       p_error_code        => 'MSC_PP_INVALID_VALUE',
25016                       p_message_text      => lv_message_text,
25017                       p_error_text        => lv_error_text,
25018                       p_token1            => 'COLUMN_NAME',
25019                       p_token_value1      => 'SOURCE_PARTNER_NAME');
25020 
25021       IF lv_return <> 0 THEN
25022         RAISE ex_logging_err;
25023       END IF;
25024 
25025 
25026    -- Now derive the source_partner_id
25027     lv_return :=
25028     msc_st_util.derive_partner_org_id(p_table_name      => 'MSC_ST_ITEM_SOURCING',
25029                                     p_org_partner_name  =>'SOURCE_PARTNER_NAME',
25030                                     p_org_partner_id    =>'SOURCE_PARTNER_ID',
25031                                     p_instance_code     => v_instance_code,
25032                                     p_partner_type      => G_VENDOR,
25033                                     p_severity          => G_SEV3_ERROR,
25034                                     p_message_text      => lv_message_text,
25035                                     p_batch_id          => lv_batch_id,
25036                                     p_debug             => v_debug,
25037                                     p_error_text        => lv_error_text);
25038 
25039      IF(lv_return <> 0) then
25040         RAISE ex_logging_err;
25041      END IF;
25042 
25043      -- Set the message
25044 
25045      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
25046                      (p_app_short_name    => 'MSC',
25047                       p_error_code        => 'MSC_PP_INVALID_VALUE',
25048                       p_message_text      => lv_message_text,
25049                       p_error_text        => lv_error_text,
25050                       p_token1            => 'COLUMN_NAME',
25051                       p_token_value1      => 'SOURCE_PARTNER_SITE_CODE');
25052 
25053       IF lv_return <> 0 THEN
25054         RAISE ex_logging_err;
25055       END IF;
25056 
25057 
25058    -- Derive source_partner_site_id
25059 
25060      lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
25061             (p_table_name          => 'MSC_ST_ITEM_SOURCING',
25062             p_partner_name         => 'SOURCE_PARTNER_NAME',
25063             p_partner_site_code    => 'SOURCE_PARTNER_SITE_CODE',
25064             p_partner_site_id      => 'SOURCE_PARTNER_SITE_ID',
25065             p_instance_code        => v_instance_code,
25066             p_partner_type         => G_VENDOR,
25067             p_batch_id             => lv_batch_id ,
25068             p_severity             => G_SEV3_ERROR,
25069             p_message_text         => lv_message_text,
25070             p_debug                => v_debug,
25071             p_error_text           => lv_error_text);
25072 
25073 
25074      IF(lv_return <> 0) then
25075         RAISE ex_logging_err;
25076      END IF;
25077 
25078     -- Set the message
25079 
25080      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
25081                      (p_app_short_name    => 'MSC',
25082                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
25083                       p_message_text      => lv_message_text,
25084                       p_error_text        => lv_error_text,
25085                       p_token1            => 'COLUMN_NAME',
25086                       p_token_value1      => 'SOURCE_ORGANIZATION_CODE');
25087 
25088       IF lv_return <> 0 THEN
25089         RAISE ex_logging_err;
25090       END IF;
25091 
25092 
25093    -- If source_type = 1 then , source_organization_code should not be NULL
25094 
25095    v_sql_stmt :=17;
25096 
25097    lv_sql_stmt :=
25098    'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25099    ||'  SET       process_flag     ='||G_ERROR_FLG||','
25100    ||'            error_text   = '||''''||lv_message_text||''''
25101    ||'  WHERE    NVL(source_organization_code,'||''''||NULL_CHAR||''''||') '
25102    ||'         = '||''''||NULL_CHAR||''''
25103    ||'  AND      source_type      = 1 '
25104    ||'  AND      sr_instance_code = :v_instance_code '
25105    ||'  AND      process_flag     = '||G_IN_PROCESS
25106    ||'  AND      batch_id         = :lv_batch_id';
25107 
25108 
25109             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25110 
25111   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25112 
25113 
25114      -- Set the message
25115 
25116      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
25117                      (p_app_short_name    => 'MSC',
25118                       p_error_code        => 'MSC_PP_INVALID_VALUE',
25119                       p_message_text      => lv_message_text,
25120                       p_error_text        => lv_error_text,
25121                       p_token1            => 'COLUMN_NAME',
25122                       p_token_value1      => 'SOURCE_ORGANIZATION_CODE');
25123 
25124       IF lv_return <> 0 THEN
25125         RAISE ex_logging_err;
25126       END IF;
25127 
25128     -- Derive source_organization_id
25129 
25130     lv_return :=
25131     msc_st_util.derive_partner_org_id(p_table_name     => 'MSC_ST_ITEM_SOURCING',
25132                                     p_org_partner_name  =>'SOURCE_ORGANIZATION_CODE',
25133                                     p_org_partner_id    =>'SOURCE_ORGANIZATION_ID',
25134                                     p_instance_code     => v_instance_code,
25135                                     p_partner_type      => G_ORGANIZATION,
25136                                     p_severity          => G_SEV3_ERROR,
25137                                     p_message_text      => lv_message_text,
25138                                     p_batch_id          => lv_batch_id,
25139                                     p_debug             => v_debug,
25140                                     p_error_text        => lv_error_text);
25141 
25142      IF(lv_return <> 0) then
25143         RAISE ex_logging_err;
25144      END IF;
25145 
25146    --  Udpate SOURCING_RULE_ID
25147    v_sql_stmt := 18;
25148 
25149   lv_sql_stmt:=
25150   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25151   ||' SET        sourcing_rule_id = (SELECT  local_id '
25152   ||'            FROM  msc_local_id_misc mlim'
25153   ||'            WHERE    mlim.entity_name= ''SOURCING_RULE_ID'' '
25154   ||'            AND      mlim.char1 =  msis.sr_instance_code'
25155   ||'            AND   NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25156   ||'             =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25157   ||'            AND   NVL(mlim.char3,'||''''||NULL_CHAR||''''||') '
25158   ||'             =    NVL(msis.organization_code,'||''''||NULL_CHAR||''''||') '
25159   ||'            AND      mlim.char4 =  msis.sourcing_rule_name)'
25160   ||' WHERE      sr_instance_code = :v_instance_code'
25161   ||' AND        process_flag     = '||G_IN_PROCESS
25162   ||' AND        batch_id         = :lv_batch_id';
25163 
25164 
25165             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25166 
25167   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25168 
25169   --  Udpate ASSIGNMENT_SET_ID
25170    v_sql_stmt := 19;
25171   lv_sql_stmt:=
25172   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25173   ||' SET        assignment_set_id = (SELECT  local_id'
25174   ||'            FROM  msc_local_id_misc mlim'
25175   ||'            WHERE    mlim.entity_name= ''ASSIGNMENT_SET_ID'' '
25176   ||'            AND      mlim.char1 =  msis.sr_instance_code'
25177   ||'            AND   NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25178   ||'             =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25179   ||'            AND      mlim.char3 =  msis.assignment_set_name)'
25180   ||' WHERE      sr_instance_code = :v_instance_code'
25181   ||' AND        process_flag     = '||G_IN_PROCESS
25182   ||' AND        batch_id         = :lv_batch_id';
25183 
25184 
25185            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25186 
25187   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25188 
25189   --  Udpate SR_RECEIPT_ID
25190 
25191   v_sql_stmt := 20;
25192 
25193   lv_sql_stmt:=
25194   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25195   ||' SET        sr_receipt_id = (SELECT  local_id'
25196   ||'               FROM  msc_local_id_misc mlim'
25197   ||'               WHERE    mlim.entity_name= ''SR_RECEIPT_ID'' '
25198   ||'               AND      mlim.char1 =  msis.sr_instance_code'
25199   ||'               AND      NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25200   ||'                        =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25201   ||'               AND      NVL(mlim.char3,'||''''||NULL_CHAR||''''||') '
25202   ||'                        =    NVL(msis.organization_code,'||''''||NULL_CHAR||''''||') '
25203   ||'               AND      mlim.char4 =  msis.sourcing_rule_name'
25204   ||'               AND      NVL(mlim.char5, '||''''||NULL_CHAR||''''||') '
25205   ||'                       = NVL(msis.receipt_organization_code,'||''''||NULL_CHAR||''''||') '
25206   ||'               AND     NVL(mlim.char6, '||''''||NULL_CHAR||''''||') '
25207   ||'                       = NVL(msis.receipt_org_instance_code,'||''''||NULL_CHAR||''''||') '
25208   ||'              AND      mlim.date1 =  msis.effective_date)'
25209   ||' WHERE        sr_instance_code = :v_instance_code'
25210   ||' AND          process_flag     = '||G_IN_PROCESS
25211   ||' AND          batch_id         = :lv_batch_id';
25212 
25213 
25214             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25215 
25216   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25217 
25218 
25219 
25220   --  Udpate SR_SOURCE_ID
25221   v_sql_stmt := 21;
25222 
25223   lv_sql_stmt:=
25224   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25225   ||' SET        sr_source_id = (SELECT  local_id'
25226   ||'             FROM  msc_local_id_misc mlim'
25227   ||'             WHERE    mlim.entity_name= ''SR_SOURCE_ID'' '
25228   ||'             AND      mlim.char1 =  msis.sr_instance_code'
25229   ||'             AND      NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25230   ||'                        =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25231   ||'             AND   NVL(mlim.char3,'||''''||NULL_CHAR||''''||') '
25232   ||'                        =    NVL(msis.organization_code,'||''''||NULL_CHAR||''''||') '
25233   ||'             AND      mlim.char4 =  msis.sourcing_rule_name'
25234   ||'             AND   NVL(mlim.char5,'||''''||NULL_CHAR||''''||') '
25235   ||'                    =  NVL(msis.source_organization_code,'||''''||NULL_CHAR||''''||') '
25236   ||'             AND   NVL(mlim.char6, '||''''||NULL_CHAR||''''||') '
25237   ||'                    = NVL(msis.source_org_instance_code,'||''''||NULL_CHAR||''''||') '
25238   ||'             AND   NVL(mlim.char7,'||''''||NULL_CHAR||''''||') '
25239   ||'                   =  NVL(msis.source_partner_site_code,'||''''||NULL_CHAR||''''||') '
25240   ||'             AND   NVL(mlim.char8, '||''''||NULL_CHAR||''''||') '
25241   ||'                    =  NVL(msis.source_partner_name,'||''''||NULL_CHAR||''''||') '
25242   ||'                     AND   NVL(mlim.char9, '||''''||NULL_CHAR||''''||') '
25243   ||'                    = NVL(msis.receipt_organization_code,'||''''||NULL_CHAR||''''||') '
25244   ||'              AND   NVL(mlim.char10,'||''''||NULL_CHAR||''''||') '
25245   ||'                    =  NVL(msis.receipt_org_instance_code,'||''''||NULL_CHAR||''''||') '
25246   ||'              AND      mlim.date1 =  msis.effective_date)'
25247   ||' WHERE        sr_instance_code = :v_instance_code'
25248   ||' AND          process_flag     ='||G_IN_PROCESS
25249   ||' AND          batch_id         = :lv_batch_id';
25250 
25251 
25252             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25253 
25254   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25255 
25256 
25257   -- Udpate      assignment_id
25258    v_sql_stmt := 22;
25259 
25260   lv_sql_stmt:=
25261   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25262   ||' SET        assignment_id = (SELECT  local_id '
25263   ||'                     FROM  msc_local_id_misc mlim'
25264   ||'                     WHERE    mlim.entity_name= ''ASSIGNMENT_ID'' '
25265   ||'                     AND      mlim.instance_id = '||v_instance_id
25266   ||'                     AND      mlim.char1 =  msis.sr_instance_code'
25267   ||'                     AND      NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25268   ||'                          =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25269   ||'                     AND      NVL(mlim.char3,'||''''||NULL_CHAR||''''||') '
25270   ||'                          =    NVL(msis.organization_code,'||''''||NULL_CHAR||''''||') '
25271   ||'                     AND      mlim.char4 =  msis.assignment_set_name'
25272   ||'                     AND      NVL(mlim.char5,'||''''||NULL_CHAR||''''||') '
25273   ||'                              = NVL(msis.category_name,'||''''||NULL_CHAR||''''||') '
25274   ||'                     AND      NVL(mlim.char6,'||''''||NULL_CHAR||''''||') '
25275   ||'                              = NVL(msis.item_name,'||''''||NULL_CHAR||''''||')) '
25276   ||' WHERE        sr_instance_code = :v_instance_code'
25277   ||' AND          process_flag     ='||G_IN_PROCESS
25278   ||' AND          batch_id         = :lv_batch_id';
25279 
25280 
25281             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25282 
25283   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25284 
25285   -- Set the message
25286 
25287     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
25288                  (p_app_short_name    => 'MSC',
25289                   p_error_code        => 'MSC_PP_DELETE_FAIL',
25290                   p_message_text      => lv_message_text,
25291                   p_error_text        => lv_error_text);
25292 
25293     IF lv_return <> 0 THEN
25294       RAISE ex_logging_err;
25295     END IF;
25296 
25297    -- Error out records if any id derivation fails and deleted_flag = 1
25298      v_sql_stmt := 23;
25299     lv_sql_stmt:=
25300     '   UPDATE        MSC_ST_ITEM_SOURCING  msis'
25301     ||' SET           process_flag   = '||G_ERROR_FLG||','
25302     ||'               error_text   = '||''''||lv_message_text||''''
25303     ||' WHERE        ( NVL(sourcing_rule_id,'||NULL_VALUE||') ='||NULL_VALUE
25304     ||' OR            NVL(sr_receipt_id,'||NULL_VALUE||') = '||NULL_VALUE
25305     ||' OR            NVL(sr_receipt_id,'||NULL_VALUE||') = '||NULL_VALUE
25306     ||' OR            NVL(sr_source_id,'||NULL_VALUE||') = '||NULL_VALUE
25307     ||' OR            NVL(assignment_id,'||NULL_VALUE||') = '||NULL_VALUE||')'
25308     ||' AND           sr_instance_code = :v_instance_code'
25309     ||' AND          deleted_flag    = '||SYS_YES
25310     ||' AND          process_flag     ='||G_IN_PROCESS
25311     ||' AND          batch_id         = :lv_batch_id';
25312 
25313 
25314    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25315 
25316    --Call to customised validation.
25317 
25318     MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
25319         (ERRBUF         => lv_error_text,
25320          RETCODE        => lv_return,
25321          pBatchID       => lv_batch_id,
25322          pInstanceCode  => v_instance_code,
25323          pEntityName    => 'MSC_ST_ITEM_SOURCING',
25324          pInstanceID    => v_instance_id);
25325 
25326       IF NVL(lv_return,0) <> 0 THEN
25327         RAISE ex_logging_err;
25328       END IF;
25329 
25330    -- Genearte sourcing rule id
25331      v_sql_stmt := 24;
25332 
25333      OPEN c3(lv_batch_id);
25334      FETCH c3 BULK COLLECT INTO lb_rowid ;
25335 
25336      IF c3%ROWCOUNT > 0  THEN
25337 
25338      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25339 
25340         UPDATE msc_st_item_sourcing
25341         SET    sourcing_rule_id       = msc_st_sourcing_rule_id_s.NEXTVAL
25342         WHERE  rowid                  = lb_rowid(j);
25343 
25344     v_sql_stmt := 25;
25345 
25346     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25347 
25348     -- Insert into the LID table
25349 
25350        INSERT INTO  msc_local_id_misc
25351        (local_id,
25352        st_transaction_id,
25353        instance_id,
25354        entity_name,
25355        data_source_type,
25356        char1,
25357        char2,
25358        char3,
25359        char4,
25360        SOURCE_ORG_ID,
25361        SOURCE_ASSIGNMENT_SET_ID,
25362        SOURCE_INVENTORY_ITEM_ID,
25363        SOURCE_CATEGORY_SET_ID,
25364        SOURCE_ASSIGNMENT_ID,
25365        SOURCE_CATEGORY_ID,
25366        SOURCE_SOURCING_RULE_ID,
25367        last_update_date,
25368        last_updated_by,
25369        creation_date,
25370        created_by  )
25371       SELECT
25372        sourcing_rule_id,
25373        st_transaction_id,
25374        v_instance_id,
25375        'SOURCING_RULE_ID',
25376        data_source_type,
25377        sr_instance_code,
25378        company_name,
25379        organization_code,
25380        sourcing_rule_name,
25381        SOURCE_ORG_ID,
25382        SOURCE_ASSIGNMENT_SET_ID,
25383        SOURCE_INVENTORY_ITEM_ID,
25384        SOURCE_CATEGORY_SET_ID,
25385        SOURCE_ASSIGNMENT_ID,
25386        SOURCE_CATEGORY_ID,
25387        SOURCE_SOURCING_RULE_ID,
25388        v_current_date,
25389        v_current_user,
25390        v_current_date,
25391        v_current_user
25392       FROM  msc_st_item_sourcing
25393       WHERE rowid = lb_rowid(j);
25394 
25395    END IF;
25396    CLOSE c3;
25397 
25398    --  Udpate SOURCING_RULE_ID
25399    v_sql_stmt := 26;
25400 
25401   lv_sql_stmt:=
25402   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25403   ||' SET        sourcing_rule_id = (SELECT  local_id '
25404   ||'            FROM  msc_local_id_misc mlim'
25405   ||'            WHERE    mlim.entity_name= ''SOURCING_RULE_ID'' '
25406   ||'            AND      mlim.char1 =  msis.sr_instance_code'
25407   ||'            AND   NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25408   ||'             =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25409   ||'            AND   NVL(mlim.char3,'||''''||NULL_CHAR||''''||') '
25410   ||'             =    NVL(msis.organization_code,'||''''||NULL_CHAR||''''||') '
25411   ||'            AND      mlim.char4 =  msis.sourcing_rule_name)'
25412   ||' WHERE      sr_instance_code = :v_instance_code'
25413   ||' AND        NVL(sourcing_rule_id,'||NULL_VALUE||') = '||NULL_VALUE
25414   ||' AND        process_flag     = '||G_IN_PROCESS
25415   ||' AND        batch_id         = :lv_batch_id';
25416 
25417 
25418             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25419 
25420   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25421 
25422 
25423    -- Genearte assignment_set_id
25424 
25425      v_sql_stmt := 27;
25426 
25427      OPEN c4(lv_batch_id);
25428      FETCH c4 BULK COLLECT INTO lb_rowid ;
25429 
25430      IF c4%ROWCOUNT > 0  THEN
25431 
25432      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25433 
25434         UPDATE msc_st_item_sourcing
25435         SET    assignment_set_id      = msc_st_assignment_set_id_s.NEXTVAL
25436         WHERE  rowid                  = lb_rowid(j);
25437 
25438     v_sql_stmt := 28;
25439 
25440     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25441 
25442     -- Insert into the LID table
25443 
25444        INSERT INTO  msc_local_id_misc
25445        (local_id,
25446        st_transaction_id,
25447        instance_id,
25448        entity_name,
25449        data_source_type,
25450        char1,
25451        char2,
25452        char3,
25453        SOURCE_ASSIGNMENT_SET_ID,
25454        last_update_date,
25455        last_updated_by,
25456        creation_date,
25457        created_by  )
25458       SELECT
25459        assignment_set_id,
25460        st_transaction_id,
25461        v_instance_id,
25462        'ASSIGNMENT_SET_ID',
25463        data_source_type,
25464        sr_instance_code,
25465        company_name,
25466        assignment_set_name,
25467        SOURCE_ASSIGNMENT_SET_ID,
25468        v_current_date,
25469        v_current_user,
25470        v_current_date,
25471        v_current_user
25472       FROM  msc_st_item_sourcing
25473       WHERE rowid = lb_rowid(j);
25474 
25475    END IF;
25476    CLOSE c4;
25477 
25478   --  Udpate ASSIGNMENT_SET_ID
25479    v_sql_stmt := 29;
25480   lv_sql_stmt:=
25481   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25482   ||' SET        assignment_set_id = (SELECT  local_id'
25483   ||'            FROM  msc_local_id_misc mlim'
25484   ||'            WHERE    mlim.entity_name= ''ASSIGNMENT_SET_ID'' '
25485   ||'            AND      mlim.char1 =  msis.sr_instance_code'
25486   ||'            AND   NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25487   ||'             =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25488   ||'            AND      mlim.char3 =  msis.assignment_set_name)'
25489   ||' WHERE      sr_instance_code = :v_instance_code'
25490   ||' AND        NVL(assignment_set_id,'||NULL_VALUE||') = '||NULL_VALUE
25491   ||' AND        process_flag     = '||G_IN_PROCESS
25492   ||' AND        batch_id         = :lv_batch_id';
25493 
25494 
25495             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25496 
25497   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25498 
25499    -- Genearte sr_receipt_id
25500      v_sql_stmt := 30;
25501 
25502      OPEN c5(lv_batch_id);
25503      FETCH c5 BULK COLLECT INTO lb_rowid ;
25504 
25505      IF c5%ROWCOUNT > 0  THEN
25506 
25507      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25508 
25509         UPDATE msc_st_item_sourcing
25510         SET    sr_receipt_id      = msc_st_sr_receipt_id_s.NEXTVAL
25511         WHERE  rowid               = lb_rowid(j);
25512 
25513     v_sql_stmt := 31;
25514 
25515     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25516 
25517     -- Insert into the LID table
25518 
25519        INSERT INTO  msc_local_id_misc
25520        (local_id,
25521        st_transaction_id,
25522        instance_id,
25523        entity_name,
25524        data_source_type,
25525        char1,
25526        char2,
25527        char3,
25528        char4,
25529        char5,
25530        char6,
25531        date1,
25532        SOURCE_ORG_ID,
25533        SOURCE_ASSIGNMENT_SET_ID,
25534        SOURCE_INVENTORY_ITEM_ID,
25535        SOURCE_CATEGORY_SET_ID,
25536        SOURCE_CATEGORY_ID,
25537        SOURCE_ASSIGNMENT_ID,
25538        SOURCE_SOURCING_RULE_ID,
25539        SOURCE_SR_RECEIPT_ID,
25540        SOURCE_RECEIPT_ORGANIZATION_ID,
25541        last_update_date,
25542        last_updated_by,
25543        creation_date,
25544        created_by  )
25545       SELECT
25546       sr_receipt_id,
25547       st_transaction_id,
25548       v_instance_id,
25549       'SR_RECEIPT_ID',
25550       data_source_type,
25551       v_instance_code,
25552       company_name,
25553       organization_code,
25554       sourcing_rule_name,
25555       receipt_organization_code,
25556       receipt_org_instance_code,
25557       effective_date,
25558       SOURCE_ORG_ID,
25559       SOURCE_ASSIGNMENT_SET_ID,
25560       SOURCE_INVENTORY_ITEM_ID,
25561       SOURCE_CATEGORY_SET_ID,
25562       SOURCE_CATEGORY_ID,
25563       SOURCE_ASSIGNMENT_ID,
25564       SOURCE_SOURCING_RULE_ID,
25565       SOURCE_SR_RECEIPT_ID,
25566       SOURCE_RECEIPT_ORGANIZATION_ID,
25567       v_current_date,
25568       v_current_user,
25569       v_current_date,
25570       v_current_user
25571      FROM  msc_st_item_sourcing
25572       WHERE rowid = lb_rowid(j);
25573 
25574    END IF;
25575    CLOSE c5;
25576 
25577   --  Udpate SR_RECEIPT_ID
25578 
25579   v_sql_stmt := 32;
25580 
25581   lv_sql_stmt:=
25582   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25583   ||' SET        sr_receipt_id = (SELECT  local_id'
25584   ||'               FROM  msc_local_id_misc mlim'
25585   ||'               WHERE    mlim.entity_name= ''SR_RECEIPT_ID'' '
25586   ||'               AND      mlim.char1 =  msis.sr_instance_code'
25587   ||'               AND      NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25588   ||'                        =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25589   ||'               AND   NVL(mlim.char3,'||''''||NULL_CHAR||''''||') '
25590   ||'                        =    NVL(msis.organization_code,'||''''||NULL_CHAR||''''||') '
25591   ||'               AND      mlim.char4 =  msis.sourcing_rule_name'
25592   ||'               AND      NVL(mlim.char5, '||''''||NULL_CHAR||''''||') '
25593   ||'                       = NVL(msis.receipt_organization_code,'||''''||NULL_CHAR||''''||') '
25594   ||'               AND     NVL(mlim.char6, '||''''||NULL_CHAR||''''||') '
25595   ||'                       = NVL(msis.receipt_org_instance_code,'||''''||NULL_CHAR||''''||') '
25596   ||'               AND      mlim.date1 =  msis.effective_date)'
25597   ||' WHERE        sr_instance_code = :v_instance_code'
25598   ||' AND          NVL(sr_receipt_id,'||NULL_VALUE||') = '||NULL_VALUE
25599   ||' AND          process_flag     = '||G_IN_PROCESS
25600   ||' AND          batch_id         = :lv_batch_id';
25601 
25602 
25603             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25604 
25605   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25606 
25607 
25608   -- Genearte sr_source_id
25609 
25610     v_sql_stmt := 33;
25611 
25612      OPEN c6(lv_batch_id);
25613      FETCH c6 BULK COLLECT INTO lb_rowid ;
25614 
25615      IF c6%ROWCOUNT > 0  THEN
25616 
25617      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25618 
25619         UPDATE msc_st_item_sourcing
25620         SET    sr_source_id        = msc_st_sr_source_id_s.NEXTVAL
25621         WHERE  rowid               = lb_rowid(j);
25622 
25623     v_sql_stmt := 34;
25624 
25625     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25626 
25627     -- Insert into the LID table
25628 
25629        INSERT INTO  msc_local_id_misc
25630        (local_id,
25631        st_transaction_id,
25632        instance_id,
25633        entity_name,
25634        data_source_type,
25635        char1,
25636        char2,
25637        char3,
25638        char4,
25639        char5,
25640        char6,
25641        char7,
25642        char8,
25643        char9,
25644        char10,
25645        date1,
25646        SOURCE_ORG_ID,
25647        SOURCE_ASSIGNMENT_SET_ID,
25648        SOURCE_INVENTORY_ITEM_ID,
25649        SOURCE_CATEGORY_SET_ID,
25650        SOURCE_CATEGORY_ID,
25651        SOURCE_ASSIGNMENT_ID,
25652        SOURCE_SOURCING_RULE_ID,
25653        SOURCE_SR_RECEIPT_ID,
25654        SOURCE_RECEIPT_ORGANIZATION_ID,
25655        SOURCE_SOURCE_PARTNER_ID,
25656        SOURCE_SOURCE_PARTNER_SITE_ID,
25657        SOURCE_SOURCE_ORGANIZATION_ID,
25658        SOURCE_SR_SOURCE_ID,
25659        last_update_date,
25660        last_updated_by,
25661        creation_date,
25662        created_by  )
25663       SELECT
25664        sr_source_id,
25665        st_transaction_id,
25666        v_instance_id,
25667        'SR_SOURCE_ID',
25668        data_source_type,
25669        sr_instance_code,
25670        company_name,
25671        organization_code,
25672        sourcing_rule_name,
25673        source_organization_code,
25674        source_org_instance_code,
25675        source_partner_site_code,
25676        source_partner_name,
25677        receipt_organization_code,
25678        receipt_org_instance_code,
25679        effective_date,
25680        SOURCE_ORG_ID,
25681        SOURCE_ASSIGNMENT_SET_ID,
25682        SOURCE_INVENTORY_ITEM_ID,
25683        SOURCE_CATEGORY_SET_ID,
25684        SOURCE_CATEGORY_ID,
25685        SOURCE_ASSIGNMENT_ID,
25686        SOURCE_SOURCING_RULE_ID,
25687        SOURCE_SR_RECEIPT_ID,
25688        SOURCE_RECEIPT_ORGANIZATION_ID,
25689        SOURCE_SOURCE_PARTNER_ID,
25690        SOURCE_SOURCE_PARTNER_SITE_ID,
25691        SOURCE_SOURCE_ORGANIZATION_ID,
25692        SOURCE_SR_SOURCE_ID,
25693        v_current_date,
25694        v_current_user,
25695        v_current_date,
25696        v_current_user
25697       FROM  msc_st_item_sourcing
25698       WHERE rowid = lb_rowid(j);
25699 
25700      END IF;
25701     CLOSE c6;
25702 
25703   --  Udpate SR_SOURCE_ID
25704   v_sql_stmt := 35;
25705 
25706   lv_sql_stmt:=
25707   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25708   ||' SET        sr_source_id = (SELECT  local_id'
25709   ||'             FROM  msc_local_id_misc mlim'
25710   ||'             WHERE    mlim.entity_name= ''SR_SOURCE_ID'' '
25711   ||'             AND      mlim.char1 =  msis.sr_instance_code'
25712   ||'             AND      NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25713   ||'                        =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25714   ||'             AND      NVL(mlim.char3,'||''''||NULL_CHAR||''''||') '
25715   ||'                        =    NVL(msis.organization_code,'||''''||NULL_CHAR||''''||') '
25716   ||'             AND      mlim.char4 =  msis.sourcing_rule_name'
25717   ||'             AND   NVL(mlim.char5,'||''''||NULL_CHAR||''''||') '
25718   ||'                    =  NVL(msis.source_organization_code,'||''''||NULL_CHAR||''''||') '
25719   ||'             AND   NVL(mlim.char6, '||''''||NULL_CHAR||''''||') '
25720   ||'                    = NVL(msis.source_org_instance_code,'||''''||NULL_CHAR||''''||') '
25721   ||'             AND   NVL(mlim.char7,'||''''||NULL_CHAR||''''||') '
25722   ||'                   =  NVL(msis.source_partner_site_code,'||''''||NULL_CHAR||''''||') '
25723   ||'             AND   NVL(mlim.char8, '||''''||NULL_CHAR||''''||') '
25724   ||'                    =  NVL(msis.source_partner_name,'||''''||NULL_CHAR||''''||') '
25725   ||'                     AND   NVL(mlim.char9, '||''''||NULL_CHAR||''''||') '
25726   ||'                    = NVL(msis.receipt_organization_code,'||''''||NULL_CHAR||''''||') '
25727   ||'              AND   NVL(mlim.char10,'||''''||NULL_CHAR||''''||') '
25728   ||'                    =  NVL(msis.receipt_org_instance_code,'||''''||NULL_CHAR||''''||') '
25729   ||'              AND      mlim.date1 =  msis.effective_date)'
25730   ||' WHERE        sr_instance_code = :v_instance_code'
25731   ||' AND          NVL(sr_source_id,'||NULL_VALUE||') = '||NULL_VALUE
25732   ||' AND          process_flag     ='||G_IN_PROCESS
25733   ||' AND          batch_id         = :lv_batch_id';
25734 
25735 
25736             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25737 
25738   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25739 
25740 
25741 
25742    -- Genearte assignment id
25743 
25744     v_sql_stmt := 36;
25745 
25746      OPEN c7(lv_batch_id);
25747      FETCH c7 BULK COLLECT INTO lb_rowid ;
25748 
25749      IF c7%ROWCOUNT > 0  THEN
25750 
25751      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25752 
25753         UPDATE msc_st_item_sourcing
25754         SET    assignment_id        = msc_st_assignment_id_s.NEXTVAL
25755         WHERE  rowid               = lb_rowid(j);
25756 
25757     v_sql_stmt := 37;
25758 
25759     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
25760     -- Insert into the LID table
25761 
25762        INSERT INTO  msc_local_id_misc
25763        (local_id,
25764        st_transaction_id,
25765        instance_id,
25766        entity_name,
25767        data_source_type,
25768        char1,
25769        char2,
25770        char3,
25771        char4,
25772        char5,
25773        char6,
25774        SOURCE_ORG_ID,
25775        SOURCE_ASSIGNMENT_SET_ID,
25776        SOURCE_INVENTORY_ITEM_ID,
25777        SOURCE_CATEGORY_SET_ID,
25778        SOURCE_CATEGORY_ID,
25779        SOURCE_ASSIGNMENT_ID,
25780        SOURCE_SOURCING_RULE_ID,
25781        last_update_date,
25782        last_updated_by,
25783        creation_date,
25784        created_by  )
25785       SELECT
25786        assignment_id,
25787        st_transaction_id,
25788        v_instance_id,
25789        'ASSIGNMENT_ID',
25790        data_source_type,
25791        sr_instance_code,
25792        company_name,
25793        organization_code,
25794        assignment_set_name,
25795        category_name,
25796        item_name,
25797        SOURCE_ORG_ID,
25798        SOURCE_ASSIGNMENT_SET_ID,
25799        SOURCE_INVENTORY_ITEM_ID,
25800        SOURCE_CATEGORY_SET_ID,
25801        SOURCE_CATEGORY_ID,
25802        SOURCE_ASSIGNMENT_ID,
25803        SOURCE_SOURCING_RULE_ID,
25804        v_current_date,
25805        v_current_user,
25806        v_current_date,
25807        v_current_user
25808       FROM  msc_st_item_sourcing
25809       WHERE rowid = lb_rowid(j);
25810 
25811      END IF;
25812     CLOSE c7;
25813 
25814   -- Udpate      assignment_id
25815    v_sql_stmt := 38;
25816 
25817   lv_sql_stmt:=
25818   'UPDATE        MSC_ST_ITEM_SOURCING  msis'
25819   ||' SET        assignment_id = (SELECT  local_id '
25820   ||'                     FROM  msc_local_id_misc mlim'
25821   ||'                     WHERE    mlim.entity_name= ''ASSIGNMENT_ID'' '
25822   ||'                     AND      mlim.instance_id = '||v_instance_id
25823   ||'                     AND      mlim.char1 =  msis.sr_instance_code'
25824   ||'                     AND      NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
25825   ||'                          =    NVL(msis.company_name,'||''''||NULL_CHAR||''''||') '
25826   ||'                     AND      NVL(mlim.char3,'||''''||NULL_CHAR||''''||') '
25827   ||'                          =    NVL(msis.organization_code,'||''''||NULL_CHAR||''''||') '
25828   ||'                     AND      mlim.char4 =  msis.assignment_set_name'
25829   ||'                     AND      NVL(mlim.char5,'||''''||NULL_CHAR||''''||') '
25830   ||'                              = NVL(msis.category_name,'||''''||NULL_CHAR||''''||') '
25831   ||'                     AND      NVL(mlim.char6,'||''''||NULL_CHAR||''''||') '
25832   ||'                              = NVL(msis.item_name,'||''''||NULL_CHAR||''''||')) '
25833   ||' WHERE       sr_instance_code = :v_instance_code'
25834   ||' AND         NVL(assignment_id,'||NULL_VALUE||') = '||NULL_VALUE
25835   ||' AND         process_flag     ='||G_IN_PROCESS
25836   ||' AND         batch_id         = :lv_batch_id';
25837 
25838 
25839             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25840 
25841   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25842 
25843 
25844    v_sql_stmt := 39;
25845    lv_sql_stmt :=
25846    'INSERT INTO MSC_ST_SOURCING_RULES'
25847    ||' ( SR_SOURCING_RULE_ID,'
25848    ||' SOURCING_RULE_NAME,'
25849    ||' ORGANIZATION_ID,'
25850    ||' DESCRIPTION,'
25851    ||' STATUS,'
25852    ||' SOURCING_RULE_TYPE,'
25853    ||' PLANNING_ACTIVE,'
25854    ||' DELETED_FLAG,'
25855    ||' SR_INSTANCE_ID,'
25856    ||' REFRESH_ID,'
25857    ||' LAST_UPDATED_BY,'
25858    ||' LAST_UPDATE_DATE,'
25859    ||' CREATED_BY,'
25860    ||' CREATION_DATE)'
25861    ||' SELECT DISTINCT '
25862    ||' SOURCING_RULE_ID,'
25863    ||' SOURCING_RULE_NAME,'
25864    ||' ORGANIZATION_ID,'
25865    ||' DESCRIPTION,'
25866    ||' 1, '/* Hard coding as 1*/
25867    ||' 1, '/*Always as sourcing rule not Bill of Distribution*/
25868    ||' PLANNING_ACTIVE,'
25869    ||' DELETED_FLAG,'
25870    ||  v_instance_id||','
25871    ||' REFRESH_ID,'
25872    ||' LAST_UPDATED_BY,'
25873    ||' LAST_UPDATE_DATE,'
25874    ||' CREATED_BY,'
25875    ||' CREATION_DATE '
25876    ||' FROM MSC_ST_ITEM_SOURCING'
25877    ||' WHERE SR_INSTANCE_CODE = :v_instance_code'
25878    ||' AND   process_flag      = '||G_IN_PROCESS
25879    ||' AND   batch_id          = :lv_batch_id' ;
25880 
25881 
25882             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25883 
25884     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25885 
25886    v_sql_stmt := 40;
25887    lv_sql_stmt :=
25888    'INSERT INTO MSC_ST_SR_RECEIPT_ORG'
25889    ||' ( SR_RECEIPT_ID,'
25890    ||' SR_SR_RECEIPT_ORG,'
25891    ||' RECEIPT_ORG_INSTANCE_ID,'
25892    ||' SOURCING_RULE_ID,'
25893    ||' EFFECTIVE_DATE,'
25894    ||' DISABLE_DATE,'
25895    ||' DELETED_FLAG,'
25896    ||' SR_INSTANCE_ID,'
25897    ||' REFRESH_ID,'
25898    ||' LAST_UPDATED_BY,'
25899    ||' LAST_UPDATE_DATE,'
25900    ||' CREATED_BY,'
25901    ||' CREATION_DATE)'
25902    ||' SELECT DISTINCT'
25903    ||' SR_RECEIPT_ID,'
25904    ||' RECEIPT_ORGANIZATION_ID,'
25905    ||  v_instance_id||','
25906    ||' SOURCING_RULE_ID,'
25907    ||' EFFECTIVE_DATE,'
25908    ||' DISABLE_DATE,'
25909    ||' DELETED_FLAG,'
25910    ||  v_instance_id||','
25911    ||' REFRESH_ID,'
25912    ||' LAST_UPDATED_BY,'
25913    ||' LAST_UPDATE_DATE,'
25914    ||' CREATED_BY,'
25915    ||' CREATION_DATE '
25916    ||' FROM MSC_ST_ITEM_SOURCING'
25917    ||' WHERE SR_INSTANCE_CODE = :v_instance_code'
25918    ||' AND   PROCESS_FLAG =    '||G_IN_PROCESS
25919    ||' AND   batch_id          = :lv_batch_id' ;
25920 
25921 
25922             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25923 
25924     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25925 
25926    v_sql_stmt := 41;
25927 
25928   lv_sql_stmt :=
25929   'INSERT INTO MSC_ST_SR_SOURCE_ORG'
25930   ||' ( SR_SR_SOURCE_ID,'
25931   ||'   SR_RECEIPT_ID,'
25932   ||'  SOURCE_ORGANIZATION_ID,'
25933   ||'  SOURCE_ORG_INSTANCE_ID,'
25934   ||'  ALLOCATION_PERCENT,'
25935   ||'  SOURCE_TYPE,'
25936   ||'  RANK,'
25937   ||'  SOURCE_PARTNER_ID,'
25938   ||'  SOURCE_PARTNER_SITE_ID,'
25939   ||'  SHIP_METHOD,'
25940   ||'  DELETED_FLAG,'
25941   ||'  SR_INSTANCE_ID,'
25942   ||' REFRESH_ID,'
25943   ||' LAST_UPDATED_BY,'
25944   ||' LAST_UPDATE_DATE,'
25945   ||' CREATED_BY,'
25946   ||' CREATION_DATE)'
25947   ||' select distinct'
25948   ||' SR_SOURCE_ID,'
25949   ||' SR_RECEIPT_ID,'
25950   ||' SOURCE_ORGANIZATION_ID,'
25951   ||  v_instance_id||','
25952   ||' ALLOCATION_PERCENT,'
25953   ||' SOURCE_TYPE,'
25954   ||' RANK,'
25955   ||' SOURCE_PARTNER_ID,'
25956   ||' SOURCE_PARTNER_SITE_ID,'
25957   ||' SHIP_METHOD,'
25958   ||' DELETED_FLAG,'
25959   ||  v_instance_id||','
25960   ||' REFRESH_ID,'
25961   ||' LAST_UPDATED_BY,'
25962   ||' LAST_UPDATE_DATE,'
25963   ||' CREATED_BY,'
25964   ||' CREATION_DATE '
25965   ||' FROM MSC_ST_ITEM_SOURCING'
25966   ||' where SR_INSTANCE_CODE = :v_instance_code'
25967   ||' AND PROCESS_FLAG       = '||G_IN_PROCESS
25968   ||' AND   batch_id         = :lv_batch_id' ;
25969 
25970 
25971             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
25972 
25973     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
25974 
25975 
25976   v_sql_stmt := 42;
25977   lv_sql_stmt :=
25978   'insert into MSC_ST_ASSIGNMENT_SETS'
25979    ||'(SR_ASSIGNMENT_SET_ID,'
25980    ||' ASSIGNMENT_SET_NAME,'
25981    ||' DESCRIPTION,'
25982    ||' DELETED_FLAG,'
25983    ||' SR_INSTANCE_ID,'
25984    ||' REFRESH_ID,'
25985    ||' LAST_UPDATED_BY,'
25986    ||' LAST_UPDATE_DATE,'
25987    ||' CREATED_BY,'
25988    ||' CREATION_DATE)'
25989    ||' SELECT DISTINCT'
25990    ||'  ASSIGNMENT_SET_ID,'
25991    ||'  SR_INSTANCE_CODE||'':''||ASSIGNMENT_SET_NAME,'
25992    ||'  DESCRIPTION,'
25993    ||'  DELETED_FLAG,'
25994    ||   v_instance_id||','
25995    ||' REFRESH_ID,'
25996    ||' LAST_UPDATED_BY,'
25997    ||' LAST_UPDATE_DATE,'
25998    ||' CREATED_BY,'
25999    ||' CREATION_DATE '
26000    ||'  FROM MSC_ST_ITEM_SOURCING'
26001    ||'  where SR_INSTANCE_CODE = :v_instance_code'
26002    ||' AND PROCESS_FLAG = '||G_IN_PROCESS
26003    ||' AND   batch_id   = :lv_batch_id' ;
26004 
26005 
26006             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26007 
26008     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
26009 
26010   v_sql_stmt := 43;
26011   lv_sql_stmt :=
26012   'insert into MSC_ST_SR_ASSIGNMENTS'
26013   ||'  ( SR_ASSIGNMENT_ID,'
26014   ||'  ASSIGNMENT_TYPE,'
26015   ||'  SOURCING_RULE_ID,'
26016   ||'  SOURCING_RULE_TYPE,'
26017   ||'  ASSIGNMENT_SET_ID,'
26018   ||'  INVENTORY_ITEM_ID,'
26019   ||'  CATEGORY_NAME,'
26020   ||'  CATEGORY_SET_IDENTIFIER,'
26021   ||'  SR_ASSIGNMENT_INSTANCE_ID,'
26022   ||'  ORGANIZATION_ID,'
26023   ||'  SR_INSTANCE_ID,'
26024   ||'  DELETED_FLAG, '
26025   ||'  REFRESH_ID,'
26026   ||'  LAST_UPDATED_BY,'
26027   ||'  LAST_UPDATE_DATE,'
26028   ||'  CREATED_BY,'
26029   ||'  CREATION_DATE )'
26030   ||'select distinct '
26031   ||'  ASSIGNMENT_ID,'
26032   ||'  ASSIGNMENT_TYPE,'
26033   ||'  SOURCING_RULE_ID,'
26034   ||'  1,'
26035   ||'  ASSIGNMENT_SET_ID,'
26036   ||'  INVENTORY_ITEM_ID,'
26037   ||'  CATEGORY_NAME,'
26038   ||'  CATEGORY_SET_ID,'
26039   ||   v_instance_id||','
26040   ||'  ORGANIZATION_ID,'
26041   ||   v_instance_id||','
26042   ||'  DELETED_FLAG,'
26043   ||' REFRESH_ID,'
26044   ||' LAST_UPDATED_BY,'
26045   ||' LAST_UPDATE_DATE,'
26046   ||' CREATED_BY,'
26047   ||' CREATION_DATE '
26048   ||'  from MSC_ST_ITEM_SOURCING'
26049   ||'  where SR_INSTANCE_CODE = :v_instance_code'
26050   ||' AND PROCESS_FLAG = '||G_IN_PROCESS
26051   ||' AND   batch_id   = :lv_batch_id' ;
26052 
26053 
26054             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26055 
26056 
26057   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
26058 
26059    -- Set the processf flag as Valid and poulate instance_id
26060     lv_return :=
26061     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name     => 'MSC_ST_ITEM_SOURCING',
26062                                p_instance_id    => v_instance_id,
26063                                p_instance_code  => v_instance_code,
26064                                p_process_flag   => G_VALID,
26065                                p_error_text     => lv_error_text,
26066                                p_debug          => v_debug,
26067                                p_batch_id       => lv_batch_id);
26068 
26069      IF(lv_return <> 0) then
26070         RAISE ex_logging_err;
26071      END IF;
26072 
26073     -- At the end calling  the LOG_ERROR for logging all errored out records
26074 
26075    lv_return :=
26076    msc_st_util.log_error(p_table_name    => 'MSC_ST_ITEM_SOURCING',
26077                          p_instance_code => v_instance_code,
26078                          p_row           => lv_column_names,
26079                          p_severity      => G_SEV_ERROR,
26080                          p_error_text    => lv_error_text,
26081                          p_message_text  => NULL,
26082                          p_debug         => v_debug,
26083                          p_batch_id      => lv_batch_id  );
26084 
26085      IF(lv_return <> 0) then
26086         RAISE ex_logging_err;
26087      END IF;
26088 
26089     COMMIT;
26090    END LOOP;
26091 
26092 
26093  EXCEPTION
26094     WHEN too_many_rows THEN
26095          lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_SOURCING_RULES'||'('
26096                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
26097         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
26098          ROLLBACK ;
26099 
26100     WHEN ex_logging_err THEN
26101         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
26102         ROLLBACK;
26103 
26104    WHEN OTHERS THEN
26105        lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_SOURCING_RULE'||'('
26106                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
26107         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
26108       ROLLBACK;
26109 
26110    END LOAD_SOURCING_RULE;
26111 
26112 
26113   /*==========================================================================+
26114   | DESCRIPTION  : This function performs the validation and loads the data   |
26115   |                for business object Project and Task.                      |
26116   +==========================================================================*/
26117   PROCEDURE  LOAD_PROJ_TASK  IS
26118 
26119   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
26120   TYPE CurTyp       IS REF CURSOR;
26121   c1                CurTyp;
26122   lb_rowid          RowidTab;
26123 
26124   lv_local_id       NUMBER;
26125   lv_column_names   VARCHAR2(5000);     -- Stores cocatenated column names
26126   lv_return         NUMBER;
26127   lv_error_text     VARCHAR2(250);
26128   lv_where_str      VARCHAR2(5000);
26129   lv_sql_stmt       VARCHAR2(5000);
26130   lv_cursor_stmt    VARCHAR2(5000);
26131   lv_batch_id       msc_st_project_tasks.batch_id%TYPE;
26132   lv_message_text   msc_errors.error_text%TYPE;
26133 
26134   ex_logging_err    EXCEPTION;
26135 
26136   CURSOR c2(p_batch_id NUMBER) IS
26137     SELECT rowid
26138     FROM   msc_st_project_tasks
26139     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
26140     AND    sr_instance_code = v_instance_code
26141     AND    batch_id         = p_batch_id;
26142 
26143     CURSOR c3(p_batch_id NUMBER) IS
26144     SELECT max(rowid)
26145     FROM   msc_st_project_tasks
26146     WHERE  process_flag     = G_IN_PROCESS
26147     AND    sr_instance_code = v_instance_code
26148     AND    batch_id         = p_batch_id
26149     AND    NVL(project_id,NULL_VALUE) = NULL_VALUE
26150     AND    deleted_flag             = SYS_NO
26151     GROUP BY sr_instance_code,company_name,
26152              project_number  ;
26153 
26154     CURSOR c4(p_batch_id NUMBER) IS
26155     SELECT MAX(rowid)
26156     FROM   msc_st_project_tasks
26157     WHERE  process_flag     = G_IN_PROCESS
26158     AND    sr_instance_code = v_instance_code
26159     AND    batch_id         = p_batch_id
26160     AND    NVL(task_id,NULL_VALUE) = NULL_VALUE
26161     AND    NVL(task_number,NULL_CHAR) <> NULL_CHAR
26162     AND    deleted_flag     = SYS_NO
26163     GROUP BY sr_instance_code,company_name,
26164              project_number,task_number ;
26165 
26166 
26167     CURSOR c5(p_batch_id NUMBER) IS
26168     SELECT max(rowid)
26169     FROM   msc_st_project_tasks
26170     WHERE  process_flag     = G_IN_PROCESS
26171     AND    sr_instance_code = v_instance_code
26172     AND    batch_id         = p_batch_id
26173     AND    NVL(costing_group_id,NULL_VALUE) = NULL_VALUE
26174     AND    NVL(costing_group_code,NULL_CHAR)  <> NULL_CHAR
26175     AND    deleted_flag     = SYS_NO
26176     GROUP BY sr_instance_code,company_name,
26177              costing_group_code  ;
26178 
26179   BEGIN
26180 
26181 
26182     --Duplicate records check for the records whose source is XML
26183 
26184     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
26185                  (p_app_short_name    => 'MSC',
26186                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
26187                   p_message_text      => lv_message_text,
26188                   p_error_text        => lv_error_text);
26189 
26190     IF lv_return <> 0 THEN
26191       RAISE ex_logging_err;
26192     END IF;
26193 
26194   v_sql_stmt := 01;
26195   lv_sql_stmt:=
26196   'UPDATE       msc_st_project_tasks mspt1 '
26197   ||' SET       process_flag ='||G_ERROR_FLG||','
26198   ||'           error_text   = '||''''||lv_message_text||''''
26199   ||' WHERE     message_id < ( SELECT max(message_id) FROM msc_st_project_tasks mspt2'
26200   ||'               WHERE mspt2.sr_instance_code   = mspt1.sr_instance_code'
26201   ||'               AND   NVL(mspt2.company_name,'||''''||NULL_CHAR||''''||') '
26202   ||'                 =    NVL(mspt1.company_name,'||''''||NULL_CHAR||''''||') '
26203   ||'               AND   mspt2.organization_code  = mspt1.organization_code'
26204   ||'               AND   mspt2.project_number     = mspt1.project_number'
26205   ||'               AND  NVL(mspt2.task_number, '||''''||NULL_CHAR||''''||') '
26206   ||'                    = NVL(mspt1.task_number, '||''''||NULL_CHAR||''''||') '
26207   ||'               AND   mspt2.process_flag        ='||G_IN_PROCESS
26208   ||'               AND    NVL(mspt2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
26209   ||'  AND     mspt1.process_flag ='|| G_IN_PROCESS
26210   ||'  AND     mspt1.sr_instance_code = :v_instance_code'
26211   ||'  AND     NVL(mspt1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
26212 
26213 
26214          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26215 
26216   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
26217 
26218    --Duplicate records check for the records whose source is batch load
26219 
26220     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
26221                  (p_app_short_name    => 'MSC',
26222                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
26223                   p_message_text      => lv_message_text,
26224                   p_error_text        => lv_error_text);
26225 
26226     IF lv_return <> 0 THEN
26227       RAISE ex_logging_err;
26228     END IF;
26229 
26230   v_sql_stmt := 02;
26231 
26232   lv_sql_stmt :=
26233   'UPDATE      msc_st_project_tasks mspt1'
26234   ||' SET      process_flag ='|| G_ERROR_FLG||','
26235   ||'          error_text   = '||''''||lv_message_text||''''
26236   ||' WHERE    EXISTS ( SELECT 1 FROM msc_st_project_tasks mspt2'
26237   ||'               WHERE mspt2.sr_instance_code   = mspt1.sr_instance_code'
26238   ||'               AND   NVL(mspt2.company_name,'||''''||NULL_CHAR||''''||') '
26239   ||'                 =    NVL(mspt1.company_name,'||''''||NULL_CHAR||''''||') '
26240   ||'               AND   mspt2.organization_code  = mspt1.organization_code'
26241   ||'               AND   mspt2.project_number      = mspt1.project_number'
26242   ||'               AND   NVL(mspt2.task_number,'||''''||NULL_CHAR||''''||') '
26243   ||'                = NVL(mspt1.task_number, '||''''||NULL_CHAR||''''||') '
26244   ||'               AND   mspt2.process_flag        ='||G_IN_PROCESS
26245   ||'               AND NVL(mspt2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
26246   ||'               GROUP BY sr_instance_code,organization_code,company_name,'
26247   ||'               project_number,task_number '
26248   ||'               HAVING COUNT(*) >1 )'
26249   ||' AND     mspt1.process_flag = '||G_IN_PROCESS
26250   ||' AND     mspt1.sr_instance_code = :v_instance_code'
26251   ||' AND     NVL(mspt1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
26252 
26253 
26254          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26255 
26256   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
26257 
26258   lv_column_names :=
26259   'PROJECT_NUMBER               ||''~''||'
26260   ||'SEIBAN_NUMBER_FLAG         ||''~''||'
26261   ||'ORGANIZATION_CODE          ||''~''||'
26262   ||'TASK_NUMBER                ||''~''||'
26263   ||'SR_INSTANCE_CODE           ||''~''||'
26264   ||'PROJECT_DESCRIPTION        ||''~''||'
26265   ||'PLANNING_GROUP             ||''~''||'
26266   ||'WIP_ACCT_CLASS_CODE        ||''~''||'
26267   ||'COSTING_GROUP_CODE         ||''~''||'
26268   ||'MATERIAL_ACCOUNT           ||''~''||'
26269   ||'PROJECT_START_DATE         ||''~''||'
26270   ||'PROJECT_COMPLETION_DATE    ||''~''||'
26271   ||'DESCRIPTION                ||''~''||'
26272   ||'MANAGER                    ||''~''||'
26273   ||'START_DATE                 ||''~''||'
26274   ||'END_DATE                   ||''~''||'
26275   ||'MANAGER_CONTACT            ||''~''||'
26276   ||' COMPANY_NAME              ||''~''||'
26277   ||' DELETED_FLAG ' ;
26278 
26279 
26280    LOOP
26281       v_sql_stmt := 03;
26282       lv_cursor_stmt :=
26283       ' SELECT msc_st_batch_id_s.NEXTVAL '
26284       ||' FROM   dual';
26285 
26286       OPEN  c1 FOR lv_cursor_stmt;
26287       FETCH c1 INTO lv_batch_id;
26288       CLOSE c1;
26289 
26290       v_sql_stmt := 04;
26291       lv_sql_stmt :=
26292       ' UPDATE msc_st_project_tasks '
26293       ||' SET    batch_id  = :lv_batch_id'
26294       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
26295       ||' AND    sr_instance_code               = :v_instance_code'
26296       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
26297       ||' AND    rownum                        <= '||v_batch_size;
26298 
26299 
26300          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26301 
26302       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
26303 
26304       EXIT WHEN SQL%NOTFOUND;
26305 
26306       OPEN c2(lv_batch_id);
26307       FETCH c2 BULK COLLECT INTO lb_rowid;
26308       CLOSE c2;
26309 
26310       v_sql_stmt := 05;
26311       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
26312       UPDATE msc_st_project_tasks
26313       SET  st_transaction_id = msc_st_project_tasks_s.NEXTVAL,
26314            refresh_id        = v_refresh_id,
26315            last_update_date  = v_current_date,
26316            last_updated_by   = v_current_user,
26317            creation_date     = v_current_date,
26318            created_by        = v_current_user
26319       WHERE  rowid           = lb_rowid(j);
26320 
26321 
26322       -- Set the  message
26323       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
26324                      (p_app_short_name    => 'MSC',
26325                       p_error_code        => 'MSC_PP_NO_DELETION',
26326                       p_message_text      => lv_message_text,
26327                       p_error_text        => lv_error_text,
26328                       p_token1            => 'TABLE_NAME',
26329                       p_token_value1      => 'MSC_ST_PROJECT_TASKS');
26330 
26331       IF lv_return <> 0 THEN
26332         RAISE ex_logging_err;
26333       END IF;
26334 
26335        --Deletion is not allowed on this table.
26336       v_sql_stmt := 06;
26337       lv_sql_stmt :=
26338       ' UPDATE     msc_st_project_tasks '
26339       ||' SET      process_flag     = '||G_ERROR_FLG||','
26340       ||'          error_text       = '||''''||lv_message_text||''''
26341       ||' WHERE    deleted_flag     = '||SYS_YES
26342       ||' AND      process_flag     = '||G_IN_PROCESS
26343       ||' AND      batch_id         =  :lv_batch_id'
26344       ||' AND      sr_instance_code = :v_instance_code';
26345 
26346 
26347             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26348 
26349       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
26350 
26351        -- Set the error message
26352       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
26353                      (p_app_short_name    => 'MSC',
26354                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
26355                       p_message_text      => lv_message_text,
26356                       p_error_text        => lv_error_text,
26357                       p_token1            => 'COLUMN_NAME',
26358                       p_token_value1      => 'PROJECT_NUMBER');
26359 
26360       IF lv_return <> 0 THEN
26361         RAISE ex_logging_err;
26362       END IF;
26363 
26364      -- Error out record if project_number is NULL
26365 
26366       v_sql_stmt := 07;
26367      lv_sql_stmt :=
26368       ' UPDATE  msc_st_project_tasks '
26369       ||' SET   process_flag        = '||G_ERROR_FLG||','
26370       ||'         error_text   = '||''''||lv_message_text||''''
26371       ||' WHERE NVL(project_number,'||''''||NULL_CHAR||''''||')'
26372       ||'       = '||''''||NULL_CHAR||''''
26373       ||' AND   batch_id         = :lv_batch_id'
26374       ||' AND   process_flag     = '||G_IN_PROCESS
26375       ||' AND   sr_instance_code = :v_instance_code';
26376 
26377 
26378         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26379       EXECUTE IMMEDIATE lv_sql_stmt
26380               USING     lv_batch_id,
26381                         v_instance_code;
26382 
26383 
26384      -- Set the  message
26385      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
26386                      (p_app_short_name    => 'MSC',
26387                       p_error_code        => 'MSC_PP_INVALID_VALUE',
26388                       p_message_text      => lv_message_text,
26389                       p_error_text        => lv_error_text,
26390                       p_token1            => 'COLUMN_NAME',
26391                       p_token_value1      => 'ORGANIZATION_CODE');
26392 
26393       IF lv_return <> 0 THEN
26394         RAISE ex_logging_err;
26395       END IF;
26396 
26397       -- Derive  organization id
26398      lv_return :=
26399      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     =>'MSC_ST_PROJECT_TASKS',
26400                                     p_org_partner_name  =>'ORGANIZATION_CODE',
26401                                     p_org_partner_id    =>'ORGANIZATION_ID',
26402                                     p_instance_code     => v_instance_code,
26403                                     p_partner_type      => G_ORGANIZATION,
26404                                     p_severity          => G_SEV_ERROR,
26405                                     p_message_text      => lv_message_text,
26406                                     p_batch_id          => lv_batch_id,
26407                                     p_debug             => v_debug,
26408                                     p_error_text        => lv_error_text);
26409 
26410      IF(lv_return <> 0) then
26411         RAISE ex_logging_err;
26412      END IF;
26413 
26414     -- Set the error message
26415     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
26416                      (p_app_short_name    => 'MSC',
26417                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
26418                       p_message_text      => lv_message_text,
26419                       p_error_text        => lv_error_text,
26420                       p_token1            => 'COLUMN_NAME',
26421                       p_token_value1      => 'DELETED_FLAG',
26422                       p_token2            => 'DEFAULT_VALUE',
26423                       p_token_value2      =>  SYS_NO  );
26424 
26425       IF lv_return <> 0 THEN
26426         RAISE ex_logging_err;
26427       END IF;
26428 
26429       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
26430       -- Log error procedure inserts warning and also defaults the delete flag
26431 
26432      lv_where_str :=
26433      ' AND NVL(deleted_flag,'||NULL_VALUE||') <>'||SYS_NO;
26434 
26435     lv_return := MSC_ST_UTIL.LOG_ERROR
26436                     (p_table_name        => 'MSC_ST_PROJECT_TASKS',
26437                      p_instance_code     => v_instance_code,
26438                      p_row               => lv_column_names,
26439                      p_severity          => G_SEV_WARNING,
26440                      p_message_text      => lv_message_text,
26441                      p_error_text        => lv_error_text,
26442                      p_batch_id          => lv_batch_id,
26443                      p_where_str         => lv_where_str,
26444                      p_col_name          => 'DELETED_FLAG',
26445                      p_debug             => v_debug,
26446                      p_default_value     => SYS_NO);
26447 
26448 
26449       IF lv_return <> 0 THEN
26450           RAISE ex_logging_err;
26451       END IF;
26452 
26453 
26454     -- Set the error message
26455     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
26456                      (p_app_short_name    => 'MSC',
26457                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
26458                       p_message_text      => lv_message_text,
26459                       p_error_text        => lv_error_text,
26460                       p_token1            => 'COLUMN_NAME',
26461                       p_token_value1      => 'SEIBAN_NUMBER_FLAG',
26462                       p_token2            => 'DEFAULT_VALUE',
26463                       p_token_value2      =>  G_SEIBAN_NUMBER_FLAG );
26464 
26465       IF lv_return <> 0 THEN
26466         RAISE ex_logging_err;
26467       END IF;
26468 
26469      -- Default SEIBAN NUMBER FLAG as 2 if not in (1,2)
26470 
26471     lv_where_str := ' AND NVL(seiban_number_flag,'||NULL_VALUE||') NOT IN (1,2) ';
26472 
26473     lv_return := MSC_ST_UTIL.LOG_ERROR
26474                     (p_table_name        => 'MSC_ST_PROJECT_TASKS',
26475                      p_instance_code     => v_instance_code,
26476                      p_row               => lv_column_names,
26477                      p_severity          => G_SEV_WARNING,
26478                      p_message_text      => lv_message_text,
26479                      p_error_text        => lv_error_text,
26480                      p_batch_id          => lv_batch_id,
26481                      p_where_str         => lv_where_str,
26482                      p_col_name          => 'SEIBAN_NUMBER_FLAG',
26483                      p_debug             => v_debug,
26484                      p_default_value     => G_SEIBAN_NUMBER_FLAG);
26485 
26486      IF(lv_return <> 0) then
26487         RAISE ex_logging_err;
26488      END IF;
26489 
26490     -- Derive Project id
26491      lv_return :=
26492      MSC_ST_UTIL.DERIVE_PROJ_TASK_ID(p_table_name => 'MSC_ST_PROJECT_TASKS',
26493                              p_proj_col_name      => 'PROJECT_NUMBER',
26494                              p_proj_task_col_id   => 'PROJECT_ID',
26495                              p_instance_code      => v_instance_code,
26496                              p_entity_name        => 'PROJECT_ID',
26497                              p_batch_id           => lv_batch_id,
26498                              p_error_text         => lv_error_text,
26499                              p_debug              => v_debug,
26500                              p_row                => lv_column_names);
26501 
26502      IF(lv_return <> 0) then
26503         RAISE ex_logging_err;
26504      END IF;
26505 
26506      -- Derive task id
26507      lv_return :=
26508      MSC_ST_UTIL.DERIVE_PROJ_TASK_ID(p_table_name =>'MSC_ST_PROJECT_TASKS',
26509                              p_proj_col_name      =>'PROJECT_NUMBER',
26510                              p_proj_task_col_id   =>'TASK_ID',
26511                              p_task_col_name      =>'TASK_NUMBER',
26512                              p_instance_code      => v_instance_code,
26513                              p_entity_name        => 'TASK_ID',
26514                              p_batch_id           =>lv_batch_id,
26515                              p_error_text         =>lv_error_text,
26516                              p_debug              => v_debug,
26517                              p_row                =>lv_column_names);
26518 
26519      IF(lv_return <> 0) then
26520         RAISE ex_logging_err;
26521      END IF;
26522 
26523     -- UPdate task_name with the task_number
26524 
26525      v_sql_stmt := 07;
26526     lv_sql_stmt :=
26527     'UPDATE msc_st_project_tasks '
26528     ||' SET    task_name = task_number'
26529     ||' WHERE  sr_instance_code = :v_instance_code'
26530     ||' AND    process_flag = '||G_IN_PROCESS
26531     ||' AND    NVL(task_number,'||''''||NULL_CHAR||''''||') '
26532     ||'         <> '||''''||NULL_CHAR||''''
26533     ||' AND    batch_id = :lv_batch_id ';
26534 
26535 
26536             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26537 
26538      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
26539 
26540 
26541     -- derive costing_group_id
26542 
26543      v_sql_stmt := 08;
26544 
26545      lv_sql_stmt :=
26546      'UPDATE        msc_st_project_tasks   mstp'
26547      ||' SET        costing_group_id   = ( SELECT local_id '
26548      ||'           FROM msc_local_id_misc mlim'
26549      ||'           WHERE     mlim.char1       = mstp.sr_instance_code'
26550      ||'           AND   NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
26551      ||'              =    NVL(mstp.company_name,'||''''||NULL_CHAR||''''||') '
26552 /*     ||'           AND       mlim.char3       = mstp.organization_code'*/
26553      ||'           AND       mlim.char4       = mstp.costing_group_code'
26554      ||'           AND       mlim.entity_name = ''COSTING_GROUP_ID'')'
26555      ||' WHERE      sr_instance_code = :v_instance_code'
26556      ||' AND        NVL(costing_group_code, '||''''||NULL_CHAR||''''||') '
26557      ||'         <> '||''''||NULL_CHAR||''''
26558      ||' AND        process_flag     = '||G_IN_PROCESS
26559      ||' AND        batch_id         = :lv_batch_id' ;
26560 
26561 
26562 
26563             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26564 
26565       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
26566 
26567    --Call to customised validation.
26568     MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
26569         (ERRBUF         => lv_error_text,
26570          RETCODE        => lv_return,
26571          pBatchID       => lv_batch_id,
26572          pInstanceCode  => v_instance_code,
26573          pEntityName    => 'MSC_ST_PROJECT_TASKS',
26574          pInstanceID    => v_instance_id);
26575 
26576       IF NVL(lv_return,0) <> 0 THEN
26577         RAISE ex_logging_err;
26578       END IF;
26579 
26580     -- Generate costing_group_id where cost group code is Not null but id is Null
26581 
26582      v_sql_stmt := 09;
26583 
26584      OPEN c5(lv_batch_id);
26585      FETCH c5 BULK COLLECT INTO lb_rowid ;
26586 
26587      IF c5%ROWCOUNT > 0  THEN
26588 
26589     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
26590 
26591         UPDATE msc_st_project_tasks
26592         SET    costing_group_id          = msc_st_costing_group_id_s.NEXTVAL
26593         WHERE  rowid                     = lb_rowid(j);
26594 
26595 
26596     v_sql_stmt := 10;
26597 
26598      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
26599     -- Insert into the LID table  with the  user defined key
26600 
26601        INSERT INTO  msc_local_id_misc
26602        (local_id,
26603        st_transaction_id,
26604        instance_id,
26605        entity_name,
26606        data_source_type,
26607        char1,
26608        char2,
26609        char3,
26610        char4,
26611        SOURCE_ORG_ID,
26612        SOURCE_COSTING_GROUP_ID,
26613        last_update_date,
26614        last_updated_by,
26615        creation_date,
26616        created_by  )
26617       SELECT
26618        costing_group_id,
26619        st_transaction_id,
26620        v_instance_id,
26621        'COSTING_GROUP_ID',
26622        data_source_type,
26623        sr_instance_code,
26624        company_name,
26625        organization_code,
26626        costing_group_code,
26627        SOURCE_ORG_ID,
26628        SOURCE_COSTING_GROUP_ID,
26629        v_current_date,
26630        v_current_user,
26631        v_current_date,
26632        v_current_user
26633       FROM msc_st_project_tasks
26634       WHERE rowid = lb_rowid(j);
26635    END IF;
26636    CLOSE c5;
26637 
26638     -- update costing_group_id
26639 
26640      v_sql_stmt := 11;
26641 
26642      lv_sql_stmt :=
26643      'UPDATE       msc_st_project_tasks   mstp'
26644      ||' SET       costing_group_id   = ( SELECT local_id '
26645      ||'           FROM msc_local_id_misc mlim'
26646      ||'           WHERE     mlim.char1       = mstp.sr_instance_code'
26647      ||'           AND   NVL(mlim.char2,'||''''||NULL_CHAR||''''||') '
26648      ||'              =    NVL(mstp.company_name,'||''''||NULL_CHAR||''''||') '
26649 /*     ||'           AND       mlim.char3       = mstp.organization_code'*/
26650      ||'           AND       mlim.char4       = mstp.costing_group_code'
26651      ||'           AND       mlim.entity_name = ''COSTING_GROUP_ID'')'
26652      ||' WHERE      sr_instance_code = :v_instance_code'
26653      ||' AND        NVL(costing_group_code, '||''''||NULL_CHAR||''''||') '
26654      ||'            <> '||''''||NULL_CHAR||''''
26655      ||' AND    NVL(costing_group_id,'||NULL_VALUE||') = '||NULL_VALUE
26656      ||' AND        process_flag     = '||G_IN_PROCESS
26657      ||' AND        batch_id         = :lv_batch_id' ;
26658 
26659 
26660 
26661             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
26662 
26663       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
26664 
26665    -- Project id generation
26666 
26667     v_sql_stmt := 12;
26668 
26669      OPEN c3(lv_batch_id);
26670      FETCH c3 BULK COLLECT INTO lb_rowid ;
26671 
26672      IF c3%ROWCOUNT > 0  THEN
26673 
26674     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
26675 
26676         UPDATE msc_st_project_tasks
26677         SET    project_id             = msc_st_project_id_s.NEXTVAL
26678         WHERE  rowid                  = lb_rowid(j);
26679 
26680 
26681     v_sql_stmt := 13;
26682 
26683     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
26684 
26685     -- Insert into the LID table  with the  user defined key
26686     INSERT INTO  msc_local_id_misc
26687        (local_id,
26688        st_transaction_id,
26689        instance_id,
26690        entity_name,
26691        data_source_type,
26692        char1,
26693        char2,
26694        char3,
26695        char4,
26696        SOURCE_ORG_ID,
26697        SOURCE_COSTING_GROUP_ID,
26698        SOURCE_PROJECT_ID,
26699        last_update_date,
26700        last_updated_by,
26701        creation_date,
26702        created_by  )
26703       SELECT
26704        project_id,
26705        st_transaction_id,
26706        v_instance_id,
26707        'PROJECT_ID',
26708        data_source_type,
26709        sr_instance_code,
26710        company_name,
26711        organization_code,
26712        project_number,
26713        SOURCE_ORG_ID,
26714        SOURCE_COSTING_GROUP_ID,
26715        SOURCE_PROJECT_ID,
26716        v_current_date,
26717        v_current_user,
26718        v_current_date,
26719        v_current_user
26720       FROM msc_st_project_tasks
26721       WHERE rowid = lb_rowid(j);
26722      END IF;
26723 
26724    CLOSE c3;
26725 
26726     -- Derive Project id
26727      lv_return :=
26728      MSC_ST_UTIL.DERIVE_PROJ_TASK_ID(p_table_name => 'MSC_ST_PROJECT_TASKS',
26729                              p_proj_col_name      => 'PROJECT_NUMBER',
26730                              p_proj_task_col_id   => 'PROJECT_ID',
26731                              p_instance_code      => v_instance_code,
26732                              p_entity_name        => 'PROJECT_ID',
26733                              p_batch_id           => lv_batch_id,
26734                              p_error_text         => lv_error_text,
26735                              p_debug              => v_debug,
26736                              p_row                => lv_column_names);
26737 
26738      IF(lv_return <> 0) then
26739         RAISE ex_logging_err;
26740      END IF;
26741 
26742   -- Task Id generation
26743 
26744     v_sql_stmt := 14;
26745 
26746      OPEN c4(lv_batch_id);
26747      FETCH c4 BULK COLLECT INTO lb_rowid ;
26748 
26749      IF c4%ROWCOUNT > 0  THEN
26750 
26751     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
26752 
26753         UPDATE msc_st_project_tasks
26754         SET    task_id                = msc_st_task_id_s.NEXTVAL
26755         WHERE  rowid                  = lb_rowid(j);
26756 
26757     v_sql_stmt := 15;
26758 
26759     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
26760 
26761     -- Insert into the LID table  with the  user defined key
26762     INSERT INTO  msc_local_id_misc
26763        (local_id,
26764        st_transaction_id,
26765        instance_id,
26766        entity_name,
26767        data_source_type,
26768        char1,
26769        char2,
26770        char3,
26771        char4,
26772        char5,
26773        SOURCE_ORG_ID,
26774        SOURCE_COSTING_GROUP_ID,
26775        SOURCE_PROJECT_ID,
26776        SOURCE_TASK_ID,
26777        last_update_date,
26778        last_updated_by,
26779        creation_date,
26780        created_by  )
26781       SELECT
26782        task_id,
26783        st_transaction_id,
26784        v_instance_id,
26785        'TASK_ID',
26786        data_source_type,
26787        sr_instance_code,
26788        company_name,
26789        organization_code,
26790        project_number,
26791        task_number,
26792        SOURCE_ORG_ID,
26793        SOURCE_COSTING_GROUP_ID,
26794        SOURCE_PROJECT_ID,
26795        SOURCE_TASK_ID,
26796        v_current_date,
26797        v_current_user,
26798        v_current_date,
26799        v_current_user
26800       FROM msc_st_project_tasks
26801       WHERE rowid = lb_rowid(j);
26802      END IF;
26803   CLOSE c4;
26804 
26805 -- Derive task id
26806      lv_return :=
26807      MSC_ST_UTIL.DERIVE_PROJ_TASK_ID(p_table_name =>'MSC_ST_PROJECT_TASKS',
26808                              p_proj_col_name      =>'PROJECT_NUMBER',
26809                              p_proj_task_col_id   =>'TASK_ID',
26810                              p_task_col_name      =>'TASK_NUMBER',
26811                              p_instance_code      => v_instance_code,
26812                              p_entity_name        => 'TASK_ID',
26813                              p_batch_id           =>lv_batch_id,
26814                              p_error_text         =>lv_error_text,
26815                              p_debug              => v_debug,
26816                              p_row                =>lv_column_names);
26817 
26818      IF(lv_return <> 0) then
26819         RAISE ex_logging_err;
26820      END IF;
26821   -- Insert records in MSC_ST_PROJECTS
26822  v_sql_stmt := 16;
26823 
26824  lv_sql_stmt:=
26825    'INSERT INTO msc_st_projects'
26826    ||' (project_id,'
26827    ||'  organization_id,'
26828    ||'  planning_group,'
26829    ||'  costing_group_id, '
26830    ||'  wip_acct_class_code, '
26831    ||'  seiban_number_flag, '
26832    ||'  project_name,'
26833    ||'  project_number, '
26834    ||'  project_description, '
26835    ||'  start_date, '
26836    ||'  completion_date, '
26837    ||'  material_account,'
26838    ||'  manager_contact,'
26839    ||'  sr_instance_id,'
26840    ||'  refresh_id, '
26841    ||'  last_update_date,'
26842    ||'  last_updated_by, '
26843    ||'  creation_date, '
26844    ||'  created_by ) '
26845    ||'   SELECT'
26846    ||'  project_id, '
26847    ||'  organization_id,'
26848    ||'  planning_group,'
26849    ||'  costing_group_id,'
26850    ||'  wip_acct_class_code,'
26851    ||'  seiban_number_flag,'
26852    ||'  project_number,'
26853    ||'  project_number,'
26854    ||'  project_description,'
26855    ||'  project_start_date,'
26856    ||'  project_completion_date, '
26857    ||'  material_account,'
26858    ||'  manager_contact,'
26859    ||   v_instance_id||','
26860    ||'  REFRESH_ID,'
26861    ||'  last_update_date,'
26862    ||'  last_updated_by,'
26863    ||'  creation_date,'
26864    ||'  created_by'
26865    ||' FROM msc_st_project_tasks'
26866    ||' WHERE sr_instance_code  = :v_instance_code'
26867    ||' AND   process_flag      = '||G_IN_PROCESS
26868    ||' AND   batch_id          = :lv_batch_id' ;
26869 
26870   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
26871 
26872   -- Delete all the records from msc_st_projects_tasks where task number is NULL
26873   -- This is done as records with only task name is inserted in msc_st_projects_tasks
26874 
26875    lv_sql_stmt :=
26876    ' DELETE FROM msc_st_project_tasks '
26877    ||' WHERE        NVL(task_number, '||''''||NULL_CHAR||''''||') '
26878    ||'         = '||''''||NULL_CHAR||''''
26879    ||' AND   sr_instance_code  = :v_instance_code'
26880    ||' AND   process_flag      = '||G_IN_PROCESS
26881    ||' AND   batch_id          = :lv_batch_id' ;
26882 
26883    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
26884 
26885     -- Set the process flag as Valid and poulate instance_id
26886     lv_return :=
26887     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_PROJECT_TASKS',
26888                                p_instance_id    => v_instance_id,
26889                                p_instance_code  => v_instance_code,
26890                                p_process_flag   => G_VALID,
26891                                p_error_text     => lv_error_text,
26892                                p_debug          => v_debug,
26893                                p_batch_id       => lv_batch_id);
26894 
26895       IF lv_return < 0 THEN
26896         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
26897       END IF;
26898 
26899 
26900     -- At the end calling  the LOG_ERROR for logging all errored out records
26901 
26902    lv_return :=
26903    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_PROJECT_TASKS',
26904                          p_instance_code => v_instance_code,
26905                          p_row           => lv_column_names,
26906                          p_severity      => G_SEV_ERROR,
26907                          p_message_text  => NULL,
26908                          p_error_text    => lv_error_text,
26909                          p_debug         => v_debug,
26910                          p_batch_id      => lv_batch_id  );
26911 
26912      IF(lv_return <> 0) then
26913         RAISE ex_logging_err;
26914      END IF;
26915 
26916    COMMIT;
26917   END LOOP;
26918 
26919  EXCEPTION
26920     WHEN too_many_rows THEN
26921          lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_PROJ_TASK'||'('
26922                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
26923         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
26924          ROLLBACK ;
26925 
26926     WHEN ex_logging_err THEN
26927         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
26928         ROLLBACK;
26929 
26930    WHEN OTHERS THEN
26931        lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_PROJ_TASK'||'('
26932                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
26933         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
26934       ROLLBACK;
26935 
26936    END LOAD_PROJ_TASK;
26937 
26938 
26939   /*==========================================================================+
26940   | DESCRIPTION  : This function performs the validation and loads the data   |
26941   |                for business object Resource and Department.               |
26942   +==========================================================================*/
26943   PROCEDURE  LOAD_DEPT_RES IS
26944 
26945   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
26946   TYPE CurTyp           IS REF CURSOR;
26947   c1                    CurTyp;
26948   lb_rowid              RowidTab;
26949 
26950   lv_column_names        VARCHAR2(5000);     -- Stores cocatenated column names
26951   lv_return              NUMBER;
26952   lv_error_text          VARCHAR2(250);
26953   lv_where_str           VARCHAR2(5000);
26954   lv_sql_stmt            VARCHAR2(5000);
26955   lv_cursor_stmt         VARCHAR2(5000);
26956 
26957   lv_simulation_set      msc_st_resource_changes.simulation_set%type;
26958   lv_batch_id            msc_st_department_resources.batch_id%TYPE;
26959   lv_message_text        msc_errors.error_text%TYPE;
26960 
26961 
26962   ex_logging_err    EXCEPTION;
26963 
26964   CURSOR c2(p_batch_id NUMBER) IS
26965     SELECT rowid
26966     FROM   msc_st_department_resources
26967     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
26968     AND    sr_instance_code = v_instance_code
26969     AND    batch_id         = p_batch_id;
26970 
26971  CURSOR c3(p_batch_id NUMBER) IS
26972     SELECT max(rowid)
26973     FROM   msc_st_department_resources
26974     WHERE  process_flag     = G_IN_PROCESS
26975     AND    sr_instance_code = v_instance_code
26976     AND    batch_id         = p_batch_id
26977     AND    NVL(department_id,NULL_VALUE) = NULL_VALUE
26978     AND    deleted_flag      = SYS_NO
26979     AND    line_flag         = '2'
26980     GROUP BY sr_instance_code,company_name,
26981              organization_code,department_code  ;
26982 
26983  CURSOR c4(p_batch_id NUMBER) IS
26984     SELECT max(rowid)
26985     FROM   msc_st_department_resources
26986     WHERE  process_flag     = G_IN_PROCESS
26987     AND    sr_instance_code = v_instance_code
26988     AND    batch_id         = p_batch_id
26989     AND    NVL(resource_id,NULL_VALUE) = NULL_VALUE
26990     AND    deleted_flag      = SYS_NO
26991     AND    line_flag         = '2'
26992     GROUP BY sr_instance_code,company_name,
26993              organization_code,resource_code  ;
26994 
26995  CURSOR c5 (p_batch_id NUMBER) IS
26996     SELECT rowid
26997     FROM   msc_st_department_resources
26998     WHERE  process_flag     = G_IN_PROCESS
26999     AND    sr_instance_code = v_instance_code
27000     AND    batch_id         = p_batch_id
27001     AND    NVL(department_id,NULL_VALUE) = NULL_VALUE
27002     AND    deleted_flag      = SYS_NO
27003     AND    line_flag         = '1'   ;
27004 
27005 
27006   CURSOR c6(p_batch_id NUMBER) IS
27007     SELECT rowid
27008     FROM   msc_st_resource_shifts
27009     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
27010     AND    sr_instance_code = v_instance_code
27011     AND    batch_id         = p_batch_id;
27012 
27013  CURSOR c7(p_batch_id NUMBER) IS
27014     SELECT rowid
27015     FROM   msc_st_resource_changes
27016     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
27017     AND    sr_instance_code = v_instance_code
27018     AND    batch_id         = p_batch_id;
27019 
27020  CURSOR c8(p_batch_id NUMBER) IS
27021     SELECT max(rowid)
27022     FROM   msc_st_resource_changes msrc
27023     WHERE  NOT EXISTS (SELECT 1 FROM msc_simulation_sets mss
27024                        WHERE msrc.simulation_set = mss.simulation_set
27025                        AND   msrc.sr_instance_id = mss.sr_instance_id
27026                        AND   msrc.organization_id = mss.organization_id)
27027     AND    msrc.process_flag     = G_VALID
27028     AND    msrc.sr_instance_code = v_instance_code
27029     AND    msrc.deleted_flag     = SYS_NO
27030     AND    msrc.batch_id         = p_batch_id
27031     GROUP BY simulation_set,sr_instance_code,
27032              company_name,organization_code ;
27033 
27034  CURSOR c9(p_batch_id NUMBER) IS
27035     SELECT rowid
27036     FROM   msc_st_resource_groups
27037     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
27038     AND    sr_instance_code = v_instance_code
27039     AND    batch_id         = p_batch_id;
27040 
27041 BEGIN
27042 
27043     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27044                  (p_app_short_name    => 'MSC',
27045                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
27046                   p_message_text      => lv_message_text,
27047                   p_error_text        => lv_error_text);
27048 
27049     IF lv_return <> 0 THEN
27050       RAISE ex_logging_err;
27051     END IF;
27052 
27053     --Duplicate records check for the records whose source is XML
27054     v_sql_stmt := 01;
27055     lv_sql_stmt :=
27056     ' UPDATE   msc_st_resource_groups mrg1'
27057     ||' SET    process_flag = '||G_ERROR_FLG||','
27058     ||'        error_text   = '||''''||lv_message_text||''''
27059     ||' WHERE  message_id <  (SELECT MAX(message_id)'
27060     ||'        FROM  msc_st_resource_groups mrg2'
27061     ||'        WHERE  mrg2.sr_instance_code  = mrg1.sr_instance_code'
27062     ||'        AND    mrg2.group_code        = mrg1.group_code'
27063     ||'        AND   NVL(mrg2.company_name,  '   ||''''||NULL_CHAR||''''||') = '
27064     ||'              NVL(mrg1.company_name,  '   ||''''||NULL_CHAR||''''||')'
27065     ||'        AND   mrg2.process_flag = mrg1.process_flag'
27066     ||'        AND   NVL(mrg2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
27067     ||' AND    mrg1.process_flag     = '||G_IN_PROCESS
27068     ||' AND    mrg1.sr_instance_code = :v_instance_code'
27069     ||' AND    NVL(mrg1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
27070 
27071 
27072       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27073 
27074     EXECUTE IMMEDIATE lv_sql_stmt
27075             USING     v_instance_code;
27076 
27077     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27078                  (p_app_short_name    => 'MSC',
27079                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
27080                   p_message_text      => lv_message_text,
27081                   p_error_text        => lv_error_text);
27082 
27083     IF lv_return <> 0 THEN
27084       RAISE ex_logging_err;
27085     END IF;
27086 
27087     --Duplicate records check for the records whose source is other than XML
27088     --Different SQL is used because in XML we can identify the latest records
27089     --whereas in batch load we cannot.
27090     v_sql_stmt := 02;
27091     lv_sql_stmt :=
27092     ' UPDATE   msc_st_resource_groups mrg1'
27093     ||' SET    process_flag = '||G_ERROR_FLG||','
27094     ||'        error_text   = '||''''||lv_message_text||''''
27095     ||' WHERE  EXISTS( SELECT 1 '
27096     ||'        FROM  msc_st_resource_groups mrg2'
27097     ||'        WHERE mrg2.sr_instance_code  = mrg1.sr_instance_code'
27098     ||'        AND   mrg2.group_code        = mrg1.group_code'
27099     ||'        AND   NVL(mrg2.company_name,  '   ||''''||NULL_CHAR||''''||') = '
27100     ||'              NVL(mrg1.company_name,  '   ||''''||NULL_CHAR||''''||')'
27101     ||'        AND   mrg2.process_flag      = mrg1.process_flag'
27102     ||'        AND   NVL(mrg2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
27103     ||'        GROUP BY sr_instance_code,group_code,company_name '
27104     ||'         HAVING COUNT(*) > 1)'
27105     ||' AND     mrg1.process_flag     = '||G_IN_PROCESS
27106     ||' AND     mrg1.sr_instance_code = :v_instance_code'
27107     ||' AND     NVL(mrg1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
27108 
27109 
27110       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27111 
27112     EXECUTE IMMEDIATE lv_sql_stmt
27113             USING     v_instance_code;
27114 
27115     lv_column_names :=
27116     'GROUP_CODE         ||''~''||'
27117     ||'MEANING          ||''~''||'
27118     ||'DESCRIPTION      ||''~''||'
27119     ||'FROM_DATE        ||''~''||'
27120     ||'TO_DATE          ||''~''||'
27121     ||'ENABLED_FLAG     ||''~''||'
27122     ||'SR_INSTANCE_CODE ||''~''||'
27123     ||'COMPANY_NAME     ||''~''||'
27124     ||'DELETED_FLAG';
27125 
27126     LOOP
27127       v_sql_stmt := 03;
27128       SELECT msc_st_batch_id_s.NEXTVAL
27129       INTO   lv_batch_id
27130       FROM   dual;
27131 
27132       v_sql_stmt := 04;
27133       lv_sql_stmt :=
27134       ' UPDATE   msc_st_resource_groups '
27135       ||' SET    batch_id  = :lv_batch_id'
27136       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
27137       ||' AND    sr_instance_code               = :v_instance_code'
27138       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
27139       ||' AND    rownum                        <= '||v_batch_size;
27140 
27141 
27142         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27143 
27144       EXECUTE IMMEDIATE lv_sql_stmt
27145               USING     lv_batch_id,
27146                         v_instance_code;
27147 
27148       EXIT WHEN SQL%NOTFOUND;
27149 
27150       OPEN c9(lv_batch_id);
27151       FETCH c9 BULK COLLECT INTO lb_rowid;
27152       CLOSE c9;
27153 
27154       v_sql_stmt := 05;
27155       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
27156       UPDATE msc_st_resource_groups
27157       SET  st_transaction_id = msc_st_resource_groups_s.NEXTVAL,
27158            refresh_id        = v_refresh_id,
27159            last_update_date  = v_current_date,
27160            last_updated_by   = v_current_user,
27161            creation_date     = v_current_date,
27162            created_by        = v_current_user
27163       WHERE  rowid           = lb_rowid(j);
27164 
27165       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27166                      (p_app_short_name    => 'MSC',
27167                       p_error_code        => 'MSC_PP_NO_DELETION',
27168                       p_message_text      => lv_message_text,
27169                       p_error_text        => lv_error_text,
27170                       p_token1            => 'TABLE_NAME',
27171                       p_token_value1      => 'MSC_ST_RESOURCE_GROUPS');
27172 
27173       IF lv_return <> 0 THEN
27174         RAISE ex_logging_err;
27175       END IF;
27176 
27177       --Deletion is not allowed for this entity.
27178       v_sql_stmt := 06;
27179       lv_sql_stmt :=
27180       ' UPDATE     msc_st_resource_groups '
27181       ||' SET      process_flag     = '||G_ERROR_FLG||','
27182       ||'          error_text       = '||''''||lv_message_text||''''
27183       ||' WHERE    deleted_flag     = '||SYS_YES
27184       ||' AND      process_flag     = '||G_IN_PROCESS
27185       ||' AND      batch_id         =  :lv_batch_id'
27186       ||' AND      sr_instance_code = :v_instance_code';
27187 
27188 
27189         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27190 
27191       EXECUTE IMMEDIATE lv_sql_stmt
27192               USING     lv_batch_id,
27193                         v_instance_code;
27194 
27195       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27196                      (p_app_short_name    => 'MSC',
27197                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
27198                       p_message_text      => lv_message_text,
27199                       p_error_text        => lv_error_text,
27200                       p_token1            => 'COLUMN_NAME',
27201                       p_token_value1      => 'GROUP_CODE OR MEANING');
27202 
27203       IF lv_return <> 0 THEN
27204         RAISE ex_logging_err;
27205       END IF;
27206 
27207       v_sql_stmt := 07;
27208       lv_sql_stmt :=
27209       ' UPDATE  msc_st_resource_groups '
27210       ||' SET   process_flag        = '||G_ERROR_FLG||','
27211       ||'       error_text          = '||''''||lv_message_text||''''
27212       ||' WHERE (group_code IS NULL '
27213       ||' OR    meaning IS NULL) '
27214       ||' AND   batch_id            = :lv_batch_id'
27215       ||' AND   process_flag        = '||G_IN_PROCESS
27216       ||' AND   sr_instance_code    = :v_instance_code';
27217 
27218 
27219         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27220       EXECUTE IMMEDIATE lv_sql_stmt
27221               USING     lv_batch_id,
27222                         v_instance_code;
27223 
27224 
27225      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27226                      (p_app_short_name    => 'MSC',
27227                       p_error_code        => 'MSC_PP_INVALID_VALUE',
27228                       p_message_text      => lv_message_text,
27229                       p_error_text        => lv_error_text,
27230                       p_token1            => 'COLUMN_NAME',
27231                       p_token_value1      => 'FROM_DATE AND TO_DATE');
27232 
27233       IF lv_return <> 0 THEN
27234         RAISE ex_logging_err;
27235       END IF;
27236 
27237       v_sql_stmt := 08;
27238       lv_sql_stmt :=
27239       ' UPDATE  msc_st_resource_groups '
27240       ||' SET   process_flag        = '||G_ERROR_FLG||','
27241       ||'       error_text          = '||''''||lv_message_text||''''
27242       ||' WHERE from_date           > to_date '
27243       ||' AND   batch_id            = :lv_batch_id'
27244       ||' AND   process_flag        = '||G_IN_PROCESS
27245       ||' AND   sr_instance_code    = :v_instance_code';
27246 
27247 
27248         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27249       EXECUTE IMMEDIATE lv_sql_stmt
27250               USING     lv_batch_id,
27251                         v_instance_code;
27252 
27253       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27254                      (p_app_short_name    => 'MSC',
27255                       p_error_code        => 'MSC_PP_NO_UPDATION',
27256                       p_message_text      => lv_message_text,
27257                       p_error_text        => lv_error_text,
27258                       p_token1            => 'TABLE_NAME',
27259                       p_token_value1      => 'MSC_ST_RESOURCE_GROUPS');
27260 
27261       IF lv_return <> 0 THEN
27262         RAISE ex_logging_err;
27263       END IF;
27264 
27265       --Updation is not allowed on this entity(this is done to make the
27266       --Post-Processor in sync with the collection program).
27267       v_sql_stmt := 09;
27268       lv_sql_stmt :=
27269       ' UPDATE   msc_st_resource_groups mrg1'
27270       ||' SET    process_flag     = '||G_ERROR_FLG||','
27271       ||'        error_text       = '||''''||lv_message_text||''''
27272       ||' WHERE  process_flag     = '||G_IN_PROCESS
27273       ||' AND    batch_id         =  :lv_batch_id'
27274       ||' AND    sr_instance_code =  :v_instance_code'
27275       ||' AND    EXISTS'
27276       ||'        (SELECT 1 FROM msc_resource_groups mrg2'
27277       ||'        WHERE  mrg2.group_code = mrg1.group_code'
27278       ||'        AND    mrg2.sr_instance_id = ' ||v_instance_id ||')';
27279 
27280 
27281         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27282 
27283       EXECUTE IMMEDIATE lv_sql_stmt
27284               USING     lv_batch_id,
27285                         v_instance_code;
27286 
27287      -- Set the error message
27288     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27289                      (p_app_short_name    => 'MSC',
27290                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
27291                       p_message_text      => lv_message_text,
27292                       p_error_text        => lv_error_text,
27293                       p_token1            => 'COLUMN_NAME',
27294                       p_token_value1      => 'ENABLED_FLAG',
27295                       p_token2            => 'DEFAULT_VALUE',
27296                       p_token_value2      => SYS_YES );
27297 
27298       IF lv_return <> 0 THEN
27299         RAISE ex_logging_err;
27300       END IF;
27301 
27302       -- Valid values for enabled_flag is 1(ie.,'Y') and 2('N'). For any other
27303       -- value generates a warning and defaults the value to 'Y'.
27304 
27305      lv_where_str :=
27306      '    AND NVL(enabled_flag,'||NULL_VALUE||') NOT IN (1,2) ';
27307 
27308      lv_return := MSC_ST_UTIL.LOG_ERROR
27309                     (p_table_name        => 'MSC_ST_RESOURCE_GROUPS',
27310                      p_instance_code     => v_instance_code,
27311                      p_row               => lv_column_names,
27312                      p_severity          => G_SEV_WARNING,
27313                      p_message_text      => lv_message_text,
27314                      p_error_text        => lv_error_text,
27315                      p_batch_id          => lv_batch_id,
27316                      p_where_str         => lv_where_str,
27317                      p_col_name          => 'ENABLED_FLAG',
27318                      p_debug             => v_debug,
27319                      p_default_value     => SYS_YES);
27320 
27321      IF(lv_return <> 0) then
27322         RAISE ex_logging_err;
27323      END IF;
27324 
27325       --Call to customised validation.
27326       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
27327         (ERRBUF         => lv_error_text,
27328          RETCODE        => lv_return,
27329          pBatchID       => lv_batch_id,
27330          pInstanceCode  => v_instance_code,
27331          pEntityName    => 'MSC_ST_RESOURCE_GROUPS',
27332          pInstanceID    => v_instance_id);
27333 
27334       IF NVL(lv_return,0) <> 0 THEN
27335         RAISE ex_logging_err;
27336       END IF;
27337 
27338       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
27339                   (p_table_name     => 'MSC_ST_RESOURCE_GROUPS',
27340                    p_instance_id    => v_instance_id,
27341                    p_instance_code  => v_instance_code,
27342                    p_process_flag   => G_VALID,
27343                    p_error_text     => lv_error_text,
27344                    p_debug          => v_debug,
27345                    p_batch_id       => lv_batch_id);
27346 
27347       IF lv_return <> 0 THEN
27348         RAISE ex_logging_err;
27349       END IF;
27350 
27351       lv_return := MSC_ST_UTIL.LOG_ERROR
27352                    (p_table_name        => 'MSC_ST_RESOURCE_GROUPS',
27353                     p_instance_code     => v_instance_code,
27354                     p_row               => lv_column_names,
27355                     p_severity          => G_SEV_ERROR,
27356                     p_message_text      => NULL,
27357                     p_error_text        => lv_error_text,
27358                     p_debug             => v_debug,
27359                     p_batch_id          => lv_batch_id);
27360 
27361       IF lv_return <> 0 THEN
27362         RAISE ex_logging_err;
27363       END IF;
27364 
27365      --Inserts the valid records into the ODS table, Since the collection program
27366      --doesn't collect this entity incrementally and whereras post-processor always
27367      --runs in the incremental mode.
27368 
27369      v_sql_stmt := 10;
27370      lv_sql_stmt :=
27371       ' INSERT INTO msc_resource_groups '
27372       ||' ( group_code,'
27373       ||'   meaning,'
27374       ||'   description,'
27375       ||'   from_date,'
27376       ||'   to_date,'
27377       ||'   enabled_flag,'
27378       ||'   sr_instance_id,'
27379       ||'   last_update_date,'
27380       ||'   last_updated_by,'
27381       ||'   creation_date,'
27382       ||'   created_by)'
27383       ||' SELECT'
27384       ||'   group_code,'
27385       ||'   meaning,'
27386       ||'   description,'
27387       ||'   from_date,'
27388       ||'   to_date,'
27389       ||'   enabled_flag,'
27390       ||'   sr_instance_id,'
27391       ||'   last_update_date,'
27392       ||'   last_updated_by,'
27393       ||'   creation_date,'
27394       ||'   created_by '
27395       ||' FROM   msc_st_resource_groups'
27396       ||' WHERE  sr_instance_code = :sr_instance_code'
27397       ||' AND    batch_id         = :lv_batch_id'
27398       ||' AND    process_flag     = '||G_VALID;
27399 
27400 
27401         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27402 
27403       EXECUTE IMMEDIATE lv_sql_stmt
27404               USING     v_instance_code,
27405                         lv_batch_id;
27406 
27407       COMMIT;
27408     END LOOP;
27409 
27410   -- Before we start processing the record by group id( batch size ) we are going
27411   -- to check whether that there
27412   -- is any duplicates for user defined unique keys (UDKs,)
27413 
27414 
27415    --Duplicate records check for the records whose source is XML
27416 
27417     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27418                  (p_app_short_name    => 'MSC',
27419                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
27420                   p_message_text      => lv_message_text,
27421                   p_error_text        => lv_error_text);
27422 
27423     IF lv_return <> 0 THEN
27424       RAISE ex_logging_err;
27425     END IF;
27426 
27427   v_sql_stmt := 01;
27428   lv_sql_stmt :=
27429   'UPDATE   msc_st_department_resources msdr1'
27430   ||' SET       process_flag = '||G_ERROR_FLG||','
27431   ||'         error_text   = '||''''||lv_message_text||''''
27432   ||' WHERE     message_id < ( SELECT max(message_id) FROM msc_st_department_resources msdr2'
27433   ||'                WHERE msdr2.sr_instance_code  = msdr1.sr_instance_code'
27434   ||'                AND   NVL(msdr2.company_name,'||''''||NULL_CHAR||''''||') '
27435   ||'                =    NVL(msdr1.company_name,'||''''||NULL_CHAR||''''||') '
27436   ||'                AND   msdr2.organization_code = msdr1.organization_code'
27437   ||'                AND   msdr2.department_code   = msdr1.department_code'
27438   ||'                AND   msdr2.resource_code     = msdr1.resource_code'
27439   ||'                AND   msdr2.process_flag      = '||G_IN_PROCESS
27440   ||'                AND   NVL(msdr2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
27441   ||' AND       msdr1.process_flag     = '|| G_IN_PROCESS
27442   ||' AND       msdr1.sr_instance_code = :v_instance_code '
27443   ||' AND       NVL(msdr1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
27444 
27445 
27446          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27447 
27448   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
27449 
27450 
27451     --Duplicate records check for the records whose source is batch load
27452 
27453     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27454                  (p_app_short_name    => 'MSC',
27455                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
27456                   p_message_text      => lv_message_text,
27457                   p_error_text        => lv_error_text);
27458 
27459     IF lv_return <> 0 THEN
27460       RAISE ex_logging_err;
27461     END IF;
27462   lv_sql_stmt :=
27463   'UPDATE      msc_st_department_resources msdr1'
27464   ||' SET      process_flag ='|| G_ERROR_FLG||','
27465   ||'          error_text   = '||''''||lv_message_text||''''
27466   ||' WHERE    EXISTS ( SELECT 1 FROM msc_st_department_resources msdr2'
27467   ||'               WHERE msdr2.sr_instance_code  = msdr1.sr_instance_code'
27468   ||'          AND   NVL(msdr2.company_name,'||''''||NULL_CHAR||''''||') '
27469   ||'                 =    NVL(msdr1.company_name,'||''''||NULL_CHAR||''''||') '
27470   ||'               AND   msdr2.organization_code = msdr1.organization_code'
27471   ||'               AND   msdr2.department_code   = msdr1.department_code'
27472   ||'               AND   msdr2.resource_code     = msdr1.resource_code'
27473   ||'               AND   msdr2.process_flag      = '||G_IN_PROCESS
27474   ||'               AND NVL(msdr2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
27475   ||'              GROUP BY sr_instance_code,company_name,organization_code,'
27476   ||'              department_code,resource_code'
27477   ||'              HAVING COUNT(*) >1 )'
27478   ||' AND      msdr1.process_flag           ='|| G_IN_PROCESS
27479   ||' AND      msdr1.sr_instance_code       = :v_instance_code'
27480   ||' AND      NVL(msdr1.message_id,'||NULL_VALUE||')= '||NULL_VALUE;
27481 
27482 
27483          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27484 
27485   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
27486 
27487   -- Duplicate UDK validation for XML message
27488 
27489   v_sql_stmt := 02;
27490 
27491   lv_column_names :=
27492   'ORGANIZATION_CODE            ||''~''||'
27493   ||'LINE_FLAG                  ||''~''||'
27494   ||'AGGREGATED_RESOURCE_FLAG   ||''~''||'
27495   ||'AVAILABLE_24_HOURS_FLAG    ||''~''||'
27496   ||'SR_INSTANCE_CODE           ||''~''||'
27497   ||'RESOURCE_CODE              ||''~''||'
27498   ||'DEPARTMENT_CODE            ||''~''||'
27499   ||'DEPARTMENT_CLASS           ||''~''||'
27500   ||'OWNING_DEPARTMENT_CODE     ||''~''||'
27501   ||'CAPACITY_UNITS             ||''~''||'
27502   ||'MAX_RATE                   ||''~''||'
27503   ||'MIN_RATE                   ||''~''||'
27504   ||'RESOURCE_GROUP_NAME        ||''~''||'
27505   ||'RESOURCE_GROUP_CODE        ||''~''||'
27506   ||'RESOURCE_BALANCE_FLAG      ||''~''||'
27507   ||'BOTTLENECK_FLAG            ||''~''||'
27508   ||'START_TIME                 ||''~''||'
27509   ||'STOP_TIME                  ||''~''||'
27510   ||'DEPARTMENT_DESCRIPTION     ||''~''||'
27511   ||'RESOURCE_DESCRIPTION       ||''~''||'
27512   ||'OVER_UTILIZED_PERCENT      ||''~''||'
27513   ||'UNDER_UTILIZED_PERCENT     ||''~''||'
27514   ||'RESOURCE_SHORTAGE_TYPE     ||''~''||'
27515   ||'RESOURCE_EXCESS_TYPE       ||''~''||'
27516   ||'USER_TIME_FENCE            ||''~''||'
27517   ||'UTILIZATION                ||''~''||'
27518   ||'EFFICIENCY                 ||''~''||'
27519   ||'RESOURCE_INCLUDE_FLAG      ||''~''||'
27520   ||'CRITICAL_RESOURCE_FLAG     ||''~''||'
27521   ||'RESOURCE_TYPE              ||''~''||'
27522   ||'DISABLE_DATE               ||''~''||'
27523   ||'CTP_FLAG                   ||''~''||'
27524   ||'DEPT_OVERHEAD_COST         ||''~''||'
27525   ||'RESOURCE_COST              ||''~''||'
27526   ||'RESOURCE_OVER_UTIL_COST    ||''~''||'
27527   ||'PLANNING_EXCEPTION_SET     ||''~''||'
27528   ||'DELETED_FLAG               ||''~''||'
27529   ||'COMPANY_NAME';
27530 
27531  -- Now processing by batch
27532 
27533    LOOP
27534       v_sql_stmt := 03;
27535       lv_cursor_stmt :=
27536       ' SELECT msc_st_batch_id_s.NEXTVAL '
27537       ||' FROM   dual';
27538 
27539       OPEN  c1 FOR lv_cursor_stmt;
27540       FETCH c1 INTO lv_batch_id;
27541       CLOSE c1;
27542 
27543       v_sql_stmt := 04;
27544       lv_sql_stmt :=
27545       ' UPDATE msc_st_department_resources '
27546       ||' SET    batch_id  = :lv_batch_id'
27547       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
27548       ||' AND    sr_instance_code               = :v_instance_code'
27549       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
27550       ||' AND    rownum                        <= '||v_batch_size;
27551 
27552 
27553          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27554 
27555       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
27556 
27557       EXIT WHEN SQL%NOTFOUND;
27558 
27559       OPEN c2(lv_batch_id);
27560       FETCH c2 BULK COLLECT INTO lb_rowid;
27561       CLOSE c2;
27562 
27563       v_sql_stmt := 05;
27564       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
27565       UPDATE msc_st_department_resources
27566       SET  st_transaction_id = msc_st_department_resources_s.NEXTVAL,
27567            refresh_id        = v_refresh_id,
27568            last_update_date  = v_current_date,
27569            last_updated_by   = v_current_user,
27570            creation_date     = v_current_date,
27571            created_by        = v_current_user
27572       WHERE  rowid           = lb_rowid(j);
27573 
27574 
27575      -- Set the message
27576     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27577                      (p_app_short_name    => 'MSC',
27578                       p_error_code        => 'MSC_PP_NO_DELETION',
27579                       p_message_text      => lv_message_text,
27580                       p_error_text        => lv_error_text,
27581                       p_token1            => 'TABLE_NAME',
27582                       p_token_value1      => 'MSC_ST_DEPARTMENT_RESOURCES');
27583 
27584       IF lv_return <> 0 THEN
27585         RAISE ex_logging_err;
27586       END IF;
27587        --Deletion is not allowed on this table.
27588       v_sql_stmt := 06;
27589       lv_sql_stmt :=
27590       ' UPDATE     msc_st_department_resources '
27591       ||' SET      process_flag     ='||G_ERROR_FLG||','
27592       ||'          error_text   = '||''''||lv_message_text||''''
27593       ||' WHERE    deleted_flag     = '||SYS_YES
27594       ||' AND      process_flag     = '||G_IN_PROCESS
27595       ||' AND      batch_id         =  :lv_batch_id'
27596       ||' AND      sr_instance_code = :v_instance_code';
27597 
27598 
27599             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27600 
27601       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
27602 
27603 
27604     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27605                      (p_app_short_name    => 'MSC',
27606                       p_error_code        => 'MSC_PP_INVALID_VALUE',
27607                       p_message_text      => lv_message_text,
27608                       p_error_text        => lv_error_text,
27609                       p_token1            => 'COLUMN_NAME',
27610                       p_token_value1      => 'RESOURCE_GROUP_NAME');
27611 
27612       IF lv_return <> 0 THEN
27613         RAISE ex_logging_err;
27614       END IF;
27615 
27616     -- Resource group Name should refer to a valid group code
27617     -- in the msc_st_resource_groups entity.
27618      v_sql_stmt := 08;
27619 
27620      lv_sql_stmt :=
27621      '   UPDATE  msc_st_department_resources mdr'
27622      ||' SET     process_flag = '||G_ERROR_FLG||','
27623      ||'         error_text   = '||''''||lv_message_text||''''
27624      ||' WHERE   NOT   EXISTS (SELECT 1'
27625      ||'         FROM  msc_resource_groups mrg'
27626      ||'         WHERE mrg.group_code         = mdr.resource_group_name '
27627      ||'         AND   (trunc(mrg.from_date) <= trunc(sysdate) '
27628      ||'         OR    trunc(mrg.to_date)    >= trunc(sysdate)) '
27629      ||'         AND   mrg.enabled_flag       = '||SYS_YES
27630      ||'         AND   mrg.sr_instance_id     = :v_instance_id)'
27631      ||' AND mdr.resource_group_name IS NOT NULL'
27632      ||' AND mdr.sr_instance_code   = :v_instance_code'
27633      ||' AND mdr.batch_id           = :lv_batch_id'
27634      ||' AND mdr.process_flag       = '||G_IN_PROCESS ;
27635 
27636 
27637          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27638 
27639     EXECUTE IMMEDIATE lv_sql_stmt
27640             USING     v_instance_id,
27641                       v_instance_code,
27642                       lv_batch_id;
27643 
27644       -- Set the error message
27645 
27646       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27647                      (p_app_short_name    => 'MSC',
27648                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
27649                       p_message_text      => lv_message_text,
27650                       p_error_text        => lv_error_text,
27651                       p_token1            => 'COLUMN_NAME',
27652                       p_token_value1      => 'DEPARTMENT_CODE OR RESOURCE_CODE');
27653 
27654       IF lv_return <> 0 THEN
27655         RAISE ex_logging_err;
27656       END IF;
27657 
27658     -- Error out record if department_code is null/resource_code is NULL and line_flag=2
27659 
27660       v_sql_stmt := 07;
27661      lv_sql_stmt :=
27662       ' UPDATE  msc_st_department_resources '
27663       ||' SET   process_flag        = '||G_ERROR_FLG||','
27664       ||'         error_text   = '||''''||lv_message_text||''''
27665       ||' WHERE (NVL(department_code,'||''''||NULL_CHAR||''''||')'
27666       ||'       = '||''''||NULL_CHAR||''''
27667       ||' OR  (NVL(resource_code,'||''''||NULL_CHAR||''''||')'
27668       ||'       = '||''''||NULL_CHAR||''''||') AND LINE_FLAG=2 )'
27669       ||' AND   batch_id         = :lv_batch_id'
27670       ||' AND   process_flag     = '||G_IN_PROCESS
27671       ||' AND   sr_instance_code = :v_instance_code';
27672 
27673 
27674         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27675       EXECUTE IMMEDIATE lv_sql_stmt
27676               USING     lv_batch_id,
27677                         v_instance_code;
27678 
27679     -- Set the error message
27680     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27681                      (p_app_short_name    => 'MSC',
27682                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
27683                       p_message_text      => lv_message_text,
27684                       p_error_text        => lv_error_text,
27685                       p_token1            => 'COLUMN_NAME',
27686                       p_token_value1      => 'MAX_RATE OR MIN_RATE'
27687                                               ||'OR START_TIME OR STOP_TIME' );
27688 
27689       IF lv_return <> 0 THEN
27690         RAISE ex_logging_err;
27691       END IF;
27692 
27693    -- Error out records for lines when
27694    -- max_rate, min_rate, start_time and stop_time is NULL
27695 
27696     v_sql_stmt:= 08;
27697 
27698     lv_sql_stmt :=
27699    'UPDATE    msc_st_department_resources '
27700    ||' SET    process_flag                     ='|| G_ERROR_FLG||','
27701    ||'        error_text                       ='||''''||lv_message_text||''''
27702    ||' WHERE  (NVL(max_rate,'||NULL_VALUE||') ='||NULL_VALUE
27703    ||' OR     NVL(min_rate,'||NULL_VALUE||')   ='|| NULL_VALUE
27704    ||' OR     NVL(start_time,'||NULL_VALUE||') ='|| NULL_VALUE
27705    ||' OR     NVL(stop_time,'||NULL_VALUE||')  ='|| NULL_VALUE||')'
27706    ||' AND    sr_instance_code                 = :v_instance_code'
27707    ||' AND    line_flag                        = ''1'' '
27708    ||' AND    process_flag                     ='|| G_IN_PROCESS
27709    ||' AND    batch_id                         = :lv_batch_id' ;
27710 
27711 
27712             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
27713 
27714     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
27715 
27716     -- Set the error message
27717     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27718                      (p_app_short_name    => 'MSC',
27719                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
27720                       p_message_text      => lv_message_text,
27721                       p_error_text        => lv_error_text,
27722                       p_token1            => 'COLUMN_NAME',
27723                       p_token_value1      => 'DELETE_FLAG',
27724                       p_token2            => 'DEFAULT_VALUE',
27725                       p_token_value2      =>  SYS_NO  );
27726 
27727       IF lv_return <> 0 THEN
27728         RAISE ex_logging_err;
27729       END IF;
27730 
27731       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
27732       -- Log error procedure inserts warning and also defaults the delete flag
27733 
27734      lv_where_str :=
27735      ' AND NVL(deleted_flag,'||NULL_VALUE||') <>'||SYS_NO;
27736 
27737      lv_return := MSC_ST_UTIL.LOG_ERROR
27738                     (p_table_name        => 'MSC_ST_DEPARTMENT_RESOURCES',
27739                      p_instance_code     => v_instance_code,
27740                      p_row               => lv_column_names,
27741                      p_severity          => G_SEV_WARNING,
27742                      p_message_text      => lv_message_text,
27743                      p_error_text        => lv_error_text,
27744                      p_batch_id          => lv_batch_id,
27745                      p_where_str         => lv_where_str,
27746                      p_col_name          => 'DELETED_FLAG',
27747                      p_debug             => v_debug,
27748                      p_default_value     => SYS_NO);
27749 
27750 
27751       IF lv_return <> 0 THEN
27752           RAISE ex_logging_err;
27753       END IF;
27754 
27755       -- Set the error message
27756 
27757      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27758                      (p_app_short_name    => 'MSC',
27759                       p_error_code        => 'MSC_PP_INVALID_VALUE',
27760                       p_message_text      => lv_message_text,
27761                       p_error_text        => lv_error_text,
27762                       p_token1            => 'COLUMN_NAME',
27763                       p_token_value1      => 'ORGANIZATION_CODE');
27764 
27765       IF lv_return <> 0 THEN
27766         RAISE ex_logging_err;
27767       END IF;
27768 
27769       -- Derive  organization id
27770      lv_return :=
27771      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_DEPARTMENT_RESOURCES',
27772                                     p_org_partner_name  => 'ORGANIZATION_CODE',
27773                                     p_org_partner_id    => 'ORGANIZATION_ID',
27774                                     p_instance_code     => v_instance_code,
27775                                     p_partner_type      => G_ORGANIZATION,
27776                                     p_severity          => G_SEV_ERROR,
27777                                     p_message_text      => lv_message_text,
27778                                     p_batch_id          => lv_batch_id,
27779                                     p_debug             => v_debug,
27780                                     p_error_text        => lv_error_text);
27781 
27782      IF(lv_return <> 0) then
27783         RAISE ex_logging_err;
27784      END IF;
27785 
27786      -- Set the error message
27787     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27788                      (p_app_short_name    => 'MSC',
27789                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
27790                       p_message_text      => lv_message_text,
27791                       p_error_text        => lv_error_text,
27792                       p_token1            => 'COLUMN_NAME',
27793                       p_token_value1      => 'LINE_FLAG',
27794                       p_token2            => 'DEFAULT_VALUE',
27795                       p_token_value2      =>  G_LINE_FLAG  );
27796 
27797       IF lv_return <> 0 THEN
27798         RAISE ex_logging_err;
27799       END IF;
27800 
27801      -- Default line_flag  as 2 (Dept is Not a line) if not in (1,2)
27802 
27803      lv_where_str :=
27804      ' AND NVL(line_flag,'||NULL_VALUE||')  NOT IN (''1'',''2'')';
27805 
27806      lv_return := MSC_ST_UTIL.LOG_ERROR
27807                     (p_table_name        => 'MSC_ST_DEPARTMENT_RESOURCES',
27808                      p_instance_code     => v_instance_code,
27809                      p_row               => lv_column_names,
27810                      p_severity          => G_SEV_WARNING,
27811                      p_message_text      => lv_message_text,
27812                      p_error_text        => lv_error_text,
27813                      p_batch_id          => lv_batch_id,
27814                      p_where_str         => lv_where_str,
27815                      p_col_name          => 'LINE_FLAG',
27816                      p_debug             => v_debug,
27817                      p_default_value     => G_LINE_FLAG );
27818 
27819      IF(lv_return <> 0) then
27820         RAISE ex_logging_err;
27821      END IF;
27822 
27823      -- Set the error message
27824     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27825                      (p_app_short_name    => 'MSC',
27826                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
27827                       p_message_text      => lv_message_text,
27828                       p_error_text        => lv_error_text,
27829                       p_token1            => 'COLUMN_NAME',
27830                       p_token_value1      => 'AVAILABLE_24_HOURS_FLAG',
27831                       p_token2            => 'DEFAULT_VALUE',
27832                       p_token_value2      =>  G_AVAILABLE_24_HOURS_FLAG  );
27833 
27834       IF lv_return <> 0 THEN
27835         RAISE ex_logging_err;
27836       END IF;
27837 
27838 
27839       -- Default available 24 hr as Yes (1) for dept/line
27840 
27841      lv_where_str :=
27842      ' AND NVL(available_24_hours_flag,'||NULL_VALUE||') NOT IN (1,2)';
27843 
27844      lv_return := MSC_ST_UTIL.LOG_ERROR
27845                     (p_table_name        => 'MSC_ST_DEPARTMENT_RESOURCES',
27846                      p_instance_code     => v_instance_code,
27847                      p_row               => lv_column_names,
27848                      p_severity          => G_SEV_WARNING,
27849                      p_message_text      => lv_message_text,
27850                      p_error_text        => lv_error_text,
27851                      p_batch_id          => lv_batch_id,
27852                      p_where_str         => lv_where_str,
27853                      p_col_name          => 'AVAILABLE_24_HOURS_FLAG',
27854                      p_debug             => v_debug,
27855                      p_default_value     =>  G_AVAILABLE_24_HOURS_FLAG);
27856 
27857 
27858      IF(lv_return <> 0) then
27859         RAISE ex_logging_err;
27860      END IF;
27861 
27862      -- Set the error message
27863     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27864                      (p_app_short_name    => 'MSC',
27865                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
27866                       p_message_text      => lv_message_text,
27867                       p_error_text        => lv_error_text,
27868                       p_token1            => 'COLUMN_NAME',
27869                       p_token_value1      => 'CTP_FLAG ',
27870                       p_token2            => 'DEFAULT_VALUE',
27871                       p_token_value2      => G_CTP_FLAG   );
27872 
27873       IF lv_return <> 0 THEN
27874         RAISE ex_logging_err;
27875       END IF;
27876 
27877      -- Default CTP flag as 2 if NOT IN (1,2) for dept only
27878 
27879      lv_where_str :=
27880      ' AND NVL(ctp_flag,'||NULL_VALUE||') NOT IN (1,2) AND line_flag = ''2'' ';
27881 
27882      lv_return := MSC_ST_UTIL.LOG_ERROR
27883                     (p_table_name        => 'MSC_ST_DEPARTMENT_RESOURCES',
27884                      p_instance_code     => v_instance_code,
27885                      p_row               => lv_column_names,
27886                      p_severity          => G_SEV_WARNING,
27887                      p_message_text      => lv_message_text,
27888                      p_error_text        => lv_error_text,
27889                      p_batch_id          => lv_batch_id,
27890                      p_where_str         => lv_where_str,
27891                      p_col_name          => 'CTP_FLAG',
27892                      p_debug             => v_debug,
27893                      p_default_value     => G_CTP_FLAG );
27894 
27895      IF(lv_return <> 0) then
27896         RAISE ex_logging_err;
27897      END IF;
27898 
27899      -- Set the error message
27900     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27901                      (p_app_short_name    => 'MSC',
27902                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
27903                       p_message_text      => lv_message_text,
27904                       p_error_text        => lv_error_text,
27905                       p_token1            => 'COLUMN_NAME',
27906                       p_token_value1      => 'RESOURCE_TYPE ',
27907                       p_token2            => 'DEFAULT_VALUE',
27908                       p_token_value2      => G_RESOURCE_TYPE   );
27909 
27910       IF lv_return <> 0 THEN
27911         RAISE ex_logging_err;
27912       END IF;
27913 
27914      -- Default resource_type as 1 (Machine) if not in (1,2,3,4,5)
27915 
27916      lv_where_str :=
27917      '      AND NVL(resource_type,'||NULL_VALUE||')'
27918      ||'    NOT IN (1,2,3,4,5) AND line_flag = ''2'' ';
27919 
27920      lv_return := MSC_ST_UTIL.LOG_ERROR
27921                     (p_table_name        => 'MSC_ST_DEPARTMENT_RESOURCES',
27922                      p_instance_code     => v_instance_code,
27923                      p_row               => lv_column_names,
27924                      p_severity          => G_SEV_WARNING,
27925                      p_message_text      => lv_message_text,
27926                      p_error_text        => lv_error_text,
27927                      p_batch_id          => lv_batch_id,
27928                      p_where_str         => lv_where_str,
27929                      p_col_name          => 'RESOURCE_TYPE',
27930                      p_debug             => v_debug,
27931                      p_default_value     => G_RESOURCE_TYPE );
27932 
27933 
27934      IF(lv_return <> 0) then
27935         RAISE ex_logging_err;
27936      END IF;
27937 
27938 /* commented for Bug 2970460
27939      -- Set the error message
27940     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27941                      (p_app_short_name    => 'MSC',
27942                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
27943                       p_message_text      => lv_message_text,
27944                       p_error_text        => lv_error_text,
27945                       p_token1            => 'COLUMN_NAME',
27946                       p_token_value1      => 'RESOURCE_INCLUDE_FLAG',
27947                       p_token2            => 'DEFAULT_VALUE',
27948                       p_token_value2      => G_RESOURCE_INCLUDE_FLAG );
27949 
27950       IF lv_return <> 0 THEN
27951         RAISE ex_logging_err;
27952       END IF;
27953 
27954       -- Default resource_include_flag   as 1  if not in (1,2)
27955 
27956      lv_where_str :=
27957      '    AND NVL(resource_include_flag,'||NULL_VALUE||')'
27958      ||'  NOT IN (1,2) AND line_flag = 2';
27959 
27960      lv_return := MSC_ST_UTIL.LOG_ERROR
27961                     (p_table_name        => 'MSC_ST_DEPARTMENT_RESOURCES',
27962                      p_instance_code     => v_instance_code,
27963                      p_row               => lv_column_names,
27964                      p_severity          => G_SEV_WARNING,
27965                      p_message_text      => lv_message_text,
27966                      p_error_text        => lv_error_text,
27967                      p_batch_id          => lv_batch_id,
27968                      p_where_str         => lv_where_str,
27969                      p_col_name          => 'RESOURCE_INCLUDE_FLAG',
27970                      p_debug             => v_debug,
27971                      p_default_value     => G_RESOURCE_INCLUDE_FLAG);
27972 
27973      IF(lv_return <> 0) then
27974         RAISE ex_logging_err;
27975      END IF;
27976 */
27977 
27978     -- Set the error message
27979     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
27980                      (p_app_short_name    => 'MSC',
27981                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
27982                       p_message_text      => lv_message_text,
27983                       p_error_text        => lv_error_text,
27984                       p_token1            => 'COLUMN_NAME',
27985                       p_token_value1      => 'AGGREGATED_RESOURCE_FLAG',
27986                       p_token2            => 'DEFAULT_VALUE',
27987                       p_token_value2      => G_AGGREGATED_RESOURCE_FLAG);
27988 
27989       IF lv_return <> 0 THEN
27990         RAISE ex_logging_err;
27991       END IF;
27992 
27993      -- Default aggregated_resource_flag  as
27994      -- 2(Resource is not aggregated one) if not in (1,2)
27995 
27996      lv_where_str :=
27997      '     AND NVL(aggregated_resource_flag,'||NULL_VALUE||')'
27998      ||'   NOT IN (1,2) AND LINE_FLAG = 2';
27999 
28000      lv_return := MSC_ST_UTIL.LOG_ERROR
28001                     (p_table_name        => 'MSC_ST_DEPARTMENT_RESOURCES',
28002                      p_instance_code     => v_instance_code,
28003                      p_row               => lv_column_names,
28004                      p_severity          => G_SEV_WARNING,
28005                      p_message_text      => lv_message_text,
28006                      p_error_text        => lv_error_text,
28007                      p_batch_id          => lv_batch_id,
28008                      p_where_str         => lv_where_str,
28009                      p_col_name          => 'AGGREGATED_RESOURCE_FLAG',
28010                      p_debug             => v_debug,
28011                      p_default_value     => G_AGGREGATED_RESOURCE_FLAG );
28012 
28013      IF(lv_return <> 0) then
28014         RAISE ex_logging_err;
28015      END IF;
28016 
28017 
28018 
28019     -- Now derive department id
28020      v_sql_stmt:= 10;
28021 
28022     lv_sql_stmt :=
28023     'UPDATE     msc_st_department_resources msdr'
28024     ||' SET     department_id=  (SELECT local_id'
28025     ||'                        FROM msc_local_id_setup mlis'
28026     ||'                        WHERE  mlis.char1 = msdr.sr_instance_code'
28027     ||'                        AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28028     ||'                        =    NVL(msdr.company_name,'||''''||NULL_CHAR||''''||') '
28029     ||'                        AND    mlis.char3 = msdr.organization_code'
28030     ||'                        AND    mlis.char4 = msdr.department_code'
28031     ||'                        AND    mlis.entity_name = ''DEPARTMENT_ID'')'
28032     ||' WHERE      msdr.sr_instance_code = :v_instance_code'
28033     ||' AND        msdr.line_flag        = ''2'' '
28034     ||' AND        msdr.process_flag     ='|| G_IN_PROCESS
28035     ||' AND        msdr.batch_id         = :lv_batch_id';
28036 
28037 
28038             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28039 
28040     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28041 
28042 
28043     -- Now derive line id
28044      v_sql_stmt:= 10;
28045 
28046     lv_sql_stmt :=
28047     'UPDATE     msc_st_department_resources msdr'
28048     ||' SET     department_id=    (SELECT local_id '
28049     ||'                        FROM msc_local_id_setup mlis'
28050     ||'                        WHERE  mlis.char1 = msdr.sr_instance_code'
28051     ||'                        AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28052     ||'                        =    NVL(msdr.company_name,'||''''||NULL_CHAR||''''||') '
28053     ||'                        AND    mlis.char3 = msdr.organization_code'
28054     ||'                        AND    mlis.char4 = msdr.department_code'
28055     ||'                        AND    mlis.entity_name = ''LINE_ID'')'
28056     ||' WHERE      msdr.sr_instance_code = :v_instance_code'
28057     ||' AND        msdr.line_flag        = ''1'' '
28058     ||' AND        msdr.process_flag     ='|| G_IN_PROCESS
28059     ||' AND        msdr.batch_id         = :lv_batch_id';
28060 
28061 
28062             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28063 
28064     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28065 
28066 
28067     -- Derive resource id
28068 
28069      v_sql_stmt:= 11;
28070 
28071     lv_sql_stmt :=
28072     'UPDATE     msc_st_department_resources msdr'
28073     ||' SET        resource_id=    (SELECT local_id'
28074     ||'                        FROM msc_local_id_setup mlis'
28075     ||'                        WHERE  mlis.char1 = msdr.sr_instance_code'
28076     ||'                        AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28077     ||'                        =    NVL(msdr.company_name,'||''''||NULL_CHAR||''''||') '
28078     ||'                        AND    mlis.char3 = msdr.organization_code'
28079     ||'                        AND    mlis.char4 = msdr.resource_code'
28080     ||'                        AND    mlis.entity_name = ''RESOURCE_ID'')'
28081     ||' WHERE      msdr.sr_instance_code = :v_instance_code'
28082     ||' AND        msdr.process_flag     ='|| G_IN_PROCESS
28083     ||' AND        msdr.batch_id         = :lv_batch_id';
28084 
28085 
28086             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28087 
28088     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28089 
28090  -- Set resoure_id=-1 for line resources
28091 
28092  lv_sql_stmt :=
28093     'UPDATE     msc_st_department_resources msdr'
28094     ||' SET        resource_id=  -1 '
28095     ||' WHERE      msdr.sr_instance_code = :v_instance_code'
28096     ||' AND        msdr.process_flag     ='|| G_IN_PROCESS
28097     ||' AND        NVL(msdr.resource_code, '||''''||NULL_CHAR||''''||') '
28098     ||'            = '||''''||NULL_CHAR||''''
28099     ||' AND        line_flag             = ''1'' '
28100     ||' AND        msdr.batch_id         = :lv_batch_id';
28101 
28102 
28103             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28104 
28105     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28106 
28107     --Call to customised validation.
28108 
28109       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
28110         (ERRBUF         => lv_error_text,
28111          RETCODE        => lv_return,
28112          pBatchID       => lv_batch_id,
28113          pInstanceCode  => v_instance_code,
28114          pEntityName    => 'MSC_ST_DEPARTMENT_RESOURCES',
28115          pInstanceID    => v_instance_id);
28116 
28117       IF NVL(lv_return,0) <> 0 THEN
28118         RAISE ex_logging_err;
28119       END IF;
28120 
28121     -- Now generate the department_id
28122 
28123      v_sql_stmt := 13;
28124 
28125      OPEN c3(lv_batch_id);
28126      FETCH c3 BULK COLLECT INTO lb_rowid ;
28127 
28128      IF c3%ROWCOUNT > 0  THEN
28129 
28130     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
28131 
28132         UPDATE msc_st_department_resources
28133         SET    department_id          = msc_st_sr_inventory_item_id_s.NEXTVAL
28134         WHERE  rowid                  = lb_rowid(j);
28135 
28136 
28137     v_sql_stmt := 14;
28138 
28139     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
28140 
28141    -- Insert into the LID table this new department with the  user defined key
28142 
28143        INSERT INTO  msc_local_id_setup
28144        (local_id,
28145        st_transaction_id,
28146        instance_id,
28147        entity_name,
28148        data_source_type,
28149        char1,
28150        char2,
28151        char3,
28152        char4,
28153        SOURCE_ORGANIZATION_ID,
28154        SOURCE_DEPARTMENT_ID,
28155        last_update_date,
28156        last_updated_by,
28157        creation_date,
28158        created_by  )
28159       SELECT
28160        department_id,
28161        st_transaction_id,
28162        v_instance_id,
28163        'DEPARTMENT_ID',
28164        data_source_type,
28165        sr_instance_code,
28166        company_name,
28167        organization_code,
28168        department_code,
28169        SOURCE_ORGANIZATION_ID,
28170        SOURCE_DEPARTMENT_ID,
28171        v_current_date,
28172        v_current_user,
28173        v_current_date,
28174        v_current_user
28175       FROM msc_st_department_resources
28176       WHERE rowid = lb_rowid(j);
28177 
28178    END IF;
28179    CLOSE c3;
28180 
28181     -- Now derive department id
28182      v_sql_stmt:= 15;
28183 
28184     lv_sql_stmt :=
28185     'UPDATE     msc_st_department_resources msdr'
28186     ||' SET     department_id=  (SELECT local_id'
28187     ||'                        FROM msc_local_id_setup mlis'
28188     ||'                        WHERE  mlis.char1 = msdr.sr_instance_code'
28189     ||'                        AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28190     ||'                        =    NVL(msdr.company_name,'||''''||NULL_CHAR||''''||') '
28191     ||'                        AND    mlis.char3 = msdr.organization_code'
28192     ||'                        AND    mlis.char4 = msdr.department_code'
28193     ||'                        AND    mlis.entity_name = ''DEPARTMENT_ID'')'
28194     ||' WHERE      msdr.sr_instance_code = :v_instance_code'
28195     ||' AND        NVL(msdr.department_id,'||NULL_VALUE||') ='|| NULL_VALUE
28196     ||' AND        msdr.line_flag        = ''2'' '
28197     ||' AND        msdr.process_flag     ='|| G_IN_PROCESS
28198     ||' AND        msdr.batch_id         = :lv_batch_id';
28199 
28200 
28201             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28202 
28203     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28204 
28205   -- Update owning_department_code as department_code if that is NULL
28206    v_sql_stmt := 16;
28207    lv_sql_stmt  :=
28208    'UPDATE     msc_st_department_resources msdr'
28209     ||' SET    owning_department_code = department_code'
28210     ||' WHERE  NVL(owning_department_code,'||''''||NULL_CHAR||''''||') '
28211     ||'       = '||''''||NULL_CHAR||''''
28212     ||' AND    sr_instance_code = :v_instance_code'
28213     ||' AND    line_flag        = ''2'' '
28214     ||' AND    process_flag     ='|| G_IN_PROCESS
28215     ||' AND    batch_id         = :lv_batch_id';
28216 
28217 
28218             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28219 
28220     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28221 
28222     -- Now derive owning_department id
28223      v_sql_stmt := 17;
28224 
28225     lv_sql_stmt :=
28226     'UPDATE     msc_st_department_resources msdr'
28227     ||' SET     owning_department_id=  (SELECT local_id'
28228     ||'                        FROM msc_local_id_setup mlis'
28229     ||'                        WHERE  mlis.char1 = msdr.sr_instance_code'
28230     ||'                        AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28231     ||'                        =    NVL(msdr.company_name,'||''''||NULL_CHAR||''''||') '
28232     ||'                        AND    mlis.char3 = msdr.organization_code'
28233     ||'                        AND    mlis.char4 = msdr.owning_department_code'
28234     ||'                        AND    mlis.entity_name = ''DEPARTMENT_ID'')'
28235     ||' WHERE      msdr.sr_instance_code = :v_instance_code'
28236     ||' AND        NVL(msdr.owning_department_code,'||''''||NULL_CHAR||''''||') '
28237     ||'            <> '||''''||NULL_CHAR||''''
28238     ||' AND        msdr.line_flag        = ''2'' '
28239     ||' AND        msdr.process_flag     ='|| G_IN_PROCESS
28240     ||' AND        msdr.batch_id         = :lv_batch_id';
28241 
28242 
28243             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28244 
28245     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28246 
28247    -- Error out if owning_department_id is NULL
28248   -- Set the error message
28249     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28250                      (p_app_short_name    => 'MSC',
28251                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
28252                       p_message_text      => lv_message_text,
28253                       p_error_text        => lv_error_text,
28254                       p_token1            => 'COLUMN_NAMES',
28255                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
28256                                              ||'ORGANIZATION_CODE,OWNING_DEPARTMENT_CODE',
28257                       p_token2            => 'MASTER_TABLE',
28258                       p_token_value2      => 'MSC_ST_DEPARTMENT_RESOURCES',
28259                       p_token3            =>  'CHILD_TABLE' ,
28260                       p_token_value3      =>  'MSC_ST_DEPARTMENT_RESOURCES' );
28261 
28262       IF lv_return <> 0 THEN
28263         RAISE ex_logging_err;
28264       END IF;
28265       v_sql_stmt := 18;
28266 
28267       lv_sql_stmt :=
28268       ' UPDATE     msc_st_department_resources '
28269       ||' SET      process_flag     ='||G_ERROR_FLG||','
28270       ||'          error_text   = '||''''||lv_message_text||''''
28271       ||' WHERE    NVL(owning_department_id,'||NULL_VALUE||') ='|| NULL_VALUE
28272       ||' AND      deleted_flag     = '||SYS_NO
28273       ||' AND      line_flag        = ''2'' '
28274       ||' AND      process_flag     = '||G_IN_PROCESS
28275       ||' AND      sr_instance_code = :v_instance_code'
28276       ||' AND      batch_id         =  :lv_batch_id';
28277 
28278 
28279 
28280             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28281 
28282       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28283 
28284 
28285   -- Now  generate the resource_id
28286 
28287      v_sql_stmt := 19;
28288 
28289      OPEN c4(lv_batch_id);
28290      FETCH c4 BULK COLLECT INTO lb_rowid ;
28291 
28292      IF c4%ROWCOUNT > 0  THEN
28293 
28294     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
28295 
28296         UPDATE msc_st_department_resources
28297         SET    resource_id            = msc_st_sr_inventory_item_id_s.NEXTVAL
28298         WHERE  rowid                  = lb_rowid(j);
28299 
28300 
28301     v_sql_stmt := 20;
28302 
28303     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
28304 
28305    -- Insert into the LID table this new resource with the  user defined key
28306 
28307        INSERT INTO  msc_local_id_setup
28308        (local_id,
28309        st_transaction_id,
28310        instance_id,
28311        entity_name,
28312        data_source_type,
28313        char1,
28314        char2,
28315        char3,
28316        char4,
28317        SOURCE_ORGANIZATION_ID,
28318        SOURCE_RESOURCE_ID,
28319        SOURCE_OWNING_DEPARTMENT_ID,
28320        last_update_date,
28321        last_updated_by,
28322        creation_date,
28323        created_by  )
28324       SELECT
28325        resource_id,
28326        st_transaction_id,
28327        v_instance_id,
28328        'RESOURCE_ID',
28329        data_source_type,
28330        sr_instance_code,
28331        company_name,
28332        organization_code,
28333        resource_code,
28334        SOURCE_ORGANIZATION_ID,
28335        SOURCE_RESOURCE_ID,
28336        SOURCE_OWNING_DEPARTMENT_ID,
28337        v_current_date,
28338        v_current_user,
28339        v_current_date,
28340        v_current_user
28341       FROM msc_st_department_resources
28342       WHERE rowid = lb_rowid(j);
28343 
28344    END IF;
28345    CLOSE c4;
28346 
28347     -- Derive resource id
28348 
28349      v_sql_stmt:= 21;
28350 
28351     lv_sql_stmt :=
28352     'UPDATE     msc_st_department_resources msdr'
28353     ||' SET        resource_id=    (SELECT local_id'
28354     ||'                        FROM msc_local_id_setup mlis'
28355     ||'                        WHERE  mlis.char1 = msdr.sr_instance_code'
28356     ||'                        AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28357     ||'                        =    NVL(msdr.company_name,'||''''||NULL_CHAR||''''||') '
28358     ||'                        AND    mlis.char3 = msdr.organization_code'
28359     ||'                        AND    mlis.char4 = msdr.resource_code'
28360     ||'                        AND    mlis.entity_name = ''RESOURCE_ID'')'
28361     ||' WHERE      msdr.sr_instance_code = :v_instance_code'
28362     ||' AND        NVL(msdr.resource_id,'||NULL_VALUE||') ='|| NULL_VALUE
28363     ||' AND        msdr.process_flag     ='|| G_IN_PROCESS
28364     ||' AND        msdr.batch_id         = :lv_batch_id';
28365 
28366 
28367             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28368 
28369     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28370 
28371 
28372      v_sql_stmt := 22;
28373 
28374      OPEN c5(lv_batch_id);
28375      FETCH c5 BULK COLLECT INTO lb_rowid ;
28376 
28377      IF c5%ROWCOUNT > 0  THEN
28378 
28379     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
28380 
28381         UPDATE msc_st_department_resources
28382         SET    department_id          = msc_st_sr_inventory_item_id_s.NEXTVAL
28383         WHERE  rowid                  = lb_rowid(j);
28384 
28385 
28386     v_sql_stmt := 23;
28387 
28388     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
28389 
28390    -- Insert into the LID table this new department with the  user defined key
28391 
28392        INSERT INTO  msc_local_id_setup
28393        (local_id,
28394        st_transaction_id,
28395        instance_id,
28396        entity_name,
28397        data_source_type,
28398        char1,
28399        char2,
28400        char3,
28401        char4,
28402        SOURCE_ORGANIZATION_ID,
28403        SOURCE_DEPARTMENT_ID,
28404        last_update_date,
28405        last_updated_by,
28406        creation_date,
28407        created_by  )
28408       SELECT
28409        department_id,
28410        st_transaction_id,
28411        v_instance_id,
28412        'LINE_ID',
28413        data_source_type,
28414        sr_instance_code,
28415        company_name,
28416        organization_code,
28417        department_code,
28418        SOURCE_ORGANIZATION_ID,
28419        SOURCE_DEPARTMENT_ID,
28420        v_current_date,
28421        v_current_user,
28422        v_current_date,
28423        v_current_user
28424       FROM msc_st_department_resources
28425       WHERE rowid = lb_rowid(j);
28426 
28427    END IF;
28428    CLOSE c5;
28429 
28430    -- Set the process flag as Valid and poulate instance_id
28431     lv_return :=
28432     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_DEPARTMENT_RESOURCES',
28433                                p_instance_id    => v_instance_id,
28434                                p_instance_code  => v_instance_code,
28435                                p_process_flag   => G_VALID,
28436                                p_error_text     => lv_error_text,
28437                                p_debug          => v_debug,
28438                                p_batch_id       => lv_batch_id);
28439 
28440       IF lv_return <> 0 THEN
28441         RAISE ex_logging_err;
28442       END IF;
28443 
28444     -- At the end calling  the LOG_ERROR for logging all errored out records
28445 
28446    lv_return :=
28447    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_DEPARTMENT_RESOURCES',
28448                          p_instance_code => v_instance_code,
28449                          p_row           => lv_column_names,
28450                          p_severity      => G_SEV_ERROR,
28451                          p_message_text  => NULL,
28452                          p_error_text    => lv_error_text,
28453                          p_debug         => v_debug,
28454                          p_batch_id      => lv_batch_id  );
28455 
28456     IF lv_return <> 0 THEN
28457         RAISE ex_logging_err;
28458    END IF;
28459 
28460    COMMIT;
28461   END LOOP;
28462 
28463 
28464    -- Validation for table MSC_ST_RESOURCE_SHIFTS
28465 
28466    --Duplicate records check for the records whose source is XML
28467 
28468     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28469                  (p_app_short_name    => 'MSC',
28470                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
28471                   p_message_text      => lv_message_text,
28472                   p_error_text        => lv_error_text);
28473 
28474     IF lv_return <> 0 THEN
28475       RAISE ex_logging_err;
28476     END IF;
28477 
28478   -- UDK validations
28479 
28480   v_sql_stmt := 19;
28481 
28482   lv_sql_stmt:=
28483   'UPDATE   msc_st_resource_shifts msrs1'
28484   ||' SET     process_flag = '||G_ERROR_FLG||','
28485   ||'         error_text   = '||''''||lv_message_text||''''
28486   ||' WHERE     message_id < ( SELECT max(message_id)'
28487   ||'                 FROM msc_st_resource_shifts msrs2'
28488   ||'                 WHERE msrs2.sr_instance_code  = msrs1.sr_instance_code'
28489   ||'                 AND   NVL(msrs2.company_name,'||''''||NULL_CHAR||''''||') '
28490   ||'                 =  NVL(msrs1.company_name,'||''''||NULL_CHAR||''''||') '
28491   ||'                 AND   msrs2.organization_code = msrs1.organization_code'
28492   ||'                 AND   msrs2.department_code   = msrs1.department_code'
28493   ||'                 AND   msrs2.resource_code     = msrs1.resource_code'
28494   ||'                 AND   msrs2.shift_name        = msrs1.shift_name'
28495   ||'                 AND   msrs2.process_flag      = '||G_IN_PROCESS
28496   ||'                 AND   NVL(msrs2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
28497   ||' AND       msrs1.process_flag             ='|| G_IN_PROCESS
28498   ||' AND       msrs1.sr_instance_code         = :v_instance_code'
28499   ||' AND     NVL(msrs1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
28500 
28501 
28502          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28503 
28504   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
28505 
28506 
28507   --Duplicate records check for the records whose source is batch load
28508 
28509     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28510                  (p_app_short_name    => 'MSC',
28511                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
28512                   p_message_text      => lv_message_text,
28513                   p_error_text        => lv_error_text);
28514 
28515     IF lv_return <> 0 THEN
28516       RAISE ex_logging_err;
28517     END IF;
28518 
28519   v_sql_stmt := 20;
28520 
28521   lv_sql_stmt:=
28522   'UPDATE   msc_st_resource_shifts msrs1'
28523   ||' SET      process_flag ='||G_ERROR_FLG||','
28524   ||'         error_text    = '||''''||lv_message_text||''''
28525   ||' WHERE    EXISTS ( SELECT 1 FROM msc_st_resource_shifts msrs2'
28526   ||'               WHERE msrs2.sr_instance_code          = msrs1.sr_instance_code'
28527   ||'               AND   NVL(msrs2.company_name,'||''''||NULL_CHAR||''''||') '
28528   ||'                 =    NVL(msrs1.company_name,'||''''||NULL_CHAR||''''||') '
28529   ||'               AND   msrs2.organization_code         = msrs1.organization_code'
28530   ||'               AND   msrs2.department_code           = msrs1.department_code'
28531   ||'               AND   msrs2.resource_code             = msrs1.resource_code'
28532   ||'               AND   msrs2.shift_name                = msrs1.shift_name'
28533   ||'               AND   msrs2.process_flag              ='||G_IN_PROCESS
28534   ||'               AND NVL(msrs2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
28535   ||'               GROUP BY sr_instance_code,organization_code,company_name,'
28536   ||'                      department_code,resource_code,shift_name '
28537   ||'                  HAVING COUNT(*) >1 )'
28538   ||' AND     msrs1.process_flag              ='|| G_IN_PROCESS
28539   ||' AND     msrs1.sr_instance_code          = :v_instance_code'
28540   ||' AND     NVL(msrs1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
28541 
28542 
28543          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28544 
28545   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
28546 
28547 
28548  lv_column_names :=
28549  'DEPARTMENT_CODE      ||''~''||'
28550  ||'RESOURCE_CODE      ||''~''||'
28551  ||'SHIFT_NAME         ||''~''||'
28552  ||'ORGANIZATION_CODE  ||''~''||'
28553  ||'SR_INSTANCE_CODE   ||''~''||'
28554  ||'DELETED_FLAG       ||''~''||'
28555  ||'COMPANY_NAME ';
28556 
28557    LOOP
28558       v_sql_stmt := 21;
28559       lv_cursor_stmt :=
28560       ' SELECT msc_st_batch_id_s.NEXTVAL '
28561       ||' FROM   dual';
28562 
28563       OPEN  c1 FOR lv_cursor_stmt;
28564       FETCH c1 INTO lv_batch_id;
28565       CLOSE c1;
28566 
28567       v_sql_stmt := 22;
28568       lv_sql_stmt :=
28569       ' UPDATE msc_st_resource_shifts '
28570       ||' SET    batch_id  = :lv_batch_id'
28571       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
28572       ||' AND    sr_instance_code               = :v_instance_code'
28573       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
28574       ||' AND    rownum                        <= '||v_batch_size;
28575 
28576 
28577          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28578 
28579       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
28580 
28581       EXIT WHEN SQL%NOTFOUND;
28582 
28583       OPEN c6(lv_batch_id);
28584       FETCH c6 BULK COLLECT INTO lb_rowid;
28585       CLOSE c6;
28586 
28587       v_sql_stmt := 23;
28588       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
28589       UPDATE msc_st_resource_shifts
28590       SET  st_transaction_id = msc_st_resource_shifts_s.NEXTVAL,
28591            refresh_id        = v_refresh_id,
28592            last_update_date  = v_current_date,
28593            last_updated_by   = v_current_user,
28594            creation_date     = v_current_date,
28595            created_by        = v_current_user
28596       WHERE  rowid           = lb_rowid(j);
28597 
28598 
28599 
28600       -- Set the error message
28601 
28602       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28603                      (p_app_short_name    => 'MSC',
28604                       p_error_code        => 'MSC_PP_INVALID_VALUE',
28605                       p_message_text      => lv_message_text,
28606                       p_error_text        => lv_error_text,
28607                       p_token1            => 'COLUMN_NAME',
28608                       p_token_value1      => 'COMPANY_NAME');
28609 
28610       IF lv_return <> 0 THEN
28611         RAISE ex_logging_err;
28612       END IF;
28613 
28614 
28615      -- Deletion is not allowed for this table
28616 
28617      v_sql_stmt := 24;
28618 
28619       lv_sql_stmt :=
28620       ' UPDATE     msc_st_resource_shifts '
28621       ||' SET      process_flag     ='||G_ERROR_FLG||','
28622       ||'          error_text        = '||''''||lv_message_text||''''
28623       ||' WHERE    deleted_flag     = '||SYS_YES
28624       ||' AND      process_flag     = '||G_IN_PROCESS
28625       ||' AND      batch_id         =  :lv_batch_id'
28626       ||' AND      sr_instance_code = :v_instance_code';
28627 
28628 
28629             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28630 
28631       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
28632 
28633 
28634     -- Set the error message
28635     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28636                      (p_app_short_name    => 'MSC',
28637                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
28638                       p_message_text      => lv_message_text,
28639                       p_error_text        => lv_error_text,
28640                       p_token1            => 'COLUMN_NAME',
28641                       p_token_value1      => 'DELETED_FLAG',
28642                       p_token2            => 'DEFAULT_VALUE',
28643                       p_token_value2      =>  SYS_NO  );
28644 
28645       IF lv_return <> 0 THEN
28646         RAISE ex_logging_err;
28647       END IF;
28648 
28649       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
28650       -- Log error procedure inserts warning and also defaults the delete flag
28651 
28652      lv_where_str :=
28653      ' AND NVL(deleted_flag,'||NULL_VALUE||') <>'||SYS_NO;
28654 
28655        lv_return := MSC_ST_UTIL.LOG_ERROR
28656                     (p_table_name        => 'MSC_ST_RESOURCE_SHIFTS',
28657                      p_instance_code     => v_instance_code,
28658                      p_row               => lv_column_names,
28659                      p_severity          => G_SEV_WARNING,
28660                      p_message_text      => lv_message_text,
28661                      p_error_text        => lv_error_text,
28662                      p_batch_id          => lv_batch_id,
28663                      p_where_str         => lv_where_str,
28664                      p_col_name          => 'DELETED_FLAG',
28665                      p_debug             => v_debug,
28666                      p_default_value     => SYS_NO);
28667 
28668 
28669       IF lv_return <> 0 THEN
28670           RAISE ex_logging_err;
28671       END IF;
28672 
28673      -- Set the error message
28674 
28675     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28676                      (p_app_short_name    => 'MSC',
28677                       p_error_code        => 'MSC_PP_NO_DELETION',
28678                       p_message_text      => lv_message_text,
28679                       p_error_text        => lv_error_text,
28680                       p_token1            => 'TABLE_NAME',
28681                       p_token_value1      => 'MSC_ST_RESOURCE_SHIFTS');
28682 
28683       IF lv_return <> 0 THEN
28684         RAISE ex_logging_err;
28685       END IF;
28686 
28687     -- Popluate the Department id from LOCAL_ID table
28688 
28689     v_sql_stmt := 25;
28690     lv_sql_stmt :=
28691     'UPDATE            msc_st_resource_shifts msrs'
28692     ||' SET            department_id = (SELECT local_id'
28693     ||'                     FROM msc_local_id_setup mlis'
28694     ||'                     WHERE  mlis.char1       = msrs.sr_instance_code'
28695     ||'                     AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28696     ||'                      =    NVL(msrs.company_name,'||''''||NULL_CHAR||''''||') '
28697     ||'                     AND    mlis.char3       = msrs.organization_code'
28698     ||'                     AND    mlis.char4       = msrs.department_code'
28699     ||'                     AND    mlis.entity_name = ''DEPARTMENT_ID'' '
28700     ||'                     AND    mlis.instance_id = '||v_instance_id||')'
28701     ||' WHERE      sr_instance_code = :v_instance_code'
28702     ||' AND        process_flag     = '|| G_IN_PROCESS
28703     ||' AND        batch_id         = :lv_batch_id';
28704 
28705 
28706             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28707 
28708       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28709 
28710     -- Set the error message
28711     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28712                      (p_app_short_name    => 'MSC',
28713                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
28714                       p_message_text      => lv_message_text,
28715                       p_error_text        => lv_error_text,
28716                       p_token1            => 'COLUMN_NAMES',
28717                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME'
28718                                              ||' ORGANIZATION_CODE,DEPARTMENT_CODE',
28719                       p_token2            => 'MASTER_TABLE',
28720                       p_token_value2      =>  'MSC_ST_DEPARTMENT_RESOURCES',
28721                       p_token3            =>  'CHILD_TABLE' ,
28722                       p_token_value3      =>  'MSC_ST_RESOURCE_SHIFTS' );
28723 
28724       IF lv_return <> 0 THEN
28725         RAISE ex_logging_err;
28726       END IF;
28727 
28728 
28729     -- Error out the reords where department id is NULL
28730 
28731     v_sql_stmt := 26;
28732     lv_sql_stmt:=
28733     'UPDATE   msc_st_resource_shifts '
28734     ||' SET     process_flag            = '||G_ERROR_FLG||','
28735     ||'         error_text   = '||''''||lv_message_text||''''
28736     ||' WHERE    NVL(department_id,'||NULL_VALUE||') = '||NULL_VALUE
28737     ||' AND      sr_instance_code              = :v_instance_code'
28738     ||' AND      process_flag                  ='|| G_IN_PROCESS
28739     ||' AND      batch_id                      = :lv_batch_id';
28740 
28741 
28742             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28743 
28744       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28745 
28746     -- Popluate the  resource id from LOCAL_ID table
28747     v_sql_stmt := 27;
28748     lv_sql_stmt:=
28749     'UPDATE         msc_st_resource_shifts msrs'
28750     ||' SET            resource_id = (SELECT local_id FROM msc_local_id_setup mlis'
28751     ||'                             WHERE  mlis.char1       = msrs.sr_instance_code'
28752     ||'                             AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28753     ||'                             =    NVL(msrs.company_name,'||''''||NULL_CHAR||''''||') '
28754     ||'                             AND    mlis.char3       = msrs.organization_code'
28755     ||'                             AND    mlis.char4       = msrs.resource_code'
28756     ||'                             AND    mlis.entity_name = ''RESOURCE_ID'' '
28757     ||'                             AND    mlis.instance_id = '||v_instance_id||')'
28758     ||' WHERE      sr_instance_code = :v_instance_code'
28759     ||' AND        process_flag     = '|| G_IN_PROCESS
28760     ||' AND        batch_id         = :lv_batch_id';
28761 
28762 
28763             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28764 
28765       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28766 
28767     -- Set the error message
28768     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28769                      (p_app_short_name    => 'MSC',
28770                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
28771                       p_message_text      => lv_message_text,
28772                       p_error_text        => lv_error_text,
28773                       p_token1            => 'COLUMN_NAMES',
28774                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
28775                                              ||' ORGANIZATION_CODE,RESOURCE_CODE',
28776                       p_token2            => 'MASTER_TABLE',
28777                       p_token_value2      =>  'MSC_ST_DEPARTMENT_RESOURCES',
28778                       p_token3            =>  'CHILD_TABLE' ,
28779                       p_token_value3      =>  'MSC_ST_RESOURCE_SHIFTS' );
28780 
28781       IF lv_return <> 0 THEN
28782         RAISE ex_logging_err;
28783       END IF;
28784 
28785     -- Error out the records where resource id is NULL
28786 
28787     v_sql_stmt := 28;
28788     lv_sql_stmt:=
28789     'UPDATE     msc_st_resource_shifts '
28790     ||' SET     process_flag            = '||G_ERROR_FLG||','
28791     ||'         error_text   = '||''''||lv_message_text||''''
28792     ||' WHERE   NVL(resource_id,'||NULL_VALUE||') = '||NULL_VALUE
28793     ||' AND     sr_instance_code              = :v_instance_code'
28794     ||' AND     process_flag                  ='|| G_IN_PROCESS
28795     ||' AND     batch_id                      = :lv_batch_id';
28796 
28797 
28798             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28799 
28800      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28801 
28802 
28803   /* Bug 2172537 ,instance code not prefixed in calendar and org*/
28804   -- Derive the SHIFT_NUM
28805 
28806    v_sql_stmt := 29;
28807    lv_sql_stmt :=
28808    'UPDATE    msc_st_resource_shifts msrs'
28809    ||' SET       shift_num =(SELECT    mlis.local_id'
28810    ||'                      FROM       msc_local_id_setup mlis, msc_st_trading_partners mtp '
28811    ||'                      WHERE      mtp.sr_instance_id      = '||v_instance_id
28812    ||'                      AND        mlis.char1              = msrs.sr_instance_code'
28813    ||'                             AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28814    ||'                             =    NVL(msrs.company_name,'||''''||NULL_CHAR||''''||') '
28815    ||'                      AND        mtp.calendar_code       = mlis.char3'
28816    ||'                      AND        mlis.char4              = msrs.shift_name'
28817    ||'                      AND        mlis.entity_name        = ''SHIFT_NUM'' '
28818    ||'                      AND        mlis.instance_id        = '||v_instance_id
28819    ||'                      AND        mtp.organization_code   = msrs.organization_code'
28820    ||'                      AND        mtp.process_flag    = '||G_VALID
28821    ||'                      AND        mtp.partner_type    = 3 )'
28822    ||' WHERE    msrs.sr_instance_code          = :v_instance_code'
28823    ||' AND      msrs.process_flag              ='|| G_IN_PROCESS
28824    ||' AND      msrs.batch_id                  = :lv_batch_id';
28825 
28826 
28827             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28828 
28829      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28830 
28831   /* Bug 2172537 ,instance code not prefixed in calendar and org*/
28832 
28833    lv_sql_stmt :=
28834    'UPDATE    msc_st_resource_shifts msrs'
28835    ||' SET       shift_num =(SELECT    mlis.local_id'
28836    ||'                      FROM       msc_local_id_setup mlis, msc_trading_partners mtp '
28837    ||'                      WHERE      mtp.sr_instance_id      = '||v_instance_id
28838    ||'                      AND        mlis.char1              = msrs.sr_instance_code'
28839    ||'                             AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
28840    ||'                             =    NVL(msrs.company_name,'||''''||NULL_CHAR||''''||') '
28841    ||'                      AND        mtp.calendar_code       = mlis.char3'
28842    ||'                      AND        mlis.char4              = msrs.shift_name'
28843    ||'                      AND        mlis.entity_name        = ''SHIFT_NUM'' '
28844    ||'                      AND        mlis.instance_id        = '||v_instance_id
28845    ||'                      AND        mtp.organization_code   = msrs.organization_code'
28846    ||'                      AND        mtp.partner_type    = 3 )'
28847    ||' WHERE    msrs.sr_instance_code          = :v_instance_code'
28848    ||' AND      NVL(msrs.shift_num,'||NULL_VALUE||') = '||NULL_VALUE
28849    ||' AND      msrs.process_flag              ='|| G_IN_PROCESS
28850    ||' AND      msrs.batch_id                  = :lv_batch_id';
28851 
28852 
28853             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28854 
28855 
28856      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28857 
28858 
28859     -- Set the error message
28860     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28861                      (p_app_short_name    => 'MSC',
28862                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
28863                       p_message_text      => lv_message_text,
28864                       p_error_text        => lv_error_text,
28865                       p_token1            => 'COLUMN_NAMES',
28866                       p_token_value1      => 'SR_INSTANCE_CODE,'
28867                                               ||' ORGANIZATION_CODE,SHIFT_NAME',
28868                       p_token2            => 'MASTER_TABLE',
28869                       p_token_value2      =>  'MSC_ST_CALENDAR_SHIFTS',
28870                       p_token3            =>  'CHILD_TABLE' ,
28871                       p_token_value3      =>  'MSC_ST_RESOURCE_SHIFTS' );
28872 
28873       IF lv_return <> 0 THEN
28874         RAISE ex_logging_err;
28875       END IF;
28876 
28877 
28878     -- Error out records where shift_num is NULL
28879 
28880     v_sql_stmt := 30;
28881 
28882     lv_sql_stmt:=
28883     'UPDATE   msc_st_resource_shifts '
28884     ||' SET     process_flag        = '||G_ERROR_FLG||','
28885     ||'         error_text          =  '||''''||lv_message_text||''''
28886     ||' WHERE    NVL(shift_num,'||NULL_VALUE||') = '||NULL_VALUE
28887     ||' AND      sr_instance_code              = :v_instance_code'
28888     ||' AND      process_flag                  ='|| G_IN_PROCESS
28889     ||' AND      batch_id                      = :lv_batch_id';
28890 
28891 
28892             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
28893 
28894   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
28895 
28896 
28897    --Call to customised validation.
28898 
28899       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
28900         (ERRBUF         => lv_error_text,
28901          RETCODE        => lv_return,
28902          pBatchID       => lv_batch_id,
28903          pInstanceCode  => v_instance_code,
28904          pEntityName    => 'MSC_ST_RESOURCE_SHIFTS',
28905          pInstanceID    => v_instance_id);
28906 
28907       IF NVL(lv_return,0) <> 0 THEN
28908         RAISE ex_logging_err;
28909       END IF;
28910 
28911 
28912     -- Set the process flag as Valid and populate instance_id
28913     lv_return :=
28914     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_RESOURCE_SHIFTS',
28915                                p_instance_id    => v_instance_id,
28916                                p_instance_code  => v_instance_code,
28917                                p_process_flag   => G_VALID,
28918                                p_error_text     => lv_error_text,
28919                                p_debug          => v_debug,
28920                                p_batch_id       => lv_batch_id);
28921 
28922       IF lv_return <> 0 THEN
28923           RAISE ex_logging_err;
28924       END IF;
28925 
28926     -- At the end calling  the LOG_ERROR for logging all errored out records
28927 
28928    lv_return :=
28929    MSC_ST_UTIL.LOG_ERROR(p_table_name    =>'MSC_ST_RESOURCE_SHIFTS',
28930                          p_instance_code =>v_instance_code,
28931                          p_row           =>lv_column_names,
28932                          p_severity      =>G_ERROR_FLG,
28933                          p_message_text  =>NULL,
28934                          p_error_text    =>lv_error_text,
28935                          p_debug         => v_debug,
28936                          p_batch_id      => lv_batch_id  );
28937 
28938       IF lv_return <> 0 THEN
28939           RAISE ex_logging_err;
28940       END IF;
28941 
28942    COMMIT;
28943   END LOOP;
28944 
28945 
28946   -- Validation for table MSC_ST_RESOURCE_CHANGES
28947 
28948   --Duplicate records check for the records whose source is XML
28949 
28950     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
28951                  (p_app_short_name    => 'MSC',
28952                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
28953                   p_message_text      => lv_message_text,
28954                   p_error_text        => lv_error_text);
28955 
28956     IF lv_return <> 0 THEN
28957       RAISE ex_logging_err;
28958     END IF;
28959 
28960   -- Duplicate UDK validation for XML message
28961 
28962   v_sql_stmt := 31;
28963   lv_sql_stmt :=
28964   'UPDATE   msc_st_resource_changes msrc1'
28965   ||' SET       process_flag = '||G_ERROR_FLG||','
28966   ||'         error_text   = '||''''||lv_message_text||''''
28967   ||' WHERE     message_id < ( SELECT max(message_id)'
28968   ||'           FROM msc_st_resource_changes msrc2'
28969   ||'           WHERE msrc2.sr_instance_code '
28970   ||'                    = msrc1.sr_instance_code'
28971   ||'           AND   msrc2.organization_code '
28972   ||'                   = msrc1.organization_code'
28973   ||'           AND   NVL(msrc2.company_name,'||''''||NULL_CHAR||''''||') '
28974   ||'                    = NVL(msrc1.company_name,'||''''||NULL_CHAR||''''||') '
28975   ||'           AND   msrc2.department_code '
28976   ||'                    = msrc1.department_code'
28977   ||'           AND   msrc2.resource_code '
28978   ||'                      = msrc1.resource_code'
28979   ||'           AND   msrc2.shift_name '
28980   ||'                      = msrc1.shift_name '
28981   ||'           AND   NVL(msrc2.simulation_set,'||''''||NULL_CHAR||''''||') '
28982   ||'                    = NVL(msrc1.simulation_set,'||''''||NULL_CHAR||''''||') '
28983   ||'           AND   msrc2.from_date    '
28984   ||'                    = msrc1.from_date'
28985   ||'           AND   NVL(msrc2.to_date,(sysdate-36500)) '
28986   ||'                    = NVL(msrc1.to_date,(sysdate-36500)) '
28987   ||'           AND   NVL(msrc2.from_time,'||NULL_VALUE||')'
28988   ||'                    = NVL(msrc1.from_time,'||NULL_VALUE||')'
28989   ||'           AND   NVL(msrc2.to_time,'||NULL_VALUE||')'
28990   ||'                    = NVL(msrc1.to_time,'||NULL_VALUE||')'
28991   ||'           AND   msrc2.action_type '
28992   ||'                     = msrc2.action_type'
28993   ||'           AND   msrc2.deleted_flag '
28994   ||'                      = msrc1.deleted_flag '
28995   ||'           AND   msrc2.process_flag                   ='||G_IN_PROCESS
28996   ||'           AND   NVL(msrc2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
28997   ||' AND       msrc1.process_flag             ='|| G_IN_PROCESS
28998   ||' AND       msrc1.sr_instance_code         = :v_instance_code '
28999   ||' AND     NVL(msrc1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
29000 
29001 
29002          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29003 
29004   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
29005 
29006   --Duplicate records check for the records whose source is batch load
29007 
29008     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29009                  (p_app_short_name    => 'MSC',
29010                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
29011                   p_message_text      => lv_message_text,
29012                   p_error_text        => lv_error_text);
29013 
29014     IF lv_return <> 0 THEN
29015       RAISE ex_logging_err;
29016     END IF;
29017 
29018   lv_sql_stmt :=
29019   'UPDATE      msc_st_resource_changes msrc1'
29020   ||' SET      process_flag = '||G_ERROR_FLG||','
29021   ||'          error_text   = '||''''||lv_message_text||''''
29022   ||' WHERE    EXISTS   ( SELECT 1'
29023   ||'           FROM msc_st_resource_changes msrc2'
29024   ||'           WHERE msrc2.sr_instance_code '
29025   ||'                    = msrc1.sr_instance_code'
29026   ||'           AND   msrc2.organization_code '
29027   ||'                   = msrc1.organization_code'
29028   ||'           AND   NVL(msrc2.company_name,'||''''||NULL_CHAR||''''||') '
29029   ||'                    = NVL(msrc1.company_name,'||''''||NULL_CHAR||''''||') '
29030   ||'           AND   msrc2.department_code '
29031   ||'                    = msrc1.department_code'
29032   ||'           AND   msrc2.resource_code '
29033   ||'                      = msrc1.resource_code'
29034   ||'           AND   msrc2.shift_name '
29035   ||'                      = msrc1.shift_name '
29036   ||'           AND   NVL(msrc2.simulation_set,'||''''||NULL_CHAR||''''||') '
29037   ||'                    = NVL(msrc1.simulation_set,'||''''||NULL_CHAR||''''||') '
29038   ||'           AND   msrc2.from_date    '
29039   ||'                    = msrc1.from_date'
29040   ||'           AND   NVL(msrc2.to_date,(sysdate-36500)) '
29041   ||'                    = NVL(msrc1.to_date,(sysdate-36500)) '
29042   ||'           AND   NVL(msrc2.from_time,'||NULL_VALUE||')'
29043   ||'                    = NVL(msrc1.from_time,'||NULL_VALUE||')'
29044   ||'           AND   NVL(msrc2.to_time,'||NULL_VALUE||')'
29045   ||'                    = NVL(msrc1.to_time,'||NULL_VALUE||')'
29046   ||'           AND   msrc2.action_type '
29047   ||'                     = msrc1.action_type'
29048   ||'           AND   msrc2.deleted_flag '
29049   ||'                      = msrc1.deleted_flag '
29050   ||'           AND   msrc2.process_flag                   ='||G_IN_PROCESS
29051   ||'           AND NVL(msrc2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
29052   ||'           GROUP BY sr_instance_code,organization_code,company_name,department_code,'
29053   ||'           resource_code,shift_name,simulation_set,from_date,'
29054   ||'           to_date,from_time,to_time,action_type,deleted_flag'
29055   ||'           HAVING COUNT(*) >1 )'
29056   ||' AND     msrc1.process_flag                   ='|| G_IN_PROCESS
29057   ||' AND     msrc1.sr_instance_code               = :v_instance_code'
29058   ||' AND     NVL(msrc1.message_id,'||NULL_VALUE||') = '||NULL_VALUE ;
29059 
29060 
29061          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29062 
29063   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
29064 
29065 
29066   lv_column_names :=
29067    'DEPARTMENT_CODE     ||''~''||'
29068   ||'RESOURCE_CODE      ||''~''||'
29069   ||'SHIFT_NAME         ||''~''||'
29070   ||'FROM_DATE          ||''~''||'
29071   ||'SIMULATION_SET     ||''~''||'
29072   ||'ACTION_TYPE        ||''~''||'
29073   ||'ORGANIZATION_CODE  ||''~''||'
29074   ||'SR_INSTANCE_CODE   ||''~''||'
29075   ||'TO_DATE            ||''~''||'
29076   ||'FROM_TIME          ||''~''||'
29077   ||'TO_TIME            ||''~''||'
29078   ||'CAPACITY_CHANGE    ||''~''||'
29079   ||'DELETED_FLAG       ||''~''||'
29080   ||'COMPANY_NAME' ;
29081 
29082 
29083 
29084    LOOP
29085       v_sql_stmt := 33;
29086       lv_cursor_stmt :=
29087       ' SELECT msc_st_batch_id_s.NEXTVAL '
29088       ||' FROM   dual';
29089 
29090       OPEN  c1 FOR lv_cursor_stmt;
29091       FETCH c1 INTO lv_batch_id;
29092       CLOSE c1;
29093 
29094       v_sql_stmt := 34;
29095       lv_sql_stmt :=
29096       ' UPDATE msc_st_resource_changes '
29097       ||' SET    batch_id  = :lv_batch_id'
29098       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
29099       ||' AND    sr_instance_code               = :v_instance_code'
29100       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
29101       ||' AND    rownum                        <= '||v_batch_size;
29102 
29103 
29104          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29105 
29106       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
29107 
29108       EXIT WHEN SQL%NOTFOUND;
29109 
29110       OPEN c7(lv_batch_id);
29111       FETCH c7 BULK COLLECT INTO lb_rowid;
29112       CLOSE c7;
29113 
29114       v_sql_stmt := 35;
29115       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
29116       UPDATE msc_st_resource_changes
29117       SET  st_transaction_id = msc_st_resource_changes_s.NEXTVAL,
29118            refresh_id        = v_refresh_id,
29119            last_update_date  = v_current_date,
29120            last_updated_by   = v_current_user,
29121            creation_date     = v_current_date,
29122            created_by        = v_current_user
29123       WHERE  rowid           = lb_rowid(j);
29124 
29125 
29126 
29127     -- Set the error message
29128 
29129       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29130                      (p_app_short_name    => 'MSC',
29131                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
29132                       p_message_text      => lv_message_text,
29133                       p_error_text        => lv_error_text,
29134                       p_token1            => 'COLUMN_NAME',
29135                       p_token_value1      => 'FROM_DATE');
29136 
29137       IF lv_return <> 0 THEN
29138         RAISE ex_logging_err;
29139       END IF;
29140 
29141      -- Error out records where from_date is NULL
29142 
29143     v_sql_stmt := 37;
29144     lv_sql_Stmt:=
29145     'UPDATE     msc_st_resource_changes '
29146     ||' SET     process_flag              ='||G_ERROR_FLG||','
29147     ||'         error_text   = '||''''||lv_message_text||''''
29148     ||' WHERE   NVL(from_date,(sysdate-36500))  = (sysdate-36500)'
29149     ||' AND     sr_instance_code          = :v_instance_code'
29150     ||' AND     process_flag              ='|| G_IN_PROCESS
29151     ||' AND     batch_id                  = :lv_batch_id';
29152 
29153 
29154            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29155 
29156    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
29157 
29158         -- Set the  message
29159 
29160      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29161                      (p_app_short_name    => 'MSC',
29162                       p_error_code        => 'MSC_PP_INVALID_VALUE',
29163                       p_message_text      => lv_message_text,
29164                       p_error_text        => lv_error_text,
29165                       p_token1            => 'COLUMN_NAME',
29166                       p_token_value1      => 'ACTION_TYPE');
29167 
29168       IF lv_return <> 0 THEN
29169         RAISE ex_logging_err;
29170       END IF;
29171 
29172 
29173     -- error out record with ACTION_TYPE NOT IN (1,2,3)
29174 
29175     v_sql_stmt := 38;
29176 
29177     lv_sql_Stmt:=
29178     'UPDATE   msc_st_resource_changes '
29179     ||' SET     process_flag              ='||G_ERROR_FLG||','
29180     ||'         error_text   = '||''''||lv_message_text||''''
29181     ||' WHERE    NVL(action_type,'||NULL_VALUE||')  NOT IN (1,2,3) '
29182     ||' AND      sr_instance_code          = :v_instance_code'
29183     ||' AND      process_flag              ='|| G_IN_PROCESS
29184     ||' AND      batch_id                  = :lv_batch_id';
29185 
29186 
29187            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29188 
29189    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
29190 
29191 
29192     -- Set the error message
29193      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29194                      (p_app_short_name    => 'MSC',
29195                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
29196                       p_message_text      => lv_message_text,
29197                       p_error_text        => lv_error_text,
29198                       p_token1            => 'COLUMN_NAME',
29199                       p_token_value1      => 'DELETED_FLAG',
29200                       p_token2            => 'DEFAULT_VALUE',
29201                       p_token_value2      =>  SYS_NO  );
29202 
29203       IF lv_return <> 0 THEN
29204         RAISE ex_logging_err;
29205       END IF;
29206 
29207      -- Check whether delete flag value is in (1,2),if not default to SYS_NO
29208 
29209      lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||')NOT IN (1,2)';
29210 
29211      lv_return := MSC_ST_UTIL.LOG_ERROR
29212                     (p_table_name        => 'MSC_ST_RESOURCE_CHANGES',
29213                      p_instance_code     => v_instance_code,
29214                      p_row               => lv_column_names,
29215                      p_severity          => G_SEV_WARNING,
29216                      p_message_text      => lv_message_text,
29217                      p_error_text        => lv_error_text,
29218                      p_batch_id          => lv_batch_id,
29219                      p_where_str         => lv_where_str,
29220                      p_col_name          => 'DELETED_FLAG',
29221                      p_debug             => v_debug,
29222                      p_default_value     => SYS_NO);
29223 
29224 
29225       IF lv_return <> 0 THEN
29226          RAISE ex_logging_err;
29227       END IF;
29228 
29229      --Set the  message
29230      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29231                      (p_app_short_name    => 'MSC',
29232                       p_error_code        => 'MSC_PP_INVALID_VALUE',
29233                       p_message_text      => lv_message_text,
29234                       p_error_text        => lv_error_text,
29235                       p_token1            => 'COLUMN_NAME',
29236                       p_token_value1      => 'ORGANIZATION_CODE');
29237 
29238       IF lv_return <> 0 THEN
29239         RAISE ex_logging_err;
29240       END IF;
29241 
29242       -- Derive  organization id
29243      lv_return :=
29244      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_RESOURCE_CHANGES',
29245                                     p_org_partner_name  => 'ORGANIZATION_CODE',
29246                                     p_org_partner_id    => 'ORGANIZATION_ID',
29247                                     p_instance_code     => v_instance_code,
29248                                     p_partner_type      => G_ORGANIZATION,
29249                                     p_severity          => G_SEV_ERROR,
29250                                     p_message_text      => lv_message_text,
29251                                     p_batch_id          => lv_batch_id,
29252                                     p_debug             => v_debug,
29253                                     p_error_text        => lv_error_text);
29254 
29255      IF(lv_return <> 0) then
29256         RAISE ex_logging_err;
29257      END IF;
29258 
29259 
29260    -- Popluate the Department id from LOCAL_ID table
29261 
29262     v_sql_stmt := 39;
29263     lv_sql_stmt :=
29264     'UPDATE         msc_st_resource_changes msrc'
29265     ||' SET         department_id = (SELECT local_id'
29266     ||'             FROM msc_local_id_setup mlis'
29267     ||'             WHERE  mlis.char1    = msrc.sr_instance_code'
29268     ||'             AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
29269     ||'             =    NVL(msrc.company_name,'||''''||NULL_CHAR||''''||') '
29270     ||'             AND    mlis.char3       = msrc.organization_code'
29271     ||'             AND    mlis.char4       = msrc.department_code'
29272     ||'             AND    mlis.entity_name = ''DEPARTMENT_ID'' '
29273     ||'             AND    mlis.instance_id = '||v_instance_id||')'
29274     ||' WHERE      sr_instance_code = :v_instance_code'
29275     ||' AND        process_flag     = '|| G_IN_PROCESS
29276     ||' AND        batch_id         = :lv_batch_id';
29277 
29278 
29279             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29280 
29281       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code, lv_batch_id;
29282 
29283     -- Set the error message
29284     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29285                      (p_app_short_name    => 'MSC',
29286                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
29287                       p_message_text      => lv_message_text,
29288                       p_error_text        => lv_error_text,
29289                       p_token1            => 'COLUMN_NAMES',
29290                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
29291                                              ||' ORGANIZATION_CODE,DEPARTMENT_CODE',
29292                       p_token2            => 'MASTER_TABLE',
29293                       p_token_value2      =>  'MSC_ST_DEPARTMENT_RESOURCES',
29294                       p_token3            =>  'CHILD_TABLE' ,
29295                       p_token_value3      =>  'MSC_ST_RESOURCE_CHANGES' );
29296 
29297       IF lv_return <> 0 THEN
29298         RAISE ex_logging_err;
29299       END IF;
29300 
29301     -- Error out the records where department id is NULL
29302 
29303     v_sql_stmt := 40;
29304     lv_sql_stmt:=
29305     'UPDATE   msc_st_resource_changes '
29306     ||' SET     process_flag            = '||G_ERROR_FLG||','
29307     ||'         error_text   = '||''''||lv_message_text||''''
29308     ||' WHERE    NVL(department_id,'||NULL_VALUE||') = '||NULL_VALUE
29309     ||' AND      sr_instance_code              = :v_instance_code'
29310     ||' AND      process_flag                  ='|| G_IN_PROCESS
29311     ||' AND      batch_id                      = :lv_batch_id';
29312 
29313 
29314             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29315 
29316     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id ;
29317 
29318 
29319    -- Popluate the  resource id from LOCAL_ID table
29320     v_sql_stmt := 41;
29321     lv_sql_stmt:=
29322     'UPDATE         msc_st_resource_changes msrc'
29323     ||' SET         resource_id = (SELECT local_id'
29324     ||'             FROM msc_local_id_setup mlis'
29325     ||'             WHERE  mlis.char1       = msrc.sr_instance_code'
29326     ||'             AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
29327     ||'             =    NVL(msrc.company_name,'||''''||NULL_CHAR||''''||') '
29328     ||'             AND    mlis.char3       = msrc.organization_code'
29329     ||'             AND    mlis.char4       = msrc.resource_code'
29330     ||'             AND    mlis.entity_name = ''RESOURCE_ID'' '
29331     ||'             AND    mlis.instance_id = '||v_instance_id||')'
29332     ||' WHERE      sr_instance_code = :v_instance_code'
29333     ||' AND        process_flag     = '|| G_IN_PROCESS
29334     ||' AND        batch_id         = :lv_batch_id';
29335 
29336 
29337             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29338 
29339       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
29340 
29341 
29342     -- Set the error message
29343     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29344                      (p_app_short_name    => 'MSC',
29345                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
29346                       p_message_text      => lv_message_text,
29347                       p_error_text        => lv_error_text,
29348                       p_token1            => 'COLUMN_NAMES',
29349                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
29350                                              ||' ORGANIZATION_CODE,RESOURCE_CODE',
29351                       p_token2            => 'MASTER_TABLE',
29352                       p_token_value2      =>  'MSC_ST_DEPARTMENT_RESOURCES',
29353                       p_token3            =>  'CHILD_TABLE' ,
29354                       p_token_value3      =>  'MSC_ST_RESOURCE_CHANGES' );
29355 
29356       IF lv_return <> 0 THEN
29357         RAISE ex_logging_err;
29358       END IF;
29359 
29360     -- Error out the records where resource id is NULL
29361 
29362     v_sql_stmt := 42;
29363     lv_sql_stmt:=
29364     'UPDATE     msc_st_resource_changes '
29365     ||' SET     process_flag            = '||G_ERROR_FLG||','
29366     ||'         error_text   = '||''''||lv_message_text||''''
29367     ||' WHERE   NVL(resource_id,'||NULL_VALUE||') = '||NULL_VALUE
29368     ||' AND     sr_instance_code              = :v_instance_code'
29369     ||' AND     process_flag                  ='|| G_IN_PROCESS
29370     ||' AND     batch_id                      = :lv_batch_id';
29371 
29372 
29373             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29374 
29375      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
29376 
29377 
29378   /* Bug 2172537 ,instance code not prefixed in calendar and org*/
29379    -- Populate the SHIFT_NUM
29380    v_sql_stmt := 43;
29381 
29382    lv_sql_stmt :=
29383    'UPDATE    msc_st_resource_changes msrc'
29384    ||' SET       shift_num =(SELECT    mlis.local_id'
29385    ||'                      FROM       msc_local_id_setup mlis, msc_st_trading_partners mtp '
29386    ||'                      WHERE      mtp.sr_instance_id      = '||v_instance_id
29387    ||'                      AND        mlis.char1              = msrc.sr_instance_code'
29388    ||'                             AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
29389    ||'                             =    NVL(msrc.company_name,'||''''||NULL_CHAR||''''||') '
29390    ||'                      AND        mtp.calendar_code       = mlis.char3'
29391    ||'                      AND        mlis.char4              = msrc.shift_name'
29392    ||'                      AND        mlis.entity_name        = ''SHIFT_NUM'' '
29393    ||'                      AND        mlis.instance_id        = '||v_instance_id
29394    ||'                      AND        mtp.organization_code   = msrc.organization_code'
29395    ||'                      AND        mtp.process_flag    = '||G_VALID
29396    ||'                      AND        mtp.partner_type    = 3 )'
29397    ||' WHERE    msrc.sr_instance_code          = :v_instance_code'
29398    ||' AND      msrc.process_flag              ='|| G_IN_PROCESS
29399    ||' AND      msrc.batch_id                  = :lv_batch_id';
29400 
29401 
29402             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29403 
29404 
29405      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
29406 
29407 
29408    lv_sql_stmt :=
29409    'UPDATE    msc_st_resource_changes msrc'
29410    ||' SET       shift_num =(SELECT    mlis.local_id'
29411    ||'                      FROM       msc_local_id_setup mlis, msc_trading_partners mtp '
29412    ||'                      WHERE      mtp.sr_instance_id      = '||v_instance_id
29413    ||'                      AND        mlis.char1              = msrc.sr_instance_code'
29414    ||'                             AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
29415    ||'                             =    NVL(msrc.company_name,'||''''||NULL_CHAR||''''||') '
29416    ||'                      AND        mtp.calendar_code       = mlis.char3'
29417    ||'                      AND        mlis.char4              = msrc.shift_name'
29418    ||'                      AND        mlis.entity_name        = ''SHIFT_NUM'' '
29419    ||'                      AND        mlis.instance_id        = '||v_instance_id
29420    ||'                      AND        mtp.organization_code   = msrc.organization_code'
29421    ||'                      AND        mtp.partner_type    = 3 )'
29422    ||' WHERE    msrc.sr_instance_code          = :v_instance_code'
29423    ||' AND      NVL(msrc.shift_num,'||NULL_VALUE||') = '||NULL_VALUE
29424    ||' AND      msrc.process_flag              ='|| G_IN_PROCESS
29425    ||' AND      msrc.batch_id                  = :lv_batch_id';
29426 
29427 
29428             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29429 
29430 
29431      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
29432 
29433 
29434     -- Set the error message
29435      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29436                      (p_app_short_name    => 'MSC',
29437                       p_error_code        => 'MSC_PP_INVALID_VALUE',
29438                       p_message_text      => lv_message_text,
29439                       p_error_text        => lv_error_text,
29440                       p_token1            => 'COLUMN_NAME',
29441                       p_token_value1      => 'SHIFT_NAME');
29442       IF lv_return <> 0 THEN
29443         RAISE ex_logging_err;
29444       END IF;
29445 
29446 
29447    -- Error out records where shift_num is NULL
29448 
29449     v_sql_stmt := 30;
29450 
29451     lv_sql_stmt:=
29452     'UPDATE      msc_st_resource_changes '
29453     ||' SET      process_flag            = '||G_ERROR_FLG||','
29454     ||'          error_text   = '||''''||lv_message_text||''''
29455     ||' WHERE    NVL(shift_num,'||NULL_VALUE||') = '||NULL_VALUE
29456     ||' AND      sr_instance_code              = :v_instance_code'
29457     ||' AND      process_flag                  ='|| G_IN_PROCESS
29458     ||' AND      batch_id                      = :lv_batch_id';
29459 
29460 
29461             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29462 
29463      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
29464 
29465     lv_simulation_set := v_instance_code||':'||'SIMSET'  ;
29466 
29467     v_sql_stmt := 31;
29468 
29469     lv_sql_stmt:=
29470     'UPDATE      msc_st_resource_changes '
29471     ||' SET      simulation_set = '||''''||lv_simulation_set||''''
29472     ||' WHERE     NVL(simulation_set,'||''''||NULL_CHAR||''''||') '
29473     ||'         = '||''''||NULL_CHAR||''''
29474     ||' AND      sr_instance_code              = :v_instance_code'
29475     ||' AND      process_flag                  ='|| G_IN_PROCESS
29476     ||' AND      batch_id                      = :lv_batch_id';
29477 
29478 
29479             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29480 
29481      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
29482 
29483 
29484    --Call to customised validation.
29485 
29486       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
29487         (ERRBUF         => lv_error_text,
29488          RETCODE        => lv_return,
29489          pBatchID       => lv_batch_id,
29490          pInstanceCode  => v_instance_code,
29491          pEntityName    => 'MSC_ST_RESOURCE_CHANGES',
29492          pInstanceID    => v_instance_id);
29493 
29494       IF NVL(lv_return,0) <> 0 THEN
29495         RAISE ex_logging_err;
29496       END IF;
29497 
29498 
29499      -- Set the process flag as Valid and populate instance_id
29500     lv_return :=
29501     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_RESOURCE_CHANGES',
29502                                p_instance_id    => v_instance_id,
29503                                p_instance_code  => v_instance_code,
29504                                p_process_flag   => G_VALID,
29505                                p_error_text     => lv_error_text,
29506                                p_debug          => v_debug,
29507                                p_batch_id       => lv_batch_id);
29508 
29509       IF lv_return <> 0 THEN
29510           RAISE ex_logging_err;
29511       END IF;
29512 
29513    -- Also insert the simulation set
29514      OPEN c8(lv_batch_id);
29515      FETCH c8 BULK COLLECT INTO lb_rowid ;
29516 
29517      IF c8%ROWCOUNT > 0  THEN
29518 
29519     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
29520 
29521     INSERT INTO msc_st_simulation_sets
29522      ( ORGANIZATION_ID,
29523       SIMULATION_SET,
29524       DESCRIPTION,
29525       USE_IN_WIP_FLAG,
29526       DELETED_FLAG,
29527       REFRESH_ID,
29528       SR_INSTANCE_ID)
29529       SELECT
29530       ORGANIZATION_ID,
29531       SIMULATION_SET,
29532       NULL,
29533       NULL,
29534       DELETED_FLAG,
29535       v_refresh_id,
29536       SR_INSTANCE_ID
29537       FROM msc_st_resource_changes
29538       where rowid = lb_rowid(j) ;
29539 
29540       END IF;
29541       CLOSE c8;
29542 
29543 
29544     -- At the end calling  the LOG_ERROR for logging all errored out records
29545 
29546    lv_return :=
29547    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_RESOURCE_CHANGES',
29548                          p_instance_code => v_instance_code,
29549                          p_row           => lv_column_names,
29550                          p_severity      => G_SEV_ERROR,
29551                          p_message_text  => NULL,
29552                          p_error_text    => lv_error_text,
29553                          p_debug         => v_debug,
29554                          p_batch_id      => lv_batch_id  );
29555 
29556       IF lv_return <> 0 THEN
29557           RAISE ex_logging_err;
29558       END IF;
29559   COMMIT;
29560   END LOOP;
29561 
29562 
29563 
29564   EXCEPTION
29565     WHEN too_many_rows THEN
29566       lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_DEPT_RES'||'('
29567                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
29568         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
29569          ROLLBACK ;
29570 
29571     WHEN ex_logging_err THEN
29572         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
29573       ROLLBACK;
29574 
29575     WHEN OTHERS THEN
29576       lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_DEPT_RES'||'('
29577                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
29578         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
29579       ROLLBACK;
29580 
29581   END LOAD_DEPT_RES;
29582 
29583 
29584 /*==========================================================================+
29585 | DESCRIPTION  : This procedure is called for validating supplies related   |
29586 |                records from the msc_st_supplies table. The following      |
29587 |                types are considered for the validation.                   |
29588 |                Supply Source                    Order Type                |
29589 |                --------------------------------------------               |
29590 |                Purchase Order                   1                         |
29591 |                Requisition                      2                         |
29592 |                Plan Orders                      5                         |
29593 |                PO Receiving                     8                         |
29594 |                Intransit Shipment               11                        |
29595 |                Intransit Receipt                12                        |
29596 |                On-Hand Quantities               18                        |
29597 +==========================================================================*/
29598   PROCEDURE LOAD_SUPPLY(p_batch_id IN NUMBER DEFAULT NULL_VALUE) IS
29599   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
29600   lb_rowid            RowidTab;
29601   lv_return           NUMBER;
29602   lv_return2           NUMBER;
29603   lv_planning_tp_type Number;
29604   lv_owning_tp_type   Number;
29605   lv_error_text       VARCHAR2(250);
29606   lv_where_str        VARCHAR2(5000);
29607   lv_sql_stmt         VARCHAR2(5000);
29608   lv_column_names     VARCHAR2(5000);               --stores concatenated column names
29609   lv_message_text     msc_errors.error_text%TYPE;
29610   ex_logging_err        EXCEPTION;
29611   busy EXCEPTION;
29612   PRAGMA EXCEPTION_INIT(busy, -54);
29613 
29614   CURSOR c1(p_batch_id NUMBER) IS
29615     SELECT rowid
29616     FROM   msc_st_supplies
29617     WHERE  order_type              IN(1,2,5,8,11,12,18,73,74,87)
29618     AND    process_flag            IN (G_IN_PROCESS,G_ERROR_FLG)
29619     AND    NVL(batch_id,NULL_VALUE)=p_batch_id
29620     AND    sr_instance_code        = v_instance_code;
29621 
29622   CURSOR c2(p_batch_id NUMBER) IS
29623     SELECT rowid
29624     FROM   msc_st_supplies
29625     WHERE  NVL(sr_mtl_supply_id,NULL_VALUE) = NULL_VALUE
29626     AND    order_type                       IN(1,2,8,11,12,73,74,87)
29627     AND    process_flag                     = G_IN_PROCESS
29628     AND    NVL(batch_id,NULL_VALUE)         =p_batch_id
29629     AND    sr_instance_code                 = v_instance_code;
29630 
29631   CURSOR c3(p_batch_id NUMBER) IS
29632     SELECT max(rowid)
29633     FROM   msc_st_supplies
29634     WHERE  NVL(disposition_id,NULL_VALUE)   = NULL_VALUE
29635     AND    order_type                       IN(1,2,8,11,12,73,74,87)
29636     AND    deleted_flag                     = SYS_NO
29637     AND    process_flag                     = G_IN_PROCESS
29638     AND    NVL(batch_id,NULL_VALUE)         =p_batch_id
29639     AND    sr_instance_code                 = v_instance_code
29640     GROUP BY sr_instance_code,company_name,order_number,order_type;
29641 
29642   CURSOR c4(p_batch_id NUMBER) IS
29643     SELECT rowid
29644     FROM   msc_st_supplies
29645     WHERE  NVL(po_line_id,NULL_VALUE)   = NULL_VALUE
29646     AND    deleted_flag                 = SYS_NO
29647     AND    order_type                   IN(1,2,8,11,12,73,74,87)
29648     AND    process_flag                 = G_IN_PROCESS
29649     AND    NVL(batch_id,NULL_VALUE)     =p_batch_id
29650     AND    sr_instance_code             = v_instance_code;
29651 
29652   CURSOR c5(p_batch_id NUMBER) IS
29653     SELECT rowid
29654     FROM   msc_st_supplies
29655     WHERE  NVL(disposition_id,NULL_VALUE)   = NULL_VALUE
29656     AND    order_type                       = 5
29657     AND    process_flag                     = G_IN_PROCESS
29658     AND    NVL(batch_id,NULL_VALUE)         =p_batch_id
29659     AND    sr_instance_code                 = v_instance_code;
29660 
29661   CURSOR c6(p_batch_id NUMBER) IS
29662     SELECT rowid
29663     FROM   msc_st_supplies
29664     WHERE  order_type              = 18
29665     AND    process_flag            IN (G_IN_PROCESS,G_ERROR_FLG)
29666     AND    NVL(batch_id,NULL_VALUE)=p_batch_id
29667     AND    sr_instance_code        = v_instance_code;
29668 
29669   CURSOR c_lock(p_batch_id NUMBER)  IS
29670     SELECT  'X'
29671     FROM    msc_st_supplies ms1 ,
29672             msc_st_supplies ms2
29673     WHERE   ms1.batch_id      = p_batch_id
29674     AND     ms1.batch_id     <>  nvl(ms2.batch_id,NULL_VALUE)
29675     AND     ms1.disposition_id IS NULL
29676     AND     ms2.disposition_id IS NULL
29677     AND     ms1.sr_instance_code   =  ms2.sr_instance_code
29678     AND     NVL(ms1.company_name,NULL_CHAR)= NVL(ms2.company_name,NULL_CHAR)
29679     AND     ms1.order_number  =  ms2.order_number
29680     AND     ms1.order_type    =  ms2.order_type
29681     AND     ms1.order_type  IN(1,2,8,11,12,73,74,87)
29682     FOR UPDATE NOWAIT   ;
29683 
29684 
29685 
29686   BEGIN
29687 
29688     lv_column_names :=
29689     'ITEM_NAME                ||''~''||'
29690     ||'ORGANIZATION_CODE      ||''~''||'
29691     ||'NEW_SCHEDULE_DATE      ||''~''||'
29692     ||'NEW_ORDER_QUANTITY     ||''~''||'
29693     ||'FIRM_PLANNED_TYPE      ||''~''||'
29694     ||'SUPPLIER_NAME          ||''~''||'
29695     ||'PURCH_LINE_NUM         ||''~''||'
29696     ||'ORDER_NUMBER           ||''~''||'
29697     ||'SUPPLIER_SITE_CODE     ||''~''||'
29698     ||'SR_INSTANCE_CODE       ||''~''||'
29699     ||'REVISION               ||''~''||'
29700     ||'UNIT_NUMBER            ||''~''||'
29701     ||'NEW_DOCK_DATE          ||''~''||'
29702     ||'PROJECT_NUMBER         ||''~''||'
29703     ||'TASK_NUMBER            ||''~''||'
29704     ||'PLANNING_GROUP         ||''~''||'
29705     ||'DELIVERY_PRICE         ||''~''||'
29706     ||'QTY_SCRAPPED           ||''~''||'
29707     ||'FROM_ORGANIZATION_CODE ||''~''||'
29708     ||'DELETED_FLAG           ||''~''||'
29709     ||'ORDER_TYPE             ||''~''||'
29710     ||'SUBINVENTORY_CODE      ||''~''||'
29711     ||'LOT_NUMBER             ||''~''||'
29712     ||'EXPIRATION_DATE        ||''~''||'
29713     ||'SCHEDULE_LINE_NUM      ||''~''||'
29714     ||'LAST_UNIT_START_DATE   ||''~''||'
29715     ||'SCHEDULE_DESIGNATOR    ||''~''||'
29716     ||'COMPANY_NAME           ||''~''||'
29717     ||'DAILY_RATE';
29718 
29719     LOOP
29720       BEGIN
29721           v_sql_stmt := 01;
29722           OPEN c_lock(p_batch_id) ;
29723           EXIT;
29724        EXCEPTION
29725         WHEN busy THEN
29726            lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_SUPPLY'||'(' ||v_sql_stmt||')'|| SQLERRM, 1, 240);
29727         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
29728             dbms_lock.sleep(5);
29729         WHEN OTHERS THEN
29730          lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_SUPPLY'||'(' ||v_sql_stmt||')'|| SQLERRM, 1, 240);
29731         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
29732        v_sql_stmt := 2.0;
29733        lv_sql_stmt :=
29734        'UPDATE msc_st_supplies '
29735        ||' SET process_flag = 1, '
29736        ||' batch_id = NULL'
29737        ||' WHERE batch_id = :p_batch_id'
29738        ||' AND  order_type IN(1,2,8,11,12,73,74,87)'
29739        ||' AND process_flag      = '||G_IN_PROCESS
29740        ||' AND sr_instance_code  =:v_instance_code';
29741 
29742 
29743          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29744 
29745       EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
29746       COMMIT;
29747       RETURN;
29748       END ;
29749     END LOOP ;
29750 
29751     v_sql_stmt := 2.1;
29752     lv_sql_stmt :=
29753     'UPDATE msc_st_supplies mss '
29754     ||' SET disposition_id = (SELECT local_id'
29755     ||'       FROM   msc_local_id_supply mls'
29756     ||'       WHERE  mls.char3       = mss.order_number'
29757     ||'       AND    mls.number1     = mss.order_type'
29758     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
29759     ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
29760     ||'       AND    mls.char1       = mss.sr_instance_code'
29761     ||'       AND    mls.entity_name = ''DISPOSITION_ID'' ) '
29762     ||' WHERE  order_type  IN(1,2,8,11,12,73,74,87)'
29763     ||' AND    NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
29764     ||' AND    deleted_flag                   = '||SYS_NO
29765     ||' AND    process_flag                   = '||G_IN_PROCESS
29766     ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
29767     ||' AND    sr_instance_code               = :v_instance_code';
29768 
29769 
29770       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29771 
29772     EXECUTE IMMEDIATE lv_sql_stmt
29773             USING     p_batch_id,
29774                       v_instance_code;
29775 
29776 
29777     --Generation of disposition_id(PO, PO Req. and Intransit)
29778     OPEN  c3(p_batch_id);
29779     FETCH c3 BULK COLLECT INTO lb_rowid ;
29780 
29781     IF c3%ROWCOUNT > 0  THEN
29782       v_sql_stmt := 3.0;
29783       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
29784       UPDATE msc_st_supplies
29785       SET    disposition_id = msc_st_disposition_id_s.NEXTVAL
29786       WHERE  rowid          = lb_rowid(j);
29787 
29788       v_sql_stmt := 3.1;
29789       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
29790       INSERT INTO msc_local_id_supply
29791         (local_id,
29792          st_transaction_id,
29793          instance_id,
29794          entity_name,
29795          data_source_type,
29796          char1,
29797          char2,
29798          char3,
29799          number1,
29800          SOURCE_DISPOSITION_ID,
29801          last_update_date,
29802          last_updated_by,
29803          creation_date,
29804          created_by)
29805       SELECT
29806          disposition_id,
29807          st_transaction_id,
29808          v_instance_id,
29809          'DISPOSITION_ID',
29810          data_source_type,
29811          v_instance_code,
29812          company_name,
29813          order_number,
29814          order_type,
29815          SOURCE_DISPOSITION_ID,
29816          v_current_date,
29817          v_current_user,
29818          v_current_date,
29819          v_current_user
29820       FROM  msc_st_supplies
29821       WHERE rowid = lb_rowid(j);
29822     END IF;
29823     CLOSE c3 ;
29824 
29825     v_sql_stmt := 4.0;
29826     lv_sql_stmt :=
29827     'UPDATE msc_st_supplies mss '
29828     ||' SET disposition_id = (SELECT local_id'
29829     ||'       FROM   msc_local_id_supply mls'
29830     ||'       WHERE  mls.char3       = mss.order_number'
29831     ||'       AND    mls.number1     = mss.order_type'
29832     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
29833     ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
29834     ||'       AND    mls.char1       = mss.sr_instance_code'
29835     ||'       AND    mls.entity_name = ''DISPOSITION_ID'' ) '
29836     ||' WHERE  order_type  IN(1,2,8,11,12,73,74,87)'
29837     ||' AND    NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
29838     ||' AND    deleted_flag                   = '||SYS_NO
29839     ||' AND    process_flag                   = '||G_IN_PROCESS
29840     ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
29841     ||' AND    sr_instance_code               = :v_instance_code';
29842 
29843 
29844       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29845 
29846     EXECUTE IMMEDIATE lv_sql_stmt
29847             USING     p_batch_id,
29848                      v_instance_code;
29849 
29850 
29851   COMMIT ;
29852   IF c_lock%ISOPEN THEN
29853     CLOSE c_lock;
29854   END IF;
29855 
29856 
29857     OPEN c1(p_batch_id);
29858     FETCH c1 BULK COLLECT INTO lb_rowid;
29859     IF c1%ROWCOUNT > 0  THEN
29860 
29861     v_sql_stmt := 5.0;
29862     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
29863     UPDATE msc_st_supplies
29864     SET    st_transaction_id   = msc_st_supplies_s.NEXTVAL,
29865            refresh_id          = v_refresh_id,
29866            last_update_date    = v_current_date,
29867            last_updated_by     = v_current_user,
29868            creation_date       = v_current_date,
29869            created_by          = v_current_user
29870     WHERE  rowid               = lb_rowid(j);
29871 
29872     END IF;
29873     CLOSE c1;
29874 
29875     lv_where_str :=
29876     '   AND NVL(deleted_flag,'||NULL_VALUE||') '
29877     ||' NOT IN(1,2)';
29878 
29879     --Log a warning for those records where the deleted_flag has a value other
29880     --SYS_NO
29881     lv_return := MSC_ST_UTIL.LOG_ERROR
29882                    (p_table_name        => 'MSC_ST_SUPPLIES',
29883                     p_instance_code     => v_instance_code,
29884                     p_row               => lv_column_names,
29885                     p_severity          => G_SEV_WARNING,
29886                     p_message_text      => lv_message_text,
29887                     p_error_text        => lv_error_text,
29888                     p_batch_id          => p_batch_id,
29889                     p_where_str         => lv_where_str,
29890                     p_col_name          => 'DELETED_FLAG',
29891                     p_debug             => v_debug,
29892                     p_default_value     => SYS_NO);
29893 
29894     IF lv_return <> 0 THEN
29895         RAISE ex_logging_err;
29896     END IF;
29897 
29898 --bug 2773881 (ssurendr) validation on need_by_date/promised_date -- changes begin
29899 
29900     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29901                    (p_app_short_name    => 'MSC',
29902                     p_error_code        => 'MSC_PP_COL_VAL_NULL',
29903                     p_message_text      => lv_message_text,
29904                     p_error_text        => lv_error_text,
29905                     p_token1            => 'COLUMN_NAME',
29906                     p_token_value1      => 'NEED_BY_DATE OR '
29907                                            ||' PROMISED_DATE');
29908 
29909     IF lv_return <> 0 THEN
29910       RAISE ex_logging_err;
29911     END IF;
29912 
29913     v_sql_stmt := 5.1;
29914     lv_sql_stmt :=
29915     'UPDATE   msc_st_supplies '
29916     ||' SET   error_text   = ''' || lv_message_text || ''','
29917     ||'       process_flag  = '||G_ERROR_FLG
29918     ||' WHERE NEED_BY_DATE                   is null '
29919     ||' AND   PROMISED_DATE                  is null '
29920     ||' AND   order_type                   in (1,74)  '
29921     ||' AND   process_flag                   = '||G_IN_PROCESS
29922     ||' AND   NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
29923     ||' AND   sr_instance_code               = :v_instance_code';
29924 
29925 
29926       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
29927 
29928     EXECUTE IMMEDIATE lv_sql_stmt
29929             USING     p_batch_id,
29930                       v_instance_code;
29931 
29932 --bug 2773881 (ssurendr) validation on need_by_date/promised_date -- changes end
29933 
29934     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
29935                    (p_app_short_name    => 'MSC',
29936                     p_error_code        => 'MSC_PP_INVALID_VALUE',
29937                     p_message_text      => lv_message_text,
29938                     p_error_text        => lv_error_text,
29939                     p_token1            => 'COLUMN_NAME',
29940                     p_token_value1      => 'ORGANIZATION_CODE');
29941 
29942     IF lv_return <> 0 THEN
29943       RAISE ex_logging_err;
29944     END IF;
29945 
29946     --Derive Organization_id
29947     lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
29948                    (p_table_name       => 'MSC_ST_SUPPLIES',
29949                     p_org_partner_name => 'ORGANIZATION_CODE',
29950                     p_org_partner_id   => 'ORGANIZATION_ID',
29951                     p_instance_code    => v_instance_code,
29952                     p_partner_type     => G_ORGANIZATION,
29953                     p_error_text       => lv_error_text,
29954                     p_batch_id         => p_batch_id,
29955                     p_severity         => G_SEV_ERROR,
29956                     p_message_text     => lv_message_text,
29957                     p_debug            => v_debug,
29958                     p_row              => lv_column_names);
29959 
29960     IF lv_return <> 0 THEN
29961         RAISE ex_logging_err;
29962     END IF;
29963 
29964     -------- populate condition type
29965 
29966 
29967 OPEN c6(p_batch_id);
29968     FETCH c6 BULK COLLECT INTO lb_rowid;
29969     IF c6%ROWCOUNT > 0  THEN
29970 
29971     v_sql_stmt := 2.2;
29972     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
29973       UPDATE msc_st_supplies mss
29974       SET mss.condition_type = (SELECT nvl(msi.condition_type,'G' )
29975               FROM msc_sub_inventories msi
29976               WHERE msi.organization_id = mss.organization_id
29977               AND msi.plan_id = -1
29978               AND msi.sub_inventory_code = mss.subinventory_code
29979               AND msi.sr_instance_id = v_instance_id
29980               AND mss.sr_instance_code = v_instance_code
29981               UNION
29982               SELECT nvl(mssi.condition_type ,'G' ) from msc_st_sub_inventories mssi
29983               WHERE mssi.organization_code = mss.organization_code
29984               AND mssi.sub_inventory_code = mss.subinventory_code
29985               AND mssi.sr_instance_code = mss.sr_instance_code)
29986     WHERE  rowid          = lb_rowid(j);
29987 
29988   msc_util.initialize_common_globals (v_instance_id);
29989 
29990       v_sql_stmt := 2.3;
29991       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
29992       UPDATE msc_st_supplies mss
29993       SET mss.ITEM_TYPE_ID = MSC_UTIL.G_PARTCONDN_ITEMTYPEID,
29994           mss.ITEM_TYPE_VALUE  =DECODE(mss.condition_type,'G',MSC_UTIL.G_PARTCONDN_GOOD,'B',MSC_UTIL.G_PARTCONDN_BAD,MSC_UTIL.G_PARTCONDN_GOOD)
29995       WHERE  rowid          = lb_rowid(j);
29996 
29997     END IF;
29998     CLOSE c6;
29999 
30000   IF v_instance_type = G_INS_OTHER THEN
30001 
30002       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30003                      (p_app_short_name    => 'MSC',
30004                       p_error_code        => 'MSC_PP_INVALID_VALUE',
30005                       p_message_text      => lv_message_text,
30006                       p_error_text        => lv_error_text,
30007                       p_token1            => 'COLUMN_NAME',
30008                       p_token_value1      => 'PLANNING_PARTNER_SITE_CODE');
30009 
30010       IF lv_return <> 0 THEN
30011         RAISE ex_logging_err;
30012       END IF;
30013 
30014       UPDATE  MSC_ST_SUPPLIES
30015        SET PLANNING_PARTNER_SITE_ID = (SELECT local_id
30016                                       FROM msc_local_id_setup
30017                                       WHERE  char1 = sr_instance_code
30018                                       AND    NVL(char2,NULL_CHAR) = NVL(company_name,NULL_CHAR)
30019                                       AND    char4            = PLANNING_PARTNER_SITE_CODE
30020                                       AND    number1          = G_VENDOR
30021                                       AND    entity_name      = 'SR_TP_SITE_ID' )
30022       WHERE  sr_instance_code = v_instance_code
30023        AND    NVL(PLANNING_PARTNER_SITE_ID,NULL_VALUE) = NULL_VALUE
30024       AND    NVL(batch_id,NULL_VALUE)= p_batch_id
30025       AND    process_flag     = G_IN_PROCESS
30026       AND    PLANNING_TP_TYPE = 1
30027       AND    ORDER_TYPE = 18;
30028 
30029 
30030 
30031       UPDATE  MSC_ST_SUPPLIES
30032        SET PLANNING_PARTNER_SITE_ID = (SELECT local_id
30033                                       FROM msc_local_id_setup
30034                                       WHERE  char1 = sr_instance_code
30035                                       AND    char3            = PLANNING_PARTNER_SITE_CODE
30036                                       AND    number1          = G_ORGANIZATION
30037                                       AND    entity_name      = 'SR_TP_ID' )
30038       WHERE  sr_instance_code = v_instance_code
30039        AND    NVL(PLANNING_PARTNER_SITE_ID,NULL_VALUE) = NULL_VALUE
30040       AND    NVL(batch_id,NULL_VALUE)= p_batch_id
30041       AND    process_flag     = G_IN_PROCESS
30042       AND    PLANNING_TP_TYPE = 2
30043       AND     ORDER_TYPE =18;
30044 
30045       UPDATE  MSC_ST_SUPPLIES mss1
30046       SET     process_flag = G_ERROR_FLG,
30047               error_text   = lv_message_text
30048       WHERE   EXISTS( SELECT 1
30049                       FROM   MSC_ST_SUPPLIES mss2
30050                       WHERE  mss2.sr_instance_code  = mss1.sr_instance_code
30051                       AND    mss2.process_flag      = G_IN_PROCESS
30052                       AND    NVL(mss2.planning_partner_site_id,NULL_VALUE) = NULL_VALUE
30053                       GROUP BY sr_instance_code HAVING COUNT(*) > 0)
30054       AND     mss1.process_flag             = G_IN_PROCESS
30055       AND     mss1.sr_instance_code            = v_instance_code
30056       AND     NVL(mss1.planning_partner_site_id,NULL_VALUE) = NULL_VALUE
30057       AND     ORDER_TYPE=18;
30058 
30059       UPDATE  MSC_ST_SUPPLIES
30060        SET OWNING_PARTNER_SITE_ID = (SELECT local_id
30061                                       FROM msc_local_id_setup
30062                                       WHERE  char1 = sr_instance_code
30063                                       AND    NVL(char2,NULL_CHAR) = NVL(company_name,NULL_CHAR)
30064                                       AND    char4            = OWNING_PARTNER_SITE_CODE
30065                                       AND    number1          = G_VENDOR
30066                                       AND    entity_name      = 'SR_TP_SITE_ID' )
30067       WHERE  sr_instance_code = v_instance_code
30068        AND    NVL(OWNING_PARTNER_SITE_ID,NULL_VALUE) = NULL_VALUE
30069       AND    NVL(batch_id,NULL_VALUE)= p_batch_id
30070       AND    process_flag     = G_IN_PROCESS
30071       AND    OWNING_TP_TYPE = 1
30072       AND    ORDER_TYPE=18;
30073 
30074       UPDATE  MSC_ST_SUPPLIES
30075        SET OWNING_PARTNER_SITE_ID = (SELECT local_id
30076                                       FROM msc_local_id_setup
30077                                       WHERE  char1 = sr_instance_code
30078                                       AND    char3            = OWNING_PARTNER_SITE_CODE
30079                                       AND    number1          = G_ORGANIZATION
30080                                       AND    entity_name      = 'SR_TP_ID')
30081       WHERE  sr_instance_code = v_instance_code
30082        AND    NVL(OWNING_PARTNER_SITE_ID,NULL_VALUE) = NULL_VALUE
30083       AND    NVL(batch_id,NULL_VALUE)= p_batch_id
30084       AND    process_flag     = G_IN_PROCESS
30085       AND    OWNING_TP_TYPE = 2
30086       AND    ORDER_TYPE=18;
30087 
30088         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30089                      (p_app_short_name    => 'MSC',
30090                       p_error_code        => 'MSC_PP_INVALID_VALUE',
30091                       p_message_text      => lv_message_text,
30092                       p_error_text        => lv_error_text,
30093                       p_token1            => 'COLUMN_NAME',
30094                       p_token_value1      => 'OWNING_PARTNER_SITE_CODE');
30095 
30096       IF lv_return <> 0 THEN
30097         RAISE ex_logging_err;
30098       END IF;
30099 
30100       UPDATE  MSC_ST_SUPPLIES mss1
30101       SET     process_flag = G_ERROR_FLG,
30102               error_text   = lv_message_text
30103       WHERE   EXISTS( SELECT 1
30104                       FROM   MSC_ST_SUPPLIES mss2
30105                       WHERE  mss2.sr_instance_code  = mss1.sr_instance_code
30106                       AND    mss2.process_flag      = G_IN_PROCESS
30107                       AND    NVL(mss2.owning_partner_site_id,NULL_VALUE) = NULL_VALUE
30108                       GROUP BY sr_instance_code HAVING COUNT(*) > 0)
30109       AND     mss1.process_flag             = G_IN_PROCESS
30110       AND     mss1.sr_instance_code            = v_instance_code
30111       AND     NVL(mss1.owning_partner_site_id,NULL_VALUE) = NULL_VALUE
30112       AND     ORDER_TYPE =18 ;
30113 
30114     END IF ;
30115 
30116 
30117 
30118     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30119                    (p_app_short_name    => 'MSC',
30120                     p_error_code        => 'MSC_PP_INVALID_VALUE',
30121                     p_message_text      => lv_message_text,
30122                     p_error_text        => lv_error_text,
30123                     p_token1            => 'COLUMN_NAME',
30124                     p_token_value1      => 'ITEM_NAME');
30125 
30126     IF lv_return <> 0 THEN
30127       RAISE ex_logging_err;
30128     END IF;
30129 
30130     --Derive Inventory_item_id
30131     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
30132                    (p_table_name       => 'MSC_ST_SUPPLIES',
30133                     p_item_col_name    => 'ITEM_NAME',
30134                     p_item_col_id      => 'INVENTORY_ITEM_ID',
30135                     p_instance_id      => v_instance_id,
30136                     p_instance_code    => v_instance_code,
30137                     p_error_text       => lv_error_text,
30138                     p_batch_id         => p_batch_id,
30139                     p_severity         => G_SEV_ERROR,
30140                     p_message_text     => lv_message_text,
30141                     p_debug            => v_debug,
30142                     p_row              => lv_column_names);
30143 
30144     IF lv_return <> 0 THEN
30145         RAISE ex_logging_err;
30146     END IF;
30147 
30148     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30149                    (p_app_short_name    => 'MSC',
30150                     p_error_code        => 'MSC_PP_INVALID_VALUE',
30151                     p_message_text      => lv_message_text,
30152                     p_error_text        => lv_error_text,
30153                     p_token1            => 'COLUMN_NAME',
30154                     p_token_value1      => 'SUPPLIER_NAME');
30155 
30156     IF lv_return <> 0 THEN
30157       RAISE ex_logging_err;
30158     END IF;
30159 
30160     --Derive Supplier_id
30161     lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
30162                    (p_table_name       => 'MSC_ST_SUPPLIES',
30163                     p_org_partner_name => 'SUPPLIER_NAME',
30164                     p_org_partner_id   => 'SUPPLIER_ID',
30165                     p_instance_code    => v_instance_code,
30166                     p_partner_type     => G_VENDOR,
30167                     p_error_text       => lv_error_text,
30168                     p_batch_id         => p_batch_id,
30169                     p_severity         => G_SEV3_ERROR,
30170                     p_message_text     => lv_message_text,
30171                     p_debug            => v_debug,
30172                     p_row              => lv_column_names);
30173 
30174     IF lv_return <> 0 THEN
30175       RAISE ex_logging_err;
30176     END IF;
30177 
30178 
30179     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30180                    (p_app_short_name    => 'MSC',
30181                     p_error_code        => 'MSC_PP_INVALID_VALUE',
30182                     p_message_text      => lv_message_text,
30183                     p_error_text        => lv_error_text,
30184                     p_token1            => 'COLUMN_NAME',
30185                     p_token_value1      => 'SUPPLIER_SITE_CODE');
30186 
30187     IF lv_return <> 0 THEN
30188       RAISE ex_logging_err;
30189     END IF;
30190 
30191     --Derive Supplier_site_id
30192     lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
30193                    (p_table_name        => 'MSC_ST_SUPPLIES',
30194                     p_partner_name      => 'SUPPLIER_NAME',
30195                     p_partner_site_code => 'SUPPLIER_SITE_CODE',
30196                     p_partner_site_id   => 'SUPPLIER_SITE_ID',
30197                     p_instance_code     => v_instance_code,
30198                     p_partner_type      => G_VENDOR,
30199                     p_error_text        => lv_error_text,
30200                     p_batch_id          => p_batch_id,
30201                     p_severity          => G_SEV3_ERROR,
30202                     p_message_text      => lv_message_text,
30203                     p_debug             => v_debug,
30204                     p_row               => lv_column_names);
30205 
30206     IF lv_return <> 0 THEN
30207       RAISE ex_logging_err;
30208     END IF;
30209 
30210     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30211                    (p_app_short_name    => 'MSC',
30212                     p_error_code        => 'MSC_PP_COL_VAL_NULL',
30213                     p_message_text      => lv_message_text,
30214                     p_error_text        => lv_error_text,
30215                     p_token1            => 'COLUMN_NAME',
30216                     p_token_value1      => 'ORDER_NUMBER OR '
30217                                            ||' PURCH_LINE_NUM OR '
30218                                            ||' SCHEDULE_DESIGNATOR OR '
30219                                            ||' SCHEDULE_LINE_NUM OR '
30220                                            ||' NEW_SCHEDULE_DATE OR '
30221                                            ||' NEW_ORDER_QUANTITY');
30222 
30223     IF lv_return <> 0 THEN
30224       RAISE ex_logging_err;
30225     END IF;
30226 
30227     v_sql_stmt := 10;
30228     lv_sql_stmt :=
30229     'UPDATE   msc_st_supplies '
30230     ||' SET   error_text   = '||''''||lv_message_text||''''||','
30231     ||'       process_flag  = '||G_ERROR_FLG
30232     ||' WHERE (((NVL(order_number,       '||''''||NULL_CHAR||''''||') '
30233     ||'       =                          '||''''||NULL_CHAR||''''
30234     ||' OR    NVL(purch_line_num, '||NULL_VALUE|| ') = '||NULL_VALUE||')'
30235     ||' AND   order_type       IN(1,2,8,11,12,73,74,87))'
30236     ||' OR    ((NVL(schedule_designator, '||''''||NULL_CHAR||''''||') '
30237     ||'       =                          '||''''||NULL_CHAR||''''
30238     ||' OR    NVL(schedule_line_num ,    '||''''||NULL_CHAR||''''||') '
30239     ||'       =                          '||''''||NULL_CHAR||''''||')'
30240     ||' AND   ORDER_TYPE  = 5)'
30241 --bug 2773881 (ssurendr) new_schedule_date will not be provided for PO,ONHAND (bug 2903052)
30242     ||' OR    ((NVL(new_schedule_date,SYSDATE-36500 ) = SYSDATE-36500'
30243     ||' AND   ORDER_TYPE  NOT IN (1,18,74))'
30244     ||' OR    NVL(new_order_quantity,'||NULL_VALUE||') = '||NULL_VALUE||')'
30245     ||' AND   deleted_flag                   = '||SYS_NO||')'
30246     ||' AND   process_flag                   = '||G_IN_PROCESS
30247     ||' AND   NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
30248     ||' AND   sr_instance_code               = :v_instance_code';
30249 
30250 
30251       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30252 
30253     EXECUTE IMMEDIATE lv_sql_stmt
30254             USING     p_batch_id,
30255                       v_instance_code;
30256 
30257 
30258    -- bug 2903356
30259 
30260    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30261                    (p_app_short_name    => 'MSC',
30262                     p_error_code        => 'MSC_DOCK_INVALID_VAL',
30263                     p_message_text      => lv_message_text,
30264                     p_error_text        => lv_error_text,
30265                     p_token1            => 'COLUMN_NAME1',
30266                     p_token_value1      => 'NEW_SCHEDULE_DATE',
30267                     p_token2            => 'COLUMN_NAME2',
30268                     p_token_value2      => 'NEW_DOCK_DATE');
30269 
30270     IF lv_return <> 0 THEN
30271       RAISE ex_logging_err;
30272     END IF;
30273 
30274     --If NEW_DOCK_DATE is greater than NEW_SCHEDULE_DATE we will raise an error
30275 -- start
30276     lv_where_str :=
30277     ' AND NVL(new_dock_date,SYSDATE-36500) > NVL(new_schedule_date,SYSDATE-36500)'
30278     ||' AND   deleted_flag        ='||SYS_NO
30279     ||' AND   order_type          IN(2,11,12,73,87)'
30280     ||' AND   process_flag        = '||G_IN_PROCESS;
30281 
30282     lv_return := MSC_ST_UTIL.LOG_ERROR
30283                    (p_table_name        => 'MSC_ST_SUPPLIES',
30284                     p_instance_code     => v_instance_code,
30285                     p_row               => lv_column_names,
30286                     p_severity          => G_SEV_WARNING,
30287                     p_message_text      => lv_message_text,
30288                     p_error_text        => lv_error_text,
30289                     p_batch_id          => p_batch_id,
30290                     p_where_str         => lv_where_str);
30291 
30292     IF lv_return <> 0 THEN
30293         RAISE ex_logging_err;
30294     END IF;
30295 
30296 -- end
30297 
30298 /*
30299     lv_sql_stmt :=
30300     'UPDATE   msc_st_supplies '
30301     ||' SET   process_flag        = '||G_ERROR_FLG||','
30302     ||'       error_text   = '||''''||lv_message_text||''''
30303     ||' WHERE NVL(new_dock_date,SYSDATE-36500) > NVL(new_schedule_date,SYSDATE-36500)'
30304     ||' AND   deleted_flag        ='||SYS_NO
30305     ||' AND   order_type          IN(2,11,12,73,87)'
30306     ||' AND   process_flag        = '||G_IN_PROCESS
30307     ||' AND   NVL(batch_id,'||NULL_VALUE||')= :p_batch_id '
30308     ||' AND   sr_instance_code    =:v_instance_code';
30309 
30310 
30311       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30312 
30313     EXECUTE IMMEDIATE lv_sql_stmt
30314             USING     p_batch_id,
30315                       v_instance_code;
30316 */
30317 
30318     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30319                    (p_app_short_name    => 'MSC',
30320                     p_error_code        => 'MSC_PP_POS_NON_ZERO',
30321                     p_message_text      => lv_message_text,
30322                     p_error_text        => lv_error_text,
30323                     p_token1            => 'COLUMN_NAME',
30324                     p_token_value1      => 'NEW_ORDER_QUANTITY');
30325 
30326     IF lv_return <> 0 THEN
30327       RAISE ex_logging_err;
30328     END IF;
30329 
30330     --Negative quantities are allowed only for the order type On-Hand(18)
30331     v_sql_stmt := 11;
30332     lv_sql_stmt :=
30333     'UPDATE    msc_st_supplies'
30334     ||' SET    process_flag                   = '||G_ERROR_FLG||','
30335     ||'        error_text   = '||''''||lv_message_text||''''
30336     ||' WHERE  new_order_quantity            <= 0'
30337     ||' AND    order_type                    <> 18'
30338     ||' AND    process_flag                   = '||G_IN_PROCESS
30339     ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
30340     ||' AND    sr_instance_code               = :v_instance_code';
30341 
30342 
30343       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30344 
30345     EXECUTE IMMEDIATE lv_sql_stmt
30346             USING     p_batch_id,
30347                       v_instance_code;
30348 
30349 
30350     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30351                    (p_app_short_name    => 'MSC',
30352                     p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
30353                     p_message_text      => lv_message_text,
30354                     p_error_text        => lv_error_text,
30355                     p_token1            => 'COLUMN_NAMES',
30356                     p_token_value1      => 'SR_INSTANCE_CODE,'
30357                                            ||' COMPANY_NAME,'
30358                                            ||' ORGANIZATION_CODE,'
30359                                            ||' DESIGNATOR_TYPE AND'
30360                                            ||' DESIGNATOR',
30361                     p_token2            => 'MASTER_TABLE',
30362                     p_token_value2      => 'MSC_ST_DESIGNATORS',
30363                     p_token3            => 'CHILD_TABLE',
30364                     p_token_value3      => 'MSC_ST_SUPPLIES');
30365 
30366     IF lv_return <> 0 THEN
30367       RAISE ex_logging_err;
30368     END IF;
30369 
30370     v_sql_stmt := 12;
30371     lv_sql_stmt :=
30372     'UPDATE  msc_st_supplies mss'
30373     ||' SET  process_flag = '||G_ERROR_FLG||','
30374     ||'         error_text   = '||''''||lv_message_text||''''
30375     ||' WHERE NOT EXISTS(SELECT 1'
30376     ||'       FROM   msc_st_designators msd'
30377     ||'       WHERE  msd.designator        = mss.schedule_designator'
30378     ||'       AND    msd.organization_code = mss.organization_code'
30379     ||'       AND    NVL(msd.company_name,'||''''||NULL_CHAR||''''||') = '
30380     ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
30381     ||'       AND    msd.sr_instance_code  = mss.sr_instance_code'
30382     ||'       AND    msd.process_flag      = '||G_VALID
30383     ||'       AND    msd.designator_type   = 2'
30384     ||'       AND    NVL(msd.disable_date, SYSDATE+1) > SYSDATE'
30385     ||'       UNION'
30386     ||'       SELECT 1'
30387     ||'       FROM   msc_designators md'
30388     ||'       WHERE  md.designator      = mss.schedule_designator'
30389     ||'       AND    md.organization_id = mss.organization_id '
30390     ||'       AND    md.sr_instance_id  = :v_instance_id '
30391     ||'       AND    md.designator_type = 2'
30392     ||'       AND    NVL(md.disable_date, SYSDATE+1) > SYSDATE)'
30393     ||' AND order_type    = 5'
30394     ||' AND deleted_flag  = '||SYS_NO
30395     ||' AND process_flag  = '||G_IN_PROCESS
30396     ||' AND NVL(batch_id,'||NULL_VALUE||')= :p_batch_id'
30397     ||' AND sr_instance_code = :v_instance_code';
30398 
30399 
30400       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30401 
30402     EXECUTE IMMEDIATE lv_sql_stmt
30403             USING     v_instance_id,
30404                       p_batch_id,
30405                       v_instance_code;
30406 
30407     --Ship_to_party_name and ship_to_site_code validation is specific to
30408     --Supply chain exchange and the validation will be done only for PO's
30409 
30410     IF v_instance_type = G_SCE THEN
30411 
30412       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30413                    (p_app_short_name    => 'MSC',
30414                     p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
30415                     p_message_text      => lv_message_text,
30416                     p_error_text        => lv_error_text,
30417                     p_token1            => 'COLUMN_NAMES',
30418                     p_token_value1      => 'SHIP_TO_PARTY_NAME',
30419                     p_token2            => 'MASTER_TABLE',
30420                     p_token_value2      => 'MSC_ST_DESIGNATORS',
30421                     p_token3            => 'CHILD_TABLE',
30422                     p_token_value3      => 'MSC_ST_SUPPLIES');
30423 
30424       IF lv_return <> 0 THEN
30425         RAISE ex_logging_err;
30426       END IF;
30427 
30428       v_sql_stmt := 13;
30429       lv_sql_stmt :=
30430       'UPDATE    msc_st_supplies '
30431       ||' SET    process_flag        = '||G_ERROR_FLG||','
30432       ||'         error_text   = '||''''||lv_message_text||''''
30433       ||' WHERE  NOT EXISTS (SELECT 1'
30434       ||'        FROM  hz_parties '
30435       ||'        WHERE party_name = ship_to_party_name) '
30436       ||' AND    order_type       in (1,74)'
30437       ||' AND    process_flag     = '||G_IN_PROCESS
30438       ||' AND    NVL(batch_id,'||NULL_VALUE||')= :p_batch_id'
30439       ||' AND    sr_instance_code = :v_instance_code';
30440 
30441 
30442 
30443         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30444 
30445       EXECUTE IMMEDIATE lv_sql_stmt
30446               USING     p_batch_id,
30447                         v_instance_code;
30448 
30449 
30450       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30451                    (p_app_short_name    => 'MSC',
30452                     p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
30453                     p_message_text      => lv_message_text,
30454                     p_error_text        => lv_error_text,
30455                     p_token1            => 'COLUMN_NAMES',
30456                     p_token_value1      => 'SHIP_TO_PARTY_NAME AND'
30457                                            ||' SHIP_TO_SITE_CODE',
30458                     p_token2            => 'MASTER_TABLE',
30459                     p_token_value2      => 'MSC_ST_DESIGNATORS',
30460                     p_token3            => 'CHILD_TABLE',
30461                     p_token_value3      => 'MSC_ST_SUPPLIES');
30462 
30463       IF lv_return <> 0 THEN
30464         RAISE ex_logging_err;
30465       END IF;
30466 
30467       v_sql_stmt := 14;
30468       lv_sql_stmt :=
30469       'UPDATE    msc_st_supplies ms'
30470       ||' SET    process_flag        = '||G_ERROR_FLG||','
30471       ||'         error_text   = '||''''||lv_message_text||''''
30472       ||' WHERE  NOT EXISTS (SELECT 1'
30473       ||'        FROM  hz_parties hp,hz_party_sites hps'
30474       ||'        WHERE hp.party_name       = ms.ship_to_party_name '
30475       ||'        AND   hp.party_id         = hps.party_id '
30476       ||'        AND   hps.party_site_name = ms.ship_to_site_code )'
30477       ||' AND    order_type       in (1,74)'
30478       ||' AND    process_flag     = '||G_IN_PROCESS
30479       ||' AND    NVL(batch_id,'||NULL_VALUE||')= :p_batch_id'
30480       ||' AND    sr_instance_code = :v_instance_code';
30481 
30482 
30483 
30484         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30485 
30486       EXECUTE IMMEDIATE lv_sql_stmt
30487               USING     p_batch_id,
30488                         v_instance_code;
30489 
30490     END IF;
30491 
30492     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30493                    (p_app_short_name    => 'MSC',
30494                     p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
30495                     p_message_text      => lv_message_text,
30496                     p_error_text        => lv_error_text,
30497                     p_token1            => 'COLUMN_NAME',
30498                     p_token_value1      => 'FIRM_PLANNED_TYPE',
30499                     p_token2            => 'DEFAULT_VALUE',
30500                     p_token_value2      => SYS_NO);
30501 
30502     IF lv_return <> 0 THEN
30503       RAISE ex_logging_err;
30504     END IF;
30505 
30506     lv_where_str :=  ' AND NVL(firm_planned_type, '||NULL_VALUE||') NOT IN(1,2)'
30507                    ||' AND deleted_flag = '||SYS_NO;
30508 
30509     --Log a warning for those records where the firm_planned_type has a value other
30510     --NOT IN (1,2)
30511     lv_return := MSC_ST_UTIL.LOG_ERROR
30512                    (p_table_name        => 'MSC_ST_SUPPLIES',
30513                     p_instance_code     => v_instance_code,
30514                     p_row               => lv_column_names,
30515                     p_severity          => G_SEV_WARNING,
30516                     p_message_text      => lv_message_text,
30517                     p_error_text        => lv_error_text,
30518                     p_batch_id          => p_batch_id,
30519                     p_where_str         => lv_where_str,
30520                     p_col_name          => 'FIRM_PLANNED_TYPE',
30521                     p_debug             => v_debug,
30522                     p_default_value     => SYS_NO);
30523 
30524     IF lv_return <> 0 THEN
30525       RAISE ex_logging_err;
30526     END IF;
30527 
30528     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30529                    (p_app_short_name    => 'MSC',
30530                     p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
30531                     p_message_text      => lv_message_text,
30532                     p_error_text        => lv_error_text,
30533                     p_token1            => 'COLUMN_NAMES',
30534                     p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
30535                                            ||' ORGANIZATION_CODE AND PROJECT_NUMBER',
30536                     p_token2            => 'MASTER_TABLE',
30537                     p_token_value2      => 'MSC_ST_PROJECT_TASKS');
30538 
30539     IF lv_return <> 0 THEN
30540       RAISE ex_logging_err;
30541     END IF;
30542 
30543     --Derive Project Id.
30544     lv_return := MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
30545                            (p_table_name          => 'MSC_ST_SUPPLIES',
30546                             p_proj_col_name       => 'PROJECT_NUMBER',
30547                             p_proj_task_col_id    => 'PROJECT_ID',
30548                             p_instance_code       => v_instance_code,
30549                             p_entity_name         => 'PROJECT_ID',
30550                             p_error_text          => lv_error_text,
30551                             p_batch_id            => p_batch_id,
30552                             p_severity            => G_SEV_WARNING,
30553                             p_message_text        => lv_message_text,
30554                             p_debug               => v_debug,
30555                             p_row                 => lv_column_names);
30556     IF lv_return <> 0 THEN
30557       RAISE ex_logging_err;
30558     END IF;
30559 
30560     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30561                    (p_app_short_name    => 'MSC',
30562                     p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
30563                     p_message_text      => lv_message_text,
30564                     p_error_text        => lv_error_text,
30565                     p_token1            => 'COLUMN_NAMES',
30566                     p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
30567                                            ||' ORGANIZATION_CODE, PROJECT_NUMBER,'
30568                                            ||' TASK_NUMBER',
30569                     p_token2            => 'MASTER_TABLE',
30570                     p_token_value2      => 'MSC_ST_PROJECT_TASKS');
30571 
30572     IF lv_return <> 0 THEN
30573       RAISE ex_logging_err;
30574     END IF;
30575 
30576     --Derive Task Id.
30577     lv_return := MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
30578                            (p_table_name          => 'MSC_ST_SUPPLIES',
30579                             p_proj_col_name       => 'PROJECT_NUMBER',
30580                             p_proj_task_col_id    => 'TASK_ID',
30581                             p_instance_code       => v_instance_code,
30582                             p_entity_name         => 'TASK_ID',
30583                             p_error_text          => lv_error_text,
30584                             p_task_col_name       => 'TASK_NUMBER',
30585                             p_batch_id            => p_batch_id,
30586                             p_severity            => G_SEV_WARNING,
30587                             p_message_text        => lv_message_text,
30588                             p_debug               => v_debug,
30589                             p_row                 => lv_column_names);
30590 
30591     IF lv_return <> 0 THEN
30592       RAISE ex_logging_err;
30593     END IF;
30594 
30595     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30596                    (p_app_short_name    => 'MSC',
30597                     p_error_code        => 'MSC_PP_INVALID_VALUE',
30598                     p_message_text      => lv_message_text,
30599                     p_error_text        => lv_error_text,
30600                     p_token1            => 'COLUMN_NAME',
30601                     p_token_value1      => 'FROM_ORGANIZATION_CODE');
30602 
30603     IF lv_return <> 0 THEN
30604       RAISE ex_logging_err;
30605     END IF;
30606 
30607     --Derive FROM_ORGANIZATION_ID
30608     lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
30609                    (p_table_name       => 'MSC_ST_SUPPLIES',
30610                     p_org_partner_name => 'FROM_ORGANIZATION_CODE',
30611                     p_org_partner_id   => 'FROM_ORGANIZATION_ID',
30612                     p_instance_code    => v_instance_code,
30613                     p_partner_type     => G_ORGANIZATION,
30614                     p_error_text       => lv_error_text,
30615                     p_batch_id         => p_batch_id,
30616                     p_severity         => G_SEV3_ERROR,
30617                     p_message_text     => lv_message_text,
30618                     p_debug            => v_debug,
30619                     p_row              => lv_column_names);
30620 
30621     IF lv_return <> 0 THEN
30622       RAISE ex_logging_err;
30623     END IF;
30624 
30625     --Derive source_Organization_id and source_sr_instance_id,
30626     v_sql_stmt := 15;
30627     lv_sql_stmt :=
30628     'UPDATE msc_st_supplies mss'
30629     ||' SET source_organization_id = from_organization_id,'
30630     ||'     source_sr_instance_id  = (select instance_id'
30631     ||'     FROM   msc_apps_instances mai'
30632     ||'     WHERE  mai.instance_code = mss.source_sr_instance_code)'
30633     ||' WHERE   deleted_flag       ='|| SYS_NO
30634     ||' AND     process_flag       ='|| G_IN_PROCESS
30635     ||' AND     NVL(batch_id,'||NULL_VALUE||')= :p_batch_id'
30636     ||' AND     sr_instance_code   = :v_instance_code';
30637 
30638 
30639       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30640 
30641     EXECUTE IMMEDIATE lv_sql_stmt
30642             USING     p_batch_id,
30643                       v_instance_code;
30644 
30645     --Deriving disposition_id and po_line_id for the supply type PO, Purchase
30646     --Req. and intransit supplies
30647     v_sql_stmt := 16;
30648     lv_sql_stmt :=
30649     'UPDATE msc_st_supplies mss '
30650     ||' SET disposition_id = (SELECT local_id'
30651     ||'       FROM   msc_local_id_supply mls'
30652     ||'       WHERE  mls.char3       = mss.order_number'
30653     ||'       AND    mls.number1     = mss.order_type'
30654     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
30655     ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
30656     ||'       AND    mls.char1       = mss.sr_instance_code'
30657     ||'       AND    mls.entity_name = ''DISPOSITION_ID'' ),'
30658     ||'     po_line_id     = (SELECT local_id'
30659     ||'       FROM   msc_local_id_supply mls'
30660     ||'       WHERE  mls.number1  = mss.purch_line_num'
30661     ||'       AND    mls.number2  = mss.order_type'
30662     ||'       AND    mls.char3    = mss.order_number'
30663     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
30664     ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
30665     ||'       AND    mls.char1 = mss.sr_instance_code'
30666     ||'       AND    mls.entity_name = ''PO_LINE_ID'')'
30667     ||' WHERE  order_type  IN(1,2,8,11,12,73,74,87)'
30668     ||' AND    deleted_flag     = '||SYS_NO
30669     ||' AND    process_flag     = '||G_IN_PROCESS
30670     ||' AND    NVL(batch_id,'||NULL_VALUE||')= :p_batch_id'
30671     ||' AND    sr_instance_code = :v_instance_code';
30672 
30673 
30674       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30675 
30676     EXECUTE IMMEDIATE lv_sql_stmt
30677             USING     p_batch_id,
30678                       v_instance_code;
30679 
30680     v_sql_stmt := 17;
30681     lv_sql_stmt :=
30682     'UPDATE msc_st_supplies mss'
30683     ||' SET sr_mtl_supply_id = (SELECT local_id'
30684     ||'       FROM   msc_local_id_supply mls'
30685     ||'       WHERE  mls.char4    = mss.order_number'
30686     ||'       AND    mls.number1  = mss.purch_line_num'
30687     ||'       AND    mls.number2  = mss.order_type'
30688     ||'       AND    mls.char3    = mss.organization_code'
30689     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
30690     ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
30691     ||'       AND    mls.char1    = mss.sr_instance_code'
30692     ||'       AND    mls.entity_name = ''SR_MTL_SUPPLY_ID'' )'
30693     ||' WHERE order_type      IN(1,2,8,11,12,73,74,87)'
30694     ||' AND   process_flag    = '||G_IN_PROCESS
30695     ||' AND   NVL(batch_id,'||NULL_VALUE||')= :p_batch_id'
30696     ||' AND   sr_instance_code = :v_instance_code';
30697 
30698 
30699       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30700 
30701     EXECUTE IMMEDIATE lv_sql_stmt
30702             USING     p_batch_id,
30703                       v_instance_code;
30704 
30705     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
30706                    (p_app_short_name    => 'MSC',
30707                     p_error_code        => 'MSC_PP_DELETE_FAIL',
30708                     p_message_text      => lv_message_text,
30709                     p_error_text        => lv_error_text);
30710 
30711     IF lv_return <> 0 THEN
30712       RAISE ex_logging_err;
30713     END IF;
30714 
30715     --If the deleted_flag is yes and the sr_mtl_supply_id(unique key)
30716     --derivation fails for the order_types po, req and intransit, then
30717     --the record will be errored out.
30718     v_sql_stmt := 18;
30719 
30720     lv_sql_stmt :=
30721     'UPDATE   msc_st_supplies '
30722     ||' SET   process_flag        = '||G_ERROR_FLG||','
30723     ||'         error_text   = '||''''||lv_message_text||''''
30724     ||' WHERE NVL(sr_mtl_supply_id,'||NULL_VALUE||') = '||NULL_VALUE
30725     ||' AND   deleted_flag        ='||SYS_YES
30726     ||' AND   order_type          IN(1,2,8,11,12,73,74,87)'
30727     ||' AND   process_flag        = '||G_IN_PROCESS
30728     ||' AND   NVL(batch_id,'||NULL_VALUE||')= :p_batch_id '
30729     ||' AND   sr_instance_code    =:v_instance_code';
30730 
30731 
30732       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30733 
30734     EXECUTE IMMEDIATE lv_sql_stmt
30735             USING     p_batch_id,
30736                       v_instance_code;
30737 
30738     --Deriving disposition_id for the Plan Orders.
30739     v_sql_stmt := 19;
30740     lv_sql_stmt :=
30741     'UPDATE msc_st_supplies mss'
30742     ||' SET disposition_id = (SELECT local_id'
30743     ||'       FROM   msc_local_id_supply mls'
30744     ||'       WHERE  mls.char4       = mss.schedule_designator'
30745     ||'       AND    mls.char5       = mss.schedule_line_num'
30746     ||'       AND    mls.number1     = mss.order_type'
30747     ||'       AND    mls.char1       = mss.sr_instance_code'
30748     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
30749     ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
30750     ||'       AND    mls.char3       = mss.organization_code'
30751     ||'       AND    mls.entity_name = ''DISPOSITION_ID_MPS'')'
30752     ||' WHERE  order_type             = 5'
30753     ||' AND    process_flag           = '||G_IN_PROCESS
30754     ||' AND   NVL(batch_id,'||NULL_VALUE||')= :p_batch_id'
30755     ||' AND    sr_instance_code       = :v_instance_code';
30756 
30757 
30758       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30759 
30760     EXECUTE IMMEDIATE lv_sql_stmt
30761             USING     p_batch_id,
30762                       v_instance_code;
30763 
30764     --If the deleted_flag is yes and the disposition_id(unique key)
30765     --derivation fails for the plan orders then the record will be
30766     --errored out.
30767     v_sql_stmt := 20;
30768 
30769     lv_sql_stmt :=
30770     'UPDATE   msc_st_supplies '
30771     ||' SET   process_flag        = '||G_ERROR_FLG||','
30772     ||'         error_text   = '||''''||lv_message_text||''''
30773     ||' WHERE NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
30774     ||' AND   deleted_flag        ='||SYS_YES
30775     ||' AND   order_type          = 5'
30776     ||' AND   process_flag        = '||G_IN_PROCESS
30777     ||' AND   NVL(batch_id,'||NULL_VALUE||')= :p_batch_id '
30778     ||' AND   sr_instance_code    =:v_instance_code';
30779 
30780 
30781       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
30782 
30783     EXECUTE IMMEDIATE lv_sql_stmt
30784             USING     p_batch_id,
30785                       v_instance_code;
30786 
30787     --Call to customised validation.
30788     MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
30789       (ERRBUF         => lv_error_text,
30790        RETCODE        => lv_return,
30791        pBatchID       => p_batch_id,
30792        pInstanceCode  => v_instance_code,
30793        pEntityName    => 'MSC_ST_SUPPLIES',
30794        pInstanceID    => v_instance_id);
30795 
30796     IF NVL(lv_return,0) <> 0 THEN
30797       RAISE ex_logging_err;
30798     END IF;
30799 
30800 
30801     --Generation of sr_mtl_supply_id
30802     OPEN  c2(p_batch_id);
30803     FETCH c2 BULK COLLECT INTO lb_rowid ;
30804 
30805     IF c2%ROWCOUNT > 0  THEN
30806 
30807       v_sql_stmt := 22;
30808       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
30809       UPDATE msc_st_supplies
30810       SET    sr_mtl_supply_id = msc_st_sr_mtl_supply_id_s.NEXTVAL
30811       WHERE  rowid            = lb_rowid(j);
30812 
30813       v_sql_stmt := 23;
30814       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
30815       INSERT INTO msc_local_id_supply
30816        (local_id,
30817         st_transaction_id,
30818         instance_id,
30819         entity_name,
30820         data_source_type,
30821         char1,
30822         char2,
30823         char3,
30824         char4,
30825         number1,
30826         number2,
30827         SOURCE_ORG_ID,
30828         SOURCE_INVENTORY_ITEM_ID,
30829         SOURCE_VENDOR_ID,
30830         SOURCE_VENDOR_SITE_ID,
30831         SOURCE_TASK_ID,
30832         SOURCE_FROM_ORGANIZATION_ID,
30833         SOURCE_SR_MTL_SUPPLY_ID,
30834         SOURCE_DISPOSITION_ID,
30835         SOURCE_PROJECT_ID,
30836         last_update_date,
30837         last_updated_by,
30838         creation_date,
30839         created_by)
30840       SELECT
30841         sr_mtl_supply_id,
30842         st_transaction_id,
30843         v_instance_id,
30844         'SR_MTL_SUPPLY_ID',
30845         data_source_type,
30846         v_instance_code,
30847         company_name,
30848         organization_code ,
30849         order_number,
30850         purch_line_num,
30851         order_type,
30852         SOURCE_ORG_ID,
30853         SOURCE_INVENTORY_ITEM_ID,
30854         SOURCE_VENDOR_ID,
30855         SOURCE_VENDOR_SITE_ID,
30856         SOURCE_TASK_ID,
30857         SOURCE_FROM_ORGANIZATION_ID,
30858         SOURCE_SR_MTL_SUPPLY_ID,
30859         SOURCE_DISPOSITION_ID,
30860         SOURCE_PROJECT_ID,
30861         v_current_date,
30862         v_current_user,
30863         v_current_date,
30864         v_current_user
30865       FROM  msc_st_supplies
30866       WHERE rowid = lb_rowid(j);
30867     END IF;
30868     CLOSE c2 ;
30869 
30870 
30871     --Generation of po_line_id
30872     OPEN c4(p_batch_id);
30873     FETCH c4 BULK COLLECT INTO lb_rowid ;
30874 
30875     IF c4%ROWCOUNT > 0  THEN
30876       v_sql_stmt := 26;
30877       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
30878       UPDATE msc_st_supplies
30879       SET    po_line_id = msc_st_po_line_id_s.NEXTVAL
30880       WHERE  rowid      = lb_rowid(j);
30881 
30882       v_sql_stmt := 27;
30883       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
30884       INSERT INTO msc_local_id_supply
30885         (local_id,
30886          st_transaction_id,
30887          instance_id,
30888          entity_name,
30889          data_source_type,
30890          char1,
30891          char2,
30892          char3,
30893          number1,
30894          number2,
30895          SOURCE_ORG_ID,
30896          SOURCE_INVENTORY_ITEM_ID,
30897          SOURCE_VENDOR_ID,
30898          SOURCE_VENDOR_SITE_ID,
30899          SOURCE_TASK_ID,
30900          SOURCE_FROM_ORGANIZATION_ID,
30901          SOURCE_SR_MTL_SUPPLY_ID,
30902          SOURCE_DISPOSITION_ID,
30903          SOURCE_PROJECT_ID,
30904          last_update_date,
30905          last_updated_by,
30906          creation_date,
30907          created_by)
30908       SELECT
30909          po_line_id,
30910          st_transaction_id,
30911          v_instance_id,
30912          'PO_LINE_ID',
30913          data_source_type,
30914          v_instance_code,
30915          company_name,
30916          order_number,
30917          purch_line_num,
30918          order_type,
30919          SOURCE_ORG_ID,
30920          SOURCE_INVENTORY_ITEM_ID,
30921          SOURCE_VENDOR_ID,
30922          SOURCE_VENDOR_SITE_ID,
30923          SOURCE_TASK_ID,
30924          SOURCE_FROM_ORGANIZATION_ID,
30925          SOURCE_SR_MTL_SUPPLY_ID,
30926          SOURCE_DISPOSITION_ID,
30927          SOURCE_PROJECT_ID,
30928          v_current_date,
30929          v_current_user,
30930          v_current_date,
30931          v_current_user
30932       FROM  msc_st_supplies
30933       WHERE rowid  = lb_rowid(j);
30934 
30935     END IF;
30936     CLOSE c4 ;
30937 
30938     --Generation of disposition_id(MPS)
30939     OPEN c5(p_batch_id);
30940     FETCH c5 BULK COLLECT INTO lb_rowid ;
30941 
30942     IF c5%ROWCOUNT > 0  THEN
30943       v_sql_stmt := 28;
30944       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
30945       UPDATE msc_st_supplies
30946       SET    disposition_id  = msc_st_disposition_id_s.NEXTVAL
30947       WHERE  rowid           = lb_rowid(j);
30948 
30949       v_sql_stmt := 29;
30950       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
30951       INSERT INTO msc_local_id_supply
30952         (local_id,
30953          st_transaction_id,
30954          instance_id,
30955          entity_name,
30956          data_source_type,
30957          char1,
30958          char2,
30959          char3,
30960          char4,
30961          char5,
30962          number1,
30963          SOURCE_ORG_ID,
30964          SOURCE_INVENTORY_ITEM_ID,
30965          SOURCE_VENDOR_ID,
30966          SOURCE_VENDOR_SITE_ID,
30967          SOURCE_TASK_ID,
30968          SOURCE_FROM_ORGANIZATION_ID,
30969          SOURCE_SR_MTL_SUPPLY_ID,
30970          SOURCE_DISPOSITION_ID,
30971          SOURCE_PROJECT_ID,
30972          last_update_date,
30973          last_updated_by,
30974          creation_date,
30975          created_by)
30976       SELECT
30977          disposition_id,
30978          st_transaction_id,
30979          v_instance_id,
30980          'DISPOSITION_ID_MPS',
30981          data_source_type,
30982          v_instance_code,
30983          company_name,
30984          organization_code,
30985          schedule_designator,
30986          schedule_line_num,
30987          order_type,
30988          SOURCE_ORG_ID,
30989          SOURCE_INVENTORY_ITEM_ID,
30990          SOURCE_VENDOR_ID,
30991          SOURCE_VENDOR_SITE_ID,
30992          SOURCE_TASK_ID,
30993          SOURCE_FROM_ORGANIZATION_ID,
30994          SOURCE_SR_MTL_SUPPLY_ID,
30995          SOURCE_DISPOSITION_ID,
30996          SOURCE_PROJECT_ID,
30997          v_current_date,
30998          v_current_user,
30999          v_current_date,
31000          v_current_user
31001       FROM  msc_st_supplies
31002       WHERE rowid = lb_rowid(j);
31003 
31004       END IF;
31005       CLOSE c5 ;
31006 
31007 
31008     --Updating line_id using disposition_id for the Plan Orders.
31009     v_sql_stmt := 30;
31010     lv_sql_stmt :=
31011     'UPDATE msc_st_supplies mss'
31012     ||' SET line_id = disposition_id'
31013     ||' WHERE  order_type             = 5'
31014     ||' AND    process_flag           = '||G_IN_PROCESS
31015     ||' AND   NVL(batch_id,'||NULL_VALUE||')= :p_batch_id'
31016     ||' AND    sr_instance_code       = :v_instance_code';
31017 
31018 
31019       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31020 
31021     EXECUTE IMMEDIATE lv_sql_stmt
31022             USING     p_batch_id,
31023                       v_instance_code;
31024 
31025     v_sql_stmt := 31;
31026     lv_sql_stmt :=
31027     'UPDATE msc_st_supplies mss '
31028     ||' SET disposition_id = (SELECT local_id'
31029     ||'       FROM   msc_local_id_supply mls'
31030     ||'       WHERE  mls.char3       = mss.order_number'
31031     ||'       AND    mls.number1     = mss.order_type'
31032     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
31033     ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
31034     ||'       AND    mls.char1       = mss.sr_instance_code'
31035     ||'       AND    mls.entity_name = ''DISPOSITION_ID'' ) '
31036     ||' WHERE  order_type  IN(1,2,8,11,12,73,74,87)'
31037     ||' AND    NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
31038     ||' AND    deleted_flag                   = '||SYS_NO
31039     ||' AND    process_flag                   = '||G_IN_PROCESS
31040     ||' AND    NVL(batch_id,'||NULL_VALUE||') = :p_batch_id'
31041     ||' AND    sr_instance_code               = :v_instance_code';
31042 
31043 
31044       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31045 
31046     EXECUTE IMMEDIATE lv_sql_stmt
31047             USING     p_batch_id,
31048                       v_instance_code;
31049 
31050 
31051     -- Bug 2821525
31052     -- Updating vmi_flag with 1 for Purchase orders ,ASN (Intransit shipments) ,
31053     -- Shipment Receipts and Requisitions if Item has record of approved supplier list
31054     -- in msc_item_suppliers and that particular ASL is VMI enabled
31055 
31056     v_sql_stmt := 32;
31057     lv_sql_stmt :=
31058       ' UPDATE msc_st_supplies mss '
31059     ||' SET vmi_flag = ''Y'''
31060     ||' WHERE   exists (SELECT 1 FROM MSC_TP_ID_LID mtil,'
31061     ||'                 MSC_TP_SITE_ID_LID mtsil,'
31062     ||'                 MSC_ITEM_ID_LID t1,'
31063     ||'                 MSC_ITEM_SUPPLIERS mis'
31064     ||'                 WHERE mis.organization_id = mss.organization_id '
31065     ||'                 AND t1.SR_INVENTORY_ITEM_ID= mss.inventory_item_id'
31066     ||'                 AND t1.sr_instance_id= :v_instance_id'
31067     ||'                 AND t1.INVENTORY_ITEM_ID =mis.INVENTORY_ITEM_ID'
31068     ||'                 AND mtil.SR_TP_ID = mss.SUPPLIER_ID'
31069     ||'                 AND mtil.SR_INSTANCE_ID = :v_instance_id'
31070     ||'                 AND mtil.PARTNER_TYPE = 1'
31071     ||'                 AND nvl(mtil.sr_company_id, -1) = nvl(mss.company_id, -1)'
31072     ||'                 AND mtil.TP_ID = mis.SUPPLIER_ID'
31073     ||'                 AND mis.SR_INSTANCE_ID = :v_instance_id'
31074     ||'                 AND mis.plan_id = -1 '
31075     ||'                 AND mtsil.SR_TP_SITE_ID = mss.Supplier_Site_ID'
31076     ||'                 AND mtsil.SR_INSTANCE_ID= :v_instance_id'
31077     ||'                 AND mtsil.Partner_Type = 1'
31078     ||'                 AND nvl(mtsil.sr_company_id, -1) = nvl(mss.company_id, -1)'
31079     ||'                 AND mss.Supplier_Site_ID is NOT NULL '
31080     ||'                 AND mis.SUPPLIER_SITE_ID is NOT NULL'
31081     ||'                 AND mtsil.TP_SITE_ID = mis.SUPPLIER_SITE_ID'
31082     ||'                 AND mis.VMI_FLAG           = 1'
31083     ||'                 UNION ALL '
31084     ||'                 SELECT 1 FROM MSC_TP_ID_LID mtil2,'
31085     ||'                 MSC_TP_SITE_ID_LID mtsil2,'
31086     ||'                 MSC_ITEM_ID_LID t2,'
31087     ||'                 MSC_ITEM_SUPPLIERS mis2'
31088     ||'                 WHERE mis2.organization_id = mss.organization_id '
31089     ||'                 AND t2.SR_INVENTORY_ITEM_ID= mss.inventory_item_id'
31090     ||'                 AND t2.sr_instance_id= :v_instance_id'
31091     ||'                 AND t2.INVENTORY_ITEM_ID =mis2.INVENTORY_ITEM_ID'
31092     ||'                 AND mtil2.SR_TP_ID = mss.SUPPLIER_ID'
31093     ||'                 AND mtil2.SR_INSTANCE_ID = :v_instance_id'
31094     ||'                 AND mtil2.PARTNER_TYPE = 1'
31095     ||'                 AND nvl(mtil2.sr_company_id, -1) = nvl(mss.company_id, -1)'
31096     ||'                 AND mtil2.TP_ID = mis2.SUPPLIER_ID'
31097     ||'                 AND mis2.SR_INSTANCE_ID = :v_instance_id'
31098     ||'                 AND mis2.plan_id = -1 '
31099     ||'                 AND mtsil2.SR_TP_SITE_ID = mss.Supplier_Site_ID'
31100     ||'                 AND mtsil2.SR_INSTANCE_ID= :v_instance_id'
31101     ||'                 AND mtsil2.Partner_Type = 1'
31102     ||'                 AND nvl(mtsil2.sr_company_id, -1) = nvl(mss.company_id, -1)'
31103     ||'                 AND mss.Supplier_Site_ID is NOT NULL '
31104     ||'                 AND mis2.SUPPLIER_SITE_ID is NULL'
31105     ||'                 AND mis2.VMI_FLAG           = 1 '
31106     ||'                 AND not exists ( SELECT 1 from MSC_ITEM_SUPPLIERS mis3'
31107     ||'                                  WHERE mtsil2.TP_SITE_ID = mis3.SUPPLIER_SITE_ID'
31108     ||'                                  AND mis3.organization_id = mss.organization_id '
31109     ||'                                  AND t2.SR_INVENTORY_ITEM_ID= mss.inventory_item_id'
31110     ||'                                  AND t2.sr_instance_id= :v_instance_id'
31111     ||'                                  AND t2.INVENTORY_ITEM_ID =mis3.INVENTORY_ITEM_ID'
31112     ||'                                  AND mtil2.SR_TP_ID = mss.SUPPLIER_ID'
31113     ||'                                  AND mtil2.SR_INSTANCE_ID = :v_instance_id'
31114     ||'                                  AND mtil2.PARTNER_TYPE = 1'
31115     ||'                                  AND nvl(mtil2.sr_company_id, -1) = nvl(mss.company_id, -1)'
31116     ||'                                  AND mtil2.TP_ID = mis3.SUPPLIER_ID '
31117     ||'                                  AND mis3.SR_INSTANCE_ID = :v_instance_id'
31118     ||'                                  AND mis3.plan_id = -1)'
31119     ||'                 UNION ALL '
31120     ||'                 SELECT 1 FROM MSC_TP_ID_LID mtil4,'
31121     ||'                 MSC_TP_SITE_ID_LID mtsil4,'
31122     ||'                 MSC_ITEM_ID_LID t4,'
31123     ||'                 MSC_ITEM_SUPPLIERS mis4'
31124     ||'                 WHERE mis4.organization_id = mss.organization_id '
31125     ||'                 AND t4.SR_INVENTORY_ITEM_ID= mss.inventory_item_id'
31126     ||'                 AND t4.sr_instance_id= :v_instance_id'
31127     ||'                 AND t4.INVENTORY_ITEM_ID =mis4.INVENTORY_ITEM_ID'
31128     ||'                 AND mtil4.SR_TP_ID = mss.SUPPLIER_ID'
31129     ||'                 AND mtil4.SR_INSTANCE_ID = :v_instance_id'
31130     ||'                 AND mtil4.PARTNER_TYPE = 1'
31131     ||'                 AND nvl(mtil4.sr_company_id, -1) = nvl(mss.company_id, -1)'
31132     ||'                 AND mtil4.TP_ID = mis4.SUPPLIER_ID'
31133     ||'                 AND mis4.SR_INSTANCE_ID = :v_instance_id'
31134     ||'                 AND mis4.plan_id = -1 '
31135     ||'                 AND mss.Supplier_Site_ID is NULL '
31136     ||'                 AND mis4.SUPPLIER_SITE_ID is NULL'
31137     ||'                 AND mis4.VMI_FLAG           = 1)'
31138     ||'  AND   order_type  IN (1,2,8,11,12,73,74,87) '
31139     ||'  AND   process_flag        = '||G_IN_PROCESS
31140     ||'  AND   NVL(batch_id,'||NULL_VALUE||')= :p_batch_id '
31141     ||'  AND   sr_instance_code    =:v_instance_code';
31142 
31143 
31144 
31145 
31146       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31147 
31148     EXECUTE IMMEDIATE lv_sql_stmt
31149             USING     v_instance_id, v_instance_id, v_instance_id, v_instance_id, v_instance_id,
31150                       v_instance_id, v_instance_id,v_instance_id, v_instance_id, v_instance_id,
31151                       v_instance_id, v_instance_id, v_instance_id, v_instance_id, p_batch_id,
31152                       v_instance_code;
31153 
31154 
31155   -- bug 2903052 defaulting the new_schedule_date as sysdate for onhand supply
31156 
31157     lv_sql_stmt :=
31158     'UPDATE msc_st_supplies mss'
31159     ||' SET new_schedule_date = SYSDATE'
31160     ||' WHERE   deleted_flag       ='|| SYS_NO
31161     ||' AND     process_flag       ='|| G_IN_PROCESS
31162     ||' AND     order_type         = 18'
31163     ||' AND     NVL(batch_id,'||NULL_VALUE||')= :p_batch_id'
31164     ||' AND     sr_instance_code   = :v_instance_code';
31165 
31166 
31167       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31168 
31169     EXECUTE IMMEDIATE lv_sql_stmt
31170             USING     p_batch_id,
31171                       v_instance_code;
31172 
31173 
31174 
31175 
31176     lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
31177                   (p_table_name     => 'MSC_ST_SUPPLIES',
31178                    p_instance_id    => v_instance_id,
31179                    p_instance_code  => v_instance_code,
31180                    p_process_flag   => G_VALID,
31181                    p_error_text     => lv_error_text,
31182                   p_debug           => v_debug,
31183                    p_batch_id       => p_batch_id);
31184 
31185     IF lv_return <> 0 THEN
31186       RAISE ex_logging_err;
31187     END IF;
31188 
31189     lv_return := MSC_ST_UTIL.LOG_ERROR
31190                    (p_table_name        => 'MSC_ST_SUPPLIES',
31191                     p_instance_code     => v_instance_code,
31192                     p_row               => lv_column_names,
31193                     p_severity          => G_SEV_ERROR,
31194                     p_message_text      => NULL,
31195                     p_error_text        => lv_error_text,
31196                     p_debug             => v_debug,
31197                     p_batch_id          => p_batch_id);
31198 
31199     IF lv_return <> 0 THEN
31200       RAISE ex_logging_err;
31201     END IF;
31202 
31203     COMMIT;
31204 
31205   EXCEPTION
31206     WHEN too_many_rows THEN
31207       lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_SUPPLY'||'('
31208                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
31209         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
31210       ROLLBACK;
31211       IF c_lock%ISOPEN THEN
31212         CLOSE c_lock;
31213       END IF;
31214 
31215     WHEN ex_logging_err THEN
31216         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
31217       ROLLBACK;
31218       IF c_lock%ISOPEN THEN
31219         CLOSE c_lock;
31220       END IF;
31221 
31222     WHEN OTHERS THEN
31223       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_SUPPLY'||'('
31224                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
31225         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
31226       ROLLBACK;
31227       IF c_lock%ISOPEN THEN
31228         CLOSE c_lock;
31229       END IF;
31230 
31231   END LOAD_SUPPLY;
31232 
31233 /*==========================================================================+
31234 | DESCRIPTION  : This procedure is called for validating demand related     |
31235 |                records from the msc_st_sales_orders table.                |
31236 +==========================================================================*/
31237   PROCEDURE  LOAD_SALES_ORDER (p_batch_id  IN NUMBER ) IS
31238   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
31239 
31240   lb_rowid          RowidTab;
31241 
31242   lv_column_names   VARCHAR2(5000);     -- Stores cocatenated column names
31243   lv_return         NUMBER;
31244   lv_error_text     VARCHAR2(250);
31245   lv_where_str      VARCHAR2(5000);
31246   lv_sql_stmt       VARCHAR2(5000);
31247   lv_cursor_stmt    VARCHAR2(5000);
31248   lv_message_text   msc_errors.error_text%TYPE;
31249   lv_batch_id       NUMBER;
31250 
31251   ex_logging_err    EXCEPTION;
31252   busy EXCEPTION;
31253   PRAGMA EXCEPTION_INIT(busy, -54);
31254 
31255   CURSOR c1(p_batch_id NUMBER) IS
31256     SELECT rowid
31257     FROM   msc_st_sales_orders
31258     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
31259     AND    batch_id                = p_batch_id
31260     AND    sr_instance_code        = v_instance_code;
31261 
31262   CURSOR c2(p_batch_id NUMBER) IS
31263     SELECT max(rowid)
31264     FROM   msc_st_sales_orders
31265     WHERE  process_flag     = G_IN_PROCESS
31266     AND    sr_instance_code = v_instance_code
31267     AND    batch_id         = p_batch_id
31268     AND    NVL(demand_source_header_id,NULL_VALUE) = NULL_VALUE
31269     AND    deleted_flag      = SYS_NO
31270     GROUP BY SR_INSTANCE_CODE,COMPANY_NAME,
31271              ORGANIZATION_CODE,SALES_ORDER_NUMBER;
31272 
31273   CURSOR c3(p_batch_id NUMBER) IS
31274     SELECT rowid
31275     FROM   msc_st_sales_orders
31276     WHERE  process_flag     = G_IN_PROCESS
31277     AND    sr_instance_code = v_instance_code
31278     AND    batch_id         = p_batch_id
31279     AND    NVL(demand_id,NULL_VALUE) = NULL_VALUE
31280     AND    deleted_flag      = SYS_NO;
31281 
31282   CURSOR c4(p_batch_id NUMBER) IS
31283     SELECT max(rowid)
31284     FROM   msc_st_sales_orders
31285     WHERE  process_flag     = G_IN_PROCESS
31286     AND    sr_instance_code = v_instance_code
31287     AND    batch_id         = p_batch_id
31288     AND    NVL(ship_set_id,NULL_VALUE) = NULL_VALUE
31289     AND    NVL(ship_set_name,NULL_CHAR) <> NULL_CHAR
31290     AND    deleted_flag      = SYS_NO
31291     GROUP BY SR_INSTANCE_CODE,COMPANY_NAME,
31292              ORGANIZATION_CODE,SALES_ORDER_NUMBER, SHIP_SET_NAME;
31293 
31294   CURSOR c5(p_batch_id NUMBER) IS
31295     SELECT max(rowid)
31296     FROM   msc_st_sales_orders
31297     WHERE  process_flag     = G_IN_PROCESS
31298     AND    sr_instance_code = v_instance_code
31299     AND    batch_id         = p_batch_id
31300     AND    NVL(arrival_set_id,NULL_VALUE) = NULL_VALUE
31301     AND    NVL(arrival_set_name,NULL_CHAR) <> NULL_CHAR
31302     AND    deleted_flag      = SYS_NO
31303     GROUP BY SR_INSTANCE_CODE,COMPANY_NAME,
31304              ORGANIZATION_CODE,SALES_ORDER_NUMBER, ARRIVAL_SET_NAME;
31305 
31306   CURSOR c_lock(p_batch_id NUMBER)  IS
31307     SELECT  'X'
31308     FROM    msc_st_sales_orders mso ,
31309             msc_st_sales_orders mcr
31310     WHERE   mso.batch_id           = p_batch_id
31311     AND     mso.batch_id          <>  nvl(mcr.batch_id,NULL_VALUE)
31312     AND     mso.demand_source_header_id IS NULL
31313     AND     mcr.demand_source_header_id IS NULL
31314     AND     NVL(mso.company_name,NULL_CHAR)= NVL(mcr.company_name,NULL_CHAR)
31315     AND     mso.organization_code  =  mcr.organization_code
31316     AND     mso.sales_order_number =  mcr.sales_order_number
31317     AND     mso.sr_instance_code   =  mcr.sr_instance_code
31318     AND     mso.batch_id           <> mcr.batch_id
31319     FOR UPDATE NOWAIT   ;
31320 
31321   BEGIN
31322 
31323   lv_column_names :=
31324   'ITEM_NAME                              ||''~''||'
31325   ||' ORGANIZATION_CODE                   ||''~''||'
31326   ||' PRIMARY_UOM_QUANTITY                ||''~''||'
31327   ||' COMPLETED_QUANTITY                  ||''~''||'
31328   ||' REQUIREMENT_DATE                    ||''~''||'
31329   ||' SALES_ORDER_NUMBER                  ||''~''||'
31330   ||' LINE_NUM                            ||''~''||'
31331   ||' SR_INSTANCE_CODE                    ||''~''||'
31332   ||' RESERVATION_TYPE                    ||''~''||'
31333   ||' RESERVATION_QUANTITY                ||''~''||'
31334   ||' SUBINVENTORY                        ||''~''||'
31335   ||' DEMAND_CLASS                        ||''~''||'
31336   ||' SALESREP_CONTACT                    ||''~''||'
31337   ||' ORDERED_ITEM_NAME                   ||''~''||'
31338   ||' AVAILABLE_TO_MRP                    ||''~''||'
31339   ||' CUSTOMER_NAME                       ||''~''||'
31340   ||' SHIP_TO_SITE_CODE                   ||''~''||'
31341   ||' BILL_TO_SITE_CODE                   ||''~''||'
31342   ||' PLANNING_GROUP                      ||''~''||'
31343   ||' DEMAND_PRIORITY                     ||''~''||'
31344   ||' REQUEST_DATE                        ||''~''||'
31345   ||' SELLING_PRICE                       ||''~''||'
31346   ||' DEMAND_VISIBLE                      ||''~''||'
31347   ||' FORECAST_VISIBLE                    ||''~''||'
31348   ||' CTO_FLAG                            ||''~''||'
31349   ||' ORIGINAL_SYSTEM_REFERENCE           ||''~''||'
31350   ||' ORIGINAL_SYSTEM_LINE_REFERENCE      ||''~''||'
31351   ||' COMPANY_NAME                         ||''~''||'
31352   ||' DELETED_FLAG                        ||''~''||'
31353   ||' PROJECT_NUMBER                      ||''~''||'
31354   ||' TASK_NUMBER                         ||''~''||'
31355   ||' DEMAND_SOURCE_TYPE';
31356 
31357     -- Derive demand source header id
31358 
31359     v_sql_stmt := 1.0;
31360 
31361 
31362     -- lock all the  sales order lines in this batch  that match the key .
31363     -- Vinay 15-NOV-2001.
31364 
31365     LOOP
31366         BEGIN
31367            OPEN c_lock(p_batch_id) ;
31368            EXIT;
31369         EXCEPTION
31370            WHEN busy THEN
31371              lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_SALES_ORDER'||'('||v_sql_stmt||')'||'p_batch_id :'||p_batch_id|| SQLERRM, 1, 240);
31372          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
31373              dbms_lock.sleep(5);
31374           WHEN OTHERS THEN
31375                  lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_SALES_ORDER'||'('||v_sql_stmt||')'||'p_batch_id :'||p_batch_id|| SQLERRM, 1, 240);
31376                   MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
31377 
31378        v_sql_stmt := 1.1;
31379        lv_sql_stmt :=
31380        'UPDATE msc_st_sales_orders '
31381        ||' SET process_flag = 1,'
31382        ||'     batch_id = NULL'
31383        ||' WHERE batch_id = :p_batch_id'
31384        ||' AND process_flag      = '||G_IN_PROCESS
31385        ||' AND sr_instance_code  =:v_instance_code';
31386 
31387 
31388          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31389 
31390       EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
31391       COMMIT;
31392 
31393       RETURN ;
31394       END;
31395     END LOOP;
31396 
31397 
31398     -- Generate demand_source_header_id
31399 
31400     v_sql_stmt := 2.0;
31401    lv_sql_stmt :=
31402    'UPDATE msc_st_sales_orders mso '
31403     ||'  SET  demand_source_header_id = (SELECT local_id'
31404     ||'       FROM   msc_local_id_demand mlid'
31405     ||'       WHERE  mlid.char1    = mso.sr_instance_code'
31406     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
31407     ||'       =     NVL(mso.company_name,'||''''||NULL_CHAR||''''||') '
31408     ||'       AND    mlid.char3    = mso.organization_code'
31409     ||'       AND    mlid.char4    = mso.sales_order_number'
31410     ||'       AND    mlid.instance_id = '||v_instance_id
31411     ||'       AND    mlid.entity_name = ''SALES_ORDER_ID'' )'
31412     ||' WHERE process_flag      = '||G_IN_PROCESS
31413     ||' AND   NVL(demand_source_header_id,'||NULL_VALUE||') = '||NULL_VALUE
31414     ||' AND   batch_id          = :p_batch_id '
31415     ||' AND   sr_instance_code  =:v_instance_code';
31416 
31417 
31418       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31419 
31420     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
31421     OPEN c2(p_batch_id);
31422     FETCH c2 BULK COLLECT INTO lb_rowid ;
31423 
31424     IF c2%ROWCOUNT > 0  THEN
31425       v_sql_stmt := 2.1;
31426       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
31427       UPDATE msc_st_sales_orders
31428       SET    demand_source_header_id  = msc_st_sales_order_id_s.NEXTVAL
31429       WHERE  rowid        = lb_rowid(j)
31430       AND demand_Source_header_id is null;
31431 
31432       v_sql_stmt := 2.2;
31433       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
31434       INSERT INTO msc_local_id_demand
31435         (local_id,
31436          st_transaction_id,
31437          instance_id,
31438          entity_name,
31439          data_source_type,
31440          char1,
31441          char2,
31442          char3,
31443          char4,
31444          SOURCE_DEMAND_SOURCE_HEADER_ID,
31445          last_update_date,
31446          last_updated_by,
31447          creation_date,
31448          created_by)
31449       SELECT
31450          demand_source_header_id,
31451          st_transaction_id,
31452          v_instance_id,
31453          'SALES_ORDER_ID',
31454          data_source_type,
31455          sr_instance_code,
31456          company_name,
31457          organization_code,
31458          sales_order_number,
31459          SOURCE_DEMAND_SOURCE_HEADER_ID,
31460          v_current_date,
31461          v_current_user,
31462          v_current_date,
31463          v_current_user
31464       FROM  msc_st_sales_orders
31465       WHERE rowid = lb_rowid(j);
31466 
31467       END IF;
31468       CLOSE c2;
31469 
31470     -- Update demand source header id
31471 
31472     v_sql_stmt := 2.4;
31473    lv_sql_stmt :=
31474    'UPDATE msc_st_sales_orders mso '
31475     ||'  SET  demand_source_header_id = (SELECT local_id'
31476     ||'       FROM   msc_local_id_demand mlid'
31477     ||'       WHERE  mlid.char1    = mso.sr_instance_code'
31478     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
31479     ||'       =     NVL(mso.company_name,'||''''||NULL_CHAR||''''||') '
31480     ||'       AND    mlid.char3    = mso.organization_code'
31481     ||'       AND    mlid.char4    = mso.sales_order_number'
31482     ||'       AND    mlid.instance_id = '||v_instance_id
31483     ||'       AND    mlid.entity_name = ''SALES_ORDER_ID'' )'
31484     ||' WHERE process_flag      = '||G_IN_PROCESS
31485     ||' AND   NVL(demand_source_header_id,'||NULL_VALUE||') = '||NULL_VALUE
31486     ||' AND   batch_id          = :p_batch_id '
31487     ||' AND   sr_instance_code  =:v_instance_code';
31488 
31489 
31490       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31491 
31492     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
31493 
31494 
31495 
31496     COMMIT;
31497     IF c_lock%ISOPEN THEN
31498       CLOSE c_lock;
31499     END IF;
31500 -- release locks
31501 
31502 
31503 
31504     OPEN c1(p_batch_id);
31505     FETCH c1 BULK COLLECT INTO lb_rowid;
31506     IF c1%ROWCOUNT > 0  THEN
31507     v_sql_stmt := 03;
31508     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
31509     UPDATE msc_st_sales_orders
31510     SET    st_transaction_id   = msc_st_sales_orders_s.NEXTVAL,
31511            refresh_id          = v_refresh_id,
31512            last_update_date    = v_current_date,
31513            last_updated_by     = v_current_user,
31514            creation_date       = v_current_date,
31515            created_by          = v_current_user
31516     WHERE  rowid               = lb_rowid(j);
31517 
31518     END IF;
31519     CLOSE c1;
31520 
31521 
31522     -- Set the  message
31523 
31524      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31525                      (p_app_short_name    => 'MSC',
31526                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
31527                       p_message_text      => lv_message_text,
31528                       p_error_text        => lv_error_text,
31529                       p_token1            => 'COLUMN_NAME',
31530                       p_token_value1      => 'SALES_ORDER_NUMBER'
31531                                               ||'OR LINE_NUM' );
31532 
31533       IF lv_return <> 0 THEN
31534         RAISE ex_logging_err;
31535       END IF;
31536 
31537     -- Error out records where NOT NULL columns are not present
31538 
31539     v_sql_stmt := 04;
31540 
31541     lv_sql_stmt :=
31542     'UPDATE    msc_st_sales_orders '
31543     ||' SET    process_flag  = '||G_ERROR_FLG||','
31544     ||'         error_text   = '||''''||lv_message_text||''''
31545     ||' WHERE  ( NVL(line_num,'||NULL_VALUE||') = '||NULL_VALUE
31546     ||' OR     NVL(sales_order_number,'||''''||NULL_CHAR||''''||') '
31547     ||'         = '||''''||NULL_CHAR||''''||')'
31548     ||' AND   process_flag             = '||G_IN_PROCESS
31549     ||' AND   batch_id                 = :p_batch_id'
31550     ||' AND   sr_instance_code         = :v_instance_code';
31551 
31552 
31553 
31554         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31555 
31556       EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
31557 
31558 
31559     -- Set the error message
31560     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31561                      (p_app_short_name    => 'MSC',
31562                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
31563                       p_message_text      => lv_message_text,
31564                       p_error_text        => lv_error_text,
31565                       p_token1            => 'COLUMN_NAME',
31566                       p_token_value1      => 'DELETED_FLAG',
31567                       p_token2            => 'DEFAULT_VALUE',
31568                       p_token_value2      =>  SYS_NO  );
31569 
31570       IF lv_return <> 0 THEN
31571         RAISE ex_logging_err;
31572       END IF;
31573 
31574 
31575     lv_where_str :=
31576     ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
31577 
31578     --Log a warning for those records where the deleted_flag has a value other
31579     --SYS_NO or SYS_YES
31580     lv_return := MSC_ST_UTIL.LOG_ERROR
31581                    (p_table_name        => 'MSC_ST_SALES_ORDERS',
31582                     p_instance_code     => v_instance_code,
31583                     p_row               => lv_column_names,
31584                     p_severity          => G_SEV_WARNING,
31585                     p_message_text      => lv_message_text,
31586                     p_error_text        => lv_error_text,
31587                     p_batch_id          => p_batch_id,
31588                     p_where_str         => lv_where_str,
31589                     p_col_name          => 'DELETED_FLAG',
31590                     p_debug             => v_debug,
31591                     p_default_value     => SYS_NO);
31592 
31593     IF lv_return <> 0 THEN
31594         RAISE ex_logging_err;
31595     END IF;
31596 
31597      -- Set the  message
31598 
31599      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31600                      (p_app_short_name    => 'MSC',
31601                       p_error_code        => 'MSC_PP_INVALID_VALUE',
31602                       p_message_text      => lv_message_text,
31603                       p_error_text        => lv_error_text,
31604                       p_token1            => 'COLUMN_NAME',
31605                       p_token_value1      => 'ORGANIZATION_CODE');
31606 
31607       IF lv_return <> 0 THEN
31608         RAISE ex_logging_err;
31609       END IF;
31610 
31611      --Derive Organization_id
31612     lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
31613                    (p_table_name       => 'MSC_ST_SALES_ORDERS',
31614                     p_org_partner_name => 'ORGANIZATION_CODE',
31615                     p_org_partner_id   => 'ORGANIZATION_ID',
31616                     p_instance_code    => v_instance_code,
31617                     p_partner_type     => G_ORGANIZATION,
31618                     p_error_text       => lv_error_text,
31619                     p_batch_id         => p_batch_id,
31620                     p_severity         => G_SEV_ERROR,
31621                     p_message_text     => lv_message_text,
31622                     p_debug            => v_debug,
31623                     p_row              => lv_column_names);
31624 
31625     IF lv_return <> 0 THEN
31626         RAISE ex_logging_err;
31627     END IF;
31628 
31629      -- Set the  message
31630 
31631      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31632                      (p_app_short_name    => 'MSC',
31633                       p_error_code        => 'MSC_PP_INVALID_VALUE',
31634                       p_message_text      => lv_message_text,
31635                       p_error_text        => lv_error_text,
31636                       p_token1            => 'COLUMN_NAME',
31637                       p_token_value1      => 'ORIGINAL_ITEM_NAME');
31638 
31639       IF lv_return <> 0 THEN
31640         RAISE ex_logging_err;
31641       END IF;
31642 
31643     --DERIVE ORIGINAL_ITEM_ID
31644 
31645    lv_return :=  MSC_ST_UTIL.DERIVE_ITEM_ID
31646                      (p_table_name    =>'MSC_ST_SALES_ORDERS',
31647                       p_item_col_name =>'ORIGINAL_ITEM_NAME',
31648                       p_item_col_id   =>'ORIGINAL_ITEM_ID',
31649                       p_instance_code => v_instance_code,
31650                       p_instance_id   => v_instance_id,
31651                       p_batch_id      => p_batch_id,
31652                       p_debug         => v_debug,
31653                       p_severity      => G_SEV3_ERROR,
31654                       p_message_text  => lv_message_text,
31655                       p_row           => lv_column_names,
31656                       p_error_text    =>lv_error_text);
31657 
31658     IF lv_return <> 0 THEN
31659         RAISE ex_logging_err;
31660     END IF;
31661 
31662    -- Derive Demand id
31663 
31664    v_sql_stmt := 06;
31665    lv_sql_stmt :=
31666    'UPDATE msc_st_sales_orders mso '
31667     ||'  SET  demand_id = (SELECT local_id'
31668     ||'       FROM   msc_local_id_demand mlid'
31669     ||'       WHERE  mlid.char1    = mso.sr_instance_code'
31670     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
31671     ||'       =     NVL(mso.company_name,'||''''||NULL_CHAR||''''||') '
31672     ||'       AND    mlid.char3    = mso.organization_code'
31673     ||'       AND    mlid.char4    = mso.sales_order_number'
31674     ||'       AND    nvl(mlid.number2,'||NULL_VALUE||') = nvl(mso.reservation_type,'||NULL_VALUE||')'
31675     ||'       AND   NVL(mlid.char5,'||''''||NULL_CHAR||''''||') '
31676     ||'       =     NVL(mso.SUBINVENTORY,'||''''||NULL_CHAR||''''||') '
31677     ||'       AND    mlid.number1     = mso.line_num'
31678     ||'       AND    mlid.entity_name = ''DEMAND_ID'' )'
31679     ||' WHERE process_flag      ='||G_IN_PROCESS
31680     ||' AND   batch_id          = :p_batch_id '
31681     ||' AND   sr_instance_code  =:v_instance_code';
31682 
31683 
31684 
31685       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31686 
31687     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
31688 
31689 
31690     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31691                  (p_app_short_name    => 'MSC',
31692                   p_error_code        => 'MSC_PP_DELETE_FAIL',
31693                   p_message_text      => lv_message_text,
31694                   p_error_text        => lv_error_text);
31695 
31696     IF lv_return <> 0 THEN
31697       RAISE ex_logging_err;
31698     END IF;
31699    -- Error out the records where deleted_flag = SYS_YES
31700    -- but NULL demand_id or demand_source_header_id
31701 
31702     v_sql_stmt := 07;
31703 
31704   lv_sql_stmt :=
31705   'UPDATE      msc_st_sales_orders '
31706   ||' SET      process_flag    ='||G_ERROR_FLG||','
31707   ||'          error_text   = '||''''||lv_message_text||''''
31708   ||' WHERE    (NVL(demand_id,'||NULL_VALUE||') = '||NULL_VALUE
31709   ||' OR       NVL(demand_source_header_id,'||NULL_VALUE||') = '||NULL_VALUE||')'
31710   ||' AND      deleted_flag  ='||SYS_YES
31711   ||' AND      batch_id          = :p_batch_id '
31712   ||' AND      sr_instance_code  =:v_instance_code';
31713 
31714 
31715       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31716 
31717     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
31718 
31719 
31720       -- Set the  message
31721 
31722      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31723                      (p_app_short_name    => 'MSC',
31724                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
31725                       p_message_text      => lv_message_text,
31726                       p_error_text        => lv_error_text,
31727                       p_token1            => 'COLUMN_NAME',
31728                       p_token_value1      => 'ITEM_NAME OR PRIMARY_UOM_QUANTITY'
31729                                               ||' OR REQUIREMENT_DATE' );
31730 
31731       IF lv_return <> 0 THEN
31732         RAISE ex_logging_err;
31733       END IF;
31734 
31735     -- Error out records where NOT NULL columns are not present
31736 
31737     v_sql_stmt := 08;
31738 
31739     lv_sql_stmt :=
31740     'UPDATE    msc_st_sales_orders '
31741     ||' SET    process_flag  = '||G_ERROR_FLG||','
31742     ||'         error_text   = '||''''||lv_message_text||''''
31743     ||' WHERE  ( NVL(primary_uom_quantity,'||NULL_VALUE||') = '||NULL_VALUE
31744     ||' OR     NVL(requirement_date,SYSDATE-36500 ) = SYSDATE-36500 '
31745     ||' OR     NVL(ITEM_NAME,'||''''||NULL_CHAR||''''||') '
31746     ||'         = '||''''||NULL_CHAR||''''||')'
31747     ||' AND   process_flag        = '||G_IN_PROCESS
31748     ||' AND   deleted_flag        = '||SYS_NO
31749     ||' AND   batch_id            = :p_batch_id'
31750     ||' AND   sr_instance_code    = :v_instance_code';
31751 
31752 
31753 
31754         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
31755 
31756       EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
31757 
31758 
31759      -- Set the  message
31760 
31761      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31762                      (p_app_short_name    => 'MSC',
31763                       p_error_code        => 'MSC_PP_INVALID_VALUE',
31764                       p_message_text      => lv_message_text,
31765                       p_error_text        => lv_error_text,
31766                       p_token1            => 'COLUMN_NAME',
31767                       p_token_value1      => 'ITEM_NAME');
31768 
31769       IF lv_return <> 0 THEN
31770         RAISE ex_logging_err;
31771       END IF;
31772 
31773     --Derive Inventory_item_id
31774     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
31775                    (p_table_name       => 'MSC_ST_SALES_ORDERS',
31776                     p_item_col_name    => 'ITEM_NAME',
31777                     p_item_col_id      => 'INVENTORY_ITEM_ID',
31778                     p_instance_id      => v_instance_id,
31779                     p_instance_code    => v_instance_code,
31780                     p_error_text       => lv_error_text,
31781                     p_batch_id         => p_batch_id,
31782                     p_severity         => G_SEV_ERROR,
31783                     p_message_text     => lv_message_text,
31784                     p_debug            => v_debug,
31785                     p_row              => lv_column_names);
31786 
31787     IF lv_return <> 0 THEN
31788         RAISE ex_logging_err;
31789     END IF;
31790 
31791     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31792                      (p_app_short_name    => 'MSC',
31793                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
31794                       p_message_text      => lv_message_text,
31795                       p_error_text        => lv_error_text,
31796                       p_token1            => 'COLUMN_NAMES',
31797                       p_token_value1      => 'ORIGINAL_ITEM,ITEM_NAME,'
31798                                              ||'RECIPROCAL_FLAG,SR_INSTANCE_CODE',
31799                       p_token2            => 'MASTER_TABLE',
31800                       p_token_value2      => 'MSC_ST_ITEM_SUBSTITUTES/MSC_ITEM_SUBSTITUTES',
31801                       p_token3            =>  'CHILD_TABLE' ,
31802                       p_token_value3      =>  'MSC_ST_SALES_ORDERS' );
31803 
31804       IF lv_return <> 0 THEN
31805         RAISE ex_logging_err;
31806       END IF;
31807 
31808       --Validates the product substitution effectivity and directionality rules.
31809 
31810       UPDATE   msc_st_sales_orders
31811       SET      process_flag      = G_ERROR_FLG,
31812                error_text        = lv_message_text
31813       WHERE    original_item_name is not null
31814       AND      deleted_flag      = SYS_NO
31815       AND      NOT EXISTS(SELECT   1
31816                           FROM     msc_item_substitutes mis
31817                           WHERE  ((mis.reciprocal_flag              = 1
31818                           AND    ((mis.lower_item_id                = original_item_id
31819                           AND      mis.higher_item_id               = inventory_item_id )
31820                           OR      (mis.higher_item_id               = original_item_id
31821                           AND      mis.lower_item_id                = inventory_item_id )))
31822                           OR      (mis.reciprocal_flag              = 2
31823                           AND      mis.lower_item_id                = original_item_id
31824                           AND      mis.higher_item_id               = inventory_item_id ))
31825                           AND      nvl(mis.effective_date,sysdate)  <=sysdate
31826                           AND      nvl(mis.disable_date,sysdate+1)  > sysdate
31827                           AND      mis.sr_instance_id               = v_instance_id
31828                           AND      mis.plan_id                      = -1
31829                           UNION
31830                           SELECT 1
31831                           FROM     msc_st_item_substitutes mss
31832                           WHERE  ((mss.reciprocal_flag         = 1
31833                           AND    ((mss.lower_item_id           = original_item_id
31834                           AND      mss.higher_item_id          = inventory_item_id)
31835                           OR      (mss.higher_item_id          = original_item_id
31836                           AND      mss.lower_item_id           = inventory_item_id  )))
31837                           OR      (mss.reciprocal_flag         = 2
31838                           AND      mss.lower_item_id           = original_item_id
31839                           AND      mss.higher_item_id          = inventory_item_id ))
31840                           AND      nvl(effective_date,sysdate) <= sysdate
31841                           AND      nvl(disable_date,sysdate+1) >  sysdate
31842                           AND      mss.sr_instance_id          = v_instance_id
31843                           AND      mss.process_flag            = G_VALID )
31844       AND      process_flag     = G_IN_PROCESS
31845       AND      sr_instance_code = v_instance_code
31846       AND      batch_id         = p_batch_id;
31847 
31848 
31849     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31850                      (p_app_short_name    => 'MSC',
31851                       p_error_code        => 'MSC_PP_INVALID_VALUE',
31852                       p_message_text      => lv_message_text,
31853                       p_error_text        => lv_error_text,
31854                       p_token1            => 'COLUMN_NAME',
31855                       p_token_value1      => 'DEMAND_CLASS');
31856 
31857       IF lv_return <> 0 THEN
31858         RAISE ex_logging_err;
31859       END IF;
31860 
31861 
31862       -- Validate Demand Class, if value provided it should exists
31863       -- in ODS or staging table
31864 
31865      lv_return :=
31866      MSC_ST_UTIL.VALIDATE_DMD_CLASS(p_table_name       => 'MSC_ST_SALES_ORDERS',
31867                                     p_dmd_class_column => 'DEMAND_CLASS',
31868                                     p_instance_id      => v_instance_id,
31869                                     p_instance_code    => v_instance_code,
31870                                     p_severity         => G_SEV3_ERROR,
31871                                     p_message_text     => lv_message_text,
31872                                     p_batch_id         => p_batch_id,
31873                                     p_debug            => v_debug,
31874                                     p_error_text       => lv_error_text);
31875 
31876      if(lv_return <> 0) then
31877         RAISE ex_logging_err;
31878      end if;
31879 
31880     -- Set the error message
31881     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31882                      (p_app_short_name    => 'MSC',
31883                       p_error_code        => 'MSC_PP_COL_VAL_NULL_DEFAULT',
31884                       p_message_text      => lv_message_text,
31885                       p_error_text        => lv_error_text,
31886                       p_token1            => 'COLUMN_NAME',
31887                       p_token_value1      => 'COMPLETED_QUANTITY',
31888                       p_token2            => 'DEFAULT_VALUE',
31889                       p_token_value2      =>  G_COMPLETED_QUANTITY  );
31890 
31891       IF lv_return <> 0 THEN
31892         RAISE ex_logging_err;
31893       END IF;
31894 
31895   --  Default completed_quantity
31896 
31897   lv_where_str :=
31898   '   AND NVL(completed_quantity,'||NULL_VALUE||')'
31899   ||'  = '||NULL_VALUE;
31900 
31901   lv_return := MSC_ST_UTIL.LOG_ERROR
31902                    (p_table_name        => 'MSC_ST_SALES_ORDERS',
31903                     p_instance_code     => v_instance_code,
31904                     p_row               => lv_column_names,
31905                     p_severity          => G_SEV_WARNING,
31906                     p_message_text      => lv_message_text,
31907                     p_error_text        => lv_error_text,
31908                     p_batch_id          => p_batch_id,
31909                     p_where_str         => lv_where_str,
31910                     p_col_name          => 'COMPLETED_QUANTITY',
31911                     p_debug             => v_debug,
31912                     p_default_value     => G_COMPLETED_QUANTITY);
31913 
31914     IF lv_return <> 0 THEN
31915         RAISE ex_logging_err;
31916     END IF;
31917 
31918     -- Set the error message
31919     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31920                      (p_app_short_name    => 'MSC',
31921                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
31922                       p_message_text      => lv_message_text,
31923                       p_error_text        => lv_error_text,
31924                       p_token1            => 'COLUMN_NAME',
31925                       p_token_value1      => 'DEMAND_SOURCE_TYPE',
31926                       p_token2            => 'DEFAULT_VALUE',
31927                       p_token_value2      =>  G_SO_DEMAND_SOURCE_TYPE );
31928 
31929       IF lv_return <> 0 THEN
31930         RAISE ex_logging_err;
31931       END IF;
31932 
31933   -- Default demand_source_type
31934 
31935   lv_where_str :=
31936   '   AND NVL(demand_source_type ,'||NULL_VALUE||') NOT IN (2,8) '
31937   ||' AND deleted_flag = '||SYS_NO;
31938 
31939   lv_return := MSC_ST_UTIL.LOG_ERROR
31940                    (p_table_name        => 'MSC_ST_SALES_ORDERS',
31941                     p_instance_code     => v_instance_code,
31942                     p_row               => lv_column_names,
31943                     p_severity          => G_SEV_WARNING,
31944                     p_message_text      => lv_message_text,
31945                     p_error_text        => lv_error_text,
31946                     p_batch_id          => p_batch_id,
31947                     p_where_str         => lv_where_str,
31948                     p_col_name          => 'DEMAND_SOURCE_TYPE',
31949                     p_debug             => v_debug,
31950                     p_default_value     => G_SO_DEMAND_SOURCE_TYPE);
31951 
31952     IF lv_return <> 0 THEN
31953         RAISE ex_logging_err;
31954     END IF;
31955 
31956     -- Set the error message
31957     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31958                      (p_app_short_name    => 'MSC',
31959                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
31960                       p_message_text      => lv_message_text,
31961                       p_error_text        => lv_error_text,
31962                       p_token1            => 'COLUMN_NAME',
31963                       p_token_value1      => 'RESERVATION_TYPE',
31964                       p_token2            => 'DEFAULT_VALUE',
31965                       p_token_value2      =>  G_RESERVATION_TYPE );
31966 
31967       IF lv_return <> 0 THEN
31968         RAISE ex_logging_err;
31969       END IF;
31970 
31971    --  Default reservation_type
31972    lv_where_str :=
31973    '   AND NVL(reservation_type,'||NULL_VALUE||') NOT IN (1,2,3)'
31974    ||' AND deleted_flag = '||SYS_NO;
31975 
31976 
31977   lv_return := MSC_ST_UTIL.LOG_ERROR
31978                    (p_table_name        => 'MSC_ST_SALES_ORDERS',
31979                     p_instance_code     => v_instance_code,
31980                     p_row               => lv_column_names,
31981                     p_severity          => G_SEV_WARNING,
31982                     p_message_text      => lv_message_text,
31983                     p_error_text        => lv_error_text,
31984                     p_batch_id          => p_batch_id,
31985                     p_where_str         => lv_where_str,
31986                     p_col_name          => 'RESERVATION_TYPE',
31987                     p_debug             => v_debug,
31988                     p_default_value     => G_RESERVATION_TYPE);
31989 
31990     IF lv_return <> 0 THEN
31991         RAISE ex_logging_err;
31992     END IF;
31993 
31994     -- Set the error message
31995     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
31996                      (p_app_short_name    => 'MSC',
31997                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
31998                       p_message_text      => lv_message_text,
31999                       p_error_text        => lv_error_text,
32000                       p_token1            => 'COLUMN_NAME',
32001                       p_token_value1      => 'AVAILABLE_TO_MRP',
32002                       p_token2            => 'DEFAULT_VALUE',
32003                       p_token_value2      => G_AVAILABLE_TO_MRP);
32004 
32005       IF lv_return <> 0 THEN
32006         RAISE ex_logging_err;
32007       END IF;
32008 
32009   --  Default available_to_mrp
32010 
32011   lv_where_str :=
32012   '   AND NVL(available_to_mrp ,'||''''||NULL_CHAR||''''||') <> ''Y'' '
32013   ||' AND deleted_flag = '||SYS_NO;
32014 
32015 
32016   lv_return := MSC_ST_UTIL.LOG_ERROR
32017                    (p_table_name        => 'MSC_ST_SALES_ORDERS',
32018                     p_instance_code     => v_instance_code,
32019                     p_row               => lv_column_names,
32020                     p_severity          => G_SEV_WARNING,
32021                     p_message_text      => lv_message_text,
32022                     p_error_text        => lv_error_text,
32023                     p_batch_id          => p_batch_id,
32024                     p_where_str         => lv_where_str,
32025                     p_col_name          => 'AVAILABLE_TO_MRP',
32026                     p_debug             => v_debug,
32027                     p_default_value     => '''Y''' );
32028 
32029     IF lv_return <> 0 THEN
32030         RAISE ex_logging_err;
32031     END IF;
32032 
32033     -- Set the error message
32034     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32035                      (p_app_short_name    => 'MSC',
32036                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
32037                       p_message_text      => lv_message_text,
32038                       p_error_text        => lv_error_text,
32039                       p_token1            => 'COLUMN_NAME',
32040                       p_token_value1      => 'CTO_FLAG',
32041                       p_token2            => 'DEFAULT_VALUE',
32042                       p_token_value2      =>  G_CTO_FLAG);
32043 
32044       IF lv_return <> 0 THEN
32045         RAISE ex_logging_err;
32046       END IF;
32047 
32048     -- Default cto_flag
32049 
32050   lv_where_str :=
32051   '   AND NVL(cto_flag,'||NULL_VALUE||') NOT IN (1,2)' ;
32052 
32053   lv_return := MSC_ST_UTIL.LOG_ERROR
32054                    (p_table_name        => 'MSC_ST_SALES_ORDERS',
32055                     p_instance_code     => v_instance_code,
32056                     p_row               => lv_column_names,
32057                     p_severity          => G_SEV_WARNING,
32058                     p_message_text      => lv_message_text,
32059                     p_error_text        => lv_error_text,
32060                     p_batch_id          => p_batch_id,
32061                     p_where_str         => lv_where_str,
32062                     p_col_name          => 'CTO_FLAG',
32063                     p_debug             => v_debug,
32064                     p_default_value     => G_CTO_FLAG);
32065 
32066     IF lv_return <> 0 THEN
32067         RAISE ex_logging_err;
32068     END IF;
32069 
32070     -- Set the error message
32071     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32072                      (p_app_short_name    => 'MSC',
32073                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
32074                       p_message_text      => lv_message_text,
32075                       p_error_text        => lv_error_text,
32076                       p_token1            => 'COLUMN_NAME',
32077                       p_token_value1      => 'FORECAST_VISIBLE',
32078                       p_token2            => 'DEFAULT_VALUE',
32079                       p_token_value2      =>  G_FORECAST_VISIBLE);
32080 
32081       IF lv_return <> 0 THEN
32082         RAISE ex_logging_err;
32083       END IF;
32084 
32085   -- Default forecast_visible
32086 
32087   lv_where_str :=
32088   '    AND NVL(forecast_visible ,'||''''||NULL_CHAR||''''||')'
32089   ||'  NOT IN (''Y'', ''N'') AND deleted_flag = '||SYS_NO ;
32090 
32091   lv_return := MSC_ST_UTIL.LOG_ERROR
32092                    (p_table_name        => 'MSC_ST_SALES_ORDERS',
32093                     p_instance_code     => v_instance_code,
32094                     p_row               => lv_column_names,
32095                     p_severity          => G_SEV_WARNING,
32096                     p_message_text      => lv_message_text,
32097                     p_error_text        =>lv_error_text,
32098                     p_batch_id          => p_batch_id,
32099                     p_where_str         => lv_where_str,
32100                     p_col_name          => 'FORECAST_VISIBLE',
32101                     p_debug             => v_debug,
32102                     p_default_value     => '''Y''');
32103 
32104     IF lv_return <> 0 THEN
32105         RAISE ex_logging_err;
32106     END IF;
32107 
32108     -- Set the error message
32109     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32110                      (p_app_short_name    => 'MSC',
32111                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
32112                       p_message_text      => lv_message_text,
32113                       p_error_text        => lv_error_text,
32114                       p_token1            => 'COLUMN_NAME',
32115                       p_token_value1      => 'DEMAND_VISIBLE',
32116                       p_token2            => 'DEFAULT_VALUE',
32117                       p_token_value2      =>  G_DEMAND_VISIBLE);
32118 
32119       IF lv_return <> 0 THEN
32120         RAISE ex_logging_err;
32121       END IF;
32122 
32123   -- Default demand_visible
32124 
32125   lv_where_str :=
32126    '    AND NVL(demand_visible,'||''''||NULL_CHAR||''''||')'
32127   ||'   NOT IN (''Y'', ''N'') AND deleted_flag = '||SYS_NO ;
32128 
32129   lv_return := MSC_ST_UTIL.LOG_ERROR
32130                    (p_table_name        => 'MSC_ST_SALES_ORDERS',
32131                     p_instance_code     => v_instance_code,
32132                     p_row               => lv_column_names,
32133                     p_severity          => G_SEV_WARNING,
32134                     p_message_text      => lv_message_text,
32135                     p_error_text        => lv_error_text,
32136                     p_batch_id          => p_batch_id,
32137                     p_where_str         => lv_where_str,
32138                     p_col_name          => 'DEMAND_VISIBLE',
32139                     p_debug             => v_debug,
32140                     p_default_value     => '''Y''');
32141 
32142     IF lv_return <> 0 THEN
32143         RAISE ex_logging_err;
32144     END IF;
32145 
32146 
32147      -- Set the  message
32148 
32149      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32150                      (p_app_short_name    => 'MSC',
32151                       p_error_code        => 'MSC_PP_INVALID_VALUE',
32152                       p_message_text      => lv_message_text,
32153                       p_error_text        => lv_error_text,
32154                       p_token1            => 'COLUMN_NAME',
32155                       p_token_value1      => 'CUSTOMER_NAME');
32156 
32157       IF lv_return <> 0 THEN
32158         RAISE ex_logging_err;
32159       END IF;
32160 
32161    -- Derive customer_id
32162 
32163    lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
32164                      (p_table_name       => 'MSC_ST_SALES_ORDERS',
32165                       p_org_partner_name => 'CUSTOMER_NAME',
32166                       p_org_partner_id   => 'CUSTOMER_ID',
32167                       p_instance_code    => v_instance_code,
32168                       p_partner_type     => G_CUSTOMER,
32169                       p_error_text       => lv_error_text,
32170                       p_batch_id         => p_batch_id,
32171                       p_severity         => G_SEV3_ERROR,
32172                       p_message_text     => lv_message_text,
32173                       p_debug            => v_debug,
32174                       p_row              => lv_column_names);
32175 
32176       IF lv_return <> 0 THEN
32177         RAISE ex_logging_err;
32178       END IF;
32179 
32180      -- Set the  message
32181 
32182      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32183                      (p_app_short_name    => 'MSC',
32184                       p_error_code        => 'MSC_PP_INVALID_VALUE',
32185                       p_message_text      => lv_message_text,
32186                       p_error_text        => lv_error_text,
32187                       p_token1            => 'COLUMN_NAME',
32188                       p_token_value1      => 'BILL_TO_SITE_CODE');
32189 
32190       IF lv_return <> 0 THEN
32191         RAISE ex_logging_err;
32192       END IF;
32193 
32194   -- Derive bill_to_site_id
32195   lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
32196                      (p_table_name        => 'MSC_ST_SALES_ORDERS',
32197                       p_partner_name      => 'CUSTOMER_NAME',
32198                       p_partner_site_code => 'BILL_TO_SITE_CODE',
32199                       p_partner_site_id   => 'BILL_TO_SITE_USE_ID',
32200                       p_instance_code     => v_instance_code,
32201                       p_partner_type      => G_CUSTOMER,
32202                       p_error_text        => lv_error_text,
32203                       p_batch_id          => p_batch_id,
32204                       p_severity          => G_SEV3_ERROR,
32205                       p_message_text      => lv_message_text,
32206                       p_debug             => v_debug,
32207                       p_row               => lv_column_names);
32208 
32209       IF lv_return <> 0 THEN
32210         RAISE ex_logging_err;
32211       END IF;
32212 
32213     -- Set the  message
32214 
32215      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32216                      (p_app_short_name    => 'MSC',
32217                       p_error_code        => 'MSC_PP_INVALID_VALUE',
32218                       p_message_text      => lv_message_text,
32219                       p_error_text        => lv_error_text,
32220                       p_token1            => 'COLUMN_NAME',
32221                       p_token_value1      => 'SHIP_TO_SITE_CODE');
32222 
32223       IF lv_return <> 0 THEN
32224         RAISE ex_logging_err;
32225       END IF;
32226 
32227   -- Derive ship_to_site_id
32228   lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
32229                      (p_table_name        => 'MSC_ST_SALES_ORDERS',
32230                       p_partner_name      => 'CUSTOMER_NAME',
32231                       p_partner_site_code => 'SHIP_TO_SITE_CODE',
32232                       p_partner_site_id   => 'SHIP_TO_SITE_USE_ID',
32233                       p_instance_code     => v_instance_code,
32234                       p_partner_type      => G_CUSTOMER,
32235                       p_error_text        => lv_error_text,
32236                       p_batch_id          => p_batch_id,
32237                       p_severity          => G_SEV3_ERROR,
32238                       p_message_text      => lv_message_text,
32239                       p_debug             => v_debug,
32240                       p_row               => lv_column_names);
32241 
32242       IF lv_return <> 0 THEN
32243         RAISE ex_logging_err;
32244       END IF;
32245    -- Set the error message
32246    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32247                      (p_app_short_name    => 'MSC',
32248                       p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
32249                       p_message_text      => lv_message_text,
32250                       p_error_text        => lv_error_text,
32251                       p_token1            => 'COLUMN_NAMES',
32252                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
32253                                              ||'ORGANIZATION_CODE,PROJECT_NUMBER',
32254                       p_token2            => 'MASTER_TABLE',
32255 		      p_token_value2      =>  'MSC_ST_PROJECT_TASKS');
32256 
32257       IF lv_return <> 0 THEN
32258         RAISE ex_logging_err;
32259       END IF;
32260 
32261    --Derive Project Id.
32262     lv_return:= MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
32263                            (p_table_name          => 'MSC_ST_SALES_ORDERS',
32264                             p_proj_col_name       => 'PROJECT_NUMBER',
32265                             p_proj_task_col_id    => 'PROJECT_ID',
32266                             p_instance_code       =>  v_instance_code,
32267                             p_entity_name         => 'PROJECT_ID',
32268                             p_error_text          =>  lv_error_text,
32269                             p_batch_id            =>  p_batch_id,
32270                             p_severity            =>  G_SEV_WARNING,
32271                             p_message_text        =>  lv_message_text,
32272                             p_debug               => v_debug,
32273                             p_row                 =>  lv_column_names);
32274     IF lv_return<> 0 THEN
32275       RAISE ex_logging_err;
32276     END IF;
32277 
32278     -- Set the error message
32279    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32280                      (p_app_short_name    => 'MSC',
32281                       p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
32282                       p_message_text      => lv_message_text,
32283                       p_error_text        => lv_error_text,
32284                       p_token1            => 'COLUMN_NAMES',
32285                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
32286                                              ||'ORGANIZATION_CODE,PROJECT_NUMBER,'
32287                                              ||'TASK_NUMBER' ,
32288                       p_token2            => 'MASTER_TABLE',
32289                       p_token_value2      =>  'MSC_ST_PROJECT_TASKS');
32290 
32291       IF lv_return <> 0 THEN
32292         RAISE ex_logging_err;
32293       END IF;
32294 
32295     --Derive Task Id.
32296     lv_return:= MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
32297                            (p_table_name          => 'MSC_ST_SALES_ORDERS',
32298                             p_proj_col_name       => 'PROJECT_NUMBER',
32299                             p_proj_task_col_id    => 'TASK_ID',
32300                             p_instance_code       => v_instance_code,
32301                             p_entity_name         => 'TASK_ID',
32302                             p_error_text          => lv_error_text,
32303                             p_task_col_name       => 'TASK_NUMBER',
32304                             p_batch_id            => p_batch_id,
32305                             p_severity            => G_SEV_WARNING,
32306                             p_message_text        => lv_message_text,
32307                             p_debug               => v_debug,
32308                             p_row                 => lv_column_names);
32309 
32310     IF lv_return<> 0 THEN
32311       RAISE ex_logging_err;
32312     END IF;
32313       -- ordered_item_id is null in ERP collection
32314 /*    v_sql_stmt := 09;
32315 
32316     lv_sql_stmt :=
32317 
32318     'UPDATE msc_st_sales_orders  '
32319     ||' SET    ordered_item_id   = inventory_item_id'
32320     ||' WHERE  sr_instance_code    = :v_instance_code'
32321     ||' AND    deleted_flag        ='||SYS_NO
32322     ||' AND    process_flag        ='||G_IN_PROCESS
32323     ||' AND    batch_id            = :p_batch_id';
32324 
32325 
32326             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32327 
32328   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,p_batch_id;
32329 */
32330   -- valid values for order_date_type_code are 'SHIP' and 'ARRIVE'
32331   -- Set the error message
32332 
32333      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32334                      (p_app_short_name    => 'MSC',
32335                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
32336                       p_message_text      => lv_message_text,
32337                       p_error_text        => lv_error_text,
32338                       p_token1            => 'COLUMN_NAME',
32339                       p_token_value1      => 'ORDER_DATE_TYPE_CODE',
32340                       p_token2            => 'DEFAULT_VALUE',
32341                       p_token_value2      => 1);
32342 
32343       IF lv_return <> 0 THEN
32344         RAISE ex_logging_err;
32345       END IF;
32346 
32347     lv_where_str :=
32348     '    AND NVL(order_date_type_code  ,'||NULL_VALUE||') '
32349     ||'  NOT IN (1,2)';
32350 
32351     lv_return := MSC_ST_UTIL.LOG_ERROR
32352                     (p_table_name        => 'MSC_ST_SALES_ORDERS',
32353                      p_instance_code     => v_instance_code,
32354                      p_row               => lv_column_names,
32355                      p_severity          => G_SEV_WARNING,
32356                      p_message_text      => lv_message_text,
32357                      p_error_text        => lv_error_text,
32358                      p_batch_id          => p_batch_id,
32359                      p_where_str         => lv_where_str,
32360                      p_col_name          => 'ORDER_DATE_TYPE_CODE',
32361                      p_debug             => v_debug,
32362                      p_default_value     => 1);
32363 
32364     IF lv_return <> 0 THEN
32365         RAISE ex_logging_err;
32366     END IF;
32367 
32368     --Call to customised validation.
32369     MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
32370       (ERRBUF         => lv_error_text,
32371        RETCODE        => lv_return,
32372        pBatchID       => p_batch_id,
32373        pInstanceCode  => v_instance_code,
32374        pEntityName    => 'MSC_ST_SALES_ORDERS',
32375        pInstanceID    => v_instance_id);
32376 
32377     IF NVL(lv_return,0) <> 0 THEN
32378       RAISE ex_logging_err;
32379     END IF;
32380 
32381   -- Generate demand_id
32382 
32383     OPEN c3(p_batch_id);
32384     FETCH c3 BULK COLLECT INTO lb_rowid ;
32385 
32386     IF c3%ROWCOUNT > 0  THEN
32387       v_sql_stmt := 13;
32388       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
32389       UPDATE msc_st_sales_orders
32390       SET    demand_id    = msc_st_demand_id_s.NEXTVAL
32391       WHERE  rowid        = lb_rowid(j);
32392 
32393       v_sql_stmt := 14;
32394       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
32395       INSERT INTO msc_local_id_demand
32396         (local_id,
32397          st_transaction_id,
32398          instance_id,
32399          entity_name,
32400          data_source_type,
32401          char1,
32402          char2,
32403          char3,
32404          char4,
32405          char5,
32406          number1,
32407          number2,
32408          SOURCE_DEMAND_SOURCE_HEADER_ID,
32409          SOURCE_ORG_ID,
32410          SOURCE_ORIGINAL_ITEM_ID,
32411          SOURCE_DEMAND_ID,
32412          SOURCE_INVENTORY_ITEM_ID,
32413          SOURCE_CUSTOMER_ID,
32414          SOURCE_BILL_TO_SITE_USE_ID,
32415          SOURCE_SHIP_TO_SITE_USE_ID,
32416          last_update_date,
32417          last_updated_by,
32418          creation_date,
32419          created_by)
32420       SELECT
32421          demand_id,
32422          st_transaction_id,
32423          v_instance_id,
32424          'DEMAND_ID',
32425          data_source_type,
32426          sr_instance_code,
32427          company_name,
32428          organization_code,
32429          sales_order_number,
32430          subinventory,
32431          line_num,
32432          decode(reservation_type,1,1,2,2,3,3,G_RESERVATION_TYPE),
32433          SOURCE_DEMAND_SOURCE_HEADER_ID,
32434          SOURCE_ORGANIZATION_ID,
32435          SOURCE_ORIGINAL_ITEM_ID,
32436          SOURCE_DEMAND_ID,
32437          SOURCE_INVENTORY_ITEM_ID,
32438          SOURCE_CUSTOMER_ID,
32439          SOURCE_BILL_TO_SITE_USE_ID,
32440          SOURCE_SHIP_TO_SITE_USE_ID,
32441          v_current_date,
32442          v_current_user,
32443          v_current_date,
32444          v_current_user
32445       FROM  msc_st_sales_orders
32446       WHERE rowid = lb_rowid(j);
32447 
32448       END IF;
32449       CLOSE c3 ;
32450 
32451     lv_sql_stmt :=
32452 
32453     'UPDATE msc_st_sales_orders  '
32454     ||' SET    demand_source_line  = demand_id,'
32455     ||'        atp_refresh_number  = '||v_refresh_id
32456     ||' WHERE  sr_instance_code    = :v_instance_code'
32457     ||' AND    deleted_flag        ='||SYS_NO
32458     ||' AND    process_flag        ='||G_IN_PROCESS
32459     ||' AND    batch_id            = :p_batch_id';
32460 
32461 
32462             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32463 
32464   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,p_batch_id;
32465 
32466   -- Generate ship_set_id
32467 
32468    v_sql_stmt := 15;
32469 
32470    lv_sql_stmt :=
32471    'UPDATE msc_st_sales_orders mso '
32472     ||'  SET  ship_set_id = (SELECT local_id'
32473     ||'       FROM   msc_local_id_demand mlid'
32474     ||'       WHERE  mlid.char1    = mso.sr_instance_code'
32475     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
32476     ||'       =     NVL(mso.company_name,'||''''||NULL_CHAR||''''||') '
32477     ||'       AND    mlid.char3    = mso.organization_code'
32478     ||'       AND    mlid.char4    = mso.sales_order_number'
32479     ||'       AND    NVL(mlid.char5, '||''''||NULL_CHAR||''''||') '
32480     ||'       =     NVL(mso.ship_set_name,'||''''||NULL_CHAR||''''||') '
32481     ||'       AND    mlid.instance_id = '||v_instance_id
32482     ||'       AND    mlid.entity_name = ''SHIP_SET_ID'' )'
32483     ||' WHERE process_flag      = '||G_IN_PROCESS
32484     ||' AND   NVL(ship_set_id,'||NULL_VALUE||') = '||NULL_VALUE
32485     ||' AND   batch_id          = :p_batch_id '
32486     ||' AND   sr_instance_code  =:v_instance_code';
32487 
32488 
32489       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32490 
32491     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
32492     OPEN c4(p_batch_id);
32493     FETCH c4 BULK COLLECT INTO lb_rowid ;
32494 
32495     IF c4%ROWCOUNT > 0  THEN
32496       v_sql_stmt := 15.1;
32497       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
32498       UPDATE msc_st_sales_orders
32499       SET    ship_set_id  = msc_st_set_id_s.NEXTVAL
32500       WHERE  rowid        = lb_rowid(j)
32501       AND ship_set_id is null;
32502 
32503       v_sql_stmt := 15.2;
32504       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
32505       INSERT INTO msc_local_id_demand
32506         (local_id,
32507          st_transaction_id,
32508          instance_id,
32509          entity_name,
32510          data_source_type,
32511          char1,
32512          char2,
32513          char3,
32514          char4,
32515          char5,
32516          last_update_date,
32517          last_updated_by,
32518          creation_date,
32519          created_by)
32520       SELECT
32521          ship_set_id,
32522          st_transaction_id,
32523          v_instance_id,
32524          'SHIP_SET_ID',
32525          data_source_type,
32526          sr_instance_code,
32527          company_name,
32528          organization_code,
32529          sales_order_number,
32530          ship_set_name,
32531          v_current_date,
32532          v_current_user,
32533          v_current_date,
32534          v_current_user
32535       FROM  msc_st_sales_orders
32536       WHERE rowid = lb_rowid(j);
32537 
32538       END IF;
32539       CLOSE c4;
32540 
32541     -- Update ship_set_id
32542 
32543    v_sql_stmt := 15.4;
32544    lv_sql_stmt :=
32545    'UPDATE msc_st_sales_orders mso '
32546     ||'  SET  ship_set_id = (SELECT local_id'
32547     ||'       FROM   msc_local_id_demand mlid'
32548     ||'       WHERE  mlid.char1    = mso.sr_instance_code'
32549     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
32550     ||'       =     NVL(mso.company_name,'||''''||NULL_CHAR||''''||') '
32551     ||'       AND    mlid.char3    = mso.organization_code'
32552     ||'       AND    mlid.char4    = mso.sales_order_number'
32553     ||'       AND    NVL(mlid.char5, '||''''||NULL_CHAR||''''||') '
32554     ||'       =     NVL(mso.ship_set_name,'||''''||NULL_CHAR||''''||') '
32555     ||'       AND    mlid.instance_id = '||v_instance_id
32556     ||'       AND    mlid.entity_name = ''SHIP_SET_ID'' )'
32557     ||' WHERE process_flag      = '||G_IN_PROCESS
32558     ||' AND   NVL(ship_set_id,'||NULL_VALUE||') = '||NULL_VALUE
32559     ||' AND   batch_id          = :p_batch_id '
32560     ||' AND   sr_instance_code  =:v_instance_code';
32561 
32562 
32563       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32564 
32565     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
32566 
32567 
32568  -- Generate arrival_set_id
32569 
32570    v_sql_stmt := 16;
32571 
32572    lv_sql_stmt :=
32573    'UPDATE msc_st_sales_orders mso '
32574     ||'  SET  arrival_set_id = (SELECT local_id'
32575     ||'       FROM   msc_local_id_demand mlid'
32576     ||'       WHERE  mlid.char1    = mso.sr_instance_code'
32577     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
32578     ||'       =     NVL(mso.company_name,'||''''||NULL_CHAR||''''||') '
32579     ||'       AND    mlid.char3    = mso.organization_code'
32580     ||'       AND    mlid.char4    = mso.sales_order_number'
32581     ||'       AND    NVL(mlid.char5, '||''''||NULL_CHAR||''''||') '
32582     ||'       =     NVL(mso.arrival_set_name,'||''''||NULL_CHAR||''''||') '
32583     ||'       AND    mlid.instance_id = '||v_instance_id
32584     ||'       AND    mlid.entity_name = ''ARRIVAL_SET_ID'' )'
32585     ||' WHERE process_flag      = '||G_IN_PROCESS
32586     ||' AND   NVL(arrival_set_id,'||NULL_VALUE||') = '||NULL_VALUE
32587     ||' AND   batch_id          = :p_batch_id '
32588     ||' AND   sr_instance_code  =:v_instance_code';
32589 
32590 
32591       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32592 
32593     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
32594     OPEN c5(p_batch_id);
32595     FETCH c5 BULK COLLECT INTO lb_rowid ;
32596 
32597     IF c5%ROWCOUNT > 0  THEN
32598       v_sql_stmt := 16.1;
32599       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
32600       UPDATE msc_st_sales_orders
32601       SET    arrival_set_id  = msc_st_set_id_s.NEXTVAL
32602       WHERE  rowid        = lb_rowid(j)
32603       AND arrival_set_id is null;
32604 
32605       v_sql_stmt := 16.2;
32606       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
32607       INSERT INTO msc_local_id_demand
32608         (local_id,
32609          st_transaction_id,
32610          instance_id,
32611          entity_name,
32612          data_source_type,
32613          char1,
32614          char2,
32615          char3,
32616          char4,
32617          char5,
32618          last_update_date,
32619          last_updated_by,
32620          creation_date,
32621          created_by)
32622       SELECT
32623          arrival_set_id,
32624          st_transaction_id,
32625          v_instance_id,
32626          'ARRIVAL_SET_ID',
32627          data_source_type,
32628          sr_instance_code,
32629          company_name,
32630          organization_code,
32631          sales_order_number,
32632          arrival_set_name,
32633          v_current_date,
32634          v_current_user,
32635          v_current_date,
32636          v_current_user
32637       FROM  msc_st_sales_orders
32638       WHERE rowid = lb_rowid(j);
32639 
32640       END IF;
32641       CLOSE c5;
32642 
32643     -- Update arrival_set_id
32644 
32645    v_sql_stmt := 16.4;
32646    lv_sql_stmt :=
32647    'UPDATE msc_st_sales_orders mso '
32648     ||'  SET  arrival_set_id = (SELECT local_id'
32649     ||'       FROM   msc_local_id_demand mlid'
32650     ||'       WHERE  mlid.char1    = mso.sr_instance_code'
32651     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
32652     ||'       =     NVL(mso.company_name,'||''''||NULL_CHAR||''''||') '
32653     ||'       AND    mlid.char3    = mso.organization_code'
32654     ||'       AND    mlid.char4    = mso.sales_order_number'
32655     ||'       AND    NVL(mlid.char5, '||''''||NULL_CHAR||''''||') '
32656     ||'       =     NVL(mso.arrival_set_name,'||''''||NULL_CHAR||''''||') '
32657     ||'       AND    mlid.instance_id = '||v_instance_id
32658     ||'       AND    mlid.entity_name = ''ARRIVAL_SET_ID'' )'
32659     ||' WHERE process_flag      = '||G_IN_PROCESS
32660     ||' AND   NVL(arrival_set_id,'||NULL_VALUE||') = '||NULL_VALUE
32661     ||' AND   batch_id          = :p_batch_id '
32662     ||' AND   sr_instance_code  =:v_instance_code';
32663 
32664 
32665       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32666 
32667     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
32668 
32669         -- populate row_type
32670 
32671    v_sql_stmt := 17;
32672    lv_sql_stmt :=
32673    'UPDATE msc_st_sales_orders mso '
32674     ||'  SET  ROW_TYPE= decode(reservation_type,1,decode(AVAILABLE_TO_MRP,''N'',''3'',''1'')
32675                                                ,2,1
32676                                                ,3,1
32677                                                ,NULL)' -- resv tp already validated for 1,2,3 so this decode should never return NULL.
32678     ||' WHERE process_flag      = '||G_IN_PROCESS
32679     ||' AND   batch_id          = :p_batch_id '
32680     ||' AND   sr_instance_code  =:v_instance_code';
32681 
32682 
32683       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32684 
32685     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
32686 
32687     lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
32688                   (p_table_name     => 'MSC_ST_SALES_ORDERS',
32689                    p_instance_id    => v_instance_id,
32690                    p_instance_code  => v_instance_code,
32691                    p_process_flag   => G_VALID,
32692                    p_error_text     => lv_error_text,
32693                    p_debug          => v_debug,
32694                    p_batch_id       => p_batch_id);
32695 
32696     IF lv_return <> 0 THEN
32697       RAISE ex_logging_err;
32698     END IF;
32699 
32700     lv_return := MSC_ST_UTIL.LOG_ERROR
32701                    (p_table_name        => 'MSC_ST_SALES_ORDERS',
32702                     p_instance_code     => v_instance_code,
32703                     p_row               => lv_column_names,
32704                     p_severity          => G_SEV_ERROR,
32705                     p_message_text      => NULL,
32706                     p_error_text        => lv_error_text,
32707                     p_debug             => v_debug,
32708                     p_batch_id          => p_batch_id);
32709 
32710     IF lv_return <> 0 THEN
32711       RAISE ex_logging_err;
32712     END IF;
32713 
32714     COMMIT;
32715 
32716  EXCEPTION
32717     WHEN too_many_rows THEN
32718       lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_SALES_ORDER'||'('
32719                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
32720       ROLLBACK ;
32721       IF c_lock%ISOPEN THEN
32722         CLOSE c_lock;
32723       END IF;
32724 
32725     WHEN ex_logging_err THEN
32726         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
32727 
32728       ROLLBACK;
32729       IF c_lock%ISOPEN THEN
32730         CLOSE c_lock;
32731       END IF;
32732 
32733     WHEN OTHERS THEN
32734       lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_SALES_ORDER'||'('
32735                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
32736         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
32737 
32738       ROLLBACK;
32739       IF c_lock%ISOPEN THEN
32740        CLOSE c_lock;
32741       END IF;
32742 
32743   END LOAD_SALES_ORDER;
32744 
32745 /*==========================================================================+
32746 | DESCRIPTION  : This function performs the validation and loads the data   |
32747 |                for business object Hard Reservations                      |
32748 +==========================================================================*/
32749   PROCEDURE  LOAD_RESERVATIONS  IS
32750   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
32751   lb_rowid          RowidTab; --bulk collects rowid
32752   lv_return         NUMBER;
32753   lv_batch_id       NUMBER;
32754   lv_error_text     VARCHAR2(250);
32755   lv_where_str      VARCHAR2(5000);
32756   lv_sql_stmt       VARCHAR2(5000);
32757   lv_column_names   VARCHAR2(5000);     --stores concatenated column names
32758   lv_message_text   msc_errors.error_text%TYPE;
32759 
32760   ex_logging_err        EXCEPTION;
32761 
32762   CURSOR c1(p_batch_id NUMBER) IS
32763     SELECT rowid
32764     FROM   msc_st_reservations
32765     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
32766     AND    sr_instance_code          = v_instance_code
32767     AND    batch_id                  = p_batch_id;
32768 
32769   BEGIN
32770 
32771     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
32772                  (p_app_short_name    => 'MSC',
32773                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
32774                   p_message_text      => lv_message_text,
32775                   p_error_text        => lv_error_text);
32776 
32777     IF lv_return <> 0 THEN
32778       RAISE ex_logging_err;
32779     END IF;
32780 
32781     --Validation check for the table MSC_ST_RESERVATIONS
32782 
32783     --Duplicate records check for the records whose source is other than XML
32784     --Different SQL is used because in XML we can identify the latest records
32785     --whereas in batch load we cannot.
32786 
32787     v_sql_stmt := 01;
32788     lv_sql_stmt :=
32789     'UPDATE    msc_st_reservations msr1 '
32790     ||' SET    process_flag = '||G_ERROR_FLG||','
32791     ||'         error_text   = '||''''||lv_message_text||''''
32792     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_reservations msr2 '
32793     ||'        WHERE msr2.sr_instance_code  = msr1.sr_instance_code'
32794     ||'       AND   msr2.sales_order_number = msr1.sales_order_number'
32795     ||'       AND   msr2.line_num           = msr1.line_num'
32796     ||'       AND   msr2.organization_code  = msr1.organization_code'
32797     ||'       AND   NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
32798     ||'              NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
32799     ||'       AND   msr2.process_flag       = msr1.process_flag'
32800     ||'       AND   NVL(msr2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
32801     ||'       GROUP BY sr_instance_code, sales_order_number,line_num,'
32802     ||'                organization_code,company_name '
32803     ||'        HAVING COUNT(*) >1 )'
32804     ||' AND    msr1.process_flag              = '||G_IN_PROCESS
32805     ||' AND    msr1.sr_instance_code          = :v_instance_code'
32806     ||' AND    msr1.reservation_type          = 1'
32807     ||' AND    NVL(msr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
32808 
32809 
32810       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32811 
32812     EXECUTE IMMEDIATE lv_sql_stmt
32813             USING     v_instance_code;
32814 
32815 
32816     --Duplicate records check for the records whose source is XML
32817     v_sql_stmt := 02;
32818     lv_sql_stmt :=
32819     'UPDATE    msc_st_reservations msr1 '
32820     ||' SET   process_flag = '||G_ERROR_FLG||','
32821     ||'       error_text   = '||''''||lv_message_text||''''
32822     ||' WHERE message_id   < ( SELECT max(message_id) '
32823     ||'       FROM  msc_st_reservations msr2'
32824     ||'       WHERE msr2.sr_instance_code  = msr1.sr_instance_code'
32825     ||'       AND   msr2.sales_order_number = msr1.sales_order_number'
32826     ||'       AND   msr2.line_num           = msr1.line_num'
32827     ||'       AND   msr2.organization_code  = msr1.organization_code'
32828     ||'       AND   NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
32829     ||'              NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
32830     ||'       AND   msr2.process_flag       = msr1.process_flag'
32831     ||'       AND   msr2.reservation_type   = msr1.reservation_type'
32832     ||'       AND   NVL(msr2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
32833     ||' AND   msr1.process_flag            = '||G_IN_PROCESS
32834     ||' AND   msr1.sr_instance_code        = :v_instance_code '
32835      ||' AND    msr1.reservation_type      = 1'
32836     ||' AND   NVL(msr1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
32837 
32838 
32839       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32840 
32841     EXECUTE IMMEDIATE lv_sql_stmt
32842             USING     v_instance_code;
32843 
32844 
32845     /*duplicate records for reservation_type =3 */
32846     lv_sql_stmt :=
32847     'UPDATE    msc_st_reservations msr1 '
32848     ||' SET    process_flag = '||G_ERROR_FLG||','
32849     ||'         error_text   = '||''''||lv_message_text||''''
32850     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_reservations msr2 '
32851     ||'        WHERE msr2.sr_instance_code  = msr1.sr_instance_code'
32852     ||'       AND   msr2.supply_order_number = msr1.supply_order_number'
32853     ||'       AND   msr2.organization_code  = msr1.organization_code'
32854     ||'       AND   msr2.process_flag       = msr1.process_flag'
32855     ||'       AND   NVL(msr2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
32856     ||'       AND   msr2.reservation_type = msr1.reservation_type '
32857     ||'       GROUP BY sr_instance_code, supply_order_number, '
32858     ||'                organization_code '
32859     ||'        HAVING COUNT(*) >1 )'
32860     ||' AND    msr1.process_flag              = '||G_IN_PROCESS
32861     ||' AND    msr1.sr_instance_code          = :v_instance_code'
32862     ||' AND    msr1.reservation_type          = 3'
32863     ||' AND    NVL(msr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
32864 
32865 
32866       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32867 
32868     EXECUTE IMMEDIATE lv_sql_stmt
32869             USING     v_instance_code;
32870 
32871       /*duplicate records for reservation_type =4 */
32872     lv_sql_stmt :=
32873     'UPDATE    msc_st_reservations msr1 '
32874     ||' SET    process_flag = '||G_ERROR_FLG||','
32875     ||'         error_text   = '||''''||lv_message_text||''''
32876     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_reservations msr2 '
32877     ||'        WHERE msr2.sr_instance_code  = msr1.sr_instance_code'
32878     ||'       AND   msr2.supply_order_number = msr1.supply_order_number'
32879     ||'       AND   msr2.purch_line_num = msr1.purch_line_num'
32880     ||'       AND   msr2.organization_code  = msr1.organization_code'
32881     ||'       AND   msr2.process_flag       = msr1.process_flag'
32882     ||'       AND   NVL(msr2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
32883      ||'       AND   NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
32884     ||'              NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
32885     ||'       AND   msr2.reservation_type = msr1.reservation_type '
32886     ||'       GROUP BY sr_instance_code, supply_order_number,purch_line_num,'
32887     ||'                organization_code ,company_name'
32888     ||'        HAVING COUNT(*) >1 )'
32889     ||' AND    msr1.process_flag              = '||G_IN_PROCESS
32890     ||' AND    msr1.sr_instance_code          = :v_instance_code'
32891     ||' AND    msr1.reservation_type          = 4'
32892     ||' AND    NVL(msr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
32893 
32894 
32895       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32896 
32897     EXECUTE IMMEDIATE lv_sql_stmt
32898             USING     v_instance_code;
32899 
32900       /*duplicate records for reservation_type =5 */
32901     lv_sql_stmt :=
32902     'UPDATE    msc_st_reservations msr1 '
32903     ||' SET    process_flag = '||G_ERROR_FLG||','
32904     ||'         error_text   = '||''''||lv_message_text||''''
32905     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_reservations msr2 '
32906     ||'        WHERE msr2.sr_instance_code  = msr1.sr_instance_code'
32907     ||'       AND   msr2.supply_order_number = msr1.supply_order_number'
32908     ||'       AND   msr2.organization_code  = msr1.organization_code'
32909     ||'       AND   msr2.supply_org_code  = msr1.supply_org_code'
32910     ||'       AND   msr2.process_flag       = msr1.process_flag'
32911     ||'       AND   NVL(msr2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
32912      ||'       AND   NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
32913     ||'              NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
32914     ||'       AND   msr2.reservation_type = msr1.reservation_type '
32915     ||'       GROUP BY sr_instance_code, supply_order_number,'
32916     ||'                organization_code ,supply_org_code,company_name'
32917     ||'        HAVING COUNT(*) >1 )'
32918     ||' AND    msr1.process_flag              = '||G_IN_PROCESS
32919     ||' AND    msr1.sr_instance_code          = :v_instance_code'
32920     ||' AND    msr1.reservation_type          = 5'
32921     ||' AND    NVL(msr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
32922 
32923 
32924 
32925 
32926       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32927 
32928     EXECUTE IMMEDIATE lv_sql_stmt
32929             USING     v_instance_code;
32930 
32931      /*duplicate records for reservation_type = 7 */
32932 
32933     lv_sql_stmt :=
32934     'UPDATE    msc_st_reservations msr1 '
32935     ||' SET    process_flag = '||G_ERROR_FLG||','
32936     ||'         error_text   = '||''''||lv_message_text||''''
32937     ||' WHERE  EXISTS ( SELECT 1 FROM msc_st_reservations msr2 '
32938     ||'        WHERE msr2.sr_instance_code  = msr1.sr_instance_code'
32939     ||'       AND   msr2.supply_order_number = msr1.supply_order_number'
32940     ||'       AND   msr2.organization_code  = msr1.organization_code'
32941     ||'       AND   msr2.process_flag       = msr1.process_flag'
32942     ||'       AND   NVL(msr2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
32943      ||'       AND   NVL(msr2.company_name,  '||''''||NULL_CHAR||''''||') = '
32944     ||'              NVL(msr1.company_name,  '||''''||NULL_CHAR||''''||')'
32945     ||'       AND   msr2.reservation_type = msr1.reservation_type '
32946     ||'       GROUP BY sr_instance_code, supply_order_number,'
32947     ||'                organization_code ,company_name'
32948     ||'        HAVING COUNT(*) >1 )'
32949     ||' AND    msr1.process_flag              = '||G_IN_PROCESS
32950     ||' AND    msr1.sr_instance_code          = :v_instance_code'
32951     ||' AND    msr1.reservation_type          = 7'
32952     ||' AND    NVL(msr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
32953 
32954 
32955       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32956 
32957     EXECUTE IMMEDIATE lv_sql_stmt
32958             USING     v_instance_code;
32959 
32960     lv_column_names :=
32961     '  ITEM_NAME                ||''~''||'
32962     ||' ORGANIZATION_CODE       ||''~''||'
32963     ||' SALES_ORDER_NUMBER      ||''~''||'
32964     ||' LINE_NUM                ||''~''||'
32965     ||' REQUIREMENT_DATE        ||''~''||'
32966     ||' RESERVED_QUANTITY       ||''~''||'
32967     ||' SUBINVENTORY            ||''~''||'
32968     ||' DEMAND_CLASS            ||''~''||'
32969     ||' PROJECT_NUMBER          ||''~''||'
32970     ||' TASK_NUMBER             ||''~''||'
32971     ||' PLANNING_GROUP          ||''~''||'
32972     ||' COMPANY_NAME            ||''~''||'
32973     ||' SR_INSTANCE_CODE        ||''~''||'
32974     ||' DELETED_FLAG  ';
32975 
32976 
32977     -- Dividing records in batches based on batchsize
32978     LOOP
32979       v_sql_stmt := 03;
32980       SELECT       msc_st_batch_id_s.NEXTVAL
32981       INTO         lv_batch_id
32982       FROM         DUAL;
32983 
32984       v_sql_stmt := 04;
32985       lv_sql_stmt :=
32986       'UPDATE   msc_st_reservations '
32987       ||' SET   batch_id                       = :lv_batch_id '
32988       ||' WHERE process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
32989       ||' AND   sr_instance_code               = :v_instance_code'
32990       ||' AND   NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
32991       ||' AND    rownum                        <= '||v_batch_size;
32992 
32993 
32994         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
32995 
32996       EXECUTE IMMEDIATE lv_sql_stmt
32997               USING     lv_batch_id,
32998                         v_instance_code;
32999 
33000       EXIT WHEN SQL%NOTFOUND ;
33001 
33002       OPEN  c1(lv_batch_id);
33003       FETCH c1 BULK COLLECT INTO lb_rowid;
33004       CLOSE c1;
33005 
33006       v_sql_stmt := 05;
33007       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
33008       UPDATE msc_st_reservations
33009       SET  st_transaction_id = msc_st_demand_classes_s.NEXTVAL,
33010            refresh_id        = v_refresh_id,
33011            last_update_date  = v_current_date,
33012            last_updated_by   = v_current_user,
33013            creation_date     = v_current_date,
33014            created_by        = v_current_user
33015       WHERE  rowid           = lb_rowid(j);
33016 
33017 
33018       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33019                      (p_app_short_name    => 'MSC',
33020                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
33021                       p_message_text      => lv_message_text,
33022                       p_error_text        => lv_error_text,
33023                       p_token1            => 'COLUMN_NAME',
33024                       p_token_value1      => 'SALE_ORDER_NUMBER OR LINE_NUM');
33025 
33026       IF lv_return <> 0 THEN
33027         RAISE ex_logging_err;
33028       END IF;
33029 
33030     v_sql_stmt := 04;
33031 
33032     lv_sql_stmt :=
33033     'UPDATE    msc_st_reservations '
33034     ||' SET    process_flag  = '||G_ERROR_FLG||','
33035     ||'         error_text   = '||''''||lv_message_text||''''
33036     ||' WHERE  ( NVL(line_num,'||NULL_VALUE||') = '||NULL_VALUE
33037     ||' OR     NVL(sales_order_number,'||''''||NULL_CHAR||''''||') '
33038     ||'         = '||''''||NULL_CHAR||''''||')'
33039     ||' AND   process_flag             = '||G_IN_PROCESS
33040     ||' AND   batch_id                 = :lv_batch_id'
33041     ||' AND   reservation_type         =1 '
33042     ||' AND   sr_instance_code         = :v_instance_code';
33043 
33044 
33045 
33046         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33047 
33048       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33049 
33050        lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33051                      (p_app_short_name    => 'MSC',
33052                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
33053                       p_message_text      => lv_message_text,
33054                       p_error_text        => lv_error_text,
33055                       p_token1            => 'COLUMN_NAME',
33056                       p_token_value1      => 'SUPPLY_ORDER_NUMBER');
33057 
33058       IF lv_return <> 0 THEN
33059         RAISE ex_logging_err;
33060       END IF;
33061 
33062 
33063      lv_sql_stmt :=
33064     'UPDATE    msc_st_reservations '
33065     ||' SET    process_flag  = '||G_ERROR_FLG||','
33066     ||'         error_text   = '||''''||lv_message_text||''''
33067       ||' WHERE   NVL(supply_order_number,'||''''||NULL_CHAR||''''||') = '||''''||NULL_CHAR||''''||''
33068     ||' AND   process_flag             = '||G_IN_PROCESS
33069     ||' AND   batch_id                 = :lv_batch_id'
33070     ||' AND   reservation_type         in (3,4,5,7) '
33071     ||' AND   sr_instance_code         = :v_instance_code';
33072 
33073 
33074 
33075         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33076 
33077       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33078 
33079         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33080                      (p_app_short_name    => 'MSC',
33081                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
33082                       p_message_text      => lv_message_text,
33083                       p_error_text        => lv_error_text,
33084                       p_token1            => 'COLUMN_NAME',
33085                       p_token_value1      => 'PURCH_LINE_NUMBER');
33086 
33087       IF lv_return <> 0 THEN
33088         RAISE ex_logging_err;
33089       END IF;
33090 
33091 
33092       lv_sql_stmt :=
33093     'UPDATE    msc_st_reservations '
33094     ||' SET    process_flag  = '||G_ERROR_FLG||','
33095     ||'         error_text   = '||''''||lv_message_text||''''
33096     ||' WHERE   NVL(purch_line_num,'||NULL_VALUE||') = '||NULL_VALUE
33097     ||' AND   process_flag             = '||G_IN_PROCESS
33098     ||' AND   batch_id                 = :lv_batch_id'
33099     ||' AND   reservation_type         = 4 '
33100     ||' AND   sr_instance_code         = :v_instance_code';
33101 
33102 
33103 
33104         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33105 
33106       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33107 
33108         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33109                      (p_app_short_name    => 'MSC',
33110                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
33111                       p_message_text      => lv_message_text,
33112                       p_error_text        => lv_error_text,
33113                       p_token1            => 'COLUMN_NAME',
33114                       p_token_value1      => 'SUPPLY_ORG_CODE');
33115 
33116       IF lv_return <> 0 THEN
33117         RAISE ex_logging_err;
33118       END IF;
33119 
33120 
33121         lv_sql_stmt :=
33122     'UPDATE    msc_st_reservations '
33123     ||' SET    process_flag  = '||G_ERROR_FLG||','
33124     ||'         error_text   = '||''''||lv_message_text||''''
33125     ||' WHERE   NVL(supply_org_code,'||''''||NULL_CHAR||''''||') = '||''''||NULL_CHAR||''''||''
33126     ||' AND   process_flag             = '||G_IN_PROCESS
33127     ||' AND   batch_id                 = :lv_batch_id'
33128     ||' AND   reservation_type         = 5 '
33129     ||' AND   sr_instance_code         = :v_instance_code';
33130 
33131 
33132 
33133         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33134 
33135       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33136 
33137 
33138     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33139                      (p_app_short_name    => 'MSC',
33140                       p_error_code        => 'MSC_PP_INVALID_VALUE',
33141                       p_message_text      => lv_message_text,
33142                       p_error_text        => lv_error_text,
33143                       p_token1            => 'COLUMN_NAME',
33144                       p_token_value1      => 'DEMAND_CLASS');
33145 
33146       IF lv_return <> 0 THEN
33147         RAISE ex_logging_err;
33148       END IF;
33149 
33150 
33151       -- Validate Demand Class, if value provided it should exists
33152       -- in ODS or staging table
33153 
33154      lv_return :=
33155      MSC_ST_UTIL.VALIDATE_DMD_CLASS(p_table_name       => 'MSC_ST_RESERVATIONS',
33156                                     p_dmd_class_column => 'DEMAND_CLASS',
33157                                     p_instance_id      => v_instance_id,
33158                                     p_instance_code    => v_instance_code,
33159                                     p_severity         => G_SEV3_ERROR,
33160                                     p_message_text     => lv_message_text,
33161                                     p_batch_id         => lv_batch_id,
33162                                     p_debug            => v_debug,
33163                                     p_error_text       => lv_error_text);
33164 
33165      if(lv_return <> 0) then
33166         RAISE ex_logging_err;
33167      end if;
33168 
33169 
33170 
33171       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33172                      (p_app_short_name    => 'MSC',
33173                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
33174                       p_message_text      => lv_message_text,
33175                       p_error_text        => lv_error_text,
33176                       p_token1            => 'COLUMN_NAME',
33177                       p_token_value1      => 'DELETED_FLAG',
33178                       p_token2            => 'DEFAULT_VALUE',
33179                       p_token_value2      => SYS_NO);
33180 
33181       IF lv_return <> 0 THEN
33182         RAISE ex_logging_err;
33183       END IF;
33184 
33185       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
33186       -- Log error procedure inserts warning and also defaults the delete flag
33187 
33188       lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') '
33189                       ||' NOT IN(1,2)';
33190 
33191       lv_return := MSC_ST_UTIL.LOG_ERROR
33192                         (p_table_name        => 'MSC_ST_RESERVATIONS',
33193                          p_instance_code     => v_instance_code,
33194                          p_row               => lv_column_names,
33195                          p_severity          => G_SEV_WARNING,
33196                          p_message_text      => lv_message_text,
33197                          p_error_text        => lv_error_text,
33198                          p_batch_id          => lv_batch_id,
33199                          p_where_str         => lv_where_str,
33200                          p_col_name          => 'DELETED_FLAG',
33201                          p_debug             => v_debug,
33202                          p_default_value     => SYS_NO);
33203 
33204       IF lv_return <> 0 THEN
33205         RAISE ex_logging_err;
33206       END IF;
33207 
33208 
33209 
33210     lv_sql_stmt :=
33211     'UPDATE    msc_st_reservations '
33212     ||' SET   supply_source_type_id = 200,'
33213     ||'       disposition_type =2 '
33214     ||' WHERE  process_flag             = '||G_IN_PROCESS
33215     ||' AND   batch_id                 = :lv_batch_id'
33216     ||' AND   reservation_type         = 3 '
33217     ||' AND   sr_instance_code         = :v_instance_code';
33218 
33219 
33220 
33221         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33222 
33223       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33224 
33225        lv_sql_stmt :=
33226     'UPDATE    msc_st_reservations '
33227     ||' SET   supply_source_type_id = 7,'
33228     ||'       disposition_type =200 '
33229     ||' WHERE  process_flag             = '||G_IN_PROCESS
33230     ||' AND   batch_id                 = :lv_batch_id'
33231     ||' AND   reservation_type         = 4 '
33232     ||' AND   sr_instance_code         = :v_instance_code';
33233 
33234 
33235 
33236         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33237 
33238       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33239 
33240       lv_sql_stmt :=
33241     'UPDATE    msc_st_reservations '
33242     ||' SET   supply_source_type_id = 5,'
33243     ||'       disposition_type =1 '
33244     ||' WHERE  process_flag             = '||G_IN_PROCESS
33245     ||' AND   batch_id                 = :lv_batch_id'
33246     ||' AND   reservation_type         = 5 '
33247     ||' AND   sr_instance_code         = :v_instance_code';
33248 
33249 
33250 
33251         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33252 
33253       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33254 
33255      lv_sql_stmt :=
33256     'UPDATE    msc_st_reservations '
33257     ||' SET   supply_source_type_id = 1,'
33258     ||'       disposition_type =2 '
33259     ||' WHERE  process_flag             = '||G_IN_PROCESS
33260     ||' AND   batch_id                 = :lv_batch_id'
33261     ||' AND   reservation_type         = 7 '
33262     ||' AND   sr_instance_code         = :v_instance_code';
33263 
33264 
33265 
33266         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33267 
33268     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33269 
33270       -- Check DISPOSITION_TYPE column has valid value or not and if not set it to 2
33271         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33272                      (p_app_short_name    => 'MSC',
33273                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
33274                       p_message_text      => lv_message_text,
33275                       p_error_text        => lv_error_text,
33276                       p_token1            => 'COLUMN_NAME',
33277                       p_token_value1      => 'DISPOSITION_TYPE',
33278                       p_token2            => 'DEFAULT_VALUE',
33279                       p_token_value2      =>  G_DISPOSITION_TYPE);
33280 
33281       IF lv_return <> 0 THEN
33282         RAISE ex_logging_err;
33283       END IF;
33284 
33285       lv_where_str := ' AND NVL(disposition_type,'||NULL_VALUE||') '
33286                       ||' <> 2 AND RESERVATION_TYPE =1';
33287 
33288       lv_return := MSC_ST_UTIL.LOG_ERROR
33289                         (p_table_name        => 'MSC_ST_RESERVATIONS',
33290                          p_instance_code     => v_instance_code,
33291                          p_row               => lv_column_names,
33292                          p_severity          => G_SEV_WARNING,
33293                          p_message_text      => lv_message_text,
33294                          p_error_text        => lv_error_text,
33295                          p_batch_id          => lv_batch_id,
33296                          p_where_str         => lv_where_str,
33297                          p_col_name          => 'DISPOSITION_TYPE',
33298                          p_debug             => v_debug,
33299                          p_default_value     => G_DISPOSITION_TYPE);
33300 
33301       IF lv_return <> 0 THEN
33302         RAISE ex_logging_err;
33303       END IF;
33304 
33305      /* lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33306                      (p_app_short_name    => 'MSC',
33307                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
33308                       p_message_text      => lv_message_text,
33309                       p_error_text        => lv_error_text,
33310                       p_token1            => 'COLUMN_NAME',
33311                       p_token_value1      => 'RESERVATION_TYPE',
33312                       p_token2            => 'DEFAULT_VALUE',
33313                       p_token_value2      =>  G_RESERVATION_TYPE);
33314 
33315       IF lv_return <> 0 THEN
33316         RAISE ex_logging_err;
33317       END IF;
33318 
33319       -- Check RESERVATION_TYPE column has valid value or not and if not set it to 2
33320 
33321       lv_where_str := ' AND NVL(reservation_type,'||NULL_VALUE||') '
33322                       ||' <> 1 ';
33323 
33324       lv_return := MSC_ST_UTIL.LOG_ERROR
33325                         (p_table_name        => 'MSC_ST_RESERVATIONS',
33326                          p_instance_code     => v_instance_code,
33327                          p_row               => lv_column_names,
33328                          p_severity          => G_SEV_WARNING,
33329                          p_message_text      => lv_message_text,
33330                          p_error_text        => lv_error_text,
33331                          p_batch_id          => lv_batch_id,
33332                          p_where_str         => lv_where_str,
33333                          p_col_name          => 'RESERVATION_TYPE',
33334                          p_debug             => v_debug,
33335                          p_default_value     => G_RESERVATION_TYPE);
33336 
33337       IF lv_return <> 0 THEN
33338         RAISE ex_logging_err;
33339       END IF;
33340     */
33341      -- Set the  message
33342 
33343      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33344                      (p_app_short_name    => 'MSC',
33345                       p_error_code        => 'MSC_PP_INVALID_VALUE',
33346                       p_message_text      => lv_message_text,
33347                       p_error_text        => lv_error_text,
33348                       p_token1            => 'COLUMN_NAME',
33349                       p_token_value1      => 'ORGANIZATION_CODE');
33350 
33351       IF lv_return <> 0 THEN
33352         RAISE ex_logging_err;
33353       END IF;
33354 
33355      --Derive Organization_id
33356     lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
33357                    (p_table_name       => 'MSC_ST_RESERVATIONS',
33358                     p_org_partner_name => 'ORGANIZATION_CODE',
33359                     p_org_partner_id   => 'ORGANIZATION_ID',
33360                     p_instance_code    => v_instance_code,
33361                     p_partner_type     => G_ORGANIZATION,
33362                     p_error_text       => lv_error_text,
33363                     p_batch_id         => lv_batch_id,
33364                     p_severity         => G_SEV_ERROR,
33365                     p_message_text     => lv_message_text,
33366                     p_debug            => v_debug,
33367                     p_row              => lv_column_names);
33368 
33369     IF lv_return <> 0 THEN
33370         RAISE ex_logging_err;
33371     END IF;
33372 
33373 
33374      -- Set the  message
33375 
33376      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33377                      (p_app_short_name    => 'MSC',
33378                       p_error_code        => 'MSC_PP_INVALID_VALUE',
33379                       p_message_text      => lv_message_text,
33380                       p_error_text        => lv_error_text,
33381                       p_token1            => 'COLUMN_NAME',
33382                       p_token_value1      => 'ITEM_NAME');
33383 
33384       IF lv_return <> 0 THEN
33385         RAISE ex_logging_err;
33386       END IF;
33387 
33388     --Derive Inventory_item_id
33389     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
33390                    (p_table_name       => 'MSC_ST_RESERVATIONS',
33391                     p_item_col_name    => 'ITEM_NAME',
33392                     p_item_col_id      => 'INVENTORY_ITEM_ID',
33393                     p_instance_id      => v_instance_id,
33394                     p_instance_code    => v_instance_code,
33395                     p_error_text       => lv_error_text,
33396                     p_batch_id         => lv_batch_id,
33397                     p_severity         => G_SEV_ERROR,
33398                     p_message_text     => lv_message_text,
33399                     p_debug            => v_debug,
33400                     p_row              => lv_column_names);
33401 
33402     IF lv_return <> 0 THEN
33403         RAISE ex_logging_err;
33404     END IF;
33405 
33406     -- Derive dispostion id
33407 
33408     v_sql_stmt := 5.0;
33409    lv_sql_stmt :=
33410    'UPDATE msc_st_reservations msr '
33411     ||'  SET  disposition_id = (SELECT local_id'
33412     ||'       FROM   msc_local_id_demand mlid'
33413     ||'       WHERE  mlid.char1    = msr.sr_instance_code'
33414     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
33415     ||'       =     NVL(msr.company_name,'||''''||NULL_CHAR||''''||') '
33416     ||'       AND    mlid.char3    = msr.organization_code'
33417     ||'       AND    mlid.char4    = msr.sales_order_number'
33418         ||'       AND    mlid.instance_id = '||v_instance_id
33419     ||'       AND    mlid.entity_name = ''SALES_ORDER_ID'' )'
33420     ||' WHERE process_flag      = '||G_IN_PROCESS
33421     ||' AND   NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
33422     ||' AND   (RESERVATION_TYPE in (1,2) or RESERVATION_TYPE is null) '
33423     ||' AND   batch_id          = :lv_batch_id '
33424     ||' AND   sr_instance_code  =:v_instance_code';
33425 
33426 
33427       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33428 
33429     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33430 
33431     /* derive disposition_id for reservation_type in (3,7) */
33432      v_sql_stmt := 5.1;
33433    lv_sql_stmt :=
33434    'UPDATE msc_st_reservations msr '
33435     ||'  SET  disposition_id = (SELECT local_id'
33436     ||'       FROM   msc_local_id_demand mlid'
33437     ||'       WHERE  mlid.char1    = msr.sr_instance_code'
33438     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
33439     ||'       =     NVL(msr.company_name,'||''''||NULL_CHAR||''''||') '
33440     ||'       AND    mlid.char3    = msr.organization_code'
33441     ||'       AND    mlid.char4    = msr.sales_order_number'
33442     ||'       AND    nvl(mlid.number2,'||NULL_VALUE||') = 1'
33443     ||'       AND    mlid.number1     = msr.line_num'
33444     ||'       AND    mlid.entity_name = ''DEMAND_ID'' )'
33445     ||' WHERE process_flag      = '||G_IN_PROCESS
33446     ||' AND   NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
33447     ||' AND   RESERVATION_TYPE in (3,7) '
33448     ||' AND   batch_id          = :lv_batch_id '
33449     ||' AND   sr_instance_code  =:v_instance_code';
33450 
33451 
33452       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33453 
33454     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33455 
33456      /* derive disposition_id for reservation_type =4 */
33457      v_sql_stmt := 5.2;
33458    lv_sql_stmt :=
33459    'UPDATE msc_st_reservations msr '
33460     ||'  SET  disposition_id =(SELECT local_id'
33461     ||'       FROM   msc_local_id_supply mls'
33462     ||'       WHERE  mls.char4 = msr.demand_order_number'
33463     ||'       AND    mls.char3 = msr.organization_code'
33464     ||'       AND    mls.char1 = msr.sr_instance_code'
33465     ||'       AND    mls.entity_name = ''REPAIR_NUMBER'' )'
33466     ||' WHERE process_flag      = '||G_IN_PROCESS
33467     ||' AND   NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
33468     ||' AND   RESERVATION_TYPE =4 '
33469     ||' AND   batch_id          = :lv_batch_id '
33470     ||' AND   sr_instance_code  =:v_instance_code';
33471 
33472 
33473       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33474 
33475     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33476 
33477      /* derive disposition_id for reservation_type =5 */
33478      v_sql_stmt := 5.3;
33479    lv_sql_stmt :=
33480    'UPDATE msc_st_reservations msr '
33481     ||'  SET  disposition_id =(SELECT local_id'
33482     ||'       FROM   msc_local_id_supply mls'
33483     ||'       WHERE  mls.char3       = msr.demand_order_number'
33484     ||'       AND    mls.number1     = 74' -- order_type
33485     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
33486     ||'              NVL(msr.company_name,'||''''||NULL_CHAR||''''||') '
33487     ||'       AND    mls.char1       = msr.sr_instance_code'
33488     ||'       AND    mls.entity_name = ''DISPOSITION_ID'' ) '
33489     ||' WHERE process_flag      = '||G_IN_PROCESS
33490     ||' AND   NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
33491     ||' AND   RESERVATION_TYPE =5 '
33492     ||' AND   batch_id          = :lv_batch_id '
33493     ||' AND   sr_instance_code  =:v_instance_code';
33494 
33495 
33496       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33497 
33498     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33499 
33500 
33501     /* derieve transaction_id for reservation_type =3 */
33502      v_sql_stmt := 5.5;
33503    lv_sql_stmt :=
33504    'UPDATE msc_st_reservations msr '
33505     ||'  SET  transaction_id =(SELECT local_id'
33506     ||'       FROM   msc_local_id_supply mls'
33507     ||'       WHERE  mls.char4 = msr.supply_order_number'
33508     ||'       AND    mls.char3 = msr.organization_code'
33509     ||'       AND    mls.char1 = msr.sr_instance_code'
33510     ||'       AND    mls.entity_name = ''REPAIR_NUMBER'' )'
33511     ||' WHERE process_flag      = '||G_IN_PROCESS
33512     ||' AND   NVL(transaction_id,'||NULL_VALUE||') = '||NULL_VALUE
33513     ||' AND   RESERVATION_TYPE =3 '
33514     ||' AND   batch_id          = :lv_batch_id '
33515     ||' AND   sr_instance_code  =:v_instance_code';
33516 
33517 
33518       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33519 
33520     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33521 
33522     /* derieve transaction_id for reservation_type =4 */
33523      v_sql_stmt := 5.5;
33524    lv_sql_stmt :=
33525    'UPDATE msc_st_reservations msr '
33526     ||'  SET  transaction_id =(SELECT local_id'
33527     ||'       FROM   msc_local_id_supply mls'
33528     ||'       WHERE  mls.number1  = msr.purch_line_num'
33529     ||'       AND    mls.number2  =2' -- order_type
33530     ||'       AND    mls.char3    = msr.supply_order_number'
33531     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
33532     ||'              NVL(msr.company_name,'||''''||NULL_CHAR||''''||') '
33533     ||'       AND    mls.char1 = msr.sr_instance_code'
33534     ||'       AND    mls.entity_name = ''PO_LINE_ID'')'
33535     ||' WHERE process_flag      = '||G_IN_PROCESS
33536     ||' AND   NVL(transaction_id,'||NULL_VALUE||') = '||NULL_VALUE
33537     ||' AND   RESERVATION_TYPE =4 '
33538     ||' AND   batch_id          = :lv_batch_id '
33539     ||' AND   sr_instance_code  =:v_instance_code';
33540 
33541 
33542       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33543 
33544     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33545 
33546      /* derieve transaction_id for reservation_type =5 */
33547      v_sql_stmt := 5.6;
33548    lv_sql_stmt :=
33549    'UPDATE msc_st_reservations msr '
33550     ||'  SET  transaction_id =(SELECT local_id'
33551       ||'       FROM   msc_local_id_supply mls'
33552       ||'       WHERE  mls.char4 = msr.supply_order_number'
33553       ||'       AND    mls.char3 = msr.supply_org_code'
33554       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
33555       ||'              NVL(msr.company_name,'||''''||NULL_CHAR||''''||') '
33556       ||'       AND    mls.char1 = msr.sr_instance_code'
33557       ||'       AND    mls.entity_name = ''WIP_ENTITY_ID'' )'
33558     ||' WHERE process_flag      = '||G_IN_PROCESS
33559     ||' AND   NVL(transaction_id,'||NULL_VALUE||') = '||NULL_VALUE
33560     ||' AND   RESERVATION_TYPE =5 '
33561     ||' AND   batch_id          = :lv_batch_id '
33562     ||' AND   sr_instance_code  =:v_instance_code';
33563 
33564 
33565       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33566 
33567     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33568 
33569       /* derieve transaction_id for reservation_type =7*/
33570      v_sql_stmt := 5.6;
33571    lv_sql_stmt :=
33572    'UPDATE msc_st_reservations msr '
33573     ||'  SET  transaction_id =(SELECT local_id'
33574     ||'       FROM   msc_local_id_supply mls'
33575     ||'       WHERE  mls.char3       = msr.supply_order_number'
33576     ||'       AND    mls.number1     = 74' -- order_type
33577     ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
33578     ||'              NVL(msr.company_name,'||''''||NULL_CHAR||''''||') '
33579     ||'       AND    mls.char1       = msr.sr_instance_code'
33580     ||'       AND    mls.entity_name = ''DISPOSITION_ID'' ) '
33581     ||' WHERE process_flag      = '||G_IN_PROCESS
33582     ||' AND   NVL(transaction_id,'||NULL_VALUE||') = '||NULL_VALUE
33583     ||' AND   RESERVATION_TYPE =7'
33584     ||' AND   batch_id          = :lv_batch_id '
33585     ||' AND   sr_instance_code  =:v_instance_code';
33586 
33587 
33588       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33589 
33590     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33591 
33592     -- Derive parent_demand_id
33593 
33594    v_sql_stmt := 06;
33595    lv_sql_stmt :=
33596    'UPDATE msc_st_reservations msr '
33597     ||'  SET  parent_demand_id = (SELECT local_id'
33598     ||'       FROM   msc_local_id_demand mlid'
33599     ||'       WHERE  mlid.char1    = msr.sr_instance_code'
33600     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
33601     ||'       =     NVL(msr.company_name,'||''''||NULL_CHAR||''''||') '
33602     ||'       AND    mlid.char3    = msr.organization_code'
33603     ||'       AND    mlid.char4    = msr.sales_order_number'
33604     ||'       AND    nvl(mlid.number2,'||NULL_VALUE||') = nvl(msr.reservation_type,'||NULL_VALUE||')'
33605     ||'       AND   NVL(mlid.char5,'||''''||NULL_CHAR||''''||') '
33606     ||'       =     NVL(msr.SUBINVENTORY,'||''''||NULL_CHAR||''''||') '
33607     ||'       AND    mlid.number1     = msr.line_num'
33608     ||'       AND    mlid.entity_name = ''DEMAND_ID'' )'
33609     ||' WHERE process_flag      ='||G_IN_PROCESS
33610     ||' AND   batch_id          = :lv_batch_id '
33611     ||' AND   (RESERVATION_TYPE in (1,2) or RESERVATION_TYPE is null) '
33612     ||' AND   sr_instance_code  =:v_instance_code';
33613 
33614 
33615 
33616       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33617 
33618     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33619 
33620     -- Error out the records if any parent_demand_id or dispostion id
33621     -- derivation fails
33622 
33623    -- Set the error message
33624    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33625                      (p_app_short_name    => 'MSC',
33626                       p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
33627                       p_message_text      => lv_message_text,
33628                       p_error_text        => lv_error_text,
33629                       p_token1            => 'COLUMN_NAMES',
33630                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
33631                                              ||'ORGANIZATION_CODE,SALES_ORDER_NUMBER'
33632                                              ||' LINE_NUM',
33633                       p_token2            => 'MASTER_TABLE');
33634 
33635       IF lv_return <> 0 THEN
33636         RAISE ex_logging_err;
33637       END IF;
33638 
33639      v_sql_stmt := 07;
33640 
33641    lv_sql_stmt :=
33642    'UPDATE      msc_st_reservations '
33643    ||' SET      process_flag    ='||G_ERROR_FLG||','
33644    ||'          error_text   = '||''''||lv_message_text||''''
33645    ||' WHERE    (NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
33646    ||' OR       NVL(parent_demand_id,'||NULL_VALUE||') = '||NULL_VALUE||')'
33647    ||' AND      process_flag      ='||G_IN_PROCESS
33648    ||' AND      batch_id          = :lv_batch_id '
33649     ||' AND   (RESERVATION_TYPE in (1,2) or RESERVATION_TYPE is null) '
33650    ||' AND      sr_instance_code  =:v_instance_code';
33651 
33652 
33653       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33654 
33655     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33656 
33657     v_sql_stmt := 07.1;
33658      lv_sql_stmt :=
33659    'UPDATE      msc_st_reservations '
33660    ||' SET      process_flag    ='||G_ERROR_FLG||','
33661    ||'          error_text   = '||''''||lv_message_text||''''
33662    ||' WHERE    (NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
33663    ||' OR       NVL(transaction_id,'||NULL_VALUE||') = '||NULL_VALUE||')'
33664    ||' AND      process_flag      ='||G_IN_PROCESS
33665    ||' AND      batch_id          = :lv_batch_id '
33666     ||' AND   RESERVATION_TYPE in (3,4,5,7)  '
33667    ||' AND      sr_instance_code  =:v_instance_code';
33668 
33669 
33670       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33671 
33672     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33673 
33674    -- Update transaction_id by parent_demand_id
33675 
33676      v_sql_stmt := 07;
33677 
33678    lv_sql_stmt :=
33679    'UPDATE      msc_st_reservations '
33680    ||' SET      transaction_id  = parent_demand_id'
33681    ||' WHERE    NVL(parent_demand_id,'||NULL_VALUE||') <> '||NULL_VALUE
33682    ||' AND      process_flag      ='||G_IN_PROCESS
33683    ||' AND      batch_id          = :lv_batch_id '
33684     ||' AND   (RESERVATION_TYPE in (1,2) or RESERVATION_TYPE is null) '
33685    ||' AND      sr_instance_code  =:v_instance_code';
33686 
33687 
33688       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
33689 
33690     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
33691 
33692 
33693 
33694    -- Set the error message
33695    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33696                      (p_app_short_name    => 'MSC',
33697                       p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
33698                       p_message_text      => lv_message_text,
33699                       p_error_text        => lv_error_text,
33700                       p_token1            => 'COLUMN_NAMES',
33701                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
33702                                              ||'ORGANIZATION_CODE,PROJECT_NUMBER',
33703                       p_token2            => 'MASTER_TABLE');
33704 
33705       IF lv_return <> 0 THEN
33706         RAISE ex_logging_err;
33707       END IF;
33708 
33709    --Derive Project Id.
33710     lv_return:= MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
33711                            (p_table_name          => 'MSC_ST_RESERVATIONS',
33712                             p_proj_col_name       => 'PROJECT_NUMBER',
33713                             p_proj_task_col_id    => 'PROJECT_ID',
33714                             p_instance_code       =>  v_instance_code,
33715                             p_entity_name         => 'PROJECT_ID',
33716                             p_error_text          =>  lv_error_text,
33717                             p_batch_id            =>  lv_batch_id,
33718                             p_severity            =>  G_SEV_WARNING,
33719                             p_message_text        =>  lv_message_text,
33720                             p_debug               => v_debug,
33721                             p_row                 =>  lv_column_names);
33722     IF lv_return<> 0 THEN
33723       RAISE ex_logging_err;
33724     END IF;
33725 
33726     -- Set the error message
33727    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33728                      (p_app_short_name    => 'MSC',
33729                       p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
33730                       p_message_text      => lv_message_text,
33731                       p_error_text        => lv_error_text,
33732                       p_token1            => 'COLUMN_NAMES',
33733                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
33734                                              ||'ORGANIZATION_CODE,PROJECT_NUMBER'
33735                                              ||'TASK_NUMBER' ,
33736                       p_token2            => 'MASTER_TABLE',
33737                       p_token_value2      =>  'MSC_ST_PROJECT_TASKS');
33738 
33739       IF lv_return <> 0 THEN
33740         RAISE ex_logging_err;
33741       END IF;
33742 
33743     --Derive Task Id.
33744     lv_return:= MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
33745                            (p_table_name          => 'MSC_ST_RESERVATIONS',
33746                             p_proj_col_name       => 'PROJECT_NUMBER',
33747                             p_proj_task_col_id    => 'TASK_ID',
33748                             p_instance_code       => v_instance_code,
33749                             p_entity_name         => 'TASK_ID',
33750                             p_error_text          => lv_error_text,
33751                             p_task_col_name       => 'TASK_NUMBER',
33752                             p_batch_id            => lv_batch_id,
33753                             p_severity            => G_SEV_WARNING,
33754                             p_message_text        => lv_message_text,
33755                             p_debug               => v_debug,
33756                             p_row                 => lv_column_names);
33757 
33758     IF lv_return<> 0 THEN
33759       RAISE ex_logging_err;
33760     END IF;
33761 
33762 
33763 
33764       --Call to customised validation.
33765       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
33766         (ERRBUF         => lv_error_text,
33767          RETCODE        => lv_return,
33768          pBatchID       => lv_batch_id,
33769          pInstanceCode  => v_instance_code,
33770          pEntityName    => 'MSC_ST_RESERVATIONS',
33771          pInstanceID    => v_instance_id);
33772 
33773       IF NVL(lv_return,0) <> 0 THEN
33774         RAISE ex_logging_err;
33775       END IF;
33776 
33777 
33778       -- Set the process flag as Valid and populate instance_id
33779       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
33780                         (p_table_name   => 'MSC_ST_RESERVATIONS',
33781                          p_instance_id    => v_instance_id,
33782                          p_instance_code  => v_instance_code,
33783                          p_process_flag   => G_VALID,
33784                          p_error_text     => lv_error_text,
33785                          p_debug          => v_debug,
33786                          p_batch_id       => lv_batch_id);
33787 
33788       IF lv_return <> 0 THEN
33789         RAISE ex_logging_err;
33790       END IF;
33791 
33792       -- At the end calling  the LOG_ERROR for logging all
33793       -- errored out records.
33794 
33795       lv_return := MSC_ST_UTIL.LOG_ERROR
33796                         (p_table_name    => 'MSC_ST_RESERVATIONS',
33797                          p_instance_code => v_instance_code,
33798                          p_row           => lv_column_names,
33799                          p_severity      => G_SEV_ERROR,
33800                          p_message_text  => NULL,
33801                          p_error_text    => lv_error_text,
33802                          p_debug         => v_debug,
33803                          p_batch_id      => lv_batch_id  );
33804 
33805       IF lv_return <> 0 THEN
33806         RAISE ex_logging_err;
33807       END IF;
33808 
33809      COMMIT;
33810 
33811     END LOOP;
33812 
33813   EXCEPTION
33814 
33815     WHEN too_many_rows THEN
33816       lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_RESRVATIONS'||'('
33817                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
33818       ROLLBACK ;
33819 
33820     WHEN ex_logging_err THEN
33821         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
33822 
33823       ROLLBACK;
33824 
33825     WHEN OTHERS THEN
33826       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_RESERVATIONS '||'('
33827                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
33828         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
33829 
33830       ROLLBACK;
33831 
33832   END LOAD_RESERVATIONS ;
33833 
33834 /*==========================================================================+
33835 | DESCRIPTION  : This procedure is called for validating demand related     |
33836 |                records from the msc_st_demands table. The following       |
33837 |                types are considered for the validation.                   |
33838 |                Demand origination          Origination Type               |
33839 |                -------------------------------------------------          |
33840 |                MDS Demand                       8                         |
33841 |                Forecast Demand                  29                        |
33842 |	 	Forecast MDS             	   7	                    |
33843 +==========================================================================*/
33844   PROCEDURE  LOAD_MDS_FORECAST (p_batch_id  IN NUMBER ) IS
33845   TYPE RowidTab      IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
33846   lb_rowid           RowidTab;
33847 
33848   lv_column_names    VARCHAR2(5000);     -- Stores cocatenated column names
33849   lv_return          NUMBER;
33850   lv_error_text      VARCHAR2(250);
33851   lv_where_str       VARCHAR2(5000);
33852   lv_sql_stmt        VARCHAR2(5000);
33853   lv_cursor_stmt     VARCHAR2(5000);
33854   lv_message_text    msc_errors.error_text%TYPE;
33855 
33856   ex_logging_err     EXCEPTION;
33857 
33858   CURSOR c1(p_batch_id NUMBER) IS
33859     SELECT rowid
33860     FROM   msc_st_demands
33861     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
33862     AND    origination_type  IN (7,8,29)
33863     AND    batch_id                = p_batch_id
33864     AND    sr_instance_code        = v_instance_code ;
33865 
33866   CURSOR c2(p_batch_id NUMBER) IS
33867     SELECT rowid
33868     FROM   msc_st_demands
33869     WHERE  process_flag     = G_IN_PROCESS
33870     AND    sr_instance_code = v_instance_code
33871     AND    batch_id         = p_batch_id
33872     AND    NVL(disposition_id,NULL_VALUE) = NULL_VALUE
33873     AND    origination_type in(7, 8)
33874     AND    deleted_flag     = SYS_NO;
33875 
33876   CURSOR c3(p_batch_id NUMBER) IS
33877     SELECT rowid
33878     FROM   msc_st_demands
33879     WHERE  process_flag     = G_IN_PROCESS
33880     AND    sr_instance_code = v_instance_code
33881     AND    batch_id         = p_batch_id
33882     AND    NVL(sales_order_line_id,NULL_VALUE) = NULL_VALUE
33883     AND    origination_type = 29
33884     AND    deleted_flag     = SYS_NO;
33885 
33886 
33887   BEGIN
33888 
33889   lv_column_names :=
33890   'ITEM_NAME                        ||''~''||'
33891   ||' ORGANIZATION_CODE             ||''~''||'
33892   ||' USING_ASSEMBLY_ITEM_NAME      ||''~''||'
33893   ||' USING_ASSEMBLY_DEMAND_DATE    ||''~''||'
33894   ||' USING_REQUIREMENT_QUANTITY    ||''~''||'
33895   ||' SCHEDULE_DATE                 ||''~''||'
33896   ||' DAILY_DEMAND_RATE             ||''~''||'
33897   ||' DEMAND_TYPE                   ||''~''||'
33898   ||' SR_INSTANCE_CODE              ||''~''||'
33899   ||' ASSEMBLY_DEMAND_COMP_DATE     ||''~''||'
33900   ||' DEMAND_SCHEDULE_NAME          ||''~''||'
33901   ||' SCHEDULE_LINE_NUM             ||''~''||'
33902   ||' PROJECT_NUMBER                ||''~''||'
33903   ||' TASK_NUMBER                   ||''~''||'
33904   ||' DEMAND_CLASS                  ||''~''||'
33905   ||' DMD_LATENESS_COST             ||''~''||'
33906   ||' DMD_SATISFIED_DATE            ||''~''||'
33907   ||' DMD_SPLIT_FLAG                ||''~''||'
33908   ||' CONFIDENCE_PERCENTAGE         ||''~''||'
33909   ||' BUCKET_TYPE                   ||''~''||'
33910   ||' BILL_CODE                     ||''~''||'
33911   ||' FORECAST_DESIGNATOR           ||''~''||'
33912   ||' CUSTOMER_NAME                 ||''~''||'
33913   ||' FORECAST_MAD                  ||''~''||'
33914   ||' PROBABILITY                   ||''~''||'
33915   ||' ORIGINATION_TYPE              ||''~''||'
33916   ||' END_ITEM_UNIT_NUMBER          ||''~''||'
33917   ||' REQUEST_DATE                  ||''~''||'
33918   ||' DELETED_FLAG                  ||''~''||'
33919   ||'COMPANY_NAME'  ;
33920 
33921     OPEN c1(p_batch_id);
33922     FETCH c1 BULK COLLECT INTO lb_rowid;
33923     CLOSE c1;
33924 
33925     v_sql_stmt := 03;
33926     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
33927     UPDATE msc_st_demands
33928     SET    st_transaction_id   = msc_st_demands_s.NEXTVAL,
33929            refresh_id          = v_refresh_id,
33930            last_update_date    = v_current_date,
33931            last_updated_by     = v_current_user,
33932            creation_date       = v_current_date,
33933            created_by          = v_current_user
33934     WHERE  rowid               = lb_rowid(j);
33935 
33936 
33937     -- Set the error message
33938     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33939                      (p_app_short_name    => 'MSC',
33940                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
33941                       p_message_text      => lv_message_text,
33942                       p_error_text        => lv_error_text,
33943                       p_token1            => 'COLUMN_NAME',
33944                       p_token_value1      => 'DELETED_FLAG',
33945                       p_token2            => 'DEFAULT_VALUE',
33946                       p_token_value2      =>  SYS_NO  );
33947 
33948       IF lv_return <> 0 THEN
33949         RAISE ex_logging_err;
33950       END IF;
33951 
33952     --Log a warning for those records where the deleted_flag has a value other
33953     --SYS_NO or SYS_YES
33954     lv_where_str :=
33955    ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
33956 
33957     lv_return:= MSC_ST_UTIL.LOG_ERROR
33958                    (p_table_name        => 'MSC_ST_DEMANDS',
33959                     p_instance_code     => v_instance_code,
33960                     p_row               => lv_column_names,
33961                     p_severity          => G_SEV_WARNING,
33962                     p_message_text      => lv_message_text,
33963                     p_error_text        => lv_error_text,
33964                     p_batch_id          => p_batch_id,
33965                     p_where_str         => lv_where_str,
33966                     p_col_name          => 'DELETED_FLAG',
33967                     p_debug             => v_debug,
33968                     p_default_value     =>  SYS_NO);
33969 
33970     IF lv_return<> 0 THEN
33971         RAISE ex_logging_err;
33972     END IF;
33973 
33974 
33975 
33976     -- USING_ASSEMBLY_DEMAND_DATE check
33977      -- Set the  message
33978 
33979      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
33980                      (p_app_short_name    => 'MSC',
33981                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
33982                       p_message_text      => lv_message_text,
33983                       p_error_text        => lv_error_text,
33984                       p_token1            => 'COLUMN_NAME',
33985                       p_token_value1      => 'USING_ASSEMBLY_DEMAND_DATE');
33986       IF lv_return <> 0 THEN
33987         RAISE ex_logging_err;
33988       END IF;
33989 
33990   -- Error out records where USING_ASSEMBLY_DEMAND_DATE is NULL
33991 
33992   v_sql_stmt := 04;
33993   lv_sql_stmt :=
33994   'UPDATE     msc_st_demands '
33995   ||' SET     process_flag    =  '||G_ERROR_FLG||','
33996   ||'         error_text   = '||''''||lv_message_text||''''
33997   ||' WHERE NVL(using_assembly_demand_date,SYSDATE-36500) = SYSDATE-36500'
33998   ||' AND   process_flag      = '||G_IN_PROCESS
33999   ||' AND   origination_type  IN (7,8,29)'
34000   ||' AND   batch_id       = :p_batch_id'
34001   ||' AND   sr_instance_code  = :v_instance_code';
34002 
34003 
34004       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34005 
34006   EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
34007 
34008      -- Set the  message
34009 
34010      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34011                      (p_app_short_name    => 'MSC',
34012                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
34013                       p_message_text      => lv_message_text,
34014                       p_error_text        => lv_error_text,
34015                       p_token1            => 'COLUMN_NAME',
34016                       p_token_value1      => 'USING_REQUIREMENT_QUANTITY' );
34017 
34018       IF lv_return <> 0 THEN
34019         RAISE ex_logging_err;
34020       END IF;
34021 
34022 
34023   -- Error out if USING_REQUIREMENT_QUNATITY is NULL
34024   -- and deleted_flag = SYS_NO
34025 
34026   v_sql_stmt := 05;
34027   lv_sql_stmt :=
34028   'UPDATE     msc_st_demands '
34029   ||' SET     process_flag    =  '||G_ERROR_FLG||','
34030   ||'         error_text   = '||''''||lv_message_text||''''
34031   ||' WHERE   NVL(using_requirement_quantity,'||NULL_VALUE||') ='||NULL_VALUE
34032   ||' AND     process_flag      = '||G_IN_PROCESS
34033   ||' AND     origination_type  IN (7,8,29)'
34034   ||' AND     deleted_flag   = '||SYS_NO
34035   ||' AND     batch_id       = :p_batch_id'
34036   ||' AND     sr_instance_code  = :v_instance_code';
34037 
34038 
34039       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34040 
34041   EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
34042     -- Set the  message
34043 
34044      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34045                      (p_app_short_name    => 'MSC',
34046                       p_error_code        => 'MSC_PP_INVALID_VALUE',
34047                       p_message_text      => lv_message_text,
34048                       p_error_text        => lv_error_text,
34049                       p_token1            => 'COLUMN_NAME',
34050                       p_token_value1      => 'ORGANIZATION_CODE');
34051 
34052       IF lv_return <> 0 THEN
34053         RAISE ex_logging_err;
34054       END IF;
34055 
34056     --Derive Organization_id
34057     lv_return:= MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
34058                    (p_table_name       => 'MSC_ST_DEMANDS',
34059                     p_org_partner_name => 'ORGANIZATION_CODE',
34060                     p_org_partner_id   => 'ORGANIZATION_ID',
34061                     p_instance_code    => v_instance_code,
34062                     p_partner_type     => G_ORGANIZATION,
34063                     p_error_text       => lv_error_text,
34064                     p_batch_id         => p_batch_id,
34065                     p_severity         => G_SEV_ERROR,
34066                     p_message_text     => lv_message_text,
34067                     p_debug            => v_debug,
34068                     p_row              => lv_column_names);
34069 
34070     IF lv_return<> 0 THEN
34071         RAISE ex_logging_err;
34072     END IF;
34073 
34074       -- Set the  message
34075 
34076      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34077                      (p_app_short_name    => 'MSC',
34078                       p_error_code        => 'MSC_PP_INVALID_VALUE',
34079                       p_message_text      => lv_message_text,
34080                       p_error_text        => lv_error_text,
34081                       p_token1            => 'COLUMN_NAME',
34082                       p_token_value1      => 'ITEM_NAME');
34083 
34084       IF lv_return <> 0 THEN
34085         RAISE ex_logging_err;
34086       END IF;
34087 
34088 
34089     --Derive Inventory_item_id
34090     lv_return:= MSC_ST_UTIL.DERIVE_ITEM_ID
34091                    (p_table_name       => 'MSC_ST_DEMANDS',
34092                     p_item_col_name    => 'ITEM_NAME',
34093                     p_item_col_id      => 'INVENTORY_ITEM_ID',
34094                     p_instance_id      => v_instance_id,
34095                     p_instance_code    => v_instance_code,
34096                     p_error_text       => lv_error_text,
34097                     p_batch_id         => p_batch_id,
34098                     p_severity         => G_SEV_ERROR,
34099                     p_message_text     => lv_message_text,
34100                     p_debug            => v_debug,
34101                     p_row              => lv_column_names);
34102 
34103     IF lv_return<> 0 THEN
34104         RAISE ex_logging_err;
34105     END IF;
34106 
34107     -- Set the  message
34108      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34109                      (p_app_short_name    => 'MSC',
34110                       p_error_code        => 'MSC_PP_INVALID_VALUE',
34111                       p_message_text      => lv_message_text,
34112                       p_error_text        => lv_error_text,
34113                       p_token1            => 'COLUMN_NAME',
34114                       p_token_value1      => 'USING_ASSEMBLY_ITEM_NAME');
34115 
34116       IF lv_return <> 0 THEN
34117         RAISE ex_logging_err;
34118       END IF;
34119 
34120     --Derive Inventory_item_id
34121     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
34122                    (p_table_name       => 'MSC_ST_DEMANDS',
34123                     p_item_col_name    => 'USING_ASSEMBLY_ITEM_NAME',
34124                     p_item_col_id      => 'USING_ASSEMBLY_ITEM_ID',
34125                     p_instance_id      => v_instance_id,
34126                     p_instance_code    => v_instance_code,
34127                     p_error_text       => lv_error_text,
34128                     p_batch_id         => p_batch_id,
34129                     p_severity         => G_SEV3_ERROR,
34130                     p_message_text     => lv_message_text,
34131                     p_debug            => v_debug,
34132                     p_row              => lv_column_names);
34133 
34134     IF lv_return <> 0 THEN
34135         RAISE ex_logging_err;
34136     END IF;
34137 
34138     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34139                      (p_app_short_name    => 'MSC',
34140                       p_error_code        => 'MSC_PP_INVALID_VALUE',
34141                       p_message_text      => lv_message_text,
34142                       p_error_text        => lv_error_text,
34143                       p_token1            => 'COLUMN_NAME',
34144                       p_token_value1      => 'DEMAND_CLASS');
34145 
34146       IF lv_return <> 0 THEN
34147         RAISE ex_logging_err;
34148       END IF;
34149 
34150 
34151       -- Validate Demand Class, if value provided it should exists
34152       -- in ODS or staging table
34153 
34154      lv_return :=
34155      MSC_ST_UTIL.VALIDATE_DMD_CLASS(p_table_name       => 'MSC_ST_DEMANDS',
34156                                     p_dmd_class_column => 'DEMAND_CLASS',
34157                                     p_instance_id      => v_instance_id,
34158                                     p_instance_code    => v_instance_code,
34159                                     p_severity         => G_SEV3_ERROR,
34160                                     p_message_text     => lv_message_text,
34161                                     p_batch_id         => p_batch_id,
34162                                     p_debug            => v_debug,
34163                                     p_error_text       => lv_error_text);
34164 
34165      if(lv_return <> 0) then
34166         RAISE ex_logging_err;
34167      end if;
34168 
34169 
34170 
34171   -- Update using_assembly_item_id = inventory_item_id
34172 
34173   v_sql_stmt := 06;
34174   lv_sql_stmt :=
34175   ' UPDATE      msc_st_demands'
34176   ||' SET    using_assembly_item_id  =  inventory_item_id'
34177   ||' WHERE  process_flag  = '||G_IN_PROCESS
34178   ||' AND    NVL(using_assembly_item_id,'||NULL_VALUE||') = '||NULL_VALUE
34179   ||' AND    deleted_flag   = '||SYS_NO
34180   ||' AND    process_flag  = '||G_IN_PROCESS
34181   ||' AND    origination_type IN (7,8,29)'
34182   ||' AND    deleted_flag   = '||SYS_NO
34183   ||' AND    batch_id       = :p_batch_id'
34184   ||' AND    sr_instance_code  = :v_instance_code';
34185 
34186 
34187       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34188 
34189   EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
34190 
34191 
34192   -- Update schedule_date = using_assembly_demand_date
34193 
34194 
34195   v_sql_stmt := 07;
34196   lv_sql_stmt :=
34197   ' UPDATE   msc_st_demands'
34198   ||' SET    schedule_date = using_assembly_demand_date '
34199   ||' WHERE  process_flag  = '||G_IN_PROCESS
34200   ||' AND    deleted_flag   = '||SYS_NO
34201   ||' AND    origination_type IN (7,8,29)'
34202   ||' AND    batch_id       = :p_batch_id'
34203   ||' AND    sr_instance_code  = :v_instance_code';
34204 
34205 
34206       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34207 
34208   EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
34209 
34210   -- Derive disposition_id for MDS
34211 
34212    v_sql_stmt := 08;
34213    lv_sql_stmt :=
34214    'UPDATE msc_st_demands msd '
34215     ||'  SET disposition_id = (SELECT local_id'
34216     ||'       FROM   msc_local_id_demand mlid'
34217     ||'       WHERE  mlid.char1    = msd.sr_instance_code'
34218     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
34219     ||'       =      NVL(msd.company_name,'||''''||NULL_CHAR||''''||') '
34220     ||'       AND    mlid.char3    = msd.organization_code'
34221     ||'       AND    mlid.char4    = msd.demand_schedule_name'
34222     ||'       AND    mlid.char5    = msd.item_name'
34223     ||'       AND    NVL(mlid.char6,'||''''||NULL_CHAR||''''||') '
34224     ||'       =      NVL(msd.schedule_line_num,'||''''||NULL_CHAR||''''||') '
34225     ||'       AND    mlid.date1    = msd.schedule_date'
34226     ||'       AND    mlid.number1  = msd.origination_type'
34227     ||'       AND    mlid.entity_name = ''DISPOSITION_ID_MDS'' )'
34228     ||'  WHERE origination_type in (7,8)'
34229     ||'  AND   process_flag     ='||G_IN_PROCESS
34230     ||'  AND   batch_id       = :p_batch_id'
34231     ||'  AND   sr_instance_code  =:v_instance_code' ;
34232 
34233 
34234       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34235 
34236     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
34237 
34238    --   Set the message
34239 
34240     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34241                  (p_app_short_name    => 'MSC',
34242                   p_error_code        => 'MSC_PP_DELETE_FAIL',
34243                   p_message_text      => lv_message_text,
34244                   p_error_text        => lv_error_text);
34245 
34246     IF lv_return <> 0 THEN
34247       RAISE ex_logging_err;
34248     END IF;
34249   -- Error out records with deleted flag = SYS_YES and NULL disposition_id
34250 
34251   v_sql_stmt := 09;
34252 
34253   lv_sql_stmt :=
34254   'UPDATE         msc_st_demands '
34255   ||' SET         process_flag    ='||G_ERROR_FLG||','
34256   ||'             error_text   = '||''''||lv_message_text||''''
34257   ||' WHERE  NVL(disposition_id,'||NULL_VALUE||') = '||NULL_VALUE
34258   ||' AND    deleted_flag  ='||SYS_YES
34259   ||' AND    origination_type in  (7,8)'
34260   ||' AND    batch_id       = :p_batch_id'
34261   ||' AND    sr_instance_code  =:v_instance_code';
34262 
34263 
34264       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34265 
34266     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
34267 
34268 
34269    -- Derive sales_order_line_id for forecast
34270 
34271    v_sql_stmt := 10;
34272    lv_sql_stmt :=
34273    'UPDATE msc_st_demands msd '
34274     ||'  SET  sales_order_line_id = (SELECT local_id'
34275     ||'       FROM   msc_local_id_demand mlid'
34276     ||'       WHERE  mlid.char1    = msd.sr_instance_code'
34277     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
34278     ||'       =      NVL(msd.company_name,'||''''||NULL_CHAR||''''||') '
34279     ||'       AND    mlid.char3    = msd.organization_code'
34280     ||'       AND    mlid.char4    = msd.forecast_designator'
34281     ||'       AND    mlid.char5    = msd.item_name'
34282     ||'       AND    NVL(mlid.char6,'||''''||NULL_CHAR||''''||') '
34283     ||'       =      NVL(msd.schedule_line_num,'||''''||NULL_CHAR||''''||') '
34284     ||'       AND    mlid.date1    = msd.schedule_date'
34285     ||'       AND    mlid.number1     = msd.origination_type'
34286     ||'       AND    mlid.entity_name = ''DISPOSITION_ID_FCT'' )'
34287     ||'  WHERE origination_type = 29'
34288     ||'  AND   process_flag     ='||G_IN_PROCESS
34289     ||'  AND   batch_id       = :p_batch_id'
34290     ||'  AND   sr_instance_code  =:v_instance_code';
34291 
34292 
34293       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34294 
34295     EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
34296 
34297    --   Set the message
34298 
34299     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34300                  (p_app_short_name    => 'MSC',
34301                   p_error_code        => 'MSC_PP_DELETE_FAIL',
34302                   p_message_text      => lv_message_text,
34303                   p_error_text        => lv_error_text);
34304 
34305     IF lv_return <> 0 THEN
34306       RAISE ex_logging_err;
34307     END IF;
34308 
34309   -- Error out records with deleted flag = SYS_YES and NULL disposition_id
34310 
34311   v_sql_stmt := 11;
34312 
34313   lv_sql_stmt :=
34314   'UPDATE     msc_st_demands '
34315   ||' SET     process_flag    ='||G_ERROR_FLG||','
34316   ||'         error_text   = '||''''||lv_message_text||''''
34317   ||' WHERE   NVL(sales_order_line_id,'||NULL_VALUE||') = '||NULL_VALUE
34318   ||' AND     deleted_flag  ='||SYS_YES
34319   ||' AND     origination_type = 29'
34320   ||' AND     batch_id       = :p_batch_id'
34321   ||' AND     sr_instance_code  =:v_instance_code';
34322 
34323 
34324       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34325 
34326    EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
34327 
34328    -- Set the error message
34329    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34330                      (p_app_short_name    => 'MSC',
34331                       p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
34332                       p_message_text      => lv_message_text,
34333                       p_error_text        => lv_error_text,
34334                       p_token1            => 'COLUMN_NAMES',
34335                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
34336                                              ||'ORGANIZATION_CODE,PROJECT_NUMBER',
34337                       p_token2            => 'MASTER_TABLE');
34338 
34339       IF lv_return <> 0 THEN
34340         RAISE ex_logging_err;
34341       END IF;
34342 
34343    --Derive Project Id.
34344     lv_return:= MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
34345                            (p_table_name          => 'MSC_ST_DEMANDS',
34346                             p_proj_col_name       => 'PROJECT_NUMBER',
34347                             p_proj_task_col_id    => 'PROJECT_ID',
34348                             p_instance_code       =>  v_instance_code,
34349                             p_entity_name         => 'PROJECT_ID',
34350                             p_error_text          =>  lv_error_text,
34351                             p_batch_id            =>  p_batch_id,
34352                             p_severity            =>  G_SEV_WARNING,
34353                             p_message_text        =>  lv_message_text,
34354                             p_debug               => v_debug,
34355                             p_row                 =>  lv_column_names);
34356     IF lv_return<> 0 THEN
34357       RAISE ex_logging_err;
34358     END IF;
34359 
34360     -- Set the error message
34361    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34362                      (p_app_short_name    => 'MSC',
34363                       p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
34364                       p_message_text      => lv_message_text,
34365                       p_error_text        => lv_error_text,
34366                       p_token1            => 'COLUMN_NAMES',
34367                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
34368                                              ||'ORGANIZATION_CODE,PROJECT_NUMBER'
34369                                              ||'TASK_NUMBER' ,
34370                       p_token2            => 'MASTER_TABLE',
34371                       p_token_value2      =>  'MSC_ST_PROJECT_TASKS');
34372 
34373       IF lv_return <> 0 THEN
34374         RAISE ex_logging_err;
34375       END IF;
34376 
34377     --Derive Task Id.
34378     lv_return:= MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
34379                            (p_table_name          => 'MSC_ST_DEMANDS',
34380                             p_proj_col_name       => 'PROJECT_NUMBER',
34381                             p_proj_task_col_id    => 'TASK_ID',
34382                             p_instance_code       => v_instance_code,
34383                             p_entity_name         => 'TASK_ID',
34384                             p_error_text          => lv_error_text,
34385                             p_task_col_name       => 'TASK_NUMBER',
34386                             p_batch_id            => p_batch_id,
34387                             p_severity            => G_SEV_WARNING,
34388                             p_message_text        => lv_message_text,
34389                             p_debug               => v_debug,
34390                             p_row                 => lv_column_names);
34391 
34392     IF lv_return<> 0 THEN
34393       RAISE ex_logging_err;
34394     END IF;
34395 
34396    -- Validations for MDS ( origination_type = 8)
34397 
34398     -- Set the error message
34399     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34400                      (p_app_short_name    => 'MSC',
34401                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
34402                       p_message_text      => lv_message_text,
34403                       p_error_text        => lv_error_text,
34404                       p_token1            => 'COLUMN_NAMES',
34405                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
34406                                              ||' ORGANIZATION_CODE,'
34407                                              ||' DEMAND_SCHEDULE_NAME',
34408                       p_token2            => 'MASTER_TABLE',
34409                       p_token_value2      => 'MSC_ST_DESIGNATORS',
34410                       p_token3            => 'CHILD_TABLE' ,
34411                       p_token_value3      => 'MSC_ST_DEMANDS' );
34412 
34413       IF lv_return <> 0 THEN
34414         RAISE ex_logging_err;
34415       END IF;
34416 
34417    -- For MDS check whether designator exists
34418 
34419      v_sql_stmt := 12;
34420     lv_sql_stmt :=
34421     'UPDATE  msc_st_demands msdm'
34422     ||' SET  process_flag = '||G_ERROR_FLG||','
34423     ||'         error_text   = '||''''||lv_message_text||''''
34424     ||' WHERE NOT EXISTS(SELECT 1'
34425     ||'       FROM   msc_st_designators msd'
34426     ||'       WHERE  msd.designator        = msdm.demand_schedule_name'
34427     ||'       AND   NVL(msd.company_name,'||''''||NULL_CHAR||''''||') '
34428     ||'             =    NVL(msdm.company_name,'||''''||NULL_CHAR||''''||') '
34429     ||'       AND    msd.organization_code = msdm.organization_code'
34430     ||'       AND    msd.sr_instance_code  = msdm.sr_instance_code'
34431     ||'       AND    msd.process_flag      = '||G_VALID
34432     ||'       AND    msd.designator_type   = 1'
34433     ||'       AND    NVL(msd.disable_date, SYSDATE+1) > SYSDATE'
34434     ||'       UNION'
34435     ||'       SELECT 1'
34436     ||'       FROM   msc_designators md'
34437     ||'       WHERE  md.designator         = msdm.demand_schedule_name'
34438     ||'       AND    md.organization_id    = msdm.organization_id '
34439     ||'       AND    md.sr_instance_id     = :v_instance_id '
34440     ||'       AND    md.designator_type    = 1'
34441     ||'       AND    NVL(md.disable_date, SYSDATE+1) > SYSDATE)'
34442     ||' AND origination_type   in (7,8)'
34443     ||' AND deleted_flag  = '||SYS_NO
34444     ||' AND process_flag  = '||G_IN_PROCESS
34445     ||' AND  batch_id       = :p_batch_id'
34446     ||' AND sr_instance_code = :v_instance_code';
34447 
34448 
34449       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34450 
34451     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_id,
34452                                         p_batch_id,
34453                                         v_instance_code;
34454 
34455 
34456 
34457     -- Repetitve planned items not supported
34458     -- So error out records with demand type = 2 ,or daily_demand_rate or
34459     -- assembly_demand_comp_date is NOT NULL for origination type = 8
34460 
34461       -- Set the  message
34462 
34463      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34464                      (p_app_short_name    => 'MSC',
34465                       p_error_code        => 'MSC_PP_INVALID_VALUE',
34466                       p_message_text      => lv_message_text,
34467                       p_error_text        => lv_error_text,
34468                       p_token1            => 'COLUMN_NAME',
34469                       p_token_value1      => 'DEMAND_TYPE OR DAILY_DEMAND_RATE ,'
34470                                              ||' ASSEMBLY_DEMAND_COMP_DATE'	 );
34471 
34472       IF lv_return <> 0 THEN
34473         RAISE ex_logging_err;
34474       END IF;
34475 
34476   v_sql_stmt := 13;
34477 
34478   lv_sql_stmt :=
34479   'UPDATE       msc_st_demands '
34480   ||'  SET      process_flag    =  '||G_ERROR_FLG||','
34481   ||'           error_text   = '||''''||lv_message_text||''''
34482   ||' WHERE     (demand_type     = 2'
34483   ||' OR        assembly_demand_comp_date IS NOT NULL '
34484   ||' OR        daily_demand_rate         IS NOT NULL )  '
34485   ||' AND       deleted_flag   = '||SYS_NO
34486   ||' AND       origination_type in (7,8)'
34487   ||' AND       batch_id       = :p_batch_id'
34488   ||' AND       sr_instance_code  = :v_instance_code';
34489 
34490 
34491       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34492 
34493   EXECUTE IMMEDIATE lv_sql_stmt USING p_batch_id,v_instance_code;
34494 
34495    -- Validations for Forecast ( origination_type = 29)
34496 
34497     -- Set the error message
34498     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34499                      (p_app_short_name    => 'MSC',
34500                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
34501                       p_message_text      => lv_message_text,
34502                       p_error_text        => lv_error_text,
34503                       p_token1            => 'COLUMN_NAMES',
34504                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
34505                                              ||'ORGANIZATION_CODE,FORECAST_DESIGNATOR',
34506                       p_token2            => 'MASTER_TABLE',
34507                       p_token_value2      =>  'MSC_ST_DESIGNATORS',
34508                       p_token3            =>  'CHILD_TABLE' ,
34509                       p_token_value3      =>  'MSC_ST_DEMANDS' );
34510 
34511       IF lv_return <> 0 THEN
34512         RAISE ex_logging_err;
34513       END IF;
34514 
34515   -- For Forecast,check whether designator exists
34516 
34517      v_sql_stmt := 14;
34518     lv_sql_stmt :=
34519     'UPDATE  msc_st_demands msdm'
34520     ||' SET  process_flag = '||G_ERROR_FLG||','
34521     ||'           error_text   = '||''''||lv_message_text||''''
34522     ||' WHERE NOT EXISTS(SELECT 1'
34523     ||'       FROM   msc_st_designators msd'
34524     ||'       WHERE  msd.designator        = msdm.forecast_designator'
34525     ||'       AND   NVL(msd.company_name,'||''''||NULL_CHAR||''''||') '
34526     ||'             =    NVL(msd.company_name,'||''''||NULL_CHAR||''''||') '
34527     ||'       AND    msd.organization_code = msdm.organization_code'
34528     ||'       AND    msd.sr_instance_code  = msdm.sr_instance_code'
34529     ||'       AND    msd.process_flag      = '||G_VALID
34530     ||'       AND    msd.designator_type   = 6'
34531     ||'       AND    NVL(msd.disable_date, SYSDATE+1) > SYSDATE'
34532     ||'       UNION'
34533     ||'       SELECT 1'
34534     ||'       FROM   msc_designators md'
34535     ||'       WHERE  md.designator      = msdm.forecast_designator'
34536     ||'       AND    md.organization_id = msdm.organization_id '
34537     ||'       AND    md.sr_instance_id  = :v_instance_id '
34538     ||'       AND    md.designator_type = 6'
34539     ||'       AND    NVL(md.disable_date, SYSDATE+1) > SYSDATE)'
34540     ||' AND origination_type   = 29'
34541     ||' AND deleted_flag  = '||SYS_NO
34542     ||' AND process_flag  = '||G_IN_PROCESS
34543     ||' AND  batch_id       = :p_batch_id'
34544     ||' AND sr_instance_code = :v_instance_code';
34545 
34546 
34547       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
34548 
34549     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_id,
34550                                         p_batch_id,
34551                                         v_instance_code;
34552 
34553 
34554     -- Set the error message
34555     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34556                      (p_app_short_name    => 'MSC',
34557                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
34558                       p_message_text      => lv_message_text,
34559                       p_error_text        => lv_error_text,
34560                       p_token1            => 'COLUMN_NAME',
34561                       p_token_value1      => 'DEMAND_TYPE',
34562                       p_token2            => 'DEFAULT_VALUE',
34563                       p_token_value2      =>  1  );
34564 
34565       IF lv_return <> 0 THEN
34566         RAISE ex_logging_err;
34567       END IF;
34568 
34569 
34570     --  Default demand_type to 1 if assembly_demand_comp_date is  NULL
34571 
34572     lv_where_str := '   AND NVL(assembly_demand_comp_date,SYSDATE-36500 ) = SYSDATE-36500'
34573                     ||' AND NVL(demand_type,'||NULL_VALUE||') <> 1'
34574                     ||' AND origination_type = 29 AND deleted_flag  ='||SYS_NO ;
34575 
34576     lv_return:= MSC_ST_UTIL.LOG_ERROR
34577                    (p_table_name        => 'MSC_ST_DEMANDS',
34578                     p_instance_code     => v_instance_code,
34579                     p_row               => lv_column_names,
34580                     p_severity          => G_SEV_WARNING,
34581                     p_message_text      => lv_message_text,
34582                     p_error_text        => lv_error_text,
34583                     p_batch_id          => p_batch_id,
34584                     p_where_str         => lv_where_str,
34585                     p_col_name          => 'DEMAND_TYPE',
34586                     p_debug             => v_debug,
34587                     p_default_value     => 1);
34588 
34589     IF lv_return<> 0 THEN
34590         RAISE ex_logging_err;
34591     END IF;
34592 
34593     -- Set the error message
34594     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34595                      (p_app_short_name    => 'MSC',
34596                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
34597                       p_message_text      => lv_message_text,
34598                       p_error_text        => lv_error_text,
34599                       p_token1            => 'COLUMN_NAME',
34600                       p_token_value1      => 'DEMAND_TYPE',
34601                       p_token2            => 'DEFAULT_VALUE',
34602                       p_token_value2      =>  2  );
34603 
34604       IF lv_return <> 0 THEN
34605         RAISE ex_logging_err;
34606       END IF;
34607 
34608     --  Default demand_type as 2 if assembly_demand_comp_date is NOT NULL
34609 
34610     lv_where_str := '   AND NVL(assembly_demand_comp_date,SYSDATE-36500 ) <> SYSDATE-36500 '
34611                     ||' AND NVL(demand_type,'||NULL_VALUE||') <> 2'
34612                     ||' AND origination_type = 29 AND deleted_flag  ='||SYS_NO ;
34613 
34614     lv_return:= MSC_ST_UTIL.LOG_ERROR
34615                    (p_table_name        => 'MSC_ST_DEMANDS',
34616                     p_instance_code     => v_instance_code,
34617                     p_row               => lv_column_names,
34618                     p_severity          => G_SEV_WARNING,
34619                     p_message_text      => lv_message_text,
34620                     p_error_text        => lv_error_text,
34621                     p_batch_id          => p_batch_id,
34622                     p_where_str         => lv_where_str,
34623                     p_col_name          => 'DEMAND_TYPE',
34624                     p_debug             => v_debug,
34625                     p_default_value     => 2);
34626 
34627     IF lv_return<> 0 THEN
34628         RAISE ex_logging_err;
34629     END IF;
34630 
34631     -- Set the error message
34632     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34633                      (p_app_short_name    => 'MSC',
34634                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
34635                       p_message_text      => lv_message_text,
34636                       p_error_text        => lv_error_text,
34637                       p_token1            => 'COLUMN_NAME',
34638                       p_token_value1      => 'BUCKET_TYPE',
34639                       p_token2            => 'DEFAULT_VALUE',
34640                       p_token_value2      =>  G_BUCKET_TYPE );
34641 
34642       IF lv_return <> 0 THEN
34643         RAISE ex_logging_err;
34644       END IF;
34645 
34646    -- Default bucket type to 1, if NOT IN (1 -days,2 - Weeks,3 - Periods)
34647 
34648     lv_where_str := '   AND  NVL(bucket_type,'||NULL_VALUE||') NOT IN (1,2,3)'
34649                     ||' AND origination_type = 29 AND deleted_flag  ='||SYS_NO ;
34650 
34651     lv_return:= MSC_ST_UTIL.LOG_ERROR
34652                    (p_table_name        => 'MSC_ST_DEMANDS',
34653                     p_instance_code     => v_instance_code,
34654                     p_row               => lv_column_names,
34655                     p_severity          => G_SEV_WARNING,
34656                     p_message_text      => lv_message_text,
34657                     p_error_text        => lv_error_text,
34658                     p_batch_id          => p_batch_id,
34659                     p_where_str         => lv_where_str,
34660                     p_col_name          => 'BUCKET_TYPE',
34661                     p_debug             => v_debug,
34662                     p_default_value     =>  G_BUCKET_TYPE);
34663 
34664     IF lv_return<> 0 THEN
34665         RAISE ex_logging_err;
34666     END IF;
34667 
34668       -- Set the  message
34669 
34670      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34671                      (p_app_short_name    => 'MSC',
34672                       p_error_code        => 'MSC_PP_INVALID_VALUE',
34673                       p_message_text      => lv_message_text,
34674                       p_error_text        => lv_error_text,
34675                       p_token1            => 'COLUMN_NAME',
34676                       p_token_value1      => 'CUSTOMER_NAME');
34677 
34678       IF lv_return <> 0 THEN
34679         RAISE ex_logging_err;
34680       END IF;
34681 
34682    -- Derive customer_id
34683 
34684    lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
34685                      (p_table_name       => 'MSC_ST_DEMANDS',
34686                       p_org_partner_name => 'CUSTOMER_NAME',
34687                       p_org_partner_id   => 'CUSTOMER_ID',
34688                       p_instance_code    => v_instance_code,
34689                       p_partner_type     => G_CUSTOMER,
34690                       p_error_text       => lv_error_text,
34691                       p_batch_id         => p_batch_id,
34692                       p_severity         => G_SEV3_ERROR,
34693                       p_message_text     => lv_message_text,
34694                       p_debug            => v_debug,
34695                       p_row              => lv_column_names);
34696 
34697       IF lv_return <> 0 THEN
34698         RAISE ex_logging_err;
34699       END IF;
34700 
34701       -- Set the  message
34702      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34703                      (p_app_short_name    => 'MSC',
34704                       p_error_code        => 'MSC_PP_INVALID_VALUE',
34705                       p_message_text      => lv_message_text,
34706                       p_error_text        => lv_error_text,
34707                       p_token1            => 'COLUMN_NAME',
34708                       p_token_value1      => 'CUSTOMER_SITE_CODE');
34709 
34710       IF lv_return <> 0 THEN
34711         RAISE ex_logging_err;
34712       END IF;
34713 
34714 
34715   -- Derive customer_site_id
34716 
34717   lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
34718                      (p_table_name        => 'MSC_ST_DEMANDS',
34719                       p_partner_name      => 'CUSTOMER_NAME',
34720                       p_partner_site_code => 'CUSTOMER_SITE_CODE',
34721                       p_partner_site_id   => 'CUSTOMER_SITE_ID',
34722                       p_instance_code     => v_instance_code,
34723                       p_partner_type      => G_CUSTOMER,
34724                       p_error_text        => lv_error_text,
34725                       p_batch_id          => p_batch_id,
34726                       p_severity          => G_SEV3_ERROR,
34727                       p_message_text      => lv_message_text,
34728                       p_debug             => v_debug,
34729                       p_row               => lv_column_names);
34730 
34731       IF lv_return <> 0 THEN
34732         RAISE ex_logging_err;
34733       END IF;
34734 
34735       -- Set the  message
34736      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34737                      (p_app_short_name    => 'MSC',
34738                       p_error_code        => 'MSC_PP_INVALID_VALUE',
34739                       p_message_text      => lv_message_text,
34740                       p_error_text        => lv_error_text,
34741                       p_token1            => 'COLUMN_NAME',
34742                       p_token_value1      => 'BILL_CODE');
34743 
34744       IF lv_return <> 0 THEN
34745         RAISE ex_logging_err;
34746       END IF;
34747 
34748 
34749   -- Derive bill_to_site_id
34750   lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
34751                      (p_table_name        => 'MSC_ST_DEMANDS',
34752                       p_partner_name      => 'CUSTOMER_NAME',
34753                       p_partner_site_code => 'BILL_CODE',
34754                       p_partner_site_id   => 'BILL_ID',
34755                       p_instance_code     => v_instance_code,
34756                       p_partner_type      => G_CUSTOMER,
34757                       p_error_text        => lv_error_text,
34758                       p_batch_id          => p_batch_id,
34759                       p_severity          => G_SEV3_ERROR,
34760                       p_message_text      => lv_message_text,
34761                       p_debug             => v_debug,
34762                       p_row               => lv_column_names);
34763 
34764       IF lv_return <> 0 THEN
34765         RAISE ex_logging_err;
34766       END IF;
34767 
34768   -- valid values for order_date_type_code are 'SHIP' and 'ARRIVE'
34769   -- Set the error message
34770 
34771      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
34772                      (p_app_short_name    => 'MSC',
34773                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
34774                       p_message_text      => lv_message_text,
34775                       p_error_text        => lv_error_text,
34776                       p_token1            => 'COLUMN_NAME',
34777                       p_token_value1      => 'ORDER_DATE_TYPE_CODE',
34778                       p_token2            => 'DEFAULT_VALUE',
34779                       p_token_value2      => 1);
34780 
34781       IF lv_return <> 0 THEN
34782         RAISE ex_logging_err;
34783       END IF;
34784 
34785     lv_where_str :=
34786     '    AND NVL(order_date_type_code  ,'||NULL_VALUE||') '
34787     ||'  NOT IN (1,2) AND origination_type in (7, 8)'
34788     ||' AND     NVL(order_date_type_code,'||NULL_VALUE||')<> '||NULL_VALUE;
34789 
34790     lv_return := MSC_ST_UTIL.LOG_ERROR
34791                     (p_table_name        => 'MSC_ST_DEMANDS',
34792                      p_instance_code     => v_instance_code,
34793                      p_row               => lv_column_names,
34794                      p_severity          => G_SEV_WARNING,
34795                      p_message_text      => lv_message_text,
34796                      p_error_text        => lv_error_text,
34797                      p_batch_id          => p_batch_id,
34798                      p_where_str         => lv_where_str,
34799                      p_col_name          => 'ORDER_DATE_TYPE_CODE',
34800                      p_debug             => v_debug,
34801                      p_default_value     => 1);
34802 
34803     IF lv_return <> 0 THEN
34804         RAISE ex_logging_err;
34805     END IF;
34806 
34807     --Call to customised validation.
34808     MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
34809       (ERRBUF         => lv_error_text,
34810        RETCODE        => lv_return,
34811        pBatchID       => p_batch_id,
34812        pInstanceCode  => v_instance_code,
34813        pEntityName    => 'MSC_ST_DEMANDS',
34814        pInstanceID    => v_instance_id);
34815 
34816     IF NVL(lv_return,0) <> 0 THEN
34817       RAISE ex_logging_err;
34818     END IF;
34819 
34820   -- Generate disposition_id for MDS
34821 
34822     OPEN c2(p_batch_id);
34823     FETCH c2 BULK COLLECT INTO lb_rowid ;
34824 
34825     IF c2%ROWCOUNT > 0  THEN
34826       v_sql_stmt := 15;
34827       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
34828       UPDATE msc_st_demands
34829       SET    disposition_id  = msc_st_demand_disposition_id_s.NEXTVAL
34830       WHERE  rowid           = lb_rowid(j);
34831 
34832       v_sql_stmt := 16;
34833       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
34834       INSERT INTO msc_local_id_demand
34835         (local_id,
34836          st_transaction_id,
34837          instance_id,
34838          entity_name,
34839          data_source_type,
34840          char1,
34841          char2,
34842          char3,
34843          char4,
34844          char5,
34845          char6,
34846          number1,
34847          date1,
34848          SOURCE_ORG_ID,
34849          SOURCE_INVENTORY_ITEM_ID,
34850          SOURCE_USING_ASSEMBLY_ITEM_ID ,
34851          SOURCE_PROJECT_ID  ,
34852          SOURCE_TASK_ID,
34853          SOURCE_CUSTOMER_SITE_ID ,
34854          SOURCE_BILL_ID ,
34855          SOURCE_DISPOSITION_ID,
34856          SOURCE_CUSTOMER_ID,
34857          last_update_date,
34858          last_updated_by,
34859          creation_date,
34860          created_by)
34861       SELECT
34862          disposition_id,
34863          st_transaction_id,
34864          v_instance_id,
34865          'DISPOSITION_ID_MDS',
34866          data_source_type,
34867          sr_instance_code,
34868          company_name,
34869          organization_code,
34870          demand_schedule_name,
34871          item_name,
34872          schedule_line_num,
34873          origination_type,
34874          schedule_date,
34875          SOURCE_ORG_ID,
34876          SOURCE_INVENTORY_ITEM_ID,
34877          SOURCE_USING_ASSEMBLY_ITEM_ID ,
34878          SOURCE_PROJECT_ID  ,
34879          SOURCE_TASK_ID,
34880          SOURCE_CUSTOMER_SITE_ID ,
34881          SOURCE_BILL_ID ,
34882          SOURCE_DISPOSITION_ID,
34883          SOURCE_CUSTOMER_ID,
34884          v_current_date,
34885          v_current_user,
34886          v_current_date,
34887          v_current_user
34888       FROM  msc_st_demands
34889       WHERE rowid = lb_rowid(j);
34890 
34891       END IF;
34892       CLOSE c2 ;
34893 
34894 
34895   -- Generate sales_order_line_id for Forecast
34896 
34897     OPEN c3(p_batch_id);
34898     FETCH c3 BULK COLLECT INTO lb_rowid ;
34899 
34900     IF c3%ROWCOUNT > 0  THEN
34901       v_sql_stmt := 17;
34902       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
34903       UPDATE msc_st_demands
34904       SET    sales_order_line_id  = msc_st_demand_disposition_id_s.NEXTVAL
34905       WHERE  rowid                = lb_rowid(j);
34906 
34907       v_sql_stmt := 18;
34908       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
34909       INSERT INTO msc_local_id_demand
34910         (local_id,
34911          st_transaction_id,
34912          instance_id,
34913          entity_name,
34914          data_source_type,
34915          char1,
34916          char2,
34917          char3,
34918          char4,
34919          char5,
34920          char6,
34921          number1,
34922          date1,
34923          SOURCE_ORG_ID,
34924          SOURCE_INVENTORY_ITEM_ID,
34925          SOURCE_USING_ASSEMBLY_ITEM_ID ,
34926          SOURCE_SALES_ORDER_LINE_ID ,
34927          SOURCE_PROJECT_ID  ,
34928          SOURCE_TASK_ID,
34929          SOURCE_CUSTOMER_SITE_ID ,
34930          SOURCE_BILL_ID ,
34931          SOURCE_CUSTOMER_ID,
34932          last_update_date,
34933          last_updated_by,
34934          creation_date,
34935          created_by)
34936       SELECT
34937          sales_order_line_id,
34938          st_transaction_id,
34939          v_instance_id,
34940          'DISPOSITION_ID_FCT',
34941          data_source_type,
34942          sr_instance_code,
34943          company_name,
34944          organization_code,
34945          forecast_designator,
34946          item_name,
34947          schedule_line_num,
34948          origination_type,
34949          schedule_date,
34950          SOURCE_ORG_ID,
34951          SOURCE_INVENTORY_ITEM_ID,
34952          SOURCE_USING_ASSEMBLY_ITEM_ID ,
34953          SOURCE_SALES_ORDER_LINE_ID ,
34954          SOURCE_PROJECT_ID  ,
34955          SOURCE_TASK_ID,
34956          SOURCE_CUSTOMER_SITE_ID ,
34957          SOURCE_BILL_ID ,
34958          SOURCE_CUSTOMER_ID,
34959          v_current_date,
34960          v_current_user,
34961          v_current_date,
34962          v_current_user
34963       FROM  msc_st_demands
34964       WHERE rowid = lb_rowid(j);
34965 
34966       END IF;
34967       CLOSE c3 ;
34968 
34969     lv_return:= MSC_ST_UTIL.SET_PROCESS_FLAG
34970                   (p_table_name     => 'MSC_ST_DEMANDS',
34971                    p_instance_id    => v_instance_id,
34972                    p_instance_code  => v_instance_code,
34973                    p_process_flag   => G_VALID,
34974                    p_error_text     => lv_error_text,
34975                    p_debug          => v_debug,
34976                    p_batch_id       => p_batch_id);
34977 
34978     IF lv_return<> 0 THEN
34979       RAISE ex_logging_err;
34980     END IF;
34981 
34982     lv_return:= MSC_ST_UTIL.LOG_ERROR
34983                    (p_table_name        => 'MSC_ST_DEMANDS',
34984                     p_instance_code     => v_instance_code,
34985                     p_row               => lv_column_names,
34986                     p_severity          => G_SEV_ERROR,
34987                     p_message_text      => NULL,
34988                     p_error_text        => lv_error_text,
34989                     p_debug             => v_debug,
34990                     p_batch_id          => p_batch_id);
34991 
34992     IF lv_return<> 0 THEN
34993       RAISE ex_logging_err;
34994     END IF;
34995 
34996     COMMIT;
34997 
34998  EXCEPTION
34999 
35000     WHEN too_many_rows THEN
35001       lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_MDS_FORECAST'||'('
35002                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
35003         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
35004 
35005       ROLLBACK ;
35006 
35007     WHEN ex_logging_err THEN
35008         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
35009 
35010       ROLLBACK;
35011 
35012     WHEN OTHERS THEN
35013       lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_MDS_FORECAST'||'('
35014                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
35015         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
35016 
35017       ROLLBACK;
35018 
35019   END LOAD_MDS_FORECAST ;
35020 
35021   /*==========================================================================+
35022   | DESCRIPTION  : This procedure derives the sr_company_id and the partner   |
35023   |                type for the  business object Company users.               |
35024   +==========================================================================*/
35025   PROCEDURE  LOAD_COMPANY_USERS IS
35026 
35027   ex_logging_err        EXCEPTION;
35028   lv_return             NUMBER;
35029   lv_error_text         VARCHAR2(250);
35030   lv_sql_stmt           VARCHAR2(5000);
35031   lv_batch_id           msc_st_company_users.batch_id%TYPE;
35032   lv_message_text       msc_errors.error_text%TYPE;
35033 
35034   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
35035   lb_rowid         RowidTab;
35036 
35037   CURSOR c1(p_batch_id NUMBER) IS
35038     SELECT mscu.rowid
35039     FROM   MSC_ST_COMPANY_USERS mscu
35040     WHERE  mscu.process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
35041     AND    mscu.sr_instance_code  = v_instance_code
35042     AND    batch_id         = p_batch_id;
35043 
35044   BEGIN
35045 
35046     LOOP
35047       v_sql_stmt := 01;
35048 
35049       SELECT msc_st_batch_id_s.NEXTVAL
35050       INTO   lv_batch_id
35051       FROM   DUAL;
35052 
35053       v_sql_stmt := 02;
35054       lv_sql_stmt :=
35055       ' UPDATE   MSC_ST_COMPANY_USERS '
35056       ||' SET    batch_id  = :lv_batch_id'
35057       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
35058       ||' AND    sr_instance_code               = :v_instance_code'
35059       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
35060       ||' AND    rownum                        <= '||v_batch_size;
35061 
35062 
35063         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
35064 
35065       EXECUTE IMMEDIATE lv_sql_stmt
35066               USING     lv_batch_id,
35067                         v_instance_code;
35068 
35069       EXIT WHEN SQL%NOTFOUND;
35070 
35071       OPEN  c1(lv_batch_id);
35072       FETCH c1 BULK COLLECT INTO lb_rowid;
35073       CLOSE c1;
35074 
35075       v_sql_stmt := 03;
35076 
35077       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
35078       UPDATE MSC_ST_COMPANY_USERS
35079       SET    st_transaction_id = msc_st_company_users_s.NEXTVAL,
35080              refresh_id          = v_refresh_id,
35081              last_update_date    = v_current_date,
35082              last_update_login   = v_current_user,
35083              creation_date       = v_current_date,
35084              created_by          = v_current_user
35085       WHERE  rowid           = lb_rowid(j);
35086 
35087       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
35088                      (p_app_short_name    => 'MSC',
35089                       p_error_code        => 'MSC_PP_INVALID_VALUE',
35090                       p_message_text      => lv_message_text,
35091                       p_error_text        => lv_error_text,
35092                       p_token1            => 'COLUMN_NAME',
35093                       p_token_value1      => 'COMPANY_NAME');
35094 
35095       IF lv_return <> 0 THEN
35096         RAISE ex_logging_err;
35097       END IF;
35098 
35099 
35100       lv_return := MSC_ST_UTIL.DERIVE_COMPANY_ID
35101                         (p_table_name     => 'MSC_ST_COMPANY_USERS',
35102                          p_company_name   => 'COMPANY_NAME',
35103                          p_company_id     => 'SR_COMPANY_ID',
35104                          p_instance_code  => v_instance_code,
35105                          p_error_text     => lv_error_text,
35106                          p_batch_id       => lv_batch_id,
35107                          p_message_text   => lv_message_text,
35108                          p_debug          => v_debug);
35109 
35110       IF lv_return <> 0 THEN
35111         RAISE ex_logging_err;
35112       END IF;
35113 
35114 /*   Calling derive_company_id
35115       v_sql_stmt := 04;
35116 
35117       UPDATE MSC_ST_COMPANY_USERS mscu
35118       SET    SR_COMPANY_ID          = (SELECT local_id from MSC_LOCAL_ID_SETUP
35119               WHERE char1           = mscu.sr_instance_code
35120               and   NVL(char3,NULL_CHAR) = NVL(mscu.company_name,NULL_CHAR)
35121               and   entity_name     = 'SR_TP_ID'
35122               and   rownum          = 1) -- we need the first occurence of sr_tp_id
35123       WHERE  mscu.process_flag      = G_IN_PROCESS
35124       AND    mscu.sr_instance_code  = v_instance_code
35125       AND    mscu.batch_id          = lv_batch_id;
35126 
35127       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
35128                      (p_app_short_name    => 'MSC',
35129                       p_error_code        => 'MSC_PP_INVALID_VALUE',
35130                       p_message_text      => lv_message_text,
35131                       p_error_text        => lv_error_text,
35132                       p_token1            => 'COLUMN_NAME',
35133                       p_token_value1      => 'COMPANY_NAME');
35134 
35135       IF lv_return <> 0 THEN
35136         RAISE ex_logging_err;
35137       END IF;
35138 
35139       v_sql_stmt := 05;
35140 
35141       UPDATE msc_st_company_users
35142       SET    process_flag     = G_ERROR_FLG,
35143              error_text       = lv_message_text
35144       WHERE NVL(sr_company_id,NULL_VALUE) = NULL_VALUE
35145       AND   NVL(company_name,'-1') <> '-1'
35146       AND   process_flag   = G_IN_PROCESS
35147       AND   batch_id       = lv_batch_id
35148       AND   sr_instance_code = v_instance_code;
35149 */
35150       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
35151                      (p_app_short_name    => 'MSC',
35152                       p_error_code        => 'MSC_PP_INVALID_VALUE',
35153                       p_message_text      => lv_message_text,
35154                       p_error_text        => lv_error_text,
35155                       p_token1            => 'COLUMN_NAME',
35156                       p_token_value1      => 'USER_NAME');
35157 
35158       IF lv_return <> 0 THEN
35159         RAISE ex_logging_err;
35160       END IF;
35161 
35162       v_sql_stmt := 05.1;
35163 
35164 
35165       UPDATE msc_st_company_users mscu
35166       SET    mscu.process_flag     = G_ERROR_FLG,
35167              mscu.error_text       = lv_message_text
35168       WHERE ( mscu.user_name IS NULL OR NOT EXISTS(select '1' from fnd_user fu where fu.user_name =UPPER(mscu.user_name)))
35169       AND   mscu.process_flag   = G_IN_PROCESS
35170       AND   mscu.batch_id       = lv_batch_id
35171       AND   mscu.sr_instance_code = v_instance_code;
35172 
35173       v_sql_stmt := 05;
35174 
35175       UPDATE MSC_ST_COMPANY_USERS mscu
35176       SET PARTNER_TYPE                = (SELECT number1 from MSC_LOCAL_ID_SETUP
35177                 WHERE char1             = mscu.sr_instance_code
35178                 and   NVL(char3,NULL_CHAR) = NVL(mscu.company_name,NULL_CHAR)
35179                 and   entity_name       = 'SR_TP_ID'
35180                 and   number1 in (1,2)
35181                 and   rownum            = 1) -- we need the first occurence of sr_tp_id
35182       WHERE  mscu.process_flag        = G_IN_PROCESS
35183       AND    mscu.sr_instance_code    = v_instance_code
35184       AND    mscu.batch_id            = lv_batch_id;
35185 
35186 
35187       --Call to customised validation.
35188 
35189       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
35190           (ERRBUF         => lv_error_text,
35191            RETCODE        => lv_return,
35192            pBatchID       => lv_batch_id,
35193            pInstanceCode  => v_instance_code,
35194            pEntityName    => 'MSC_ST_COMPANY_USERS',
35195            pInstanceID    => v_instance_id);
35196 
35197       IF NVL(lv_return,0) <> 0 THEN
35198         RAISE ex_logging_err;
35199       END IF;
35200 
35201       -- Set the process flag as Valid and poulate instance_id
35202       lv_return :=
35203       MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name     => 'MSC_ST_COMPANY_USERS',
35204                                p_instance_id    => v_instance_id,
35205                                p_instance_code  => v_instance_code,
35206                                p_process_flag   => G_VALID,
35207                                p_error_text     => lv_error_text,
35208                                p_debug          => v_debug,
35209                                p_batch_id       => lv_batch_id);
35210 
35211       IF lv_return < 0 THEN
35212         RAISE ex_logging_err;
35213       END IF;
35214       COMMIT;
35215 
35216     END LOOP;
35217 
35218   EXCEPTION
35219     WHEN too_many_rows THEN
35220       lv_error_text :=substr('MSC_CL_PRE_PROCESS.LOAD_COMPANY_USERS'||'('
35221                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
35222         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
35223 
35224       ROLLBACK ;
35225 
35226     WHEN ex_logging_err THEN
35227       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'MSC_CL_PRE_PROCESS.LOAD_COMPANY_USERS: '||lv_error_text);
35228       ROLLBACK;
35229 
35230     WHEN OTHERS THEN
35231       lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_COMPANY_USERS'||'('
35232                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
35233         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
35234       ROLLBACK;
35235 
35236   END LOAD_COMPANY_USERS;
35237 
35238  /*==========================================================================+
35239   | DESCRIPTION  : This function performs insertion of co products records    |
35240   |                into msc_st_bom_componenets plus updates the records in    |
35241   |                msc_st_boms. Its called from LOAD_BOM after pre processing |
35242   |                CO PRODUCTS. Included for OSFM Integration                 |
35243   +==========================================================================*/
35244 
35245    PROCEDURE LOAD_CO_PRODUCT_BOMS IS
35246 
35247    TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
35248    lb_rowid               RowidTab;
35249    lv_sql_stmt            VARCHAR2(5000);
35250    v_query_str1           varchar2(6000);
35251    v_query_str2           varchar2(6000);
35252    v_query_str3           varchar2(6000);
35253    type cur_type is ref cursor;
35254    cur1 cur_type;
35255    cur2 cur_type;
35256    cur3 cur_type;
35257    l_co_product_id_cur1 	Number;
35258    l_component_id_cur1 		Number;
35259    l_split_cur1 		Number;
35260    l_primary_flag_cur1 		Number;
35261    l_bill_sequence_id_cur2	Number;
35262    l_rowid_cur2                 rowid;
35263    l_co_product_id_cur3		Number;
35264    l_component_id_cur3		Number;
35265    l_split_cur3			Number;
35266    l_primary_flag_cur3		Number;
35267    lv_return                    NUMBER;
35268    lv_error_text                VARCHAR2(250);
35269    lv_ODS_flag                  NUMBER; -- 1 if record from BOM ODS else 0.
35270    ex_logging_err               EXCEPTION;
35271 
35272  CURSOR c1 IS
35273     SELECT rowid
35274     FROM   msc_st_bom_components
35275     WHERE  process_flag          = G_IN_PROCESS
35276     AND    sr_instance_code      = v_instance_code
35277     AND    NVL(component_sequence_id,NULL_VALUE) = NULL_VALUE
35278     AND    deleted_flag           = SYS_NO;
35279 
35280 
35281 
35282    BEGIN
35283 
35284     -- selecting all the valid Co Products in the table --
35285      v_query_str1 :=    ' select mcp.co_product_id,  '
35286                      || ' mcp.component_id,          '
35287                      || ' mcp.split,                 '
35288        		     || ' mcp.primary_flag           '
35289        		     || ' from msc_st_co_products mcp'
35290       		     || ' where process_flag = '|| G_VALID
35291       		     || ' and sr_instance_code = :v_instance_code ';
35292 
35293 
35294    -- selecting all the valid boms for the selected Co Product from staging table + ODS--
35295     v_query_str2 :=   ' select  msb.bill_sequence_id,msb.rowid,0 '
35296          	   || ' from msc_st_boms msb '
35297                    || ' where msb.assembly_item_id = :lv_assembly_item_id '
35298                    || ' AND msb.process_flag = '|| G_VALID
35299                    || ' and sr_instance_code = :v_instance_code '
35300                    || ' UNION '
35301                    || ' select  mb.bill_sequence_id,mb.rowid,1 '
35302                    || ' from msc_boms mb, MSC_ITEM_ID_LID lid '
35303                    || ' where mb.assembly_item_id = lid.INVENTORY_ITEM_ID '
35304                    || ' and mb.sr_instance_id= :v_instance_id '
35305                    || ' and lid.SR_INVENTORY_ITEM_ID = :lv_assembly_item_id '
35306                    || ' and lid.SR_INSTANCE_ID = :v_instance_id '
35307                    || ' and mb.bill_sequence_id not in '
35308                    || '   ( select  msb1.bill_sequence_id '
35309                    || '     from msc_st_boms msb1'
35310                    || '     where msb1.assembly_item_id = :lv_assembly_item_id '
35311                    || '     AND msb1.process_flag = '|| G_VALID
35312                    || '     and sr_instance_code = :v_instance_code )';
35313 
35314 
35315 
35316   -- selecting all the other valid Co products  for the selected Co Product and Component--
35317     v_query_str3 :=   ' select mcp.co_product_id,  '
35318                    || ' mcp.component_id,          '
35319                    || ' mcp.split,                 '
35320        		   || ' mcp.primary_flag           '
35321        		   || ' from msc_st_co_products mcp'
35322       		   || ' where process_flag = '|| G_VALID
35323       		   || ' and component_id   = :lv_assembly_item_id '
35324       		   || ' and co_product_id   <> :lv_co_product_id  ';
35325 
35326 
35327 
35328 
35329 
35330          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,v_query_str1);
35331 
35332     v_sql_stmt := 01;
35333     OPEN cur1 FOR v_query_str1 USING v_instance_code;
35334     LOOP
35335 
35336       FETCH cur1 INTO
35337       l_co_product_id_cur1,l_component_id_cur1,l_split_cur1,l_primary_flag_cur1;
35338 
35339       EXIT WHEN cur1%NOTFOUND;
35340 
35341 
35342          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,v_query_str2);
35343 
35344 
35345       v_sql_stmt := 02;
35346       OPEN cur2 FOR v_query_str2 USING l_co_product_id_cur1,v_instance_code,
35347       v_instance_id,l_co_product_id_cur1,v_instance_id,l_co_product_id_cur1,v_instance_code;
35348 
35349       LOOP
35350 
35351         FETCH cur2 INTO l_bill_sequence_id_cur2,l_rowid_cur2,lv_ODS_flag ;
35352 
35353         EXIT WHEN cur2%NOTFOUND;
35354 
35355     -- set the primary_flag for the actual component to "not a primary"    --
35356     -- this means whether a co product is primary or not would matter only --
35357     -- for the set of products not including the actual component          --
35358      v_sql_stmt := 03;
35359     lv_sql_stmt :=
35360 
35361        'update MSC_ST_BOM_COMPONENTS x'
35362     || ' set x.primary_flag = 2'
35363     || ' where x.bill_Sequence_id='||l_bill_sequence_id_cur2
35364     || ' and x.primary_flag is null '
35365     || ' and nvl(x.component_type,0) != 10 ' -- component_type is 10 for co products
35366     || ' and x.usage_quantity > 0 '
35367     || ' and x.inventory_item_id =' ||l_component_id_cur1;
35368 
35369 
35370          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
35371 
35372      EXECUTE IMMEDIATE lv_sql_stmt;
35373      OPEN cur3 FOR v_query_str3 USING l_component_id_cur1, l_co_product_id_cur1;
35374 
35375       LOOP
35376 
35377 
35378          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,v_query_str3);
35379 
35380         v_sql_stmt := 04;
35381         FETCH cur3 INTO
35382         l_co_product_id_cur3,l_component_id_cur3,l_split_cur3,l_primary_flag_cur3;
35383 
35384         EXIT WHEN cur3%NOTFOUND;
35385 
35386         v_sql_stmt := 05;
35387         insert into MSC_ST_BOM_COMPONENTS
35388         ( INVENTORY_ITEM_ID,
35389           BILL_SEQUENCE_ID,
35390           COMPONENT_TYPE,
35391           USAGE_QUANTITY,
35392           COMPONENT_YIELD_FACTOR,
35393           EFFECTIVITY_DATE,
35394           DISABLE_DATE,
35395           OPERATION_OFFSET_PERCENT,
35396           OPTIONAL_COMPONENT,
35397           WIP_SUPPLY_TYPE,
35398           PLANNING_FACTOR,
35399           REVISED_ITEM_SEQUENCE_ID,
35400           ATP_FLAG,
35401           STATUS_TYPE,
35402           USE_UP_CODE,
35403           CHANGE_NOTICE,
35404           ORGANIZATION_ID,
35405           USING_ASSEMBLY_ID,
35406           FROM_UNIT_NUMBER,
35407           TO_UNIT_NUMBER,
35408           DRIVING_ITEM_ID,
35409           DELETED_FLAG,
35410           REFRESH_ID,
35411           SR_INSTANCE_CODE,
35412           PRIMARY_FLAG,
35413           ROUNDING_DIRECTION,
35414           process_flag)
35415           select
35416           l_co_product_id_cur3,
35417           l_bill_sequence_id_cur2,
35418           10,
35419           -(l_split_cur3/100),
35420           x.COMPONENT_YIELD_FACTOR,
35421           x.EFFECTIVITY_DATE,
35422           x.DISABLE_DATE,
35423           x.OPERATION_OFFSET_PERCENT,
35424           x.OPTIONAL_COMPONENT,
35425           x.WIP_SUPPLY_TYPE,
35426           x.PLANNING_FACTOR,
35427           x.REVISED_ITEM_SEQUENCE_ID,
35428           x.ATP_FLAG,
35429           x.STATUS_TYPE,
35430           x.USE_UP_CODE,
35431           x.CHANGE_NOTICE,
35432           x.ORGANIZATION_ID,
35433           l_co_product_id_cur1,
35434           x.FROM_UNIT_NUMBER,
35435           x.TO_UNIT_NUMBER,
35436           x.DRIVING_ITEM_ID,
35437           2,
35438           refresh_id,
35439           v_instance_code,
35440           l_primary_flag_cur3,
35441           3,
35442           2
35443         FROM MSC_ST_BOM_COMPONENTS x
35444         WHERE x.bill_Sequence_id = l_bill_sequence_id_cur2
35445         AND   x.sr_instance_id = v_instance_id
35446         AND rownum = 1; -- so that in the next loop only 1 record is inserted
35447 
35448       End loop;
35449 
35450   IF lv_ODS_flag = 0 THEN  -- if the BOM record is in the staging table
35451 
35452     v_sql_stmt := 06;
35453     update msc_st_boms
35454     set assembly_quantity = (l_split_cur1/100)
35455     where rowid = l_rowid_cur2 ;
35456 
35457   ELSE                    -- if the BOM record is in the ODS
35458 
35459       v_sql_stmt := 07;
35460       insert into MSC_ST_BOMS
35461        ( BILL_SEQUENCE_ID,
35462          ORGANIZATION_ID,
35463          ASSEMBLY_ITEM_ID,
35464          ASSEMBLY_TYPE,
35465          ALTERNATE_BOM_DESIGNATOR,
35466          SPECIFIC_ASSEMBLY_COMMENT,
35467          SCALING_TYPE,
35468          PENDING_FROM_ECN,
35469          UOM,
35470          ASSEMBLY_QUANTITY,
35471          DELETED_FLAG,
35472          REFRESH_ID,
35473          SR_INSTANCE_ID)
35474         SELECT
35475          msb.BILL_SEQUENCE_ID,
35476          msb.ORGANIZATION_ID,
35477          lid.SR_INVENTORY_ITEM_ID,
35478          msb.ASSEMBLY_TYPE,
35479          msb.ALTERNATE_BOM_DESIGNATOR,
35480          msb.SPECIFIC_ASSEMBLY_COMMENT,
35481          msb.SCALING_TYPE,
35482          msb.PENDING_FROM_ECN,
35483          msb.UOM,
35484          (l_split_cur1/100),
35485          2,
35486          v_refresh_id,
35487          v_instance_id
35488        -- note the join on MSC_ITEM_ID_LID because MSC_BOMS stores ASSEMBLY_ITEM_ID
35489        -- and MSC_ST_BOMS stores  SR_INVENTORY_ITEM_ID in ASSEMBLY_ITEM_ID
35490         FROM MSC_BOMS msb ,MSC_ITEM_ID_LID lid
35491         WHERE msb. BILL_SEQUENCE_ID = l_bill_sequence_id_cur2
35492         And lid.SR_INSTANCE_ID = msb.SR_INSTANCE_ID
35493         AND lid.INVENTORY_ITEM_ID = msb.ASSEMBLY_ITEM_ID ;
35494 
35495     END IF;
35496 
35497 
35498    ENd loop;
35499 
35500    v_sql_stmt := 08;
35501    -- Derive the component_sequence_id --
35502      lv_sql_stmt :=
35503      'UPDATE     msc_st_bom_components mbc'
35504      ||' SET     component_sequence_id=  (SELECT local_id '
35505      ||'         FROM msc_local_id_setup mlis'
35506      ||'         WHERE  mlis.char1 = mbc.sr_instance_code'
35507      ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
35508      ||'               =  NVL(mbc.company_name,'||''''||NULL_CHAR||''''||') '
35509      ||'         AND    mlis.char3 = mbc.organization_code'
35510      ||'         AND    mlis.char4 = mbc.bom_name'
35511      ||'         AND    mlis.char5 = mbc.component_name'
35512      ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
35513      ||'               =  NVL(mbc.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
35514      ||'         AND    mlis.char7 = mbc.operation_seq_code'
35515      ||'         AND    mlis.date1 = mbc.effectivity_date'
35516      ||'         AND    mlis.entity_name = ''COMPONENT_SEQUENCE_ID'')'
35517      ||'   WHERE      mbc.sr_instance_code = :v_instance_code'
35518      ||'   AND        mbc.process_flag     ='|| G_IN_PROCESS ;
35519 
35520 
35521 
35522 
35523             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
35524 
35525     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
35526 
35527    -- Generate the  component sequence id
35528       v_sql_stmt := 09;
35529       OPEN c1;
35530       FETCH c1 BULK COLLECT INTO lb_rowid ;
35531 
35532       IF c1%ROWCOUNT > 0  THEN
35533         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
35534 
35535           UPDATE msc_st_bom_components
35536           SET    component_sequence_id   = msc_st_component_sequence_id_s.NEXTVAL
35537           WHERE  rowid                   = lb_rowid(j);
35538 
35539 
35540             FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
35541 
35542             -- Insert into the LID table
35543 
35544              INSERT INTO  msc_local_id_setup
35545 	     (local_id,
35546 	      st_transaction_id,
35547 	      instance_id,
35548 	      entity_name,
35549 	      data_source_type,
35550 	      char1,
35551 	      char2,
35552 	      char3,
35553 	      char4,
35554 	      char5,
35555 	      char6,
35556 	      char7,
35557 	      date1,
35558 	      date2,
35559 	      last_update_date,
35560 	      last_updated_by,
35561 	      creation_date,
35562 	      created_by    )
35563 	      SELECT
35564 	      component_sequence_id,
35565 	      st_transaction_id,
35566 	      v_instance_id,
35567 	      'COMPONENT_SEQUENCE_ID',
35568 	      data_source_type,
35569 	      sr_instance_code,
35570 	      company_name,
35571 	      organization_code,
35572 	      bom_name,
35573 	      component_name,
35574 	      alternate_bom_designator,
35575 	      operation_seq_code,
35576 	      effectivity_date,
35577 	      disable_date,
35578 	      v_current_date,
35579 	      v_current_user,
35580 	      v_current_date,
35581 	      v_current_user
35582 	      FROM msc_st_bom_components
35583 	      WHERE rowid = lb_rowid(j) ;
35584 
35585 	END IF;
35586        CLOSE c1;
35587        COMMIT;
35588    End Loop;
35589 
35590    -- Set the process flag as Valid and poulate instance_id
35591     lv_return :=
35592     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name     => 'MSC_ST_BOM_COMPONENTS',
35593                                p_instance_id    => v_instance_id,
35594                                p_instance_code  => v_instance_code,
35595                                p_process_flag   => G_VALID,
35596                                p_error_text     => lv_error_text,
35597                                p_debug          => v_debug);
35598 
35599       IF lv_return <> 0 THEN
35600         RAISE ex_logging_err;
35601       END IF;
35602 
35603  EXCEPTION
35604 
35605     WHEN OTHERS THEN
35606       lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_CO_PRODUCT_BOMS'||'('
35607                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
35608         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
35609       ROLLBACK;
35610 
35611 
35612    END LOAD_CO_PRODUCT_BOMS; /*osfm change */
35613 
35614 
35615   /*==========================================================================+
35616   | DESCRIPTION  : This function performs the validation and loads the data   |
35617   |                for business object BOM.                                   |
35618   +==========================================================================*/
35619 PROCEDURE  LOAD_BOM IS
35620 
35621   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
35622   TYPE CurTyp           IS REF CURSOR;
35623   c1                    CurTyp;
35624   lb_rowid              RowidTab;
35625 
35626   lv_column_names       VARCHAR2(5000);     -- Stores cocatenated column names
35627   lv_return             NUMBER;
35628   lv_error_text         VARCHAR2(250);
35629   lv_where_str          VARCHAR2(5000);
35630   lv_sql_stmt           VARCHAR2(5000);
35631   lv_cursor_stmt        VARCHAR2(5000);
35632   lv_batch_id           msc_st_boms.batch_id%TYPE;
35633   lv_message_text       msc_errors.error_text%TYPE;
35634 
35635   ex_logging_err    EXCEPTION;
35636 
35637   CURSOR c2(p_batch_id NUMBER) IS
35638     SELECT rowid
35639     FROM   msc_st_boms
35640     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
35641     AND    sr_instance_code = v_instance_code
35642     AND    batch_id         = p_batch_id;
35643 
35644   CURSOR c3(p_batch_id NUMBER) IS
35645     SELECT rowid
35646     FROM   msc_st_boms
35647     WHERE  process_flag     = G_IN_PROCESS
35648     AND    sr_instance_code = v_instance_code
35649     AND    batch_id         = p_batch_id
35650     AND    NVL(bill_sequence_id,NULL_VALUE) = NULL_VALUE
35651     AND    deleted_flag      = SYS_NO;
35652 
35653   CURSOR c4(p_batch_id NUMBER) IS
35654     SELECT rowid
35655     FROM   msc_st_bom_components
35656     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
35657     AND    sr_instance_code = v_instance_code
35658     AND    batch_id         = p_batch_id;
35659 
35660  CURSOR c5(p_batch_id NUMBER) IS
35661     SELECT rowid
35662     FROM   msc_st_bom_components
35663     WHERE  process_flag          = G_IN_PROCESS
35664     AND    sr_instance_code      = v_instance_code
35665     AND    batch_id              = p_batch_id
35666     AND    NVL(component_sequence_id,NULL_VALUE) = NULL_VALUE
35667     AND    deleted_flag           = SYS_NO;
35668 
35669   CURSOR c6(p_batch_id NUMBER) IS
35670     SELECT rowid
35671     FROM   msc_st_component_substitutes
35672     WHERE  process_flag     IN (G_IN_PROCESS,G_ERROR_FLG)
35673     AND    sr_instance_code = v_instance_code
35674     AND    batch_id         = p_batch_id;
35675 
35676   CURSOR c7(p_batch_id NUMBER) IS
35677     SELECT mlis.rowid
35678     FROM   msc_st_bom_components mbc, msc_local_id_setup mlis
35679     WHERE  mlis.char1               = mbc.sr_instance_code
35680     AND    NVL(mlis.char2,'-23453') = NVL(mbc.company_name,'-23453')
35681     AND    mlis.char3               = mbc.organization_code
35682     AND    mlis.char4               = mbc.bom_name
35683     AND    mlis.char5               = mbc.component_name
35684     AND    NVL(mlis.char6,'-23453') = NVL(mbc.alternate_bom_designator,'-23453')
35685     AND    mlis.char7               = mbc.operation_seq_code
35686     AND    mlis.date1               = mbc.effectivity_date
35687     AND    mbc.batch_id             = p_batch_id
35688     AND    mbc.deleted_flag         = SYS_YES
35689     AND    mbc.process_flag         = G_IN_PROCESS
35690     AND    mlis.entity_name         = 'COMPONENT_SEQUENCE_ID'
35691     AND    mlis.instance_id         = v_instance_id ;
35692 
35693      --  For Co Products --
35694   CURSOR c8(p_batch_id NUMBER) IS
35695     SELECT rowid
35696     FROM   msc_st_co_products
35697     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
35698     AND    sr_instance_code = v_instance_code
35699     AND    batch_id         = p_batch_id;
35700 
35701   CURSOR c9 IS
35702     SELECT max(rowid)
35703     FROM   msc_st_co_products
35704     WHERE  process_flag     = G_IN_PROCESS
35705     AND    sr_instance_code = v_instance_code
35706     AND    NVL(co_product_group_id,NULL_VALUE) = NULL_VALUE
35707     AND    deleted_flag     = SYS_NO
35708     GROUP BY component_name,sr_instance_code,company_name;
35709 
35710 
35711   BEGIN
35712 
35713   -- Validation fo table msc_st_boms
35714 
35715   -- Before we start processing the record by group id( batch size ) we are going
35716   -- to check whether that there
35717   -- is any duplicates for user defined unique keys (UDKs,)
35718 
35719    --Duplicate records check for the records whose source is XML
35720 
35721     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
35722                  (p_app_short_name    => 'MSC',
35723                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
35724                   p_message_text      => lv_message_text,
35725                   p_error_text        => lv_error_text);
35726 
35727     IF lv_return <> 0 THEN
35728       RAISE ex_logging_err;
35729     END IF;
35730 
35731   v_sql_stmt := 01;
35732   lv_sql_stmt :=
35733   'UPDATE   msc_st_boms msb1'
35734   ||' SET     process_flag  = '||G_ERROR_FLG||','
35735   ||'         error_text   = '||''''||lv_message_text||''''
35736   ||' WHERE   message_id <  (SELECT MAX(message_id)'
35737   ||'         FROM msc_st_boms  msb2'
35738   ||'               WHERE msb2.sr_instance_code = msb1.sr_instance_code'
35739   ||'               AND msb2.organization_code  = msb1.organization_code'
35740   ||'               AND NVL(msb2.bom_name,'||''''||NULL_CHAR||''''||') '
35741   ||'                   = NVL(msb1.bom_name, '||''''||NULL_CHAR||''''||') '
35742   ||'               AND NVL(msb2.company_name,'||''''||NULL_CHAR||''''||') '
35743   ||'                   = NVL(msb1.company_name, '||''''||NULL_CHAR||''''||') '
35744   ||'               AND msb2.assembly_name      = msb1.assembly_name'
35745   ||'               AND NVL(msb2.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
35746   ||'                   = NVL(msb1.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
35747   ||'               AND msb2.process_flag        = '||G_IN_PROCESS
35748   ||'               AND    NVL(msb2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||') '
35749   ||' AND     msb1.process_flag ='|| G_IN_PROCESS
35750   ||' AND     msb1.sr_instance_code = :v_instance_code '
35751   ||' AND     NVL(msb1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE ;
35752 
35753 
35754          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
35755 
35756   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
35757 
35758   --Duplicate records check for the records whose source is batch load
35759 
35760     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
35761                  (p_app_short_name    => 'MSC',
35762                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
35763                   p_message_text      => lv_message_text,
35764                   p_error_text        => lv_error_text);
35765 
35766     IF lv_return <> 0 THEN
35767       RAISE ex_logging_err;
35768     END IF;
35769   v_sql_stmt := 02;
35770 
35771   lv_sql_stmt :=
35772   'UPDATE     msc_st_boms msb1'
35773   ||' SET     process_flag  = '||G_ERROR_FLG||','
35774   ||'         error_text   = '||''''||lv_message_text||''''
35775   ||' WHERE   EXISTS ( SELECT 1 FROM msc_st_boms msb2'
35776   ||'               WHERE msb2.sr_instance_code = msb1.sr_instance_code'
35777   ||'               AND msb2.organization_code  = msb1.organization_code'
35778   ||'               AND NVL(msb2.bom_name,'||''''||NULL_CHAR||''''||') '
35779   ||'                   = NVL(msb1.bom_name, '||''''||NULL_CHAR||''''||') '
35780   ||'               AND NVL(msb2.company_name,'||''''||NULL_CHAR||''''||') '
35781   ||'                   = NVL(msb1.company_name, '||''''||NULL_CHAR||''''||') '
35782   ||'               AND msb2.assembly_name      = msb1.assembly_name'
35783   ||'               AND NVL(msb2.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
35784   ||'                   = NVL(msb1.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
35785   ||'               AND msb2.process_flag        = '||G_IN_PROCESS
35786   ||'               AND NVL(msb2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
35787   ||'               AND msb2.rowid <> msb1.rowid)'
35788   ||' AND     msb1.process_flag ='|| G_IN_PROCESS
35789   ||' AND     msb1.sr_instance_code = :v_instance_code'
35790   ||' AND     NVL(msb1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
35791 
35792 
35793          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
35794 
35795   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
35796 
35797 
35798   lv_column_names :=
35799   'BOM_NAME                     ||''~''||'
35800   ||'ORGANIZATION_CODE          ||''~''||'
35801   ||'ASSEMBLY_NAME              ||''~''||'
35802   ||'ASSEMBLY_TYPE              ||''~''||'
35803   ||'SR_INSTANCE_CODE           ||''~''||'
35804   ||'ALTERNATE_BOM_DESIGNATOR   ||''~''||'
35805   ||'SPECIFIC_ASSEMBLY_COMMENT  ||''~''||'
35806   ||'PENDING_FROM_ECN           ||''~''||'
35807   ||'BOM_SCALING_TYPE           ||''~''||'
35808   ||'ASSEMBLY_QUANTITY          ||''~''||'
35809   ||'UOM                        ||''~''||'
35810   ||'DELETED_FLAG               ||''~''||'
35811   ||'COMPANY_NAME' ;
35812 
35813    LOOP
35814       v_sql_stmt := 03;
35815       lv_cursor_stmt :=
35816       ' SELECT msc_st_batch_id_s.NEXTVAL '
35817       ||' FROM   dual';
35818 
35819       OPEN  c1 FOR lv_cursor_stmt;
35820       FETCH c1 INTO lv_batch_id;
35821       CLOSE c1;
35822 
35823       v_sql_stmt := 04;
35824       lv_sql_stmt :=
35825       ' UPDATE msc_st_boms '
35826       ||' SET    batch_id  = :lv_batch_id'
35827       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
35828       ||' AND    sr_instance_code               = :v_instance_code'
35829       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
35830       ||' AND    rownum                        <= '||v_batch_size;
35831 
35832 
35833          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
35834 
35835       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
35836 
35837       EXIT WHEN SQL%NOTFOUND;
35838 
35839       OPEN c2(lv_batch_id);
35840       FETCH c2 BULK COLLECT INTO lb_rowid;
35841       CLOSE c2;
35842 
35843       v_sql_stmt := 05;
35844       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
35845       UPDATE msc_st_boms
35846       SET  st_transaction_id = msc_st_boms_s.NEXTVAL,
35847            refresh_id        = v_refresh_id,
35848            last_update_date  = v_current_date,
35849            last_updated_by   = v_current_user,
35850            creation_date     = v_current_date,
35851            created_by        = v_current_user
35852       WHERE  rowid           = lb_rowid(j);
35853 
35854 
35855      -- Set the error message
35856 
35857     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
35858                      (p_app_short_name    => 'MSC',
35859                       p_error_code        => 'MSC_PP_NO_DELETION',
35860                       p_message_text      => lv_message_text,
35861                       p_error_text        => lv_error_text,
35862                       p_token1            => 'TABLE_NAME',
35863                       p_token_value1      => 'MSC_ST_BOMS');
35864 
35865       IF lv_return <> 0 THEN
35866         RAISE ex_logging_err;
35867       END IF;
35868 
35869       --Deletion is not allowed on this table.
35870       v_sql_stmt := 06;
35871       lv_sql_stmt :=
35872       ' UPDATE     msc_st_boms '
35873       ||' SET      process_flag     = '||G_ERROR_FLG||','
35874       ||'          error_text       = '||''''||lv_message_text||''''
35875       ||' WHERE    deleted_flag     = '||SYS_YES
35876       ||' AND      process_flag     = '||G_IN_PROCESS
35877       ||' AND      batch_id         =  :lv_batch_id'
35878       ||' AND      sr_instance_code =  :v_instance_code';
35879 
35880 
35881             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
35882 
35883       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
35884 
35885     -- Set the message
35886 
35887     lv_return :=    MSC_ST_UTIL.GET_ERROR_MESSAGE
35888                      (p_app_short_name    => 'MSC',
35889                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
35890                       p_message_text      => lv_message_text,
35891                       p_error_text        => lv_error_text,
35892                       p_token1            => 'COLUMN_NAME',
35893                       p_token_value1      => 'DELETED_FLAG',
35894                       p_token2            => 'DEFAULT_VALUE',
35895                       p_token_value2      =>  SYS_NO);
35896 
35897       IF lv_return <> 0 THEN
35898         RAISE ex_logging_err;
35899       END IF;
35900 
35901 
35902       -- Check DELETED_FLAG column has valid value or not and if not set it to SYS_NO
35903       -- Log error procedure inserts warning and also defaults the delete flag
35904 
35905      lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||') <>'||SYS_NO;
35906 
35907      lv_return := MSC_ST_UTIL.LOG_ERROR
35908                     (p_table_name        => 'MSC_ST_BOMS',
35909                      p_instance_code     => v_instance_code,
35910                      p_row               => lv_column_names,
35911                      p_severity          => G_SEV_WARNING,
35912                      p_message_text      => lv_message_text,
35913                      p_error_text        => lv_error_text,
35914                      p_batch_id          => lv_batch_id,
35915                      p_where_str         => lv_where_str,
35916                      p_col_name          => 'DELETED_FLAG',
35917                      p_debug             => v_debug,
35918                      p_default_value     =>  SYS_NO);
35919 
35920 
35921       IF lv_return <> 0 THEN
35922           RAISE ex_logging_err;
35923       END IF;
35924 
35925       -- Set the message
35926 
35927      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
35928                      (p_app_short_name    => 'MSC',
35929                       p_error_code        => 'MSC_PP_INVALID_VALUE',
35930                       p_message_text      => lv_message_text,
35931                       p_error_text        => lv_error_text,
35932                       p_token1            => 'COLUMN_NAME',
35933                       p_token_value1      => 'ORGANIZATION_CODE');
35934 
35935       IF lv_return <> 0 THEN
35936         RAISE ex_logging_err;
35937       END IF;
35938 
35939       -- Populate  organization id
35940 
35941      lv_return :=
35942      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_BOMS',
35943                                     p_org_partner_name  => 'ORGANIZATION_CODE',
35944                                     p_org_partner_id    => 'ORGANIZATION_ID',
35945                                     p_instance_code     => v_instance_code,
35946                                     p_partner_type      => G_ORGANIZATION,
35947                                     p_severity          => G_SEV_ERROR,
35948                                     p_message_text      => lv_message_text,
35949                                     p_batch_id          => lv_batch_id,
35950                                     p_debug             => v_debug,
35951                                     p_error_text        => lv_error_text);
35952 
35953      if(lv_return <> 0) then
35954         RAISE ex_logging_err;
35955      end if;
35956 
35957       -- Set the message
35958 
35959      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
35960                      (p_app_short_name    => 'MSC',
35961                       p_error_code        => 'MSC_PP_INVALID_VALUE',
35962                       p_message_text      => lv_message_text,
35963                       p_error_text        => lv_error_text,
35964                       p_token1            => 'COLUMN_NAME',
35965                       p_token_value1      => 'ASSEMBLY_NAME');
35966 
35967       IF lv_return <> 0 THEN
35968         RAISE ex_logging_err;
35969       END IF;
35970 
35971       -- Poplulate assembly item id
35972      lv_return :=
35973      MSC_ST_UTIL.DERIVE_ITEM_ID(p_table_name =>'MSC_ST_BOMS',
35974                              p_item_col_name => 'ASSEMBLY_NAME',
35975                              p_item_col_id   =>'ASSEMBLY_ITEM_ID',
35976                              p_instance_code => v_instance_code,
35977                              p_instance_id   => v_instance_id,
35978                              p_severity      => G_SEV_ERROR,
35979                              p_message_text  => lv_message_text,
35980                              p_batch_id      =>lv_batch_id,
35981                              p_debug         => v_debug,
35982                              p_error_text    =>lv_error_text);
35983 
35984 
35985     if(lv_return <> 0) then
35986          RAISE ex_logging_err;
35987     end if;
35988 
35989 
35990      -- Now we will check whether BOM Name is NULL , if it is NULL we will populate the
35991      -- ASSEMBLY NAME in BOM NAME column for all the records
35992 
35993     v_sql_stmt := 07;
35994 
35995     lv_sql_stmt :=
35996 
35997     'UPDATE msc_st_boms  msb'
35998     ||' SET    msb.bom_name            = msb.assembly_name'
35999     ||' WHERE  sr_instance_code        = :v_instance_code'
36000     ||' AND    msb.process_flag        ='||G_IN_PROCESS
36001     ||' AND    NVL(msb.bom_name,'||''''||NULL_CHAR||''''||') '
36002     ||'              = '||''''||NULL_CHAR||''''
36003     ||' AND    msb.batch_id            = :lv_batch_id';
36004 
36005 
36006             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36007 
36008     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
36009 
36010 
36011     -- Derive the BILL_SEQUENCE_ID for the BOM from LOCAL ID table
36012 
36013     lv_return :=
36014     MSC_ST_UTIL.DERIVE_BILL_SEQUENCE_ID
36015            (p_table_name    => 'MSC_ST_BOMS',
36016            p_bom_col_name   => 'BOM_NAME',
36017            p_bom_col_id     => 'BILL_SEQUENCE_ID',
36018            p_instance_code  => v_instance_code,
36019            p_batch_id       => lv_batch_id,
36020            p_debug          => v_debug,
36021            p_error_text     => lv_error_text);
36022 
36023     IF (lv_return <> 0 ) THEN
36024            RAISE ex_logging_err;
36025     END IF;
36026 
36027      -- Set the error message
36028     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36029                      (p_app_short_name    => 'MSC',
36030                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
36031                       p_message_text      => lv_message_text,
36032                       p_error_text        => lv_error_text,
36033                       p_token1            => 'COLUMN_NAME',
36034                       p_token_value1      => 'ASSEMBLY_QUANTITY',
36035                       p_token2           => 'DEFAULT_VALUE',
36036                       p_token_value2     =>  G_ASSEMBLY_QUANTITY  );
36037 
36038       IF lv_return <> 0 THEN
36039         RAISE ex_logging_err;
36040       END IF;
36041 
36042 -- To make ERP collection and Legacy in sync
36043 /*     --ASSEMBLY_QUANTITY default to 1 if not provided
36044      --Log warning in error table
36045      lv_where_str :=
36046     ' AND NVL(assembly_quantity,'||NULL_VALUE||') ='|| NULL_VALUE;
36047 
36048     lv_return := MSC_ST_UTIL.LOG_ERROR
36049                     (p_table_name        => 'MSC_ST_BOMS',
36050                      p_instance_code     => v_instance_code,
36051                      p_row               => lv_column_names,
36052                      p_severity          => G_SEV_WARNING,
36053                      p_message_text      => lv_message_text,
36054                      p_error_text        => lv_error_text,
36055                      p_batch_id          => lv_batch_id,
36056                      p_where_str         => lv_where_str,
36057                      p_col_name          => 'assembly_quantity',
36058                      p_debug             => v_debug,
36059                      p_default_value     => G_ASSEMBLY_QUANTITY);
36060 
36061       IF lv_return <> 0 THEN
36062           RAISE ex_logging_err;
36063       END IF;
36064 */
36065     -- Now check for UOM code if it is Invalid error out
36066     -- Set the error message
36067 
36068     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36069                      (p_app_short_name    => 'MSC',
36070                       p_error_code        => 'MSC_PP_INVALID_VALUE',
36071                       p_message_text      => lv_message_text,
36072                       p_error_text        => lv_error_text,
36073                       p_token1            => 'COLUMN_NAME',
36074                       p_token_value1      => 'UOM_CODE');
36075 
36076       IF lv_return <> 0 THEN
36077         RAISE ex_logging_err;
36078       END IF;
36079 
36080     -- If UOM is  provided  validate the UOM_CODE
36081     -- For validation, use ODS table MSC_UNITS_OF_MEASURE table
36082      v_sql_stmt := 08;
36083 
36084      lv_sql_stmt :=
36085      '   UPDATE     msc_st_boms msb'
36086      ||' SET        process_flag = '||G_ERROR_FLG||','
36087      ||'            error_text   = '||''''||lv_message_text||''''
36088      ||' WHERE      NOT EXISTS (SELECT 1'
36089      ||'            FROM msc_units_of_measure muom'
36090      ||'            WHERE muom.uom_code       = msb.uom'
36091      ||'            UNION'
36092      ||'            SELECT 1 FROM msc_st_units_of_measure msuom'
36093      ||'            WHERE msuom.uom_code       = msb.uom'
36094      ||'       AND   msuom.sr_instance_id     = :v_instance_id'
36095      ||'       AND   msuom.process_flag       = '||G_VALID||' )'
36096      ||' AND NVL(msb.uom,'||''''||NULL_CHAR||''''||') '
36097      ||'        <> '||''''||NULL_CHAR||''''
36098      ||' AND msb.sr_instance_code   = :v_instance_code'
36099      ||' AND msb.batch_id           = :lv_batch_id'
36100      ||' AND msb.process_flag       = '||G_IN_PROCESS ;
36101 
36102 
36103 
36104          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36105 
36106       EXECUTE IMMEDIATE lv_sql_stmt
36107               USING     v_instance_id,
36108                         v_instance_code,
36109                         lv_batch_id;
36110 
36111      -- Set the error message
36112     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36113                      (p_app_short_name    => 'MSC',
36114                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
36115                       p_message_text      => lv_message_text,
36116                       p_error_text        => lv_error_text,
36117                       p_token1            => 'COLUMN_NAME',
36118                       p_token_value1      => 'ASSEMBLY_TYPE',
36119                       p_token2           => 'DEFAULT_VALUE',
36120                       p_token_value2     =>  G_ASSEMBLY_TYPE  );
36121 
36122       IF lv_return <> 0 THEN
36123         RAISE ex_logging_err;
36124       END IF;
36125 
36126   --ASSEMBLY_TYPE default to 1 if value NOT IN (1,2)
36127   --Log warning in error table
36128 
36129     lv_where_str :=
36130     ' AND NVL(assembly_type,'||NULL_VALUE||') NOT IN (1,2)';
36131 
36132     lv_return := MSC_ST_UTIL.LOG_ERROR
36133                     (p_table_name       => 'MSC_ST_BOMS',
36134                      p_instance_code     => v_instance_code,
36135                      p_row               => lv_column_names,
36136                      p_severity          => G_SEV_WARNING,
36137                      p_message_text      => lv_message_text,
36138                      p_error_text        => lv_error_text,
36139                      p_batch_id          => lv_batch_id,
36140                      p_where_str         => lv_where_str,
36141                      p_col_name          => 'ASSEMBLY_TYPE',
36142                      p_debug             => v_debug,
36143                      p_default_value     => G_ASSEMBLY_TYPE);
36144 
36145 
36146       IF lv_return <> 0 THEN
36147            RAISE ex_logging_err;
36148       END IF;
36149 
36150     -- Set the error message
36151     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36152                      (p_app_short_name    => 'MSC',
36153                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
36154                       p_message_text      => lv_message_text,
36155                       p_error_text        => lv_error_text,
36156                       p_token1            => 'COLUMN_NAME',
36157                       p_token_value1      => 'BOM_SCALING_TYPE',
36158                       p_token2           => 'DEFAULT_VALUE',
36159                       p_token_value2     =>  G_BOM_SCALING_TYPE  );
36160 
36161       IF lv_return <> 0 THEN
36162         RAISE ex_logging_err;
36163       END IF;
36164 
36165    --  SCALING_TYPE NULL ( valid value NULL,0,1,2)
36166    --  Defaulting as NULL if invalid values
36167 
36168     lv_where_str := ' AND bom_scaling_type NOT IN (0,1,2)';
36169 
36170     lv_return := MSC_ST_UTIL.LOG_ERROR
36171                     (p_table_name        => 'MSC_ST_BOMS',
36172                      p_instance_code     => v_instance_code,
36173                      p_row               => lv_column_names,
36174                      p_severity          => G_SEV_WARNING,
36175                      p_message_text      => lv_message_text,
36176                      p_error_text        => lv_error_text,
36177                      p_batch_id          => lv_batch_id,
36178                      p_where_str         => lv_where_str,
36179                      p_col_name          => 'BOM_SCALING_TYPE',
36180                      p_debug             => v_debug,
36181                      p_default_value     => G_BOM_SCALING_TYPE);
36182 
36183       IF lv_return <> 0 THEN
36184          RAISE ex_logging_err;
36185       END IF;
36186 
36187     --Call to customised validation.
36188 
36189       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
36190         (ERRBUF         => lv_error_text,
36191          RETCODE        => lv_return,
36192          pBatchID       => lv_batch_id,
36193          pInstanceCode  => v_instance_code,
36194          pEntityName    => 'MSC_ST_BOMS',
36195          pInstanceID    => v_instance_id);
36196 
36197       IF NVL(lv_return,0) <> 0 THEN
36198         RAISE ex_logging_err;
36199       END IF;
36200 
36201    -- Now  generate bill_sequence_id for New BOM and populate the same in
36202    -- LID table and MSC_ST_BOMS  , Sequence to be used MSC_ST_BILL_SEQUENCE_ID_S
36203 
36204      v_sql_stmt := 09;
36205 
36206      OPEN c3(lv_batch_id);
36207      FETCH c3 BULK COLLECT INTO lb_rowid ;
36208 
36209      IF c3%ROWCOUNT > 0  THEN
36210 
36211     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
36212 
36213         UPDATE msc_st_boms
36214         SET    bill_sequence_id       = msc_st_bill_sequence_id_s.NEXTVAL
36215         WHERE  rowid                  = lb_rowid(j);
36216 
36217     v_sql_stmt := 10;
36218 
36219    FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
36220 
36221     -- Insert into the LID table this new BOM with the  user defined key
36222 
36223        INSERT INTO  msc_local_id_setup
36224        (local_id,
36225        st_transaction_id,
36226        instance_id,
36227        entity_name,
36228        data_source_type,
36229        char1,
36230        char2,
36231        char3,
36232        char4,
36233        char5,
36234        char6,
36235        SOURCE_ORGANIZATION_ID,
36236        SOURCE_BILL_SEQUENCE_ID,
36237        SOURCE_INVENTORY_ITEM_ID,
36238        last_update_date,
36239        last_updated_by,
36240        creation_date,
36241        created_by  )
36242        SELECT
36243        bill_sequence_id,
36244        st_transaction_id,
36245        v_instance_id,
36246        'BILL_SEQUENCE_ID',
36247        data_source_type,
36248        sr_instance_code,
36249        company_name,
36250        organization_code,
36251        bom_name,
36252        assembly_name,
36253        alternate_bom_designator,
36254        SOURCE_ORGANIZATION_ID,
36255        SOURCE_BILL_SEQUENCE_ID,
36256        SOURCE_INVENTORY_ITEM_ID,
36257        v_current_date,
36258        v_current_user,
36259        v_current_date,
36260        v_current_user
36261        FROM msc_st_boms
36262        WHERE   rowid   =   lb_rowid(j);
36263 
36264        END IF;
36265     CLOSE c3;
36266 
36267     -- Set the process flag as Valid and poulate instance_id
36268     lv_return :=
36269     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_BOMS',
36270                                p_instance_id    => v_instance_id,
36271                                p_instance_code  => v_instance_code,
36272                                p_process_flag   => G_VALID,
36273                                p_error_text     => lv_error_text,
36274                                p_debug          => v_debug,
36275                                p_batch_id       => lv_batch_id);
36276 
36277       IF lv_return <> 0 THEN
36278           RAISE ex_logging_err;
36279       END IF;
36280 
36281     -- At the end calling  the LOG_ERROR for logging all errored out records
36282 
36283    lv_return :=
36284    MSC_ST_UTIL.LOG_ERROR(p_table_name    =>'MSC_ST_BOMS',
36285                          p_instance_code =>v_instance_code,
36286                          p_row           =>lv_column_names,
36287                          p_severity      =>G_SEV_ERROR,
36288                          p_message_text  =>NULL,
36289                          p_error_text    =>lv_error_text,
36290                          p_debug         => v_debug,
36291                          p_batch_id      => lv_batch_id  );
36292 
36293   IF(lv_return <> 0) then
36294        RAISE ex_logging_err;
36295   END IF;
36296    COMMIT;
36297   END LOOP;
36298 
36299 
36300    -- Now Validation for MSC_ST_BOM_COMPONENTS
36301 
36302    --Duplicate records check for the records whose source is XML
36303 
36304     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36305                  (p_app_short_name    => 'MSC',
36306                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
36307                   p_message_text      => lv_message_text,
36308                   p_error_text        => lv_error_text);
36309 
36310     IF lv_return <> 0 THEN
36311       RAISE ex_logging_err;
36312     END IF;
36313 
36314   v_sql_stmt := 11;
36315 
36316   lv_sql_stmt :=
36317   'UPDATE msc_st_bom_components mbc1'
36318   ||'  SET process_flag = '||G_ERROR_FLG ||','
36319   ||'         error_text   = '||''''||lv_message_text||''''
36320   ||'  WHERE message_id < ( SELECT max(message_id)'
36321   ||'                       FROM msc_st_bom_components mbc2'
36322   ||'            WHERE mbc2.sr_instance_code        = mbc1.sr_instance_code'
36323   ||'            AND mbc2.organization_code         = mbc1.organization_code'
36324   ||'            AND NVL(mbc2.bom_name,'||''''||NULL_CHAR||''''||') '
36325   ||'                  = NVL(mbc1.bom_name, '||''''||NULL_CHAR||''''||') '
36326   ||'            AND NVL(mbc2.company_name,'||''''||NULL_CHAR||''''||') '
36327   ||'                   = NVL(mbc1.company_name, '||''''||NULL_CHAR||''''||') '
36328   ||'            AND mbc2.assembly_name = mbc1.assembly_name '
36329   ||'            AND NVL(mbc2.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
36330   ||'                   = NVL(mbc1.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
36331   ||'            AND mbc2.component_name            = mbc1.component_name'
36332   ||'            AND NVL(mbc2.effectivity_date,SYSDATE-36500 )'
36333   ||'                   = NVL(mbc1.effectivity_date,SYSDATE-36500)'
36334   ||'            AND NVL(mbc2.operation_seq_code, '||''''||NULL_CHAR||''''||') '
36335   ||'                   = NVL(mbc1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
36336   ||'            AND mbc2.process_flag              ='||G_IN_PROCESS
36337   ||'            AND NVL(mbc2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
36338   ||' AND  mbc1.process_flag     = '||G_IN_PROCESS
36339   ||' AND  mbc1.sr_instance_code =  :v_instance_code'
36340   ||' AND  NVL(mbc1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
36341 
36342 
36343          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36344 
36345   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
36346 
36347   --Duplicate records check for the records whose source is batch load
36348 
36349     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36350                  (p_app_short_name    => 'MSC',
36351                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
36352                   p_message_text      => lv_message_text,
36353                   p_error_text        => lv_error_text);
36354 
36355     IF lv_return <> 0 THEN
36356       RAISE ex_logging_err;
36357     END IF;
36358 
36359   v_sql_stmt := 12;
36360   lv_sql_stmt:=
36361   'UPDATE msc_st_bom_components mbc1'
36362   ||' SET         process_flag ='|| G_ERROR_FLG||','
36363   ||'             error_text   = '||''''||lv_message_text||''''
36364   ||' WHERE EXISTS ( SELECT 1'
36365   ||'            FROM msc_st_bom_components mbc2'
36366   ||'            WHERE mbc2.sr_instance_code        = mbc1.sr_instance_code'
36367   ||'            AND mbc2.organization_code         = mbc1.organization_code'
36368   ||'            AND NVL(mbc2.bom_name,'||''''||NULL_CHAR||''''||') '
36369   ||'                  = NVL(mbc1.bom_name, '||''''||NULL_CHAR||''''||') '
36370   ||'            AND NVL(mbc2.company_name,'||''''||NULL_CHAR||''''||') '
36371   ||'                   = NVL(mbc1.company_name, '||''''||NULL_CHAR||''''||') '
36372   ||'            AND mbc2.assembly_name = mbc1.assembly_name '
36373   ||'           AND NVL(mbc2.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
36374   ||'                   = NVL(mbc1.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
36375   ||'            AND mbc2.component_name            = mbc1.component_name'
36376   ||'            AND NVL(mbc2.effectivity_date,SYSDATE-36500 )'
36377   ||'                   = NVL(mbc1.effectivity_date,SYSDATE-36500 )'
36378   ||'            AND NVL(mbc2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
36379   ||'                    = NVL(mbc1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
36380   ||'            AND mbc2.process_flag              ='||G_IN_PROCESS
36381   ||'            AND NVL(mbc2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
36382   ||'            AND mbc2.rowid <> mbc1.rowid)'
36383   ||' AND mbc1.process_flag              ='|| G_IN_PROCESS
36384   ||' AND mbc1.sr_instance_code          = :v_instance_code'
36385   ||' AND NVL(mbc1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
36386 
36387 
36388             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36389 
36390   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
36391 
36392   lv_column_names :=
36393   'COMPONENT_NAME             ||''~''||'
36394   ||'COMPONENT_TYPE           ||''~''||'
36395   ||'BOM_NAME                 ||''~''||'
36396   ||'ORGANIZATION_CODE        ||''~''||'
36397   ||'ASSEMBLY_NAME            ||''~''||'
36398   ||'REVISION                 ||''~''||'
36399   ||'USAGE_QUANTITY           ||''~''||'
36400   ||'COMPONENT_YIELD_FACTOR   ||''~''||'
36401   ||'UOM_CODE                 ||''~''||'
36402   ||'SR_INSTANCE_CODE         ||''~''||'
36403   ||'ALTERNATE_BOM_DESIGNATOR ||''~''||'
36404   ||'CHANGE_NOTICE            ||''~''||'
36405   ||'SCALING_TYPE             ||''~''||'
36406   ||'EFFECTIVITY_DATE         ||''~''||'
36407   ||'DISABLE_DATE             ||''~''||'
36408   ||'USE_UP_CODE              ||''~''||'
36409   ||'OPERATION_OFFSET_PERCENT ||''~''||'
36410   ||'OPTIONAL_COMPONENT       ||''~''||'
36411   ||'WIP_SUPPLY_TYPE          ||''~''||'
36412   ||'PLANNING_FACTOR          ||''~''||'
36413   ||'ATP_FLAG                 ||''~''||'
36414   ||'OPERATION_SEQ_CODE       ||''~''||'
36415   ||'DELETED_FLAG             ||''~''||'
36416   ||'DRIVING_ITEM_NAME        ||''~''||'
36417   ||'COMPANY_NAME' ;
36418 
36419 
36420    -- Process  msc_st_bom_components table
36421 
36422    LOOP
36423       v_sql_stmt := 13;
36424       lv_cursor_stmt :=
36425       ' SELECT msc_st_batch_id_s.NEXTVAL '
36426       ||' FROM   dual';
36427 
36428       OPEN  c1 FOR lv_cursor_stmt;
36429       FETCH c1 INTO lv_batch_id;
36430       CLOSE c1;
36431 
36432       v_sql_stmt := 14;
36433       lv_sql_stmt :=
36434       ' UPDATE   msc_st_bom_components '
36435       ||' SET    batch_id  = :lv_batch_id'
36436       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
36437       ||' AND    sr_instance_code               = :v_instance_code'
36438       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
36439       ||' AND    rownum                        <= '||v_batch_size;
36440 
36441 
36442          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36443 
36444       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
36445 
36446       EXIT WHEN SQL%NOTFOUND;
36447 
36448       OPEN c4(lv_batch_id);
36449       FETCH c4 BULK COLLECT INTO lb_rowid;
36450       CLOSE c4;
36451 
36452       v_sql_stmt := 1;
36453       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
36454       UPDATE msc_st_bom_components
36455       SET  st_transaction_id = msc_st_bom_components_s.NEXTVAL,
36456            refresh_id        = v_refresh_id,
36457            last_update_date  = v_current_date,
36458            last_updated_by   = v_current_user,
36459            creation_date     = v_current_date,
36460            created_by        = v_current_user
36461       WHERE  rowid           = lb_rowid(j);
36462 
36463       -- Set the message
36464 
36465      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36466                      (p_app_short_name    => 'MSC',
36467                       p_error_code        => 'MSC_PP_INVALID_VALUE',
36468                       p_message_text      => lv_message_text,
36469                       p_error_text        => lv_error_text,
36470                       p_token1            => 'COLUMN_NAME',
36471                       p_token_value1      => 'COMPONENT_NAME');
36472 
36473       IF lv_return <> 0 THEN
36474         RAISE ex_logging_err;
36475       END IF;
36476 
36477    -- Populate Inventory Item id for component name
36478 
36479      lv_return :=
36480      MSC_ST_UTIL.DERIVE_ITEM_ID(p_table_name => 'MSC_ST_BOM_COMPONENTS',
36481                              p_item_col_name => 'COMPONENT_NAME',
36482                              p_item_col_id   => 'INVENTORY_ITEM_ID',
36483                              p_instance_code =>  v_instance_code,
36484                              p_instance_id   =>  v_instance_id,
36485                              p_severity      =>  G_SEV_ERROR,
36486                              p_message_text  =>  lv_message_text,
36487                              p_batch_id      =>  lv_batch_id,
36488                              p_debug         => v_debug,
36489                              p_error_text    =>  lv_error_text);
36490 
36491       if(lv_return <> 0) then
36492           RAISE ex_logging_err;
36493       end if;
36494 
36495     -- Set the error message
36496      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36497                      (p_app_short_name    => 'MSC',
36498                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
36499                       p_message_text      => lv_message_text,
36500                       p_error_text        => lv_error_text,
36501                       p_token1            => 'COLUMN_NAME',
36502                       p_token_value1      => 'DELETED_FLAG',
36503                       p_token2            => 'DEFAULT_VALUE',
36504                       p_token_value2      =>  SYS_NO  );
36505 
36506       IF lv_return <> 0 THEN
36507         RAISE ex_logging_err;
36508       END IF;
36509 
36510      -- Check whether delete flag value is in (1,2),if not default to SYS_NO
36511 
36512      lv_where_str := ' AND NVL(deleted_flag,'||NULL_VALUE||')NOT IN (1,2)';
36513 
36514      lv_return := MSC_ST_UTIL.LOG_ERROR
36515                     (p_table_name        => 'MSC_ST_BOM_COMPONENTS',
36516                      p_instance_code     => v_instance_code,
36517                      p_row               => lv_column_names,
36518                      p_severity          => G_SEV_WARNING,
36519                      p_message_text      => lv_message_text,
36520                      p_error_text        => lv_error_text,
36521                      p_batch_id          => lv_batch_id,
36522                      p_where_str         => lv_where_str,
36523                      p_col_name          => 'DELETED_FLAG',
36524                      p_debug             => v_debug,
36525                      p_default_value     => SYS_NO);
36526 
36527 
36528       IF lv_return <> 0 THEN
36529          RAISE ex_logging_err;
36530       END IF;
36531 
36532       -- Set the message
36533 
36534      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36535                      (p_app_short_name    => 'MSC',
36536                       p_error_code        => 'MSC_PP_INVALID_VALUE',
36537                       p_message_text      => lv_message_text,
36538                       p_error_text        => lv_error_text,
36539                       p_token1            => 'COLUMN_NAME',
36540                       p_token_value1      => 'ORGANIZATION_CODE');
36541 
36542       IF lv_return <> 0 THEN
36543         RAISE ex_logging_err;
36544       END IF;
36545 
36546     -- Populate  organization id
36547 
36548     lv_return :=
36549     MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name         => 'MSC_ST_BOM_COMPONENTS',
36550                                       p_org_partner_name   =>'ORGANIZATION_CODE',
36551                                       p_org_partner_id     =>'ORGANIZATION_ID',
36552                                       p_instance_code      => v_instance_code,
36553                                       p_partner_type       => G_ORGANIZATION,
36554                                       p_severity           => G_SEV_ERROR,
36555                                       p_message_text       => lv_message_text,
36556                                       p_batch_id           =>lv_batch_id,
36557                                       p_debug              => v_debug,
36558                                       p_error_text         =>lv_error_text);
36559 
36560    if(lv_return <> 0) then
36561       RAISE ex_logging_err;
36562    end if;
36563 
36564      -- Now we will check whether BOM Name is NULL ,
36565      -- if it is NULL we will populate the
36566      -- ASSEMBLY NAME in BOM NAME column for all such records
36567 
36568     v_sql_stmt := 17;
36569 
36570     lv_sql_stmt :=
36571 
36572     'UPDATE msc_st_bom_components  '
36573     ||' SET    bom_name            = assembly_name'
36574     ||' WHERE  sr_instance_code    = :v_instance_code'
36575     ||' AND    process_flag        ='||G_IN_PROCESS
36576     ||' AND    NVL(bom_name,'||''''||NULL_CHAR||''''||') '
36577     ||'         = '||''''||NULL_CHAR||''''
36578     ||' AND    NVL(assembly_name,'||''''||NULL_CHAR||''''||') '
36579     ||'         <> '||''''||NULL_CHAR||''''
36580     ||' AND    batch_id            = :lv_batch_id';
36581 
36582 
36583 
36584             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36585 
36586     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
36587 
36588     -- Now  derive assembly  name for those records where assembly name is NULL
36589     -- If not able to get this error out those records
36590     -- In child table of BOM we will accept either BOM name/Assembly name or both
36591 
36592     v_sql_stmt := 18;
36593 
36594     lv_sql_stmt:=
36595     'UPDATE     msc_st_bom_components mbc'
36596     ||' SET        assembly_name  = (SELECT char5'
36597     ||'                     FROM msc_local_id_setup mlis'
36598     ||'                     WHERE  mlis.char1 = mbc.sr_instance_code'
36599     ||'                     AND    mlis.char3 = mbc.organization_code'
36600     ||'                     AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
36601     ||'                     =    NVL(mbc.company_name,'||''''||NULL_CHAR||''''||') '
36602     ||'                     AND    mlis.char4 = mbc.bom_name'
36603     ||'                     AND    NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
36604     ||'                     = NVL(mbc.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
36605     ||'                     AND    mlis.entity_name =''BILL_SEQUENCE_ID'' '
36606     ||'                     AND    mlis.instance_id = '||v_instance_id||' )'
36607     ||' WHERE      sr_instance_code = :v_instance_code'
36608     ||' AND        NVL(assembly_name,'||''''||NULL_CHAR||''''||') '
36609     ||'                = '||''''||NULL_CHAR||''''
36610     ||' AND        process_flag     ='|| G_IN_PROCESS
36611     ||' AND        batch_id         = :lv_batch_id';
36612 
36613 
36614 
36615             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36616 
36617     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
36618 
36619        -- Set the message
36620 
36621      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36622                      (p_app_short_name    => 'MSC',
36623                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
36624                       p_message_text      => lv_message_text,
36625                       p_error_text        => lv_error_text,
36626                       p_token1            => 'COLUMN_NAMES',
36627                       p_token_value1      => 'BOM_NAME,INSTANCE_CODE,'
36628                                              ||' COMPANY_NAME,'
36629                                              ||'ALETERNATE_BOM_DESIGNATOR',
36630                       p_token2            => 'MASTER_TABLE',
36631                       p_token_value2      => 'MSC_ST_BOMS',
36632                       p_token3            => 'CHILD_TABLE',
36633                       p_token_value3      => 'MSC_ST_BOM_COMPONENTS');
36634 
36635       IF lv_return <> 0 THEN
36636         RAISE ex_logging_err;
36637       END IF;
36638 
36639    -- Error out records if assembly name is NULL
36640 
36641    v_sql_stmt := 19;
36642 
36643     lv_sql_stmt :=
36644     'UPDATE       MSC_ST_BOM_COMPONENTS'
36645     ||' SET       process_flag   = '||G_ERROR_FLG||','
36646     ||'           error_text     = '||''''||lv_message_text||''''
36647     ||' WHERE     NVL(assembly_name,'||''''||NULL_CHAR||''''||') '
36648     ||'         = '||''''||NULL_CHAR||''''
36649     ||' AND       process_flag   = '||G_IN_PROCESS
36650     ||' AND       sr_instance_code = :v_instance_code'
36651     ||' AND       batch_id         = :lv_batch_id';
36652 
36653 
36654             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36655 
36656     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
36657 
36658      -- Set the message
36659 
36660      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36661                      (p_app_short_name    => 'MSC',
36662                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
36663                       p_message_text      => lv_message_text,
36664                       p_error_text        => lv_error_text,
36665                       p_token1            => 'COLUMN_NAMES',
36666                       p_token_value1      => 'BOM_NAME,INSTANCE_CODE,'
36667                                              ||' COMPANY_NAME,'
36668                                              ||'ALETERNATE_BOM_DESIGNATOR',
36669                       p_token2            => 'MASTER_TABLE',
36670                       p_token_value2      => 'MSC_ST_BOMS',
36671                       p_token3            => 'CHILD_TABLE',
36672                       p_token_value3      => 'MSC_ST_BOM_COMPONENTS');
36673 
36674       IF lv_return <> 0 THEN
36675         RAISE ex_logging_err;
36676       END IF;
36677 
36678 
36679     -- Derive the Bill Sequence Id for the MSC_ST_BOM_COMPONENTS
36680     lv_return :=
36681     MSC_ST_UTIL.DERIVE_BILL_SEQUENCE_ID
36682                   (p_table_name    => 'MSC_ST_BOM_COMPONENTS',
36683                    p_bom_col_name   => 'BOM_NAME',
36684                    p_bom_col_id     => 'BILL_SEQUENCE_ID',
36685                    p_instance_code  =>  v_instance_code,
36686                    p_severity       =>  G_SEV_ERROR,
36687                    p_message_text   =>  lv_message_text,
36688                    p_batch_id       =>  lv_batch_id,
36689                    p_debug          => v_debug,
36690                    p_error_text     =>  lv_error_text);
36691 
36692     IF (lv_return <> 0 )then
36693         RAISE ex_logging_err;
36694     END IF;
36695 
36696     -- Set the error message
36697     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36698                      (p_app_short_name    => 'MSC',
36699                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
36700                       p_message_text      => lv_message_text,
36701                       p_error_text        => lv_error_text,
36702                       p_token1            => 'COLUMN_NAME',
36703                       p_token_value1      => 'OPERATION_SEQ_CODE',
36704                       p_token2           => 'DEFAULT_VALUE',
36705                       p_token_value2     =>  G_OPERATION_SEQ_CODE );
36706 
36707       IF lv_return <> 0 THEN
36708         RAISE ex_logging_err;
36709       END IF;
36710 
36711      -- Default OPERATION_SEQ_CODE as 1 if it is NULL
36712      lv_where_str :=
36713      '    AND NVL(operation_seq_code,'||''''||NULL_CHAR||''''||') '
36714      ||'  = '||''''||NULL_CHAR||'''' ;
36715 
36716      lv_return := MSC_ST_UTIL.LOG_ERROR
36717                     (p_table_name        => 'MSC_ST_BOM_COMPONENTS',
36718                      p_instance_code     => v_instance_code,
36719                      p_row               => lv_column_names,
36720                      p_severity          => G_SEV_WARNING,
36721                      p_message_text      => lv_message_text,
36722                      p_error_text        => lv_error_text,
36723                      p_batch_id          => lv_batch_id,
36724                      p_where_str         => lv_where_str,
36725                      p_col_name          => 'OPERATION_SEQ_CODE',
36726                      p_debug             => v_debug,
36727                      p_default_value     => G_OPERATION_SEQ_CODE);
36728 
36729 
36730       IF lv_return <> 0 THEN
36731          RAISE ex_logging_err;
36732       END IF;
36733 
36734     -- Set the error message
36735     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36736                      (p_app_short_name    => 'MSC',
36737                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
36738                       p_message_text      => lv_message_text,
36739                       p_error_text        => lv_error_text,
36740                       p_token1            => 'COLUMN_NAME',
36741                       p_token_value1      => 'EFFECTIVITY_DATE',
36742                       p_token2           => 'DEFAULT_VALUE',
36743                       p_token_value2     =>  'SYSDATE' );
36744 
36745       IF lv_return <> 0 THEN
36746         RAISE ex_logging_err;
36747       END IF;
36748 
36749 
36750       -- Check the EFFECTIVITY_DATE , if it is NULL,default SYSDATE
36751 
36752      lv_where_str :=
36753      ' AND NVL(effectivity_date,SYSDATE-36500 ) = SYSDATE-36500'  ;
36754 
36755      lv_return := MSC_ST_UTIL.LOG_ERROR
36756                     (p_table_name        => 'MSC_ST_BOM_COMPONENTS',
36757                      p_instance_code     => v_instance_code,
36758                      p_row               => lv_column_names,
36759                      p_severity          => G_SEV_WARNING,
36760                      p_message_text      => lv_message_text,
36761                      p_error_text        => lv_error_text,
36762                      p_batch_id          => lv_batch_id,
36763                      p_where_str         => lv_where_str,
36764                      p_col_name          => 'EFFECTIVITY_DATE',
36765                      p_debug             => v_debug,
36766                      p_default_value     =>  'SYSDATE');
36767 
36768       IF lv_return <> 0 THEN
36769            RAISE ex_logging_err;
36770       END IF;
36771 
36772 
36773      -- Derive the component_sequence_id
36774      v_sql_stmt := 20;
36775      lv_sql_stmt :=
36776      'UPDATE     msc_st_bom_components mbc'
36777      ||' SET     component_sequence_id=  (SELECT local_id '
36778      ||'         FROM msc_local_id_setup mlis'
36779      ||'         WHERE  mlis.char1 = mbc.sr_instance_code'
36780      ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
36781      ||'               =  NVL(mbc.company_name,'||''''||NULL_CHAR||''''||') '
36782      ||'         AND    mlis.char3 = mbc.organization_code'
36783      ||'         AND    mlis.char4 = mbc.bom_name'
36784      ||'         AND    mlis.char5 = mbc.component_name'
36785      ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
36786      ||'               =  NVL(mbc.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
36787      ||'         AND    mlis.char7 = mbc.operation_seq_code'
36788      ||'         AND    mlis.date1 = mbc.effectivity_date'
36789      ||'         AND    mlis.entity_name = ''COMPONENT_SEQUENCE_ID'')'
36790      ||'   WHERE      mbc.sr_instance_code = :v_instance_code'
36791      ||'   AND        mbc.process_flag     ='|| G_IN_PROCESS
36792      ||'   AND        mbc.batch_id         = :lv_batch_id';
36793 
36794 
36795             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36796 
36797     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
36798 
36799      -- Set the message
36800 
36801      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36802                      (p_app_short_name    => 'MSC',
36803                       p_error_code        => 'MSC_PP_DELETE_FAIL',
36804                       p_message_text      => lv_message_text,
36805                       p_error_text        => lv_error_text);
36806 
36807       IF lv_return <> 0 THEN
36808         RAISE ex_logging_err;
36809       END IF;
36810 
36811 
36812      -- At this point Error out those record with DELETE FLAG 1 and NULL
36813      -- Component sequence id
36814 
36815     v_sql_stmt := 21;
36816 
36817     lv_sql_stmt:=
36818     'UPDATE   msc_st_bom_components '
36819     ||'    SET   process_flag          ='|| G_ERROR_FLG||','
36820     ||'          error_text   = '||''''||lv_message_text||''''
36821     ||' WHERE    NVL(component_sequence_id,'||NULL_VALUE||')='|| NULL_VALUE
36822     ||' AND      deleted_flag           ='|| SYS_YES
36823     ||' AND      sr_instance_code      = :v_instance_code'
36824     ||' AND      process_flag          = '||G_IN_PROCESS
36825     ||' AND      batch_id              = :lv_batch_id';
36826 
36827 
36828             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36829 
36830    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
36831 
36832 
36833       -- Set the message
36834 
36835      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36836                      (p_app_short_name    => 'MSC',
36837                       p_error_code        => 'MSC_PP_DATE_OVERLAP',
36838                       p_message_text      => lv_message_text,
36839                       p_error_text        => lv_error_text);
36840 
36841       IF lv_return <> 0 THEN
36842         RAISE ex_logging_err;
36843       END IF;
36844 
36845 
36846     -- Now we are checking the overlapping of effectivity date of components
36847     v_sql_stmt := 22;
36848 
36849     lv_sql_stmt:=
36850     'UPDATE          msc_st_bom_components mbc'
36851     ||' SET          process_flag = '||G_ERROR_FLG||','
36852     ||'              error_text   = '||''''||lv_message_text||''''
36853     ||' WHERE EXISTS (SELECT 1 FROM'
36854     ||'           msc_local_id_setup mlis'
36855     ||'    WHERE  mlis.char1 = mbc.sr_instance_code'
36856     ||'    AND    NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
36857     ||'           =  NVL(mbc.company_name,'||''''||NULL_CHAR||''''||') '
36858     ||'    AND    mlis.char3 = mbc.organization_code'
36859     ||'    AND    mlis.char4 = mbc.bom_name'
36860     ||'    AND    mlis.char5 = mbc.component_name'
36861     ||'    AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
36862     ||'             =  NVL(mbc.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
36863     ||'    AND    mlis.char7 = mbc.operation_seq_code'
36864     ||'    AND   ((mlis.date1 >= mbc.effectivity_date AND'
36865     ||'            mlis.date1 <= NVL(mbc.disable_date,SYSDATE+36500))'
36866     ||'            OR'
36867     ||'           (NVL(mlis.date2,SYSDATE+36500) >= mbc.effectivity_date AND'
36868     ||'            NVL(mlis.date2,SYSDATE+36500) <= NVL(mbc.disable_date,SYSDATE+36500))'
36869     ||'            OR'
36870     ||'           (mlis.date1 <= mbc.effectivity_date AND'
36871     ||'            NVL(mlis.date2,SYSDATE+36500) >= NVL(mbc.disable_date,SYSDATE+36500))'
36872     ||'            OR'
36873     ||'           (mlis.date1 >= mbc.effectivity_date AND'
36874     ||'            NVL(mlis.date2,SYSDATE+36500) <= NVL(mbc.disable_date,SYSDATE+36500)))'
36875     ||'            AND mlis.entity_name = ''COMPONENT_SEQUENCE_ID'')'
36876     ||'  AND    NVL(mbc.component_sequence_id,'||NULL_VALUE||')='|| NULL_VALUE
36877     ||'  AND     mbc.sr_instance_code   =   :v_instance_code'
36878     ||'  AND     mbc.deleted_flag       =   '||SYS_NO
36879     ||'  AND     mbc.batch_id           =   :lv_batch_id'
36880     ||'  AND     mbc.process_flag       =   '||G_IN_PROCESS;
36881 
36882 
36883             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
36884 
36885     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
36886 
36887 
36888       -- Set the message
36889 
36890      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36891                      (p_app_short_name    => 'MSC',
36892                       p_error_code        => 'MSC_PP_INVALID_VALUE',
36893                       p_message_text      => lv_message_text,
36894                       p_error_text        => lv_error_text,
36895                       p_token1            => 'COLUMN_NAMES',
36896                       p_token_value1      => 'ASSEMBLY_NAME',
36897                       p_token2            => 'MASTER_TABLE',
36898                       p_token_value2      => 'MSC_ST_BOMS'
36899                                                              );
36900 
36901       IF lv_return <> 0 THEN
36902         RAISE ex_logging_err;
36903       END IF;
36904 
36905 
36906     -- Populate the using_assembly_id from assembly name
36907 
36908      lv_return :=
36909      MSC_ST_UTIL.DERIVE_ITEM_ID(p_table_name =>'MSC_ST_BOM_COMPONENTS',
36910                              p_item_col_name => 'ASSEMBLY_NAME',
36911                              p_item_col_id   =>'USING_ASSEMBLY_ID',
36912                              p_instance_code => v_instance_code,
36913                              p_instance_id   => v_instance_id,
36914                              p_severity      => G_SEV_ERROR,
36915                              p_message_text  => lv_message_text,
36916                              p_batch_id      =>lv_batch_id,
36917                              p_debug         => v_debug,
36918                              p_error_text    =>lv_error_text);
36919 
36920     if(lv_return <> 0) then
36921               RAISE ex_logging_err;
36922     end if;
36923 
36924 
36925     -- Set the error message
36926     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36927                      (p_app_short_name    => 'MSC',
36928                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
36929                       p_message_text      => lv_message_text,
36930                       p_error_text        => lv_error_text,
36931                       p_token1            => 'COLUMN_NAME',
36932                       p_token_value1      => 'USAGE_QUANTITY',
36933                       p_token2           => 'DEFAULT_VALUE',
36934                       p_token_value2     =>  G_USAGE_QUANTITY  );
36935 
36936       IF lv_return <> 0 THEN
36937         RAISE ex_logging_err;
36938       END IF;
36939 
36940     -- default it  to 1 if usage quantity is NULL
36941 
36942      lv_where_str :=
36943      ' AND NVL(usage_quantity,'||NULL_VALUE||')='|| NULL_VALUE ;
36944 
36945      lv_return := MSC_ST_UTIL.LOG_ERROR
36946                     (p_table_name        => 'MSC_ST_BOM_COMPONENTS',
36947                      p_instance_code     => v_instance_code,
36948                      p_row               => lv_column_names,
36949                      p_severity          => G_SEV_WARNING,
36950                      p_message_text      => lv_message_text,
36951                      p_error_text        => lv_error_text,
36952                      p_batch_id          => lv_batch_id,
36953                      p_where_str         => lv_where_str,
36954                      p_col_name          => 'USAGE_QUANTITY',
36955                      p_debug             => v_debug,
36956                      p_default_value     => G_USAGE_QUANTITY);
36957 
36958       IF lv_return <> 0 THEN
36959         RAISE ex_logging_err;
36960       END IF;
36961 
36962       -- Set the message
36963 
36964      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
36965                      (p_app_short_name    => 'MSC',
36966                       p_error_code        => 'MSC_PP_INVALID_VALUE',
36967                       p_message_text      => lv_message_text,
36968                       p_error_text        => lv_error_text,
36969                       p_token1            => 'COLUMN_NAME',
36970                       p_token_value1      => 'UOM_CODE');
36971 
36972       IF lv_return <> 0 THEN
36973         RAISE ex_logging_err;
36974       END IF;
36975 
36976 
36977 
36978    -- If UOM is  provided  validate the UOM_CODE
36979       v_sql_stmt := 23;
36980 
36981      lv_sql_stmt :=
36982      '   UPDATE     msc_st_bom_components mbc'
36983      ||' SET        process_flag = '||G_ERROR_FLG||','
36984      ||'            error_text   = '||''''||lv_message_text||''''
36985      ||' WHERE      NOT EXISTS (SELECT 1'
36986      ||'            FROM msc_units_of_measure muom'
36987      ||'            WHERE muom.uom_code       = mbc.uom_code'
36988      ||'            UNION'
36989      ||'            SELECT 1 FROM msc_st_units_of_measure msuom'
36990      ||'            WHERE msuom.uom_code       = mbc.uom_code'
36991      ||'            AND   msuom.sr_instance_id     = :v_instance_id'
36992      ||'            AND   msuom.process_flag       = '||G_VALID||' )'
36993      ||' AND NVL(mbc.uom_code,'||''''||NULL_CHAR||''''||') '
36994      ||'        <> '||''''||NULL_CHAR||''''
36995      ||' AND mbc.sr_instance_code   = :v_instance_code'
36996      ||' AND mbc.batch_id           = :lv_batch_id'
36997      ||' AND mbc.process_flag       = '||G_IN_PROCESS ;
36998 
36999             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37000 
37001       EXECUTE IMMEDIATE lv_sql_stmt
37002               USING     v_instance_id,
37003                         v_instance_code,
37004                         lv_batch_id;
37005 
37006     -- Set the error message
37007     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37008                      (p_app_short_name    => 'MSC',
37009                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37010                       p_message_text      => lv_message_text,
37011                       p_error_text        => lv_error_text,
37012                       p_token1            => 'COLUMN_NAME',
37013                       p_token_value1      => 'COMPONENT_YEILD_FACTOR',
37014                       p_token2           => 'DEFAULT_VALUE',
37015                       p_token_value2     =>  G_COMPONENT_YEILD_FACTOR );
37016 
37017       IF lv_return <> 0 THEN
37018         RAISE ex_logging_err;
37019       END IF;
37020 
37021      -- Default the component_yield_factor to 1 if NULL
37022 
37023      lv_where_str :=
37024      ' AND NVL(component_yield_factor,'||NULL_VALUE||')= '||NULL_VALUE ;
37025 
37026      lv_return := MSC_ST_UTIL.LOG_ERROR
37027                     (p_table_name        => 'MSC_ST_BOM_COMPONENTS',
37028                      p_instance_code     => v_instance_code,
37029                      p_row               => lv_column_names,
37030                      p_severity          => G_SEV_WARNING,
37031                      p_message_text      => lv_message_text,
37032                      p_error_text        => lv_error_text,
37033                      p_batch_id          => lv_batch_id,
37034                      p_where_str         => lv_where_str,
37035                      p_col_name          => 'COMPONENT_YIELD_FACTOR',
37036                      p_debug             => v_debug,
37037                      p_default_value     => G_COMPONENT_YEILD_FACTOR );
37038 
37039     if(lv_return <> 0) then
37040          RAISE ex_logging_err;
37041     end if;
37042 
37043      -- Set the message
37044 
37045      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37046                      (p_app_short_name    => 'MSC',
37047                       p_error_code        => 'MSC_PP_INVALID_VALUE',
37048                       p_message_text      => lv_message_text,
37049                       p_error_text        => lv_error_text,
37050                       p_token1            => 'COLUMN_NAME',
37051                       p_token_value1      => 'DRIVING_ITEM_NAME');
37052 
37053       IF lv_return <> 0 THEN
37054         RAISE ex_logging_err;
37055       END IF;
37056 
37057      --Derive  driving item id
37058 
37059      lv_return :=
37060      MSC_ST_UTIL.DERIVE_ITEM_ID(p_table_name => 'MSC_ST_BOM_COMPONENTS',
37061                              p_item_col_name => 'DRIVING_ITEM_NAME',
37062                              p_item_col_id   => 'DRIVING_ITEM_ID',
37063                              p_instance_code => v_instance_code,
37064                              p_instance_id   => v_instance_id,
37065                              p_severity      => G_SEV3_ERROR,
37066                              p_message_text  => lv_message_text,
37067                              p_batch_id      => lv_batch_id,
37068                              p_debug         => v_debug,
37069                              p_error_text    => lv_error_text);
37070 
37071     if(lv_return <> 0) then
37072          RAISE ex_logging_err;
37073     end if;
37074 
37075     -- Set the error message
37076     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37077                      (p_app_short_name    => 'MSC',
37078                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37079                       p_message_text      => lv_message_text,
37080                       p_error_text        => lv_error_text,
37081                       p_token1            => 'COLUMN_NAME',
37082                       p_token_value1      => 'USE_UP_CODE',
37083                       p_token2           => 'DEFAULT_VALUE',
37084                       p_token_value2     =>  G_USE_UP_CODE );
37085 
37086       IF lv_return <> 0 THEN
37087         RAISE ex_logging_err;
37088       END IF;
37089 
37090      -- Default the use_up_code as NULL if not in 1,2
37091 
37092      lv_where_str :=  ' AND use_up_code NOT IN (1,2)';
37093 
37094      lv_return := MSC_ST_UTIL.LOG_ERROR
37095                     (p_table_name        => 'MSC_ST_BOM_COMPONENTS',
37096                      p_instance_code     => v_instance_code,
37097                      p_row               => lv_column_names,
37098                      p_severity          => G_SEV_WARNING,
37099                      p_message_text      => lv_message_text,
37100                      p_error_text        => lv_error_text,
37101                      p_batch_id          => lv_batch_id,
37102                      p_where_str         => lv_where_str,
37103                      p_col_name          => 'USE_UP_CODE',
37104                      p_debug             => v_debug,
37105 	             p_default_value     => G_USE_UP_CODE);
37106 
37107     if(lv_return <> 0) then
37108           RAISE ex_logging_err;
37109     end if;
37110 
37111     -- Set the error message
37112     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37113                      (p_app_short_name    => 'MSC',
37114                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37115                       p_message_text      => lv_message_text,
37116                       p_error_text        => lv_error_text,
37117                       p_token1            => 'COLUMN_NAME',
37118                       p_token_value1      => 'OPTIONAL_COMPONENT',
37119                       p_token2           => 'DEFAULT_VALUE',
37120                       p_token_value2     =>  G_OPTIONAL_COMPONENT );
37121 
37122       IF lv_return <> 0 THEN
37123         RAISE ex_logging_err;
37124       END IF;
37125 
37126    -- Default the optional_component as NULL if not in 1,2
37127 
37128      lv_where_str := ' AND optional_component NOT IN (1,2)';
37129 
37130      lv_return := MSC_ST_UTIL.LOG_ERROR
37131                      (p_table_name       => 'MSC_ST_BOM_COMPONENTS',
37132                      p_instance_code     => v_instance_code,
37133                      p_row               => lv_column_names,
37134                      p_severity          => G_SEV_WARNING,
37135                      p_message_text      => lv_message_text,
37136                      p_error_text        => lv_error_text,
37137                      p_batch_id          => lv_batch_id,
37138                      p_where_str         => lv_where_str,
37139                      p_col_name          => 'OPTIONAL_COMPONENT',
37140                      p_debug             => v_debug,
37141         	    p_default_value      => G_OPTIONAL_COMPONENT );
37142 
37143     if(lv_return <> 0) then
37144          RAISE ex_logging_err;
37145     end if;
37146 
37147     -- Set the error message
37148     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37149                      (p_app_short_name    => 'MSC',
37150                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37151                       p_message_text      => lv_message_text,
37152                       p_error_text        => lv_error_text,
37153                       p_token1            => 'COLUMN_NAME',
37154                       p_token_value1      => 'WIP_SUPPLY_TYPE',
37155                       p_token2           => 'DEFAULT_VALUE',
37156                       p_token_value2     =>  G_WIP_SUPPLY_TYPE  );
37157 
37158       IF lv_return <> 0 THEN
37159         RAISE ex_logging_err;
37160       END IF;
37161 
37162     -- Default the wip_supply_type as 1(PUSH) if not in (1,2,3,4,5,6,7)
37163 
37164      lv_where_str :=
37165      ' AND NVL(wip_supply_type,'||NULL_VALUE||') NOT IN (1,2,3,4,5,6,7)';
37166 
37167      lv_return :=
37168      MSC_ST_UTIL.LOG_ERROR (p_table_name => 'MSC_ST_BOM_COMPONENTS',
37169                      p_instance_code     => v_instance_code,
37170                      p_row               => lv_column_names,
37171                      p_severity          => G_SEV_WARNING,
37172                      p_message_text      => lv_message_text,
37173                      p_error_text        => lv_error_text,
37174                      p_batch_id          => lv_batch_id,
37175                      p_where_str         => lv_where_str,
37176                      p_col_name          => 'WIP_SUPPLY_TYPE',
37177                      p_debug             => v_debug,
37178                      p_default_value     => G_WIP_SUPPLY_TYPE );
37179 
37180     if(lv_return <> 0) then
37181         RAISE ex_logging_err;
37182     end if;
37183 
37184     -- Set the error message
37185     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37186                      (p_app_short_name    => 'MSC',
37187                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37188                       p_message_text      => lv_message_text,
37189                       p_error_text        => lv_error_text,
37190                       p_token1            => 'COLUMN_NAME',
37191                       p_token_value1      => 'COMPONENT_TYPE',
37192                       p_token2           => 'DEFAULT_VALUE',
37193                       p_token_value2     =>  G_COMPONENT_TYPE );
37194 
37195       IF lv_return <> 0 THEN
37196         RAISE ex_logging_err;
37197       END IF;
37198 
37199 -- Comented out to match ERP behaviour
37200 /*     -- Default the component_type 1 if not in (-1,1,2)
37201 
37202 
37203      lv_where_str :=
37204      ' AND NVL(component_type,'||NULL_VALUE||') NOT IN (-1,1,2)';
37205 
37206      lv_return := MSC_ST_UTIL.LOG_ERROR
37207                     (p_table_name        => 'MSC_ST_BOM_COMPONENTS',
37208                      p_instance_code     => v_instance_code,
37209                      p_row               => lv_column_names,
37210                      p_severity          => G_SEV_WARNING,
37211                      p_message_text      => lv_message_text,
37212                      p_error_text        => lv_error_text,
37213                      p_batch_id          => lv_batch_id,
37214                      p_where_str         => lv_where_str,
37215                      p_col_name          => 'COMPONENT_TYPE',
37216                      p_debug             => v_debug,
37217    		     p_default_value     => G_COMPONENT_TYPE );
37218 
37219     if(lv_return <> 0) then
37220          RAISE ex_logging_err;
37221     end if;
37222 */
37223 
37224     -- Set the error message
37225     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37226                      (p_app_short_name    => 'MSC',
37227                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37228                       p_message_text      => lv_message_text,
37229                       p_error_text        => lv_error_text,
37230                       p_token1            => 'COLUMN_NAME',
37231                       p_token_value1      => 'ATP_FLAG',
37232                       p_token2           => 'DEFAULT_VALUE',
37233                       p_token_value2     =>  G_ATP_FLAG_BOM  );
37234 
37235       IF lv_return <> 0 THEN
37236         RAISE ex_logging_err;
37237       END IF;
37238 
37239      -- Default the atp_flag 2 if not in (1,2)
37240 
37241      lv_where_str :=
37242      ' AND NVL(atp_flag,'||NULL_VALUE||')NOT IN (1,2)';
37243 
37244      lv_return := MSC_ST_UTIL.LOG_ERROR
37245                     (p_table_name        => 'MSC_ST_BOM_COMPONENTS',
37246                      p_instance_code     => v_instance_code,
37247                      p_row               => lv_column_names,
37248                      p_severity          => G_SEV_WARNING,
37249                      p_message_text      => lv_message_text,
37250                      p_error_text        => lv_error_text,
37251                      p_batch_id          => lv_batch_id,
37252                      p_where_str         => lv_where_str,
37253                      p_col_name          => 'ATP_FLAG',
37254                      p_debug             => v_debug,
37255       	             p_default_value     => G_ATP_FLAG_BOM );
37256 
37257   if(lv_return <> 0) then
37258          RAISE ex_logging_err;
37259     end if;
37260 
37261      --Call to customised validation.
37262 
37263       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
37264         (ERRBUF         => lv_error_text,
37265          RETCODE        => lv_return,
37266          pBatchID       => lv_batch_id,
37267          pInstanceCode  => v_instance_code,
37268          pEntityName    => 'MSC_ST_BOM_COMPONENTS',
37269          pInstanceID    => v_instance_id);
37270 
37271       IF NVL(lv_return,0) <> 0 THEN
37272         RAISE ex_logging_err;
37273       END IF;
37274 
37275 v_sql_stmt := 23.1;
37276 
37277     lv_sql_stmt :=
37278     'UPDATE       MSC_ST_BOM_COMPONENTS'
37279     ||' SET       usage_quantity   =  ( usage_quantity / decode(component_yield_factor,0,1,abs(component_yield_factor)) )* nvl(planning_factor,100)/100'
37280     ||' WHERE     process_flag   = '||G_IN_PROCESS
37281     ||' AND       sr_instance_code = :v_instance_code'
37282     ||' AND       batch_id         = :lv_batch_id';
37283 
37284 
37285             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37286 
37287     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
37288 
37289 
37290     -- Now we will generate the  component sequence id
37291 
37292      v_sql_stmt := 24;
37293 
37294      OPEN c5(lv_batch_id);
37295      FETCH c5 BULK COLLECT INTO lb_rowid ;
37296 
37297      IF c5%ROWCOUNT > 0  THEN
37298 
37299     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
37300 
37301         UPDATE msc_st_bom_components
37302         SET    component_sequence_id   = msc_st_component_sequence_id_s.NEXTVAL
37303         WHERE  rowid                   = lb_rowid(j);
37304 
37305     v_sql_stmt := 23;
37306 
37307    FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
37308 
37309    -- Insert into the LID table
37310 
37311      INSERT INTO  msc_local_id_setup
37312      (local_id,
37313      st_transaction_id,
37314      instance_id,
37315      entity_name,
37316      data_source_type,
37317      char1,
37318      char2,
37319      char3,
37320      char4,
37321      char5,
37322      char6,
37323      char7,
37324      date1,
37325      date2,
37326      SOURCE_ORGANIZATION_ID ,
37327      SOURCE_BILL_SEQUENCE_ID ,
37328      SOURCE_COMPONENT_SEQ_ID ,
37329      SOURCE_USING_ASSEMBLY_ID ,
37330      SOURCE_INVENTORY_ITEM_ID ,
37331      SOURCE_DRIVING_ITEM_ID ,
37332      last_update_date,
37333      last_updated_by,
37334      creation_date,
37335      created_by    )
37336      SELECT
37337       component_sequence_id,
37338       st_transaction_id,
37339       v_instance_id,
37340       'COMPONENT_SEQUENCE_ID',
37341       data_source_type,
37342       sr_instance_code,
37343       company_name,
37344       organization_code,
37345       bom_name,
37346       component_name,
37347       alternate_bom_designator,
37348       operation_seq_code,
37349       effectivity_date,
37350       disable_date,
37351       SOURCE_ORGANIZATION_ID ,
37352       SOURCE_BILL_SEQUENCE_ID ,
37353       SOURCE_COMPONENT_SEQUENCE_ID ,
37354       SOURCE_USING_ASSEMBLY_ID ,
37355       SOURCE_INVENTORY_ITEM_ID ,
37356       SOURCE_DRIVING_ITEM_ID ,
37357       v_current_date,
37358       v_current_user,
37359       v_current_date,
37360       v_current_user
37361       FROM msc_st_bom_components
37362       WHERE rowid = lb_rowid(j) ;
37363 
37364     END IF;
37365   CLOSE c5;
37366 
37367 
37368    -- Update the DISABLE_DATE to SYSDATE for the deleted component in LID table
37369     v_sql_stmt := 25;
37370 /*
37371     lv_sql_stmt :=
37372     'UPDATE      msc_local_id_setup mlis'
37373     ||' SET      date2   = SYSDATE'
37374     ||' WHERE    EXISTS ( SELECT 1 FROM  msc_st_bom_components mbc'
37375     ||'               WHERE  mlis.char1 = mbc.sr_instance_code'
37376     ||'               AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
37377     ||'               =  NVL(mbc.company_name,'||''''||NULL_CHAR||''''||') '
37378     ||'               AND    mlis.char3 = mbc.organization_code'
37379     ||'               AND    mlis.char4 = mbc.bom_name'
37380     ||'               AND    mlis.char5 = mbc.component_name'
37381     ||'               AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
37382     ||'               =  NVL(mbc.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
37383     ||'               AND    mlis.char7 = mbc.operation_seq_code'
37384     ||'               AND    trunc(mlis.date1) = trunc(mbc.effectivity_date)'
37385     ||'               AND    mbc.batch_id= :lv_batch_id'
37386     ||'               AND    mbc.deleted_flag = '||SYS_YES
37387     ||'               AND    mbc.process_flag ='|| G_IN_PROCESS||')'
37388     ||' AND    mlis.entity_name = ''COMPONENT_SEQUENCE_ID'' '
37389     ||' AND    mlis.instance_id = :v_instance_id' ;
37390 
37391 
37392             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37393 
37394     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_id;
37395 */
37396     OPEN c7(lv_batch_id);
37397     FETCH c7 BULK COLLECT INTO lb_rowid ;
37398     IF c7%ROWCOUNT > 0  THEN
37399 
37400     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
37401 
37402         UPDATE msc_local_id_setup
37403         SET    date2   = SYSDATE
37404         WHERE  rowid                   = lb_rowid(j);
37405 
37406     END IF;
37407     CLOSE c7;
37408 
37409     -- Set the process flag as Valid and poulate instance_id
37410     lv_return :=
37411     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name     => 'MSC_ST_BOM_COMPONENTS',
37412                                p_instance_id    => v_instance_id,
37413                                p_instance_code  => v_instance_code,
37414                                p_process_flag   => G_VALID,
37415                                p_error_text     => lv_error_text,
37416                                p_debug          => v_debug,
37417                                p_batch_id       => lv_batch_id);
37418 
37419       IF lv_return <> 0 THEN
37420         RAISE ex_logging_err;
37421       END IF;
37422 
37423     -- At the end calling  the LOG_ERROR for logging all errored out records
37424 
37425    lv_return :=
37426    msc_st_util.log_error(p_table_name    =>'MSC_ST_BOM_COMPONENTS',
37427                          p_instance_code =>v_instance_code,
37428                          p_row           =>lv_column_names,
37429                          p_severity      =>G_SEV_ERROR,
37430                          p_message_text  => NULL,
37431                          p_error_text    =>lv_error_text,
37432                          p_debug         => v_debug,
37433                          p_batch_id      => lv_batch_id  );
37434 
37435      IF(lv_return <> 0) THEN
37436              RAISE ex_logging_err;
37437      END IF;
37438     COMMIT;
37439    END LOOP;
37440 
37441 
37442    -- Now validtion for MSC_ST_COMPONENT_SUBSTITUTES
37443 
37444 
37445    --Duplicate records check for the records whose source is XML
37446 
37447     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37448                  (p_app_short_name    => 'MSC',
37449                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
37450                   p_message_text      => lv_message_text,
37451                   p_error_text        => lv_error_text);
37452 
37453     IF lv_return <> 0 THEN
37454       RAISE ex_logging_err;
37455     END IF;
37456 
37457 
37458   v_sql_stmt := 26;
37459   lv_sql_stmt :=
37460   'UPDATE msc_st_component_substitutes mcs1 '
37461   ||' SET process_flag ='|| G_ERROR_FLG||','
37462   ||'         error_text   = '||''''||lv_message_text||''''
37463   ||' WHERE message_id < ( SELECT max(message_id)'
37464   ||'            FROM msc_st_component_substitutes mcs2'
37465   ||'            WHERE mcs2.sr_instance_code       = mcs1.sr_instance_code'
37466   ||'            AND mcs2.organization_code        = mcs1.organization_code'
37467   ||'            AND NVL(mcs2.company_name,'||''''||NULL_CHAR||''''||') '
37468   ||'                  = NVL(mcs1.company_name, '||''''||NULL_CHAR||''''||') '
37469   ||'            AND NVL(mcs2.bom_name,'||''''||NULL_CHAR||''''||') '
37470   ||'                  = NVL(mcs1.bom_name, '||''''||NULL_CHAR||''''||') '
37471   ||'            AND mcs2.assembly_name = mcs1.assembly_name '
37472   ||'            AND NVL(mcs2.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
37473   ||'                  = NVL(mcs1.alternate_bom_designator, '||''''||NULL_CHAR||''''||') '
37474   ||'            AND mcs2.component_name           = mcs1.component_name'
37475   ||'            AND NVL(mcs2.effectivity_date,SYSDATE-36500 )'
37476   ||'                    = NVL(mcs1.effectivity_date,SYSDATE-36500 )'
37477   ||'            AND NVL(mcs2.operation_seq_code ,'||''''||NULL_CHAR||''''||') '
37478   ||'                  = NVL(mcs1.operation_seq_code , '||''''||NULL_CHAR||''''||') '
37479   ||'            AND mcs2.sub_item_name            = mcs1.sub_item_name'
37480   ||'            AND mcs2.process_flag             = mcs1.process_flag'
37481   ||'            AND NVL(mcs2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
37482   ||' AND mcs1.process_flag ='|| G_IN_PROCESS
37483   ||' AND mcs1.sr_instance_code = :v_instance_code '
37484   ||' AND NVL(mcs1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
37485 
37486 
37487           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37488 
37489   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
37490 
37491 
37492   --Duplicate records check for the records whose source is batch load
37493 
37494     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37495                  (p_app_short_name    => 'MSC',
37496                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
37497                   p_message_text      => lv_message_text,
37498                   p_error_text        => lv_error_text);
37499 
37500     IF lv_return <> 0 THEN
37501       RAISE ex_logging_err;
37502     END IF;
37503 
37504   v_sql_stmt := 27;
37505 
37506   lv_sql_stmt :=
37507   'UPDATE     msc_st_component_substitutes mcs1'
37508   ||' SET     process_flag = '||G_ERROR_FLG||','
37509   ||'         error_text   = '||''''||lv_message_text||''''
37510   ||' WHERE EXISTS ( SELECT 1 FROM msc_st_component_substitutes mcs2'
37511   ||'            WHERE mcs2.sr_instance_code       = mcs1.sr_instance_code'
37512   ||'            AND mcs2.organization_code        = mcs1.organization_code'
37513   ||'            AND NVL(mcs2.company_name,'||''''||NULL_CHAR||''''||') '
37514   ||'                  = NVL(mcs1.company_name, '||''''||NULL_CHAR||''''||') '
37515   ||'            AND NVL(mcs2.bom_name,'||''''||NULL_CHAR||''''||') '
37516   ||'                  = NVL(mcs1.bom_name, '||''''||NULL_CHAR||''''||') '
37517   ||'            AND mcs2.assembly_name = mcs1.assembly_name '
37518   ||'            AND NVL(mcs2.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
37519   ||'                  = NVL(mcs1.alternate_bom_designator, '||''''||NULL_CHAR||''''||') '
37520   ||'            AND mcs2.component_name           = mcs1.component_name'
37521   ||'            AND NVL(mcs2.effectivity_date,SYSDATE-36500 )'
37522   ||'                    = NVL(mcs1.effectivity_date,SYSDATE-36500 )'
37523   ||'            AND NVL(mcs2.operation_seq_code ,'||''''||NULL_CHAR||''''||') '
37524   ||'                  = NVL(mcs1.operation_seq_code , '||''''||NULL_CHAR||''''||') '
37525   ||'            AND mcs2.sub_item_name            = mcs1.sub_item_name'
37526   ||'            AND mcs2.process_flag             = mcs1.process_flag'
37527   ||'            AND NVL(mcs2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
37528   ||'            GROUP BY sr_instance_code,organization_code, bom_name,company_name,'
37529   ||'            assembly_name,alternate_bom_designator,component_name,'
37530   ||'            operation_seq_code,effectivity_date,sub_item_name'
37531   ||'            HAVING count(*) > 1)'
37532   ||' AND  mcs1.process_flag              ='|| G_IN_PROCESS
37533   ||' AND  mcs1.sr_instance_code          = :v_instance_code'
37534   ||' AND  NVL(mcs1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
37535 
37536 
37537          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37538 
37539   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
37540 
37541 
37542 
37543  lv_column_names :=
37544   'ORGANIZATION_CODE          ||''~''||'
37545   ||'BOM_NAME                 ||''~''||'
37546   ||'ASSEMBLY_NAME            ||''~''||'
37547   ||'COMPONENT_NAME           ||''~''||'
37548   ||'SUB_ITEM_NAME            ||''~''||'
37549   ||'USAGE_QUANTITY           ||''~''||'
37550   ||'PRIORITY                 ||''~''||'
37551   ||'SR_INSTANCE_CODE         ||''~''||'
37552   ||'ALTERNATE_BOM_DESIGNATOR ||''~''||'
37553   ||'OPERATION_SEQ_CODE       ||''~''||'
37554   ||'EFFECTIVITY_DATE         ||''~''||'
37555   ||'COMPANY_NAME             ||''~''||'
37556   ||'DELETED_FLAG ';
37557 
37558 
37559   -- Processing  the MSC_ST_COMPONENT_SUBSTITUTES table
37560 
37561    LOOP
37562       v_sql_stmt := 28;
37563       lv_cursor_stmt :=
37564       ' SELECT msc_st_batch_id_s.NEXTVAL '
37565       ||' FROM   dual';
37566 
37567       OPEN  c1 FOR lv_cursor_stmt;
37568       FETCH c1 INTO lv_batch_id;
37569       CLOSE c1;
37570 
37571       v_sql_stmt := 29;
37572       lv_sql_stmt :=
37573       ' UPDATE msc_st_component_substitutes '
37574       ||' SET    batch_id  = :lv_batch_id'
37575       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
37576       ||' AND    sr_instance_code               = :v_instance_code'
37577       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
37578       ||' AND    rownum                        <= '||v_batch_size;
37579 
37580 
37581          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37582 
37583       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
37584 
37585       EXIT WHEN SQL%NOTFOUND;
37586 
37587       OPEN c6(lv_batch_id);
37588       FETCH c6 BULK COLLECT INTO lb_rowid;
37589       CLOSE c6;
37590 
37591       v_sql_stmt := 29;
37592       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
37593       UPDATE msc_st_component_substitutes
37594       SET  st_transaction_id = msc_st_component_substitutes_s.NEXTVAL,
37595            refresh_id        = v_refresh_id,
37596            last_update_date  = v_current_date,
37597            last_updated_by   = v_current_user,
37598            creation_date     = v_current_date,
37599            created_by        = v_current_user
37600       WHERE  rowid           = lb_rowid(j);
37601 
37602 
37603      -- Set the error message
37604 
37605     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37606                      (p_app_short_name    => 'MSC',
37607                       p_error_code        => 'MSC_PP_NO_DELETION',
37608                       p_message_text      => lv_message_text,
37609                       p_error_text        => lv_error_text,
37610                       p_token1            => 'TABLE_NAME',
37611                       p_token_value1      => 'MSC_ST_COMPONENT_SUBSTITUTES');
37612 
37613       IF lv_return <> 0 THEN
37614         RAISE ex_logging_err;
37615       END IF;
37616 
37617       --Deletion is not allowed on this table.
37618       v_sql_stmt := 30;
37619       lv_sql_stmt :=
37620       ' UPDATE     msc_st_component_substitutes'
37621       ||' SET      process_flag     = '||G_ERROR_FLG||','
37622       ||'          error_text       = '||''''||lv_message_text||''''
37623       ||' WHERE    deleted_flag     = '||SYS_YES
37624       ||' AND      process_flag     = '||G_IN_PROCESS
37625       ||' AND      batch_id         =  :lv_batch_id'
37626       ||' AND      sr_instance_code =  :v_instance_code';
37627 
37628 
37629             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37630 
37631       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
37632 
37633     -- Set the error message
37634     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37635                      (p_app_short_name    => 'MSC',
37636                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37637                       p_message_text      => lv_message_text,
37638                       p_error_text        => lv_error_text,
37639                       p_token1            => 'COLUMN_NAME',
37640                       p_token_value1      => 'DELETED_FLAG',
37641                       p_token2            => 'DEFAULT_VALUE',
37642                       p_token_value2      =>  SYS_NO );
37643 
37644 
37645 
37646      -- Check whether delete flag value is in (SYS_YES,SYS_NO),if not default to SYS_NO
37647 
37648      lv_where_str :=
37649      ' AND NVL(deleted_flag,'||NULL_VALUE||')NOT IN (1,2)';
37650 
37651      lv_return := MSC_ST_UTIL.LOG_ERROR
37652                     (p_table_name        => 'MSC_ST_COMPONENT_SUBSTITUTES',
37653                      p_instance_code     => v_instance_code,
37654                      p_row               => lv_column_names,
37655                      p_severity          => G_SEV_WARNING,
37656                      p_message_text      => lv_message_text,
37657                      p_error_text        => lv_error_text,
37658                      p_batch_id          => lv_batch_id,
37659                      p_where_str         => lv_where_str,
37660                      p_col_name          => 'DELETED_FLAG',
37661                      p_debug             => v_debug,
37662                      p_default_value     =>  SYS_NO);
37663 
37664 
37665       IF lv_return <> 0 THEN
37666         RAISE ex_logging_err;
37667       END IF;
37668 
37669       -- Set the message
37670 
37671      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37672                      (p_app_short_name    => 'MSC',
37673                       p_error_code        => 'MSC_PP_INVALID_VALUE',
37674                       p_message_text      => lv_message_text,
37675                       p_error_text        => lv_error_text,
37676                       p_token1            => 'COLUMN_NAME',
37677                       p_token_value1      => 'SUB_ITEM_NAME');
37678 
37679       IF lv_return <> 0 THEN
37680         RAISE ex_logging_err;
37681       END IF;
37682 
37683      --Derive SUBSTITUTE ITEM ID
37684      lv_return :=
37685      MSC_ST_UTIL.DERIVE_ITEM_ID(p_table_name => 'MSC_ST_COMPONENT_SUBSTITUTES',
37686                              p_item_col_name => 'SUB_ITEM_NAME',
37687                              p_item_col_id   => 'SUBSTITUTE_ITEM_ID',
37688                              p_instance_code => v_instance_code,
37689                              p_instance_id   => v_instance_id,
37690                              p_severity      => G_SEV_ERROR,
37691                              p_message_text  => lv_message_text,
37692                              p_batch_id      => lv_batch_id,
37693                              p_debug         => v_debug,
37694                              p_error_text    => lv_error_text);
37695 
37696       IF lv_return <> 0 THEN
37697         RAISE ex_logging_err;
37698       END IF;
37699 
37700       -- Set the message
37701 
37702      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37703                      (p_app_short_name    => 'MSC',
37704                       p_error_code        => 'MSC_PP_INVALID_VALUE',
37705                       p_message_text      => lv_message_text,
37706                       p_error_text        => lv_error_text,
37707                       p_token1            => 'COLUMN_NAME',
37708                       p_token_value1      => 'ORGANIZATION_CODE');
37709 
37710       IF lv_return <> 0 THEN
37711         RAISE ex_logging_err;
37712       END IF;
37713 
37714     -- Populate  organization id ,
37715 
37716     lv_return :=
37717     MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name         => 'MSC_ST_COMPONENT_SUBSTITUTES',
37718                                       p_org_partner_name   => 'ORGANIZATION_CODE',
37719                                       p_org_partner_id     => 'ORGANIZATION_ID',
37720                                       p_instance_code      => v_instance_code,
37721                                       p_partner_type       => G_ORGANIZATION,
37722                                       p_severity           => G_SEV_ERROR,
37723                                       p_message_text       => lv_message_text,
37724                                       p_batch_id           => lv_batch_id,
37725                                       p_debug              => v_debug,
37726                                       p_error_text         => lv_error_text);
37727 
37728      IF lv_return <> 0 THEN
37729         RAISE ex_logging_err;
37730      END IF;
37731 
37732      -- Now we will check whether BOM Name is NULL , if it is NULL we will populate the
37733      -- ASSEMBLY NAME in BOM NAME column for all such records
37734 
37735     v_sql_stmt := 31;
37736 
37737     lv_sql_stmt :=
37738 
37739     'UPDATE msc_st_component_substitutes '
37740     ||' SET    bom_name            = assembly_name'
37741     ||' WHERE  sr_instance_code    = :v_instance_code'
37742     ||' AND    process_flag     ='||G_IN_PROCESS
37743     ||' AND    NVL(bom_name,'||''''||NULL_CHAR||''''||') '
37744     ||'         = '||''''||NULL_CHAR||''''
37745     ||' AND    NVL(assembly_name,'||''''||NULL_CHAR||''''||') '
37746     ||'         <> '||''''||NULL_CHAR||''''
37747     ||' AND    batch_id            = :lv_batch_id';
37748 
37749 
37750             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37751 
37752     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
37753 
37754 
37755     -- Now  derive assembly  name for those records where assembly name is NULL
37756     -- If not able to get this error out those records
37757     -- In child table of BOM we will accept either BOM name/Assembly name or both
37758 
37759     v_sql_stmt := 32;
37760 
37761     lv_sql_stmt:=
37762     'UPDATE     msc_st_component_substitutes mcs'
37763     ||' SET        assembly_name  = (SELECT char5'
37764     ||'                     FROM msc_local_id_setup mlis'
37765     ||'                     WHERE  mlis.char1 = mcs.sr_instance_code'
37766     ||'                     AND    mlis.char3 = mcs.organization_code'
37767     ||'                     AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
37768     ||'                      =    NVL(mcs.company_name,'||''''||NULL_CHAR||''''||') '
37769     ||'                     AND    mlis.char4 = mcs.bom_name'
37770     ||'                     AND    NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
37771     ||'                     = NVL(mcs.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
37772     ||'                     AND    mlis.entity_name =''BILL_SEQUENCE_ID'') '
37773     ||' WHERE      sr_instance_code = :v_instance_code'
37774     ||' AND        NVL(assembly_name,'||''''||NULL_CHAR||''''||') '
37775     ||'                = '||''''||NULL_CHAR||''''
37776     ||' AND        process_flag     ='|| G_IN_PROCESS
37777     ||' AND        batch_id         = :lv_batch_id';
37778 
37779 
37780             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37781 
37782     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
37783 
37784     -- Set the message
37785 
37786      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37787                      (p_app_short_name    => 'MSC',
37788                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
37789                       p_message_text      => lv_message_text,
37790                       p_error_text        => lv_error_text,
37791                       p_token1            => 'COLUMN_NAMES',
37792                       p_token_value1      => 'BOM_NAME,SR_INSTANCE_CODE,'
37793                                              ||' COMPANY_NAME,'
37794                                              ||' ALETERNATE_BOM_DESIGNATOR',
37795                       p_token2            => 'MASTER_TABLE',
37796                       p_token_value2      => 'MSC_ST_BOMS',
37797                       p_token3            => 'CHILD_TABLE',
37798                       p_token_value3      => 'MSC_ST_COMPONENT_SUBSTITUTES');
37799 
37800       IF lv_return <> 0 THEN
37801         RAISE ex_logging_err;
37802       END IF;
37803 
37804 
37805    -- Error out those records where assembly name is still NULL,
37806 
37807     v_sql_stmt := 33;
37808     lv_sql_stmt :=
37809     'UPDATE       msc_st_component_substitutes '
37810     ||' SET       process_flag   = '||G_ERROR_FLG||','
37811     ||'           error_text     = '||''''||lv_message_text||''''
37812     ||' WHERE     NVL(assembly_name,'||''''||NULL_CHAR||''''||') '
37813     ||'                = '||''''||NULL_CHAR||''''
37814     ||' AND       sr_instance_code = :v_instance_code'
37815     ||' AND       process_flag   = '||G_IN_PROCESS
37816     ||' AND       batch_id       = :lv_batch_id';
37817 
37818 
37819             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37820 
37821     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
37822 
37823 
37824     -- Derive bill sequence id
37825     lv_return :=
37826     MSC_ST_UTIL.DERIVE_BILL_SEQUENCE_ID
37827                      (p_table_name    =>  'MSC_ST_COMPONENT_SUBSTITUTES',
37828                       p_bom_col_name   => 'BOM_NAME',
37829                       p_bom_col_id     => 'BILL_SEQUENCE_ID',
37830                       p_instance_code  => v_instance_code,
37831                       p_severity       => G_SEV_ERROR,
37832                       p_message_text   => lv_message_text,
37833                       p_batch_id       => lv_batch_id,
37834                       p_debug          => v_debug,
37835                       p_error_text     => lv_error_text);
37836 
37837     IF (lv_return <> 0 )then
37838               RAISE ex_logging_err;
37839     END IF;
37840 
37841     -- Set the error message
37842     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37843                      (p_app_short_name    => 'MSC',
37844                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37845                       p_message_text      => lv_message_text,
37846                       p_error_text        => lv_error_text,
37847                       p_token1            => 'COLUMN_NAME',
37848                       p_token_value1      => 'EFFECTIVITY_DATE',
37849                       p_token2           => 'DEFAULT_VALUE',
37850                       p_token_value2     =>  'SYSDATE' );
37851 
37852       IF lv_return <> 0 THEN
37853         RAISE ex_logging_err;
37854       END IF;
37855 
37856 
37857       -- Check the EFFECTIVITY_DATE , if it is NULL,default SYSDATE
37858 
37859      lv_where_str :=
37860      ' AND NVL(effectivity_date,SYSDATE-36500 ) = SYSDATE-36500'  ;
37861 
37862      lv_return := MSC_ST_UTIL.LOG_ERROR
37863                     (p_table_name        => 'MSC_ST_COMPONENT_SUBSTITUTES',
37864                      p_instance_code     => v_instance_code,
37865                      p_row               => lv_column_names,
37866                      p_severity          => G_SEV_WARNING,
37867                      p_message_text      => lv_message_text,
37868                      p_error_text        => lv_error_text,
37869                      p_batch_id          => lv_batch_id,
37870                      p_where_str         => lv_where_str,
37871                      p_col_name          => 'EFFECTIVITY_DATE',
37872                      p_debug             => v_debug,
37873                      p_default_value     =>  'SYSDATE');
37874 
37875       IF lv_return <> 0 THEN
37876            RAISE ex_logging_err;
37877       END IF;
37878 
37879     -- Now derive the component sequence id for the Unique key combination
37880     v_sql_stmt := 34;
37881 
37882     lv_sql_stmt :=
37883     'UPDATE     msc_st_component_substitutes mcs'
37884     ||' SET     component_sequence_id=  (SELECT local_id '
37885     ||'         FROM msc_local_id_setup mlis'
37886     ||'         WHERE  mlis.char1 = mcs.sr_instance_code'
37887     ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
37888     ||'               =  NVL(mcs.company_name,'||''''||NULL_CHAR||''''||') '
37889     ||'         AND    mlis.char3 = mcs.organization_code'
37890     ||'         AND    mlis.char4 = mcs.bom_name'
37891     ||'         AND    mlis.char5 = mcs.component_name'
37892     ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
37893     ||'               =  NVL(mcs.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
37894     ||'         AND    mlis.char7 = mcs.operation_seq_code'
37895     ||'         AND    mlis.date1 = mcs.effectivity_date'
37896     ||'         AND    mlis.entity_name = ''COMPONENT_SEQUENCE_ID'' )'
37897     ||'   WHERE      sr_instance_code = :v_instance_code'
37898     ||'   AND        process_flag     ='|| G_IN_PROCESS
37899     ||'   AND        batch_id         = :lv_batch_id';
37900 
37901 
37902             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37903 
37904     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
37905 
37906     -- Set the error message
37907 
37908    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37909                      (p_app_short_name    => 'MSC',
37910                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
37911                       p_message_text      => lv_message_text,
37912                       p_error_text        => lv_error_text,
37913                       p_token1            => 'COLUMN_NAMES',
37914                       p_token_value1      => 'BOM_NAME,SR_INSTANCE_CODE,EFFECTIVITY_DATE'
37915                                              ||' COMPANY_NAME,COMPONENT_NAME'
37916                                              ||' ALETERNATE_BOM_DESIGNATOR,OPERATION_SEQ_CODE',
37917                       p_token2            => 'MASTER_TABLE',
37918                       p_token_value2      => 'MSC_ST_BOM_COMPONENTS',
37919                       p_token3            => 'CHILD_TABLE',
37920                       p_token_value3      => 'MSC_ST_COMPONENT_SUBSTITUTES');
37921 
37922 
37923      IF lv_return <> 0 THEN
37924         RAISE ex_logging_err;
37925      END IF;
37926 
37927    v_sql_stmt := 35;
37928     lv_sql_stmt:=
37929     'UPDATE   msc_st_component_substitutes '
37930     ||'    SET   process_flag    ='|| G_ERROR_FLG||','
37931     ||'          error_text      ='||''''||lv_message_text||''''
37932     ||' WHERE    NVL(component_sequence_id,'||NULL_VALUE||')='|| NULL_VALUE
37933     ||' AND      sr_instance_code      = :v_instance_code'
37934     ||' AND      process_flag          = '||G_IN_PROCESS
37935     ||' AND      batch_id              = :lv_batch_id';
37936 
37937 
37938             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
37939 
37940    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
37941 
37942     -- Set the error message
37943     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37944                      (p_app_short_name    => 'MSC',
37945                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37946                       p_message_text      => lv_message_text,
37947                       p_error_text        => lv_error_text,
37948                       p_token1            => 'COLUMN_NAME',
37949                       p_token_value1      => 'USAGE_QUANTITY',
37950                       p_token2           => 'DEFAULT_VALUE',
37951                       p_token_value2     =>  G_USAGE_QUANTITY  );
37952 
37953       IF lv_return <> 0 THEN
37954         RAISE ex_logging_err;
37955       END IF;
37956 
37957      -- Default 1 where usage quantity is NULL
37958 
37959      lv_where_str := ' AND NVL(usage_quantity,'||NULL_VALUE||')='|| NULL_VALUE ;
37960 
37961      lv_return := MSC_ST_UTIL.LOG_ERROR
37962                     (p_table_name        => 'MSC_ST_COMPONENT_SUBSTITUTES',
37963                      p_instance_code     => v_instance_code,
37964                      p_row               => lv_column_names,
37965                      p_severity          => G_SEV_WARNING,
37966                      p_message_text      => lv_message_text,
37967                      p_error_text        => lv_error_text,
37968                      p_batch_id          => lv_batch_id,
37969                      p_where_str         => lv_where_str,
37970                      p_col_name          => 'USAGE_QUANTITY',
37971                      p_debug             => v_debug,
37972                      p_default_value     => G_USAGE_QUANTITY );
37973 
37974 
37975       IF lv_return <> 0 THEN
37976         RAISE ex_logging_err;
37977       END IF;
37978 
37979 
37980     -- Set the error message
37981     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
37982                      (p_app_short_name    => 'MSC',
37983                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
37984                       p_message_text      => lv_message_text,
37985                       p_error_text        => lv_error_text,
37986                       p_token1            => 'COLUMN_NAME',
37987                       p_token_value1      => 'PRIORITY',
37988                       p_token2           => 'DEFAULT_VALUE',
37989                       p_token_value2     =>  G_PRIORITY  );
37990 
37991       IF lv_return <> 0 THEN
37992         RAISE ex_logging_err;
37993       END IF;
37994 
37995      -- Default PRIORITY as 1 if NULL
37996 
37997      lv_where_str := ' AND NVL(priority,'||NULL_VALUE||')='|| NULL_VALUE ;
37998 
37999      lv_return := MSC_ST_UTIL.LOG_ERROR
38000                     (p_table_name        => 'MSC_ST_COMPONENT_SUBSTITUTES',
38001                      p_instance_code     => v_instance_code,
38002                      p_row               => lv_column_names,
38003                      p_severity          => G_SEV_WARNING,
38004                      p_message_text      => lv_message_text,
38005                      p_error_text        => lv_error_text,
38006                      p_batch_id          => lv_batch_id,
38007                      p_where_str         => lv_where_str,
38008                      p_col_name          => 'PRIORITY',
38009                      p_debug             => v_debug,
38010                      p_default_value     => G_PRIORITY );
38011 
38012 
38013       IF lv_return <> 0 THEN
38014         RAISE ex_logging_err;
38015       END IF;
38016 
38017    --Call to customised validation.
38018 
38019       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
38020         (ERRBUF         => lv_error_text,
38021          RETCODE        => lv_return,
38022          pBatchID       => lv_batch_id,
38023          pInstanceCode  => v_instance_code,
38024          pEntityName    => 'MSC_ST_COMPONENT_SUBSTITUTES',
38025          pInstanceID    => v_instance_id);
38026 
38027       IF NVL(lv_return,0) <> 0 THEN
38028         RAISE ex_logging_err;
38029       END IF;
38030 
38031     -- Set the process flag as Valid and poulate instance_id
38032     lv_return :=
38033     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name     => 'MSC_ST_COMPONENT_SUBSTITUTES',
38034                                p_instance_id    => v_instance_id,
38035                                p_instance_code  => v_instance_code,
38036                                p_process_flag   => G_VALID,
38037                                p_error_text     => lv_error_text,
38038                                p_debug          => v_debug,
38039                                p_batch_id       => lv_batch_id);
38040 
38041       IF lv_return < 0 THEN
38042          RAISE ex_logging_err;
38043       END IF;
38044 
38045     -- At the end calling  the LOG_ERROR for logging all errored out records
38046 
38047    lv_return :=
38048    msc_st_util.log_error(p_table_name    => 'MSC_ST_COMPONENT_SUBSTITUTES',
38049                          p_instance_code => v_instance_code,
38050                          p_row           => lv_column_names,
38051                          p_severity      => G_SEV_ERROR,
38052                          p_error_text    => lv_error_text,
38053                          p_message_text  => NULL,
38054                          p_debug         => v_debug,
38055                          p_batch_id      => lv_batch_id  );
38056 
38057      IF(lv_return <> 0) THEN
38058          RAISE ex_logging_err;
38059      END IF;
38060     COMMIT;
38061   END LOOP;
38062 
38063    -- For OSFM Integration. Support for Co Products --
38064    -- Now validtion for MSC_ST_CO_PRODUCTS
38065 
38066    --Duplicate records check for the records whose source is XML
38067 
38068     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38069                  (p_app_short_name    => 'MSC',
38070                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
38071                   p_message_text      => lv_message_text,
38072                   p_error_text        => lv_error_text);
38073 
38074     IF lv_return <> 0 THEN
38075       RAISE ex_logging_err;
38076     END IF;
38077 
38078 
38079   v_sql_stmt := 36;
38080   lv_sql_stmt :=
38081   'UPDATE msc_st_co_products mcp1 '
38082   ||' SET process_flag ='|| G_ERROR_FLG||','
38083   ||'         error_text   = '||''''||lv_message_text||''''
38084   ||' WHERE message_id < ( SELECT max(message_id)'
38085   ||'            FROM msc_st_co_products mcp2'
38086   ||'            WHERE mcp2.co_product_name           = mcp1.co_product_name'
38087   ||'            AND mcp2.component_name           = mcp1.component_name'
38088   ||'            AND  mcp2.sr_instance_code       = mcp1.sr_instance_code'
38089   ||'            AND NVL(mcp2.company_name,'||''''||NULL_CHAR||''''||') '
38090   ||'                  = NVL(mcp1.company_name, '||''''||NULL_CHAR||''''||') '
38091   ||'            AND mcp2.process_flag             = mcp1.process_flag'
38092   ||'            AND NVL(mcp2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
38093   ||' AND mcp1.process_flag ='|| G_IN_PROCESS
38094   ||' AND mcp1.sr_instance_code = :v_instance_code '
38095   ||' AND NVL(mcp1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
38096 
38097 
38098           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38099 
38100   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
38101 
38102 
38103   --Duplicate records check for the records whose source is batch load
38104 
38105     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38106                  (p_app_short_name    => 'MSC',
38107                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
38108                   p_message_text      => lv_message_text,
38109                   p_error_text        => lv_error_text);
38110 
38111     IF lv_return <> 0 THEN
38112       RAISE ex_logging_err;
38113     END IF;
38114 
38115   v_sql_stmt := 37;
38116 
38117   lv_sql_stmt :=
38118   'UPDATE     msc_st_co_products mcp1 '
38119   ||' SET     process_flag = '||G_ERROR_FLG||','
38120   ||'         error_text   = '||''''||lv_message_text||''''
38121   ||' WHERE EXISTS ( SELECT 1 FROM msc_st_co_products mcp2 '
38122   ||'            WHERE mcp2.co_product_name           = mcp1.co_product_name'
38123   ||'            AND mcp2.component_name           = mcp1.component_name'
38124   ||'            AND  mcp2.sr_instance_code       = mcp1.sr_instance_code'
38125   ||'            AND NVL(mcp2.company_name,'||''''||NULL_CHAR||''''||') '
38126   ||'                  = NVL(mcp1.company_name, '||''''||NULL_CHAR||''''||') '
38127   ||'            AND mcp2.process_flag             = mcp1.process_flag'
38128   ||'            AND NVL(mcp2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
38129   ||'            AND mcp2.rowid <> mcp1.rowid )'
38130   ||' AND  mcp1.process_flag              ='|| G_IN_PROCESS
38131   ||' AND  mcp1.sr_instance_code          = :v_instance_code'
38132   ||' AND  NVL(mcp1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
38133 
38134 
38135          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38136 
38137   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
38138 
38139 
38140  lv_column_names :=
38141     'CO_PRODUCT_NAME          ||''~''||'
38142   ||'COMPONENT_NAME           ||''~''||'
38143   ||'SPLIT                    ||''~''||'
38144   ||'PRIMARY_FLAG             ||''~''||'
38145   ||'ORGANIZATION_CODE        ||''~''||'
38146   ||'SR_INSTANCE_CODE         ||''~''||'
38147   ||'COMPANY_NAME             ||''~''||'
38148   ||'DELETED_FLAG ';
38149 
38150 
38151   -- Processing  the MSC_ST_CO_PRODUCTS table in batch --
38152 
38153    LOOP
38154       v_sql_stmt := 38;
38155       lv_cursor_stmt :=
38156       ' SELECT msc_st_batch_id_s.NEXTVAL '
38157       ||' FROM   dual';
38158       OPEN  c1 FOR lv_cursor_stmt;
38159       FETCH c1 INTO lv_batch_id;
38160       CLOSE c1;
38161 
38162       v_sql_stmt := 39;
38163       lv_sql_stmt :=
38164 	      ' UPDATE msc_st_co_products '
38165 	      ||' SET    batch_id  =  :lv_batch_id '
38166 	      ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
38167 	      ||' AND    sr_instance_code               = :v_instance_code'
38168 	      ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
38169 	      ||' AND    rownum                        <= '||v_batch_size;
38170 
38171 
38172 
38173 
38174          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38175 
38176       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
38177 
38178       EXIT WHEN SQL%NOTFOUND;
38179 
38180 
38181       OPEN c8(lv_batch_id);
38182       FETCH c8 BULK COLLECT INTO lb_rowid;
38183       CLOSE c8;
38184 
38185       v_sql_stmt := 40;
38186       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
38187       UPDATE msc_st_co_products
38188       SET  st_transaction_id = msc_st_co_products_s.NEXTVAL,
38189            refresh_id        = v_refresh_id,
38190            last_update_date  = v_current_date,
38191            last_updated_by   = v_current_user,
38192            creation_date     = v_current_date,
38193            created_by        = v_current_user
38194       WHERE  rowid           = lb_rowid(j);
38195 
38196 
38197      -- Set the error message
38198 
38199     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38200                      (p_app_short_name    => 'MSC',
38201                       p_error_code        => 'MSC_PP_NO_DELETION',
38202                       p_message_text      => lv_message_text,
38203                       p_error_text        => lv_error_text,
38204                       p_token1            => 'TABLE_NAME',
38205                       p_token_value1      => 'MSC_ST_CO_PRODUCTS');
38206 
38207       IF lv_return <> 0 THEN
38208         RAISE ex_logging_err;
38209       END IF;
38210 
38211       --Deletion is not allowed on this table.
38212       v_sql_stmt := 41;
38213       lv_sql_stmt :=
38214       ' UPDATE     msc_st_co_products'
38215       ||' SET      process_flag     = '||G_ERROR_FLG||','
38216       ||'          error_text       = '||''''||lv_message_text||''''
38217       ||' WHERE    deleted_flag     = '||SYS_YES
38218       ||' AND      process_flag     = '||G_IN_PROCESS
38219       ||' AND      batch_id         =  :lv_batch_id'
38220       ||' AND      sr_instance_code =  :v_instance_code';
38221 
38222 
38223             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38224 
38225       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
38226 
38227     -- Set the error message
38228     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38229                      (p_app_short_name    => 'MSC',
38230                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
38231                       p_message_text      => lv_message_text,
38232                       p_error_text        => lv_error_text,
38233                       p_token1            => 'COLUMN_NAME',
38234                       p_token_value1      => 'DELETED_FLAG',
38235                       p_token2            => 'DEFAULT_VALUE',
38236                       p_token_value2      =>  SYS_NO );
38237 
38238 
38239 
38240      -- Check whether delete flag value is in (SYS_YES,SYS_NO),if not default to SYS_NO
38241 
38242      lv_where_str :=
38243      ' AND NVL(deleted_flag,'||NULL_VALUE||')NOT IN (1,2)';
38244 
38245      lv_return := MSC_ST_UTIL.LOG_ERROR
38246                     (p_table_name        => 'MSC_ST_CO_PRODUCTS',
38247                      p_instance_code     => v_instance_code,
38248                      p_row               => lv_column_names,
38249                      p_severity          => G_SEV_WARNING,
38250                      p_message_text      => lv_message_text,
38251                      p_error_text        => lv_error_text,
38252                      p_batch_id          => lv_batch_id,
38253                      p_where_str         => lv_where_str,
38254                      p_col_name          => 'DELETED_FLAG',
38255                      p_debug             => v_debug,
38256                      p_default_value     =>  SYS_NO);
38257 
38258 
38259       IF lv_return <> 0 THEN
38260         RAISE ex_logging_err;
38261       END IF;
38262 
38263 
38264      -- Set the error message
38265 
38266     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38267                      (p_app_short_name    => 'MSC',
38268                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
38269                       p_message_text      => lv_message_text,
38270                       p_error_text        => lv_error_text,
38271                       p_token1            => 'COLUMN_NAME',
38272                       p_token_value1      => 'SPLIT');
38273 
38274       IF lv_return <> 0 THEN
38275         RAISE ex_logging_err;
38276       END IF;
38277 
38278     -- Error out records where split  is NULL
38279 
38280      v_sql_stmt := 42;
38281 
38282      lv_sql_stmt :=
38283       ' UPDATE  msc_st_co_products '
38284       ||' SET   process_flag        = '||G_ERROR_FLG||','
38285       ||'       error_text          = '||''''||lv_message_text||''''
38286       ||' WHERE NVL(split,'||NULL_VALUE||') = '||NULL_VALUE
38287       ||' AND   batch_id         = :lv_batch_id'
38288       ||' AND   process_flag     = '||G_IN_PROCESS
38289       ||' AND   sr_instance_code = :v_instance_code';
38290 
38291 
38292         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38293 
38294       EXECUTE IMMEDIATE lv_sql_stmt
38295               USING     lv_batch_id,
38296                         v_instance_code;
38297 
38298 
38299 
38300     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38301                      (p_app_short_name    => 'MSC',
38302                       p_error_code        => 'MSC_PP_INVALID_VALUE',
38303                       p_message_text      => lv_message_text,
38304                       p_error_text        => lv_error_text,
38305                       p_token1            => 'COLUMN_NAME',
38306                       p_token_value1      => 'COMPONENT_NAME');
38307 
38308       IF lv_return <> 0 THEN
38309         RAISE ex_logging_err;
38310       END IF;
38311 
38312       -- Derive Component_id
38313       lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
38314                      (p_table_name       => 'MSC_ST_CO_PRODUCTS',
38315                       p_item_col_name    => 'COMPONENT_NAME',
38316                       p_item_col_id      => 'COMPONENT_ID',
38317                       p_instance_id      => v_instance_id,
38318                       p_instance_code    => v_instance_code,
38319                       p_error_text       => lv_error_text,
38320                       p_batch_id         => lv_batch_id,
38321                       p_severity         => G_SEV_ERROR,
38322                       p_message_text     => lv_message_text,
38323                       p_debug            => v_debug,
38324                       p_row              => lv_column_names);
38325 
38326       IF lv_return <> 0 THEN
38327         RAISE ex_logging_err;
38328       END IF;
38329 
38330 
38331       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38332                      (p_app_short_name    => 'MSC',
38333                       p_error_code        => 'MSC_PP_INVALID_VALUE',
38334                       p_message_text      => lv_message_text,
38335                       p_error_text        => lv_error_text,
38336                       p_token1            => 'COLUMN_NAME',
38337                       p_token_value1      => 'CO_PRODUCT_NAME');
38338 
38339       IF lv_return <> 0 THEN
38340         RAISE ex_logging_err;
38341       END IF;
38342 
38343       -- Derive Co_product_id
38344       lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
38345                      (p_table_name       => 'MSC_ST_CO_PRODUCTS',
38346                       p_item_col_name    => 'CO_PRODUCT_NAME',
38347                       p_item_col_id      => 'CO_PRODUCT_ID',
38348                       p_instance_id      => v_instance_id,
38349                       p_instance_code    => v_instance_code,
38350                       p_error_text       => lv_error_text,
38351                       p_batch_id         => lv_batch_id,
38352                       p_severity         => G_SEV_ERROR,
38353                       p_message_text     => lv_message_text,
38354                       p_debug            => v_debug,
38355                       p_row              => lv_column_names);
38356 
38357       IF lv_return <> 0 THEN
38358         RAISE ex_logging_err;
38359       END IF;
38360 
38361 
38362 
38363     -- Derive the CO_PRODUCT_GROUP_ID from LOCAL ID table
38364     lv_sql_stmt :=
38365     'UPDATE MSC_ST_CO_PRODUCTS t1'
38366     ||' SET CO_PRODUCT_GROUP_ID '
38367     ||' = (SELECT local_id'
38368     ||' FROM   msc_local_id_setup t2'
38369     ||' WHERE  t2.char1         = t1.sr_instance_code'
38370     ||' AND    NVL(t2.char2,'||''''||NULL_CHAR||''''||') '
38371     ||'        =    NVL(t1.company_name,'||''''||NULL_CHAR||''''||') '
38372     ||' AND    t2.char4         = t1.COMPONENT_NAME '
38373     ||' AND    t2.entity_name   = ''CO_PRODUCT_GROUP_ID'')'
38374     ||' WHERE  sr_instance_code = :v_instance_code '
38375     ||' AND    batch_id         = :lv_batch_id '
38376     ||' AND    process_flag     = '||G_IN_PROCESS;
38377 
38378 
38379         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38380 
38381       EXECUTE IMMEDIATE lv_sql_stmt
38382               USING     v_instance_code,
38383                         lv_batch_id;
38384 
38385 
38386     -- Error out all the records for which a CO_PRODUCT_GROUP_ID is derived.
38387     -- the idea is this record was already loaded earlier. Updation not allowed.
38388 
38389      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38390                      (p_app_short_name    => 'MSC',
38391                       p_error_code        => 'MSC_PP_NO_CO_PROD_UPDATE',
38392                       p_message_text      => lv_message_text,
38393                       p_error_text        => lv_error_text);
38394 
38395       IF lv_return <> 0 THEN
38396         RAISE ex_logging_err;
38397       END IF;
38398 
38399       v_sql_stmt := 43;
38400 
38401      lv_sql_stmt :=
38402       ' UPDATE  msc_st_co_products '
38403       ||' SET   process_flag        = '||G_ERROR_FLG||','
38404       ||'       error_text          = '||''''||lv_message_text||''''
38405       ||' WHERE NVL(co_product_group_id,'||NULL_VALUE||') <> '||NULL_VALUE
38406       ||' AND   batch_id         = :lv_batch_id'
38407       ||' AND   process_flag     = '||G_IN_PROCESS
38408       ||' AND   sr_instance_code = :v_instance_code';
38409 
38410 
38411         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38412 
38413       EXECUTE IMMEDIATE lv_sql_stmt
38414               USING     lv_batch_id,
38415                         v_instance_code;
38416 
38417 
38418     -- Call to customised validation.
38419     -- Note this is called prior to next validation, so that records getting
38420     -- errored out in the customised validation later otherwise donot violate this.
38421 
38422       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
38423         (ERRBUF         => lv_error_text,
38424          RETCODE        => lv_return,
38425          pBatchID       => lv_batch_id,
38426          pInstanceCode  => v_instance_code,
38427          pEntityName    => 'MSC_ST_CO_PRODUCTS',
38428          pInstanceID    => v_instance_id);
38429 
38430       IF NVL(lv_return,0) <> 0 THEN
38431         RAISE ex_logging_err;
38432       END IF;
38433 
38434     -- Set the error message
38435 
38436     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38437                      (p_app_short_name    => 'MSC',
38438                       p_error_code        => 'MSC_PP_INVALID_CO_PROD_SET',
38439                       p_message_text      => lv_message_text,
38440                       p_error_text        => lv_error_text);
38441 
38442       IF lv_return <> 0 THEN
38443         RAISE ex_logging_err;
38444       END IF;
38445 
38446     -- Error out all the co products records for that component
38447     -- if any of its co product record has errored out. The idea being
38448     -- the complete set of co-products for that component goes invalid
38449     -- Note using the request id, so that it doesn't consider the old records.
38450      v_sql_stmt := 44;
38451 
38452      lv_sql_stmt :=
38453   'UPDATE     msc_st_co_products mcp1 '
38454   ||' SET     process_flag = '||G_PROPAGATION||','
38455   ||'         error_text   = '||''''||lv_message_text||''''
38456   ||' WHERE EXISTS ( SELECT 1 FROM msc_st_co_products mcp2 '
38457   ||'            WHERE mcp2.sr_instance_code       = mcp1.sr_instance_code'
38458   ||'            AND mcp2.component_id             = mcp1.component_id'
38459   ||'            AND mcp2.request_id               = :v_request_id '
38460   ||'            AND mcp2.process_flag             = '||G_ERROR_FLG ||' )'
38461   ||' AND  mcp1.process_flag              ='|| G_IN_PROCESS
38462   ||' AND   batch_id         = :lv_batch_id'
38463   ||' AND  mcp1.sr_instance_code          = :v_instance_code';
38464 
38465 
38466         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38467 
38468       EXECUTE IMMEDIATE lv_sql_stmt
38469               USING     v_request_id,
38470                         lv_batch_id,
38471                         v_instance_code;
38472 
38473     COMMIT;
38474  END LOOP;
38475 
38476      -- Next two validations donot need to propagate the error to the other in the set
38477      -- as the validation itself does it
38478 
38479      -- split percentage for a component should add up to 100.
38480      -- Note: assumption is a component can have only one group of co products.
38481      -- Set the error message
38482     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38483                      (p_app_short_name    => 'MSC',
38484                       p_error_code        => 'MSC_PP_CO_PROD_SPLIT_INVALID',
38485                       p_message_text      => lv_message_text,
38486                       p_error_text        => lv_error_text,
38487                       p_token1            => 'COLUMN_NAME',
38488                       p_token_value1      => 'SPLIT');
38489 
38490       IF lv_return <> 0 THEN
38491         RAISE ex_logging_err;
38492       END IF;
38493 
38494      v_sql_stmt := 45;
38495     lv_sql_stmt :=
38496   'UPDATE   msc_st_co_products mcp1'
38497   ||' SET      process_flag = '||G_ERROR_FLG||','
38498   ||'         error_text   = '||''''||lv_message_text||''''
38499   ||' WHERE  100 <>  ( SELECT sum(split) '
38500   ||'            FROM msc_st_co_products mcp2'
38501   ||'            WHERE  mcp2.component_name           = mcp1.component_name'
38502   ||'            AND mcp2.sr_instance_code       = mcp1.sr_instance_code'
38503   ||'            AND NVL(mcp2.company_name,'||''''||NULL_CHAR||''''||') '
38504   ||'                  = NVL(mcp1.company_name, '||''''||NULL_CHAR||''''||') '
38505   ||'            AND mcp2.process_flag             = mcp1.process_flag'
38506   ||'            GROUP BY sr_instance_code,organization_code,company_name,'
38507   ||'            component_name)'
38508   ||'  AND     mcp1.process_flag          = '||G_IN_PROCESS
38509   ||'  AND     mcp1.sr_instance_code       = :v_instance_code';
38510 
38511 
38512          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38513 
38514   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
38515 
38516     -- one component should have one and only one primary co product.
38517     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38518                      (p_app_short_name    => 'MSC',
38519                       p_error_code        => 'MSC_PP_MULTI_PRI_CO_PROD',
38520                       p_message_text      => lv_message_text,
38521                       p_error_text        => lv_error_text,
38522                       p_token1            => 'COLUMN_NAME',
38523                       p_token_value1      => 'PRIMARY_FLAG');
38524 
38525       IF lv_return <> 0 THEN
38526         RAISE ex_logging_err;
38527       END IF;
38528 
38529      v_sql_stmt := 46;
38530     lv_sql_stmt :=
38531   'UPDATE   msc_st_co_products mcp1'
38532   ||' SET      process_flag = '||G_ERROR_FLG||','
38533   ||'         error_text   = '||''''||lv_message_text||''''
38534   ||' WHERE   1 <> ( SELECT COUNT(*) '
38535   ||'            FROM msc_st_co_products mcp2'
38536   ||'            WHERE mcp2.component_name           = mcp1.component_name'
38537   ||'            AND mcp2.sr_instance_code       = mcp1.sr_instance_code'
38538   ||'            AND NVL(mcp2.company_name,'||''''||NULL_CHAR||''''||') '
38539   ||'                  = NVL(mcp1.company_name, '||''''||NULL_CHAR||''''||') '
38540   ||'            AND mcp2.primary_flag             = 1'
38541   ||'            AND mcp2.process_flag             = mcp1.process_flag'
38542   ||'            GROUP BY sr_instance_code,organization_code,company_name,'
38543   ||'            component_name )'
38544   ||'  AND     mcp1.process_flag          = '||G_IN_PROCESS
38545   ||'  AND     mcp1.sr_instance_code       = :v_instance_code';
38546 
38547 
38548          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38549 
38550   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
38551 
38552   -- Generate co_product_group_id. This is only needed in the local_id table
38553   -- and not used any where else.
38554 
38555      v_sql_stmt := 47;
38556 
38557      OPEN c9 ;
38558      FETCH c9 BULK COLLECT INTO lb_rowid ;
38559 
38560      IF c9%ROWCOUNT > 0  THEN
38561 
38562     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
38563 
38564         UPDATE msc_st_co_products
38565         SET    co_product_group_id    = msc_st_co_product_group_id_s.NEXTVAL
38566         WHERE  rowid                  = lb_rowid(j);
38567 
38568 
38569    v_sql_stmt := 48;
38570    FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
38571 
38572     -- Insert into the LID table  with the  user defined key
38573 
38574        INSERT INTO  msc_local_id_setup
38575        (local_id,
38576        st_transaction_id,
38577        instance_id,
38578        entity_name,
38579        data_source_type,
38580        char1,
38581        char2,
38582        char4,
38583        SOURCE_COMPONENT_ID ,
38584        SOURCE_CO_PRODUCT_ID ,
38585        SOURCE_CO_PRODUCT_GROUP_ID  ,
38586        last_update_date,
38587        last_updated_by,
38588        creation_date,
38589        created_by  )
38590       SELECT
38591        co_product_group_id,
38592        st_transaction_id,
38593        v_instance_id,
38594        'CO_PRODUCT_GROUP_ID',
38595        data_source_type,
38596        v_instance_code,
38597        company_name,
38598        component_name,
38599        SOURCE_COMPONENT_ID ,
38600        SOURCE_CO_PRODUCT_ID ,
38601        SOURCE_CO_PRODUCT_GROUP_ID  ,
38602        v_current_date,
38603        v_current_user,
38604        v_current_date,
38605        v_current_user
38606       FROM msc_st_co_products
38607      WHERE   rowid   =   lb_rowid(j);
38608 
38609      END IF;
38610      CLOSE c9;
38611 
38612 
38613 
38614     -- Set the process flag as Valid and derive sr_instance_id
38615     lv_return :=
38616     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_CO_PRODUCTS',
38617                                p_instance_id    => v_instance_id,
38618                                p_instance_code  => v_instance_code,
38619                                p_process_flag   => G_VALID,
38620                                p_error_text     => lv_error_text,
38621                                p_debug          => v_debug);
38622 
38623       IF lv_return < 0 THEN
38624         RAISe ex_logging_err;
38625       END IF;
38626 
38627 
38628     -- At the end calling  the LOG_ERROR for logging all errored out records
38629 
38630    lv_return :=
38631    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_CO_PRODUCTS',
38632                          p_instance_code => v_instance_code,
38633                          p_row           => lv_column_names,
38634                          p_severity      => G_SEV_ERROR,
38635                          p_message_text  => NULL,
38636                          p_error_text    => lv_error_text,
38637                          p_debug         => v_debug );
38638 
38639     IF lv_return < 0 THEN
38640         RAISe ex_logging_err;
38641     END IF;
38642 
38643   COMMIT;
38644   -- Now Updating the tables MSC_ST_BOMS and MSC_ST_BOM_COMPONENTS.
38645 
38646    LOAD_CO_PRODUCT_BOMS;
38647 
38648 
38649   EXCEPTION
38650     WHEN too_many_rows THEN
38651       lv_error_text :=substr('MSC_CL_PRE_PROCESS.LOAD_BOM'||'('
38652                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
38653         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
38654       ROLLBACK ;
38655 
38656     WHEN ex_logging_err THEN
38657         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
38658       ROLLBACK;
38659 
38660     WHEN OTHERS THEN
38661       lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_BOM'||'('
38662                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
38663         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
38664       ROLLBACK;
38665 
38666   END LOAD_BOM;
38667 
38668 
38669 
38670 
38671   /*==========================================================================+
38672   | DESCRIPTION  : This function performs the validation and loads the data   |
38673   |                for business object ROUTING.                               |
38674   +==========================================================================*/
38675 
38676   PROCEDURE  LOAD_ROUTING IS
38677 
38678   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
38679   TYPE CurTyp       IS REF CURSOR;
38680   c1                CurTyp;
38681   lb_rowid          RowidTab;
38682 
38683   lv_local_id       NUMBER;
38684   lv_column_names   VARCHAR2(5000);     -- Stores concatenated column names
38685   lv_return         NUMBER;
38686   lv_error_text     VARCHAR2(250);
38687   lv_where_str      VARCHAR2(5000);
38688   lv_sql_stmt       VARCHAR2(5000);
38689   lv_cursor_stmt    VARCHAR2(5000);
38690   lv_batch_id       msc_st_routings.batch_id%TYPE;
38691   lv_message_text   msc_errors.error_text%TYPE;
38692   ex_logging_err    EXCEPTION;
38693 
38694 
38695   CURSOR c2(p_batch_id NUMBER) IS
38696     SELECT rowid
38697     FROM   msc_st_routings
38698     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
38699     AND    sr_instance_code = v_instance_code
38700     AND    batch_id         = p_batch_id;
38701 
38702   CURSOR c3(p_batch_id NUMBER) IS
38703     SELECT rowid
38704     FROM   msc_st_routings
38705     WHERE  process_flag     = G_IN_PROCESS
38706     AND    sr_instance_code = v_instance_code
38707     AND    batch_id         = p_batch_id
38708     AND    NVL(routing_sequence_id,NULL_VALUE) = NULL_VALUE
38709     AND    deleted_flag     = SYS_NO;
38710 
38711   CURSOR c4(p_batch_id NUMBER) IS
38712     SELECT rowid
38713     FROM   msc_st_routing_operations
38714     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
38715     AND    sr_instance_code = v_instance_code
38716     AND    batch_id         = p_batch_id;
38717 
38718   CURSOR c5(p_batch_id NUMBER) IS
38719     SELECT rowid
38720     FROM   msc_st_routing_operations
38721     WHERE  process_flag     = G_IN_PROCESS
38722     AND    sr_instance_code = v_instance_code
38723     AND    batch_id         = p_batch_id
38724     AND    NVL(operation_sequence_id,NULL_VALUE) = NULL_VALUE
38725     AND    deleted_flag     = SYS_NO;
38726 
38727   CURSOR c6(p_batch_id NUMBER) IS
38728     SELECT rowid
38729     FROM   msc_st_operation_resources
38730     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
38731     AND    sr_instance_code = v_instance_code
38732     AND    batch_id         = p_batch_id;
38733 
38734   CURSOR c7(p_batch_id NUMBER) IS
38735     SELECT max(rowid)
38736     FROM   msc_st_operation_resources
38737     WHERE  process_flag     = G_IN_PROCESS
38738     AND    sr_instance_code = v_instance_code
38739     AND    batch_id         = p_batch_id
38740     AND    NVL(resource_seq_num,NULL_VALUE) = NULL_VALUE
38741     AND    deleted_flag     = SYS_NO
38742     GROUP BY sr_instance_code,company_name,
38743              organization_code,routing_name,
38744              operation_seq_code,resource_seq_code,
38745              alternate_routing_designator,alternate_number;
38746 
38747   CURSOR c8(p_batch_id NUMBER) IS
38748     SELECT max(rowid)
38749     FROM   msc_st_operation_resources
38750     WHERE  process_flag  = G_IN_PROCESS
38751     AND    sr_instance_code = v_instance_code
38752     AND    batch_id         = p_batch_id
38753     AND    NVL(resource_seq_num,NULL_VALUE) <> NULL_VALUE
38754     AND    NVL(operation_sequence_id,NULL_VALUE) <> NULL_VALUE
38755     AND    NVL(routing_sequence_id,NULL_VALUE) <> NULL_VALUE
38756     GROUP BY sr_instance_code, operation_sequence_id,
38757              routing_sequence_id,resource_seq_num ;
38758 
38759      -- For OSFM Integration
38760 
38761    CURSOR c9(p_batch_id NUMBER) IS
38762     SELECT rowid
38763     FROM   msc_st_operation_networks
38764     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
38765     AND    sr_instance_code = v_instance_code
38766     AND    batch_id         = p_batch_id;
38767 
38768 
38769    -- For bug 3549086
38770 
38771     CURSOR c10(p_batch_id NUMBER) IS
38772     SELECT max(rowid)
38773     FROM   msc_st_routing_operations
38774     WHERE  process_flag      IN (G_IN_PROCESS)
38775     AND    sr_instance_code = v_instance_code
38776     AND    batch_id         = p_batch_id
38777     AND    NVL(department_id,NULL_VALUE) = NULL_VALUE
38778     GROUP BY sr_instance_code,company_name,
38779              organization_code,department_code  ;
38780 
38781   BEGIN
38782     --Duplicate records check for the records whose source is XML
38783 
38784     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38785                  (p_app_short_name    => 'MSC',
38786                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
38787                   p_message_text      => lv_message_text,
38788                   p_error_text        => lv_error_text);
38789 
38790     IF lv_return <> 0 THEN
38791       RAISE ex_logging_err;
38792     END IF;
38793 
38794   -- Duplicate UDK validation for XML message
38795 
38796   v_sql_stmt := 01;
38797   lv_sql_stmt :=
38798   'UPDATE   msc_st_routings msr1'
38799   ||' SET     process_flag  = '||G_ERROR_FLG||','
38800   ||'         error_text   = '||''''||lv_message_text||''''
38801   ||' WHERE   message_id <  (SELECT MAX(message_id)'
38802   ||'                      FROM msc_st_routings msr2'
38803   ||'          WHERE msr2.sr_instance_code   = msr1.sr_instance_code'
38804   ||'          AND   NVL(msr2.company_name,'||''''||NULL_CHAR||''''||') '
38805   ||'                 =    NVL(msr1.company_name,'||''''||NULL_CHAR||''''||') '
38806   ||'          AND   msr2.organization_code  = msr1.organization_code'
38807   ||'          AND   NVL(msr2.routing_name,'||''''||NULL_CHAR||''''||') '
38808   ||'               = NVL(msr1.routing_name,'||''''||NULL_CHAR||''''||') '
38809   ||'          AND   msr2.assembly_name      = msr1.assembly_name'
38810   ||'          AND   NVL(msr2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
38811   ||'               = NVL(msr1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
38812   ||'          AND   msr2.process_flag        ='||G_IN_PROCESS
38813   ||'          AND    NVL(msr2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
38814   ||'  AND     msr1.process_flag = '||G_IN_PROCESS
38815   ||'  AND     msr1.sr_instance_code = :v_instance_code'
38816   ||'  AND     NVL(msr1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
38817 
38818 
38819          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38820 
38821   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
38822 
38823   --Duplicate records check for the records whose source is batch load
38824 
38825     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38826                  (p_app_short_name    => 'MSC',
38827                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
38828                   p_message_text      => lv_message_text,
38829                   p_error_text        => lv_error_text);
38830 
38831     IF lv_return <> 0 THEN
38832       RAISE ex_logging_err;
38833     END IF;
38834 
38835    v_sql_stmt := 02;
38836 
38837   lv_sql_stmt :=
38838   'UPDATE   msc_st_routings msr1 '
38839   ||' SET     process_flag  = '||G_ERROR_FLG||','
38840   ||'         error_text   = '||''''||lv_message_text||''''
38841   ||' WHERE    EXISTS ( SELECT 1 FROM msc_st_routings msr2'
38842   ||'          WHERE msr2.sr_instance_code   = msr1.sr_instance_code'
38843   ||'          AND   NVL(msr2.company_name,'||''''||NULL_CHAR||''''||') '
38844   ||'                 =    NVL(msr1.company_name,'||''''||NULL_CHAR||''''||') '
38845   ||'          AND   msr2.organization_code  = msr1.organization_code'
38846   ||'          AND   NVL(msr2.routing_name,'||''''||NULL_CHAR||''''||') '
38847   ||'               = NVL(msr1.routing_name,'||''''||NULL_CHAR||''''||') '
38848   ||'          AND   msr2.assembly_name      = msr1.assembly_name'
38849   ||'          AND   NVL(msr2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
38850   ||'               = NVL(msr1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
38851   ||'          AND   msr2.process_flag        ='||G_IN_PROCESS
38852   ||'          AND NVL(msr2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
38853   ||'     GROUP BY sr_instance_code,organization_code,company_name,routing_name, '
38854   ||'     assembly_name, alternate_routing_designator '
38855   ||'              HAVING COUNT(*) >1 )'
38856   ||'  AND     msr1.process_flag = '||G_IN_PROCESS
38857   ||'  AND     msr1.sr_instance_code = :v_instance_code'
38858   ||' AND     NVL(msr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
38859 
38860 
38861          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38862 
38863   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
38864 
38865   -- For OSFM integration
38866   -- Network routings should have FIRST_OP_SEQ_NUM and LAST_OP_SEQ_NUM
38867   -- corresponding to these should have FIRST_OPERATION_SEQ_CODE and LAST_OPERATION_SEQ_CODE
38868   -- in the control files.
38869   lv_column_names :=
38870   'ROUTING_NAME                    ||''~''||'
38871   ||' ASSEMBLY_NAME                ||''~''||'
38872   ||' ROUTING_TYPE                 ||''~''||'
38873   ||' ORGANIZATION_CODE            ||''~''||'
38874   ||' SR_INSTANCE_CODE             ||''~''||'
38875   ||' ROUTING_COMMENT              ||''~''||'
38876   ||' ALTERNATE_ROUTING_DESIGNATOR ||''~''||'
38877   ||' PROJECT_NUMBER               ||''~''||'
38878   ||' TASK_NUMBER                  ||''~''||'
38879   ||' LINE_CODE                    ||''~''||'
38880   ||' UOM_CODE                     ||''~''||'
38881   ||' CFM_ROUTING_FLAG             ||''~''||'
38882   ||' CTP_FLAG                     ||''~''||'
38883   ||' ROUTING_QUANTITY             ||''~''||'
38884   ||' DELETED_FLAG                 ||''~''||'
38885   ||' BOM_NAME                     ||''~''||'
38886   ||' ALTERNATE_BOM_DESIGNATOR     ||''~''||'
38887   ||' FIRST_OPERATION_SEQ_CODE     ||''~''||'
38888   ||' LAST_OPERATION_SEQ_CODE      ||''~''||'
38889   ||' COMPANY_NAME ' ;
38890 
38891 
38892   -- Now processing by batch
38893    LOOP
38894       v_sql_stmt := 03;
38895       lv_cursor_stmt :=
38896       ' SELECT msc_st_batch_id_s.NEXTVAL '
38897       ||' FROM   dual';
38898 
38899       OPEN  c1 FOR lv_cursor_stmt;
38900       FETCH c1 INTO lv_batch_id;
38901       CLOSE c1;
38902 
38903       v_sql_stmt := 04;
38904       lv_sql_stmt :=
38905       ' UPDATE msc_st_routings '
38906       ||' SET    batch_id  = :lv_batch_id'
38907       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
38908       ||' AND    sr_instance_code               = :v_instance_code'
38909       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
38910       ||' AND    rownum                        <= '||v_batch_size;
38911 
38912 
38913          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38914 
38915       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
38916 
38917       EXIT WHEN SQL%NOTFOUND;
38918 
38919       OPEN c2(lv_batch_id);
38920       FETCH c2 BULK COLLECT INTO lb_rowid;
38921       CLOSE c2;
38922 
38923       v_sql_stmt := 05;
38924       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
38925       UPDATE msc_st_routings
38926       SET  st_transaction_id = msc_st_routings_s.NEXTVAL,
38927            refresh_id        = v_refresh_id,
38928            last_update_date  = v_current_date,
38929            last_updated_by   = v_current_user,
38930            creation_date     = v_current_date,
38931            created_by        = v_current_user
38932       WHERE  rowid           = lb_rowid(j);
38933 
38934 
38935       -- Set the  message
38936       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38937                      (p_app_short_name    => 'MSC',
38938                       p_error_code        => 'MSC_PP_NO_DELETION',
38939                       p_message_text      => lv_message_text,
38940                       p_error_text        => lv_error_text,
38941                       p_token1            => 'TABLE_NAME',
38942                       p_token_value1      => 'MSC_ST_ROUTINGS');
38943 
38944       IF lv_return <> 0 THEN
38945         RAISE ex_logging_err;
38946       END IF;
38947 
38948      --Deletion is not allowed on this table.
38949       v_sql_stmt := 06;
38950       lv_sql_stmt :=
38951       ' UPDATE     msc_st_routings '
38952       ||' SET      process_flag     ='||G_ERROR_FLG||','
38953       ||'          error_text       = '||''''||lv_message_text||''''
38954       ||' WHERE    deleted_flag     = '||SYS_YES
38955       ||' AND      process_flag     = '||G_IN_PROCESS
38956       ||' AND      batch_id         =  :lv_batch_id'
38957       ||' AND      sr_instance_code = :v_instance_code';
38958 
38959 
38960             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
38961 
38962       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
38963 
38964     -- Set the error message
38965     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
38966                      (p_app_short_name    => 'MSC',
38967                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
38968                       p_message_text      => lv_message_text,
38969                       p_error_text        => lv_error_text,
38970                       p_token1            => 'COLUMN_NAME',
38971                       p_token_value1      => 'DELETED_FLAG',
38972                       p_token2            => 'DEFAULT_VALUE',
38973                       p_token_value2      =>  SYS_NO  );
38974 
38975       IF lv_return <> 0 THEN
38976         RAISE ex_logging_err;
38977       END IF;
38978 
38979       -- Check DELETE_FLAG column has valid value or not and if not set it to SYS_NO
38980       -- Log error procedure inserts warning and also defaults the delete flag
38981 
38982       lv_where_str :=
38983       ' AND NVL(deleted_flag,'||NULL_VALUE||') <>'||SYS_NO;
38984 
38985        lv_return := MSC_ST_UTIL.LOG_ERROR
38986                     (p_table_name        => 'MSC_ST_ROUTINGS',
38987                      p_instance_code     => v_instance_code,
38988                      p_row               => lv_column_names,
38989                      p_severity          => G_SEV_WARNING,
38990                      p_message_text      => lv_message_text,
38991                      p_error_text        => lv_error_text,
38992                      p_batch_id          => lv_batch_id,
38993                      p_where_str         => lv_where_str,
38994                      p_col_name          => 'DELETED_FLAG',
38995                      p_debug             => v_debug,
38996                      p_default_value     => SYS_NO);
38997 
38998       IF lv_return <> 0 THEN
38999         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_error_text);
39000       END IF;
39001 
39002       -- Set the  message
39003 
39004      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39005                      (p_app_short_name    => 'MSC',
39006                       p_error_code        => 'MSC_PP_INVALID_VALUE',
39007                       p_message_text      => lv_message_text,
39008                       p_error_text        => lv_error_text,
39009                       p_token1            => 'COLUMN_NAME',
39010                       p_token_value1      => 'ORGANIZATION_CODE');
39011 
39012       IF lv_return <> 0 THEN
39013         RAISE ex_logging_err;
39014       END IF;
39015 
39016       -- derive  organization id , call utilty package- msc_st_util's procedure
39017 
39018      lv_return :=
39019      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_ROUTINGS',
39020                                     p_org_partner_name  => 'ORGANIZATION_CODE',
39021                                     p_org_partner_id    => 'ORGANIZATION_ID',
39022                                     p_instance_code     => v_instance_code,
39023                                     p_partner_type      => G_ORGANIZATION,
39024                                     p_severity          => G_SEV_ERROR,
39025                                     p_message_text      => lv_message_text,
39026                                     p_batch_id          => lv_batch_id,
39027                                     p_debug             => v_debug,
39028                                     p_error_text        => lv_error_text);
39029 
39030      if(lv_return <> 0) then
39031         RAISE ex_logging_err;
39032      end if;
39033 
39034       -- Set the  message
39035 
39036      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39037                      (p_app_short_name    => 'MSC',
39038                       p_error_code        => 'MSC_PP_INVALID_VALUE',
39039                       p_message_text      => lv_message_text,
39040                       p_error_text        => lv_error_text,
39041                       p_token1            => 'COLUMN_NAME',
39042                       p_token_value1      => 'ASSEMBLY_NAME');
39043 
39044       IF lv_return <> 0 THEN
39045         RAISE ex_logging_err;
39046       END IF;
39047 
39048      -- Derive assembly item id
39049 
39050      lv_return :=
39051      MSC_ST_UTIL.DERIVE_ITEM_ID(p_table_name => 'MSC_ST_ROUTINGS',
39052                              p_item_col_name => 'ASSEMBLY_NAME',
39053                              p_item_col_id   => 'ASSEMBLY_ITEM_ID',
39054                              p_instance_code => v_instance_code,
39055                              p_instance_id   => v_instance_id,
39056                              p_severity      => G_SEV_ERROR,
39057                              p_message_text  => lv_message_text,
39058                              p_batch_id      => lv_batch_id,
39059                              p_debug         => v_debug,
39060                              p_error_text    => lv_error_text);
39061 
39062 
39063     if(lv_return <> 0) then
39064          RAISE ex_logging_err;
39065     end if;
39066 
39067 
39068      -- Now check whether Routing  Name is NULL , if it is NULL we will populate
39069      -- Assembly  Name in Routing Name column for all such records
39070 
39071     v_sql_stmt := 07;
39072 
39073     lv_sql_stmt :=
39074     'UPDATE msc_st_routings   '
39075     ||' SET    routing_name             = assembly_name'
39076     ||' WHERE  sr_instance_code         = :v_instance_code'
39077     ||' AND    process_flag             ='|| G_IN_PROCESS
39078     ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||') '
39079     ||'         = '||''''||NULL_CHAR||''''
39080     ||' AND    batch_id                 = :lv_batch_id ';
39081 
39082 
39083             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39084 
39085      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
39086 
39087 
39088     -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
39089 
39090     lv_return :=msc_st_util.derive_routing_sequence_id
39091                 (p_table_name     => 'MSC_ST_ROUTINGS',
39092                  p_rtg_col_name   => 'ROUTING_NAME',
39093                  p_rtg_col_id     =>'ROUTING_SEQUENCE_ID',
39094                  p_instance_code  => v_instance_code,
39095                  p_batch_id       => lv_batch_id,
39096                  p_debug          => v_debug,
39097                  p_error_text     => lv_error_text);
39098 
39099     if (lv_return <> 0 )then
39100        MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_error_text);
39101     end if;
39102 
39103     -- Set the error message
39104     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39105                      (p_app_short_name    => 'MSC',
39106                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
39107                       p_message_text      => lv_message_text,
39108                       p_error_text        => lv_error_text,
39109                       p_token1            => 'COLUMN_NAME',
39110                       p_token_value1      => 'ROUTING_TYPE',
39111                       p_token2            => 'DEFAULT_VALUE',
39112                       p_token_value2      =>  G_ROUTING_TYPE);
39113 
39114       IF lv_return <> 0 THEN
39115         RAISE ex_logging_err;
39116       END IF;
39117 
39118      -- Defualt routing type
39119 
39120      lv_where_str :=
39121      ' AND NVL(routing_type,'||NULL_VALUE||') NOT IN (1,2)';
39122 
39123      lv_return := MSC_ST_UTIL.LOG_ERROR
39124                     (p_table_name       => 'MSC_ST_ROUTINGS',
39125                      p_instance_code     => v_instance_code,
39126                      p_row               => lv_column_names,
39127                      p_severity          => G_SEV_WARNING,
39128                      p_message_text      => lv_message_text,
39129                      p_error_text        => lv_error_text,
39130                      p_batch_id          => lv_batch_id,
39131                      p_where_str         => lv_where_str,
39132                      p_col_name          => 'ROUTING_TYPE',
39133                      p_debug             => v_debug,
39134                      p_default_value     => G_ROUTING_TYPE);
39135 
39136 
39137       IF lv_return <> 0 THEN
39138         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_error_text);
39139       END IF;
39140 
39141     -- Set the error message
39142     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39143                    (p_app_short_name    => 'MSC',
39144                     p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
39145                     p_message_text      => lv_message_text,
39146                     p_error_text        => lv_error_text,
39147                     p_token1            => 'COLUMN_NAMES',
39148                     p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
39149                                            ||' ORGANIZATION_CODE AND PROJECT_NUMBER',
39150                     p_token2            => 'MASTER_TABLE',
39151                     p_token_value2      => 'MSC_ST_PROJECT_TASKS');
39152 
39153     IF lv_return <> 0 THEN
39154       RAISE ex_logging_err;
39155     END IF;
39156 
39157     --Derive Project Id.
39158     lv_return:= MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
39159                            (p_table_name          => 'MSC_ST_ROUTINGS',
39160                             p_proj_col_name       => 'PROJECT_NUMBER',
39161                             p_proj_task_col_id    => 'PROJECT_ID',
39162                             p_instance_code       => v_instance_code,
39163                             p_entity_name         => 'PROJECT_ID',
39164                             p_error_text          => lv_error_text,
39165                             p_batch_id            => lv_batch_id,
39166                             p_severity            => G_SEV_WARNING,
39167                             p_message_text        => lv_message_text,
39168                             p_debug               => v_debug,
39169                             p_row                 => lv_column_names);
39170     IF lv_return<> 0 THEN
39171       RAISE ex_logging_err;
39172     END IF;
39173 
39174     -- Set the error message
39175       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39176                    (p_app_short_name    => 'MSC',
39177                     p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
39178                     p_message_text      => lv_message_text,
39179                     p_error_text        => lv_error_text,
39180                     p_token1            => 'COLUMN_NAMES',
39181                     p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
39182                                            ||' ORGANIZATION_CODE, PROJECT_NUMBER,'
39183                                            ||' TASK_NUMBER',
39184                     p_token2            => 'MASTER_TABLE',
39185                     p_token_value2      => 'MSC_ST_PROJECT_TASKS');
39186 
39187     IF lv_return <> 0 THEN
39188       RAISE ex_logging_err;
39189     END IF;
39190 
39191     --Derive Task Id.
39192     lv_return:= MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
39193                            (p_table_name          => 'MSC_ST_ROUTINGS',
39194                             p_proj_col_name       => 'PROJECT_NUMBER',
39195                             p_proj_task_col_id    => 'TASK_ID',
39196                             p_instance_code       => v_instance_code,
39197                             p_entity_name         => 'TASK_ID',
39198                             p_error_text          => lv_error_text,
39199                             p_task_col_name       => 'TASK_NUMBER',
39200                             p_batch_id            => lv_batch_id,
39201                             p_severity            => G_SEV_WARNING,
39202                             p_message_text        => lv_message_text,
39203                             p_debug               => v_debug,
39204                             p_row                 => lv_column_names);
39205 
39206     IF lv_return<> 0 THEN
39207       RAISE ex_logging_err;
39208     END IF;
39209 
39210     -- Derive LINE_ID
39211      v_sql_stmt := 08;
39212 
39213      lv_sql_stmt :=
39214      'UPDATE        msc_st_routings   msr'
39215      ||' SET        line_id  = ( SELECT local_id'
39216      ||'                        FROM msc_local_id_setup mlis'
39217      ||'                        WHERE  mlis.char1 = msr.sr_instance_code'
39218      ||'                        AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
39219      ||'                        =    NVL(msr.company_name,'||''''||NULL_CHAR||''''||') '
39220      ||'                        AND    mlis.char3 = msr.organization_code'
39221      ||'                        AND    mlis.char4 = msr.line_code'
39222      ||'                        AND    mlis.entity_name = ''LINE_ID'')'
39223      ||' WHERE      sr_instance_code = :v_instance_code'
39224      ||' AND        process_flag     = '||G_IN_PROCESS
39225      ||' AND        batch_id         = :lv_batch_id';
39226 
39227 
39228             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39229 
39230      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
39231 
39232       -- Set the  message
39233 
39234      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39235                      (p_app_short_name    => 'MSC',
39236                       p_error_code        => 'MSC_PP_INVALID_VALUE',
39237                       p_message_text      => lv_message_text,
39238                       p_error_text        => lv_error_text,
39239                       p_token1            => 'COLUMN_NAME',
39240                       p_token_value1      => 'UOM_CODE');
39241 
39242       IF lv_return <> 0 THEN
39243         RAISE ex_logging_err;
39244       END IF;
39245 
39246      -- Validate the UOM code
39247 
39248       v_sql_stmt := 09;
39249       lv_sql_stmt :=
39250       'UPDATE   msc_st_routings msr'
39251       ||' SET   process_flag ='||G_ERROR_FLG||','
39252       ||'       error_text       =        '||''''||lv_message_text||''''
39253       ||' WHERE NOT   EXISTS (SELECT 1'
39254       ||'       FROM  msc_units_of_measure muom'
39255       ||'       WHERE NVL(muom.uom_code,  '||''''||NULL_CHAR||''''||') = '
39256       ||'             NVL(msr.uom_code,   '||''''||NULL_CHAR||''''||')'
39257       ||'       UNION'
39258       ||'       SELECT 1 FROM msc_st_units_of_measure msuom'
39259       ||'       WHERE NVL(msuom.uom_code,  '||''''||NULL_CHAR||''''||') = '
39260       ||'             NVL(msr.uom_code,'||''''||NULL_CHAR||''''||')'
39261       ||'             AND msuom.process_flag = '||G_VALID
39262       ||'             AND msuom.sr_instance_id = :v_instance_id)'
39263       ||' AND NVL(msr.uom_code,'||''''||NULL_CHAR||''''||') '
39264       ||'         <> '||''''||NULL_CHAR||''''
39265       ||' AND msr.sr_instance_code = :v_instance_code'
39266       ||' AND msr.batch_id         = :lv_batch_id'
39267       ||' AND msr.process_flag     = '||G_IN_PROCESS ;
39268 
39269 
39270             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39271 
39272       EXECUTE IMMEDIATE lv_sql_stmt
39273               USING     v_instance_id,
39274                         v_instance_code,
39275                         lv_batch_id;
39276 
39277     -- Set the error message
39278     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39279                      (p_app_short_name    => 'MSC',
39280                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
39281                       p_message_text      => lv_message_text,
39282                       p_error_text        => lv_error_text,
39283                       p_token1            => 'COLUMN_NAME',
39284                       p_token_value1      => 'CTP_FLAG',
39285                       p_token2            => 'DEFAULT_VALUE',
39286                       p_token_value2      =>  G_CTP_FLAG );
39287 
39288       IF lv_return <> 0 THEN
39289         RAISE ex_logging_err;
39290       END IF;
39291 
39292    -- Defualt CTP FLAG  as 2(No) if NOT IN (1,2)
39293 
39294     lv_where_str := ' AND ctp_flag NOT IN (1,2)';
39295 
39296     lv_return := MSC_ST_UTIL.LOG_ERROR
39297                     (p_table_name       => 'MSC_ST_ROUTINGS',
39298                      p_instance_code     => v_instance_code,
39299                      p_row               => lv_column_names,
39300                      p_severity          => G_SEV_WARNING,
39301                      p_message_text      => lv_message_text,
39302                      p_error_text        => lv_error_text,
39303                      p_batch_id          => lv_batch_id,
39304                      p_where_str         => lv_where_str,
39305                      p_col_name          => 'CTP_FLAG',
39306                      p_debug             => v_debug,
39307                      p_default_value     => G_CTP_FLAG);
39308 
39309     IF lv_return<> 0 THEN
39310       RAISE ex_logging_err;
39311     END IF;
39312 
39313     -- Set the error message
39314     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39315                      (p_app_short_name    =>  'MSC',
39316                       p_error_code        =>  'MSC_PP_INVALID_COL_VALUE',
39317                       p_message_text      =>  lv_message_text,
39318                       p_error_text        =>  lv_error_text,
39319                       p_token1            =>  'COLUMN_NAME',
39320                       p_token_value1      =>  'CFM_ROUTING_FLAG',
39321                       p_token2            =>  'DEFAULT_VALUE',
39322                       p_token_value2      =>  G_CFM_ROUTING_FLAG );
39323 
39324       IF lv_return <> 0 THEN
39325         RAISE ex_logging_err;
39326       END IF;
39327 
39328      -- Defualt CFM flag as 2(No) if NOT IN (1,2,3)
39329      -- 3 added for Network Routing, now supported by OSFM integration.
39330 
39331      lv_where_str := ' AND cfm_routing_flag NOT IN (1,2,3)';
39332 
39333      lv_return := MSC_ST_UTIL.LOG_ERROR
39334                     (p_table_name        => 'MSC_ST_ROUTINGS',
39335                      p_instance_code     => v_instance_code,
39336                      p_row               => lv_column_names,
39337                      p_severity          => G_SEV_WARNING,
39338                      p_message_text      => lv_message_text,
39339                      p_error_text        => lv_error_text,
39340                      p_batch_id          => lv_batch_id,
39341                      p_where_str         => lv_where_str,
39342                      p_col_name          => 'CFM_ROUTING_FLAG',
39343                      p_debug             => v_debug,
39344                      p_default_value     => G_CFM_ROUTING_FLAG);
39345 
39346     IF lv_return<> 0 THEN
39347       RAISE ex_logging_err;
39348     END IF;
39349 
39350     -- Now we will check whether BOM Name is NULL , if it is NULL we will populate the
39351     -- ASSEMBLY NAME in BOM NAME column for all such records
39352 
39353     v_sql_stmt := 10;
39354     lv_sql_stmt :=
39355     'UPDATE msc_st_routings '
39356     ||' SET    bom_name = assembly_name'
39357     ||' WHERE  sr_instance_code = :v_instance_code'
39358     ||' AND    process_flag = '||G_IN_PROCESS
39359     ||' AND    NVL(bom_name,'||''''||NULL_CHAR||''''||') '
39360     ||'         = '||''''||NULL_CHAR||''''
39361     ||' AND    batch_id = :lv_batch_id ';
39362 
39363 
39364             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39365 
39366      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
39367 
39368 
39369     -- Set the error message
39370     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39371                      (p_app_short_name    => 'MSC',
39372                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
39373                       p_message_text      => lv_message_text,
39374                       p_error_text        => lv_error_text,
39375                       p_token1            => 'COLUMN_NAMES',
39376                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
39377                                              ||'ORGANIZATION_CODE,BOM_NAME',
39378                       p_token2            => 'MASTER_TABLE',
39379                       p_token_value2      =>  'MSC_ST_BOMS',
39380                       p_token3            =>  'CHILD_TABLE' ,
39381                       p_token_value3      =>  'MSC_ST_ROUTINGS' );
39382 
39383     -- Derive the bill_sequence_id
39384 
39385     lv_return :=
39386     MSC_ST_UTIL.DERIVE_BILL_SEQUENCE_ID
39387                       (p_table_name    => 'MSC_ST_ROUTINGS',
39388                       p_row            => lv_column_names,
39389                       p_bom_col_name   => 'BOM_NAME',
39390                       p_bom_col_id     => 'BILL_SEQUENCE_ID',
39391                       p_instance_code  =>  v_instance_code,
39392                       p_severity       =>  G_SEV_WARNING,
39393                       p_message_text   => lv_message_text,
39394                       p_batch_id       =>  lv_batch_id,
39395                       p_debug          => v_debug,
39396                       p_error_text     =>  lv_error_text);
39397 
39398     IF lv_return<> 0 THEN
39399       RAISE ex_logging_err;
39400     END IF;
39401 
39402    -- For OSFM Integration
39403    -- for Network Routings first_operation_seq_code or last_operation_seq_code cannot be null.
39404    -- Set the error message
39405     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39406                      (p_app_short_name    => 'MSC',
39407                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
39408                       p_message_text      => lv_message_text,
39409                       p_error_text        => lv_error_text,
39410                       p_token1            => 'COLUMN_NAME',
39411                       p_token_value1      => 'FIRST_OPERATION_SEQ_CODE OR '
39412                                            ||'LAST_OPERATION_SEQ_CODE'  );
39413       IF lv_return <> 0 THEN
39414         RAISE ex_logging_err;
39415       END IF;
39416 
39417      -- Error out the records whose first_operation_seq_code or last_operation_seq_code is NULL
39418 
39419     v_sql_stmt := 11;
39420     lv_sql_stmt :=
39421     'UPDATE    msc_st_routings'
39422     ||' SET    process_flag     ='||G_ERROR_FLG||','
39423     ||'         error_text   = '||''''||lv_message_text||''''
39424     ||' WHERE ( NVL(first_operation_seq_code,'||NULL_VALUE||') ='||NULL_VALUE
39425     ||' OR      NVL(last_operation_seq_code,'||NULL_VALUE||') ='||NULL_VALUE ||')'
39426     ||' AND    sr_instance_code  = :v_instance_code'
39427     ||' AND    process_flag      = '||G_IN_PROCESS
39428     ||' AND    cfm_routing_flag      = 3'
39429     ||' AND    batch_id          = :lv_batch_id';
39430 
39431 
39432          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39433 
39434    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
39435 
39436 
39437    -- For Network routings populate the FIRST_OP_SEQ_NUM and LAST_OP_SEQ_NUM
39438     v_sql_stmt := 13;
39439     lv_sql_stmt :=
39440     'UPDATE    msc_st_routings'
39441     ||' SET    first_op_seq_num     = to_number(decode(length(rtrim(first_operation_seq_code,''0123456789'')),'
39442     ||'        NULL,first_operation_seq_code,''1'')),'
39443     ||'        last_op_seq_num     = to_number(decode(length(rtrim(last_operation_seq_code,''0123456789'')),'
39444     ||'        NULL,last_operation_seq_code,''1''))'
39445     ||' WHERE    sr_instance_code  = :v_instance_code'
39446     ||' AND    process_flag      = '||G_IN_PROCESS
39447     ||' AND    cfm_routing_flag      = 3'
39448     ||' AND    batch_id          = :lv_batch_id';
39449 
39450 
39451          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39452 
39453    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
39454 
39455    --Call to customised validation.
39456 
39457     MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
39458         (ERRBUF         => lv_error_text,
39459          RETCODE        => lv_return,
39460          pBatchID       => lv_batch_id,
39461          pInstanceCode  => v_instance_code,
39462          pEntityName    => 'MSC_ST_ROUTINGS',
39463          pInstanceID    => v_instance_id);
39464 
39465     IF NVL(lv_return,0) <> 0 THEN
39466         RAISE ex_logging_err;
39467     END IF;
39468 
39469     -- Now generate routing sequence id
39470 
39471      v_sql_stmt := 15;
39472 
39473      OPEN c3(lv_batch_id);
39474      FETCH c3 BULK COLLECT INTO lb_rowid ;
39475 
39476      IF c3%ROWCOUNT > 0  THEN
39477 
39478     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
39479 
39480         UPDATE msc_st_routings
39481         SET    routing_sequence_id    = msc_st_routing_sequence_id_s.NEXTVAL
39482         WHERE  rowid                  = lb_rowid(j);
39483 
39484 
39485    v_sql_stmt := 16;
39486    FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
39487 
39488     -- Insert into the LID table  with the  user defined key
39489 
39490        INSERT INTO  msc_local_id_setup
39491        (local_id,
39492        st_transaction_id,
39493        instance_id,
39494        entity_name,
39495        data_source_type,
39496        char1,
39497        char2,
39498        char3,
39499        char4,
39500        char5,
39501        char6,
39502        number1,
39503        SOURCE_ORGANIZATION_ID,
39504        SOURCE_ASSEMBLY_ITEM_ID ,
39505        SOURCE_ROUTING_SEQUENCE_ID ,
39506        SOURCE_TASK_ID ,
39507        SOURCE_PROJECT_ID ,
39508        SOURCE_BILL_SEQUENCE_ID ,
39509        SOURCE_COMMON_ROUTING_SEQ_ID ,
39510        SOURCE_LINE_ID ,
39511        last_update_date,
39512        last_updated_by,
39513        creation_date,
39514        created_by  )
39515       SELECT
39516        routing_sequence_id,
39517        st_transaction_id,
39518        v_instance_id,
39519        'ROUTING_SEQUENCE_ID',
39520        data_source_type,
39521        v_instance_code,
39522        company_name,
39523        organization_code,
39524        routing_name,
39525        assembly_name,
39526        alternate_routing_designator,
39527        bill_sequence_id,
39528        SOURCE_ORGANIZATION_ID,
39529        SOURCE_ASSEMBLY_ITEM_ID ,
39530        SOURCE_ROUTING_SEQUENCE_ID ,
39531        SOURCE_TASK_ID ,
39532        SOURCE_PROJECT_ID ,
39533        SOURCE_BILL_SEQUENCE_ID ,
39534        SOURCE_COMMON_ROUTING_SEQ_ID ,
39535        SOURCE_LINE_ID ,
39536        v_current_date,
39537        v_current_user,
39538        v_current_date,
39539        v_current_user
39540       FROM msc_st_routings
39541      WHERE   rowid   =   lb_rowid(j);
39542 
39543      END IF;
39544      CLOSE c3;
39545 
39546      -- set the message
39547 
39548         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39549                      (p_app_short_name    => 'MSC',
39550                       p_error_code        => 'MSC_PP_INVALID_VALUE',
39551                       p_message_text      => lv_message_text,
39552                       p_error_text        => lv_error_text,
39553                       p_token1            => 'COLUMN_NAME',
39554                       p_token_value1      => 'COMMON_ROUTING_NAME');
39555 
39556       IF lv_return <> 0 THEN
39557         RAISE ex_logging_err;
39558       END IF;
39559 
39560       -- Derive the COMMON_ROUTING_SEQUENCE_ID from LOCAL ID table
39561 
39562       lv_where_str :=
39563       '   AND NVL(routing_name, '||''''||NULL_CHAR||''''||') <> '
39564       ||'    NVL(common_routing_name, '||''''||NULL_CHAR||''''||')'
39565       ||' AND     NVL(common_routing_name,  '||''''||NULL_CHAR||''''||')'
39566       ||'        <>               '||''''||NULL_CHAR||'''' ;
39567 
39568 
39569 
39570     lv_return := msc_st_util.derive_routing_sequence_id
39571                 (p_table_name     => 'MSC_ST_ROUTINGS',
39572                  p_rtg_col_name   => 'COMMON_ROUTING_NAME',
39573                  p_rtg_col_id     => 'COMMON_ROUTING_SEQUENCE_ID',
39574                  p_instance_code  => v_instance_code,
39575                  p_batch_id       => lv_batch_id,
39576                  p_debug          => v_debug,
39577                  p_error_text     => lv_error_text,
39578                  p_where_str      => lv_where_str);
39579 
39580     if (lv_return <> 0 )then
39581        MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_error_text);
39582     end if;
39583 
39584 
39585     lv_sql_stmt :=
39586     'UPDATE    msc_st_routings'
39587     ||' SET    process_flag     ='||G_ERROR_FLG||','
39588     ||'         error_text   = '||''''||lv_message_text||''''
39589     ||' WHERE  NVL(COMMON_ROUTING_SEQUENCE_ID,'||NULL_VALUE||') ='||NULL_VALUE
39590     ||' AND    NVL(routing_name, '||''''||NULL_CHAR||''''||') <> '
39591     ||'        NVL(common_routing_name, '||''''||NULL_CHAR||''''||')'
39592     ||' AND    NVL(common_routing_name,  '||''''||NULL_CHAR||''''||')'
39593     ||'        <>               '||''''||NULL_CHAR||''''
39594     ||' AND    sr_instance_code  = :v_instance_code'
39595     ||' AND    process_flag      = '||G_IN_PROCESS
39596     ||' AND    batch_id          = :lv_batch_id';
39597 
39598 
39599          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39600 
39601    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
39602 
39603 
39604      -- Set the process flag as Valid and poulate instance_id
39605     lv_return :=
39606     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_ROUTINGS',
39607                                p_instance_id    => v_instance_id,
39608                                p_instance_code  => v_instance_code,
39609                                p_process_flag   => G_VALID,
39610                                p_error_text     => lv_error_text,
39611                                p_debug          => v_debug,
39612                                p_batch_id       => lv_batch_id);
39613 
39614       IF lv_return <> 0 THEN
39615           RAISE ex_logging_err;
39616       END IF;
39617 
39618     -- At the end calling  the LOG_ERROR for logging all errored out records
39619 
39620    lv_return :=
39621    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_ROUTINGS',
39622                          p_instance_code => v_instance_code,
39623                          p_row           => lv_column_names,
39624                          p_severity      => G_SEV_ERROR,
39625                          p_message_text  => NULL,
39626                          p_error_text    => lv_error_text,
39627                          p_debug         => v_debug,
39628                          p_batch_id      => lv_batch_id  );
39629 
39630    IF(lv_return <> 0) then
39631        RAISE ex_logging_err;
39632    END IF;
39633 
39634    COMMIT;
39635    END LOOP;
39636 
39637 
39638 
39639   -- Validation for MSC_ST_ROUTING_OPERATIONS
39640 
39641 
39642    --Duplicate records check for the records whose source is XML
39643 
39644     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39645                  (p_app_short_name    => 'MSC',
39646                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
39647                   p_message_text      => lv_message_text,
39648                   p_error_text        => lv_error_text);
39649 
39650     IF lv_return <> 0 THEN
39651       RAISE ex_logging_err;
39652     END IF;
39653 
39654   v_sql_stmt := 18;
39655 
39656   lv_sql_stmt :=
39657   'UPDATE    msc_st_routing_operations msro1 '
39658   ||' SET       process_flag = '||G_ERROR_FLG||','
39659   ||'         error_text   = '||''''||lv_message_text||''''
39660   ||' WHERE     message_id < ( SELECT max(message_id) '
39661   ||'          FROM msc_st_routing_operations msro2'
39662   ||'          WHERE msro2.sr_instance_code   = msro1.sr_instance_code'
39663   ||'          AND   msro2.organization_code  = msro1.organization_code'
39664   ||'          AND   msro2.effectivity_date   = msro1.effectivity_date'
39665   ||'          AND   NVL(msro2.routing_name,'||''''||NULL_CHAR||''''||') '
39666   ||'                = NVL(msro1.routing_name, '||''''||NULL_CHAR||''''||') '
39667   ||'          AND   msro2.assembly_name      = msro1.assembly_name'
39668   ||'          AND   NVL(msro2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
39669   ||'                = NVL(msro1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
39670   ||'          AND   NVL(msro2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
39671   ||'                = NVL(msro1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
39672   ||'          AND   msro2.process_flag   ='||G_IN_PROCESS
39673   ||'          AND    NVL(msro2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
39674   ||' AND     msro1.process_flag = '||G_IN_PROCESS
39675   ||' AND     msro1.sr_instance_code = :v_instance_code'
39676   ||' AND     NVL(msro1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
39677 
39678 
39679          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39680 
39681   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
39682 
39683 
39684   --Duplicate records check for the records whose source is batch load
39685 
39686     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39687                  (p_app_short_name    => 'MSC',
39688                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
39689                   p_message_text      => lv_message_text,
39690                   p_error_text        => lv_error_text);
39691 
39692     IF lv_return <> 0 THEN
39693       RAISE ex_logging_err;
39694     END IF;
39695 
39696   v_sql_stmt := 19;
39697   lv_sql_stmt :=
39698   'UPDATE   msc_st_routing_operations msro1'
39699   ||' SET      process_flag = '||G_ERROR_FLG||','
39700   ||'         error_text   = '||''''||lv_message_text||''''
39701   ||' WHERE    EXISTS ( SELECT 1 '
39702   ||'          FROM msc_st_routing_operations msro2'
39703   ||'          WHERE msro2.sr_instance_code   = msro1.sr_instance_code'
39704   ||'          AND   msro2.organization_code  = msro1.organization_code'
39705   ||'          AND   msro2.effectivity_date   = msro1.effectivity_date'
39706   ||'          AND   NVL(msro2.routing_name,'||''''||NULL_CHAR||''''||') '
39707   ||'                = NVL(msro1.routing_name, '||''''||NULL_CHAR||''''||') '
39708   ||'          AND   msro2.assembly_name      = msro1.assembly_name'
39709   ||'          AND   NVL(msro2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
39710   ||'                = NVL(msro1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
39711   ||'          AND   NVL(msro2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
39712   ||'                = NVL(msro1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
39713   ||'          AND   msro2.process_flag   ='||G_IN_PROCESS
39714   ||'               AND NVL(msro2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
39715   ||'         GROUP BY sr_instance_code,company_name,organization_code,routing_name,'
39716   ||'         assembly_name, alternate_routing_designator,operation_seq_code,effectivity_date'
39717   ||'          HAVING COUNT(*) >1 )'
39718   ||'  AND     msro1.process_flag          = '||G_IN_PROCESS
39719   ||'  AND     msro1.sr_instance_code       = :v_instance_code'
39720   ||'  AND     NVL(msro1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
39721 
39722 
39723          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39724 
39725   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
39726 
39727 
39728   lv_column_names :=
39729   'ROUTING_NAME                   ||''~''||'
39730   ||' ASSEMBLY_NAME               ||''~''||'
39731   ||' ALTERNATE_ROUTING_DESIGNATOR ||''~''||'
39732   ||' OPERATION_SEQ_CODE          ||''~''||'
39733   ||' EFFECTIVITY_DATE            ||''~''||'
39734   ||' OPTION_DEPENDENT_FLAG       ||''~''||'
39735   ||' DEPARTMENT_CODE             ||''~''||'
39736   ||' ORGANIZATION_CODE           ||''~''||'
39737   ||' SR_INSTANCE_CODE            ||''~''||'
39738   ||' OPERATION_DESCRIPTION       ||''~''||'
39739   ||' DISABLE_DATE                ||''~''||'
39740   ||' FROM_UNIT_NUMBER            ||''~''||'
39741   ||' TO_UNIT_NUMBER              ||''~''||'
39742   ||' OPERATION_TYPE              ||''~''||'
39743   ||' MINIMUM_TRANSFER_QUANTITY   ||''~''||'
39744   ||' YIELD                       ||''~''||'
39745   ||' OPERATION_LEAD_TIME_PERCENT ||''~''||'
39746   ||' CUMULATIVE_YIELD            ||''~''||'
39747   ||' REVERSE_CUMULATIVE_YIELD    ||''~''||'
39748   ||' NET_PLANNING_PERCENT        ||''~''||'
39749   ||' SETUP_DURATION              ||''~''||'
39750   ||' TEAR_DOWN_DURATION          ||''~''||'
39751   ||' UOM_CODE                    ||''~''||'
39752   ||' STANDARD_OPERATION_CODE     ||''~''||'
39753   ||' DELETED_FLAG                ||''~''||'
39754   ||' COMPANY_NAME ';
39755 
39756 
39757 
39758     LOOP
39759 
39760       v_sql_stmt := 20;
39761       lv_cursor_stmt :=
39762       ' SELECT msc_st_batch_id_s.NEXTVAL '
39763       ||' FROM   dual';
39764 
39765       OPEN  c1 FOR lv_cursor_stmt;
39766       FETCH c1 INTO lv_batch_id;
39767       CLOSE c1;
39768 
39769 
39770       SELECT msc_st_batch_id_s.NEXTVAL
39771       INTO   lv_batch_id
39772       FROM   dual;
39773 
39774       v_sql_stmt := 21;
39775       lv_sql_stmt :=
39776       ' UPDATE msc_st_routing_operations '
39777       ||' SET    batch_id  = :lv_batch_id'
39778       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
39779       ||' AND    sr_instance_code               = :v_instance_code'
39780       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
39781       ||' AND    rownum                        <= '||v_batch_size;
39782 
39783 
39784          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39785 
39786       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
39787 
39788       EXIT WHEN SQL%NOTFOUND;
39789 
39790 
39791       OPEN  c4(lv_batch_id);
39792       FETCH c4 BULK COLLECT INTO lb_rowid;
39793       CLOSE c4;
39794 
39795       v_sql_stmt := 22;
39796       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
39797       UPDATE msc_st_routing_operations
39798       SET  st_transaction_id = msc_st_routing_operations_s.NEXTVAL,
39799            refresh_id        = v_refresh_id,
39800            last_update_date  = v_current_date,
39801            last_updated_by   = v_current_user,
39802            creation_date     = v_current_date,
39803            created_by        = v_current_user
39804       WHERE  rowid           = lb_rowid(j);
39805 
39806     -- Set the error message
39807     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39808                      (p_app_short_name    => 'MSC',
39809                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
39810                       p_message_text      => lv_message_text,
39811                       p_error_text        => lv_error_text,
39812                       p_token1            => 'COLUMN_NAME',
39813                       p_token_value1      => 'DELETED_FLAG',
39814                       p_token2            => 'DEFAULT_VALUE',
39815                       p_token_value2      =>  SYS_NO  );
39816 
39817       IF lv_return <> 0 THEN
39818         RAISE ex_logging_err;
39819       END IF;
39820 
39821 
39822      -- Check whether delete flag value is in (1,2),if not default to SYS_NO
39823 
39824    lv_where_str :=
39825    ' AND NVL(deleted_flag,'||NULL_VALUE||') <>'||SYS_NO;
39826 
39827      lv_return := MSC_ST_UTIL.LOG_ERROR
39828                     (p_table_name        => 'MSC_ST_ROUTING_OPERATIONS',
39829                      p_instance_code     => v_instance_code,
39830                      p_row               => lv_column_names,
39831                      p_severity          => G_SEV_WARNING,
39832                      p_message_text      => lv_message_text,
39833                      p_error_text        => lv_error_text,
39834                      p_batch_id          => lv_batch_id,
39835                      p_where_str         => lv_where_str,
39836                      p_col_name          => 'DELETED_FLAG',
39837                      p_debug             => v_debug,
39838                      p_default_value     => SYS_NO);
39839 
39840       IF lv_return <> 0 THEN
39841         RAISE ex_logging_err;
39842       END IF;
39843 
39844     -- Set the  message
39845 
39846      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39847                      (p_app_short_name    => 'MSC',
39848                       p_error_code        => 'MSC_PP_INVALID_VALUE',
39849                       p_message_text      => lv_message_text,
39850                       p_error_text        => lv_error_text,
39851                       p_token1            => 'COLUMN_NAME',
39852                       p_token_value1      => 'ORGANIZATION_CODE');
39853 
39854       IF lv_return <> 0 THEN
39855         RAISE ex_logging_err;
39856       END IF;
39857 
39858     -- derive  organization id ,
39859      lv_return :=
39860      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_ROUTING_OPERATIONS',
39861                                     p_org_partner_name  =>'ORGANIZATION_CODE',
39862                                     p_org_partner_id    =>'ORGANIZATION_ID',
39863                                     p_instance_code     => v_instance_code,
39864                                     p_partner_type      => G_ORGANIZATION,
39865                                     p_severity          => G_SEV_ERROR,
39866                                     p_message_text      => lv_message_text,
39867                                     p_batch_id          =>lv_batch_id,
39868                                     p_debug             => v_debug,
39869                                     p_error_text        =>lv_error_text);
39870 
39871      if(lv_return <> 0) then
39872         RAISE ex_logging_err;
39873      end if;
39874 
39875      -- Now we will check whether Routing Name is NULL , if it is NULL we will populate the
39876      -- ASSEMBLY NAME in ROUTING_NAME column for all such records
39877     v_sql_stmt := 23;
39878 
39879     lv_sql_stmt :=
39880     'UPDATE msc_st_routing_operations '
39881     ||' SET    routing_name = assembly_name'
39882     ||' WHERE  sr_instance_code = :v_instance_code'
39883     ||' AND    process_flag     = '||G_IN_PROCESS
39884     ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||') '
39885     ||'         = '||''''||NULL_CHAR||''''
39886     ||' AND    batch_id         = :lv_batch_id';
39887 
39888 
39889          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
39890 
39891       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
39892 
39893     -- Set the error message
39894     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39895                      (p_app_short_name    => 'MSC',
39896                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
39897                       p_message_text      => lv_message_text,
39898                       p_error_text        => lv_error_text,
39899                       p_token1            => 'COLUMN_NAMES',
39900                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
39901                                              ||'ORGANIZATION_CODE,ROUTING_NAME',
39902                       p_token2            => 'MASTER_TABLE',
39903                       p_token_value2      =>  'MSC_ST_ROUTINGS',
39904                       p_token3            =>  'CHILD_TABLE' ,
39905                       p_token_value3      =>  'MSC_ST_ROUTING_OPERATIONS' );
39906 
39907       IF lv_return <> 0 THEN
39908         RAISE ex_logging_err;
39909       END IF;
39910 
39911     -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
39912 
39913     lv_return :=
39914     MSC_ST_UTIL.DERIVE_ROUTING_SEQUENCE_ID
39915                       (p_table_name     => 'MSC_ST_ROUTING_OPERATIONS',
39916                        p_rtg_col_name   => 'ROUTING_NAME',
39917                        p_rtg_col_id     => 'ROUTING_SEQUENCE_ID',
39918                        p_instance_code  => v_instance_code,
39919                        p_severity       => G_SEV_ERROR,
39920                        p_message_text   => lv_message_text,
39921                        p_batch_id       => lv_batch_id,
39922                        p_debug          => v_debug,
39923                        p_error_text     => lv_error_text);
39924 
39925     if (lv_return <> 0 )then
39926        RAISE ex_logging_err;
39927     end if;
39928 
39929     -- Set the error message
39930     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39931                      (p_app_short_name    => 'MSC',
39932                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
39933                       p_message_text      => lv_message_text,
39934                       p_error_text        => lv_error_text,
39935                       p_token1            => 'COLUMN_NAME',
39936                       p_token_value1      => 'EFFECTIVITY_DATE',
39937                       p_token2            => 'DEFAULT_VALUE',
39938                       p_token_value2      => 'SYSDATE' );
39939 
39940       IF lv_return <> 0 THEN
39941         RAISE ex_logging_err;
39942       END IF;
39943 
39944     -- Default effectivity date as SYSDATE if NULL
39945 
39946      lv_where_str :=
39947      ' AND NVL(effectivity_date,SYSDATE-36500 ) = SYSDATE-36500 ' ;
39948 
39949      lv_return := MSC_ST_UTIL.LOG_ERROR
39950                     (p_table_name        => 'MSC_ST_ROUTING_OPERATIONS',
39951                      p_instance_code     => v_instance_code,
39952                      p_row               => lv_column_names,
39953                      p_severity          => G_SEV_WARNING,
39954                      p_message_text      => lv_message_text,
39955                      p_error_text        => lv_error_text,
39956                      p_batch_id          => lv_batch_id,
39957                      p_where_str         => lv_where_str,
39958                      p_col_name          => 'EFFECTIVITY_DATE',
39959                      p_debug             => v_debug,
39960                      p_default_value     => 'SYSDATE');
39961 
39962       IF lv_return <> 0 THEN
39963         RAISE ex_logging_err;
39964       END IF;
39965 
39966     -- Set the error message
39967     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
39968                      (p_app_short_name    => 'MSC',
39969                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
39970                       p_message_text      => lv_message_text,
39971                       p_error_text        => lv_error_text,
39972                       p_token1            => 'COLUMN_NAME',
39973                       p_token_value1      => 'OPERATION_SEQ_CODE',
39974                       p_token2            => 'DEFAULT_VALUE',
39975                       p_token_value2      =>  G_OPERATION_SEQ_CODE  );
39976 
39977       IF lv_return <> 0 THEN
39978         RAISE ex_logging_err;
39979       END IF;
39980 
39981      -- Default operation_seq_code as 1 if NULL
39982      lv_where_str :=
39983      '    AND NVL(operation_seq_code,'||''''||NULL_CHAR||''''||') '
39984      ||'         = '||''''||NULL_CHAR||'''' ;
39985 
39986      lv_return := MSC_ST_UTIL.LOG_ERROR
39987                     (p_table_name        => 'MSC_ST_ROUTING_OPERATIONS',
39988                      p_instance_code     => v_instance_code,
39989                      p_row               => lv_column_names,
39990                      p_severity          => G_SEV_WARNING,
39991                      p_message_text      => lv_message_text,
39992                      p_error_text        => lv_error_text,
39993                      p_batch_id          => lv_batch_id,
39994                      p_where_str         => lv_where_str,
39995                      p_col_name          => 'OPERATION_SEQ_CODE',
39996                      p_debug             => v_debug,
39997                      p_default_value     => G_OPERATION_SEQ_CODE);
39998 
39999       IF lv_return <> 0 THEN
40000         RAISE ex_logging_err;
40001       END IF;
40002 
40003    -- Derive operation_sequence id
40004 
40005     v_sql_stmt := 24;
40006     lv_sql_Stmt :=
40007     'UPDATE     msc_st_routing_operations msro'
40008     ||' SET     operation_sequence_id=  (SELECT local_id'
40009     ||'         FROM msc_local_id_setup mlis'
40010     ||'         WHERE  mlis.char1 = msro.sr_instance_code'
40011     ||'         AND     NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
40012     ||'          =    NVL(msro.company_name,'||''''||NULL_CHAR||''''||') '
40013     ||'         AND    mlis.char3 = msro.organization_code'
40014     ||'         AND    mlis.char4 = msro.routing_name'
40015     ||'         AND    mlis.char5 = msro.operation_seq_code'
40016     ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
40017     ||'           =   NVL(msro.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
40018     ||'         AND    mlis.date1 = msro.effectivity_date'
40019     ||'         AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
40020     ||'         AND    mlis.instance_id = '||v_instance_id||')'
40021     ||'  WHERE      sr_instance_code = :v_instance_code'
40022     ||'  AND        process_flag     ='||G_IN_PROCESS
40023     ||'  AND        batch_id         = :lv_batch_id';
40024 
40025 
40026          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40027 
40028    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40029 
40030   -- derive the operation_seq_num column from LID
40031   -- where operation_sequence_id exist
40032 
40033   v_sql_stmt  := 25;
40034 
40035   lv_sql_stmt :=
40036   'UPDATE       msc_st_routing_operations msro'
40037   ||' SET       operation_seq_num =  ( SELECT number1'
40038   ||'           FROM msc_local_id_setup mlis'
40039   ||'           WHERE  local_id = msro.operation_sequence_id'
40040   ||'           AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
40041   ||'           AND    mlis.instance_id = '||v_instance_id||')'
40042   ||' WHERE     sr_instance_code = :v_instance_code'
40043   ||' AND       NVL(operation_sequence_id,'||NULL_VALUE||') <>'||NULL_VALUE
40044   ||' AND       process_flag     = '||G_IN_PROCESS
40045   ||' AND       batch_id         = :lv_batch_id';
40046 
40047 
40048          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40049 
40050    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40051 
40052     -- Set the error message
40053     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40054                      (p_app_short_name    => 'MSC',
40055                       p_error_code        => 'MSC_PP_DELETE_FAIL',
40056                       p_message_text      => lv_message_text,
40057                       p_error_text        => lv_error_text );
40058 
40059       IF lv_return <> 0 THEN
40060         RAISE ex_logging_err;
40061       END IF;
40062 
40063    -- Error out if DELETED_FLAG = SYS_YES but operation_sequence_id is NULL
40064 
40065     v_sql_stmt := 26;
40066     lv_sql_stmt :=
40067     'UPDATE    msc_st_routing_operations'
40068     ||' SET    process_flag     ='||G_ERROR_FLG||','
40069     ||'         error_text   = '||''''||lv_message_text||''''
40070     ||' WHERE  NVL(operation_sequence_id,'||NULL_VALUE||') ='||NULL_VALUE
40071     ||' AND    deleted_flag      ='||SYS_YES
40072     ||' AND    sr_instance_code  = :v_instance_code'
40073     ||' AND    process_flag      = '||G_IN_PROCESS
40074     ||' AND    batch_id          = :lv_batch_id';
40075 
40076 
40077          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40078 
40079    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40080 
40081     -- Set the error message
40082 
40083       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40084                      (p_app_short_name    => 'MSC',
40085                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
40086                       p_message_text      => lv_message_text,
40087                       p_error_text        => lv_error_text,
40088                       p_token1            => 'COLUMN_NAME',
40089                       p_token_value1      => 'DEPARTMENT_CODE');
40090 
40091       IF lv_return <> 0 THEN
40092         RAISE ex_logging_err;
40093       END IF;
40094 
40095     -- Error out record if department_code is null/resource_code is NULL and line_flag=2
40096 
40097       v_sql_stmt := 26.1;
40098      lv_sql_stmt :=
40099       ' UPDATE  msc_st_routing_operations '
40100       ||' SET   process_flag        = '||G_ERROR_FLG||','
40101       ||'         error_text   = '||''''||lv_message_text||''''
40102       ||' WHERE NVL(department_code,'||''''||NULL_CHAR||''''||')'
40103       ||'       = '||''''||NULL_CHAR||''''
40104       ||' AND   batch_id         = :lv_batch_id'
40105       ||' AND   process_flag     = '||G_IN_PROCESS
40106       ||' AND   sr_instance_code = :v_instance_code';
40107 
40108 
40109         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40110       EXECUTE IMMEDIATE lv_sql_stmt
40111               USING     lv_batch_id,
40112                         v_instance_code;
40113 
40114     -- Derive department id
40115     v_sql_stmt := 27;
40116     lv_sql_stmt :=
40117     'UPDATE     msc_st_routing_operations msro'
40118     ||' SET     department_id=    (SELECT local_id'
40119     ||'         FROM msc_local_id_setup mlis'
40120     ||'         WHERE  mlis.char1 = msro.sr_instance_code'
40121     ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
40122     ||'         =    NVL(msro.company_name,'||''''||NULL_CHAR||''''||') '
40123     ||'         AND    mlis.char3 = msro.organization_code'
40124     ||'         AND    mlis.char4 = msro.department_code'
40125     ||'         AND    mlis.entity_name = ''DEPARTMENT_ID'')'
40126     ||' WHERE      sr_instance_code = :v_instance_code'
40127     ||' AND        deleted_flag     = '||SYS_NO
40128     ||' AND        process_flag     = '||G_IN_PROCESS
40129     ||' AND        batch_id         = :lv_batch_id';
40130 
40131 
40132          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40133 
40134       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40135 
40136 
40137 
40138       -- Now generate the department_id
40139 
40140      v_sql_stmt := 27.1;
40141 
40142      OPEN c10(lv_batch_id);
40143      FETCH c10 BULK COLLECT INTO lb_rowid ;
40144 
40145      IF c10%ROWCOUNT > 0  THEN
40146 
40147     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
40148 
40149         UPDATE msc_st_routing_operations
40150         SET    department_id          = msc_st_department_id_s.NEXTVAL
40151         WHERE  rowid                  = lb_rowid(j);
40152 
40153 
40154     v_sql_stmt := 14;
40155 
40156     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
40157 
40158    -- Insert into the LID table this new department with the  user defined key
40159 
40160        INSERT INTO  msc_local_id_setup
40161        (local_id,
40162        st_transaction_id,
40163        instance_id,
40164        entity_name,
40165        data_source_type,
40166        char1,
40167        char2,
40168        char3,
40169        char4,
40170        SOURCE_ORGANIZATION_ID,
40171        SOURCE_DEPARTMENT_ID,
40172        last_update_date,
40173        last_updated_by,
40174        creation_date,
40175        created_by  )
40176       SELECT
40177        department_id,
40178        st_transaction_id,
40179        v_instance_id,
40180        'DEPARTMENT_ID',
40181        data_source_type,
40182        sr_instance_code,
40183        company_name,
40184        organization_code,
40185        department_code,
40186        SOURCE_ORGANIZATION_ID,
40187        SOURCE_DEPARTMENT_ID,
40188        v_current_date,
40189        v_current_user,
40190        v_current_date,
40191        v_current_user
40192       FROM msc_st_routing_operations
40193       WHERE rowid = lb_rowid(j);
40194 
40195    END IF;
40196    CLOSE c10;
40197 
40198     -- Now derive department id
40199      v_sql_stmt:= 15;
40200 
40201     lv_sql_stmt :=
40202     'UPDATE     msc_st_routing_operations msdr'
40203     ||' SET     department_id=  (SELECT local_id'
40204     ||'                        FROM msc_local_id_setup mlis'
40205     ||'                        WHERE  mlis.char1 = msdr.sr_instance_code'
40206     ||'                        AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
40207     ||'                        =    NVL(msdr.company_name,'||''''||NULL_CHAR||''''||') '
40208     ||'                        AND    mlis.char3 = msdr.organization_code'
40209     ||'                        AND    mlis.char4 = msdr.department_code'
40210     ||'                        AND    mlis.entity_name = ''DEPARTMENT_ID'')'
40211     ||' WHERE      msdr.sr_instance_code = :v_instance_code'
40212     ||' AND        NVL(msdr.department_id,'||NULL_VALUE||') ='|| NULL_VALUE
40213     ||' AND        msdr.process_flag     ='|| G_IN_PROCESS
40214     ||' AND        msdr.batch_id         = :lv_batch_id';
40215 
40216 
40217             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40218 
40219     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40220 
40221 
40222    /* Bug 3549086 -- we shouldn't be erroing out if department doen't exists.
40223       ERP allows such records. Hence we would genearet department it.
40224     -- Set the error message
40225     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40226                      (p_app_short_name    => 'MSC',
40227                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
40228                       p_message_text      => lv_message_text,
40229                       p_error_text        => lv_error_text,
40230                       p_token1            => 'COLUMN_NAMES',
40231                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
40232                                              ||'ORGANIZATION_CODE,DEPARTMENT_CODE',
40233                       p_token2            => 'MASTER_TABLE',
40234                       p_token_value2      => 'MSC_ST_DEPARTMENT_RESOURCES',
40235                       p_token3            =>  'CHILD_TABLE' ,
40236                       p_token_value3      =>  'MSC_ST_ROUTING_OPERATIONS' );
40237 
40238       IF lv_return <> 0 THEN
40239         RAISE ex_logging_err;
40240       END IF;
40241 
40242    -- Log an error if unable to derive department id
40243 
40244    v_sql_stmt := 28;
40245    lv_sql_stmt :=
40246    'UPDATE      msc_st_routing_operations '
40247    ||' SET      process_flag ='||G_ERROR_FLG||','
40248    ||'          error_text   = '||''''||lv_message_text||''''
40249    ||' WHERE    NVL(department_id,'||NULL_VALUE||') ='||NULL_VALUE
40250    ||' AND      sr_instance_code    = :v_instance_code'
40251    ||' AND      deleted_flag        ='||SYS_NO
40252    ||' AND      process_flag        ='|| G_IN_PROCESS
40253    ||' AND      batch_id            = :lv_batch_id' ;
40254 
40255 
40256          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40257 
40258    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40259    */
40260 
40261     -- Set the error message
40262     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40263                      (p_app_short_name    => 'MSC',
40264                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
40265                       p_message_text      => lv_message_text,
40266                       p_error_text        => lv_error_text,
40267                       p_token1            => 'COLUMN_NAME',
40268                       p_token_value1      => 'OPTION_DEPENDENT_FLAG',
40269                       p_token2            => 'DEFAULT_VALUE',
40270                       p_token_value2      =>  G_OPTION_DEPENDENT_FLAG  );
40271 
40272       IF lv_return <> 0 THEN
40273         RAISE ex_logging_err;
40274       END IF;
40275 
40276     -- Default option_dependent_flag
40277 
40278     lv_where_str := ' AND NVL(option_dependent_flag,'||NULL_VALUE||')'
40279                       ||' NOT IN (1,2) AND deleted_flag = '||SYS_NO ;
40280 
40281     lv_return := MSC_ST_UTIL.LOG_ERROR
40282                     (p_table_name        => 'MSC_ST_ROUTING_OPERATIONS',
40283                      p_instance_code     => v_instance_code,
40284                      p_row               => lv_column_names,
40285                      p_severity          => G_SEV_WARNING,
40286                      p_message_text      => lv_message_text,
40287                      p_error_text        => lv_error_text,
40288                      p_batch_id          => lv_batch_id,
40289                      p_where_str         => lv_where_str,
40290                      p_col_name          => 'OPTION_DEPENDENT_FLAG',
40291                      p_debug             => v_debug,
40292                      p_default_value     => G_OPTION_DEPENDENT_FLAG);
40293 
40294       IF lv_return <> 0 THEN
40295         RAISE ex_logging_err ;
40296       END IF;
40297 
40298     --Call to customised validation.
40299 
40300       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
40301         (ERRBUF         => lv_error_text,
40302          RETCODE        => lv_return,
40303          pBatchID       => lv_batch_id,
40304          pInstanceCode  => v_instance_code,
40305          pEntityName    => 'MSC_ST_ROUTING_OPERATIONS',
40306          pInstanceID    => v_instance_id);
40307 
40308       IF NVL(lv_return,0) <> 0 THEN
40309         RAISE ex_logging_err;
40310       END IF;
40311 
40312    -- Generate operation_sequence_id
40313    -- Also update operation_seq_num as operation_seq_code,
40314    -- here we assume that  operation_seq_code is a number
40315     -- field,otherwise we will default it to 1
40316 
40317      v_sql_stmt := 29;
40318 
40319      OPEN c5(lv_batch_id);
40320      FETCH c5 BULK COLLECT INTO lb_rowid ;
40321 
40322      IF c5%ROWCOUNT > 0  THEN
40323 
40324     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
40325 
40326         UPDATE msc_st_routing_operations
40327         SET    operation_sequence_id  = msc_st_operation_sequence_id_s.NEXTVAL ,
40328                operation_seq_num      =
40329                to_number(decode(length(rtrim(operation_seq_code,'0123456789')),
40330                          NULL,operation_seq_code,'1'))
40331         WHERE  rowid                  = lb_rowid(j);
40332 
40333     v_sql_stmt := 30;
40334 
40335    FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
40336   -- Insert into the LID table  with the  user defined key
40337 
40338        INSERT INTO  msc_local_id_setup
40339        (local_id,
40340        st_transaction_id,
40341        instance_id,
40342        entity_name,
40343        data_source_type,
40344        char1,
40345        char2,
40346        char3,
40347        char4,
40348        char5,
40349        char6,
40350        number1,
40351        date1,
40352        date2,
40353        SOURCE_ORGANIZATION_ID ,
40354        SOURCE_OPERATION_SEQUENCE_ID ,
40355        SOURCE_OPERATION_SEQ_NUM ,
40356        SOURCE_DEPARTMENT_ID ,
40357        last_update_date,
40358        last_updated_by,
40359        creation_date,
40360        created_by  )
40361       SELECT
40362        operation_sequence_id,
40363        st_transaction_id,
40364        v_instance_id,
40365        'OPERATION_SEQUENCE_ID',
40366        data_source_type,
40367        v_instance_code,
40368        company_name,
40369        organization_code,
40370        routing_name,
40371        operation_seq_code,
40372        alternate_routing_designator,
40373        operation_seq_num,
40374        effectivity_date,
40375        disable_date,
40376        SOURCE_ORGANIZATION_ID ,
40377        SOURCE_OPERATION_SEQUENCE_ID ,
40378        SOURCE_OPERATION_SEQ_NUM ,
40379        SOURCE_DEPARTMENT_ID ,
40380        v_current_date,
40381        v_current_user,
40382        v_current_date,
40383        v_current_user
40384        FROM msc_st_routing_operations
40385       WHERE rowid = lb_rowid(j) ;
40386 
40387     END IF;
40388   CLOSE c5;
40389 
40390    -- Insert records in MSC_ST_OPERATION_COMPONENT
40391    v_sql_stmt  := 31;
40392    lv_sql_stmt :=
40393    'INSERT INTO msc_st_operation_components '
40394    ||' (organization_id,'
40395    ||' operation_sequence_id,'
40396    ||' component_sequence_id,'
40397    ||' bill_sequence_id,'
40398    ||' routing_sequence_id,'
40399    ||' deleted_flag,'
40400    ||' sr_instance_id,'
40401    ||' last_update_date,'
40402    ||' last_updated_by, '
40403    ||' creation_date,'
40404    ||' created_by,'
40405    ||' refresh_id)'
40406    ||' SELECT'
40407    ||' msro.organization_id,'
40408    ||' msro.operation_sequence_id,'
40409    ||' msbc.component_sequence_id,'
40410    ||' msbc.bill_sequence_id,'
40411    ||' msro.routing_sequence_id,'
40412    ||' msro.deleted_flag,'
40413    ||  v_instance_id||','
40414    ||' msro.last_update_date,'
40415    ||' msro.last_updated_by,'
40416    ||' msro.creation_date,'
40417    ||' msro.created_by,'
40418    ||' msro.REFRESH_ID'
40419    ||' FROM msc_st_routing_operations msro,'
40420    ||'      msc_st_bom_components msbc,'
40421    ||'      msc_local_id_setup mlis'
40422    ||' WHERE msbc.bill_sequence_id      = mlis.number1'
40423    ||' AND   mlis.entity_name           = ''ROUTING_SEQUENCE_ID'' '
40424    ||' AND   mlis.local_id              = msro.routing_sequence_id'
40425    ||' AND   msbc.process_flag          = '||G_VALID
40426    ||' AND   mlis.instance_id           = :v_instance_id'
40427    ||' AND   msro.sr_instance_code      =  :v_instance_code'
40428    ||' AND   msro.operation_seq_code    =  msbc.operation_seq_code'
40429    ||' AND   msro.process_flag          ='||G_IN_PROCESS
40430    ||' AND   msro.batch_id              = :lv_batch_id'
40431    ||' UNION'
40432    ||' SELECT'
40433    ||' msro.organization_id,'
40434    ||' msro.operation_sequence_id,'
40435    ||' mbc.component_sequence_id,'
40436    ||' mbc.bill_sequence_id,'
40437    ||' msro.routing_sequence_id,'
40438    ||' msro.deleted_flag,'
40439    ||  v_instance_id||','
40440    ||' msro.last_update_date,'
40441    ||' msro.last_updated_by,'
40442    ||' msro.creation_date,'
40443    ||' msro.created_by,'
40444    ||' msro.REFRESH_ID'
40445    ||' FROM msc_st_routing_operations msro,'
40446    ||'      msc_bom_components mbc,'
40447    ||'      msc_local_id_setup mlis,'
40448    ||'      msc_local_id_setup mlis1'
40449    ||' WHERE mbc.component_sequence_id  = mlis1.local_id '
40450    ||' AND   mbc.bill_sequence_id       = mlis.number1'
40451    ||' AND   mbc.sr_instance_id         = :v_instance_id'
40452    ||' AND   mbc.plan_id                = -1'
40453    ||' AND   mlis.entity_name           = ''ROUTING_SEQUENCE_ID'' '
40454    ||' AND   mlis.local_id              = msro.routing_sequence_id '
40455    ||' AND   mlis.instance_id           = :v_instance_id '
40456    ||' AND   msro.sr_instance_code      = :v_instance_code'
40457    ||' AND   msro.organization_id       = mbc.organization_id'
40458    ||' AND   msro.operation_seq_code    = mlis1.char7'
40459    ||' AND   mlis1.instance_id          = :v_instance_id'
40460    ||' AND   mlis1.entity_name          = ''COMPONENT_SEQUENCE_ID'' '
40461    ||' AND   msro.process_flag          ='||G_IN_PROCESS
40462    ||' AND   msro.batch_id              = :lv_batch_id';
40463 
40464          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40465 
40466   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_id,v_instance_code,lv_batch_id,
40467         v_instance_id,v_instance_id,v_instance_code,v_instance_id,lv_batch_id;
40468 
40469     -- Set the process flag as Valid and derive sr_instance_id
40470     lv_return :=
40471     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_ROUTING_OPERATIONS',
40472                                p_instance_id    => v_instance_id,
40473                                p_instance_code  => v_instance_code,
40474                                p_process_flag   => G_VALID,
40475                                p_error_text     => lv_error_text,
40476                                p_debug          => v_debug,
40477                                p_batch_id       => lv_batch_id);
40478 
40479       IF lv_return < 0 THEN
40480         RAISe ex_logging_err;
40481       END IF;
40482 
40483 
40484     -- At the end calling  the LOG_ERROR for logging all errored out records
40485 
40486    lv_return :=
40487    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_ROUTING_OPERATIONS',
40488                          p_instance_code => v_instance_code,
40489                          p_row           => lv_column_names,
40490                          p_severity      => G_SEV_ERROR,
40491                          p_message_text  => NULL,
40492                          p_error_text    => lv_error_text,
40493                          p_debug         => v_debug,
40494                          p_batch_id      => lv_batch_id  );
40495 
40496     IF lv_return < 0 THEN
40497         RAISe ex_logging_err;
40498     END IF;
40499 
40500    COMMIT;
40501  END LOOP;
40502 
40503    -- Validation for the MSC_ST_OPERATION_RESOURCES
40504 
40505    --Duplicate records check for the records whose source is XML
40506 
40507     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40508                  (p_app_short_name    => 'MSC',
40509                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
40510                   p_message_text      => lv_message_text,
40511                   p_error_text        => lv_error_text);
40512 
40513     IF lv_return <> 0 THEN
40514       RAISE ex_logging_err;
40515     END IF;
40516 
40517 
40518   v_sql_stmt := 32;
40519 
40520   lv_sql_stmt:=
40521   'UPDATE    msc_st_operation_resources msor1 '
40522   ||'  SET   process_flag = '||G_ERROR_FLG||','
40523   ||'         error_text   = '||''''||lv_message_text||''''
40524   ||' WHERE  message_id < ( SELECT max(message_id)'
40525   ||'        FROM msc_st_operation_resources msor2'
40526   ||'          WHERE msor2.sr_instance_code   = msor1.sr_instance_code'
40527   ||'          AND   msor2.organization_code  = msor1.organization_code'
40528   ||'          AND   NVL(msor2.company_name,'||''''||NULL_CHAR||''''||') '
40529   ||'                 =    NVL(msor1.company_name,'||''''||NULL_CHAR||''''||') '
40530   ||'          AND   NVL(msor2.routing_name,'||''''||NULL_CHAR||''''||') '
40531   ||'                 = NVL(msor1.routing_name,'||''''||NULL_CHAR||''''||') '
40532   ||'          AND   msor2.assembly_name      = msor1.assembly_name'
40533   ||'          AND   NVL(msor2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
40534   ||'                = NVL(msor1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
40535   ||'          AND   NVL(msor2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
40536   ||'                = NVL(msor1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
40537   ||'          AND   msor2.resource_code      = msor1.resource_code'
40538   ||'          AND   NVL(msor2.resource_seq_code,'||''''||NULL_CHAR||''''||') '
40539   ||'               = NVL(msor1.resource_seq_code,'||''''||NULL_CHAR||''''||') '
40540   ||'          AND   NVL(msor2.alternate_number,'||NULL_VALUE||')'
40541   ||'               =  NVL(msor1.alternate_number,'||NULL_VALUE||')'
40542   ||'          AND   msor2.process_flag  ='||G_IN_PROCESS
40543   ||'          AND    NVL(msor2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
40544   ||'  AND     msor1.process_flag = '||G_IN_PROCESS
40545   ||'  AND     msor1.sr_instance_code = :v_instance_code'
40546   ||'  AND     NVL(msor1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE ;
40547 
40548 
40549          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40550 
40551   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
40552 
40553 
40554   --Duplicate records check for the records whose source is batch load
40555 
40556     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40557                  (p_app_short_name    => 'MSC',
40558                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
40559                   p_message_text      => lv_message_text,
40560                   p_error_text        => lv_error_text);
40561 
40562     IF lv_return <> 0 THEN
40563       RAISE ex_logging_err;
40564     END IF;
40565 
40566   v_sql_stmt := 33;
40567   lv_sql_stmt :=
40568   'UPDATE    msc_st_operation_resources msor1'
40569   ||' SET    process_flag = '||G_ERROR_FLG||','
40570   ||'         error_text   = '||''''||lv_message_text||''''
40571   ||' WHERE    EXISTS ( SELECT 1 '
40572   ||'          FROM msc_st_operation_resources msor2'
40573   ||'          WHERE msor2.sr_instance_code   = msor1.sr_instance_code'
40574   ||'          AND   msor2.organization_code  = msor1.organization_code'
40575   ||'          AND   NVL(msor2.company_name,'||''''||NULL_CHAR||''''||') '
40576   ||'                 =    NVL(msor1.company_name,'||''''||NULL_CHAR||''''||') '
40577   ||'          AND   NVL(msor2.routing_name,'||''''||NULL_CHAR||''''||') '
40578   ||'                 = NVL(msor1.routing_name,'||''''||NULL_CHAR||''''||') '
40579   ||'          AND   msor2.assembly_name      = msor1.assembly_name'
40580   ||'          AND   NVL(msor2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
40581   ||'                = NVL(msor1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
40582   ||'          AND   NVL(msor2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
40583   ||'                = NVL(msor1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
40584   ||'          AND   msor2.resource_code      = msor1.resource_code'
40585   ||'          AND   NVL(msor2.resource_seq_code,'||''''||NULL_CHAR||''''||') '
40586   ||'               = NVL(msor1.resource_seq_code,'||''''||NULL_CHAR||''''||') '
40587   ||'          AND   NVL(msor2.alternate_number,'||NULL_VALUE||')'
40588   ||'               =  NVL(msor1.alternate_number,'||NULL_VALUE||')'
40589   ||'          AND   msor2.process_flag  ='||G_IN_PROCESS
40590   ||'               AND NVL(msor2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
40591   ||'         GROUP BY sr_instance_code,organization_code,company_name,routing_name,'
40592   ||'         assembly_name, alternate_routing_designator,operation_seq_code,'
40593   ||'         resource_code,resource_seq_code,alternate_number '
40594   ||'         HAVING COUNT(*) >1 )'
40595   ||'  AND    msor1.process_flag = '||G_IN_PROCESS
40596   ||'  AND    msor1.sr_instance_code = :v_instance_code'
40597   ||'  AND    NVL(msor1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
40598 
40599   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
40600 
40601 
40602   --Duplicate records check for the records whose source is XML
40603 
40604  lv_column_names :=
40605   'ROUTING_NAME              ||''~''||'
40606   ||' ASSEMBLY_NAME          ||''~''||'
40607   ||' OPERATION_SEQ_CODE     ||''~''||'
40608   ||' RESOURCE_SEQ_CODE      ||''~''||'
40609   ||' DEPARTMENT_CODE        ||''~''||'
40610   ||' RESOURCE_CODE          ||''~''||'
40611   ||' ALTERNATE_NUMBER       ||''~''||'
40612   ||' PRINCIPAL_FLAG         ||''~''||'
40613   ||' BASIS_TYPE             ||''~''||'
40614   ||' RESOURCE_USAGE         ||''~''||'
40615   ||' UOM_CODE               ||''~''||'
40616   ||' ORGANIZATION_CODE      ||''~''||'
40617   ||' SR_INSTANCE_CODE       ||''~''||'
40618   ||' MAX_RESOURCE_UNITS     ||''~''||'
40619   ||' RESOURCE_UNITS         ||''~''||'
40620   ||' DELETED_FLAG           ||''~''||'
40621   ||' RESOURCE_TYPE          ||''~''||'
40622   ||' COMPANY_NAME';
40623 
40624   LOOP
40625       lv_cursor_stmt :=
40626       ' SELECT msc_st_batch_id_s.NEXTVAL '
40627       ||' FROM   dual';
40628 
40629       OPEN  c1 FOR lv_cursor_stmt;
40630       FETCH c1 INTO lv_batch_id;
40631       CLOSE c1;
40632 
40633       v_sql_stmt := 34;
40634       lv_sql_stmt :=
40635       ' UPDATE msc_st_operation_resources '
40636       ||' SET    batch_id  = :lv_batch_id'
40637       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
40638       ||' AND    sr_instance_code               = :v_instance_code'
40639       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
40640       ||' AND    rownum                        <= '||v_batch_size;
40641 
40642 
40643          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40644 
40645       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
40646 
40647       EXIT WHEN SQL%NOTFOUND;
40648 
40649       OPEN c6(lv_batch_id);
40650       FETCH c6 BULK COLLECT INTO lb_rowid;
40651       CLOSE c6;
40652 
40653       v_sql_stmt := 35;
40654       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
40655       UPDATE msc_st_operation_resources
40656       SET  st_transaction_id = msc_st_operation_resources_s.NEXTVAL,
40657            refresh_id        = v_refresh_id,
40658            last_update_date  = v_current_date,
40659            last_updated_by   = v_current_user,
40660            creation_date     = v_current_date,
40661            created_by        = v_current_user
40662       WHERE  rowid           = lb_rowid(j);
40663 
40664 
40665      -- Set the error message
40666     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40667                      (p_app_short_name    => 'MSC',
40668                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
40669                       p_message_text      => lv_message_text,
40670                       p_error_text        => lv_error_text,
40671                       p_token1            => 'COLUMN_NAME',
40672                       p_token_value1      => 'DELETED_FLAG',
40673                       p_token2            => 'DEFAULT_VALUE',
40674                       p_token_value2      =>  SYS_NO  );
40675 
40676       IF lv_return <> 0 THEN
40677         RAISE ex_logging_err;
40678       END IF;
40679 
40680      -- Check whether delete flag value is in (SYS_YES,SYS_NO),
40681      -- if not default to SYS_NO
40682 
40683      lv_where_str :=
40684      ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
40685 
40686      lv_return := MSC_ST_UTIL.LOG_ERROR
40687                     (p_table_name        => 'MSC_ST_OPERATION_RESOURCES',
40688                      p_instance_code     => v_instance_code,
40689                      p_row               => lv_column_names,
40690                      p_severity          => G_SEV_WARNING,
40691                      p_message_text       => 'lv_message_text',
40692                      p_error_text        => lv_error_text,
40693                      p_batch_id          => lv_batch_id,
40694                      p_where_str         => lv_where_str,
40695                      p_col_name          => 'DELETED_FLAG',
40696                      p_debug             => v_debug,
40697                      p_default_value     => SYS_NO);
40698 
40699 
40700       IF lv_return <> 0 THEN
40701         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_error_text);
40702       END IF;
40703 
40704     -- Check whether routing_name is NULL
40705     -- then populate the ASSEMBLY_NAME in that
40706 
40707     v_sql_stmt := 36;
40708     lv_sql_stmt :=
40709     'UPDATE msc_st_operation_resources '
40710     ||' SET    routing_name      = assembly_name'
40711     ||' WHERE  sr_instance_code  = :v_instance_code'
40712     ||' AND    process_flag      ='||G_IN_PROCESS
40713     ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||')'
40714     ||'       = '||''''||NULL_CHAR||''''
40715     ||' AND    batch_id = :lv_batch_id';
40716 
40717 
40718 
40719 
40720          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40721 
40722     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40723 
40724    -- Error out records where resource_usage is NULL
40725 
40726     v_sql_stmt := 37;
40727     lv_sql_stmt :=
40728     'UPDATE    msc_st_operation_resources '
40729     ||'  SET    process_flag         ='||G_ERROR_FLG||','
40730     ||'         error_text   = '||''''||lv_message_text||''''
40731     ||'  WHERE   NVL(resource_usage,'||NULL_VALUE||') ='||NULL_VALUE
40732     ||'  AND     sr_instance_code       = :v_instance_code'
40733     ||'  AND     deleted_flag           = '||SYS_NO
40734     ||'  AND     process_flag           = '||G_IN_PROCESS
40735     ||'  AND     batch_id               = :lv_batch_id';
40736 
40737 
40738          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40739 
40740     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40741 
40742      -- Set the  message
40743      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40744                      (p_app_short_name    => 'MSC',
40745                       p_error_code        => 'MSC_PP_INVALID_VALUE',
40746                       p_message_text      => lv_message_text,
40747                       p_error_text        => lv_error_text,
40748                       p_token1            => 'COLUMN_NAME',
40749                       p_token_value1      => 'ROUTING_NAME');
40750 
40751       IF lv_return <> 0 THEN
40752         RAISE ex_logging_err;
40753       END IF;
40754 
40755    -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
40756     lv_return :=
40757     MSC_ST_UTIL.DERIVE_ROUTING_SEQUENCE_ID
40758                     (p_table_name     => 'MSC_ST_OPERATION_RESOURCES',
40759                      p_rtg_col_name   => 'ROUTING_NAME',
40760                      p_rtg_col_id     =>'ROUTING_SEQUENCE_ID',
40761                      p_instance_code  => v_instance_code,
40762                      p_severity       => G_SEV_ERROR,
40763                      p_message_text   => lv_message_text,
40764                      p_batch_id       => lv_batch_id,
40765                      p_debug          => v_debug,
40766                      p_error_text     => lv_error_text);
40767 
40768     if (lv_return <> 0 )then
40769        RAISE ex_logging_err;
40770     end if;
40771 
40772 
40773     -- Set the error message
40774     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40775                      (p_app_short_name    => 'MSC',
40776                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
40777                       p_message_text      => lv_message_text,
40778                       p_error_text        => lv_error_text,
40779                       p_token1            => 'COLUMN_NAME',
40780                       p_token_value1      => 'OPERATION_SEQ_CODE',
40781                       p_token2            => 'DEFAULT_VALUE',
40782                       p_token_value2      =>  G_OPERATION_SEQ_CODE );
40783 
40784       IF lv_return <> 0 THEN
40785         RAISE ex_logging_err;
40786       END IF;
40787 
40788      -- Default operation_seq_code as 1 if NULL
40789 
40790      lv_where_str := ' AND NVL(operation_seq_code,'||''''||NULL_CHAR||''''
40791                       ||' ) ='||''''||NULL_CHAR||'''' ;
40792 
40793      lv_return := MSC_ST_UTIL.LOG_ERROR
40794                     (p_table_name        => 'MSC_ST_OPERATION_RESOURCES',
40795                      p_instance_code     => v_instance_code,
40796                      p_row               => lv_column_names,
40797                      p_severity          => G_SEV_WARNING,
40798                      p_message_text      => lv_message_text,
40799                      p_error_text        => lv_error_text,
40800                      p_batch_id          => lv_batch_id,
40801                      p_where_str         => lv_where_str,
40802                      p_col_name          => 'OPERATION_SEQ_CODE',
40803                      p_debug             => v_debug,
40804                      p_default_value     => G_OPERATION_SEQ_CODE);
40805 
40806       IF lv_return <> 0 THEN
40807         RAISE ex_logging_err;
40808       END IF;
40809 
40810 
40811     -- Set the error message
40812     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40813                      (p_app_short_name    => 'MSC',
40814                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
40815                       p_message_text      => lv_message_text,
40816                       p_error_text        => lv_error_text,
40817                       p_token1            => 'COLUMN_NAME',
40818                       p_token_value1      => 'OPERATION_EFFECTIVITY_DATE',
40819                       p_token2            => 'DEFAULT_VALUE',
40820                       p_token_value2      =>  'SYSDATE' );
40821 
40822       IF lv_return <> 0 THEN
40823         RAISE ex_logging_err;
40824       END IF;
40825 
40826     -- Default effectivity date as SYSDATE if NULL
40827 
40828      lv_where_str :=
40829      ' AND NVL(operation_effectivity_date, SYSDATE-36500 ) = SYSDATE-36500 '  ;
40830 
40831      lv_return := MSC_ST_UTIL.LOG_ERROR
40832                     (p_table_name        => 'MSC_ST_OPERATION_RESOURCES',
40833                      p_instance_code     => v_instance_code,
40834                      p_row               => lv_column_names,
40835                      p_severity          => G_SEV_WARNING,
40836                      p_message_text      => lv_message_text,
40837                      p_error_text        => lv_error_text,
40838                      p_batch_id          => lv_batch_id,
40839                      p_where_str         => lv_where_str,
40840                      p_col_name          => 'OPERATION_EFFECTIVITY_DATE',
40841                      p_debug             => v_debug,
40842                      p_default_value     => 'SYSDATE');
40843 
40844       IF lv_return <> 0 THEN
40845         RAISE ex_logging_err;
40846       END IF;
40847 
40848      -- Derive operation sequence id from local id table
40849 
40850     v_sql_stmt := 38;
40851     lv_sql_stmt:=
40852     'UPDATE     msc_st_operation_resources msor'
40853     ||' SET     operation_sequence_id=    (SELECT local_id'
40854     ||'                    FROM msc_local_id_setup mlis'
40855     ||'                    WHERE  mlis.char1 = msor.sr_instance_code'
40856     ||'                    AND NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
40857     ||'                    =   NVL(msor.company_name,'||''''||NULL_CHAR||''''||') '
40858     ||'                    AND    mlis.char3 = msor.organization_code'
40859     ||'                    AND    mlis.char4 = msor.routing_name'
40860     ||'                    AND    mlis.char5 = msor.operation_seq_code'
40861     ||'                    AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
40862     ||'                          = NVL(msor.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
40863     ||'                    AND   mlis.date1 = msor.operation_effectivity_date'
40864     ||'                    AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'') '
40865     ||' WHERE      sr_instance_code = :v_instance_code'
40866     ||' AND        process_flag     = '||G_IN_PROCESS
40867     ||' AND        batch_id         = :lv_batch_id';
40868 
40869 
40870 
40871          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40872 
40873     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40874 
40875       -- Set the error message
40876     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40877                      (p_app_short_name    => 'MSC',
40878                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
40879                       p_message_text      => lv_message_text,
40880                       p_error_text        => lv_error_text,
40881                       p_token1            => 'COLUMN_NAMES',
40882                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
40883                                              ||'ORGANIZATION_CODE,ROUTING_NAME,'
40884                                              ||' OPERATION_SEQ_CODE,EFFECTIVITY_DATE,'
40885                                              ||' ALTERNATE_ROUTING_DESIGNATOR'  ,
40886                       p_token2            => 'MASTER_TABLE',
40887                       p_token_value2      =>  'MSC_ST_ROUTING_OPERATIONS',
40888                       p_token3            =>  'CHILD_TABLE' ,
40889                       p_token_value3      =>  'MSC_ST_OPERATION_RESOURCES' );
40890 
40891       IF lv_return <> 0 THEN
40892         RAISE ex_logging_err;
40893       END IF;
40894 
40895 
40896    --Error out record where operation_sequence_id is NULL
40897 
40898     v_sql_stmt := 39;
40899 
40900     lv_sql_stmt :=
40901     'UPDATE       msc_st_operation_resources'
40902     ||'  SET      process_flag     = '||G_ERROR_FLG||','
40903     ||'           error_text   = '||''''||lv_message_text||''''
40904     ||'  WHERE    NVL(operation_sequence_id,'||NULL_VALUE||') = '||NULL_VALUE
40905     ||'  AND      sr_instance_code = :v_instance_code'
40906     ||'  AND      process_flag     = '||G_IN_PROCESS
40907     ||'  AND      batch_id         = :lv_batch_id';
40908 
40909 
40910 
40911          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
40912 
40913     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
40914 
40915     -- Set the error message
40916     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40917                      (p_app_short_name    => 'MSC',
40918                       p_error_code        => 'MSC_PP_COL_VAL_NULL_DEFAULT',
40919                       p_message_text      => lv_message_text,
40920                       p_error_text        => lv_error_text,
40921                       p_token1            => 'COLUMN_NAME',
40922                       p_token_value1      => 'RESOURCE_SEQ_CODE',
40923                       p_token2            => 'DEFAULT_VALUE',
40924                       p_token_value2      =>  G_RESOURCE_SEQ_CODE );
40925 
40926       IF lv_return <> 0 THEN
40927         RAISE ex_logging_err;
40928       END IF;
40929 
40930      -- Default resource_seq_code as 1 if NULL
40931 
40932      lv_where_str := ' AND NVL(resource_seq_code,'||''''||NULL_CHAR||''''
40933                       ||' ) ='||''''||NULL_CHAR||'''' ;
40934 
40935      lv_return := MSC_ST_UTIL.LOG_ERROR
40936                     (p_table_name        => 'MSC_ST_OPERATION_RESOURCES',
40937                      p_instance_code     => v_instance_code,
40938                      p_row               => lv_column_names,
40939                      p_severity          => G_SEV_WARNING,
40940                      p_message_text      => lv_message_text,
40941                      p_error_text        => lv_error_text,
40942                      p_batch_id          => lv_batch_id,
40943                      p_where_str         => lv_where_str,
40944                      p_col_name          => 'RESOURCE_SEQ_CODE',
40945                      p_debug             => v_debug,
40946                      p_default_value     => G_RESOURCE_SEQ_CODE);
40947 
40948       IF lv_return <> 0 THEN
40949         RAISE ex_logging_err;
40950       END IF;
40951 
40952     -- Set the error message
40953     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
40954                      (p_app_short_name    => 'MSC',
40955                       p_error_code        => 'MSC_PP_COL_VAL_NULL_DEFAULT',
40956                       p_message_text      => lv_message_text,
40957                       p_error_text        => lv_error_text,
40958                       p_token1            => 'COLUMN_NAME',
40959                       p_token_value1      => 'ALTERNATE_NUMBER',
40960                       p_token2            => 'DEFAULT_VALUE',
40961                       p_token_value2      =>  G_ALTERNATE_NUMBER );
40962 
40963       IF lv_return <> 0 THEN
40964         RAISE ex_logging_err;
40965       END IF;
40966 
40967     -- Default alternate number as 0 if NULL
40968 
40969     lv_where_str := ' AND NVL(alternate_number,'||NULL_VALUE||') ='||NULL_VALUE ;
40970 
40971     lv_return := MSC_ST_UTIL.LOG_ERROR
40972                     (p_table_name        => 'MSC_ST_OPERATION_RESOURCES',
40973                      p_instance_code     => v_instance_code,
40974                      p_row               => lv_column_names,
40975                      p_severity          => G_SEV_WARNING,
40976                      p_message_text      => lv_message_text,
40977                      p_error_text        => lv_error_text,
40978                      p_batch_id          => lv_batch_id,
40979                      p_where_str         => lv_where_str,
40980                      p_col_name          => 'ALTERNATE_NUMBER',
40981                      p_debug             => v_debug,
40982                      p_default_value     => G_ALTERNATE_NUMBER);
40983 
40984       IF lv_return <> 0 THEN
40985         RAISE ex_logging_err;
40986       END IF;
40987 
40988    -- Derive resource seq num from the local  id table
40989 
40990     v_sql_stmt := 40;
40991     lv_sql_stmt :=
40992     'UPDATE     msc_st_operation_resources msor'
40993     ||' SET     resource_seq_num =    (SELECT local_id '
40994     ||'         FROM msc_local_id_setup mlis'
40995     ||'         WHERE  mlis.char1 = msor.sr_instance_code'
40996     ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
40997     ||'         =    NVL(msor.company_name,'||''''||NULL_CHAR||''''||') '
40998     ||'         AND    mlis.char3 = msor.organization_code'
40999     ||'         AND    mlis.char4 = msor.routing_name'
41000     ||'         AND    mlis.char5 = msor.operation_seq_code'
41001     ||'         AND    mlis.char6 = msor.resource_seq_code'
41002     ||'         AND    NVL(mlis.char7,'||''''||NULL_CHAR||''''||') '
41003     ||'         =      NVL(msor.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
41004     ||'         AND    NVL(mlis.number1,'||NULL_VALUE||') = NVL(msor.alternate_number,'||NULL_VALUE||')'
41005     ||'		AND    rownum = 1'	/*change made to solve res req issue*/
41006     ||'         AND    mlis.entity_name = ''RESOURCE_SEQ_NUM'')'
41007     ||' WHERE   sr_instance_code = :v_instance_code'
41008     ||' AND     process_flag     = '||G_IN_PROCESS
41009     ||' AND     batch_id         = :lv_batch_id';
41010 
41011 
41012 
41013          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41014 
41015     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41016 
41017 
41018     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41019                  (p_app_short_name    => 'MSC',
41020                   p_error_code        => 'MSC_PP_DELETE_FAIL',
41021                   p_message_text      => lv_message_text,
41022                   p_error_text        => lv_error_text);
41023 
41024     IF lv_return <> 0 THEN
41025       RAISE ex_logging_err;
41026     END IF;
41027 
41028    -- Error out the records where delete flag yes
41029    -- and resource_seq_num is NULL
41030 
41031     v_sql_stmt := 41;
41032     lv_sql_stmt :=
41033     'UPDATE     msc_st_operation_resources '
41034     ||' SET     process_flag     = '||G_ERROR_FLG||','
41035     ||'         error_text   = '||''''||lv_message_text||''''
41036     ||' WHERE   NVL(resource_seq_num,'||NULL_VALUE||') ='||NULL_VALUE
41037     ||' AND     deleted_flag     = '||SYS_YES
41038     ||' AND     sr_instance_code = :v_instance_code'
41039     ||' AND     process_flag     = '||G_IN_PROCESS
41040     ||' AND     batch_id         = :lv_batch_id';
41041 
41042 
41043 
41044          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41045 
41046     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41047 
41048 
41049     -- Derive resource id
41050     v_sql_stmt := 42;
41051 
41052     lv_sql_stmt :=
41053     'UPDATE     msc_st_operation_resources msor'
41054     ||' SET     resource_id = (SELECT local_id'
41055     ||'                 FROM msc_local_id_setup mlis'
41056     ||'                 WHERE  mlis.char1 = msor.sr_instance_code'
41057     ||'                 AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
41058     ||'                 =    NVL(msor.company_name,'||''''||NULL_CHAR||''''||') '
41059     ||'                 AND    mlis.char3 = msor.organization_code'
41060     ||'                 AND    mlis.char4 = msor.resource_code'
41061     ||'                 AND    mlis.entity_name = ''RESOURCE_ID'')'
41062     ||' WHERE      sr_instance_code = :v_instance_code'
41063     ||' AND        deleted_flag     = '|| SYS_NO
41064     ||' AND        process_flag     = '||G_IN_PROCESS
41065     ||' AND        batch_id         = :lv_batch_id';
41066 
41067 
41068          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41069 
41070     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41071 
41072     -- Derive department id
41073     v_sql_stmt := 43;
41074 
41075     lv_sql_stmt :=
41076     'UPDATE     msc_st_operation_resources msor'
41077     ||' SET     department_id = (SELECT local_id'
41078     ||'                 FROM msc_local_id_setup mlis'
41079     ||'                 WHERE  mlis.char1 = msor.sr_instance_code'
41080     ||'                 AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
41081     ||'                 =    NVL(msor.company_name,'||''''||NULL_CHAR||''''||') '
41082     ||'                 AND    mlis.char3 = msor.organization_code'
41083     ||'                 AND    mlis.char4 = msor.department_code'
41084     ||'                 AND    mlis.entity_name = ''DEPARTMENT_ID'')'
41085     ||' WHERE      sr_instance_code = :v_instance_code'
41086     ||' AND        deleted_flag     = '|| SYS_NO
41087     ||' AND        process_flag     = '||G_IN_PROCESS
41088     ||' AND        batch_id         = :lv_batch_id';
41089 
41090 
41091          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41092 
41093     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41094 
41095     -- Set the error message
41096     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41097                      (p_app_short_name    => 'MSC',
41098                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
41099                       p_message_text      => lv_message_text,
41100                       p_error_text        => lv_error_text,
41101                       p_token1            => 'COLUMN_NAMES',
41102                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
41103                                              ||' ORGANIZATION_CODE,DEPARTMENT_CODE'
41104                                              ||' OR RESOURCE_CODE' ,
41105                       p_token2            => 'MASTER_TABLE',
41106                       p_token_value2      => 'MSC_ST_DEPARTMENT_RESOURCES',
41107                       p_token3            => 'CHILD_TABLE' ,
41108                       p_token_value3      => 'MSC_ST_OPERATION_RESOURCES' );
41109 
41110       IF lv_return <> 0 THEN
41111         RAISE ex_logging_err;
41112       END IF;
41113 
41114     -- Error out records where resource_id or department_id is NULL
41115 
41116     v_sql_stmt := 44;
41117     lv_sql_stmt :=
41118     'UPDATE       msc_st_operation_resources'
41119     ||'  SET       process_flag    ='||G_ERROR_FLG||','
41120     ||'         error_text   = '||''''||lv_message_text||''''
41121     ||'  WHERE   (NVL(resource_id,'||NULL_VALUE||') ='|| NULL_VALUE
41122     ||'  OR        NVL(department_id,'||NULL_VALUE||') ='|| NULL_VALUE||')'
41123     ||'  AND      sr_instance_code       = :v_instance_code'
41124     ||'  AND      process_flag           ='||G_IN_PROCESS
41125     ||'  AND      batch_id               = :lv_batch_id';
41126 
41127 
41128          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41129 
41130     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41131 
41132 
41133     -- Set the error message
41134     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41135                      (p_app_short_name    => 'MSC',
41136                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
41137                       p_message_text      => lv_message_text,
41138                       p_error_text        => lv_error_text,
41139                       p_token1            => 'COLUMN_NAME',
41140                       p_token_value1      => 'BASIS_TYPE',
41141                       p_token2            => 'DEFAULT_VALUE',
41142                       p_token_value2      =>  G_BASIS_TYPE  );
41143 
41144       IF lv_return <> 0 THEN
41145         RAISE ex_logging_err;
41146       END IF;
41147 
41148 -- Default basis type as 1 if INVALID
41149 
41150     lv_where_str :=
41151     ' AND NVL(basis_type,'||NULL_VALUE||')'
41152     ||' NOT IN (1,2,3,4,5) AND deleted_flag = 2' ;
41153 
41154      lv_return := MSC_ST_UTIL.LOG_ERROR
41155                     (p_table_name        => 'MSC_ST_OPERATION_RESOURCES',
41156                      p_instance_code     => v_instance_code,
41157                      p_row               => lv_column_names,
41158                      p_severity          => G_SEV_WARNING,
41159                      p_message_text      => lv_message_text,
41160                      p_error_text        => lv_error_text,
41161                      p_batch_id          => lv_batch_id,
41162                      p_where_str         => lv_where_str,
41163                      p_col_name          => 'BASIS_TYPE',
41164                      p_debug             => v_debug,
41165                      p_default_value     => G_BASIS_TYPE);
41166       IF lv_return <> 0 THEN
41167         RAISE ex_logging_err;
41168       END IF;
41169 
41170     -- Set the error message
41171     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41172                      (p_app_short_name    => 'MSC',
41173                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
41174                       p_message_text      => lv_message_text,
41175                       p_error_text        => lv_error_text,
41176                       p_token1            => 'COLUMN_NAME',
41177                       p_token_value1      => 'PRINCIPAL_FLAG',
41178                       p_token2            => 'DEFAULT_VALUE',
41179                       p_token_value2      =>  G_PRINCIPAL_FLAG  );
41180 
41181       IF lv_return <> 0 THEN
41182         RAISE ex_logging_err;
41183       END IF;
41184 
41185      -- Default principal_flag as 1 if NOT IN (1,2)
41186 
41187      lv_where_str := ' AND NVL(principal_flag,'||NULL_VALUE||')'||' NOT IN (1,2)'
41188                        ||'AND deleted_flag = 2';
41189 
41190      lv_return := MSC_ST_UTIL.LOG_ERROR
41191                     (p_table_name        => 'MSC_ST_OPERATION_RESOURCES',
41192                      p_instance_code     => v_instance_code,
41193                      p_row               => lv_column_names,
41194                      p_severity          => G_SEV_WARNING,
41195                      p_message_text      => lv_message_text,
41196                      p_error_text        => lv_error_text,
41197                      p_batch_id          => lv_batch_id,
41198                      p_where_str         => lv_where_str,
41199                      p_col_name          => 'PRINCIPAL_FLAG',
41200                      p_debug             => v_debug,
41201                      p_default_value     => G_PRINCIPAL_FLAG);
41202 
41203       IF lv_return <> 0 THEN
41204         RAISE ex_logging_err;
41205       END IF;
41206 
41207      --Call to customised validation.
41208 
41209       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
41210         (ERRBUF         => lv_error_text,
41211          RETCODE        => lv_return,
41212          pBatchID       => lv_batch_id,
41213          pInstanceCode  => v_instance_code,
41214          pEntityName    => 'MSC_ST_OPERATION_RESOURCES',
41215          pInstanceID    => v_instance_id);
41216 
41217       IF NVL(lv_return,0) <> 0 THEN
41218         RAISE ex_logging_err;
41219       END IF;
41220 
41221     -- Now generate resource_seq_num
41222     -- Its an update of resource_seq_num with the resource_seq_code
41223 
41224      v_sql_stmt := 45;
41225 
41226      OPEN c7(lv_batch_id);
41227      FETCH c7 BULK COLLECT INTO lb_rowid ;
41228 
41229      IF c7%ROWCOUNT > 0  THEN
41230 
41231     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
41232 
41233         UPDATE msc_st_operation_resources
41234         SET    resource_seq_num     =
41235                to_number(decode(length(rtrim(resource_seq_code,'0123456789')),
41236                           NULL,resource_seq_code,'1'))
41237         WHERE  rowid                = lb_rowid(j);
41238 
41239     v_sql_stmt := 46;
41240 
41241    FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
41242   -- Insert into the LID table  with the  user defined key
41243 
41244        INSERT INTO  msc_local_id_setup
41245        (local_id,
41246        st_transaction_id,
41247        instance_id,
41248        entity_name,
41249        data_source_type,
41250        char1,
41251        char2,
41252        char3,
41253        char4,
41254        char5,
41255        char6,
41256        char7,
41257        number1,
41258        SOURCE_ORGANIZATION_ID,
41259        SOURCE_ROUTING_SEQUENCE_ID ,
41260        SOURCE_OPERATION_SEQUENCE_ID  ,
41261        SOURCE_RESOURCE_SEQ_NUM  ,
41262        SOURCE_RESOURCE_ID ,
41263        SOURCE_DEPARTMENT_ID  ,
41264        last_update_date,
41265        last_updated_by,
41266        creation_date,
41267        created_by  )
41268       SELECT
41269        resource_seq_num,
41270        st_transaction_id,
41271        v_instance_id,
41272        'RESOURCE_SEQ_NUM',
41273        data_source_type,
41274        v_instance_code,
41275        company_name,
41276        organization_code,
41277        routing_name,
41278        operation_seq_code,
41279        resource_seq_code,
41280        alternate_routing_designator,
41281        alternate_number,
41282        SOURCE_ORGANIZATION_ID,
41283        SOURCE_ROUTING_SEQUENCE_ID ,
41284        SOURCE_OPERATION_SEQUENCE_ID  ,
41285        SOURCE_RESOURCE_SEQ_NUM  ,
41286        SOURCE_RESOURCE_ID ,
41287        SOURCE_DEPARTMENT_ID  ,
41288        v_current_date,
41289        v_current_user,
41290        v_current_date,
41291        v_current_user
41292        FROM msc_st_operation_resources
41293       WHERE rowid = lb_rowid(j) ;
41294 
41295     END IF;
41296   CLOSE c7;
41297 
41298    -- Update resource seq num from the local  id table
41299 
41300     v_sql_stmt := 47;
41301     lv_sql_stmt :=
41302     'UPDATE     msc_st_operation_resources msor'
41303     ||' SET     resource_seq_num =    (SELECT local_id '
41304     ||'         FROM msc_local_id_setup mlis'
41305     ||'         WHERE  mlis.char1 = msor.sr_instance_code'
41306     ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
41307     ||'         =    NVL(msor.company_name,'||''''||NULL_CHAR||''''||') '
41308     ||'         AND    mlis.char3 = msor.organization_code'
41309     ||'         AND    mlis.char4 = msor.routing_name'
41310     ||'         AND    mlis.char5 = msor.operation_seq_code'
41311     ||'         AND    mlis.char6 = msor.resource_seq_code'
41312     ||'         AND    NVL(mlis.char7,'||''''||NULL_CHAR||''''||') '
41313     ||'         =      NVL(msor.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
41314     ||'         AND    NVL(mlis.number1,'||NULL_VALUE||') = NVL(msor.alternate_number,'||NULL_VALUE||')'
41315     ||'		AND    rownum = 1'	/*change made to solve res req issue*/
41316     ||'         AND    mlis.entity_name = ''RESOURCE_SEQ_NUM'')'
41317     ||' WHERE   sr_instance_code = :v_instance_code'
41318     ||' AND     NVL(resource_seq_num,'||NULL_VALUE||') = '||NULL_VALUE
41319     ||' AND     process_flag     = '||G_IN_PROCESS
41320     ||' AND     batch_id         = :lv_batch_id';
41321 
41322 
41323 
41324          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41325 
41326     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41327 
41328 
41329      OPEN c8(lv_batch_id);
41330      FETCH c8 BULK COLLECT INTO lb_rowid ;
41331 
41332     v_sql_stmt := 48;
41333 
41334     IF c8%ROWCOUNT > 0  THEN
41335     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
41336 
41337 
41338     INSERT INTO  msc_st_operation_resource_seqs
41339     ( routing_sequence_id,
41340       operation_sequence_id,
41341       resource_seq_num,
41342       schedule_flag,
41343       resource_offset_percent,
41344       department_id,
41345       deleted_flag,
41346       sr_instance_id,
41347       activity_group_id,
41348        refresh_id,
41349       last_update_date,
41350       last_updated_by,
41351       creation_date,
41352       created_by  )
41353     SELECT
41354      routing_sequence_id,
41355      operation_sequence_id,
41356      resource_seq_num,
41357      schedule_flag,
41358      resource_offset_percent,
41359      department_id,
41360      deleted_flag,
41361      v_instance_id,
41362      activity_group_id,
41363      v_refresh_id,
41364      last_update_date,
41365      last_updated_by,
41366      creation_date,
41367      created_by
41368      FROM msc_st_operation_resources
41369      WHERE rowid = lb_rowid(j) ;
41370 
41371    END IF ;
41372    CLOSE c8 ;
41373 
41374     -- Set the process flag as Valid and derive sr_instance_id
41375     lv_return :=
41376     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_OPERATION_RESOURCES',
41377                                p_instance_id    => v_instance_id,
41378                                p_instance_code  => v_instance_code,
41379                                p_process_flag   => G_VALID,
41380                                p_error_text     => lv_error_text,
41381                                p_debug          => v_debug,
41382                                p_batch_id       => lv_batch_id);
41383 
41384       IF lv_return < 0 THEN
41385         RAISE ex_logging_err;
41386       END IF;
41387 
41388 
41389     -- At the end calling  the LOG_ERROR for logging all errored out records
41390 
41391    lv_return :=
41392    MSC_ST_UTIL.LOG_ERROR(p_table_name    =>'MSC_ST_OPERATION_RESOURCES',
41393                          p_instance_code =>v_instance_code,
41394                          p_row           =>lv_column_names,
41395                          p_severity      =>G_SEV_ERROR,
41396                          p_message_text  =>NULL,
41397                          p_error_text    =>lv_error_text,
41398                          p_debug         => v_debug,
41399                          p_batch_id      => lv_batch_id  );
41400 
41401       IF lv_return <> 0 THEN
41402         RAISE ex_logging_err;
41403       END IF;
41404 
41405   COMMIT;
41406 
41407   END LOOP;
41408 
41409    -- From here added for OSFM Integration
41410    -- Pre processing for MSC_ST_OPERATION_NETWORKS
41411 
41412    --Duplicate records check for the records whose source is XML
41413 
41414     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41415                  (p_app_short_name    => 'MSC',
41416                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
41417                   p_message_text      => lv_message_text,
41418                   p_error_text        => lv_error_text);
41419 
41420     IF lv_return <> 0 THEN
41421       RAISE ex_logging_err;
41422     END IF;
41423 
41424   v_sql_stmt := 51;
41425 
41426   lv_sql_stmt :=
41427   'UPDATE    msc_st_operation_networks mson1 '
41428   ||' SET       process_flag = '||G_ERROR_FLG||','
41429   ||'         error_text   = '||''''||lv_message_text||''''
41430   ||' WHERE     message_id < ( SELECT max(message_id) '
41431   ||'          FROM msc_st_operation_networks mson2'
41432   ||'          WHERE mson2.assembly_name      = mson1.assembly_name'
41433   ||'          AND   mson2.organization_code  = mson1.organization_code'
41434   ||'          AND   mson2.from_operation_seq_code = mson1.from_operation_seq_code'
41435   ||'          AND   mson2.to_operation_seq_code = mson1.to_operation_seq_code'
41436   ||'          AND   mson2.sr_instance_code   = mson1.sr_instance_code'
41437   ||'          AND   NVL(mson2.company_name,'||''''||NULL_CHAR||''''||') '
41438   ||'                 =    NVL(mson1.company_name,'||''''||NULL_CHAR||''''||') '
41439   ||'          AND   NVL(mson2.routing_name,'||''''||NULL_CHAR||''''||') '
41440   ||'                = NVL(mson1.routing_name, '||''''||NULL_CHAR||''''||') '
41441   ||'          AND   NVL(mson2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
41442   ||'                = NVL(mson1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
41443   ||'          AND   mson2.process_flag   ='||G_IN_PROCESS
41444   ||'          AND    NVL(mson2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
41445   ||' AND     mson1.process_flag = '||G_IN_PROCESS
41446   ||' AND     mson1.sr_instance_code = :v_instance_code'
41447   ||' AND     NVL(mson1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
41448 
41449 
41450          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41451 
41452   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
41453 
41454 
41455 
41456   --Duplicate records check for the records whose source is batch load
41457 
41458     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41459                  (p_app_short_name    => 'MSC',
41460                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
41461                   p_message_text      => lv_message_text,
41462                   p_error_text        => lv_error_text);
41463 
41464     IF lv_return <> 0 THEN
41465       RAISE ex_logging_err;
41466     END IF;
41467 
41468   v_sql_stmt := 52;
41469 
41470   lv_sql_stmt :=
41471   'UPDATE   msc_st_operation_networks mson1'
41472   ||' SET      process_flag = '||G_ERROR_FLG||','
41473   ||'         error_text   = '||''''||lv_message_text||''''
41474   ||' WHERE    EXISTS ( SELECT 1 '
41475   ||'          FROM msc_st_operation_networks mson2'
41476   ||'          WHERE mson2.assembly_name      = mson1.assembly_name'
41477   ||'          AND   mson2.organization_code  = mson1.organization_code'
41478   ||'          AND   mson2.from_operation_seq_code = mson1.from_operation_seq_code'
41479   ||'          AND   mson2.to_operation_seq_code = mson1.to_operation_seq_code'
41480   ||'          AND   mson2.sr_instance_code   = mson1.sr_instance_code'
41481   ||'          AND   NVL(mson2.company_name,'||''''||NULL_CHAR||''''||') '
41482   ||'                 =    NVL(mson1.company_name,'||''''||NULL_CHAR||''''||') '
41483   ||'          AND   NVL(mson2.routing_name,'||''''||NULL_CHAR||''''||') '
41484   ||'                = NVL(mson1.routing_name, '||''''||NULL_CHAR||''''||') '
41485   ||'          AND   NVL(mson2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
41486   ||'                = NVL(mson1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
41487   ||'          AND   mson2.process_flag   ='||G_IN_PROCESS
41488   ||'          AND NVL(mson2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
41489   ||'          AND mson2.rowid <> mson1.rowid )'
41490   ||'  AND     mson1.process_flag          = '||G_IN_PROCESS
41491   ||'  AND     mson1.sr_instance_code       = :v_instance_code'
41492   ||'  AND     NVL(mson1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
41493 
41494 
41495          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41496 
41497   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
41498 
41499   COMMIT;
41500 
41501   lv_column_names :=
41502    '  FROM_OPERATION_SEQ_CODE      ||''~''||'
41503  ||'  FROM_OP_EFFECTIVITY_DATE     ||''~''||'
41504  ||'  FROM_OP_DISABLE_DATE         ||''~''||'
41505  ||'  TO_OPERATION_SEQ_CODE        ||''~''||'
41506  ||'  TO_OP_EFFECTIVITY_DATE       ||''~''||'
41507  ||'  TO_OP_DISABLE_DATE           ||''~''||'
41508  ||'  ROUTING_NAME                 ||''~''||'
41509  ||'  ASSEMBLY_NAME                ||''~''||'
41510  ||'  ALTERNATE_ROUTING_DESIGNATOR ||''~''||'
41511  ||'  TRANSITION_TYPE              ||''~''||'
41512  ||'  PLANNING_PCT                 ||''~''||'
41513  ||'  CUMMULATIVE_PCT              ||''~''||'
41514  ||'  DISABLE_DATE                 ||''~''||'
41515  ||'  EFECTIVITY_DATE             ||''~''||'
41516  ||'  ORGANIZATION_CODE            ||''~''||'
41517  ||'  SR_INSTANCE_CODE             ||''~''||'
41518  ||'  DELETED_FLAG                 ||''~''||'
41519  ||'  COMPANY_NAME ';
41520 
41521 
41522 
41523    -- Now processing in batch
41524     LOOP
41525 
41526       v_sql_stmt := 53;
41527 
41528       lv_cursor_stmt :=
41529       ' SELECT msc_st_batch_id_s.NEXTVAL '
41530       ||' FROM   dual';
41531 
41532       OPEN  c1 FOR lv_cursor_stmt;
41533       FETCH c1 INTO lv_batch_id;
41534       CLOSE c1;
41535 
41536 
41537       v_sql_stmt := 54;
41538 
41539       lv_sql_stmt :=
41540       ' UPDATE msc_st_operation_networks '
41541       ||' SET    batch_id  = :lv_batch_id'
41542       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
41543       ||' AND    sr_instance_code               = :v_instance_code'
41544       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
41545       ||' AND    rownum                        <= '||v_batch_size;
41546 
41547 
41548          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41549 
41550       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
41551 
41552       EXIT WHEN SQL%NOTFOUND;
41553 
41554       OPEN  c9(lv_batch_id);
41555       FETCH c9 BULK COLLECT INTO lb_rowid;
41556       CLOSE c9;
41557 
41558       v_sql_stmt := 55;
41559 
41560       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
41561       UPDATE msc_st_operation_networks
41562       SET  st_transaction_id = msc_st_operation_networks_s.NEXTVAL,
41563            refresh_id        = v_refresh_id,
41564            last_update_date  = v_current_date,
41565            last_updated_by   = v_current_user,
41566            creation_date     = v_current_date,
41567            created_by        = v_current_user
41568       WHERE  rowid           = lb_rowid(j);
41569 
41570 
41571     -- Set the error message
41572     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41573                      (p_app_short_name    => 'MSC',
41574                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
41575                       p_message_text      => lv_message_text,
41576                       p_error_text        => lv_error_text,
41577                       p_token1            => 'COLUMN_NAME',
41578                       p_token_value1      => 'DELETED_FLAG',
41579                       p_token2            => 'DEFAULT_VALUE',
41580                       p_token_value2      =>  SYS_NO  );
41581 
41582       IF lv_return <> 0 THEN
41583         RAISE ex_logging_err;
41584       END IF;
41585 
41586      -- Check whether delete flag value is in (1,2),if not default to SYS_NO
41587 
41588    lv_where_str :=
41589    ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
41590 
41591      lv_return := MSC_ST_UTIL.LOG_ERROR
41592                     (p_table_name        => 'MSC_ST_OPERATION_NETWORKS',
41593                      p_instance_code     => v_instance_code,
41594                      p_row               => lv_column_names,
41595                      p_severity          => G_SEV_WARNING,
41596                      p_message_text      => lv_message_text,
41597                      p_error_text        => lv_error_text,
41598                      p_batch_id          => lv_batch_id,
41599                      p_where_str         => lv_where_str,
41600                      p_col_name          => 'DELETED_FLAG',
41601                      p_debug             => v_debug,
41602                      p_default_value     => SYS_NO);
41603 
41604       IF lv_return <> 0 THEN
41605         RAISE ex_logging_err;
41606       END IF;
41607 
41608 
41609      -- Set the error message
41610     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41611                      (p_app_short_name    => 'MSC',
41612                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
41613                       p_message_text      => lv_message_text,
41614                       p_error_text        => lv_error_text,
41615                       p_token1            => 'COLUMN_NAME',
41616                       p_token_value1      => 'PLANNING_PCT');
41617 
41618       IF lv_return <> 0 THEN
41619         RAISE ex_logging_err;
41620       END IF;
41621 
41622      -- Error out the records whose PLANNING_PCT is NULL
41623 
41624      v_sql_stmt := 56;
41625     lv_sql_stmt :=
41626     'UPDATE    msc_st_operation_networks'
41627     ||' SET    process_flag     ='||G_ERROR_FLG||','
41628     ||'         error_text   = '||''''||lv_message_text||''''
41629     ||' WHERE  NVL(PLANNING_PCT,'||NULL_VALUE||') = '||NULL_VALUE
41630     ||' AND    sr_instance_code  = :v_instance_code'
41631     ||' AND    process_flag      = '||G_IN_PROCESS
41632     ||' AND    batch_id          = :lv_batch_id';
41633 
41634 
41635          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41636 
41637    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41638 
41639     -- Set the error message
41640     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41641                      (p_app_short_name    => 'MSC',
41642                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
41643                       p_message_text      => lv_message_text,
41644                       p_error_text        => lv_error_text,
41645                       p_token1            => 'COLUMN_NAME',
41646                       p_token_value1      => 'ASSEMBLY_NAME');
41647 
41648       IF lv_return <> 0 THEN
41649         RAISE ex_logging_err;
41650       END IF;
41651 
41652      -- Error out the records whose Assembly Name is NULL
41653 
41654      v_sql_stmt := 57;
41655     lv_sql_stmt :=
41656     'UPDATE    msc_st_operation_networks'
41657     ||' SET    process_flag     ='||G_ERROR_FLG||','
41658     ||'         error_text   = '||''''||lv_message_text||''''
41659     ||' WHERE  NVL(assembly_name,'||''''||NULL_CHAR||''''||') '
41660     ||'         = '||''''||NULL_CHAR||''''
41661     ||' AND    sr_instance_code  = :v_instance_code'
41662     ||' AND    process_flag      = '||G_IN_PROCESS
41663     ||' AND    batch_id          = :lv_batch_id';
41664 
41665 
41666          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41667 
41668    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41669 
41670      -- Now we will check whether Routing Name is NULL , if it is NULL we will populate the
41671      -- ASSEMBLY NAME in ROUTING_NAME column for all such records
41672 
41673     v_sql_stmt := 58;
41674 
41675     lv_sql_stmt :=
41676     'UPDATE msc_st_operation_networks '
41677     ||' SET    routing_name = assembly_name'
41678     ||' WHERE  sr_instance_code = :v_instance_code'
41679     ||' AND    process_flag     = '||G_IN_PROCESS
41680     ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||') '
41681     ||'         = '||''''||NULL_CHAR||''''
41682     ||' AND    batch_id         = :lv_batch_id';
41683 
41684 
41685          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41686 
41687       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41688 
41689 
41690 
41691     -- Set the error message
41692     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41693                      (p_app_short_name    => 'MSC',
41694                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
41695                       p_message_text      => lv_message_text,
41696                       p_error_text        => lv_error_text,
41697                       p_token1            => 'COLUMN_NAMES',
41698                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
41699                                              ||'ORGANIZATION_CODE,ROUTING_NAME',
41700                       p_token2            => 'MASTER_TABLE',
41701                       p_token_value2      =>  'MSC_ST_ROUTINGS',
41702                       p_token3            =>  'CHILD_TABLE' ,
41703                       p_token_value3      =>  'MSC_ST_OPERATION_NETWORKS' );
41704 
41705       IF lv_return <> 0 THEN
41706         RAISE ex_logging_err;
41707       END IF;
41708 
41709     -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
41710     v_sql_stmt := 59;
41711     lv_return :=
41712     MSC_ST_UTIL.DERIVE_ROUTING_SEQUENCE_ID
41713                       (p_table_name     => 'MSC_ST_OPERATION_NETWORKS',
41714                        p_rtg_col_name   => 'ROUTING_NAME',
41715                        p_rtg_col_id     => 'ROUTING_SEQUENCE_ID',
41716                        p_instance_code  => v_instance_code,
41717                        p_severity       => G_SEV_ERROR,
41718                        p_message_text   => lv_message_text,
41719                        p_batch_id       => lv_batch_id,
41720                        p_debug          => v_debug,
41721                        p_error_text     => lv_error_text);
41722 
41723     if (lv_return <> 0 )then
41724        RAISE ex_logging_err;
41725     end if;
41726 
41727 
41728     -- Set the error message
41729     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41730                      (p_app_short_name    => 'MSC',
41731                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
41732                       p_message_text      => lv_message_text,
41733                       p_error_text        => lv_error_text,
41734                       p_token1            => 'COLUMN_NAME',
41735                       p_token_value1      => 'FROM_OP_EFFECTIVITY_DATE',
41736                       p_token2            => 'DEFAULT_VALUE',
41737                       p_token_value2      => 'SYSDATE' );
41738 
41739       IF lv_return <> 0 THEN
41740         RAISE ex_logging_err;
41741       END IF;
41742 
41743       -- The effectivity date does not get defaulted to sysdate in ERP collection
41744 /*    -- Default from operation effectivity date as SYSDATE if NULL
41745 
41746      v_sql_stmt := 60;
41747      lv_where_str :=
41748      ' AND NVL(from_op_effectivity_date,SYSDATE-36500 ) = SYSDATE-36500 ' ;
41749 
41750      lv_return := MSC_ST_UTIL.LOG_ERROR
41751                     (p_table_name        => 'MSC_ST_OPERATION_NETWORKS',
41752                      p_instance_code     => v_instance_code,
41753                      p_row               => lv_column_names,
41754                      p_severity          => G_SEV_WARNING,
41755                      p_message_text      => lv_message_text,
41756                      p_error_text        => lv_error_text,
41757                      p_batch_id          => lv_batch_id,
41758                      p_where_str         => lv_where_str,
41759                      p_col_name          => 'FROM_OP_EFFECTIVITY_DATE',
41760                      p_debug             => v_debug,
41761                      p_default_value     => 'SYSDATE');
41762 
41763       IF lv_return <> 0 THEN
41764         RAISE ex_logging_err;
41765       END IF;
41766 */
41767 
41768    -- Set the error message
41769     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41770                      (p_app_short_name    => 'MSC',
41771                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
41772                       p_message_text      => lv_message_text,
41773                       p_error_text        => lv_error_text,
41774                       p_token1            => 'COLUMN_NAME',
41775                       p_token_value1      => 'TO_OP_EFFECTIVITY_DATE',
41776                       p_token2            => 'DEFAULT_VALUE',
41777                       p_token_value2      => 'SYSDATE' );
41778 
41779       IF lv_return <> 0 THEN
41780         RAISE ex_logging_err;
41781       END IF;
41782 
41783     -- Default to operation effectivity date as SYSDATE if NULL
41784      v_sql_stmt := 61;
41785      lv_where_str :=
41786      ' AND NVL(to_op_effectivity_date,SYSDATE-36500 ) = SYSDATE-36500 ' ;
41787 
41788      lv_return := MSC_ST_UTIL.LOG_ERROR
41789                     (p_table_name        => 'MSC_ST_OPERATION_NETWORKS',
41790                      p_instance_code     => v_instance_code,
41791                      p_row               => lv_column_names,
41792                      p_severity          => G_SEV_WARNING,
41793                      p_message_text      => lv_message_text,
41794                      p_error_text        => lv_error_text,
41795                      p_batch_id          => lv_batch_id,
41796                      p_where_str         => lv_where_str,
41797                      p_col_name          => 'TO_OP_EFFECTIVITY_DATE',
41798                      p_debug             => v_debug,
41799                      p_default_value     => 'SYSDATE');
41800 
41801       IF lv_return <> 0 THEN
41802         RAISE ex_logging_err;
41803       END IF;
41804 
41805    -- Set the error message
41806     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41807                      (p_app_short_name    => 'MSC',
41808                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
41809                       p_message_text      => lv_message_text,
41810                       p_error_text        => lv_error_text,
41811                       p_token1            => 'COLUMN_NAME',
41812                       p_token_value1      => 'EFECTIVITY_DATE',
41813                       p_token2            => 'DEFAULT_VALUE',
41814                       p_token_value2      => 'SYSDATE' );
41815 
41816       IF lv_return <> 0 THEN
41817         RAISE ex_logging_err;
41818       END IF;
41819 
41820     -- Default effectivity date as SYSDATE if NULL
41821      v_sql_stmt := 62;
41822      lv_where_str :=
41823      ' AND NVL(efectivity_date,SYSDATE-36500 ) = SYSDATE-36500 ' ;
41824 
41825      lv_return := MSC_ST_UTIL.LOG_ERROR
41826                     (p_table_name        => 'MSC_ST_OPERATION_NETWORKS',
41827                      p_instance_code     => v_instance_code,
41828                      p_row               => lv_column_names,
41829                      p_severity          => G_SEV_WARNING,
41830                      p_message_text      => lv_message_text,
41831                      p_error_text        => lv_error_text,
41832                      p_batch_id          => lv_batch_id,
41833                      p_where_str         => lv_where_str,
41834                      p_col_name          => 'EFECTIVITY_DATE',
41835                      p_debug             => v_debug,
41836                      p_default_value     => 'SYSDATE');
41837 
41838       IF lv_return <> 0 THEN
41839         RAISE ex_logging_err;
41840       END IF;
41841 
41842 
41843 
41844    -- from_operation_seq_code or to_operation_seq_code cannot be null.
41845    -- Set the error message
41846     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41847                      (p_app_short_name    => 'MSC',
41848                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
41849                       p_message_text      => lv_message_text,
41850                       p_error_text        => lv_error_text,
41851                       p_token1            => 'COLUMN_NAME',
41852                       p_token_value1      => 'FROM_OPERATION_SEQ_CODE OR '
41853                                            ||'TO_OPERATION_SEQ_CODE');
41854 
41855       IF lv_return <> 0 THEN
41856         RAISE ex_logging_err;
41857       END IF;
41858 
41859      -- Error out the records whose from_operation_seq_code or to_operation_seq_code is NULL
41860 
41861     v_sql_stmt := 63;
41862     lv_sql_stmt :=
41863     'UPDATE    msc_st_operation_networks'
41864     ||' SET    process_flag     ='||G_ERROR_FLG||','
41865     ||'         error_text   = '||''''||lv_message_text||''''
41866     ||' WHERE ( NVL(from_operation_seq_code,'||NULL_VALUE||') ='||NULL_VALUE
41867     ||' OR      NVL(to_operation_seq_code,'||NULL_VALUE||') ='||NULL_VALUE ||')'
41868     ||' AND    sr_instance_code  = :v_instance_code'
41869     ||' AND    process_flag      = '||G_IN_PROCESS
41870     ||' AND    batch_id          = :lv_batch_id';
41871 
41872 
41873          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41874 
41875    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41876 
41877 
41878 
41879    -- from op_seq_code and to_op_seq_codes cannot be same
41880       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41881                      (p_app_short_name    => 'MSC',
41882                       p_error_code        => 'MSC_PP_FROM_TO_SAME',
41883                       p_message_text      => lv_message_text,
41884                       p_error_text        => lv_error_text,
41885                       p_token1            => 'COLUMN_NAME',
41886                       p_token_value1      => 'TO_OPERATION_SEQ_CODE');
41887 
41888       IF lv_return <> 0 THEN
41889         RAISE ex_logging_err;
41890       END IF;
41891 
41892      -- Error out the records whose from_operation_seq_code is same as to_op_seq_code
41893 
41894      v_sql_stmt := 64;
41895     lv_sql_stmt :=
41896     'UPDATE    msc_st_operation_networks'
41897     ||' SET    process_flag     ='||G_ERROR_FLG||','
41898     ||'         error_text   = '||''''||lv_message_text||''''
41899     ||' WHERE  NVL(to_operation_seq_code,'||NULL_VALUE||') = NVL(from_operation_seq_code,'||NULL_VALUE||')'
41900     ||' AND    sr_instance_code  = :v_instance_code'
41901     ||' AND    process_flag      = '||G_IN_PROCESS
41902     ||' AND    batch_id          = :lv_batch_id';
41903 
41904 
41905          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41906 
41907     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41908 
41909 
41910    -- Derive from_operation_sequence id
41911 
41912     v_sql_stmt := 65;
41913     lv_sql_Stmt :=
41914     'UPDATE     msc_st_operation_networks mson'
41915     ||' SET     from_op_seq_id=  (SELECT local_id'
41916     ||'         FROM msc_local_id_setup mlis'
41917     ||'         WHERE  mlis.char1 = mson.sr_instance_code'
41918     ||'         AND     NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
41919     ||'          =    NVL(mson.company_name,'||''''||NULL_CHAR||''''||') '
41920     ||'         AND    mlis.char3 = mson.organization_code'
41921     ||'         AND    mlis.char4 = mson.routing_name'
41922     ||'         AND    mlis.char5 = mson.from_operation_seq_code'
41923     ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
41924     ||'           =   NVL(mson.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
41925     ||'         AND    mlis.date1 = mson.FROM_OP_EFFECTIVITY_DATE'
41926     ||'         AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
41927     ||'         AND    mlis.instance_id = '||v_instance_id||')'
41928     ||'  WHERE      sr_instance_code = :v_instance_code'
41929     ||'  AND        process_flag     ='||G_IN_PROCESS
41930     ||'  AND        batch_id         = :lv_batch_id';
41931 
41932 
41933          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41934 
41935    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41936 
41937 
41938    -- from_op_seq_id cannot be null. Error out the records with null value for them.
41939    -- Set the error message
41940     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
41941                      (p_app_short_name    => 'MSC',
41942                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
41943                       p_message_text      => lv_message_text,
41944                       p_error_text        => lv_error_text,
41945                       p_token1            => 'COLUMN_NAMES',
41946                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
41947                                              ||'ORGANIZATION_CODE,ROUTING_NAME,'
41948                                              ||' FROM_OPERATION_SEQ_CODE,FROM_OP_EFFECTIVITY_DATE,'
41949                                              ||' ALTERNATE_ROUTING_DESIGNATOR'  ,
41950                       p_token2            => 'MASTER_TABLE',
41951                       p_token_value2      =>  'MSC_ST_ROUTING_OPERATIONS',
41952                       p_token3            =>  'CHILD_TABLE' ,
41953                       p_token_value3      =>  'MSC_ST_OPERATION_NETWORKS' );
41954 
41955      -- Error out the records whose from_op_seq_id is NULL
41956 
41957     v_sql_stmt := 66;
41958     lv_sql_stmt :=
41959     'UPDATE    msc_st_operation_networks'
41960     ||' SET    process_flag     ='||G_ERROR_FLG||','
41961     ||'         error_text   = '||''''||lv_message_text||''''
41962     ||' WHERE  NVL(from_op_seq_id,'||NULL_VALUE||') ='||NULL_VALUE
41963     ||' AND    sr_instance_code  = :v_instance_code'
41964     ||' AND    process_flag      = '||G_IN_PROCESS
41965     ||' AND    batch_id          = :lv_batch_id';
41966 
41967 
41968          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41969 
41970     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41971 
41972    -- Derive to_operation_sequence id
41973 
41974     v_sql_stmt := 67;
41975     lv_sql_Stmt :=
41976     'UPDATE     msc_st_operation_networks mson'
41977     ||' SET     to_op_seq_id=  (SELECT local_id'
41978     ||'         FROM msc_local_id_setup mlis'
41979     ||'         WHERE  mlis.char1 = mson.sr_instance_code'
41980     ||'         AND     NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
41981     ||'          =    NVL(mson.company_name,'||''''||NULL_CHAR||''''||') '
41982     ||'         AND    mlis.char3 = mson.organization_code'
41983     ||'         AND    mlis.char4 = mson.routing_name'
41984     ||'         AND    mlis.char5 = mson.to_operation_seq_code'
41985     ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
41986     ||'           =   NVL(mson.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
41987     ||'         AND    mlis.date1 = mson.TO_OP_EFFECTIVITY_DATE'
41988     ||'         AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
41989     ||'         AND    mlis.instance_id = '||v_instance_id||')'
41990     ||'  WHERE      sr_instance_code = :v_instance_code'
41991     ||'  AND        process_flag     ='||G_IN_PROCESS
41992     ||'  AND        batch_id         = :lv_batch_id';
41993 
41994 
41995          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
41996 
41997    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
41998 
41999 
42000    -- to_op_seq_id cannot be null. Error out the records with null value for them.
42001    -- Set the error message
42002     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42003                      (p_app_short_name    => 'MSC',
42004                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
42005                       p_message_text      => lv_message_text,
42006                       p_error_text        => lv_error_text,
42007                       p_token1            => 'COLUMN_NAMES',
42008                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
42009                                              ||'ORGANIZATION_CODE,ROUTING_NAME,'
42010                                              ||' TO_OPERATION_SEQ_CODE,TO_OP_EFFECTIVITY_DATE,'
42011                                              ||' ALTERNATE_ROUTING_DESIGNATOR'  ,
42012                       p_token2            => 'MASTER_TABLE',
42013                       p_token_value2      =>  'MSC_ST_ROUTING_OPERATIONS',
42014                       p_token3            =>  'CHILD_TABLE' ,
42015                       p_token_value3      =>  'MSC_ST_OPERATION_NETWORKS' );
42016 
42017       IF lv_return <> 0 THEN
42018         RAISE ex_logging_err;
42019       END IF;
42020 
42021      -- Error out the records whose to_op_seq_id is NULL
42022 
42023     v_sql_stmt := 68;
42024     lv_sql_stmt :=
42025     'UPDATE    msc_st_operation_networks'
42026     ||' SET    process_flag     ='||G_ERROR_FLG||','
42027     ||'         error_text   = '||''''||lv_message_text||''''
42028     ||' WHERE  NVL(to_op_seq_id,'||NULL_VALUE||') ='||NULL_VALUE
42029     ||' AND    sr_instance_code  = :v_instance_code'
42030     ||' AND    process_flag      = '||G_IN_PROCESS
42031     ||' AND    batch_id          = :lv_batch_id';
42032 
42033 
42034          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42035 
42036     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
42037 
42038 
42039    -- populating from_op_seq_num and to_op_seq_num
42040    v_sql_stmt := 69;
42041     lv_sql_Stmt :=
42042     'UPDATE     msc_st_operation_networks mson'
42043     ||' SET    from_op_seq_num= to_number(decode(length(rtrim(from_operation_seq_code,''0123456789'')),'
42044     ||'        NULL,from_operation_seq_code,''1'')),'
42045     ||'        to_op_seq_num= to_number(decode(length(rtrim(to_operation_seq_code,''0123456789'')),'
42046     ||'        NULL,to_operation_seq_code,''1''))'
42047     ||' WHERE      sr_instance_code = :v_instance_code'
42048     ||' AND        process_flag     ='||G_IN_PROCESS
42049     ||' AND        deleted_flag     <> 1'
42050     ||' AND        batch_id         = :lv_batch_id';
42051 
42052 
42053          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42054 
42055    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
42056 
42057 
42058 
42059    -- Set the error message
42060     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42061                      (p_app_short_name    => 'MSC',
42062                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
42063                       p_message_text      => lv_message_text,
42064                       p_error_text        => lv_error_text,
42065                       p_token1            => 'COLUMN_NAME',
42066                       p_token_value1      => 'TRANSITION_TYPE',
42067                       p_token2            => 'DEFAULT_VALUE',
42068                       p_token_value2      =>  G_TRANSITION_TYPE);
42069 
42070       IF lv_return <> 0 THEN
42071         RAISE ex_logging_err;
42072       END IF;
42073 
42074      -- Defualt transition type to 2(i.e. alternate) if not in (1,2)
42075      v_sql_stmt := 70;
42076      lv_where_str :=
42077      ' AND NVL(transition_type,'||NULL_VALUE||') NOT IN (1,2)';
42078 
42079      lv_return := MSC_ST_UTIL.LOG_ERROR
42080                     (p_table_name       => 'MSC_ST_OPERATION_NETWORKS',
42081                      p_instance_code     => v_instance_code,
42082                      p_row               => lv_column_names,
42083                      p_severity          => G_SEV_WARNING,
42084                      p_message_text      => lv_message_text,
42085                      p_error_text        => lv_error_text,
42086                      p_batch_id          => lv_batch_id,
42087                      p_where_str         => lv_where_str,
42088                      p_col_name          => 'TRANSITION_TYPE',
42089                      p_debug             => v_debug,
42090                      p_default_value     => G_TRANSITION_TYPE);
42091 
42092 
42093       IF lv_return <> 0 THEN
42094         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_error_text);
42095       END IF;
42096 
42097     -- Set the error message
42098     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42099                      (p_app_short_name    => 'MSC',
42100                       p_error_code        => 'MSC_PP_END_GT_ST_DATE',
42101                       p_message_text      => lv_message_text,
42102                       p_error_text        => lv_error_text,
42103                       p_token1            => 'COLUMN_NAME',
42104                       p_token_value1      => 'EFFECTIVITY_DATE');
42105 
42106       IF lv_return <> 0 THEN
42107         RAISE ex_logging_err;
42108       END IF;
42109 
42110      -- Disable date should be greater than effectivity date if a value is there for disable date
42111 
42112      v_sql_stmt := 71;
42113     lv_sql_stmt :=
42114     'UPDATE    msc_st_operation_networks'
42115     ||' SET    process_flag     ='||G_ERROR_FLG||','
42116     ||'         error_text   = '||''''||lv_message_text||''''
42117     ||' WHERE  NVL(DISABLE_DATE,SYSDATE-36500) <>  SYSDATE-36500'
42118     ||' AND DISABLE_DATE <= EFECTIVITY_DATE '
42119     ||' AND    sr_instance_code  = :v_instance_code'
42120     ||' AND    process_flag      = '||G_IN_PROCESS
42121     ||' AND        deleted_flag     <> 1'
42122     ||' AND    batch_id          = :lv_batch_id';
42123 
42124 
42125          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42126 
42127     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
42128 
42129 
42130     --Call to customised validation.
42131 
42132       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
42133         (ERRBUF         => lv_error_text,
42134          RETCODE        => lv_return,
42135          pBatchID       => lv_batch_id,
42136          pInstanceCode  => v_instance_code,
42137          pEntityName    => 'MSC_ST_OPERATION_NETWORKS',
42138          pInstanceID    => v_instance_id);
42139 
42140       IF NVL(lv_return,0) <> 0 THEN
42141         RAISE ex_logging_err;
42142       END IF;
42143 
42144    COMMIT;
42145  END LOOP;
42146 
42147     -- There should be at the max only one primary path from a from_operation_seq_code
42148     -- Else error out all the records for that from_operation_seq_code
42149 
42150     -- Set the error message
42151     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42152                      (p_app_short_name    => 'MSC',
42153                       p_error_code        => 'MSC_PP_MULTI_PRI_PATH',
42154                       p_message_text      => lv_message_text,
42155                       p_error_text        => lv_error_text,
42156                       p_token1            => 'COLUMN_NAME',
42157                       p_token_value1      => 'TRANSITION_TYPE');
42158 
42159       IF lv_return <> 0 THEN
42160         RAISE ex_logging_err;
42161       END IF;
42162 
42163      v_sql_stmt := 72;
42164     lv_sql_stmt :=
42165   'UPDATE   msc_st_operation_networks mson1'
42166   ||' SET      process_flag = '||G_ERROR_FLG||','
42167   ||'         error_text   = '||''''||lv_message_text||''''
42168   ||' WHERE  1 <  ( SELECT COUNT(*) '
42169   ||'          FROM msc_st_operation_networks mson2'
42170   ||'          WHERE mson2.assembly_name      = mson1.assembly_name'
42171   ||'          AND   mson2.organization_code  = mson1.organization_code'
42172   ||'          AND   mson2.from_operation_seq_code  = mson1.from_operation_seq_code'
42173   ||'          AND   mson2.sr_instance_code   = mson1.sr_instance_code'
42174   ||'          AND   NVL(mson2.company_name,'||''''||NULL_CHAR||''''||') '
42175   ||'                 =    NVL(mson1.company_name,'||''''||NULL_CHAR||''''||') '
42176   ||'          AND   mson2.transition_type = 1 '
42177   ||'          AND   NVL(mson2.routing_name,'||''''||NULL_CHAR||''''||') '
42178   ||'                = NVL(mson1.routing_name, '||''''||NULL_CHAR||''''||') '
42179   ||'          AND   NVL(mson2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
42180   ||'                = NVL(mson1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
42181   ||'          AND   mson2.process_flag   ='||G_IN_PROCESS
42182   ||'          AND   mson2.deleted_flag <>1 '
42183   ||'         GROUP BY sr_instance_code,company_name,organization_code,from_operation_seq_code,'
42184   ||'         routing_name,assembly_name, alternate_routing_designator )'
42185   ||'  AND     mson1.process_flag          = '||G_IN_PROCESS
42186   ||'  AND     mson1.deleted_flag <>1 '
42187   ||'  AND     mson1.sr_instance_code       = :v_instance_code';
42188 
42189 
42190 
42191          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42192 
42193    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
42194 
42195     -- The summation of the planning_pct's from a from_operation_sed_code should be exactly 100.
42196     -- Else error out all the records for that from_operation_seq_code
42197 
42198     -- Set the error message
42199     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42200                      (p_app_short_name    => 'MSC',
42201                       p_error_code        => 'MSC_INVALID_PLANNING_PCT',
42202                       p_message_text      => lv_message_text,
42203                       p_error_text        => lv_error_text,
42204                       p_token1            => 'COLUMN_NAME',
42205                       p_token_value1      => 'PLANNING_PCT');
42206 
42207       IF lv_return <> 0 THEN
42208         RAISE ex_logging_err;
42209       END IF;
42210 
42211      v_sql_stmt := 73;
42212     lv_sql_stmt :=
42213   'UPDATE   msc_st_operation_networks mson1'
42214   ||' SET      process_flag = '||G_ERROR_FLG||','
42215   ||'         error_text   = '||''''||lv_message_text||''''
42216   ||' WHERE 100 <>   ( SELECT SUM(planning_pct) '
42217   ||'          FROM msc_st_operation_networks mson2'
42218   ||'          WHERE mson2.assembly_name      = mson1.assembly_name'
42219   ||'          AND   mson2.organization_code  = mson1.organization_code'
42220   ||'          AND   mson2.from_operation_seq_code  = mson1.from_operation_seq_code'
42221   ||'          AND   mson2.sr_instance_code   = mson1.sr_instance_code'
42222   ||'          AND   NVL(mson2.company_name,'||''''||NULL_CHAR||''''||') '
42223   ||'                 =    NVL(mson1.company_name,'||''''||NULL_CHAR||''''||') '
42224   ||'          AND   NVL(mson2.routing_name,'||''''||NULL_CHAR||''''||') '
42225   ||'                = NVL(mson1.routing_name, '||''''||NULL_CHAR||''''||') '
42226   ||'          AND   NVL(mson2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
42227   ||'                = NVL(mson1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
42228   ||'          AND   mson2.process_flag   ='||G_IN_PROCESS
42229   ||'          AND   mson2.deleted_flag <>1 '
42230   ||'         GROUP BY sr_instance_code,company_name,organization_code,from_operation_seq_code,'
42231   ||'         routing_name,assembly_name, alternate_routing_designator )'
42232   ||'  AND     mson1.process_flag          = '||G_IN_PROCESS
42233   ||'  AND     mson1.deleted_flag <>1 '
42234   ||'  AND     mson1.sr_instance_code       = :v_instance_code';
42235 
42236 
42237          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42238 
42239   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
42240 
42241   -- Set the process flag as Valid and derive sr_instance_id
42242     lv_return :=
42243     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_OPERATION_NETWORKS',
42244                                p_instance_id    => v_instance_id,
42245                                p_instance_code  => v_instance_code,
42246                                p_process_flag   => G_VALID,
42247                                p_error_text     => lv_error_text,
42248                                p_debug          => v_debug);
42249 
42250       IF lv_return < 0 THEN
42251         RAISe ex_logging_err;
42252       END IF;
42253 
42254 
42255     -- At the end calling  the LOG_ERROR for logging all errored out records
42256 
42257    lv_return :=
42258    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_OPERATION_NETWORKS',
42259                          p_instance_code => v_instance_code,
42260                          p_row           => lv_column_names,
42261                          p_severity      => G_SEV_ERROR,
42262                          p_message_text  => NULL,
42263                          p_error_text    => lv_error_text,
42264                          p_debug         => v_debug );
42265 
42266     IF lv_return < 0 THEN
42267         RAISe ex_logging_err;
42268     END IF;
42269 
42270   -- till here added for pre processing Opertion Networks (OSFM Integration)
42271 
42272 
42273   EXCEPTION
42274     WHEN too_many_rows THEN
42275          lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_ROUTING'||'('
42276                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
42277          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
42278          ROLLBACK ;
42279 
42280     WHEN ex_logging_err THEN
42281         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
42282         ROLLBACK;
42283 
42284     WHEN OTHERS THEN
42285       lv_error_text :=substr('MSC_CL_PRE_PROCESS.LOAD_ROUTING'||'('
42286                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
42287       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
42288       ROLLBACK;
42289 
42290   END LOAD_ROUTING ;
42291 
42292 
42293  /*==========================================================================+
42294   | DESCRIPTION  : This function inserts records into process effectivity    |
42295   +==========================================================================*/
42296 
42297   PROCEDURE  LOAD_PROCESS_EFFECTIVITY IS
42298 
42299   lv_error_text     VARCHAR2(250);
42300   lv_sql_stmt       VARCHAR2(5000);
42301 
42302 
42303   BEGIN
42304   -- populating preference for the bugfix 2862276
42305   v_sql_stmt := 1;
42306   lv_sql_stmt :=
42307   'INSERT INTO msc_st_process_effectivity'
42308   ||'( item_id,'
42309   ||' organization_id,'
42310   ||' bill_sequence_id,'
42311   ||' routing_sequence_id,'
42312   ||' effectivity_date,'
42313   ||' line_id,'
42314   ||' preference,'
42315   ||' deleted_flag,'
42316   ||' sr_instance_id,'
42317   ||' refresh_id, '
42318   ||' ITEM_PROCESS_COST,'
42319   ||' last_update_date,'
42320   ||' last_updated_by, '
42321   ||' creation_date,'
42322   ||' created_by  )'
42323   ||'  SELECT '
42324   ||'  assembly_item_id,'
42325   ||'  organization_id,'
42326   ||'  bill_sequence_id,'
42327   ||'  routing_sequence_id,'
42328   ||'  sysdate,'
42329   ||   NULL_VALUE||','
42330   ||'  decode(alternate_bom_designator,NULL, DECODE( alternate_routing_designator,NULL, 1,2),2),'
42331   ||'  deleted_flag,'
42332   ||   v_instance_id||','
42333   ||'  REFRESH_ID,'
42334   ||'  ITEM_PROCESS_COST ,'
42335   ||'  last_update_date,'
42336   ||'  last_updated_by, '
42337   ||'  creation_date,'
42338   ||'  created_by '
42339   ||'   FROM msc_st_routings'
42340   ||'   WHERE sr_instance_id  = :v_instance_id';
42341 
42342 
42343 
42344 
42345             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42346 
42347    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_id;
42348 
42349 
42350 
42351    v_sql_stmt := 2;
42352   lv_sql_stmt :=
42353   'INSERT INTO msc_st_process_effectivity'
42354   ||'( item_id,'
42355   ||' organization_id,'
42356   ||' bill_sequence_id,'
42357   ||' effectivity_date,'
42358   ||' line_id,'
42359   ||' preference,'
42360   ||' deleted_flag,'
42361   ||' sr_instance_id,'
42362   ||' refresh_id,'
42363   ||' ITEM_PROCESS_COST,'
42364   ||' last_update_date,'
42365   ||' last_updated_by, '
42366   ||' creation_date,'
42367   ||' created_by  )'
42368   ||'  SELECT '
42369   ||'  assembly_item_id,'
42370   ||'  organization_id,'
42371   ||'  bill_sequence_id,'
42372   ||'  sysdate,'
42373   ||   NULL_VALUE||','
42374   ||'  decode(msb.alternate_bom_designator,NULL,1,2),'
42375   ||'  deleted_flag,'
42376   ||   v_instance_id||','
42377   ||'  REFRESH_ID,'
42378   ||'  ITEM_PROCESS_COST ,'
42379   ||'  last_update_date,'
42380   ||'  last_updated_by, '
42381   ||'  creation_date,'
42382   ||'  created_by '
42383   ||'   FROM msc_st_boms msb'
42384   ||'   WHERE sr_instance_id  = :v_instance_id'
42385   ||'   AND   NOT EXISTS ( SELECT 1 from msc_st_routings msr '
42386   ||'                      WHERE  msr.bill_sequence_id = msb.bill_sequence_id '
42387   ||'                      AND    msr.sr_instance_id = msb.sr_instance_id )';
42388 
42389 
42390             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42391 
42392    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_id;
42393 
42394 
42395    COMMIT;
42396 
42397   EXCEPTION
42398     WHEN OTHERS THEN
42399       ROLLBACK;
42400       lv_error_text :=substr('MSC_CL_PRE_PROCESS.LOAD_PROCESS_EFFECTIVITY'||'('
42401                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
42402       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
42403 
42404   END LOAD_PROCESS_EFFECTIVITY ;
42405 
42406 
42407 /*==========================================================================+
42408 | DESCRIPTION  : This procedure validates Work Order supply related reocrds |
42409 |                in the msc_st_supplies table. Order_types considered are   |
42410 |                Supply Source                    Order Type                |
42411 |                --------------------------------------------               |
42412 |                Discrete Job                     3                         |
42413 |                Non-Standard Job                 7                         |
42414 |                Flow Schedule                   27                         |
42415 |                Discrete job by-product         14                         |
42416 |                Nonstandard job by-product      15                         |
42417 +==========================================================================*/
42418   PROCEDURE LOAD_WO_SUPPLY IS
42419   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
42420   lb_rowid               RowidTab;
42421   lv_return              NUMBER;
42422   lv_error_text          VARCHAR2(250);
42423   lv_where_str           VARCHAR2(5000);
42424   lv_sql_stmt            VARCHAR2(5000);
42425   lv_column_names        VARCHAR2(5000);                   --stores concatenated column names
42426   lv_message_text        msc_errors.error_text%TYPE;
42427   lv_batch_id            msc_st_supplies.batch_id%TYPE;
42428   ex_logging_err         EXCEPTION;
42429 
42430   CURSOR c1(p_batch_id NUMBER) IS
42431     SELECT rowid
42432     FROM   msc_st_supplies
42433     WHERE  order_type              IN(3,7,27,14,15)
42434     AND    process_flag            IN (G_IN_PROCESS,G_ERROR_FLG)
42435     AND    NVL(batch_id,NULL_VALUE)=p_batch_id
42436     AND    sr_instance_code        = v_instance_code;
42437 
42438   CURSOR c2(p_batch_id NUMBER) IS
42439     SELECT rowid
42440     FROM   msc_st_supplies
42441     WHERE  NVL(wip_entity_id,NULL_VALUE) = NULL_VALUE
42442     AND    process_flag                  = G_IN_PROCESS
42443     AND    NVL(batch_id,NULL_VALUE)      =p_batch_id
42444     AND    sr_instance_code              = v_instance_code;
42445 
42446   CURSOR c3(p_batch_id NUMBER) IS
42447     SELECT max(rowid)
42448     FROM   msc_st_supplies
42449     WHERE  NVL(schedule_group_id,NULL_VALUE) = NULL_VALUE
42450     AND    deleted_flag                      = SYS_NO
42451     AND    process_flag                      = G_IN_PROCESS
42452     AND    NVL(batch_id,NULL_VALUE)          = p_batch_id
42453     AND    sr_instance_code                  = v_instance_code
42454     GROUP BY sr_instance_code,company_name,organization_code,schedule_group_name;
42455 
42456   CURSOR c4(p_batch_id NUMBER) IS
42457     SELECT rowid
42458     FROM   msc_st_supplies
42459     WHERE  process_flag     = G_IN_PROCESS
42460     AND    sr_instance_code = v_instance_code
42461     AND    batch_id         = p_batch_id
42462     AND    NVL(JOB_OP_SEQ_NUM, NULL_VALUE) = NULL_VALUE
42463     AND    NVL(JOB_OP_SEQ_CODE, NULL_CHAR) <> NULL_CHAR
42464     AND    order_type        IN (3,7,14,15)
42465     AND    deleted_flag     = SYS_NO;
42466 
42467   BEGIN
42468 
42469     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42470                  (p_app_short_name    => 'MSC',
42471                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
42472                   p_message_text      => lv_message_text,
42473                   p_error_text        => lv_error_text);
42474 
42475     IF lv_return <> 0 THEN
42476       RAISE ex_logging_err;
42477     END IF;
42478 
42479     --Duplicate records check for the records whose source is XML for
42480     --WO supplies
42481     v_sql_stmt := 01;
42482     lv_sql_stmt :=
42483     'UPDATE  msc_st_supplies mss1'
42484     ||' SET     process_flag = '||G_ERROR_FLG||','
42485     ||'         error_text   = '||''''||lv_message_text||''''
42486     ||' WHERE   message_id <  (SELECT MAX(message_id)'
42487     ||'         FROM   msc_st_supplies mss2'
42488     ||'         WHERE  mss2.sr_instance_code'
42489     ||'                = mss1.sr_instance_code'
42490     ||'         AND    NVL(mss2.company_name,'||''''||NULL_CHAR||''''||') = '
42491     ||'                NVL(mss1.company_name,'||''''||NULL_CHAR||''''||')'
42492     ||'         AND    mss2.wip_entity_name = mss1.wip_entity_name '
42493     ||'         AND    mss2.order_type    = mss1.order_type'
42494     ||'         AND    mss2.process_flag      = '||G_IN_PROCESS
42495     ||'         AND    mss2.organization_code  = mss1.organization_code'
42496     ||'         AND    NVL(mss2.message_id,'||NULL_VALUE||')<>'||NULL_VALUE||')'
42497     ||' AND    mss1.order_type         IN(3,7,27,14,15)'
42498     ||' AND    mss1.process_flag       = '||G_IN_PROCESS
42499     ||' AND    mss1.sr_instance_code   = :v_instance_code'
42500     ||' AND    NVL(mss1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
42501 
42502 
42503       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42504 
42505     EXECUTE IMMEDIATE lv_sql_stmt
42506             USING     v_instance_code;
42507 
42508 
42509     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42510                  (p_app_short_name    => 'MSC',
42511                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
42512                   p_message_text      => lv_message_text,
42513                   p_error_text        => lv_error_text);
42514 
42515     IF lv_return <> 0 THEN
42516       RAISE ex_logging_err;
42517     END IF;
42518 
42519     --Duplicate records check for the records whose source is other than XML
42520     --Different SQL is used because in XML we can identify the latest records
42521     --whereas in batch load we cannot.
42522     v_sql_stmt := 02;
42523     lv_sql_stmt :=
42524     'UPDATE  msc_st_supplies mss1 '
42525     ||' SET     process_flag  = '||G_ERROR_FLG||','
42526     ||'         error_text   = '||''''||lv_message_text||''''
42527     ||' WHERE   EXISTS( SELECT 1 '
42528     ||'         FROM   msc_st_supplies mss2'
42529     ||'         WHERE  mss2.sr_instance_code'
42530     ||'                = mss1.sr_instance_code'
42531     ||'         AND    NVL(mss2.company_name,   '||''''||NULL_CHAR||''''||')= '
42532     ||'                NVL(mss1.company_name,   '||''''||NULL_CHAR||''''||')'
42533     ||'         AND    mss2.wip_entity_name = mss1.wip_entity_name '
42534     ||'         AND    mss2.order_type    = mss1.order_type'
42535     ||'         AND    mss2.process_flag  = '||G_IN_PROCESS
42536     ||'         AND    mss2.organization_code = mss1.organization_code'
42537     ||'         AND    NVL(mss2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
42538     ||'         GROUP BY sr_instance_code,wip_entity_name,organization_code,company_name,'
42539     ||'                order_type'
42540     ||'         HAVING COUNT(*) > 1)'
42541     ||' AND     mss1.order_type    IN (3,7,27,14,15)'
42542     ||' AND     mss1.process_flag     = '||G_IN_PROCESS
42543     ||' AND     mss1.sr_instance_code = :v_instance_code'
42544     ||' AND     NVL(mss1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
42545 
42546 
42547       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42548 
42549     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
42550 
42551     lv_column_names :=
42552     'ITEM_NAME                      ||''~''||'
42553     ||'ORGANIZATION_CODE            ||''~''||'
42554     ||'NEW_SCHEDULE_DATE            ||''~''||'
42555     ||'FIRM_PLANNED_TYPE            ||''~''||'
42556     ||'WIP_ENTITY_NAME              ||''~''||'
42557     ||'SR_INSTANCE_CODE             ||''~''||'
42558     ||'REVISION                     ||''~''||'
42559     ||'UNIT_NUMBER                  ||''~''||'
42560     ||'NEW_WIP_START_DATE           ||''~''||'
42561     ||'NEW_ORDER_QUANTITY           ||''~''||'
42562     ||'ALTERNATE_BOM_DESIGNATOR     ||''~''||'
42563     ||'ALTERNATE_ROUTING_DESIGNATOR ||''~''||'
42564     ||'LINE_CODE                    ||''~''||'
42565     ||'PROJECT_NUMBER               ||''~''||'
42566     ||'TASK_NUMBER                  ||''~''||'
42567     ||'PLANNING_GROUP               ||''~''||'
42568     ||'SCHEDULE_GROUP_NAME          ||''~''||'
42569     ||'BUILD_SEQUENCE               ||''~''||'
42570     ||'WO_LATENESS_COST             ||''~''||'
42571     ||'IMPLEMENT_PROCESSING_DAYS    ||''~''||'
42572     ||'LATE_SUPPLY_DATE             ||''~''||'
42573     ||'LATE_SUPPLY_QTY              ||''~''||'
42574     ||'QTY_SCRAPPED                 ||''~''||'
42575     ||'QTY_COMPLETED                ||''~''||'
42576     ||'WIP_STATUS_CODE              ||''~''||'
42577     ||'BILL_NAME                    ||''~''||'
42578     ||'ROUTING_NAME                 ||''~''||'
42579     ||'DELETED_FLAG                 ||''~''||'
42580     ||'COMPANY_NAME                 ||''~''||'
42581     ||'ORDER_TYPE                   ||''~''||'
42582     ||'ORDER_NUMBER';
42583 
42584     LOOP
42585       v_sql_stmt := 03;
42586       SELECT msc_st_batch_id_s.NEXTVAL
42587       INTO   lv_batch_id
42588       FROM   dual;
42589 
42590       v_sql_stmt := 04;
42591       lv_sql_stmt :=
42592       ' UPDATE   msc_st_supplies '
42593       ||' SET    batch_id  = :lv_batch_id'
42594       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
42595       ||' AND    order_type IN(3,7,27,14,15)'
42596       ||' AND    sr_instance_code               = :v_instance_code'
42597       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
42598       ||' AND    rownum                        <= '||v_batch_size;
42599 
42600 
42601         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42602 
42603       EXECUTE IMMEDIATE lv_sql_stmt
42604               USING     lv_batch_id,
42605                         v_instance_code;
42606 
42607       EXIT WHEN SQL%NOTFOUND;
42608 
42609       OPEN c1(lv_batch_id);
42610       FETCH c1 BULK COLLECT INTO lb_rowid;
42611       CLOSE c1;
42612 
42613       v_sql_stmt := 03;
42614       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
42615       UPDATE msc_st_supplies
42616       SET    st_transaction_id   = msc_st_supplies_s.NEXTVAL,
42617              refresh_id          = v_refresh_id,
42618              last_update_date    = v_current_date,
42619              last_updated_by     = v_current_user,
42620              creation_date       = v_current_date,
42621              created_by          = v_current_user
42622       WHERE  rowid               = lb_rowid(j);
42623 
42624       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42625                      (p_app_short_name    => 'MSC',
42626                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
42627                       p_message_text      => lv_message_text,
42628                       p_error_text        => lv_error_text,
42629                       p_token1            => 'COLUMN_NAME',
42630                       p_token_value1      => 'DELETED_FLAG',
42631                       p_token2            => 'DEFAULT_VALUE',
42632                       p_token_value2      => SYS_NO);
42633 
42634       IF lv_return <> 0 THEN
42635         RAISE ex_logging_err;
42636       END IF;
42637 
42638       lv_where_str :=
42639       ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
42640       --Log a warning for those records where the deleted_flag has a value other
42641       --SYS_NO
42642       lv_return := MSC_ST_UTIL.LOG_ERROR
42643                      (p_table_name        => 'MSC_ST_SUPPLIES',
42644                       p_instance_code     => v_instance_code,
42645                       p_row               => lv_column_names,
42646                       p_severity          => G_SEV_WARNING,
42647                       p_message_text      => lv_message_text,
42648                       p_error_text        => lv_error_text,
42649                       p_batch_id          => lv_batch_id,
42650                       p_where_str         => lv_where_str,
42651                       p_col_name          => 'DELETED_FLAG',
42652                       p_debug             => v_debug,
42653                       p_default_value     => SYS_NO);
42654 
42655       IF lv_return <> 0 THEN
42656         RAISE ex_logging_err;
42657       END IF;
42658 
42659       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42660                      (p_app_short_name    => 'MSC',
42661                       p_error_code        => 'MSC_PP_INVALID_VALUE',
42662                       p_message_text      => lv_message_text,
42663                       p_error_text        => lv_error_text,
42664                       p_token1            => 'COLUMN_NAME',
42665                       p_token_value1      => 'ORGANIZATION_CODE');
42666 
42667       IF lv_return <> 0 THEN
42668         RAISE ex_logging_err;
42669       END IF;
42670 
42671       --Derive Organization_id
42672       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
42673                      (p_table_name       => 'MSC_ST_SUPPLIES',
42674                       p_org_partner_name => 'ORGANIZATION_CODE',
42675                       p_org_partner_id   => 'ORGANIZATION_ID',
42676                       p_instance_code    => v_instance_code,
42677                       p_partner_type     => G_ORGANIZATION,
42678                       p_error_text       => lv_error_text,
42679                       p_batch_id         => lv_batch_id,
42680                       p_severity         => G_SEV_ERROR,
42681                       p_message_text     => lv_message_text,
42682                       p_debug            => v_debug,
42683                       p_row              => lv_column_names);
42684 
42685       IF lv_return <> 0 THEN
42686         RAISE ex_logging_err;
42687       END IF;
42688 
42689       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42690                      (p_app_short_name    => 'MSC',
42691                       p_error_code        => 'MSC_PP_INVALID_VALUE',
42692                       p_message_text      => lv_message_text,
42693                       p_error_text        => lv_error_text,
42694                       p_token1            => 'COLUMN_NAME',
42695                       p_token_value1      => 'ITEM_NAME');
42696 
42697       IF lv_return <> 0 THEN
42698         RAISE ex_logging_err;
42699       END IF;
42700 
42701       --Derive Inventory_item_id
42702       lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
42703                      (p_table_name       => 'MSC_ST_SUPPLIES',
42704                       p_item_col_name    => 'ITEM_NAME',
42705                       p_item_col_id      => 'INVENTORY_ITEM_ID',
42706                       p_instance_id      => v_instance_id,
42707                       p_instance_code    => v_instance_code,
42708                       p_error_text       => lv_error_text,
42709                       p_batch_id         => lv_batch_id,
42710                       p_severity         => G_SEV_ERROR,
42711                       p_message_text     => lv_message_text,
42712                       p_debug            => v_debug,
42713                       p_row              => lv_column_names);
42714 
42715       IF lv_return <> 0 THEN
42716         RAISE ex_logging_err;
42717       END IF;
42718 
42719       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42720                      (p_app_short_name    => 'MSC',
42721                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
42722                       p_message_text      => lv_message_text,
42723                       p_error_text        => lv_error_text,
42724                       p_token1            => 'COLUMN_NAME',
42725                       p_token_value1      => 'NEW_SCHEDULE_DATE OR NEW_ORDER_QUANTITY');
42726 
42727       IF lv_return <> 0 THEN
42728         RAISE ex_logging_err;
42729       END IF;
42730 
42731       v_sql_stmt := 04;
42732       lv_sql_stmt :=
42733       'UPDATE msc_st_supplies '
42734       ||' SET   error_text   = '||''''||lv_message_text||''''||','
42735       ||'     process_flag = '||G_ERROR_FLG
42736       ||' WHERE (NVL(new_schedule_date,sysdate-36500) = sysdate-36500'
42737       ||' OR    NVL(new_order_quantity,'||NULL_VALUE|| ')= '||NULL_VALUE||')'
42738       ||' AND   deleted_flag                   = '||SYS_NO
42739       ||' AND   process_flag                   = '||G_IN_PROCESS
42740       ||' AND   NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
42741       ||' AND   sr_instance_code               = :v_instance_code';
42742 
42743 
42744         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42745 
42746       EXECUTE IMMEDIATE lv_sql_stmt
42747               USING     lv_batch_id,
42748                         v_instance_code;
42749 
42750       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42751                      (p_app_short_name    => 'MSC',
42752                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
42753                       p_message_text      => lv_message_text,
42754                       p_error_text        => lv_error_text,
42755                       p_token1            => 'COLUMN_NAME',
42756                       p_token_value1      => 'WIP_ENTITY_NAME');
42757 
42758       IF lv_return <> 0 THEN
42759         RAISE ex_logging_err;
42760       END IF;
42761 
42762       v_sql_stmt := 05;
42763       lv_sql_stmt :=
42764       'UPDATE   msc_st_supplies '
42765       ||' SET   error_text   = '||''''||lv_message_text||''''||','
42766       ||'       process_flag = '||G_ERROR_FLG
42767       ||' WHERE NVL(wip_entity_name,'||''''||NULL_CHAR||''''||') '
42768       ||'       =                   '||''''||NULL_CHAR||''''
42769       ||' AND   process_flag                  = '||G_IN_PROCESS
42770       ||' AND   NVL(batch_id,'||NULL_VALUE||')= :lv_batch_id'
42771       ||' AND   sr_instance_code              = :v_instance_code';
42772 
42773 
42774         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42775 
42776       EXECUTE IMMEDIATE lv_sql_stmt
42777               USING     lv_batch_id,
42778                         v_instance_code;
42779 
42780       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42781                      (p_app_short_name    => 'MSC',
42782                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
42783                       p_message_text      => lv_message_text,
42784                       p_error_text        => lv_error_text,
42785                       p_token1            => 'COLUMN_NAME',
42786                       p_token_value1      => 'FIRM_PLANNED_TYPE',
42787                       p_token2            => 'DEFAULT_VALUE',
42788                       p_token_value2      => SYS_NO);
42789 
42790       IF lv_return <> 0 THEN
42791         RAISE ex_logging_err;
42792       END IF;
42793 
42794       lv_where_str :=
42795       ' AND NVL(firm_planned_type,'||NULL_VALUE||') NOT IN(1,2)'
42796       ||' AND deleted_flag = '||SYS_NO;
42797 
42798       --Log a warning for those records where the firm_planned_type has a value
42799       --other than 1 and 2
42800 
42801       lv_return := MSC_ST_UTIL.LOG_ERROR
42802                      (p_table_name        => 'MSC_ST_SUPPLIES',
42803                       p_instance_code     => v_instance_code,
42804                       p_row               => lv_column_names,
42805                       p_severity          => G_SEV_WARNING,
42806                       p_message_text      => lv_message_text,
42807                       p_error_text        => lv_error_text,
42808                       p_batch_id          => lv_batch_id,
42809                       p_where_str         => lv_where_str,
42810                       p_col_name          => 'FIRM_PLANNED_TYPE',
42811                       p_debug             => v_debug,
42812                       p_default_value     => SYS_NO);
42813 
42814       IF lv_return <> 0 THEN
42815         RAISE ex_logging_err;
42816       END IF;
42817 
42818       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42819                      (p_app_short_name    => 'MSC',
42820                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
42821                       p_message_text      => lv_message_text,
42822                       p_error_text        => lv_error_text,
42823                       p_token1            => 'COLUMN_NAME',
42824                       p_token_value1      => 'WIP_STATUS_CODE',
42825                       p_token2            => 'DEFAULT_VALUE',
42826                       p_token_value2      => 1);
42827 
42828       IF lv_return <> 0 THEN
42829         RAISE ex_logging_err;
42830       END IF;
42831 
42832       lv_where_str := '   AND wip_status_code   <= 1'
42833                       ||' AND wip_status_code   >= 15'
42834                       ||' AND deleted_flag       = '||SYS_NO;
42835 
42836       --Log a warning for those records where the wip_status_code has a value other
42837       --than SYS_NO
42838 
42839       lv_return := MSC_ST_UTIL.LOG_ERROR
42840                      (p_table_name        => 'MSC_ST_SUPPLIES',
42841                       p_instance_code     => v_instance_code,
42842                       p_row               => lv_column_names,
42843                       p_severity          => G_SEV_WARNING,
42844                       p_message_text      => lv_message_text,
42845                       p_error_text        => lv_error_text,
42846                       p_batch_id          => lv_batch_id,
42847                       p_where_str         => lv_where_str,
42848                       p_col_name          => 'WIP_STATUS_CODE',
42849                       p_debug             => v_debug,
42850                       p_default_value     => 1);
42851 
42852       IF lv_return <> 0 THEN
42853         RAISE ex_logging_err;
42854       END IF;
42855 
42856       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42857                      (p_app_short_name    => 'MSC',
42858                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
42859                       p_message_text      => lv_message_text,
42860                       p_error_text        => lv_error_text,
42861                       p_token1            => 'COLUMN_NAME',
42862                       p_token_value1      => 'DISPOSITION_STATUS_TYPE',
42863                       p_token2            => 'DEFAULT_VALUE',
42864                       p_token_value2      => 1);
42865 
42866       IF lv_return <> 0 THEN
42867         RAISE ex_logging_err;
42868       END IF;
42869 
42870       lv_where_str :=
42871       ' AND NVL(disposition_status_type,'||NULL_VALUE||') NOT IN(1,2)'
42872       ||' AND deleted_flag       = '||SYS_NO;
42873 
42874       --Log a warning for those records where the firm_planned_type has a value other
42875       --than SYS_NO
42876 
42877       lv_return := MSC_ST_UTIL.LOG_ERROR
42878                      (p_table_name        => 'MSC_ST_SUPPLIES',
42879                       p_instance_code     => v_instance_code,
42880                       p_row               => lv_column_names,
42881                       p_severity          => G_SEV_WARNING,
42882                       p_message_text      => lv_message_text,
42883                       p_error_text        => lv_error_text,
42884                       p_batch_id          => lv_batch_id,
42885                       p_where_str         => lv_where_str,
42886                       p_col_name          => 'DISPOSITION_STATUS_TYPE',
42887                       p_debug             => v_debug,
42888                       p_default_value     => 1);
42889 
42890       IF lv_return <> 0 THEN
42891         RAISE ex_logging_err;
42892       END IF;
42893 
42894       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
42895                      (p_app_short_name    => 'MSC',
42896                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
42897                       p_message_text      => lv_message_text,
42898                       p_error_text        => lv_error_text,
42899                       p_token1            => 'COLUMN_NAME',
42900                       p_token_value1      => 'WIP_SUPPLY_TYPE',
42901                       p_token2            => 'DEFAULT_VALUE',
42902                       p_token_value2      => 1);
42903 
42904       IF lv_return <> 0 THEN
42905         RAISE ex_logging_err;
42906       END IF;
42907 
42908       lv_where_str := '   AND wip_supply_type <= 1'
42909                       ||' AND wip_supply_type >= 7'
42910                       ||' AND deleted_flag     = '||SYS_NO;
42911 
42912       --Log a warning for those records where the wip_supply_type has a value other
42913       --than SYS_NO
42914 
42915       lv_return := MSC_ST_UTIL.LOG_ERROR
42916                      (p_table_name        => 'MSC_ST_SUPPLIES',
42917                       p_instance_code     => v_instance_code,
42918                       p_row               => lv_column_names,
42919                       p_severity          => G_SEV_WARNING,
42920                       p_message_text      => lv_message_text,
42921                       p_error_text        => lv_error_text,
42922                       p_batch_id          => lv_batch_id,
42923                       p_where_str         => lv_where_str,
42924                       p_col_name          => 'WIP_SUPPLY_TYPE',
42925                       p_debug             => v_debug,
42926                       p_default_value     => 1);
42927 
42928       IF lv_return <> 0 THEN
42929         RAISE ex_logging_err;
42930       END IF;
42931 
42932       v_sql_stmt := 06;
42933       lv_sql_stmt :=
42934       'UPDATE   msc_st_supplies'
42935       ||' SET   order_number = wip_entity_name'
42936       ||' WHERE NVL(order_number,'||''''||NULL_CHAR||''''||')'
42937       ||'       =                '||''''||NULL_CHAR||''''
42938       ||' AND   deleted_flag                   = '||SYS_NO
42939       ||' AND   process_flag                   = '||G_IN_PROCESS
42940       ||' AND   NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
42941       ||' AND   sr_instance_code               = :v_instance_code';
42942 
42943 
42944         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42945 
42946       EXECUTE IMMEDIATE lv_sql_stmt
42947               USING     lv_batch_id,
42948                         v_instance_code;
42949 
42950       -- Now we will check whether BOM Name is NULL , if it is NULL we will populate the
42951       -- ASSEMBLY NAME in BOM NAME column for all such records
42952 
42953       v_sql_stmt := 07;
42954       lv_sql_stmt :=
42955       'UPDATE msc_st_supplies '
42956       ||' SET    bill_name           = item_name'
42957       ||' WHERE  sr_instance_code    = :v_instance_code'
42958       ||' AND    order_type in (3, 14)'
42959       ||' AND    process_flag        ='||G_IN_PROCESS
42960       ||' AND    NVL(bill_name,'||''''||NULL_CHAR||''''||') '
42961       ||'         =            '||''''||NULL_CHAR||''''
42962       ||' AND    NVL(item_name,'||''''||NULL_CHAR||''''||') '
42963       ||'         <>           '||''''||NULL_CHAR||''''
42964       ||' AND    batch_id            = :lv_batch_id';
42965 
42966 
42967         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42968 
42969       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
42970 
42971       -- Now check whether Routing  Name is NULL , if it is NULL we will populate
42972       -- Assembly  Name in Routing Name column for all such records
42973 
42974       v_sql_stmt := 08;
42975 
42976       lv_sql_stmt :=
42977       'UPDATE msc_st_supplies '
42978       ||' SET    routing_name             = item_name'
42979       ||' WHERE  sr_instance_code         = :v_instance_code'
42980       ||' AND    order_type in (3, 14)'
42981       ||' AND    process_flag             ='|| G_IN_PROCESS
42982       ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||') '
42983       ||'         =               '||''''||NULL_CHAR||''''
42984       ||' AND    NVL(item_name,   '||''''||NULL_CHAR||''''||') '
42985       ||'         <>              '||''''||NULL_CHAR||''''
42986       ||' AND    batch_id                 = :lv_batch_id ';
42987 
42988 
42989         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
42990 
42991       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
42992 
42993       lv_return := MSC_ST_UTIL.DERIVE_BILL_SEQUENCE_ID
42994                    (p_table_name     => 'MSC_ST_SUPPLIES',
42995                     p_bom_col_name   => 'BILL_NAME',
42996                     p_bom_col_id     => 'BILL_SEQUENCE_ID',
42997                     p_instance_code  => v_instance_code,
42998                     p_batch_id       => lv_batch_id,
42999                     p_debug          => v_debug,
43000                     p_error_text     => lv_error_text);
43001 
43002 
43003      IF (lv_return <> 0 ) THEN
43004            RAISE ex_logging_err;
43005      END IF;
43006 
43007      lv_return := MSC_ST_UTIL.DERIVE_ROUTING_SEQUENCE_ID
43008                       (p_table_name     => 'MSC_ST_SUPPLIES',
43009                        p_rtg_col_name   => 'ROUTING_NAME',
43010                        p_rtg_col_id     => 'ROUTING_SEQUENCE_ID',
43011                        p_instance_code  => v_instance_code,
43012                        p_batch_id       => lv_batch_id,
43013                        p_debug          => v_debug,
43014                        p_error_text     => lv_error_text);
43015 
43016     if (lv_return <> 0 )then
43017        RAISE ex_logging_err;
43018     end if;
43019 
43020 
43021 
43022 
43023       v_sql_stmt := 09;
43024       lv_sql_stmt :=
43025       'UPDATE msc_st_supplies mss'
43026       ||' SET   schedule_group_id   = (SELECT local_id'
43027       ||'       FROM   msc_local_id_supply mls'
43028       ||'       WHERE  mls.char4 = mss.schedule_group_name'
43029       ||'       AND    mls.char3 = mss.organization_code'
43030       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
43031       ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
43032       ||'       AND    mls.char1 = mss.sr_instance_code'
43033       ||'       AND    mls.entity_name = ''SCHEDULE_GROUP_ID'' ),'
43034       ||'     line_id             = (SELECT local_id'
43035       ||'       FROM   msc_local_id_setup mls'
43036       ||'       WHERE  mls.char4 = mss.line_code'
43037       ||'       AND    mls.char3 = mss.organization_code'
43038       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
43039       ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
43040       ||'       AND    mls.char1 = mss.sr_instance_code'
43041       ||'       AND    mls.entity_name = ''LINE_ID''),'
43042       ||'     operation_seq_num   = (SELECT number1'
43043       ||'       FROM   msc_local_id_setup mls'
43044       ||'       WHERE  mls.char5 = mss.operation_seq_code'
43045       ||'       AND    mls.char4 = mss.routing_name'
43046       ||'       AND    NVL(mls.char6, '||''''||NULL_CHAR||''''||') = '
43047       ||'              NVL(mss.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
43048       ||'       AND    mls.char3 = mss.organization_code'
43049       ||'       AND    mls.date1 = mss.effectivity_date'
43050       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
43051       ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
43052       ||'       AND    mls.char1 = mss.sr_instance_code'
43053       ||'       AND    mls.entity_name = ''OPERATION_SEQUENCE_ID'' )'
43054       ||' WHERE  deleted_flag               = '||SYS_NO
43055       ||' AND    process_flag               = '||G_IN_PROCESS
43056       ||' AND NVL(batch_id,'||NULL_VALUE||')= :lv_batch_id'
43057       ||' AND    sr_instance_code           = :v_instance_code';
43058 
43059 
43060         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43061 
43062       EXECUTE IMMEDIATE lv_sql_stmt
43063               USING     lv_batch_id,
43064                         v_instance_code;
43065 
43066 
43067       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43068                      (p_app_short_name    => 'MSC',
43069                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
43070                       p_message_text      => lv_message_text,
43071                       p_error_text        => lv_error_text,
43072                       p_token1            => 'COLUMN_NAME',
43073                       p_token_value1      => 'BILL_SEQUENCE_ID OR ROUTING_SEQUENCE_ID',
43074                       p_token2            => 'DEFAULT_VALUE',
43075                       p_token_value2      => SYS_NO);
43076 
43077       IF lv_return <> 0 THEN
43078         RAISE ex_logging_err;
43079       END IF;
43080 
43081       lv_where_str :=
43082       '   AND (NVL(bill_sequence_id,  '||NULL_VALUE||') = '||NULL_VALUE
43083       ||' OR  NVL(routing_sequence_id,'||NULL_VALUE||') = '||NULL_VALUE||')'
43084       ||' AND deleted_flag = '||SYS_NO;
43085 
43086       --Log a warning for those records where the bill_sequence_id or
43087       --routing_sequence_id has null values
43088 
43089       lv_return := MSC_ST_UTIL.LOG_ERROR
43090                      (p_table_name        => 'MSC_ST_SUPPLIES',
43091                       p_instance_code     => v_instance_code,
43092                       p_row               => lv_column_names,
43093                       p_severity          => G_SEV_WARNING,
43094                       p_message_text      => lv_message_text,
43095                       p_error_text        => lv_error_text,
43096                       p_batch_id          => lv_batch_id,
43097                       p_debug             => v_debug,
43098                       p_where_str         => lv_where_str);
43099 
43100       IF lv_return <> 0 THEN
43101         RAISE ex_logging_err;
43102       END IF;
43103 
43104       --Deriving wip_entity_id
43105       v_sql_stmt := 10;
43106       lv_sql_stmt :=
43107       'UPDATE msc_st_supplies mss'
43108       ||' SET wip_entity_id     = (SELECT local_id'
43109       ||'       FROM   msc_local_id_supply mls'
43110       ||'       WHERE  mls.char4 = mss.wip_entity_name'
43111       ||'       AND    mls.char3 = mss.organization_code'
43112       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
43113       ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
43114       ||'       AND    mls.char1 = mss.sr_instance_code'
43115       ||'       AND    mls.entity_name = ''WIP_ENTITY_ID'' )'
43116       ||' WHERE  process_flag                   = '||G_IN_PROCESS
43117       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
43118       ||' AND    sr_instance_code               = :v_instance_code';
43119 
43120 
43121         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43122 
43123       EXECUTE IMMEDIATE lv_sql_stmt
43124               USING     lv_batch_id,
43125                         v_instance_code;
43126 
43127 
43128       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43129                      (p_app_short_name    => 'MSC',
43130                       p_error_code        => 'MSC_PP_DELETE_FAIL',
43131                       p_message_text      => lv_message_text,
43132                       p_error_text        => lv_error_text);
43133 
43134       IF lv_return <> 0 THEN
43135         RAISE ex_logging_err;
43136       END IF;
43137 
43138       v_sql_stmt := 11;
43139 
43140       lv_sql_stmt :=
43141       'UPDATE   msc_st_supplies '
43142       ||' SET   process_flag        = '||G_ERROR_FLG||','
43143       ||'         error_text   = '||''''||lv_message_text||''''
43144       ||' WHERE NVL(wip_entity_id,'||NULL_VALUE||') = '||NULL_VALUE
43145       ||' AND   deleted_flag        ='||SYS_YES
43146       ||' AND   process_flag        = '||G_IN_PROCESS
43147       ||' AND   NVL(batch_id,'||NULL_VALUE||')= :lv_batch_id '
43148       ||' AND   sr_instance_code    =:v_instance_code';
43149 
43150 
43151         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43152 
43153       EXECUTE IMMEDIATE lv_sql_stmt
43154               USING     lv_batch_id,
43155                         v_instance_code;
43156 
43157 
43158      -- update the jump_op_seq_num for lot based jobs for the operations jumped outside the network
43159 
43160       lv_sql_stmt :=
43161       'UPDATE   msc_st_supplies '
43162       ||' SET jump_op_seq_num = 50000'
43163       ||' WHERE NVL(jump_op_seq_code,'||''''||NULL_CHAR||''''||') '
43164       ||'         = '||''''||50000||''''
43165       ||' AND   process_flag        = '||G_IN_PROCESS
43166       ||' AND   NVL(batch_id,'||NULL_VALUE||')= :lv_batch_id '
43167       ||' AND   order_type    IN (3,7,14,15)'
43168       ||' AND   sr_instance_code    =:v_instance_code';
43169 
43170 
43171         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43172 
43173       EXECUTE IMMEDIATE lv_sql_stmt
43174               USING     lv_batch_id,
43175                         v_instance_code;
43176 
43177 
43178       lv_sql_stmt :=
43179       'UPDATE msc_st_supplies mss'
43180       ||' SET  jump_op_seq_num   = (SELECT number1'
43181       ||'       FROM   msc_local_id_setup mls'
43182       ||'       WHERE  NVL(mls.char5,'||''''||NULL_CHAR||''''||') = '
43183       ||'              NVL(mss.jump_op_seq_code,'||''''||NULL_CHAR||''''||') '
43184       ||'       AND    mls.char4 = mss.routing_name'
43185       ||'       AND    NVL(mls.char6, '||''''||NULL_CHAR||''''||') = '
43186       ||'              NVL(mss.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
43187       ||'       AND    mls.char3 = mss.organization_code'
43188       ||'       AND    mls.date1 = mss.jump_op_effectivity_date'
43189       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
43190       ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
43191       ||'       AND    mls.char1 = mss.sr_instance_code'
43192       ||'       AND    mls.entity_name = ''OPERATION_SEQUENCE_ID'' )'
43193       ||' WHERE  deleted_flag               = '||SYS_NO
43194       ||' AND    process_flag               = '||G_IN_PROCESS
43195       ||' AND    jump_op_seq_num            <> 50000 '
43196       ||' AND NVL(batch_id,'||NULL_VALUE||')= :lv_batch_id'
43197       ||' AND    sr_instance_code           = :v_instance_code';
43198 
43199 
43200 
43201         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43202 
43203       EXECUTE IMMEDIATE lv_sql_stmt
43204               USING     lv_batch_id,
43205                         v_instance_code;
43206 
43207 
43208       --Call to customised validation.
43209       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
43210         (ERRBUF         => lv_error_text,
43211          RETCODE        => lv_return,
43212          pBatchID       => lv_batch_id,
43213          pInstanceCode  => v_instance_code,
43214          pEntityName    => 'MSC_ST_SUPPLIES_WO',
43215          pInstanceID    => v_instance_id);
43216 
43217       IF NVL(lv_return,0) <> 0 THEN
43218         RAISE ex_logging_err;
43219       END IF;
43220 
43221       --Generation of wip_entity_id
43222       OPEN  c2(lv_batch_id);
43223       FETCH c2 BULK COLLECT INTO lb_rowid ;
43224 
43225       IF c2%ROWCOUNT > 0  THEN
43226         v_sql_stmt := 12;
43227         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
43228         UPDATE  msc_st_supplies
43229         SET     wip_entity_id = msc_st_wip_entity_id_s.NEXTVAL
43230         WHERE rowid           = lb_rowid(j);
43231 
43232         v_sql_stmt := 13;
43233         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
43234         INSERT INTO msc_local_id_supply
43235           (local_id,
43236            st_transaction_id,
43237            instance_id,
43238            entity_name,
43239            data_source_type,
43240            char1,
43241            char2,
43242            char3,
43243            char4,
43244            SOURCE_ORG_ID,
43245            SOURCE_INVENTORY_ITEM_ID,
43246            SOURCE_BILL_SEQUENCE_ID,
43247            SOURCE_ROUTING_SEQUENCE_ID,
43248            SOURCE_SCHEDULE_GROUP_ID,
43249            SOURCE_WIP_ENTITY_ID,
43250            last_update_date,
43251            last_updated_by,
43252            creation_date,
43253            created_by)
43254         SELECT
43255             wip_entity_id,
43256             st_transaction_id,
43257             v_instance_id,
43258             'WIP_ENTITY_ID',
43259             data_source_type,
43260             v_instance_code,
43261             company_name,
43262             organization_code ,
43263             wip_entity_name,
43264             SOURCE_ORG_ID,
43265             SOURCE_INVENTORY_ITEM_ID,
43266             SOURCE_BILL_SEQUENCE_ID,
43267             SOURCE_ROUTING_SEQUENCE_ID,
43268             SOURCE_SCHEDULE_GROUP_ID,
43269             SOURCE_WIP_ENTITY_ID,
43270             v_current_date,
43271             v_current_user,
43272             v_current_date,
43273             v_current_user
43274         FROM msc_st_supplies
43275         WHERE  rowid            = lb_rowid(j);
43276 
43277       END IF;
43278       CLOSE c2 ;
43279 
43280       --Generation of schedule_group_id
43281       OPEN c3(lv_batch_id);
43282       FETCH c3 BULK COLLECT INTO lb_rowid ;
43283 
43284       IF c3%ROWCOUNT > 0  THEN
43285         v_sql_stmt := 14;
43286         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
43287         UPDATE  msc_st_supplies
43288         SET     schedule_group_id = msc_st_schedule_group_id_s.NEXTVAL
43289         WHERE rowid               = lb_rowid(j);
43290 
43291         v_sql_stmt := 15;
43292         FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
43293         INSERT INTO msc_local_id_supply
43294           (local_id,
43295            st_transaction_id,
43296            instance_id,
43297            entity_name,
43298            data_source_type,
43299            char1,
43300            char2,
43301            char3,
43302            char4,
43303            SOURCE_ORG_ID,
43304            SOURCE_INVENTORY_ITEM_ID,
43305            SOURCE_BILL_SEQUENCE_ID,
43306            SOURCE_ROUTING_SEQUENCE_ID,
43307            SOURCE_SCHEDULE_GROUP_ID,
43308            SOURCE_WIP_ENTITY_ID,
43309            last_update_date,
43310            last_updated_by,
43311            creation_date,
43312            created_by)
43313         SELECT
43314             schedule_group_id,
43315             st_transaction_id,
43316             v_instance_id,
43317            'SCHEDULE_GROUP_ID',
43318             data_source_type,
43319             v_instance_code,
43320             company_name,
43321             organization_code ,
43322             schedule_group_name,
43323             SOURCE_ORG_ID,
43324             SOURCE_INVENTORY_ITEM_ID,
43325             SOURCE_BILL_SEQUENCE_ID,
43326             SOURCE_ROUTING_SEQUENCE_ID,
43327             SOURCE_SCHEDULE_GROUP_ID,
43328             SOURCE_WIP_ENTITY_ID,
43329             v_current_date,
43330             v_current_user,
43331             v_current_date,
43332             v_current_user
43333         FROM msc_st_supplies
43334         WHERE  rowid            = lb_rowid(j);
43335 
43336       END IF;
43337       CLOSE c3;
43338 
43339       --Update disposition_id with the wip_entity_id.
43340       v_sql_stmt := 16;
43341       UPDATE msc_st_supplies
43342       SET    disposition_id    = wip_entity_id
43343       WHERE  process_flag      = G_IN_PROCESS
43344       AND    batch_id          = lv_batch_id
43345       AND    sr_instance_code  = v_instance_code;
43346 
43347       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43348                      (p_app_short_name    => 'MSC',
43349                       p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
43350                       p_message_text      => lv_message_text,
43351                       p_error_text        => lv_error_text,
43352                       p_token1            => 'COLUMN_NAMES',
43353                       p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
43354                                              ||' ORGANIZATION_CODE AND PROJECT_NUMBER',
43355                       p_token2            => 'MASTER_TABLE',
43356                       p_token_value2      => 'MSC_ST_PROJECT_TASKS');
43357 
43358       IF lv_return <> 0 THEN
43359         RAISE ex_logging_err;
43360       END IF;
43361 
43362      --Derive Project Id.
43363       lv_return := MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
43364                              (p_table_name          => 'MSC_ST_SUPPLIES',
43365                               p_proj_col_name       => 'PROJECT_NUMBER',
43366                               p_proj_task_col_id    => 'PROJECT_ID',
43367                               p_instance_code       => v_instance_code,
43368                               p_entity_name         => 'PROJECT_ID',
43369                               p_error_text          => lv_error_text,
43370                               p_batch_id            => lv_batch_id,
43371                               p_severity            => G_SEV_WARNING,
43372                               p_message_text        => lv_message_text,
43373                               p_debug               => v_debug,
43374                               p_row                 => lv_column_names);
43375       IF lv_return <> 0 THEN
43376         RAISE ex_logging_err;
43377       END IF;
43378 
43379       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43380                      (p_app_short_name    => 'MSC',
43381                       p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
43382                       p_message_text      => lv_message_text,
43383                       p_error_text        => lv_error_text,
43384                       p_token1            => 'COLUMN_NAMES',
43385                       p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
43386                                              ||' ORGANIZATION_CODE, PROJECT_NUMBER,'
43387                                              ||' TASK_NUMBER',
43388                       p_token2            => 'MASTER_TABLE',
43389                       p_token_value2      => 'MSC_ST_PROJECT_TASKS');
43390 
43391       IF lv_return <> 0 THEN
43392         RAISE ex_logging_err;
43393       END IF;
43394 
43395       --Derive Task Id.
43396       lv_return := MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
43397                              (p_table_name          => 'MSC_ST_SUPPLIES',
43398                               p_proj_col_name       => 'PROJECT_NUMBER',
43399                               p_proj_task_col_id    => 'TASK_ID',
43400                               p_instance_code       => v_instance_code,
43401                               p_entity_name         => 'TASK_ID',
43402                               p_error_text          => lv_error_text,
43403                               p_task_col_name       => 'TASK_NUMBER',
43404                               p_batch_id            => lv_batch_id,
43405                               p_severity            => G_SEV_WARNING,
43406                               p_message_text        => lv_message_text,
43407                               p_debug               => v_debug,
43408                               p_row                 => lv_column_names);
43409 
43410       IF lv_return <> 0 THEN
43411         RAISE ex_logging_err;
43412       END IF;
43413 
43414 
43415       v_sql_stmt := 17;
43416       lv_sql_stmt :=
43417       'UPDATE msc_st_supplies mss '
43418       ||' SET  schedule_group_id   = (SELECT local_id'
43419       ||'       FROM   msc_local_id_supply mls'
43420       ||'       WHERE  mls.char4 = mss.schedule_group_name'
43421       ||'       AND    mls.char3 = mss.organization_code'
43422       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
43423       ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
43424       ||'       AND    mls.char1 = mss.sr_instance_code'
43425       ||'       AND    mls.entity_name = ''SCHEDULE_GROUP_ID'' )'
43426       ||' WHERE  deleted_flag                      = '||SYS_NO
43427       ||' AND    process_flag                      = '||G_IN_PROCESS
43428       ||' AND    NVL(schedule_group_id,'||NULL_VALUE||') = '||NULL_VALUE
43429       ||' AND NVL(batch_id,'||NULL_VALUE||')= :lv_batch_id'
43430       ||' AND    sr_instance_code           = :v_instance_code';
43431 
43432 
43433         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43434 
43435       EXECUTE IMMEDIATE lv_sql_stmt
43436               USING     lv_batch_id,
43437                         v_instance_code;
43438 
43439      OPEN c4(lv_batch_id);
43440      FETCH c4 BULK COLLECT INTO lb_rowid ;
43441 
43442      IF c4%ROWCOUNT > 0  THEN
43443 
43444      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
43445 
43446         UPDATE msc_st_supplies
43447         SET    job_op_seq_num      =
43448                to_number(decode(length(rtrim(job_op_seq_code,'0123456789')),
43449                          NULL,job_op_seq_code,'1'))
43450         WHERE  rowid     = lb_rowid(j);
43451      END IF;
43452      CLOSE c4;
43453 
43454       lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
43455                   (p_table_name     => 'MSC_ST_SUPPLIES',
43456                    p_instance_id    => v_instance_id,
43457                    p_instance_code  => v_instance_code,
43458                    p_process_flag   => G_VALID,
43459                    p_error_text     => lv_error_text,
43460                    p_debug          => v_debug,
43461                    p_batch_id       => lv_batch_id);
43462       IF lv_return <> 0 THEN
43463         RAISE ex_logging_err;
43464       END IF;
43465 
43466       lv_return := MSC_ST_UTIL.LOG_ERROR
43467                    (p_table_name        => 'MSC_ST_SUPPLIES',
43468                     p_instance_code     => v_instance_code,
43469                     p_row               => lv_column_names,
43470                     p_severity          => G_SEV_ERROR,
43471                     p_message_text      => NULL,
43472                     p_error_text        => lv_error_text,
43473                     p_debug             => v_debug,
43474                     p_batch_id          => lv_batch_id);
43475 
43476       IF lv_return <> 0 THEN
43477         RAISE ex_logging_err;
43478       END IF;
43479 
43480       COMMIT;
43481     END LOOP;
43482   EXCEPTION
43483     WHEN too_many_rows THEN
43484       lv_error_text  := substr('MSC_CL_PRE_PROCESS.LOAD_WO_SUPPLY'||'('
43485                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
43486       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
43487       ROLLBACK;
43488 
43489     WHEN ex_logging_err THEN
43490       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
43491       ROLLBACK;
43492 
43493     WHEN OTHERS THEN
43494       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_WO_SUPPLY'||'('
43495                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
43496       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
43497       ROLLBACK;
43498 
43499   END LOAD_WO_SUPPLY;
43500 
43501 
43502   PROCEDURE  LOAD_RES_DEMAND   IS
43503 
43504   TYPE RowidTab         IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
43505   TYPE CurTyp           IS REF CURSOR;
43506   c1                    CurTyp;
43507   lb_rowid              RowidTab;
43508 
43509   lv_local_id           NUMBER;
43510   lv_column_names       VARCHAR2(5000);     -- Stores cocatenated column names
43511   lv_return             NUMBER;
43512   lv_error_text         VARCHAR2(250);
43513   lv_where_str          VARCHAR2(5000);
43514   lv_sql_stmt           VARCHAR2(5000);
43515   lv_cursor_stmt        VARCHAR2(5000);
43516   lv_batch_id           msc_st_resource_requirements.batch_id%TYPE;
43517   lv_message_text       msc_errors.error_text%TYPE;
43518 
43519   ex_logging_err    EXCEPTION;
43520 
43521   CURSOR c2(p_batch_id NUMBER) IS
43522     SELECT rowid
43523     FROM   msc_st_resource_requirements
43524     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
43525     AND    sr_instance_code = v_instance_code
43526     AND    batch_id         = p_batch_id;
43527 
43528    CURSOR c3(p_batch_id NUMBER) IS
43529     SELECT max(rowid)
43530     FROM   msc_st_resource_requirements
43531     WHERE  process_flag     = G_IN_PROCESS
43532     AND    sr_instance_code = v_instance_code
43533     AND    batch_id         = p_batch_id
43534     AND    NVL(operation_sequence_id,NULL_VALUE) = NULL_VALUE
43535     AND    deleted_flag     = SYS_NO
43536     GROUP BY sr_instance_code,company_name,organization_code,routing_name,
43537     operation_seq_code,alternate_routing_designator,operation_effectivity_date;
43538 
43539    CURSOR c4(p_batch_id NUMBER) IS
43540     SELECT max(rowid)
43541     FROM   msc_st_resource_requirements
43542     WHERE  process_flag     = G_IN_PROCESS
43543     AND    sr_instance_code = v_instance_code
43544     AND    batch_id         = p_batch_id
43545     AND    NVL(resource_seq_num,NULL_VALUE) = NULL_VALUE
43546     AND    NVL(resource_seq_code, NULL_CHAR) <> NULL_CHAR
43547     AND    deleted_flag     = SYS_NO
43548     GROUP BY sr_instance_code,company_name,organization_code,routing_name,
43549     operation_seq_code,resource_seq_code,alternate_routing_designator, alternate_number;
43550 
43551   BEGIN
43552 
43553    --Duplicate records check for the records whose source is XML
43554 
43555     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43556                  (p_app_short_name    => 'MSC',
43557                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
43558                   p_message_text      => lv_message_text,
43559                   p_error_text        => lv_error_text);
43560 
43561     IF lv_return <> 0 THEN
43562       RAISE ex_logging_err;
43563     END IF;
43564 
43565   v_sql_stmt := 01;
43566 
43567   lv_sql_stmt:=
43568    'UPDATE    msc_st_resource_requirements msrr1 '
43569    ||' SET       process_flag ='||G_ERROR_FLG||','
43570    ||'           error_text   = '||''''||lv_message_text||''''
43571    ||' WHERE     message_id < ( SELECT max(message_id) FROM msc_st_resource_requirements msrr2'
43572    ||'           WHERE msrr2.sr_instance_code  '
43573    ||'                 = msrr1.sr_instance_code'
43574    ||'         AND   NVL(msrr2.company_name,'||''''||NULL_CHAR||''''||') '
43575    ||'                =    NVL(msrr1.company_name,'||''''||NULL_CHAR||''''||') '
43576    ||'         AND   msrr2.wip_entity_name    = msrr1.wip_entity_name'
43577    ||'         AND   msrr2.organization_code  = msrr1.organization_code'
43578    ||'         AND   NVL(msrr2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
43579    ||'                  = NVL(msrr1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
43580    ||'         AND   NVL(msrr2.resource_seq_code,'||''''||NULL_CHAR||''''||') '
43581    ||'                  = NVL(msrr1.resource_seq_code,'||''''||NULL_CHAR||''''||') '
43582    ||'         AND   NVL(msrr2.resource_code,  '||''''||NULL_CHAR||''''||') '
43583    ||'                  = NVL(msrr1.resource_code, '||''''||NULL_CHAR||''''||') '
43584    ||'         AND   msrr2.process_flag       = '||G_IN_PROCESS
43585    ||'         AND    NVL(msrr2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
43586    ||' AND     msrr1.process_flag     = '|| G_IN_PROCESS
43587    ||' AND     msrr1.sr_instance_code = :v_instance_code '
43588    ||' AND     NVL(msrr1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
43589 
43590 
43591          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43592 
43593   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
43594 
43595   --Duplicate records check for the records whose source is batch load
43596 
43597   lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43598                  (p_app_short_name    => 'MSC',
43599                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
43600                   p_message_text      => lv_message_text,
43601                   p_error_text        => lv_error_text);
43602 
43603     IF lv_return <> 0 THEN
43604       RAISE ex_logging_err;
43605     END IF;
43606   v_sql_stmt := 02;
43607 
43608    lv_sql_stmt:=
43609    'UPDATE      msc_st_resource_requirements msrr1'
43610    ||' SET      process_flag = '||G_ERROR_FLG||','
43611    ||'          error_text   = '||''''||lv_message_text||''''
43612    ||' WHERE    EXISTS ( SELECT 1 '
43613    ||'          FROM msc_st_resource_requirements msrr2'
43614    ||'          WHERE msrr2.sr_instance_code  '
43615    ||'                 = msrr1.sr_instance_code'
43616    ||'         AND   NVL(msrr2.company_name,'||''''||NULL_CHAR||''''||') '
43617    ||'                =    NVL(msrr1.company_name,'||''''||NULL_CHAR||''''||') '
43618    ||'         AND   msrr2.wip_entity_name    = msrr1.wip_entity_name'
43619    ||'         AND   msrr2.organization_code  = msrr1.organization_code'
43620    ||'         AND   NVL(msrr2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
43621    ||'                  = NVL(msrr1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
43622    ||'         AND   NVL(msrr2.resource_seq_code,'||''''||NULL_CHAR||''''||') '
43623    ||'                  = NVL(msrr1.resource_seq_code,'||''''||NULL_CHAR||''''||') '
43624    ||'         AND   NVL(msrr2.resource_code,  '||''''||NULL_CHAR||''''||') '
43625    ||'                  = NVL(msrr1.resource_code, '||''''||NULL_CHAR||''''||') '
43626    ||'         AND   msrr2.process_flag       = '||G_IN_PROCESS
43627    ||'         AND   NVL(msrr2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
43628    ||'       GROUP BY sr_instance_code,company_name,organization_code,wip_entity_name,'
43629    ||'       operation_seq_code,resource_seq_code,resource_code '
43630    ||'       HAVING COUNT(*) >1 )'
43631    ||' AND   msrr1.process_flag = '||G_IN_PROCESS
43632    ||' AND   msrr1.sr_instance_code = :v_instance_code'
43633    ||' AND   NVL(msrr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
43634 
43635 
43636          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43637 
43638     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
43639 
43640   lv_column_names :=
43641   'DEPARTMENT_CODE             ||''~''||'
43642   ||'RESOURCE_CODE             ||''~''||'
43643   ||'ORGANIZATION_CODE         ||''~''||'
43644   ||'RESOURCE_SEQ_CODE         ||''~''||'
43645   ||'START_DATE                ||''~''||'
43646   ||'OPERATION_HOURS_REQUIRED  ||''~''||'
43647   ||'ASSIGNED_UNITS            ||''~''||'
43648   ||'WIP_ENTITY_NAME           ||''~''||'
43649   ||'SR_INSTANCE_CODE          ||''~''||'
43650   ||'ITEM_NAME                 ||''~''||'
43651   ||'OPERATION_SEQ_CODE        ||''~''||'
43652   ||'HOURS_EXPENDED            ||''~''||'
43653   ||'DEMAND_CLASS              ||''~''||'
43654   ||'BASIS_TYPE                ||''~''||'
43655   ||'END_DATE                  ||''~''||'
43656   ||'WIP_JOB_TYPE              ||''~''||'
43657   ||'SCHEDULED_COMPLETION_DATE ||''~''||'
43658   ||'SCHEDULED_QUANTITY        ||''~''||'
43659   ||'QUANTITY_COMPLETED        ||''~''||'
43660   ||'STD_OP_CODE               ||''~''||'
43661   ||'SUPPLY_TYPE               ||''~''||'
43662   ||'DELETED_FLAG              ||''~''||'
43663   ||'COMPANY_NAME ' ;
43664 
43665 
43666   -- Now processing by batch
43667    LOOP
43668       v_sql_stmt := 03;
43669       lv_cursor_stmt :=
43670       ' SELECT msc_st_batch_id_s.NEXTVAL '
43671       ||' FROM   dual';
43672 
43673       OPEN  c1 FOR lv_cursor_stmt;
43674       FETCH c1 INTO lv_batch_id;
43675       CLOSE c1;
43676 
43677       v_sql_stmt := 04;
43678       lv_sql_stmt :=
43679       ' UPDATE msc_st_resource_requirements '
43680       ||' SET    batch_id  = :lv_batch_id'
43681       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
43682       ||' AND    sr_instance_code               = :v_instance_code'
43683       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
43684       ||' AND    rownum                        <= '||v_batch_size;
43685 
43686 
43687          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43688 
43689 
43690       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
43691 
43692       EXIT WHEN SQL%NOTFOUND;
43693 
43694       OPEN c2(lv_batch_id);
43695       FETCH c2 BULK COLLECT INTO lb_rowid;
43696       CLOSE c2;
43697 
43698       v_sql_stmt := 05;
43699       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
43700       UPDATE msc_st_resource_requirements
43701       SET  st_transaction_id = msc_st_resource_requirements_s.NEXTVAL,
43702            refresh_id        = v_refresh_id,
43703            last_update_date  = v_current_date,
43704            last_updated_by   = v_current_user,
43705            creation_date     = v_current_date,
43706            created_by        = v_current_user
43707       WHERE  rowid           = lb_rowid(j);
43708 
43709 
43710     -- Set the error message
43711     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43712                      (p_app_short_name    => 'MSC',
43713                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
43714                       p_message_text      => lv_message_text,
43715                       p_error_text        => lv_error_text,
43716                       p_token1            => 'COLUMN_NAME',
43717                       p_token_value1      => 'DELETED_FLAG',
43718                       p_token2            => 'DEFAULT_VALUE',
43719                       p_token_value2      =>  SYS_NO  );
43720 
43721       IF lv_return <> 0 THEN
43722         RAISE ex_logging_err;
43723       END IF;
43724 
43725      -- Check whether delete flag value is in (SYS_YES,SYS_NO),if not default to SYS_NO
43726 
43727      lv_where_str :=
43728      ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
43729 
43730      lv_return := MSC_ST_UTIL.LOG_ERROR
43731                     (p_table_name        => 'MSC_ST_RESOURCE_REQUIREMENTS',
43732                      p_instance_code     => v_instance_code,
43733                      p_row               => lv_column_names,
43734                      p_severity          => G_SEV_WARNING,
43735                      p_message_text      => lv_message_text,
43736                      p_error_text        => lv_error_text,
43737                      p_batch_id          => lv_batch_id,
43738                      p_where_str         => lv_where_str,
43739                      p_col_name          => 'DELETED_FLAG',
43740                      p_debug             => v_debug,
43741                      p_default_value     => SYS_NO);
43742 
43743 
43744       IF lv_return <> 0 THEN
43745           RAISE ex_logging_err;
43746       END IF;
43747 
43748       -- Set the  message
43749      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43750                      (p_app_short_name    => 'MSC',
43751                       p_error_code        => 'MSC_PP_INVALID_VALUE',
43752                       p_message_text      => lv_message_text,
43753                       p_error_text        => lv_error_text,
43754                       p_token1            => 'COLUMN_NAME',
43755                       p_token_value1      => 'ORGANIZATION_CODE');
43756 
43757       IF lv_return <> 0 THEN
43758         RAISE ex_logging_err;
43759       END IF;
43760 
43761 
43762      -- Populate  organization id , call utilty package- msc_st_util's procedure
43763 
43764      lv_return :=
43765      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_RESOURCE_REQUIREMENTS',
43766                                     p_org_partner_name  =>'ORGANIZATION_CODE',
43767                                     p_org_partner_id    =>'ORGANIZATION_ID',
43768                                     p_instance_code     => v_instance_code,
43769                                     p_partner_type      => G_ORGANIZATION,
43770                                     p_severity          => G_SEV_ERROR,
43771                                     p_message_text      => lv_message_text,
43772                                     p_batch_id          => lv_batch_id,
43773                                     p_debug             => v_debug,
43774                                     p_error_text        => lv_error_text);
43775 
43776      IF(lv_return <> 0) then
43777         RAISE ex_logging_err;
43778      END IF;
43779 
43780 
43781     -- Derive  WIP_ENTITY_ID
43782     v_sql_stmt := 06;
43783     lv_sql_stmt:=
43784     'UPDATE        msc_st_resource_requirements msrr'
43785     ||' SET        wip_entity_id = (SELECT local_id '
43786     ||'            FROM msc_local_id_supply mlis'
43787     ||'            WHERE  mlis.char1 = msrr.sr_instance_code'
43788     ||'            AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
43789     ||'                  =    NVL(msrr.company_name,'||''''||NULL_CHAR||''''||') '
43790     ||'            AND    mlis.char3 = msrr.organization_code'
43791     ||'            AND    mlis.char4 = msrr.wip_entity_name'
43792     ||'            AND    mlis.entity_name = ''WIP_ENTITY_ID'' '
43793     ||'            AND    mlis.instance_id ='||v_instance_id||')'
43794     ||' WHERE      sr_instance_code  = :v_instance_code'
43795     ||' AND        NVL(wip_entity_name,'||''''||NULL_CHAR||''''||') '
43796     ||'             <> '||''''||NULL_CHAR||''''
43797     ||' AND        process_flag     ='||G_IN_PROCESS
43798     ||' AND        batch_id         = :lv_batch_id';
43799 
43800 
43801             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43802 
43803     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
43804 
43805      -- Set the error message
43806 
43807     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43808                      (p_app_short_name    => 'MSC',
43809                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
43810                       p_message_text      => lv_message_text,
43811                       p_error_text        => lv_error_text,
43812                       p_token1            => 'COLUMN_NAMES',
43813                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
43814                                              ||'ORGANIZATION_CODE,WIP_ENTITY_NAME',
43815                       p_token2            => 'MASTER_TABLE',
43816                       p_token_value2      =>  'MSC_ST_SUPPLIES',
43817                       p_token3            =>  'CHILD_TABLE' ,
43818                       p_token_value3      =>  'MSC_ST_RESOURCE_REQUIREMENTS' );
43819 
43820       IF lv_return <> 0 THEN
43821         RAISE ex_logging_err;
43822       END IF;
43823 
43824 
43825     -- Error out the records if WIP_ENTITY_ID NULL
43826 
43827     v_sql_stmt := 07;
43828     lv_sql_stmt:=
43829     'UPDATE      msc_st_resource_requirements msrr'
43830     ||' SET      process_flag              ='||G_ERROR_FLG||','
43831     ||'         error_text   = '||''''||lv_message_text||''''
43832     ||' WHERE    NVL(wip_entity_id,'||NULL_VALUE||') = '||NULL_VALUE
43833     ||' AND      sr_instance_code            = :v_instance_code'
43834     ||' AND      process_flag                ='||G_IN_PROCESS
43835     ||' AND      batch_id                    = :lv_batch_id';
43836 
43837 
43838             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43839 
43840     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
43841 
43842 
43843     -- Set supply_id = wip_entity_id
43844     v_sql_stmt := 09;
43845 
43846     lv_sql_stmt:=
43847     'UPDATE     msc_st_resource_requirements msrr'
43848     ||' SET        supply_id        = wip_entity_id '
43849     ||' WHERE      sr_instance_code = :v_instance_code'
43850     ||' AND        process_flag     = '||G_IN_PROCESS
43851     ||' AND        NVL(wip_entity_id,'||NULL_VALUE||')='||NULL_VALUE
43852     ||' AND        batch_id         = :lv_batch_id';
43853 
43854 
43855             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43856 
43857     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
43858 
43859 /* bug 3768813 */
43860     -- Derive resource id
43861 /*
43862      v_sql_stmt := 10;
43863 
43864     lv_sql_stmt :=
43865     'UPDATE     msc_st_resource_requirements msrr'
43866     ||' SET     resource_id=   (SELECT local_id '
43867     ||'                 FROM msc_local_id_setup mlis'
43868     ||'                 WHERE  mlis.char1 = msrr.sr_instance_code'
43869     ||'                 AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
43870     ||'                 =    NVL(msrr.company_name,'||''''||NULL_CHAR||''''||') '
43871     ||'                 AND    mlis.char3 = msrr.organization_code'
43872     ||'                 AND    mlis.char4 = msrr.resource_code'
43873     ||'                 AND    mlis.entity_name = ''RESOURCE_ID'''
43874     ||'                 AND    mlis.instance_id ='||v_instance_id||' )'
43875     ||' WHERE      msrr.sr_instance_code = :v_instance_code'
43876     ||' AND        msrr.deleted_flag     = '||SYS_NO
43877     ||' AND        msrr.process_flag     ='|| G_IN_PROCESS
43878     ||' AND        msrr.schedule_flag    <> 2'
43879     ||' AND        msrr.batch_id         = :lv_batch_id';
43880 
43881 
43882             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43883 
43884     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
43885 
43886 */
43887     -- set the resource_id, department_id, resource_seq_num to -1 for those records where resource_code is null and resource is an unscheduled resource
43888 
43889     lv_sql_stmt:=
43890     'UPDATE      msc_st_resource_requirements msrr'
43891     ||' SET      resource_id    = -1, department_id = -1, resource_seq_num = -1 '
43892     ||' WHERE    sr_instance_code            = :v_instance_code'
43893     ||' AND      deleted_flag                = '||SYS_NO
43894     ||' AND      process_flag                = '|| G_IN_PROCESS
43895     ||' AND      batch_id                    = :lv_batch_id'
43896     ||' AND      NVL(resource_code,'||''''||NULL_CHAR||''''||') = '||''''||NULL_CHAR||''''
43897     ||' AND      schedule_flag =2';
43898 
43899             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43900 
43901     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
43902 
43903      -- Set the error message
43904 
43905     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43906                      (p_app_short_name    => 'MSC',
43907                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
43908                       p_message_text      => lv_message_text,
43909                       p_error_text        => lv_error_text,
43910                       p_token1            => 'COLUMN_NAMES',
43911                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
43912                                              ||'ORGANIZATION_CODE,RESOURCE_CODE',
43913                       p_token2            => 'MASTER_TABLE',
43914                       p_token_value2      =>  'MSC_ST_DEPARTMENT_RESOURCES',
43915                       p_token3            =>  'CHILD_TABLE' ,
43916                       p_token_value3      =>  'MSC_ST_RESOURCE_REQUIREMENTS' );
43917 
43918       IF lv_return <> 0 THEN
43919         RAISE ex_logging_err;
43920       END IF;
43921 
43922 /* created the new function to derive resource_id for bug 3768813 */
43923       lv_where_str :=' AND        schedule_flag    <> 2';
43924       lv_return := MSC_ST_UTIL.DERIVE_RESOURCE_ID
43925                      (p_table_name       => 'MSC_ST_RESOURCE_REQUIREMENTS',
43926                       p_resource_col_name => 'RESOURCE_CODE',
43927                       p_department_col_name => 'DEPARTMENT_CODE',
43928                       p_resource_col_id => 'RESOURCE_ID',
43929                       p_instance_code    => v_instance_code,
43930                       p_error_text       => lv_error_text,
43931                       p_batch_id         => lv_batch_id,
43932                       p_severity         => G_SEV_ERROR,
43933                       p_message_text     => lv_message_text,
43934                       p_debug            => v_debug,
43935                       p_row              => lv_column_names,
43936                       p_where_str         => lv_where_str);
43937 
43938       IF lv_return <> 0 THEN
43939         RAISE ex_logging_err;
43940       END IF;
43941 /*
43942     -- Error out records where resource_id is NULL
43943 
43944     v_sql_stmt := 11;
43945 
43946     lv_sql_stmt:=
43947     'UPDATE      msc_st_resource_requirements msrr'
43948     ||' SET      process_flag              ='||G_ERROR_FLG||','
43949     ||'         error_text   = '||''''||lv_message_text||''''
43950     ||' WHERE    NVL(resource_id,'||NULL_VALUE||') = '||NULL_VALUE
43951     ||' AND      sr_instance_code            = :v_instance_code'
43952     ||' AND      deleted_flag                = '||SYS_NO
43953     ||' AND      process_flag                ='|| G_IN_PROCESS
43954     ||' AND      batch_id                    = :lv_batch_id';
43955 
43956     IF v_debug THEN
43957             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43958     END IF;
43959 
43960     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
43961 
43962  */
43963 /* bug 3768813 */
43964     -- Derive Department id
43965 /*
43966      v_sql_stmt := 12;
43967 
43968     lv_sql_stmt :=
43969     'UPDATE     msc_st_resource_requirements msrr'
43970     ||' SET      department_id =  (SELECT local_id'
43971     ||'               FROM msc_local_id_setup mlis'
43972     ||'               WHERE  mlis.char1 = msrr.sr_instance_code'
43973     ||'               AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
43974     ||'                 =    NVL(msrr.company_name,'||''''||NULL_CHAR||''''||') '
43975     ||'               AND    mlis.char3 = msrr.organization_code'
43976     ||'               AND    mlis.char4 = msrr.department_code'
43977     ||'               AND    mlis.entity_name = ''DEPARTMENT_ID'''
43978     ||'               AND    mlis.instance_id ='||v_instance_id||' )'
43979     ||' WHERE      msrr.sr_instance_code = :v_instance_code'
43980     ||' AND        msrr.process_flag     ='||G_IN_PROCESS
43981     ||' AND        NVL(msrr.department_id,'||NULL_VALUE||') <> -1'
43982     ||' AND        msrr.batch_id         = :lv_batch_id';
43983 
43984 
43985 
43986             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
43987 
43988 
43989     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
43990  */
43991 
43992          -- Set the error message
43993 
43994     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
43995                      (p_app_short_name    => 'MSC',
43996                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
43997                       p_message_text      => lv_message_text,
43998                       p_error_text        => lv_error_text,
43999                       p_token1            => 'COLUMN_NAMES',
44000                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
44001                                              ||'ORGANIZATION_CODE,DEPARTMENT_CODE',
44002                       p_token2            => 'MASTER_TABLE',
44003                       p_token_value2      =>  'MSC_ST_DEPARTMENT_RESOURCES',
44004                       p_token3            =>  'CHILD_TABLE' ,
44005                       p_token_value3      =>  'MSC_ST_RESOURCE_REQUIREMENTS' );
44006 
44007       IF lv_return <> 0 THEN
44008         RAISE ex_logging_err;
44009       END IF;
44010 
44011 /* created the new function to derive department_id for bug 3768813 */
44012       lv_return := MSC_ST_UTIL.DERIVE_DEPARTMENT_ID
44013                      (p_table_name       => 'MSC_ST_RESOURCE_REQUIREMENTS',
44014                       p_resource_col_name => 'RESOURCE_CODE',
44015                       p_department_col_name => 'DEPARTMENT_CODE',
44016                       p_department_col_id => 'DEPARTMENT_ID',
44017                       p_instance_code    => v_instance_code,
44018                       p_error_text       => lv_error_text,
44019                       p_batch_id         => lv_batch_id,
44020                       p_severity         => G_SEV_ERROR,
44021                       p_message_text     => lv_message_text,
44022                       p_debug            => v_debug,
44023                       p_row              => lv_column_names);
44024 
44025       IF lv_return <> 0 THEN
44026         RAISE ex_logging_err;
44027       END IF;
44028 
44029 /*
44030     -- Error out records where department_id is NULL
44031 
44032     v_sql_stmt := 11;
44033 
44034     lv_sql_stmt:=
44035     'UPDATE      msc_st_resource_requirements msrr'
44036     ||' SET      process_flag              ='||G_ERROR_FLG||','
44037     ||'         error_text   = '||''''||lv_message_text||''''
44038     ||' WHERE    NVL(department_id,'||NULL_VALUE||') = '||NULL_VALUE
44039     ||' AND      sr_instance_code            = :v_instance_code'
44040     ||' AND      deleted_flag                = '||SYS_NO
44041     ||' AND      process_flag                ='|| G_IN_PROCESS
44042     ||' AND      batch_id                    = :lv_batch_id';
44043 
44044 
44045             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44046 
44047     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
44048 
44049 */
44050 
44051       -- Set the  message
44052      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44053                      (p_app_short_name    => 'MSC',
44054                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
44055                       p_message_text      => lv_message_text,
44056                       p_error_text        => lv_error_text,
44057                       p_token1            => 'COLUMN_NAME',
44058                       p_token_value1      => 'ASSIGNED_UNITS');
44059 
44060       IF lv_return <> 0 THEN
44061         RAISE ex_logging_err;
44062       END IF;
44063 
44064     -- set the assigned units to 0 if resource_code is NULL
44065     lv_sql_stmt:=
44066     'UPDATE     msc_st_resource_requirements msrr'
44067     ||' SET     assigned_units  = 0 '
44068     ||' WHERE   NVL(assigned_units,'||NULL_VALUE||') = '||NULL_VALUE
44069     ||' AND     resource_id                 = -1 '
44070     ||' AND     sr_instance_code            = :v_instance_code'
44071     ||' AND     deleted_flag                ='|| SYS_NO
44072     ||' AND     process_flag                ='|| G_IN_PROCESS
44073     ||' AND     batch_id                    = :lv_batch_id';
44074 
44075 
44076             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44077 
44078     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
44079 
44080     -- Error out records where assigned units NULL
44081     v_sql_stmt := 08;
44082 
44083     lv_sql_stmt:=
44084     'UPDATE     msc_st_resource_requirements msrr'
44085     ||' SET     process_flag              ='||G_ERROR_FLG||','
44086     ||'         error_text   = '||''''||lv_message_text||''''
44087     ||' WHERE   NVL(assigned_units,'||NULL_VALUE||') = '||NULL_VALUE
44088     ||' AND     sr_instance_code            = :v_instance_code'
44089     ||' AND     deleted_flag                ='|| SYS_NO
44090     ||' AND     process_flag                ='|| G_IN_PROCESS
44091     ||' AND     batch_id                    = :lv_batch_id';
44092 
44093 
44094             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44095 
44096     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
44097 
44098 
44099     -- Set the error message
44100     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44101                      (p_app_short_name    => 'MSC',
44102                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
44103                       p_message_text      => lv_message_text,
44104                       p_error_text        => lv_error_text,
44105                       p_token1            => 'COLUMN_NAME',
44106                       p_token_value1      => 'OPERATION_SEQ_CODE',
44107                       p_token2            => 'DEFAULT_VALUE',
44108                       p_token_value2      =>  G_OPERATION_SEQ_CODE  );
44109 
44110       IF lv_return <> 0 THEN
44111         RAISE ex_logging_err;
44112       END IF;
44113 
44114      -- Default operation_seq_code as 1 if NULL
44115      lv_where_str :=
44116      '    AND NVL(operation_seq_code,'||''''||NULL_CHAR||''''||') '
44117      ||'         = '||''''||NULL_CHAR||'''' ;
44118 
44119      lv_return := MSC_ST_UTIL.LOG_ERROR
44120                     (p_table_name        => 'MSC_ST_RESOURCE_REQUIREMENTS',
44121                      p_instance_code     => v_instance_code,
44122                      p_row               => lv_column_names,
44123                      p_severity          => G_SEV_WARNING,
44124                      p_message_text      => lv_message_text,
44125                      p_error_text        => lv_error_text,
44126                      p_batch_id          => lv_batch_id,
44127                      p_where_str         => lv_where_str,
44128                      p_col_name          => 'OPERATION_SEQ_CODE',
44129                      p_debug             => v_debug,
44130                      p_default_value     => G_OPERATION_SEQ_CODE);
44131 
44132       IF lv_return <> 0 THEN
44133         RAISE ex_logging_err;
44134       END IF;
44135 
44136     -- Set the error message
44137     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44138                      (p_app_short_name    => 'MSC',
44139                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
44140                       p_message_text      => lv_message_text,
44141                       p_error_text        => lv_error_text,
44142                       p_token1            => 'COLUMN_NAME',
44143                       p_token_value1      => 'OPERATION_EFFECTIVITY_DATE',
44144                       p_token2            => 'DEFAULT_VALUE',
44145                       p_token_value2      => 'SYSDATE' );
44146 
44147       IF lv_return <> 0 THEN
44148         RAISE ex_logging_err;
44149       END IF;
44150 
44151     -- Default operation_effectivity date as SYSDATE if NULL
44152 
44153      lv_where_str :=
44154      ' AND NVL(operation_effectivity_date,(sysdate-36500)) =(sysdate-36500)';
44155 
44156      lv_return := MSC_ST_UTIL.LOG_ERROR
44157                     (p_table_name        => 'MSC_ST_RESOURCE_REQUIREMENTS',
44158                      p_instance_code     => v_instance_code,
44159                      p_row               => lv_column_names,
44160                      p_severity          => G_SEV_WARNING,
44161                      p_message_text      => lv_message_text,
44162                      p_error_text        => lv_error_text,
44163                      p_batch_id          => lv_batch_id,
44164                      p_where_str         => lv_where_str,
44165                      p_col_name          => 'OPERATION_EFFECTIVITY_DATE',
44166                      p_debug             => v_debug,
44167                      p_default_value     => 'SYSDATE');
44168 
44169       IF lv_return <> 0 THEN
44170         RAISE ex_logging_err;
44171       END IF;
44172 
44173 /* bug 3768813 */
44174       -- Set the  message
44175      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44176                      (p_app_short_name    => 'MSC',
44177                       p_error_code        => 'MSC_PP_INVALID_VALUE',
44178                        p_message_text      => lv_message_text,
44179                       p_error_text        => lv_error_text,
44180                        p_token1            => 'COLUMN_NAME',
44181                        p_token_value1      => 'ITEM_NAME');
44182 
44183       IF lv_return <> 0 THEN
44184         RAISE ex_logging_err;
44185       END IF;
44186 
44187     --Derive Inventory_item_id
44188     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
44189                    (p_table_name       => 'MSC_ST_RESOURCE_REQUIREMENTS',
44190                     p_item_col_name    => 'ITEM_NAME',
44191                     p_item_col_id      => 'INVENTORY_ITEM_ID',
44192                     p_instance_id      => v_instance_id,
44193                     p_instance_code    => v_instance_code,
44194                     p_message_text     => lv_message_text,
44195                     p_error_text       => lv_error_text,
44196                     p_batch_id         => lv_batch_id,
44197                     p_severity         => G_SEV_ERROR,
44198                     p_debug            => v_debug,
44199                     p_row              => lv_column_names);
44200 
44201     IF lv_return <> 0 THEN
44202         RAISE ex_logging_err;
44203     END IF;
44204 
44205   -- If routing_name is is NULL populate the item_name in routing name
44206 
44207   v_sql_stmt := 13;
44208   lv_sql_stmt :=
44209   ' UPDATE   msc_st_resource_requirements'
44210   ||' SET    routing_name            =  item_name'
44211   ||' WHERE  process_flag            = '||G_IN_PROCESS
44212   ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||')'
44213   ||'       = '||''''||NULL_CHAR||''''
44214   ||' AND   batch_id        = :lv_batch_id'
44215   ||' AND   sr_instance_code = :v_instance_code';
44216 
44217 
44218       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44219 
44220   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
44221 
44222 /* Bug 3768813 */
44223     -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
44224 
44225     lv_return :=msc_st_util.derive_routing_sequence_id
44226                 (p_table_name     => 'MSC_ST_RESOURCE_REQUIREMENTS',
44227                  p_rtg_col_name   => 'ROUTING_NAME',
44228                  p_rtg_col_id     =>'ROUTING_SEQUENCE_ID',
44229                  p_instance_code  => v_instance_code,
44230                  p_batch_id       => lv_batch_id,
44231                  p_debug          => v_debug,
44232                  p_error_text     => lv_error_text);
44233 
44234     if (lv_return <> 0 )then
44235        MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
44236     end if;
44237 
44238     -- Derive operation seq num and operation_seq_id
44239 
44240     v_sql_stmt := 14;
44241     lv_sql_stmt:=
44242     'UPDATE     msc_st_resource_requirements msrr'
44243     ||' SET     operation_seq_num =    (SELECT number1'
44244     ||'                    FROM msc_local_id_setup mlis'
44245     ||'                    WHERE  mlis.char1 = msrr.sr_instance_code'
44246     ||'                    AND NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
44247     ||'                    =   NVL(msrr.company_name,'||''''||NULL_CHAR||''''||') '
44248     ||'                    AND    mlis.char3 = msrr.organization_code'
44249     ||'                    AND    mlis.char4 = msrr.routing_name'
44250     ||'                    AND    mlis.char5 = msrr.operation_seq_code'
44251     ||'                    AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
44252     ||'                          = NVL(msrr.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
44253     ||'                    AND    mlis.date1 = msrr.operation_effectivity_date'
44254     ||'                    AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
44255     ||'                    AND    mlis.instance_id = '||v_instance_id ||')'
44256     ||' WHERE      sr_instance_code = :v_instance_code'
44257     ||' AND        process_flag     = '||G_IN_PROCESS
44258     ||' AND        batch_id         = :lv_batch_id';
44259 
44260 
44261 
44262          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44263 
44264     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
44265 
44266 /* Bug 3768813 */
44267    -- Derive operation_sequence id
44268 /*
44269     v_sql_stmt := 15;
44270     lv_sql_Stmt :=
44271     'UPDATE     msc_st_resource_requirements msrr'
44272     ||' SET     operation_sequence_id=  (SELECT local_id'
44273     ||'         FROM msc_local_id_setup mlis'
44274     ||'         WHERE  mlis.char1 = msrr.sr_instance_code'
44275     ||'         AND     NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
44276     ||'          =    NVL(msrr.company_name,'||''''||NULL_CHAR||''''||') '
44277     ||'         AND    mlis.char3 = msrr.organization_code'
44278     ||'         AND    mlis.char4 = msrr.routing_name'
44279     ||'         AND    mlis.char5 = msrr.operation_seq_code'
44280     ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
44281     ||'           =   NVL(msrr.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
44282     ||'         AND    mlis.date1 = msrr.operation_effectivity_date'
44283     ||'         AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
44284     ||'         AND    mlis.instance_id = '||v_instance_id||')'
44285     ||'  WHERE      sr_instance_code = :v_instance_code'
44286     ||'  AND        process_flag     ='||G_IN_PROCESS
44287     ||'  AND        batch_id         = :lv_batch_id';
44288 
44289 
44290          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44291 
44292    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
44293 
44294 */
44295 
44296 /* created new function to derive operation sequence id for bug 3768813 */
44297       lv_return := MSC_ST_UTIL.DERIVE_OPERATION_SEQUENCE_ID
44298                      (p_table_name       => 'MSC_ST_RESOURCE_REQUIREMENTS',
44299                       p_operation_seq_num => 'OPERATION_SEQ_NUM',
44300                        p_routing_col_name => 'ROUTING_NAME',
44301                       p_sequence_col_id  => 'OPERATION_SEQUENCE_ID',
44302                        p_op_effectivity_date => 'OPERATION_EFFECTIVITY_DATE',
44303                        p_operation_seq_code => 'OPERATION_SEQ_CODE',
44304                        p_routing_sequence_id => 'ROUTING_SEQUENCE_ID',
44305                       p_instance_code    => v_instance_code,
44306                       p_error_text       => lv_error_text,
44307                       p_batch_id         => lv_batch_id,
44308                       p_message_text     => lv_message_text,
44309                        p_debug            => v_debug,
44310                        p_row              => lv_column_names);
44311 
44312       IF lv_return <> 0 THEN
44313         RAISE ex_logging_err;
44314       END IF;
44315 
44316     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44317                  (p_app_short_name    => 'MSC',
44318                   p_error_code        => 'MSC_PP_DELETE_FAIL',
44319                   p_message_text      => lv_message_text,
44320                   p_error_text        => lv_error_text);
44321 
44322     IF lv_return <> 0 THEN
44323       RAISE ex_logging_err;
44324     END IF;
44325 
44326    -- Error out the records where delete flag yes and operation_sequence_id is NULL
44327     v_sql_stmt := 16;
44328     lv_sql_stmt:=
44329     'UPDATE   msc_st_resource_requirements '
44330     ||' SET     process_flag     ='||G_ERROR_FLG||','
44331     ||'         error_text   = '||''''||lv_message_text||''''
44332     ||' WHERE    NVL(operation_sequence_id,'||NULL_VALUE||') ='|| NULL_VALUE
44333     ||' AND      deleted_flag     = '||SYS_YES
44334     ||' AND      sr_instance_code = :v_instance_code'
44335     ||' AND      process_flag     = '||G_IN_PROCESS
44336     ||' AND      batch_id         = :lv_batch_id';
44337 
44338 
44339             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44340 
44341     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
44342 
44343      -- Derive resource seq num from the local  id table
44344 
44345     lv_sql_stmt :=
44346     'UPDATE     msc_st_resource_requirements msor'
44347     ||' SET     resource_seq_num =    (SELECT local_id '
44348     ||'         FROM msc_local_id_setup mlis'
44349     ||'         WHERE  mlis.char1 = msor.sr_instance_code'
44350     ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
44351     ||'         =    NVL(msor.company_name,'||''''||NULL_CHAR||''''||') '
44352     ||'         AND    mlis.char3 = msor.organization_code'
44353     ||'         AND    mlis.char4 = msor.routing_name'
44354     ||'         AND    mlis.char5 = msor.operation_seq_code'
44355     ||'         AND    mlis.char6 = msor.resource_seq_code'
44356     ||'         AND    NVL(mlis.char7,'||''''||NULL_CHAR||''''||') '
44357     ||'         =      NVL(msor.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
44358     ||'         AND    NVL(mlis.number1,'||NULL_VALUE||') = NVL(msor.alternate_number,'||NULL_VALUE||')'
44359     ||'		AND    rownum = 1'	/*change made to solve res req issue*/
44360     ||'         AND    mlis.entity_name = ''RESOURCE_SEQ_NUM'')'
44361     ||' WHERE   sr_instance_code = :v_instance_code'
44362     ||' AND     process_flag     = '||G_IN_PROCESS
44363     ||' AND     NVL(msor.resource_seq_num,'||NULL_VALUE||') <> -1'
44364     ||' AND     batch_id         = :lv_batch_id';
44365 
44366 
44367 
44368          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44369 
44370     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
44371 
44372 
44373     -- Default the operation yield to 1 where operation yield is less than 0 or greater than 1
44374 
44375        lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44376                      (p_app_short_name    => 'MSC',
44377                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
44378                       p_message_text      => lv_message_text,
44379                       p_error_text        => lv_error_text,
44380                       p_token1            => 'COLUMN_NAME',
44381                       p_token_value1      => 'YIELD',
44382                       p_token2            => 'DEFAULT_VALUE',
44383                       p_token_value2      =>  1  );
44384 
44385       IF lv_return <> 0 THEN
44386         RAISE ex_logging_err;
44387       END IF;
44388 
44389      lv_where_str :=
44390      ' AND (yield  < 0 OR yield  > 1 OR NVL(yield,'||NULL_VALUE||') ='|| NULL_VALUE||')';
44391 
44392     lv_return := MSC_ST_UTIL.LOG_ERROR
44393                     (p_table_name        => 'MSC_ST_RESOURCE_REQUIREMENTS',
44394                      p_instance_code     => v_instance_code,
44395                      p_row               => lv_column_names,
44396                      p_severity          => G_SEV_WARNING,
44397                      p_message_text      => lv_message_text,
44398                      p_error_text        => lv_error_text,
44399                      p_batch_id          => lv_batch_id,
44400                      p_where_str         => lv_where_str,
44401                      p_col_name          => 'YIELD',
44402                      p_debug             => v_debug,
44403                      p_default_value     =>1);
44404 
44405       IF lv_return <> 0 THEN
44406         RAISE ex_logging_err;
44407       END IF;
44408 
44409       -- Set the error message
44410     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44411                      (p_app_short_name    => 'MSC',
44412                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
44413                       p_message_text      => lv_message_text,
44414                       p_error_text        => lv_error_text,
44415                       p_token1            => 'COLUMN_NAME',
44416                       p_token_value1      => 'SCHEDULE_FLAG',
44417                       p_token2            => 'DEFAULT_VALUE',
44418                       p_token_value2      => 1 );
44419 
44420       IF lv_return <> 0 THEN
44421         RAISE ex_logging_err;
44422       END IF;
44423 
44424     -- Default schedule flag as 1 if values are other than 1,2,3,4
44425 
44426      lv_where_str := ' AND NVL(schedule_flag,'||NULL_VALUE||') '
44427                       ||' NOT IN(1,2,3,4)';
44428 
44429      lv_return := MSC_ST_UTIL.LOG_ERROR
44430                     (p_table_name        => 'MSC_ST_RESOURCE_REQUIREMENTS',
44431                      p_instance_code     => v_instance_code,
44432                      p_row               => lv_column_names,
44433                      p_severity          => G_SEV_WARNING,
44434                      p_message_text      => lv_message_text,
44435                      p_error_text        => lv_error_text,
44436                      p_batch_id          => lv_batch_id,
44437                      p_where_str         => lv_where_str,
44438                      p_col_name          => 'SCHEDULE_FLAG',
44439                      p_debug             => v_debug,
44440                      p_default_value     => 1);
44441 
44442       IF lv_return <> 0 THEN
44443         RAISE ex_logging_err;
44444       END IF;
44445 
44446 
44447    -- Error out the records where quantity_in_queue is negative OR quantity_running is negative
44448    -- OR quantity_waiting_to_move is negative OR quantity_completed is negative
44449 
44450     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44451                        (p_app_short_name    => 'MSC',
44452                         p_error_code        => 'MSC_PP_CONV_RT_NEG',
44453                         p_message_text      => lv_message_text,
44454                         p_error_text        => lv_error_text,
44455                         p_token1            => 'COLUMN_NAME',
44456                         p_token_value1      => 'QUANTITY_IN_QUEUE AND QUANTITY_RUNNING AND QUANTITY_WAITING_TO_MOVE AND QUANTITY_COMPLETED');
44457 
44458           IF lv_return <> 0 THEN
44459             RAISE ex_logging_err;
44460           END IF;
44461 
44462      UPDATE  msc_st_resource_requirements
44463              SET      process_flag   = G_ERROR_FLG,
44464                       error_text   = lv_message_text
44465              WHERE    deleted_flag     = SYS_NO
44466              AND      sr_instance_code = v_instance_code
44467              AND      process_flag     = G_IN_PROCESS
44468              AND      batch_id         = lv_batch_id
44469              AND   (quantity_in_queue <0 OR quantity_running <0 OR quantity_waiting_to_move <0 OR quantity_completed <0);
44470 
44471 
44472 
44473     --Call to customised validation.
44474 
44475      MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
44476         (ERRBUF         => lv_error_text,
44477          RETCODE        => lv_return,
44478          pBatchID       => lv_batch_id,
44479          pInstanceCode  => v_instance_code,
44480          pEntityName    => 'MSC_ST_RESOURCE_REQUIREMENTS',
44481          pInstanceID    => v_instance_id);
44482 
44483      IF NVL(lv_return,0) <> 0 THEN
44484         RAISE ex_logging_err;
44485      END IF;
44486 
44487     v_sql_stmt := 17 ;
44488 
44489      OPEN c3(lv_batch_id);
44490      FETCH c3 BULK COLLECT INTO lb_rowid ;
44491 
44492      IF c3%ROWCOUNT > 0  THEN
44493 
44494      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
44495 
44496         UPDATE msc_st_resource_requirements
44497         SET    operation_sequence_id  = msc_st_operation_sequence_id_s.NEXTVAL ,
44498                operation_seq_num      =
44499                to_number(decode(length(rtrim(operation_seq_code,'0123456789')),
44500                          NULL,operation_seq_code,'1'))
44501         WHERE  rowid                  = lb_rowid(j);
44502 
44503     v_sql_stmt := 18;
44504 
44505       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
44506 
44507    -- Insert into the LID table
44508      INSERT INTO  msc_local_id_setup
44509      (local_id,
44510      st_transaction_id,
44511      instance_id,
44512      entity_name,
44513      data_source_type,
44514      char1,
44515      char2,
44516      char3,
44517      char4,
44518      char5,
44519      char6,
44520      number1,
44521      date1,
44522      SOURCE_ORGANIZATION_ID,
44523      SOURCE_DEPARTMENT_ID,
44524      SOURCE_RESOURCE_ID,
44525      SOURCE_RESOURCE_SEQ_NUM,
44526      SOURCE_OPERATION_SEQUENCE_ID,
44527      SOURCE_WIP_ENTITY_ID,
44528      SOURCE_OPERATION_SEQ_NUM,
44529      last_update_date,
44530      last_updated_by,
44531      creation_date,
44532      created_by    )
44533       SELECT
44534       operation_sequence_id,
44535       st_transaction_id,
44536       v_instance_id,
44537       'OPERATION_SEQUENCE_ID',
44538       data_source_type,
44539       v_instance_code,
44540       company_name,
44541       organization_code,
44542       routing_name,
44543       operation_seq_code,
44544       alternate_routing_designator,
44545       operation_seq_num,
44546       operation_effectivity_date,
44547       SOURCE_ORGANIZATION_ID,
44548       SOURCE_DEPARTMENT_ID,
44549       SOURCE_RESOURCE_ID,
44550       SOURCE_RESOURCE_SEQ_NUM,
44551       SOURCE_OPERATION_SEQUENCE_ID,
44552       SOURCE_WIP_ENTITY_ID,
44553       SOURCE_OPERATION_SEQ_NUM,
44554       v_current_date,
44555       v_current_user,
44556       v_current_date,
44557       v_current_user
44558       FROM msc_st_resource_requirements
44559       WHERE rowid = lb_rowid(j);
44560 
44561      END IF;
44562      close c3;
44563 
44564    -- Update operation_sequence_id
44565 
44566     v_sql_stmt := 19;
44567     lv_sql_Stmt :=
44568     'UPDATE     msc_st_resource_requirements msrr'
44569     ||' SET     (operation_sequence_id, operation_seq_num) =  (SELECT local_id, number1'
44570     ||'         FROM msc_local_id_setup mlis'
44571     ||'         WHERE  mlis.char1 = msrr.sr_instance_code'
44572     ||'         AND     NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
44573     ||'          =    NVL(msrr.company_name,'||''''||NULL_CHAR||''''||') '
44574     ||'         AND    mlis.char3 = msrr.organization_code'
44575     ||'         AND    mlis.char4 = msrr.routing_name'
44576     ||'         AND    mlis.char5 = TO_CHAR(msrr.operation_seq_code)'
44577     ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
44578     ||'           =   NVL(msrr.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
44579     ||'         AND    mlis.date1 = msrr.operation_effectivity_date'
44580     ||'         AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
44581     ||'         AND    mlis.instance_id = '||v_instance_id||')'
44582     ||'  WHERE   sr_instance_code = :v_instance_code'
44583     ||'  AND     NVL(operation_sequence_id,'||NULL_VALUE||') ='|| NULL_VALUE
44584     ||'  AND     process_flag     ='||G_IN_PROCESS
44585     ||'  AND      batch_id         = :lv_batch_id';
44586 
44587 
44588          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44589 
44590    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
44591 
44592 
44593      OPEN c4(lv_batch_id);
44594      FETCH c4 BULK COLLECT INTO lb_rowid ;
44595 
44596      IF c4%ROWCOUNT > 0  THEN
44597 
44598      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
44599 
44600         UPDATE msc_st_resource_requirements
44601         SET    resource_seq_num      =
44602                to_number(decode(length(rtrim(resource_seq_code,'0123456789')),
44603                          NULL,resource_seq_code,'1'))
44604         WHERE  rowid                  = lb_rowid(j);
44605 
44606 
44607      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
44608   -- Insert into the LID table  with the  user defined key
44609 
44610        INSERT INTO  msc_local_id_setup
44611        (local_id,
44612        st_transaction_id,
44613        instance_id,
44614        entity_name,
44615        data_source_type,
44616        char1,
44617        char2,
44618        char3,
44619        char4,
44620        char5,
44621        char6,
44622        char7,
44623        number1,
44624        SOURCE_ORGANIZATION_ID,
44625        SOURCE_DEPARTMENT_ID,
44626        SOURCE_RESOURCE_ID,
44627        SOURCE_RESOURCE_SEQ_NUM,
44628        SOURCE_OPERATION_SEQUENCE_ID,
44629        SOURCE_WIP_ENTITY_ID,
44630        SOURCE_OPERATION_SEQ_NUM,
44631        last_update_date,
44632        last_updated_by,
44633        creation_date,
44634        created_by  )
44635       SELECT
44636        resource_seq_num,
44637        st_transaction_id,
44638        v_instance_id,
44639        'RESOURCE_SEQ_NUM',
44640        data_source_type,
44641        v_instance_code,
44642        company_name,
44643        organization_code,
44644        routing_name,
44645        operation_seq_code,
44646        resource_seq_code,
44647        alternate_routing_designator,
44648        alternate_number,
44649        SOURCE_ORGANIZATION_ID,
44650        SOURCE_DEPARTMENT_ID,
44651        SOURCE_RESOURCE_ID,
44652        SOURCE_RESOURCE_SEQ_NUM,
44653        SOURCE_OPERATION_SEQUENCE_ID,
44654        SOURCE_WIP_ENTITY_ID,
44655        SOURCE_OPERATION_SEQ_NUM,
44656        v_current_date,
44657        v_current_user,
44658        v_current_date,
44659        v_current_user
44660        FROM msc_st_resource_requirements
44661       WHERE rowid = lb_rowid(j) ;
44662 
44663     END IF;
44664   CLOSE c4;
44665 
44666   -- Update resource seq num from the local  id table
44667 
44668     lv_sql_stmt :=
44669     'UPDATE     msc_st_resource_requirements msor'
44670     ||' SET     resource_seq_num =    (SELECT local_id '
44671     ||'         FROM msc_local_id_setup mlis'
44672     ||'         WHERE  mlis.char1 = msor.sr_instance_code'
44673     ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
44674     ||'         =    NVL(msor.company_name,'||''''||NULL_CHAR||''''||') '
44675     ||'         AND    mlis.char3 = msor.organization_code'
44676     ||'         AND    mlis.char4 = msor.routing_name'
44677     ||'         AND    mlis.char5 = msor.operation_seq_code'
44678     ||'         AND    mlis.char6 = msor.resource_seq_code'
44679     ||'         AND    NVL(mlis.char7,'||''''||NULL_CHAR||''''||') '
44680     ||'         =      NVL(msor.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
44681     ||'         AND    NVL(mlis.number1,'||NULL_VALUE||') = NVL(msor.alternate_number,'||NULL_VALUE||')'
44682     ||'		AND    rownum = 1'	/*change made to solve res req issue*/
44683     ||'         AND    mlis.entity_name = ''RESOURCE_SEQ_NUM'')'
44684     ||' WHERE   sr_instance_code = :v_instance_code'
44685     ||' AND     NVL(resource_seq_num,'||NULL_VALUE||') = '||NULL_VALUE
44686     ||' AND     process_flag     = '||G_IN_PROCESS
44687     ||' AND     batch_id         = :lv_batch_id';
44688 
44689 
44690 
44691          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44692 
44693     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
44694 
44695     -- Set the processf flag as Valid and populate sr_instance_id
44696     lv_return :=
44697     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_RESOURCE_REQUIREMENTS',
44698                                p_instance_id    => v_instance_id,
44699                                p_instance_code  => v_instance_code,
44700                                p_process_flag   => G_VALID,
44701                                p_error_text     => lv_error_text,
44702                                p_debug          => v_debug,
44703                                p_batch_id       => lv_batch_id);
44704 
44705      IF lv_return <> 0 THEN
44706         RAISE ex_logging_err;
44707      END IF;
44708 
44709 
44710     -- At the end calling  the LOG_ERROR for logging all errored out records
44711 
44712    lv_return :=
44713    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_RESOURCE_REQUIREMENTS',
44714                          p_instance_code => v_instance_code,
44715                          p_row           => lv_column_names,
44716                          p_severity      => G_SEV_ERROR,
44717                          p_message_text  => NULL,
44718                          p_error_text    => lv_error_text,
44719                          p_debug         => v_debug,
44720                          p_batch_id      => lv_batch_id  );
44721 
44722       IF lv_return <> 0 THEN
44723         RAISE ex_logging_err;
44724       END IF;
44725 
44726   COMMIT;
44727   END LOOP;
44728 
44729 
44730   EXCEPTION
44731 
44732     WHEN too_many_rows THEN
44733          lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_RES_DEMAND'||'('
44734                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
44735          ROLLBACK ;
44736 
44737     WHEN ex_logging_err THEN
44738         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
44739         ROLLBACK;
44740 
44741     WHEN OTHERS THEN
44742       lv_error_text :=substr('MSC_CL_PRE_PROCESS.LOAD_RES_DEMAND'||'('
44743                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
44744       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
44745       ROLLBACK;
44746 
44747   END LOAD_RES_DEMAND;
44748 
44749 
44750 /*==========================================================================+
44751 | DESCRIPTION  : This procedure is called for validating demand originating |
44752 |                from WIP Supply and is for msc_st_demands table. Following |
44753 |                types are considered for the validation.                   |
44754 |                Demand origination          Origination Type               |
44755 |                -------------------------------------------------          |
44756 |                Std Job Demand                   2                         |
44757 |                Non Std. Demand                  3                         |
44758 |                Flow schedule                    25                        |
44759 +==========================================================================*/
44760 
44761   PROCEDURE  LOAD_WIP_DEMAND  IS
44762 
44763   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
44764 
44765   lb_rowid          RowidTab;
44766 
44767   lv_local_id       NUMBER;
44768   lv_sequence       NUMBER;
44769   lv_column_names   VARCHAR2(5000);     -- Stores cocatenated column names
44770   lv_return         NUMBER;
44771   lv_error_text     VARCHAR2(250);
44772   lv_where_str      VARCHAR2(5000);
44773   lv_sql_stmt       VARCHAR2(5000);
44774   lv_cursor_stmt    VARCHAR2(5000);
44775   lv_batch_id       msc_st_demands.batch_id%TYPE;
44776   lv_message_text   msc_errors.error_text%TYPE;
44777 
44778   ex_logging_err    EXCEPTION;
44779 
44780   CURSOR c1(p_batch_id NUMBER) IS
44781     SELECT rowid
44782     FROM   msc_st_demands
44783     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
44784     AND    origination_type  IN (2,3,25)
44785     AND    batch_id          = p_batch_id
44786     AND    sr_instance_code  = v_instance_code;
44787 
44788    CURSOR c2(p_batch_id NUMBER) IS
44789     SELECT max(rowid)
44790     FROM   msc_st_demands
44791     WHERE  process_flag     = G_IN_PROCESS
44792     AND    sr_instance_code = v_instance_code
44793     AND    batch_id         = p_batch_id
44794     AND    origination_type IN (2,3)  -- Not for flow schedule
44795     AND    NVL(operation_seq_num,NULL_VALUE) = NULL_VALUE
44796     AND    deleted_flag     = SYS_NO
44797     GROUP BY sr_instance_code,company_name,organization_code,routing_name,
44798     operation_seq_code,alternate_routing_designator,operation_effectivity_date;
44799 
44800 
44801   BEGIN
44802 
44803   -- Before we start processing the record by group id( batch size ) we are going
44804   -- to check whether that there
44805   -- is any duplicates for user defined unique keys (UDKs,)
44806 
44807    --For WIP component demand
44808 
44809    --Duplicate records check for the records whose source is XML
44810     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44811                  (p_app_short_name    => 'MSC',
44812                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
44813                   p_message_text      => lv_message_text,
44814                   p_error_text        => lv_error_text);
44815 
44816     IF lv_return <> 0 THEN
44817       RAISE ex_logging_err;
44818     END IF;
44819 
44820   v_sql_stmt := 01;
44821 
44822   lv_sql_stmt :=
44823   'UPDATE   msc_st_demands msd1'
44824   ||' SET     process_flag  = '||G_ERROR_FLG||','
44825   ||'         error_text   = '||''''||lv_message_text||''''
44826   ||' WHERE   message_id <  (SELECT MAX(message_id)'
44827   ||'         FROM msc_st_demands msd2'
44828   ||'         WHERE  msd2.sr_instance_code '
44829   ||'                = msd1.sr_instance_code '
44830   ||'         AND   msd2.organization_code '
44831   ||'                = msd1.organization_code '
44832   ||'         AND   NVL(msd2.company_name,'||''''||NULL_CHAR||''''||') '
44833   ||'                 =    NVL(msd1.company_name,'||''''||NULL_CHAR||''''||') '
44834   ||'         AND msd2.wip_entity_name = msd1.wip_entity_name'
44835   ||'         AND NVL(msd2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
44836   ||'                = NVL(msd1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
44837   ||'         AND    msd2.item_name '
44838   ||'                = msd1.item_name '
44839   ||'         AND  msd2.origination_type '
44840   ||'               = msd1.origination_type'
44841   ||'         AND   msd2.process_flag = '||G_IN_PROCESS
44842   ||'         AND    NVL(msd2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
44843   ||' AND     msd1.process_flag ='|| G_IN_PROCESS
44844   ||' AND     msd1.origination_type IN (2,3,25)'
44845   ||' AND     msd1.sr_instance_code = :v_instance_code '
44846   ||' AND     NVL(msd1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
44847 
44848 
44849          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44850 
44851   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
44852 
44853   --Duplicate records check for the records whose source is batch load
44854 
44855     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44856                  (p_app_short_name    => 'MSC',
44857                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
44858                   p_message_text      => lv_message_text,
44859                   p_error_text        => lv_error_text);
44860 
44861     IF lv_return <> 0 THEN
44862       RAISE ex_logging_err;
44863     END IF;
44864 
44865   v_sql_stmt := 02;
44866 
44867   lv_sql_stmt :=
44868   'UPDATE msc_st_demands   msd1'
44869   ||' SET     process_flag  = '||G_ERROR_FLG||','
44870   ||'         error_text   = '||''''||lv_message_text||''''
44871   ||' WHERE   EXISTS( SELECT 1 '
44872   ||'         FROM msc_st_demands msd2'
44873   ||'         WHERE  msd2.sr_instance_code '
44874   ||'                = msd1.sr_instance_code '
44875   ||'          AND   msd2.organization_code '
44876   ||'                = msd1.organization_code '
44877   ||'          AND   NVL(msd2.company_name,'||''''||NULL_CHAR||''''||') '
44878   ||'                 =    NVL(msd1.company_name,'||''''||NULL_CHAR||''''||') '
44879   ||'          AND   msd2.wip_entity_name = msd1.wip_entity_name'
44880   ||'          AND   NVL(msd2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
44881   ||'                = NVL(msd1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
44882   ||'          AND   msd2.item_name '
44883   ||'                = msd1.item_name '
44884   ||'          AND   msd2.origination_type '
44885   ||'                = msd1.origination_type'
44886   ||'          AND   msd2.process_flag = '||G_IN_PROCESS
44887   ||'          AND NVL(msd2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
44888   ||'       GROUP BY  sr_instance_code,organization_code,wip_entity_name,'
44889   ||'       company_name,operation_seq_code,item_name,origination_type'
44890   ||'       HAVING COUNT(*) > 1)'
44891   ||' AND   msd1.process_flag  = '||G_IN_PROCESS
44892   ||' AND   msd1.origination_type IN (2,3,25)'
44893   ||' AND   msd1.sr_instance_code = :v_instance_code'
44894   ||' AND   NVL(msd1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
44895 
44896 
44897          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44898 
44899   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
44900 
44901   lv_column_names :=
44902   'ITEM_NAME                          ||''~''||'
44903   ||' ORGANIZATION_CODE               ||''~''||'
44904   ||' USING_REQUIREMENT_QUANTITY      ||''~''||'
44905   ||' WIP_ENTITY_NAME                 ||''~''||'
44906   ||' USING_ASSEMBLY_DEMAND_DATE      ||''~''||'
44907   ||' SR_INSTANCE_CODE                ||''~''||'
44908   ||' USING_ASSEMBLY_ITEM_NAME        ||''~''||'
44909   ||' OPERATION_SEQ_CODE              ||''~''||'
44910   ||' ORIGINATION_TYPE                ||''~''||'
44911   ||' PROJECT_NUMBER                  ||''~''||'
44912   ||' TASK_NUMBER                     ||''~''||'
44913   ||' PLANNING_GROUP                  ||''~''||'
44914   ||' END_ITEM_UNIT_NUMBER            ||''~''||'
44915   ||' DEMAND_CLASS                    ||''~''||'
44916   ||' WIP_STATUS_CODE                 ||''~''||'
44917   ||' WIP_SUPPLY_TYPE                 ||''~''||'
44918   ||' DELETED_FLAG                    ||''~''||'
44919   ||' COMPANY_NAME                    ||''~''||'
44920   ||' DEMAND_TYPE' ;
44921 
44922 
44923     LOOP
44924       v_sql_stmt := 03;
44925       SELECT       msc_st_batch_id_s.NEXTVAL
44926       INTO         lv_batch_id
44927       FROM         DUAL;
44928 
44929       v_sql_stmt := 04;
44930       lv_sql_stmt :=
44931       ' UPDATE    msc_st_demands '
44932       ||' SET     batch_id  = :lv_batch_id'
44933       ||' WHERE   process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
44934       ||' AND     sr_instance_code               = :v_instance_code'
44935       ||' AND     origination_type IN (2,3,25)'
44936       ||' AND     NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
44937       ||' AND     rownum                        <= '||v_batch_size;
44938 
44939 
44940 
44941         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
44942 
44943       EXECUTE IMMEDIATE lv_sql_stmt
44944               USING     lv_batch_id,
44945                         v_instance_code;
44946 
44947       EXIT WHEN SQL%NOTFOUND ;
44948 
44949     OPEN c1(lv_batch_id);
44950     FETCH c1 BULK COLLECT INTO lb_rowid;
44951     CLOSE c1;
44952 
44953     v_sql_stmt := 03;
44954     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
44955     UPDATE msc_st_demands
44956     SET    st_transaction_id   = msc_st_demands_s.NEXTVAL,
44957            refresh_id          = v_refresh_id,
44958            last_update_date    = v_current_date,
44959            last_updated_by     = v_current_user,
44960            creation_date       = v_current_date,
44961            created_by          = v_current_user
44962     WHERE  rowid               = lb_rowid(j);
44963 
44964     -- Set the error message
44965     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
44966                      (p_app_short_name    => 'MSC',
44967                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
44968                       p_message_text      => lv_message_text,
44969                       p_error_text        => lv_error_text,
44970                       p_token1            => 'COLUMN_NAME',
44971                       p_token_value1      => 'DELETED_FLAG',
44972                       p_token2            => 'DEFAULT_VALUE',
44973                       p_token_value2      =>  SYS_NO  );
44974 
44975       IF lv_return <> 0 THEN
44976         RAISE ex_logging_err;
44977       END IF;
44978     --Log a warning for those records where the deleted_flag has a value other
44979     --SYS_NO
44980 
44981     lv_where_str :=
44982      ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
44983 
44984     lv_return := MSC_ST_UTIL.LOG_ERROR
44985                    (p_table_name        => 'MSC_ST_DEMANDS',
44986                     p_instance_code     => v_instance_code,
44987                     p_row               => lv_column_names,
44988                     p_severity          => G_SEV_WARNING,
44989                     p_message_text      => lv_message_text,
44990                     p_error_text        => lv_error_text,
44991                     p_batch_id          => lv_batch_id,
44992                     p_where_str         => lv_where_str,
44993                     p_col_name          => 'DELETED_FLAG',
44994                     p_debug             => v_debug,
44995                     p_default_value     => SYS_NO);
44996 
44997     IF lv_return <> 0 THEN
44998         RAISE ex_logging_err;
44999     END IF;
45000 
45001       -- Set the  message
45002      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45003                      (p_app_short_name    => 'MSC',
45004                       p_error_code        => 'MSC_PP_INVALID_VALUE',
45005                       p_message_text      => lv_message_text,
45006                       p_error_text        => lv_error_text,
45007                       p_token1            => 'COLUMN_NAME',
45008                       p_token_value1      => 'ORGANIZATION_CODE');
45009 
45010       IF lv_return <> 0 THEN
45011         RAISE ex_logging_err;
45012       END IF;
45013 
45014      --Derive Organization_id
45015     lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
45016                    (p_table_name       => 'MSC_ST_DEMANDS',
45017                     p_org_partner_name => 'ORGANIZATION_CODE',
45018                     p_org_partner_id   => 'ORGANIZATION_ID',
45019                     p_instance_code    => v_instance_code,
45020                     p_partner_type     => G_ORGANIZATION,
45021                     p_error_text       => lv_error_text,
45022                     p_batch_id         => lv_batch_id,
45023                     p_severity         => G_SEV_ERROR,
45024                     p_message_text     => lv_message_text,
45025                     p_debug            => v_debug,
45026                     p_row              => lv_column_names);
45027 
45028     IF lv_return <> 0 THEN
45029         RAISE ex_logging_err;
45030     END IF;
45031 
45032       -- Set the  message
45033      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45034                      (p_app_short_name    => 'MSC',
45035                       p_error_code        => 'MSC_PP_INVALID_VALUE',
45036                       p_message_text      => lv_message_text,
45037                       p_error_text        => lv_error_text,
45038                       p_token1            => 'COLUMN_NAME',
45039                       p_token_value1      => 'ITEM_NAME');
45040 
45041       IF lv_return <> 0 THEN
45042         RAISE ex_logging_err;
45043       END IF;
45044 
45045     --Derive Inventory_item_id
45046     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
45047                    (p_table_name       => 'MSC_ST_DEMANDS',
45048                     p_item_col_name    => 'ITEM_NAME',
45049                     p_item_col_id      => 'INVENTORY_ITEM_ID',
45050                     p_instance_id      => v_instance_id,
45051                     p_instance_code    => v_instance_code,
45052                     p_message_text     => lv_message_text,
45053                     p_error_text       => lv_error_text,
45054                     p_batch_id         => lv_batch_id,
45055                     p_severity         => G_SEV_ERROR,
45056                     p_debug            => v_debug,
45057                     p_row              => lv_column_names);
45058 
45059     IF lv_return <> 0 THEN
45060         RAISE ex_logging_err;
45061     END IF;
45062 
45063     -- Set the  message
45064      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45065                      (p_app_short_name    => 'MSC',
45066                       p_error_code        => 'MSC_PP_INVALID_VALUE',
45067                       p_message_text      => lv_message_text,
45068                       p_error_text        => lv_error_text,
45069                       p_token1            => 'COLUMN_NAME',
45070                       p_token_value1      => 'USING_ASSEMBLY_ITEM_NAME');
45071 
45072       IF lv_return <> 0 THEN
45073         RAISE ex_logging_err;
45074       END IF;
45075 
45076     --Derive Using_assembly_item_id
45077     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
45078                    (p_table_name       => 'MSC_ST_DEMANDS',
45079                     p_item_col_name    => 'USING_ASSEMBLY_ITEM_NAME',
45080                     p_item_col_id      => 'USING_ASSEMBLY_ITEM_ID',
45081                     p_instance_id      => v_instance_id,
45082                     p_instance_code    => v_instance_code,
45083                     p_message_text     => lv_message_text,
45084                     p_error_text       => lv_error_text,
45085                     p_batch_id         => lv_batch_id,
45086                     p_severity         => G_SEV3_ERROR,
45087                     p_debug            => v_debug,
45088                     p_row              => lv_column_names);
45089 
45090     IF lv_return <> 0 THEN
45091         RAISE ex_logging_err;
45092     END IF;
45093 
45094   -- Derive WIP_ENTITY_ID
45095    v_sql_stmt := 04;
45096 
45097    lv_sql_stmt :=
45098     'UPDATE   msc_st_demands  msd'
45099     ||' SET   wip_entity_id = ( SELECT local_id '
45100     ||'       FROM   msc_local_id_supply mlid'
45101     ||'       WHERE  mlid.char1    = msd.sr_instance_code'
45102     ||'       AND   NVL(mlid.char2,'||''''||NULL_CHAR||''''||') '
45103     ||'       =    NVL(msd.company_name,'||''''||NULL_CHAR||''''||') '
45104     ||'       AND    mlid.char3    = msd.organization_code'
45105     ||'       AND    mlid.char4    = msd.wip_entity_name'
45106     ||'       AND    mlid.entity_name = ''WIP_ENTITY_ID'' )'
45107     ||'  WHERE origination_type IN (2,3,25) '
45108     ||'  AND   process_flag     ='||G_IN_PROCESS
45109     ||'  AND   batch_id       = :lv_batch_id'
45110     ||'  AND   sr_instance_code  =:v_instance_code';
45111 
45112 
45113       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45114 
45115     EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
45116 
45117     -- Set the error message
45118     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45119                      (p_app_short_name    => 'MSC',
45120                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
45121                       p_message_text      => lv_message_text,
45122                       p_error_text        => lv_error_text,
45123                       p_token1            => 'COLUMN_NAMES',
45124                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
45125                                              ||' ORGANIZATION_CODE,WIP_ENTITY_NAME',
45126                       p_token2            => 'MASTER_TABLE',
45127                       p_token_value2      =>  'MSC_ST_SUPPLIES',
45128                       p_token3            =>  'CHILD_TABLE' ,
45129                       p_token_value3      =>  'MSC_ST_DEMANDS' );
45130 
45131       IF lv_return <> 0 THEN
45132         RAISE ex_logging_err;
45133       END IF;
45134 
45135   -- Error out records where WIP_ENTITY_ID is  NULL;
45136 
45137   v_sql_stmt := 05;
45138   lv_sql_stmt :=
45139   'UPDATE     msc_st_demands '
45140   ||' SET     process_flag    =  '||G_ERROR_FLG||','
45141   ||'         error_text   = '||''''||lv_message_text||''''
45142   ||' WHERE   NVL(wip_entity_id,'||NULL_VALUE||') ='||NULL_VALUE
45143   ||' AND     process_flag      = '||G_IN_PROCESS
45144   ||' AND     origination_type  IN (2,3,25)'
45145   ||' AND     batch_id           = :lv_batch_id'
45146   ||' AND     sr_instance_code  = :v_instance_code';
45147 
45148 
45149       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45150 
45151   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
45152 
45153 
45154   -- Set the  message
45155    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45156                      (p_app_short_name    => 'MSC',
45157                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
45158                       p_message_text      => lv_message_text,
45159                       p_error_text        => lv_error_text,
45160                       p_token1            => 'COLUMN_NAME',
45161                       p_token_value1      => 'USING_REQUIREMENT_QUANTITY'
45162                                              || ' OR USING_ASSEMBLY_DEMAND_DATE');
45163 
45164       IF lv_return <> 0 THEN
45165         RAISE ex_logging_err;
45166       END IF;
45167 
45168 
45169   -- Error out records where USING_REQUIREMENT_QUANTITY is NULL;
45170   -- Error out records where using_assembly_demand_date is NULL
45171 
45172   v_sql_stmt := 06;
45173   lv_sql_stmt :=
45174   'UPDATE     msc_st_demands '
45175   ||' SET     process_flag    =  '||G_ERROR_FLG||','
45176   ||'         error_text   = '||''''||lv_message_text||''''
45177   ||' WHERE (NVL(using_requirement_quantity,'||NULL_VALUE||')='||NULL_VALUE
45178   ||'  OR  NVL(using_assembly_demand_date,SYSDATE-36500) = SYSDATE-36500 )'
45179   ||' AND    process_flag      = '||G_IN_PROCESS
45180   ||' AND    origination_type  IN (2,3,25)'
45181   ||' AND    deleted_flag      = '||SYS_NO
45182   ||' AND    batch_id          = :lv_batch_id'
45183   ||' AND    sr_instance_code  = :v_instance_code';
45184 
45185 
45186       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45187 
45188   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
45189 
45190 
45191   -- Update using_assembly_item_id = inventory_item_id
45192 
45193   v_sql_stmt := 07;
45194   lv_sql_stmt :=
45195   ' UPDATE      msc_st_demands'
45196   ||' SET    using_assembly_item_id  =  inventory_item_id'
45197   ||' WHERE  process_flag            = '||G_IN_PROCESS
45198   ||' AND    NVL(using_assembly_item_id,'||NULL_VALUE||') = '||NULL_VALUE
45199   ||' AND    deleted_flag   = '||SYS_NO
45200   ||' AND   process_flag    = '||G_IN_PROCESS
45201   ||' AND   origination_type IN (2,3,25)'
45202   ||' AND   batch_id        = :lv_batch_id'
45203   ||' AND   deleted_flag    = '||SYS_NO
45204   ||' AND   sr_instance_code = :v_instance_code';
45205 
45206 
45207       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45208 
45209   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
45210 
45211   -- Update order_number = wip_entity_name
45212 
45213   v_sql_stmt := 08;
45214   lv_sql_stmt :=
45215   ' UPDATE   msc_st_demands'
45216   ||' SET    order_number   = wip_entity_name'
45217   ||' WHERE  process_flag   = '||G_IN_PROCESS
45218   ||' AND    deleted_flag   = '||SYS_NO
45219   ||' AND    process_flag   = '||G_IN_PROCESS
45220   ||' AND    origination_type IN (2,3,25)'
45221   ||' AND    deleted_flag   = '||SYS_NO
45222   ||' AND    batch_id       = :lv_batch_id'
45223   ||' AND    sr_instance_code  = :v_instance_code';
45224 
45225 
45226       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45227 
45228   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
45229 
45230   -- Update disposition_id = wip_entity_id
45231 
45232   v_sql_stmt := 09;
45233   lv_sql_stmt :=
45234   ' UPDATE       msc_st_demands'
45235   ||' SET        disposition_id = wip_entity_id'
45236   ||' WHERE      process_flag  = '||G_IN_PROCESS
45237   ||' AND        NVL(wip_entity_id,'||NULL_VALUE||') = '||NULL_VALUE
45238   ||' AND        deleted_flag   = '||SYS_NO
45239   ||' AND        process_flag  = '||G_IN_PROCESS
45240   ||' AND        origination_type IN (2,3,25)'
45241   ||' AND        batch_id          = :lv_batch_id'
45242   ||' AND   sr_instance_code  = :v_instance_code';
45243 
45244 
45245       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45246 
45247   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
45248 
45249 
45250 
45251  -- UPdate MPS_DATE_REQUIRED as using_assembly_demand_date if NULL
45252  -- This is not reqd for flow schedule
45253 
45254   v_sql_stmt := 10;
45255   lv_sql_stmt :=
45256   ' UPDATE     msc_st_demands'
45257   ||' SET      mps_date_required =  using_assembly_demand_date'
45258   ||' WHERE    process_flag  = '||G_IN_PROCESS
45259   ||' AND      NVL(mps_date_required,SYSDATE-36500) = SYSDATE-36500'
45260   ||' AND      process_flag  = '||G_IN_PROCESS
45261   ||' AND      origination_type IN (2,3)'
45262   ||' AND      deleted_flag   = '||SYS_NO
45263   ||' AND      batch_id          = :lv_batch_id'
45264   ||' AND      sr_instance_code  = :v_instance_code';
45265 
45266 
45267       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45268 
45269   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
45270 
45271 
45272     -- Set the error message
45273     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45274                      (p_app_short_name    => 'MSC',
45275                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
45276                       p_message_text      => lv_message_text,
45277                       p_error_text        => lv_error_text,
45278                       p_token1            => 'COLUMN_NAME',
45279                       p_token_value1      => 'OPERATION_SEQ_CODE',
45280                       p_token2            => 'DEFAULT_VALUE',
45281                       p_token_value2      =>  G_OPERATION_SEQ_CODE  );
45282 
45283       IF lv_return <> 0 THEN
45284         RAISE ex_logging_err;
45285       END IF;
45286 
45287      -- Default operation_seq_code as 1 if NULL
45288      lv_where_str :=
45289      '    AND NVL(operation_seq_code,'||''''||NULL_CHAR||''''||') '
45290      ||'         = '||''''||NULL_CHAR||''''
45291      ||'  AND origination_type IN (2,3)';
45292 
45293      lv_return := MSC_ST_UTIL.LOG_ERROR
45294                     (p_table_name        => 'MSC_ST_DEMANDS',
45295                      p_instance_code     => v_instance_code,
45296                      p_row               => lv_column_names,
45297                      p_severity          => G_SEV_WARNING,
45298                      p_message_text      => lv_message_text,
45299                      p_error_text        => lv_error_text,
45300                      p_batch_id          => lv_batch_id,
45301                      p_where_str         => lv_where_str,
45302                      p_col_name          => 'OPERATION_SEQ_CODE',
45303                      p_debug             => v_debug,
45304                      p_default_value     => G_OPERATION_SEQ_CODE);
45305 
45306       IF lv_return <> 0 THEN
45307         RAISE ex_logging_err;
45308       END IF;
45309 
45310     -- Set the error message
45311     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45312                      (p_app_short_name    => 'MSC',
45313                       p_error_code        => 'MSC_PP_COL_VAL_NULL_DEFAULT',
45314                       p_message_text      => lv_message_text,
45315                       p_error_text        => lv_error_text,
45316                       p_token1            => 'COLUMN_NAME',
45317                       p_token_value1      => 'OPERATION_EFFECTIVITY_DATE',
45318                       p_token2            => 'DEFAULT_VALUE',
45319                       p_token_value2      =>  SYSDATE  );
45320 
45321       IF lv_return <> 0 THEN
45322         RAISE ex_logging_err;
45323       END IF;
45324 
45325     -- Default operation_effectivity date as SYSDATE if NULL
45326 
45327      lv_where_str :=
45328      '   AND NVL(operation_effectivity_date,SYSDATE-36500 ) = SYSDATE-36500 '
45329      ||' AND origination_type IN (2,3)' ;
45330 
45331      lv_return := MSC_ST_UTIL.LOG_ERROR
45332                     (p_table_name        => 'MSC_ST_DEMANDS',
45333                      p_instance_code     => v_instance_code,
45334                      p_row               => lv_column_names,
45335                      p_severity          => G_SEV_WARNING,
45336                      p_message_text      => lv_message_text,
45337                      p_error_text        => lv_error_text,
45338                      p_batch_id          => lv_batch_id,
45339                      p_where_str         => lv_where_str,
45340                      p_col_name          => 'OPERATION_EFFECTIVITY_DATE',
45341                      p_debug             => v_debug,
45342                      p_default_value     => 'SYSDATE');
45343 
45344       IF lv_return <> 0 THEN
45345         RAISE ex_logging_err;
45346       END IF;
45347 
45348   -- If routing_name is is NULL populate the item_name in routing name
45349 
45350   v_sql_stmt := 11;
45351   lv_sql_stmt :=
45352   ' UPDATE   msc_st_demands'
45353   ||' SET    routing_name            =  nvl(USING_ASSEMBLY_ITEM_NAME,item_name)'  /* bug 3768813 */
45354   ||' WHERE  process_flag            = '||G_IN_PROCESS
45355   ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||')'
45356   ||'       = '||''''||NULL_CHAR||''''
45357   ||' AND   process_flag    = '||G_IN_PROCESS
45358   ||' AND   origination_type IN (2,3)'
45359   ||' AND   batch_id        = :lv_batch_id'
45360   ||' AND   sr_instance_code = :v_instance_code';
45361 
45362 
45363       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45364 
45365   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
45366 
45367 /* bug 3768813 */
45368     IF v_instance_type <> G_INS_OTHER THEN
45369 
45370     -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
45371 
45372       lv_return :=msc_st_util.derive_routing_sequence_id
45373                 (p_table_name     => 'MSC_ST_DEMANDS',
45374                  p_rtg_col_name   => 'ROUTING_NAME',
45375                  p_rtg_col_id     =>'ROUTING_SEQUENCE_ID',
45376                  p_instance_code  => v_instance_code,
45377                  p_batch_id       => lv_batch_id,
45378                  p_debug          => v_debug,
45379                  p_error_text     => lv_error_text,
45380                  p_item_id        => 'using_assembly_item_id');
45381 
45382       if (lv_return <> 0 )then
45383          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
45384       end if;
45385 
45386       v_sql_stmt := 11;
45387       lv_sql_stmt:=
45388       'update msc_st_demands msd'
45389       ||' set operation_seq_num = '
45390       ||'  (select operation_seq_num '
45391       ||'   from msc_routing_operations mro '
45392       ||'   where mro.routing_sequence_id = msd.routing_sequence_id and '
45393       ||'         mro.effectivity_date = msd.operation_effectivity_date and '
45394       ||'         mro.SR_INSTANCE_ID = '||v_instance_id||' and '
45395       ||'      mro.operation_seq_num = to_number(decode(length(rtrim(msd.operation_seq_code,''0123456789'')),'
45396       ||'                   NULL,msd.operation_seq_code,''1'')) and'
45397       ||'         mro.plan_id = -1 and '
45398       ||'         mro.operation_type = 1)'
45399       ||' WHERE  sr_instance_code = :v_instance_code'
45400       ||' AND    process_flag     = '|| G_IN_PROCESS
45401       ||' AND    batch_id         = :lv_batch_id ';
45402 
45403 
45404            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45405 
45406       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
45407     END IF;
45408 
45409     -- Derive operation seq num from local id table
45410 
45411     v_sql_stmt := 12;
45412     lv_sql_stmt:=
45413     'UPDATE     msc_st_demands msd'
45414     ||' SET     operation_seq_num=    (SELECT number1'
45415     ||'                    FROM msc_local_id_setup mlis'
45416     ||'                    WHERE  mlis.char1 = msd.sr_instance_code'
45417     ||'                    AND NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
45418     ||'                    =   NVL(msd.company_name,'||''''||NULL_CHAR||''''||') '
45419     ||'                    AND    mlis.char3 = msd.organization_code'
45420     ||'                    AND    mlis.char4 = msd.routing_name'
45421     ||'                    AND    mlis.char5 = msd.operation_seq_code'
45422     ||'                    AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
45423     ||'                          = NVL(msd.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
45424     ||'                    AND    mlis.date1 = msd.operation_effectivity_date'
45425     ||'                    AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'') '
45426     ||' WHERE      sr_instance_code = :v_instance_code'
45427     ||' AND        process_flag     = '||G_IN_PROCESS
45428     ||' AND        batch_id         = :lv_batch_id'
45429     ||' AND        operation_seq_num is null'; /* bug 3768813 */
45430 
45431 
45432 
45433          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45434 
45435     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
45436 
45437    -- Set the message
45438 
45439     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45440                  (p_app_short_name    => 'MSC',
45441                   p_error_code        => 'MSC_PP_DELETE_FAIL',
45442                   p_message_text      => lv_message_text,
45443                   p_error_text        => lv_error_text);
45444 
45445     IF lv_return <> 0 THEN
45446       RAISE ex_logging_err;
45447     END IF;
45448 
45449    -- Error out the records where operation_seq_num is NULL
45450    -- And deleted_flag = SYS_YES
45451 
45452   v_sql_stmt := 13;
45453 
45454   lv_sql_stmt :=
45455   'UPDATE     msc_st_demands '
45456   ||' SET     process_flag    ='||G_ERROR_FLG||','
45457   ||'         error_text   = '||''''||lv_message_text||''''
45458   ||' WHERE   NVL(operation_seq_num,'||NULL_VALUE||') = '||NULL_VALUE
45459   ||' AND     deleted_flag  ='||SYS_YES
45460   ||' AND     origination_type IN (2,3)'
45461   ||' AND     batch_id       = :lv_batch_id'
45462   ||' AND     sr_instance_code  =:v_instance_code';
45463 
45464 
45465       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45466 
45467    EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
45468 
45469   -- Set the error message
45470     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45471                    (p_app_short_name    => 'MSC',
45472                     p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
45473                     p_message_text      => lv_message_text,
45474                     p_error_text        => lv_error_text,
45475                     p_token1            => 'COLUMN_NAMES',
45476                     p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
45477                                            ||' ORGANIZATION_CODE AND PROJECT_NUMBER',
45478                     p_token2            => 'MASTER_TABLE',
45479                     p_token_value2      => 'MSC_ST_PROJECT_TASKS');
45480 
45481     IF lv_return <> 0 THEN
45482       RAISE ex_logging_err;
45483     END IF;
45484 
45485     --Derive Project Id.
45486     lv_return := MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
45487                            (p_table_name          => 'MSC_ST_DEMANDS',
45488                             p_proj_col_name       => 'PROJECT_NUMBER',
45489                             p_proj_task_col_id    => 'PROJECT_ID',
45490                             p_instance_code       => v_instance_code,
45491                             p_entity_name         => 'PROJECT_ID',
45492                             p_error_text          => lv_error_text,
45493                             p_batch_id            => lv_batch_id,
45494                             p_severity            => G_SEV_WARNING,
45495                             p_message_text        => lv_message_text,
45496                             p_debug               => v_debug,
45497                             p_row                 => lv_column_names);
45498     IF lv_return <> 0 THEN
45499       RAISE ex_logging_err;
45500     END IF;
45501 
45502      -- Set the error message
45503     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45504                    (p_app_short_name    => 'MSC',
45505                     p_error_code        => 'MSC_PP_FK_REF_NOT_EXIST',
45506                     p_message_text      => lv_message_text,
45507                     p_error_text        => lv_error_text,
45508                     p_token1            => 'COLUMN_NAMES',
45509                     p_token_value1      => '  SR_INSTANCE_CODE, COMPANY_NAME,'
45510                                            ||' ORGANIZATION_CODE, PROJECT_NUMBER,'
45511                                            ||' TASK_NUMBER',
45512                     p_token2            => 'MASTER_TABLE',
45513                     p_token_value2      => 'MSC_ST_PROJECT_TASKS');
45514 
45515     IF lv_return <> 0 THEN
45516       RAISE ex_logging_err;
45517     END IF;
45518 
45519     --Derive Task Id.
45520     lv_return := MSC_ST_UTIL.DERIVE_PROJ_TASK_ID
45521                            (p_table_name          => 'MSC_ST_DEMANDS',
45522                             p_proj_col_name       => 'PROJECT_NUMBER',
45523                             p_proj_task_col_id    => 'TASK_ID',
45524                             p_instance_code       => v_instance_code,
45525                             p_entity_name         => 'TASK_ID',
45526                             p_error_text          => lv_error_text,
45527                             p_task_col_name       => 'TASK_NUMBER',
45528                             p_batch_id            => lv_batch_id,
45529                             p_severity            => G_SEV_WARNING,
45530                             p_message_text        => lv_message_text,
45531                             p_debug               => v_debug,
45532                             p_row                 => lv_column_names);
45533 
45534     IF lv_return <> 0 THEN
45535       RAISE ex_logging_err;
45536     END IF;
45537 
45538     -- Set the error message
45539     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45540                      (p_app_short_name    => 'MSC',
45541                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
45542                       p_message_text      => lv_message_text,
45543                       p_error_text        => lv_error_text,
45544                       p_token1            => 'COLUMN_NAME',
45545                       p_token_value1      => 'DEMAND_TYPE',
45546                       p_token2            => 'DEFAULT_VALUE',
45547                       p_token_value2      =>  G_DEMAND_TYPE );
45548 
45549       IF lv_return <> 0 THEN
45550         RAISE ex_logging_err;
45551       END IF;
45552 
45553     --  Default demand_type to 1 always
45554 
45555     lv_where_str := '   AND NVL(demand_type,'||NULL_VALUE||') <> '||G_DEMAND_TYPE
45556                     ||' AND origination_type IN (2,3) AND deleted_flag ='||SYS_NO ;
45557 
45558     lv_return := MSC_ST_UTIL.LOG_ERROR
45559                    (p_table_name        => 'MSC_ST_DEMANDS',
45560                     p_instance_code     => v_instance_code,
45561                     p_row               => lv_column_names,
45562                     p_severity          => G_SEV_WARNING,
45563                     p_message_text      => lv_message_text,
45564                     p_error_text        => lv_error_text,
45565                     p_batch_id          => lv_batch_id,
45566                     p_where_str         => lv_where_str,
45567                     p_col_name          => 'DEMAND_TYPE',
45568                     p_debug             => v_debug,
45569                     p_default_value     => G_DEMAND_TYPE);
45570 
45571     IF lv_return <> 0 THEN
45572         RAISE ex_logging_err;
45573     END IF;
45574 
45575 
45576     --  Default demand_type to 2 always for flow schedule
45577 
45578     lv_where_str := '   AND NVL(demand_type,'||NULL_VALUE||') <> 2'
45579                     ||' AND origination_type = 25 AND deleted_flag ='||SYS_NO ;
45580 
45581     lv_return := MSC_ST_UTIL.LOG_ERROR
45582                    (p_table_name        => 'MSC_ST_DEMANDS',
45583                     p_instance_code     => v_instance_code,
45584                     p_row               => lv_column_names,
45585                     p_severity          => G_SEV_WARNING,
45586                     p_message_text      => lv_message_text,
45587                     p_error_text        => lv_error_text,
45588                     p_batch_id          => lv_batch_id,
45589                     p_where_str         => lv_where_str,
45590                     p_col_name          => 'DEMAND_TYPE',
45591                     p_debug             => v_debug,
45592                     p_default_value     => 2);
45593 
45594     IF lv_return <> 0 THEN
45595         RAISE ex_logging_err;
45596     END IF;
45597 
45598 
45599     -- Set the error message
45600     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45601                      (p_app_short_name    => 'MSC',
45602                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
45603                       p_message_text      => lv_message_text,
45604                       p_error_text        => lv_error_text,
45605                       p_token1            => 'COLUMN_NAME',
45606                       p_token_value1      => 'WIP_SUPPLY_TYPE',
45607                       p_token2            => 'DEFAULT_VALUE',
45608                       p_token_value2      =>  G_WIP_SUPPLY_TYPE );
45609 
45610       IF lv_return <> 0 THEN
45611         RAISE ex_logging_err;
45612       END IF;
45613 
45614     --  Default wip_supply_type as 1
45615 
45616     lv_where_str := ' AND NVL(wip_supply_type,'||NULL_VALUE||') NOT IN (1,2,3,4,5,6,7)'
45617                     ||' AND origination_type IN (2,3) AND deleted_flag ='||SYS_NO ;
45618 
45619     lv_return := MSC_ST_UTIL.LOG_ERROR
45620                    (p_table_name        => 'MSC_ST_DEMANDS',
45621                     p_instance_code     => v_instance_code,
45622                     p_row               => lv_column_names,
45623                     p_severity          => G_SEV_WARNING,
45624                     p_message_text      => lv_message_text,
45625                     p_error_text        => lv_error_text,
45626                     p_batch_id          => lv_batch_id,
45627                     p_where_str         => lv_where_str,
45628                     p_col_name          => 'WIP_SUPPLY_TYPE',
45629                     p_debug             => v_debug,
45630                     p_default_value     => G_WIP_SUPPLY_TYPE );
45631 
45632     IF lv_return <> 0 THEN
45633         RAISE ex_logging_err;
45634     END IF;
45635 
45636 
45637     -- Set the error message
45638     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45639                      (p_app_short_name    => 'MSC',
45640                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
45641                       p_message_text      => lv_message_text,
45642                       p_error_text        => lv_error_text,
45643                       p_token1            => 'COLUMN_NAME',
45644                       p_token_value1      => 'WIP_STATUS_CODE',
45645                       p_token2            => 'DEFAULT_VALUE',
45646                       p_token_value2      =>  G_WIP_STATUS_CODE );
45647 
45648       IF lv_return <> 0 THEN
45649         RAISE ex_logging_err;
45650       END IF;
45651 
45652     --  Default wip_status_code as 1(unrelased)
45653 
45654     lv_where_str := '   AND NVL(wip_status_code,'||NULL_VALUE||') NOT IN (1,3,4,6,7,12)'
45655                     ||' AND origination_type IN (2,3,25)AND deleted_flag ='||SYS_NO ;
45656 
45657     lv_return := MSC_ST_UTIL.LOG_ERROR
45658                    (p_table_name        => 'MSC_ST_DEMANDS',
45659                     p_instance_code     => v_instance_code,
45660                     p_row               => lv_column_names,
45661                     p_severity          => G_SEV_WARNING,
45662                     p_message_text      => lv_message_text,
45663                     p_error_text        => lv_error_text,
45664                     p_batch_id          => lv_batch_id,
45665                     p_where_str         => lv_where_str,
45666                     p_col_name          => 'WIP_STATUS_CODE',
45667                     p_debug             => v_debug,
45668                     p_default_value     => G_WIP_STATUS_CODE);
45669 
45670     IF lv_return <> 0 THEN
45671         RAISE ex_logging_err;
45672     END IF;
45673 
45674     --Call to customised validation.
45675     MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
45676       (ERRBUF         => lv_error_text,
45677        RETCODE        => lv_return,
45678        pBatchID       => lv_batch_id,
45679        pInstanceCode  => v_instance_code,
45680        pEntityName    => 'MSC_ST_DEMANDS',
45681        pInstanceID    => v_instance_id);
45682 
45683     IF NVL(lv_return,0) <> 0 THEN
45684       RAISE ex_logging_err;
45685     END IF;
45686 
45687    -- Generate the operation_seq_num  and populate the LID table
45688 
45689     OPEN c2(lv_batch_id);
45690     FETCH c2 BULK COLLECT INTO lb_rowid ;
45691 
45692 
45693     IF c2%ROWCOUNT > 0  THEN
45694        FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
45695 
45696      --    SELECT msc_st_operation_sequence_id_s.NEXTVAL
45697      --    INTO   lv_local_id
45698      --    FROM   DUAL;
45699 
45700          UPDATE msc_st_demands
45701           SET  operation_seq_num     =
45702                to_number(decode(length(rtrim(operation_seq_code,'0123456789')),
45703                          NULL,operation_seq_code,'1'))
45704           WHERE  rowid                  = lb_rowid(j);
45705 
45706      FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
45707 
45708      -- Insert into the LID table
45709 
45710       INSERT INTO  msc_local_id_setup
45711      (local_id,
45712      st_transaction_id,
45713      instance_id,
45714      entity_name,
45715      data_source_type,
45716      char1,
45717      char2,
45718      char3,
45719      char4,
45720      char5,
45721      char6,
45722      number1,
45723      date1,
45724      SOURCE_ORGANIZATION_ID,
45725      SOURCE_INVENTORY_ITEM_ID,
45726      SOURCE_PROJECT_ID,
45727      SOURCE_TASK_ID,
45728      SOURCE_WIP_ENTITY_ID,
45729      SOURCE_OPERATION_SEQ_NUM,
45730      SOURCE_USING_ASSEMBLY_ID,
45731      last_update_date,
45732      last_updated_by,
45733      creation_date,
45734      created_by    )
45735      SELECT
45736       msc_st_operation_sequence_id_s.NEXTVAL,
45737       st_transaction_id,
45738       v_instance_id,
45739       'OPERATION_SEQUENCE_ID',
45740       data_source_type,
45741       v_instance_code,
45742       company_name,
45743       organization_code,
45744       routing_name,
45745       operation_seq_code,
45746       alternate_routing_designator,
45747       operation_seq_num,
45748       operation_effectivity_date,
45749       SOURCE_ORGANIZATION_ID,
45750       SOURCE_INVENTORY_ITEM_ID,
45751       SOURCE_PROJECT_ID,
45752       SOURCE_TASK_ID,
45753       SOURCE_WIP_ENTITY_ID,
45754       SOURCE_OPERATION_SEQ_NUM,
45755       SOURCE_USING_ASSEMBLY_ITEM_ID,
45756       v_current_date,
45757       v_current_user,
45758       v_current_date,
45759       v_current_user
45760       FROM msc_st_demands
45761       WHERE rowid = lb_rowid(j) ;
45762 
45763     END IF;
45764     CLOSE c2;
45765 
45766     -- Update operation seq num from local id table
45767 
45768     v_sql_stmt := 12;
45769     lv_sql_stmt:=
45770     'UPDATE     msc_st_demands msd'
45771     ||' SET     operation_seq_num=    (SELECT number1'
45772     ||'                    FROM msc_local_id_setup mlis'
45773     ||'                    WHERE  mlis.char1 = msd.sr_instance_code'
45774     ||'                    AND NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
45775     ||'                    =   NVL(msd.company_name,'||''''||NULL_CHAR||''''||') '
45776     ||'                    AND    mlis.char3 = msd.organization_code'
45777     ||'                    AND    mlis.char4 = msd.routing_name'
45778     ||'                    AND    mlis.char5 = msd.operation_seq_code'
45779     ||'                    AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
45780     ||'                          = NVL(msd.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
45781     ||'                    AND    mlis.date1 = msd.operation_effectivity_date'
45782     ||'                    AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'') '
45783     ||' WHERE      sr_instance_code = :v_instance_code'
45784     ||' AND        NVL(operation_seq_num,'||NULL_VALUE||')= '||NULL_VALUE
45785     ||' AND        process_flag     = '||G_IN_PROCESS
45786     ||' AND        batch_id         = :lv_batch_id';
45787 
45788 
45789 
45790          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45791 
45792     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
45793 
45794 
45795     lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
45796                   (p_table_name     => 'MSC_ST_DEMANDS',
45797                    p_instance_id    => v_instance_id,
45798                    p_instance_code  => v_instance_code,
45799                    p_process_flag   => G_VALID,
45800                    p_error_text     => lv_error_text,
45801                    p_debug          => v_debug,
45802                    p_batch_id       => lv_batch_id);
45803 
45804     IF lv_return <> 0 THEN
45805       RAISE ex_logging_err;
45806     END IF;
45807 
45808     lv_return := MSC_ST_UTIL.LOG_ERROR
45809                    (p_table_name        => 'MSC_ST_DEMANDS',
45810                     p_instance_code     => v_instance_code,
45811                     p_row               => lv_column_names,
45812                     p_severity          => G_SEV_ERROR,
45813                     p_message_text      => NULL,
45814                     p_error_text        => lv_error_text,
45815                     p_debug             => v_debug,
45816                     p_batch_id          => lv_batch_id);
45817 
45818     IF lv_return <> 0 THEN
45819       RAISE ex_logging_err;
45820     END IF;
45821 
45822     COMMIT;
45823    END LOOP ;
45824 
45825  EXCEPTION
45826     WHEN too_many_rows THEN
45827          lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_WIP_DEMAND'||'('
45828                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
45829          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
45830          ROLLBACK ;
45831 
45832     WHEN ex_logging_err THEN
45833         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
45834         ROLLBACK;
45835 
45836    WHEN OTHERS THEN
45837        lv_error_text :=  substr('MSC_CL_PRE_PROCESS.LOAD_WIP_DEMAND'||'('
45838                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
45839       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
45840       ROLLBACK;
45841 
45842   END LOAD_WIP_DEMAND;
45843 
45844 
45845 
45846 /*==========================================================================+
45847 | DESCRIPTION  : This procedure loads records into new tables for OFSM      |
45848 |                These tables contains the details for a lot based job      |
45849 |                                    |
45850 +==========================================================================*/
45851 
45852 PROCEDURE  LOAD_JOB_DETAILS IS
45853 
45854   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
45855   TYPE CurTyp       IS REF CURSOR;
45856   c1                CurTyp;
45857   lb_rowid          RowidTab;
45858   lv_local_id       NUMBER;
45859   lv_column_names   VARCHAR2(5000);     -- Stores concatenated column names
45860   lv_return         NUMBER;
45861   lv_error_text     VARCHAR2(250);
45862   lv_where_str      VARCHAR2(5000);
45863   lv_sql_stmt       VARCHAR2(5000);
45864   lv_cursor_stmt    VARCHAR2(5000);
45865   lv_batch_id       msc_st_routings.batch_id%TYPE;
45866   lv_message_text   msc_errors.error_text%TYPE;
45867   ex_logging_err    EXCEPTION;
45868 
45869 
45870   CURSOR c2(p_batch_id NUMBER) IS
45871     SELECT rowid
45872     FROM   msc_st_job_operation_networks
45873     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
45874     AND    sr_instance_code = v_instance_code
45875     AND    batch_id         = p_batch_id;
45876 
45877   CURSOR c3(p_batch_id NUMBER) IS
45878     SELECT rowid
45879     FROM   msc_st_job_operations
45880     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
45881     AND    sr_instance_code = v_instance_code
45882     AND    batch_id         = p_batch_id;
45883 
45884  CURSOR c4 (p_batch_id NUMBER) IS
45885     SELECT rowid
45886     FROM   msc_st_job_requirement_ops
45887     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
45888     AND    batch_id          = p_batch_id
45889     AND    sr_instance_code  = v_instance_code;
45890 
45891   CURSOR c5(p_batch_id NUMBER) IS
45892     SELECT rowid
45893     FROM   msc_st_job_op_resources
45894     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
45895     AND    sr_instance_code = v_instance_code
45896     AND    batch_id         = p_batch_id;
45897 
45898   CURSOR c6(p_batch_id NUMBER) IS
45899     SELECT rowid
45900     FROM   msc_st_job_op_resources
45901     WHERE  process_flag     = G_IN_PROCESS
45902     AND    sr_instance_code = v_instance_code
45903     AND    batch_id         = p_batch_id
45904     AND    NVL(resource_seq_num,NULL_VALUE) = NULL_VALUE
45905     AND    NVL(RESOURCE_SEQ_CODE, NULL_CHAR) <> NULL_CHAR
45906     AND    deleted_flag     = SYS_NO;
45907 
45908     BEGIN
45909 
45910     -- -- Pre processing for MSC_ST_JOB_OPERATION_NETWORKS
45911     --Duplicate records check for the records whose source is XML
45912 
45913        lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45914                  (p_app_short_name    => 'MSC',
45915                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
45916                   p_message_text      => lv_message_text,
45917                   p_error_text        => lv_error_text);
45918 
45919     IF lv_return <> 0 THEN
45920       RAISE ex_logging_err;
45921     END IF;
45922 
45923     v_sql_stmt := 1;
45924 
45925     lv_sql_stmt :=
45926   'UPDATE    msc_st_job_operation_networks mson1 '
45927   ||' SET       process_flag = '||G_ERROR_FLG||','
45928   ||'         error_text   = '||''''||lv_message_text||''''
45929   ||' WHERE     message_id < ( SELECT max(message_id) '
45930   ||'          FROM msc_st_job_operation_networks mson2'
45931   ||'          WHERE mson2.wip_entity_name  = mson1.wip_entity_name'
45932   ||'          AND   mson2.organization_code  = mson1.organization_code'
45933   ||'          AND   mson2.from_operation_seq_code = mson1.from_operation_seq_code'
45934   ||'          AND   mson2.to_operation_seq_code = mson1.to_operation_seq_code'
45935   ||'          AND   mson2.sr_instance_code   = mson1.sr_instance_code'
45936   ||'          AND   NVL(mson2.routing_name,'||''''||NULL_CHAR||''''||') '
45937   ||'                = NVL(mson1.routing_name, '||''''||NULL_CHAR||''''||') '
45938   ||'          AND   NVL(mson2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
45939   ||'                = NVL(mson1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
45940   ||'          AND   NVL(mson2.company_name,'||''''||NULL_CHAR||''''||') '
45941   ||'                 =    NVL(mson1.company_name,'||''''||NULL_CHAR||''''||') '
45942   ||'          AND   mson2.process_flag   ='||G_IN_PROCESS
45943   ||'          AND    NVL(mson2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
45944   ||' AND     mson1.process_flag = '||G_IN_PROCESS
45945   ||' AND     mson1.sr_instance_code = :v_instance_code'
45946   ||' AND     NVL(mson1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
45947 
45948 
45949          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45950 
45951   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
45952 
45953   --Duplicate records check for the records whose source is batch load
45954 
45955     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
45956                  (p_app_short_name    => 'MSC',
45957                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
45958                   p_message_text      => lv_message_text,
45959                   p_error_text        => lv_error_text);
45960 
45961     IF lv_return <> 0 THEN
45962       RAISE ex_logging_err;
45963     END IF;
45964 
45965     v_sql_stmt := 2;
45966 
45967   lv_sql_stmt :=
45968   'UPDATE   msc_st_job_operation_networks mson1'
45969   ||' SET      process_flag = '||G_ERROR_FLG||','
45970   ||'         error_text   = '||''''||lv_message_text||''''
45971   ||' WHERE    EXISTS ( SELECT 1 '
45972   ||'          FROM msc_st_job_operation_networks mson2'
45973   ||'          WHERE mson2.wip_entity_name  = mson1.wip_entity_name'
45974   ||'          AND   mson2.organization_code  = mson1.organization_code'
45975   ||'          AND   mson2.from_operation_seq_code = mson1.from_operation_seq_code'
45976   ||'          AND   mson2.to_operation_seq_code = mson1.to_operation_seq_code'
45977   ||'          AND   mson2.sr_instance_code   = mson1.sr_instance_code'
45978   ||'          AND   NVL(mson2.routing_name,'||''''||NULL_CHAR||''''||') '
45979   ||'                = NVL(mson1.routing_name, '||''''||NULL_CHAR||''''||') '
45980   ||'          AND   NVL(mson2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
45981   ||'                = NVL(mson1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
45982   ||'          AND   NVL(mson2.company_name,'||''''||NULL_CHAR||''''||') '
45983   ||'                 =    NVL(mson1.company_name,'||''''||NULL_CHAR||''''||') '
45984   ||'          AND   mson2.process_flag   ='||G_IN_PROCESS
45985   ||'          AND NVL(mson2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
45986   ||'          AND mson2.rowid <> mson1.rowid )'
45987   ||'  AND     mson1.process_flag          = '||G_IN_PROCESS
45988   ||'  AND     mson1.sr_instance_code       = :v_instance_code'
45989   ||'  AND     NVL(mson1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
45990 
45991 
45992          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
45993 
45994   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
45995 
45996   COMMIT;
45997 
45998   lv_column_names :=
45999    '  WIP_ENTITY_NAME              ||''~''||'
46000  ||'  FROM_OPERATION_SEQ_CODE      ||''~''||'
46001  ||'  FROM_OP_EFFECTIVITY_DATE     ||''~''||'
46002  ||'  TO_OPERATION_SEQ_CODE        ||''~''||'
46003  ||'  TO_OP_EFFECTIVITY_DATE       ||''~''||'
46004  ||'  ROUTING_NAME                 ||''~''||'
46005  ||'  ASSEMBLY_NAME                ||''~''||'
46006  ||'  ALTERNATE_ROUTING_DESIGNATOR ||''~''||'
46007  ||'  TRANSITION_TYPE              ||''~''||'
46008  ||'  PLANNING_PCT                 ||''~''||'
46009  ||'  ORGANIZATION_CODE            ||''~''||'
46010  ||'  SR_INSTANCE_CODE             ||''~''||'
46011  ||'  DELETED_FLAG                 ||''~''||'
46012  ||'  COMPANY_NAME ';
46013 
46014  -- Now processing in batch
46015     LOOP
46016 
46017       v_sql_stmt := 3;
46018 
46019       lv_cursor_stmt :=
46020       ' SELECT msc_st_batch_id_s.NEXTVAL '
46021       ||' FROM   dual';
46022 
46023       OPEN  c1 FOR lv_cursor_stmt;
46024       FETCH c1 INTO lv_batch_id;
46025       CLOSE c1;
46026 
46027 
46028       v_sql_stmt := 4;
46029 
46030       lv_sql_stmt :=
46031       ' UPDATE msc_st_job_operation_networks '
46032       ||' SET    batch_id  = :lv_batch_id'
46033       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
46034       ||' AND    sr_instance_code               = :v_instance_code'
46035       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
46036       ||' AND    rownum                        <= '||v_batch_size;
46037 
46038 
46039          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46040 
46041       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
46042 
46043       EXIT WHEN SQL%NOTFOUND;
46044 
46045       OPEN  c2(lv_batch_id);
46046       FETCH c2 BULK COLLECT INTO lb_rowid;
46047       CLOSE c2;
46048 
46049       v_sql_stmt := 5;
46050 
46051       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
46052       UPDATE msc_st_job_operation_networks
46053       SET  st_transaction_id = msc_st_job_op_networks_s.NEXTVAL,
46054            refresh_id        = v_refresh_id,
46055            last_update_date  = v_current_date,
46056            last_updated_by   = v_current_user,
46057            creation_date     = v_current_date,
46058            created_by        = v_current_user
46059       WHERE  rowid           = lb_rowid(j);
46060 
46061 
46062       -- error out the records where wip_entity_name is null
46063 
46064       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46065                      (p_app_short_name    => 'MSC',
46066                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
46067                       p_message_text      => lv_message_text,
46068                       p_error_text        => lv_error_text,
46069                       p_token1            => 'COLUMN_NAME',
46070                       p_token_value1      => 'WIP_ENTITY_NAME');
46071 
46072       IF lv_return <> 0 THEN
46073         RAISE ex_logging_err;
46074       END IF;
46075 
46076       v_sql_stmt := 6;
46077       lv_sql_stmt :=
46078       'UPDATE   msc_st_job_operation_networks'
46079       ||' SET   error_text   = '||''''||lv_message_text||''''||','
46080       ||'       process_flag = '||G_ERROR_FLG
46081       ||' WHERE NVL(wip_entity_name,'||''''||NULL_CHAR||''''||') '
46082       ||'       =                   '||''''||NULL_CHAR||''''
46083       ||' AND   process_flag                  = '||G_IN_PROCESS
46084       ||' AND   NVL(batch_id,'||NULL_VALUE||')= :lv_batch_id'
46085       ||' AND   sr_instance_code              = :v_instance_code';
46086 
46087 
46088         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46089 
46090       EXECUTE IMMEDIATE lv_sql_stmt
46091               USING     lv_batch_id,
46092                         v_instance_code;
46093 
46094 
46095       -- Set the  message
46096       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46097                      (p_app_short_name    => 'MSC',
46098                       p_error_code        => 'MSC_PP_NO_DELETION',
46099                       p_message_text      => lv_message_text,
46100                       p_error_text        => lv_error_text,
46101                       p_token1            => 'TABLE_NAME',
46102                       p_token_value1      => 'MSC_ST_JOB_OPERATION_NETWORKS');
46103 
46104       IF lv_return <> 0 THEN
46105         RAISE ex_logging_err;
46106       END IF;
46107 
46108      --Deletion is not allowed on this table.
46109       v_sql_stmt := 06;
46110       lv_sql_stmt :=
46111       ' UPDATE     msc_st_job_operation_networks '
46112       ||' SET      process_flag     ='||G_ERROR_FLG||','
46113       ||'          error_text       = '||''''||lv_message_text||''''
46114       ||' WHERE    deleted_flag     = '||SYS_YES
46115       ||' AND      process_flag     = '||G_IN_PROCESS
46116       ||' AND      batch_id         =  :lv_batch_id'
46117       ||' AND      sr_instance_code = :v_instance_code';
46118 
46119 
46120             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46121 
46122       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
46123 
46124       -- Set the error message
46125     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46126                      (p_app_short_name    => 'MSC',
46127                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
46128                       p_message_text      => lv_message_text,
46129                       p_error_text        => lv_error_text,
46130                       p_token1            => 'COLUMN_NAME',
46131                       p_token_value1      => 'DELETED_FLAG',
46132                       p_token2            => 'DEFAULT_VALUE',
46133                       p_token_value2      =>  SYS_NO  );
46134 
46135       IF lv_return <> 0 THEN
46136         RAISE ex_logging_err;
46137       END IF;
46138 
46139      -- Check whether delete flag value is in (1,2),if not default to SYS_NO
46140 
46141    lv_where_str :=
46142    ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
46143 
46144      lv_return := MSC_ST_UTIL.LOG_ERROR
46145                     (p_table_name        => 'MSC_ST_JOB_OPERATION_NETWORKS',
46146                      p_instance_code     => v_instance_code,
46147                      p_row               => lv_column_names,
46148                      p_severity          => G_SEV_WARNING,
46149                      p_message_text      => lv_message_text,
46150                      p_error_text        => lv_error_text,
46151                      p_batch_id          => lv_batch_id,
46152                      p_where_str         => lv_where_str,
46153                      p_col_name          => 'DELETED_FLAG',
46154                      p_debug             => v_debug,
46155                      p_default_value     => SYS_NO);
46156 
46157       IF lv_return <> 0 THEN
46158         RAISE ex_logging_err;
46159       END IF;
46160 
46161       -- Set the error message
46162     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46163                      (p_app_short_name    => 'MSC',
46164                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
46165                       p_message_text      => lv_message_text,
46166                       p_error_text        => lv_error_text,
46167                       p_token1            => 'COLUMN_NAME',
46168                       p_token_value1      => 'PLANNING_PCT');
46169 
46170       IF lv_return <> 0 THEN
46171         RAISE ex_logging_err;
46172       END IF;
46173 
46174      -- Error out the records whose PLANNING_PCT is NULL
46175 
46176      v_sql_stmt := 7;
46177     lv_sql_stmt :=
46178     'UPDATE    msc_st_job_operation_networks'
46179     ||' SET    process_flag     ='||G_ERROR_FLG||','
46180     ||'         error_text   = '||''''||lv_message_text||''''
46181     ||' WHERE  NVL(PLANNING_PCT,'||NULL_VALUE||') = '||NULL_VALUE
46182     ||' AND    sr_instance_code  = :v_instance_code'
46183     ||' AND    process_flag      = '||G_IN_PROCESS
46184     ||' AND    batch_id          = :lv_batch_id';
46185 
46186 
46187          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46188 
46189    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46190 
46191     -- Set the error message
46192     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46193                      (p_app_short_name    => 'MSC',
46194                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
46195                       p_message_text      => lv_message_text,
46196                       p_error_text        => lv_error_text,
46197                       p_token1            => 'COLUMN_NAME',
46198                       p_token_value1      => 'RECOMMENDED');
46199 
46200       IF lv_return <> 0 THEN
46201         RAISE ex_logging_err;
46202       END IF;
46203 
46204      -- Error out the records where RECOMMENDED is not 'Y' or 'N'
46205 
46206      v_sql_stmt := 7;
46207     lv_sql_stmt :=
46208     'UPDATE    msc_st_job_operation_networks'
46209     ||' SET    process_flag     ='||G_ERROR_FLG||','
46210     ||'         error_text   = '||''''||lv_message_text||''''
46211     ||' WHERE  recommended NOT IN (''Y'', ''N'')'
46212     ||' AND    sr_instance_code  = :v_instance_code'
46213     ||' AND    process_flag      = '||G_IN_PROCESS
46214     ||' AND    batch_id          = :lv_batch_id';
46215 
46216 
46217          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46218 
46219    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46220 
46221    -- Set the error message
46222     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46223                      (p_app_short_name    => 'MSC',
46224                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
46225                       p_message_text      => lv_message_text,
46226                       p_error_text        => lv_error_text,
46227                       p_token1            => 'COLUMN_NAME',
46228                       p_token_value1      => 'ASSEMBLY_NAME');
46229 
46230       IF lv_return <> 0 THEN
46231         RAISE ex_logging_err;
46232       END IF;
46233 
46234      -- Error out the records whose Assembly Name is NULL
46235 
46236      v_sql_stmt := 8;
46237     lv_sql_stmt :=
46238     'UPDATE    msc_st_job_operation_networks'
46239     ||' SET    process_flag     ='||G_ERROR_FLG||','
46240     ||'         error_text   = '||''''||lv_message_text||''''
46241     ||' WHERE  NVL(assembly_name,'||''''||NULL_CHAR||''''||') '
46242     ||'         = '||''''||NULL_CHAR||''''
46243     ||' AND    sr_instance_code  = :v_instance_code'
46244     ||' AND    process_flag      = '||G_IN_PROCESS
46245     ||' AND    batch_id          = :lv_batch_id';
46246 
46247 
46248          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46249 
46250    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46251 
46252    -- Now we will check whether Routing Name is NULL , if it is NULL we will populate the
46253      -- ASSEMBLY NAME in ROUTING_NAME column for all such records
46254 
46255     v_sql_stmt := 9;
46256 
46257     lv_sql_stmt :=
46258     'UPDATE msc_st_job_operation_networks '
46259     ||' SET    routing_name = assembly_name'
46260     ||' WHERE  sr_instance_code = :v_instance_code'
46261     ||' AND    process_flag     = '||G_IN_PROCESS
46262     ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||') '
46263     ||'         = '||''''||NULL_CHAR||''''
46264     ||' AND    batch_id         = :lv_batch_id';
46265 
46266 
46267          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46268 
46269       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46270 
46271    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46272                      (p_app_short_name    => 'MSC',
46273                       p_error_code        => 'MSC_PP_INVALID_VALUE',
46274                       p_message_text      => lv_message_text,
46275                       p_error_text        => lv_error_text,
46276                       p_token1            => 'COLUMN_NAME',
46277                       p_token_value1      => 'ORGANIZATION_CODE');
46278 
46279       IF lv_return <> 0 THEN
46280         RAISE ex_logging_err;
46281       END IF;
46282 
46283       --Derive Organization_id
46284       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
46285                      (p_table_name       => 'MSC_ST_JOB_OPERATION_NETWORKS',
46286                       p_org_partner_name => 'ORGANIZATION_CODE',
46287                       p_org_partner_id   => 'ORGANIZATION_ID',
46288                       p_instance_code    => v_instance_code,
46289                       p_partner_type     => G_ORGANIZATION,
46290                       p_error_text       => lv_error_text,
46291                       p_batch_id         => lv_batch_id,
46292                       p_severity         => G_SEV_ERROR,
46293                       p_message_text     => lv_message_text,
46294                       p_debug            => v_debug,
46295                       p_row              => lv_column_names);
46296 
46297       IF lv_return <> 0 THEN
46298         RAISE ex_logging_err;
46299       END IF;
46300 
46301     --Deriving wip_entity_id
46302       v_sql_stmt := 10;
46303       lv_sql_stmt :=
46304       'UPDATE msc_st_job_operation_networks mss'
46305       ||' SET wip_entity_id     = (SELECT local_id'
46306       ||'       FROM   msc_local_id_supply mls'
46307       ||'       WHERE  mls.char4 = mss.wip_entity_name'
46308       ||'       AND    mls.char3 = mss.organization_code'
46309       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
46310       ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
46311       ||'       AND    mls.char1 = mss.sr_instance_code'
46312       ||'       AND    mls.entity_name = ''WIP_ENTITY_ID'' )'
46313       ||' WHERE  process_flag                   = '||G_IN_PROCESS
46314       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
46315       ||' AND    sr_instance_code               = :v_instance_code';
46316 
46317 
46318         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46319 
46320       EXECUTE IMMEDIATE lv_sql_stmt
46321               USING     lv_batch_id,
46322                         v_instance_code;
46323 
46324     -- Set the error message
46325 
46326     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46327                      (p_app_short_name    => 'MSC',
46328                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
46329                       p_message_text      => lv_message_text,
46330                       p_error_text        => lv_error_text,
46331                       p_token1            => 'COLUMN_NAMES',
46332                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
46333                                              ||'ORGANIZATION_CODE,WIP_ENTITY_NAME',
46334                       p_token2            => 'MASTER_TABLE',
46335                       p_token_value2      =>  'MSC_ST_SUPPLIES',
46336                       p_token3            =>  'CHILD_TABLE' ,
46337                       p_token_value3      =>  'MSC_ST_JOB_OPERATION_NETWORKS' );
46338 
46339       IF lv_return <> 0 THEN
46340         RAISE ex_logging_err;
46341       END IF;
46342 
46343 
46344     -- Error out the records if WIP_ENTITY_ID NULL
46345 
46346     v_sql_stmt := 11;
46347     lv_sql_stmt:=
46348     'UPDATE      msc_st_job_operation_networks msrr'
46349     ||' SET      process_flag              ='||G_ERROR_FLG||','
46350     ||'          error_text   = '||''''||lv_message_text||''''
46351     ||' WHERE    NVL(wip_entity_id,'||NULL_VALUE||') = '||NULL_VALUE
46352     ||' AND      sr_instance_code            = :v_instance_code'
46353     ||' AND      process_flag                ='||G_IN_PROCESS
46354     ||' AND      batch_id                    = :lv_batch_id';
46355 
46356 
46357             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46358 
46359     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46360 
46361    -- from_operation_seq_code or to_operation_seq_code cannot be null.
46362    -- Set the error message
46363     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46364                      (p_app_short_name    => 'MSC',
46365                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
46366                       p_message_text      => lv_message_text,
46367                       p_error_text        => lv_error_text,
46368                       p_token1            => 'COLUMN_NAME',
46369                       p_token_value1      => 'FROM_OPERATION_SEQ_CODE OR '
46370                                            ||'TO_OPERATION_SEQ_CODE');
46371 
46372       IF lv_return <> 0 THEN
46373         RAISE ex_logging_err;
46374       END IF;
46375 
46376      -- Error out the records whose from_operation_seq_code or to_operation_seq_code is NULL
46377 
46378     v_sql_stmt := 12;
46379     lv_sql_stmt :=
46380     'UPDATE    msc_st_job_operation_networks'
46381     ||' SET    process_flag     ='||G_ERROR_FLG||','
46382     ||'         error_text   = '||''''||lv_message_text||''''
46383     ||' WHERE ( NVL(from_operation_seq_code,'||''''||NULL_CHAR||''''||') = '||''''||NULL_CHAR||''''
46384     ||' OR      NVL(to_operation_seq_code,'||''''||NULL_CHAR||''''||') = '||''''||NULL_CHAR||''''||') '
46385     ||' AND    sr_instance_code  = :v_instance_code'
46386     ||' AND    process_flag      = '||G_IN_PROCESS
46387     ||' AND    batch_id          = :lv_batch_id';
46388 
46389 
46390          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46391 
46392    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46393 
46394    -- from op_seq_code and to_op_seq_codes cannot be same
46395       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46396                      (p_app_short_name    => 'MSC',
46397                       p_error_code        => 'MSC_PP_FROM_TO_SAME',
46398                       p_message_text      => lv_message_text,
46399                       p_error_text        => lv_error_text,
46400                       p_token1            => 'COLUMN_NAME',
46401                       p_token_value1      => 'TO_OPERATION_SEQ_CODE');
46402 
46403       IF lv_return <> 0 THEN
46404         RAISE ex_logging_err;
46405       END IF;
46406 
46407      -- Error out the records whose from_operation_seq_code is same as to_op_seq_code
46408 
46409      v_sql_stmt := 13;
46410     lv_sql_stmt :=
46411     'UPDATE    msc_st_job_operation_networks'
46412     ||' SET    process_flag     ='||G_ERROR_FLG||','
46413     ||'        error_text   = '||''''||lv_message_text||''''
46414     ||' WHERE  NVL(to_operation_seq_code,'||''''||NULL_CHAR||''''||') = NVL(from_operation_seq_code,'||''''||NULL_CHAR||''''||')'
46415     ||' AND    sr_instance_code  = :v_instance_code'
46416     ||' AND    process_flag      = '||G_IN_PROCESS
46417     ||' AND    batch_id          = :lv_batch_id';
46418 
46419 
46420          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46421 
46422     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46423 
46424     -- Derive routing_sequence_id
46425 
46426    -- Set the error message
46427     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46428                      (p_app_short_name    => 'MSC',
46429                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
46430                       p_message_text      => lv_message_text,
46431                       p_error_text        => lv_error_text,
46432                       p_token1            => 'COLUMN_NAMES',
46433                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
46434                                              ||'ORGANIZATION_CODE,ROUTING_NAME',
46435                       p_token2            => 'MASTER_TABLE',
46436                       p_token_value2      =>  'MSC_ST_ROUTINGS',
46437                       p_token3            =>  'CHILD_TABLE' ,
46438                       p_token_value3      =>  'MSC_ST_JOB_OPERATION_NETWORKS' );
46439 
46440       IF lv_return <> 0 THEN
46441         RAISE ex_logging_err;
46442       END IF;
46443 
46444     -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
46445 
46446     lv_return :=
46447     MSC_ST_UTIL.DERIVE_ROUTING_SEQUENCE_ID
46448                       (p_table_name     => 'MSC_ST_JOB_OPERATION_NETWORKS',
46449                        p_rtg_col_name   => 'ROUTING_NAME',
46450                        p_rtg_col_id     => 'ROUTING_SEQUENCE_ID',
46451                        p_instance_code  => v_instance_code,
46452                        p_severity       => G_SEV_ERROR,
46453                        p_message_text   => lv_message_text,
46454                        p_batch_id       => lv_batch_id,
46455                        p_debug          => v_debug,
46456                        p_error_text     => lv_error_text);
46457 
46458     if (lv_return <> 0 )then
46459        RAISE ex_logging_err;
46460     end if;
46461 
46462 
46463     -- Set the error message
46464     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46465                      (p_app_short_name    => 'MSC',
46466                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
46467                       p_message_text      => lv_message_text,
46468                       p_error_text        => lv_error_text,
46469                       p_token1            => 'COLUMN_NAME',
46470                       p_token_value1      => 'FROM_OP_EFFECTIVITY_DATE',
46471                       p_token2            => 'DEFAULT_VALUE',
46472                       p_token_value2      => 'SYSDATE' );
46473 
46474       IF lv_return <> 0 THEN
46475         RAISE ex_logging_err;
46476       END IF;
46477 
46478     -- Default from operation effectivity date as SYSDATE if NULL
46479 
46480      v_sql_stmt := 14;
46481      lv_where_str :=
46482      ' AND NVL(from_op_effectivity_date,SYSDATE-36500 ) = SYSDATE-36500 ' ;
46483 
46484      lv_return := MSC_ST_UTIL.LOG_ERROR
46485                     (p_table_name        => 'MSC_ST_JOB_OPERATION_NETWORKS',
46486                      p_instance_code     => v_instance_code,
46487                      p_row               => lv_column_names,
46488                      p_severity          => G_SEV_WARNING,
46489                      p_message_text      => lv_message_text,
46490                      p_error_text        => lv_error_text,
46491                      p_batch_id          => lv_batch_id,
46492                      p_where_str         => lv_where_str,
46493                      p_col_name          => 'FROM_OP_EFFECTIVITY_DATE',
46494                      p_debug             => v_debug,
46495                      p_default_value     => 'SYSDATE');
46496 
46497       IF lv_return <> 0 THEN
46498         RAISE ex_logging_err;
46499       END IF;
46500 
46501 
46502    -- Set the error message
46503     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46504                      (p_app_short_name    => 'MSC',
46505                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
46506                       p_message_text      => lv_message_text,
46507                       p_error_text        => lv_error_text,
46508                       p_token1            => 'COLUMN_NAME',
46509                       p_token_value1      => 'TO_OP_EFFECTIVITY_DATE',
46510                       p_token2            => 'DEFAULT_VALUE',
46511                       p_token_value2      => 'SYSDATE' );
46512 
46513       IF lv_return <> 0 THEN
46514         RAISE ex_logging_err;
46515       END IF;
46516 
46517     -- Default to operation effectivity date as SYSDATE if NULL
46518      v_sql_stmt := 15;
46519      lv_where_str :=
46520      ' AND NVL(to_op_effectivity_date,SYSDATE-36500 ) = SYSDATE-36500 ' ;
46521 
46522      lv_return := MSC_ST_UTIL.LOG_ERROR
46523                     (p_table_name        => 'MSC_ST_JOB_OPERATION_NETWORKS',
46524                      p_instance_code     => v_instance_code,
46525                      p_row               => lv_column_names,
46526                      p_severity          => G_SEV_WARNING,
46527                      p_message_text      => lv_message_text,
46528                      p_error_text        => lv_error_text,
46529                      p_batch_id          => lv_batch_id,
46530                      p_where_str         => lv_where_str,
46531                      p_col_name          => 'TO_OP_EFFECTIVITY_DATE',
46532                      p_debug             => v_debug,
46533                      p_default_value     => 'SYSDATE');
46534 
46535       IF lv_return <> 0 THEN
46536         RAISE ex_logging_err;
46537       END IF;
46538 
46539        -- Derive from_op_seq_id,  from_op_seq_num
46540 
46541     v_sql_stmt := 16;
46542     lv_sql_Stmt :=
46543     'UPDATE     msc_st_job_operation_networks mson'
46544     ||' SET     (from_op_seq_id, from_op_seq_num) =  (SELECT local_id, number1'
46545     ||'         FROM msc_local_id_setup mlis'
46546     ||'         WHERE  mlis.char1 = mson.sr_instance_code'
46547     ||'         AND     NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
46548     ||'          =    NVL(mson.company_name,'||''''||NULL_CHAR||''''||') '
46549     ||'         AND    mlis.char3 = mson.organization_code'
46550     ||'         AND    mlis.char4 = mson.routing_name'
46551     ||'         AND    mlis.char5 = mson.from_operation_seq_code'
46552     ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
46553     ||'           =   NVL(mson.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
46554     ||'         AND    mlis.date1 = mson.FROM_OP_EFFECTIVITY_DATE'
46555     ||'         AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
46556     ||'         AND    mlis.instance_id = '||v_instance_id||')'
46557     ||'  WHERE      sr_instance_code = :v_instance_code'
46558     ||'  AND        process_flag     ='||G_IN_PROCESS
46559     ||'  AND        batch_id         = :lv_batch_id';
46560 
46561 
46562          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46563 
46564    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46565 
46566    -- from_op_seq_num cannot be null. Error out the records with null value for them.
46567    -- Set the error message
46568 
46569     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46570                      (p_app_short_name    => 'MSC',
46571                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
46572                       p_message_text      => lv_message_text,
46573                       p_error_text        => lv_error_text,
46574                       p_token1            => 'COLUMN_NAME',
46575                       p_token_value1      => 'FROM_OP_SEQ_ID OR '
46576                                            ||'FROM_OP_SEQ_NUM');
46577 
46578       IF lv_return <> 0 THEN
46579         RAISE ex_logging_err;
46580       END IF;
46581 
46582      -- Error out the records whose from_op_seq_id, from _op_seq_num is NULL
46583 
46584     v_sql_stmt := 17;
46585     lv_sql_stmt :=
46586     'UPDATE    msc_st_job_operation_networks'
46587     ||' SET    process_flag     ='||G_ERROR_FLG||','
46588     ||'         error_text   = '||''''||lv_message_text||''''
46589     ||' WHERE  (NVL(from_op_seq_id,'||NULL_VALUE||') ='||NULL_VALUE
46590     ||' OR     NVL(from_op_seq_num,'||NULL_VALUE||') ='||NULL_VALUE||')'
46591     ||' AND    sr_instance_code  = :v_instance_code'
46592     ||' AND    process_flag      = '||G_IN_PROCESS
46593     ||' AND    batch_id          = :lv_batch_id';
46594 
46595 
46596          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46597 
46598     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46599 
46600 
46601    -- Derive to_op_seq_id,  to_op_seq_num
46602 
46603     v_sql_stmt := 18;
46604     lv_sql_Stmt :=
46605     'UPDATE     msc_st_job_operation_networks mson'
46606     ||' SET     (to_op_seq_id, to_op_seq_num) =  (SELECT local_id, number1'
46607     ||'         FROM msc_local_id_setup mlis'
46608     ||'         WHERE  mlis.char1 = mson.sr_instance_code'
46609     ||'         AND     NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
46610     ||'          =    NVL(mson.company_name,'||''''||NULL_CHAR||''''||') '
46611     ||'         AND    mlis.char3 = mson.organization_code'
46612     ||'         AND    mlis.char4 = mson.routing_name'
46613     ||'         AND    mlis.char5 = mson.to_operation_seq_code'
46614     ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
46615     ||'           =   NVL(mson.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
46616     ||'         AND    mlis.date1 = mson.TO_OP_EFFECTIVITY_DATE'
46617     ||'         AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
46618     ||'         AND    mlis.instance_id = '||v_instance_id||')'
46619     ||'  WHERE      sr_instance_code = :v_instance_code'
46620     ||'  AND        process_flag     ='||G_IN_PROCESS
46621     ||'  AND        batch_id         = :lv_batch_id';
46622 
46623 
46624          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46625 
46626    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46627 
46628    -- to_op_seq_num cannot be null. Error out the records with null value for them.
46629    -- Set the error message
46630 
46631     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46632                      (p_app_short_name    => 'MSC',
46633                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
46634                       p_message_text      => lv_message_text,
46635                       p_error_text        => lv_error_text,
46636                       p_token1            => 'COLUMN_NAME',
46637                       p_token_value1      => 'TO_OP_SEQ_ID OR '
46638                                            ||'TO_OP_SEQ_NUM');
46639 
46640       IF lv_return <> 0 THEN
46641         RAISE ex_logging_err;
46642       END IF;
46643 
46644      -- Error out the records whose to_op_seq_id, to_op_seq_num is NULL
46645 
46646     v_sql_stmt := 19;
46647     lv_sql_stmt :=
46648     'UPDATE    msc_st_job_operation_networks'
46649     ||' SET    process_flag     ='||G_ERROR_FLG||','
46650     ||'         error_text   = '||''''||lv_message_text||''''
46651     ||' WHERE  (NVL(to_op_seq_id,'||NULL_VALUE||') ='||NULL_VALUE
46652     ||' OR     NVL(to_op_seq_num,'||NULL_VALUE||') ='||NULL_VALUE||')'
46653     ||' AND    sr_instance_code  = :v_instance_code'
46654     ||' AND    process_flag      = '||G_IN_PROCESS
46655     ||' AND    batch_id          = :lv_batch_id';
46656 
46657 
46658          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46659 
46660     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
46661 
46662     -- Set the error message
46663     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46664                      (p_app_short_name    => 'MSC',
46665                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
46666                       p_message_text      => lv_message_text,
46667                       p_error_text        => lv_error_text,
46668                       p_token1            => 'COLUMN_NAME',
46669                       p_token_value1      => 'TRANSITION_TYPE',
46670                       p_token2            => 'DEFAULT_VALUE',
46671                       p_token_value2      =>  G_TRANSITION_TYPE);
46672 
46673       IF lv_return <> 0 THEN
46674         RAISE ex_logging_err;
46675       END IF;
46676 
46677      -- Defualt transition type to 2(i.e. alternate) if not in (1,2)
46678      v_sql_stmt := 20;
46679      lv_where_str :=
46680      ' AND NVL(transition_type,'||NULL_VALUE||') NOT IN (1,2)';
46681 
46682      lv_return := MSC_ST_UTIL.LOG_ERROR
46683                     (p_table_name       => 'MSC_ST_JOB_OPERATION_NETWORKS',
46684                      p_instance_code     => v_instance_code,
46685                      p_row               => lv_column_names,
46686                      p_severity          => G_SEV_WARNING,
46687                      p_message_text      => lv_message_text,
46688                      p_error_text        => lv_error_text,
46689                      p_batch_id          => lv_batch_id,
46690                      p_where_str         => lv_where_str,
46691                      p_col_name          => 'TRANSITION_TYPE',
46692                      p_debug             => v_debug,
46693                      p_default_value     => G_TRANSITION_TYPE);
46694 
46695 
46696       IF lv_return <> 0 THEN
46697         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
46698       END IF;
46699 
46700     --Call to customised validation.
46701 
46702       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
46703         (ERRBUF         => lv_error_text,
46704          RETCODE        => lv_return,
46705          pBatchID       => lv_batch_id,
46706          pInstanceCode  => v_instance_code,
46707          pEntityName    => 'MSC_ST_JOB_OPERATION_NETWORKS',
46708          pInstanceID    => v_instance_id);
46709 
46710       IF NVL(lv_return,0) <> 0 THEN
46711         RAISE ex_logging_err;
46712       END IF;
46713 
46714    COMMIT;
46715  END LOOP;
46716 
46717  -- There should be at the max only one primary path from a from_operation_seq_code
46718  -- Else error out all the records for that from_operation_seq_code
46719 
46720     -- Set the error message
46721     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46722                      (p_app_short_name    => 'MSC',
46723                       p_error_code        => 'MSC_PP_MULTI_PRI_PATH',
46724                       p_message_text      => lv_message_text,
46725                       p_error_text        => lv_error_text,
46726                       p_token1            => 'COLUMN_NAME',
46727                       p_token_value1      => 'TRANSITION_TYPE');
46728 
46729       IF lv_return <> 0 THEN
46730         RAISE ex_logging_err;
46731       END IF;
46732 
46733      v_sql_stmt := 21;
46734     lv_sql_stmt :=
46735   'UPDATE   msc_st_job_operation_networks mson1'
46736   ||' SET     process_flag = '||G_ERROR_FLG||','
46737   ||'         error_text   = '||''''||lv_message_text||''''
46738   ||' WHERE  1 <  ( SELECT COUNT(*) '
46739   ||'          FROM msc_st_job_operation_networks mson2'
46740   ||'          WHERE mson2.wip_entity_name      = mson1.wip_entity_name'
46741   ||'          AND   mson2.organization_code  = mson1.organization_code'
46742   ||'          AND   mson2.from_operation_seq_code  = mson1.from_operation_seq_code'
46743   ||'          AND   mson2.sr_instance_code   = mson1.sr_instance_code'
46744   ||'          AND   NVL(mson2.company_name,'||''''||NULL_CHAR||''''||') '
46745   ||'                 =    NVL(mson1.company_name,'||''''||NULL_CHAR||''''||') '
46746   ||'          AND   mson2.transition_type = 1 '
46747   ||'          AND   mson2.process_flag   ='||G_IN_PROCESS
46748   ||'          AND   mson2.deleted_flag <>1 '
46749   ||'         GROUP BY wip_entity_name, sr_instance_code,company_name,organization_code,from_operation_seq_code)'
46750   ||'  AND     mson1.process_flag          = '||G_IN_PROCESS
46751   ||'  AND     mson1.deleted_flag <>1 '
46752   ||'  AND     mson1.sr_instance_code       = :v_instance_code';
46753 
46754 
46755 
46756          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46757 
46758    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
46759 
46760    -- The summation of the planning_pct's from a from_operation_sed_code should be exactly 100.
46761     -- Else error out all the records for that from_operation_seq_code
46762 
46763     -- Set the error message
46764     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46765                      (p_app_short_name    => 'MSC',
46766                       p_error_code        => 'MSC_INVALID_PLANNING_PCT',
46767                       p_message_text      => lv_message_text,
46768                       p_error_text        => lv_error_text,
46769                       p_token1            => 'COLUMN_NAME',
46770                       p_token_value1      => 'PLANNING_PCT');
46771 
46772       IF lv_return <> 0 THEN
46773         RAISE ex_logging_err;
46774       END IF;
46775 
46776      v_sql_stmt := 22;
46777     lv_sql_stmt :=
46778   'UPDATE   msc_st_job_operation_networks mson1'
46779   ||' SET      process_flag = '||G_ERROR_FLG||','
46780   ||'         error_text   = '||''''||lv_message_text||''''
46781   ||' WHERE 100 <>   ( SELECT SUM(planning_pct) '
46782   ||'          FROM msc_st_job_operation_networks mson2'
46783   ||'          WHERE mson2.wip_entity_name      = mson1.wip_entity_name'
46784   ||'          AND   mson2.organization_code  = mson1.organization_code'
46785   ||'          AND   mson2.from_operation_seq_code  = mson1.from_operation_seq_code'
46786   ||'          AND   mson2.sr_instance_code   = mson1.sr_instance_code'
46787   ||'          AND   NVL(mson2.company_name,'||''''||NULL_CHAR||''''||') '
46788   ||'                 =    NVL(mson1.company_name,'||''''||NULL_CHAR||''''||') '
46789   ||'          AND   mson2.process_flag   ='||G_IN_PROCESS
46790   ||'          AND   mson2.deleted_flag <>1 '
46791   ||'         GROUP BY wip_entity_name,sr_instance_code,company_name,organization_code,from_operation_seq_code)'
46792   ||'  AND     mson1.process_flag          = '||G_IN_PROCESS
46793   ||'  AND     mson1.deleted_flag <>1 '
46794   ||'  AND     mson1.sr_instance_code       = :v_instance_code';
46795 
46796 
46797          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46798 
46799   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
46800 
46801   -- Set the process flag as Valid and derive sr_instance_id
46802     lv_return :=
46803     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_JOB_OPERATION_NETWORKS',
46804                                p_instance_id    => v_instance_id,
46805                                p_instance_code  => v_instance_code,
46806                                p_process_flag   => G_VALID,
46807                                p_error_text     => lv_error_text,
46808                                p_debug          => v_debug);
46809 
46810       IF lv_return < 0 THEN
46811         RAISe ex_logging_err;
46812       END IF;
46813 
46814     -- At the end calling  the LOG_ERROR for logging all errored out records
46815 
46816    lv_return :=
46817    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_JOB_OPERATION_NETWORKS',
46818                          p_instance_code => v_instance_code,
46819                          p_row           => lv_column_names,
46820                          p_severity      => G_SEV_ERROR,
46821                          p_message_text  => NULL,
46822                          p_error_text    => lv_error_text,
46823                          p_debug         => v_debug );
46824 
46825     IF lv_return < 0 THEN
46826         Raise ex_logging_err;
46827     END IF;
46828 
46829     -- Validation for MSC_ST_JOB_OPERATIONS
46830 
46831     --Duplicate records check for the records whose source is XML
46832 
46833     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46834                  (p_app_short_name    => 'MSC',
46835                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
46836                   p_message_text      => lv_message_text,
46837                   p_error_text        => lv_error_text);
46838 
46839     IF lv_return <> 0 THEN
46840       RAISE ex_logging_err;
46841     END IF;
46842 
46843     v_sql_stmt := 18;
46844 
46845   lv_sql_stmt :=
46846   'UPDATE    msc_st_job_operations msro1 '
46847   ||' SET       process_flag = '||G_ERROR_FLG||','
46848   ||'           error_text   = '||''''||lv_message_text||''''
46849   ||' WHERE    message_id < ( SELECT max(message_id) '
46850   ||'          FROM msc_st_job_operations msro2'
46851   ||'          WHERE msro2.sr_instance_code   = msro1.sr_instance_code'
46852   ||'          AND   msro2.organization_code  = msro1.organization_code'
46853   ||'          AND   msro2.wip_entity_name    = msro1.wip_entity_name'
46854   ||'          AND   NVL(msro2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
46855   ||'                = NVL(msro1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
46856   ||'          AND   NVL(msro2.routing_name,'||''''||NULL_CHAR||''''||') '
46857   ||'                = NVL(msro1.routing_name, '||''''||NULL_CHAR||''''||') '
46858   ||'          AND   NVL(msro2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
46859   ||'                = NVL(msro1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
46860   ||'          AND   msro2.process_flag   ='||G_IN_PROCESS
46861   ||'          AND    NVL(msro2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
46862   ||' AND     msro1.process_flag = '||G_IN_PROCESS
46863   ||' AND     msro1.sr_instance_code = :v_instance_code'
46864   ||' AND     NVL(msro1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
46865 
46866 
46867          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46868 
46869   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
46870 
46871   --Duplicate records check for the records whose source is batch load
46872 
46873     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46874                  (p_app_short_name    => 'MSC',
46875                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
46876                   p_message_text      => lv_message_text,
46877                   p_error_text        => lv_error_text);
46878 
46879     IF lv_return <> 0 THEN
46880       RAISE ex_logging_err;
46881     END IF;
46882 
46883   v_sql_stmt := 19;
46884   lv_sql_stmt :=
46885   'UPDATE   msc_st_job_operations msro1'
46886   ||' SET      process_flag = '||G_ERROR_FLG||','
46887   ||'         error_text   = '||''''||lv_message_text||''''
46888   ||' WHERE    EXISTS ( SELECT 1 '
46889   ||'          FROM msc_st_job_operations msro2'
46890   ||'          WHERE msro2.sr_instance_code   = msro1.sr_instance_code'
46891   ||'          AND   msro2.organization_code  = msro1.organization_code'
46892   ||'          AND   msro2.wip_entity_name    = msro1.wip_entity_name'
46893   ||'          AND   NVL(msro2.company_name,'||''''||NULL_CHAR||''''||') '
46894   ||'                 =    NVL(msro1.company_name,'||''''||NULL_CHAR||''''||') '
46895   ||'          AND   NVL(msro2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
46896   ||'                = NVL(msro1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
46897   ||'          AND   NVL(msro2.routing_name,'||''''||NULL_CHAR||''''||') '
46898   ||'                = NVL(msro1.routing_name, '||''''||NULL_CHAR||''''||') '
46899   ||'          AND   NVL(msro2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
46900   ||'                = NVL(msro1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
46901   ||'          AND   msro2.process_flag   ='||G_IN_PROCESS
46902   ||'               AND NVL(msro2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
46903   ||'         GROUP BY sr_instance_code,company_name,organization_code,wip_entity_name,operation_seq_code'
46904   ||'          HAVING COUNT(*) >1 )'
46905   ||'  AND     msro1.process_flag          = '||G_IN_PROCESS
46906   ||'  AND     msro1.sr_instance_code       = :v_instance_code'
46907   ||'  AND     NVL(msro1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
46908 
46909 
46910          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46911 
46912   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
46913 
46914 
46915   lv_column_names :=
46916   'ROUTING_NAME                   ||''~''||'
46917   ||' WIP_ENTITY_NAME             ||''~''||'
46918   ||' ASSEMBLY_NAME               ||''~''||'
46919   ||' ALTERNATE_ROUTING_DESIGNATOR ||''~''||'
46920   ||' OPERATION_SEQ_CODE          ||''~''||'
46921   ||' EFFECTIVITY_DATE            ||''~''||'
46922   ||' RECO_START_DATE             ||''~''||'
46923   ||' DEPARTMENT_CODE             ||''~''||'
46924   ||' ORGANIZATION_CODE           ||''~''||'
46925   ||' SR_INSTANCE_CODE            ||''~''||'
46926   ||' RECO_COMPLETION_DATE        ||''~''||'
46927   ||' DISABLE_DATE                ||''~''||'
46928   ||' NETWORK_START_END           ||''~''||'
46929   ||' OPERATION_TYPE              ||''~''||'
46930   ||' MINIMUM_TRANSFER_QUANTITY   ||''~''||'
46931   ||' YIELD                       ||''~''||'
46932   ||' OPERATION_LEAD_TIME_PERCENT ||''~''||'
46933   ||' CUMULATIVE_YIELD            ||''~''||'
46934   ||' REVERSE_CUMULATIVE_YIELD    ||''~''||'
46935   ||' NET_PLANNING_PERCENT        ||''~''||'
46936   ||' STANDARD_OPERATION_CODE     ||''~''||'
46937   ||' DELETED_FLAG                ||''~''||'
46938   ||' COMPANY_NAME ';
46939 
46940  LOOP
46941 
46942      lv_cursor_stmt :=
46943       ' SELECT msc_st_batch_id_s.NEXTVAL '
46944       ||' FROM   dual';
46945 
46946       OPEN  c1 FOR lv_cursor_stmt;
46947       FETCH c1 INTO lv_batch_id;
46948       CLOSE c1;
46949 
46950       v_sql_stmt := 21;
46951       lv_sql_stmt :=
46952       ' UPDATE msc_st_job_operations '
46953       ||' SET    batch_id  = :lv_batch_id'
46954       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
46955       ||' AND    sr_instance_code               = :v_instance_code'
46956       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
46957       ||' AND    rownum                        <= '||v_batch_size;
46958 
46959 
46960          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
46961 
46962       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
46963 
46964       EXIT WHEN SQL%NOTFOUND;
46965 
46966       OPEN  c3(lv_batch_id);
46967       FETCH c3 BULK COLLECT INTO lb_rowid;
46968       CLOSE c3;
46969 
46970       v_sql_stmt := 22;
46971       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
46972       UPDATE msc_st_job_operations
46973       SET  st_transaction_id = msc_st_job_operations_s.NEXTVAL,
46974            refresh_id        = v_refresh_id,
46975            last_update_date  = v_current_date,
46976            last_updated_by   = v_current_user,
46977            creation_date     = v_current_date,
46978            created_by        = v_current_user
46979       WHERE  rowid           = lb_rowid(j);
46980 
46981 
46982       -- Set the  message
46983       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
46984                      (p_app_short_name    => 'MSC',
46985                       p_error_code        => 'MSC_PP_NO_DELETION',
46986                       p_message_text      => lv_message_text,
46987                       p_error_text        => lv_error_text,
46988                       p_token1            => 'TABLE_NAME',
46989                       p_token_value1      => 'MSC_ST_JOB_OPERATIONS');
46990 
46991       IF lv_return <> 0 THEN
46992         RAISE ex_logging_err;
46993       END IF;
46994 
46995      --Deletion is not allowed on this table.
46996       v_sql_stmt := 06;
46997       lv_sql_stmt :=
46998       ' UPDATE     msc_st_job_operations '
46999       ||' SET      process_flag     ='||G_ERROR_FLG||','
47000       ||'          error_text       = '||''''||lv_message_text||''''
47001       ||' WHERE    deleted_flag     = '||SYS_YES
47002       ||' AND      process_flag     = '||G_IN_PROCESS
47003       ||' AND      batch_id         =  :lv_batch_id'
47004       ||' AND      sr_instance_code = :v_instance_code';
47005 
47006 
47007             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47008 
47009       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
47010 
47011     -- Set the error message
47012     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47013                      (p_app_short_name    => 'MSC',
47014                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
47015                       p_message_text      => lv_message_text,
47016                       p_error_text        => lv_error_text,
47017                       p_token1            => 'COLUMN_NAME',
47018                       p_token_value1      => 'DELETED_FLAG',
47019                       p_token2            => 'DEFAULT_VALUE',
47020                       p_token_value2      =>  SYS_NO  );
47021 
47022       IF lv_return <> 0 THEN
47023         RAISE ex_logging_err;
47024       END IF;
47025 
47026 
47027      -- Check whether delete flag value is in (1,2),if not default to SYS_NO
47028 
47029    lv_where_str :=
47030    ' AND NVL(deleted_flag,'||NULL_VALUE||') <>'||SYS_NO;
47031 
47032      lv_return := MSC_ST_UTIL.LOG_ERROR
47033                     (p_table_name        => 'MSC_ST_JOB_OPERATIONS',
47034                      p_instance_code     => v_instance_code,
47035                      p_row               => lv_column_names,
47036                      p_severity          => G_SEV_WARNING,
47037                      p_message_text      => lv_message_text,
47038                      p_error_text        => lv_error_text,
47039                      p_batch_id          => lv_batch_id,
47040                      p_where_str         => lv_where_str,
47041                      p_col_name          => 'DELETED_FLAG',
47042                      p_debug             => v_debug,
47043                      p_default_value     => SYS_NO);
47044 
47045       IF lv_return <> 0 THEN
47046         RAISE ex_logging_err;
47047       END IF;
47048 
47049       -- Set the  message
47050 
47051      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47052                      (p_app_short_name    => 'MSC',
47053                       p_error_code        => 'MSC_PP_INVALID_VALUE',
47054                       p_message_text      => lv_message_text,
47055                       p_error_text        => lv_error_text,
47056                       p_token1            => 'COLUMN_NAME',
47057                       p_token_value1      => 'ORGANIZATION_CODE');
47058 
47059       IF lv_return <> 0 THEN
47060         RAISE ex_logging_err;
47061       END IF;
47062 
47063     -- derive  organization id ,
47064      lv_return :=
47065      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_JOB_OPERATIONS',
47066                                     p_org_partner_name  =>'ORGANIZATION_CODE',
47067                                     p_org_partner_id    =>'ORGANIZATION_ID',
47068                                     p_instance_code     => v_instance_code,
47069                                     p_partner_type      => G_ORGANIZATION,
47070                                     p_severity          => G_SEV_ERROR,
47071                                     p_message_text      => lv_message_text,
47072                                     p_batch_id          =>lv_batch_id,
47073                                     p_debug             => v_debug,
47074                                     p_error_text        =>lv_error_text);
47075 
47076      if(lv_return <> 0) then
47077         RAISE ex_logging_err;
47078      end if;
47079 
47080     -- error out the records where wip_entity_name is null
47081 
47082       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47083                      (p_app_short_name    => 'MSC',
47084                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
47085                       p_message_text      => lv_message_text,
47086                       p_error_text        => lv_error_text,
47087                       p_token1            => 'COLUMN_NAME',
47088                       p_token_value1      => 'WIP_ENTITY_NAME');
47089 
47090       IF lv_return <> 0 THEN
47091         RAISE ex_logging_err;
47092       END IF;
47093 
47094       v_sql_stmt := 6;
47095       lv_sql_stmt :=
47096       'UPDATE   msc_st_job_operations'
47097       ||' SET   error_text   = '||''''||lv_message_text||''''||','
47098       ||'       process_flag = '||G_ERROR_FLG
47099       ||' WHERE NVL(wip_entity_name,'||''''||NULL_CHAR||''''||') '
47100       ||'       =                   '||''''||NULL_CHAR||''''
47101       ||' AND   process_flag                  = '||G_IN_PROCESS
47102       ||' AND   NVL(batch_id,'||NULL_VALUE||')= :lv_batch_id'
47103       ||' AND   sr_instance_code              = :v_instance_code';
47104 
47105 
47106         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47107 
47108       EXECUTE IMMEDIATE lv_sql_stmt
47109               USING     lv_batch_id,
47110                         v_instance_code;
47111 
47112 
47113      --Deriving wip_entity_id
47114       v_sql_stmt := 10;
47115       lv_sql_stmt :=
47116       'UPDATE msc_st_job_operations mss'
47117       ||' SET wip_entity_id     = (SELECT local_id'
47118       ||'       FROM   msc_local_id_supply mls'
47119       ||'       WHERE  mls.char4 = mss.wip_entity_name'
47120       ||'       AND    mls.char3 = mss.organization_code'
47121       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
47122       ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
47123       ||'       AND    mls.char1 = mss.sr_instance_code'
47124       ||'       AND    mls.entity_name = ''WIP_ENTITY_ID'' )'
47125       ||' WHERE  process_flag                   = '||G_IN_PROCESS
47126       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
47127       ||' AND    sr_instance_code               = :v_instance_code';
47128 
47129 
47130         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47131 
47132       EXECUTE IMMEDIATE lv_sql_stmt
47133               USING     lv_batch_id,
47134                         v_instance_code;
47135 
47136     -- Set the error message
47137 
47138     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47139                      (p_app_short_name    => 'MSC',
47140                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
47141                       p_message_text      => lv_message_text,
47142                       p_error_text        => lv_error_text,
47143                       p_token1            => 'COLUMN_NAMES',
47144                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
47145                                              ||'ORGANIZATION_CODE,WIP_ENTITY_NAME',
47146                       p_token2            => 'MASTER_TABLE',
47147                       p_token_value2      =>  'MSC_ST_SUPPLIES',
47148                       p_token3            =>  'CHILD_TABLE' ,
47149                       p_token_value3      =>  'MSC_ST_JOB_OPERATION_NETWORKS' );
47150 
47151       IF lv_return <> 0 THEN
47152         RAISE ex_logging_err;
47153       END IF;
47154 
47155 
47156     -- Error out the records if WIP_ENTITY_ID NULL
47157 
47158     v_sql_stmt := 11;
47159     lv_sql_stmt:=
47160     'UPDATE      msc_st_job_operations msrr'
47161     ||' SET      process_flag              ='||G_ERROR_FLG||','
47162     ||'          error_text   = '||''''||lv_message_text||''''
47163     ||' WHERE    NVL(wip_entity_id,'||NULL_VALUE||') = '||NULL_VALUE
47164     ||' AND      sr_instance_code            = :v_instance_code'
47165     ||' AND      process_flag                ='||G_IN_PROCESS
47166     ||' AND      batch_id                    = :lv_batch_id';
47167 
47168 
47169             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47170 
47171     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
47172 
47173      -- Set the error message
47174     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47175                      (p_app_short_name    => 'MSC',
47176                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
47177                       p_message_text      => lv_message_text,
47178                       p_error_text        => lv_error_text,
47179                       p_token1            => 'COLUMN_NAME',
47180                       p_token_value1      => 'RECOMMENDED');
47181 
47182       IF lv_return <> 0 THEN
47183         RAISE ex_logging_err;
47184       END IF;
47185 
47186      -- Error out the records where RECOMMENDED is not 'Y' or 'N'
47187 
47188      v_sql_stmt := 7;
47189     lv_sql_stmt :=
47190     'UPDATE    msc_st_job_operations'
47191     ||' SET    process_flag     ='||G_ERROR_FLG||','
47192     ||'         error_text   = '||''''||lv_message_text||''''
47193     ||' WHERE  recommended NOT IN (''Y'', ''N'')'
47194     ||' AND    sr_instance_code  = :v_instance_code'
47195     ||' AND    process_flag      = '||G_IN_PROCESS
47196     ||' AND    batch_id          = :lv_batch_id';
47197 
47198 
47199          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47200 
47201    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
47202 
47203    -- Now we will check whether Routing Name is NULL , if it is NULL we will populate the
47204    -- ASSEMBLY NAME in ROUTING_NAME column for all such records
47205 
47206     lv_sql_stmt :=
47207     'UPDATE msc_st_job_operations '
47208     ||' SET    routing_name = assembly_name'
47209     ||' WHERE  sr_instance_code = :v_instance_code'
47210     ||' AND    process_flag     = '||G_IN_PROCESS
47211     ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||') '
47212     ||'         = '||''''||NULL_CHAR||''''
47213     ||' AND    batch_id         = :lv_batch_id';
47214 
47215 
47216          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47217 
47218       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
47219 
47220       -- Derive routing_sequence_id
47221 
47222     -- Set the error message
47223     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47224                      (p_app_short_name    => 'MSC',
47225                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
47226                       p_message_text      => lv_message_text,
47227                       p_error_text        => lv_error_text,
47228                       p_token1            => 'COLUMN_NAMES',
47229                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
47230                                              ||'ORGANIZATION_CODE,ROUTING_NAME',
47231                       p_token2            => 'MASTER_TABLE',
47232                       p_token_value2      =>  'MSC_ST_ROUTINGS',
47233                       p_token3            =>  'CHILD_TABLE' ,
47234                       p_token_value3      =>  'MSC_ST_JOB_OPERATIONS' );
47235 
47236       IF lv_return <> 0 THEN
47237         RAISE ex_logging_err;
47238       END IF;
47239 
47240     -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
47241 
47242     lv_return :=
47243     MSC_ST_UTIL.DERIVE_ROUTING_SEQUENCE_ID
47244                       (p_table_name     => 'MSC_ST_JOB_OPERATIONS',
47245                        p_rtg_col_name   => 'ROUTING_NAME',
47246                        p_rtg_col_id     => 'ROUTING_SEQUENCE_ID',
47247                        p_instance_code  => v_instance_code,
47248                        p_severity       => G_SEV_ERROR,
47249                        p_message_text   => lv_message_text,
47250                        p_batch_id       => lv_batch_id,
47251                        p_debug          => v_debug,
47252                        p_error_text     => lv_error_text);
47253 
47254     if (lv_return <> 0 )then
47255        RAISE ex_logging_err;
47256     end if;
47257 
47258 
47259 
47260       -- Set the error message
47261     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47262                      (p_app_short_name    => 'MSC',
47263                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
47264                       p_message_text      => lv_message_text,
47265                       p_error_text        => lv_error_text,
47266                       p_token1            => 'COLUMN_NAME',
47267                       p_token_value1      => 'EFFECTIVITY_DATE',
47268                       p_token2            => 'DEFAULT_VALUE',
47269                       p_token_value2      => 'SYSDATE' );
47270 
47271       IF lv_return <> 0 THEN
47272         RAISE ex_logging_err;
47273       END IF;
47274 
47275     -- Default effectivity date as SYSDATE if NULL
47276 
47277      lv_where_str :=
47278      ' AND NVL(effectivity_date,SYSDATE-36500 ) = SYSDATE-36500 ' ;
47279 
47280      lv_return := MSC_ST_UTIL.LOG_ERROR
47281                     (p_table_name        => 'MSC_ST_JOB_OPERATIONS',
47282                      p_instance_code     => v_instance_code,
47283                      p_row               => lv_column_names,
47284                      p_severity          => G_SEV_WARNING,
47285                      p_message_text      => lv_message_text,
47286                      p_error_text        => lv_error_text,
47287                      p_batch_id          => lv_batch_id,
47288                      p_where_str         => lv_where_str,
47289                      p_col_name          => 'EFFECTIVITY_DATE',
47290                      p_debug             => v_debug,
47291                      p_default_value     => 'SYSDATE');
47292 
47293       IF lv_return <> 0 THEN
47294         RAISE ex_logging_err;
47295       END IF;
47296 
47297       -- Derive operation_seq_num, operation_sequence_id
47298 
47299     v_sql_stmt := 18;
47300     lv_sql_Stmt :=
47301     'UPDATE     msc_st_job_operations mson'
47302     ||' SET     (operation_sequence_id, operation_seq_num) =  (SELECT local_id, number1'
47303     ||'         FROM msc_local_id_setup mlis'
47304     ||'         WHERE  mlis.char1 = mson.sr_instance_code'
47305     ||'         AND     NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
47306     ||'          =    NVL(mson.company_name,'||''''||NULL_CHAR||''''||') '
47307     ||'         AND    mlis.char3 = mson.organization_code'
47308     ||'         AND    mlis.char4 = mson.routing_name'
47309     ||'         AND    mlis.char5 = mson.operation_seq_code'
47310     ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
47311     ||'           =   NVL(mson.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
47312     ||'         AND    mlis.date1 = mson.EFFECTIVITY_DATE'
47313     ||'         AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
47314     ||'         AND    mlis.instance_id = '||v_instance_id||')'
47315     ||'  WHERE      sr_instance_code = :v_instance_code'
47316     ||'  AND        process_flag     ='||G_IN_PROCESS
47317     ||'  AND        batch_id         = :lv_batch_id';
47318 
47319 
47320          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47321 
47322    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
47323 
47324    -- operation_seq_num cannot be null. Error out the records with null value for them.
47325    -- Set the error message
47326 
47327     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47328                      (p_app_short_name    => 'MSC',
47329                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
47330                       p_message_text      => lv_message_text,
47331                       p_error_text        => lv_error_text,
47332                       p_token1            => 'COLUMN_NAME',
47333                       p_token_value1      => 'OPERATION_SEQUENCE_ID OR '
47334                                            ||'OPERATION_SEQ_NUM');
47335 
47336       IF lv_return <> 0 THEN
47337         RAISE ex_logging_err;
47338       END IF;
47339 
47340      -- Error out the records whose operation_sequence_id, operation_seq_num is NULL
47341 
47342     v_sql_stmt := 19;
47343     lv_sql_stmt :=
47344     'UPDATE    msc_st_job_operations'
47345     ||' SET    process_flag     ='||G_ERROR_FLG||','
47346     ||'        error_text   = '||''''||lv_message_text||''''
47347     ||' WHERE  (NVL(operation_sequence_id,'||NULL_VALUE||') ='||NULL_VALUE
47348     ||' OR     NVL(operation_seq_num,'||NULL_VALUE||') ='||NULL_VALUE||')'
47349     ||' AND    sr_instance_code  = :v_instance_code'
47350     ||' AND    process_flag      = '||G_IN_PROCESS
47351     ||' AND    batch_id          = :lv_batch_id';
47352 
47353 
47354          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47355 
47356     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
47357 
47358     -- Derive department id
47359     v_sql_stmt := 27;
47360     lv_sql_stmt :=
47361     'UPDATE     msc_st_job_operations msro'
47362     ||' SET     department_id=    (SELECT local_id'
47363     ||'         FROM msc_local_id_setup mlis'
47364     ||'         WHERE  mlis.char1 = msro.sr_instance_code'
47365     ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
47366     ||'         =    NVL(msro.company_name,'||''''||NULL_CHAR||''''||') '
47367     ||'         AND    mlis.char3 = msro.organization_code'
47368     ||'         AND    mlis.char4 = msro.department_code'
47369     ||'         AND    mlis.entity_name = ''DEPARTMENT_ID'')'
47370     ||' WHERE      sr_instance_code = :v_instance_code'
47371     ||' AND        deleted_flag     = '||SYS_NO
47372     ||' AND        process_flag     = '||G_IN_PROCESS
47373     ||' AND        batch_id         = :lv_batch_id';
47374 
47375 
47376          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47377 
47378       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
47379 
47380     -- Set the error message
47381     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47382                      (p_app_short_name    => 'MSC',
47383                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
47384                       p_message_text      => lv_message_text,
47385                       p_error_text        => lv_error_text,
47386                       p_token1            => 'COLUMN_NAMES',
47387                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
47388                                              ||'ORGANIZATION_CODE,DEPARTMENT_CODE',
47389                       p_token2            => 'MASTER_TABLE',
47390                       p_token_value2      => 'MSC_ST_DEPARTMENT_RESOURCES',
47391                       p_token3            =>  'CHILD_TABLE' ,
47392                       p_token_value3      =>  'MSC_ST_JOB_OPERATIONS' );
47393 
47394       IF lv_return <> 0 THEN
47395         RAISE ex_logging_err;
47396       END IF;
47397 
47398    -- Log an error if unable to derive department id
47399 
47400    v_sql_stmt := 28;
47401    lv_sql_stmt :=
47402    'UPDATE      msc_st_job_operations '
47403    ||' SET      process_flag ='||G_ERROR_FLG||','
47404    ||'          error_text   = '||''''||lv_message_text||''''
47405    ||' WHERE    NVL(department_id,'||NULL_VALUE||') ='||NULL_VALUE
47406    ||' AND      sr_instance_code    = :v_instance_code'
47407    ||' AND      deleted_flag        ='||SYS_NO
47408    ||' AND      process_flag        ='|| G_IN_PROCESS
47409    ||' AND      batch_id            = :lv_batch_id' ;
47410 
47411 
47412          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47413 
47414    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
47415 
47416    --Call to customised validation.
47417 
47418       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
47419         (ERRBUF         => lv_error_text,
47420          RETCODE        => lv_return,
47421          pBatchID       => lv_batch_id,
47422          pInstanceCode  => v_instance_code,
47423          pEntityName    => 'MSC_ST_JOB_OPERATIONS',
47424          pInstanceID    => v_instance_id);
47425 
47426       IF NVL(lv_return,0) <> 0 THEN
47427         RAISE ex_logging_err;
47428       END IF;
47429 
47430       -- Set the process flag as Valid and derive sr_instance_id
47431     lv_return :=
47432     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_JOB_OPERATIONS',
47433                                p_instance_id    => v_instance_id,
47434                                p_instance_code  => v_instance_code,
47435                                p_process_flag   => G_VALID,
47436                                p_error_text     => lv_error_text,
47437                                p_debug          => v_debug,
47438                                p_batch_id       => lv_batch_id);
47439 
47440       IF lv_return < 0 THEN
47441         RAISe ex_logging_err;
47442       END IF;
47443 
47444 
47445     -- At the end calling  the LOG_ERROR for logging all errored out records
47446 
47447    lv_return :=
47448    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_JOB_OPERATIONS',
47449                          p_instance_code => v_instance_code,
47450                          p_row           => lv_column_names,
47451                          p_severity      => G_SEV_ERROR,
47452                          p_message_text  => NULL,
47453                          p_error_text    => lv_error_text,
47454                          p_debug         => v_debug,
47455                          p_batch_id      => lv_batch_id  );
47456 
47457     IF lv_return < 0 THEN
47458         RAISe ex_logging_err;
47459     END IF;
47460 
47461    COMMIT;
47462  END LOOP;
47463 
47464     -- Validation for MSC_ST_JOB_REQUIREMENT_OPS
47465 
47466     --Duplicate records check for the records whose source is XML
47467 
47468     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47469                  (p_app_short_name    => 'MSC',
47470                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
47471                   p_message_text      => lv_message_text,
47472                   p_error_text        => lv_error_text);
47473 
47474     IF lv_return <> 0 THEN
47475       RAISE ex_logging_err;
47476     END IF;
47477 
47478     v_sql_stmt := 18;
47479 
47480     lv_sql_stmt :=
47481   'UPDATE   msc_st_job_requirement_ops msd1'
47482   ||' SET     process_flag  = '||G_ERROR_FLG||','
47483   ||'         error_text   = '||''''||lv_message_text||''''
47484   ||' WHERE   message_id <  (SELECT MAX(message_id)'
47485   ||'         FROM msc_st_job_requirement_ops msd2'
47486   ||'         WHERE  msd2.sr_instance_code '
47487   ||'                = msd1.sr_instance_code '
47488   ||'         AND   msd2.organization_code '
47489   ||'                = msd1.organization_code '
47490   ||'         AND   NVL(msd2.company_name,'||''''||NULL_CHAR||''''||') '
47491   ||'                 =    NVL(msd1.company_name,'||''''||NULL_CHAR||''''||') '
47492   ||'         AND msd2.wip_entity_name = msd1.wip_entity_name'
47493   ||'         AND NVL(msd2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
47494   ||'                = NVL(msd1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
47495   ||'         AND NVL(msd2.source_phantom_name,'||''''||NULL_CHAR||''''||') '
47496   ||'                = NVL(msd1.source_phantom_name,'||''''||NULL_CHAR||''''||') '
47497   ||'         AND NVL(msd2.assembly_item_name,'||''''||NULL_CHAR||''''||') '
47498   ||'                = NVL(msd1.assembly_item_name,'||''''||NULL_CHAR||''''||') '
47499   ||'         AND NVL(msd2.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
47500   ||'                   = NVL(msd1.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
47501   ||'          AND   NVL(msd2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
47502   ||'               = NVL(msd1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
47503   ||'         AND    msd2.component_name '
47504   ||'                = msd1.component_name '
47505   ||'         AND    msd2.primary_component_name '
47506   ||'                = msd1.primary_component_name '
47507   ||'          AND   msd2.effectivity_date '
47508   ||'                = msd1.effectivity_date '
47509   ||'         AND   msd2.process_flag = '||G_IN_PROCESS
47510   ||'         AND    NVL(msd2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
47511   ||' AND     msd1.process_flag ='|| G_IN_PROCESS
47512   ||' AND     msd1.sr_instance_code = :v_instance_code '
47513   ||' AND     NVL(msd1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
47514 
47515 
47516          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47517 
47518   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
47519 
47520    --Duplicate records check for the records whose source is batch load
47521 
47522     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47523                  (p_app_short_name    => 'MSC',
47524                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
47525                   p_message_text      => lv_message_text,
47526                   p_error_text        => lv_error_text);
47527 
47528     IF lv_return <> 0 THEN
47529       RAISE ex_logging_err;
47530     END IF;
47531 
47532   v_sql_stmt := 02;
47533 
47534   lv_sql_stmt :=
47535   'UPDATE msc_st_job_requirement_ops  msd1'
47536   ||' SET     process_flag  = '||G_ERROR_FLG||','
47537   ||'         error_text   = '||''''||lv_message_text||''''
47538   ||' WHERE   EXISTS( SELECT 1 '
47539   ||'         FROM msc_st_job_requirement_ops msd2'
47540   ||'         WHERE  msd2.sr_instance_code '
47541   ||'                = msd1.sr_instance_code '
47542   ||'          AND   msd2.organization_code '
47543   ||'                = msd1.organization_code '
47544   ||'          AND   NVL(msd2.company_name,'||''''||NULL_CHAR||''''||') '
47545   ||'                 =    NVL(msd1.company_name,'||''''||NULL_CHAR||''''||') '
47546   ||'          AND   msd2.wip_entity_name = msd1.wip_entity_name'
47547   ||'          AND   NVL(msd2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
47548   ||'                = NVL(msd1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
47549   ||'         AND NVL(msd2.source_phantom_name,'||''''||NULL_CHAR||''''||') '
47550   ||'                = NVL(msd1.source_phantom_name,'||''''||NULL_CHAR||''''||') '
47551   ||'         AND NVL(msd2.assembly_item_name,'||''''||NULL_CHAR||''''||') '
47552   ||'                = NVL(msd1.assembly_item_name,'||''''||NULL_CHAR||''''||') '
47553   ||'         AND NVL(msd2.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
47554   ||'                   = NVL(msd1.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
47555   ||'         AND NVL(msd2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
47556   ||'               = NVL(msd1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
47557   ||'          AND   msd2.component_name '
47558   ||'                = msd1.component_name '
47559   ||'          AND   msd2.primary_component_name '
47560   ||'                = msd1.primary_component_name '
47561   ||'          AND   msd2.effectivity_date '
47562   ||'                = msd1.effectivity_date '
47563   ||'          AND   msd2.process_flag = '||G_IN_PROCESS
47564   ||'          AND NVL(msd2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
47565   ||'       GROUP BY  sr_instance_code,organization_code,wip_entity_name,'
47566   ||'       company_name,operation_seq_code,component_name,primary_component_name,source_phantom_name,effectivity_date'
47567   ||'       HAVING COUNT(*) > 1)'
47568   ||' AND   msd1.process_flag  = '||G_IN_PROCESS
47569   ||' AND   msd1.sr_instance_code = :v_instance_code'
47570   ||' AND   NVL(msd1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
47571 
47572 
47573          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47574 
47575   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
47576 
47577   lv_column_names :=
47578   'COMPONENT_NAME                     ||''~''||'
47579   ||' ORGANIZATION_CODE               ||''~''||'
47580   ||' WIP_ENTITY_NAME                 ||''~''||'
47581   ||' SR_INSTANCE_CODE                ||''~''||'
47582   ||' ASSEMBLY_ITEM_NAME              ||''~''||'
47583   ||' OPERATION_SEQ_CODE              ||''~''||'
47584   ||' FROM_END_ITEM_UNIT_NUMBER       ||''~''||'
47585   ||' TO_END_ITEM_UNIT_NUMBER         ||''~''||'
47586   ||' WIP_SUPPLY_TYPE                 ||''~''||'
47587   ||' DELETED_FLAG                    ||''~''||'
47588   ||' COMPANY_NAME                    ||''~''||'
47589   ||' RECO_DATE_REQUIRED              ||''~''||'
47590   ||' RECOMMENDED                     ||''~''||'
47591   ||' EFFECTIVITY_DATE                ||''~''||'
47592   ||' DISABLE_DATE                    ||''~''||'
47593   ||' PRIMARY_COMPONENT_NAME' ;
47594 
47595   LOOP
47596       v_sql_stmt := 03;
47597       SELECT       msc_st_batch_id_s.NEXTVAL
47598       INTO         lv_batch_id
47599       FROM         DUAL;
47600 
47601       v_sql_stmt := 04;
47602       lv_sql_stmt :=
47603       ' UPDATE    msc_st_job_requirement_ops '
47604       ||' SET     batch_id  = :lv_batch_id'
47605       ||' WHERE   process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
47606       ||' AND     sr_instance_code               = :v_instance_code'
47607       ||' AND     NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
47608       ||' AND     rownum                        <= '||v_batch_size;
47609 
47610 
47611 
47612         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47613 
47614       EXECUTE IMMEDIATE lv_sql_stmt
47615               USING     lv_batch_id,
47616                         v_instance_code;
47617 
47618       EXIT WHEN SQL%NOTFOUND ;
47619 
47620       OPEN c4 (lv_batch_id);
47621     FETCH c4 BULK COLLECT INTO lb_rowid;
47622     CLOSE c4;
47623 
47624   v_sql_stmt := 03;
47625     FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
47626     UPDATE msc_st_job_requirement_ops
47627     SET    st_transaction_id   = msc_st_job_requirement_ops_s.NEXTVAL,
47628            refresh_id          = v_refresh_id,
47629            last_update_date    = v_current_date,
47630            last_updated_by     = v_current_user,
47631            creation_date       = v_current_date,
47632            created_by          = v_current_user
47633     WHERE  rowid               = lb_rowid(j);
47634 
47635      -- Set the error message
47636     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47637                      (p_app_short_name    => 'MSC',
47638                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
47639                       p_message_text      => lv_message_text,
47640                       p_error_text        => lv_error_text,
47641                       p_token1            => 'COLUMN_NAME',
47642                       p_token_value1      => 'DELETED_FLAG',
47643                       p_token2            => 'DEFAULT_VALUE',
47644                       p_token_value2      =>  SYS_NO  );
47645 
47646       IF lv_return <> 0 THEN
47647         RAISE ex_logging_err;
47648       END IF;
47649     --Log a warning for those records where the deleted_flag has a value other
47650     --SYS_NO
47651 
47652     lv_where_str :=
47653      ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
47654 
47655     lv_return := MSC_ST_UTIL.LOG_ERROR
47656                    (p_table_name        => 'MSC_ST_JOB_REQUIREMENT_OPS',
47657                     p_instance_code     => v_instance_code,
47658                     p_row               => lv_column_names,
47659                     p_severity          => G_SEV_WARNING,
47660                     p_message_text      => lv_message_text,
47661                     p_error_text        => lv_error_text,
47662                     p_batch_id          => lv_batch_id,
47663                     p_where_str         => lv_where_str,
47664                     p_col_name          => 'DELETED_FLAG',
47665                     p_debug             => v_debug,
47666                     p_default_value     => SYS_NO);
47667 
47668     IF lv_return <> 0 THEN
47669         RAISE ex_logging_err;
47670     END IF;
47671 
47672     -- Set the  message
47673      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47674                      (p_app_short_name    => 'MSC',
47675                       p_error_code        => 'MSC_PP_INVALID_VALUE',
47676                       p_message_text      => lv_message_text,
47677                       p_error_text        => lv_error_text,
47678                       p_token1            => 'COLUMN_NAME',
47679                       p_token_value1      => 'ORGANIZATION_CODE');
47680 
47681       IF lv_return <> 0 THEN
47682         RAISE ex_logging_err;
47683       END IF;
47684 
47685      --Derive Organization_id
47686     lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
47687                    (p_table_name       => 'MSC_ST_JOB_REQUIREMENT_OPS',
47688                     p_org_partner_name => 'ORGANIZATION_CODE',
47689                     p_org_partner_id   => 'ORGANIZATION_ID',
47690                     p_instance_code    => v_instance_code,
47691                     p_partner_type     => G_ORGANIZATION,
47692                     p_error_text       => lv_error_text,
47693                     p_batch_id         => lv_batch_id,
47694                     p_severity         => G_SEV_ERROR,
47695                     p_message_text     => lv_message_text,
47696                     p_debug            => v_debug,
47697                     p_row              => lv_column_names);
47698 
47699     IF lv_return <> 0 THEN
47700         RAISE ex_logging_err;
47701     END IF;
47702 
47703     --Deriving wip_entity_id
47704       v_sql_stmt := 10;
47705       lv_sql_stmt :=
47706       'UPDATE msc_st_job_requirement_ops mss'
47707       ||' SET wip_entity_id     = (SELECT local_id'
47708       ||'       FROM   msc_local_id_supply mls'
47709       ||'       WHERE  mls.char4 = mss.wip_entity_name'
47710       ||'       AND    mls.char3 = mss.organization_code'
47711       ||'       AND    NVL(mls.char2,       '||''''||NULL_CHAR||''''||') = '
47712       ||'              NVL(mss.company_name,'||''''||NULL_CHAR||''''||') '
47713       ||'       AND    mls.char1 = mss.sr_instance_code'
47714       ||'       AND    mls.entity_name = ''WIP_ENTITY_ID'' )'
47715       ||' WHERE  process_flag                   = '||G_IN_PROCESS
47716       ||' AND    NVL(batch_id,'||NULL_VALUE||') = :lv_batch_id'
47717       ||' AND    sr_instance_code               = :v_instance_code';
47718 
47719 
47720         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47721 
47722       EXECUTE IMMEDIATE lv_sql_stmt
47723               USING     lv_batch_id,
47724                         v_instance_code;
47725 
47726     -- Set the error message
47727 
47728     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47729                      (p_app_short_name    => 'MSC',
47730                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
47731                       p_message_text      => lv_message_text,
47732                       p_error_text        => lv_error_text,
47733                       p_token1            => 'COLUMN_NAMES',
47734                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
47735                                              ||'ORGANIZATION_CODE,WIP_ENTITY_NAME',
47736                       p_token2            => 'MASTER_TABLE',
47737                       p_token_value2      =>  'MSC_ST_SUPPLIES',
47738                       p_token3            =>  'CHILD_TABLE' ,
47739                       p_token_value3      =>  'MSC_ST_JOB_REQUIREMENT_OPS' );
47740 
47741       IF lv_return <> 0 THEN
47742         RAISE ex_logging_err;
47743       END IF;
47744 
47745 
47746     -- Error out the records if WIP_ENTITY_ID NULL
47747 
47748     lv_sql_stmt:=
47749     'UPDATE      msc_st_job_requirement_ops msrr'
47750     ||' SET      process_flag              ='||G_ERROR_FLG||','
47751     ||'          error_text   = '||''''||lv_message_text||''''
47752     ||' WHERE    NVL(wip_entity_id,'||NULL_VALUE||') = '||NULL_VALUE
47753     ||' AND      sr_instance_code            = :v_instance_code'
47754     ||' AND      process_flag                ='||G_IN_PROCESS
47755     ||' AND      batch_id                    = :lv_batch_id';
47756 
47757 
47758             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47759 
47760     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
47761 
47762    -- Set the error message
47763     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47764                      (p_app_short_name    => 'MSC',
47765                       p_error_code        => 'MSC_PP_INVALID_VALUE',
47766                       p_message_text      => lv_message_text,
47767                       p_error_text        => lv_error_text,
47768                       p_token1            => 'COLUMN_NAME',
47769                       p_token_value1      => 'RECOMMENDED');
47770 
47771       IF lv_return <> 0 THEN
47772         RAISE ex_logging_err;
47773       END IF;
47774 
47775      -- Error out the records where RECOMMENDED is not 'Y' or 'N'
47776 
47777      v_sql_stmt := 7;
47778     lv_sql_stmt :=
47779     'UPDATE    msc_st_job_requirement_ops'
47780     ||' SET    process_flag     ='||G_ERROR_FLG||','
47781     ||'         error_text   = '||''''||lv_message_text||''''
47782     ||' WHERE  recommended NOT IN (''Y'', ''N'',''y'',''n'')'
47783     ||' AND    sr_instance_code  = :v_instance_code'
47784     ||' AND    process_flag      = '||G_IN_PROCESS
47785     ||' AND    batch_id          = :lv_batch_id';
47786 
47787 
47788          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47789 
47790    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
47791 
47792     -- Set the  message
47793      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47794                      (p_app_short_name    => 'MSC',
47795                       p_error_code        => 'MSC_PP_INVALID_VALUE',
47796                       p_message_text      => lv_message_text,
47797                       p_error_text        => lv_error_text,
47798                       p_token1            => 'COLUMN_NAME',
47799                       p_token_value1      => 'COMPONENT_NAME');
47800 
47801       IF lv_return <> 0 THEN
47802         RAISE ex_logging_err;
47803       END IF;
47804 
47805     --Derive component_item_id
47806     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
47807                    (p_table_name       => 'MSC_ST_JOB_REQUIREMENT_OPS',
47808                     p_item_col_name    => 'COMPONENT_NAME',
47809                     p_item_col_id      => 'COMPONENT_ITEM_ID',
47810                     p_instance_id      => v_instance_id,
47811                     p_instance_code    => v_instance_code,
47812                     p_message_text     => lv_message_text,
47813                     p_error_text       => lv_error_text,
47814                     p_batch_id         => lv_batch_id,
47815                     p_severity         => G_SEV_ERROR,
47816                     p_debug            => v_debug,
47817                     p_row              => lv_column_names);
47818 
47819     IF lv_return <> 0 THEN
47820         RAISE ex_logging_err;
47821     END IF;
47822 
47823     -- Set the  message
47824      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47825                      (p_app_short_name    => 'MSC',
47826                       p_error_code        => 'MSC_PP_INVALID_VALUE',
47827                       p_message_text      => lv_message_text,
47828                       p_error_text        => lv_error_text,
47829                       p_token1            => 'COLUMN_NAME',
47830                       p_token_value1      => 'PRIMARY_COMPONENT_NAME');
47831 
47832       IF lv_return <> 0 THEN
47833         RAISE ex_logging_err;
47834       END IF;
47835 
47836     --Derive primary_component_id
47837     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
47838                    (p_table_name       => 'MSC_ST_JOB_REQUIREMENT_OPS',
47839                     p_item_col_name    => 'PRIMARY_COMPONENT_NAME',
47840                     p_item_col_id      => 'PRIMARY_COMPONENT_ID',
47841                     p_instance_id      => v_instance_id,
47842                     p_instance_code    => v_instance_code,
47843                     p_message_text     => lv_message_text,
47844                     p_error_text       => lv_error_text,
47845                     p_batch_id         => lv_batch_id,
47846                     p_severity         => G_SEV_ERROR,
47847                     p_debug            => v_debug,
47848                     p_row              => lv_column_names);
47849 
47850     IF lv_return <> 0 THEN
47851         RAISE ex_logging_err;
47852     END IF;
47853 
47854     -- Set the  message
47855      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47856                      (p_app_short_name    => 'MSC',
47857                       p_error_code        => 'MSC_PP_INVALID_VALUE',
47858                       p_message_text      => lv_message_text,
47859                       p_error_text        => lv_error_text,
47860                       p_token1            => 'COLUMN_NAME',
47861                       p_token_value1      => 'SOURCE_PHANTOM_NAME');
47862 
47863       IF lv_return <> 0 THEN
47864         RAISE ex_logging_err;
47865       END IF;
47866 
47867     --Derive primary_component_id
47868     lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
47869                    (p_table_name       => 'MSC_ST_JOB_REQUIREMENT_OPS',
47870                     p_item_col_name    => 'SOURCE_PHANTOM_NAME',
47871                     p_item_col_id      => 'SOURCE_PHANTOM_ID',
47872                     p_instance_id      => v_instance_id,
47873                     p_instance_code    => v_instance_code,
47874                     p_message_text     => lv_message_text,
47875                     p_error_text       => lv_error_text,
47876                     p_batch_id         => lv_batch_id,
47877                     p_severity         => G_SEV3_ERROR,
47878                     p_debug            => v_debug,
47879                     p_row              => lv_column_names);
47880 
47881     IF lv_return <> 0 THEN
47882         RAISE ex_logging_err;
47883     END IF;
47884 
47885     -- Set the  message
47886    lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47887                      (p_app_short_name    => 'MSC',
47888                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
47889                       p_message_text      => lv_message_text,
47890                       p_error_text        => lv_error_text,
47891                       p_token1            => 'COLUMN_NAME',
47892                       p_token_value1      => 'QUANTITY_PER_ASSEMBLY');
47893 
47894 
47895       IF lv_return <> 0 THEN
47896         RAISE ex_logging_err;
47897       END IF;
47898 
47899 
47900   -- Error out records where QUANTITY_PER_ASSEMBLY is NULL;
47901 
47902   v_sql_stmt := 06;
47903   lv_sql_stmt :=
47904   'UPDATE     msc_st_job_requirement_ops '
47905   ||' SET     process_flag    =  '||G_ERROR_FLG||','
47906   ||'         error_text   = '||''''||lv_message_text||''''
47907   ||' WHERE NVL(quantity_per_assembly,'||NULL_VALUE||')='||NULL_VALUE
47908   ||' AND    process_flag      = '||G_IN_PROCESS
47909   ||' AND    deleted_flag      = '||SYS_NO
47910   ||' AND    batch_id          = :lv_batch_id'
47911   ||' AND    sr_instance_code  = :v_instance_code';
47912 
47913 
47914       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
47915 
47916   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
47917 
47918  -- Set the error message
47919     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47920                      (p_app_short_name    => 'MSC',
47921                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
47922                       p_message_text      => lv_message_text,
47923                       p_error_text        => lv_error_text,
47924                       p_token1            => 'COLUMN_NAME',
47925                       p_token_value1      => 'EFFECTIVITY_DATE',
47926                       p_token2            => 'DEFAULT_VALUE',
47927                       p_token_value2      => 'SYSDATE' );
47928 
47929       IF lv_return <> 0 THEN
47930         RAISE ex_logging_err;
47931       END IF;
47932 
47933     -- Default effectivity date as SYSDATE if NULL
47934 
47935      lv_where_str :=
47936      ' AND NVL(effectivity_date,SYSDATE-36500 ) = SYSDATE-36500 ' ;
47937 
47938      lv_return := MSC_ST_UTIL.LOG_ERROR
47939                     (p_table_name        => 'MSC_ST_JOB_REQUIREMENT_OPS',
47940                      p_instance_code     => v_instance_code,
47941                      p_row               => lv_column_names,
47942                      p_severity          => G_SEV_WARNING,
47943                      p_message_text      => lv_message_text,
47944                      p_error_text        => lv_error_text,
47945                      p_batch_id          => lv_batch_id,
47946                      p_where_str         => lv_where_str,
47947                      p_col_name          => 'EFFECTIVITY_DATE',
47948                      p_debug             => v_debug,
47949                      p_default_value     => 'SYSDATE');
47950 
47951       IF lv_return <> 0 THEN
47952         RAISE ex_logging_err;
47953       END IF;
47954 
47955     -- Set the error message
47956     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47957                      (p_app_short_name    => 'MSC',
47958                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
47959                       p_message_text      => lv_message_text,
47960                       p_error_text        => lv_error_text,
47961                       p_token1            => 'COLUMN_NAME',
47962                       p_token_value1      => 'COMPONENT_YIELD_FACTOR',
47963                       p_token2            => 'DEFAULT_VALUE',
47964                       p_token_value2      => 1 );
47965 
47966       IF lv_return <> 0 THEN
47967         RAISE ex_logging_err;
47968       END IF;
47969 
47970     -- Default component_yield_factor if NULL
47971 
47972      lv_where_str :=
47973      ' AND NVL(COMPONENT_YIELD_FACTOR,'||NULL_VALUE||') = '||NULL_VALUE ;
47974 
47975      lv_return := MSC_ST_UTIL.LOG_ERROR
47976                     (p_table_name        => 'MSC_ST_JOB_REQUIREMENT_OPS',
47977                      p_instance_code     => v_instance_code,
47978                      p_row               => lv_column_names,
47979                      p_severity          => G_SEV_WARNING,
47980                      p_message_text      => lv_message_text,
47981                      p_error_text        => lv_error_text,
47982                      p_batch_id          => lv_batch_id,
47983                      p_where_str         => lv_where_str,
47984                      p_col_name          => 'COMPONENT_YIELD_FACTOR',
47985                      p_debug             => v_debug,
47986                      p_default_value     => 1);
47987 
47988       IF lv_return <> 0 THEN
47989         RAISE ex_logging_err;
47990       END IF;
47991 
47992      -- Set the error message
47993     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
47994                      (p_app_short_name    => 'MSC',
47995                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
47996                       p_message_text      => lv_message_text,
47997                       p_error_text        => lv_error_text,
47998                       p_token1            => 'COLUMN_NAME',
47999                       p_token_value1      => 'PLANNING_FACTOR',
48000                       p_token2            => 'DEFAULT_VALUE',
48001                       p_token_value2      => 100 );
48002 
48003       IF lv_return <> 0 THEN
48004         RAISE ex_logging_err;
48005       END IF;
48006 
48007     -- Default planning_factor if NULL
48008 
48009      lv_where_str :=
48010      ' AND NVL(PLANNING_FACTOR,'||NULL_VALUE||') = '||NULL_VALUE ;
48011 
48012      lv_return := MSC_ST_UTIL.LOG_ERROR
48013                     (p_table_name        => 'MSC_ST_JOB_REQUIREMENT_OPS',
48014                      p_instance_code     => v_instance_code,
48015                      p_row               => lv_column_names,
48016                      p_severity          => G_SEV_WARNING,
48017                      p_message_text      => lv_message_text,
48018                      p_error_text        => lv_error_text,
48019                      p_batch_id          => lv_batch_id,
48020                      p_where_str         => lv_where_str,
48021                      p_col_name          => 'PLANNING_FACTOR',
48022                      p_debug             => v_debug,
48023                      p_default_value     => 100);
48024 
48025       IF lv_return <> 0 THEN
48026         RAISE ex_logging_err;
48027       END IF;
48028 
48029        -- Set the error message
48030     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48031                      (p_app_short_name    => 'MSC',
48032                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
48033                       p_message_text      => lv_message_text,
48034                       p_error_text        => lv_error_text,
48035                       p_token1            => 'COLUMN_NAME',
48036                       p_token_value1      => 'WIP_SUPPLY_TYPE',
48037                       p_token2            => 'DEFAULT_VALUE',
48038                       p_token_value2      =>  G_WIP_SUPPLY_TYPE );
48039 
48040       IF lv_return <> 0 THEN
48041         RAISE ex_logging_err;
48042       END IF;
48043 
48044     --  Default wip_supply_type as 1
48045 
48046     lv_where_str := ' AND NVL(wip_supply_type,'||NULL_VALUE||') NOT IN (1,2,3,4,5,6,7)'
48047                     ||' AND deleted_flag ='||SYS_NO ;
48048 
48049     lv_return := MSC_ST_UTIL.LOG_ERROR
48050                    (p_table_name        => 'MSC_ST_JOB_REQUIREMENT_OPS',
48051                     p_instance_code     => v_instance_code,
48052                     p_row               => lv_column_names,
48053                     p_severity          => G_SEV_WARNING,
48054                     p_message_text      => lv_message_text,
48055                     p_error_text        => lv_error_text,
48056                     p_batch_id          => lv_batch_id,
48057                     p_where_str         => lv_where_str,
48058                     p_col_name          => 'WIP_SUPPLY_TYPE',
48059                     p_debug             => v_debug,
48060                     p_default_value     => G_WIP_SUPPLY_TYPE );
48061 
48062     IF lv_return <> 0 THEN
48063         RAISE ex_logging_err;
48064     END IF;
48065 
48066    -- If routing_name is is NULL populate the assembly_item_name in routing name
48067 
48068   v_sql_stmt := 11;
48069   lv_sql_stmt :=
48070   ' UPDATE   msc_st_job_requirement_ops'
48071   ||' SET    routing_name            =  assembly_item_name'
48072   ||' WHERE  process_flag            = '||G_IN_PROCESS
48073   ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||')'
48074   ||'       = '||''''||NULL_CHAR||''''
48075   ||' AND   process_flag    = '||G_IN_PROCESS
48076   ||' AND   batch_id        = :lv_batch_id'
48077   ||' AND   sr_instance_code = :v_instance_code';
48078 
48079 
48080       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48081 
48082   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
48083 
48084    -- Derive routing_sequence_id
48085 
48086     -- Set the error message
48087     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48088                      (p_app_short_name    => 'MSC',
48089                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
48090                       p_message_text      => lv_message_text,
48091                       p_error_text        => lv_error_text,
48092                       p_token1            => 'COLUMN_NAMES',
48093                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
48094                                              ||'ORGANIZATION_CODE,ROUTING_NAME',
48095                       p_token2            => 'MASTER_TABLE',
48096                       p_token_value2      =>  'MSC_ST_ROUTINGS',
48097                       p_token3            =>  'CHILD_TABLE' ,
48098                       p_token_value3      =>  'MSC_ST_JOB_REQUIREMENT_OPS' );
48099 
48100       IF lv_return <> 0 THEN
48101         RAISE ex_logging_err;
48102       END IF;
48103 
48104     -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
48105 
48106     lv_return :=
48107     MSC_ST_UTIL.DERIVE_ROUTING_SEQUENCE_ID
48108                       (p_table_name     => 'MSC_ST_JOB_REQUIREMENT_OPS',
48109                        p_rtg_col_name   => 'ROUTING_NAME',
48110                        p_rtg_col_id     => 'ROUTING_SEQUENCE_ID',
48111                        p_instance_code  => v_instance_code,
48112                        p_severity       => G_SEV_ERROR,
48113                        p_message_text   => lv_message_text,
48114                        p_batch_id       => lv_batch_id,
48115                        p_debug          => v_debug,
48116                        p_error_text     => lv_error_text);
48117 
48118     if (lv_return <> 0 )then
48119        RAISE ex_logging_err;
48120     end if;
48121 
48122 
48123   -- Derive operation seq num from local id table
48124 
48125     v_sql_stmt := 12;
48126     lv_sql_stmt:=
48127     'UPDATE     msc_st_job_requirement_ops msd'
48128     ||' SET     operation_seq_num=    (SELECT number1'
48129     ||'                    FROM msc_local_id_setup mlis'
48130     ||'                    WHERE  mlis.char1 = msd.sr_instance_code'
48131     ||'                    AND NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
48132     ||'                    =   NVL(msd.company_name,'||''''||NULL_CHAR||''''||') '
48133     ||'                    AND    mlis.char3 = msd.organization_code'
48134     ||'                    AND    mlis.char4 = msd.routing_name'
48135     ||'                    AND    mlis.char5 = msd.operation_seq_code'
48136     ||'                    AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
48137     ||'                          = NVL(msd.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
48138     ||'                    AND    mlis.date1 = msd.op_effectivity_date'
48139     ||'                    AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'') '
48140     ||' WHERE      sr_instance_code = :v_instance_code'
48141     ||' AND        process_flag     = '||G_IN_PROCESS
48142     ||' AND        batch_id         = :lv_batch_id';
48143 
48144 
48145 
48146          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48147 
48148     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48149 
48150     -- Set the error message
48151 
48152     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48153                      (p_app_short_name    => 'MSC',
48154                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
48155                       p_message_text      => lv_message_text,
48156                       p_error_text        => lv_error_text,
48157                       p_token1            => 'COLUMN_NAME',
48158                       p_token_value1      => 'OPERATION_SEQ_NUM');
48159 
48160 
48161       IF lv_return <> 0 THEN
48162         RAISE ex_logging_err;
48163       END IF;
48164 
48165      -- Error out the records whose operation_seq_num is NULL
48166 
48167     v_sql_stmt := 19;
48168     lv_sql_stmt :=
48169     'UPDATE    msc_st_job_requirement_ops'
48170     ||' SET    process_flag     ='||G_ERROR_FLG||','
48171     ||'        error_text   = '||''''||lv_message_text||''''
48172     ||' WHERE  NVL(operation_seq_num,'||NULL_VALUE||') ='||NULL_VALUE
48173     ||' AND    sr_instance_code  = :v_instance_code'
48174     ||' AND    process_flag      = '||G_IN_PROCESS
48175     ||' AND    batch_id          = :lv_batch_id';
48176 
48177 
48178          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48179 
48180     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48181 
48182     -- Derive the component_sequence_id
48183 
48184      lv_sql_stmt :=
48185      'UPDATE     msc_st_job_requirement_ops mjro'
48186      ||' SET     component_sequence_id=  (SELECT local_id '
48187      ||'         FROM msc_local_id_setup mlis'
48188      ||'         WHERE  mlis.char1 = mjro.sr_instance_code'
48189      ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
48190      ||'               =  NVL(mjro.company_name,'||''''||NULL_CHAR||''''||') '
48191      ||'         AND    mlis.char3 = mjro.organization_code'
48192      ||'         AND    mlis.char4 = mjro.assembly_item_name'
48193      ||'         AND    mlis.char5 = mjro.component_name'
48194      ||'         AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
48195      ||'               =  NVL(mjro.alternate_bom_designator,'||''''||NULL_CHAR||''''||') '
48196      ||'         AND    mlis.char7 = mjro.operation_seq_code'
48197      ||'         AND    mlis.date1 = mjro.effectivity_date'
48198      ||'         AND    mlis.entity_name = ''COMPONENT_SEQUENCE_ID'')'
48199      ||'   WHERE      mjro.sr_instance_code = :v_instance_code'
48200      ||'   AND        mjro.process_flag     ='|| G_IN_PROCESS
48201      ||'   AND        mjro.batch_id         = :lv_batch_id';
48202 
48203 
48204             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48205 
48206     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48207 
48208     -- Set the error message
48209 
48210     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48211                      (p_app_short_name    => 'MSC',
48212                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
48213                       p_message_text      => lv_message_text,
48214                       p_error_text        => lv_error_text,
48215                       p_token1            => 'COLUMN_NAME',
48216                       p_token_value1      => 'COMPONENT_SEQUENCE_ID');
48217 
48218 
48219       IF lv_return <> 0 THEN
48220         RAISE ex_logging_err;
48221       END IF;
48222 
48223      -- Error out the records where component_sequence_id is NULL
48224 
48225 
48226     lv_sql_stmt :=
48227     'UPDATE    msc_st_job_requirement_ops'
48228     ||' SET    process_flag     ='||G_ERROR_FLG||','
48229     ||'        error_text   = '||''''||lv_message_text||''''
48230     ||' WHERE  NVL(component_sequence_id,'||NULL_VALUE||') ='||NULL_VALUE
48231     ||' AND    sr_instance_code  = :v_instance_code'
48232     ||' AND    process_flag      = '||G_IN_PROCESS
48233     ||' AND    batch_id          = :lv_batch_id';
48234 
48235          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48236 
48237     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48238 
48239     -- Derive department id
48240 
48241     lv_sql_stmt :=
48242     'UPDATE     msc_st_job_requirement_ops mjro'
48243     ||' SET     department_id=    (SELECT local_id'
48244     ||'         FROM msc_local_id_setup mlis'
48245     ||'         WHERE  mlis.char1 = mjro.sr_instance_code'
48246     ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
48247     ||'         =    NVL(mjro.company_name,'||''''||NULL_CHAR||''''||') '
48248     ||'         AND    mlis.char3 = mjro.organization_code'
48249     ||'         AND    mlis.char4 = mjro.department_code'
48250     ||'         AND    mlis.entity_name = ''DEPARTMENT_ID'')'
48251     ||' WHERE      sr_instance_code = :v_instance_code'
48252     ||' AND        deleted_flag     = '||SYS_NO
48253     ||' AND        process_flag     = '||G_IN_PROCESS
48254     ||' AND        batch_id         = :lv_batch_id';
48255 
48256 
48257          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48258 
48259       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48260 
48261 
48262 
48263     --Call to customised validation.
48264     MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
48265       (ERRBUF         => lv_error_text,
48266        RETCODE        => lv_return,
48267        pBatchID       => lv_batch_id,
48268        pInstanceCode  => v_instance_code,
48269        pEntityName    => 'MSC_ST_JOB_REQUIREMENT_OPS',
48270        pInstanceID    => v_instance_id);
48271 
48272     IF NVL(lv_return,0) <> 0 THEN
48273       RAISE ex_logging_err;
48274     END IF;
48275 
48276     lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
48277                   (p_table_name     => 'MSC_ST_JOB_REQUIREMENT_OPS',
48278                    p_instance_id    => v_instance_id,
48279                    p_instance_code  => v_instance_code,
48280                    p_process_flag   => G_VALID,
48281                    p_error_text     => lv_error_text,
48282                    p_debug          => v_debug,
48283                    p_batch_id       => lv_batch_id);
48284 
48285     IF lv_return <> 0 THEN
48286       RAISE ex_logging_err;
48287     END IF;
48288 
48289     lv_return := MSC_ST_UTIL.LOG_ERROR
48290                    (p_table_name        => 'MSC_ST_JOB_REQUIREMENT_OPS',
48291                     p_instance_code     => v_instance_code,
48292                     p_row               => lv_column_names,
48293                     p_severity          => G_SEV_ERROR,
48294                     p_message_text      => NULL,
48295                     p_error_text        => lv_error_text,
48296                     p_debug             => v_debug,
48297                     p_batch_id          => lv_batch_id);
48298 
48299     IF lv_return <> 0 THEN
48300       RAISE ex_logging_err;
48301     END IF;
48302 
48303     COMMIT;
48304    END LOOP ;
48305 
48306 
48307    -- Validation for MSC_ST_JOB_OP_RESOURCES
48308 
48309     --Duplicate records check for the records whose source is XML
48310 
48311     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48312                  (p_app_short_name    => 'MSC',
48313                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
48314                   p_message_text      => lv_message_text,
48315                   p_error_text        => lv_error_text);
48316 
48317     IF lv_return <> 0 THEN
48318       RAISE ex_logging_err;
48319     END IF;
48320 
48321     v_sql_stmt := 01;
48322 
48323   lv_sql_stmt:=
48324    'UPDATE    msc_st_job_op_resources msrr1 '
48325    ||' SET       process_flag ='||G_ERROR_FLG||','
48326    ||'           error_text   = '||''''||lv_message_text||''''
48327    ||' WHERE     message_id < ( SELECT max(message_id) FROM msc_st_job_op_resources msrr2'
48328    ||'           WHERE msrr2.sr_instance_code  '
48329    ||'                 = msrr1.sr_instance_code'
48330    ||'         AND   NVL(msrr2.company_name,'||''''||NULL_CHAR||''''||') '
48331    ||'                =    NVL(msrr1.company_name,'||''''||NULL_CHAR||''''||') '
48332    ||'         AND   msrr2.wip_entity_name    = msrr1.wip_entity_name'
48333    ||'         AND   msrr2.organization_code  = msrr1.organization_code'
48334    ||'         AND   NVL(msrr2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
48335    ||'                  = NVL(msrr1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
48336    ||'         AND   NVL(msrr2.resource_seq_code,'||''''||NULL_CHAR||''''||') '
48337    ||'                  = NVL(msrr1.resource_seq_code,'||''''||NULL_CHAR||''''||') '
48338    ||'         AND   NVL(msrr2.resource_code,  '||''''||NULL_CHAR||''''||') '
48339    ||'                  = NVL(msrr1.resource_code, '||''''||NULL_CHAR||''''||') '
48340    ||'         AND   NVL(msrr2.routing_name,'||''''||NULL_CHAR||''''||') '
48341    ||'               = NVL(msrr1.routing_name,'||''''||NULL_CHAR||''''||') '
48342    ||'         AND   NVL(msrr2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
48343    ||'               = NVL(msrr1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
48344    ||'         AND   msrr2.process_flag       = '||G_IN_PROCESS
48345    ||'         AND    NVL(msrr2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
48346    ||' AND     msrr1.process_flag     = '|| G_IN_PROCESS
48347    ||' AND     msrr1.sr_instance_code = :v_instance_code '
48348    ||' AND     NVL(msrr1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
48349 
48350 
48351          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48352 
48353   EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
48354 
48355   --Duplicate records check for the records whose source is batch load
48356 
48357   lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48358                  (p_app_short_name    => 'MSC',
48359                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
48360                   p_message_text      => lv_message_text,
48361                   p_error_text        => lv_error_text);
48362 
48363     IF lv_return <> 0 THEN
48364       RAISE ex_logging_err;
48365     END IF;
48366   v_sql_stmt := 02;
48367 
48368    lv_sql_stmt:=
48369    'UPDATE      msc_st_job_op_resources msrr1'
48370    ||' SET      process_flag = '||G_ERROR_FLG||','
48371    ||'          error_text   = '||''''||lv_message_text||''''
48372    ||' WHERE    EXISTS ( SELECT 1 '
48373    ||'          FROM msc_st_job_op_resources msrr2'
48374    ||'          WHERE msrr2.sr_instance_code  '
48375    ||'                 = msrr1.sr_instance_code'
48376    ||'         AND   NVL(msrr2.company_name,'||''''||NULL_CHAR||''''||') '
48377    ||'                =    NVL(msrr1.company_name,'||''''||NULL_CHAR||''''||') '
48378    ||'         AND   msrr2.wip_entity_name    = msrr1.wip_entity_name'
48379    ||'         AND   msrr2.organization_code  = msrr1.organization_code'
48380    ||'         AND   NVL(msrr2.operation_seq_code,'||''''||NULL_CHAR||''''||') '
48381    ||'                  = NVL(msrr1.operation_seq_code,'||''''||NULL_CHAR||''''||') '
48382    ||'         AND   NVL(msrr2.resource_seq_code,'||''''||NULL_CHAR||''''||') '
48383    ||'                  = NVL(msrr1.resource_seq_code,'||''''||NULL_CHAR||''''||') '
48384    ||'         AND   NVL(msrr2.resource_code,  '||''''||NULL_CHAR||''''||') '
48385    ||'                  = NVL(msrr1.resource_code, '||''''||NULL_CHAR||''''||') '
48386    ||'         AND   NVL(msrr2.routing_name,'||''''||NULL_CHAR||''''||') '
48387    ||'               = NVL(msrr1.routing_name,'||''''||NULL_CHAR||''''||') '
48388    ||'         AND   NVL(msrr2.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
48389    ||'               = NVL(msrr1.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
48390    ||'         AND   msrr2.process_flag       = '||G_IN_PROCESS
48391    ||'         AND   NVL(msrr2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
48392    ||'       GROUP BY sr_instance_code,company_name,organization_code,wip_entity_name,'
48393    ||'       operation_seq_code,resource_seq_code,resource_code '
48394    ||'       HAVING COUNT(*) >1 )'
48395    ||' AND   msrr1.process_flag = '||G_IN_PROCESS
48396    ||' AND   msrr1.sr_instance_code = :v_instance_code'
48397    ||' AND   NVL(msrr1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
48398 
48399 
48400          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48401 
48402     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
48403 
48404     lv_column_names :=
48405   'DEPARTMENT_CODE             ||''~''||'
48406   ||'RESOURCE_CODE             ||''~''||'
48407   ||'ORGANIZATION_CODE         ||''~''||'
48408   ||'RESOURCE_SEQ_CODE         ||''~''||'
48409   ||'RECO_START_DATE           ||''~''||'
48410   ||'ASSIGNED_UNITS            ||''~''||'
48411   ||'WIP_ENTITY_NAME           ||''~''||'
48412   ||'SR_INSTANCE_CODE          ||''~''||'
48413   ||'ITEM_NAME                 ||''~''||'
48414   ||'OPERATION_SEQ_CODE        ||''~''||'
48415   ||'BASIS_TYPE                ||''~''||'
48416   ||'RECO_COMPLETION_DATE      ||''~''||'
48417   ||'DELETED_FLAG              ||''~''||'
48418   ||'COMPANY_NAME ' ;
48419 
48420   -- Now processing by batch
48421    LOOP
48422       v_sql_stmt := 03;
48423       lv_cursor_stmt :=
48424       ' SELECT msc_st_batch_id_s.NEXTVAL '
48425       ||' FROM   dual';
48426 
48427       OPEN  c1 FOR lv_cursor_stmt;
48428       FETCH c1 INTO lv_batch_id;
48429       CLOSE c1;
48430 
48431       v_sql_stmt := 04;
48432       lv_sql_stmt :=
48433       ' UPDATE msc_st_job_op_resources '
48434       ||' SET    batch_id  = :lv_batch_id'
48435       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
48436       ||' AND    sr_instance_code               = :v_instance_code'
48437       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
48438       ||' AND    rownum                        <= '||v_batch_size;
48439 
48440 
48441          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48442 
48443       EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
48444 
48445       EXIT WHEN SQL%NOTFOUND;
48446 
48447       OPEN c5(lv_batch_id);
48448       FETCH c5 BULK COLLECT INTO lb_rowid;
48449       CLOSE c5;
48450 
48451       v_sql_stmt := 05;
48452       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
48453       UPDATE msc_st_job_op_resources
48454       SET  st_transaction_id = msc_st_job_op_resources_s.NEXTVAL,
48455            refresh_id        = v_refresh_id,
48456            last_update_date  = v_current_date,
48457            last_updated_by   = v_current_user,
48458            creation_date     = v_current_date,
48459            created_by        = v_current_user
48460       WHERE  rowid           = lb_rowid(j);
48461 
48462       -- Set the error message
48463     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48464                      (p_app_short_name    => 'MSC',
48465                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
48466                       p_message_text      => lv_message_text,
48467                       p_error_text        => lv_error_text,
48468                       p_token1            => 'COLUMN_NAME',
48469                       p_token_value1      => 'DELETED_FLAG',
48470                       p_token2            => 'DEFAULT_VALUE',
48471                       p_token_value2      =>  SYS_NO  );
48472 
48473       IF lv_return <> 0 THEN
48474         RAISE ex_logging_err;
48475       END IF;
48476 
48477      -- Check whether delete flag value is in (SYS_YES,SYS_NO),if not default to SYS_NO
48478 
48479      lv_where_str :=
48480      ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
48481 
48482      lv_return := MSC_ST_UTIL.LOG_ERROR
48483                     (p_table_name        => 'MSC_ST_JOB_OP_RESOURCES',
48484                      p_instance_code     => v_instance_code,
48485                      p_row               => lv_column_names,
48486                      p_severity          => G_SEV_WARNING,
48487                      p_message_text      => lv_message_text,
48488                      p_error_text        => lv_error_text,
48489                      p_batch_id          => lv_batch_id,
48490                      p_where_str         => lv_where_str,
48491                      p_col_name          => 'DELETED_FLAG',
48492                      p_debug             => v_debug,
48493                      p_default_value     => SYS_NO);
48494 
48495 
48496       IF lv_return <> 0 THEN
48497           RAISE ex_logging_err;
48498       END IF;
48499 
48500       -- Set the  message
48501      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48502                      (p_app_short_name    => 'MSC',
48503                       p_error_code        => 'MSC_PP_INVALID_VALUE',
48504                       p_message_text      => lv_message_text,
48505                       p_error_text        => lv_error_text,
48506                       p_token1            => 'COLUMN_NAME',
48507                       p_token_value1      => 'ORGANIZATION_CODE');
48508 
48509       IF lv_return <> 0 THEN
48510         RAISE ex_logging_err;
48511       END IF;
48512 
48513      -- Populate  organization id
48514      lv_return :=
48515      MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID(p_table_name     => 'MSC_ST_JOB_OP_RESOURCES',
48516                                     p_org_partner_name  =>'ORGANIZATION_CODE',
48517                                     p_org_partner_id    =>'ORGANIZATION_ID',
48518                                     p_instance_code     => v_instance_code,
48519                                     p_partner_type      => G_ORGANIZATION,
48520                                     p_severity          => G_SEV_ERROR,
48521                                     p_message_text      => lv_message_text,
48522                                     p_batch_id          => lv_batch_id,
48523                                     p_debug             => v_debug,
48524                                     p_error_text        => lv_error_text);
48525 
48526      IF(lv_return <> 0) then
48527         RAISE ex_logging_err;
48528      END IF;
48529 
48530     -- Derive  WIP_ENTITY_ID
48531     v_sql_stmt := 06;
48532     lv_sql_stmt:=
48533     'UPDATE        msc_st_job_op_resources msrr'
48534     ||' SET        wip_entity_id = (SELECT local_id '
48535     ||'            FROM msc_local_id_supply mlis'
48536     ||'            WHERE  mlis.char1 = msrr.sr_instance_code'
48537     ||'            AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
48538     ||'                  =    NVL(msrr.company_name,'||''''||NULL_CHAR||''''||') '
48539     ||'            AND    mlis.char3 = msrr.organization_code'
48540     ||'            AND    mlis.char4 = msrr.wip_entity_name'
48541     ||'            AND    mlis.entity_name = ''WIP_ENTITY_ID'' '
48542     ||'            AND    mlis.instance_id ='||v_instance_id||')'
48543     ||' WHERE      sr_instance_code  = :v_instance_code'
48544     ||' AND        NVL(wip_entity_name,'||''''||NULL_CHAR||''''||') '
48545     ||'             <> '||''''||NULL_CHAR||''''
48546     ||' AND        process_flag     ='||G_IN_PROCESS
48547     ||' AND        batch_id         = :lv_batch_id';
48548 
48549 
48550             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48551 
48552     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48553 
48554      -- Set the error message
48555 
48556     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48557                      (p_app_short_name    => 'MSC',
48558                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
48559                       p_message_text      => lv_message_text,
48560                       p_error_text        => lv_error_text,
48561                       p_token1            => 'COLUMN_NAMES',
48562                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
48563                                              ||'ORGANIZATION_CODE,WIP_ENTITY_NAME',
48564                       p_token2            => 'MASTER_TABLE',
48565                       p_token_value2      =>  'MSC_ST_SUPPLIES',
48566                       p_token3            =>  'CHILD_TABLE' ,
48567                       p_token_value3      =>  'MSC_ST_JOB_OP_RESOURCES' );
48568 
48569       IF lv_return <> 0 THEN
48570         RAISE ex_logging_err;
48571       END IF;
48572 
48573 
48574     -- Error out the records if WIP_ENTITY_ID NULL
48575 
48576     v_sql_stmt := 07;
48577     lv_sql_stmt:=
48578     'UPDATE      msc_st_job_op_resources msrr'
48579     ||' SET      process_flag              ='||G_ERROR_FLG||','
48580     ||'         error_text   = '||''''||lv_message_text||''''
48581     ||' WHERE    NVL(wip_entity_id,'||NULL_VALUE||') = '||NULL_VALUE
48582     ||' AND      sr_instance_code            = :v_instance_code'
48583     ||' AND      process_flag                ='||G_IN_PROCESS
48584     ||' AND      batch_id                    = :lv_batch_id';
48585 
48586 
48587             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48588 
48589     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48590 
48591      -- Derive resource id
48592      v_sql_stmt := 10;
48593 
48594     lv_sql_stmt :=
48595     'UPDATE     msc_st_job_op_resources msrr'
48596     ||' SET     resource_id=   (SELECT local_id '
48597     ||'                 FROM msc_local_id_setup mlis'
48598     ||'                 WHERE  mlis.char1 = msrr.sr_instance_code'
48599     ||'                 AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
48600     ||'                 =    NVL(msrr.company_name,'||''''||NULL_CHAR||''''||') '
48601     ||'                 AND    mlis.char3 = msrr.organization_code'
48602     ||'                 AND    mlis.char4 = msrr.resource_code'
48603     ||'                 AND    mlis.entity_name = ''RESOURCE_ID'''
48604     ||'                 AND    mlis.instance_id ='||v_instance_id||' )'
48605     ||' WHERE      msrr.sr_instance_code = :v_instance_code'
48606     ||' AND        msrr.deleted_flag     = '||SYS_NO
48607     ||' AND        msrr.process_flag     ='|| G_IN_PROCESS
48608     ||' AND        msrr.batch_id         = :lv_batch_id';
48609 
48610 
48611             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48612 
48613     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48614 
48615      -- Set the error message
48616 
48617     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48618                      (p_app_short_name    => 'MSC',
48619                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
48620                       p_message_text      => lv_message_text,
48621                       p_error_text        => lv_error_text,
48622                       p_token1            => 'COLUMN_NAMES',
48623                       p_token_value1      => 'SR_INSTANCE_CODE,COMPANY_NAME,'
48624                                              ||'ORGANIZATION_CODE,RESOURCE_CODE',
48625                       p_token2            => 'MASTER_TABLE',
48626                       p_token_value2      =>  'MSC_ST_DEPARTMENT_RESOURCES',
48627                       p_token3            =>  'CHILD_TABLE' ,
48628                       p_token_value3      =>  'MSC_ST_JOB_OP_RESOURCES' );
48629 
48630       IF lv_return <> 0 THEN
48631         RAISE ex_logging_err;
48632       END IF;
48633 
48634 
48635     -- Error out records where resource_id is NULL
48636 
48637     v_sql_stmt := 11;
48638 
48639     lv_sql_stmt:=
48640     'UPDATE      msc_st_job_op_resources msrr'
48641     ||' SET      process_flag              ='||G_ERROR_FLG||','
48642     ||'          error_text   = '||''''||lv_message_text||''''
48643     ||' WHERE    NVL(resource_id,'||NULL_VALUE||') = '||NULL_VALUE
48644     ||' AND      sr_instance_code            = :v_instance_code'
48645     ||' AND      deleted_flag                = '||SYS_NO
48646     ||' AND      process_flag                ='|| G_IN_PROCESS
48647     ||' AND      batch_id                    = :lv_batch_id';
48648 
48649 
48650             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48651 
48652     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48653 
48654     -- Popluate the Department id from LOCAL_ID table
48655 
48656     v_sql_stmt := 25;
48657     lv_sql_stmt :=
48658     'UPDATE            msc_st_job_op_resources msrs'
48659     ||' SET            department_id = (SELECT local_id'
48660     ||'                     FROM msc_local_id_setup mlis'
48661     ||'                     WHERE  mlis.char1       = msrs.sr_instance_code'
48662     ||'                     AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
48663     ||'                      =    NVL(msrs.company_name,'||''''||NULL_CHAR||''''||') '
48664     ||'                     AND    mlis.char3       = msrs.organization_code'
48665     ||'                     AND    mlis.char4       = msrs.department_code'
48666     ||'                     AND    mlis.entity_name = ''DEPARTMENT_ID'' '
48667     ||'                     AND    mlis.instance_id = '||v_instance_id||')'
48668     ||' WHERE      sr_instance_code = :v_instance_code'
48669     ||' AND        process_flag     = '|| G_IN_PROCESS
48670     ||' AND        batch_id         = :lv_batch_id';
48671 
48672 
48673             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48674 
48675       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48676 
48677 
48678 
48679        -- Set the  message
48680      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48681                      (p_app_short_name    => 'MSC',
48682                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
48683                       p_message_text      => lv_message_text,
48684                       p_error_text        => lv_error_text,
48685                       p_token1            => 'COLUMN_NAME',
48686                       p_token_value1      => 'ASSIGNED_UNITS');
48687 
48688       IF lv_return <> 0 THEN
48689         RAISE ex_logging_err;
48690       END IF;
48691 
48692     -- Error out records where assigned units is NULL
48693     v_sql_stmt := 08;
48694 
48695     lv_sql_stmt:=
48696     'UPDATE     msc_st_job_op_resources msrr'
48697     ||' SET     process_flag              ='||G_ERROR_FLG||','
48698     ||'         error_text   = '||''''||lv_message_text||''''
48699     ||' WHERE   NVL(assigned_units,'||NULL_VALUE||') = '||NULL_VALUE
48700     ||' AND     sr_instance_code            = :v_instance_code'
48701     ||' AND     deleted_flag                ='|| SYS_NO
48702     ||' AND     process_flag                ='|| G_IN_PROCESS
48703     ||' AND     batch_id                    = :lv_batch_id';
48704 
48705 
48706             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48707 
48708     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48709 
48710     -- Set the  message
48711      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48712                      (p_app_short_name    => 'MSC',
48713                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
48714                       p_message_text      => lv_message_text,
48715                       p_error_text        => lv_error_text,
48716                       p_token1            => 'COLUMN_NAME',
48717                       p_token_value1      => 'USAGE_RATE_OR_AMOUNT');
48718 
48719       IF lv_return <> 0 THEN
48720         RAISE ex_logging_err;
48721       END IF;
48722 
48723     -- Error out records where usage_rate_or_amount is NULL
48724     v_sql_stmt := 08;
48725 
48726     lv_sql_stmt:=
48727     'UPDATE     msc_st_job_op_resources msrr'
48728     ||' SET     process_flag              ='||G_ERROR_FLG||','
48729     ||'         error_text   = '||''''||lv_message_text||''''
48730     ||' WHERE   NVL(usage_rate_or_amount,'||NULL_VALUE||') = '||NULL_VALUE
48731     ||' AND     sr_instance_code            = :v_instance_code'
48732     ||' AND     deleted_flag                ='|| SYS_NO
48733     ||' AND     process_flag                ='|| G_IN_PROCESS
48734     ||' AND     batch_id                    = :lv_batch_id';
48735 
48736 
48737             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48738 
48739     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48740 
48741     -- Set the  message
48742      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48743                      (p_app_short_name    => 'MSC',
48744                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
48745                       p_message_text      => lv_message_text,
48746                       p_error_text        => lv_error_text,
48747                       p_token1            => 'COLUMN_NAME',
48748                       p_token_value1      => 'RESOURCE_SEQ_CODE');
48749 
48750       IF lv_return <> 0 THEN
48751         RAISE ex_logging_err;
48752       END IF;
48753 
48754     -- Error out records where resource sequence code is NULL
48755 
48756     lv_sql_stmt:=
48757     'UPDATE     msc_st_job_op_resources msrr'
48758     ||' SET     process_flag              ='||G_ERROR_FLG||','
48759     ||'         error_text   = '||''''||lv_message_text||''''
48760     ||' WHERE   NVL(resource_seq_code,'||''''||NULL_CHAR||''''||') '
48761     ||'         = '||''''||NULL_CHAR||''''
48762     ||' AND     sr_instance_code            = :v_instance_code'
48763     ||' AND     deleted_flag                ='|| SYS_NO
48764     ||' AND     process_flag                ='|| G_IN_PROCESS
48765     ||' AND     batch_id                    = :lv_batch_id';
48766 
48767 
48768             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48769 
48770     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48771 
48772     -- Set the error message
48773     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48774                      (p_app_short_name    => 'MSC',
48775                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
48776                       p_message_text      => lv_message_text,
48777                       p_error_text        => lv_error_text,
48778                       p_token1            => 'COLUMN_NAME',
48779                       p_token_value1      => 'RECOMMENDED');
48780 
48781       IF lv_return <> 0 THEN
48782         RAISE ex_logging_err;
48783       END IF;
48784 
48785      -- Error out the records where RECOMMENDED is not 'Y' or 'N'
48786 
48787      v_sql_stmt := 7;
48788     lv_sql_stmt :=
48789     'UPDATE    msc_st_job_op_resources'
48790     ||' SET    process_flag     ='||G_ERROR_FLG||','
48791     ||'         error_text   = '||''''||lv_message_text||''''
48792     ||' WHERE  recommended NOT IN (''Y'', ''N'')'
48793     ||' AND    sr_instance_code  = :v_instance_code'
48794     ||' AND    process_flag      = '||G_IN_PROCESS
48795     ||' AND    batch_id          = :lv_batch_id';
48796 
48797 
48798          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48799 
48800    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48801 
48802    -- If routing_name is is NULL populate the item_name in routing name
48803 
48804   v_sql_stmt := 13;
48805   lv_sql_stmt :=
48806   ' UPDATE   msc_st_job_op_resources'
48807   ||' SET    routing_name            =  item_name'
48808   ||' WHERE  process_flag            = '||G_IN_PROCESS
48809   ||' AND    NVL(routing_name,'||''''||NULL_CHAR||''''||')'
48810   ||'       = '||''''||NULL_CHAR||''''
48811   ||' AND   batch_id        = :lv_batch_id'
48812   ||' AND   sr_instance_code = :v_instance_code';
48813 
48814 
48815       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48816 
48817   EXECUTE IMMEDIATE lv_sql_stmt USING lv_batch_id,v_instance_code;
48818 
48819   -- Set the  message
48820      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48821                      (p_app_short_name    => 'MSC',
48822                       p_error_code        => 'MSC_PP_INVALID_VALUE',
48823                       p_message_text      => lv_message_text,
48824                       p_error_text        => lv_error_text,
48825                       p_token1            => 'COLUMN_NAME',
48826                       p_token_value1      => 'ROUTING_NAME');
48827 
48828       IF lv_return <> 0 THEN
48829         RAISE ex_logging_err;
48830       END IF;
48831 
48832    -- Derive the ROUTING_SEQUENCE_ID from LOCAL ID table
48833     lv_return :=
48834     MSC_ST_UTIL.DERIVE_ROUTING_SEQUENCE_ID
48835                     (p_table_name     => 'MSC_ST_JOB_OP_RESOURCES',
48836                      p_rtg_col_name   => 'ROUTING_NAME',
48837                      p_rtg_col_id     =>'ROUTING_SEQUENCE_ID',
48838                      p_instance_code  => v_instance_code,
48839                      p_severity       => G_SEV_ERROR,
48840                      p_message_text   => lv_message_text,
48841                      p_batch_id       => lv_batch_id,
48842                      p_debug          => v_debug,
48843                      p_error_text     => lv_error_text);
48844 
48845     if (lv_return <> 0 )then
48846        RAISE ex_logging_err;
48847     end if;
48848 
48849 
48850   -- Set the error message
48851     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48852                      (p_app_short_name    => 'MSC',
48853                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
48854                       p_message_text      => lv_message_text,
48855                       p_error_text        => lv_error_text,
48856                       p_token1            => 'COLUMN_NAME',
48857                       p_token_value1      => 'OPERATION_EFFECTIVITY_DATE',
48858                       p_token2            => 'DEFAULT_VALUE',
48859                       p_token_value2      => 'SYSDATE' );
48860 
48861       IF lv_return <> 0 THEN
48862         RAISE ex_logging_err;
48863       END IF;
48864 
48865     -- Default operation_effectivity date as SYSDATE if NULL
48866 
48867      lv_where_str :=
48868      ' AND NVL(operation_effectivity_date,(sysdate-36500)) =(sysdate-36500)';
48869 
48870      lv_return := MSC_ST_UTIL.LOG_ERROR
48871                     (p_table_name        => 'MSC_ST_JOB_OP_RESOURCES',
48872                      p_instance_code     => v_instance_code,
48873                      p_row               => lv_column_names,
48874                      p_severity          => G_SEV_WARNING,
48875                      p_message_text      => lv_message_text,
48876                      p_error_text        => lv_error_text,
48877                      p_batch_id          => lv_batch_id,
48878                      p_where_str         => lv_where_str,
48879                      p_col_name          => 'OPERATION_EFFECTIVITY_DATE',
48880                      p_debug             => v_debug,
48881                      p_default_value     => 'SYSDATE');
48882 
48883       IF lv_return <> 0 THEN
48884         RAISE ex_logging_err;
48885       END IF;
48886 
48887   -- Derive operation seq num
48888 
48889     v_sql_stmt := 14;
48890     lv_sql_stmt:=
48891     'UPDATE     msc_st_job_op_resources msrr'
48892     ||' SET     operation_seq_num =    (SELECT number1'
48893     ||'                    FROM msc_local_id_setup mlis'
48894     ||'                    WHERE  mlis.char1 = msrr.sr_instance_code'
48895     ||'                    AND NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
48896     ||'                    =   NVL(msrr.company_name,'||''''||NULL_CHAR||''''||') '
48897     ||'                    AND    mlis.char3 = msrr.organization_code'
48898     ||'                    AND    mlis.char4 = msrr.routing_name'
48899     ||'                    AND    mlis.char5 = msrr.operation_seq_code'
48900     ||'                    AND   NVL(mlis.char6,'||''''||NULL_CHAR||''''||') '
48901     ||'                          = NVL(msrr.alternate_routing_designator,'||''''||NULL_CHAR||''''||') '
48902     ||'                    AND    mlis.date1 = msrr.operation_effectivity_date'
48903     ||'                    AND    mlis.entity_name = ''OPERATION_SEQUENCE_ID'' '
48904     ||'                    AND    mlis.instance_id = '||v_instance_id ||')'
48905     ||' WHERE      sr_instance_code = :v_instance_code'
48906     ||' AND        process_flag     = '||G_IN_PROCESS
48907     ||' AND        batch_id         = :lv_batch_id';
48908 
48909 
48910 
48911          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48912 
48913     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48914 
48915     -- Set the error message
48916 
48917     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48918                      (p_app_short_name    => 'MSC',
48919                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
48920                       p_message_text      => lv_message_text,
48921                       p_error_text        => lv_error_text,
48922                       p_token1            => 'COLUMN_NAME',
48923                       p_token_value1      => 'OPERATION_SEQ_NUM');
48924 
48925 
48926       IF lv_return <> 0 THEN
48927         RAISE ex_logging_err;
48928       END IF;
48929 
48930      -- Error out the records whose operation_seq_num is NULL
48931 
48932     v_sql_stmt := 19;
48933     lv_sql_stmt :=
48934     'UPDATE    msc_st_job_op_resources'
48935     ||' SET    process_flag     ='||G_ERROR_FLG||','
48936     ||'        error_text   = '||''''||lv_message_text||''''
48937     ||' WHERE  NVL(operation_seq_num,'||NULL_VALUE||') ='||NULL_VALUE
48938     ||' AND    sr_instance_code  = :v_instance_code'
48939     ||' AND    process_flag      = '||G_IN_PROCESS
48940     ||' AND    batch_id          = :lv_batch_id';
48941 
48942 
48943          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
48944 
48945     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
48946 
48947     -- Set the error message
48948     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
48949                      (p_app_short_name    => 'MSC',
48950                       p_error_code        => 'MSC_PP_COL_VAL_NULL_DEFAULT',
48951                       p_message_text      => lv_message_text,
48952                       p_error_text        => lv_error_text,
48953                       p_token1            => 'COLUMN_NAME',
48954                       p_token_value1      => 'ALTERNATE_NUM',
48955                       p_token2            => 'DEFAULT_VALUE',
48956                       p_token_value2      =>  G_ALTERNATE_NUMBER );
48957 
48958       IF lv_return <> 0 THEN
48959         RAISE ex_logging_err;
48960       END IF;
48961 
48962     -- Default alternate number as 0 if NULL
48963 
48964     lv_where_str := ' AND NVL(ALTERNATE_NUM,'||NULL_VALUE||') ='||NULL_VALUE ;
48965 
48966     lv_return := MSC_ST_UTIL.LOG_ERROR
48967                     (p_table_name        => 'MSC_ST_JOB_OP_RESOURCES',
48968                      p_instance_code     => v_instance_code,
48969                      p_row               => lv_column_names,
48970                      p_severity          => G_SEV_WARNING,
48971                      p_message_text      => lv_message_text,
48972                      p_error_text        => lv_error_text,
48973                      p_batch_id          => lv_batch_id,
48974                      p_where_str         => lv_where_str,
48975                      p_col_name          => 'ALTERNATE_NUM',
48976                      p_debug             => v_debug,
48977                      p_default_value     => G_ALTERNATE_NUMBER);
48978 
48979       IF lv_return <> 0 THEN
48980         RAISE ex_logging_err;
48981       END IF;
48982 
48983     -- Derive resource seq num from the local  id table
48984 
48985     lv_sql_stmt :=
48986     'UPDATE     msc_st_job_op_resources msor'
48987     ||' SET     resource_seq_num =    (SELECT local_id '
48988     ||'         FROM msc_local_id_setup mlis'
48989     ||'         WHERE  mlis.char1 = msor.sr_instance_code'
48990     ||'         AND   NVL(mlis.char2,'||''''||NULL_CHAR||''''||') '
48991     ||'         =    NVL(msor.company_name,'||''''||NULL_CHAR||''''||') '
48992     ||'         AND    mlis.char3 = msor.organization_code'
48993     ||'         AND    mlis.char4 = msor.routing_name'
48994     ||'         AND    mlis.char5 = msor.operation_seq_code'
48995     ||'         AND    mlis.char6 = msor.resource_seq_code'
48996     ||'         AND    NVL(mlis.char7,'||''''||NULL_CHAR||''''||') '
48997     ||'         =      NVL(msor.alternate_routing_designator,'||''''||NULL_CHAR||''''||')'
48998     ||'         AND    mlis.number1 = msor.ALTERNATE_NUM'
48999     ||'         AND    mlis.entity_name = ''RESOURCE_SEQ_NUM'')'
49000     ||' WHERE   sr_instance_code = :v_instance_code'
49001     ||' AND     process_flag     = '||G_IN_PROCESS
49002     ||' AND     batch_id         = :lv_batch_id';
49003 
49004 
49005 
49006          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49007 
49008     EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code,lv_batch_id;
49009 
49010     -- Set the error message
49011     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49012                      (p_app_short_name    => 'MSC',
49013                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
49014                       p_message_text      => lv_message_text,
49015                       p_error_text        => lv_error_text,
49016                       p_token1            => 'COLUMN_NAME',
49017                       p_token_value1      => 'PRINCIPAL_FLAG',
49018                       p_token2            => 'DEFAULT_VALUE',
49019                       p_token_value2      =>  G_PRINCIPAL_FLAG  );
49020 
49021       IF lv_return <> 0 THEN
49022         RAISE ex_logging_err;
49023       END IF;
49024 
49025      -- Default principal_flag as 1 if NOT IN (1,2)
49026 
49027      lv_where_str := ' AND NVL(principal_flag,'||NULL_VALUE||')'||' NOT IN (1,2)'
49028                        ||'AND deleted_flag = 2';
49029 
49030      lv_return := MSC_ST_UTIL.LOG_ERROR
49031                     (p_table_name        => 'MSC_ST_JOB_OP_RESOURCES',
49032                      p_instance_code     => v_instance_code,
49033                      p_row               => lv_column_names,
49034                      p_severity          => G_SEV_WARNING,
49035                      p_message_text      => lv_message_text,
49036                      p_error_text        => lv_error_text,
49037                      p_batch_id          => lv_batch_id,
49038                      p_where_str         => lv_where_str,
49039                      p_col_name          => 'PRINCIPAL_FLAG',
49040                      p_debug             => v_debug,
49041                      p_default_value     => G_PRINCIPAL_FLAG);
49042 
49043       IF lv_return <> 0 THEN
49044         RAISE ex_logging_err;
49045       END IF;
49046 
49047       -- Set the error message
49048     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49049                      (p_app_short_name    => 'MSC',
49050                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
49051                       p_message_text      => lv_message_text,
49052                       p_error_text        => lv_error_text,
49053                       p_token1            => 'COLUMN_NAME',
49054                       p_token_value1      => 'BASIS_TYPE',
49055                       p_token2            => 'DEFAULT_VALUE',
49056                       p_token_value2      =>  G_BASIS_TYPE  );
49057 
49058       IF lv_return <> 0 THEN
49059         RAISE ex_logging_err;
49060       END IF;
49061 
49062      -- Default basis type as 1 if INVALID
49063 
49064     lv_where_str :=
49065     ' AND NVL(basis_type,'||NULL_VALUE||')'
49066     ||' NOT IN (1,2,3,4,5) AND deleted_flag = 2' ;
49067 
49068      lv_return := MSC_ST_UTIL.LOG_ERROR
49069                     (p_table_name        => 'MSC_ST_JOB_OP_RESOURCES',
49070                      p_instance_code     => v_instance_code,
49071                      p_row               => lv_column_names,
49072                      p_severity          => G_SEV_WARNING,
49073                      p_message_text      => lv_message_text,
49074                      p_error_text        => lv_error_text,
49075                      p_batch_id          => lv_batch_id,
49076                      p_where_str         => lv_where_str,
49077                      p_col_name          => 'BASIS_TYPE',
49078                      p_debug             => v_debug,
49079                      p_default_value     => G_BASIS_TYPE);
49080       IF lv_return <> 0 THEN
49081         RAISE ex_logging_err;
49082       END IF;
49083 
49084      OPEN c6(lv_batch_id);
49085       FETCH c6 BULK COLLECT INTO lb_rowid ;
49086 
49087       IF c6%ROWCOUNT > 0  THEN
49088 
49089       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
49090 
49091         UPDATE msc_st_job_op_resources
49092         SET    resource_seq_num      =
49093                to_number(decode(length(rtrim(resource_seq_code,'0123456789')),
49094                          NULL,resource_seq_code,'1'))
49095         WHERE  rowid                  = lb_rowid(j);
49096       END IF;
49097      CLOSE c6;
49098 
49099 
49100     --Call to customised validation.
49101 
49102      MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
49103         (ERRBUF         => lv_error_text,
49104          RETCODE        => lv_return,
49105          pBatchID       => lv_batch_id,
49106          pInstanceCode  => v_instance_code,
49107          pEntityName    => 'MSC_ST_JOB_OP_RESOURCES',
49108          pInstanceID    => v_instance_id);
49109 
49110      IF NVL(lv_return,0) <> 0 THEN
49111         RAISE ex_logging_err;
49112      END IF;
49113 
49114   -- Set the process flag as Valid and populate sr_instance_id
49115     lv_return :=
49116     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_JOB_OP_RESOURCES',
49117                                p_instance_id    => v_instance_id,
49118                                p_instance_code  => v_instance_code,
49119                                p_process_flag   => G_VALID,
49120                                p_error_text     => lv_error_text,
49121                                p_debug          => v_debug,
49122                                p_batch_id       => lv_batch_id);
49123 
49124      IF lv_return <> 0 THEN
49125         RAISE ex_logging_err;
49126      END IF;
49127 
49128 
49129     -- At the end calling  the LOG_ERROR for logging all errored out records
49130 
49131    lv_return :=
49132    MSC_ST_UTIL.LOG_ERROR(p_table_name    => 'MSC_ST_JOB_OP_RESOURCES',
49133                          p_instance_code => v_instance_code,
49134                          p_row           => lv_column_names,
49135                          p_severity      => G_SEV_ERROR,
49136                          p_message_text  => NULL,
49137                          p_error_text    => lv_error_text,
49138                          p_debug         => v_debug,
49139                          p_batch_id      => lv_batch_id  );
49140 
49141       IF lv_return <> 0 THEN
49142         RAISE ex_logging_err;
49143       END IF;
49144 
49145   COMMIT;
49146   END LOOP;
49147 
49148   EXCEPTION
49149 
49150     WHEN too_many_rows THEN
49151          lv_error_text := substr('MSC_CL_PRE_PROCESS.LOAD_JOB_DETAILS'||'('
49152                         ||v_sql_stmt||')'|| SQLERRM, 1, 240);
49153          ROLLBACK ;
49154 
49155     WHEN ex_logging_err THEN
49156         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
49157         ROLLBACK;
49158 
49159     WHEN OTHERS THEN
49160       lv_error_text :=substr('MSC_CL_PRE_PROCESS.LOAD_JOB_DETAILS'||'('
49161                       ||v_sql_stmt||')'|| SQLERRM, 1, 240);
49162       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
49163       ROLLBACK;
49164 
49165  END LOAD_JOB_DETAILS;
49166 
49167 
49168 /*==========================================================================+
49169 | DESCRIPTION  : This procedure generates records in msc_st_bis_periods if  |
49170 |                records are  coming into msc_st_trading_partners and/or    |
49171 |                records are coming in msc_st_calendars.                    |
49172 +==========================================================================*/
49173   PROCEDURE LOAD_BIS_PERIODS IS
49174   lv_error_text      VARCHAR2(250);
49175 
49176   BEGIN
49177 
49178     IF is_bis_periods_load_reqd = SYS_YES THEN
49179 
49180 
49181       DELETE FROM msc_st_bis_periods where sr_instance_id = v_instance_id;
49182       INSERT INTO msc_st_bis_periods
49183         (organization_id,
49184         period_set_name,
49185         period_name,
49186         start_date,
49187         end_date,
49188         period_type,
49189         period_year,
49190         period_num,
49191         quarter_num,
49192         entered_period_name,
49193         adjustment_period_flag,
49194         description,
49195         context,
49196         year_start_date,
49197         quarter_start_date,
49198         refresh_id,
49199         sr_instance_id,
49200         last_update_date,
49201         last_updated_by,
49202         creation_date,
49203         created_by)
49204       (SELECT
49205         sr_tp_id,
49206         'LEGACY',
49207         TO_CHAR(period_start_date,'DD-MON-YYYY'),
49208         period_start_date,
49209         next_date-1,
49210         'Month',
49211         TO_NUMBER(TO_CHAR(period_start_date,'YYYY')),
49212         msc_st_shift_num_s.nextval,
49213         decode(period_sequence_num,1,1,2,1,3,1,4,2,5,2,6,2,7,3,8,3,9,3,4),
49214         period_name,
49215         'N',
49216         'LEGACY',
49217         NULL,
49218         NULL,
49219         NULL,
49220         v_refresh_id,
49221         v_instance_id,
49222         v_current_date,
49223         v_current_user,
49224         v_current_date,
49225         v_current_user
49226       FROM  msc_period_start_dates mpsd,msc_trading_partners mtp
49227       WHERE mpsd.calendar_code  = mtp.calendar_code
49228       AND   mpsd.sr_instance_id = mtp.sr_instance_id
49229       AND   mtp.partner_type    = G_ORGANIZATION
49230       AND   mtp.sr_instance_id  = v_instance_id );
49231 -- insert for those orgs which are coming in now
49232 -- and do not exist in the ODS
49233       INSERT INTO msc_st_bis_periods
49234         (organization_id,
49235         period_set_name,
49236         period_name,
49237         start_date,
49238         end_date,
49239         period_type,
49240         period_year,
49241         period_num,
49242         quarter_num,
49243         entered_period_name,
49244         adjustment_period_flag,
49245         description,
49246         context,
49247         year_start_date,
49248         quarter_start_date,
49249         refresh_id,
49250         sr_instance_id,
49251         last_update_date,
49252         last_updated_by,
49253         creation_date,
49254         created_by)
49255       (SELECT
49256         sr_tp_id,
49257         'LEGACY',
49258         TO_CHAR(period_start_date,'DD-MON-YYYY'),
49259         period_start_date,
49260         next_date-1,
49261         'Month',
49262         TO_NUMBER(TO_CHAR(period_start_date,'YYYY')),
49263         msc_st_shift_num_s.nextval,
49264         decode(period_sequence_num,1,1,2,1,3,1,4,2,5,2,6,2,7,3,8,3,9,3,4),
49265         period_name,
49266         'N',
49267         'LEGACY',
49268         NULL,
49269         NULL,
49270         NULL,
49271         v_refresh_id,
49272         v_instance_id,
49273         v_current_date,
49274         v_current_user,
49275         v_current_date,
49276         v_current_user
49277       FROM  msc_period_start_dates mpsd,msc_st_trading_partners mtp
49278       WHERE mpsd.calendar_code  = mtp.calendar_code
49279       AND   mpsd.sr_instance_id = mtp.sr_instance_id
49280       AND   mtp.partner_type    = G_ORGANIZATION
49281       AND   mtp.process_flag    = G_VALID
49282       AND   mtp.sr_instance_id  = v_instance_id
49283       AND   NOT EXISTS(SELECT 1
49284                 FROM   msc_trading_partners mtpo
49285                 WHERE  mtpo.sr_instance_id = mtp.sr_instance_id
49286                 AND    mtpo.partner_type   = mtp.partner_type
49287                 AND    mtpo.sr_tp_id       = mtp.sr_tp_id));
49288     END IF;
49289     COMMIT;
49290   EXCEPTION
49291     WHEN OTHERS THEN
49292       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_BIS_PERIODS'||'('
49293                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
49294       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
49295       ROLLBACK;
49296 
49297   END LOAD_BIS_PERIODS;
49298 
49299   ----- ============== CHECK_DUPLICATES ==================
49300 
49301   Procedure CHECK_DUPLICATES ( ERRBUF OUT NOCOPY VARCHAR2, RETCODE OUT NOCOPY VARCHAR2)
49302   IS
49303     lv_return             NUMBER;
49304     lv_error_text         VARCHAR2(250);
49305     lv_sql_stmt           VARCHAR2(5000);
49306     lv_message_text       msc_errors.error_text%TYPE;
49307     ex_logging_err        EXCEPTION;
49308   BEGIN
49309     -- items
49310     -- Validation for table msc_st_system_items
49311     -- Duplicate records check for the records whose source is XML
49312 
49313     IF v_item_enabled = SYS_YES THEN
49314       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49315                  (p_app_short_name    => 'MSC',
49316                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
49317                   p_message_text      => lv_message_text,
49318                   p_error_text        => lv_error_text);
49319 
49320       IF lv_return <> 0 THEN
49321         RAISE ex_logging_err;
49322       END IF;
49323 
49324       v_sql_stmt := 01;
49325       lv_sql_stmt :=
49326       'UPDATE     msc_st_system_items mssi1'
49327       ||' SET     process_flag  = '||G_ERROR_FLG||','
49328       ||'         error_text   = '||''''||lv_message_text||''''
49329       ||' WHERE   message_id <  (SELECT MAX(message_id)'
49330       ||'         FROM   msc_st_system_items mssi2'
49331       ||'         WHERE  mssi2.sr_instance_code = mssi1.sr_instance_code'
49332       ||'         AND    mssi2.organization_code  = mssi1.organization_code'
49333       ||'         AND    mssi2.item_name = mssi1.item_name'
49334       ||'         AND    NVL(mssi1.company_name,    '||''''||NULL_CHAR||''''||') '
49335       ||'                =    NVL(mssi2.company_name,'||''''||NULL_CHAR||''''||') '
49336       ||'         AND    mssi2.process_flag ='|| G_IN_PROCESS
49337       ||'         AND    NVL(mssi2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||') '
49338       ||' AND     mssi1.process_flag             ='|| G_IN_PROCESS
49339       ||' AND     mssi1.sr_instance_code         = : v_instance_code'
49340       ||' AND     NVL(mssi1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
49341 
49342 
49343         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49344 
49345       EXECUTE IMMEDIATE lv_sql_stmt
49346               USING     v_instance_code;
49347 
49348       --Duplicate records check for the records whose source is batch load
49349       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49350                  (p_app_short_name    => 'MSC',
49351                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
49352                   p_message_text      => lv_message_text,
49353                   p_error_text        => lv_error_text);
49354 
49355       IF lv_return <> 0 THEN
49356         RAISE ex_logging_err;
49357       END IF;
49358 
49359       v_sql_stmt := 02;
49360       lv_sql_stmt :=
49361       'UPDATE   msc_st_system_items mssi1'
49362       ||' SET     process_flag  = '||G_ERROR_FLG||','
49363       ||'         error_text   = '||''''||lv_message_text||''''
49364       ||' WHERE   EXISTS( SELECT 1 '
49365       ||'         FROM msc_st_system_items mssi2'
49366       ||'         WHERE mssi2.sr_instance_code = mssi1.sr_instance_code'
49367       ||'         AND mssi2.organization_code  = mssi1.organization_code'
49368       ||'          AND   NVL(mssi1.company_name,'||''''||NULL_CHAR||''''||') '
49369       ||'                 =    NVL(mssi2.company_name,'||''''||NULL_CHAR||''''||') '
49370       ||'         AND mssi2.item_name          = mssi1.item_name'
49371       ||'         AND mssi2.process_flag       = '||G_IN_PROCESS
49372       ||'         AND NVL(mssi2.message_id,'||NULL_VALUE||')= '||NULL_VALUE
49373       ||'         GROUP BY sr_instance_code,organization_code,item_name,company_name'
49374       ||'         HAVING COUNT(*) >1 )'
49375       ||' AND     mssi1.process_flag ='|| G_IN_PROCESS
49376       ||' AND     mssi1.sr_instance_code = :v_instance_code'
49377       ||' AND     NVL(mssi1.message_id,'||NULL_VALUE||')= '||NULL_VALUE;
49378 
49379 
49380            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49381 
49382       EXECUTE IMMEDIATE   lv_sql_stmt
49383             USING       v_instance_code;
49384    END IF;
49385 
49386    commit;  -- for items
49387 
49388    IF v_mat_sup_enabled = SYS_YES THEN
49389       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49390                    (p_app_short_name    => 'MSC',
49391                     p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
49392                     p_message_text      => lv_message_text,
49393                     p_error_text        => lv_error_text);
49394 
49395       IF lv_return <> 0 THEN
49396         RAISE ex_logging_err;
49397       END IF;
49398 
49399       --Duplicate records check for the records whose source is XML for
49400       --PO, Purchase Req and Intransit supplies
49401       v_sql_stmt := 03;
49402       lv_sql_stmt :=
49403       'UPDATE  msc_st_supplies mss1 '
49404       ||' SET     process_flag = '||G_ERROR_FLG||','
49405       ||'         error_text   = '||''''||lv_message_text||''''
49406       ||' WHERE   message_id <  (SELECT MAX(message_id)'
49407       ||'         FROM   msc_st_supplies mss2'
49408       ||'         WHERE  mss2.sr_instance_code'
49409       ||'                = mss1.sr_instance_code'
49410       ||'         AND    mss2.order_number = mss1.order_number'
49411       ||'         AND    mss2.purch_line_num = mss1.purch_line_num'
49412       ||'         AND    mss2.order_type = mss1.order_type'
49413       ||'         AND    mss2.process_flag      = '||G_IN_PROCESS
49414       ||'         AND    NVL(mss2.company_name,  '||''''||NULL_CHAR||''''||') = '
49415       ||'                NVL(mss1.company_name,  '||''''||NULL_CHAR||''''||')'
49416       ||'         AND    mss2.sr_instance_code  = mss1.sr_instance_code'
49417       ||'         AND    NVL(mss2.message_id,'||NULL_VALUE||') <>'||NULL_VALUE||')'
49418       ||' AND    mss1.order_type        IN(1,8,2,11,12)'
49419       ||' AND    mss1.process_flag      = '||G_IN_PROCESS
49420       ||' AND    mss1.sr_instance_code  = :v_instance_code'
49421       ||' AND    NVL(mss1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
49422 
49423 
49424         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49425 
49426       EXECUTE IMMEDIATE lv_sql_stmt
49427               USING     v_instance_code;
49428 
49429       --Duplicate records check for the records whose source is XML for
49430       --Plan Orders
49431       v_sql_stmt := 04;
49432       lv_sql_stmt :=
49433       'UPDATE  msc_st_supplies mss1'
49434       ||' SET     process_flag = '||G_ERROR_FLG||','
49435       ||'         error_text   = '||''''||lv_message_text||''''
49436       ||' WHERE   message_id <  (SELECT MAX(message_id)'
49437       ||'         FROM   msc_st_supplies mss2'
49438       ||'         WHERE  mss2.sr_instance_code '
49439       ||'                = mss1.sr_instance_code'
49440       ||'         AND    NVL(mss2.company_name,'||''''||NULL_CHAR||''''||') = '
49441       ||'                NVL(mss1.company_name,'||''''||NULL_CHAR||''''||')'
49442       ||'         AND    mss2.organization_code = mss1.organization_code '
49443       ||'         AND    mss2.schedule_designator = mss1.schedule_designator'
49444       ||'         AND    mss2.schedule_line_num = mss1.schedule_line_num '
49445       ||'         AND    mss2.order_type = mss1.order_type '
49446       ||'         AND    mss2.process_flag        = '||G_IN_PROCESS
49447       ||'         AND    mss2.sr_instance_code    = mss1.sr_instance_code'
49448       ||'         AND    NVL(mss2.message_id,'||NULL_VALUE||')<>'||NULL_VALUE||')'
49449       ||' AND    mss1.order_type                 = 5'
49450       ||' AND    mss1.process_flag               = '||G_IN_PROCESS
49451       ||' AND    mss1.sr_instance_code           = :v_instance_code'
49452       ||' AND    NVL(mss1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
49453 
49454 
49455         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49456 
49457       EXECUTE IMMEDIATE lv_sql_stmt
49458               USING     v_instance_code;
49459 
49460       --Duplicate records check for the records whose source is XML for
49461       --on hand supplies
49462       v_sql_stmt := 05;
49463       lv_sql_stmt :=
49464       'UPDATE  msc_st_supplies mss1 '
49465       ||' SET     process_flag = '||G_ERROR_FLG||','
49466       ||'         error_text   = '||''''||lv_message_text||''''
49467       ||' WHERE   message_id <  (SELECT MAX(message_id)'
49468       ||'         FROM   msc_st_supplies mss2'
49469       ||'         WHERE  mss2.sr_instance_code    = mss1.sr_instance_code'
49470       ||'         AND    NVL(mss2.company_name,'||''''||NULL_CHAR||''''||') = '
49471       ||'                NVL(mss1.company_name,'||''''||NULL_CHAR||''''||')'
49472       ||'         AND    mss2.organization_code = mss1.organization_code '
49473       ||'         AND    mss2.item_name = mss1.item_name '
49474       ||'         AND    NVL(mss2.subinventory_code,'||''''||NULL_CHAR||''''||') = '
49475       ||'                NVL(mss1.subinventory_code,'||''''||NULL_CHAR||''''||')'
49476       ||'         AND    NVL(mss2.lot_number,'||''''||NULL_CHAR||''''||') = '
49477       ||'                NVL(mss1.lot_number,'||''''||NULL_CHAR||''''||')'
49478       ||'         AND    NVL(mss2.project_number,'||''''||NULL_CHAR||''''||') = '
49479       ||'                NVL(mss1.project_number,'||''''||NULL_CHAR||''''||')'
49480       ||'         AND    NVL(mss2.task_number,'||''''||NULL_CHAR||''''||') = '
49481       ||'                NVL(mss1.task_number,'||''''||NULL_CHAR||''''||')'
49482       ||'         AND    NVL(mss2.unit_number,'||''''||NULL_CHAR||''''||') = '
49483       ||'                NVL(mss1.unit_number,'||''''||NULL_CHAR||''''||')'
49484       ||'         AND    mss2.order_type = mss1.order_type '
49485       ||'         AND    mss2.process_flag        = '||G_IN_PROCESS
49486       ||'         AND    mss2.sr_instance_code    = mss1.sr_instance_code'
49487       ||'         AND    NVL(mss2.message_id,'||NULL_VALUE||')<>'||NULL_VALUE||')'
49488       ||' AND    mss1.order_type                 = 18'
49489       ||' AND    mss1.process_flag               = '||G_IN_PROCESS
49490       ||' AND    mss1.sr_instance_code           = :v_instance_code'
49491       ||' AND    NVL(mss1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
49492 
49493 
49494         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49495 
49496       EXECUTE IMMEDIATE lv_sql_stmt
49497               USING     v_instance_code;
49498 
49499 
49500       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49501                    (p_app_short_name    => 'MSC',
49502                     p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
49503                     p_message_text      => lv_message_text,
49504                     p_error_text        => lv_error_text);
49505 
49506       IF lv_return <> 0 THEN
49507         RAISE ex_logging_err;
49508       END IF;
49509 
49510       --Duplicate records check for the records whose source is other than XML
49511       --Different SQL is used because in XML we can identify the latest records
49512       --whereas in batch load we cannot.
49513       --Check for PO, purchase req. and intransit supplies.
49514       v_sql_stmt := 06;
49515       lv_sql_stmt :=
49516       'UPDATE  msc_st_supplies mss1 '
49517       ||' SET     process_flag  = '||G_ERROR_FLG||','
49518       ||'         error_text   = '||''''||lv_message_text||''''
49519       ||' WHERE   EXISTS( SELECT 1 '
49520       ||'         FROM   msc_st_supplies mss2'
49521       ||'         WHERE  mss2.sr_instance_code'
49522       ||'                = mss1.sr_instance_code'
49523       ||'         AND    NVL(mss2.company_name,    '||''''||NULL_CHAR||''''||') = '
49524       ||'                NVL(mss1.company_name,    '||''''||NULL_CHAR||''''||')'
49525       ||'         AND    mss2.order_number = mss1.order_number'
49526       ||'         AND    mss2.purch_line_num = mss1.purch_line_num'
49527       ||'         AND    mss2.order_type = mss1.order_type'
49528       ||'         AND    mss2.process_flag      = '||G_IN_PROCESS
49529       ||'         AND    mss2.sr_instance_code  = mss1.sr_instance_code'
49530       ||'         AND    NVL(mss2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
49531       ||'         GROUP BY sr_instance_code,order_number,company_name,'
49532       ||'                purch_line_num,order_type,organization_code'
49533       ||'         HAVING COUNT(*) > 1)'
49534       ||' AND     mss1.order_type        IN(1,8,2,11,12)'
49535       ||' AND     mss1.process_flag     = '||G_IN_PROCESS
49536       ||' AND     mss1.sr_instance_code = :v_instance_code'
49537       ||' AND     NVL(mss1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
49538 
49539 
49540         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49541 
49542       EXECUTE IMMEDIATE lv_sql_stmt
49543               USING     v_instance_code;
49544 
49545       --Duplicate Check for plan orders(batch load).
49546       v_sql_stmt := 08;
49547       lv_sql_stmt :=
49548       'UPDATE  msc_st_supplies mss1 '
49549       ||' SET     process_flag  = '||G_ERROR_FLG||','
49550       ||'         error_text   = '||''''||lv_message_text||''''
49551       ||' WHERE   EXISTS( SELECT 1 '
49552       ||'         FROM   msc_st_supplies mss2'
49553       ||'         WHERE  mss2.sr_instance_code'
49554       ||'                = mss1.sr_instance_code'
49555       ||'         AND    NVL(mss2.company_name,'||''''||NULL_CHAR||''''||') = '
49556       ||'                NVL(mss1.company_name,'||''''||NULL_CHAR||''''||')'
49557       ||'         AND    mss2.organization_code = mss1.organization_code '
49558       ||'         AND    mss2.schedule_designator = mss1.schedule_designator'
49559       ||'         AND    mss2.schedule_line_num = mss1.schedule_line_num '
49560       ||'         AND    mss2.order_type = mss1.order_type '
49561       ||'         AND    mss2.process_flag        = '||G_IN_PROCESS
49562       ||'         AND    mss2.sr_instance_code    = mss1.sr_instance_code'
49563       ||'         AND    NVL(mss2.message_id,'||NULL_VALUE||') ='||NULL_VALUE
49564       ||'         GROUP BY sr_instance_code,schedule_designator,company_name,'
49565       ||'                schedule_line_num,order_type,organization_code'
49566       ||'         HAVING COUNT(*) > 1)'
49567       ||' AND     mss1.order_type          = 5'
49568       ||' AND     mss1.process_flag           = '||G_IN_PROCESS
49569       ||' AND     mss1.sr_instance_code           = :v_instance_code'
49570       ||' AND     NVL(mss1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
49571 
49572 
49573         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49574 
49575       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
49576 
49577       --Duplicate Check for on hand supplies(batch load).
49578       v_sql_stmt := 09;
49579       lv_sql_stmt :=
49580       'UPDATE  msc_st_supplies mss1'
49581       ||' SET     process_flag  = '||G_ERROR_FLG||','
49582       ||'         error_text   = '||''''||lv_message_text||''''
49583       ||' WHERE   EXISTS( SELECT 1 '
49584       ||'         FROM   msc_st_supplies mss2'
49585       ||'         WHERE  mss2.sr_instance_code    = mss1.sr_instance_code'
49586       ||'         AND    NVL(mss2.company_name,'||''''||NULL_CHAR||''''||') = '
49587       ||'                NVL(mss1.company_name,'||''''||NULL_CHAR||''''||')'
49588       ||'         AND    mss2.organization_code = mss1.organization_code '
49589       ||'         AND    mss2.item_name = mss1.item_name '
49590       ||'         AND    NVL(mss2.subinventory_code,'||''''||NULL_CHAR||''''||') = '
49591       ||'                NVL(mss1.subinventory_code,'||''''||NULL_CHAR||''''||')'
49592       ||'         AND    NVL(mss2.lot_number,'||''''||NULL_CHAR||''''||') = '
49593       ||'                NVL(mss1.lot_number,'||''''||NULL_CHAR||''''||')'
49594       ||'         AND    NVL(mss2.project_number,'||''''||NULL_CHAR||''''||') = '
49595       ||'                NVL(mss1.project_number,'||''''||NULL_CHAR||''''||')'
49596       ||'         AND    NVL(mss2.task_number,'||''''||NULL_CHAR||''''||') = '
49597       ||'                NVL(mss1.task_number,'||''''||NULL_CHAR||''''||')'
49598       ||'         AND    NVL(mss2.unit_number,'||''''||NULL_CHAR||''''||') = '
49599       ||'                NVL(mss1.unit_number,'||''''||NULL_CHAR||''''||')'
49600       ||'         AND    mss2.order_type = mss1.order_type '
49601       ||'         AND    mss2.process_flag     = '||G_IN_PROCESS
49602       ||'         AND    mss2.sr_instance_code = mss1.sr_instance_code'
49603       ||'         AND    NVL(mss2.message_id,'||NULL_VALUE||') ='||NULL_VALUE
49604       ||'         GROUP BY sr_instance_code,organization_code,'
49605       ||'                item_name,subinventory_code,company_name,'
49606       ||'                lot_number, project_number,'
49607       ||'                task_number,unit_number,'
49608       ||'                order_type'
49609       ||'         HAVING COUNT(*) > 1)'
49610       ||' AND     mss1.order_type       = 18'
49611       ||' AND     mss1.process_flag       = '||G_IN_PROCESS
49612       ||' AND     mss1.sr_instance_code   = :v_instance_code'
49613       ||' AND     NVL(mss1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
49614 
49615 
49616         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49617 
49618       EXECUTE IMMEDIATE lv_sql_stmt
49619               USING     v_instance_code;
49620 
49621       --Since all the validation is done based on the order type,
49622       --order_type validation is done here which is common to all
49623       --the supplies including WO supplies.
49624 
49625       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49626                      (p_app_short_name    => 'MSC',
49627                       p_error_code        => 'MSC_PP_INVALID_VALUE',
49628                       p_message_text      => lv_message_text,
49629                       p_error_text        => lv_error_text,
49630                       p_token1            => 'COLUMN_NAME',
49631                       p_token_value1      => 'ORDER_TYPE');
49632 
49633       IF lv_return <> 0 THEN
49634         RAISE ex_logging_err;
49635       END IF;
49636 
49637 
49638       v_sql_stmt := 10;
49639       lv_sql_stmt :=
49640       'UPDATE  msc_st_supplies '
49641       ||' SET  process_flag = '||G_ERROR_FLG||','
49642       ||'         error_text   = '||''''||lv_message_text||''''
49643       ||' WHERE order_type NOT IN(1,2,3,5,7,8,11,12,14,15,16,18,27,75,73,74,87,86)'
49644       ||' AND   process_flag     = '||G_IN_PROCESS
49645       ||' AND   sr_instance_code = :v_instance_code';
49646 
49647 
49648         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49649 
49650       EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
49651    END IF;
49652 
49653    commit; -- for supplies
49654 
49655 
49656    IF v_mat_dmd_enabled = SYS_YES THEN
49657 
49658      --Duplicate records check for the records whose source is XML
49659 
49660       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49661                    (p_app_short_name    => 'MSC',
49662                     p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
49663                     p_message_text      => lv_message_text,
49664                     p_error_text        => lv_error_text);
49665 
49666       IF lv_return <> 0 THEN
49667         RAISE ex_logging_err;
49668       END IF;
49669 
49670      --For MDS and Forecast (origination_type 8 and 29)
49671 
49672      -- MDS
49673      lv_sql_stmt :=
49674      'UPDATE   msc_st_demands msd1'
49675      ||' SET     process_flag  = '||G_ERROR_FLG||','
49676      ||'         error_text   = '||''''||lv_message_text||''''
49677      ||' WHERE   message_id <  (SELECT MAX(message_id)'
49678      ||'         FROM  msc_st_demands msd2'
49679      ||'         WHERE  msd2.sr_instance_code  = msd1.sr_instance_code '
49680      ||'         AND    nvl(msd2.using_assembly_demand_date,SYSDATE-365000 ) = nvl(msd1.using_assembly_demand_date,SYSDATE-365000 ) '
49681      ||'         AND  msd2.item_name = msd1.item_name'
49682      ||'         AND      msd2.organization_code   = msd1.organization_code '
49683      ||'         AND   NVL(msd2.company_name,'||''''||NULL_CHAR||''''||') '
49684      ||'                 =    NVL(msd1.company_name,'||''''||NULL_CHAR||''''||') '
49685      ||'         AND msd2.demand_schedule_name = msd1.demand_schedule_name '
49686      ||'         AND  NVL(msd2.schedule_line_num,'||''''||NULL_CHAR||''''||') '
49687      ||'               = NVL(msd1.schedule_line_num,'||''''||NULL_CHAR||''''||') '
49688      ||'         AND  msd2.origination_type  = msd1.origination_type'
49689      ||'         AND   msd2.process_flag = '||G_IN_PROCESS
49690      ||'         AND  NVL(msd2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
49691      ||' AND     msd1.process_flag ='|| G_IN_PROCESS
49692      ||' AND     msd1.origination_type = 8'
49693      ||' AND     msd1.sr_instance_code = :v_instance_code '
49694      ||' AND     NVL(msd1.message_id,'||NULL_VALUE||')  <> '||NULL_VALUE;
49695 
49696 
49697             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49698 
49699      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
49700 
49701      -- Forecast
49702 
49703      lv_sql_stmt :=
49704      'UPDATE   msc_st_demands msd1'
49705      ||' SET     process_flag  = '||G_ERROR_FLG||','
49706      ||'         error_text   = '||''''||lv_message_text||''''
49707      ||' WHERE   message_id <  (SELECT MAX(message_id)'
49708      ||'         FROM  msc_st_demands msd2'
49709      ||'         WHERE  msd2.sr_instance_code     = msd1.sr_instance_code '
49710      ||'         AND    msd2.schedule_date   = msd1.schedule_date '
49711      ||'         AND    msd2.item_name  = msd1.item_name'
49712      ||'         AND    msd2.forecast_designator = msd1.forecast_designator '
49713      ||'         AND    NVL(msd2.schedule_line_num,'||''''||NULL_CHAR||''''||') '
49714      ||'                 = NVL(msd1.schedule_line_num,'||''''||NULL_CHAR||''''||') '
49715      ||'         AND    msd2.organization_code  = msd1.organization_code '
49716      ||'         AND    NVL(msd2.company_name,'||''''||NULL_CHAR||''''||') '
49717      ||'                 =    NVL(msd1.company_name,'||''''||NULL_CHAR||''''||') '
49718      ||'         AND    msd2.origination_type  = msd1.origination_type'
49719      ||'         AND    msd2.process_flag = '||G_IN_PROCESS
49720      ||'         AND    NVL(msd2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
49721      ||' AND     msd1.process_flag ='|| G_IN_PROCESS
49722      ||' AND     msd1.origination_type = 29 '
49723      ||' AND     msd1.sr_instance_code = :v_instance_code '
49724      ||' AND     NVL(msd1.message_id,'||NULL_VALUE||')  <> '||NULL_VALUE;
49725 
49726 
49727             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49728 
49729      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
49730 
49731      --Duplicate records check for the records whose source is batch load
49732 
49733        lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49734                     (p_app_short_name    => 'MSC',
49735                      p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
49736                      p_message_text      => lv_message_text,
49737                      p_error_text        => lv_error_text);
49738 
49739        IF lv_return <> 0 THEN
49740          RAISE ex_logging_err;
49741        END IF;
49742 
49743      -- Forecast
49744      v_sql_stmt := 12;
49745 
49746      lv_sql_stmt :=
49747      'UPDATE msc_st_demands   msd1'
49748      ||' SET     process_flag  = '||G_ERROR_FLG||','
49749      ||'         error_text   = '||''''||lv_message_text||''''
49750      ||' WHERE   EXISTS( SELECT 1 '
49751      ||'         FROM msc_st_demands msd2'
49752      ||'         WHERE  msd2.sr_instance_code = msd1.sr_instance_code '
49753      ||'         AND msd2.organization_code = msd1.organization_code '
49754      ||'         AND NVL(msd2.company_name,'||''''||NULL_CHAR||''''||') '
49755      ||'                 =    NVL(msd1.company_name,'||''''||NULL_CHAR||''''||') '
49756      ||'         AND msd2.forecast_designator = msd1.forecast_designator '
49757      ||'         AND msd2.item_name = msd1.item_name'
49758      ||'         AND NVL(msd2.schedule_line_num,'||''''||NULL_CHAR||''''||') '
49759      ||'               = NVL(msd1.schedule_line_num,'||''''||NULL_CHAR||''''||') '
49760      ||'         AND msd2.schedule_date = msd1.schedule_date '
49761      ||'         AND msd2.origination_type = msd1.origination_type'
49762      ||'         AND msd2.process_flag = '||G_IN_PROCESS
49763      ||'         AND NVL(msd2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
49764      ||'         AND msd2.rowid <> msd1.rowid)'
49765      ||' AND   msd1.process_flag  = '||G_IN_PROCESS
49766      ||' AND   msd1.origination_type = 29'
49767      ||' AND   msd1.sr_instance_code = :v_instance_code'
49768      ||' AND   NVL(msd1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
49769 
49770 
49771             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49772 
49773      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
49774 
49775      -- MDS
49776      v_sql_stmt := 12;
49777 
49778      lv_sql_stmt :=
49779      'UPDATE msc_st_demands   msd1'
49780      ||' SET     process_flag  = '||G_ERROR_FLG||','
49781      ||'         error_text   = '||''''||lv_message_text||''''
49782      ||' WHERE   EXISTS( SELECT 1 '
49783      ||'         FROM msc_st_demands msd2'
49784      ||'         WHERE  msd2.sr_instance_code '
49785      ||'                = msd1.sr_instance_code '
49786      ||'         AND msd2.organization_code '
49787      ||'                = msd1.organization_code '
49788      ||'         AND NVL(msd2.company_name,'||''''||NULL_CHAR||''''||') '
49789      ||'                 =    NVL(msd1.company_name,'||''''||NULL_CHAR||''''||') '
49790      ||'         AND msd2.demand_schedule_name = msd1.demand_schedule_name '
49791      ||'         AND msd2.item_name  = msd1.item_name'
49792      ||'         AND NVL(msd2.schedule_line_num,'||''''||NULL_CHAR||''''||') '
49793      ||'               = NVL(msd1.schedule_line_num,'||''''||NULL_CHAR||''''||') '
49794      ||'         AND    nvl(msd2.using_assembly_demand_date,SYSDATE-365000 ) = nvl(msd1.using_assembly_demand_date,SYSDATE-365000 ) '
49795      ||'         AND msd2.origination_type '
49796      ||'               = msd1.origination_type'
49797      ||'         AND msd2.process_flag = '||G_IN_PROCESS
49798      ||'         AND NVL(msd2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
49799      ||'         AND msd2.rowid <> msd1.rowid)'
49800      ||' AND   msd1.process_flag  = '||G_IN_PROCESS
49801      ||' AND   msd1.origination_type = 8 '
49802      ||' AND   msd1.sr_instance_code = :v_instance_code'
49803      ||' AND   NVL(msd1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
49804 
49805 
49806             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49807 
49808      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
49809 
49810 
49811      --Duplicate SO records check for the records whose source is XML
49812 
49813      lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49814                    (p_app_short_name    => 'MSC',
49815                     p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
49816                     p_message_text      => lv_message_text,
49817                     p_error_text        => lv_error_text);
49818 
49819      IF lv_return <> 0 THEN
49820        RAISE ex_logging_err;
49821      END IF;
49822      --Duplicate records check for the records whose source is XML
49823 
49824      v_sql_stmt := 13;
49825 
49826      lv_sql_stmt :=
49827      'UPDATE   msc_st_sales_orders   mso1'
49828      ||' SET     process_flag  = '||G_ERROR_FLG||','
49829      ||'         error_text   = '||''''||lv_message_text||''''
49830      ||' WHERE   message_id <  (SELECT MAX(message_id)'
49831      ||'         FROM  msc_st_sales_orders    mso2'
49832      ||'         WHERE  mso2.sr_instance_code '
49833      ||'                = mso1.sr_instance_code '
49834      ||'         AND   mso2.sales_order_number '
49835      ||'                = mso1.sales_order_number '
49836      ||'         AND mso2.line_num'
49837      ||'                = mso1.line_num'
49838      ||'         AND   mso2.organization_code '
49839      ||'                = mso1.organization_code '
49840      ||'         AND   nvl(mso2.reservation_type,'||NULL_VALUE||') '
49841      ||'                = nvl(mso1.reservation_type,'||NULL_VALUE||') '
49842      ||'         AND   nvl(mso2.SUBINVENTORY,'||''''||NULL_CHAR||''''||') '
49843      ||'                = nvl(mso1.SUBINVENTORY,'||''''||NULL_CHAR||''''||') '
49844      ||'         AND   NVL(mso2.company_name,'||''''||NULL_CHAR||''''||') '
49845      ||'                 =    NVL(mso1.company_name,'||''''||NULL_CHAR||''''||') '
49846      ||'         AND  mso2.process_flag = '||G_IN_PROCESS
49847      ||'         AND  NVL(mso2.message_id,'||NULL_VALUE||')<> '||NULL_VALUE||')'
49848      ||' AND    mso1.process_flag ='|| G_IN_PROCESS
49849      ||' AND    mso1.sr_instance_code = :v_instance_code '
49850      ||' AND    NVL(mso1.message_id,'||NULL_VALUE||')<> '||NULL_VALUE;
49851 
49852 
49853             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49854 
49855      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
49856 
49857 
49858      --Duplicate records check for the records whose source is batch load
49859 
49860        lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49861                     (p_app_short_name    => 'MSC',
49862                      p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
49863                      p_message_text      => lv_message_text,
49864                      p_error_text        => lv_error_text);
49865 
49866        IF lv_return <> 0 THEN
49867          RAISE ex_logging_err;
49868        END IF;
49869 
49870      v_sql_stmt := 14;
49871 
49872      lv_sql_stmt :=
49873      'UPDATE msc_st_sales_orders   mso1'
49874      ||' SET     process_flag  = '||G_ERROR_FLG||','
49875      ||'         error_text   = '||''''||lv_message_text||''''
49876      ||' WHERE   EXISTS( SELECT 1 '
49877      ||'         FROM msc_st_sales_orders mso2'
49878      ||'         WHERE  mso2.sr_instance_code '
49879      ||'                = mso1.sr_instance_code '
49880      ||'         AND   mso2.sales_order_number '
49881      ||'                = mso1.sales_order_number '
49882      ||'         AND mso2.line_num'
49883      ||'                = mso1.line_num'
49884      ||'         AND   mso2.organization_code '
49885      ||'                = mso1.organization_code '
49886      ||'         AND   nvl(mso2.reservation_type,'||NULL_VALUE||') '
49887      ||'                = nvl(mso1.reservation_type,'||NULL_VALUE||') '
49888      ||'         AND   nvl(mso2.SUBINVENTORY,'||''''||NULL_CHAR||''''||') '
49889      ||'                = nvl(mso1.SUBINVENTORY,'||''''||NULL_CHAR||''''||') '
49890      ||'         AND   NVL(mso2.company_name,'||''''||NULL_CHAR||''''||') '
49891      ||'                 =    NVL(mso1.company_name,'||''''||NULL_CHAR||''''||') '
49892      ||'         AND mso2.process_flag = '||G_IN_PROCESS
49893      ||'         AND NVL(mso2.message_id,'||NULL_VALUE||') = '||NULL_VALUE
49894      ||'         AND mso2.rowid <> mso1.rowid )'
49895      ||' AND   mso1.process_flag  = '||G_IN_PROCESS
49896      ||' AND   mso1.sr_instance_code = :v_instance_code'
49897      ||' AND   NVL(mso1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
49898 
49899 
49900             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49901 
49902      EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
49903    END IF;
49904 
49905    commit; -- for demands
49906 
49907   -- For LEVEL_VALUE
49908   IF v_level_value_enabled = SYS_YES THEN
49909      v_sql_stmt := 15;
49910     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
49911                  (p_app_short_name    => 'MSC',
49912                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
49913                   p_message_text      => lv_message_text,
49914                   p_error_text        => lv_error_text);
49915 
49916     IF lv_return <> 0 THEN
49917       RAISE ex_logging_err;
49918     END IF;
49919 
49920     v_sql_stmt := 01;
49921     lv_sql_stmt :=
49922     'UPDATE    msd_st_level_values mlv1'
49923     ||' SET    process_flag = '||G_ERROR_FLG||','
49924     ||'         error_text   = '||''''||lv_message_text||''''
49925     ||' WHERE  EXISTS ( SELECT 1 FROM msd_st_level_values mlv2'
49926     ||'        WHERE mlv1.level_name  = mlv2.level_name'
49927     ||'        AND   mlv1.level_value = mlv2.level_value'
49928     ||'        AND   mlv1.process_flag = mlv2.process_flag'
49929     ||'        AND   mlv1.sr_instance_code = mlv2.sr_instance_code'
49930     ||'        GROUP BY sr_instance_code,level_name, level_value'
49931     ||'        HAVING COUNT(*)> 1 )'
49932     ||' AND    mlv1.process_flag = '||G_IN_PROCESS
49933     ||' AND    sr_instance_code  =  :v_instance_code';
49934 
49935 
49936       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
49937 
49938     EXECUTE IMMEDIATE lv_sql_stmt
49939             USING     v_instance_code;
49940 
49941     commit;
49942     END IF; -- level_value
49943 
49944   EXCEPTION
49945     WHEN ex_logging_err THEN
49946       ERRBUF  := lv_error_text;
49947       RETCODE := G_ERROR;
49948       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,ERRBUF);
49949       ROLLBACK;
49950     WHEN others THEN
49951       ERRBUF  := SQLERRM;
49952       RETCODE := G_ERROR;
49953       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,ERRBUF);
49954       ROLLBACK;
49955   END CHECK_DUPLICATES ;
49956 
49957   -- Item Customer Records
49958   PROCEDURE  LOAD_ITEM_CUSTOMERS IS
49959 
49960   TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
49961   lb_rowid          RowidTab;
49962 
49963   lv_column_names       VARCHAR2(5000);     -- Stores cocatenated column names
49964   lv_return             NUMBER;
49965   lv_error_text         VARCHAR2(250);
49966   lv_where_str          VARCHAR2(5000);
49967   lv_sql_stmt           VARCHAR2(5000);
49968   lv_batch_id           msc_st_system_items.batch_id%TYPE;
49969   lv_message_text       msc_errors.error_text%TYPE;
49970 
49971   ex_logging_err    EXCEPTION;
49972   busy              EXCEPTION;
49973   PRAGMA EXCEPTION_INIT(busy, -54);
49974 
49975   CURSOR c1(p_batch_id NUMBER) IS
49976     SELECT rowid
49977     FROM   msc_st_item_customers
49978     WHERE  process_flag      IN (G_IN_PROCESS,G_ERROR_FLG)
49979     AND    sr_instance_code = v_instance_code
49980     AND    batch_id         = p_batch_id;
49981 
49982   BEGIN
49983     -- Validation of table msc_st_item_customers
49984 
49985     lv_column_names :=
49986       'ITEM_NAME                 ||''~''||'
49987     ||'CUSTOMER_ITEM_NAME        ||''~''||'
49988     ||'DESCRIPTION               ||''~''||'
49989     ||'CUSTOMER_NAME             ||''~''||'
49990     ||'CUSTOMER_SITE_NAME        ||''~''||'
49991     ||'SR_INSTANCE_CODE          ||''~''||'
49992     ||'LEAD_TIME                 ||''~''||'
49993     ||'UOM_CODE                  ||''~''||'
49994     ||'LIST_PRICE                ||''~''||'
49995     ||'PLANNER_CODE              ||''~''||'
49996     ||'COMPANY_NAME';
49997 
49998 
49999     -- For XML, duplicate UDK combinations would be allowed. We would pick up the record with the maximum MESSAGE_ID for the same user defined key combination for processing. For other records we would set process flag to 3 and log an error message.
50000     -- Duplicate records check for the records whose source is XML.
50001 
50002     -- Error out records if same OEM item is mapped to multiple customer items
50003 
50004     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50005                  (p_app_short_name    => 'MSC',
50006                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
50007                   p_message_text      => lv_message_text,
50008                   p_error_text        => lv_error_text);
50009 
50010     IF lv_return <> 0 THEN
50011       RAISE ex_logging_err;
50012     END IF;
50013 
50014     v_sql_stmt := 01;
50015     lv_sql_stmt :=
50016     'UPDATE     msc_st_item_customers mic1 '
50017     ||' SET     process_flag = '||G_ERROR_FLG||','
50018     ||'         error_text   = '||''''||lv_message_text||''''
50019     ||' WHERE   message_id <  (SELECT MAX(message_id)'
50020     ||'         FROM   msc_st_item_customers mic2'
50021     ||'         WHERE  mic2.sr_instance_code      = mic1.sr_instance_code'
50022     ||'         AND    NVL(mic2.company_name,  '||''''||NULL_CHAR||''''||') = '
50023     ||'                NVL(mic1.company_name,  '||''''||NULL_CHAR||''''||')'
50024     ||'         AND    mic2.ITEM_NAME            = mic1.ITEM_NAME'
50025     ||'         AND    mic2.CUSTOMER_NAME   = mic1.CUSTOMER_NAME'
50026     ||'         AND    NVL(mic2.CUSTOMER_SITE_NAME,  '||''''||NULL_CHAR||''''||') = '
50027     ||'                NVL(mic1.CUSTOMER_SITE_NAME,  '||''''||NULL_CHAR||''''||'))'
50028     ||' AND    mic1.process_flag     = '||G_IN_PROCESS
50029     ||' AND    mic1.sr_instance_code = :v_instance_code'
50030     ||' AND    NVL(mic1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
50031 
50032 
50033       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50034 
50035     EXECUTE IMMEDIATE lv_sql_stmt
50036             USING     v_instance_code;
50037 
50038     -- Error out records if multiple OEM items are mapped to same customer items
50039 
50040     IF lv_return <> 0 THEN
50041       RAISE ex_logging_err;
50042     END IF;
50043 
50044     v_sql_stmt := 02;
50045     lv_sql_stmt :=
50046     'UPDATE     msc_st_item_customers mic1 '
50047     ||' SET     process_flag = '||G_ERROR_FLG||','
50048     ||'         error_text   = '||''''||lv_message_text||''''
50049     ||' WHERE   message_id <  (SELECT MAX(message_id)'
50050     ||'         FROM   msc_st_item_customers mic2'
50051     ||'         WHERE  mic2.sr_instance_code      = mic1.sr_instance_code'
50052     ||'         AND    NVL(mic2.company_name,  '||''''||NULL_CHAR||''''||') = '
50053     ||'                NVL(mic1.company_name,  '||''''||NULL_CHAR||''''||')'
50054     ||'         AND    mic2.CUSTOMER_ITEM_NAME            = mic1.CUSTOMER_ITEM_NAME'
50055     ||'         AND    mic2.CUSTOMER_NAME   = mic1.CUSTOMER_NAME'
50056     ||'         AND    NVL(mic2.CUSTOMER_SITE_NAME,  '||''''||NULL_CHAR||''''||') = '
50057     ||'                NVL(mic1.CUSTOMER_SITE_NAME,  '||''''||NULL_CHAR||''''||'))'
50058     ||' AND    mic1.process_flag     = '||G_IN_PROCESS
50059     ||' AND    mic1.sr_instance_code = :v_instance_code'
50060     ||' AND    NVL(mic1.message_id,'||NULL_VALUE||') <> '||NULL_VALUE;
50061 
50062 
50063       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50064 
50065     EXECUTE IMMEDIATE lv_sql_stmt
50066             USING     v_instance_code;
50067 
50068     -- For Batch loads (excel etc.)  we error out all duplicate UDK records.
50069     -- No record would be processed in this case. To identify whether the record has come via batch/ XML we would use the MESSAGE_ID field.
50070     -- If MESSAGE_ID is not null then we would assume it to have come in via XML else via batch load.
50071 
50072     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50073                  (p_app_short_name    => 'MSC',
50074                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
50075                   p_message_text      => lv_message_text,
50076                   p_error_text        => lv_error_text);
50077 
50078     IF lv_return <> 0 THEN
50079       RAISE ex_logging_err;
50080     END IF;
50081 
50082     --Duplicate records check for the records whose source is other than XML
50083     --Different SQL is used because in XML we can identify the latest records
50084     --whereas in batch load we cannot.
50085 
50086 
50087     -- Error out records if same OEM item is mapped to multiple customer items. add index for this
50088 
50089     v_sql_stmt := 03;
50090     lv_sql_stmt :=
50091     'UPDATE     msc_st_item_customers mic1 '
50092     ||' SET     process_flag = '||G_ERROR_FLG||','
50093     ||'         error_text   = '||''''||lv_message_text||''''
50094     ||' WHERE   EXISTS( SELECT 1 '
50095     ||'         FROM   msc_st_item_customers mic2'
50096     ||'         WHERE  mic2.sr_instance_code      = mic1.sr_instance_code'
50097     ||'         AND    NVL(mic2.company_name,  '||''''||NULL_CHAR||''''||') = '
50098     ||'                NVL(mic1.company_name,  '||''''||NULL_CHAR||''''||')'
50099     ||'         AND    mic2.ITEM_NAME            = mic1.ITEM_NAME'
50100     ||'         AND    mic2.CUSTOMER_NAME   = mic1.CUSTOMER_NAME'
50101     ||'         AND    NVL(mic2.CUSTOMER_SITE_NAME,  '||''''||NULL_CHAR||''''||') = '
50102     ||'                NVL(mic1.CUSTOMER_SITE_NAME,  '||''''||NULL_CHAR||''''||')'
50103     ||'         AND    mic1.rowid <> mic2.rowid)'
50104     ||' AND    mic1.process_flag     = '||G_IN_PROCESS
50105     ||' AND    mic1.sr_instance_code = :v_instance_code'
50106     ||' AND    NVL(mic1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
50107 
50108 
50109       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50110 
50111     EXECUTE IMMEDIATE lv_sql_stmt
50112             USING     v_instance_code;
50113 
50114     -- Error out records if multiple OEM items are mapped to same customer items. add index for this
50115 
50116     v_sql_stmt := 04;
50117     lv_sql_stmt :=
50118     'UPDATE     msc_st_item_customers mic1 '
50119     ||' SET     process_flag = '||G_ERROR_FLG||','
50120     ||'         error_text   = '||''''||lv_message_text||''''
50121     ||' WHERE   EXISTS( SELECT 1 '
50122     ||'         FROM   msc_st_item_customers mic2'
50123     ||'         WHERE  mic2.sr_instance_code      = mic1.sr_instance_code'
50124     ||'         AND    NVL(mic2.company_name,  '||''''||NULL_CHAR||''''||') = '
50125     ||'                NVL(mic1.company_name,  '||''''||NULL_CHAR||''''||')'
50126     ||'         AND    mic2.CUSTOMER_ITEM_NAME            = mic1.CUSTOMER_ITEM_NAME'
50127     ||'         AND    mic2.CUSTOMER_NAME   = mic1.CUSTOMER_NAME'
50128     ||'         AND    NVL(mic2.CUSTOMER_SITE_NAME,  '||''''||NULL_CHAR||''''||') = '
50129     ||'                NVL(mic1.CUSTOMER_SITE_NAME,  '||''''||NULL_CHAR||''''||')'
50130     ||'         AND    mic1.rowid <> mic2.rowid)'
50131     ||' AND    mic1.process_flag     = '||G_IN_PROCESS
50132     ||' AND    mic1.sr_instance_code = :v_instance_code'
50133     ||' AND    NVL(mic1.message_id,'||NULL_VALUE||') = '||NULL_VALUE;
50134 
50135 
50136       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50137 
50138     EXECUTE IMMEDIATE lv_sql_stmt
50139             USING     v_instance_code;
50140 
50141 
50142     -- Generate the batch_id and update the tables with the batch_id.
50143 
50144     LOOP
50145       v_sql_stmt := 05;
50146 
50147       SELECT msc_st_batch_id_s.NEXTVAL
50148       INTO  lv_batch_id
50149       FROM  dual;
50150 
50151       v_sql_stmt := 06;
50152       lv_sql_stmt :=
50153       'UPDATE msc_st_item_customers '
50154       ||' SET    batch_id  = :lv_batch_id'
50155       ||' WHERE  process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
50156       ||' AND    sr_instance_code               = :v_instance_code'
50157       ||' AND    NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
50158       ||' AND    rownum                        <= '||v_batch_size;
50159 
50160 
50161         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50162 
50163       EXECUTE IMMEDIATE lv_sql_stmt
50164               USING     lv_batch_id,
50165                         v_instance_code;
50166 
50167       EXIT WHEN SQL%NOTFOUND;
50168 
50169       OPEN c1(lv_batch_id);
50170       FETCH c1 BULK COLLECT INTO lb_rowid;
50171       CLOSE c1;
50172 
50173 
50174      -- Generate the st_transaction_id and update all the records with this st_transaction_id.
50175      -- Set the who columns also (where PROCESS_FLAG = 2 and SR_INSTANCE_CODE = v_instance_code).
50176      -- Sequence : MSC_ST_ITEM_CUSTOMERS_S.
50177 
50178       v_sql_stmt := 07;
50179       FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
50180       UPDATE msc_st_item_customers
50181       SET    st_transaction_id   = MSC_ST_ITEM_CUSTOMERS_S.NEXTVAL,
50182              refresh_id          = v_refresh_id,
50183              last_update_date    = v_current_date,
50184              last_updated_by     = v_current_user,
50185              creation_date       = v_current_date,
50186              created_by          =  v_current_user
50187       WHERE  rowid               = lb_rowid(j);
50188 
50189 
50190      -- Error out those records where the deleted_flag is set to 'Y' if the collection program doesn't support deletion for this BO.
50191      -- Insert the records into the error_table with the severity warning where deleted_flag has a null value.
50192      -- Update the MSC_ST_ITEM_SUPPLIERS table with the default value 2.
50193 
50194       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50195                      (p_app_short_name    => 'MSC',
50196                       p_error_code        => 'MSC_PP_NO_DELETION',
50197                       p_message_text      => lv_message_text,
50198                       p_error_text        => lv_error_text,
50199                       p_token1            => 'TABLE_NAME',
50200                       p_token_value1      => 'MSC_ST_ITEM_CUSTOMERS');
50201 
50202       IF lv_return <> 0 THEN
50203         RAISE ex_logging_err;
50204       END IF;
50205 
50206       -- Deletion is not allowed for this table
50207 
50208       v_sql_stmt := 08;
50209       lv_sql_stmt :=
50210       'UPDATE       MSC_ST_ITEM_CUSTOMERS '
50211       ||' SET      process_flag     = '||G_ERROR_FLG||','
50212       ||'          error_text   = '||''''||lv_message_text||''''
50213       ||' WHERE    deleted_flag     = '||SYS_YES
50214       ||' AND      process_flag     = '||G_IN_PROCESS
50215       ||' AND      batch_id         =  :lv_batch_id'
50216       ||' AND      sr_instance_code = :v_instance_code';
50217 
50218 
50219 
50220         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50221       EXECUTE IMMEDIATE lv_sql_stmt
50222               USING     lv_batch_id,
50223                         v_instance_code;
50224 
50225       -- warning message if the DELETED_FLAG is other than 1, 2. default to SYS_NO (2)
50226 
50227       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50228                      (p_app_short_name    => 'MSC',
50229                       p_error_code        => 'MSC_PP_INVALID_COL_VALUE',
50230                       p_message_text      => lv_message_text,
50231                       p_error_text        => lv_error_text,
50232                       p_token1            => 'COLUMN_NAME',
50233                       p_token_value1      => 'DELETED_FLAG',
50234                       p_token2            => 'DEFAULT_VALUE',
50235                       p_token_value2      => SYS_NO);
50236 
50237       IF lv_return <> 0 THEN
50238         RAISE ex_logging_err;
50239       END IF;
50240 
50241       lv_where_str :=
50242       ' AND NVL(deleted_flag,'||NULL_VALUE||') NOT IN(1,2)';
50243 
50244       --Log a warning for those records where the deleted_flag has a value other
50245       --SYS_NO
50246 
50247       lv_return := MSC_ST_UTIL.LOG_ERROR
50248                      (p_table_name        => 'MSC_ST_ITEM_CUSTOMERS',
50249                       p_instance_code     => v_instance_code,
50250                       p_row               => lv_column_names,
50251                       p_severity          => G_SEV_WARNING,
50252                       p_message_text      => lv_message_text,
50253                       p_error_text        => lv_error_text,
50254                       p_batch_id          => lv_batch_id,
50255                       p_where_str         => lv_where_str,
50256                       p_col_name          => 'DELETED_FLAG',
50257                       p_debug             => v_debug,
50258                       p_default_value     => SYS_NO);
50259 
50260       IF lv_return <> 0 THEN
50261         RAISE ex_logging_err;
50262       END IF;
50263 
50264       -- derive the company_id if it is not null or -1. if not found then error out record
50265       IF v_sce_installed THEN
50266 
50267       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50268                      (p_app_short_name    => 'MSC',
50269                       p_error_code        => 'MSC_PP_INVALID_VALUE',
50270                       p_message_text      => lv_message_text,
50271                       p_error_text        => lv_error_text,
50272                       p_token1            => 'COLUMN_NAME',
50273                       p_token_value1      => 'COMPANY_NAME');
50274 
50275       IF lv_return <> 0 THEN
50276         RAISE ex_logging_err;
50277       END IF;
50278 
50279       lv_return := MSC_ST_UTIL.DERIVE_COMPANY_ID
50280                         (p_table_name     => 'MSC_ST_ITEM_CUSTOMERS',
50281                          p_company_name   => 'COMPANY_NAME',
50282                          p_company_id     => 'COMPANY_ID',
50283                          p_instance_code  => v_instance_code,
50284                          p_error_text     => lv_error_text,
50285                          p_batch_id       => lv_batch_id,
50286                          p_message_text   => lv_message_text,
50287                          p_debug          => v_debug);
50288 
50289       IF lv_return <> 0 THEN
50290         RAISE ex_logging_err;
50291       END IF;
50292 
50293       END IF;
50294 
50295 
50296 /*    Calling derive_company_id
50297       lv_sql_stmt :=
50298       ' UPDATE  MSC_ST_ITEM_CUSTOMERS mic '
50299       ||' SET   company_id        =   '
50300       ||'       (select company_id from msc_companies mc'
50301       ||'        where mic.company_name = mc.company_name) '
50302       ||' WHERE NVL(COMPANY_NAME,''-1'')   <> ''-1'''
50303       ||' AND   batch_id         = :lv_batch_id'
50304       ||' AND   process_flag     = '||G_IN_PROCESS
50305       ||' AND   sr_instance_code = :v_instance_code';
50306 
50307 
50308         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50309 
50310       EXECUTE IMMEDIATE lv_sql_stmt
50311               USING     lv_batch_id,
50312                         v_instance_code;
50313 
50314       lv_sql_stmt :=
50315       ' UPDATE  MSC_ST_ITEM_CUSTOMERS '
50316       ||' SET   process_flag        = '||G_ERROR_FLG||','
50317       ||'       error_text          = '||''''||lv_message_text||''''
50318       ||' WHERE NVL(COMPANY_NAME,''-1'')   <> ''-1'''
50319       ||' AND   COMPANY_ID IS NULL '
50320       ||' AND   batch_id         = :lv_batch_id'
50321       ||' AND   process_flag     = '||G_IN_PROCESS
50322       ||' AND   sr_instance_code = :v_instance_code';
50323 
50324       IF V_DEBUG THEN
50325         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50326       END IF;
50327 
50328       EXECUTE IMMEDIATE lv_sql_stmt
50329               USING     lv_batch_id,
50330                         v_instance_code;
50331 */
50332 
50333       -- error out records where CUSTOMER_ITEM_NAME is null
50334 
50335       v_sql_stmt := 09;
50336 
50337       -- Set the error message
50338 
50339       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50340                      (p_app_short_name    => 'MSC',
50341                       p_error_code        => 'MSC_PP_COL_VAL_NULL',
50342                       p_message_text      => lv_message_text,
50343                       p_error_text        => lv_error_text,
50344                       p_token1            => 'COLUMN_NAME',
50345                       p_token_value1      => 'CUSTOMER_ITEM_NAME');
50346 
50347       IF lv_return <> 0 THEN
50348         RAISE ex_logging_err;
50349       END IF;
50350 
50351       lv_sql_stmt :=
50352       ' UPDATE  MSC_ST_ITEM_CUSTOMERS '
50353       ||' SET   process_flag        = '||G_ERROR_FLG||','
50354       ||'       error_text          = '||''''||lv_message_text||''''
50355       ||' WHERE NVL(customer_item_name,'||''''||NULL_CHAR||''''||')'
50356       ||'                   = '||''''||NULL_CHAR||''''
50357       ||' AND   batch_id         = :lv_batch_id'
50358       ||' AND   process_flag     = '||G_IN_PROCESS
50359       ||' AND   sr_instance_code = :v_instance_code';
50360 
50361 
50362           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50363 
50364        EXECUTE IMMEDIATE lv_sql_stmt
50365               USING     lv_batch_id,
50366                         v_instance_code;
50367 
50368       -- UOM_CODE should refer to a valid record in msc_units_of_measure/msc_st_units_of_measure .
50369       -- Now check for UOM code if it is Invalid error out
50370       -- Set the error message
50371 
50372       v_sql_stmt := 10;
50373 
50374       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50375                      (p_app_short_name    => 'MSC',
50376                       p_error_code        => 'MSC_PP_COL_REF_NOT_EXIST',
50377                       p_message_text      => lv_message_text,
50378                       p_error_text        => lv_error_text,
50379                       p_token1            => 'COLUMN_NAMES',
50380                       p_token_value1      => 'SR_INSTANCE_CODE ,UOM_CODE',
50381                       p_token2            => 'MASTER_TABLE',
50382                       p_token_value2      => 'MSC_ST_UNITS_OF_MEASURE',
50383                       p_token3            => 'CHILD_TABLE',
50384                       p_token_value3      => 'MSC_ST_ITEM_CUSTOMERS');
50385       IF lv_return <> 0 THEN
50386           RAISE ex_logging_err;
50387       END IF;
50388 
50389 
50390       lv_sql_stmt :=
50391       '   UPDATE      MSC_ST_ITEM_CUSTOMERS mic'
50392       ||' SET         process_flag = '||G_ERROR_FLG||','
50393       ||'             error_text   = '||''''||lv_message_text||''''
50394       ||' WHERE       NOT EXISTS (SELECT 1 '
50395       ||'             FROM msc_units_of_measure muom'
50396       ||'             WHERE muom.uom_code       = mic.uom_code'
50397       ||'             UNION'
50398       ||'             SELECT 1 FROM msc_st_units_of_measure msuom'
50399       ||'             WHERE msuom.uom_code       = mic.uom_code'
50400       ||'             AND   msuom.sr_instance_id = :v_instance_id'
50401       ||'             AND   msuom.process_flag   = '||G_VALID||')'
50402       ||' AND mic.sr_instance_code   = :v_instance_code'
50403       ||' AND mic.batch_id           = :lv_batch_id'
50404       ||' AND mic.process_flag       = '||G_IN_PROCESS ;
50405 
50406 
50407            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50408 
50409       EXECUTE IMMEDIATE lv_sql_stmt
50410             USING     v_instance_id,
50411                       v_instance_code,
50412                       lv_batch_id;
50413 
50414 
50415       -- Derive inventory_item_id based on the item_name and error_out all the records where the derivation has failed.
50416 
50417       v_sql_stmt := 11;
50418 
50419       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50420                      (p_app_short_name    => 'MSC',
50421                       p_error_code        => 'MSC_PP_INVALID_VALUE',
50422                       p_message_text      => lv_message_text,
50423                       p_error_text        => lv_error_text,
50424                       p_token1            => 'COLUMN_NAME',
50425                       p_token_value1      => 'ITEM_NAME');
50426 
50427       --Derive Inventory_item_id
50428 
50429       lv_return := MSC_ST_UTIL.DERIVE_ITEM_ID
50430                      (p_table_name       => 'MSC_ST_ITEM_CUSTOMERS',
50431                       p_item_col_name    => 'ITEM_NAME',
50432                       p_item_col_id      => 'INVENTORY_ITEM_ID',
50433                       p_instance_id      => v_instance_id,
50434                       p_instance_code    => v_instance_code,
50435                       p_error_text       => lv_error_text,
50436                       p_batch_id         => lv_batch_id,
50437                       p_severity         => G_SEV_ERROR,
50438                       p_message_text     => lv_message_text,
50439                       p_debug            => v_debug,
50440                       p_row              => lv_column_names,
50441                       p_check_org        => FALSE);
50442 
50443       IF lv_return <> 0 THEN
50444         RAISE ex_logging_err;
50445       END IF;
50446 
50447       -- check customer exists in msc_trading_partners
50448       -- Set the  message
50449 
50450       v_sql_stmt := 12;
50451 
50452       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50453                      (p_app_short_name    => 'MSC',
50454                       p_error_code        => 'MSC_PP_INVALID_VALUE',
50455                       p_message_text      => lv_message_text,
50456                       p_error_text        => lv_error_text,
50457                       p_token1            => 'COLUMN_NAME',
50458                       p_token_value1      => 'CUSTOMER_NAME');
50459 
50460       IF lv_return <> 0 THEN
50461         RAISE ex_logging_err;
50462       END IF;
50463 
50464      -- Derive customer_id and error out records if customer is not valid
50465 
50466      lv_return := MSC_ST_UTIL.DERIVE_PARTNER_ORG_ID
50467                      (p_table_name       => 'MSC_ST_ITEM_CUSTOMERS',
50468                       p_org_partner_name => 'CUSTOMER_NAME',
50469                       p_org_partner_id   => 'CUSTOMER_ID',
50470                       p_instance_code    => v_instance_code,
50471                       p_partner_type     => G_CUSTOMER,
50472                       p_error_text       => lv_error_text,
50473                       p_batch_id         => lv_batch_id,
50474                       p_severity         => G_SEV_ERROR,
50475                       p_message_text     => lv_message_text,
50476                       p_debug            => v_debug,
50477                       p_row              => lv_column_names);
50478 
50479       IF lv_return <> 0 THEN
50480         RAISE ex_logging_err;
50481       END IF;
50482 
50483 
50484       -- Check for the valid CUSTOMER_SITE_NAME and derive the CUSTOMER_SITE_ID.
50485       -- Error out those records for which the CUSTOMER_SITE_ID derivation has failed.
50486 
50487 
50488       v_sql_stmt := 13;
50489 
50490       -- Set the  message
50491 
50492       lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50493                      (p_app_short_name    => 'MSC',
50494                       p_error_code        => 'MSC_PP_INVALID_VALUE',
50495                       p_message_text      => lv_message_text,
50496                       p_error_text        => lv_error_text,
50497                       p_token1            => 'COLUMN_NAME',
50498                       p_token_value1      => 'CUSTOMER_SITE_NAME');
50499 
50500       IF lv_return <> 0 THEN
50501         RAISE ex_logging_err;
50502       END IF;
50503 
50504       lv_return := MSC_ST_UTIL.DERIVE_PARTNER_SITE_ID
50505                     (p_table_name        => 'MSC_ST_ITEM_CUSTOMERS',
50506                     p_partner_name      => 'CUSTOMER_NAME',
50507                     p_partner_site_code => 'CUSTOMER_SITE_NAME',
50508                     p_partner_site_id   => 'CUSTOMER_SITE_ID',
50509                     p_instance_code     => v_instance_code,
50510                     p_partner_type      => G_CUSTOMER,
50511                     p_error_text        => lv_error_text,
50512                     p_batch_id          => lv_batch_id,
50513                     p_severity          => G_SEV3_ERROR,
50514                     p_message_text      => lv_message_text,
50515                     p_debug             => v_debug,
50516                     p_row               => lv_column_names);
50517 
50518 
50519       IF lv_return <> 0 THEN
50520         RAISE ex_logging_err;
50521       END IF;
50522 
50523 
50524       --  Call the HOOK to enable the customised validations if there is any.
50525       --  Call to customised validation.
50526       MSC_CL_PRE_PROCESS_HOOK.ENTITY_VALIDATION
50527         (ERRBUF         => lv_error_text,
50528          RETCODE        => lv_return,
50529          pBatchID       => lv_batch_id,
50530          pInstanceCode  => v_instance_code,
50531          pEntityName    => 'MSC_ST_ITEM_CUSTOMERS',
50532          pInstanceID    => v_instance_id);
50533 
50534       IF NVL(lv_return,0) <> 0 THEN
50535         RAISE ex_logging_err;
50536       END IF;
50537 
50538 
50539     -- Update the process_flag to 5(valid) and sr_instance_id to v_instance_id.
50540        lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
50541                      (p_table_name     => 'MSC_ST_ITEM_CUSTOMERS',
50542                       p_instance_id    => v_instance_id,
50543                       p_instance_code  => v_instance_code,
50544                       p_process_flag   => G_VALID,
50545                       p_error_text     => lv_error_text,
50546                       p_debug          => v_debug,
50547                       p_batch_id       => lv_batch_id);
50548 
50549        IF lv_return <> 0 THEN
50550           RAISE ex_logging_err;
50551        END IF;
50552 
50553     -- Insert all the errored out records into the error table.
50554        lv_return := MSC_ST_UTIL.LOG_ERROR
50555                      (p_table_name        => 'MSC_ST_ITEM_CUSTOMERS',
50556                       p_instance_code     => v_instance_code,
50557                       p_row               => lv_column_names,
50558                       p_severity          => G_SEV_ERROR,
50559                       p_message_text      => NULL,
50560                       p_error_text        => lv_error_text,
50561                       p_debug             => v_debug,
50562                       p_batch_id          => lv_batch_id);
50563        IF lv_return <> 0 THEN
50564          RAISE ex_logging_err;
50565        END IF;
50566 
50567 
50568      COMMIT;
50569 
50570   END LOOP;
50571 
50572   EXCEPTION
50573     WHEN ex_logging_err THEN
50574       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
50575       ROLLBACK;
50576 
50577     WHEN OTHERS THEN
50578       lv_error_text    := substr('MSC_CL_PRE_PROCESS.LOAD_DESIGNATOR'||'('
50579                        ||v_sql_stmt||')'|| SQLERRM, 1, 240);
50580       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
50581       ROLLBACK;
50582 
50583   END LOAD_ITEM_CUSTOMERS;
50584 
50585 
50586   ----- ============== SET_IN_PROCESS ====================
50587 
50588   FUNCTION SET_IN_PROCESS (p_table_name       VARCHAR2,
50589                            p_instance_id_col  VARCHAR2 DEFAULT 'SR_INSTANCE_ID',
50590                            p_company_name_col BOOLEAN  DEFAULT TRUE ,
50591                            p_where_str        VARCHAR2 DEFAULT NULL,
50592                            p_erp_enabled      VARCHAR2 DEFAULT 'N')
50593 
50594   RETURN NUMBER
50595   IS
50596       lv_sql_stmt VARCHAR2(1000);
50597       lv_error_text VARCHAR2(1000);
50598       ln_count NUMBER := 0;
50599       lv_message_text   msc_errors.error_text%TYPE;
50600       lv_return         NUMBER;
50601       lv_column_names   VARCHAR2(100);
50602       ex_logging_err    EXCEPTION;
50603 
50604   BEGIN
50605 
50606       IF (v_instance_type <> G_INS_OTHER AND p_erp_enabled = 'N') THEN
50607         lv_column_names := 'SR_INSTANCE_CODE';
50608 
50609         lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
50610                          (p_app_short_name    => 'MSC',
50611                           p_error_code        => 'MSC_ERP_INVALID_ENTITY',
50612                           p_message_text      => lv_message_text,
50613                           p_error_text        => lv_error_text,
50614                           p_token1            => 'TABLE_NAME',
50615                           p_token_value1      => p_table_name);
50616 
50617 
50618           IF lv_return <> 0 THEN
50619             RAISE ex_logging_err;
50620           END IF;
50621 
50622         lv_sql_stmt :=
50623         'UPDATE '||p_table_name
50624         ||' SET   error_text ='||''''||lv_message_text||''''||','
50625         ||'       process_flag = '||G_ERROR_FLG
50626         ||' WHERE sr_instance_code       = ' ||''''||v_instance_code||''''
50627         ||' AND   process_flag           = ' ||G_NEW
50628         ||p_where_str;
50629 
50630 
50631            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50632 
50633        EXECUTE IMMEDIATE lv_sql_stmt;
50634 
50635           lv_return := MSC_ST_UTIL.LOG_ERROR
50636                         (p_table_name       => p_table_name,
50637                         p_instance_code     => v_instance_code,
50638                         p_row               => lv_column_names,
50639                         p_severity          => G_SEV_ERROR,
50640                         p_message_text      => NULL,
50641                         p_error_text        => lv_error_text,
50642                         p_debug             => v_debug);
50643 
50644 
50645          IF lv_return <> 0 THEN
50646            RAISE ex_logging_err;
50647          END IF;
50648 
50649          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_message_text);
50650 
50651          RETURN (0);
50652 
50653      END IF;
50654 
50655 
50656       IF p_company_name_col THEN
50657       lv_sql_stmt := 'UPDATE '||p_table_name
50658                    ||' SET    process_flag         = ' ||G_IN_PROCESS  ||','
50659                    ||         p_instance_id_col||' = 0'                ||','
50660                    ||'        company_name = NVL(company_name,:v_my_company) ,'
50661                    ||'        REQUEST_ID        = :request_id   ,'
50662                    ||'        LAST_UPDATE_DATE  = :current_date ,'
50663                    ||'        LAST_UPDATED_BY   = :current_user '
50664                    ||' WHERE  nvl(process_flag,'||G_NEW ||') = '||G_NEW
50665                    ||' AND    sr_instance_code = :instance_code '
50666                    ||p_where_str;
50667 
50668      -- OA Self Service load changes
50669      -- If request id is not -1 or null then pre-processor is called from OA
50670 
50671       IF nvl(v_parent_request_id,-1) > 0 THEN
50672          lv_sql_stmt := lv_sql_stmt ||
50673                         ' AND REQUEST_ID = :parent_request_id ';
50674       ELSIF nvl(v_parent_request_id,-1) = -1 THEN
50675          lv_sql_stmt := lv_sql_stmt ||
50676                         ' AND nvl(REQUEST_ID,-1) = -1 ';
50677       END IF;
50678 
50679 
50680          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_sql_stmt);
50681 
50682       IF nvl(v_parent_request_id,-1) > 0 THEN
50683          EXECUTE IMMEDIATE lv_sql_stmt USING v_my_company,v_request_id,v_current_date, v_current_user, v_instance_code, v_parent_request_id;
50684       ELSE
50685          EXECUTE IMMEDIATE lv_sql_stmt USING v_my_company,v_request_id,v_current_date, v_current_user, v_instance_code;
50686       END IF;
50687 
50688 
50689       ELSE
50690       lv_sql_stmt := 'UPDATE '||p_table_name
50691                    ||' SET    process_flag         = ' ||G_IN_PROCESS  ||','
50692                    ||         p_instance_id_col||' = 0'                ||','
50693                    ||'        REQUEST_ID        = :request_id   ,'
50694                    ||'        LAST_UPDATE_DATE  = :current_date ,'
50695                    ||'        LAST_UPDATED_BY   = :current_user '
50696                    ||' WHERE  nvl(process_flag,'||G_NEW ||') = '||G_NEW
50697                    ||' AND    sr_instance_code = :instance_code '
50698                    ||p_where_str;
50699 
50700      -- OA Self Service load changes
50701      -- If request id is not -1 or null then pre-processor is called from OA
50702 
50703       IF nvl(v_parent_request_id,-1) > 0 THEN
50704          lv_sql_stmt := lv_sql_stmt ||
50705                         ' AND REQUEST_ID = :parent_request_id ';
50706       ELSIF nvl(v_parent_request_id,-1) = -1 THEN
50707          lv_sql_stmt := lv_sql_stmt ||
50708                         ' AND nvl(REQUEST_ID,-1) = -1 ';
50709       END IF;
50710 
50711 
50712          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,lv_sql_stmt);
50713 
50714       IF nvl(v_parent_request_id,-1) > 0 THEN
50715          EXECUTE IMMEDIATE lv_sql_stmt USING v_request_id,v_current_date, v_current_user, v_instance_code, v_parent_request_id;
50716       ELSE
50717          EXECUTE IMMEDIATE lv_sql_stmt USING v_request_id,v_current_date, v_current_user, v_instance_code;
50718       END IF;
50719 
50720       END IF;
50721 
50722       ln_count := ln_count + SQL%ROWCOUNT;
50723 
50724       COMMIT;
50725 
50726       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Table Name : '||p_table_name||' - No. of Records : '||ln_count);
50727 
50728       RETURN nvl(ln_count,0);
50729 
50730   EXCEPTION
50731 
50732     WHEN ex_logging_err THEN
50733       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
50734       ROLLBACK;
50735       RETURN 0;
50736 
50737     WHEN OTHERS THEN
50738 	lv_error_text := substr('MSC_CL_PRE_PROCESS.SET_IN_PROCESS- '||p_table_name||': ('||v_sql_stmt||')'|| SQLERRM, 1, 240);
50739 	MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
50740       ROLLBACK;
50741       RETURN 0;
50742   END SET_IN_PROCESS ;
50743 
50744   -----==============Divide DP entities in batches and send task to queue-----=================
50745 
50746     PROCEDURE  DIVIDE_BATCHES(p_table_name        VARCHAR2,
50747                               p_task_name         NUMBER,
50748                               p_task_not_completed  OUT NOCOPY NUMBER,
50749                               p_pipe_status         OUT NOCOPY NUMBER )
50750     IS
50751 
50752     TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
50753     lb_rowid       RowidTab;
50754     lv_batch_id    PLS_INTEGER;
50755     lv_sql_stmt    VARCHAR2(4000);
50756     lv_task_not_completed  NUMBER := 0;
50757     lv_message    VARCHAR2(240);
50758     EX_PIPE_SND   EXCEPTION ;
50759 
50760     BEGIN
50761         LOOP
50762            SELECT       msc_st_batch_id_s.NEXTVAL
50763            INTO         lv_batch_id
50764            FROM         DUAL;
50765 
50766            v_sql_stmt := 01;
50767            lv_sql_stmt :=
50768            'UPDATE   '||p_table_name
50769            ||' SET   batch_id                       = :lv_batch_id '
50770            ||' WHERE process_flag  IN ('||G_IN_PROCESS||','||G_ERROR_FLG||')'
50771            ||' AND   sr_instance_code               = :v_instance_code'
50772            ||' AND   NVL(batch_id,'||NULL_VALUE||') = '||NULL_VALUE
50773            ||' AND    rownum                        <= '||v_batch_size;
50774 
50775                MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
50776 
50777            EXECUTE IMMEDIATE lv_sql_stmt
50778                    USING     lv_batch_id,
50779                              v_instance_code;
50780 
50781 
50782        EXIT WHEN SQL%NOTFOUND ;
50783        commit;
50784 
50785           lv_message := to_char(p_task_name)||','||to_char(lv_batch_id);
50786           DBMS_PIPE.PACK_MESSAGE(lv_message);
50787             IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
50788                  RAISE EX_PIPE_SND;
50789             END IF;
50790           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_task_name: '||lv_message);
50791           lv_task_not_completed := lv_task_not_completed + 1;
50792 
50793 
50794       END LOOP;
50795          p_task_not_completed :=lv_task_not_completed ;
50796        EXCEPTION
50797          WHEN EX_PIPE_SND THEN
50798             p_pipe_status := 1;
50799             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error in Sending message in Pipe');
50800          WHEN OTHERS THEN
50801            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error in Batch ID generation: ' || SQLERRM);
50802            lv_message := to_char(p_task_name)||','||to_char(0);
50803            DBMS_PIPE.PACK_MESSAGE(lv_message );
50804            IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
50805                 RAISE EX_PIPE_SND ;
50806            END IF;
50807            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'p_task_name: '||lv_message);
50808            lv_task_not_completed := lv_task_not_completed + 1;
50809            p_task_not_completed :=lv_task_not_completed ;
50810 
50811 
50812     END DIVIDE_BATCHES;
50813 
50814 
50815 
50816   -- ==============SNAPSHOT_TABLES ============
50817   -- Snapshot is done by setting the process flag
50818   -- of all the tables to G_IN_PROCESS
50819 
50820   FUNCTION SNAPSHOT_TABLES RETURN BOOLEAN IS
50821       lv_error_text VARCHAR2(1000);
50822       lv_count       PLS_INTEGER;
50823   BEGIN
50824 	--- ===== Enter the ASCP tables to be set here ======
50825 
50826     IF v_mat_dmd_enabled = SYS_YES OR v_iro_enabled = SYS_YES or v_ero_enabled = SYS_YES THEN
50827       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_DEMANDS', p_erp_enabled => 'Y');
50828       IF lv_count > 0 Then
50829          prec.mds_flag:= SYS_YES;
50830          prec.forecast_flag:= SYS_YES;
50831          prec.wip_flag:= SYS_YES;
50832          prec.internal_repair_flag:=SYS_YES;
50833          prec.external_repair_flag:=SYS_YES;
50834       End IF;
50835       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_SALES_ORDERS ',p_erp_enabled => 'Y');
50836       IF lv_count > 0 Then
50837          prec.sales_order_flag:= SYS_YES;
50838       End IF;
50839     END IF;
50840 
50841     IF v_reserve_enabled = SYS_YES THEN
50842       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_RESERVATIONS');
50843       IF lv_count > 0 Then
50844          prec.reserves_flag:= SYS_YES;
50845       End IF;
50846     END IF;
50847 
50848     IF v_mat_sup_enabled = SYS_YES OR v_iro_enabled = SYS_YES or v_ero_enabled = SYS_YES
50849      THEN
50850       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_SUPPLIES',p_erp_enabled => 'Y');
50851       IF lv_count > 0 Then
50852          prec.oh_flag:= SYS_YES;
50853          prec.po_flag:= SYS_YES;
50854          prec.mps_flag:= SYS_YES;
50855          prec.wip_flag:= SYS_YES;
50856          prec.internal_repair_flag:=SYS_YES;
50857          prec.external_repair_flag:=SYS_YES;
50858       End IF;
50859 
50860       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_JOB_OPERATION_NETWORKS');
50861       IF lv_count > 0 Then
50862        prec.wip_flag:= SYS_YES;
50863       End IF;
50864       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_JOB_OPERATIONS');
50865       IF lv_count > 0 Then
50866        prec.wip_flag:= SYS_YES;
50867       End IF;
50868       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_JOB_REQUIREMENT_OPS');
50869       IF lv_count > 0 Then
50870        prec.wip_flag:= SYS_YES;
50871       End IF;
50872       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_JOB_OP_RESOURCES');
50873       IF lv_count > 0 Then
50874        prec.wip_flag:= SYS_YES;
50875       End IF;
50876    END IF;
50877 
50878     IF v_desig_enabled = SYS_YES THEN
50879       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_DESIGNATORS',p_erp_enabled => 'Y');
50880       IF lv_count > 0 Then
50881          prec.mds_flag:= SYS_YES;
50882          prec.forecast_flag:= SYS_YES;
50883          prec.mps_flag:= SYS_YES;
50884       End IF;
50885     END IF;
50886 
50887     IF v_sourcing_enabled = SYS_YES THEN
50888       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_ITEM_SOURCING');
50889       IF lv_count > 0 Then
50890          prec.sourcing_rule_flag:= SYS_YES;
50891       End IF;
50892       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_REGIONS');
50893       IF lv_count > 0 Then
50894          prec.sourcing_rule_flag:= SYS_YES;
50895       End IF;
50896       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_ZONE_REGIONS');
50897       IF lv_count > 0 Then
50898          prec.sourcing_rule_flag:= SYS_YES;
50899       End IF;
50900       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_REGION_SITES');
50901       IF lv_count > 0 Then
50902          prec.sourcing_rule_flag:= SYS_YES;
50903       End IF;
50904       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_REGION_LOCATIONS');
50905       IF lv_count > 0 Then
50906          prec.sourcing_rule_flag:= SYS_YES;
50907       End IF;
50908     END IF;
50909 
50910    IF v_ship_mthd_enabled = SYS_YES THEN
50911      lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_INTERORG_SHIP_METHODS');
50912       IF lv_count > 0 Then
50913          prec.sourcing_rule_flag:= SYS_YES;
50914       End IF;
50915    END IF;
50916 
50917     IF v_bom_enabled = SYS_YES THEN
50918       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_COMPONENT_SUBSTITUTES');
50919       IF lv_count > 0 Then
50920          prec.bom_flag:= SYS_YES;
50921       End IF;
50922       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_BOM_COMPONENTS');
50923       IF lv_count > 0 Then
50924          prec.bom_flag:= SYS_YES;
50925       End IF;
50926       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_BOMS');
50927       IF lv_count > 0 Then
50928          prec.bom_flag:= SYS_YES;
50929       End IF;
50930       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_CO_PRODUCTS');
50931       IF lv_count > 0 Then
50932          prec.bom_flag:= SYS_YES;
50933       End IF;
50934     END IF;
50935 
50936     IF v_cal_enabled = SYS_YES THEN
50937       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_CALENDARS');
50938       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_WORKDAY_PATTERNS');
50939       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_CALENDAR_SHIFTS');
50940       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_SHIFT_TIMES');
50941       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_SHIFT_EXCEPTIONS');
50942       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_CALENDAR_EXCEPTIONS');
50943     END IF;
50944 
50945     IF v_rtg_enabled = SYS_YES THEN
50946       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_OPERATION_RESOURCES');
50947       IF lv_count > 0 Then
50948          prec.bom_flag:= SYS_YES;
50949       End IF;
50950       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_ROUTING_OPERATIONS');
50951       IF lv_count > 0 Then
50952          prec.bom_flag:= SYS_YES;
50953       End IF;
50954       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_ROUTINGS');
50955       IF lv_count > 0 Then
50956          prec.bom_flag:= SYS_YES;
50957       End IF;
50958       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_OPERATION_NETWORKS');
50959       IF lv_count > 0 Then
50960          prec.bom_flag:= SYS_YES;
50961       End IF;
50962     END IF;
50963 
50964     IF v_res_dmd_enabled = SYS_YES THEN
50965       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_RESOURCE_REQUIREMENTS',p_erp_enabled => 'Y'); /* bug 3768813 */
50966       IF lv_count > 0 Then
50967          prec.wip_flag:= SYS_YES;
50968       End IF;
50969     END IF;
50970 
50971     IF v_resources_enabled = SYS_YES THEN
50972       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_RESOURCE_GROUPS');
50973       IF lv_count > 0 Then
50974          prec.bom_flag:= SYS_YES;
50975       End IF;
50976       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_DEPARTMENT_RESOURCES');
50977       IF lv_count > 0 Then
50978          prec.bom_flag:= SYS_YES;
50979       End IF;
50980       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_RESOURCE_SHIFTS');
50981       IF lv_count > 0 Then
50982          prec.bom_flag:= SYS_YES;
50983          prec.calendar_flag:=SYS_YES;
50984       End IF;
50985       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_RESOURCE_CHANGES');
50986       IF lv_count > 0 Then
50987          prec.bom_flag:= SYS_YES;
50988          prec.calendar_flag:=SYS_YES;
50989       End IF;
50990     END IF;
50991 
50992     IF v_tp_enabled = SYS_YES THEN
50993       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_LOCATION_ASSOCIATIONS');
50994       IF lv_count > 0 Then
50995          prec.tp_customer_flag:= SYS_YES;
50996          prec.tp_vendor_flag:= SYS_YES;
50997       End IF;
50998       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_TRADING_PARTNER_SITES');
50999       IF lv_count > 0 Then
51000          prec.tp_customer_flag:= SYS_YES;
51001          prec.tp_vendor_flag:= SYS_YES;
51002       End IF;
51003       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_PARTNER_CONTACTS');
51004       IF lv_count > 0 Then
51005          prec.tp_customer_flag:= SYS_YES;
51006          prec.tp_vendor_flag:= SYS_YES;
51007       End IF;
51008       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_TRADING_PARTNERS');
51009       IF lv_count > 0 Then
51010          prec.tp_customer_flag:= SYS_YES;
51011          prec.tp_vendor_flag:= SYS_YES;
51012          prec.sourcing_rule_flag:= SYS_YES;
51013       End IF;
51014       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_SUB_INVENTORIES');
51015       IF lv_count > 0 Then
51016          prec.sub_inventory_flag:= SYS_YES;
51017       End IF;
51018       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_GROUPS',p_company_name_col => FALSE);
51019       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_GROUP_COMPANIES');
51020     END IF;
51021 
51022     IF v_dmd_class_enabled = SYS_YES THEN
51023       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_DEMAND_CLASSES');
51024       IF lv_count > 0 Then
51025          prec.demand_class_flag:= SYS_YES;
51026       End IF;
51027     END IF;
51028 
51029     IF v_sup_cap_enabled = SYS_YES THEN
51030       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_ITEM_SUPPLIERS');
51031       IF lv_count > 0 Then
51032          prec.app_supp_cap_flag:= SYS_YES;
51033       End IF;
51034       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_SUPPLIER_CAPACITIES', p_erp_enabled => 'Y');
51035       IF lv_count > 0 Then
51036          prec.app_supp_cap_flag:= SYS_YES;
51037       End IF;
51038       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_SUPPLIER_FLEX_FENCES', p_erp_enabled => 'Y');
51039       IF lv_count > 0 Then
51040          prec.app_supp_cap_flag:= SYS_YES;
51041       End IF;
51042     END IF;
51043 
51044     IF v_item_cst_enabled = SYS_YES THEN
51045       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_ITEM_CUSTOMERS');
51046     END IF;
51047 
51048     IF v_safety_stk_enabled = SYS_YES THEN
51049       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_SAFETY_STOCKS');
51050       IF lv_count > 0 Then
51051          prec.saf_stock_flag:= SYS_YES;
51052       End IF;
51053     END IF;
51054 
51055     IF v_project_enabled = SYS_YES THEN
51056       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_PROJECT_TASKS');
51057       IF lv_count > 0 Then
51058          prec.project_flag:= SYS_YES;
51059       End IF;
51060     END IF;
51061 
51062     IF v_uom_enabled = SYS_YES THEN
51063       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_UNITS_OF_MEASURE');
51064       IF lv_count > 0 Then
51065          prec.uom_flag:= SYS_YES;
51066       End IF;
51067     END IF;
51068 
51069     IF v_uom_class_enabled = SYS_YES THEN
51070       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_UOM_CLASS_CONVERSIONS');
51071       IF lv_count > 0 Then
51072          prec.uom_flag:= SYS_YES;
51073       End IF;
51074       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_UOM_CONVERSIONS');
51075       IF lv_count > 0 Then
51076          prec.uom_flag:= SYS_YES;
51077       End IF;
51078     END IF;
51079 
51080     IF v_ctg_enabled = SYS_YES THEN
51081       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_CATEGORY_SETS');
51082       IF lv_count > 0 Then
51083          prec.item_flag:= SYS_YES;
51084       End IF;
51085     END IF;
51086 
51087     IF v_item_cat_enabled = SYS_YES THEN
51088       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_ITEM_CATEGORIES');
51089       IF lv_count > 0 Then
51090          prec.item_flag:= SYS_YES;
51091       End IF;
51092     END IF;
51093 
51094     IF v_item_enabled  = SYS_YES THEN
51095       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_SYSTEM_ITEMS');
51096       IF lv_count > 0 Then
51097          prec.item_flag:= SYS_YES;
51098       End IF;
51099     END IF;
51100 
51101     IF v_comp_users_enabled = SYS_YES THEN
51102       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_COMPANY_USERS');
51103       IF lv_count > 0 Then
51104          prec.user_company_flag:= G_COMP_USR_YES;
51105       End IF;
51106     END IF;
51107 
51108     -- Product Item Substitute
51109 
51110     IF v_item_substitute_enabled  = SYS_YES THEN
51111       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_ITEM_SUBSTITUTES');
51112       IF lv_count > 0 Then
51113          prec.item_subst_flag:= SYS_YES;
51114       End IF;
51115     END IF;
51116 
51117     IF v_planners_enabled  = SYS_YES THEN
51118       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_PLANNERS');
51119       IF lv_count > 0 Then
51120          prec.planner_flag:= SYS_YES;
51121       End IF;
51122     END IF;
51123 
51124     -- Profile Values
51125 
51126     IF v_profile_enabled  = SYS_YES THEN
51127       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_APPS_INSTANCES', p_company_name_col => FALSE);
51128     END IF;
51129 
51130     IF v_cal_assignment_enabled  = SYS_YES THEN
51131       lv_count:= SET_IN_PROCESS (p_table_name => 'MSC_ST_CALENDAR_ASSIGNMENTS');
51132       IF lv_count > 0 Then
51133          prec.calendar_flag:=SYS_YES;
51134       End IF;
51135     END IF;
51136 
51137 
51138 
51139 	--- ===== Enter the DP tables to be set here ======
51140 
51141     IF v_fiscal_cal_enabled  = SYS_YES THEN
51142      lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_TIME',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE, p_where_str => ' AND CALENDAR_TYPE <> '||G_COMPOSITE_CAL );
51143       IF lv_count = 0 Then
51144          v_fiscal_cal_enabled:= SYS_NO;
51145       End IF;
51146     END IF;
51147 
51148 
51149     IF v_comp_cal_enabled  = SYS_YES THEN
51150      lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_TIME',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE,p_where_str => ' AND CALENDAR_TYPE <> '||G_FISCAL_CAL, p_erp_enabled => 'Y' );
51151       IF lv_count = 0 Then
51152          v_comp_cal_enabled:= SYS_NO;
51153       End IF;
51154     END IF;
51155 
51156 
51157 
51158     IF v_setup_enabled  = SYS_YES THEN
51159       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_SETUP_PARAMETERS',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE,p_erp_enabled => 'Y');
51160       IF lv_count = 0 Then
51161          v_setup_enabled:= SYS_NO;
51162       End IF;
51163     END IF;
51164 
51165     IF v_level_value_enabled  = SYS_YES THEN
51166       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_LEVEL_VALUES',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE, p_erp_enabled => 'Y');
51167       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_LEVEL_ORG_ASSCNS',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE, p_erp_enabled => 'Y');
51168       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_ITEM_RELATIONSHIPS',p_instance_id_col => 'INSTANCE_ID',p_company_name_col => FALSE, p_erp_enabled => 'Y');
51169      /*
51170       IF lv_count = 0 Then
51171          v_level_value_enabled:= SYS_NO;
51172       End IF;
51173      */
51174     END IF;
51175 
51176     IF v_level_assoc_enabled  = SYS_YES THEN
51177       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_LEVEL_ASSOCIATIONS',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE, p_erp_enabled => 'Y');
51178     END IF;
51179 
51180     IF v_booking_enabled  = SYS_YES THEN
51181       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_BOOKING_DATA',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE, p_erp_enabled => 'Y');
51182       IF lv_count = 0 Then
51183          v_booking_enabled:= SYS_NO;
51184       End IF;
51185     END IF;
51186 
51187     IF v_shipment_enabled  = SYS_YES THEN
51188        lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_SHIPMENT_DATA',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE, p_erp_enabled => 'Y');
51189        IF lv_count = 0 Then
51190          v_shipment_enabled:= SYS_NO;
51191        End IF;
51192     END IF;
51193 
51194     IF v_mfg_fct_enabled  = SYS_YES THEN
51195       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_MFG_FORECAST',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE, p_erp_enabled => 'Y');
51196       IF lv_count = 0 Then
51197          v_mfg_fct_enabled:= SYS_NO;
51198       End IF;
51199     END IF;
51200 
51201     IF v_list_price_enabled  = SYS_YES THEN
51202       -- lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_ITEM_LIST_PRICE',p_instance_id_col => 'INSTANCE');
51203       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_PRICE_LIST',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE, p_erp_enabled => 'Y');
51204       IF lv_count = 0 Then
51205          v_list_price_enabled:= SYS_NO;
51206       End IF;
51207     END IF;
51208 
51209     IF v_cs_data_enabled  = SYS_YES THEN
51210       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_CS_DATA',p_instance_id_col => 'ATTRIBUTE_1',p_company_name_col => FALSE, p_erp_enabled => 'Y');
51211       IF lv_count = 0 Then
51212          v_cs_data_enabled:= SYS_NO;
51213       End IF;
51214     END IF;
51215 
51216     IF v_curr_conv_enabled  = SYS_YES THEN
51217       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_CURRENCY_CONVERSIONS',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE);
51218       IF lv_count = 0 Then
51219          v_curr_conv_enabled:= SYS_NO;
51220       End IF;
51221     END IF;
51222 
51223     IF v_uom_conv_enabled  = SYS_YES THEN
51224       lv_count:= SET_IN_PROCESS (p_table_name => 'MSD_ST_UOM_CONVERSIONS',p_instance_id_col => 'INSTANCE',p_company_name_col => FALSE, p_erp_enabled => 'Y'); --Enabling DP UOM conversions for ERP Instances -  Bug 3752937
51225       IF lv_count = 0 Then
51226          v_uom_conv_enabled:= SYS_NO;
51227       End IF;
51228     END IF;
51229 
51230 
51231 
51232       --- ===== END entering the tables here =========
51233       COMMIT;                     --  close current transaction
51234       RETURN (TRUE);
51235 
51236   EXCEPTION
51237       WHEN OTHERS THEN
51238 	lv_error_text := substr('MSC_CL_PRE_PROCESS.SNAPSHOT_TABLES - '|| SQLERRM, 1, 240);
51239 	MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,lv_error_text);
51240 	return(FALSE);
51241 
51242   END SNAPSHOT_TABLES ;
51243 
51244    -- ========= EXECUTE_TASK ==========
51245 
51246    PROCEDURE EXECUTE_TASK( p_status                   OUT NOCOPY NUMBER,
51247                            p_tasknum                  IN  NUMBER,
51248                            p_batch_id                 IN  NUMBER DEFAULT -1 )
51249 
51250    IS
51251 
51252    lv_task_start_time DATE;
51253 
51254    BEGIN
51255 
51256          lv_task_start_time:= SYSDATE;
51257 
51258          SAVEPOINT ExecuteTask;
51259 
51260          p_status := FAIL;
51261 
51262    -- ============= Execute the task according to its task number  ===========
51263 
51264          IF p_tasknum= TASK_SUPPLIER_CAPACITY THEN
51265 
51266             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51267             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_SUPPLIER_CAPACITY');
51268             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51269             LOAD_SUPPLIER_CAPACITY;
51270 
51271          ELSIF p_tasknum= TASK_SAFETY_STOCK THEN
51272 
51273             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51274             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_SAFETY_STOCKS');
51275             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51276             LOAD_SAFETY_STOCK;
51277 
51278          ELSIF p_tasknum= TASK_BIS_PERIODS THEN
51279 
51280             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51281             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_BIS_PERIODS');
51282             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51283             LOAD_BIS_PERIODS;
51284 
51285          ELSIF p_tasknum= TASK_UOM_CONV THEN
51286 
51287             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51288             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_UOM_CLASS_CONV');
51289             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51290             LOAD_UOM_CLASS_CONV;
51291 
51292          ELSIF p_tasknum= TASK_ITEM THEN
51293 
51294             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51295             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_ITEM');
51296             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51297             LOAD_ITEM(p_batch_id);
51298 
51299          -- Product Item Substitute
51300 
51301          ELSIF p_tasknum= TASK_ITEM_SUBSTITUTE THEN
51302             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51303             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_ITEM_SUBSTITUTE');
51304             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51305             LOAD_ITEM_SUBSTITUTE;
51306 
51307 
51308          ELSIF p_tasknum= TASK_CATEGORIES_SOURCING THEN
51309 
51310             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51311             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_CATEGORIES_SOURCING');
51312             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51313             LOAD_CATEGORIES_SOURCING;
51314 
51315          ELSIF p_tasknum= TASK_DEPT_RESOURCES THEN
51316 
51317             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51318             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_DEPT_RESOURCES');
51319             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51320             LOAD_DEPT_RESOURCES;
51321 
51322          ELSIF p_tasknum= TASK_MATERIAL_SUPPLY THEN
51323 
51324             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51325             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_MATERIAL_SUPPLY');
51326             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51327             LOAD_MATERIAL_SUPPLY(p_batch_id);
51328 
51329          ELSIF p_tasknum= TASK_MATERIAL_DEMAND THEN
51330 
51331             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51332             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_MATERIAL_DEMAND');
51333             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51334             LOAD_MATERIAL_DEMAND(p_batch_id);
51335 
51336          ELSIF p_tasknum= TASK_BOM_ROUTING THEN
51337 
51338             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51339             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_BOM_ROUTING');
51340             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51341             LOAD_BOM_ROUTING;
51342 
51343          ELSIF p_tasknum= TASK_RESOURCE_DEMAND THEN
51344 
51345             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51346             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_RESOURCE_DEMAND');
51347             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51348             LOAD_RESOURCE_DEMAND;
51349 
51350          ELSIF p_tasknum= TASK_SO_DEMAND  THEN
51351 
51352             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51353             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_SO_DEMAND');
51354             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51355             LOAD_SO_DEMAND(p_batch_id);
51356 
51357 
51358          ELSIF p_tasknum= TASK_RESERVATION  THEN
51359 
51360             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51361             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_RESERVATION');
51362             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51363             LOAD_RESERVATION;
51364 
51365          ELSIF p_tasknum= TASK_ITEM_CST  THEN
51366 
51367             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51368             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_ITEM_CST');
51369             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51370             LOAD_ITEM_CST;
51371 
51372          ELSIF p_tasknum= TASK_LEVEL_VALUE  THEN
51373             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51374             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_LEVEL_VALUE');
51375             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51376             LOAD_LEVEL_VALUE(p_batch_id);
51377 
51378          ELSIF p_tasknum= TASK_LEVEL_ASSOCIATION  THEN
51379             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51380             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_LEVEL_ASSOCIATION');
51381             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51382             LOAD_LEVEL_ASSOCIATION(p_batch_id);
51383 
51384          ELSIF p_tasknum= TASK_BOOKING_DATA  THEN
51385             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51386             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_BOOKING_DATA');
51387             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51388             LOAD_BOOKING_DATA(p_batch_id);
51389 
51390          ELSIF p_tasknum= TASK_SHIPMENT_DATA  THEN
51391             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51392             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_SHIPMENT_DATA');
51393             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51394             LOAD_SHIPMENT_DATA(p_batch_id);
51395 
51396          ELSIF p_tasknum= TASK_MFG_FORECAST  THEN
51397             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51398             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_MFG_FORECAST');
51399             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51400             LOAD_MFG_FORECAST(p_batch_id);
51401 
51402          ELSIF p_tasknum= TASK_PRICE_LIST  THEN
51403             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51404             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_PRICE_LIST');
51405             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51406             LOAD_PRICE_LIST(p_batch_id);
51407 /*
51408          ELSIF p_tasknum= TASK_ITEM_LIST_PRICE  THEN
51409             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51410             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_ITEM_LIST_PRICE');
51411             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51412             LOAD_ITEM_LIST_PRICE(p_batch_id);
51413 */
51414          ELSIF p_tasknum= TASK_CS_DATA  THEN
51415            FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51416            FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_CS_DATA');
51417            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51418            LOAD_CS_DATA(p_batch_id);
51419 
51420          ELSIF p_tasknum= TASK_CURR_CONV  THEN
51421             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51422             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_CURR_CONV');
51423             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51424             LOAD_CURR_CONV(p_batch_id);
51425 
51426          ELSIF p_tasknum= TASK_DP_UOM_CONV  THEN
51427             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51428             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_UOM_CONV');
51429             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51430             LOAD_UOM_CONV(p_batch_id);
51431 
51432          ELSIF p_tasknum= TASK_FISCAL_TIME THEN
51433             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51434             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_FISCAL_TIME');
51435             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51436             LOAD_FISCAL_TIME;
51437 
51438         ELSIF p_tasknum= TASK_COMPOSITE_TIME THEN
51439             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51440             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_COMPOSITE_TIME');
51441             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51442             LOAD_COMPOSITE_TIME;
51443 
51444 
51445          ELSIF p_tasknum= TASK_COMPANY_USERS THEN
51446             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51447             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'TASK_COMPANY_USERS');
51448             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51449             LOAD_COMPANY_USER;
51450 
51451         ELSIF p_tasknum= TASK_CAL_ASSIGNMENTS THEN
51452             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51453             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_CAL_ASSIGNMENTS');
51454             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51455             LOAD_CAL_ASSIGNMENTS;
51456 
51457          ELSIF p_tasknum= TASK_IRO THEN
51458             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51459             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_IRO_SUPPLY_DEMAND');
51460             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51461             LOAD_IRO_SUPPLY_DEMAND;
51462 
51463           ELSIF p_tasknum= TASK_ERO THEN
51464             FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_TASK_START');
51465             FND_MESSAGE.SET_TOKEN('PROCEDURE', 'LOAD_ERO_SUPPLY_DEMAND');
51466             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51467             LOAD_ERO_SUPPLY_DEMAND;
51468 
51469 
51470         END IF;
51471 
51472    -- ======== If no EXCEPTION occurs, then returns with status = OK =========
51473 
51474          p_status := OK;
51475 
51476          FND_MESSAGE.SET_NAME('MSC', 'MSC_ELAPSED_TIME');
51477          FND_MESSAGE.SET_TOKEN('ELAPSED_TIME',
51478                      TO_CHAR(CEIL((SYSDATE- lv_task_start_time)*14400.0)/10));
51479          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
51480 
51481     EXCEPTION
51482 
51483          WHEN others THEN
51484 
51485    -- ============= Raise the EXCEPTION ==============
51486 
51487               ROLLBACK WORK TO SAVEPOINT ExecuteTask;
51488 
51489               MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,SQLERRM);
51490 
51491               RAISE;
51492 
51493    END EXECUTE_TASK;
51494 
51495 -- ============ Load Procedures ==============================
51496 
51497   PROCEDURE LOAD_ITEM(p_batch_id NUMBER)
51498   IS
51499   BEGIN
51500     -- items
51501     IF v_item_enabled = SYS_YES and p_batch_id <> 0 THEN
51502       LOAD_ITEMS(p_batch_id);
51503     END IF;
51504   END LOAD_ITEM;
51505 
51506 -- Product item substitute
51507 
51508   PROCEDURE LOAD_ITEM_SUBSTITUTE
51509   IS
51510   BEGIN
51511     -- items
51512     IF v_item_substitute_enabled = SYS_YES THEN
51513       LOAD_ITEM_SUBSTITUTES;
51514     END IF;
51515   END LOAD_ITEM_SUBSTITUTE;
51516 
51517 
51518   PROCEDURE LOAD_SUPPLIER_CAPACITY
51519   IS
51520   BEGIN
51521     -- supplier capacity
51522     IF v_sup_cap_enabled = SYS_YES THEN
51523       LOAD_SUPP_CAPACITY;
51524     END IF;
51525   END LOAD_SUPPLIER_CAPACITY;
51526 
51527   PROCEDURE LOAD_SAFETY_STOCK
51528   IS
51529   BEGIN
51530     -- safety stocks
51531     IF v_safety_stk_enabled = SYS_YES THEN
51532       LOAD_SAFETY_STOCKS;
51533     END IF;
51534   END LOAD_SAFETY_STOCK;
51535 
51536   PROCEDURE LOAD_CATEGORIES_SOURCING
51537   IS
51538   BEGIN
51539     -- categories
51540     IF v_item_cat_enabled = SYS_YES THEN
51541       LOAD_CATEGORY;
51542     END IF;
51543     -- Inter-org ship methods
51544      IF v_ship_mthd_enabled = SYS_YES THEN
51545        LOAD_SHIP_METHODS;
51546      END IF;
51547 
51548     -- sourcing
51549     IF v_sourcing_enabled = SYS_YES THEN
51550       LOAD_SOURCING_RULE;
51551       LOAD_REGIONS;
51552     END IF;
51553   END LOAD_CATEGORIES_SOURCING;
51554 
51555   PROCEDURE LOAD_DEPT_RESOURCES
51556   IS
51557   BEGIN
51558     -- dept and resources
51559     IF v_resources_enabled = SYS_YES THEN
51560       LOAD_DEPT_RES;
51561     END IF;
51562   END LOAD_DEPT_RESOURCES;
51563 
51564   PROCEDURE LOAD_MATERIAL_SUPPLY(p_batch_id NUMBER)
51565   IS
51566   BEGIN
51567     -- Material Supply ( PO, Intransit, On Hand, MPS, Requisitions)
51568     IF v_mat_sup_enabled = SYS_YES THEN
51569       LOAD_SUPPLY(p_batch_id);
51570     END IF;
51571   END LOAD_MATERIAL_SUPPLY;
51572 
51573   PROCEDURE LOAD_MATERIAL_DEMAND(p_batch_id NUMBER)
51574   IS
51575   BEGIN
51576     -- Material Demand (forecast amd MDS)
51577     IF v_mat_dmd_enabled = SYS_YES THEN
51578       LOAD_MDS_FORECAST (p_batch_id);
51579     END IF;
51580   END LOAD_MATERIAL_DEMAND;
51581 
51582   PROCEDURE LOAD_SO_DEMAND(p_batch_id NUMBER)
51583   IS
51584   BEGIN
51585     -- Material Demand (sales orders)
51586     IF v_mat_dmd_enabled = SYS_YES THEN
51587       LOAD_SALES_ORDER ( p_batch_id);
51588     END IF;
51589   END LOAD_SO_DEMAND;
51590 
51591  PROCEDURE LOAD_RESERVATION
51592  IS
51593  BEGIN
51594    -- Hard Reservations
51595     IF v_reserve_enabled = SYS_YES THEN
51596        LOAD_RESERVATIONS;
51597     END IF;
51598  END LOAD_RESERVATION ;
51599 
51600  PROCEDURE LOAD_COMPANY_USER
51601  IS
51602  BEGIN
51603 
51604     IF v_comp_users_enabled = SYS_YES THEN
51605       LOAD_COMPANY_USERS;
51606     END IF;
51607  END LOAD_COMPANY_USER;
51608 
51609   PROCEDURE LOAD_BOM_ROUTING
51610   IS
51611   BEGIN
51612     -- bom
51613     IF v_bom_enabled = SYS_YES THEN
51614       LOAD_BOM;
51615     END IF;
51616     -- routing
51617     IF v_rtg_enabled = SYS_YES THEN
51618       LOAD_ROUTING;
51619     END IF;
51620     -- process effectivity
51621     IF v_rtg_enabled = SYS_YES OR v_bom_enabled = SYS_YES  THEN
51622       LOAD_PROCESS_EFFECTIVITY;
51623     END IF;
51624   END LOAD_BOM_ROUTING;
51625 
51626   PROCEDURE LOAD_RESOURCE_DEMAND
51627   IS
51628 
51629   BEGIN
51630     -- material supply WO
51631     IF v_mat_sup_enabled = SYS_YES THEN
51632       LOAD_WO_SUPPLY;
51633     END IF;
51634     -- material demand WO
51635     IF v_mat_dmd_enabled = SYS_YES THEN
51636       LOAD_WIP_DEMAND;
51637     END IF;
51638     -- resource demand
51639     IF v_res_dmd_enabled = SYS_YES THEN
51640       LOAD_RES_DEMAND;
51641     END IF;
51642     -- osfm job details
51643     IF v_mat_sup_enabled = SYS_YES THEN
51644        LOAD_JOB_DETAILS;
51645     END IF;
51646   END LOAD_RESOURCE_DEMAND;
51647 
51648   PROCEDURE LOAD_ITEM_CST
51649   IS
51650   BEGIN
51651     LOAD_ITEM_CUSTOMERS;
51652   END LOAD_ITEM_CST;
51653 
51654   PROCEDURE LOAD_CAL_ASSIGNMENTS
51655   IS
51656   BEGIN
51657     -- calendar assignment
51658     IF v_cal_assignment_enabled = SYS_YES THEN
51659       LOAD_CALENDAR_ASSIGNMENTS;
51660     END IF;
51661   END LOAD_CAL_ASSIGNMENTS;
51662 
51663   PROCEDURE LOAD_IRO_SUPPLY_DEMAND
51664   IS
51665   BEGIN
51666          IF v_iro_enabled = SYS_YES THEN
51667            MSC_CL_RPO_PRE_PROCESS.LOAD_IRO_SUPPLY;
51668            MSC_CL_RPO_PRE_PROCESS.LOAD_IRO_DEMAND;
51669          END IF;
51670   END LOAD_IRO_SUPPLY_DEMAND;
51671 
51672   PROCEDURE LOAD_ERO_SUPPLY_DEMAND
51673   IS
51674   BEGIN
51675          IF v_ero_enabled = SYS_YES THEN
51676            MSC_CL_RPO_PRE_PROCESS.LOAD_ERO_SUPPLY;
51677            MSC_CL_RPO_PRE_PROCESS.LOAD_ERO_DEMAND;
51678          END IF;
51679   END LOAD_ERO_SUPPLY_DEMAND;
51680 
51681 
51682 ---------------------- Added for the bug fix 2191021 -------------------------------------
51683   PROCEDURE LOAD_ITEM_ABC_CLASSES
51684   IS
51685 
51686    TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
51687    lb_rowid_abc      RowidTab;
51688 
51689    lv_sql_stmt           VARCHAR2(5000);
51690    lv_return             NUMBER;
51691    lv_error_text         VARCHAR2(250);
51692 
51693    CURSOR c1 IS
51694     SELECT max(ROWID)
51695     FROM   msc_st_system_items
51696     WHERE  ABC_CLASS_ID IS NULL
51697     AND    ABC_CLASS_NAME IS NOT NULL
51698     AND    process_flag     = G_VALID
51699     AND    sr_instance_code = v_instance_code
51700     GROUP BY ABC_CLASS_NAME, organization_code, sr_instance_code, company_name;
51701 
51702   BEGIN
51703    IF(v_install_msc) THEN
51704     -- update ABC_CLASS_ID where it exists in the planning system
51705      v_sql_stmt := 1;
51706      lv_sql_stmt :=
51707       ' UPDATE  msc_st_system_items msi'
51708       ||' SET   abc_class_id        = (SELECT local_id from MSC_LOCAL_ID_MISC mac '
51709       ||'                              WHERE  mac.char1 = msi.sr_instance_code'
51710       ||'                              AND    mac.char3  = msi.organization_code  '
51711       ||'                              AND    mac.char4 = msi.abc_class_name '
51712       ||'                              AND    mac.ENTITY_NAME = ''ABC_CLASS_ID'''
51713       ||'                              AND    NVL(mac.char2,'||''''||NULL_CHAR||''''||') '
51714       ||'                                     =    NVL(msi.company_name,'||''''||NULL_CHAR||''''||')) '
51715       ||' WHERE process_flag     = '||G_VALID
51716       ||' AND   sr_instance_code = :v_instance_code'
51717       ||' AND   abc_class_name is not null ';
51718 
51719 
51720         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
51721 
51722       EXECUTE IMMEDIATE lv_sql_stmt
51723               USING     v_instance_code;
51724 
51725      OPEN c1;
51726      FETCH c1 BULK COLLECT INTO lb_rowid_abc ;
51727 
51728      IF c1%ROWCOUNT > 0  THEN
51729 
51730         FORALL j IN lb_rowid_abc.FIRST..lb_rowid_abc.LAST
51731         UPDATE msc_st_system_items msi
51732         SET    abc_class_id   = MSC_ST_ABC_CLASS_ID_S.nextval
51733         WHERE  rowid   = lb_rowid_abc(j);
51734 
51735 
51736        FORALL j IN lb_rowid_abc.FIRST..lb_rowid_abc.LAST
51737        INSERT INTO  msc_local_id_misc
51738        (local_id,
51739        st_transaction_id,
51740        instance_id,
51741        entity_name,
51742        data_source_type,
51743        char1,
51744        char2,
51745        char3,
51746        char4,
51747        SOURCE_ABC_CLASS_ID,
51748        last_update_date,
51749        last_updated_by,
51750        creation_date,
51751        created_by  )
51752        SELECT
51753        abc_class_id,
51754        st_transaction_id,
51755        v_instance_id,
51756        'ABC_CLASS_ID',
51757        data_source_type,
51758        sr_instance_code,
51759        company_name,
51760        organization_code,
51761        ABC_CLASS_NAME,
51762        SOURCE_ABC_CLASS_ID,
51763        v_current_date,
51764        v_current_user,
51765        v_current_date,
51766        v_current_user
51767       FROM msc_st_system_items
51768       WHERE  rowid            = lb_rowid_abc(j);
51769 
51770      END IF;
51771      CLOSE c1;
51772 
51773      -- update ABC_CLASS_ID for ABC_CLASS_NAME for which we have just populated the local id
51774      v_sql_stmt := 2;
51775      lv_sql_stmt :=
51776       ' UPDATE  msc_st_system_items msi'
51777       ||' SET   abc_class_id        = (SELECT local_id from MSC_LOCAL_ID_MISC mac '
51778       ||'                              WHERE  mac.char1 = msi.sr_instance_code'
51779       ||'                              AND    mac.char3  = msi.organization_code  '
51780       ||'                              AND    mac.char4 = msi.abc_class_name '
51781       ||'                              AND    mac.ENTITY_NAME = ''ABC_CLASS_ID'''
51782       ||'                              AND    NVL(mac.char2,'||''''||NULL_CHAR||''''||') '
51783       ||'                                     =    NVL(msi.company_name,'||''''||NULL_CHAR||''''||')) '
51784       ||' WHERE process_flag     = '||G_VALID
51785       ||' AND   sr_instance_code = :v_instance_code'
51786       ||' AND   abc_class_id  IS NULL'
51787       ||' AND   abc_class_name is not null ';
51788 
51789 
51790         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
51791 
51792       EXECUTE IMMEDIATE lv_sql_stmt
51793               USING     v_instance_code;
51794 
51795      v_sql_stmt := 3;
51796      lv_sql_stmt :=
51797        ' INSERT INTO MSC_ABC_CLASSES '
51798        ||' ( ABC_CLASS_ID, '
51799        ||' ABC_CLASS_NAME, '
51800        ||' ORGANIZATION_ID, '
51801        ||' SR_INSTANCE_ID, '
51802        ||' LAST_UPDATE_DATE, '
51803        ||' LAST_UPDATED_BY, '
51804        ||' CREATION_DATE, '
51805        ||' CREATED_BY) '
51806        ||' SELECT distinct '
51807        ||' msi.abc_class_id,'
51808        ||' msi.abc_class_name,'
51809        ||' msi.organization_id,'
51810        ||  v_instance_id ||','
51811        ||' last_update_date,'
51812        ||' last_updated_by,'
51813        ||' creation_date,'
51814        ||' created_by '
51815        ||' FROM   MSC_ST_SYSTEM_ITEMS msi'
51816        ||' WHERE  sr_instance_code = :v_instance_code'
51817        ||' AND    process_flag    = '||G_VALID
51818        ||' AND    msi.abc_class_id is not null'
51819        ||' AND    msi.abc_class_name is not null'
51820        ||' AND    NOT EXISTS'
51821        ||'        ( SELECT 1 FROM MSC_ABC_CLASSES abc'
51822        ||'        WHERE  abc.abc_class_id = msi.abc_class_id'
51823        ||'        AND    abc.abc_class_name = msi.abc_class_name'
51824        ||'        AND    abc.organization_id = msi.organization_id'
51825        ||'        AND    abc.sr_instance_id = ' ||v_instance_id ||')';
51826 
51827 
51828         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
51829 
51830 
51831       EXECUTE IMMEDIATE lv_sql_stmt
51832               USING     v_instance_code;
51833 
51834   /* -- Set the process flag as Valid and populate instance_id
51835     lv_return :=
51836     MSC_ST_UTIL.SET_PROCESS_FLAG(p_table_name   => 'MSC_ST_SYSTEM_ITEMS',
51837                                p_instance_id    => v_instance_id,
51838                                p_instance_code  => v_instance_code,
51839                                p_process_flag   => G_VALID,
51840                                p_error_text     => lv_error_text,
51841                                p_debug          => v_debug);
51842 
51843       IF lv_return <> 0 THEN
51844         msc_st_util.log_message(lv_error_text);
51845       END IF;
51846   */
51847 
51848   lv_sql_stmt :=
51849     'UPDATE msc_st_system_items msi'
51850     ||' SET product_family_id = (SELECT local_id'
51851     ||'     FROM   msc_local_id_item mli'
51852     ||'     WHERE  mli.char1         = msi.sr_instance_code '
51853     ||'     AND    NVL(mli.char2,       '||''''||NULL_CHAR||''''||')='
51854     ||'            NVL(msi.company_name,'||''''||NULL_CHAR||''''||')'
51855     ||'     AND    mli.char4         = msi.product_family_item_name'
51856     ||'     AND    mli.entity_name   = ''SR_INVENTORY_ITEM_ID'' '
51857     ||'     AND    mli.instance_id   = :v_instance_id'
51858     ||'     AND    rownum            < 2)'
51859     ||' WHERE  sr_instance_code      = :v_instance_code'
51860     ||' AND    product_family_item_name is not null '
51861     ||' AND    process_flag          = '||G_VALID;
51862 
51863 
51864          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
51865 
51866     EXECUTE IMMEDIATE lv_sql_stmt
51867             USING      v_instance_id,
51868                        v_instance_code;
51869 
51870    lv_sql_stmt :=
51871     'UPDATE msc_st_system_items msi'
51872     ||' SET        BASE_ITEM_ID = (SELECT local_id'
51873     ||'     FROM   msc_local_id_item mli'
51874     ||'     WHERE  mli.char1         = msi.sr_instance_code '
51875     ||'     AND    NVL(mli.char2,       '||''''||NULL_CHAR||''''||')='
51876     ||'            NVL(msi.company_name,'||''''||NULL_CHAR||''''||')'
51877     ||'     AND    mli.char4         = msi.BASE_ITEM_NAME'
51878     ||'     AND    mli.entity_name   = ''SR_INVENTORY_ITEM_ID'' '
51879     ||'     AND    mli.instance_id   = :v_instance_id'
51880     ||'     AND    rownum            < 2)'
51881     ||' WHERE  sr_instance_code      = :v_instance_code'
51882     ||' AND    base_item_name is not null '
51883     ||' AND    process_flag          = '||G_VALID;
51884 
51885 
51886          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
51887 
51888     EXECUTE IMMEDIATE lv_sql_stmt
51889             USING      v_instance_id,
51890                        v_instance_code;
51891 
51892 
51893   COMMIT;
51894 
51895   END IF; --v_install_msc
51896 
51897  EXCEPTION
51898   WHEN OTHERS THEN
51899     MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_ITEM_ABC_CLASSES'||SQLERRM,1,240));
51900 
51901   END LOAD_ITEM_ABC_CLASSES;
51902  ---------------------- Till here added for the bug fix 2191021 -------------------------------------
51903 
51904 
51905   ----=========DP specific local procdure=======-------
51906   PROCEDURE LOAD_LEVEL_VALUE(p_batch_id NUMBER)
51907   IS
51908   lv_sql_stmt  VARCHAR2(4000);
51909   BEGIN
51910    IF (v_level_value_enabled = SYS_YES) AND (v_install_msd) THEN
51911        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
51912                        ||' retcode  number ;'
51913                        ||' BEGIN '
51914                        ||' MSD_CL_PRE_PROCESS.LOAD_LEVEL_VALUE'
51915                        ||' (ERRBUF         =>  errbuf ,'
51916                        ||'  RETCODE        =>  retcode ,'
51917                        ||'  p_instance_code => :v_instance_code,'
51918                        ||'  p_instance_id   => :v_instance_id,'
51919                        ||'  p_batch_id      => :lv_batch_id);'
51920                        ||'  IF ( retcode = '||G_WARNING||') THEN'
51921                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
51922                        ||'  END IF;'
51923                        ||'  END ;';
51924 
51925 
51926         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
51927         EXECUTE IMMEDIATE lv_sql_stmt
51928                 USING     v_instance_code,
51929                           v_instance_id,
51930                           p_batch_id ;
51931     END IF ;
51932 
51933    EXCEPTION
51934     WHEN OTHERS THEN
51935      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_LEVEL_VALUE'||SQLERRM,1,240));
51936 
51937    END ;
51938 
51939   PROCEDURE LOAD_LEVEL_ASSOCIATION(p_batch_id NUMBER)
51940   IS
51941   lv_sql_stmt  VARCHAR2(4000);
51942   BEGIN
51943    IF (v_level_assoc_enabled = SYS_YES) AND (v_install_msd) THEN
51944        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
51945                        ||' retcode  number ;'
51946                        ||' BEGIN '
51947                        ||' MSD_CL_PRE_PROCESS.LOAD_LEVEL_ASSOC'
51948                        ||' (ERRBUF         =>  errbuf ,'
51949                        ||'  RETCODE        =>  retcode ,'
51950                        ||'  p_instance_code => :v_instance_code,'
51951                        ||'  p_instance_id   => :v_instance_id,'
51952                        ||'  p_batch_id      => :lv_batch_id);'
51953                        ||'  IF ( retcode = '||G_WARNING||') THEN'
51954                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
51955                        ||'  END IF;'
51956                        ||'  END ;';
51957 
51958 
51959         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
51960         EXECUTE IMMEDIATE lv_sql_stmt
51961                 USING     v_instance_code,
51962                           v_instance_id,
51963                           p_batch_id ;
51964     END IF ;
51965 
51966    EXCEPTION
51967     WHEN OTHERS THEN
51968      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_LEVEL_ASSOCIATION'||SQLERRM,1,240));
51969 
51970    END ;
51971 
51972    PROCEDURE  LOAD_BOOKING_DATA (p_batch_id   NUMBER )
51973    IS
51974    lv_sql_stmt  VARCHAR2(4000);
51975    BEGIN
51976    IF (v_booking_enabled = SYS_YES) AND (v_install_msd) THEN
51977        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
51978                        ||' retcode  number ;'
51979                        ||' BEGIN '
51980                        ||' MSD_CL_PRE_PROCESS.LOAD_BOOKING_DATA'
51981                        ||' (ERRBUF         =>  errbuf ,'
51982                        ||'  RETCODE        =>  retcode ,'
51983                        ||'  p_instance_code => :v_instance_code,'
51984                        ||'  p_instance_id   => :v_instance_id,'
51985                        ||'  p_batch_id      => :lv_batch_id);'
51986                        ||'  IF ( retcode = '||G_WARNING||') THEN'
51987                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
51988                        ||'  END IF;'
51989                        ||'  END ;';
51990 
51991 
51992         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
51993         EXECUTE IMMEDIATE lv_sql_stmt
51994                 USING     v_instance_code,
51995                           v_instance_id,
51996                           p_batch_id ;
51997     END IF ;
51998 
51999    EXCEPTION
52000     WHEN OTHERS THEN
52001      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_BOOKING_DATA'||SQLERRM,1,240));
52002 
52003    END ;
52004 
52005    PROCEDURE  LOAD_SHIPMENT_DATA (p_batch_id   NUMBER )
52006    IS
52007    lv_sql_stmt  VARCHAR2(4000);
52008    BEGIN
52009    IF (v_shipment_enabled = SYS_YES) AND (v_install_msd) THEN
52010        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52011                        ||' retcode  number ;'
52012                        ||' BEGIN '
52013                        ||' MSD_CL_PRE_PROCESS.LOAD_SHIPMENT_DATA'
52014                        ||' (ERRBUF         =>  errbuf ,'
52015                        ||'  RETCODE        =>  retcode ,'
52016                        ||'  p_instance_code => :v_instance_code,'
52017                        ||'  p_instance_id   => :v_instance_id,'
52018                        ||'  p_batch_id      => :lv_batch_id);'
52019                        ||'  IF ( retcode = '||G_WARNING||') THEN'
52020                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
52021                        ||'  END IF;'
52022                        ||'  END ;';
52023 
52024 
52025         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52026         EXECUTE IMMEDIATE lv_sql_stmt
52027                 USING     v_instance_code,
52028                           v_instance_id,
52029                           p_batch_id ;
52030     END IF ;
52031 
52032    EXCEPTION
52033     WHEN OTHERS THEN
52034      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_SHIPMENT_DATA'||SQLERRM,1,240));
52035 
52036    END ;
52037 
52038    PROCEDURE  LOAD_MFG_FORECAST (p_batch_id   NUMBER )
52039    IS
52040    lv_sql_stmt  VARCHAR2(4000);
52041    BEGIN
52042    IF (v_mfg_fct_enabled = SYS_YES) AND (v_install_msd) THEN
52043        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52044                        ||' retcode  number ;'
52045                        ||' BEGIN '
52046                        ||' MSD_CL_PRE_PROCESS.LOAD_MFG_FORECAST'
52047                        ||' (ERRBUF         =>  errbuf ,'
52048                        ||'  RETCODE        =>  retcode ,'
52049                        ||'  p_instance_code => :v_instance_code,'
52050                        ||'  p_instance_id   => :v_instance_id,'
52051                        ||'  p_batch_id      => :lv_batch_id);'
52052                        ||'  IF ( retcode = '||G_WARNING||') THEN'
52053                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
52054                        ||'  END IF;'
52055                        ||'  END ;';
52056 
52057 
52058         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52059         EXECUTE IMMEDIATE lv_sql_stmt
52060                 USING     v_instance_code,
52061                           v_instance_id,
52062                           p_batch_id ;
52063     END IF ;
52064 
52065    EXCEPTION
52066     WHEN OTHERS THEN
52067      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_MFG_FORECAST'||SQLERRM,1,240));
52068 
52069    END ;
52070 
52071   PROCEDURE  LOAD_PRICE_LIST (p_batch_id   NUMBER )
52072    IS
52073    lv_sql_stmt  VARCHAR2(4000);
52074    BEGIN
52075    IF (v_list_price_enabled = SYS_YES) AND (v_install_msd) THEN
52076        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52077                        ||' retcode  number ;'
52078                        ||' BEGIN '
52079                        ||' MSD_CL_PRE_PROCESS.LOAD_PRICE_LIST'
52080                        ||' (ERRBUF         =>  errbuf ,'
52081                        ||'  RETCODE        =>  retcode ,'
52082                        ||'  p_instance_code => :v_instance_code,'
52083                        ||'  p_instance_id   => :v_instance_id,'
52084                        ||'  p_batch_id      => :lv_batch_id);'
52085                        ||'  IF ( retcode = '||G_WARNING||') THEN'
52086                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
52087                        ||'  END IF;'
52088                        ||'  END ;';
52089 
52090 
52091         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52092         EXECUTE IMMEDIATE lv_sql_stmt
52093                 USING     v_instance_code,
52094                           v_instance_id,
52095                           p_batch_id ;
52096     END IF ;
52097 
52098    EXCEPTION
52099     WHEN OTHERS THEN
52100      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_PRICE_LIST'||SQLERRM,1,240));
52101 
52102    END ;
52103 /*
52104   PROCEDURE  LOAD_ITEM_LIST_PRICE(p_batch_id   NUMBER )
52105    IS
52106    lv_sql_stmt  VARCHAR2(4000);
52107    BEGIN
52108    IF (v_list_price_enabled = SYS_YES) AND (v_install_msd) THEN
52109        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52110                        ||' retcode  number ;'
52111                        ||' BEGIN '
52112                        ||' MSD_CL_PRE_PROCESS.LOAD_ITEM_LIST_PRICE'
52113                        ||' (ERRBUF         =>  errbuf ,'
52114                        ||'  RETCODE        =>  retcode ,'
52115                        ||'  p_instance_code => :v_instance_code,'
52116                        ||'  p_instance_id   => :v_instance_id,'
52117                        ||'  p_batch_id      => :lv_batch_id);'
52118                        ||'  END ;';
52119 
52120 
52121         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52122         EXECUTE IMMEDIATE lv_sql_stmt
52123                 USING     v_instance_code,
52124                           v_instance_id,
52125                           p_batch_id ;
52126     END IF ;
52127 
52128    EXCEPTION
52129     WHEN OTHERS THEN
52130      msc_st_util.log_message(substr('MSC_CL_PRE_PROCESS.LOAD_ITEM_LIST_PRICE'||SQLERRM,1,240));
52131 
52132    END ;
52133 */
52134 
52135   PROCEDURE  LOAD_CS_DATA (p_batch_id   NUMBER )
52136    IS
52137    lv_sql_stmt  VARCHAR2(4000);
52138    BEGIN
52139    IF (v_cs_data_enabled = SYS_YES) AND (v_install_msd) THEN
52140        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52141                        ||' retcode  number ;'
52142                        ||' BEGIN '
52143                        ||' MSD_CL_PRE_PROCESS.LOAD_CS_DATA'
52144                        ||' (ERRBUF         =>  errbuf ,'
52145                        ||'  RETCODE        =>  retcode ,'
52146                        ||'  p_instance_code => :v_instance_code,'
52147                        ||'  p_instance_id   => :v_instance_id,'
52148                        ||'  p_batch_id      => :lv_batch_id);'
52149                        ||'  IF ( retcode = '||G_WARNING||') THEN'
52150                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
52151                        ||'  END IF;'
52152                        ||'  END ;';
52153 
52154 
52155         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52156         EXECUTE IMMEDIATE lv_sql_stmt
52157                 USING     v_instance_code,
52158                           v_instance_id,
52159                           p_batch_id ;
52160     END IF ;
52161 
52162    EXCEPTION
52163     WHEN OTHERS THEN
52164      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_CS_DATA'||SQLERRM,1,240));
52165 
52166   END ;
52167 
52168   PROCEDURE  LOAD_CURR_CONV (p_batch_id   NUMBER )
52169    IS
52170    lv_sql_stmt  VARCHAR2(4000);
52171    BEGIN
52172    IF (v_curr_conv_enabled = SYS_YES) AND (v_install_msd) THEN
52173        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52174                        ||' retcode  number ;'
52175                        ||' BEGIN '
52176                        ||' MSD_CL_PRE_PROCESS.LOAD_CURRENCY_CONV'
52177                        ||' (ERRBUF         =>  errbuf ,'
52178                        ||'  RETCODE        =>  retcode ,'
52179                        ||'  p_instance_code => :v_instance_code,'
52180                        ||'  p_instance_id   => :v_instance_id,'
52181                        ||'  p_batch_id      => :lv_batch_id);'
52182                        ||'  IF ( retcode = '||G_WARNING||') THEN'
52183                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
52184                        ||'  END IF;'
52185                        ||'  END ;';
52186 
52187 
52188         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52189 
52190         EXECUTE IMMEDIATE lv_sql_stmt
52191                 USING     v_instance_code,
52192                           v_instance_id,
52193                           p_batch_id ;
52194     END IF ;
52195 
52196    EXCEPTION
52197     WHEN OTHERS THEN
52198      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_CURR_CONV'||SQLERRM,1,240));
52199 
52200    END ;
52201 
52202   PROCEDURE  LOAD_UOM_CONV (p_batch_id   NUMBER )
52203   IS
52204    lv_sql_stmt  VARCHAR2(4000);
52205    BEGIN
52206    IF (v_uom_conv_enabled = SYS_YES) AND (v_install_msd) THEN
52207        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52208                        ||' retcode  number ;'
52209                        ||' BEGIN '
52210                        ||' MSD_CL_PRE_PROCESS.LOAD_UOM_CONV'
52211                        ||' (ERRBUF         =>  errbuf ,'
52212                        ||'  RETCODE        =>  retcode ,'
52213                        ||'  p_instance_code => :v_instance_code,'
52214                        ||'  p_instance_id   => :v_instance_id,'
52215                        ||'  p_batch_id      => :lv_batch_id);'
52216                        ||'  IF ( retcode = '||G_WARNING||') THEN'
52217                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
52218                        ||'  END IF;'
52219                        ||'  END ;';
52220 
52221 
52222         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52223 
52224         EXECUTE IMMEDIATE lv_sql_stmt
52225                 USING     v_instance_code,
52226                           v_instance_id,
52227                           p_batch_id ;
52228     END IF ;
52229 
52230    EXCEPTION
52231     WHEN OTHERS THEN
52232      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_UOM_CONV'||SQLERRM,1,240));
52233 
52234    END ;
52235 
52236   PROCEDURE LOAD_FISCAL_TIME
52237   IS
52238   lv_sql_stmt  VARCHAR2(4000);
52239    BEGIN
52240     IF (v_fiscal_cal_enabled = SYS_YES ) AND (v_install_msd) THEN
52241        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52242                        ||' retcode  number ;'
52243                        ||' BEGIN '
52244                        ||' MSD_CL_PRE_PROCESS.LOAD_FISCAL_TIME'
52245                        ||' (ERRBUF         =>  errbuf ,'
52246                        ||'  RETCODE        =>  retcode ,'
52247                        ||'  p_instance_code => :v_instance_code,'
52248                        ||'  p_instance_id   => :v_instance_id );'
52249                        ||'  IF ( retcode = '||G_WARNING||') THEN'
52250                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
52251                        ||'  END IF;'
52252                        ||'  END ;';
52253 
52254 
52255         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52256 
52257         EXECUTE IMMEDIATE lv_sql_stmt
52258                 USING     v_instance_code,
52259                           v_instance_id ;
52260     END IF ;
52261 
52262    EXCEPTION
52263     WHEN OTHERS THEN
52264      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_FISCAL_TIME'||SQLERRM,1,240));
52265 
52266    END ;
52267 
52268 
52269 PROCEDURE LOAD_COMPOSITE_TIME
52270   IS
52271   lv_sql_stmt  VARCHAR2(4000);
52272    BEGIN
52273     IF (v_comp_cal_enabled = SYS_YES ) AND (v_install_msd) THEN
52274        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52275                        ||' retcode  number ;'
52276                        ||' BEGIN '
52277                        ||' MSD_CL_PRE_PROCESS.LOAD_COMPOSITE_TIME'
52278                        ||' (ERRBUF         =>  errbuf ,'
52279                        ||'  RETCODE        =>  retcode ,'
52280                        ||'  p_instance_code => :v_instance_code,'
52281                        ||'  p_instance_id   => :v_instance_id );'
52282                        ||'  IF ( retcode = '||G_WARNING||') THEN'
52283                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
52284                        ||'  END IF;'
52285                        ||'  END ;';
52286 
52287 
52288         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52289 
52290         EXECUTE IMMEDIATE lv_sql_stmt
52291                 USING     v_instance_code,
52292                           v_instance_id ;
52293     END IF ;
52294 
52295    EXCEPTION
52296     WHEN OTHERS THEN
52297      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_COMPOSITE_TIME'||SQLERRM,1,240));
52298 
52299    END ;
52300 
52301 
52302   PROCEDURE LOAD_SETUP_PARAMETER
52303   IS
52304   lv_sql_stmt  VARCHAR2(4000);
52305    BEGIN
52306     IF (v_setup_enabled = SYS_YES ) AND (v_install_msd) THEN
52307        lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52308                        ||' retcode  number ;'
52309                        ||' BEGIN '
52310                        ||' MSD_CL_PRE_PROCESS.LOAD_SETUP_PARAMETER'
52311                        ||' (ERRBUF         =>  errbuf ,'
52312                        ||'  RETCODE        =>  retcode ,'
52313                        ||'  p_instance_code => :v_instance_code,'
52314                        ||'  p_instance_id   => :v_instance_id );'
52315                        ||'  IF ( retcode = '||G_WARNING||') THEN'
52316                        ||'  MSC_CL_PRE_PROCESS.v_flag := '||SYS_YES||';'
52317                        ||'  END IF;'
52318                        ||'  END ;';
52319 
52320 
52321         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52322 
52323         EXECUTE IMMEDIATE lv_sql_stmt
52324                 USING     v_instance_code,
52325                           v_instance_id ;
52326     END IF ;
52327 
52328    EXCEPTION
52329     WHEN OTHERS THEN
52330      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_SETUP_PARAMETER'||SQLERRM,1,240));
52331 
52332    END ;
52333 
52334   PROCEDURE LOAD_MFG_TIME
52335   IS
52336   lv_sql_stmt VARCHAR2(4000);
52337 
52338    CURSOR c1 IS
52339    SELECT calendar_code
52340    FROM   msc_st_calendars
52341    WHERE  process_flag    = G_VALID
52342    AND    sr_instance_id  = v_instance_id;
52343 
52344    BEGIN
52345       IF(v_install_msd) THEN
52346       FOR rec1 IN c1
52347        LOOP
52348 
52349         lv_sql_stmt :=  ' DECLARE errbuf varchar2(250);'
52350                        ||' retcode  number ;'
52351                        ||' BEGIN '
52352                        ||' MSD_CL_PRE_PROCESS.LOAD_MFG_TIME'
52353                        ||' (ERRBUF         =>  errbuf ,'
52354                        ||'  RETCODE        =>  retcode ,'
52355                        ||'  p_calendar_code => :v_calendar_code,'
52356                        ||'  p_instance_id   => :v_instance_id );'
52357                        ||'  IF ( retcode = '||G_WARNING||') THEN '
52358                        ||' update msc_apps_instances'
52359                        ||' set dbs_ver = '||SYS_YES
52360                        ||' where instance_id='||v_instance_id||';'
52361                        ||'  END IF;'
52362                        ||'  END ;';
52363 
52364 
52365           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52366 
52367          EXECUTE IMMEDIATE lv_sql_stmt
52368                 USING     rec1.calendar_code,
52369                           v_instance_id ;
52370         END LOOP ;
52371         END IF ; -- v_install_msd
52372 
52373    EXCEPTION
52374     WHEN OTHERS THEN
52375      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_MFG_TIME'||SQLERRM,1,240));
52376 
52377    END ;
52378 
52379    -- Load Profiles
52380 
52381    PROCEDURE LOAD_PROFILES IS
52382 
52383    lv_bom_explosion     NUMBER;
52384    lv_lbj_copy_routing  NUMBER;
52385    lv_error_text        VARCHAR2(250);
52386    lv_where_str         VARCHAR2(5000);
52387    lv_message_text      VARCHAR2(5000);
52388    lv_return            NUMBER;
52389    lv_transaction_id    NUMBER;
52390    lv_sql_stmt          VARCHAR2(5000);
52391 
52392    ex_logging_err     EXCEPTION;
52393 
52394 
52395 
52396     BEGIN
52397 
52398     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
52399                  (p_app_short_name    => 'MSC',
52400                   p_error_code        => 'MSC_PP_DUP_REC_FOR_XML',
52401                   p_message_text      => lv_message_text,
52402                   p_error_text        => lv_error_text);
52403 
52404     IF lv_return <> 0 THEN
52405       RAISE ex_logging_err;
52406     END IF;
52407 
52408     --Duplicate records check for the records whose source is XML
52409 
52410     UPDATE  msc_st_apps_instances mai1
52411     SET     process_flag = G_ERROR_FLG,
52412             error_text   = lv_message_text
52413     WHERE   message_id <  (SELECT MAX(message_id)
52414                            FROM   msc_st_apps_instances mai2
52415                            WHERE  mai2.sr_instance_code  = mai1.sr_instance_code
52416                            AND    mai2.process_flag      = G_IN_PROCESS
52417                            AND    NVL(mai2.message_id,NULL_VALUE) <>NULL_VALUE)
52418     AND     mai1.process_flag             = G_IN_PROCESS
52419     AND     mai1.sr_instance_code         = v_instance_code
52420     AND     NVL(mai1.message_id,NULL_VALUE) <> NULL_VALUE;
52421 
52422 
52423     lv_return := MSC_ST_UTIL.GET_ERROR_MESSAGE
52424                  (p_app_short_name    => 'MSC',
52425                   p_error_code        => 'MSC_PP_DUP_REC_FOR_BATCH_LOAD',
52426                   p_message_text      => lv_message_text,
52427                   p_error_text        => lv_error_text);
52428 
52429     IF lv_return <> 0 THEN
52430       RAISE ex_logging_err;
52431     END IF;
52432 
52433     --Duplicate records check for the records whose source is other than XML
52434     --Different SQL is used because in XML we can identify the latest records
52435     --whereas in batch load we cannot.
52436 
52437     UPDATE  msc_st_apps_instances mai1
52438     SET     process_flag = G_ERROR_FLG,
52439             error_text   = lv_message_text
52440     WHERE   EXISTS( SELECT 1
52441                     FROM   msc_st_apps_instances mai2
52442                     WHERE  mai2.sr_instance_code  = mai1.sr_instance_code
52443                     AND    mai2.process_flag      = G_IN_PROCESS
52444                     AND    NVL(mai2.message_id,NULL_VALUE) = NULL_VALUE
52445                     GROUP BY sr_instance_code HAVING COUNT(*) > 1)
52446     AND     mai1.process_flag             = G_IN_PROCESS
52447     AND     mai1.sr_instance_code            = v_instance_code
52448     AND     NVL(mai1.message_id,NULL_VALUE) = NULL_VALUE;
52449 
52450 
52451     SELECT msc_st_apps_instances_s.NEXTVAL
52452       INTO   lv_transaction_id
52453       FROM   dual;
52454 
52455 
52456         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Refresh ID: '||v_refresh_id);
52457 
52458 
52459       --Update the transaction_id and the who columns
52460 
52461       lv_sql_stmt :=
52462       'UPDATE   msc_st_apps_instances '
52463       ||' SET   st_transaction_id = :lv_transaction_id,'
52464       ||'       refresh_id        = '||v_refresh_id ||','
52465       ||'       last_update_date  = :v_current_date,'
52466       ||'       last_updated_by   = :v_current_user,'
52467       ||'       creation_date     = :v_current_date,'
52468       ||'       created_by        = :v_current_user'
52469       ||' WHERE sr_instance_code  = :v_instance_code';
52470 
52471 
52472         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52473 
52474       EXECUTE IMMEDIATE lv_sql_stmt
52475               USING     lv_transaction_id,
52476                         v_current_date,
52477                         v_current_user,
52478                         v_current_date,
52479                         v_current_user,
52480                         v_instance_code;
52481 
52482 
52483     lv_sql_stmt :=
52484       'UPDATE   msc_st_apps_instances '
52485       ||' SET  wsm_create_lbj_copy_routing = 2'
52486       ||' WHERE sr_instance_code  = :v_instance_code'
52487       ||' AND   process_flag      = '||G_IN_PROCESS
52488       ||' AND   wsm_create_lbj_copy_routing NOT IN (1,2)';
52489 
52490 
52491         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52492 
52493       EXECUTE IMMEDIATE lv_sql_stmt
52494               USING v_instance_code;
52495 
52496    -- set the message
52497    lv_return :=  MSC_ST_UTIL.GET_ERROR_MESSAGE
52498                       (p_app_short_name    => 'MSC',
52499                        p_error_code        => 'MSC_PP_INVALID_VALUE',
52500                        p_message_text      => lv_message_text,
52501                        p_error_text        => lv_error_text,
52502                        p_token1            => 'COLUMN_NAME',
52503                        p_token_value1      => 'MSC_ORG_FOR_BOM_EXPLOSION');
52504 
52505       IF lv_return <> 0 THEN
52506         RAISE ex_logging_err;
52507       END IF;
52508 
52509    -- derive validation_org_id
52510 
52511     lv_sql_stmt :=
52512     'UPDATE msc_st_apps_instances mai'
52513     ||' SET validation_org_id'
52514     ||' = (SELECT local_id'
52515     ||' FROM msc_local_id_setup '
52516     ||' WHERE  char1            = sr_instance_code'
52517     ||' AND    char3            = msc_org_for_bom_explosion'
52518     ||' AND    number1          = '|| G_ORGANIZATION
52519     ||' AND    entity_name      = ''SR_TP_ID'' )'
52520     ||' WHERE  sr_instance_code = :v_instance_code'
52521     ||' AND    NVL(validation_org_id,'||NULL_VALUE||') = '||NULL_VALUE
52522     ||' AND    process_flag     = '||G_IN_PROCESS;
52523 
52524 
52525         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52526 
52527       EXECUTE IMMEDIATE lv_sql_stmt
52528               USING     v_instance_code;
52529 
52530     -- error out the record if validation_org_id is not a valid organization_id
52531 
52532    lv_sql_stmt :=
52533       'UPDATE    msc_st_apps_instances mai'
52534       ||' SET    process_flag  = '||G_ERROR_FLG||','
52535       ||'        error_text    = '||''''||lv_message_text||''''
52536       ||' WHERE NVL(validation_org_id,'||NULL_VALUE||') = '||NULL_VALUE
52537       ||' AND   NVL(msc_org_for_bom_explosion,'||''''||NULL_CHAR||''''||')'
52538       ||'       <> '||''''||NULL_CHAR||''''
52539       ||' AND   sr_instance_code    = :v_instance_code'
52540       ||' AND   process_flag        = '||G_IN_PROCESS ;
52541 
52542 
52543         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52544 
52545       EXECUTE IMMEDIATE lv_sql_stmt
52546               USING     v_instance_code;
52547 
52548    lv_return := MSC_ST_UTIL.SET_PROCESS_FLAG
52549                      (p_table_name     => 'MSC_ST_APPS_INSTANCES',
52550                       p_instance_id    => v_instance_id,
52551                       p_instance_code  => v_instance_code,
52552                       p_process_flag   => G_VALID,
52553                       p_debug          => v_debug,
52554                       p_error_text     => lv_error_text);
52555 
52556       IF lv_return <> 0 THEN
52557         RAISE ex_logging_err;
52558       END IF;
52559 
52560 
52561     lv_sql_stmt :=
52562     'UPDATE     msc_apps_instances mai'
52563     ||' SET     (validation_org_id, lbj_details) = (SELECT validation_org_id, wsm_create_lbj_copy_routing'
52564     ||'         FROM  msc_st_apps_instances msai'
52565     ||'         WHERE msai.sr_instance_code = mai.instance_code'
52566     ||'         AND   msai.sr_instance_id   = mai.instance_id'
52567     ||'         AND   msai.process_flag =' || G_VALID ||')'
52568     ||'  WHERE  instance_code = :v_instance_code';
52569 
52570 
52571 
52572          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_DEBUG_1,lv_sql_stmt);
52573 
52574    EXECUTE IMMEDIATE lv_sql_stmt USING v_instance_code;
52575 
52576    COMMIT;
52577 
52578    EXCEPTION
52579     WHEN OTHERS THEN
52580      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,substr('MSC_CL_PRE_PROCESS.LOAD_PROFILES'||SQLERRM,1,240));
52581 
52582   END LOAD_PROFILES;
52583 
52584 
52585 
52586 
52587 
52588   --============= End Load Procedures=============
52589 
52590 
52591    FUNCTION SET_ST_STATUS( ERRBUF                   OUT NOCOPY VARCHAR2,
52592                            RETCODE                  OUT NOCOPY NUMBER,
52593                            p_instance_id            IN  NUMBER,
52594                            p_st_status              IN  NUMBER)
52595    RETURN BOOLEAN
52596    IS
52597 
52598    lv_staging_table_status NUMBER;
52599 
52600    BEGIN
52601 
52602    ---=================== PRE-PROCESSING ===========================
52603 
52604    IF p_st_status= G_ST_PRE_PROCESSING THEN
52605 
52606            SELECT mai.ST_STATUS
52607            INTO lv_staging_table_status
52608            FROM MSC_APPS_INSTANCES mai
52609            WHERE mai.INSTANCE_ID= p_instance_id
52610            FOR UPDATE;
52611 
52612          IF lv_staging_table_status= G_ST_PULLING THEN
52613            FND_MESSAGE.SET_NAME('MSC', 'MSC_ST_ERROR_PULLING');
52614            ERRBUF:= FND_MESSAGE.GET;
52615 
52616            RETCODE := G_ERROR;
52617            RETURN FALSE;
52618 
52619          ELSIF lv_staging_table_status= G_ST_COLLECTING THEN
52620            FND_MESSAGE.SET_NAME('MSC', 'MSC_ST_ERROR_LOADING');
52621            ERRBUF:= FND_MESSAGE.GET;
52622 
52623            RETCODE := G_ERROR;
52624            RETURN FALSE;
52625 
52626          ELSIF lv_staging_table_status= G_ST_PURGING THEN
52627 
52628            FND_MESSAGE.SET_NAME('MSC', 'MSC_ST_ERROR_PURGING');
52629            ERRBUF:= FND_MESSAGE.GET;
52630 
52631            RETCODE := G_ERROR;
52632            RETURN FALSE;
52633 
52634          ELSIF lv_staging_table_status= G_ST_PRE_PROCESSING THEN
52635 
52636            FND_MESSAGE.SET_NAME('MSC', 'MSC_ST_ERROR_PRE_PROCESSING');
52637            ERRBUF:= FND_MESSAGE.GET;
52638 
52639            RETCODE := G_ERROR;
52640            RETURN FALSE;
52641 
52642          ELSE
52643            RETCODE := G_SUCCESS;
52644 
52645            UPDATE MSC_APPS_INSTANCES
52646               SET ST_STATUS= G_ST_PRE_PROCESSING,
52647                   LCID= MSC_COLLECTION_S.NEXTVAL,
52648                   LAST_UPDATE_DATE= v_current_date,
52649                   LAST_UPDATED_BY= v_current_user,
52650                   REQUEST_ID= FND_GLOBAL.CONC_REQUEST_ID
52651             WHERE INSTANCE_ID= p_instance_id;
52652 
52653            SELECT MSC_COLLECTION_S.CURRVAL
52654              INTO v_last_collection_id
52655              FROM DUAL;
52656 
52657            RETURN TRUE;
52658 
52659          END IF;
52660 
52661    ---===================== READY ====================
52662    ELSIF p_st_status= G_ST_READY THEN
52663 
52664        UPDATE MSC_APPS_INSTANCES
52665           SET ST_STATUS= G_ST_READY,
52666               LAST_UPDATE_DATE= v_current_date,
52667               LAST_UPDATED_BY= v_current_user,
52668               REQUEST_ID= FND_GLOBAL.CONC_REQUEST_ID
52669         WHERE INSTANCE_ID= p_instance_id;
52670 
52671        RETCODE:= G_SUCCESS;
52672        RETURN TRUE;
52673 
52674    END IF;
52675 
52676    END SET_ST_STATUS;
52677 
52678    -- ============= LAUNCH WORKER ==============
52679 
52680    PROCEDURE LAUNCH_WORKER(  ERRBUF                OUT NOCOPY VARCHAR2,
52681                              RETCODE               OUT NOCOPY NUMBER,
52682                              p_monitor_request_id  IN  NUMBER,
52683                              p_instance_id         IN  NUMBER,
52684                              p_lcid                IN  NUMBER,
52685                              p_timeout             IN  NUMBER,
52686                              p_batch_size          IN  NUMBER DEFAULT 1000,
52687                              p_uom_class_enabled   IN  NUMBER DEFAULT SYS_NO,
52688                              p_item_cat_enabled    IN  NUMBER DEFAULT SYS_NO,
52689                              p_item_enabled        IN  NUMBER DEFAULT SYS_NO,
52690                              p_sup_cap_enabled     IN  NUMBER DEFAULT SYS_NO,
52691                              p_safety_stk_enabled  IN  NUMBER DEFAULT SYS_NO,
52692                              p_ship_mthd_enabled   IN  NUMBER DEFAULT SYS_NO,
52693                              p_sourcing_enabled    IN  NUMBER DEFAULT SYS_NO,
52694                              p_bom_enabled         IN  NUMBER DEFAULT SYS_NO,
52695                              p_rtg_enabled         IN  NUMBER DEFAULT SYS_NO,
52696                              p_resources_enabled   IN  NUMBER DEFAULT SYS_NO,
52697                              p_mat_sup_enabled     IN  NUMBER DEFAULT SYS_NO,
52698                              p_mat_dmd_enabled     IN  NUMBER DEFAULT SYS_NO,
52699                              p_reserve_enabled     IN  NUMBER DEFAULT SYS_NO,
52700                              p_res_dmd_enabled     IN  NUMBER DEFAULT SYS_NO,
52701                              p_item_cst_enabled    IN  NUMBER DEFAULT SYS_NO,
52702                              p_fiscal_cal_enabled  IN  NUMBER DEFAULT SYS_NO,
52703                              p_comp_cal_enabled    IN  NUMBER DEFAULT SYS_NO,
52704                              p_setup_enabled       IN  NUMBER DEFAULT SYS_NO,
52705                              p_item_rollup         IN  NUMBER DEFAULT SYS_YES,
52706                              p_level_value_enabled IN  NUMBER DEFAULT SYS_NO,
52707                              p_level_assoc_enabled IN  NUMBER DEFAULT SYS_NO,
52708                              p_booking_enabled     IN  NUMBER DEFAULT SYS_NO,
52709                              p_shipment_enabled    IN  NUMBER DEFAULT SYS_NO,
52710                              p_mfg_fct_enabled     IN  NUMBER DEFAULT SYS_NO,
52711                              p_list_price_enabled  IN  NUMBER DEFAULT SYS_NO,
52712                              p_cs_data_enabled     IN  NUMBER DEFAULT SYS_NO,
52713                              p_curr_conv_enabled   IN  NUMBER DEFAULT SYS_NO,
52714                              p_uom_conv_enabled    IN  NUMBER DEFAULT SYS_NO,
52715                              p_comp_users_enabled  IN  NUMBER DEFAULT SYS_NO,
52716                              p_item_substitute_enabled  IN  NUMBER DEFAULT SYS_NO,
52717                              p_cal_assignment_enabled   IN  NUMBER DEFAULT SYS_NO,
52718                              p_iro_enabled         IN  NUMBER DEFAULT SYS_NO,
52719                              p_ero_enabled         IN  NUMBER DEFAULT SYS_NO,
52720                              p_request_id          IN  NUMBER DEFAULT -1 )
52721 
52722        IS
52723 
52724    ----- TASK CONTROL --------------------------------------------------
52725 
52726    lv_task_number       NUMBER;    -- NEGATIVE: Unknown Error Occurs
52727                                    -- 99      : All Task Are Done (TASK_ALL_COMPLETED)
52728                                    -- POSITIVE: The Task Number
52729 
52730    lv_task_status       NUMBER;    -- ::OK  : THE TASK IS Done in MSC
52731                                    -- OTHERS: THE TASK Fails
52732 
52733    lv_message           VARCHAR2(240):='';
52734    lv_batch_id          NUMBER;
52735    lv_process_time      NUMBER;
52736 
52737    EX_PROCESS_TIME_OUT  EXCEPTION;
52738 
52739    ------ PIPE CONTROL ----------------------------------------------
52740 
52741    lv_pipe_ret_code     NUMBER;   -- The return value of Sending/Receiving Pipe Messages
52742 
52743    EX_PIPE_RCV          EXCEPTION;
52744    EX_PIPE_SND          EXCEPTION;
52745    BEGIN
52746    msc_util.print_trace_file_name(FND_GLOBAL.CONC_REQUEST_ID);
52747       -- ===== Switch on/ off debug based on MRP: Debug Profile
52748 
52749       v_debug := FND_PROFILE.VALUE('MRP_DEBUG') = 'Y';
52750 
52751       -- bug 3304390
52752       -- IF FND_PROFILE.VALUE('MRP_TRACE')  = 'Y' THEN
52753       --    dbms_session.set_sql_trace(true);
52754       -- END IF;
52755 
52756       ---========Initialize the global variables ========
52757 
52758       v_batch_size         := p_batch_size;
52759       v_res_dmd_enabled    := p_res_dmd_enabled;
52760       v_mat_dmd_enabled    := p_mat_dmd_enabled;
52761       v_reserve_enabled    := p_reserve_enabled;
52762       v_mat_sup_enabled    := p_mat_sup_enabled;
52763       v_rtg_enabled        := p_rtg_enabled;
52764       v_bom_enabled        := p_bom_enabled;
52765       v_resources_enabled  := p_resources_enabled;
52766       v_sourcing_enabled   := p_sourcing_enabled;
52767       v_item_cat_enabled   := p_item_cat_enabled;
52768       v_uom_class_enabled  := p_uom_class_enabled;
52769       v_sup_cap_enabled    := p_sup_cap_enabled;
52770       v_safety_stk_enabled := p_safety_stk_enabled;
52771       v_item_enabled       := p_item_enabled;
52772       v_ship_mthd_enabled  := p_ship_mthd_enabled;
52773       v_item_cst_enabled   := p_item_cst_enabled;
52774       v_comp_users_enabled := p_comp_users_enabled;
52775       V_item_substitute_enabled := p_item_substitute_enabled;
52776       v_cal_assignment_enabled := p_cal_assignment_enabled;
52777       v_iro_enabled        := p_iro_enabled;
52778       v_ero_enabled        := p_ero_enabled;
52779       v_request_id         := p_request_id;
52780 
52781 
52782       ---=========DP variables===========================
52783 
52784       v_fiscal_cal_enabled  := p_fiscal_cal_enabled;
52785       v_comp_cal_enabled    := p_comp_cal_enabled;
52786       v_setup_enabled       := p_setup_enabled;
52787       v_item_rollup         := p_item_rollup;
52788       v_level_value_enabled := p_level_value_enabled;
52789       v_level_assoc_enabled := p_level_assoc_enabled;
52790       v_booking_enabled     := p_booking_enabled ;
52791       v_shipment_enabled    := p_shipment_enabled;
52792       v_mfg_fct_enabled     := p_mfg_fct_enabled;
52793       v_list_price_enabled  := p_list_price_enabled;
52794       v_cs_data_enabled     := p_cs_data_enabled;
52795       v_curr_conv_enabled   := p_curr_conv_enabled;
52796       v_uom_conv_enabled    := p_uom_conv_enabled;
52797 
52798 
52799 
52800       v_monitor_request_id := p_monitor_request_id;
52801 
52802       PBS := TO_NUMBER( FND_PROFILE.VALUE('MRP_PURGE_BATCH_SIZE'));
52803 
52804       IF fnd_global.conc_request_id > 0 THEN
52805          v_cp_enabled:= SYS_YES;
52806       ELSE
52807          v_cp_enabled:= SYS_NO;
52808       END IF;
52809 
52810       INITIALIZE( p_instance_id);
52811 
52812       v_last_collection_id:= p_lcid;
52813 
52814       LOOP
52815 
52816          EXIT WHEN is_monitor_status_running <> SYS_YES;
52817 
52818          EXIT WHEN is_request_status_running <> SYS_YES;
52819 
52820           -- ============= Check the execution time ==============
52821 
52822           select (SYSDATE- START_TIME) into lv_process_time from dual;
52823           IF lv_process_time > p_timeout/1440.0 THEN Raise EX_PROCESS_TIME_OUT;
52824           END IF;
52825 
52826           -- ============= Get the Task from Task Que ==============
52827 
52828           lv_pipe_ret_code := DBMS_PIPE.RECEIVE_MESSAGE( v_pipe_task_que, PIPE_TIME_OUT);
52829 
52830           IF lv_pipe_ret_code<>0 THEN
52831 
52832              IF lv_pipe_ret_code<> 1 THEN Raise EX_PIPE_RCV; END IF;
52833 
52834           ELSE
52835 
52836              IF DBMS_PIPE.next_item_type = 0 then
52837 
52838                   dbms_lock.sleep(2); -- sleep for 2 seconds and then check again
52839 
52840              ELSE
52841 
52842                   DBMS_PIPE.UNPACK_MESSAGE(lv_message);
52843                   lv_task_number := get_task_num (lv_message);
52844 
52845 		 IF lv_task_number< 0 or lv_task_number = TASK_ALL_COMPLETED then
52846                    EXIT ;
52847 		 ELSE
52848 
52849    		-- ============= Execute the Task =============
52850 
52851                   lv_task_status := FAIL;
52852 
52853                   lv_batch_id := get_batch_id(lv_message);
52854                   EXECUTE_TASK( lv_task_status, lv_task_number, lv_batch_id );
52855 
52856                   IF lv_task_status <> OK THEN
52857                         DBMS_PIPE.PACK_MESSAGE( -lv_task_number);
52858                   ELSE
52859                         DBMS_PIPE.PACK_MESSAGE( lv_task_number);
52860                   END IF;
52861 
52862              	  IF DBMS_PIPE.SEND_MESSAGE( v_pipe_wm)<>0 THEN
52863                         RAISE EX_PIPE_SND;
52864                   END IF;
52865 
52866                   IF lv_task_status <> OK THEN
52867                         DBMS_LOCK.SLEEP( 2);
52868                   END IF;
52869 
52870              END IF;   -- lv_task_number
52871 
52872            END IF; -- no messages. Waiting for dependent tasks to come
52873 
52874          END IF; -- lv_pipe_ret_code
52875 
52876       END LOOP;
52877 
52878       IF lv_task_number = TASK_ALL_COMPLETED THEN
52879          COMMIT;
52880 
52881          DBMS_PIPE.PACK_MESSAGE( SYS_YES);
52882 
52883          IF DBMS_PIPE.SEND_MESSAGE( v_pipe_status)<>0 THEN
52884             RAISE EX_PIPE_SND;
52885          END IF;
52886 
52887          FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_SUCCEED');
52888          ERRBUF:= FND_MESSAGE.GET;
52889 
52890          IF v_flag=SYS_YES THEN
52891             RETCODE:= G_WARNING;
52892          ELSE
52893             RETCODE := G_SUCCESS;
52894          END IF;
52895 
52896       ELSE    -- unknown error occurs
52897 
52898          ROLLBACK;
52899 
52900          DBMS_PIPE.PACK_MESSAGE( SYS_YES);
52901 
52902          IF DBMS_PIPE.SEND_MESSAGE( v_pipe_status)<>0 THEN
52903             RAISE EX_PIPE_SND;
52904          END IF;
52905 
52906          FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_FAIL');
52907          ERRBUF:= FND_MESSAGE.GET;
52908 
52909          RETCODE := G_ERROR;
52910 
52911       END IF;
52912 
52913     EXCEPTION
52914 
52915       WHEN EX_PIPE_RCV THEN
52916         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,dbms_utility.format_error_stack);
52917 
52918          ROLLBACK;
52919 
52920          FND_MESSAGE.SET_NAME('MSC', 'MSC_MSG_RCV_FAIL');
52921          FND_MESSAGE.SET_TOKEN('PIPE', v_pipe_mw);
52922          ERRBUF:= FND_MESSAGE.GET;
52923          RETCODE := G_ERROR;
52924          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, ERRBUF);
52925 
52926          -- send a message of 'unresolvable error' to monitor
52927          DBMS_PIPE.PACK_MESSAGE( UNRESOVLABLE_ERROR);
52928 
52929          IF DBMS_PIPE.SEND_MESSAGE( v_pipe_wm)<>0 THEN
52930             FND_MESSAGE.SET_NAME('MSC', 'MSC_MSG_SEND_FAIL');
52931             FND_MESSAGE.SET_TOKEN('PIPE', v_pipe_wm);
52932             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
52933          END IF;
52934 
52935       WHEN EX_PIPE_SND THEN
52936       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,dbms_utility.format_error_stack);
52937 
52938          ROLLBACK;
52939 
52940          FND_MESSAGE.SET_NAME('MSC', 'MSC_MSG_SEND_FAIL');
52941          FND_MESSAGE.SET_TOKEN('PIPE', v_pipe_wm);
52942          ERRBUF:= FND_MESSAGE.GET;
52943          RETCODE := G_ERROR;
52944          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,ERRBUF);
52945 
52946          -- send a message of 'unresolavable error' to monitor
52947          DBMS_PIPE.PACK_MESSAGE( UNRESOVLABLE_ERROR);
52948 
52949          IF DBMS_PIPE.SEND_MESSAGE( v_pipe_wm)<>0 THEN
52950             FND_MESSAGE.SET_NAME('MSC', 'MSC_MSG_SEND_FAIL');
52951             FND_MESSAGE.SET_TOKEN('PIPE', v_pipe_wm);
52952             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
52953          END IF;
52954 
52955       WHEN EX_PROCESS_TIME_OUT THEN
52956       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,dbms_utility.format_error_stack);
52957 
52958          ROLLBACK;
52959 
52960          FND_MESSAGE.SET_NAME('MSC', 'MSC_TIMEOUT');
52961          ERRBUF:= FND_MESSAGE.GET;
52962          RETCODE := G_ERROR;
52963          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, ERRBUF);
52964 
52965          -- send a message of 'unresolavable error' to monitor
52966          DBMS_PIPE.PACK_MESSAGE( UNRESOVLABLE_ERROR);
52967 
52968          IF DBMS_PIPE.SEND_MESSAGE( v_pipe_wm)<>0 THEN
52969             FND_MESSAGE.SET_NAME('MSC', 'MSC_MSG_SEND_FAIL');
52970             FND_MESSAGE.SET_TOKEN('PIPE', v_pipe_wm);
52971             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
52972          END IF;
52973 
52974       WHEN OTHERS THEN
52975         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,dbms_utility.format_error_stack);
52976          ROLLBACK;
52977 
52978          ERRBUF  := SQLERRM;
52979          RETCODE := G_ERROR;
52980 
52981          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, ERRBUF);
52982 
52983          -- send a message of 'unresolavable error' to monitor
52984          DBMS_PIPE.PACK_MESSAGE( UNRESOVLABLE_ERROR);
52985 
52986          IF DBMS_PIPE.SEND_MESSAGE( v_pipe_wm)<>0 THEN
52987             FND_MESSAGE.SET_NAME('MSC', 'MSC_MSG_SEND_FAIL');
52988             FND_MESSAGE.SET_TOKEN('PIPE', v_pipe_wm);
52989             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
52990          END IF;
52991 
52992          -- send a message of 'the worker ends its process' to monitor
52993          DBMS_PIPE.PACK_MESSAGE( SYS_YES);
52994 
52995          IF DBMS_PIPE.SEND_MESSAGE( v_pipe_status)<>0 THEN
52996             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, 'FAIL TO SEND MESSAGE');
52997          END IF;
52998 
52999    END LAUNCH_WORKER;
53000 
53001    -- ===============================================================
53002 
53003      PROCEDURE LAUNCH_MONITOR( ERRBUF              OUT NOCOPY VARCHAR2,
53004                              RETCODE               OUT NOCOPY NUMBER,
53005                              p_instance_id         IN  NUMBER,
53006                              p_timeout             IN  NUMBER DEFAULT 60,
53007                              p_batch_size          IN  NUMBER DEFAULT 1000,
53008                              p_total_worker_num    IN  NUMBER DEFAULT 3,
53009                              p_cal_enabled         IN  NUMBER DEFAULT SYS_NO,
53010                              p_dmd_class_enabled   IN  NUMBER DEFAULT SYS_NO,
53011                              p_tp_enabled          IN  NUMBER DEFAULT SYS_NO,
53012                              p_ctg_enabled         IN  NUMBER DEFAULT SYS_NO,
53013                              p_item_cat_enabled    IN  NUMBER DEFAULT SYS_NO,
53014                              p_uom_enabled         IN  NUMBER DEFAULT SYS_NO,
53015                              p_uom_class_enabled   IN  NUMBER DEFAULT SYS_NO,
53016                              p_desig_enabled       IN  NUMBER DEFAULT SYS_NO,
53017                              p_project_enabled     IN  NUMBER DEFAULT SYS_NO,
53018                              p_item_enabled        IN  NUMBER DEFAULT SYS_NO,
53019                              p_sup_cap_enabled     IN  NUMBER DEFAULT SYS_NO,
53020                              p_safety_stk_enabled  IN  NUMBER DEFAULT SYS_NO,
53021                              p_ship_mthd_enabled   IN  NUMBER DEFAULT SYS_NO,
53022                              p_sourcing_enabled    IN  NUMBER DEFAULT SYS_NO,
53023                              p_bom_enabled         IN  NUMBER DEFAULT SYS_NO,
53024                              p_rtg_enabled         IN  NUMBER DEFAULT SYS_NO,
53025                              p_resources_enabled   IN  NUMBER DEFAULT SYS_NO,
53026                              p_mat_sup_enabled     IN  NUMBER DEFAULT SYS_NO,
53027                              p_mat_dmd_enabled     IN  NUMBER DEFAULT SYS_NO,
53028                              p_reserve_enabled     IN  NUMBER DEFAULT SYS_NO,
53029                              p_res_dmd_enabled     IN  NUMBER DEFAULT SYS_NO,
53030                              p_item_cst_enabled    IN  NUMBER DEFAULT SYS_NO,
53031                              p_parent_request_id   IN  NUMBER DEFAULT -1,
53032                              p_fiscal_cal_enabled  IN  NUMBER DEFAULT SYS_NO,
53033                              p_setup_enabled       IN  NUMBER DEFAULT SYS_NO,
53034                              p_link_dummy          IN  VARCHAR2 DEFAULT NULL,
53035                              p_item_rollup         IN  NUMBER DEFAULT SYS_YES,
53036                              p_level_value_enabled IN  NUMBER DEFAULT SYS_NO,
53037                              p_level_assoc_enabled IN  NUMBER DEFAULT SYS_NO,
53038                              p_booking_enabled     IN  NUMBER DEFAULT SYS_NO,
53039                              p_shipment_enabled    IN  NUMBER DEFAULT SYS_NO,
53040                              p_mfg_fct_enabled     IN  NUMBER DEFAULT SYS_NO,
53041                              p_list_price_enabled  IN  NUMBER DEFAULT SYS_NO,
53042                              p_cs_data_enabled     IN  NUMBER DEFAULT SYS_NO,
53043                              p_cs_dummy            IN  VARCHAR2 DEFAULT NULL,
53044                              p_cs_refresh          IN  NUMBER DEFAULT SYS_NO,
53045                              p_curr_conv_enabled   IN  NUMBER DEFAULT SYS_NO,
53046                              p_uom_conv_enabled    IN  NUMBER DEFAULT SYS_NO ,
53047                              p_calling_module      IN  NUMBER DEFAULT G_APS,
53048                              p_comp_users_enabled  IN  NUMBER DEFAULT SYS_NO,
53049                              p_item_substitute_enabled     IN  NUMBER DEFAULT SYS_NO,
53050                              p_planners_enabled            IN  NUMBER DEFAULT SYS_NO,
53051                              p_comp_cal_enabled    IN  NUMBER DEFAULT SYS_NO,
53052                              p_profile_enabled     IN  NUMBER DEFAULT SYS_NO,
53053                              p_cal_assignment_enabled      IN  NUMBER DEFAULT SYS_NO,
53054                               p_iro_enabled         IN  NUMBER DEFAULT SYS_NO,
53055                               p_ero_enabled         IN  NUMBER DEFAULT SYS_NO)
53056 
53057 
53058    IS
53059 
53060    lc_i                         PLS_INTEGER;
53061    lv_batch_id                  PLS_INTEGER := 0;
53062    lv_task_number               PLS_INTEGER := 0;
53063    lv_message                   VARCHAR2(240):= '';
53064    lv_task_not_completed        PLS_INTEGER := 0;
53065 
53066    lv_process_time              PLS_INTEGER := 0;
53067 
53068    EX_PIPE_RCV                  EXCEPTION;
53069    EX_PIPE_SND                  EXCEPTION;
53070    EX_PROCESS_TIME_OUT          EXCEPTION;
53071    EX_SECOND_PASS               EXCEPTION;
53072 
53073    lv_pipe_ret_code             PLS_INTEGER := 0;
53074 
53075    lv_check_point               PLS_INTEGER := 0;
53076 
53077    lvs_request_id               NumTblTyp := NumTblTyp(0);
53078 
53079    lv_total_item_batches        PLS_INTEGER := 0;
53080    lv_total_supply_batches      PLS_INTEGER := 0;
53081    lv_total_demand_batches      PLS_INTEGER := 0;
53082    lv_total_lvalue_batches      PLS_INTEGER := 0;
53083 
53084    lv_item_batches_completed    PLS_INTEGER := 0;
53085    lv_supply_batches_completed  PLS_INTEGER := 0;
53086    lv_demand_batches_completed  PLS_INTEGER := 0;
53087    lv_lvalue_batches_completed  PLS_INTEGER := 0;
53088    lv_pipe_status               PLS_INTEGER := 0;
53089 
53090 
53091 
53092 
53093    lv_worker_committed          PLS_INTEGER := 0;
53094 
53095    lv_start_time                DATE;
53096 
53097    lv_collection_plan_exists    PLS_INTEGER := 0;
53098    lv_is_build_over             NUMBER;
53099    lv_monitor_id                PLS_INTEGER;
53100 
53101    lv_plan_per_profile                 NUMBER:=nvl(fnd_profile.value('MSD_PLANNING_PERCENTAGE'), G_NO_PLAN_PERCENTAGE);
53102    lv_retval                           BOOLEAN;
53103    lv_dummy1                           VARCHAR2(32) := '';
53104    lv_dummy2                           VARCHAR2(32) := '';
53105 
53106 
53107 
53108    CURSOR Warn_Flag IS
53109    SELECT dbs_ver
53110    FROM MSC_APPS_INSTANCES
53111    WHERE instance_id = p_instance_id;
53112 
53113 
53114    BEGIN
53115     msc_util.print_trace_file_name(FND_GLOBAL.CONC_REQUEST_ID);
53116      lv_retval := FND_INSTALLATION.GET_APP_INFO(
53117                    'MSD', lv_dummy1,lv_dummy2, v_applsys_schema);
53118 
53119      IF (lv_dummy1 = 'I' OR lv_dummy1 = 'S')THEN
53120             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Oracle Demand Planning Installed');
53121             v_install_msd := TRUE ;
53122      END IF;
53123 
53124 
53125     --=====set the value for sub-request============
53126      v_req_data := nvl(fnd_conc_global.request_data,G_NEW_REQUEST);
53127 
53128      --IF program resumes after completing the last activity(ie., planning
53129      --ODS load) then exit the legacy pre-processor. Whenever a child request
53130      --is submitted, parent exits to PAUSE mode so that the parent does'nt
53131      --consume any resources.
53132 
53133 
53134      IF v_req_data = G_DP_BOM_DATA THEN
53135 
53136        OPEN Warn_Flag;
53137         FETCH Warn_Flag INTO v_warning_flag;
53138        CLOSE Warn_Flag;
53139 
53140        IF v_warning_flag=SYS_YES THEN
53141           RETCODE:= G_WARNING;
53142        ELSE
53143           RETCODE := G_SUCCESS;
53144        END IF;
53145 
53146       RETURN;
53147      END IF;
53148 
53149 
53150       IF ((v_req_data=G_ODS_REQ_DATA) AND v_install_msd AND (lv_plan_per_profile=G_EXCLUDE_OPTION_CLASS) AND ( p_bom_enabled = SYS_YES) ) THEN
53151 
53152            lv_monitor_id := fnd_request.submit_request('MSC','MSDBCP',NULL,NULL,TRUE,p_instance_id);
53153 
53154             IF lv_monitor_id <> 0 THEN
53155               fnd_conc_global.set_req_globals(conc_status => 'PAUSED',
53156                                      request_data => to_char(G_DP_BOM_DATA)) ;
53157 
53158               commit;
53159 
53160             ELSE
53161 
53162               FND_MESSAGE.SET_NAME('MSC', 'MSC_DP_BOM_PROCESSING_FAIL');
53163               MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, FND_MESSAGE.GET);
53164               RETCODE := G_WARNING;
53165 
53166               commit;
53167 
53168             END IF;
53169 
53170             RETURN;
53171 
53172 ELSE
53173 
53174       IF v_req_data=G_ODS_REQ_DATA THEN
53175 
53176          OPEN Warn_Flag;
53177            FETCH Warn_Flag INTO v_warning_flag;
53178          CLOSE Warn_Flag;
53179 
53180          IF v_warning_flag=SYS_YES THEN
53181            RETCODE:= G_WARNING;
53182          ELSE
53183             RETCODE := G_SUCCESS;
53184          END IF;
53185 
53186         RETURN;
53187       END IF;
53188 
53189 END IF;
53190 
53191 
53192      -- ===== Switch on/ off debug based on MRP: Debug Profile
53193 
53194 --      v_debug := FND_PROFILE.VALUE('MRP_DEBUG') = 'Y';
53195 
53196       -- bug 3304390
53197       -- IF FND_PROFILE.VALUE('MRP_TRACE')  = 'Y' THEN
53198       --     dbms_session.set_sql_trace(true);
53199       -- END IF;
53200 
53201 -- Print the parameters coming in
53202    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_instance_id        : '||p_instance_id);
53203    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_batch_size         : '||p_batch_size);
53204    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_total_worker_num   : '||p_total_worker_num);
53205    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Calendars        : '||p_cal_enabled);
53206    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Trading Partners         : '||p_tp_enabled);
53207    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Demand Class  : '||p_dmd_class_enabled);
53208    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Cat Set       : '||p_ctg_enabled);
53209    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Product Categories   : '||p_item_cat_enabled);
53210    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'UOM        : '||p_uom_enabled);
53211    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'UOM Conversions  : '||p_uom_class_enabled);
53212    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Designators      : '||p_desig_enabled);
53213    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Projects and Tasks    : '||p_project_enabled);
53214    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Items       : '||p_item_enabled);
53215    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Shipping Method  : '||p_ship_mthd_enabled);
53216    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Supplier    : '||p_sup_cap_enabled);
53217    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Safety Stocks: '||p_safety_stk_enabled);
53218    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Sourcing Rules   : '||p_sourcing_enabled);
53219    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'BOM       : '||p_bom_enabled);
53220    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Routings        : '||p_rtg_enabled );
53221    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Resources  : '||p_resources_enabled );
53222    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Material Supplies   : '||p_mat_sup_enabled );
53223    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Material Demands    : '||p_mat_dmd_enabled );
53224    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Reservations    : '||p_reserve_enabled );
53225    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Resource Demands    : '||p_res_dmd_enabled);
53226    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Customer Items   : '||p_item_cst_enabled);
53227    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_parent_request_id  : '||p_parent_request_id);
53228    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_comp_users_enabled : '||p_comp_users_enabled);
53229    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Item Substitutes : '||p_item_substitute_enabled);
53230    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Planners        : '||p_planners_enabled);
53231    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Profils   : '||p_profile_enabled);
53232    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Calendar Assignments  : '||p_cal_assignment_enabled);
53233    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'IRO  : '||p_iro_enabled);
53234    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'ERO  : '||p_ero_enabled);
53235   -- DP Variables
53236    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Fiscal Calendars   : '||p_fiscal_cal_enabled);
53237    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_comp_cal_enabled   : '||p_comp_cal_enabled);
53238    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_setup_enabled        : '||p_setup_enabled);
53239    --Bug Fix 2813465  To enable the Item Rollup whenever Categories are loaded via self service loads.
53240    --MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_item_rollup          : '||p_item_rollup);
53241    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_level_value_enabled  : '||p_level_value_enabled);
53242    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_level_assoc_enabled  : '||p_level_assoc_enabled);
53243    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_booking_enabled      : '||p_booking_enabled);
53244    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_shipment_enabled     : '||p_shipment_enabled);
53245    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_mfg_fct_enabled      : '||p_mfg_fct_enabled);
53246    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_list_price_enabled   : '||p_list_price_enabled);
53247    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_cs_data_enabled      : '||p_cs_data_enabled);
53248    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_cs_refresh          : ' ||p_cs_refresh);
53249    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_curr_conv_enabled    : '||p_curr_conv_enabled);
53250    MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'p_uom_conv_enabled     : '||p_uom_conv_enabled);
53251 
53252 
53253 
53254 
53255      ---========Get the new refresh ID =================
53256      IF  v_req_data = G_NEW_REQUEST THEN
53257        UPDATE MSC_APPS_INSTANCES mai
53258        SET LRID= msc_cl_refresh_s.nextval, mai.DBS_VER= SYS_NO
53259        WHERE mai.INSTANCE_ID = p_instance_id;
53260        commit;
53261      END IF;
53262 
53263 
53264 
53265 
53266      ---========Initialize the global variables ========
53267 
53268      v_res_dmd_enabled    := p_res_dmd_enabled;
53269      v_mat_dmd_enabled    := p_mat_dmd_enabled;
53270      v_reserve_enabled    := p_reserve_enabled;
53271      v_mat_sup_enabled    := p_mat_sup_enabled;
53272      v_rtg_enabled        := p_rtg_enabled;
53273      v_bom_enabled        := p_bom_enabled;
53274      v_resources_enabled  := p_resources_enabled;
53275      v_sourcing_enabled   := p_sourcing_enabled;
53276      v_ctg_enabled        := p_ctg_enabled;
53277      v_sup_cap_enabled    := p_sup_cap_enabled;
53278      v_safety_stk_enabled := p_safety_stk_enabled;
53279      v_item_enabled       := p_item_enabled;
53280      v_tp_enabled         := p_tp_enabled ;
53281      v_dmd_class_enabled  := p_dmd_class_enabled ;
53282      v_uom_enabled        := p_uom_enabled;
53283      v_desig_enabled      := p_desig_enabled;
53284      v_project_enabled    := p_project_enabled;
53285      v_cal_enabled        := p_cal_enabled;
53286      v_item_cat_enabled   := p_item_cat_enabled ;
53287      v_uom_class_enabled  := p_uom_class_enabled ;
53288      v_ship_mthd_enabled  := p_ship_mthd_enabled ;
53289      v_item_cst_enabled   := p_item_cst_enabled;
53290      v_parent_request_id  := p_parent_request_id;
53291      v_comp_users_enabled := p_comp_users_enabled;
53292      v_item_substitute_enabled := p_item_substitute_enabled;
53293      v_planners_enabled        := p_planners_enabled;
53294      v_profile_enabled    := p_profile_enabled;
53295      v_cal_assignment_enabled := p_cal_assignment_enabled;
53296 
53297      ---DP variables
53298      v_fiscal_cal_enabled  := p_fiscal_cal_enabled;
53299      v_comp_cal_enabled  := p_comp_cal_enabled;
53300      v_setup_enabled       := p_setup_enabled;
53301      v_item_rollup         := p_item_cat_enabled; --Bug Fix 2813465 To enable the Item Rollup whenever Categories are loaded via self service loads.
53302      v_level_value_enabled := p_level_value_enabled;
53303      v_level_assoc_enabled := p_level_assoc_enabled;
53304      v_booking_enabled     := p_booking_enabled ;
53305      v_shipment_enabled    := p_shipment_enabled;
53306      v_mfg_fct_enabled     := p_mfg_fct_enabled;
53307      v_list_price_enabled  := p_list_price_enabled;
53308      v_cs_data_enabled     := p_cs_data_enabled;
53309      v_cs_refresh          := p_cs_refresh;
53310      v_curr_conv_enabled   := p_curr_conv_enabled;
53311      v_uom_conv_enabled    := p_uom_conv_enabled;
53312      v_iro_enabled        :=  p_iro_enabled;
53313      v_ero_enabled        :=  p_ero_enabled;
53314 
53315 
53316 
53317      v_batch_size := p_batch_size;
53318 
53319       INITIALIZE( p_instance_id);
53320 
53321      -- Initializing v_request_id with the p_parent_request_id if
53322      -- OA upload else with   current request id of the pre process
53323      -- monitor using fnd_global.conc_request_id.
53324      IF nvl(p_parent_request_id, -1) < 1 THEN
53325        v_request_id := -(fnd_global.conc_request_id);
53326      ELSE
53327        v_request_id := v_parent_request_id;
53328      END IF;
53329 
53330      -- for L-flow Initialize the parameters
53331      prec.purge_ods_flag          := SYS_NO;
53332      prec.app_supp_cap_flag       := SYS_NO;
53333      prec.atp_rules_flag          := SYS_NO;
53334      prec.bom_flag                := SYS_NO;
53335      prec.bor_flag                := SYS_NO;
53336      prec.calendar_flag           := SYS_NO;
53337      prec.demand_class_flag       := SYS_NO;
53338      prec.forecast_flag           := SYS_NO;
53339      prec.item_flag               := SYS_NO;
53340      prec.kpi_bis_flag            := SYS_NO;
53341      prec.mds_flag                := SYS_NO;
53342      prec.mps_flag                := SYS_NO;
53343      prec.oh_flag                 := SYS_NO;
53344      prec.parameter_flag          := SYS_NO;
53345      prec.planner_flag            := SYS_NO;
53346      prec.item_subst_flag         := SYS_NO;
53347      prec.project_flag            := SYS_NO;
53348      prec.po_flag                 := SYS_NO;
53349      prec.reserves_flag           := SYS_NO;
53350      prec.resource_nra_flag       := SYS_NO;
53351      prec.saf_stock_flag          := SYS_NO;
53352      prec.sales_order_flag        := SYS_NO;
53353      prec.source_hist_flag        := SYS_NO;
53354      prec.sourcing_rule_flag      := SYS_NO;
53355      prec.sub_inventory_flag      := SYS_NO;
53356      prec.tp_customer_flag        := SYS_NO;
53357      prec.tp_vendor_flag          := SYS_NO;
53358      prec.unit_number_flag        := SYS_NO;
53359      prec.uom_flag                := SYS_NO;
53360      prec.user_supply_demand_flag := SYS_NO;
53361      prec.wip_flag                := SYS_NO;
53362      prec.user_company_flag       := G_COMP_USR_NO;
53363 
53364 
53365     -- end of changes for L flow
53366 
53367      IF fnd_global.conc_request_id > 0 THEN
53368         v_cp_enabled:= SYS_YES;
53369      ELSE
53370         v_cp_enabled:= SYS_NO;
53371      END IF;
53372 
53373      lv_check_point:= 1;
53374      IF v_req_data = G_NEW_REQUEST THEN
53375 
53376        -- OA upload changes, status is already set in JCP for OA i.e. nvl(v_parent_request_id, -1) >0
53377        IF nvl(v_parent_request_id, -1) < 1 THEN
53378          IF SET_ST_STATUS( ERRBUF, RETCODE, p_instance_id, G_ST_PRE_PROCESSING) THEN
53379           COMMIT;
53380           lv_check_point:= 2;
53381          ELSE
53382           ROLLBACK;
53383           RETURN;
53384          END IF;
53385        END IF;
53386 
53387        IF NOT SNAPSHOT_TABLES then
53388         ROLLBACK;
53389         RETURN;
53390        END IF;
53391 
53392        CREATE_INDEXES;
53393      END IF;
53394 
53395 
53396      --=========== call the main tasks here===================
53397 
53398    IF v_req_data IN (G_NEW_REQUEST,G_CAL_REQ_DATA) THEN
53399 
53400     --------------  Get current ERROR_ID from MSC_ERRORS ---
53401      IF (nvl(v_parent_request_id,-1) < 1) THEN
53402         SELECT max(ERROR_ID) INTO v_error_no_begin FROM MSC_ERRORS;
53403      END IF;
53404 
53405 
53406      -- initiaize global variables
53407      v_total_worker_num := p_total_worker_num;
53408      -- calendar
53409      IF p_cal_enabled = SYS_YES and v_req_data = G_NEW_REQUEST THEN
53410        LOAD_CALENDAR;
53411      ELSE
53412        v_cal_build := False;
53413      END IF;
53414 
53415      IF v_cal_build THEN
53416        RETURN;
53417      END IF;
53418 
53419      IF (p_cal_enabled = SYS_YES) THEN
53420         LOAD_MFG_TIME ;
53421      END IF;
53422 
53423 
53424      -- unit of measures
53425       IF p_uom_enabled = SYS_YES THEN
53426          LOAD_UOM;
53427       END IF;
53428      -- demand class
53429 
53430      IF p_dmd_class_enabled = SYS_YES THEN
53431        LOAD_DEMAND_CLASS;
53432      END IF;
53433 
53434      -- trading partner
53435      IF p_tp_enabled = SYS_YES THEN
53436        LOAD_TRADING_PARTNERS;
53437      END IF;
53438 
53439       -- Profile Values
53440 
53441      IF p_profile_enabled = SYS_YES THEN
53442        LOAD_PROFILES;
53443      END IF;
53444 
53445      -- Planners
53446 
53447      IF p_planners_enabled = SYS_YES THEN
53448        LOAD_PLANNERS;
53449      END IF;
53450 
53451 
53452      -- category sets
53453      IF p_ctg_enabled = SYS_YES THEN
53454        LOAD_CATEGORY_SET;
53455      END IF;
53456      -- unit of measures
53457   --   IF p_uom_enabled = SYS_YES THEN
53458   --     LOAD_UOM;
53459   --   END IF;
53460      -- designators
53461      IF p_desig_enabled = SYS_YES THEN
53462        LOAD_DESIGNATOR;
53463      END IF;
53464      -- project and tasks
53465      IF p_project_enabled = SYS_YES THEN
53466        LOAD_PROJ_TASK;
53467      END IF;
53468 
53469      -- DP Setup Parameters
53470       IF v_setup_enabled = SYS_YES THEN
53471         LOAD_SETUP_PARAMETER;
53472       END IF ;
53473 
53474      -- process items/ supply / demands records and error out duplicate records for batch
53475      check_duplicates(ERRBUF, RETCODE);
53476      IF RETCODE = G_ERROR THEN
53477         ROLLBACK;
53478         RETURN;
53479      END IF;
53480 
53481      -- fix for bug 2788719
53482      IF is_bis_periods_load_reqd = SYS_YES THEN
53483         prec.kpi_bis_flag:=SYS_YES;
53484      END IF;
53485 
53486      --- Purge the existing pipes so that new pipes would be created
53487      DBMS_PIPE.PURGE( v_pipe_task_que);
53488      DBMS_PIPE.PURGE( v_pipe_wm);
53489      DBMS_PIPE.PURGE( v_pipe_mw);
53490      DBMS_PIPE.PURGE( v_pipe_status);
53491 
53492      -- ============ Lauch the Workers here ===============
53493 
53494      lvs_request_id.EXTEND( p_total_worker_num);
53495 
53496      IF v_cp_enabled= SYS_YES THEN
53497 
53498      FOR lc_i IN 1..p_total_worker_num LOOP
53499        lvs_request_id(lc_i) := FND_REQUEST.SUBMIT_REQUEST(
53500                           'MSC',
53501                           'MSCPPW', /* launch worker procedure called */
53502                           NULL,  -- description
53503                           NULL,  -- start date
53504                           FALSE, -- TRUE,
53505                           FND_GLOBAL.CONC_REQUEST_ID,
53506                           p_instance_id,
53507                           v_last_collection_id,
53508                           p_timeout,
53509                           p_batch_size,
53510                           p_uom_class_enabled,
53511                           p_item_cat_enabled,
53512                           p_item_enabled,
53513                           p_sup_cap_enabled,
53514                           p_safety_stk_enabled,
53515                           p_ship_mthd_enabled,
53516                           p_sourcing_enabled,
53517                           p_bom_enabled,
53518                           p_rtg_enabled,
53519                           p_resources_enabled,
53520                           p_mat_sup_enabled,
53521                           p_mat_dmd_enabled,
53522                           p_reserve_enabled,
53523                           p_res_dmd_enabled,
53524                           p_item_cst_enabled,
53525                           p_fiscal_cal_enabled,
53526                           p_comp_cal_enabled,
53527                           p_setup_enabled,
53528                           v_item_rollup,  --Bug Fix 2813465 To enable the Item Rollup whenever Categories are loaded via self service loads.
53529                           v_level_value_enabled,
53530                           p_level_assoc_enabled,
53531                           p_booking_enabled,
53532                           p_shipment_enabled,
53533                           p_mfg_fct_enabled,
53534                           p_list_price_enabled,
53535                           p_cs_data_enabled,
53536                           p_curr_conv_enabled,
53537                           p_uom_conv_enabled,
53538                           p_comp_users_enabled,
53539                           p_item_substitute_enabled,
53540                           p_cal_assignment_enabled,
53541                           p_iro_enabled,
53542                           p_ero_enabled,
53543                           v_request_id);
53544 
53545 
53546        COMMIT;
53547 
53548        IF lvs_request_id(lc_i)= 0 THEN
53549 
53550        ---- If the request_id=0 then
53551        ---- 1. Restore the status of the staging table.
53552        ---- 2. Send termination messages to the other launched workers.
53553        ---- 3. Return.
53554 
53555           ROLLBACK;
53556 
53557           IF SET_ST_STATUS( ERRBUF, RETCODE, p_instance_id, G_ST_READY) THEN
53558               COMMIT;
53559           END IF;
53560 
53561           FOR lc_i IN 1..p_total_worker_num LOOP
53562 
53563               DBMS_PIPE.PACK_MESSAGE( '-1');
53564 
53565               IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53566                  RAISE EX_PIPE_SND;
53567               END IF;
53568 
53569           END LOOP;
53570 
53571           FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_LAUNCH_WORKER_FAIL');
53572           ERRBUF:= FND_MESSAGE.GET;
53573           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,ERRBUF);
53574           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,dbms_utility.format_error_stack);
53575           RETCODE := G_ERROR;
53576 
53577           COMMIT;
53578           RETURN;
53579        ELSE
53580           FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_WORKER_REQUEST_ID');
53581           FND_MESSAGE.SET_TOKEN('REQUEST_ID', lvs_request_id(lc_i));
53582           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
53583           --MSC_UTIL.print_trace_file_name(lvs_request_id(lc_i));
53584        END IF;
53585 
53586      END LOOP;
53587 
53588      ELSE -- CP not enabled
53589 
53590           COMMIT;
53591 
53592      END IF;  -- CP enabled
53593 
53594 
53595 
53596 
53597      -- ============ Send Tasks to Task Que 'v_pipe_task_que' =============
53598 
53599      lv_message := to_char(TASK_BIS_PERIODS);
53600      DBMS_PIPE.PACK_MESSAGE(lv_message );
53601      IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53602            RAISE EX_PIPE_SND;
53603      END IF;
53604      MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_BIS_PERIODS: '||lv_message);
53605      lv_task_not_completed := lv_task_not_completed + 1;
53606 
53607 
53608      IF v_item_enabled = SYS_YES THEN
53609 
53610      BEGIN
53611 
53612      DECLARE
53613        TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
53614        lb_rowid       RowidTab;
53615        lb_batch       NumTblTyp := NumTblTyp(0);
53616        lv_batch_id    PLS_INTEGER;
53617        lv_batch_start PLS_INTEGER;
53618        lv_batch_last  PLS_INTEGER;
53619        cursor c2 is SELECT rowid FROM MSC_ST_SYSTEM_ITEMS
53620        WHERE  PROCESS_FLAG IN (G_IN_PROCESS,G_ERROR_FLG)
53621        AND    NVL(batch_id,NULL_VALUE) = NULL_VALUE
53622        AND    sr_instance_code   = v_instance_code;
53623 
53624      BEGIN
53625 
53626        OPEN c2;
53627        FETCH c2 BULK COLLECT INTO lb_rowid ;
53628 
53629        IF c2%ROWCOUNT > 0  THEN
53630 
53631          SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
53632          lv_batch_start := lv_batch_id;
53633          lb_batch.extend(lb_rowid.count);
53634 
53635          FOR j IN lb_rowid.FIRST..lb_rowid.LAST LOOP
53636 
53637            lb_batch(j):= lv_batch_id;
53638            IF MOD(j,v_batch_size) = 0 THEN
53639               SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
53640            END IF;
53641 
53642          END LOOP;
53643 
53644          lv_batch_last := lv_batch_id;
53645 
53646          FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
53647          UPDATE MSC_ST_SYSTEM_ITEMS
53648             SET batch_id = lb_batch(j)
53649             WHERE  rowid = lb_rowid(j);
53650 
53651          commit;
53652 
53653          CREATE_INDEXES('ITEM');
53654          -- send out the batches to the workers' task Pipe
53655 
53656          FOR j in lv_batch_start..lv_batch_last LOOP
53657            lv_message := to_char(TASK_ITEM)||','||to_char(j);
53658            DBMS_PIPE.PACK_MESSAGE(lv_message );
53659            IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53660                  RAISE EX_PIPE_SND;
53661            END IF;
53662            MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_ITEM: '||lv_message);
53663            lv_total_item_batches := lv_total_item_batches + 1;
53664            lv_task_not_completed := lv_task_not_completed + 1;
53665          END LOOP;
53666 
53667        ELSE
53668          lv_message := to_char(TASK_ITEM)||','||to_char(0);
53669          DBMS_PIPE.PACK_MESSAGE(lv_message );
53670          IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53671                 RAISE EX_PIPE_SND;
53672          END IF;
53673          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_ITEM: '||lv_message);
53674          lv_total_item_batches := lv_total_item_batches + 1;
53675          lv_task_not_completed := lv_task_not_completed + 1;
53676 
53677 
53678        END IF;
53679 
53680        CLOSE c2;
53681      EXCEPTION
53682        when others then
53683          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Error in Item Batch ID generation: ' || SQLERRM);
53684          lv_message := to_char(TASK_ITEM)||','||to_char(0);
53685          DBMS_PIPE.PACK_MESSAGE(lv_message );
53686          IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53687                 RAISE EX_PIPE_SND;
53688          END IF;
53689          MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_ITEM: '||lv_message);
53690          lv_total_item_batches := lv_total_item_batches + 1;
53691          lv_task_not_completed := lv_task_not_completed + 1;
53692      END;
53693 
53694      END;
53695 
53696      ELSE
53697         -- kludge to launch the dependent tasks even if
53698         -- item is not enabled for processing
53699 
53700         lv_message := to_char(TASK_ITEM)||','||to_char(0);
53701         DBMS_PIPE.PACK_MESSAGE(lv_message );
53702         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53703                    RAISE EX_PIPE_SND;
53704          END IF;
53705         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_ITEM: '||lv_message);
53706         lv_total_item_batches := lv_total_item_batches + 1;
53707         lv_task_not_completed := lv_task_not_completed + 1;
53708      END IF;
53709 
53710      DBMS_LOCK.SLEEP( 5);   -- initial estimated sleep time
53711 
53712      LOOP
53713 
53714          EXIT WHEN is_request_status_running <> SYS_YES;
53715 
53716          EXIT WHEN is_worker_status_valid(lvs_request_id) <> SYS_YES;
53717 
53718          lv_pipe_ret_code:= DBMS_PIPE.RECEIVE_MESSAGE( v_pipe_wm, PIPE_TIME_OUT);
53719 
53720          IF lv_pipe_ret_code=0 THEN
53721            IF DBMS_PIPE.next_item_type <> 0 then
53722 
53723               DBMS_PIPE.UNPACK_MESSAGE( lv_task_number );
53724 
53725               IF lv_task_number>0 THEN   -- If it's ok, the value is the task number
53726                 lv_task_not_completed := lv_task_not_completed -1;
53727 
53728                 -- ======= Add the dependent Tasks to the task queue ==========
53729 
53730                 IF lv_task_number= TASK_ITEM THEN
53731                   -- check whether all the item batches are completed
53732                   -- if yes then put the dependent tasks in the task pipe
53733 
53734                   lv_item_batches_completed := lv_item_batches_completed + 1;
53735 
53736                   IF lv_item_batches_completed = lv_total_item_batches THEN
53737                          /* Doing the piece dealing with items commented in Load_Items for the bug fix 2191021 */
53738                         IF v_item_enabled = SYS_YES THEN
53739                           LOAD_ITEM_ABC_CLASSES;
53740                         END IF;
53741 
53742                         IF p_comp_users_enabled = SYS_YES AND v_MSC_CONFIGURATION IN(2,3) THEN
53743                           lv_message := to_char(TASK_COMPANY_USERS);
53744                           DBMS_PIPE.PACK_MESSAGE(lv_message);
53745                           IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53746                                 RAISE EX_PIPE_SND;
53747                           END IF;
53748                           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_COMPANY_USERS: '||lv_message);
53749                           lv_task_not_completed := lv_task_not_completed + 1;
53750                         END IF;
53751 
53752                         IF p_uom_class_enabled = SYS_YES THEN
53753                           lv_message := to_char(TASK_UOM_CONV);
53754                           DBMS_PIPE.PACK_MESSAGE(lv_message);
53755                           IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53756                                 RAISE EX_PIPE_SND;
53757                           END IF;
53758                           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_UOM_CONV: '||lv_message);
53759                           lv_task_not_completed := lv_task_not_completed + 1;
53760                         END IF;
53761 
53762                         IF p_item_cst_enabled = SYS_YES  AND v_sce_installed THEN
53763                           lv_message := to_char(TASK_ITEM_CST);
53764                           DBMS_PIPE.PACK_MESSAGE(lv_message);
53765                           IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53766                                 RAISE EX_PIPE_SND;
53767                           END IF;
53768                           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_ITEM_CST: '||lv_message);
53769                           lv_task_not_completed := lv_task_not_completed + 1;
53770                         END IF;
53771 
53772 
53773                         lv_message := to_char(TASK_SUPPLIER_CAPACITY);
53774                         DBMS_PIPE.PACK_MESSAGE(lv_message);
53775                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53776 		               	RAISE EX_PIPE_SND;
53777                         END IF;
53778                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_SUPPLIER_CAPACITY: '||lv_message);
53779                         lv_task_not_completed := lv_task_not_completed + 1;
53780 
53781                         lv_message := to_char(TASK_CAL_ASSIGNMENTS);
53782                         DBMS_PIPE.PACK_MESSAGE(lv_message);
53783                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53784 		               	RAISE EX_PIPE_SND;
53785                         END IF;
53786                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_CAL_ASSIGNMENTS: '||lv_message);
53787                         lv_task_not_completed := lv_task_not_completed + 1;
53788 
53789                         lv_message := to_char(TASK_SAFETY_STOCK);
53790                         DBMS_PIPE.PACK_MESSAGE(lv_message);
53791                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53792 		               	RAISE EX_PIPE_SND;
53793                         END IF;
53794                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_SAFETY_STOCK: '||lv_message);
53795                         lv_task_not_completed := lv_task_not_completed + 1;
53796 
53797 
53798                         lv_message := to_char(TASK_CATEGORIES_SOURCING);
53799                         DBMS_PIPE.PACK_MESSAGE(lv_message);
53800                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53801                              RAISE EX_PIPE_SND;
53802                         END IF;
53803                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_CATEGORIES_SOURCING: '||lv_message);
53804                         lv_task_not_completed := lv_task_not_completed + 1;
53805 
53806                         lv_message := to_char(TASK_DEPT_RESOURCES);
53807                         DBMS_PIPE.PACK_MESSAGE(lv_message);
53808                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53809                              RAISE EX_PIPE_SND;
53810                         END IF;
53811                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_DEPT_RESOURCES: '||lv_message);
53812                         lv_task_not_completed := lv_task_not_completed + 1;
53813 
53814                         lv_message := to_char(TASK_ITEM_SUBSTITUTE);
53815                         DBMS_PIPE.PACK_MESSAGE(lv_message);
53816                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53817                               RAISE EX_PIPE_SND;
53818                         END IF;
53819                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_ITEM_SUBSTITUTE: '||lv_message);
53820                         lv_task_not_completed := lv_task_not_completed + 1;
53821 
53822 
53823                         IF p_fiscal_cal_enabled = SYS_YES THEN
53824                           lv_message := to_char(TASK_FISCAL_TIME);
53825                           DBMS_PIPE.PACK_MESSAGE(lv_message);
53826                           IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53827                                 RAISE EX_PIPE_SND;
53828                           END IF;
53829                           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_FISCAL_TIME: '||lv_message);
53830                           lv_task_not_completed := lv_task_not_completed + 1;
53831                         END IF;
53832 
53833 
53834                        IF p_comp_cal_enabled = SYS_YES THEN
53835                           lv_message := to_char(TASK_COMPOSITE_TIME);
53836                           DBMS_PIPE.PACK_MESSAGE(lv_message);
53837                           IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53838                                 RAISE EX_PIPE_SND;
53839                           END IF;
53840                           MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_COMPOSITE_TIME: '||lv_message);
53841                           lv_task_not_completed := lv_task_not_completed + 1;
53842                         END IF;
53843 
53844 
53845 
53846                         IF p_mat_sup_enabled = SYS_YES THEN
53847                           -- divide material supply into batches and put each batch
53848                           -- in the task pipe so that these can be picked up by the workers
53849                           BEGIN
53850 
53851                           DECLARE
53852                             TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
53853                             lb_rowid       RowidTab;
53854                             lb_batch       NumTblTyp := NumTblTyp(0);
53855                             lv_batch_id    PLS_INTEGER;
53856                             lv_batch_start PLS_INTEGER;
53857                             lv_batch_last  PLS_INTEGER;
53858                             cursor c2 is SELECT rowid FROM msc_st_supplies
53859                             WHERE  PROCESS_FLAG IN(G_IN_PROCESS,G_ERROR_FLG)
53860                             AND    ORDER_TYPE  IN(1,2,5,8,11,12,18,73,74,87)
53861                             AND    NVL(batch_id,NULL_VALUE) = NULL_VALUE
53862                             AND    sr_instance_code   = v_instance_code;
53863 
53864                           BEGIN
53865 
53866                             OPEN c2;
53867                             FETCH c2 BULK COLLECT INTO lb_rowid ;
53868 
53869                             IF c2%ROWCOUNT > 0  THEN
53870 
53871                               SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
53872                               lv_batch_start := lv_batch_id;
53873                               lb_batch.extend(lb_rowid.count);
53874 
53875                               FOR j IN lb_rowid.FIRST..lb_rowid.LAST LOOP
53876 
53877                                 lb_batch(j):= lv_batch_id;
53878                                 IF MOD(j,v_batch_size) = 0 THEN
53879                                    SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
53880                                 END IF;
53881 
53882                               END LOOP;
53883 
53884                               lv_batch_last := lv_batch_id;
53885 
53886                               FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
53887                               UPDATE msc_st_supplies
53888                                  SET batch_id = lb_batch(j)
53889                                  WHERE  rowid = lb_rowid(j);
53890 
53891                               commit;
53892 
53893                               -- send out the batches to the workers' task Pipe
53894 
53895                               FOR j in lv_batch_start..lv_batch_last LOOP
53896                                 lv_message := to_char(TASK_MATERIAL_SUPPLY)||','||to_char(j);
53897                                 DBMS_PIPE.PACK_MESSAGE(lv_message);
53898                                 IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53899                                     RAISE EX_PIPE_SND;
53900                                 END IF;
53901                                 MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_MATERIAL_SUPPLY: '||lv_message);
53902                                 lv_total_supply_batches := lv_total_supply_batches + 1;
53903                                 lv_task_not_completed := lv_task_not_completed + 1;
53904                               END LOOP;
53905 
53906                               CREATE_INDEXES('SUPPLY');
53907 
53908                             END IF;
53909 
53910                             CLOSE c2;
53911                           EXCEPTION
53912                             when others then
53913                             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_WARNING,'Error in Supplies Batch ID generation: ' || SQLERRM);
53914                           END;
53915 
53916                           END;
53917 
53918                         END IF;
53919 
53920 
53921                         IF p_mat_dmd_enabled = SYS_YES THEN
53922                           -- divide material demand into batches and put each batch
53923                           -- in the task pipe so that these can be picked up by the workers
53924                           BEGIN
53925 
53926                           DECLARE
53927                             TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
53928                             lb_rowid       RowidTab;
53929                             lb_batch       NumTblTyp := NumTblTyp(0);
53930                             lv_batch_id    PLS_INTEGER;
53931                             lv_batch_start PLS_INTEGER;
53932                             lv_batch_last  PLS_INTEGER;
53933                             cursor c2 is SELECT rowid FROM msc_st_demands
53934                             WHERE PROCESS_FLAG IN (G_IN_PROCESS,G_ERROR_FLG)
53935                             AND   origination_type IN (7,8,29)
53936                             AND   NVL(batch_id,NULL_VALUE) = NULL_VALUE
53937                             AND   sr_instance_code   = v_instance_code;
53938 
53939                           BEGIN
53940 
53941                             OPEN c2;
53942                             FETCH c2 BULK COLLECT INTO lb_rowid ;
53943 
53944                             IF c2%ROWCOUNT > 0  THEN
53945 
53946                               SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
53947                               lv_batch_start := lv_batch_id;
53948                               lb_batch.extend(lb_rowid.count);
53949 
53950                               FOR j IN lb_rowid.FIRST..lb_rowid.LAST LOOP
53951 
53952                                 lb_batch(j):= lv_batch_id;
53953                                 IF MOD(j,v_batch_size) = 0 THEN
53954                                    SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
53955                                 END IF;
53956 
53957                               END LOOP;
53958 
53959                               lv_batch_last := lv_batch_id;
53960 
53961                               FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
53962                               UPDATE msc_st_demands
53963                                  SET batch_id = lb_batch(j)
53964                                  WHERE  rowid = lb_rowid(j);
53965 
53966                               commit;
53967 
53968                               -- send out the batches to the workers' task Pipe
53969 
53970                               FOR j in lv_batch_start..lv_batch_last LOOP
53971                                 lv_message := to_char(TASK_MATERIAL_DEMAND)||','||to_char(j);
53972                                 DBMS_PIPE.PACK_MESSAGE(lv_message);
53973                                 IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
53974                                     RAISE EX_PIPE_SND;
53975                                 END IF;
53976                                 MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_MATERIAL_DEMAND: '||lv_message);
53977                                 lv_total_demand_batches := lv_total_demand_batches + 1;
53978                                 lv_task_not_completed := lv_task_not_completed + 1;
53979                               END LOOP;
53980 
53981                               CREATE_INDEXES('DEMAND');
53982 
53983                             END IF;
53984 
53985                             CLOSE c2;
53986                           EXCEPTION
53987                             when others then
53988                             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Error in Demands Batch ID generation: ' || SQLERRM);
53989                           END;
53990 
53991                           END;
53992 
53993 
53994 
53995 
53996 
53997                      END IF;	-- Demand is not enabled
53998 
53999                   END IF; -- all items batches completed
54000 
54001                 ELSIF lv_task_number= TASK_ITEM_SUBSTITUTE THEN
54002 
54003                   IF p_mat_dmd_enabled = SYS_YES THEN
54004 
54005                           -- divide sales order demand into batches and put each batch
54006                           -- in the task pipe so that these can be picked up by the workers
54007                           BEGIN
54008 
54009                           DECLARE
54010                             TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
54011                             lb_rowid       RowidTab;
54012                             lb_batch       NumTblTyp := NumTblTyp(0);
54013                             lv_batch_id    PLS_INTEGER;
54014                             lv_batch_start PLS_INTEGER;
54015                             lv_batch_last  PLS_INTEGER;
54016                             cursor c2 is SELECT rowid FROM msc_st_sales_orders
54017                             WHERE PROCESS_FLAG IN (G_IN_PROCESS,G_ERROR_FLG)
54018                             AND   NVL(batch_id,NULL_VALUE) = NULL_VALUE
54019                             AND   sr_instance_code   = v_instance_code;
54020 
54021                           BEGIN
54022 
54023                             OPEN c2;
54024                             FETCH c2 BULK COLLECT INTO lb_rowid ;
54025 
54026                             IF c2%ROWCOUNT > 0  THEN
54027 
54028                               SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
54029                               lv_batch_start := lv_batch_id;
54030                               lb_batch.extend(lb_rowid.count);
54031 
54032                               FOR j IN lb_rowid.FIRST..lb_rowid.LAST LOOP
54033 
54034                                 lb_batch(j):= lv_batch_id;
54035                                 IF MOD(j,v_batch_size) = 0 THEN
54036                                    SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
54037                                 END IF;
54038 
54039                               END LOOP;
54040 
54041                               lv_batch_last := lv_batch_id;
54042 
54043                               FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
54044                               UPDATE msc_st_sales_orders
54045                                  SET batch_id = lb_batch(j)
54046                                  WHERE  rowid = lb_rowid(j);
54047 
54048                               commit;
54049 
54050                               -- send out the batches to the workers' task Pipe
54051 
54052                               FOR j in lv_batch_start..lv_batch_last LOOP
54053                                 lv_message := to_char(TASK_SO_DEMAND)||','||to_char(j);
54054                                 DBMS_PIPE.PACK_MESSAGE(lv_message);
54055                                 IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54056                                     RAISE EX_PIPE_SND;
54057                                 END IF;
54058                                 MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_SO_DEMAND: '||lv_message);
54059                                 lv_total_demand_batches := lv_total_demand_batches + 1;
54060                                 lv_task_not_completed := lv_task_not_completed + 1;
54061                               END LOOP;
54062 
54063                               CREATE_INDEXES('SO');
54064                             ELSE
54065                                 lv_message := to_char(TASK_SO_DEMAND)||','||to_char(0);
54066                                 DBMS_PIPE.PACK_MESSAGE(lv_message );
54067                                 IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54068                                    RAISE EX_PIPE_SND;
54069                                  END IF;
54070                                  MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_SO_DEMAND: '||lv_message);
54071                                  lv_total_demand_batches := lv_total_demand_batches + 1;
54072                                  lv_task_not_completed := lv_task_not_completed + 1;
54073                                  END IF;
54074 
54075                             CLOSE c2;
54076                           EXCEPTION
54077                             when others then
54078                             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Error in Sales Order Demands Batch ID generation: ' || SQLERRM);
54079                             lv_message := to_char(TASK_SO_DEMAND)||','||to_char(0);
54080                             DBMS_PIPE.PACK_MESSAGE(lv_message );
54081                             IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54082                               RAISE EX_PIPE_SND;
54083                              END IF;
54084                             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_SO_DEMAND: '||lv_message);
54085                             lv_total_demand_batches := lv_total_demand_batches + 1;
54086                             lv_task_not_completed := lv_task_not_completed + 1;
54087 
54088                           END;
54089 
54090                           END;
54091 
54092                   ELSE -- demand is not enabled
54093                   -- kludge to launch the dependent tasks even if
54094                   -- demand  is not enabled for processing
54095 
54096                       lv_message := to_char(TASK_SO_DEMAND)||','||to_char(0);
54097                       DBMS_PIPE.PACK_MESSAGE(lv_message );
54098                        IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54099                             RAISE EX_PIPE_SND;
54100                        END IF;
54101                       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_SO_DEMAND: '||lv_message);
54102                        lv_total_demand_batches := lv_total_demand_batches + 1;
54103                        lv_task_not_completed   := lv_task_not_completed + 1;
54104 
54105                   END IF; -- Demand is not enabled
54106 
54107                /* ELSIF lv_task_number= TASK_SO_DEMAND THEN
54108 
54109                     lv_demand_batches_completed := lv_demand_batches_completed + 1;
54110 
54111                        IF lv_demand_batches_completed = lv_total_demand_batches THEN
54112 
54113                           lv_message := to_char(TASK_RESERVATION);
54114                           DBMS_PIPE.PACK_MESSAGE(lv_message);
54115                           IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54116                                RAISE EX_PIPE_SND;
54117                           END IF;
54118                           LOG_MESSAGE('TASK_RESERVATION: '||lv_message);
54119 
54120                           lv_task_not_completed := lv_task_not_completed + 1;
54121                         END IF; -- all SO batches completed
54122                  */ -- task_reservation has been shifted to monitor
54123 
54124                 ELSIF lv_task_number= TASK_DEPT_RESOURCES THEN
54125 
54126                         lv_message := to_char(TASK_BOM_ROUTING);
54127                         DBMS_PIPE.PACK_MESSAGE(lv_message);
54128                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54129                              RAISE EX_PIPE_SND;
54130                         END IF;
54131                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_BOM_ROUTING: '||lv_message);
54132 
54133                         lv_task_not_completed := lv_task_not_completed + 1;
54134 
54135 
54136 
54137                 ELSIF lv_task_number= TASK_BOM_ROUTING THEN
54138 
54139                         lv_message := to_char(TASK_RESOURCE_DEMAND);
54140                         DBMS_PIPE.PACK_MESSAGE(lv_message);
54141                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54142                              RAISE EX_PIPE_SND;
54143                         END IF;
54144                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_RESOURCE_DEMAND: '||lv_message);
54145 
54146                         lv_task_not_completed := lv_task_not_completed + 1;
54147 
54148                          lv_message := to_char(TASK_IRO);
54149                         DBMS_PIPE.PACK_MESSAGE(lv_message);
54150                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54151                              RAISE EX_PIPE_SND;
54152                         END IF;
54153 
54154                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_IRO: '||lv_message);
54155                         lv_task_not_completed := lv_task_not_completed + 1;
54156 
54157                         lv_message := to_char(TASK_ERO);
54158                         DBMS_PIPE.PACK_MESSAGE(lv_message);
54159                         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54160                              RAISE EX_PIPE_SND;
54161                         END IF;
54162                         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_ERO: '||lv_message);
54163                         lv_task_not_completed := lv_task_not_completed + 1;
54164 
54165 
54166 
54167              --  END IF; -- lv_task_number= TASK_ITEM/ lv_task_number= TASK_DEPT_RESOURCES
54168 
54169                 ELSIF lv_task_number= TASK_CATEGORIES_SOURCING  THEN
54170 
54171                       IF p_level_value_enabled = SYS_YES THEN
54172                           -- divide level values into batches and put each batch
54173                           -- in the task pipe so that these can be picked up by the workers
54174                           BEGIN
54175 
54176                           DECLARE
54177                             TYPE RowidTab IS TABLE OF ROWID INDEX BY BINARY_INTEGER;
54178                             lb_rowid       RowidTab;
54179                             lb_batch       NumTblTyp := NumTblTyp(0);
54180                             lv_batch_id    PLS_INTEGER;
54181                             lv_batch_start PLS_INTEGER;
54182                             lv_batch_last  PLS_INTEGER;
54183 
54184                             cursor c2 is SELECT rowid FROM msd_st_level_values
54185                             WHERE  PROCESS_FLAG IN(G_IN_PROCESS,G_ERROR_FLG)
54186                             AND    NVL(batch_id,NULL_VALUE) = NULL_VALUE
54187                             AND    sr_instance_code   = v_instance_code;
54188 
54189                           BEGIN
54190 
54191                             OPEN c2;
54192                             FETCH c2 BULK COLLECT INTO lb_rowid ;
54193 
54194                             IF c2%ROWCOUNT > 0  THEN
54195 
54196                               SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
54197                               lv_batch_start := lv_batch_id;
54198                               lb_batch.extend(lb_rowid.count);
54199 
54200                               FOR j IN lb_rowid.FIRST..lb_rowid.LAST LOOP
54201 
54202                                 lb_batch(j):= lv_batch_id;
54203                                 IF MOD(j,v_batch_size) = 0 THEN
54204                                    SELECT msc_st_batch_id_s.NEXTVAL INTO lv_batch_id FROM dual;
54205                                 END IF;
54206 
54207                               END LOOP;
54208 
54209                               lv_batch_last := lv_batch_id;
54210 
54211                               FORALL j IN lb_rowid.FIRST..lb_rowid.LAST
54212                               UPDATE msd_st_level_values
54213                                  SET batch_id = lb_batch(j)
54214                                  WHERE  rowid = lb_rowid(j);
54215 
54216                               commit;
54217 
54218                               -- send out the batches to the workers' task Pipe
54219 
54220                               FOR j in lv_batch_start..lv_batch_last LOOP
54221                                 lv_message := to_char(TASK_LEVEL_VALUE)||','||to_char(j);
54222                                 DBMS_PIPE.PACK_MESSAGE(lv_message);
54223                                 IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54224                                     RAISE EX_PIPE_SND;
54225                                 END IF;
54226                                 MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_SO_DEMAND: '||lv_message);
54227                                 lv_total_lvalue_batches := lv_total_lvalue_batches + 1;
54228                                 lv_task_not_completed := lv_task_not_completed + 1;
54229                               END LOOP;
54230                            -- CREATE_INDEXES('LEVEL_VALUE');
54231                             ELSE
54232                                 lv_message := to_char(TASK_LEVEL_VALUE)||','||to_char(0);
54233                                 DBMS_PIPE.PACK_MESSAGE(lv_message );
54234                                 IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54235                                    RAISE EX_PIPE_SND;
54236                                  END IF;
54237                                  MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_LEVEL_VALUE: '||lv_message);
54238                                  lv_total_lvalue_batches := lv_total_lvalue_batches + 1;
54239                                  lv_task_not_completed    := lv_task_not_completed + 1;
54240                                  END IF;
54241 
54242                             CLOSE c2;
54243 
54244                           EXCEPTION
54245                             when others then
54246                             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'Error in Batch ID generation: ' || SQLERRM);
54247                             lv_message := to_char(TASK_LEVEL_VALUE)||','||to_char(0);
54248                             DBMS_PIPE.PACK_MESSAGE(lv_message );
54249                             IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54250                               RAISE EX_PIPE_SND;
54251                              END IF;
54252                             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_LEVEL_VALUE: '||lv_message);
54253                             lv_total_lvalue_batches := lv_total_lvalue_batches + 1;
54254                             lv_task_not_completed   := lv_task_not_completed + 1;
54255 
54256                            END;
54257                        END ;
54258 
54259                   ELSE -- level_value is not enabled
54260                   -- kludge to launch the dependent tasks even if
54261                   -- level_value  is not enabled for processing
54262 
54263                       lv_message := to_char(TASK_LEVEL_VALUE)||','||to_char(0);
54264                       DBMS_PIPE.PACK_MESSAGE(lv_message );
54265                        IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54266                             RAISE EX_PIPE_SND;
54267                        END IF;
54268                       MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'TASK_LEVEL_VALUE: '||lv_message);
54269                        lv_total_lvalue_batches     := lv_total_lvalue_batches + 1;
54270                        lv_task_not_completed       := lv_task_not_completed + 1;
54271 
54272                      END IF;	-- Level Value is not enabled
54273 
54274                ELSIF lv_task_number= TASK_LEVEL_VALUE THEN
54275                       lv_lvalue_batches_completed  := lv_lvalue_batches_completed+1 ;
54276 
54277                        IF lv_lvalue_batches_completed = lv_total_lvalue_batches THEN
54278 
54279 
54280                            IF v_level_assoc_enabled = SYS_YES THEN
54281                              DIVIDE_BATCHES (p_table_name  => 'MSD_ST_LEVEL_ASSOCIATIONS' ,
54282                                              p_task_name   => TASK_LEVEL_ASSOCIATION,
54283                                              p_task_not_completed =>lv_task_not_completed,
54284                                              p_pipe_status => lv_pipe_status );
54285                               IF(lv_pipe_status = 1) THEN
54286                                    RAISE EX_PIPE_SND;
54287                               END IF;
54288                            END IF;
54289 
54290 
54291                            IF v_booking_enabled = SYS_YES THEN
54292                              DIVIDE_BATCHES (p_table_name  => 'MSD_ST_BOOKING_DATA' ,
54293                                              p_task_name   => TASK_BOOKING_DATA,
54294                                              p_task_not_completed =>lv_task_not_completed,
54295                                              p_pipe_status => lv_pipe_status );
54296                               IF(lv_pipe_status = 1) THEN
54297                                    RAISE EX_PIPE_SND;
54298                               END IF;
54299 
54300                            END IF;
54301 
54302                            IF v_shipment_enabled = SYS_YES THEN
54303                              DIVIDE_BATCHES (p_table_name  => 'MSD_ST_SHIPMENT_DATA' ,
54304                                              p_task_name   => TASK_SHIPMENT_DATA,
54305                                              p_task_not_completed =>lv_task_not_completed,
54306                                              p_pipe_status => lv_pipe_status );
54307                               IF(lv_pipe_status = 1) THEN
54308                                    RAISE EX_PIPE_SND;
54309                               END IF;
54310                             END IF;
54311 
54312                            IF v_mfg_fct_enabled = SYS_YES THEN
54313                              DIVIDE_BATCHES (p_table_name  => 'MSD_ST_MFG_FORECAST' ,
54314                                              p_task_name   => TASK_MFG_FORECAST,
54315                                              p_task_not_completed =>lv_task_not_completed,
54316                                              p_pipe_status => lv_pipe_status );
54317                               IF(lv_pipe_status = 1) THEN
54318                                    RAISE EX_PIPE_SND;
54319                               END IF;
54320                             END IF;
54321 
54322                            IF v_list_price_enabled = SYS_YES THEN
54323                              DIVIDE_BATCHES (p_table_name  => 'MSD_ST_PRICE_LIST' ,
54324                                              p_task_name   => TASK_PRICE_LIST,
54325                                              p_task_not_completed =>lv_task_not_completed,
54326                                              p_pipe_status => lv_pipe_status );
54327 
54328                               IF(lv_pipe_status = 1) THEN
54329                                    RAISE EX_PIPE_SND;
54330                               END IF;
54331 /*
54332                              DIVIDE_BATCHES (p_table_name  => 'MSD_ST_ITEM_LIST_PRICE' ,
54333                                              p_task_name   => TASK_ITEM_LIST_PRICE,
54334                                              p_task_not_completed =>lv_task_not_completed,
54335                                              p_pipe_status => lv_pipe_status );
54336 
54337                               IF(lv_pipe_status = 1) THEN
54338                                    RAISE EX_PIPE_SND;
54339                               END IF;
54340 */
54341                            END IF;
54342 
54343                           IF v_cs_data_enabled = SYS_YES THEN
54344                              DIVIDE_BATCHES (p_table_name  => 'MSD_ST_CS_DATA' ,
54345                                              p_task_name   => TASK_CS_DATA,
54346                                              p_task_not_completed =>lv_task_not_completed,
54347                                              p_pipe_status => lv_pipe_status );
54348 
54349                               IF(lv_pipe_status = 1) THEN
54350                                    RAISE EX_PIPE_SND;
54351                               END IF;
54352 
54353                               END IF;
54354 
54355                            IF v_curr_conv_enabled = SYS_YES THEN
54356                              DIVIDE_BATCHES (p_table_name  => 'MSD_ST_CURRENCY_CONVERSIONS' ,
54357                                              p_task_name   => TASK_CURR_CONV,
54358                                              p_task_not_completed =>lv_task_not_completed,
54359                                              p_pipe_status => lv_pipe_status );
54360                               IF(lv_pipe_status = 1) THEN
54361                                    RAISE EX_PIPE_SND;
54362                               END IF;
54363 
54364                            END IF;
54365 
54366                            IF v_uom_conv_enabled = SYS_YES THEN
54367                              DIVIDE_BATCHES (p_table_name  => 'MSD_ST_UOM_CONVERSIONS' ,
54368                                              p_task_name   => TASK_DP_UOM_CONV,
54369                                              p_task_not_completed =>lv_task_not_completed,
54370                                              p_pipe_status => lv_pipe_status );
54371                               IF(lv_pipe_status = 1) THEN
54372                                    RAISE EX_PIPE_SND;
54373                               END IF;
54374                             END IF;  --v_dp_uom_conv_enabled
54375 
54376                     END IF;  -- all level value batch completed
54377 
54378                   END IF ; --lv_task_number = TASK_ITEM
54379 
54380 
54381                 IF lv_task_not_completed= 0 THEN
54382                     EXIT;
54383                 END IF;
54384 
54385               ELSE -- lv_task_number is not greater than 0
54386 
54387                   EXIT;
54388 
54389                   /* not required since worker always send unresolvable errors
54390                   ** even if we enable this how do we get the batch id ?
54391                   EXIT WHEN lv_task_number= UNRESOVLABLE_ERROR;
54392                   lv_message := to_char (-lv_task_number);
54393                   DBMS_PIPE.PACK_MESSAGE(lv_message);  -- resend the task to the task que
54394                   IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54395                        RAISE EX_PIPE_SND;
54396                   END IF;
54397                   */
54398 
54399               END IF; -- lv_task_number > 0
54400 
54401            END IF; -- DBMS_PIPE.next_item_type <> 0
54402 
54403          ELSIF lv_pipe_ret_code <> 1 THEN --  not time out, either 2 (record too big for buffer) or 3 (interuppted)
54404 
54405               RAISE EX_PIPE_RCV;
54406 
54407          END IF;
54408 
54409          -- ============= Check the execution time ==============
54410 
54411          select (SYSDATE- START_TIME) into lv_process_time from dual;
54412 
54413          IF lv_process_time > p_timeout/1440.0 THEN Raise EX_PROCESS_TIME_OUT;  END IF;
54414 
54415      END LOOP;
54416 
54417      lv_check_point:= 3;
54418 
54419      if p_reserve_enabled =SYS_YES then
54420       LOAD_RESERVATION;
54421      end if ;
54422 
54423      IF lv_task_not_completed > 0 THEN
54424 
54425         DBMS_PIPE.PURGE( v_pipe_task_que);
54426 
54427         lv_task_number:= -1;
54428 
54429         ROLLBACK;
54430 
54431         FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_FAIL');
54432         ERRBUF:= FND_MESSAGE.GET;
54433         RETCODE := G_ERROR;
54434         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, ERRBUF);
54435 
54436      ELSE
54437 
54438         lv_task_number:= TASK_ALL_COMPLETED;
54439 
54440         UPDATE MSC_APPS_INSTANCES mai
54441            SET LAST_UPDATE_DATE= v_current_date,
54442                LAST_UPDATED_BY= v_current_user,
54443                REQUEST_ID= FND_GLOBAL.CONC_REQUEST_ID
54444          WHERE mai.INSTANCE_ID= v_instance_id;
54445 
54446         FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_SUCCEED');
54447         ERRBUF:= FND_MESSAGE.GET;
54448         RETCODE := G_SUCCESS;
54449         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, ERRBUF);
54450 
54451         FINAL;
54452 
54453      END IF;
54454 
54455      IF SET_ST_STATUS( ERRBUF, RETCODE, p_instance_id, G_ST_READY) THEN
54456          NULL;
54457      END IF;
54458 
54459      FOR lc_i IN 1..p_total_worker_num LOOP
54460 
54461         lv_message := to_char(lv_task_number);
54462         DBMS_PIPE.PACK_MESSAGE( lv_message);
54463         IF DBMS_PIPE.SEND_MESSAGE( v_pipe_task_que)<>0 THEN
54464             RAISE EX_PIPE_SND;
54465         END IF;
54466         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,'v_pipe_task_que: '||lv_message);
54467 
54468      END LOOP;
54469 
54470      lv_worker_committed:= 0;
54471 
54472      lv_start_time:= SYSDATE;
54473 
54474      LOOP
54475 
54476         lv_pipe_ret_code:= DBMS_PIPE.RECEIVE_MESSAGE( v_pipe_status, PIPE_TIME_OUT);
54477 
54478         IF lv_pipe_ret_code=0 THEN
54479 
54480            lv_worker_committed:= lv_worker_committed+1;
54481 
54482            EXIT WHEN lv_worker_committed= p_total_worker_num;
54483 
54484         ELSIF lv_pipe_ret_code<> 1 THEN
54485            RAISE EX_PIPE_RCV;   -- If the error is not time-out error
54486         END IF;
54487 
54488          -- WHY ?
54489          -- SELECT (SYSDATE- lv_start_time) INTO lv_process_time FROM dual;
54490          -- EXIT WHEN lv_process_time > 10.0/1440.0;   -- wait for 10 minutes
54491 
54492      END LOOP;
54493 
54494      IF lv_worker_committed<> p_total_worker_num THEN
54495 
54496         FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_FAIL_TO_COMMIT');
54497         ERRBUF:= FND_MESSAGE.GET;
54498         RETCODE := G_ERROR;
54499 
54500      END IF;
54501 
54502      IF RETCODE= G_ERROR THEN RETURN; END IF;
54503 
54504      -- send error/ warning emails for the errors/ warnings encountered
54505      -- during pre processing
54506      -- SEND_ERROR ;
54507 
54508      -- get current ERROR_ID from MSC_ERRORS
54509      IF (nvl(v_parent_request_id,-1) < 1) THEN
54510          select max(ERROR_ID) into v_error_no_end from MSC_ERRORS;
54511      END IF;
54512 
54513      -- write summary of errors and warning encountered into log
54514      WRITE_ERROR_SUMM_LOG(nvl(v_parent_request_id,-1));
54515 
54516      DROP_INDEXES;
54517 
54518     END IF; --Handling of New Request and calendar sub-request completes
54519 
54520     LAUNCH_DELETE_DUPLICATES(ERRBUF =>ERRBUF , RETCODE => RETCODE) ;
54521 
54522     LAUNCH_DP_PULL(ERRBUF =>ERRBUF , RETCODE => RETCODE) ;
54523 
54524      IF ( ( nvl(RETCODE,-1) <> G_WARNING) AND ( nvl(RETCODE,-1) <> G_ERROR) ) THEN
54525 
54526        OPEN Warn_Flag;
54527          FETCH Warn_Flag INTO v_warning_flag;
54528        CLOSE Warn_Flag;
54529 
54530        IF v_warning_flag=SYS_YES THEN
54531           RETCODE:= G_WARNING;
54532        ELSE
54533           RETCODE := G_SUCCESS;
54534        END IF;
54535 
54536      END IF;
54537 
54538      --Exit the program if Level Values is submitted as a child request and then
54539      --resume the processing when the processing of Level values is completed.
54540      IF nvl(fnd_conc_global.request_data,G_NEW_REQUEST) = G_DP_LV_REQ_DATA THEN
54541        RETURN;
54542      END IF;
54543 
54544     -- OA Self Service load changes
54545     -- If request id is not -1 or null then pre-processor is called from OA
54546     -- launch ODS load if this instance of pre-processor is called from OA
54547     -- bug# 2383498 - always launch ODS load for DP
54548     -- IF ((nvl(v_parent_request_id,-1) > 0) OR (p_calling_module = G_DP AND v_bom_enabled = SYS_YES)) THEN
54549     IF ((nvl(v_parent_request_id,-1) > 0) OR (p_calling_module = G_DP)) THEN
54550        lv_monitor_id := fnd_request.submit_request('MSC',
54551                                                   'MSCPDC',
54552                                                   NULL,
54553                                                   NULL,
54554                                                   TRUE,
54555                                                   v_instance_id,
54556                                                   p_timeout,
54557                                                   p_total_worker_num,
54558                                                   p_resources_enabled,
54559                                                   SYS_NO,
54560                                                   SYS_NO);
54561 
54562 
54563       IF lv_monitor_id <> 0 THEN
54564         fnd_conc_global.set_req_globals(conc_status => 'PAUSED',
54565                                      request_data => to_char(G_ODS_REQ_DATA)) ;
54566         FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_WORKER_REQUEST_ID');
54567         FND_MESSAGE.SET_TOKEN('REQUEST_ID', lv_monitor_id);
54568         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,FND_MESSAGE.GET);
54569       END IF;
54570 
54571       commit;
54572 
54573     END IF;
54574 
54575      EXCEPTION --  LAUNCH_MONITOR
54576 
54577      WHEN EX_PIPE_RCV THEN
54578         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,dbms_utility.format_error_stack);
54579         ROLLBACK;
54580 
54581         IF lv_check_point> 1 THEN
54582             IF SET_ST_STATUS( ERRBUF, RETCODE, p_instance_id, G_ST_READY) THEN
54583                NULL;
54584             END IF;
54585             DROP_INDEXES;
54586         END IF;
54587 
54588         RETCODE := G_ERROR;
54589         FND_MESSAGE.SET_NAME('MSC', 'MSC_MSG_RCV_FAIL');
54590         FND_MESSAGE.SET_TOKEN('PIPE', v_pipe_wm);
54591         ERRBUF:= FND_MESSAGE.GET;
54592         RETCODE := G_ERROR;
54593         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR, ERRBUF);
54594 
54595         IF lv_check_point < 3 THEN
54596             FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_FAIL');
54597             ERRBUF:= FND_MESSAGE.GET;
54598             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,ERRBUF);
54599         END IF;
54600 
54601         COMMIT;
54602 
54603       WHEN EX_PIPE_SND THEN
54604         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,dbms_utility.format_error_stack);
54605         ROLLBACK;
54606 
54607         IF lv_check_point> 1 THEN
54608             IF SET_ST_STATUS( ERRBUF, RETCODE, p_instance_id, G_ST_READY) THEN
54609                NULL;
54610             END IF;
54611             DROP_INDEXES;
54612         END IF;
54613 
54614         FND_MESSAGE.SET_NAME('MSC', 'MSC_MSG_SEND_FAIL');
54615         FND_MESSAGE.SET_TOKEN('PIPE', v_pipe_wm);
54616         ERRBUF:= FND_MESSAGE.GET;
54617         RETCODE := G_ERROR;
54618         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,ERRBUF);
54619 
54620         IF lv_check_point < 3 THEN
54621             FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_FAIL');
54622             ERRBUF:= FND_MESSAGE.GET;
54623             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS,ERRBUF);
54624         END IF;
54625 
54626         COMMIT;
54627 
54628       WHEN EX_PROCESS_TIME_OUT THEN
54629         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,dbms_utility.format_error_stack);
54630         ROLLBACK;
54631 
54632         IF lv_check_point> 1 THEN
54633             IF SET_ST_STATUS( ERRBUF, RETCODE, p_instance_id, G_ST_READY) THEN
54634                NULL;
54635             END IF;
54636             DROP_INDEXES;
54637         END IF;
54638 
54639         FND_MESSAGE.SET_NAME('MSC', 'MSC_TIMEOUT');
54640         ERRBUF:= FND_MESSAGE.GET;
54641         RETCODE := G_ERROR;
54642         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,ERRBUF);
54643 
54644         IF lv_check_point < 3 THEN
54645             FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_FAIL');
54646             ERRBUF:= FND_MESSAGE.GET;
54647             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, ERRBUF);
54648         END IF;
54649 
54650         COMMIT;
54651 
54652 
54653       WHEN others THEN
54654         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,dbms_utility.format_error_stack);
54655         ROLLBACK;
54656 
54657         IF lv_check_point> 1 THEN
54658             IF SET_ST_STATUS( ERRBUF, RETCODE, p_instance_id, G_ST_READY) THEN
54659                NULL;
54660             END IF;
54661         END IF;
54662 
54663         ERRBUF  := SQLERRM;
54664         RETCODE := G_ERROR;
54665         MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_FATAL_ERR,ERRBUF);
54666 
54667         IF lv_check_point < 3 THEN
54668             FND_MESSAGE.SET_NAME('MSC', 'MSC_PP_FAIL');
54669             ERRBUF:= FND_MESSAGE.GET;
54670             MSC_UTIL.LOG_MSG(MSC_UTIL.G_LVL_STATUS, ERRBUF);
54671         END IF;
54672 
54673         COMMIT;
54674 
54675    END LAUNCH_MONITOR;
54676 
54677 END MSC_CL_PRE_PROCESS;