[Home] [Help]
1: package body msc_demantra_pkg as
2: /* $Header: MSCHBDMB.pls 120.59.12020000.2 2012/12/21 17:00:14 wexia ship $ */
3: LF constant varchar2(1) := ''; /* use chr(10) for debug */
4: DUMMY constant varchar2(10) := 'dummy';
5:
15: begin
16: execute immediate 'begin '||g_demantra_schema||'.dbex(:1, :2); end;' using p_message, p_caller;
17: exception
18: when others then
19: msc_phub_util.log('msc_demantra_pkg.log_dbex: '||sqlerrm);
20: end log_dbex;
21:
22: procedure truncate_ods_f
23: is
26: dummy1 varchar2(50);
27: dummy2 varchar2(50);
28: e_truncate_ods_f exception;
29: begin
30: msc_phub_util.log('msc_demantra_pkg.truncate_ods_f');
31: if (fnd_installation.get_app_info('FND', dummy1, dummy2, l_applsys_schema) = false) then
32: msc_phub_util.log(fnd_message.get_string('MSC', 'MSC_PART_UNDEFINED_SCHEMA'));
33: raise e_truncate_ods_f;
34: end if;
38: commit;
39:
40: exception
41: when others then
42: msc_phub_util.log('msc_demantra_pkg.truncate_ods_f: '||sqlerrm);
43: raise;
44: end truncate_ods_f;
45:
46: procedure create_item_hierarchy_view
47: is
48: l_sql varchar2(1024);
49: l_apps_schema varchar2(30) := msc_phub_util.apps_schema;
50: begin
51: msc_phub_util.log('msc_demantra_pkg.create_item_hierarchy_view');
52: if (g_demantra_schema is null) then
53: l_sql :=
54: ' create or replace view '||l_apps_schema||'.msc_dmtra_item_hies_v as'||
55: ' select cast(null as varchar2(100)) level_name from dual where 1=2';
72: end if;
73: execute immediate l_sql;
74: exception
75: when others then
76: msc_phub_util.log('msc_demantra_pkg.create_item_hierarchy_view: '||sqlerrm);
77: end create_item_hierarchy_view;
78:
79:
80: function list_result(p_sql varchar2, p_delim varchar2)
146: l_sql varchar2(16384);
147: l_sql2 varchar2(16384);
148: l_n number;
149: begin
150: msc_phub_util.log('msc_demantra_pkg.build_custom_hierarchy');
151: if (g_demantra_schema is null) then
152: msc_phub_util.log('(g_demantra_schema is null)');
153: return;
154: end if;
153: return;
154: end if;
155:
156: delete from msc_apcc_custom_hier;
157: msc_phub_util.log('msc_demantra_pkg.build_custom_hierarchy: delete from msc_apcc_custom_hier: '||sql%rowcount);
158: commit;
159:
160: delete from msc_translated_message where lang_id='00' and msg_num like g_msg_num_prefix1||'%';
161: msc_phub_util.log('msc_demantra_pkg.build_custom_hierarchy: delete from msc_translated_message: '||sql%rowcount);
157: msc_phub_util.log('msc_demantra_pkg.build_custom_hierarchy: delete from msc_apcc_custom_hier: '||sql%rowcount);
158: commit;
159:
160: delete from msc_translated_message where lang_id='00' and msg_num like g_msg_num_prefix1||'%';
161: msc_phub_util.log('msc_demantra_pkg.build_custom_hierarchy: delete from msc_translated_message: '||sql%rowcount);
162: commit;
163:
164: l_sql :=
165: ' select rownum level_id, gtable, cust_code_field, gtable2, id_field2, table_label'||LF||
196: ' t_ep_item.ebs_item_dest_key is not null';
197:
198: msc_phub_util.log_sql(l_sql2);
199: execute immediate l_sql2;
200: msc_phub_util.log('msc_demantra_pkg.build_custom_hierarchy: insert into msc_apcc_custom_hier: '||sql%rowcount);
201: commit;
202:
203: l_sql2 :=
204: ' insert into msc_translated_message(lang_id, msg_num, msg_text)'||LF||
206: ' from ('||l_sql||')';
207:
208: msc_phub_util.log_sql(l_sql2);
209: execute immediate l_sql2;
210: msc_phub_util.log('msc_demantra_pkg.build_custom_hierarchy: insert into msc_translated_message: '||sql%rowcount);
211: commit;
212: end if;
213:
214: exception
212: end if;
213:
214: exception
215: when others then
216: msc_phub_util.log('msc_demantra_pkg.build_custom_hierarchy: '||sqlerrm);
217: raise;
218: end build_custom_hierarchy;
219:
220: procedure init_cust_measures_table
219:
220: procedure init_cust_measures_table
221: is
222: begin
223: msc_phub_util.log('msc_demantra_pkg.init_cust_measures_table');
224:
225: merge into msc_apcc_custom_measures b
226: using
227: (select m.fact_type, f.column_name
245: fnd_global.user_id, sysdate,
246: sysdate, fnd_global.user_id, fnd_global.login_id,
247: fnd_global.conc_program_id, fnd_global.conc_login_id,
248: fnd_global.prog_appl_id, fnd_global.conc_request_id);
249: msc_phub_util.log('msc_demantra_pkg.init_cust_measures_table: merge into msc_apcc_custom_measures: '||sql%rowcount);
250: commit;
251:
252: delete from msc_apcc_custom_measures b
253: where not exists
260: and f.owner=syn.table_owner
261: and f.table_name=syn.table_name
262: and f.column_name=b.column_name
263: and m.fact_type=b.fact_type);
264: msc_phub_util.log('msc_demantra_pkg.init_cust_measures_table: delete from msc_apcc_custom_measures: '||sql%rowcount);
265: commit;
266:
267: msc_phub_util.log(fnd_message.get_string('MSC','MSC_APCC_DM_R01'));
268:
267: msc_phub_util.log(fnd_message.get_string('MSC','MSC_APCC_DM_R01'));
268:
269: exception
270: when others then
271: msc_phub_util.log('msc_demantra_pkg.init_cust_measures_table: '||sqlerrm);
272: raise;
273: end init_cust_measures_table;
274:
275: procedure populate_details(errbuf out nocopy varchar2, retcode out nocopy varchar2,
276: p_plan_id number, p_plan_run_id number)
277: is
278: l_owning_currency_code varchar2(20) := msc_phub_util.get_owning_currency_code(p_plan_run_id);
279: begin
280: msc_phub_util.log('msc_demantra_pkg.populate_details');
281: retcode := 0;
282: errbuf := '';
283:
284: insert into msc_demantra_f (
401: where mcc.to_currency(+) = fnd_profile.value('MSC_HUB_CUR_CODE_RPT')
402: and mcc.from_currency(+) = t.currency_code
403: and mcc.calendar_date(+) = t.start_date;
404:
405: msc_phub_util.log('msc_demantra_pkg.populate_details: insert='||sql%rowcount);
406: commit;
407:
408: summarize_demantra_f(errbuf, retcode, p_plan_id, p_plan_run_id);
409:
410: exception
411: when others then
412: if (retcode = 0) then
413: retcode := 2;
414: errbuf := 'msc_demantra_pkg.populate_details: '||sqlerrm;
415: end if;
416: msc_phub_util.log(errbuf);
417: end populate_details;
418:
429: l_column_name varchar2(30);
430: l_msg_num varchar2(80);
431: l_qid_wf number;
432: begin
433: msc_phub_util.log('msc_demantra_pkg.register_custom_measures');
434: init_cust_measures_table;
435:
436: if (g_demantra_schema is null) then
437: msc_phub_util.log('(g_demantra_schema is null)');
485: ' '||g_demantra_schema||'.computed_fields c'||LF||
486: ' where m.id=qs.id'||LF||
487: ' and qs.series_id=c.forecast_type_id'||LF||
488: ' and upper(c.computed_name) not in ('||g_dimension_columns||')'||LF||
489: ' and c.computed_name not in (select source_column_name from table(msc_demantra_pkg.seeded_measure_mapping) where fact_type=m.fact_type)'||LF||
490: ' ) v'||LF||
491: ' on (b.fact_type=v.fact_type and b.source_column_name=v.computed_name)'||LF||
492: ' order by 3,1';
493:
549:
550: end case;
551: exception
552: when others then
553: msc_phub_util.log('msc_demantra_pkg.register_custom_measures: '||sqlerrm);
554: end;
555: end loop;
556: close c;
557:
555: end loop;
556: close c;
557:
558: delete from msc_translated_message where lang_id='00' and msg_num like g_msg_num_prefix2||'%';
559: msc_phub_util.log('msc_demantra_pkg.register_custom_measures: delete from msc_translated_message: '||sql%rowcount);
560: commit;
561:
562: insert into msc_translated_message(lang_id, msg_num, msg_text)
563: select
567: from msc_apcc_custom_measures f, table(msc_phub_pkg.meta_info) m
568: where f.fact_type=m.fact_type
569: and f.source_column_name is not null
570: order by to_number(substr(f.column_name,10));
571: msc_phub_util.log('msc_demantra_pkg.register_custom_measures: insert into msc_translated_message: '||sql%rowcount);
572: commit;
573:
574: exception
575: when others then
572: commit;
573:
574: exception
575: when others then
576: msc_phub_util.log('msc_demantra_pkg.register_custom_measures: '||sqlerrm);
577: raise;
578: end register_custom_measures;
579:
580: function seeded_measure_mapping return msc_apcc_measure_map_table
669: ' b.column_name, b.formula'||LF||
670: ' from'||LF||
671: ' (select c.computed_name, c.int_aggr_func, m.column_name, m.formula, m.aggregation_method'||LF||
672: ' from'||LF||
673: ' table(msc_demantra_pkg.seeded_measure_mapping) m,'||LF||
674: ' '||g_demantra_schema||'.computed_fields c'||LF||
675: ' where m.source_column_name=c.computed_name'||LF||
676: ' and m.fact_type='||p_fact_type||LF||
677: ' ) b,'||LF||
693: ' )';
694:
695: l_sql2 :=
696: ' select distinct column_name, aggregation_method'||LF||
697: ' from table(msc_demantra_pkg.seeded_measure_mapping)'||LF||
698: ' where fact_type='||p_fact_type||LF||
699: ' union all'||LF||
700: ' select column_name, nvl(aggregation_method, ''sum'')'||LF||
701: ' from msc_apcc_custom_measures'||LF||
809: if (p_zone_column <> DUMMY) then
810: p_dkey_site_column := DUMMY;
811: end if;
812:
813: msc_phub_util.log('msc_demantra_pkg.find_dimension_columns'||
814: ':p_organization_column='||p_organization_column||
815: ',p_demand_class_column='||p_demand_class_column||
816: ',p_dkey_item_column='||p_dkey_item_column||
817: ',p_dkey_site_column='||p_dkey_site_column||
823: is
824: l_category_set_id1 number := fnd_profile.value('MSC_HUB_CAT_SET_ID_1');
825: l_sql varchar2(16384);
826: begin
827: msc_phub_util.log('msc_demantra_pkg.summarize');
828:
829: if (p_fact_type <> 5) then
830: return;
831: end if;
831: end if;
832:
833: delete from msc_demantra_f
834: where plan_id=p_plan_id and plan_run_id=p_plan_run_id and aggr_type>0;
835: msc_phub_util.log('msc_demantra_pkg.summarize, delete='||sql%rowcount);
836: commit;
837:
838: l_sql :=
839: ' insert into msc_demantra_f ('||LF||
879: ' nvl(q.sr_category_id, -23453)';
880:
881: msc_phub_util.log_sql(l_sql);
882: execute immediate l_sql;
883: msc_phub_util.log('msc_demantra_pkg.summarize, level1='||sql%rowcount);
884: commit;
885:
886: exception
887: when others then
884: commit;
885:
886: exception
887: when others then
888: msc_phub_util.log('msc_demantra_pkg.summarize: '||sqlerrm);
889: raise;
890: end summarize;
891:
892: procedure append_from_staging(p_plan_id number, p_plan_run_id number, p_fact_type number, p_st_transaction_id number)
896: l_staging_table varchar2(30) := staging_table(p_fact_type);
897: l_plan_cols1 varchar2(200);
898: l_plan_cols2 varchar2(200);
899: begin
900: msc_phub_util.log('msc_demantra_pkg.append_from_staging');
901:
902: if (p_fact_type = 5) then
903: l_plan_cols1 := 'plan_id, plan_run_id, aggr_type, category_set_id, sr_category_id, start_date, ';
904: l_plan_cols2 := p_plan_id||', '||p_plan_run_id||', 0, -23453, -23453, t.start_date, ';
948: ' where st_transaction_id='||p_st_transaction_id||' and error_code=0';
949:
950: msc_phub_util.log_sql(l_sql);
951: execute immediate l_sql;
952: msc_phub_util.log('msc_demantra_pkg.append_from_staging: insert into '||l_target_table||': '||sql%rowcount);
953: commit;
954:
955: exception
956: when others then
953: commit;
954:
955: exception
956: when others then
957: msc_phub_util.log('msc_demantra_pkg.append_from_staging: '||sqlerrm);
958: raise;
959: end append_from_staging;
960:
961: procedure archive(p_plan_id number, p_plan_run_id number, p_fact_type number, p_publisher varchar2, p_transfer_id number)
977: l_plan_cols2 varchar2(200);
978: e_archive exception;
979: l_start_time timestamp := systimestamp;
980: begin
981: msc_phub_util.log('msc_demantra_pkg.archive('||
982: p_plan_id||','||p_plan_run_id||','||p_fact_type||','||p_publisher||','||p_transfer_id||')');
983: if (g_demantra_schema is null) then
984: msc_phub_util.log('(g_demantra_schema is null)');
985: return;
1168: ' and mcc.calendar_date(+)=t.sdate';
1169:
1170: msc_phub_util.log_sql(l_sql);
1171: execute immediate l_sql;
1172: msc_phub_util.log('msc_demantra_pkg.archive: insert into '||l_target_table||': '||sql%rowcount);
1173: commit;
1174:
1175: if (p_fact_type = 4 and l_publish_mode = 2) then
1176: delete from msc_demantra_ods_f f where exists (
1186: and region_id=f.region_id
1187: and demand_class=f.demand_class
1188: and end_date=f.end_date
1189: );
1190: msc_phub_util.log('msc_demantra_pkg.archive: delete from msc_demantra_ods_f: '||sql%rowcount);
1191: commit;
1192:
1193: append_from_staging(p_plan_id, p_plan_run_id, p_fact_type, p_transfer_id);
1194: end if;
1196: if (p_fact_type = 5) then
1197: summarize(p_plan_id, p_plan_run_id, p_fact_type);
1198: end if;
1199:
1200: msc_phub_util.log('msc_demantra_pkg.archive.complete, duration='||(systimestamp-l_start_time));
1201:
1202: exception
1203: when others then
1204: msc_phub_util.log('msc_demantra_pkg.archive: '||sqlerrm);
1200: msc_phub_util.log('msc_demantra_pkg.archive.complete, duration='||(systimestamp-l_start_time));
1201:
1202: exception
1203: when others then
1204: msc_phub_util.log('msc_demantra_pkg.archive: '||sqlerrm);
1205: log_dbex(sqlerrm, 'msc_demantra_pkg.archive');
1206: raise;
1207: end archive;
1208:
1201:
1202: exception
1203: when others then
1204: msc_phub_util.log('msc_demantra_pkg.archive: '||sqlerrm);
1205: log_dbex(sqlerrm, 'msc_demantra_pkg.archive');
1206: raise;
1207: end archive;
1208:
1209: procedure import_fact (
1217: l_fact_table varchar2(30) := fact_table(p_fact_type);
1218: l_date_column varchar2(30);
1219: l_is_plan_data number;
1220: begin
1221: msc_phub_util.log('msc_demantra_pkg.import_fact');
1222: retcode := 0;
1223: errbuf := null;
1224:
1225: if (p_fact_type not in (4,5)) then
1222: retcode := 0;
1223: errbuf := null;
1224:
1225: if (p_fact_type not in (4,5)) then
1226: msc_phub_util.log('msc_demantra_pkg.import_fact: (p_fact_type not in (4,5))');
1227: return;
1228: end if;
1229:
1230: if (p_fact_type = 4) then
1270: if (l_result > 0) then
1271: retcode := -1;
1272: end if;
1273:
1274: msc_phub_util.log('msc_demantra_pkg.import_fact: complete, retcode='||retcode);
1275: end import_fact;
1276:
1277: procedure import_demantra_ods_f (
1278: errbuf out nocopy varchar2, retcode out nocopy varchar2,
1280: p_plan_type number, p_plan_start_date date, p_plan_cutoff_date date,
1281: p_upload_mode number, p_overwrite_after_date date, p_def_instance_code varchar2)
1282: is
1283: begin
1284: msc_phub_util.log('msc_demantra_pkg.import_demantra_ods_f');
1285: import_fact(errbuf, retcode,
1286: p_st_transaction_id, p_plan_id, p_plan_run_id, 4,
1287: p_plan_type, p_plan_start_date, p_plan_cutoff_date,
1288: p_upload_mode, p_overwrite_after_date, p_def_instance_code);
1289:
1290: exception
1291: when others then
1292: retcode := 2;
1293: errbuf := 'msc_demantra_pkg.import_demantra_ods_f: '||sqlerrm;
1294: raise;
1295: end import_demantra_ods_f;
1296:
1297: procedure import_demantra_f (
1300: p_plan_type number, p_plan_start_date date, p_plan_cutoff_date date,
1301: p_upload_mode number, p_overwrite_after_date date, p_def_instance_code varchar2)
1302: is
1303: begin
1304: msc_phub_util.log('msc_demantra_pkg.import_demantra_f');
1305: import_fact(errbuf, retcode,
1306: p_st_transaction_id, p_plan_id, p_plan_run_id, 5,
1307: p_plan_type, p_plan_start_date, p_plan_cutoff_date,
1308: p_upload_mode, p_overwrite_after_date, p_def_instance_code);
1309:
1310: exception
1311: when others then
1312: retcode := 2;
1313: errbuf := 'msc_demantra_pkg.import_demantra_f: '||sqlerrm;
1314: raise;
1315: end import_demantra_f;
1316:
1317: procedure summarize_demantra_f(errbuf out nocopy varchar2, retcode out nocopy varchar2,
1318: p_plan_id number, p_plan_run_id number)
1319: is
1320: l_category_set_id1 number := fnd_profile.value('MSC_HUB_CAT_SET_ID_1');
1321: begin
1322: msc_phub_util.log('msc_demantra_pkg.summarize_demantra_f');
1323: retcode := 0;
1324: errbuf := '';
1325:
1326: summarize(p_plan_id, p_plan_run_id, 5);
1327:
1328: exception
1329: when others then
1330: retcode := 2;
1331: errbuf := 'msc_demantra_pkg.summarize_demantra_f: '||sqlerrm;
1332: raise;
1333: end summarize_demantra_f;
1334:
1335: procedure export_demantra_f (
1342: l_apps_schema varchar2(30) := msc_phub_util.apps_schema;
1343: l_plan_start_date date := null;
1344: l_plan_cutoff_date date := null;
1345: begin
1346: msc_phub_util.log('msc_demantra_pkg.export_demantra_f');
1347: retcode := 0;
1348: errbuf := null;
1349:
1350: delete from msc_st_demantra_f where st_transaction_id=p_st_transaction_id;
1507: ' and cmv.region_id(+)=f.region_id'||
1508: ' and cmv.sr_instance_id(+)=decode(f.region_id,-23453,cmv.sr_instance_id(+),f.owning_inst_id)';
1509:
1510: execute immediate l_sql using p_st_transaction_id, p_plan_run_id;
1511: msc_phub_util.log('msc_demantra_pkg.export_demantra_f: insert into msc_st_demantra_f: '||sql%rowcount);
1512: else
1513: l_sql :=
1514: ' select min(start_date), max(start_date)'||
1515: ' from '||l_apps_schema||'.msc_demantra_f'||l_suffix||
1516: ' where plan_run_id=:p_plan_run_id and aggr_type=0';
1517:
1518: msc_phub_util.log_sql(l_sql);
1519: execute immediate l_sql into l_plan_start_date, l_plan_cutoff_date using p_plan_run_id;
1520: msc_phub_util.log('msc_demantra_pkg.export_demantra_f: l_plan_start_date='||l_plan_start_date||', l_plan_cutoff_date='||l_plan_cutoff_date);
1521:
1522: l_sql :=
1523: ' insert into msc_st_demantra_f('||
1524: ' st_transaction_id,'||
1728: ' f.start_date';
1729:
1730: msc_phub_util.log_sql(l_sql);
1731: execute immediate l_sql using p_st_transaction_id, p_plan_run_id, l_plan_start_date, l_plan_cutoff_date;
1732: msc_phub_util.log('msc_demantra_pkg.export_demantra_f: insert into msc_st_demantra_f: '||sql%rowcount);
1733: commit;
1734: end if;
1735: msc_phub_util.log('msc_demantra_pkg.export_demantra_f: complete, retcode='||retcode);
1736:
1731: execute immediate l_sql using p_st_transaction_id, p_plan_run_id, l_plan_start_date, l_plan_cutoff_date;
1732: msc_phub_util.log('msc_demantra_pkg.export_demantra_f: insert into msc_st_demantra_f: '||sql%rowcount);
1733: commit;
1734: end if;
1735: msc_phub_util.log('msc_demantra_pkg.export_demantra_f: complete, retcode='||retcode);
1736:
1737: exception
1738: when others then
1739: retcode := 2;
1736:
1737: exception
1738: when others then
1739: retcode := 2;
1740: errbuf := 'msc_demantra_pkg.export_demantra_f: '||sqlerrm;
1741: msc_phub_util.log(errbuf);
1742: end export_demantra_f;
1743:
1744: procedure export_demantra_ods_f (
1749: l_sql varchar2(16384);
1750: l_suffix varchar2(32) := msc_phub_util.suffix(p_dblink);
1751: l_apps_schema varchar2(30) := msc_phub_util.apps_schema;
1752: begin
1753: msc_phub_util.log('msc_demantra_pkg.export_demantra_ods_f');
1754: retcode := 0;
1755: errbuf := null;
1756:
1757: delete from msc_st_demantra_ods_f where st_transaction_id=p_st_transaction_id;
1881:
1882: msc_phub_util.log_sql(l_sql);
1883: execute immediate l_sql using p_st_transaction_id;
1884: commit;
1885: msc_phub_util.log('msc_demantra_pkg.export_demantra_ods_f: complete, retcode='||retcode);
1886:
1887: exception
1888: when others then
1889: retcode := 2;
1886:
1887: exception
1888: when others then
1889: retcode := 2;
1890: errbuf := 'msc_demantra_pkg.export_demantra_ods_f: '||sqlerrm;
1891: msc_phub_util.log(errbuf);
1892: end export_demantra_ods_f;
1893:
1894: end msc_demantra_pkg;
1890: errbuf := 'msc_demantra_pkg.export_demantra_ods_f: '||sqlerrm;
1891: msc_phub_util.log(errbuf);
1892: end export_demantra_ods_f;
1893:
1894: end msc_demantra_pkg;