DBA Data[Home] [Help]

PACKAGE BODY: APPS.BIM_DBI_MKTG_MGMT_PVT

Source


1 PACKAGE BODY BIM_DBI_MKTG_MGMT_PVT AS
2 /* $Header: bimvsqlb.pls 120.24 2006/05/16 02:02:14 arvikuma noship $ */
3 
4 l_prog_view CONSTANT varchar2(1) :=  fnd_profile.VALUE('BIM_VIEW_PROGRAM');
5 
6 l_prog_cost CONSTANT varchar2(50) := fnd_profile.VALUE('BIM_PROG_COST');
7 l_cost_type CONSTANT varchar2(50) := fnd_profile.VALUE('BIM_COST_PER_LEAD');
8 l_revenue   CONSTANT varchar2(50) := fnd_profile.VALUE('BIM_REVENUE');
9 l_csch_mode CONSTANT varchar2(50) := fnd_profile.VALUE('BIM_CSCH_OA_JTF');
10 
11 L_viewby_c        CONSTANT varchar2(100):=BIM_PMV_DBI_UTL_PKG.GET_CONTEXT_VIEWBY('COUNTRY');
12 L_viewby_pc       CONSTANT varchar2(100):=BIM_PMV_DBI_UTL_PKG.GET_CONTEXT_VIEWBY('ENI_ITEM_VBH_CAT');
13 L_viewby_r        CONSTANT varchar2(100):=BIM_PMV_DBI_UTL_PKG.GET_CONTEXT_VIEWBY('REGION');
14 L_viewby_mc       CONSTANT varchar2(100):=BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('CHANNEL');
15 
16 
17 PROCEDURE write_debug(p_param_name varchar2, p_param_id varchar2 ,
18     p_param_value varchar2 ,
19    p_query varchar2 := NULL, p_dimension varchar2 := NULL,
20     p_period_date date := null)
21 IS
22 BEGIN
23 
24 --INSERT INTO BIM_PARAM_TEST values(p_param_name,p_param_id,
25   -- p_param_value,DBMS_UTILITY.get_time,p_query,p_dimension,p_period_date);
26 --COMMIT;
27 NULL;
28 END;
29 
30 FUNCTION GET_RESOURCE_ID return NUMBER IS
31 l_resource_id NUMBER := NULL;
32 CURSOR c_rid IS
33        SELECT resource_id
34        FROM   JTF_RS_RESOURCE_EXTNS
35        WHERE  user_id = FND_GLOBAL.user_id;
36  BEGIN
37  OPEN c_rid;
38          FETCH c_rid INTO l_resource_id;
39  CLOSE c_rid;
40  if (l_resource_id=null) then
41    l_resource_id := -1;
42  end if;
43  if (l_resource_id='') then
44     l_resource_id := -1;
45  end if;
46 
47 return l_resource_id;
48 END GET_RESOURCE_ID;
49 
50 FUNCTION GET_DIM RETURN VARCHAR2 IS
51 period_id NUMBER;
52 BEGIN
53  period_id   := -1;
54  return '&AS_OF_DATE='||TO_CHAR(TRUNC(sysdate),'DD-MON-YYYY')||
55 '&BIM_DIM5='||'TIME_COMPARISON_TYPE+YEARLY'||
56 '&BIM_DIM2_FROM='||period_id||'&BIM_DIM2_TO='||period_id||
57 '&BIM_DIM7=All&BIM_DIM8=All&VIEW_BY=CAMPAIGN+CAMPAIGN' ;
58 
59 END GET_DIM;
60 
61 FUNCTION GET_DIM_PARAM RETURN VARCHAR2 IS
62 period_id NUMBER;
63 BEGIN
64  period_id   := -1;
65  return '&AS_OF_DATE='||TO_CHAR(TRUNC(sysdate),'DD-MON-YYYY')||
66 '&BIM_DIM5='||'TIME_COMPARISON_TYPE+YEARLY'||
67 '&BIM_DIM2_FROM='||period_id||'&BIM_DIM2_TO='||period_id||
68 '&BIM_DIM7=All&BIM_DIM8=All&CURRENCY=FII_GLOBAL1&ENI_ITEM_VBH_CAT=All' ;
69 
70 END GET_DIM_PARAM;
71 
72 FUNCTION GET_ADMIN_STATUS return VARCHAR2 IS
73 l_admin_count NUMBER := 0;
74 l_admin_flag varchar2(20) ;
75  CURSOR c_rid IS
76        SELECT count(*)
77        FROM   JTF_RS_RESOURCE_EXTNS r, bim_i_admin_group a
78        WHERE  user_id = FND_GLOBAL.user_id
79        AND r.resource_id = a.resource_id;
80 
81 BEGIN
82 If GET_RESOURCE_ID is null
83 then
84 l_admin_flag := 'Y';
85 
86 else
87 
88  OPEN c_rid;
89          FETCH c_rid INTO l_admin_count;
90  CLOSE c_rid;
91    if (l_admin_count<=0)
92    then
93       l_admin_flag := 'N';
94    else
95       l_admin_flag := 'Y';
96    end if;
97 end if;
98    return l_admin_flag;
99 END GET_ADMIN_STATUS;
100 
101 
102 
103 PROCEDURE GET_KPI_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
104                           x_custom_sql OUT NOCOPY VARCHAR2,
105                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
106 			  IS
107 l_sqltext varchar2(32766);
108 iFlag number;
109 l_period_type_hc number;
110 l_as_of_date  DATE;
111 l_period_type	varchar2(2000);
112 l_record_type_id NUMBER;
113 l_comp_type    varchar2(2000);
114 l_country      varchar2(4000);
115 l_view_by      varchar2(4000);
116 l_sql_errm      varchar2(4000);
117 l_previous_report_start_date DATE;
118 l_current_report_start_date DATE;
119 l_previous_as_of_date DATE;
120 l_period_type_id NUMBER;
121 l_user_id NUMBER;
122 l_resource_id NUMBER;
123 l_time_id_column  VARCHAR2(1000);
124 l_admin_status VARCHAR2(20);
125 l_admin_flag VARCHAR2(1);
126 l_admin_count Number;
127 l_rsid NUMBER;
128 l_prev_aod_str varchar2(80);
129 l_curr_aod_str varchar2(80);
130 l_country_clause varchar2(4000);
131 l_admin_clause varchar2(4000);
132 --l_cat_id NUMBER;
133 l_campaign_id VARCHAR2(50);
134 l_cat_id VARCHAR2(50);
135 l_custom_rec BIS_QUERY_ATTRIBUTES;
136 l_table_name varchar2(100);
137 l_table_name1 varchar2(100);
138 l_pc_where varchar2(500);
139 l_pc_from  varchar2(100);
140 l_kpi_revenue  varchar2(50);
141 l_inr_cond   varchar2(5000);
142 l_inner   varchar2(5000);
143 l_inner_p   varchar2(5000);
144 l_top_cond  varchar2(100);
145 l_top_cond_tot   varchar2(100);
146 l_kpi_revenue_in varchar2(100);
147 l_curr VARCHAR2(50);
148 l_curr_suffix VARCHAR2(50);
149 l_col_id NUMBER;
150 l_area VARCHAR2(300);
151 l_report_name VARCHAR2(300);
152 l_media VARCHAR2(300);
153 BEGIN
154 
155 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
156 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
157 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
158    			     			      l_as_of_date,
159 			    			      l_period_type,
160 				                      l_record_type_id,
161 				                      l_comp_type,
162 				                      l_country,
163 						      l_view_by,
164 						      l_cat_id,
165 						      l_campaign_id,
166 						      l_curr,
167 						      l_col_id,
168 						      l_area,
169 						      l_media,
170 						      l_report_name
171 				                      );
172  IF (l_curr = '''FII_GLOBAL1''')
173     THEN l_curr_suffix := '';
174   ELSIF (l_curr = '''FII_GLOBAL2''')
175     THEN l_curr_suffix := '_s';
176     ELSE l_curr_suffix := '';
177   END IF;
178    l_admin_status := GET_ADMIN_STATUS;
179 
180    /*l_previous_as_of_date := BIM_PMV_DBI_UTL_PKG.Previous_Period_Asof_Date(l_as_of_date, l_period_type, l_comp_type);
181    l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
182    l_prev_aod_str := 'to_date('||to_char(l_previous_as_of_date,'J')||',''J'')';*/
183    l_rsid := GET_RESOURCE_ID;
184    IF l_country IS NULL THEN
185       l_country := 'N';
186    END IF;
187 
188   IF l_cat_id IS NULL THEN
189       l_pc_where:='AND a.category_id= -9';
190   ELSE
191           l_pc_from   :=  ', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
192 	  l_pc_where  :=  ' AND a.category_id = edh.child_id
193 			   AND edh.object_type = ''CATEGORY_SET''
194 			   AND edh.object_id = mdcs.category_set_id
195 			   AND mdcs.functional_area_id = 11
196 			   AND edh.dbi_flag = ''Y''
197 			   AND edh.parent_id = :l_cat_id ';
198    END IF;
199 
200    IF l_admin_status='N' THEN
201       if l_prog_view='Y' then
202       l_table_name:='bim_i_obj_mets_mv a,bim_i_top_objects ac';
203       l_table_name1:='bim_i_obj_mets_mv a,bim_i_top_objects ac';
204       l_top_cond_tot :=' AND a.immediate_parent_id is null ';
205       l_top_cond :=' AND a.immediate_parent_id is null ';
206       else
207       l_table_name:='bim_i_obj_mets_mv a,ams_act_access_denorm ac';
208       l_table_name1:='bim_i_obj_mets_mv a,ams_act_access_denorm ac';
209       l_top_cond :=' and a.object_type in (''CAMP'',''EVEH'',''EONE'') ';
210       l_top_cond_tot :=' and a.object_type in (''CAMP'',''EVEH'',''EONE'') ';
211       end if;
212    ELSE
213       l_table_name:='bim_mkt_kpi_cnt_mv a';
214       l_table_name1:='bim_i_obj_mets_mv a';
215       l_top_cond_tot :=' AND a.immediate_parent_id is null ';
216    END IF;
217 
218 /********************************************************************************/
219  /********************************************************************************/
220 
221 /***********Start: pick revenue column based on  profile setting ************/
222 
223    IF l_revenue = 'BOOKED_AMT' THEN
224 
225      l_kpi_revenue :='ORDERS_BOOKED_AMT';
226      l_kpi_revenue_in :='BOOKED_AMT';
227 
228    ELSIF l_revenue = 'INVOICED_AMT'   THEN
229 
230     l_kpi_revenue :='ORDERS_INVOICED_AMT';
231     l_kpi_revenue_in :='INVOICED_AMT';
232 
233    ELSIF l_revenue = 'WON_OPPR_AMT' THEN
234 
235     l_kpi_revenue :='WON_OPPORTUNITY_AMT';
236     l_kpi_revenue_in :='WON_OPPORTUNITY_AMT';
237 
238    END IF;
239 
240    /***********End : pick revenue column based on  profile setting ************/
241 
242 
243    /********************************************************************************/
244    /********************************************************************************/
245 
246    /************Start Inner Query to get current acitve objects *************************/
247 
248 l_inner:=', ( select distinct  a.object_id,a.object_type
249 from BIM_I_CPB_METS_MV a
250 ,fii_time_rpt_struct_v cal';
251 
252 IF l_admin_status='N' THEN
253 if l_prog_view='Y' then
254   l_inner:=l_inner||',bim_i_top_objects r ';
255 else
256   l_inner:=l_inner||',ams_act_access_denorm r ';
257 end if;
258 end if;
259 
260 IF l_cat_id is not null then
261   l_inner := l_inner ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
262 end if;
263 
264  l_inner := l_inner || ' WHERE
265  a.time_id=cal.time_id
266 AND a.period_type_id=cal.period_type_id
267 AND cal.calendar_id=-1
268 AND cal.report_date   in (&BIS_CURRENT_ASOF_DATE)
269 AND a.object_country = :l_country
270 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
271 and ( a.'||l_kpi_revenue_in||' >0 or a.total_leads >0)'||l_top_cond_tot;
272 
273 IF l_admin_status = 'N' THEN
274   l_inner :=  l_inner||'   a.source_code_id = r.source_code_id AND r.resource_id = :l_resource_id ';
275 END IF;
276 
277 IF l_cat_id is null then
278  l_inner :=  l_inner ||' AND a.category_id = -9 ';
279 else
280    l_inner :=  l_inner ||' AND a.category_id = edh.child_id
281 			   AND edh.object_type = ''CATEGORY_SET''
282 			   AND edh.object_id = mdcs.category_set_id
283 			   AND mdcs.functional_area_id = 11
284 			   AND edh.dbi_flag = ''Y''
285 			   AND edh.parent_id = :l_cat_id ';
286   end if;
287 
288 l_inner :=  l_inner ||' ) inr ';
289 
290 
291 
292 l_inr_cond:='and a.source_code_id= inr.source_code_id
293               ';
294 
295 /************ End Inner Query to get current acitve objects *************************/
296 
297 
298 
299 /************Start Inner Query to get prevoius acitve objects *************************/
300 
301 l_inner_p:=', ( select distinct a.source_code_id
302 from BIM_I_CPB_METS_MV a
303 ,fii_time_rpt_struct_v cal';
304 
305 IF l_admin_status='N' THEN
306   if l_prog_view='Y' then
307   l_inner_p:=l_inner_p||',bim_i_top_objects r ';
308   else
309   l_inner_p:=l_inner_p||',ams_act_access_denorm r ';
310   end if;
311 End if;
312 
313 IF l_cat_id is not null then
314   l_inner_p := l_inner_p ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
315 end if;
316 
317  l_inner_p := l_inner_p || ' WHERE
318  a.time_id=cal.time_id
319 AND a.period_type_id=cal.period_type_id
320 AND cal.calendar_id=-1
321 AND cal.report_date   in (&BIS_PREVIOUS_ASOF_DATE)
322 AND a.object_country = :l_country
323 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
324 and ( a.'||l_kpi_revenue_in||' > 0 or a.total_leads > 0)'||l_top_cond_tot;
325 
326 IF l_admin_status = 'N' THEN
327   l_inner_p :=  l_inner_p||' AND  a.source_code_id = r.source_code_id AND r.resource_id = :l_resource_id ';
328 END IF;
329 
330 IF l_cat_id is null then
331  l_inner_p :=  l_inner_p ||' AND a.category_id = -9 ';
332 else
333    l_inner_p :=  l_inner_p ||' AND a.category_id = edh.child_id
334 			   AND edh.object_type = ''CATEGORY_SET''
335 			   AND edh.object_id = mdcs.category_set_id
336 			   AND mdcs.functional_area_id = 11
337 			   AND edh.dbi_flag = ''Y''
338 			   AND edh.parent_id = :l_cat_id ';
339   end if;
340 
341 l_inner_p :=  l_inner_P ||' ) inr ';
342 
343 
344 
345 
346 
347 /************ End Inner Query to get current acitve objects *************************/
348 
349 
350 l_sqltext := 'SELECT sum(x.c_resp) BIM_MEASURE1,
351 sum(x.c_resp) BIM_GRAND_TOTAL1,
352 sum(x.p_resp) BIM_MEASURE2,
353 sum(x.p_resp) BIM_CGRAND_TOTAL1,
354 sum(x.c_lds) BIM_MEASURE3,
358 decode(sum(x.c_lds),0,null,100*sum(x.c_alds)/sum(x.c_lds)) BIM_MEASURE5,
355 sum(x.c_lds) BIM_GRAND_TOTAL2,
356 sum(x.p_lds) BIM_MEASURE4,
357 sum(x.p_lds)  BIM_CGRAND_TOTAL2,
359 decode(sum(x.p_lds),0,null,100*sum(x.p_alds)/sum(x.p_lds)) BIM_MEASURE6,
360 decode(sum(x.c_lds),0,null,100*sum(x.c_alds)/sum(x.c_lds)) BIM_GRAND_TOTAL5,
361 decode(sum(x.p_lds),0,null,100*sum(x.p_alds)/sum(x.p_lds)) BIM_CGRAND_TOTAL5,
362 case when '''|| l_cost_type ||''' = ''BIM_PTD_COST''  then
363    decode(sum(x.c_lds),0,null,sum(x.c_rev_profl)/sum(x.c_lds))
364 else
365    decode(sum(x.t_leads),0,null,sum(x.t_revenue)/sum(x.t_leads))
366 end BIM_MEASURE9,
367 case when '''|| l_cost_type ||''' = ''BIM_PTD_COST''  then
368    decode(sum(x.p_lds),0,null,sum(x.p_rev_profl)/sum(x.p_lds))
369 else
370     decode(sum(x.pt_leads),0,null,sum(x.pt_revenue)/sum(x.pt_leads))
371 end BIM_MEASURE10,
372 case when '''|| l_cost_type ||''' = ''BIM_PTD_COST''  then
373    decode(sum(x.c_lds),0,null,sum(x.c_rev_profl)/sum(x.c_lds))
374 else
375    decode(sum(x.t_leads),0,null,sum(x.t_revenue)/sum(x.t_leads))
376 end BIM_GRAND_TOTAL6,
377 case when '''|| l_cost_type ||''' = ''BIM_PTD_COST''  then
378    decode(sum(x.p_lds),0,null,sum(x.p_rev_profl)/sum(x.p_lds))
379 else
380     decode(sum(x.pt_leads),0,null,sum(x.pt_revenue)/sum(x.pt_leads))
381 end BIM_CGRAND_TOTAL6,
382 decode(sum(x.c_popen)+sum(x.c_lds),0,null,100*sum(x.c_clds)/(sum(x.c_popen)+sum(x.c_lds))) BIM_MEASURE11,
383 decode(sum(x.p_popen)+sum(x.p_lds),0,null,100*sum(x.p_clds)/(sum(x.p_popen)+sum(x.p_lds))) BIM_MEASURE12,
384 decode(sum(x.c_popen)+sum(x.c_lds),0,null,100*sum(x.c_clds)/(sum(x.c_popen)+sum(x.c_lds))) BIM_GRAND_TOTAL7,
385 decode(sum(x.p_popen)+sum(x.p_lds),0,null,100*sum(x.p_clds)/(sum(x.p_popen)+sum(x.p_lds))) BIM_CGRAND_TOTAL7,
386 sum(x.c_opps) BIM_MEASURE13,
387 sum(x.p_opps) BIM_MEASURE14,
388 sum(x.c_opps) BIM_GRAND_TOTAL8,
389 sum(x.p_opps) BIM_CGRAND_TOTAL8,
390 sum(x.c_order) BIM_MEASURE15,
391 sum(x.p_order) BIM_MEASURE16,
392 sum(x.c_order) BIM_GRAND_TOTAL10,
393 sum(x.p_order) BIM_CGRAND_TOTAL10,
394 sum(x.c_camps) BIM_MEASURE17,
395 sum(x.p_camps) BIM_MEASURE18,
396 sum(x.c_camps) BIM_GRAND_TOTAL12,
397 sum(x.p_camps) BIM_CGRAND_TOTAL12,
398 sum(x.c_events) BIM_MEASURE19,
399 sum(x.p_events) BIM_MEASURE20,
400 sum(x.c_events) BIM_GRAND_TOTAL13,
401 sum(x.p_events) BIM_CGRAND_TOTAL13,
402 sum(x.c_rev) BIM_MEASURE23,
403 sum(x.p_rev) BIM_MEASURE24,
404 sum(x.c_rev) BIM_GRAND_TOTAL11,
405 sum(x.p_rev) BIM_CGRAND_TOTAL11,
406 sum(x.c_leadsc) BIM_MEASURE25,
407 sum(x.p_leadsc) BIM_MEASURE26,
408 sum(x.c_leadsc) BIM_GRAND_TOTAL3,
409 sum(x.p_leadsc) BIM_CGRAND_TOTAL3,
410 sum(x.c_leadsp) BIM_MEASURE27,
411 sum(x.p_leadsp) BIM_MEASURE28,
412 sum(x.c_leadsp) BIM_GRAND_TOTAL4,
413 sum(x.p_leadsp) BIM_CGRAND_TOTAL4,
414 sum(x.c_won_opps) BIM_MEASURE29,
415 sum(x.p_won_opps) BIM_MEASURE30,
416 sum(x.c_won_opps) BIM_GRAND_TOTAL9,
417 sum(x.p_won_opps) BIM_CGRAND_TOTAL9,
418 sum(x.c_alds) BIM_MEASURE31,
419 sum(x.p_alds) BIM_MEASURE32,
420 sum(x.c_alds) BIM_GRAND_TOTAL14,
421 sum(x.p_alds) BIM_CGRAND_TOTAL14
422 FROM
423 (SELECT sum(c_resp) c_resp ,sum(p_resp) p_resp,sum(c_lds) c_lds,sum(p_lds) p_lds,sum(c_alds) c_alds,sum(p_alds) p_alds,sum(c_opps)
424 c_opps,sum(p_opps) p_opps,sum(c_won_opps) c_won_opps,sum(p_won_opps) p_won_opps,sum(c_order) c_order,sum(p_order) p_order,
425 sum(c_rev_profl) c_rev_profl,sum(p_rev_profl) p_rev_profl,
426 decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(c_bapp),SUM(c_cost)) c_cost,
427 decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(p_bapp),SUM(p_cost)) p_cost,
428 sum(c_rev) c_rev,sum(p_rev) p_rev,sum(c_events) c_events,sum(p_events) p_events,sum(c_camps) c_camps,sum(p_camps) p_camps ,sum(c_popen)
429 c_popen,sum(p_popen) p_popen,sum(c_clds) c_clds,sum(p_clds) p_clds ,
430 sum(t_revenue) t_revenue,
431 sum(t_leads) t_leads,
432 sum(pt_revenue) pt_revenue,
433 sum(pt_leads) pt_leads,
434 SUM(c_leadsc) c_leadsc,
435 SUM(p_leadsc) p_leadsc,
436 SUM(c_leadsp) c_leadsp,
437 SUM(p_leadsp) p_leadsp
438 FROM(
439 SELECT SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.responses_positive else 0 end) c_resp,
440 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.responses_positive else 0 end) p_resp,
441 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.leads else 0 end) c_lds,
442 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.leads else 0 end) p_lds,
443 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.rank_a else 0 end) c_alds,
444 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.rank_a else 0 end) p_alds,
445 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.new_opportunity_amt'||l_curr_suffix||' else 0 end) c_opps,
446 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.new_opportunity_amt'||l_curr_suffix||' else 0 end) p_opps,
447 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.won_opportunity_amt'||l_curr_suffix||' else 0 end) c_won_opps,
448 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.won_opportunity_amt'||l_curr_suffix||' else 0 end) p_won_opps,
449 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.orders_booked_amt'||l_curr_suffix||' else 0 end) c_order,
450 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.orders_booked_amt'||l_curr_suffix||' else 0 end) p_order,
454 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.budget_approved'||l_curr_suffix||' else 0 end) p_bapp,
451 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.'||l_kpi_revenue||l_curr_suffix||' else 0 end) c_rev_profl,
452 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.'||l_kpi_revenue||l_curr_suffix||' else 0 end) p_rev_profl,
453 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.budget_approved'||l_curr_suffix||' else 0 end) c_bapp,
455 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.cost_actual'||l_curr_suffix||' else 0 end) c_cost,
456 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.cost_actual'||l_curr_suffix||' else 0 end) p_cost,
457 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.orders_invoiced_amt'||l_curr_suffix||' else 0 end) c_rev,
458 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.orders_invoiced_amt'||l_curr_suffix||' else 0 end) p_rev,
459 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.even_started else 0 end) c_events,
460 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.even_started else 0 end) p_events,
461 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.camp_started else 0 end) c_camps,
462 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.camp_started else 0 end) p_camps,
463 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.leads_converted else 0 end) c_clds,
464 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.leads_converted else 0 end) p_clds,
465 0 c_popen,0 p_popen,0 t_revenue,0 t_leads,0 pt_revenue,0 pt_leads,
466 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.leads_customer else 0 end) c_leadsc,
467 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.leads_customer else 0 end) p_leadsc,
468 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  a.leads_prospect else 0 end) c_leadsp,
469 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.leads_prospect else 0 end) p_leadsp
470 FROM '||l_table_name||',
471 fii_time_rpt_struct_v cal'||l_pc_from;
472 
473 l_sqltext := l_sqltext ||' WHERE a.time_id = cal.time_id
474 AND a.period_type_id = cal.period_type_id
475 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
476 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
477 AND cal.calendar_id=-1 ';
478 l_sqltext := l_sqltext ||l_pc_where;
479 
480 IF l_admin_status = 'N' THEN
481 l_sqltext := l_sqltext||' AND a.source_code_id=ac.source_code_id
482                            AND ac.resource_id = :l_resource_id ';
483 END IF;
484 l_sqltext := l_sqltext ||
485 ' AND a.object_country = :l_country'|| l_top_cond||'
486 UNION ALL
487 SELECT
488 0 c_resp,0 p_resp,0 c_lds,0 p_lds,0 c_alds,0 p_alds,0 c_opps,0 p_opps,0 c_won_opps,0 p_won_opps,0 c_order,0 p_order,0 c_rev_profl,0 p_rev_profl,
489 0 c_bapp,0 p_bapp,0 c_cost,0 p_cost,0 c_rev,0 p_rev,0 c_events,0 p_events,0 c_camps,0 p_camps,
490 0 c_clds,0 p_clds,
491 SUM(case when cal.report_date=&BIS_CURRENT_EFFECTIVE_START_DATE - 1 then a.leads-(a.leads_closed+a.leads_dead+a.leads_converted) else 0 end)
492 c_popen,
493 SUM(case when cal.report_date=&BIS_PREVIOUS_EFFECTIVE_START_DATE-1 then a.leads-(a.leads_closed+a.leads_dead+a.leads_converted) else 0 end)
494 p_popen,0 t_revenue,0 t_leads,0 pt_revenue,0 pt_leads,0 c_leadsc,0 p_leadsc,0 c_leadsp,0 p_leadsp
495 FROM '||l_table_name||',
496 fii_time_rpt_struct_v cal '||l_pc_from;
497 l_sqltext := l_sqltext ||
498 ' WHERE a.time_id = cal.time_id
499 AND a.period_type_id = cal.period_type_id
500 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
501 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1,&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
502 AND cal.calendar_id=-1 ';
503 l_sqltext := l_sqltext ||l_pc_where;
504 IF l_admin_status = 'N' THEN
505 l_sqltext := l_sqltext||' AND a.source_code_id=ac.source_code_id
506 			  AND ac.resource_id = :l_resource_id ';
507 END IF;
508 l_sqltext := l_sqltext ||
509 ' AND a.object_country = :l_country '|| l_top_cond;
510 
511 if l_cost_type <> 'BIM_PTD_COST' then
512 l_sqltext := l_sqltext||'
513 union all
514 SELECT
515 0 c_resp,0 p_resp,0 c_lds,0 p_lds,0 c_alds,0 p_alds,0 c_opps,0 p_opps,0 c_won_opps,0 p_won_opps,0 c_order,0 p_order,0 c_rev_profl,0 p_rev_profl,0 c_bapp,0 p_bapp,0 c_cost,0 p_cost,
516 0 c_rev,0 p_rev,0 c_events,0 p_events,0 c_camps,0 p_camps,0 c_clds,0 p_clds,0 c_popen,0 p_popen,sum(a.'||l_kpi_revenue||l_curr_suffix||') t_revenue ,sum( a.leads) t_leads,0 pt_revenue,0 pt_leads,
517 0 c_leadsc,0 p_leadsc,0 c_leadsp,0 p_leadsp
518 FROM '||l_table_name1||',
519 fii_time_rpt_struct_v cal '||l_pc_from||l_inner;
520 l_sqltext := l_sqltext ||
521 ' WHERE a.time_id = cal.time_id
522 AND a.period_type_id = cal.period_type_id
523 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
524 AND cal.report_date = trunc(sysdate)
525 AND cal.calendar_id=-1 '||l_top_cond_tot;
526 
527 l_sqltext := l_sqltext ||l_inr_cond||l_pc_where;
528 
529 IF l_admin_status = 'N' THEN
530 l_sqltext := l_sqltext||' AND a.source_code_id=ac.source_code_id
531 			  AND ac.resource_id = :l_resource_id '||l_top_cond;
532 END IF;
533 
534 l_sqltext := l_sqltext ||
535 ' AND a.object_country = :l_country
536 union all
537 SELECT
538 0 c_resp,0 p_resp,0 c_lds,0 p_lds,0 c_alds,0 p_alds,0 c_opps,0 p_opps,0 c_won_opps,0 p_won_opps,0 c_order,0 p_order,0 c_rev_profl,0 p_rev_profl,0 c_bapp,0 p_bapp,0 c_cost,0 p_cost,
542 fii_time_rpt_struct_v cal '||l_pc_from||l_inner_p;
539 0 c_rev,0 p_rev,0 c_events,0 p_events,0 c_camps,0 p_camps,0 c_clds,0 p_clds,0 c_popen,0 p_popen,0 t_revenue ,0 t_leads,sum(a.'||l_kpi_revenue||l_curr_suffix||') pt_revenue,sum( a.leads) pt_leads,
540 0 c_leadsc,0 p_leadsc,0 c_leadsp,0 p_leadsp
541 FROM '||l_table_name1||',
543 
544 
545 l_sqltext := l_sqltext ||
546 ' WHERE a.time_id = cal.time_id
547 AND a.period_type_id = cal.period_type_id
548 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
549 AND cal.report_date = trunc(sysdate)
550 AND cal.calendar_id=-1 '||l_top_cond_tot;
551 
552 
553 l_sqltext := l_sqltext ||l_inr_cond||l_pc_where;
554 
555 IF l_admin_status = 'N' THEN
556 l_sqltext := l_sqltext||' AND a.source_code_id=ac.source_code_id
557                           AND ac.resource_id = :l_resource_id '||l_top_cond;
558 END IF;
559 
560 l_sqltext := l_sqltext ||
561 ' AND a.object_country = :l_country';
562 END IF;
563 l_sqltext := l_sqltext ||'
564   )
565   ) x ';
566 
567 
568 
569 
570   x_custom_sql := l_sqltext;
571   l_custom_rec.attribute_name := ':l_record_type';
572   l_custom_rec.attribute_value := l_record_type_id;
573   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
574   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
575   x_custom_output.EXTEND;
576   x_custom_output(1) := l_custom_rec;
577 
578   l_custom_rec.attribute_name := ':l_resource_id';
579   l_custom_rec.attribute_value := GET_RESOURCE_ID;
580   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
581   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
582   x_custom_output.EXTEND;
583   x_custom_output(2) := l_custom_rec;
584 
585   l_custom_rec.attribute_name := ':l_admin_flag';
586   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
587   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
588   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
589   x_custom_output.EXTEND;
590   x_custom_output(3) := l_custom_rec;
591 
592   l_custom_rec.attribute_name := ':l_country';
593   l_custom_rec.attribute_value := l_country;
594   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
595   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
596   x_custom_output.EXTEND;
597   x_custom_output(4) := l_custom_rec;
598 
599   l_custom_rec.attribute_name := ':l_cat_id';
600   l_custom_rec.attribute_value := l_cat_id;
601   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
602   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
603   x_custom_output.EXTEND;
604   x_custom_output(5) := l_custom_rec;
605 
606 write_debug('MKTG KPI SQL','QUERY','test',l_sqltext,NULL,null);
607 EXCEPTION
608 WHEN others THEN
609 l_sql_errm := SQLERRM;
610 write_debug('MKTG KPI SQL','ERROR',l_sql_errm,l_sqltext,NULL,null);
611 END GET_KPI_SQL;
612 
613 PROCEDURE GET_PO_RACK_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
614                           x_custom_sql  OUT NOCOPY VARCHAR2,
615                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
616  IS
617 l_sqltext varchar2(32000);
618 iFlag number;
619 l_period_type_hc number;
620 l_as_of_date  DATE;
621 l_period_type	varchar2(2000);
622 l_record_type_id NUMBER;
623 l_comp_type    varchar2(2000);
624 l_country      varchar2(4000);
625 l_view_by      varchar2(4000);
626 l_sql_errm      varchar2(4000);
627 l_previous_report_start_date DATE;
628 l_current_report_start_date DATE;
629 l_previous_as_of_date DATE;
630 l_period_type_id NUMBER;
631 l_user_id NUMBER;
632 l_resource_id NUMBER;
633 l_time_id_column  VARCHAR2(1000);
634 l_admin_status VARCHAR2(20);
635 l_admin_flag VARCHAR2(1);
636 l_admin_count Number;
637 l_rsid NUMBER;
638 l_curr_aod_str varchar2(80);
639 l_country_clause varchar2(4000);
640 l_access_clause varchar2(4000);
641 l_access_table varchar2(4000);
642 l_object_type varchar2(30);
643 l_url_str varchar2(1000);
644 l_url_str_csch varchar2(1000);
645 l_url_str_csch_jtf varchar2(1000);
646 l_url_str_type varchar2(1000);
647 l_url_str_tga varchar2(1000);
648 l_csch_chnl  varchar2(100);
649 l_chnl_select  varchar2(1000);
650 l_chnl_from  varchar2(1000);
651 l_chnl_where  varchar2(1000);
652 l_chnl_group  varchar2(1000);
653 l_chnl_col   varchar2(10);
654 --l_cat_id NUMBER;
655 l_cat_id VARCHAR2(50):= NULL;
656 l_campaign_id VARCHAR2(50):= NULL;
657 l_custom_rec BIS_QUERY_ATTRIBUTES;
658 l_curr VARCHAR2(50);
659 l_curr_suffix VARCHAR2(50);
660 l_dass                          varchar2(100);  -- variable to store value for  directly assigned lookup value
661 --l_una                          varchar2(100);   -- variable to store value for  Unassigned lookup value
662 l_col_id NUMBER;
663 l_area VARCHAR2(300);
664 l_report_name VARCHAR2(300);
665 l_media VARCHAR2(300);
666 
667 
668  /* cursor to get type of object passed from the page ******/
669     cursor get_obj_type
670     is
671     select object_type
672     from bim_i_source_codes
676 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
673     where source_code_id=replace(l_campaign_id,'''');
674 
675 BEGIN
677 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
678 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
679 										l_as_of_date,
680 										l_period_type,
681 										l_record_type_id,
682 										l_comp_type,
683 										l_country,
684 										l_view_by,
685 										l_cat_id,
686 										l_campaign_id,
687 										l_curr,
688 										l_col_id,
689 										l_area,
690 										l_media,
691 										l_report_name
692 										);
693 IF (l_curr = '''FII_GLOBAL1''')
694     THEN l_curr_suffix := '';
695   ELSIF (l_curr = '''FII_GLOBAL2''')
696     THEN l_curr_suffix := '_s';
697     ELSE l_curr_suffix := '';
698   END IF;
699    IF l_country IS NULL THEN
700       l_country := 'N';
701    END IF;
702     l_admin_status := GET_ADMIN_STATUS;
703 
704 
705   l_url_str :='pFunctionName=BIM_I_PROG_ORDER_PHD&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
706   --l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&addBreadCrumb=S&addBreadCrumb=Y&objId=';
707   l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
708   l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
709   l_url_str_tga  :='pFunctionName=AMS_LIST_UPDATE_PG&retainAM=Y&MidTab=ChartsRN&addBreadCrumb=Y&NavMode=UPD&OAPB=AMS_AUDIENCE_USER_BRANDING&ListHeaderId=';
710   l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
711   &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
712 
713 IF(l_campaign_id IS NULL) THEN
714 /***************** NO DRILL DOWN IN CAMPAIGN HIRERACHY**********/
715 	IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') THEN
716 
717 		IF l_cat_id IS NULL THEN
718 		/************** START OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NULL ***********/
719 		l_sqltext :=
720 		'
721 		SELECT
722 		VIEWBY,
723 		VIEWBYID,
724 		BIM_ATTRIBUTE2,
725 		BIM_ATTRIBUTE3,
726 		BIM_ATTRIBUTE4,
727 		BIM_ATTRIBUTE5,
728 		BIM_ATTRIBUTE6,
729 		BIM_ATTRIBUTE7,
730 		BIM_ATTRIBUTE8,
731 		BIM_ATTRIBUTE9,
732 		BIM_ATTRIBUTE10,
733 		BIM_ATTRIBUTE11,
734 		BIM_ATTRIBUTE12,
735 		BIM_ATTRIBUTE13,
736 		BIM_ATTRIBUTE14,
737 		BIM_ATTRIBUTE8 BIM_ATTRIBUTE17,
738 		decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) BIM_URL1,
739 		null BIM_URL2,
740 		null BIM_URL3,
741 		null BIM_URL4,
742 		BIM_GRAND_TOTAL1,
743 		BIM_GRAND_TOTAL2,
744 		BIM_GRAND_TOTAL3,
745 		BIM_GRAND_TOTAL4,
746 		BIM_GRAND_TOTAL5,
747 		BIM_GRAND_TOTAL6,
748 		BIM_GRAND_TOTAL7,
749 		BIM_GRAND_TOTAL8,
750 		BIM_GRAND_TOTAL9,
751 		BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
752 		FROM
753 		(
754 		SELECT name VIEWBY,
755 		leaf_node_flag,
756 		null BIM_ATTRIBUTE2,
757 		targeted_audience BIM_ATTRIBUTE3,
758 		responses_positive BIM_ATTRIBUTE4,
759 		leads BIM_ATTRIBUTE5,
760 		rank_a BIM_ATTRIBUTE6,
761 		decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
762 		new_opportunity_amt BIM_ATTRIBUTE8,
763 		won_opportunity_amt BIM_ATTRIBUTE9,
764 		orders_booked_amt BIM_ATTRIBUTE10,
765 		orders_invoiced_amt BIM_ATTRIBUTE11,
766 		null BIM_ATTRIBUTE12,
767 		null BIM_ATTRIBUTE13,
768 		DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE14,
769 		sum(targeted_audience) over() BIM_GRAND_TOTAL1,
770 		sum(responses_positive) over() BIM_GRAND_TOTAL2,
771 		sum(leads) over() BIM_GRAND_TOTAL3,
772 		sum(rank_a) over() BIM_GRAND_TOTAL4,
773 		decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
774 		sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
775 		sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
776 		sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
777 		sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
778 		VIEWBYID
779 		FROM
780 		(
781 		SELECT
782 		VIEWBYID,
783 		name,
784 		leaf_node_flag,
785 		sum(targeted_audience) targeted_audience,
786 		sum(responses_positive) responses_positive,
787 		sum(leads) leads,
788 		sum(rank_a) rank_a ,
789 		sum(new_opportunity_amt) new_opportunity_amt,
790 		sum(won_opportunity_amt) won_opportunity_amt,
791 		sum(orders_booked_amt) orders_booked_amt,
792 		sum(orders_invoiced_amt) orders_invoiced_amt,
793 		sum(prior_open) prior_open,
797 		( SELECT /*+ORDERED*/
794 		sum(leads_converted) leads_converted,
795 		sum(prev_new_opportunity_amt) prev_new_opportunity_amt
796 		FROM
798 		p.parent_id VIEWBYID,
799 		p.value  name,
800 		p.leaf_node_flag leaf_node_flag,
801 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
802 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
803 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
804 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
805 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
806 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
807 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
808 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
809 		0 prior_open,
810 		sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
811 		SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
812 		FROM fii_time_rpt_struct_v cal,
813 			 BIM_I_OBJ_METS_MV a
814 			,eni_denorm_hierarchies edh
815 						,mtl_default_category_sets mdcs
816 						,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
817 						   FROM eni_item_vbh_nodes_v e
818 						   WHERE e.top_node_flag=''Y''
819 						   AND e.child_id = e.parent_id) p ';
820 		IF l_admin_status = 'N' THEN
821 		l_sqltext := l_sqltext ||',bim_i_top_objects ac ';
822 		END IF;
823 		l_sqltext :=  l_sqltext ||
824 		' WHERE a.time_id = cal.time_id
825 		AND  a.period_type_id = cal.period_type_id
826 		AND a.category_id = edh.child_id
827 		AND edh.object_type = ''CATEGORY_SET''
828 		AND edh.object_id = mdcs.category_set_id
829 		AND mdcs.functional_area_id = 11
830 		AND edh.dbi_flag = ''Y''
831 		AND edh.parent_id = p.parent_id';
832 		IF l_admin_status = 'N' THEN
833 		l_sqltext := l_sqltext ||
834 		' AND a.source_code_id = ac.source_code_id
835 		AND ac.resource_id = :l_resource_id';
836 		ELSE
837 		l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
838 		END IF;
839 		l_sqltext :=  l_sqltext ||
840 		' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
841 		AND a.object_country = :l_country';
842 		l_sqltext :=  l_sqltext ||
843 		' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
844 		AND cal.calendar_id=-1
845 		GROUP BY p.value,p.parent_id,p.leaf_node_flag
846 		UNION ALL
847 		SELECT /*+ORDERED*/
848 		p.parent_id VIEWBYID,
849 		p.value  name,
850 		p.leaf_node_flag leaf_node_flag,
851 		0 targeted_audience,
852 		0 responses_positive,
853 		0 leads,
854 		0 rank_a ,
855 		0 new_opportunity_amt,
856 		0 won_opportunity_amt,
857 		0 orders_booked_amt,
858 		0 orders_invoiced_amt,
859 		sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
860 		0 leads_converted,
861 		0 prev_new_opportunity_amt
862 		FROM fii_time_rpt_struct_v cal,
863 			 BIM_I_OBJ_METS_MV a
864 			,eni_denorm_hierarchies edh
865 						,mtl_default_category_sets mdcs
866 						,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
867 						   FROM eni_item_vbh_nodes_v e
868 						   WHERE e.top_node_flag=''Y''
869 						   AND e.child_id = e.parent_id) p ';
870 		IF l_admin_status = 'N' THEN
871 		l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
872 		END IF;
873 		l_sqltext :=  l_sqltext ||
874 		' WHERE a.time_id = cal.time_id
875 		AND  a.period_type_id = cal.period_type_id
876 		AND a.category_id = edh.child_id
877 		AND edh.object_type = ''CATEGORY_SET''
878 		AND edh.object_id = mdcs.category_set_id
879 		AND mdcs.functional_area_id = 11
880 		AND edh.dbi_flag = ''Y''
881 		AND edh.parent_id = p.parent_id';
882 		IF l_admin_status = 'N' THEN
883 		l_sqltext := l_sqltext ||
884 		' AND a.source_code_id = ac.source_code_id
885 		AND ac.resource_id = :l_resource_id';
886 		ELSE
887 		l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
888 		END IF;
889 		l_sqltext :=  l_sqltext ||
890 		' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
891 		AND a.object_country = :l_country';
892 		l_sqltext :=  l_sqltext ||
893 		' AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
894 		AND cal.calendar_id=-1
895 		GROUP BY p.value,p.parent_id,p.leaf_node_flag
896 		)GROUP BY name,VIEWBYID,leaf_node_flag
897 		)
898 		)
899 		WHERE
900 		BIM_ATTRIBUTE3 > 0
901 		OR BIM_ATTRIBUTE4 > 0
902 		OR BIM_ATTRIBUTE5 > 0
903 		OR BIM_ATTRIBUTE6 > 0
904 		OR BIM_ATTRIBUTE7 > 0
905 		OR BIM_ATTRIBUTE8 > 0
906 		OR BIM_ATTRIBUTE9 > 0
910 		/************** END OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NULL ***********/
907 		OR BIM_ATTRIBUTE10 > 0
908 		OR BIM_ATTRIBUTE11 > 0
909 		&ORDER_BY_CLAUSE';
911 	ELSE
912 		/************** START OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NOT NULL ***********/
913 		l_dass:=  BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('DASS');
914 		l_sqltext :=
915 		'
916 		SELECT
917 		VIEWBY,
918 		VIEWBYID,
919 		BIM_ATTRIBUTE2,
920 		BIM_ATTRIBUTE3,
921 		BIM_ATTRIBUTE4,
922 		BIM_ATTRIBUTE5,
923 		BIM_ATTRIBUTE6,
924 		BIM_ATTRIBUTE7,
925 		BIM_ATTRIBUTE8,
926 		BIM_ATTRIBUTE9,
927 		BIM_ATTRIBUTE10,
928 		BIM_ATTRIBUTE11,
929 		BIM_ATTRIBUTE12,
930 		BIM_ATTRIBUTE13,
931 		BIM_ATTRIBUTE14,
932 		BIM_ATTRIBUTE8 BIM_ATTRIBUTE17,
933 		decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) BIM_URL1,
934 		null BIM_URL2,
935 		null BIM_URL3,
936 		null BIM_URL4,
937 		BIM_GRAND_TOTAL1,
938 		BIM_GRAND_TOTAL2,
939 		BIM_GRAND_TOTAL3,
940 		BIM_GRAND_TOTAL4,
941 		BIM_GRAND_TOTAL5,
942 		BIM_GRAND_TOTAL6,
943 		BIM_GRAND_TOTAL7,
944 		BIM_GRAND_TOTAL8,
945 		BIM_GRAND_TOTAL9,
946 		BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
947 		FROM
948 		(
949 		SELECT name VIEWBY,
950 		leaf_node_flag,
951 		null BIM_ATTRIBUTE2,
952 		targeted_audience BIM_ATTRIBUTE3,
953 		responses_positive BIM_ATTRIBUTE4,
954 		leads BIM_ATTRIBUTE5,
955 		rank_a BIM_ATTRIBUTE6,
956 		decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
957 		new_opportunity_amt BIM_ATTRIBUTE8,
958 		won_opportunity_amt BIM_ATTRIBUTE9,
959 		orders_booked_amt BIM_ATTRIBUTE10,
960 		orders_invoiced_amt BIM_ATTRIBUTE11,
961 		null BIM_ATTRIBUTE12,
962 		null BIM_ATTRIBUTE13,
963 		DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE14,
964 		sum(targeted_audience) over() BIM_GRAND_TOTAL1,
965 		sum(responses_positive) over() BIM_GRAND_TOTAL2,
966 		sum(leads) over() BIM_GRAND_TOTAL3,
967 		sum(rank_a) over() BIM_GRAND_TOTAL4,
968 		decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
969 		sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
970 		sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
971 		sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
972 		sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
973 		VIEWBYID
974 		FROM
975 		(
976 		SELECT
977 		VIEWBYID,
978 		name,
979 		leaf_node_flag,
980 		sum(targeted_audience) targeted_audience,
981 		sum(responses_positive) responses_positive,
982 		sum(leads) leads,
983 		sum(rank_a) rank_a ,
984 		sum(new_opportunity_amt) new_opportunity_amt,
985 		sum(won_opportunity_amt) won_opportunity_amt,
986 		sum(orders_booked_amt) orders_booked_amt,
987 		sum(orders_invoiced_amt) orders_invoiced_amt,
988 		sum(prior_open) prior_open,
989 		sum(leads_converted) leads_converted,
990 		sum(prev_new_opportunity_amt) prev_new_opportunity_amt
991 		FROM
992 		(
993 		SELECT /*+ORDERED*/
994 		p.id VIEWBYID,
995 		p.value  name,
996 		p.leaf_node_flag leaf_node_flag,
997 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
998 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
999 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
1000 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
1001 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
1002 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
1003 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
1004 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
1005 		0 prior_open,
1006 		sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
1007 		SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
1008 		FROM fii_time_rpt_struct_v cal,
1009 			 BIM_I_OBJ_METS_MV a
1010 			,eni_denorm_hierarchies edh
1011 					,mtl_default_category_sets mdc
1012 					,(select e.id,e.value,e.leaf_node_flag
1013 					  from eni_item_vbh_nodes_v e
1014 				  where
1015 					  e.parent_id =:l_cat_id
1016 					  AND e.id = e.child_id
1017 					  AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
1018 			  ) p ';
1019 		IF l_admin_status = 'N' THEN
1020 		l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
1021 		END IF;
1022 		l_sqltext :=  l_sqltext ||
1023 		' WHERE a.time_id = cal.time_id
1024 		AND  a.period_type_id = cal.period_type_id
1025 		AND a.category_id = edh.child_id
1026 		AND edh.object_type = ''CATEGORY_SET''
1027 		AND edh.object_id = mdc.category_set_id
1028 		AND mdc.functional_area_id = 11
1029 		AND edh.dbi_flag = ''Y''
1033 		'
1030 		AND edh.parent_id = p.id ';
1031 		IF l_admin_status = 'N' THEN
1032 		l_sqltext := l_sqltext ||
1034 		AND a.source_code_id = ac.source_code_id
1035 		AND ac.resource_id = :l_resource_id';
1036 		ELSE
1037 		l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
1038 		END IF;
1039 		l_sqltext :=  l_sqltext ||
1040 		' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
1041 		AND a.object_country = :l_country';
1042 		l_sqltext :=  l_sqltext ||
1043 		' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
1044 		AND cal.calendar_id=-1
1045 		GROUP BY p.id,p.value,p.leaf_node_flag
1046 		UNION ALL
1047 		SELECT /*+ORDERED*/
1048 		p.id VIEWBYID,
1049 		p.value  name,
1050 		p.leaf_node_flag leaf_node_flag,
1051 		0 targeted_audience,
1052 		0 responses_positive,
1053 		0 leads,
1054 		0 rank_a ,
1055 		0 new_opportunity_amt,
1056 		0 won_opportunity_amt,
1057 		0 orders_booked_amt,
1058 		0 orders_invoiced_amt,
1059 		sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
1060 		0 leads_converted,
1061 		0 prev_new_opportunity_amt
1062 		FROM fii_time_rpt_struct_v cal,
1063 			 BIM_I_OBJ_METS_MV a
1064 			,eni_denorm_hierarchies edh
1065 					,mtl_default_category_sets mdc
1066 					,(select e.id,e.value,e.leaf_node_flag
1067 					  from eni_item_vbh_nodes_v e
1068 				  where
1069 					  e.parent_id =:l_cat_id
1070 					  AND e.id = e.child_id
1071 					  AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
1072 			  ) p ';
1073 		IF l_admin_status = 'N' THEN
1074 		l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
1075 		END IF;
1076 		l_sqltext :=  l_sqltext ||
1077 		' WHERE a.time_id = cal.time_id
1078 		AND  a.period_type_id = cal.period_type_id
1079 		AND a.category_id = edh.child_id
1080 		AND edh.object_type = ''CATEGORY_SET''
1081 		AND edh.object_id = mdc.category_set_id
1082 		AND mdc.functional_area_id = 11
1083 		AND edh.dbi_flag = ''Y''
1084 		AND edh.parent_id = p.id ';
1085 		IF l_admin_status = 'N' THEN
1086 		l_sqltext := l_sqltext ||
1087 		'
1088 		AND a.source_code_id = ac.source_code_id
1089 		AND ac.resource_id = :l_resource_id';
1090 		ELSE
1091 		l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
1092 		END IF;
1093 		l_sqltext :=  l_sqltext ||
1094 		' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
1095 		AND a.object_country = :l_country';
1096 		l_sqltext :=  l_sqltext ||
1097 		' AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
1098 		AND cal.calendar_id=-1
1099 		GROUP BY p.id,p.value,p.leaf_node_flag
1100 		/*** directly assigned to the category *************/
1101 		UNION ALL
1102 		SELECT /*+ORDERED*/
1103 		p.id VIEWBYID,
1104 		bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||' name,
1105 		''Y'' leaf_node_flag,
1106 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
1107 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
1108 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
1109 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
1110 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
1111 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
1112 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
1113 		SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
1114 		0 prior_open,
1115 		sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
1116 		SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
1117 		FROM fii_time_rpt_struct_v cal,
1118 			 BIM_I_OBJ_METS_MV a
1119 			,(select e.id id,e.value value
1120 							  from eni_item_vbh_nodes_v e
1121 							  where e.parent_id =  :l_cat_id
1122 							  AND e.parent_id = e.child_id
1123 							  AND leaf_node_flag <> ''Y''
1124 							  ) p ';
1125 		IF l_admin_status = 'N' THEN
1126 		l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
1127 		END IF;
1128 		l_sqltext :=  l_sqltext ||
1129 		' WHERE a.time_id = cal.time_id
1130 		AND  a.period_type_id = cal.period_type_id
1131 		AND a.category_id = p.id';
1132 		IF l_admin_status = 'N' THEN
1133 		l_sqltext := l_sqltext ||
1134 		'
1135 		AND a.source_code_id = ac.source_code_id
1136 		AND ac.resource_id = :l_resource_id';
1137 		ELSE
1138 		l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
1139 		END IF;
1140 		l_sqltext :=  l_sqltext ||
1141 		' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
1142 		AND a.object_country = :l_country';
1143 		l_sqltext :=  l_sqltext ||
1144 		' AND cal.report_date IN (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
1145 		AND cal.calendar_id=-1
1146 		GROUP BY p.id
1147 		UNION ALL
1148 		SELECT /*+ORDERED*/
1149 		p.id VIEWBYID,
1153 		0 responses_positive,
1150 		bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||'   name,
1151 		''Y'' leaf_node_flag,
1152 		0 targeted_audience,
1154 		0 leads,
1155 		0 rank_a ,
1156 		0 new_opportunity_amt,
1157 		0 won_opportunity_amt,
1158 		0 orders_booked_amt,
1159 		0 orders_invoiced_amt,
1160 		sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
1161 		0 leads_converted,
1162 		0 prev_new_opportunity_amt
1163 		FROM  fii_time_rpt_struct_v cal,
1164 			  BIM_I_OBJ_METS_MV a
1165 			,(select e.id id,e.value value
1166 							  from eni_item_vbh_nodes_v e
1167 							  where e.parent_id =  :l_cat_id
1168 							  AND e.parent_id = e.child_id
1169 							  AND leaf_node_flag <> ''Y''
1170 							  ) p ';
1171 		IF l_admin_status = 'N' THEN
1172 		l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
1173 		END IF;
1174 		l_sqltext :=  l_sqltext ||
1175 		' WHERE a.time_id = cal.time_id
1176 		AND  a.period_type_id = cal.period_type_id
1177 		AND a.category_id = p.id ';
1178 		IF l_admin_status = 'N' THEN
1179 		l_sqltext := l_sqltext ||
1180 		'
1181 		AND a.source_code_id = ac.source_code_id
1182 		AND ac.resource_id = :l_resource_id';
1183 		ELSE
1184 		l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
1185 		END IF;
1186 		l_sqltext :=  l_sqltext ||
1187 		' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
1188 		AND a.object_country = :l_country';
1189 		l_sqltext :=  l_sqltext ||
1190 		' AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
1191 		AND cal.calendar_id=-1
1192 		GROUP BY p.id
1193 		)GROUP BY name,VIEWBYID,leaf_node_flag
1194 		)
1195 		)
1196 		WHERE
1197 		BIM_ATTRIBUTE3 > 0
1198 		OR BIM_ATTRIBUTE4 > 0
1199 		OR BIM_ATTRIBUTE5 > 0
1200 		OR BIM_ATTRIBUTE6 > 0
1201 		OR BIM_ATTRIBUTE7 > 0
1202 		OR BIM_ATTRIBUTE8 > 0
1203 		OR BIM_ATTRIBUTE9 > 0
1204 		OR BIM_ATTRIBUTE10 > 0
1205 		OR BIM_ATTRIBUTE11 > 0
1206 		&ORDER_BY_CLAUSE';
1207 		/************** END OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NOT NULL ***********/
1208 		END IF;
1209 
1210 	ELSIF (l_view_by ='MEDIA+MEDIA') THEN
1211 	--l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
1212 	l_sqltext :=
1213 	'
1214 	SELECT
1215 	VIEWBY,
1216 	VIEWBYID,
1217 	BIM_ATTRIBUTE2,
1218 	BIM_ATTRIBUTE3,
1219 	BIM_ATTRIBUTE4,
1220 	BIM_ATTRIBUTE5,
1221 	BIM_ATTRIBUTE6,
1222 	BIM_ATTRIBUTE7,
1223 	BIM_ATTRIBUTE8,
1224 	BIM_ATTRIBUTE9,
1225 	BIM_ATTRIBUTE10,
1226 	BIM_ATTRIBUTE11,
1227 	BIM_ATTRIBUTE12,
1228 	BIM_ATTRIBUTE13,
1229 	BIM_ATTRIBUTE14,
1230 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE17,
1231 	null BIM_URL1,
1232 	null BIM_URL2,
1233 	null BIM_URL3,
1234 	null BIM_URL4,
1235 	BIM_GRAND_TOTAL1,
1236 	BIM_GRAND_TOTAL2,
1237 	BIM_GRAND_TOTAL3,
1238 	BIM_GRAND_TOTAL4,
1239 	BIM_GRAND_TOTAL5,
1240 	BIM_GRAND_TOTAL6,
1241 	BIM_GRAND_TOTAL7,
1242 	BIM_GRAND_TOTAL8,
1243 	BIM_GRAND_TOTAL9,
1244 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
1245 	FROM
1246 	(
1247 	SELECT name VIEWBY,
1248 	meaning BIM_ATTRIBUTE2,
1249 	targeted_audience BIM_ATTRIBUTE3,
1250 	responses_positive BIM_ATTRIBUTE4,
1251 	leads BIM_ATTRIBUTE5,
1252 	rank_a BIM_ATTRIBUTE6,
1253 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
1254 	new_opportunity_amt BIM_ATTRIBUTE8,
1255 	won_opportunity_amt BIM_ATTRIBUTE9,
1256 	orders_booked_amt BIM_ATTRIBUTE10,
1257 	orders_invoiced_amt BIM_ATTRIBUTE11,
1258 	null BIM_ATTRIBUTE12,
1259 	null BIM_ATTRIBUTE13,
1260 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE14,
1261 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
1262 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
1263 	sum(leads) over() BIM_GRAND_TOTAL3,
1264 	sum(rank_a) over() BIM_GRAND_TOTAL4,
1265 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
1266 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
1267 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
1268 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
1269 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
1270 	VIEWBYID
1271 	FROM
1272 	(
1273 	SELECT
1274 	null VIEWBYID,
1275 	name,
1276 	null meaning,
1277 	SUM(targeted_audience) targeted_audience,
1278 	SUM(responses_positive) responses_positive,
1279 	SUM(leads) leads,
1280 	SUM(rank_a) rank_a ,
1281 	SUM(new_opportunity_amt) new_opportunity_amt,
1282 	SUM(won_opportunity_amt) won_opportunity_amt,
1283 	SUM(orders_booked_amt) orders_booked_amt,
1284 	SUM(orders_invoiced_amt) orders_invoiced_amt,
1285 	SUM(prior_open) prior_open,
1286 	sum(leads_converted) leads_converted,
1287 	SUM(prev_new_opportunity_amt) prev_new_opportunity_amt
1288 	FROM
1289 	(
1290 	SELECT
1291 	decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
1292 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
1293 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
1297 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
1294 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
1295 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
1296 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
1298 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
1299 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
1300 	0 prior_open,
1301 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
1302 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt ';
1303 
1304 	IF l_admin_status = 'N' THEN
1305 	l_sqltext := l_sqltext ||' FROM bim_obj_chnl_mv a,bim_i_top_objects ac,  ';
1306 	ELSE
1307 	l_sqltext := l_sqltext ||' FROM bim_mkt_chnl_mv a, ';
1308 	END IF;
1309 	l_sqltext := l_sqltext ||'
1310 		fii_time_rpt_struct_v cal,
1311 		bim_dimv_media d ';
1312 
1313 	 IF l_cat_id is not null then
1314 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
1315 	  end if;
1316 
1317 	l_sqltext :=  l_sqltext ||
1318 	' WHERE a.time_id = cal.time_id
1319 	  AND  a.period_type_id = cal.period_type_id
1320 	  AND  a.object_country = :l_country';
1321 	IF l_admin_status = 'N' THEN
1322 	l_sqltext := l_sqltext ||
1323 	'
1324 	AND a.source_code_id = ac.source_code_id
1325 	AND ac.resource_id = :l_resource_id';
1326 	END IF;
1327 	IF l_cat_id is null then
1328 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
1329 	else
1330 	  l_sqltext := l_sqltext ||
1331 							 ' AND a.category_id = edh.child_id
1332 				   AND edh.object_type = ''CATEGORY_SET''
1333 				   AND edh.object_id = mdcs.category_set_id
1334 				   AND mdcs.functional_area_id = 11
1335 				   AND edh.dbi_flag = ''Y''
1336 				   AND edh.parent_id = :l_cat_id ';
1337 	  end if;
1338 	l_sqltext :=  l_sqltext ||
1339 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
1340 	  AND  d.id (+)= a.activity_id
1341 	  AND cal.report_date IN (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
1342 	  AND cal.calendar_id=-1
1343 	GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
1344 	UNION ALL
1345 	SELECT
1346 	decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
1347 	0 targeted_audience,
1348 	0 responses_positive,
1349 	0 leads,
1350 	0 rank_a ,
1351 	0 new_opportunity_amt,
1352 	0 won_opportunity_amt,
1353 	0 orders_booked_amt,
1354 	0 orders_invoiced_amt,
1355 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
1356 	0 leads_converted,
1357 	0 prev_new_opportunity_amt ';
1358 	IF l_admin_status = 'N' THEN
1359 	l_sqltext := l_sqltext ||' FROM bim_obj_chnl_mv a,bim_i_top_objects ac,  ';
1360 	ELSE
1361 	l_sqltext := l_sqltext ||' FROM bim_mkt_chnl_mv a, ';
1362 	END IF;
1363 	l_sqltext := l_sqltext ||'
1364 		fii_time_rpt_struct_v cal,
1365 		bim_dimv_media d';
1366 	 IF l_cat_id is not null then
1367 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
1368 	  end if;
1369 	l_sqltext :=  l_sqltext ||
1370 	' WHERE a.time_id = cal.time_id
1371 	  AND  a.period_type_id = cal.period_type_id
1372 	  AND  a.object_country = :l_country';
1373 	IF l_admin_status = 'N' THEN
1374 	l_sqltext := l_sqltext ||
1375 	'
1376 	AND a.source_code_id = ac.source_code_id
1377 	AND ac.resource_id = :l_resource_id';
1378 	END IF;
1379 	IF l_cat_id is null then
1380 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
1381 	else
1382 	  l_sqltext := l_sqltext ||
1383 							 ' AND a.category_id = edh.child_id
1384 				   AND edh.object_type = ''CATEGORY_SET''
1385 				   AND edh.object_id = mdcs.category_set_id
1386 				   AND mdcs.functional_area_id = 11
1387 				   AND edh.dbi_flag = ''Y''
1388 				   AND edh.parent_id = :l_cat_id ';
1389 	  end if;
1390 	  l_sqltext :=  l_sqltext ||
1391 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
1392 	AND  d.id (+)= a.activity_id
1393 	AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
1394 	AND cal.calendar_id=-1
1395 	GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
1396 	)GROUP BY name
1397 	)
1398 	)
1399 	WHERE
1400 	BIM_ATTRIBUTE3 > 0
1401 	OR BIM_ATTRIBUTE4 > 0
1402 	OR BIM_ATTRIBUTE5 > 0
1403 	OR BIM_ATTRIBUTE6 > 0
1404 	OR BIM_ATTRIBUTE7 > 0
1405 	OR BIM_ATTRIBUTE8 > 0
1406 	OR BIM_ATTRIBUTE9 > 0
1407 	OR BIM_ATTRIBUTE10 > 0
1408 	OR BIM_ATTRIBUTE11 > 0
1409 	&ORDER_BY_CLAUSE';
1410 	ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
1411 	--l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
1412 	l_sqltext :=
1413 	'
1414 	SELECT
1415 	VIEWBY,
1416 	VIEWBYID,
1417 	BIM_ATTRIBUTE2,
1418 	BIM_ATTRIBUTE3,
1419 	BIM_ATTRIBUTE4,
1420 	BIM_ATTRIBUTE5,
1421 	BIM_ATTRIBUTE6,
1422 	BIM_ATTRIBUTE7,
1423 	BIM_ATTRIBUTE8,
1424 	BIM_ATTRIBUTE9,
1425 	BIM_ATTRIBUTE10,
1426 	BIM_ATTRIBUTE11,
1427 	BIM_ATTRIBUTE12,
1431 	null BIM_URL1,
1428 	BIM_ATTRIBUTE13,
1429 	BIM_ATTRIBUTE14,
1430 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE17,
1432 	null BIM_URL2,
1433 	null BIM_URL3,
1434 	null BIM_URL4,
1435 	BIM_GRAND_TOTAL1,
1436 	BIM_GRAND_TOTAL2,
1437 	BIM_GRAND_TOTAL3,
1438 	BIM_GRAND_TOTAL4,
1439 	BIM_GRAND_TOTAL5,
1440 	BIM_GRAND_TOTAL6,
1441 	BIM_GRAND_TOTAL7,
1442 	BIM_GRAND_TOTAL8,
1443 	BIM_GRAND_TOTAL9,
1444 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
1445 	FROM
1446 	(
1447 	SELECT name VIEWBY,
1448 	meaning BIM_ATTRIBUTE2,
1449 	targeted_audience BIM_ATTRIBUTE3,
1450 	responses_positive BIM_ATTRIBUTE4,
1451 	leads BIM_ATTRIBUTE5,
1452 	rank_a BIM_ATTRIBUTE6,
1453 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
1454 	new_opportunity_amt BIM_ATTRIBUTE8,
1455 	won_opportunity_amt BIM_ATTRIBUTE9,
1456 	orders_booked_amt BIM_ATTRIBUTE10,
1457 	orders_invoiced_amt BIM_ATTRIBUTE11,
1458 	null BIM_ATTRIBUTE12,
1459 	null BIM_ATTRIBUTE13,
1460 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE14,
1461 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
1462 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
1463 	sum(leads) over() BIM_GRAND_TOTAL3,
1464 	sum(rank_a) over() BIM_GRAND_TOTAL4,
1465 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
1466 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
1467 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
1468 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
1469 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
1470 	VIEWBYID
1471 	FROM
1472 	(
1473 	SELECT
1474 	null VIEWBYID,
1475 	name,
1476 	null meaning,
1477 	SUM(targeted_audience) targeted_audience,
1478 	SUM(responses_positive) responses_positive,
1479 	SUM(leads) leads,
1480 	SUM(rank_a) rank_a ,
1481 	SUM(new_opportunity_amt) new_opportunity_amt,
1482 	SUM(won_opportunity_amt) won_opportunity_amt,
1483 	SUM(orders_booked_amt) orders_booked_amt,
1484 	SUM(orders_invoiced_amt) orders_invoiced_amt,
1485 	SUM(prior_open) prior_open,
1486 	sum(leads_converted) leads_converted,
1487 	SUM(prev_new_opportunity_amt) prev_new_opportunity_amt
1488 	FROM
1489 	(
1490 	SELECT
1491 	decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
1492 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
1493 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
1494 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
1495 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
1496 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
1497 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
1498 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
1499 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
1500 	0 prior_open,
1501 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
1502 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt ';
1503 
1504 	IF l_admin_status = 'N' THEN
1505 	l_sqltext := l_sqltext ||' FROM bim_obj_regn_mv a,bim_i_top_objects ac,  ';
1506 	ELSE
1507 	l_sqltext := l_sqltext ||' FROM bim_mkt_regn_mv a, ';
1508 	END IF;
1509 	l_sqltext := l_sqltext ||'
1510 		fii_time_rpt_struct_v cal,
1511 		bis_areas_v d ';
1512 
1513 	 IF l_cat_id is not null then
1514 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
1515 	  end if;
1516 
1517 	l_sqltext :=  l_sqltext ||
1518 	' WHERE a.time_id = cal.time_id
1519 	  AND  a.period_type_id = cal.period_type_id
1520 	  AND  a.object_country = :l_country';
1521 	IF l_admin_status = 'N' THEN
1522 	l_sqltext := l_sqltext ||
1523 	'
1524 	AND a.source_code_id = ac.source_code_id
1525 	AND ac.resource_id = :l_resource_id';
1526 	END IF;
1527 	IF l_cat_id is null then
1528 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
1529 	else
1530 	  l_sqltext := l_sqltext ||
1531 							 ' AND a.category_id = edh.child_id
1532 				   AND edh.object_type = ''CATEGORY_SET''
1533 				   AND edh.object_id = mdcs.category_set_id
1534 				   AND mdcs.functional_area_id = 11
1535 				   AND edh.dbi_flag = ''Y''
1536 				   AND edh.parent_id = :l_cat_id ';
1537 	  end if;
1538 	l_sqltext :=  l_sqltext ||
1539 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
1540 	  AND  d.id (+)= a.object_region
1541 	  AND cal.report_date IN (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
1542 	  AND cal.calendar_id=-1
1543 	GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
1544 	UNION ALL
1545 	SELECT
1546 	decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
1547 	0 targeted_audience,
1548 	0 responses_positive,
1552 	0 won_opportunity_amt,
1549 	0 leads,
1550 	0 rank_a ,
1551 	0 new_opportunity_amt,
1553 	0 orders_booked_amt,
1554 	0 orders_invoiced_amt,
1555 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
1556 	0 leads_converted,
1557 	0 prev_new_opportunity_amt ';
1558 	IF l_admin_status = 'N' THEN
1559 	l_sqltext := l_sqltext ||' FROM bim_obj_regn_mv a,bim_i_top_objects ac,  ';
1560 	ELSE
1561 	l_sqltext := l_sqltext ||' FROM bim_mkt_regn_mv a, ';
1562 	END IF;
1563 	l_sqltext := l_sqltext ||'
1564 		fii_time_rpt_struct_v cal,
1565 		bis_areas_v d';
1566 	 IF l_cat_id is not null then
1567 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
1568 	  end if;
1569 	l_sqltext :=  l_sqltext ||
1570 	' WHERE a.time_id = cal.time_id
1571 	  AND  a.period_type_id = cal.period_type_id
1572 	  AND  a.object_country = :l_country';
1573 	IF l_admin_status = 'N' THEN
1574 	l_sqltext := l_sqltext ||
1575 	'
1576 	AND a.source_code_id = ac.source_code_id
1577 	AND ac.resource_id = :l_resource_id';
1578 	END IF;
1579 	IF l_cat_id is null then
1580 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
1581 	else
1582 	  l_sqltext := l_sqltext ||
1583 							 ' AND a.category_id = edh.child_id
1584 				   AND edh.object_type = ''CATEGORY_SET''
1585 				   AND edh.object_id = mdcs.category_set_id
1586 				   AND mdcs.functional_area_id = 11
1587 				   AND edh.dbi_flag = ''Y''
1588 				   AND edh.parent_id = :l_cat_id ';
1589 	  end if;
1590 	  l_sqltext :=  l_sqltext ||
1591 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
1592 	AND  d.id (+)= a.object_region
1593 	AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
1594 	AND cal.calendar_id=-1
1595 	GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
1596 	)GROUP BY name
1597 	)
1598 	)
1599 	WHERE
1600 	BIM_ATTRIBUTE3 > 0
1601 	OR BIM_ATTRIBUTE4 > 0
1602 	OR BIM_ATTRIBUTE5 > 0
1603 	OR BIM_ATTRIBUTE6 > 0
1604 	OR BIM_ATTRIBUTE7 > 0
1605 	OR BIM_ATTRIBUTE8 > 0
1606 	OR BIM_ATTRIBUTE9 > 0
1607 	OR BIM_ATTRIBUTE10 > 0
1608 	OR BIM_ATTRIBUTE11 > 0
1609 	&ORDER_BY_CLAUSE';
1610 	ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
1611 	--l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
1612 	l_sqltext :=
1613 	'
1614 	SELECT
1615 	VIEWBY,
1616 	VIEWBYID,
1617 	BIM_ATTRIBUTE2,
1618 	BIM_ATTRIBUTE3,
1619 	BIM_ATTRIBUTE4,
1620 	BIM_ATTRIBUTE5,
1621 	BIM_ATTRIBUTE6,
1622 	BIM_ATTRIBUTE7,
1623 	BIM_ATTRIBUTE8,
1624 	BIM_ATTRIBUTE9,
1625 	BIM_ATTRIBUTE10,
1626 	BIM_ATTRIBUTE11,
1627 	BIM_ATTRIBUTE12,
1628 	BIM_ATTRIBUTE13,
1629 	BIM_ATTRIBUTE14,
1630 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE17,
1631 	null BIM_URL1,
1632 	null BIM_URL2,
1633 	null BIM_URL3,
1634 	null BIM_URL4,
1635 	BIM_GRAND_TOTAL1,
1636 	BIM_GRAND_TOTAL2,
1637 	BIM_GRAND_TOTAL3,
1638 	BIM_GRAND_TOTAL4,
1639 	BIM_GRAND_TOTAL5,
1640 	BIM_GRAND_TOTAL6,
1641 	BIM_GRAND_TOTAL7,
1642 	BIM_GRAND_TOTAL8,
1643 	BIM_GRAND_TOTAL9,
1644 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
1645 	FROM
1646 	(
1647 	SELECT name VIEWBY,
1648 	meaning BIM_ATTRIBUTE2,
1649 	targeted_audience BIM_ATTRIBUTE3,
1650 	responses_positive BIM_ATTRIBUTE4,
1651 	leads BIM_ATTRIBUTE5,
1652 	rank_a BIM_ATTRIBUTE6,
1653 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
1654 	new_opportunity_amt BIM_ATTRIBUTE8,
1655 	won_opportunity_amt BIM_ATTRIBUTE9,
1656 	orders_booked_amt BIM_ATTRIBUTE10,
1657 	orders_invoiced_amt BIM_ATTRIBUTE11,
1658 	null BIM_ATTRIBUTE12,
1659 	null BIM_ATTRIBUTE13,
1660 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE14,
1661 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
1662 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
1663 	sum(leads) over() BIM_GRAND_TOTAL3,
1664 	sum(rank_a) over() BIM_GRAND_TOTAL4,
1665 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
1666 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
1667 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
1668 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
1669 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
1670 	VIEWBYID
1671 	FROM
1672 	(
1673 	SELECT
1674 	VIEWBYID,
1675 	name,
1676 	null meaning,
1677 	SUM(targeted_audience) targeted_audience,
1678 	SUM(responses_positive) responses_positive,
1679 	SUM(leads) leads,
1680 	SUM(rank_a) rank_a ,
1681 	SUM(new_opportunity_amt) new_opportunity_amt,
1682 	SUM(won_opportunity_amt) won_opportunity_amt,
1683 	SUM(orders_booked_amt) orders_booked_amt,
1684 	SUM(orders_invoiced_amt) orders_invoiced_amt,
1685 	SUM(prior_open) prior_open,
1686 	sum(leads_converted) leads_converted,
1687 	SUM(prev_new_opportunity_amt) prev_new_opportunity_amt
1688 	FROM
1689 	(
1690 	SELECT
1694 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
1691 	decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME)  name,
1692 	a.object_country viewbyid,
1693 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
1695 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
1696 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
1697 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
1698 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
1699 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
1700 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
1701 	0 prior_open,
1702 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
1703 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
1704 	FROM BIM_I_OBJ_METS_MV a,
1705 		fii_time_rpt_struct_v cal,
1706 		fnd_territories_tl  d ';
1707 	 IF l_cat_id is not null then
1708 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
1709 	  end if;
1710 
1711 	IF l_admin_status = 'N' THEN
1712 	l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
1713 	END IF;
1714 	l_sqltext :=  l_sqltext ||
1715 	' WHERE a.time_id = cal.time_id
1716 	  AND  a.period_type_id = cal.period_type_id';
1717 	IF l_admin_status = 'N' THEN
1718 	l_sqltext := l_sqltext ||
1719 	'
1720 	AND a.source_code_id = ac.source_code_id
1721 	AND ac.resource_id = :l_resource_id';
1722 	ELSE
1723 	l_sqltext := l_sqltext ||
1724 	' AND  a.immediate_parent_id is null ';
1725 	END IF;
1726 	IF l_cat_id is null then
1727 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
1728 	else
1729 	  l_sqltext := l_sqltext ||
1730 							 ' AND a.category_id = edh.child_id
1731 				   AND edh.object_type = ''CATEGORY_SET''
1732 				   AND edh.object_id = mdcs.category_set_id
1733 				   AND mdcs.functional_area_id = 11
1734 				   AND edh.dbi_flag = ''Y''
1735 				   AND edh.parent_id = :l_cat_id ';
1736 	  end if;
1737 	  if l_country <>'N' then
1738 	  l_sqltext :=  l_sqltext || ' AND a.object_country = :l_country ';
1739 	  else
1740 	  l_sqltext :=  l_sqltext || ' AND a.object_country <>''N'' ';
1741 	  end if;
1742 	l_sqltext :=  l_sqltext ||
1743 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
1744 	  AND  a.object_country =d.territory_code(+)
1745 	  AND  d.language(+) = userenv(''LANG'')
1746 	  AND cal.report_date IN (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
1747 	  AND cal.calendar_id=-1
1748 	GROUP BY decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country
1749 	UNION ALL
1750 	SELECT
1751 	decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
1752 	a.object_country viewbyid,
1753 	0 targeted_audience,
1754 	0 responses_positive,
1755 	0 leads,
1756 	0 rank_a ,
1757 	0 new_opportunity_amt,
1758 	0 won_opportunity_amt,
1759 	0 orders_booked_amt,
1760 	0 orders_invoiced_amt,
1761 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
1762 	0 leads_converted,
1763 	0 prev_new_opportunity_amt
1764 	FROM BIM_I_OBJ_METS_MV a,
1765 		fii_time_rpt_struct_v cal,
1766 		fnd_territories_tl d ';
1767 	 IF l_cat_id is not null then
1768 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
1769 	  end if;
1770 
1771 	IF l_admin_status = 'N' THEN
1772 	l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
1773 	END IF;
1774 	l_sqltext :=  l_sqltext ||
1775 	' WHERE a.time_id = cal.time_id
1776 	  AND  a.period_type_id = cal.period_type_id';
1777 	IF l_admin_status = 'N' THEN
1778 	l_sqltext := l_sqltext ||
1779 	'
1780 	AND a.source_code_id = ac.source_code_id
1781 	AND ac.resource_id = :l_resource_id';
1782 	ELSE
1783 	l_sqltext := l_sqltext ||
1784 	' AND  a.immediate_parent_id is null ';
1785 	END IF;
1786 	IF l_cat_id is null then
1787 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
1788 	else
1789 	  l_sqltext := l_sqltext ||
1790 							 ' AND a.category_id = edh.child_id
1791 				   AND edh.object_type = ''CATEGORY_SET''
1792 				   AND edh.object_id = mdcs.category_set_id
1793 				   AND mdcs.functional_area_id = 11
1794 				   AND edh.dbi_flag = ''Y''
1795 				   AND edh.parent_id = :l_cat_id ';
1796 	  end if;
1797 	  if l_country <>'N' then
1798 	  l_sqltext :=  l_sqltext || ' AND a.object_country = :l_country ';
1799 	  else
1800 	  l_sqltext :=  l_sqltext || ' AND a.object_country <>''N'' ';
1801 	  end if;
1802 	l_sqltext :=  l_sqltext ||
1803 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
1804 	AND  a.object_country =d.territory_code(+)
1808 	GROUP BY decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country
1805 	AND d.language(+) = userenv(''LANG'')
1806 	AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
1807 	AND cal.calendar_id=-1
1809 	)GROUP BY name,viewbyid
1810 	)
1811 	)
1812 	WHERE
1813 	BIM_ATTRIBUTE3 > 0
1814 	OR BIM_ATTRIBUTE4 > 0
1815 	OR BIM_ATTRIBUTE5 > 0
1816 	OR BIM_ATTRIBUTE6 > 0
1817 	OR BIM_ATTRIBUTE7 > 0
1818 	OR BIM_ATTRIBUTE8 > 0
1819 	OR BIM_ATTRIBUTE9 > 0
1820 	OR BIM_ATTRIBUTE10 > 0
1821 	OR BIM_ATTRIBUTE11 > 0
1822 	&ORDER_BY_CLAUSE';
1823 
1824 ELSE
1825 	/************** START OF VIEW BY IS CAMPAIGN AND L_CAMPAIGN_ID IS NULL ***********/
1826 
1827 	l_sqltext :=
1828 	'
1829 	SELECT
1830 	VIEWBY,
1831 	VIEWBYID,
1832 	BIM_ATTRIBUTE2,
1833 	BIM_ATTRIBUTE3,
1834 	BIM_ATTRIBUTE4,
1835 	BIM_ATTRIBUTE5,
1836 	BIM_ATTRIBUTE6,
1837 	BIM_ATTRIBUTE7,
1838 	BIM_ATTRIBUTE8,
1839 	BIM_ATTRIBUTE9,
1840 	BIM_ATTRIBUTE10,
1841 	BIM_ATTRIBUTE11,
1842 	BIM_ATTRIBUTE12,
1843 	BIM_ATTRIBUTE13,
1844 	BIM_ATTRIBUTE14,
1845 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE17,
1846 	null BIM_URL1,
1847 	decode(BIM_ATTRIBUTE13,''EONE'',NULL,'||''''||l_url_str||''''||' ) BIM_URL2,
1848 	null BIM_URL3,
1849 	null BIM_URL4,
1850 	BIM_GRAND_TOTAL1,
1851 	BIM_GRAND_TOTAL2,
1852 	BIM_GRAND_TOTAL3,
1853 	BIM_GRAND_TOTAL4,
1854 	BIM_GRAND_TOTAL5,
1855 	BIM_GRAND_TOTAL6,
1856 	BIM_GRAND_TOTAL7,
1857 	BIM_GRAND_TOTAL8,
1858 	BIM_GRAND_TOTAL9,
1859 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
1860 	FROM
1861 	(
1862 	SELECT name VIEWBY,
1863 	meaning BIM_ATTRIBUTE2,
1864 	targeted_audience BIM_ATTRIBUTE3,
1865 	responses_positive BIM_ATTRIBUTE4,
1866 	leads BIM_ATTRIBUTE5,
1867 	rank_a BIM_ATTRIBUTE6,
1868 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
1869 	new_opportunity_amt BIM_ATTRIBUTE8,
1870 	won_opportunity_amt BIM_ATTRIBUTE9,
1871 	orders_booked_amt BIM_ATTRIBUTE10,
1872 	orders_invoiced_amt BIM_ATTRIBUTE11,
1873 	null BIM_ATTRIBUTE12,
1874 	object_type BIM_ATTRIBUTE13,
1875 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE14,
1876 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
1877 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
1878 	sum(leads) over() BIM_GRAND_TOTAL3,
1879 	sum(rank_a) over() BIM_GRAND_TOTAL4,
1880 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
1881 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
1882 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
1883 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
1884 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
1885 	VIEWBYID
1886 	FROM
1887 	(
1888 	select
1889 	VIEWBYID,object_type,name,meaning,SUM(targeted_audience) targeted_audience,SUM(responses_positive) responses_positive,
1890 	SUM(leads) leads,SUM(rank_a) rank_a ,SUM(new_opportunity_amt) new_opportunity_amt,SUM(won_opportunity_amt) won_opportunity_amt,SUM(orders_booked_amt) orders_booked_amt,
1891 	SUM(orders_invoiced_amt) orders_invoiced_amt,SUM(prior_open) prior_open,sum(leads_converted) leads_converted,SUM(prev_new_opportunity_amt) prev_new_opportunity_amt
1892 	FROM
1893 	(
1894 	select
1895 	campname.object_type object_type,camp.VIEWBYID,
1896 	campname.name name,l.meaning meaning,targeted_audience,responses_positive,
1897 	leads,rank_a ,new_opportunity_amt,won_opportunity_amt,orders_booked_amt,orders_invoiced_amt,prior_open,
1898 	leads_converted,prev_new_opportunity_amt
1899 	FROM
1900 	(
1901 	SELECT /*+ NO_MERGE */
1902 	a.source_code_id VIEWBYID,
1903 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
1904 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
1905 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
1906 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
1907 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
1908 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
1909 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
1910 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
1911 	0 prior_open,
1912 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
1913 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
1914 	FROM BIM_I_OBJ_METS_MV a,
1915 		fii_time_rpt_struct_v cal ';
1916 	 IF l_cat_id is not null then
1917 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
1918 	  end if;
1919 
1920 	IF l_admin_status = 'N' THEN
1921 		l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
1922 	END IF;
1923 
1924 	l_sqltext :=  l_sqltext ||' WHERE a.time_id = cal.time_id
1925 						AND  a.period_type_id = cal.period_type_id ';
1926 
1927 	/*if (l_prog_view = 'Y') then
1928 	l_sqltext := l_sqltext ||
1929 	' AND a.object_type in (''CAMP'',''RCAM'')';
1930 	ELSE
1931 	l_sqltext := l_sqltext ||
1935 	IF l_admin_status = 'N' THEN
1932 	' AND a.object_type =''CAMP''';
1933 	END IF; */
1934 
1936 	l_sqltext := l_sqltext ||
1937 	' AND a.source_code_id = ac.source_code_id
1938 	  AND ac.resource_id = :l_resource_id';
1939 	ELSE
1940 	  IF l_prog_view='Y' then
1941 		  l_sqltext := l_sqltext ||
1942 		 ' AND  a.immediate_parent_id is null ';
1943 	   END IF;
1944 	END IF;
1945 
1946 	IF l_cat_id is null then
1947 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
1948 	else
1949 	  l_sqltext := l_sqltext ||
1950 							 ' AND a.category_id = edh.child_id
1951 				   AND edh.object_type = ''CATEGORY_SET''
1952 				   AND edh.object_id = mdcs.category_set_id
1953 				   AND mdcs.functional_area_id = 11
1954 				   AND edh.dbi_flag = ''Y''
1955 				   AND edh.parent_id = :l_cat_id ';
1956 	  end if;
1957 	l_sqltext :=  l_sqltext ||
1958 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
1959 	AND a.object_country = :l_country
1960 	AND cal.report_date IN (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
1961 	AND cal.calendar_id=-1
1962 	AND (a.leads >0 OR a.rank_a >0 OR a.leads_converted >0 OR  a.customers_targeted >0 OR a.responses_positive>0 OR new_opportunity_amt >0 OR won_opportunity_amt >0 OR orders_booked_amt >0 OR a.orders_invoiced_amt >0 )
1963 	GROUP BY a.source_code_id
1964 	UNION ALL
1965 	SELECT /*+ NO_MERGE */
1966 	a.source_code_id VIEWBYID,0 targeted_audience,0 responses_positive,
1967 	0 leads,0 rank_a ,0 new_opportunity_amt,0 won_opportunity_amt,0 orders_booked_amt,0 orders_invoiced_amt,
1968 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,0 leads_converted,0 prev_new_opportunity_amt
1969 	FROM BIM_I_OBJ_METS_MV a,
1970 		fii_time_rpt_struct_v cal ';
1971 		 IF l_cat_id is not null then
1972 	  l_sqltext := l_sqltext ||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
1973 	  end if;
1974 	IF l_admin_status = 'N' THEN
1975 		l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
1976 	END IF;
1977 	l_sqltext :=  l_sqltext ||
1978 	' WHERE a.time_id = cal.time_id
1979 	AND  a.period_type_id = cal.period_type_id ';
1980 	/*if (l_prog_view = 'Y') then
1981 	l_sqltext := l_sqltext ||
1982 	' AND a.object_type in (''CAMP'',''RCAM'')';
1983 	ELSE
1984 	l_sqltext := l_sqltext ||
1985 	' AND a.object_type =''CAMP''';
1986 	END IF; */
1987 
1988 	IF l_admin_status = 'N' THEN
1989 	l_sqltext := l_sqltext ||
1990 	'  AND a.source_code_id = ac.source_code_id
1991 	AND ac.resource_id = :l_resource_id';
1992 	ELSE
1993 	IF l_prog_view='Y' then
1994 	l_sqltext := l_sqltext ||
1995 	' AND  a.immediate_parent_id is null ';
1996 	end if;
1997 	END IF;
1998 	IF l_cat_id is null then
1999 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
2000 	else
2001 	  l_sqltext := l_sqltext ||
2002 							 ' AND a.category_id = edh.child_id
2003 				   AND edh.object_type = ''CATEGORY_SET''
2004 				   AND edh.object_id = mdcs.category_set_id
2005 				   AND mdcs.functional_area_id = 11
2006 				   AND edh.dbi_flag = ''Y''
2007 				   AND edh.parent_id = :l_cat_id ';
2008 	  end if;
2009 	l_sqltext :=  l_sqltext ||
2010 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
2011 	AND a.object_country = :l_country
2012 	AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
2013 	AND cal.calendar_id=-1
2014 	AND (a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) <> 0
2015 	GROUP BY a.source_code_id
2016 	) camp ,BIM_I_OBJ_NAME_MV campname,ams_lookups l
2017 	WHERE campname.source_code_id = camp.viewbyid
2018 	AND campname.language =USERENV(''LANG'')
2019 	AND l.lookup_code = campname.object_type
2020 	AND l.lookup_type = ''AMS_SYS_ARC_QUALIFIER''
2021 	)GROUP BY viewbyid,object_type,name,meaning ) )
2022 	WHERE
2023 	BIM_ATTRIBUTE3 > 0 OR BIM_ATTRIBUTE4 > 0 OR BIM_ATTRIBUTE5 > 0 OR BIM_ATTRIBUTE6 > 0 OR BIM_ATTRIBUTE7 > 0
2024 	OR BIM_ATTRIBUTE8 > 0 OR BIM_ATTRIBUTE9 > 0 OR BIM_ATTRIBUTE10 > 0 OR BIM_ATTRIBUTE11 > 0
2025 	&ORDER_BY_CLAUSE';
2026 
2027 	END IF;
2028 
2029 	ELSE
2030 	/***************** DRILL DOWN IN CAMPAIGN HIRERACHY**********/
2031 	if (l_view_by = 'CAMPAIGN+CAMPAIGN') then
2032 
2033 	 -- checking for the object type passed from page
2034 
2035 	 for i in get_obj_type
2036 	 loop
2037 	 l_object_type:=i.object_type;
2038 	 end loop;
2039 
2040 	/*IF l_object_type='CAMP' THEN
2041 	 l_csch_chnl :='|| '' - '' || channel';
2042 	 l_chnl_col:= 'channel,';
2043 	 END IF;*/
2044 
2045 	 if l_object_type='CAMP' THEN
2046 	 l_csch_chnl :='|| '' - '' || channel';
2047 	 l_chnl_col := 'channel,';
2048 	 l_chnl_select := ' decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel,';
2049 	 l_chnl_from:= ' ,bim_dimv_media chnl ';
2050 	 l_chnl_where := ' AND campname.activity_id =chnl.id (+) ';
2051 	 l_chnl_group := ' decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) ,';
2052 
2053 	 END IF;
2054 
2055 
2056 	l_sqltext :=
2057 	'
2058 	SELECT
2059 	VIEWBY,
2060 	VIEWBYID,
2061 	BIM_ATTRIBUTE2,
2062 	BIM_ATTRIBUTE3,
2066 	BIM_ATTRIBUTE7,
2063 	BIM_ATTRIBUTE4,
2064 	BIM_ATTRIBUTE5,
2065 	BIM_ATTRIBUTE6,
2067 	BIM_ATTRIBUTE8,
2068 	BIM_ATTRIBUTE9,
2069 	BIM_ATTRIBUTE10,
2070 	BIM_ATTRIBUTE11,
2071 	BIM_ATTRIBUTE12,
2072 	BIM_ATTRIBUTE13,
2073 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE14,
2074 	BIM_ATTRIBUTE15,
2075 	null BIM_URL1,
2076 	BIM_URL2,
2077 	BIM_URL3,
2078 	BIM_URL4,
2079 	BIM_GRAND_TOTAL1,
2080 	BIM_GRAND_TOTAL2,
2081 	BIM_GRAND_TOTAL3,
2082 	BIM_GRAND_TOTAL4,
2083 	BIM_GRAND_TOTAL5,
2084 	BIM_GRAND_TOTAL6,
2085 	BIM_GRAND_TOTAL7,
2086 	BIM_GRAND_TOTAL8,
2087 	BIM_GRAND_TOTAL9,
2088 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
2089 	FROM
2090 	(
2091 	SELECT name VIEWBY,
2092 	meaning'||l_csch_chnl||' BIM_ATTRIBUTE2,
2093 	decode(object_type,''CSCH'','||''''||l_url_str_csch||''''||'||object_id,''EONE'',NULL,''EVEO'',NULL,'||''''||l_url_str||''''||') BIM_URL2,
2094 	decode(object_type,''CSCH'','||''''||l_url_str_type||''''||'||object_id,NULL) BIM_URL3,
2095 	decode(object_type,''CSCH'',decode(usage,''LITE'',decode(list_header_id,null,null,'||''''||l_url_str_tga||''''||'||list_header_id),NULL),NULL ) BIM_URL4,
2096 	targeted_audience BIM_ATTRIBUTE3,
2097 	responses_positive BIM_ATTRIBUTE4,
2098 	leads BIM_ATTRIBUTE5,
2099 	rank_a BIM_ATTRIBUTE6,
2100 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
2101 	new_opportunity_amt BIM_ATTRIBUTE8,
2102 	won_opportunity_amt BIM_ATTRIBUTE9,
2103 	orders_booked_amt BIM_ATTRIBUTE10,
2104 	orders_invoiced_amt BIM_ATTRIBUTE11,
2105 	null BIM_ATTRIBUTE12,
2106 	object_type BIM_ATTRIBUTE13,
2107 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE15,
2108 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
2109 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
2110 	sum(leads) over() BIM_GRAND_TOTAL3,
2111 	sum(rank_a) over() BIM_GRAND_TOTAL4,
2112 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
2113 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
2114 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
2115 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
2116 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
2117 	VIEWBYID
2118 	FROM
2119 	(
2120 	SELECT
2121 	object_id,
2122 	object_type ,
2123 	VIEWBYID ,
2124 	name ,
2125 	meaning,'||l_chnl_col||'
2126 	decode(object_type,''CSCH'',usage,NULL) usage,
2127 	list_header_id,
2128 	SUM(targeted_audience) targeted_audience,
2129 	SUM(responses_positive) responses_positive,
2130 	SUM(leads) leads,
2131 	SUM(rank_a) rank_a ,
2132 	SUM(new_opportunity_amt) new_opportunity_amt,
2133 	SUM(won_opportunity_amt) won_opportunity_amt,
2134 	SUM(orders_booked_amt) orders_booked_amt,
2135 	SUM(orders_invoiced_amt) orders_invoiced_amt,
2136 	SUM(prior_open) prior_open,
2137 	sum(leads_converted) leads_converted,
2138 	SUM(prev_new_opportunity_amt) prev_new_opportunity_amt
2139 	FROM
2140 	( ';
2141 	/********** CHILDERN OF PROGRAM *********************/
2142 	l_sqltext := l_sqltext||
2143 	' SELECT /*+LEADING(b)*/
2144 	campname.object_id,
2145 	campname.object_type object_type,
2146 	a.source_code_id VIEWBYID,
2147 	campname.name name,
2148 	l.meaning meaning,'||l_chnl_select||'
2149 	campname.child_object_usage usage,
2150 	NULL list_header_id,
2151 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
2155 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
2152 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
2153 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
2154 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
2156 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
2157 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
2158 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
2159 	0 prior_open,
2160 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
2161 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
2162 	FROM BIM_I_OBJ_METS_MV a,
2163 		fii_time_rpt_struct_v cal,
2164 		bim_I_obj_name_mv campname,
2165 		ams_lookups l '||l_chnl_from ;
2166 	 IF l_cat_id is not null then
2167 	  l_sqltext := l_sqltext ||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
2168 	  end if;
2169 	/* IF l_admin_status = 'N' THEN
2170 	l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
2171 	END IF; */
2172 	l_sqltext :=  l_sqltext ||
2173 	' WHERE a.time_id = cal.time_id
2174 	AND  a.period_type_id = cal.period_type_id
2175 	AND  a.source_code_id = campname.source_code_id
2176 	AND  a.immediate_parent_id = :l_campaign_id
2177 	AND  l.lookup_code = campname.object_type
2178 	AND l.lookup_type = ''AMS_SYS_ARC_QUALIFIER'''||l_chnl_where;
2179 	 /* IF l_admin_status = 'N' THEN
2180 	l_sqltext := l_sqltext ||
2181 	'
2182 	AND a.source_code_id = ac.source_code_id
2183 	AND ac.resource_id = :l_resource_id';
2184 	END IF;*/
2185 	IF l_cat_id is null then
2186 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
2187 	else
2188 	  l_sqltext := l_sqltext ||
2189 	' AND a.category_id = edh.child_id
2190 	  AND edh.object_type = ''CATEGORY_SET''
2191 	  AND edh.object_id = mdcs.category_set_id
2192 	  AND mdcs.functional_area_id = 11
2193 	  AND edh.dbi_flag = ''Y''
2194 	  AND edh.parent_id = :l_cat_id ';
2195 	  end if;
2196 	l_sqltext :=  l_sqltext ||
2197 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
2198 	AND a.object_country = :l_country
2199 	AND cal.report_date  in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
2200 	AND cal.calendar_id=-1
2201 	AND campname.language =USERENV(''LANG'')
2202 	GROUP BY a.source_code_id,campname.object_type,campname.object_id,
2203 	campname.name, l.meaning,'||l_chnl_group||'campname.child_object_usage
2204 	UNION ALL
2205 	SELECT /*+LEADING(b)*/
2206 	campname.object_id object_id,
2207 	campname.object_type object_type,
2208 	a.source_code_id VIEWBYID,
2209 	campname.name name,
2210 	l.meaning meaning,'||l_chnl_select||'
2211 	campname.child_object_usage usage,
2212 	NULL list_header_id,
2213 	0 targeted_audience,
2214 	0 responses_positive,
2215 	0 leads,
2216 	0 rank_a ,
2217 	0 new_opportunity_amt,
2218 	0 won_opportunity_amt,
2219 	0 orders_booked_amt,
2220 	0 orders_invoiced_amt,
2221 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
2222 	0 leads_converted,
2223 	0 prev_new_opportunity_amt
2224 	FROM BIM_I_OBJ_METS_MV a,
2225 		fii_time_rpt_struct_v cal,
2226 		bim_i_obj_name_mv campname,
2227 		ams_lookups l '||l_chnl_from;
2228 	 IF l_cat_id is not null then
2229 	  l_sqltext := l_sqltext ||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
2230 	  end if;
2231 	/* IF l_admin_status = 'N' THEN
2232 	l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
2233 	END IF; */
2234 	l_sqltext :=  l_sqltext ||
2235 	' WHERE a.time_id = cal.time_id
2236 	AND  a.period_type_id = cal.period_type_id
2237 	AND campname.source_code_id = a.source_code_id
2238 	AND  a.immediate_parent_id = :l_campaign_id
2239 	AND l.lookup_code =  campname.object_type
2240 	AND l.lookup_type = ''AMS_SYS_ARC_QUALIFIER'''||l_chnl_where;
2241 	/* IF l_admin_status = 'N' THEN
2242 	l_sqltext := l_sqltext ||
2243 	'
2244 	AND a.source_code_id = ac.source_code_id
2245 	AND ac.resource_id = :l_resource_id';
2246 	END IF; */
2247 	IF l_cat_id is null then
2248 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
2249 	else
2250 	  l_sqltext := l_sqltext ||
2251 	' AND a.category_id = edh.child_id
2252 	  AND edh.object_type = ''CATEGORY_SET''
2253 	  AND edh.object_id = mdcs.category_set_id
2254 	  AND mdcs.functional_area_id = 11
2255 	  AND edh.dbi_flag = ''Y''
2256 	  AND edh.parent_id = :l_cat_id ';
2257 	  end if;
2258 	l_sqltext :=  l_sqltext ||
2259 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
2260 	AND a.object_country = :l_country
2261 	AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
2262 	AND cal.calendar_id=-1
2263 	AND campname.language =USERENV(''LANG'')
2264 	GROUP BY a.source_code_id,campname.object_type,campname.object_id,
2265 	campname.name, l.meaning,'||l_chnl_group||'campname.child_object_usage ';
2266 	--END IF;
2267 	l_sqltext := l_sqltext ||
2271 	WHERE
2268 	') group by VIEWBYID,object_id,object_type,name,meaning,'||l_chnl_col||' usage,list_header_id
2269 	)
2270 	)
2272 	BIM_ATTRIBUTE3 > 0
2273 	OR BIM_ATTRIBUTE4 > 0
2274 	OR BIM_ATTRIBUTE5 > 0
2275 	OR BIM_ATTRIBUTE6 > 0
2276 	OR BIM_ATTRIBUTE7 > 0
2277 	OR BIM_ATTRIBUTE8 > 0
2278 	OR BIM_ATTRIBUTE9 > 0
2279 	OR BIM_ATTRIBUTE10 > 0
2280 	OR BIM_ATTRIBUTE11 > 0
2281 	&ORDER_BY_CLAUSE';
2282 
2283 	ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
2284 
2285 	IF l_cat_id is null THEN
2286 	/************** START OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NULL AND CAMPAIGN ID IS NOT NULL ***********/
2287 	l_sqltext :=
2288 	'
2289 	SELECT
2290 	VIEWBY,
2291 	VIEWBYID,
2292 	BIM_ATTRIBUTE2,
2293 	BIM_ATTRIBUTE3,
2294 	BIM_ATTRIBUTE4,
2295 	BIM_ATTRIBUTE5,
2296 	BIM_ATTRIBUTE6,
2297 	BIM_ATTRIBUTE7,
2298 	BIM_ATTRIBUTE8,
2299 	BIM_ATTRIBUTE9,
2300 	BIM_ATTRIBUTE10,
2301 	BIM_ATTRIBUTE11,
2302 	BIM_ATTRIBUTE12,
2303 	BIM_ATTRIBUTE13,
2304 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE14,
2305 	BIM_ATTRIBUTE15,
2306 	decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) BIM_URL1,
2307 	null BIM_URL2,
2308 	null BIM_URL3,
2309 	null BIM_URL4,
2310 	BIM_GRAND_TOTAL1,
2311 	BIM_GRAND_TOTAL2,
2312 	BIM_GRAND_TOTAL3,
2313 	BIM_GRAND_TOTAL4,
2314 	BIM_GRAND_TOTAL5,
2315 	BIM_GRAND_TOTAL6,
2316 	BIM_GRAND_TOTAL7,
2317 	BIM_GRAND_TOTAL8,
2318 	BIM_GRAND_TOTAL9,
2319 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
2320 	FROM
2321 	(
2322 	SELECT name VIEWBY,
2323 	leaf_node_flag,
2324 	null BIM_ATTRIBUTE2,
2325 	targeted_audience BIM_ATTRIBUTE3,
2326 	responses_positive BIM_ATTRIBUTE4,
2327 	leads BIM_ATTRIBUTE5,
2328 	rank_a BIM_ATTRIBUTE6,
2329 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
2330 	new_opportunity_amt BIM_ATTRIBUTE8,
2331 	won_opportunity_amt BIM_ATTRIBUTE9,
2332 	orders_booked_amt BIM_ATTRIBUTE10,
2333 	orders_invoiced_amt BIM_ATTRIBUTE11,
2334 	null BIM_ATTRIBUTE12,
2335 	null BIM_ATTRIBUTE13,
2336 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE15,
2337 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
2338 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
2339 	sum(leads) over() BIM_GRAND_TOTAL3,
2340 	sum(rank_a) over() BIM_GRAND_TOTAL4,
2341 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
2342 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
2343 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
2344 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
2345 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
2346 	VIEWBYID
2347 	FROM
2348 	(
2349 	SELECT
2350 	VIEWBYID,
2351 	name,
2352 	leaf_node_flag,
2353 	sum(targeted_audience) targeted_audience,
2354 	sum(responses_positive) responses_positive,
2355 	sum(leads) leads,
2356 	sum(rank_a) rank_a ,
2357 	sum(new_opportunity_amt) new_opportunity_amt,
2358 	sum(won_opportunity_amt) won_opportunity_amt,
2359 	sum(orders_booked_amt) orders_booked_amt,
2360 	sum(orders_invoiced_amt) orders_invoiced_amt,
2361 	sum(prior_open) prior_open,
2362 	sum(leads_converted) leads_converted,
2363 	sum(prev_new_opportunity_amt) prev_new_opportunity_amt
2364 	FROM
2365 	( SELECT /*+ORDERED*/
2366 	p.parent_id VIEWBYID,
2367 	p.value  name,
2368 	p.leaf_node_flag leaf_node_flag,
2369 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
2370 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
2371 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
2372 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
2373 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
2374 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
2375 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
2376 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
2377 	0 prior_open,
2378 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
2379 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
2380 	FROM  fii_time_rpt_struct_v cal
2384 					,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
2381 		  ,BIM_I_OBJ_METS_MV a
2382 		,eni_denorm_hierarchies edh
2383 					,mtl_default_category_sets mdcs
2385 					   FROM eni_item_vbh_nodes_v e
2386 					   WHERE e.top_node_flag=''Y''
2387 					   AND e.child_id = e.parent_id) p ';
2388 
2389 	/* IF l_admin_status = 'N' THEN
2390 	l_sqltext := l_sqltext ||',bim_i_top_objects ac ';
2391 	END IF;*/
2392 
2393 	l_sqltext :=  l_sqltext ||
2394 	' WHERE a.time_id = cal.time_id
2395 	AND  a.period_type_id = cal.period_type_id
2396 	AND a.category_id = edh.child_id
2397 	AND edh.object_type = ''CATEGORY_SET''
2398 	AND edh.object_id = mdcs.category_set_id
2399 	AND mdcs.functional_area_id = 11
2400 	AND edh.dbi_flag = ''Y''
2401 	AND edh.parent_id = p.parent_id
2402 	AND  a.source_code_id = :l_campaign_id ';
2403 
2404 	/* IF l_admin_status = 'N' THEN
2405 	l_sqltext := l_sqltext ||
2406 	'
2407 	AND a.source_code_id = ac.source_code_id
2408 	AND ac.resource_id = :l_resource_id';
2409 	ELSE
2410 	l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
2411 	END IF;*/
2412 
2413 	l_sqltext :=  l_sqltext ||
2414 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
2415 	  AND a.object_country = :l_country';
2416 	l_sqltext :=  l_sqltext ||
2417 	' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
2418 	AND cal.calendar_id=-1
2419 	GROUP BY p.value,p.parent_id,p.leaf_node_flag
2420 	UNION ALL
2421 	SELECT /*+ORDERED*/
2422 	p.parent_id VIEWBYID,
2423 	p.value  name,
2424 	p.leaf_node_flag leaf_node_flag,
2425 	0 targeted_audience,
2426 	0 responses_positive,
2427 	0 leads,
2428 	0 rank_a ,
2429 	0 new_opportunity_amt,
2430 	0 won_opportunity_amt,
2431 	0 orders_booked_amt,
2432 	0 orders_invoiced_amt,
2433 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
2434 	0 leads_converted,
2435 	0 prev_new_opportunity_amt
2436 	FROM fii_time_rpt_struct_v cal
2437 		 ,BIM_I_OBJ_METS_MV a
2438 		,eni_denorm_hierarchies edh
2439 					,mtl_default_category_sets mdcs
2440 					,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
2441 					   FROM eni_item_vbh_nodes_v e
2442 					   WHERE e.top_node_flag=''Y''
2443 					   AND e.child_id = e.parent_id) p ';
2444 	/*
2445 	IF l_admin_status = 'N' THEN
2446 	l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
2447 	END IF;*/
2448 
2449 	l_sqltext :=  l_sqltext ||
2450 	' WHERE a.time_id = cal.time_id
2451 	AND  a.period_type_id = cal.period_type_id
2452 	AND a.category_id = edh.child_id
2453 	AND edh.object_type = ''CATEGORY_SET''
2454 	AND edh.object_id = mdcs.category_set_id
2455 	AND mdcs.functional_area_id = 11
2456 	AND edh.dbi_flag = ''Y''
2457 	AND edh.parent_id = p.parent_id
2458 	AND  a.source_code_id = :l_campaign_id ';
2459 
2460 	/* IF l_admin_status = 'N' THEN
2461 	l_sqltext := l_sqltext ||
2462 	'
2463 	AND a.source_code_id = ac.source_code_id
2464 	AND ac.resource_id = :l_resource_id';
2465 	ELSE
2466 	l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
2467 	END IF;*/
2468 
2469 	l_sqltext :=  l_sqltext ||
2470 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
2471 	AND a.object_country = :l_country';
2472 	l_sqltext :=  l_sqltext ||
2473 	' AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
2474 	AND cal.calendar_id=-1
2475 	GROUP BY p.value,p.parent_id,p.leaf_node_flag
2476 	)GROUP BY name,VIEWBYID,leaf_node_flag
2477 	)
2478 	)
2479 	WHERE
2480 	BIM_ATTRIBUTE3 > 0
2481 	OR BIM_ATTRIBUTE4 > 0
2482 	OR BIM_ATTRIBUTE5 > 0
2483 	OR BIM_ATTRIBUTE6 > 0
2484 	OR BIM_ATTRIBUTE7 > 0
2485 	OR BIM_ATTRIBUTE8 > 0
2486 	OR BIM_ATTRIBUTE9 > 0
2487 	OR BIM_ATTRIBUTE10 > 0
2488 	OR BIM_ATTRIBUTE11 > 0
2489 	&ORDER_BY_CLAUSE';
2490 	/************** END OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NULL AND CAMPAIGN IS NOT NULL ***********/
2491 	ELSE
2492 	/************** START OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NOT NULL AND CAMPAIGN IS NOT NULL ***********/
2493 	l_dass:=  BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('DASS');
2494 	l_sqltext :=
2495 	'
2496 	SELECT
2497 	VIEWBY,
2498 	VIEWBYID,
2499 	BIM_ATTRIBUTE2,
2500 	BIM_ATTRIBUTE3,
2501 	BIM_ATTRIBUTE4,
2502 	BIM_ATTRIBUTE5,
2503 	BIM_ATTRIBUTE6,
2504 	BIM_ATTRIBUTE7,
2505 	BIM_ATTRIBUTE8,
2506 	BIM_ATTRIBUTE9,
2507 	BIM_ATTRIBUTE10,
2508 	BIM_ATTRIBUTE11,
2509 	BIM_ATTRIBUTE12,
2510 	BIM_ATTRIBUTE13,
2511 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE14,
2512 	BIM_ATTRIBUTE15,
2513 	decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) BIM_URL1,
2514 	null BIM_URL2,
2515 	null BIM_URL3,
2516 	null BIM_URL4,
2517 	BIM_GRAND_TOTAL1,
2518 	BIM_GRAND_TOTAL2,
2519 	BIM_GRAND_TOTAL3,
2520 	BIM_GRAND_TOTAL4,
2521 	BIM_GRAND_TOTAL5,
2522 	BIM_GRAND_TOTAL6,
2523 	BIM_GRAND_TOTAL7,
2524 	BIM_GRAND_TOTAL8,
2525 	BIM_GRAND_TOTAL9,
2526 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
2527 	FROM
2528 	(
2529 	SELECT name VIEWBY,
2530 	leaf_node_flag,
2531 	null BIM_ATTRIBUTE2,
2532 	targeted_audience BIM_ATTRIBUTE3,
2536 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
2533 	responses_positive BIM_ATTRIBUTE4,
2534 	leads BIM_ATTRIBUTE5,
2535 	rank_a BIM_ATTRIBUTE6,
2537 	new_opportunity_amt BIM_ATTRIBUTE8,
2538 	won_opportunity_amt BIM_ATTRIBUTE9,
2539 	orders_booked_amt BIM_ATTRIBUTE10,
2540 	orders_invoiced_amt BIM_ATTRIBUTE11,
2541 	null BIM_ATTRIBUTE12,
2542 	null BIM_ATTRIBUTE13,
2543 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE15,
2544 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
2545 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
2546 	sum(leads) over() BIM_GRAND_TOTAL3,
2547 	sum(rank_a) over() BIM_GRAND_TOTAL4,
2548 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
2549 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
2550 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
2551 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
2552 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
2553 	VIEWBYID
2554 	FROM
2555 	(
2556 	SELECT
2557 	VIEWBYID,
2558 	name,
2559 	leaf_node_flag,
2560 	sum(targeted_audience) targeted_audience,
2561 	sum(responses_positive) responses_positive,
2562 	sum(leads) leads,
2563 	sum(rank_a) rank_a ,
2564 	sum(new_opportunity_amt) new_opportunity_amt,
2565 	sum(won_opportunity_amt) won_opportunity_amt,
2566 	sum(orders_booked_amt) orders_booked_amt,
2567 	sum(orders_invoiced_amt) orders_invoiced_amt,
2568 	sum(prior_open) prior_open,
2569 	sum(leads_converted) leads_converted,
2570 	sum(prev_new_opportunity_amt) prev_new_opportunity_amt
2571 	FROM
2572 	(
2573 	SELECT /*+ORDERED*/
2574 	p.id VIEWBYID,
2575 	p.value  name,
2576 	p.leaf_node_flag leaf_node_flag,
2577 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
2578 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
2579 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
2580 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
2581 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
2582 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
2583 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
2584 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
2585 	0 prior_open,
2586 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
2587 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
2588 	FROM  fii_time_rpt_struct_v cal
2589 		  ,BIM_I_OBJ_METS_MV a
2590 		,eni_denorm_hierarchies edh
2591 				,mtl_default_category_sets mdc
2592 				,(select e.id,e.value,e.leaf_node_flag
2593 				  from eni_item_vbh_nodes_v e
2594 			  where
2595 				  e.parent_id =:l_cat_id
2596 				  AND e.id = e.child_id
2597 				  AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
2598 		  ) p ';
2599 	/*
2600 	IF l_admin_status = 'N' THEN
2601 	l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
2602 	END IF;*/
2603 
2604 	l_sqltext :=  l_sqltext ||
2605 	' WHERE a.time_id = cal.time_id
2606 	AND  a.period_type_id = cal.period_type_id
2607 	AND a.category_id = edh.child_id
2608 	AND edh.object_type = ''CATEGORY_SET''
2609 	AND edh.object_id = mdc.category_set_id
2610 	AND mdc.functional_area_id = 11
2611 	AND edh.dbi_flag = ''Y''
2612 	AND edh.parent_id = p.id
2613 	AND  a.source_code_id = :l_campaign_id ';
2614 	/* IF l_admin_status = 'N' THEN
2615 	l_sqltext := l_sqltext ||
2616 	'
2617 	AND a.source_code_id = ac.source_code_id
2618 	AND ac.resource_id = :l_resource_id';
2619 	ELSE
2620 	l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
2621 	END IF; */
2622 	l_sqltext :=  l_sqltext ||
2623 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
2624 	AND a.object_country = :l_country';
2625 	l_sqltext :=  l_sqltext ||
2626 	' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
2627 	AND cal.calendar_id=-1
2628 	GROUP BY p.id,p.value,p.leaf_node_flag
2629 	UNION ALL
2630 	SELECT /*+ORDERED*/
2631 	p.id VIEWBYID,
2632 	p.value  name,
2633 	p.leaf_node_flag leaf_node_flag,
2634 	0 targeted_audience,
2635 	0 responses_positive,
2636 	0 leads,
2637 	0 rank_a ,
2638 	0 new_opportunity_amt,
2639 	0 won_opportunity_amt,
2640 	0 orders_booked_amt,
2641 	0 orders_invoiced_amt,
2642 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
2643 	0 leads_converted,
2644 	0 prev_new_opportunity_amt
2645 	FROM  fii_time_rpt_struct_v cal
2646 		  ,BIM_I_OBJ_METS_MV a
2647 		  ,eni_denorm_hierarchies edh
2648 				,mtl_default_category_sets mdc
2649 				,(select e.id,e.value,e.leaf_node_flag
2650 				  from eni_item_vbh_nodes_v e
2651 			  where
2652 				  e.parent_id =:l_cat_id
2653 				  AND e.id = e.child_id
2657 	/* IF l_admin_status = 'N' THEN
2654 				  AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
2655 		  ) p ';
2656 
2658 	l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
2659 	END IF;*/
2660 
2661 	l_sqltext :=  l_sqltext ||
2662 	' WHERE a.time_id = cal.time_id
2663 	AND  a.period_type_id = cal.period_type_id
2664 	AND a.category_id = edh.child_id
2665 	AND edh.object_type = ''CATEGORY_SET''
2666 	AND edh.object_id = mdc.category_set_id
2667 	AND mdc.functional_area_id = 11
2668 	AND edh.dbi_flag = ''Y''
2669 	AND edh.parent_id = p.id
2670 	AND  a.source_code_id = :l_campaign_id ';
2671 
2672 	/*IF l_admin_status = 'N' THEN
2673 	l_sqltext := l_sqltext ||
2674 	'
2675 	AND a.source_code_id = ac.source_code_id
2676 	AND ac.resource_id = :l_resource_id';
2677 	ELSE
2678 	l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
2679 	END IF;*/
2680 
2681 	l_sqltext :=  l_sqltext ||
2682 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
2683 	AND a.object_country = :l_country';
2684 	l_sqltext :=  l_sqltext ||
2685 	' AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
2686 	AND cal.calendar_id=-1
2687 	GROUP BY p.id,p.value,p.leaf_node_flag
2688 	/*** directly assigned to the category *************/
2689 	UNION ALL
2690 	SELECT /*+ORDERED*/
2691 	p.id VIEWBYID,
2692 	bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||' name,
2693 	''Y'' leaf_node_flag,
2694 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
2695 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
2696 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
2697 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
2698 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
2699 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
2700 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
2701 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
2702 	0 prior_open,
2703 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
2704 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
2705 	FROM  fii_time_rpt_struct_v cal
2706 		  ,BIM_I_OBJ_METS_MV a
2707 		,(select e.id id,e.value value
2708 						  from eni_item_vbh_nodes_v e
2709 						  where e.parent_id =  :l_cat_id
2710 						  AND e.parent_id = e.child_id
2711 						  AND leaf_node_flag <> ''Y''
2712 						  ) p ';
2713 
2714 	/* IF l_admin_status = 'N' THEN
2715 	l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
2716 	END IF;*/
2717 
2718 	l_sqltext :=  l_sqltext ||
2719 	' WHERE a.time_id = cal.time_id
2720 	  AND  a.period_type_id = cal.period_type_id
2721 	  AND a.category_id = p.id
2722 	 AND  a.immediate_parent_id = :l_campaign_id ';
2723 	/*
2724 	IF l_admin_status = 'N' THEN
2725 	l_sqltext := l_sqltext ||
2726 	'
2727 	AND a.source_code_id = ac.source_code_id
2728 	AND ac.resource_id = :l_resource_id';
2729 	ELSE
2730 	l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
2731 	END IF;
2732 	*/
2733 	l_sqltext :=  l_sqltext ||
2734 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
2735 	  AND a.object_country = :l_country
2736 	  AND cal.report_date IN (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
2737 	  AND cal.calendar_id=-1
2738 	GROUP BY p.id
2739 	UNION ALL
2740 	SELECT /*+ORDERED*/
2741 	p.id VIEWBYID,
2742 	bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||'  name,
2743 	''Y'' leaf_node_flag,
2744 	0 targeted_audience,
2745 	0 responses_positive,
2746 	0 leads,
2747 	0 rank_a ,
2748 	0 new_opportunity_amt,
2749 	0 won_opportunity_amt,
2750 	0 orders_booked_amt,
2751 	0 orders_invoiced_amt,
2752 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
2753 	0 leads_converted,
2754 	0 prev_new_opportunity_amt
2755 	FROM  fii_time_rpt_struct_v cal
2756 		 ,BIM_I_OBJ_METS_MV a
2757 		,(select e.id id,e.value value
2758 						  from eni_item_vbh_nodes_v e
2759 						  where e.parent_id =  :l_cat_id
2760 						  AND e.parent_id = e.child_id
2761 						  AND leaf_node_flag <> ''Y''
2762 						  ) p ';
2763 	/*
2764 	IF l_admin_status = 'N' THEN
2765 	l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
2766 	END IF; */
2767 
2768 	l_sqltext :=  l_sqltext ||
2769 	' WHERE a.time_id = cal.time_id
2770 	  AND  a.period_type_id = cal.period_type_id
2771 	  AND  a.category_id = p.id
2772 	  AND  a.immediate_parent_id = :l_campaign_id ';
2773 
2774 	/* IF l_admin_status = 'N' THEN
2775 	l_sqltext := l_sqltext ||
2776 	'
2777 	AND a.source_code_id = ac.source_code_id
2778 	AND ac.resource_id = :l_resource_id';
2779 	ELSE
2780 	l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
2781 	END IF;
2782 	*/
2783 	l_sqltext :=  l_sqltext ||
2784 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
2785 	  AND a.object_country = :l_country
2789 	)GROUP BY name,VIEWBYID,leaf_node_flag
2786 	  AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
2787 	  AND cal.calendar_id=-1
2788 	GROUP BY p.id
2790 	)
2791 	)
2792 	WHERE
2793 	BIM_ATTRIBUTE3 > 0
2794 	OR BIM_ATTRIBUTE4 > 0
2795 	OR BIM_ATTRIBUTE5 > 0
2796 	OR BIM_ATTRIBUTE6 > 0
2797 	OR BIM_ATTRIBUTE7 > 0
2798 	OR BIM_ATTRIBUTE8 > 0
2799 	OR BIM_ATTRIBUTE9 > 0
2800 	OR BIM_ATTRIBUTE10 > 0
2801 	OR BIM_ATTRIBUTE11 > 0
2802 	&ORDER_BY_CLAUSE';
2803 	END IF;
2804 	/************** START OF VIEW BY IS MARKETING CHANNEL CAMPAIGN IS NOT NULL ***********/
2805 	ELSIF (l_view_by ='MEDIA+MEDIA') THEN
2806 	--l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
2807 	l_sqltext :=
2808 	'
2809 	SELECT
2810 	VIEWBY,
2811 	VIEWBYID,
2812 	BIM_ATTRIBUTE2,
2813 	BIM_ATTRIBUTE3,
2814 	BIM_ATTRIBUTE4,
2815 	BIM_ATTRIBUTE5,
2816 	BIM_ATTRIBUTE6,
2817 	BIM_ATTRIBUTE7,
2818 	BIM_ATTRIBUTE8,
2819 	BIM_ATTRIBUTE9,
2820 	BIM_ATTRIBUTE10,
2821 	BIM_ATTRIBUTE11,
2822 	BIM_ATTRIBUTE12,
2823 	BIM_ATTRIBUTE13,
2824 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE14,
2825 	BIM_ATTRIBUTE15,
2826 	null BIM_URL1,
2827 	null BIM_URL2,
2828 	null BIM_URL3,
2829 	null BIM_URL4,
2830 	BIM_GRAND_TOTAL1,
2831 	BIM_GRAND_TOTAL2,
2832 	BIM_GRAND_TOTAL3,
2833 	BIM_GRAND_TOTAL4,
2834 	BIM_GRAND_TOTAL5,
2835 	BIM_GRAND_TOTAL6,
2836 	BIM_GRAND_TOTAL7,
2837 	BIM_GRAND_TOTAL8,
2838 	BIM_GRAND_TOTAL9,
2839 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
2840 	FROM
2841 	(
2842 	SELECT name VIEWBY,
2843 	meaning BIM_ATTRIBUTE2,
2844 	targeted_audience BIM_ATTRIBUTE3,
2845 	responses_positive BIM_ATTRIBUTE4,
2846 	leads BIM_ATTRIBUTE5,
2847 	rank_a BIM_ATTRIBUTE6,
2848 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
2849 	new_opportunity_amt BIM_ATTRIBUTE8,
2850 	won_opportunity_amt BIM_ATTRIBUTE9,
2851 	orders_booked_amt BIM_ATTRIBUTE10,
2852 	orders_invoiced_amt BIM_ATTRIBUTE11,
2853 	null BIM_ATTRIBUTE12,
2854 	null BIM_ATTRIBUTE13,
2855 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE15,
2856 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
2857 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
2858 	sum(leads) over() BIM_GRAND_TOTAL3,
2859 	sum(rank_a) over() BIM_GRAND_TOTAL4,
2860 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
2861 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
2862 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
2863 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
2864 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
2865 	VIEWBYID
2866 	FROM
2867 	(
2868 	SELECT
2869 	null VIEWBYID,
2870 	name,
2871 	null meaning,
2872 	SUM(targeted_audience) targeted_audience,
2873 	SUM(responses_positive) responses_positive,
2874 	SUM(leads) leads,
2875 	SUM(rank_a) rank_a ,
2876 	SUM(new_opportunity_amt) new_opportunity_amt,
2877 	SUM(won_opportunity_amt) won_opportunity_amt,
2878 	SUM(orders_booked_amt) orders_booked_amt,
2879 	SUM(orders_invoiced_amt) orders_invoiced_amt,
2880 	SUM(prior_open) prior_open,
2881 	sum(leads_converted) leads_converted,
2882 	SUM(prev_new_opportunity_amt) prev_new_opportunity_amt
2883 	FROM
2884 	(
2885 	SELECT
2886 	decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
2887 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
2888 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
2889 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
2890 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
2891 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
2892 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
2893 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
2894 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
2895 	0 prior_open,
2896 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
2897 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt ';
2898 
2899 	/* IF l_admin_status = 'N' THEN
2900 	l_sqltext := l_sqltext ||' FROM bim_obj_chnl_mv a,bim_i_top_objects ac,  ';
2901 	ELSE
2902 	l_sqltext := l_sqltext ||' FROM bim_mkt_chnl_mv a, ';
2903 	END IF;*/
2904 
2905 	l_sqltext := l_sqltext ||
2906 	' FROM bim_obj_chnl_mv a,
2907 	  fii_time_rpt_struct_v cal,
2908 	  bim_dimv_media d ';
2909 
2910 	 IF l_cat_id is not null then
2911 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
2912 	  end if;
2913 
2914 	l_sqltext :=  l_sqltext ||
2915 	' WHERE a.time_id = cal.time_id
2919 	/* IF l_admin_status = 'N' THEN
2916 	  AND  a.period_type_id = cal.period_type_id
2917 	  AND  a.object_country = :l_country';
2918 
2920 	l_sqltext := l_sqltext ||
2921 	'
2922 	  AND a.source_code_id = ac.source_code_id
2923 	  AND ac.resource_id = :l_resource_id';
2924 	END IF;*/
2925 
2926 	IF l_cat_id is null then
2927 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
2928 	else
2929 	  l_sqltext := l_sqltext ||
2930 	 ' AND a.category_id = edh.child_id
2931 	   AND edh.object_type = ''CATEGORY_SET''
2932 	   AND edh.object_id = mdcs.category_set_id
2933 	   AND mdcs.functional_area_id = 11
2934 	   AND edh.dbi_flag = ''Y''
2935 	   AND edh.parent_id = :l_cat_id ';
2936 	  end if;
2937 
2938 	l_sqltext :=  l_sqltext ||
2939 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
2940 	  AND  d.id (+)= a.activity_id
2941 	  AND cal.report_date IN (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
2942 	  AND cal.calendar_id=-1
2943 	  AND  a.source_code_id = :l_campaign_id
2944 	GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
2945 	UNION ALL
2946 	SELECT
2947 	decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
2948 	0 targeted_audience,
2949 	0 responses_positive,
2950 	0 leads,
2951 	0 rank_a ,
2952 	0 new_opportunity_amt,
2953 	0 won_opportunity_amt,
2954 	0 orders_booked_amt,
2955 	0 orders_invoiced_amt,
2956 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
2957 	0 leads_converted,
2958 	0 prev_new_opportunity_amt ';
2959 
2960 	/*
2961 	IF l_admin_status = 'N' THEN
2962 	l_sqltext := l_sqltext ||' FROM bim_obj_chnl_mv a,bim_i_top_objects ac,  ';
2963 	ELSE
2964 	l_sqltext := l_sqltext ||' FROM bim_mkt_chnl_mv a, ';
2965 	END IF;*/
2966 
2967 	l_sqltext := l_sqltext ||
2968 	 ' FROM bim_obj_chnl_mv a,
2969 	   fii_time_rpt_struct_v cal,
2970 	   bim_dimv_media d';
2971 
2972 	 IF l_cat_id is not null then
2973 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
2974 	  end if;
2975 
2976 
2977 	l_sqltext :=  l_sqltext ||
2978 	' WHERE a.time_id = cal.time_id
2979 	  AND  a.period_type_id = cal.period_type_id
2980 	  AND  a.object_country = :l_country';
2981 
2982 	/*
2983 	IF l_admin_status = 'N' THEN
2984 	l_sqltext := l_sqltext ||
2985 	'
2986 	AND a.source_code_id = ac.source_code_id
2987 	AND ac.resource_id = :l_resource_id';
2988 	END IF;
2989 	*/
2990 
2991 	IF l_cat_id is null then
2992 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
2993 	else
2994 	  l_sqltext := l_sqltext ||
2995 	  ' AND a.category_id = edh.child_id
2996 		AND edh.object_type = ''CATEGORY_SET''
2997 		AND edh.object_id = mdcs.category_set_id
2998 		AND mdcs.functional_area_id = 11
2999 		AND edh.dbi_flag = ''Y''
3000 		AND edh.parent_id = :l_cat_id ';
3001 	  end if;
3002 	  l_sqltext :=  l_sqltext ||
3003 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
3004 	AND  d.id (+)= a.activity_id
3005 	AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
3006 	AND cal.calendar_id=-1
3007 	AND  a.source_code_id = :l_campaign_id
3008 	GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
3009 	)GROUP BY name
3010 	)
3011 	)
3012 	WHERE
3013 	BIM_ATTRIBUTE3 > 0
3014 	OR BIM_ATTRIBUTE4 > 0
3015 	OR BIM_ATTRIBUTE5 > 0
3016 	OR BIM_ATTRIBUTE6 > 0
3017 	OR BIM_ATTRIBUTE7 > 0
3018 	OR BIM_ATTRIBUTE8 > 0
3019 	OR BIM_ATTRIBUTE9 > 0
3020 	OR BIM_ATTRIBUTE10 > 0
3021 	OR BIM_ATTRIBUTE11 > 0
3022 	&ORDER_BY_CLAUSE';
3023 	ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
3024 	--l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
3025 	l_sqltext :=
3026 	'
3027 	SELECT
3028 	VIEWBY,
3029 	VIEWBYID,
3030 	BIM_ATTRIBUTE2,
3031 	BIM_ATTRIBUTE3,
3032 	BIM_ATTRIBUTE4,
3033 	BIM_ATTRIBUTE5,
3034 	BIM_ATTRIBUTE6,
3035 	BIM_ATTRIBUTE7,
3036 	BIM_ATTRIBUTE8,
3037 	BIM_ATTRIBUTE9,
3038 	BIM_ATTRIBUTE10,
3039 	BIM_ATTRIBUTE11,
3040 	BIM_ATTRIBUTE12,
3041 	BIM_ATTRIBUTE13,
3042 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE14,
3043 	BIM_ATTRIBUTE15,
3044 	null BIM_URL1,
3045 	null BIM_URL2,
3046 	null BIM_URL3,
3047 	null BIM_URL4,
3048 	BIM_GRAND_TOTAL1,
3049 	BIM_GRAND_TOTAL2,
3050 	BIM_GRAND_TOTAL3,
3051 	BIM_GRAND_TOTAL4,
3052 	BIM_GRAND_TOTAL5,
3053 	BIM_GRAND_TOTAL6,
3054 	BIM_GRAND_TOTAL7,
3055 	BIM_GRAND_TOTAL8,
3056 	BIM_GRAND_TOTAL9,
3057 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
3058 	FROM
3059 	(
3060 	SELECT name VIEWBY,
3061 	meaning BIM_ATTRIBUTE2,
3062 	targeted_audience BIM_ATTRIBUTE3,
3063 	responses_positive BIM_ATTRIBUTE4,
3064 	leads BIM_ATTRIBUTE5,
3065 	rank_a BIM_ATTRIBUTE6,
3069 	orders_booked_amt BIM_ATTRIBUTE10,
3066 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
3067 	new_opportunity_amt BIM_ATTRIBUTE8,
3068 	won_opportunity_amt BIM_ATTRIBUTE9,
3070 	orders_invoiced_amt BIM_ATTRIBUTE11,
3071 	null BIM_ATTRIBUTE12,
3072 	null BIM_ATTRIBUTE13,
3073 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE15,
3074 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
3075 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
3076 	sum(leads) over() BIM_GRAND_TOTAL3,
3077 	sum(rank_a) over() BIM_GRAND_TOTAL4,
3078 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
3079 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
3080 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
3081 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
3082 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
3083 	VIEWBYID
3084 	FROM
3085 	(
3086 	SELECT
3087 	null VIEWBYID,
3088 	name,
3089 	null meaning,
3090 	SUM(targeted_audience) targeted_audience,
3091 	SUM(responses_positive) responses_positive,
3092 	SUM(leads) leads,
3093 	SUM(rank_a) rank_a ,
3094 	SUM(new_opportunity_amt) new_opportunity_amt,
3095 	SUM(won_opportunity_amt) won_opportunity_amt,
3096 	SUM(orders_booked_amt) orders_booked_amt,
3097 	SUM(orders_invoiced_amt) orders_invoiced_amt,
3098 	SUM(prior_open) prior_open,
3099 	sum(leads_converted) leads_converted,
3100 	SUM(prev_new_opportunity_amt) prev_new_opportunity_amt
3101 	FROM
3102 	(
3103 	SELECT
3104 	decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
3105 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
3106 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
3107 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
3108 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
3109 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
3110 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
3111 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
3112 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
3113 	0 prior_open,
3114 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
3115 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt ';
3116 
3117 	/* IF l_admin_status = 'N' THEN
3118 	l_sqltext := l_sqltext ||' FROM bim_obj_regn_mv a,bim_i_top_objects ac,  ';
3119 	ELSE
3120 	l_sqltext := l_sqltext ||' FROM bim_mkt_regn_mv a, ';
3121 	END IF;*/
3122 
3123 	l_sqltext := l_sqltext ||
3124 	' FROM bim_obj_regn_mv a,
3125 	  fii_time_rpt_struct_v cal,
3126 	  bis_areas_v d ';
3127 
3128 	 IF l_cat_id is not null then
3129 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
3130 	  end if;
3131 
3132 	l_sqltext :=  l_sqltext ||
3133 	' WHERE a.time_id = cal.time_id
3134 	  AND  a.period_type_id = cal.period_type_id
3135 	  AND  a.object_country = :l_country';
3136 
3137 	/* IF l_admin_status = 'N' THEN
3138 	l_sqltext := l_sqltext ||
3139 	'
3140 	  AND a.source_code_id = ac.source_code_id
3141 	  AND ac.resource_id = :l_resource_id';
3142 	END IF;*/
3143 
3144 	IF l_cat_id is null then
3145 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
3146 	else
3147 	  l_sqltext := l_sqltext ||
3148 	 ' AND a.category_id = edh.child_id
3149 	   AND edh.object_type = ''CATEGORY_SET''
3150 	   AND edh.object_id = mdcs.category_set_id
3151 	   AND mdcs.functional_area_id = 11
3152 	   AND edh.dbi_flag = ''Y''
3153 	   AND edh.parent_id = :l_cat_id ';
3154 	  end if;
3155 
3156 	l_sqltext :=  l_sqltext ||
3157 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
3158 	  AND  d.id (+)= a.object_region
3159 	  AND cal.report_date IN (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
3160 	  AND cal.calendar_id=-1
3161 	  AND  a.source_code_id = :l_campaign_id
3162 	GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
3163 	UNION ALL
3164 	SELECT
3165 	decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
3166 	0 targeted_audience,
3167 	0 responses_positive,
3168 	0 leads,
3169 	0 rank_a ,
3170 	0 new_opportunity_amt,
3171 	0 won_opportunity_amt,
3172 	0 orders_booked_amt,
3173 	0 orders_invoiced_amt,
3174 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
3175 	0 leads_converted,
3176 	0 prev_new_opportunity_amt ';
3177 
3178 	/*
3179 	IF l_admin_status = 'N' THEN
3180 	l_sqltext := l_sqltext ||' FROM bim_obj_regn_mv a,bim_i_top_objects ac,  ';
3181 	ELSE
3182 	l_sqltext := l_sqltext ||' FROM bim_mkt_regn_mv a, ';
3183 	END IF;*/
3184 
3185 	l_sqltext := l_sqltext ||
3186 	 ' FROM bim_obj_regn_mv a,
3190 	 IF l_cat_id is not null then
3187 	   fii_time_rpt_struct_v cal,
3188 	   bis_areas_v d';
3189 
3191 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
3192 	  end if;
3193 
3194 
3195 	l_sqltext :=  l_sqltext ||
3196 	' WHERE a.time_id = cal.time_id
3197 	  AND  a.period_type_id = cal.period_type_id
3198 	  AND  a.object_country = :l_country';
3199 
3200 	/*
3201 	IF l_admin_status = 'N' THEN
3202 	l_sqltext := l_sqltext ||
3203 	'
3204 	AND a.source_code_id = ac.source_code_id
3205 	AND ac.resource_id = :l_resource_id';
3206 	END IF;
3207 	*/
3208 
3209 	IF l_cat_id is null then
3210 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
3211 	else
3212 	  l_sqltext := l_sqltext ||
3213 	  ' AND a.category_id = edh.child_id
3214 		AND edh.object_type = ''CATEGORY_SET''
3215 		AND edh.object_id = mdcs.category_set_id
3216 		AND mdcs.functional_area_id = 11
3217 		AND edh.dbi_flag = ''Y''
3218 		AND edh.parent_id = :l_cat_id ';
3219 	  end if;
3220 	  l_sqltext :=  l_sqltext ||
3221 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
3222 	AND  d.id (+)= a.object_region
3223 	AND  cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
3224 	AND  cal.calendar_id=-1
3225 	AND  a.source_code_id = :l_campaign_id
3226 	GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
3227 	)GROUP BY name
3228 	)
3229 	)
3230 	WHERE
3231 	BIM_ATTRIBUTE3 > 0
3232 	OR BIM_ATTRIBUTE4 > 0
3233 	OR BIM_ATTRIBUTE5 > 0
3234 	OR BIM_ATTRIBUTE6 > 0
3235 	OR BIM_ATTRIBUTE7 > 0
3236 	OR BIM_ATTRIBUTE8 > 0
3237 	OR BIM_ATTRIBUTE9 > 0
3238 	OR BIM_ATTRIBUTE10 > 0
3239 	OR BIM_ATTRIBUTE11 > 0
3240 	&ORDER_BY_CLAUSE';
3241 	ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
3242 	--l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
3243 	l_sqltext :=
3244 	'
3245 	SELECT
3246 	VIEWBY,
3247 	VIEWBYID,
3248 	BIM_ATTRIBUTE2,
3249 	BIM_ATTRIBUTE3,
3250 	BIM_ATTRIBUTE4,
3251 	BIM_ATTRIBUTE5,
3252 	BIM_ATTRIBUTE6,
3253 	BIM_ATTRIBUTE7,
3254 	BIM_ATTRIBUTE8,
3255 	BIM_ATTRIBUTE9,
3256 	BIM_ATTRIBUTE10,
3257 	BIM_ATTRIBUTE11,
3258 	BIM_ATTRIBUTE12,
3259 	BIM_ATTRIBUTE13,
3260 	BIM_ATTRIBUTE8 BIM_ATTRIBUTE14,
3261 	BIM_ATTRIBUTE15,
3262 	null BIM_URL1,
3263 	null BIM_URL2,
3264 	null BIM_URL3,
3265 	null BIM_URL4,
3266 	BIM_GRAND_TOTAL1,
3267 	BIM_GRAND_TOTAL2,
3268 	BIM_GRAND_TOTAL3,
3269 	BIM_GRAND_TOTAL4,
3270 	BIM_GRAND_TOTAL5,
3271 	BIM_GRAND_TOTAL6,
3272 	BIM_GRAND_TOTAL7,
3273 	BIM_GRAND_TOTAL8,
3274 	BIM_GRAND_TOTAL9,
3275 	BIM_GRAND_TOTAL6 BIM_GRAND_TOTAL10
3276 	FROM
3277 	(
3278 	SELECT name VIEWBY,
3279 	meaning BIM_ATTRIBUTE2,
3280 	targeted_audience BIM_ATTRIBUTE3,
3281 	responses_positive BIM_ATTRIBUTE4,
3282 	leads BIM_ATTRIBUTE5,
3283 	rank_a BIM_ATTRIBUTE6,
3284 	decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE7,
3285 	new_opportunity_amt BIM_ATTRIBUTE8,
3286 	won_opportunity_amt BIM_ATTRIBUTE9,
3287 	orders_booked_amt BIM_ATTRIBUTE10,
3288 	orders_invoiced_amt BIM_ATTRIBUTE11,
3289 	null BIM_ATTRIBUTE12,
3290 	null BIM_ATTRIBUTE13,
3291 	DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE15,
3292 	sum(targeted_audience) over() BIM_GRAND_TOTAL1,
3293 	sum(responses_positive) over() BIM_GRAND_TOTAL2,
3294 	sum(leads) over() BIM_GRAND_TOTAL3,
3295 	sum(rank_a) over() BIM_GRAND_TOTAL4,
3296 	decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL5,
3297 	sum(new_opportunity_amt) over() BIM_GRAND_TOTAL6,
3298 	sum(won_opportunity_amt) over() BIM_GRAND_TOTAL7,
3299 	sum(orders_booked_amt) over() BIM_GRAND_TOTAL8,
3300 	sum(orders_invoiced_amt) over() BIM_GRAND_TOTAL9,
3301 	VIEWBYID
3302 	FROM
3303 	(
3304 	SELECT
3305 	VIEWBYID,
3306 	name,
3307 	null meaning,
3308 	SUM(targeted_audience) targeted_audience,
3309 	SUM(responses_positive) responses_positive,
3310 	SUM(leads) leads,
3311 	SUM(rank_a) rank_a ,
3312 	SUM(new_opportunity_amt) new_opportunity_amt,
3313 	SUM(won_opportunity_amt) won_opportunity_amt,
3314 	SUM(orders_booked_amt) orders_booked_amt,
3315 	SUM(orders_invoiced_amt) orders_invoiced_amt,
3316 	SUM(prior_open) prior_open,
3317 	sum(leads_converted) leads_converted,
3318 	SUM(prev_new_opportunity_amt) prev_new_opportunity_amt
3319 	FROM
3320 	(
3321 	SELECT
3322 	decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
3323 	a.object_country viewbyid,
3324 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.customers_targeted,0)) targeted_audience,
3325 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.responses_positive,0)) responses_positive,
3326 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0)) leads,
3327 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0)) rank_a ,
3328 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) new_opportunity_amt,
3332 	0 prior_open,
3329 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,won_opportunity_amt'||l_curr_suffix||',0)) won_opportunity_amt,
3330 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_booked_amt'||l_curr_suffix||',0)) orders_booked_amt,
3331 	SUM(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.orders_invoiced_amt'||l_curr_suffix||',0)) orders_invoiced_amt,
3333 	sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted,
3334 	SUM(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt
3335 	FROM BIM_I_OBJ_METS_MV a,
3336 		 fii_time_rpt_struct_v cal,
3337 		 fnd_territories_tl d ';
3338 	 IF l_cat_id is not null then
3339 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
3340 	  end if;
3341 
3342 	/* IF l_admin_status = 'N' THEN
3343 	l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
3344 	END IF;
3345 	*/
3346 
3347 	l_sqltext :=  l_sqltext ||
3348 	' WHERE a.time_id = cal.time_id
3349 	  AND  a.period_type_id = cal.period_type_id';
3350 	/*
3351 	IF l_admin_status = 'N' THEN
3352 	l_sqltext := l_sqltext ||
3353 	'
3354 	AND a.source_code_id = ac.source_code_id
3355 	AND ac.resource_id = :l_resource_id';
3356 	ELSE
3357 	l_sqltext := l_sqltext ||
3358 	' AND  a.parent_object_id is null ';
3359 	END IF;
3360 	*/
3361 	IF l_cat_id is null then
3362 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
3363 	else
3364 	  l_sqltext := l_sqltext ||
3365 	  ' AND a.category_id = edh.child_id
3366 	   AND edh.object_type = ''CATEGORY_SET''
3367 	   AND edh.object_id = mdcs.category_set_id
3368 	   AND mdcs.functional_area_id = 11
3369 	   AND edh.dbi_flag = ''Y''
3370 	   AND edh.parent_id = :l_cat_id ';
3371 	  end if;
3372 	  if l_country <>'N' then
3373 	  l_sqltext :=  l_sqltext || ' AND a.object_country = :l_country ';
3374 	  else
3375 	  l_sqltext :=  l_sqltext || ' AND a.object_country <>''N'' ';
3376 	  end if;
3377 	l_sqltext :=  l_sqltext ||
3378 	' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
3379 	AND  a.object_country =d.territory_code(+)
3380 	AND d.language(+) = userenv(''LANG'')
3381 	AND  cal.report_date IN (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
3382 	AND  cal.calendar_id=-1
3383 	AND  a.source_code_id = :l_campaign_id
3384 	GROUP BY decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country
3385 	UNION ALL
3386 	SELECT
3387 	decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
3388 	a.object_country viewbyid,
3389 	0 targeted_audience,
3390 	0 responses_positive,
3391 	0 leads,
3392 	0 rank_a ,
3393 	0 new_opportunity_amt,
3394 	0 won_opportunity_amt,
3395 	0 orders_booked_amt,
3396 	0 orders_invoiced_amt,
3397 	sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted)) prior_open,
3398 	0 leads_converted,
3399 	0 prev_new_opportunity_amt
3400 	FROM BIM_I_OBJ_METS_MV a,
3401 		 fii_time_rpt_struct_v cal,
3402 		 fnd_territories_tl d ';
3403 	 IF l_cat_id is not null then
3404 	  l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
3405 	  end if;
3406 	/*
3407 	IF l_admin_status = 'N' THEN
3408 	l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
3409 	END IF;
3410 	*/
3411 	l_sqltext :=  l_sqltext ||
3412 	' WHERE a.time_id = cal.time_id
3413 	  AND  a.period_type_id = cal.period_type_id';
3414 
3415 	/* IF l_admin_status = 'N' THEN
3416 	l_sqltext := l_sqltext ||
3417 	'
3418 	AND a.source_code_id = ac.source_code_id
3419 	AND ac.resource_id = :l_resource_id';
3420 	ELSE
3421 	l_sqltext := l_sqltext ||
3422 	' AND  a.parent_object_id is null ';
3423 	END IF;*/
3424 	IF l_cat_id is null then
3425 	l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
3426 	else
3427 	  l_sqltext := l_sqltext ||
3428 	  ' AND a.category_id = edh.child_id
3429 	  AND edh.object_type = ''CATEGORY_SET''
3430 	  AND edh.object_id = mdcs.category_set_id
3431 	  AND mdcs.functional_area_id = 11
3432 	  AND edh.dbi_flag = ''Y''
3433 	  AND edh.parent_id = :l_cat_id ';
3434 	  end if;
3435 	  if l_country <>'N' then
3436 	  l_sqltext :=  l_sqltext || ' AND a.object_country = :l_country ';
3437 	  else
3438 	  l_sqltext :=  l_sqltext || ' AND a.object_country <>''N'' ';
3439 	  end if;
3440 	l_sqltext :=  l_sqltext ||
3441 	' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
3442 	  AND  a.object_country =d.territory_code(+)
3443 	  AND d.language(+) = userenv(''LANG'')
3444 	  AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
3445 	  AND cal.calendar_id=-1
3446 	  AND  a.source_code_id = :l_campaign_id
3447 	GROUP BY decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country
3448 	)GROUP BY name,viewbyid
3449 	)
3450 	)
3451 	WHERE
3452 	BIM_ATTRIBUTE3 > 0
3453 	OR BIM_ATTRIBUTE4 > 0
3454 	OR BIM_ATTRIBUTE5 > 0
3455 	OR BIM_ATTRIBUTE6 > 0
3456 	OR BIM_ATTRIBUTE7 > 0
3457 	OR BIM_ATTRIBUTE8 > 0
3458 	OR BIM_ATTRIBUTE9 > 0
3459 	OR BIM_ATTRIBUTE10 > 0
3460 	OR BIM_ATTRIBUTE11 > 0
3461 	&ORDER_BY_CLAUSE';
3462 	END IF;
3463 END IF;
3464   x_custom_sql := l_sqltext;
3468   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
3465   l_custom_rec.attribute_name := ':l_record_type';
3466   l_custom_rec.attribute_value := l_record_type_id;
3467   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
3469   x_custom_output.EXTEND;
3470   x_custom_output(1) := l_custom_rec;
3471 
3472   l_custom_rec.attribute_name := ':l_resource_id';
3473   l_custom_rec.attribute_value := GET_RESOURCE_ID;
3474   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
3475   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
3476   x_custom_output.EXTEND;
3477   x_custom_output(2) := l_custom_rec;
3478 
3479   l_custom_rec.attribute_name := ':l_admin_flag';
3480   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
3481   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
3482   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
3483   x_custom_output.EXTEND;
3484   x_custom_output(3) := l_custom_rec;
3485 
3486   l_custom_rec.attribute_name := ':l_country';
3487   l_custom_rec.attribute_value := l_country;
3488   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
3489   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
3490   x_custom_output.EXTEND;
3491   x_custom_output(4) := l_custom_rec;
3492 
3493   l_custom_rec.attribute_name := ':l_cat_id';
3494   l_custom_rec.attribute_value := l_cat_id;
3495   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
3496   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
3497   x_custom_output.EXTEND;
3498   x_custom_output(5) := l_custom_rec;
3499 
3500   l_custom_rec.attribute_name := ':l_campaign_id';
3501   l_custom_rec.attribute_value := l_campaign_id;
3502   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
3503   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
3504   x_custom_output.EXTEND;
3505   x_custom_output(6) := l_custom_rec;
3506 
3507 
3508 
3509 write_debug('GET_PO_RACK_SQL','QUERY','_',l_sqltext);
3510 --return l_sqltext;
3511 
3512 EXCEPTION
3513 WHEN others THEN
3514 l_sql_errm := SQLERRM;
3515 write_debug('GET_PO_RACK_SQL','ERROR',l_sql_errm,l_sqltext);
3516 END GET_PO_RACK_SQL;
3517 
3518 PROCEDURE GET_CS_RACK_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
3519                           x_custom_sql  OUT NOCOPY VARCHAR2,
3520                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
3521 
3522 			  IS
3523 
3524 l_sqltext varchar2(15000);
3525 iFlag number;
3526 l_period_type_hc number;
3527 l_as_of_date  DATE;
3528 l_period_type	varchar2(2000);
3529 l_record_type_id NUMBER;
3530 l_comp_type    varchar2(2000);
3531 l_country      varchar2(4000);
3532 l_view_by      varchar2(4000);
3533 l_sql_errm      varchar2(5000);
3534 l_previous_report_start_date DATE;
3535 l_current_report_start_date DATE;
3536 l_previous_as_of_date DATE;
3537 l_period_type_id NUMBER;
3538 l_user_id NUMBER;
3539 l_resource_id NUMBER;
3540 l_time_id_column  VARCHAR2(1000);
3541 l_admin_status VARCHAR2(20);
3542 l_admin_flag VARCHAR2(1);
3543 l_admin_count Number;
3544 l_rsid NUMBER;
3545 l_prev_aod_str varchar2(80);
3546 l_curr_aod_str varchar2(80);
3547 l_country_clause varchar2(4000);
3548 l_access_table varchar2(4000);
3549 l_access_clause varchar2(4000);
3550 l_object_type varchar2(30);
3551 l_url_str varchar2(1000);
3552 l_url_str_csch varchar2(1000);
3553 l_url_str_csch_jtf varchar2(3000);
3554 l_url_str_type varchar2(1000);
3555 --l_cat_id NUMBER;
3556 l_cat_id VARCHAR2(50);
3557 l_campaign_id VARCHAR2(50);
3558 l_custom_rec BIS_QUERY_ATTRIBUTES;
3559 l_inner_sqltext varchar2(20000); --- to form inner query to calculate total cost
3560 l_prog_cost1  varchar2(100);
3561 l_prog_cost2  varchar2(100);
3562 l_prog_rev1  varchar2(100);
3563 l_prog_rev2  varchar2(100);
3564 l_csch_chnl  varchar2(100);
3565 l_chnl_select  varchar2(1000);
3566 l_chnl_from  varchar2(1000);
3567 l_chnl_where  varchar2(1000);
3568 l_chnl_group  varchar2(1000);
3569 l_chnl_col   varchar2(10);
3570 l_col1 varchar2(100); ---- to append product category id
3571 l_col2 varchar2(100); ---- to append product category id
3572 l_inner      varchar2(2000);
3573 l_curr VARCHAR2(50);
3574 l_curr_suffix VARCHAR2(50);
3575 l_curr_suffix1 VARCHAR2(50);
3576 l_table_bud VARCHAR2(300);
3577 l_where_bud VARCHAR2(300);
3578 
3579 l_dass       varchar2(100);  -- variable to store value for  directly assigned lookup value
3580 --l_una       varchar2(100);   -- variable to store value for  Unassigned lookup value
3581 l_col_id NUMBER;
3582 l_area VARCHAR2(300);
3583 l_report_name VARCHAR2(300);
3584 l_media VARCHAR2(300);
3585 
3586 /* cursor to get type of object passed from the page ******/
3587     cursor get_obj_type
3588     is
3592 
3589     select object_type
3590     from bim_i_source_codes
3591     where source_code_id=replace(l_campaign_id,'''');
3593 BEGIN
3594 
3595 
3596 
3597 	x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
3598 	l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
3599 	bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
3600 											l_as_of_date,
3601 											l_period_type,
3602 											l_record_type_id,
3603 											l_comp_type,
3604 											l_country,
3605 											l_view_by,
3606 											l_cat_id,
3607 											l_campaign_id,
3608 											l_curr,
3609 											l_col_id,
3610 											l_area,
3611 											l_media,
3612 											l_report_name
3613 										  );
3614 	IF (l_curr = '''FII_GLOBAL1''')	THEN
3615 
3616 		l_curr_suffix := '';
3617 
3618 	ELSIF (l_curr = '''FII_GLOBAL2''')	THEN
3619 
3620 		l_curr_suffix := '_s';
3621 
3622 	ELSE
3623 		l_curr_suffix := '';
3624 
3625 	END IF;
3626    l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
3627    IF l_country IS NULL THEN
3628       l_country := 'N';
3629    END IF;
3630 
3631    if l_cat_id is not null then
3632    l_col1:='a.category_id,';
3633    l_col2:=',a.category_id';
3634    end if;
3635 
3636 /**************************************************************/
3637    if  l_prog_cost ='BIM_APPROVED_BUDGET' then
3638         l_prog_cost1:='budget_approved';
3639         l_prog_cost2:='budget_approved';
3640    else
3641        l_prog_cost1:='actual_cost';
3642        l_prog_cost2:='cost_actual';
3643        -- column name is diffrent in mv
3644    end if;
3645 
3646    IF l_revenue = 'BOOKED_AMT' THEN
3647 
3648     l_prog_rev1 :='  booked_amt';
3649     l_prog_rev2 :=' orders_booked_amt';
3650 
3651 
3652    ELSIF l_revenue = 'INVOICED_AMT'   THEN
3653 
3654     l_prog_rev1  :=' invoiced_amt';
3655     l_prog_rev2 :=' orders_invoiced_amt';
3656 
3657 
3658    ELSIF l_revenue = 'WON_OPPR_AMT' THEN
3659 
3660     l_prog_rev1 :=' won_opportunity_amt';
3661     l_prog_rev2 :='won_opportunity_amt';
3662 
3663    END IF;
3664 /****************************************************************/
3665 
3666 
3667    l_admin_status := GET_ADMIN_STATUS;
3668     --l_admin_status := 'Y';
3669    /*IF l_admin_status = 'N' THEN
3670       l_access_table := ',AMS_ACT_ACCESS_DENORM ac ';
3671       l_access_clause := ' '||
3672          'AND a.source_code_id = ac.source_code_id AND ac.resource_id = '||GET_RESOURCE_ID;
3673    ELSE
3674       l_access_table := '';
3675       l_access_clause := '';
3676    END IF;*/
3677 
3678 
3679 l_url_str :='pFunctionName=BIM_I_CSRR_PHD_PHP&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
3683 l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
3680 --l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objId=';
3681 l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
3682 l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
3684 &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
3685 
3686 if(l_campaign_id is null) then
3687 
3688 /********* campaign is null  no drill down in campaign hirerachy*************/
3689 
3690 if (l_view_by = 'CAMPAIGN+CAMPAIGN') then
3691 /******************** view by is campaign *****************/
3692  l_sqltext := '
3693  SELECT name VIEWBY,
3694  VIEWBYID,
3695  meaning BIM_ATTRIBUTE2,
3696  cost_actual BIM_ATTRIBUTE3,
3697  actual_revenue BIM_ATTRIBUTE4,
3698  total_actual_cost BIM_ATTRIBUTE5,
3699  total_actual_revenue BIM_ATTRIBUTE6,
3700  total_roi BIM_ATTRIBUTE7,
3701  cost_forecasted BIM_ATTRIBUTE8,
3702  revenue_forecasted BIM_ATTRIBUTE9,
3703  forecast_roi BIM_ATTRIBUTE10,
3704  object_id BIM_ATTRIBUTE11,
3705  object_type  BIM_ATTRIBUTE15,
3706  cost_variance BIM_ATTRIBUTE16,
3707  rev_variance  BIM_ATTRIBUTE17,
3708  roi_variance     BIM_ATTRIBUTE18,
3709  null bim_url1,
3710  decode(object_type,''EONE'',NULL,'||''''||l_url_str||''''||' ) BIM_URL2,
3711  NULL BIM_URL3,
3712  sum(cost_actual) over() BIM_GRAND_TOTAL1,
3713  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
3714  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
3715  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
3716 case
3717 when sum(total_actual_cost) over()=0
3718 then null
3719 else
3720 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5,
3721 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
3722 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
3723 case
3724 when sum(cost_forecasted) over()=0
3725 then null
3726 else
3727 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8,
3728 case when sum(cost_forecasted) over()=0
3729 then null
3730 else
3731 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9,
3732 case
3733 when sum(revenue_forecasted) over()=0
3734 then null
3735 else
3736 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
3737 case when sum(cost_forecasted) over() =0 then null
3738      when sum(total_actual_cost) over() =0 then null
3739      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
3740      else
3741     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
3745 FROM
3742         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
3743       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
3744    end BIM_GRAND_TOTAL11
3746 (
3747 SELECT
3748 object_id,
3749 object_type,
3750 VIEWBYID,
3751 name,
3752 meaning,
3753 sum(cost_actual) cost_actual,
3754 SUM(cost_forecasted) cost_forecasted,
3755 SUM(actual_revenue) actual_revenue ,
3756 SUM(revenue_forecasted) revenue_forecasted,
3757 SUM(total_actual_cost) total_actual_cost,
3758 SUM(total_actual_revenue) total_actual_revenue,
3759 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
3760 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
3761 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
3762 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
3763 case when sum(cost_forecasted) =0 then null
3764      when sum(total_actual_cost) =0 then null
3765      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
3766      else
3767     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
3768         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
3769       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
3770    end roi_variance
3771 FROM
3772 (
3773 SELECT /*+NO_MERGE(camp)*/ campname.object_id object_id,campname.object_type object_type,camp.viewbyid,campname.name name,l.meaning meaning,camp.cost_actual,camp.cost_forecasted,camp.actual_revenue ,camp.revenue_forecasted,
3774 camp.total_actual_cost,camp.total_actual_revenue
3775 FROM
3776 (
3777 SELECT
3778 a.source_code_id VIEWBYID,
3779 SUM('||l_prog_cost1||l_curr_suffix||') cost_actual,
3780 0 cost_forecasted,
3781 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
3782 0 revenue_forecasted,
3783 0 total_actual_cost,
3784 0 total_actual_revenue
3785 FROM BIM_I_CPB_METS_MV a,fii_time_rpt_struct_v cal';
3786 IF l_cat_id is not null then
3787   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
3788   end if;
3789 
3790 IF l_admin_status = 'N' THEN
3791 -- IF l_prog_view='Y' then
3792     l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
3793  /*ELSE
3794     l_sqltext := l_sqltext ||',ams_act_access_denorm ac  ';
3795  END IF;*/
3796 END IF;
3797 
3798 l_sqltext :=  l_sqltext ||
3799 ' WHERE a.time_id = cal.time_id
3800 AND  a.period_type_id = cal.period_type_id';
3801 
3802 
3803 IF l_admin_status = 'N' THEN
3804 l_sqltext := l_sqltext ||
3805 '
3806   AND a.source_code_id = ac.source_code_id
3807   AND ac.resource_id = :l_resource_id';
3808 ELSE
3809  -- IF l_prog_view='Y' then
3810       l_sqltext := l_sqltext ||
3811      ' AND  a.immediate_parent_id is null ';
3812   -- END IF;
3813 END IF;
3814 
3815 IF l_cat_id is null then
3816 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
3817 else
3818   l_sqltext := l_sqltext ||
3822    AND mdcs.functional_area_id = 11
3819    ' AND a.category_id = edh.child_id
3820    AND edh.object_type = ''CATEGORY_SET''
3821    AND edh.object_id = mdcs.category_set_id
3823    AND edh.dbi_flag = ''Y''
3824    AND edh.parent_id = :l_cat_id ';
3825 end if;
3826 
3827 l_sqltext :=  l_sqltext ||
3828 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
3829 AND a.object_country =:l_country ';
3830 l_sqltext := l_sqltext||'
3831  AND cal.report_date = &BIS_CURRENT_ASOF_DATE
3832 AND cal.calendar_id=-1
3833 AND ( '||l_prog_cost1||' >0 OR '||l_prog_rev1||' > 0 )
3834 GROUP BY a.source_code_id
3835 UNION ALL
3836  SELECT
3837 a.source_code_id VIEWBYID,
3838 0 cost_actual,
3839 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
3840 0 actual_revenue ,
3841 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
3842 sum('||l_prog_cost2||l_curr_suffix||') total_actual_cost,
3843 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue
3844 FROM BIM_I_OBJ_METS_MV a,
3845     fii_time_rpt_struct_v cal';
3846 
3847 IF l_cat_id is not null then
3848   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
3849   end if;
3850 
3851 IF l_admin_status = 'N' THEN
3852  --IF l_prog_view='Y' then
3853     l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
3854  /*ELSE
3855     l_sqltext := l_sqltext ||',ams_act_access_denorm ac  ';
3856  END IF; */
3857 END IF;
3858 
3859 l_sqltext :=  l_sqltext ||
3860 ' WHERE a.time_id = cal.time_id
3861 AND  a.period_type_id = cal.period_type_id ';
3862 
3863 /*if (l_prog_view = 'Y') then
3864 l_sqltext := l_sqltext ||
3865 ' AND a.object_type in (''CAMP'',''RCAM'')';
3866 ELSE
3867 l_sqltext := l_sqltext ||
3868 ' AND a.object_type =''CAMP''';
3869 END IF;*/
3870 
3871 IF l_admin_status = 'N' THEN
3872 l_sqltext := l_sqltext ||
3873 '
3874   AND a.source_code_id = ac.source_code_id
3875   AND ac.resource_id = :l_resource_id';
3876 ELSE
3877   IF l_prog_view='Y' then
3878       l_sqltext := l_sqltext ||
3879      ' AND  a.immediate_parent_id is null ';
3880    END IF;
3881 END IF;
3882 
3883 IF l_cat_id is null then
3884 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
3885 else
3886   l_sqltext := l_sqltext ||
3887    ' AND a.category_id = edh.child_id
3888    AND edh.object_type = ''CATEGORY_SET''
3889    AND edh.object_id = mdcs.category_set_id
3890    AND mdcs.functional_area_id = 11
3891    AND edh.dbi_flag = ''Y''
3892    AND edh.parent_id = :l_cat_id ';
3893 end if;
3894 
3895 l_sqltext :=  l_sqltext ||
3896 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
3897 AND a.object_country =:l_country ';
3898 l_sqltext := l_sqltext||'
3899  AND cal.report_date = trunc(sysdate)
3900 AND cal.calendar_id=-1
3901 AND (cost_forecasted > 0 OR revenue_forecasted >0 OR '||l_prog_cost2||' >0 OR '||l_prog_rev2||' > 0 )
3902 GROUP BY a.source_code_id)camp,bim_i_obj_name_mv campname,ams_lookups l
3903 WHERE campname.source_code_id = camp.viewbyid
3904 AND campname.language =USERENV(''LANG'')
3905 AND l.lookup_code = campname.object_type
3906 AND l.lookup_type = ''AMS_SYS_ARC_QUALIFIER''
3907 )GROUP BY VIEWBYID,object_id,object_type,name,meaning
3908 HAVING
3909 sum(cost_actual) <> 0
3910 OR sum(actual_revenue) <> 0
3911 )&ORDER_BY_CLAUSE
3912 ';
3913 elsif (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
3914 if l_cat_id is null then
3915 /************** START OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NULL ***********/
3916 /********* building inline view to filter thosedo not have any ptd cost or ptd revenue ***********/
3917 
3918 l_inner:=   ',( SELECT DISTINCT a.source_code_id
3919                 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a';
3920 
3921 IF l_admin_status = 'N' THEN
3922 l_inner := l_inner ||',bim_i_top_objects ac ';
3923 END IF;
3924 
3925 l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
3926               AND a.period_type_id=cal.period_type_id
3927               AND cal.calendar_id=-1
3928               AND cal.report_date =&BIS_CURRENT_ASOF_DATE
3929 	      AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
3930 	      AND a.object_country = :l_country
3931 	      AND a.category_id=-9 ';
3932 
3933 IF l_admin_status = 'N' THEN
3934 l_inner := l_inner ||
3935 '
3936 AND a.source_code_id = ac.source_code_id
3937 AND ac.resource_id = :l_resource_id';
3938 ELSE
3939 l_inner := l_inner ||' AND a.immediate_parent_id is null';
3940 END IF;
3941 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr ';
3942 
3943 l_sqltext :=
3944 '
3945  SELECT
3946  VIEWBY,
3947  VIEWBYID,
3948  null BIM_ATTRIBUTE2,
3949  cost_actual BIM_ATTRIBUTE3,
3953  total_roi BIM_ATTRIBUTE7,
3950  actual_revenue BIM_ATTRIBUTE4,
3951  total_actual_cost BIM_ATTRIBUTE5,
3952  total_actual_revenue BIM_ATTRIBUTE6,
3954  cost_forecasted BIM_ATTRIBUTE8,
3955  revenue_forecasted BIM_ATTRIBUTE9,
3956  forecast_roi BIM_ATTRIBUTE10,
3957  null BIM_ATTRIBUTE11,
3958  null BIM_ATTRIBUTE15,
3959  cost_variance BIM_ATTRIBUTE16,
3960  rev_variance  BIM_ATTRIBUTE17,
3961  roi_variance     BIM_ATTRIBUTE18,
3962  decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) BIM_URL1,
3963  NULL BIM_URL2,
3964  NULL BIM_URL3,
3965  sum(cost_actual) over() BIM_GRAND_TOTAL1,
3966  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
3967  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
3968  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
3969 case
3970 when sum(total_actual_cost) over()=0
3971 then null
3972 else
3973 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5,
3974 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
3975 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
3976 case
3977 when sum(cost_forecasted) over()=0
3978 then null
3979 else
3980 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8,
3981 case when sum(cost_forecasted) over()=0
3982 then null
3983 else
3984 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9,
3985 case
3986 when sum(revenue_forecasted) over()=0
3987 then null
3988 else
3989 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
3990 case when sum(cost_forecasted) over() =0 then null
3991      when sum(total_actual_cost) over() =0 then null
3992      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
3993      else
3994     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
3995         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
3996       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
3997    end BIM_GRAND_TOTAL11
3998 FROM
3999 (
4000 SELECT
4001 VIEWBYID,
4002 viewby,
4003 leaf_node_flag,
4004 sum(cost_actual) cost_actual,
4005 SUM(cost_forecasted) cost_forecasted,
4006 SUM(actual_revenue) actual_revenue ,
4007 SUM(revenue_forecasted) revenue_forecasted,
4008 SUM(total_actual_cost) total_actual_cost,
4009 SUM(total_actual_revenue) total_actual_revenue,
4010 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
4011 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
4012 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
4013 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
4014 case when sum(cost_forecasted) =0 then null
4015      when sum(total_actual_cost) =0 then null
4016      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
4017      else
4018     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
4019         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
4020       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
4021    end roi_variance
4022 FROM
4023 ( SELECT  /*+ORDERED*/
4024 p.value viewby,
4025 p.parent_id VIEWBYID,
4026 p.leaf_node_flag leaf_node_flag,
4027 sum('|| l_prog_cost1 ||l_curr_suffix||')  cost_actual,
4028 0 cost_forecasted,
4029 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
4030 0 revenue_forecasted,
4031 0 total_actual_cost,
4032 0 total_actual_revenue
4033 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a
4034 ,eni_denorm_hierarchies edh
4035                 ,mtl_default_category_sets mdcs
4036                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag
4037                    FROM eni_item_vbh_nodes_v e
4038                    WHERE e.top_node_flag=''Y''
4039                    AND e.child_id = e.parent_id) p ';
4040 
4041 IF l_admin_status = 'N' THEN
4042 l_sqltext := l_sqltext ||',bim_i_top_objects ac ';
4043 END IF;
4044 
4045 l_sqltext :=  l_sqltext ||
4046 ' WHERE a.time_id = cal.time_id
4047 AND  a.period_type_id = cal.period_type_id
4048 AND a.category_id = edh.child_id
4049 AND edh.object_type = ''CATEGORY_SET''
4050 AND edh.object_id = mdcs.category_set_id
4051 AND mdcs.functional_area_id = 11
4052 AND edh.dbi_flag = ''Y''
4053 AND edh.parent_id = p.parent_id';
4054 
4055 
4056 IF l_admin_status = 'N' THEN
4057 l_sqltext := l_sqltext ||
4058 '
4059 AND a.source_code_id = ac.source_code_id
4060 AND ac.resource_id = :l_resource_id';
4061 ELSE
4062 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4063 END IF;
4064 
4065 l_sqltext :=  l_sqltext ||
4066 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
4067 AND a.object_country = :l_country ';
4068 l_sqltext :=  l_sqltext ||
4069 ' AND cal.report_date = &BIS_CURRENT_ASOF_DATE
4070 AND cal.calendar_id=-1
4071 GROUP BY p.value,p.parent_id,p.leaf_node_flag
4072 UNION ALL
4073 SELECT  /*+ORDERED*/
4074 p.value viewby,
4075 p.parent_id VIEWBYID,
4076 p.leaf_node_flag leaf_node_flag,
4080 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
4077 0 cost_actual,
4078 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
4079 0 actual_revenue ,
4081 sum('||l_prog_cost2||l_curr_suffix||') total_actual_cost,
4082 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue
4083 FROM fii_time_rpt_struct_v cal'||l_inner||',BIM_I_OBJ_METS_MV a
4084           ,eni_denorm_hierarchies edh
4085                 ,mtl_default_category_sets mdcs
4086                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag
4087                    FROM eni_item_vbh_nodes_v e
4088                    WHERE e.top_node_flag=''Y''
4089                    AND e.child_id = e.parent_id) p ';
4090 
4091 IF l_admin_status = 'N' THEN
4092 l_sqltext := l_sqltext ||',BIM_I_TOP_OBJECTS ac  ';
4093 END IF;
4094 
4095 l_sqltext :=  l_sqltext ||
4096 ' WHERE a.time_id = cal.time_id
4097 AND  a.period_type_id = cal.period_type_id
4098 AND a.category_id = edh.child_id
4099 AND edh.object_type = ''CATEGORY_SET''
4100 AND edh.object_id = mdcs.category_set_id
4101 AND mdcs.functional_area_id = 11
4102 AND edh.dbi_flag = ''Y''
4103 AND edh.parent_id = p.parent_id';
4104 
4105 IF l_admin_status = 'N' THEN
4106 l_sqltext := l_sqltext ||
4107 '
4108 AND a.source_code_id = ac.source_code_id
4109 AND ac.resource_id = :l_resource_id';
4110 ELSE
4111 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4112 END IF;
4113 
4114 l_sqltext :=  l_sqltext ||
4115 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
4116 AND a.object_country = :l_country ';
4117 l_sqltext :=  l_sqltext ||
4118 ' AND cal.report_date =trunc(sysdate)
4119 AND cal.calendar_id=-1
4120 AND a.source_code_id=inr.source_code_id
4121 GROUP BY p.value,p.parent_id,p.leaf_node_flag
4122 ) GROUP BY viewbyid,viewby,leaf_node_flag
4123 )
4124 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
4125 &ORDER_BY_CLAUSE';
4126 ELSE
4127 l_dass:=  BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('DASS');
4128 
4129 /********* building inline view to filter thosedo not have any ptd cost or ptd revenue ***********/
4130 
4131 l_inner:=   ',( SELECT DISTINCT a.source_code_id
4132                 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
4133 
4134 IF l_admin_status = 'N' THEN
4135 l_inner := l_inner ||',bim_i_top_objects ac ';
4136 END IF;
4137 
4138 l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
4139               AND a.period_type_id=cal.period_type_id
4140               AND cal.calendar_id=-1
4141               AND cal.report_date =&BIS_CURRENT_ASOF_DATE
4142 	      AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
4143 	      AND a.object_country = :l_country
4144               AND a.category_id = edh.child_id
4145               AND edh.object_type = ''CATEGORY_SET''
4146               AND edh.object_id = mdcs.category_set_id
4147               AND mdcs.functional_area_id = 11
4148               AND edh.dbi_flag = ''Y''
4149               AND edh.parent_id = :l_cat_id ';
4150 IF l_admin_status = 'N' THEN
4151 l_inner := l_inner ||
4152 '
4153 AND a.source_code_id = ac.source_code_id
4154 AND ac.resource_id = :l_resource_id';
4155 ELSE
4156 l_inner := l_inner ||' AND a.immediate_parent_id is null';
4157 END IF;
4158 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr ';
4159 
4160 l_sqltext :=
4161 '
4162  SELECT
4163  VIEWBY,
4164  VIEWBYID,
4165  null BIM_ATTRIBUTE2,
4166  cost_actual BIM_ATTRIBUTE3,
4167  actual_revenue BIM_ATTRIBUTE4,
4168  total_actual_cost BIM_ATTRIBUTE5,
4169  total_actual_revenue BIM_ATTRIBUTE6,
4170  total_roi BIM_ATTRIBUTE7,
4171  cost_forecasted BIM_ATTRIBUTE8,
4172  revenue_forecasted BIM_ATTRIBUTE9,
4173  forecast_roi BIM_ATTRIBUTE10,
4174  null BIM_ATTRIBUTE11,
4175  null BIM_ATTRIBUTE15,
4176  cost_variance BIM_ATTRIBUTE16,
4177  rev_variance  BIM_ATTRIBUTE17,
4178  roi_variance     BIM_ATTRIBUTE18,
4179  decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) BIM_URL1,
4180  NULL BIM_URL2,
4181  NULL BIM_URL3,
4182  sum(cost_actual) over() BIM_GRAND_TOTAL1,
4183  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
4184  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
4185  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
4186 case
4187 when sum(total_actual_cost) over()=0
4188 then null
4189 else
4190 ((sum(total_actual_revenue) over() - sum(total_actual_cost) over ()) /sum(total_actual_cost) over () )*100 end  BIM_GRAND_TOTAL5,
4191 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
4192 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
4193 case
4194 when sum(cost_forecasted) over()=0
4195 then null
4196 else
4197 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8,
4198 case when sum(cost_forecasted) over()=0
4199 then null
4200 else
4201 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9,
4202 case
4203 when sum(revenue_forecasted) over()=0
4204 then null
4205 else
4206 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
4207 case when sum(cost_forecasted) over() =0 then null
4208      when sum(total_actual_cost) over() =0 then null
4212         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
4209      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
4210      else
4211     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
4213       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
4214    end BIM_GRAND_TOTAL11
4215 FROM
4216 (
4217 SELECT
4218 VIEWBYID,
4219 viewby,
4220 leaf_node_flag,
4221 sum(cost_actual) cost_actual,
4222 SUM(cost_forecasted) cost_forecasted,
4223 SUM(actual_revenue) actual_revenue ,
4224 SUM(revenue_forecasted) revenue_forecasted,
4225 SUM(total_actual_cost) total_actual_cost,
4226 SUM(total_actual_revenue) total_actual_revenue,
4227 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
4228 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
4229 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
4230 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
4231 case when sum(cost_forecasted) =0 then null
4232      when sum(total_actual_cost) =0 then null
4233      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
4234      else
4235     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
4236         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
4237       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
4238    end roi_variance
4239 FROM
4240 (
4241 SELECT  /*+ORDERED*/
4242 p.id VIEWBYID,
4243 p.value  VIEWBY,
4244 p.leaf_node_flag leaf_node_flag,
4245 sum('||l_prog_cost1||l_curr_suffix||')  cost_actual,
4246 0 cost_forecasted,
4247 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
4248 0 revenue_forecasted,
4249 0 total_actual_cost,
4250 0 total_actual_revenue
4251 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a
4252     ,eni_denorm_hierarchies edh
4253             ,mtl_default_category_sets mdc
4254             ,(select e.id,e.value,e.leaf_node_flag
4255               from eni_item_vbh_nodes_v e
4256           where
4257               e.parent_id =:l_cat_id
4258               AND e.id = e.child_id
4259               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
4260       ) p ';
4261 IF l_admin_status = 'N' THEN
4262 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
4263 END IF;
4264 l_sqltext :=  l_sqltext ||
4265 ' WHERE a.time_id = cal.time_id
4266 AND  a.period_type_id = cal.period_type_id
4267 AND a.category_id = edh.child_id
4268 AND edh.object_type = ''CATEGORY_SET''
4269 AND edh.object_id = mdc.category_set_id
4270 AND mdc.functional_area_id = 11
4271 AND edh.dbi_flag = ''Y''
4272 AND edh.parent_id = p.id ';
4273 IF l_admin_status = 'N' THEN
4274 l_sqltext := l_sqltext ||
4275 '
4276 AND a.source_code_id = ac.source_code_id
4277 AND ac.resource_id = :l_resource_id';
4278 ELSE
4279 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4280 END IF;
4281 l_sqltext :=  l_sqltext ||
4282 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
4283 AND a.object_country = :l_country';
4284 l_sqltext :=  l_sqltext ||
4285 ' AND cal.report_date =&BIS_CURRENT_ASOF_DATE
4286 AND cal.calendar_id=-1
4287 GROUP BY p.id,p.value,p.leaf_node_flag
4288 UNION ALL
4289 SELECT /*+ORDERED*/
4290 p.id VIEWBYID,
4291 p.value  VIEWBY,
4292 p.leaf_node_flag leaf_node_flag,
4293 0 cost_actual,
4294 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
4295 0 actual_revenue ,
4296 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
4297 sum('||l_prog_cost2||l_curr_suffix||')  total_actual_cost,
4298 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue
4299 FROM fii_time_rpt_struct_v cal'||L_INNER||'
4300      ,BIM_I_OBJ_METS_MV a
4301     ,eni_denorm_hierarchies edh
4302             ,mtl_default_category_sets mdc
4303             ,(select e.id,e.value,e.leaf_node_flag
4304               from eni_item_vbh_nodes_v e
4305           where
4306               e.parent_id =:l_cat_id
4307               AND e.id = e.child_id
4308               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
4309       ) p ';
4310 IF l_admin_status = 'N' THEN
4311 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
4312 END IF;
4313 l_sqltext :=  l_sqltext ||
4314 ' WHERE a.time_id = cal.time_id
4315 AND  a.period_type_id = cal.period_type_id
4319 AND mdc.functional_area_id = 11
4316 AND a.category_id = edh.child_id
4317 AND edh.object_type = ''CATEGORY_SET''
4318 AND edh.object_id = mdc.category_set_id
4320 AND edh.dbi_flag = ''Y''
4321 AND edh.parent_id = p.id ';
4322 IF l_admin_status = 'N' THEN
4323 l_sqltext := l_sqltext ||
4324 '
4325 AND a.source_code_id = ac.source_code_id
4326 AND ac.resource_id = :l_resource_id';
4327 ELSE
4328 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4329 END IF;
4330 l_sqltext :=  l_sqltext ||
4331 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
4332 AND a.object_country = :l_country';
4333 l_sqltext :=  l_sqltext ||
4334 ' AND cal.report_date = trunc(sysdate)
4335 AND cal.calendar_id=-1
4336 AND a.source_code_id=inr.source_code_id
4337 GROUP BY p.id,p.value,p.leaf_node_flag
4338 /*** directly assigned to the category *************/
4339 UNION ALL
4340 SELECT /*+ORDERED*/
4341 p.id VIEWBYID,
4342 bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||'  viewby,
4343 ''Y'' leaf_node_flag,
4344 sum('||l_prog_cost1||l_curr_suffix||')  cost_actual,
4345 0 cost_forecasted,
4346 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
4347 0 revenue_forecasted,
4348 0 total_actual_cost,
4349 0 total_actual_revenue
4350 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a
4351     ,(select e.id id,e.value value
4352                       from eni_item_vbh_nodes_v e
4353                       where e.parent_id =  :l_cat_id
4354                       AND e.parent_id = e.child_id
4355                       AND leaf_node_flag <> ''Y''
4356                       ) p ';
4357 IF l_admin_status = 'N' THEN
4358 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
4359 END IF;
4360 l_sqltext :=  l_sqltext ||
4361 ' WHERE a.time_id = cal.time_id
4362 AND  a.period_type_id = cal.period_type_id
4363 AND a.category_id = p.id';
4364 IF l_admin_status = 'N' THEN
4365 l_sqltext := l_sqltext ||
4366 '
4367 AND a.source_code_id = ac.source_code_id
4368 AND ac.resource_id = :l_resource_id';
4369 ELSE
4370 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4371 END IF;
4372 l_sqltext :=  l_sqltext ||
4373 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
4374 AND a.object_country = :l_country';
4375 l_sqltext :=  l_sqltext ||
4376 ' AND cal.report_date =&BIS_CURRENT_ASOF_DATE
4377 AND cal.calendar_id=-1
4378 GROUP BY p.id
4379 UNION ALL
4380 SELECT /*+ORDERED*/
4381 p.id VIEWBYID,
4382 bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||'  viewby,
4383 ''Y'' leaf_node_flag,
4384 0 cost_actual,
4385 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
4386 0 actual_revenue ,
4387 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
4388 sum('||l_prog_cost2||l_curr_suffix||')  total_actual_cost,
4389 SUM('||l_prog_rev2||l_curr_suffix||')   total_actual_revenue
4390 FROM fii_time_rpt_struct_v cal'||l_inner||',BIM_I_OBJ_METS_MV a
4391     ,(select e.id id,e.value value
4392                       from eni_item_vbh_nodes_v e
4393                       where e.parent_id =  :l_cat_id
4394                       AND e.parent_id = e.child_id
4395                       AND leaf_node_flag <> ''Y''
4396                       ) p ';
4397 IF l_admin_status = 'N' THEN
4398 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
4399 END IF;
4400 l_sqltext :=  l_sqltext ||
4401 ' WHERE a.time_id = cal.time_id
4402 AND  a.period_type_id = cal.period_type_id
4403 AND a.category_id = p.id ';
4404 IF l_admin_status = 'N' THEN
4405 l_sqltext := l_sqltext ||
4406 '
4407 AND a.source_code_id = ac.source_code_id
4408 AND ac.resource_id = :l_resource_id';
4409 ELSE
4410 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4411 END IF;
4412 l_sqltext :=  l_sqltext ||
4413 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
4414 AND a.object_country = :l_country';
4415 l_sqltext :=  l_sqltext ||
4416 ' AND cal.report_date = trunc(sysdate)
4417 AND cal.calendar_id=-1
4418 AND a.source_code_id=inr.source_code_id
4419 group by p.id
4420 )GROUP BY VIEWBYID,viewby,leaf_node_flag
4421 )
4422 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
4423 &ORDER_BY_CLAUSE';
4424 end if;
4425 
4426 ELSIF (l_view_by ='MEDIA+MEDIA') THEN
4427 
4428 --l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
4429 
4430 /********* building inline view to filter those objects do not have any ptd cost or ptd revenue ***********/
4431 
4432 l_inner:=   ',( SELECT DISTINCT a.source_code_id
4433                 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a';
4434 
4435  IF l_cat_id is not null then
4436   l_inner := l_inner||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
4437   end if;
4438 
4439 IF l_admin_status = 'N' THEN
4440 l_inner := l_inner ||',bim_i_top_objects ac ';
4441 END IF;
4442 
4443 l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
4444               AND a.period_type_id=cal.period_type_id
4445               AND cal.calendar_id=-1
4446               AND cal.report_date =&BIS_CURRENT_ASOF_DATE
4447 	      AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
4448 	      AND a.object_country = :l_country ';
4449 
4450 IF l_admin_status = 'N' THEN
4451 l_inner := l_inner ||
4452 '
4453 AND a.source_code_id = ac.source_code_id
4454 AND ac.resource_id = :l_resource_id';
4458 IF l_cat_id is null then
4455 ELSE
4456 l_inner := l_inner ||' AND a.immediate_parent_id is null';
4457 END IF;
4459 l_inner := l_inner ||' AND a.category_id = -9 ';
4460 else
4461   l_inner := l_inner ||
4462   ' AND a.category_id = edh.child_id
4463     AND edh.object_type = ''CATEGORY_SET''
4464     AND edh.object_id = mdcs.category_set_id
4465     AND mdcs.functional_area_id = 11
4466     AND edh.dbi_flag = ''Y''
4467     AND edh.parent_id = :l_cat_id ';
4468   end if;
4469 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr ';
4470 
4471 l_sqltext :=
4472 '
4473 SELECT
4474  VIEWBY,
4475  null VIEWBYID,
4476  null BIM_ATTRIBUTE2,
4477  cost_actual BIM_ATTRIBUTE3,
4478  actual_revenue BIM_ATTRIBUTE4,
4479  total_actual_cost BIM_ATTRIBUTE5,
4480  total_actual_revenue BIM_ATTRIBUTE6,
4481  total_roi BIM_ATTRIBUTE7,
4482  cost_forecasted BIM_ATTRIBUTE8,
4483  revenue_forecasted BIM_ATTRIBUTE9,
4484  forecast_roi BIM_ATTRIBUTE10,
4485  null BIM_ATTRIBUTE11,
4486  null BIM_ATTRIBUTE15,
4487  cost_variance BIM_ATTRIBUTE16,
4488  rev_variance  BIM_ATTRIBUTE17,
4489  roi_variance     BIM_ATTRIBUTE18,
4490  null BIM_URL1,
4491  NULL BIM_URL2,
4492  NULL BIM_URL3,
4493  sum(cost_actual) over() BIM_GRAND_TOTAL1,
4494  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
4495  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
4496  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
4497 case
4498 when sum(total_actual_cost) over()=0
4499 then null
4500 else
4501 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5,
4502 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
4503 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
4504 case
4505 when sum(cost_forecasted) over()=0
4506 then null
4507 else
4508 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8,
4509 case when sum(cost_forecasted) over()=0
4510 then null
4511 else
4512 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9,
4513 case
4514 when sum(revenue_forecasted) over()=0
4515 then null
4516 else
4517 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
4518 case when sum(cost_forecasted) over() =0 then null
4519      when sum(total_actual_cost) over() =0 then null
4520      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
4521      else
4522     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
4523         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
4524       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
4525    end BIM_GRAND_TOTAL11
4526 
4527 FROM
4528 (
4529 SELECT
4530 viewby,
4531 sum(cost_actual) cost_actual,
4532 SUM(cost_forecasted) cost_forecasted,
4533 SUM(actual_revenue) actual_revenue ,
4534 SUM(revenue_forecasted) revenue_forecasted,
4535 SUM(total_actual_cost) total_actual_cost,
4536 SUM(total_actual_revenue) total_actual_revenue,
4537 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
4538 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
4539 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
4540 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
4541 case when sum(cost_forecasted) =0 then null
4542      when sum(total_actual_cost) =0 then null
4543      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
4544      else
4545     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
4546         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
4547       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
4548    end roi_variance
4549 FROM
4550 (
4551 SELECT
4552 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) viewby,
4553 sum('|| l_prog_cost1 ||l_curr_suffix||')  cost_actual,
4554 0 cost_forecasted,
4555 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
4556 0 revenue_forecasted,
4557 0 total_actual_cost,
4558 0 total_actual_revenue ';
4559 IF l_admin_status = 'N' THEN
4560 l_sqltext := l_sqltext ||' FROM bim_i_cpb_chnl_mv a,bim_i_top_objects ac,  ';
4561 ELSE
4562 l_sqltext := l_sqltext ||' FROM bim_i_cpb_chnl_mv a, ';
4563 END IF;
4564 l_sqltext := l_sqltext ||'
4565     fii_time_rpt_struct_v cal,
4566     bim_dimv_media d ';
4567 
4568  IF l_cat_id is not null then
4569   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
4570   end if;
4571 
4572 l_sqltext :=  l_sqltext ||
4573 ' WHERE a.time_id = cal.time_id
4574   AND  a.period_type_id = cal.period_type_id
4575   AND  a.object_country = :l_country';
4576 IF l_admin_status = 'N' THEN
4580 AND ac.resource_id = :l_resource_id';
4577 l_sqltext := l_sqltext ||
4578 '
4579 AND a.source_code_id = ac.source_code_id
4581 ELSE
4582 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4583 END IF;
4584 
4585 IF l_cat_id is null then
4586 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
4587 else
4588   l_sqltext := l_sqltext ||
4589                          ' AND a.category_id = edh.child_id
4590 			   AND edh.object_type = ''CATEGORY_SET''
4591 			   AND edh.object_id = mdcs.category_set_id
4592 			   AND mdcs.functional_area_id = 11
4593 			   AND edh.dbi_flag = ''Y''
4594 			   AND edh.parent_id = :l_cat_id ';
4595   end if;
4596 l_sqltext :=  l_sqltext ||
4597 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
4598   AND  d.id (+)= a.activity_id
4599   AND cal.report_date =&BIS_CURRENT_ASOF_DATE
4600   AND cal.calendar_id=-1
4601 GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.source_code_id
4602 UNION ALL
4603 SELECT
4604 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) viewby,
4605 0 cost_actual,
4606 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
4607 0 actual_revenue ,
4608 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
4609 sum('|| l_prog_cost2 ||l_curr_suffix||') total_actual_cost,
4610 SUM('||l_prog_rev2||l_curr_suffix||')    total_actual_revenue ';
4611 IF l_admin_status = 'N' THEN
4612 l_sqltext := l_sqltext ||' FROM fii_time_rpt_struct_v cal'||l_inner||',bim_obj_chnl_mv a,bim_i_top_objects ac,bim_dimv_media d  ';
4613 ELSE
4614 l_sqltext := l_sqltext ||' FROM fii_time_rpt_struct_v cal'||l_inner||',bim_obj_chnl_mv a,bim_dimv_media d ';
4615 END IF;
4616 
4617  IF l_cat_id is not null then
4618   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
4619   end if;
4620 l_sqltext :=  l_sqltext ||
4621 ' WHERE a.time_id = cal.time_id
4622   AND  a.period_type_id = cal.period_type_id
4623   AND  a.object_country = :l_country';
4624 IF l_admin_status = 'N' THEN
4625 l_sqltext := l_sqltext ||
4626 '
4627 AND a.source_code_id = ac.source_code_id
4628 AND ac.resource_id = :l_resource_id';
4629 ELSE
4630 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4631 END IF;
4632 IF l_cat_id is null then
4633 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
4634 else
4635   l_sqltext := l_sqltext ||
4636                          ' AND a.category_id = edh.child_id
4637 			   AND edh.object_type = ''CATEGORY_SET''
4638 			   AND edh.object_id = mdcs.category_set_id
4639 			   AND mdcs.functional_area_id = 11
4640 			   AND edh.dbi_flag = ''Y''
4641 			   AND edh.parent_id = :l_cat_id ';
4642   end if;
4643   l_sqltext :=  l_sqltext ||
4644 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
4645 AND  d.id (+)= a.activity_id
4646 AND cal.report_date = trunc(sysdate)
4647 AND cal.calendar_id=-1
4648 AND a.source_code_id=inr.source_code_id
4649 GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
4650 )
4651 GROUP BY viewby
4652 )
4653 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
4654 &ORDER_BY_CLAUSE';
4655 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
4656 --l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
4657 /********* building inline view to filter thosedo not have any ptd cost or ptd revenue ***********/
4658 
4659 l_inner:=   ',( SELECT DISTINCT a.source_code_id
4660                 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a';
4661 
4662  IF l_cat_id is not null then
4663   l_inner := l_inner||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
4664   end if;
4665 
4666 IF l_admin_status = 'N' THEN
4667 l_inner := l_inner ||',bim_i_top_objects ac ';
4668 END IF;
4669 
4670 l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
4671               AND a.period_type_id=cal.period_type_id
4672               AND cal.calendar_id=-1
4673               AND cal.report_date =&BIS_CURRENT_ASOF_DATE
4674 	      AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
4675 	      AND a.object_country = :l_country ';
4676 
4677 IF l_admin_status = 'N' THEN
4678 l_inner := l_inner ||
4679 '
4680 AND a.source_code_id = ac.source_code_id
4681 AND ac.resource_id = :l_resource_id';
4682 ELSE
4683 l_inner := l_inner ||' AND a.immediate_parent_id is null';
4684 END IF;
4685 IF l_cat_id is null then
4686 l_inner := l_inner ||' AND a.category_id = -9 ';
4687 else
4688   l_inner := l_inner ||
4689   ' AND a.category_id = edh.child_id
4690     AND edh.object_type = ''CATEGORY_SET''
4691     AND edh.object_id = mdcs.category_set_id
4692     AND mdcs.functional_area_id = 11
4693     AND edh.dbi_flag = ''Y''
4694     AND edh.parent_id = :l_cat_id ';
4695   end if;
4696 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr ';
4697 
4698 l_sqltext :=
4699 '
4700 SELECT
4701  VIEWBY,
4702  null VIEWBYID,
4703  null BIM_ATTRIBUTE2,
4704  cost_actual BIM_ATTRIBUTE3,
4705  actual_revenue BIM_ATTRIBUTE4,
4706  total_actual_cost BIM_ATTRIBUTE5,
4707  total_actual_revenue BIM_ATTRIBUTE6,
4708  total_roi BIM_ATTRIBUTE7,
4709  cost_forecasted BIM_ATTRIBUTE8,
4710  revenue_forecasted BIM_ATTRIBUTE9,
4711  forecast_roi BIM_ATTRIBUTE10,
4712  null BIM_ATTRIBUTE11,
4716  roi_variance     BIM_ATTRIBUTE18,
4713  null BIM_ATTRIBUTE15,
4714  cost_variance BIM_ATTRIBUTE16,
4715  rev_variance  BIM_ATTRIBUTE17,
4717  null BIM_URL1,
4718  NULL BIM_URL2,
4719  NULL BIM_URL3,
4720  sum(cost_actual) over() BIM_GRAND_TOTAL1,
4721  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
4722  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
4723  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
4724 case
4725 when sum(total_actual_cost) over()=0
4726 then null
4727 else
4728 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5,
4729 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
4730 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
4731 case
4732 when sum(cost_forecasted) over()=0
4733 then null
4734 else
4735 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8,
4736 case when sum(cost_forecasted) over()=0
4737 then null
4738 else
4739 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9,
4740 case
4741 when sum(revenue_forecasted) over()=0
4742 then null
4743 else
4744 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
4745 case when sum(cost_forecasted) over() =0 then null
4746      when sum(total_actual_cost) over() =0 then null
4747      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
4748      else
4749     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
4750         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
4751       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
4752    end BIM_GRAND_TOTAL11
4753 
4754 FROM
4755 (
4756 SELECT
4757 viewby,
4758 sum(cost_actual) cost_actual,
4759 SUM(cost_forecasted) cost_forecasted,
4760 SUM(actual_revenue) actual_revenue ,
4761 SUM(revenue_forecasted) revenue_forecasted,
4762 SUM(total_actual_cost) total_actual_cost,
4763 SUM(total_actual_revenue) total_actual_revenue,
4764 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
4765 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
4766 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
4767 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
4768 case when sum(cost_forecasted) =0 then null
4769      when sum(total_actual_cost) =0 then null
4770      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
4771      else
4772     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
4773         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
4774       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
4775    end roi_variance
4776 FROM
4777 (
4778 SELECT
4779 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) viewby,
4780 sum('|| l_prog_cost1 ||l_curr_suffix||')  cost_actual,
4781 0 cost_forecasted,
4782 sum('|| l_prog_rev1 ||l_curr_suffix||') actual_revenue ,
4783 0 revenue_forecasted,
4784 0 total_actual_cost,
4785 0 total_actual_revenue ';
4786 IF l_admin_status = 'N' THEN
4787 l_sqltext := l_sqltext ||' FROM bim_i_cpb_regn_mv a,bim_i_top_objects ac,  ';
4788 ELSE
4789 l_sqltext := l_sqltext ||' FROM bim_i_cpb_regn_mv a, ';
4790 END IF;
4791 l_sqltext := l_sqltext ||'
4792     fii_time_rpt_struct_v cal,
4793     bis_areas_v d ';
4794 
4795  IF l_cat_id is not null then
4796   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
4797   end if;
4798 
4799 l_sqltext :=  l_sqltext ||
4800 ' WHERE a.time_id = cal.time_id
4801   AND  a.period_type_id = cal.period_type_id
4802   AND  a.object_country = :l_country';
4803 IF l_admin_status = 'N' THEN
4804 l_sqltext := l_sqltext ||
4805 '
4806 AND a.source_code_id = ac.source_code_id
4807 AND ac.resource_id = :l_resource_id';
4808 ELSE
4809 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4810 END IF;
4811 
4812 IF l_cat_id is null then
4813 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
4814 else
4815   l_sqltext := l_sqltext ||
4816                          ' AND a.category_id = edh.child_id
4817 			   AND edh.object_type = ''CATEGORY_SET''
4818 			   AND edh.object_id = mdcs.category_set_id
4819 			   AND mdcs.functional_area_id = 11
4820 			   AND edh.dbi_flag = ''Y''
4821 			   AND edh.parent_id = :l_cat_id ';
4822   end if;
4823 l_sqltext :=  l_sqltext ||
4824 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
4825   AND  d.id (+)= a.object_region
4826   AND cal.report_date =&BIS_CURRENT_ASOF_DATE
4827   AND cal.calendar_id=-1
4828 GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
4829 UNION ALL
4830 SELECT
4831 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) viewby,
4835 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
4832 0 cost_actual,
4833 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
4834 0 actual_revenue ,
4836 sum('|| l_prog_cost2 ||l_curr_suffix||') total_actual_cost,
4837 sum('|| l_prog_rev2 ||l_curr_suffix||')  total_actual_revenue ';
4838 IF l_admin_status = 'N' THEN
4839 l_sqltext := l_sqltext ||' FROM fii_time_rpt_struct_v cal'||l_inner||',bim_obj_regn_mv a,bim_i_top_objects ac,bis_areas_v d  ';
4840 ELSE
4841 l_sqltext := l_sqltext ||' FROM fii_time_rpt_struct_v cal'||l_inner||',bim_obj_regn_mv a,bis_areas_v d ';
4842 END IF;
4843  IF l_cat_id is not null then
4844   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
4845   end if;
4846 l_sqltext :=  l_sqltext ||
4847 ' WHERE a.time_id = cal.time_id
4848   AND  a.period_type_id = cal.period_type_id
4849   AND  a.object_country = :l_country';
4850 IF l_admin_status = 'N' THEN
4851 l_sqltext := l_sqltext ||
4852 '
4853 AND a.source_code_id = ac.source_code_id
4854 AND ac.resource_id = :l_resource_id';
4855 ELSE
4856 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
4857 END IF;
4858 IF l_cat_id is null then
4859 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
4860 else
4861   l_sqltext := l_sqltext ||
4862                          ' AND a.category_id = edh.child_id
4863 			   AND edh.object_type = ''CATEGORY_SET''
4864 			   AND edh.object_id = mdcs.category_set_id
4865 			   AND mdcs.functional_area_id = 11
4866 			   AND edh.dbi_flag = ''Y''
4867 			   AND edh.parent_id = :l_cat_id ';
4868   end if;
4869   l_sqltext :=  l_sqltext ||
4870 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
4871 AND  d.id (+)= a.object_region
4872 AND cal.report_date = trunc(sysdate)
4873 AND cal.calendar_id=-1
4874 AND a.source_code_id=inr.source_code_id
4875 GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
4876 )
4877 GROUP BY viewby
4878 )
4879 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
4880 &ORDER_BY_CLAUSE';
4881 ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
4882 --l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
4883 /********* building inline view to filter thosedo not have any ptd cost or ptd revenue ***********/
4884 
4885 l_inner:=   '( SELECT DISTINCT a.source_code_id
4886                 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a';
4887 
4888  IF l_cat_id is not null then
4889   l_inner := l_inner||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
4890   end if;
4891 
4892 IF l_admin_status = 'N' THEN
4893 l_inner := l_inner ||',bim_i_top_objects ac ';
4894 END IF;
4895 
4896 l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
4897               AND a.period_type_id=cal.period_type_id
4898               AND cal.calendar_id=-1
4899               AND cal.report_date =&BIS_CURRENT_ASOF_DATE
4900 	      AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
4901 	      AND a.object_country = :l_country ';
4902 
4903 IF l_admin_status = 'N' THEN
4904 l_inner := l_inner ||
4905 '
4906 AND a.source_code_id = ac.source_code_id
4907 AND ac.resource_id = :l_resource_id';
4908 ELSE
4909 l_inner := l_inner ||' AND a.immediate_parent_id is null';
4910 END IF;
4911 IF l_cat_id is null then
4912 l_inner := l_inner ||' AND a.category_id = -9 ';
4913 else
4914   l_inner := l_inner ||
4915   ' AND a.category_id = edh.child_id
4916     AND edh.object_type = ''CATEGORY_SET''
4917     AND edh.object_id = mdcs.category_set_id
4918     AND mdcs.functional_area_id = 11
4919     AND edh.dbi_flag = ''Y''
4920     AND edh.parent_id = :l_cat_id ';
4921   end if;
4922 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr ';
4923 
4924 l_sqltext :=
4925 '
4926  SELECT
4927  VIEWBY,
4928  VIEWBYID,
4929  null BIM_ATTRIBUTE2,
4930  cost_actual BIM_ATTRIBUTE3,
4931  actual_revenue BIM_ATTRIBUTE4,
4932  total_actual_cost BIM_ATTRIBUTE5,
4933  total_actual_revenue BIM_ATTRIBUTE6,
4934  total_roi BIM_ATTRIBUTE7,
4935  cost_forecasted BIM_ATTRIBUTE8,
4936  revenue_forecasted BIM_ATTRIBUTE9,
4937  forecast_roi BIM_ATTRIBUTE10,
4938  null BIM_ATTRIBUTE11,
4939  null BIM_ATTRIBUTE15,
4940  cost_variance BIM_ATTRIBUTE16,
4941  rev_variance  BIM_ATTRIBUTE17,
4942  roi_variance    BIM_ATTRIBUTE18,
4943  null BIM_URL1,
4944  NULL BIM_URL2,
4945  NULL BIM_URL3,
4946  sum(cost_actual) over() BIM_GRAND_TOTAL1,
4947  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
4948  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
4949  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
4950 case
4951 when sum(total_actual_cost) over()=0
4952 then null
4953 else
4954 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5,
4955 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
4956 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
4957 case
4958 when sum(cost_forecasted) over()=0
4959 then null
4960 else
4961 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8,
4962 case when sum(cost_forecasted) over()=0
4963 then null
4964 else
4965 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9,
4969 else
4966 case
4967 when sum(revenue_forecasted) over()=0
4968 then null
4970 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
4971 case when sum(cost_forecasted) over() =0 then null
4972      when sum(total_actual_cost) over() =0 then null
4973      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
4974      else
4975     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
4976         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
4977       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
4978    end BIM_GRAND_TOTAL11
4979 FROM
4980 (
4981 SELECT
4982 viewby,
4983 viewbyid,
4984 sum(cost_actual) cost_actual,
4985 SUM(cost_forecasted) cost_forecasted,
4986 SUM(actual_revenue) actual_revenue ,
4987 SUM(revenue_forecasted) revenue_forecasted,
4988 SUM(total_actual_cost) total_actual_cost,
4989 SUM(total_actual_revenue) total_actual_revenue,
4990 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
4991 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
4992 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
4993 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
4994 case when sum(cost_forecasted) =0 then null
4995      when sum(total_actual_cost) =0 then null
4996      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
4997      else
4998     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
4999         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
5000       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
5001    end roi_variance
5002 FROM
5003 (
5004 SELECT
5005 decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) viewby,
5006 a.object_country viewbyid,
5007 sum('|| l_prog_cost1 ||l_curr_suffix||')  cost_actual,
5008 0 cost_forecasted,
5009 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
5010 0 revenue_forecasted,
5011 0 total_actual_cost,
5012 0 total_actual_revenue
5013 FROM BIM_I_CPB_METS_MV a,
5014      fii_time_rpt_struct_v cal,
5015      fnd_territories_tl d ';
5016  IF l_cat_id is not null then
5017   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
5018   end if;
5019 
5020 IF l_admin_status = 'N' THEN
5021 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
5022 END IF;
5023 l_sqltext :=  l_sqltext ||
5024 ' WHERE a.time_id = cal.time_id
5025   AND  a.period_type_id = cal.period_type_id';
5026 IF l_admin_status = 'N' THEN
5027 l_sqltext := l_sqltext ||
5028 '
5029 AND a.source_code_id = ac.source_code_id
5030 AND ac.resource_id = :l_resource_id';
5031 ELSE
5032 l_sqltext := l_sqltext ||
5033 ' AND  a.immediate_parent_id is null ';
5034 END IF;
5035 IF l_cat_id is null then
5036 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
5037 else
5038   l_sqltext := l_sqltext ||
5039                          ' AND a.category_id = edh.child_id
5040 			   AND edh.object_type = ''CATEGORY_SET''
5041 			   AND edh.object_id = mdcs.category_set_id
5042 			   AND mdcs.functional_area_id = 11
5043 			   AND edh.dbi_flag = ''Y''
5044 			   AND edh.parent_id = :l_cat_id ';
5045   end if;
5046   if l_country <>'N' then
5047   l_sqltext :=  l_sqltext || ' AND a.object_country = :l_country ';
5048   else
5049   l_sqltext :=  l_sqltext || ' AND a.object_country <>''N'' ';
5050   end if;
5051 l_sqltext :=  l_sqltext ||
5052 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
5053   AND  a.object_country =d.territory_code(+)
5054   AND d.language(+) = userenv(''LANG'')
5055   AND cal.report_date =&BIS_CURRENT_ASOF_DATE
5056   AND cal.calendar_id=-1
5057 GROUP BY decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country
5058 UNION ALL
5059 SELECT /*+ordered*/
5060 decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) viewby,
5061 a.object_country viewbyid,
5062 0 cost_actual,
5063 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
5064 0 actual_revenue ,
5065 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
5066 sum('|| l_prog_cost2 ||l_curr_suffix||') total_actual_cost,
5067 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue
5068 FROM '||l_inner||',fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a';
5069  IF l_cat_id is not null then
5070   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs,fnd_territories_tl d';
5071   else
5072   l_sqltext := l_sqltext ||' ,fnd_territories_tl d';
5073   end if;
5074 
5075   IF l_admin_status = 'N' THEN
5076 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
5077 END IF;
5078 l_sqltext :=  l_sqltext ||
5079 ' WHERE a.time_id = cal.time_id
5080   AND  a.period_type_id = cal.period_type_id';
5081 IF l_admin_status = 'N' THEN
5082 l_sqltext := l_sqltext ||
5083 '
5087 l_sqltext := l_sqltext ||
5084 AND a.source_code_id = ac.source_code_id
5085 AND ac.resource_id = :l_resource_id';
5086 ELSE
5088 ' AND  a.immediate_parent_id is null ';
5089 END IF;
5090 
5091 IF l_cat_id is null then
5092 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
5093 else
5094   l_sqltext := l_sqltext ||
5095                          ' AND a.category_id = edh.child_id
5096 			   AND edh.object_type = ''CATEGORY_SET''
5097 			   AND edh.object_id = mdcs.category_set_id
5098 			   AND mdcs.functional_area_id = 11
5099 			   AND edh.dbi_flag = ''Y''
5100 			   AND edh.parent_id = :l_cat_id ';
5101   end if;
5102   if l_country <>'N' then
5103   l_sqltext :=  l_sqltext || ' AND a.object_country = :l_country ';
5104   else
5105   l_sqltext :=  l_sqltext || ' AND a.object_country <>''N'' ';
5106   end if;
5107 l_sqltext :=  l_sqltext ||
5108 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
5109 AND  a.object_country =d.territory_code(+)
5110 AND d.language(+) = userenv(''LANG'')
5111 AND cal.report_date = trunc(sysdate)
5112 AND cal.calendar_id=-1
5113 AND a.source_code_id=inr.source_code_id
5114 group by decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country
5115 )GROUP BY viewby,viewbyid
5116 )
5117 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
5118 &ORDER_BY_CLAUSE';
5119 end if;
5120 
5121 ELSE
5122 /*********************** starting campaign drill down ************************/
5123 
5124 if (l_view_by = 'CAMPAIGN+CAMPAIGN') then
5125 
5126  -- checking for the object type passed from page
5127 
5128  for i in get_obj_type
5129  loop
5130  l_object_type:=i.object_type;
5131  end loop;
5132 
5133 if l_object_type='CAMP' THEN
5134  l_csch_chnl :='|| '' - '' || channel';
5135  l_chnl_col := 'channel,';
5136  l_chnl_select := ' decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel,';
5137  l_chnl_from:= ' ,bim_dimv_media chnl ';
5138  l_chnl_where := ' AND camp.activity_id =chnl.id (+) ';
5139  l_chnl_group := ' decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) ,';
5140 
5141  END IF;
5142 
5143 
5144 /******** view by is campaign **********/
5145  l_sqltext :=
5146  ' SELECT name VIEWBY,
5147  VIEWBYID,
5148  meaning'||l_csch_chnl||' BIM_ATTRIBUTE2,
5149  cost_actual BIM_ATTRIBUTE3,
5150  actual_revenue BIM_ATTRIBUTE4,
5151  total_actual_cost BIM_ATTRIBUTE5,
5152  total_actual_revenue BIM_ATTRIBUTE6,
5153  total_roi BIM_ATTRIBUTE7,
5154  cost_forecasted BIM_ATTRIBUTE8,
5155  revenue_forecasted BIM_ATTRIBUTE9,
5156  forecast_roi BIM_ATTRIBUTE10,
5157  null BIM_ATTRIBUTE11,
5158  object_type  BIM_ATTRIBUTE15,
5159  cost_variance BIM_ATTRIBUTE16,
5160  rev_variance  BIM_ATTRIBUTE17,
5161  roi_variance     BIM_ATTRIBUTE18,
5162  null BIM_URL1,
5163  decode(object_type,''CSCH'','||''''||l_url_str_csch||''''||'||object_id,''EONE'',NULL,''EVEO'',NULL,'||''''||l_url_str||''''||') BIM_URL2,
5164  decode(object_type,''CSCH'','||''''||l_url_str_type||''''||'||object_id,NULL ) BIM_URL3,
5165  sum(cost_actual) over() BIM_GRAND_TOTAL1,
5166  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
5167  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
5168  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
5169 case
5170 when sum(total_actual_cost) over()=0
5171 then null
5172 else
5173 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5, /*total roi*/
5174 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
5175 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
5176 case
5177 when sum(cost_forecasted) over()=0
5178 then null
5179 else
5180 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8, /*forecasted roi*/
5181 case when sum(cost_forecasted) over()=0
5182 then null
5183 else
5184 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9, /* cost variance*/
5185 case
5186 when sum(revenue_forecasted) over()=0
5187 then null
5188 else
5189 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
5190 case when sum(cost_forecasted) over() =0 then null
5191      when sum(total_actual_cost) over() =0 then null
5192      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
5193      else
5194     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
5195         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
5196       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
5197    end BIM_GRAND_TOTAL11
5198 FROM
5199 (
5200 SELECT
5201 object_id,
5202 object_type,
5203 VIEWBYID,
5204 name,
5205 meaning,'||l_chnl_col||'
5206 decode(object_type,''CSCH'',usage,NULL) usage,
5207 sum(cost_actual) cost_actual,
5208 SUM(cost_forecasted) cost_forecasted,
5209 SUM(actual_revenue) actual_revenue ,
5210 SUM(revenue_forecasted) revenue_forecasted,
5211 SUM(total_actual_cost) total_actual_cost,
5212 SUM(total_actual_revenue) total_actual_revenue,
5216 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
5213 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
5214 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
5215 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
5217 case when sum(cost_forecasted) =0 then null
5218      when sum(total_actual_cost) =0 then null
5219      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
5220      else
5221     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
5222         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
5223       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
5224    end roi_variance
5225 FROM
5226 ( ';
5227 
5228 l_curr_suffix1 :=l_curr_suffix;
5229 
5230 IF l_object_type in ('CAMP','EVEH','CSCH') AND l_prog_cost ='BIM_APPROVED_BUDGET'
5231 THEN
5232 
5233 --l_table_bud :=  ' bim_i_marketing_facts facts,';
5234 
5235 --l_where_bud := ' AND facts.source_code_id = a.source_code_id';
5236 	if l_curr_suffix is null then
5237 		l_prog_cost1 := 'a.budget_approved_sch';
5238 		l_prog_cost2 := 'a.budget_approved_sch';
5239 	else
5240 		l_curr_suffix1 := null;
5241 		l_prog_cost1 := 'a.budget_approved_sch_s';
5242 		l_prog_cost2 := 'a.budget_approved_sch_s';
5243 	end if;
5244 
5245 end if;
5246 l_sqltext := l_sqltext||
5247 ' SELECT /*+LEADING(b)*/
5248 camp.object_id,
5249 camp.object_type object_type,
5250 a.source_code_id VIEWBYID,
5251 camp.name name,
5252 l.meaning meaning,'||l_chnl_select||'
5253 camp.child_object_usage usage,
5254 sum('|| l_prog_cost1 ||l_curr_suffix1||')  cost_actual,
5255 0 cost_forecasted,
5256 SUM(a.'||l_prog_rev1||l_curr_suffix||')  actual_revenue ,
5257 0 revenue_forecasted,
5258 0 total_actual_cost,
5259 0 total_actual_revenue
5260 FROM  BIM_I_CPB_METS_MV a,
5261     fii_time_rpt_struct_v cal,
5262     bim_i_obj_name_mv camp,
5263     ams_lookups l'||l_chnl_from;
5264  IF l_cat_id is not null then
5265   l_sqltext := l_sqltext ||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
5266   end if;
5267 
5268 l_sqltext :=  l_sqltext ||
5269 ' WHERE a.time_id = cal.time_id
5270 AND  a.period_type_id = cal.period_type_id
5271 AND camp.source_code_id = a.source_code_id
5272 AND  a.immediate_parent_id =  :l_campaign_id
5273 AND l.lookup_code = camp.object_type
5274 AND l.lookup_type = ''AMS_SYS_ARC_QUALIFIER'''||l_chnl_where||l_where_bud;
5275 
5276 IF l_cat_id is null then
5277 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
5278 else
5279   l_sqltext := l_sqltext ||
5280 ' AND a.category_id = edh.child_id
5281   AND edh.object_type = ''CATEGORY_SET''
5282   AND edh.object_id = mdcs.category_set_id
5283   AND mdcs.functional_area_id = 11
5284   AND edh.dbi_flag = ''Y''
5285   AND edh.parent_id = :l_cat_id ';
5286   end if;
5287 l_sqltext :=  l_sqltext ||
5288 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
5289 AND a.object_country = :l_country
5290 AND cal.report_date =&BIS_CURRENT_ASOF_DATE
5291 AND cal.calendar_id=-1
5292 AND camp.language =USERENV(''LANG'')
5293 GROUP BY a.source_code_id,camp.object_id,camp.object_type,
5294 camp.name, l.meaning,'||l_chnl_group||'camp.child_object_usage
5295 UNION ALL
5296 SELECT /*+LEADING(b)*/
5297 camp.object_id,
5298 camp.object_type object_type,
5299 a.source_code_id VIEWBYID,
5300 camp.name name,
5301 l.meaning meaning,'||l_chnl_select||'
5302 camp.child_object_usage usage,
5303 0 cost_actual,
5304 SUM(a.cost_forecasted'||l_curr_suffix||') cost_forecasted,
5305 0 actual_revenue ,
5306 SUM(a.revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
5307 sum('|| l_prog_cost2 ||l_curr_suffix1||') total_actual_cost,
5308 SUM(a.'||l_prog_rev2||l_curr_suffix||') total_actual_revenue
5309 FROM  BIM_I_OBJ_METS_MV a,
5310     fii_time_rpt_struct_v cal,
5311     bim_i_obj_name_mv camp,
5312     ams_lookups l'||l_chnl_from;
5313  IF l_cat_id is not null then
5314   l_sqltext := l_sqltext ||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
5315   end if;
5316 
5317 l_sqltext :=  l_sqltext ||
5318 ' WHERE a.time_id = cal.time_id
5319 AND  a.period_type_id = cal.period_type_id
5320 AND camp.source_code_id = a.source_code_id
5321 AND  a.immediate_parent_id = :l_campaign_id
5322 AND l.lookup_code = camp.object_type
5323 AND l.lookup_type = ''AMS_SYS_ARC_QUALIFIER'''||l_chnl_where||l_where_bud;
5324 
5325 IF l_cat_id is null then
5326 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
5327 else
5328   l_sqltext := l_sqltext ||
5329 ' AND a.category_id = edh.child_id
5330   AND edh.object_type = ''CATEGORY_SET''
5331   AND edh.object_id = mdcs.category_set_id
5332   AND mdcs.functional_area_id = 11
5333   AND edh.dbi_flag = ''Y''
5334   AND edh.parent_id = :l_cat_id ';
5335   end if;
5336 l_sqltext :=  l_sqltext ||
5337 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
5338 AND a.object_country = :l_country
5339 AND cal.report_date = trunc(sysdate)
5340 AND cal.calendar_id=-1
5341 AND camp.language =USERENV(''LANG'')
5342 GROUP BY a.source_code_id,camp.object_id,camp.object_type,
5346 HAVING
5343 camp.name, l.meaning,'||l_chnl_group||'camp.child_object_usage ';
5344 l_sqltext := l_sqltext ||
5345 ') group by VIEWBYID,object_type,object_id,name, meaning'||l_csch_chnl||', usage
5347 sum(cost_actual) <> 0
5348 OR sum(actual_revenue) <> 0
5349 )
5350 &ORDER_BY_CLAUSE
5351 ';
5352 ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
5353 
5354 IF l_cat_id is null THEN
5355 /************** START OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NULL AND CAMPAIGN ID IS NOT NULL ***********/
5356 
5357 /* l_inner:=   ',( SELECT DISTINCT a.source_code_id
5358                 FROM fii_time_rpt_struct_v cal,bim_i_source_codes b,BIM_I_CPB_METS_MV a';
5359 
5360  IF l_admin_status = 'N' THEN
5361 l_inner := l_inner ||',bim_i_top_objects ac ';
5362 END IF;
5363 
5364 l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
5365               AND a.period_type_id=cal.period_type_id
5366               AND cal.calendar_id=-1
5367               AND cal.report_date =&BIS_CURRENT_ASOF_DATE
5368 	      AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
5369 	      AND  a.parent_denorm_type = b.object_type
5370               AND  a.parent_object_id = b.object_id
5371               AND  b.child_object_id = 0
5372               AND  b.source_code_id = :l_campaign_id
5373 	      AND a.object_country = :l_country
5374 	      AND a.category_id=-9 '; */
5375 
5376 /* IF l_admin_status = 'N' THEN
5377 l_inner := l_inner ||
5378 '
5379 AND a.source_code_id = ac.source_code_id
5380 AND ac.resource_id = :l_resource_id';
5381 ELSE
5382 l_inner := l_inner ||' AND a.immediate_parent_id is null';
5383 END IF; */
5384 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr ';
5385 
5386 l_sqltext :=
5387 '
5388  SELECT
5389  VIEWBY,
5390  VIEWBYID,
5391  null BIM_ATTRIBUTE2,
5392  cost_actual BIM_ATTRIBUTE3,
5393  actual_revenue BIM_ATTRIBUTE4,
5394  total_actual_cost BIM_ATTRIBUTE5,
5395  total_actual_revenue BIM_ATTRIBUTE6,
5396  total_roi BIM_ATTRIBUTE7,
5397  cost_forecasted BIM_ATTRIBUTE8,
5398  revenue_forecasted BIM_ATTRIBUTE9,
5399  forecast_roi BIM_ATTRIBUTE10,
5400  null BIM_ATTRIBUTE11,
5401  null BIM_ATTRIBUTE15,
5402  cost_variance BIM_ATTRIBUTE16,
5403  rev_variance  BIM_ATTRIBUTE17,
5404  roi_variance     BIM_ATTRIBUTE18,
5405  decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) BIM_URL1,
5406  NULL BIM_URL2,
5407  NULL BIM_URL3,
5408  sum(cost_actual) over() BIM_GRAND_TOTAL1,
5409  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
5410  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
5411  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
5412 case
5413 when sum(total_actual_cost) over()=0
5414 then null
5415 else
5416 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5,
5417 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
5418 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
5419 case
5420 when sum(cost_forecasted) over()=0
5421 then null
5422 else
5423 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8, /* forecasted roi */
5424 case when sum(cost_forecasted) over()=0
5425 then null
5426 else
5427 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9, /* cost variance */
5428 case
5429 when sum(revenue_forecasted) over()=0
5430 then null
5431 else
5432 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
5433 case when sum(cost_forecasted) over() =0 then null
5434      when sum(total_actual_cost) over() =0 then null
5435      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
5436      else
5437     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
5438         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
5439       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
5440    end BIM_GRAND_TOTAL11
5441 FROM
5442 (
5443 SELECT
5444 VIEWBYID,
5445 viewby,
5446 leaf_node_flag,
5447 sum(cost_actual) cost_actual,
5448 SUM(cost_forecasted) cost_forecasted,
5449 SUM(actual_revenue) actual_revenue ,
5450 SUM(revenue_forecasted) revenue_forecasted,
5451 SUM(total_actual_cost) total_actual_cost,
5452 SUM(total_actual_revenue) total_actual_revenue,
5453 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
5454 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
5455 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
5456 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
5457 case when sum(cost_forecasted) =0 then null
5458      when sum(total_actual_cost) =0 then null
5459      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
5460      else
5461     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
5462         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
5466 (
5463       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
5464    end roi_variance
5465 FROM
5467 SELECT /*+ORDERED*/
5468 p.parent_id VIEWBYID,
5469 p.value  viewby,
5470 p.leaf_node_flag leaf_node_flag,
5471 sum('|| l_prog_cost1 ||l_curr_suffix1||') cost_actual,
5472 0 cost_forecasted,
5473 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
5474 0 revenue_forecasted,
5475 0 total_actual_cost,
5476 0 total_actual_revenue
5477 FROM fii_time_rpt_struct_v cal ,BIM_I_CPB_METS_MV a
5478     ,eni_denorm_hierarchies edh
5479                 ,mtl_default_category_sets mdcs
5480                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag
5481                    FROM eni_item_vbh_nodes_v e
5482                    WHERE e.top_node_flag=''Y''
5483                    AND e.child_id = e.parent_id) p ';
5484 
5485 /* IF l_admin_status = 'N' THEN
5486 l_sqltext := l_sqltext ||',bim_i_top_objects ac ';
5487 END IF;*/
5488 
5489 l_sqltext :=  l_sqltext ||
5490 ' WHERE a.time_id = cal.time_id
5491 AND  a.period_type_id = cal.period_type_id
5492 AND a.category_id = edh.child_id
5493 AND edh.object_type = ''CATEGORY_SET''
5494 AND edh.object_id = mdcs.category_set_id
5495 AND mdcs.functional_area_id = 11
5496 AND edh.dbi_flag = ''Y''
5497 AND edh.parent_id = p.parent_id
5498 AND  a.source_code_id = :l_campaign_id ';
5499 
5500 /* IF l_admin_status = 'N' THEN
5501 l_sqltext := l_sqltext ||
5502 '
5503 AND a.source_code_id = ac.source_code_id
5504 AND ac.resource_id = :l_resource_id';
5505 ELSE
5506 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
5507 END IF; */
5508 
5509 l_sqltext :=  l_sqltext ||
5510 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
5511   AND a.object_country = :l_country';
5512 l_sqltext :=  l_sqltext ||
5513 ' AND cal.report_date =&BIS_CURRENT_ASOF_DATE
5514 AND cal.calendar_id=-1
5515 GROUP BY p.value,p.parent_id,p.leaf_node_flag
5516 UNION ALL
5517 SELECT /*+ORDERED*/
5518 p.parent_id VIEWBYID,
5519 p.value  viewby,
5520 p.leaf_node_flag leaf_node_flag,
5521 0 cost_actual,
5522 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
5523 0 actual_revenue ,
5524 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
5525 sum('|| l_prog_cost2 ||l_curr_suffix1||') total_actual_cost,
5526 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue
5527 FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
5528       ,eni_denorm_hierarchies edh
5529                 ,mtl_default_category_sets mdcs
5530                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag
5531                    FROM eni_item_vbh_nodes_v e
5532                    WHERE e.top_node_flag=''Y''
5533                    AND e.child_id = e.parent_id) p ';
5534 
5535 /*IF l_admin_status = 'N' THEN
5536 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
5537 END IF;*/
5538 
5539 l_sqltext :=  l_sqltext ||
5540 ' WHERE a.time_id = cal.time_id
5541 AND  a.period_type_id = cal.period_type_id
5542 AND a.category_id = edh.child_id
5543 AND edh.object_type = ''CATEGORY_SET''
5544 AND edh.object_id = mdcs.category_set_id
5545 AND mdcs.functional_area_id = 11
5546 AND edh.dbi_flag = ''Y''
5547 AND edh.parent_id = p.parent_id
5548 AND  a.source_code_id = :l_campaign_id ';
5549 
5550  /*IF l_admin_status = 'N' THEN
5551 l_sqltext := l_sqltext ||
5552 '
5553 AND a.source_code_id = ac.source_code_id
5554 AND ac.resource_id = :l_resource_id';
5555 ELSE
5556 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
5557 END IF; */
5558 
5559 l_sqltext :=  l_sqltext ||
5560 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
5561 AND a.object_country = :l_country';
5562 l_sqltext :=  l_sqltext ||
5563 ' AND cal.report_date = trunc(sysdate)
5564 AND cal.calendar_id=-1
5565 GROUP BY p.value,p.parent_id,p.leaf_node_flag
5566 ) GROUP BY VIEWBYID,viewby,leaf_node_flag
5567 )
5568 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
5569 &ORDER_BY_CLAUSE';
5570 ELSE
5571 
5572 /************** START OF VIEW BY IS PRODUCT CATEGORY AND PRODUCT CATEGORY IS NOT NULL AND CAMPAIGN ID IS NOT NULL ***********/
5573 
5574 /********* building inline view to filter thosedo not have any ptd cost or ptd revenue ***********/
5575 l_dass:=  BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('DASS');
5576 /* l_inner:=   ',( SELECT DISTINCT a.source_code_id
5577                 FROM fii_time_rpt_struct_v cal,bim_i_source_codes b,bim_i_cpb_mets_mv a,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
5578 
5579  IF l_admin_status = 'N' THEN
5580 l_inner := l_inner ||',bim_i_top_objects ac ';
5581 END IF;
5582 
5583 l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
5584               AND a.period_type_id=cal.period_type_id
5585               AND cal.calendar_id=-1
5586               AND cal.report_date =&BIS_CURRENT_ASOF_DATE
5587 	      AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
5588 	      AND  a.parent_denorm_type = b.object_type
5589               AND  a.parent_object_id = b.object_id
5590               AND  b.child_object_id = 0
5591               AND  b.source_code_id = :l_campaign_id
5592 	      AND a.object_country = :l_country
5593               AND a.category_id = edh.child_id
5594               AND edh.object_type = ''CATEGORY_SET''
5598               AND edh.parent_id = :l_cat_id '; */
5595               AND edh.object_id = mdcs.category_set_id
5596               AND mdcs.functional_area_id = 11
5597               AND edh.dbi_flag = ''Y''
5599 
5600 /* IF l_admin_status = 'N' THEN
5601 l_inner := l_inner ||
5602 '
5603 AND a.source_code_id = ac.source_code_id
5604 AND ac.resource_id = :l_resource_id';
5605 ELSE
5606 l_inner := l_inner ||' AND a.immediate_parent_id is null';
5607 END IF;*/
5608 
5609 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr ';
5610 
5611 l_sqltext :=
5612 '
5613  SELECT
5614  VIEWBY,
5615  VIEWBYID,
5616  null BIM_ATTRIBUTE2,
5617  cost_actual BIM_ATTRIBUTE3,
5618  actual_revenue BIM_ATTRIBUTE4,
5619  total_actual_cost BIM_ATTRIBUTE5,
5620  total_actual_revenue BIM_ATTRIBUTE6,
5621  total_roi BIM_ATTRIBUTE7,
5622  cost_forecasted BIM_ATTRIBUTE8,
5623  revenue_forecasted BIM_ATTRIBUTE9,
5624  forecast_roi BIM_ATTRIBUTE10,
5625  null BIM_ATTRIBUTE11,
5626  null BIM_ATTRIBUTE15,
5627  cost_variance BIM_ATTRIBUTE16,
5628  rev_variance  BIM_ATTRIBUTE17,
5629  roi_variance     BIM_ATTRIBUTE18,
5630  decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) BIM_URL1,
5631  NULL BIM_URL2,
5632  NULL BIM_URL3,
5633  sum(cost_actual) over() BIM_GRAND_TOTAL1,
5634  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
5635  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
5636  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
5637 case
5638 when sum(total_actual_cost) over()=0
5639 then null
5640 else
5641 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5,
5642 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
5643 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
5644 case
5645 when sum(cost_forecasted) over()=0
5646 then null
5647 else
5648 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8, /* forecasted roi*/
5649 case when sum(cost_forecasted) over()=0
5650 then null
5651 else
5652 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9, /*cost variance*/
5653 case
5654 when sum(revenue_forecasted) over()=0
5655 then null
5656 else
5657 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
5658 case when sum(cost_forecasted) over() =0 then null
5659      when sum(total_actual_cost) over() =0 then null
5660      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
5661      else
5662     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
5663         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
5664       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
5665    end BIM_GRAND_TOTAL11
5666 FROM
5667 (
5668 SELECT
5669 VIEWBYID,
5670 viewby,
5671 leaf_node_flag,
5672 sum(cost_actual) cost_actual,
5673 SUM(cost_forecasted) cost_forecasted,
5674 SUM(actual_revenue) actual_revenue ,
5675 SUM(revenue_forecasted) revenue_forecasted,
5676 SUM(total_actual_cost) total_actual_cost,
5677 SUM(total_actual_revenue) total_actual_revenue,
5678 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
5679 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
5680 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
5681 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
5682 case when sum(cost_forecasted) =0 then null
5683      when sum(total_actual_cost) =0 then null
5684      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
5685      else
5686     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
5687         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
5688       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
5689    end roi_variance
5690 FROM
5691 (
5692 SELECT /*+ORDERED*/
5693 p.id VIEWBYID,
5694 p.value  viewby,
5695 p.leaf_node_flag leaf_node_flag,
5696 sum('|| l_prog_cost1 ||l_curr_suffix1||') cost_actual,
5697 0 cost_forecasted,
5698 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
5699 0 revenue_forecasted,
5700 0 total_actual_cost,
5701 0 total_actual_revenue
5702 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a
5703       ,eni_denorm_hierarchies edh
5704             ,mtl_default_category_sets mdc
5705             ,(select e.id,e.value,e.leaf_node_flag
5706               from eni_item_vbh_nodes_v e
5707           where
5708               e.parent_id =:l_cat_id
5709               AND e.id = e.child_id
5710               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
5711       ) p ';
5712 
5713 /*IF l_admin_status = 'N' THEN
5714 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
5715 END IF;*/
5716 
5717 l_sqltext :=  l_sqltext ||
5718 ' WHERE a.time_id = cal.time_id
5722 AND edh.object_id = mdc.category_set_id
5719 AND  a.period_type_id = cal.period_type_id
5720 AND a.category_id = edh.child_id
5721 AND edh.object_type = ''CATEGORY_SET''
5723 AND mdc.functional_area_id = 11
5724 AND edh.dbi_flag = ''Y''
5725 AND edh.parent_id = p.id
5726 AND  a.source_code_id = :l_campaign_id ';
5727 /* IF l_admin_status = 'N' THEN
5728 l_sqltext := l_sqltext ||
5729 '
5730 AND a.source_code_id = ac.source_code_id
5731 AND ac.resource_id = :l_resource_id';
5732 ELSE
5733 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
5734 END IF;  */
5735 l_sqltext :=  l_sqltext ||
5736 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
5737 AND a.object_country = :l_country';
5738 l_sqltext :=  l_sqltext ||
5739 ' AND cal.report_date =&BIS_CURRENT_ASOF_DATE
5740 AND cal.calendar_id=-1
5741 GROUP BY p.id,p.value,p.leaf_node_flag
5742 UNION ALL
5743 SELECT /*+ORDERED*/
5744 p.id VIEWBYID,
5745 p.value  name,
5746 p.leaf_node_flag leaf_node_flag,
5747 0 cost_actual,
5748 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
5749 0 actual_revenue ,
5750 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
5751 sum('|| l_prog_cost2 ||l_curr_suffix1||') total_actual_cost,
5752 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue
5753 FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
5754     ,eni_denorm_hierarchies edh
5755             ,mtl_default_category_sets mdc
5756             ,(select e.id,e.value,e.leaf_node_flag
5757               from eni_item_vbh_nodes_v e
5758                where
5759               e.parent_id =:l_cat_id
5760               AND e.id = e.child_id
5761               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
5762       ) p ';
5763 
5764  /*IF l_admin_status = 'N' THEN
5765 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
5766 END IF;*/
5767 
5768 l_sqltext :=  l_sqltext ||
5769 ' WHERE a.time_id = cal.time_id
5770 AND  a.period_type_id = cal.period_type_id
5771 AND a.category_id = edh.child_id
5772 AND edh.object_type = ''CATEGORY_SET''
5773 AND edh.object_id = mdc.category_set_id
5774 AND mdc.functional_area_id = 11
5775 AND edh.dbi_flag = ''Y''
5776 AND edh.parent_id = p.id
5777 AND a.source_code_id = :l_campaign_id ';
5778 
5779 /*IF l_admin_status = 'N' THEN
5780 l_sqltext := l_sqltext ||
5781 '
5782 AND a.source_code_id = ac.source_code_id
5783 AND ac.resource_id = :l_resource_id';
5784 ELSE
5785 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
5786 END IF; */
5787 
5788 l_sqltext :=  l_sqltext ||
5789 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
5790 AND a.object_country = :l_country';
5791 l_sqltext :=  l_sqltext ||
5792 ' AND cal.report_date = trunc(sysdate)
5793 AND cal.calendar_id=-1
5794 GROUP BY p.id,p.value,p.leaf_node_flag
5795 /*** directly assigned to the category *************/
5796 UNION ALL
5797 SELECT /*+ORDERED*/
5798 p.id VIEWBYID,
5799 bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||'  viewby,
5800 ''Y'' leaf_node_flag ,
5801 sum('|| l_prog_cost1 ||l_curr_suffix1||') cost_actual,
5802 0 cost_forecasted,
5803 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
5804 0 revenue_forecasted,
5805 0 total_actual_cost,
5806 0 total_actual_revenue
5807 FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a
5808       ,(select e.id id,e.value value
5809                       from eni_item_vbh_nodes_v e
5810                       where e.parent_id =  :l_cat_id
5811                       AND e.parent_id = e.child_id
5812                       AND leaf_node_flag <> ''Y''
5813                       ) p ';
5814 
5815 /* IF l_admin_status = 'N' THEN
5816 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
5817 END IF; */
5818 
5819 l_sqltext :=  l_sqltext ||
5820 ' WHERE a.time_id = cal.time_id
5821   AND  a.period_type_id = cal.period_type_id
5822   AND a.category_id = p.id
5823   AND a.source_code_id = :l_campaign_id ';
5824 
5825 /*IF l_admin_status = 'N' THEN
5826 l_sqltext := l_sqltext ||
5827 '
5828 AND a.source_code_id = ac.source_code_id
5829 AND ac.resource_id = :l_resource_id';
5830 ELSE
5831 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
5832 END IF;
5833 */
5834 
5835 l_sqltext :=  l_sqltext ||
5836 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
5837   AND a.object_country = :l_country
5838   AND cal.report_date = &BIS_CURRENT_ASOF_DATE
5839   AND cal.calendar_id=-1
5840  GROUP BY p.id
5841 UNION ALL
5842 SELECT /*+ORDERED*/
5843 p.id VIEWBYID,
5844 bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||'  viewby,
5845 ''Y'' leaf_node_flag,
5846 0 cost_actual,
5847 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
5848 0 actual_revenue ,
5849 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
5850 sum('|| l_prog_cost2 ||l_curr_suffix1||') total_actual_cost,
5851 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue
5852 FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
5856                       AND e.parent_id = e.child_id
5853         ,(select e.id id,e.value value
5854                       from eni_item_vbh_nodes_v e
5855                       where e.parent_id =  :l_cat_id
5857                       AND leaf_node_flag <> ''Y''
5858                       ) p ';
5859 /*
5860 IF l_admin_status = 'N' THEN
5861 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
5862 END IF; */
5863 
5864 l_sqltext :=  l_sqltext ||
5865 ' WHERE a.time_id = cal.time_id
5866   AND  a.period_type_id = cal.period_type_id
5867   AND a.category_id = p.id
5868   AND  a.source_code_id = :l_campaign_id ';
5869 
5870 /* IF l_admin_status = 'N' THEN
5871 l_sqltext := l_sqltext ||
5872 '
5873 AND a.source_code_id = ac.source_code_id
5874 AND ac.resource_id = :l_resource_id';
5875 ELSE
5876 l_sqltext := l_sqltext ||' AND a.immediate_parent_id is null';
5877 END IF;
5878 */
5879 l_sqltext :=  l_sqltext ||
5880 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
5881   AND a.object_country = :l_country
5882   AND cal.report_date = trunc(sysdate)
5883   AND cal.calendar_id=-1
5884 GROUP BY p.id
5885 )GROUP BY VIEWBYID,viewby,leaf_node_flag
5886 )
5887 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
5888 &ORDER_BY_CLAUSE';
5889 END IF;
5890 /**********************************************************************************************************/
5891 /************** START OF VIEW BY IS MARKETING CHANNEL CAMPAIGN IS NOT NULL ***********/
5892 ELSIF (l_view_by ='MEDIA+MEDIA') THEN
5893 --l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
5894 
5895 /* l_inner:=   ',( SELECT DISTINCT a.source_code_id
5896                 FROM fii_time_rpt_struct_v cal,bim_i_source_codes b,BIM_I_CPB_METS_MV a';
5897 
5898  IF l_cat_id is not null then
5899   l_inner := l_inner||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
5900   end if;
5901 
5902 l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
5903               AND  a.period_type_id=cal.period_type_id
5904               AND  cal.calendar_id=-1
5905               AND  cal.report_date =&BIS_CURRENT_ASOF_DATE
5906 	      AND  BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
5907 	      AND  a.parent_denorm_type = b.object_type
5908               AND  a.parent_object_id = b.object_id
5909               AND  b.child_object_id = 0
5910               AND  b.source_code_id = :l_campaign_id
5911 	      AND  a.object_country = :l_country  ';
5912 
5913 IF l_cat_id is null then
5914 l_inner := l_inner ||' AND a.category_id = -9 ';
5915 else
5916   l_inner := l_inner ||
5917   ' AND a.category_id = edh.child_id
5918     AND edh.object_type = ''CATEGORY_SET''
5919     AND edh.object_id = mdcs.category_set_id
5920     AND mdcs.functional_area_id = 11
5921     AND edh.dbi_flag = ''Y''
5922     AND edh.parent_id = :l_cat_id ';
5923   end if;
5924 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr ';  */
5925 
5926 
5927 l_sqltext :=
5928 '
5929 SELECT
5930  VIEWBY,
5931  null VIEWBYID,
5932  null BIM_ATTRIBUTE2,
5933  cost_actual BIM_ATTRIBUTE3,
5934  actual_revenue BIM_ATTRIBUTE4,
5935  total_actual_cost BIM_ATTRIBUTE5,
5936  total_actual_revenue BIM_ATTRIBUTE6,
5937  total_roi BIM_ATTRIBUTE7,
5938  cost_forecasted BIM_ATTRIBUTE8,
5939  revenue_forecasted BIM_ATTRIBUTE9,
5940  forecast_roi BIM_ATTRIBUTE10,
5941  null BIM_ATTRIBUTE11,
5942  null BIM_ATTRIBUTE15,
5943  null BIM_URL1,
5944  NULL BIM_URL2,
5945  NULL BIM_URL3,
5946  cost_variance BIM_ATTRIBUTE16,
5947  rev_variance  BIM_ATTRIBUTE17,
5948  roi_variance     BIM_ATTRIBUTE18,
5949  sum(cost_actual) over() BIM_GRAND_TOTAL1,
5950  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
5951  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
5952  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
5953 case
5954 when sum(total_actual_cost) over()=0
5955 then null
5956 else
5957 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5, /*total roi*/
5958 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
5959 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
5960 case
5961 when sum(cost_forecasted) over()=0
5962 then null
5963 else
5964 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8,/* forecasted roi*/
5965 case when sum(cost_forecasted) over()=0
5966 then null
5967 else
5968 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9, /* cost variance*/
5969 case
5970 when sum(revenue_forecasted) over()=0
5971 then null
5972 else
5973 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
5974 case when sum(cost_forecasted) over() =0 then null
5975      when sum(total_actual_cost) over() =0 then null
5976      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
5977      else
5978     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
5979         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
5983 (
5980       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
5981    end BIM_GRAND_TOTAL11
5982 FROM
5984 SELECT
5985 viewby,
5986 sum(cost_actual) cost_actual,
5987 SUM(cost_forecasted) cost_forecasted,
5988 SUM(actual_revenue) actual_revenue ,
5989 SUM(revenue_forecasted) revenue_forecasted,
5990 SUM(total_actual_cost) total_actual_cost,
5991 SUM(total_actual_revenue) total_actual_revenue,
5992 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
5993 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
5994 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
5995 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
5996 case when sum(cost_forecasted) =0 then null
5997      when sum(total_actual_cost) =0 then null
5998      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
5999      else
6000     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
6001         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
6002       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
6003    end roi_variance
6004 FROM
6005 (
6006 SELECT
6007 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) viewby,
6008 sum('|| l_prog_cost1 ||l_curr_suffix1||') cost_actual,
6009 0 cost_forecasted,
6010 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
6011 0 revenue_forecasted,
6012 0 total_actual_cost,
6013 0 total_actual_revenue ';
6014 
6015 /* IF l_admin_status = 'N' THEN
6016 l_sqltext := l_sqltext ||' FROM bim_obj_chnl_mv a,bim_i_top_objects ac,  ';
6017 ELSE
6018 l_sqltext := l_sqltext ||' FROM bim_mkt_chnl_mv a, ';
6019 END IF;*/
6020 
6021 l_sqltext := l_sqltext ||
6022 ' FROM fii_time_rpt_struct_v cal,bim_i_cpb_chnl_mv a,bim_dimv_media d ';
6023 
6024  IF l_cat_id is not null then
6025   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
6026   end if;
6027 
6028 l_sqltext :=  l_sqltext ||
6029 ' WHERE a.time_id = cal.time_id
6030   AND  a.period_type_id = cal.period_type_id
6031   AND  a.object_country = :l_country';
6032 
6033 /* IF l_admin_status = 'N' THEN
6034 l_sqltext := l_sqltext ||
6035 '
6036   AND a.source_code_id = ac.source_code_id
6037   AND ac.resource_id = :l_resource_id';
6038 END IF;*/
6039 
6040 IF l_cat_id is null then
6041 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
6042 else
6043   l_sqltext := l_sqltext ||
6044  ' AND a.category_id = edh.child_id
6045    AND edh.object_type = ''CATEGORY_SET''
6046    AND edh.object_id = mdcs.category_set_id
6047    AND mdcs.functional_area_id = 11
6048    AND edh.dbi_flag = ''Y''
6049    AND edh.parent_id = :l_cat_id ';
6050   end if;
6051 
6052 l_sqltext :=  l_sqltext ||
6053 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
6054   AND  d.id (+)= a.activity_id
6055   AND cal.report_date = &BIS_CURRENT_ASOF_DATE
6056   AND cal.calendar_id=-1
6057   AND  a.source_code_id = :l_campaign_id
6058 GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
6059 UNION ALL
6060 SELECT
6061 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) viewby,
6062 0 cost_actual,
6063 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
6064 0 actual_revenue ,
6065 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
6066 sum('|| l_prog_cost2 ||l_curr_suffix1||') total_actual_cost,
6067 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue  ';
6068 
6069 /*
6070 IF l_admin_status = 'N' THEN
6071 l_sqltext := l_sqltext ||' FROM bim_obj_chnl_mv a,bim_i_top_objects ac,  ';
6072 ELSE
6073 l_sqltext := l_sqltext ||' FROM bim_mkt_chnl_mv a, ';
6074 END IF;*/
6075 
6076 l_sqltext := l_sqltext ||
6077  ' FROM fii_time_rpt_struct_v cal,bim_obj_chnl_mv a,bim_dimv_media d';
6078 
6079  IF l_cat_id is not null then
6080   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
6081   end if;
6082 
6083 l_sqltext :=  l_sqltext ||
6084 ' WHERE a.time_id = cal.time_id
6085   AND  a.period_type_id = cal.period_type_id
6086   AND  a.object_country = :l_country';
6087 
6088 /*
6089 IF l_admin_status = 'N' THEN
6090 l_sqltext := l_sqltext ||
6091 '
6092 AND a.source_code_id = ac.source_code_id
6093 AND ac.resource_id = :l_resource_id';
6094 END IF;
6095 */
6096 
6097 IF l_cat_id is null then
6098 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
6099 else
6100   l_sqltext := l_sqltext ||
6101   ' AND a.category_id = edh.child_id
6102     AND edh.object_type = ''CATEGORY_SET''
6103     AND edh.object_id = mdcs.category_set_id
6104     AND mdcs.functional_area_id = 11
6105     AND edh.dbi_flag = ''Y''
6106     AND edh.parent_id = :l_cat_id ';
6107   end if;
6108   l_sqltext :=  l_sqltext ||
6109 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
6110 AND  d.id (+)= a.activity_id
6111 AND cal.report_date =  trunc(sysdate)
6112 AND cal.calendar_id=-1
6116 )
6113 AND  a.source_code_id = :l_campaign_id
6114 GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
6115 )GROUP BY viewby
6117 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
6118 &ORDER_BY_CLAUSE';
6119 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
6120 
6121 --l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
6122 
6123 /********* building inline view to filter thosedo not have any ptd cost or ptd revenue ***********/
6124 
6125 /* l_inner:=   ',( SELECT DISTINCT a.source_code_id
6126                 FROM fii_time_rpt_struct_v cal,bim_i_source_codes b,BIM_I_CPB_METS_MV a';
6127 
6128  IF l_cat_id is not null then
6129   l_inner := l_inner||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
6130   end if;
6131 
6132 l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
6133               AND a.period_type_id=cal.period_type_id
6134               AND cal.calendar_id=-1
6135               AND cal.report_date =&BIS_CURRENT_ASOF_DATE
6136 	      AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
6137 	      AND  a.parent_denorm_type = b.object_type
6138               AND  a.parent_object_id = b.object_id
6139               AND  b.child_object_id = 0
6140               AND  b.source_code_id = :l_campaign_id
6141 	      AND a.object_country = :l_country  ';
6142 
6143 
6144 IF l_cat_id is null then
6145 l_inner := l_inner ||' AND a.category_id = -9 ';
6146 else
6147   l_inner := l_inner ||
6148   ' AND a.category_id = edh.child_id
6149     AND edh.object_type = ''CATEGORY_SET''
6150     AND edh.object_id = mdcs.category_set_id
6151     AND mdcs.functional_area_id = 11
6152     AND edh.dbi_flag = ''Y''
6153     AND edh.parent_id = :l_cat_id ';
6154   end if;
6155 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr '; */
6156 
6157 
6158 l_sqltext :=
6159 '
6160 SELECT
6161  VIEWBY,
6162  null VIEWBYID,
6163  null BIM_ATTRIBUTE2,
6164  cost_actual BIM_ATTRIBUTE3,
6165  actual_revenue BIM_ATTRIBUTE4,
6166  total_actual_cost BIM_ATTRIBUTE5,
6167  total_actual_revenue BIM_ATTRIBUTE6,
6168  total_roi BIM_ATTRIBUTE7,
6169  cost_forecasted BIM_ATTRIBUTE8,
6170  revenue_forecasted BIM_ATTRIBUTE9,
6171  forecast_roi BIM_ATTRIBUTE10,
6172  null BIM_ATTRIBUTE11,
6173  null BIM_ATTRIBUTE15,
6174  null BIM_URL1,
6175  NULL BIM_URL2,
6176  NULL BIM_URL3,
6177  cost_variance BIM_ATTRIBUTE16,
6178  rev_variance  BIM_ATTRIBUTE17,
6179  roi_variance     BIM_ATTRIBUTE18,
6180  sum(cost_actual) over() BIM_GRAND_TOTAL1,
6181  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
6182  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
6183  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
6184 case
6185 when sum(total_actual_cost) over()=0
6186 then null
6187 else
6188 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5, /*total roi*/
6189 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
6190 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
6191 case
6192 when sum(cost_forecasted) over()=0
6193 then null
6194 else
6195 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8,/* forecasted roi*/
6196 case when sum(cost_forecasted) over()=0
6197 then null
6198 else
6199 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9, /* cost variance*/
6200 case
6201 when sum(revenue_forecasted) over()=0
6202 then null
6203 else
6204 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
6205 case when sum(cost_forecasted) over() =0 then null
6206      when sum(total_actual_cost) over() =0 then null
6207      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
6208      else
6209     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
6210         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
6211       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
6212    end BIM_GRAND_TOTAL11
6213 FROM
6214 (
6215 SELECT
6216 viewby,
6217 sum(cost_actual) cost_actual,
6218 SUM(cost_forecasted) cost_forecasted,
6219 SUM(actual_revenue) actual_revenue ,
6220 SUM(revenue_forecasted) revenue_forecasted,
6221 SUM(total_actual_cost) total_actual_cost,
6222 SUM(total_actual_revenue) total_actual_revenue,
6223 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
6224 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
6225 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
6226 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
6227 case when sum(cost_forecasted) =0 then null
6228      when sum(total_actual_cost) =0 then null
6229      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
6230      else
6231     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
6235 FROM (
6232         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
6233       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
6234    end roi_variance
6236 
6237 SELECT
6238 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) viewby,
6239 sum('|| l_prog_cost1 ||l_curr_suffix1||') cost_actual,
6240 0 cost_forecasted,
6241 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
6242 0 revenue_forecasted,
6243 0 total_actual_cost,
6244 0 total_actual_revenue ';
6245 
6246 l_sqltext := l_sqltext ||
6247 ' FROM bim_i_cpb_regn_mv a,
6248   fii_time_rpt_struct_v cal,
6249   bis_areas_v d ';
6250 
6251  IF l_cat_id is not null then
6252   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
6253   end if;
6254 
6255 l_sqltext :=  l_sqltext ||
6256 ' WHERE a.time_id = cal.time_id
6257   AND  a.period_type_id = cal.period_type_id
6258   AND  a.object_country = :l_country';
6259 
6260 IF l_cat_id is null then
6261 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
6262 else
6263   l_sqltext := l_sqltext ||
6264  ' AND a.category_id = edh.child_id
6265    AND edh.object_type = ''CATEGORY_SET''
6266    AND edh.object_id = mdcs.category_set_id
6267    AND mdcs.functional_area_id = 11
6268    AND edh.dbi_flag = ''Y''
6269    AND edh.parent_id = :l_cat_id ';
6270   end if;
6271 
6272 l_sqltext :=  l_sqltext ||
6273 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
6274   AND  d.id (+)= a.object_region
6275   AND cal.report_date = &BIS_CURRENT_ASOF_DATE
6276   AND cal.calendar_id=-1
6277   AND  a.source_code_id = :l_campaign_id
6278 GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
6279 UNION ALL
6280 SELECT
6281 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) viewby,
6282 0 cost_actual,
6283 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
6284 0 actual_revenue ,
6285 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
6286 sum('|| l_prog_cost2 ||l_curr_suffix1||') total_actual_cost,
6287 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue  ';
6288 
6289 l_sqltext := l_sqltext ||
6290  ' FROM fii_time_rpt_struct_v cal,bim_obj_regn_mv a,bis_areas_v d';
6291 
6292  IF l_cat_id is not null then
6293   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
6294   end if;
6295 
6296 l_sqltext :=  l_sqltext ||
6297 ' WHERE a.time_id = cal.time_id
6298   AND  a.period_type_id = cal.period_type_id
6299   AND  a.object_country = :l_country';
6300 
6301 IF l_cat_id is null then
6302 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
6303 else
6304   l_sqltext := l_sqltext ||
6305   ' AND a.category_id = edh.child_id
6306     AND edh.object_type = ''CATEGORY_SET''
6307     AND edh.object_id = mdcs.category_set_id
6308     AND mdcs.functional_area_id = 11
6309     AND edh.dbi_flag = ''Y''
6310     AND edh.parent_id = :l_cat_id ';
6311   end if;
6312   l_sqltext :=  l_sqltext ||
6313 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
6314 AND  d.id (+)= a.object_region
6315 AND cal.report_date =  trunc(sysdate)
6316 AND cal.calendar_id=-1
6317 AND  a.source_code_id = :l_campaign_id
6318 GROUP BY decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value)
6319 )GROUP BY viewby
6320 )
6321 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
6322 &ORDER_BY_CLAUSE';
6323 ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
6324 
6325 --l_una:= BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
6326 
6327 /********* building inline view to filter thosedo not have any ptd cost or ptd revenue ***********/
6328 
6329 /* l_inner:=   ',( SELECT DISTINCT a.source_code_id
6330                 FROM fii_time_rpt_struct_v cal,bim_i_source_codes b,BIM_I_CPB_METS_MV a';
6331 
6332  IF l_cat_id is not null then
6333   l_inner := l_inner||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
6334   end if; */
6335 
6336 /* IF l_admin_status = 'N' THEN
6337 l_inner := l_inner ||',bim_i_top_objects ac ';
6338 END IF; */
6339 
6340 /* l_inner:=l_inner||' WHERE  a.time_id=cal.time_id
6341               AND a.period_type_id=cal.period_type_id
6342               AND cal.calendar_id=-1
6343               AND cal.report_date =&BIS_CURRENT_ASOF_DATE
6344 	      AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
6345 	      AND  a.parent_denorm_type = b.object_type
6346               AND  a.parent_object_id = b.object_id
6347               AND  b.child_object_id = 0
6348               AND  b.source_code_id = :l_campaign_id
6349 	      AND a.object_country = :l_country  '; */
6350 
6351 /* IF l_admin_status = 'N' THEN
6352 l_inner := l_inner ||
6353 '
6354 AND a.source_code_id = ac.source_code_id
6355 AND ac.resource_id = :l_resource_id';
6356 ELSE
6357 l_inner := l_inner ||' AND a.immediate_parent_id is null';
6358 END IF;
6359 */
6360 
6361  /* IF l_cat_id is null then
6362 l_inner := l_inner ||' AND a.category_id = -9 ';
6363 else
6364   l_inner := l_inner ||
6365   ' AND a.category_id = edh.child_id
6366     AND edh.object_type = ''CATEGORY_SET''
6367     AND edh.object_id = mdcs.category_set_id
6371   end if;
6368     AND mdcs.functional_area_id = 11
6369     AND edh.dbi_flag = ''Y''
6370     AND edh.parent_id = :l_cat_id ';
6372 l_inner:=l_inner||' AND ( '||l_prog_cost1||' <>0 or '||l_prog_rev1||' <>0)) inr '; */
6373 
6374 l_sqltext :=
6375 '
6376  SELECT
6377  VIEWBY,
6378  VIEWBYID,
6379  null BIM_ATTRIBUTE2,
6380  cost_actual BIM_ATTRIBUTE3,
6381  actual_revenue BIM_ATTRIBUTE4,
6382  total_actual_cost BIM_ATTRIBUTE5,
6383  total_actual_revenue BIM_ATTRIBUTE6,
6384  total_roi BIM_ATTRIBUTE7,
6385  cost_forecasted BIM_ATTRIBUTE8,
6386  revenue_forecasted BIM_ATTRIBUTE9,
6387  forecast_roi BIM_ATTRIBUTE10,
6388  null BIM_ATTRIBUTE11,
6389  null BIM_ATTRIBUTE15,
6390  null BIM_URL1,
6391  NULL BIM_URL2,
6392  NULL BIM_URL3,
6393  cost_variance BIM_ATTRIBUTE16,
6394  rev_variance  BIM_ATTRIBUTE17,
6395  roi_variance     BIM_ATTRIBUTE18,
6396  sum(cost_actual) over() BIM_GRAND_TOTAL1,
6397  sum(actual_revenue) over() BIM_GRAND_TOTAL2,
6398  sum(total_actual_cost) over() BIM_GRAND_TOTAL3,
6399  sum(total_actual_revenue) over() BIM_GRAND_TOTAL4,
6400 case
6401 when sum(total_actual_cost) over()=0
6402 then null
6403 else
6404 ((sum(total_actual_revenue) over()-sum(total_actual_cost) over ()) /sum(total_actual_cost)over () )*100 end  BIM_GRAND_TOTAL5, /*total roi*/
6405 sum(cost_forecasted) over() BIM_GRAND_TOTAL6,
6406 sum(revenue_forecasted) over() BIM_GRAND_TOTAL7,
6407 case
6408 when sum(cost_forecasted) over()=0
6409 then null
6410 else
6411 ((sum(revenue_forecasted) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL8, /* forecasted roi*/
6412 case when sum(cost_forecasted) over()=0
6413 then null
6414 else
6415 ((sum(total_actual_cost) over() - sum(cost_forecasted) over ()) /sum(cost_forecasted) over () )*100 end  BIM_GRAND_TOTAL9, /* cost variance*/
6416 case
6417 when sum(revenue_forecasted) over()=0
6418 then null
6419 else
6420 ((sum(total_actual_revenue) over() - sum(revenue_forecasted) over ()) /sum(revenue_forecasted) over () )*100 end  BIM_GRAND_TOTAL10 ,
6421 case when sum(cost_forecasted) over() =0 then null
6422      when sum(total_actual_cost) over() =0 then null
6423      when sum(revenue_forecasted) over() - sum(cost_forecasted) over() =0 then null
6424      else
6425     ( ( ( ( sum(total_actual_revenue) over() - sum(total_actual_cost) over())/ sum(total_actual_cost) over()) -
6426         ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over()) )
6427       / ( ( sum(revenue_forecasted) over() - sum(cost_forecasted) over())/ sum(cost_forecasted) over())   )*100
6428    end BIM_GRAND_TOTAL11
6429 FROM
6430 (
6431 SELECT
6432 viewby,
6433 viewbyid,
6434 sum(cost_actual) cost_actual,
6435 SUM(cost_forecasted) cost_forecasted,
6436 SUM(actual_revenue) actual_revenue ,
6437 SUM(revenue_forecasted) revenue_forecasted,
6438 SUM(total_actual_cost) total_actual_cost,
6439 SUM(total_actual_revenue) total_actual_revenue,
6440 case when sum(cost_forecasted) = 0 then null else ((( sum(total_actual_cost)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end  cost_variance,
6441 case when sum(revenue_forecasted) = 0 then null else ((( sum(total_actual_revenue)-sum(revenue_forecasted) )/sum(revenue_forecasted)) *100 ) end rev_variance,
6442 case when sum(total_actual_cost) = 0 then null else ((( sum(total_actual_revenue)-sum(total_actual_cost) )/sum(total_actual_cost)) *100) end  total_roi,
6443 case when sum(cost_forecasted) = 0 then null else ((( sum(revenue_forecasted)-sum(cost_forecasted) )/sum(cost_forecasted)) *100 ) end forecast_roi,
6444 case when sum(cost_forecasted) =0 then null
6445      when sum(total_actual_cost) =0 then null
6446      when sum(revenue_forecasted)-sum(cost_forecasted) =0 then null
6447      else
6448     ( ( ( ( sum(total_actual_revenue)- sum(total_actual_cost))/sum(total_actual_cost) ) -
6449         ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted)) )
6450       / ( ( sum(revenue_forecasted)- sum(cost_forecasted))/ sum(cost_forecasted))   )*100
6451    end roi_variance
6452 FROM(
6453 SELECT
6454 decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) viewby,
6455 a.object_country viewbyid,
6456 sum('|| l_prog_cost1 ||l_curr_suffix1||') cost_actual,
6457 0 cost_forecasted,
6458 SUM('||l_prog_rev1||l_curr_suffix||') actual_revenue ,
6459 0 revenue_forecasted,
6460 0 total_actual_cost,
6461 0 total_actual_revenue
6462 FROM BIM_I_CPB_METS_MV a,fii_time_rpt_struct_v cal,fnd_territories_tl d ';
6463  IF l_cat_id is not null then
6464   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
6465   end if;
6466 
6467 /* IF l_admin_status = 'N' THEN
6468 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
6469 END IF;
6470 */
6471 
6472 l_sqltext :=  l_sqltext ||
6473 ' WHERE a.time_id = cal.time_id AND  a.period_type_id = cal.period_type_id';
6474 /*
6475 IF l_admin_status = 'N' THEN
6476 l_sqltext := l_sqltext ||
6477 '
6478 AND a.source_code_id = ac.source_code_id
6479 AND ac.resource_id = :l_resource_id';
6480 ELSE
6481 l_sqltext := l_sqltext ||
6482 ' AND  a.parent_object_id is null ';
6483 END IF;
6484 */
6485 IF l_cat_id is null then
6486 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
6487 else
6488   l_sqltext := l_sqltext ||
6489   ' AND a.category_id = edh.child_id AND edh.object_type = ''CATEGORY_SET''
6493   if l_country <>'N' then
6490    AND edh.object_id = mdcs.category_set_id  AND mdcs.functional_area_id = 11  AND edh.dbi_flag = ''Y''
6491    AND edh.parent_id = :l_cat_id ';
6492   end if;
6494   l_sqltext :=  l_sqltext || ' AND a.object_country = :l_country ';
6495   else
6496   l_sqltext :=  l_sqltext || ' AND a.object_country <>''N'' ';
6497   end if;
6498 l_sqltext :=  l_sqltext ||
6499 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
6500 AND  a.object_country =d.territory_code(+)
6501 AND d.language(+) = userenv(''LANG'')
6502 AND cal.report_date =&BIS_CURRENT_ASOF_DATE
6503 AND cal.calendar_id=-1
6504 AND  a.source_code_id = :l_campaign_id
6505 GROUP BY decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country
6506 UNION ALL
6507 SELECT
6508 decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) viewby,
6509 a.object_country viewbyid,
6510 0 cost_actual,
6511 SUM(cost_forecasted'||l_curr_suffix||') cost_forecasted,
6512 0 actual_revenue ,
6513 SUM(revenue_forecasted'||l_curr_suffix||') revenue_forecasted,
6514 sum('|| l_prog_cost2 ||l_curr_suffix1||') total_actual_cost,
6515 SUM('||l_prog_rev2||l_curr_suffix||') total_actual_revenue
6516 FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a,fnd_territories_tl d ';
6517 
6518  IF l_cat_id is not null then
6519   l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
6520   end if;
6521 /*
6522 IF l_admin_status = 'N' THEN
6523 l_sqltext := l_sqltext ||',bim_i_top_objects ac  ';
6524 END IF;
6525 */
6526 l_sqltext :=  l_sqltext ||
6527 ' WHERE a.time_id = cal.time_id
6528   AND  a.period_type_id = cal.period_type_id';
6529 
6530 /* IF l_admin_status = 'N' THEN
6531 l_sqltext := l_sqltext ||
6532 '
6533 AND a.source_code_id = ac.source_code_id
6534 AND ac.resource_id = :l_resource_id';
6535 ELSE
6536 l_sqltext := l_sqltext ||
6537 ' AND  a.parent_object_id is null ';
6538 END IF;*/
6539 IF l_cat_id is null then
6540 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
6541 else
6542   l_sqltext := l_sqltext ||
6543   ' AND a.category_id = edh.child_id
6544   AND edh.object_type = ''CATEGORY_SET''
6545   AND edh.object_id = mdcs.category_set_id
6546   AND mdcs.functional_area_id = 11
6547   AND edh.dbi_flag = ''Y''
6548   AND edh.parent_id = :l_cat_id ';
6549   end if;
6550   if l_country <>'N' then
6551   l_sqltext :=  l_sqltext || ' AND a.object_country = :l_country ';
6552   else
6553   l_sqltext :=  l_sqltext || ' AND a.object_country <>''N'' ';
6554   end if;
6555 l_sqltext :=  l_sqltext ||
6556 ' AND BITAND(cal.record_type_id,1143)= cal.record_type_id
6557 AND  a.object_country =d.territory_code(+)
6558 AND d.language(+) = userenv(''LANG'')
6559 AND cal.report_date = trunc(sysdate)
6560 AND cal.calendar_id=-1
6561 AND  a.source_code_id = :l_campaign_id
6562 GROUP BY decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country
6563 )GROUP BY viewby,viewbyid
6564 )
6565 WHERE cost_actual <>0 OR actual_revenue <>0 OR total_actual_cost <>0 OR total_actual_revenue <>0 OR cost_forecasted <>0 OR revenue_forecasted <>0
6566 &ORDER_BY_CLAUSE';
6567 END IF;
6568 END IF;
6569   x_custom_sql := l_sqltext;
6570   l_custom_rec.attribute_name := ':l_record_type';
6571   l_custom_rec.attribute_value := l_record_type_id;
6572   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6573   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6574   x_custom_output.EXTEND;
6575   x_custom_output(1) := l_custom_rec;
6576 
6577   l_custom_rec.attribute_name := ':l_resource_id';
6578   l_custom_rec.attribute_value := GET_RESOURCE_ID;
6579   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6580   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6581   x_custom_output.EXTEND;
6582   x_custom_output(2) := l_custom_rec;
6583 
6584   l_custom_rec.attribute_name := ':l_admin_flag';
6585   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
6586   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6587   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6588   x_custom_output.EXTEND;
6589   x_custom_output(3) := l_custom_rec;
6590 
6591   l_custom_rec.attribute_name := ':l_country';
6592   l_custom_rec.attribute_value := l_country;
6593   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6594   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6595   x_custom_output.EXTEND;
6596   x_custom_output(4) := l_custom_rec;
6597 
6598   l_custom_rec.attribute_name := ':l_cat_id';
6599   l_custom_rec.attribute_value := l_cat_id;
6600   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6601   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6602   x_custom_output.EXTEND;
6603   x_custom_output(5) := l_custom_rec;
6604 
6605   l_custom_rec.attribute_name := ':l_campaign_id';
6606   l_custom_rec.attribute_value := l_campaign_id;
6607   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6608   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6609   x_custom_output.EXTEND;
6610   x_custom_output(6) := l_custom_rec;
6611 
6612 
6613 
6614 write_debug('GET_CS_RACK_SQL','test','test',l_sqltext);
6615 --return l_sqltext;
6616 
6620 write_debug('GET_CS_RACK_SQL','ERROR',l_sql_errm,l_sqltext);
6617 EXCEPTION
6618 WHEN others THEN
6619 l_sql_errm := SQLERRM;
6621 END GET_CS_RACK_SQL;
6622 
6623 FUNCTION GET_BIM_TEST return VARCHAR2 IS
6624 l_sqltext varchar2(4000);
6625 
6626 BEGIN
6627 --l_sqltext := 'SELECT column1 BIM_ATTRIBUTE1,column2 BIM_ATTRIBUTE2 FROM BIM_TEST';
6628 l_sqltext := 'test';
6629 return l_sqltext;
6630 END GET_BIM_TEST;
6631 
6632 
6633 
6634 PROCEDURE GET_TOP_LEADS_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
6635                           x_custom_sql  OUT NOCOPY VARCHAR2,
6636                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
6637  IS
6638 l_sqltext varchar2(4000);
6639 iFlag number;
6640 l_period_type_hc number;
6641 l_as_of_date  DATE;
6642 l_period_type	varchar2(2000);
6643 l_record_type_id NUMBER;
6644 l_comp_type    varchar2(2000);
6645 l_country      varchar2(4000);
6646 l_view_by      varchar2(4000);
6647 l_sql_errm      varchar2(4000);
6648 l_previous_report_start_date DATE;
6649 l_current_report_start_date DATE;
6650 l_previous_as_of_date DATE;
6651 l_period_type_id NUMBER;
6652 l_user_id NUMBER;
6653 l_resource_id NUMBER;
6654 l_time_id_column  VARCHAR2(1000);
6655 l_admin_status VARCHAR2(20);
6656 l_admin_flag VARCHAR2(1);
6657 l_admin_count Number;
6658 l_rsid NUMBER;
6659 l_curr_aod_str varchar2(80);
6660 l_country_clause varchar2(4000);
6661 l_access_clause varchar2(4000);
6662 l_access_table varchar2(4000);
6663 --l_cat_id NUMBER;
6664 l_cat_id VARCHAR2(50);
6665 l_campaign_id VARCHAR2(50);
6666 l_custom_rec BIS_QUERY_ATTRIBUTES;
6667 l_curr VARCHAR2(50);
6668 l_curr_suffix VARCHAR2(50);
6669 l_col_id NUMBER;
6670 l_area VARCHAR2(300);
6671 l_report_name VARCHAR2(300);
6672 l_media VARCHAR2(300);
6673 
6674 BEGIN
6675 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
6676 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
6677 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
6678    			     			      l_as_of_date,
6679 			    			      l_period_type,
6680 				                      l_record_type_id,
6681 				                      l_comp_type,
6682 				                      l_country,
6683 						      l_view_by,
6684 						      l_cat_id,
6685 						      l_campaign_id,
6686 						      l_curr,
6687 						      l_col_id,
6688 						      l_area,
6689 						      l_media,
6690 						      l_report_name
6691 				                      );
6692 
6693    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
6694    IF l_country IS NULL THEN
6695       l_country := 'N';
6696    END IF;
6697 
6698    l_admin_status := GET_ADMIN_STATUS;
6699    /*IF l_admin_flag = 'N' THEN
6700       l_access_table := ',AMS_ACT_ACCESS_DENORM ac ';
6701       l_access_clause := 'AND a.object_type = ac.object_type '||
6702          'AND a.object_id = ac.object_id AND ac.resource_id = '||GET_RESOURCE_ID;
6703    ELSE
6704       l_access_table := '';
6705       l_access_clause := '';
6706    END IF;*/
6707 
6708 --if (l_view_by = 'CAMPAIGN+CAMPAIGN') then
6709 l_sqltext :=
6710 'SELECT campaign_name BIM_ATTRIBUTE1,
6711 camp_lead_count BIM_ATTRIBUTE2,
6712 event_name BIM_ATTRIBUTE3,
6713 even_lead_count BIM_ATTRIBUTE4
6714 FROM
6715 (
6716 select object_id, campaign_name,camp_lead_count,event_name,even_lead_count
6717 FROM
6718 ( SELECT
6719 a.object_id object_id,
6720 camp.campaign_name campaign_name,
6721 SUM(leads) camp_lead_count,
6722 null event_name,
6723 0 even_lead_count
6724 FROM BIM_I_OBJ_METS_MV a,
6725     fii_time_rpt_struct_v cal,
6726     ams_campaigns_all_tl camp
6727     ';
6728    IF l_cat_id is not null then
6729   l_sqltext := l_sqltext ||' ,ENI_ITEM_VBH_NODES_V e';
6730   end if;
6731 IF l_admin_status = 'N' THEN
6732 l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
6733 END IF;
6734 l_sqltext :=  l_sqltext ||
6735 ' WHERE a.time_id = cal.time_id
6736 AND  a.period_type_id = cal.period_type_id
6737 AND  a.object_type= ''CAMP''
6738 AND camp.campaign_id(+) = a.object_id';
6739 IF l_cat_id is null then
6740 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
6741 else
6742   l_sqltext := l_sqltext ||' AND e.parent_id = :l_cat_id
6743    AND a.category_id =  e.id ';
6744   end if;
6745 IF l_admin_status = 'N' THEN
6746 l_sqltext := l_sqltext ||
6747 ' AND a.object_type = ac.object_type
6748 AND a.object_id = ac.object_id
6749 AND ac.resource_id = :l_resource_id';
6750 END IF;
6751 l_sqltext :=  l_sqltext ||
6752 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
6753 AND a.object_country = :l_country
6754 AND cal.report_date = &BIS_CURRENT_ASOF_DATE
6755 AND cal.calendar_id=-1
6756 AND camp.language(+)=USERENV(''LANG'')
6757 GROUP BY a.object_id,
6758 camp.campaign_name
6759 having SUM(leads) > 0
6760 )
6761 where rownum<10
6762 
6763 UNION ALL
6764 select object_id, campaign_name,camp_lead_count,event_name,even_lead_count
6765 from
6766 (
6767 SELECT
6768 a.object_id object_id,
6769 null campaign_name,
6770 0 camp_lead_count,
6771 eve.event_header_name event_name,
6772 sum(a.leads) even_lead_count
6773 FROM BIM_I_OBJ_METS_MV a,
6774     fii_time_rpt_struct_v cal,
6775     ams_event_headers_all_tl eve
6779   end if;
6776     ';
6777     IF l_cat_id is not null then
6778   l_sqltext := l_sqltext ||' ,ENI_ITEM_VBH_NODES_V e';
6780 IF l_admin_status = 'N' THEN
6781 l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
6782 END IF;
6783 l_sqltext :=  l_sqltext ||
6784 ' WHERE a.time_id = cal.time_id
6785 AND  a.period_type_id = cal.period_type_id
6786 AND  a.object_type= ''EVEH''
6787 AND eve.event_header_id(+) = a.object_id';
6788 IF l_admin_status = 'N' THEN
6789 l_sqltext := l_sqltext ||
6790 ' AND a.object_type = ac.object_type
6791 AND a.object_id = ac.object_id
6792 AND ac.resource_id = :l_resource_id';
6793 END IF;
6794 IF l_cat_id is null then
6795 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
6796 else
6797   l_sqltext := l_sqltext ||' AND e.parent_id = :l_cat_id
6798    AND a.category_id =  e.id ';
6799   end if;
6800 l_sqltext :=  l_sqltext ||
6801 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
6802 AND a.object_country = :l_country
6803 AND cal.report_date = &BIS_CURRENT_ASOF_DATE
6804 AND cal.calendar_id=-1
6805 AND eve.language(+)=USERENV(''LANG'')
6806 GROUP BY a.object_id,
6807 eve.event_header_name
6808 having sum(leads)>0
6809 order by even_lead_count desc)
6810 where rownum<10
6811 ) a
6812 
6813 ';
6814 --end if;
6815 
6816 
6817   x_custom_sql := l_sqltext;
6818   l_custom_rec.attribute_name := ':l_record_type';
6819   l_custom_rec.attribute_value := l_record_type_id;
6820   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6821   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6822   x_custom_output.EXTEND;
6823   x_custom_output(1) := l_custom_rec;
6824 
6825   l_custom_rec.attribute_name := ':l_resource_id';
6826   l_custom_rec.attribute_value := GET_RESOURCE_ID;
6827   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6828   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6829   x_custom_output.EXTEND;
6830   x_custom_output(2) := l_custom_rec;
6831 
6832   l_custom_rec.attribute_name := ':l_admin_flag';
6833   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
6834   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6835   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6836   x_custom_output.EXTEND;
6837   x_custom_output(3) := l_custom_rec;
6838 
6839   l_custom_rec.attribute_name := ':l_country';
6840   l_custom_rec.attribute_value := l_country;
6841   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6842   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6843   x_custom_output.EXTEND;
6844   x_custom_output(4) := l_custom_rec;
6845 
6846   l_custom_rec.attribute_name := ':l_cat_id';
6847   l_custom_rec.attribute_value := l_cat_id;
6848   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
6849   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
6850   x_custom_output.EXTEND;
6851   x_custom_output(5) := l_custom_rec;
6852 
6853 write_debug('GET_TOP_LEADS_SQL','QUERY','_',l_sqltext);
6854 --return l_sqltext;
6855 
6856 EXCEPTION
6857 WHEN others THEN
6858 l_sql_errm := SQLERRM;
6859 write_debug('GET_TOP_LEADS_SQL','ERROR',l_sql_errm,l_sqltext);
6860 END GET_TOP_LEADS_SQL;
6861 
6862 PROCEDURE GET_TOP_OPPS_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
6863                           x_custom_sql  OUT NOCOPY VARCHAR2,
6864                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
6865  IS
6866 l_sqltext varchar2(4000);
6867 iFlag number;
6868 l_period_type_hc number;
6869 l_as_of_date  DATE;
6870 l_period_type	varchar2(2000);
6871 l_record_type_id NUMBER;
6872 l_comp_type    varchar2(2000);
6873 l_country      varchar2(4000);
6874 l_view_by      varchar2(4000);
6875 l_sql_errm      varchar2(4000);
6876 l_previous_report_start_date DATE;
6877 l_current_report_start_date DATE;
6878 l_previous_as_of_date DATE;
6879 l_period_type_id NUMBER;
6880 l_user_id NUMBER;
6881 l_resource_id NUMBER;
6882 l_time_id_column  VARCHAR2(1000);
6883 l_admin_status VARCHAR2(20);
6884 l_admin_flag VARCHAR2(1);
6885 l_admin_count Number;
6886 l_rsid NUMBER;
6887 l_curr_aod_str varchar2(80);
6888 l_country_clause varchar2(4000);
6889 l_access_clause varchar2(4000);
6890 l_access_table varchar2(4000);
6891 --l_cat_id NUMBER;
6892 l_cat_id VARCHAR2(50);
6893 l_campaign_id VARCHAR2(50);
6894 l_custom_rec BIS_QUERY_ATTRIBUTES;
6895 l_curr VARCHAR2(50);
6896 l_curr_suffix VARCHAR2(50);
6897 l_col_id NUMBER;
6898 l_area VARCHAR2(300);
6899 l_report_name VARCHAR2(300);
6900 l_media VARCHAR2(300);
6901 BEGIN
6902 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
6903 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
6904 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
6905    			     			      l_as_of_date,
6906 			    			      l_period_type,
6907 				                      l_record_type_id,
6908 				                      l_comp_type,
6909 				                      l_country,
6910 						      l_view_by,
6911 						      l_cat_id,
6912 						      l_campaign_id,
6913 						      l_curr,
6914 						      l_col_id,
6915 						      l_area,
6916 						      l_media,
6917 						      l_report_name
6918 				                      );
6919 
6923    END IF;
6920    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
6921    IF l_country IS NULL THEN
6922       l_country := 'N';
6924    l_admin_status := GET_ADMIN_STATUS;
6925    /*IF l_admin_flag = 'N' THEN
6926       l_access_table := ',AMS_ACT_ACCESS_DENORM ac ';
6927       l_access_clause := 'AND a.object_type = ac.object_type '||
6928          'AND a.object_id = ac.object_id AND ac.resource_id = '||GET_RESOURCE_ID;
6929    ELSE
6930       l_access_table := '';
6931       l_access_clause := '';
6932    END IF;*/
6933 
6934 --if (l_view_by = 'CAMPAIGN+CAMPAIGN') then
6935 l_sqltext :=
6936 'SELECT campaign_name BIM_ATTRIBUTE1,
6937 camp_opp_count BIM_ATTRIBUTE2,
6938 event_name BIM_ATTRIBUTE3,
6939 even_opp_count BIM_ATTRIBUTE4
6940 FROM
6941 (
6942 SELECT
6943 object_id,campaign_name,camp_opp_count,event_name,even_opp_count
6944 FROM
6945 ( SELECT
6946 a.object_id object_id,
6947 camp.campaign_name campaign_name,
6948 0 camp_opp_count,
6949 null event_name,
6950 0 even_opp_count
6951 FROM BIM_I_OBJ_METS_MV a,
6952     fii_time_rpt_struct_v cal,
6953     ams_campaigns_all_tl camp
6954     ';
6955        IF l_cat_id is not null then
6956   l_sqltext := l_sqltext ||' ,ENI_ITEM_VBH_NODES_V e';
6957   end if;
6958 IF l_admin_status = 'N' THEN
6959 l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
6960 END IF;
6961 l_sqltext :=  l_sqltext ||
6962 ' WHERE a.time_id = cal.time_id
6963 AND  a.period_type_id = cal.period_type_id
6964 AND  a.object_type= ''CAMP''
6965 AND camp.campaign_id(+) = a.object_id';
6966 IF l_admin_status = 'N' THEN
6967 l_sqltext := l_sqltext ||
6968 ' AND a.object_type = ac.object_type
6969 AND a.object_id = ac.object_id
6970 AND ac.resource_id = :l_resource_id';
6971 END IF;
6972 IF l_cat_id is null then
6973 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
6974 else
6975   l_sqltext := l_sqltext ||' AND e.parent_id = :l_cat_id
6976    AND a.category_id =  e.id ';
6977   end if;
6978 l_sqltext :=  l_sqltext ||
6979 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
6980 AND a.object_country = :l_country
6981 AND cal.report_date = &BIS_CURRENT_ASOF_DATE
6982 AND cal.calendar_id=-1
6983 AND camp.language(+)=USERENV(''LANG'')
6984 GROUP BY a.object_id,
6985 camp.campaign_name
6986 ) where rownum<10
6987 UNION ALL
6988 SELECT
6989 object_id,campaign_name,camp_opp_count,event_name,even_opp_count
6990 FROM
6991 (
6992 SELECT
6993 a.object_id object_id,
6994 null campaign_name,
6995 0 camp_opp_count,
6996 eve.event_header_name event_name,
6997 0 even_opp_count
6998 FROM BIM_I_OBJ_METS_MV a,
6999     fii_time_rpt_struct_v cal,
7000     ams_event_headers_all_tl eve
7001     ';
7002 IF l_cat_id is not null then
7003 l_sqltext := l_sqltext ||' ,ENI_ITEM_VBH_NODES_V e ';
7004 end if;
7005 IF l_admin_status = 'N' THEN
7006 l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
7007 END IF;
7008 l_sqltext :=  l_sqltext ||
7009 ' WHERE a.time_id = cal.time_id
7010 AND  a.period_type_id = cal.period_type_id
7011 AND  a.object_type= ''EVEH''
7012 AND eve.event_header_id(+) = a.object_id';
7013 IF l_admin_status = 'N' THEN
7014 l_sqltext := l_sqltext ||
7015 ' AND a.object_type = ac.object_type
7016 AND a.object_id = ac.object_id
7017 AND ac.resource_id = :l_resource_id';
7018 END IF;
7019 IF l_cat_id is null then
7020 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
7021 else
7022   l_sqltext := l_sqltext ||' AND e.parent_id = :l_cat_id
7026 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
7023    AND a.category_id =  e.id ';
7024   end if;
7025 l_sqltext :=  l_sqltext ||
7027 AND a.object_country = :l_country
7028 AND cal.report_date = &BIS_CURRENT_ASOF_DATE
7029 AND cal.calendar_id=-1
7030 AND eve.language(+)=USERENV(''LANG'')
7031 GROUP BY a.object_id,
7032 eve.event_header_name
7033 ) where rownum<10
7034 ) a
7035 ';
7036 --end if;
7037 
7038 
7039   x_custom_sql := l_sqltext;
7040   l_custom_rec.attribute_name := ':l_record_type';
7041   l_custom_rec.attribute_value := l_record_type_id;
7042   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7043   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7044   x_custom_output.EXTEND;
7045   x_custom_output(1) := l_custom_rec;
7046 
7047   l_custom_rec.attribute_name := ':l_resource_id';
7048   l_custom_rec.attribute_value := GET_RESOURCE_ID;
7049   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7050   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7051   x_custom_output.EXTEND;
7052   x_custom_output(2) := l_custom_rec;
7053 
7054   l_custom_rec.attribute_name := ':l_admin_flag';
7055   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
7056   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7057   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7058   x_custom_output.EXTEND;
7059   x_custom_output(3) := l_custom_rec;
7060 
7061   l_custom_rec.attribute_name := ':l_country';
7062   l_custom_rec.attribute_value := l_country;
7063   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7064   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7065   x_custom_output.EXTEND;
7066   x_custom_output(4) := l_custom_rec;
7067 
7068   l_custom_rec.attribute_name := ':l_cat_id';
7069   l_custom_rec.attribute_value := l_cat_id;
7070   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7071   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7072   x_custom_output.EXTEND;
7073   x_custom_output(5) := l_custom_rec;
7074 
7075 
7076 write_debug('GET_TOP_OPPS_SQL','QUERY','_',l_sqltext);
7077 --return l_sqltext;
7078 
7079 EXCEPTION
7080 WHEN others THEN
7081 l_sql_errm := SQLERRM;
7082 write_debug('GET_TOP_OPPS_SQL','ERROR',l_sql_errm,l_sqltext);
7083 
7084 END GET_TOP_OPPS_SQL;
7085 
7086 PROCEDURE GET_TOP_EVEH_OPPS_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
7087                           x_custom_sql  OUT NOCOPY VARCHAR2,
7088                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
7089  IS
7090 l_sqltext varchar2(4000);
7091 iFlag number;
7092 l_period_type_hc number;
7093 l_as_of_date  DATE;
7094 l_period_type	varchar2(2000);
7095 l_record_type_id NUMBER;
7096 l_comp_type    varchar2(2000);
7097 l_country      varchar2(4000);
7098 l_view_by      varchar2(4000);
7099 l_sql_errm      varchar2(4000);
7100 l_previous_report_start_date DATE;
7101 l_current_report_start_date DATE;
7105 l_resource_id NUMBER;
7102 l_previous_as_of_date DATE;
7103 l_period_type_id NUMBER;
7104 l_user_id NUMBER;
7106 l_time_id_column  VARCHAR2(1000);
7107 l_admin_status VARCHAR2(20);
7108 l_admin_flag VARCHAR2(1);
7109 l_admin_count Number;
7110 l_rsid NUMBER;
7111 l_curr_aod_str varchar2(80);
7112 l_country_clause varchar2(4000);
7113 l_access_clause varchar2(4000);
7114 l_access_table varchar2(4000);
7115 --l_cat_id NUMBER;
7116 l_cat_id VARCHAR2(50);
7117 l_campaign_id VARCHAR2(50);
7118 l_custom_rec BIS_QUERY_ATTRIBUTES;
7119 l_curr VARCHAR2(50);
7120 l_curr_suffix VARCHAR2(50);
7121 l_col_id NUMBER;
7122 l_area VARCHAR2(300);
7123 l_report_name VARCHAR2(300);
7124 l_media VARCHAR2(300);
7125 BEGIN
7126 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
7127 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
7128 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
7129    			     			      l_as_of_date,
7130 			    			      l_period_type,
7131 				                      l_record_type_id,
7132 				                      l_comp_type,
7133 				                      l_country,
7134 						      l_view_by,
7135 						      l_cat_id,
7136 						      l_campaign_id,
7137 						      l_curr,
7138 						      l_col_id,
7139 						      l_area,
7140 						      l_media,
7141 						      l_report_name
7142 				                      );
7143 IF (l_curr = '''FII_GLOBAL1''')
7144     THEN l_curr_suffix := '';
7145   ELSIF (l_curr = '''FII_GLOBAL2''')
7146     THEN l_curr_suffix := '_s';
7147     ELSE l_curr_suffix := '';
7148   END IF;
7149    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
7150    IF l_country IS NULL THEN
7151       l_country := 'N';
7152    END IF;
7153    l_admin_status := GET_ADMIN_STATUS;
7154    /*IF l_admin_flag = 'N' THEN
7155       l_access_table := ',AMS_ACT_ACCESS_DENORM ac ';
7156       l_access_clause := 'AND a.object_type = ac.object_type '||
7157          'AND a.object_id = ac.object_id AND ac.resource_id = '||GET_RESOURCE_ID;
7158    ELSE
7159       l_access_table := '';
7160       l_access_clause := '';
7161    END IF;*/
7162 
7163 --if (l_view_by = 'CAMPAIGN+CAMPAIGN') then
7164 l_sqltext :=
7165 'SELECT event_name BIM_ATTRIBUTE1,
7166 even_opp_amt BIM_ATTRIBUTE2
7167 FROM
7168 (
7169 SELECT
7170 event_name,even_opp_amt
7171 FROM
7172 (
7173 SELECT
7174 eve.name event_name,
7175 sum(nvl((won_opportunity_amt'||l_curr_suffix||'),0)) even_opp_amt
7176 FROM BIM_I_OBJ_METS_MV a,
7177     fii_time_rpt_struct_v cal,
7178     bim_i_obj_name_mv eve
7179     ';
7180 IF l_cat_id is not null then
7181 l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
7182 end if;
7183 IF l_admin_status = 'N' THEN
7184 l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
7185 END IF;
7186 l_sqltext :=  l_sqltext ||
7187 ' WHERE a.time_id = cal.time_id
7188 AND  a.period_type_id = cal.period_type_id
7189 AND  eve.object_type= ''EVEH''
7190 AND eve.source_code_id = a.source_code_id';
7191 
7192 IF l_admin_status = 'N' THEN
7193 l_sqltext := l_sqltext ||
7194 ' AND eve.object_type = ac.object_type
7195 AND eve.object_id = ac.object_id
7196 AND ac.resource_id = :l_resource_id';
7197 END IF;
7198 IF l_cat_id is null then
7199 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
7200 else
7201   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
7205 			     AND edh.dbi_flag = ''Y''
7202 			     AND edh.object_type = ''CATEGORY_SET''
7203 			     AND edh.object_id = mdcs.category_set_id
7204 			     AND mdcs.functional_area_id = 11
7206 			     AND edh.parent_id = :l_cat_id ';
7207   end if;
7208 l_sqltext :=  l_sqltext ||
7209 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
7210 AND a.object_country = :l_country
7211 AND cal.report_date = &BIS_CURRENT_ASOF_DATE
7212 AND cal.calendar_id=-1
7213 AND eve.language(+)=USERENV(''LANG'')
7214 GROUP BY eve.name
7215 order by even_opp_amt desc
7216 ) where rownum<10
7217 ) a
7218 ';
7219 --end if;
7220 
7221 
7222   x_custom_sql := l_sqltext;
7223   l_custom_rec.attribute_name := ':l_record_type';
7224   l_custom_rec.attribute_value := l_record_type_id;
7225   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7226   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7227   x_custom_output.EXTEND;
7228   x_custom_output(1) := l_custom_rec;
7229 
7230   l_custom_rec.attribute_name := ':l_resource_id';
7231   l_custom_rec.attribute_value := GET_RESOURCE_ID;
7232   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7233   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7234   x_custom_output.EXTEND;
7235   x_custom_output(2) := l_custom_rec;
7236 
7237   l_custom_rec.attribute_name := ':l_admin_flag';
7238   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
7239   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7240   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7241   x_custom_output.EXTEND;
7242   x_custom_output(3) := l_custom_rec;
7243 
7244   l_custom_rec.attribute_name := ':l_country';
7245   l_custom_rec.attribute_value := l_country;
7246   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7247   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7248   x_custom_output.EXTEND;
7249   x_custom_output(4) := l_custom_rec;
7250 
7251   l_custom_rec.attribute_name := ':l_cat_id';
7252   l_custom_rec.attribute_value := l_cat_id;
7253   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7254   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7255   x_custom_output.EXTEND;
7256   x_custom_output(5) := l_custom_rec;
7257 
7258 
7259 write_debug('GET_TOP_EVEH_OPPS_SQL','QUERY','_',l_sqltext);
7260 --return l_sqltext;
7261 
7262 EXCEPTION
7263 WHEN others THEN
7264 l_sql_errm := SQLERRM;
7265 write_debug('GET_TOP_EVEH_OPPS_SQL','ERROR',l_sql_errm,l_sqltext);
7266 
7267 END GET_TOP_EVEH_OPPS_SQL;
7268 
7269 PROCEDURE GET_TOP_CAMP_OPPS_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
7270                           x_custom_sql  OUT NOCOPY VARCHAR2,
7271                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
7272  IS
7273 l_sqltext varchar2(4000);
7274 iFlag number;
7275 l_period_type_hc number;
7276 l_as_of_date  DATE;
7277 l_period_type	varchar2(2000);
7278 l_record_type_id NUMBER;
7279 l_comp_type    varchar2(2000);
7280 l_country      varchar2(4000);
7281 l_view_by      varchar2(4000);
7282 l_sql_errm      varchar2(4000);
7283 l_previous_report_start_date DATE;
7284 l_current_report_start_date DATE;
7285 l_previous_as_of_date DATE;
7286 l_period_type_id NUMBER;
7287 l_user_id NUMBER;
7288 l_resource_id NUMBER;
7289 l_time_id_column  VARCHAR2(1000);
7290 l_admin_status VARCHAR2(20);
7291 l_admin_flag VARCHAR2(1);
7292 l_admin_count Number;
7293 l_rsid NUMBER;
7294 l_curr_aod_str varchar2(80);
7295 l_country_clause varchar2(4000);
7296 l_access_clause varchar2(4000);
7297 l_access_table varchar2(4000);
7298 --l_cat_id NUMBER;
7299 l_cat_id VARCHAR2(50);
7300 l_campaign_id VARCHAR2(50);
7301 l_custom_rec BIS_QUERY_ATTRIBUTES;
7302 l_curr VARCHAR2(50);
7303 l_curr_suffix VARCHAR2(50);
7304 l_col_id NUMBER;
7305 l_area VARCHAR2(300);
7306 l_report_name VARCHAR2(300);
7307 l_media VARCHAR2(300);
7308 BEGIN
7309 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
7310 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
7311 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
7312    			     			      l_as_of_date,
7313 			    			      l_period_type,
7314 				                      l_record_type_id,
7315 				                      l_comp_type,
7316 				                      l_country,
7317 						      l_view_by,
7318 						      l_cat_id,
7319 						      l_campaign_id,
7320 						      l_curr,
7321 						      l_col_id,
7322 						      l_area,
7323 						      l_media,
7324 						      l_report_name
7325 				                      );
7326   IF (l_curr = '''FII_GLOBAL1''')
7327     THEN l_curr_suffix := '';
7328   ELSIF (l_curr = '''FII_GLOBAL2''')
7332    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
7329     THEN l_curr_suffix := '_s';
7330     ELSE l_curr_suffix := '';
7331   END IF;
7333    IF l_country IS NULL THEN
7334       l_country := 'N';
7335    END IF;
7336    l_admin_status := GET_ADMIN_STATUS;
7337    /*IF l_admin_flag = 'N' THEN
7338       l_access_table := ',AMS_ACT_ACCESS_DENORM ac ';
7339       l_access_clause := 'AND a.object_type = ac.object_type '||
7340          'AND a.object_id = ac.object_id AND ac.resource_id = '||GET_RESOURCE_ID;
7341    ELSE
7342       l_access_table := '';
7343       l_access_clause := '';
7344    END IF;*/
7345 
7346 --if (l_view_by = 'CAMPAIGN+CAMPAIGN') then
7347 l_sqltext :=
7348 'SELECT campaign_name BIM_ATTRIBUTE1,
7349 camp_opp_amt BIM_ATTRIBUTE2
7350 FROM
7351 (
7352 SELECT
7353 campaign_name,camp_opp_amt
7354 FROM
7355 (
7356 SELECT
7357 camp.name campaign_name,
7358 sum(nvl((a.won_opportunity_amt'||l_curr_suffix||'),0)) camp_opp_amt
7359 FROM BIM_I_OBJ_METS_MV a,
7360     fii_time_rpt_struct_v cal,
7361     bim_i_obj_name_mv camp
7362     ';
7363 IF l_cat_id is not null then
7364 l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
7365 end if;
7366 IF l_admin_status = 'N' THEN
7367 l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
7368 END IF;
7369 l_sqltext :=  l_sqltext ||
7370 ' WHERE a.time_id = cal.time_id
7371 AND  a.period_type_id = cal.period_type_id
7372 AND  camp.object_type= ''CAMP''
7373 AND a.source_code_id = camp.source_code_id';
7374 
7375 IF l_admin_status = 'N' THEN
7376 l_sqltext := l_sqltext ||
7377 ' AND camp.object_type = ac.object_type
7378 AND camp.object_id = ac.object_id
7379 AND ac.resource_id = :l_resource_id';
7380 END IF;
7381 
7382 IF l_cat_id is null then
7383 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
7384 else
7385   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
7386 			   AND edh.object_type = ''CATEGORY_SET''
7387 			   AND edh.object_id = mdcs.category_set_id
7388 			   AND mdcs.functional_area_id = 11
7389 			   AND edh.dbi_flag = ''Y''
7390 			   AND edh.parent_id = :l_cat_id  ';
7391   end if;
7392 l_sqltext :=  l_sqltext ||
7393 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
7394 AND a.object_country = :l_country
7395 AND cal.report_date = &BIS_CURRENT_ASOF_DATE
7396 AND cal.calendar_id=-1
7397 AND camp.language =USERENV(''LANG'')
7398 GROUP BY camp.name
7399 order by camp_opp_amt desc
7400 ) where rownum<10
7401 ) a
7402 ';
7403 --end if;
7404 
7405 
7406   x_custom_sql := l_sqltext;
7407   l_custom_rec.attribute_name := ':l_record_type';
7408   l_custom_rec.attribute_value := l_record_type_id;
7409   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7410   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7411   x_custom_output.EXTEND;
7412   x_custom_output(1) := l_custom_rec;
7413 
7414   l_custom_rec.attribute_name := ':l_resource_id';
7415   l_custom_rec.attribute_value := GET_RESOURCE_ID;
7416   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7417   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7418   x_custom_output.EXTEND;
7419   x_custom_output(2) := l_custom_rec;
7420 
7421   l_custom_rec.attribute_name := ':l_admin_flag';
7422   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
7423   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7424   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7425   x_custom_output.EXTEND;
7426   x_custom_output(3) := l_custom_rec;
7427 
7428   l_custom_rec.attribute_name := ':l_country';
7429   l_custom_rec.attribute_value := l_country;
7430   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7431   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7432   x_custom_output.EXTEND;
7433   x_custom_output(4) := l_custom_rec;
7434 
7435   l_custom_rec.attribute_name := ':l_cat_id';
7436   l_custom_rec.attribute_value := l_cat_id;
7437   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7438   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7439   x_custom_output.EXTEND;
7440   x_custom_output(5) := l_custom_rec;
7441 
7442 
7443 write_debug('GET_TOP_CAMP_OPPS_SQL','QUERY','_',l_sqltext);
7444 --return l_sqltext;
7445 
7446 EXCEPTION
7447 WHEN others THEN
7448 l_sql_errm := SQLERRM;
7449 write_debug('GET_TOP_OPPS_SQL','ERROR',l_sql_errm,l_sqltext);
7450 
7451 END GET_TOP_CAMP_OPPS_SQL;
7452 
7453 PROCEDURE GET_TOP_EVEH_LEAD_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
7454                           x_custom_sql  OUT NOCOPY VARCHAR2,
7455                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
7456  IS
7457 l_sqltext varchar2(4000);
7458 iFlag number;
7459 l_period_type_hc number;
7460 l_as_of_date  DATE;
7461 l_period_type	varchar2(2000);
7462 l_record_type_id NUMBER;
7463 l_comp_type    varchar2(2000);
7464 l_country      varchar2(4000);
7465 l_view_by      varchar2(4000);
7466 l_sql_errm      varchar2(4000);
7467 l_previous_report_start_date DATE;
7468 l_current_report_start_date DATE;
7469 l_previous_as_of_date DATE;
7470 l_period_type_id NUMBER;
7471 l_user_id NUMBER;
7472 l_resource_id NUMBER;
7473 l_time_id_column  VARCHAR2(1000);
7477 l_rsid NUMBER;
7474 l_admin_status VARCHAR2(20);
7475 l_admin_flag VARCHAR2(1);
7476 l_admin_count Number;
7478 l_curr_aod_str varchar2(80);
7479 l_country_clause varchar2(4000);
7480 l_access_clause varchar2(4000);
7481 l_access_table varchar2(4000);
7482 --l_cat_id NUMBER;
7483 l_cat_id VARCHAR2(50);
7484 l_campaign_id VARCHAR2(50);
7485 l_custom_rec BIS_QUERY_ATTRIBUTES;
7486 l_curr VARCHAR2(50);
7487 l_curr_suffix VARCHAR2(50);
7488 l_col_id NUMBER;
7489 l_area VARCHAR2(300);
7490 l_report_name VARCHAR2(300);
7491 l_media VARCHAR2(300);
7492 BEGIN
7493 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
7494 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
7495 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
7496    			     			      l_as_of_date,
7497 			    			      l_period_type,
7498 				                      l_record_type_id,
7499 				                      l_comp_type,
7500 				                      l_country,
7501 						      l_view_by,
7502 						      l_cat_id,
7503 						      l_campaign_id,
7504 						      l_curr,
7505 						      l_col_id,
7506 						      l_area,
7507 						      l_media,
7508 						      l_report_name
7509 				                      );
7510 
7511    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
7512    IF l_country IS NULL THEN
7513       l_country := 'N';
7514    END IF;
7515    l_admin_status := GET_ADMIN_STATUS;
7516    /*IF l_admin_flag = 'N' THEN
7517       l_access_table := ',AMS_ACT_ACCESS_DENORM ac ';
7518       l_access_clause := 'AND a.object_type = ac.object_type '||
7519          'AND a.object_id = ac.object_id AND ac.resource_id = '||GET_RESOURCE_ID;
7520    ELSE
7521       l_access_table := '';
7522       l_access_clause := '';
7523    END IF;*/
7524 
7525 
7526 --if (l_view_by = 'CAMPAIGN+CAMPAIGN') then
7527 l_sqltext :=
7528 'SELECT event_name BIM_ATTRIBUTE1,
7529 even_lead_count BIM_ATTRIBUTE2
7530 FROM
7531 (
7532 SELECT
7533 event_name,even_lead_count
7534 FROM
7535 (
7536 SELECT
7537 eve.name event_name,
7538 sum(a.leads) even_lead_count
7539 FROM BIM_I_OBJ_METS_MV a,
7540     fii_time_rpt_struct_v cal,
7541     bim_i_obj_name_mv eve
7542     ';
7543 IF l_cat_id is not null then
7544 l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
7545 end if;
7546 IF l_admin_status = 'N' THEN
7547 l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
7548 END IF;
7549 l_sqltext :=  l_sqltext ||
7550 ' WHERE a.time_id = cal.time_id
7551 AND  a.period_type_id = cal.period_type_id
7552 AND a.source_code_id = eve.source_code_id
7553 AND eve.object_type = ''EVEH'' ';
7554 
7555 
7556 IF l_admin_status = 'N' THEN
7557 l_sqltext := l_sqltext ||
7558 ' AND eve.object_type = ac.object_type
7559 AND eve.object_id = ac.object_id
7560 AND ac.resource_id = :l_resource_id';
7561 END IF;
7562 IF l_cat_id is null then
7563 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
7564 else
7565   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
7566 			   AND edh.object_type = ''CATEGORY_SET''
7567 			   AND edh.object_id = mdcs.category_set_id
7568 			   AND mdcs.functional_area_id = 11
7569 			   AND edh.dbi_flag = ''Y''
7570 			   AND edh.parent_id = :l_cat_id ';
7571   end if;
7572 l_sqltext :=  l_sqltext ||
7573 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
7574 AND a.object_country = :l_country
7575 AND cal.report_date = &BIS_CURRENT_ASOF_DATE
7576 AND cal.calendar_id=-1
7577 AND eve.language(+)=USERENV(''LANG'')
7578 GROUP BY eve.name
7579 having sum(a.leads) >0
7580 order by even_lead_count desc
7581 ) where rownum<10
7582 ) a
7583 ';
7584 --end if;
7585 
7586 
7587   x_custom_sql := l_sqltext;
7588   l_custom_rec.attribute_name := ':l_record_type';
7589   l_custom_rec.attribute_value := l_record_type_id;
7590   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7591   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7592   x_custom_output.EXTEND;
7593   x_custom_output(1) := l_custom_rec;
7594 
7595   l_custom_rec.attribute_name := ':l_resource_id';
7596   l_custom_rec.attribute_value := GET_RESOURCE_ID;
7597   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7598   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7599   x_custom_output.EXTEND;
7600   x_custom_output(2) := l_custom_rec;
7601 
7602   l_custom_rec.attribute_name := ':l_admin_flag';
7603   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
7604   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7605   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7606   x_custom_output.EXTEND;
7607   x_custom_output(3) := l_custom_rec;
7608 
7609   l_custom_rec.attribute_name := ':l_country';
7610   l_custom_rec.attribute_value := l_country;
7611   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7612   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7613   x_custom_output.EXTEND;
7614   x_custom_output(4) := l_custom_rec;
7615 
7616   l_custom_rec.attribute_name := ':l_cat_id';
7617   l_custom_rec.attribute_value := l_cat_id;
7618   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7619   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7623 
7620   x_custom_output.EXTEND;
7621   x_custom_output(5) := l_custom_rec;
7622 
7624 write_debug('GET_TOP_EVEH_LEAD_SQL','QUERY','_',l_sqltext);
7625 --return l_sqltext;
7626 
7627 EXCEPTION
7628 WHEN others THEN
7629 l_sql_errm := SQLERRM;
7630 write_debug('GET_TOP_EVEH_LEAD_SQL','ERROR',l_sql_errm,l_sqltext);
7631 
7632 END GET_TOP_EVEH_LEAD_SQL;
7633 
7634 PROCEDURE GET_TOP_CAMP_LEAD_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
7635                           x_custom_sql  OUT NOCOPY VARCHAR2,
7636                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
7637  IS
7638 l_sqltext varchar2(4000);
7639 iFlag number;
7640 l_period_type_hc number;
7641 l_as_of_date  DATE;
7642 l_period_type	varchar2(2000);
7643 l_record_type_id NUMBER;
7644 l_comp_type    varchar2(2000);
7645 l_country      varchar2(4000);
7646 l_view_by      varchar2(4000);
7647 l_sql_errm      varchar2(4000);
7648 l_previous_report_start_date DATE;
7649 l_current_report_start_date DATE;
7650 l_previous_as_of_date DATE;
7651 l_period_type_id NUMBER;
7652 l_user_id NUMBER;
7653 l_resource_id NUMBER;
7654 l_time_id_column  VARCHAR2(1000);
7655 l_admin_status VARCHAR2(20);
7656 l_admin_flag VARCHAR2(1);
7657 l_admin_count Number;
7658 l_rsid NUMBER;
7659 l_curr_aod_str varchar2(80);
7660 l_country_clause varchar2(4000);
7661 l_access_clause varchar2(4000);
7662 l_access_table varchar2(4000);
7663 --l_cat_id NUMBER;
7664 l_cat_id VARCHAR2(50);
7665 l_campaign_id VARCHAR2(50);
7666 l_custom_rec BIS_QUERY_ATTRIBUTES;
7667 l_curr VARCHAR2(50);
7668 l_curr_suffix VARCHAR2(50);
7669 l_col_id NUMBER;
7670 l_area VARCHAR2(300);
7671 l_report_name VARCHAR2(300);
7672 l_media VARCHAR2(300);
7673 BEGIN
7674 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
7675 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
7676 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
7677    			     			      l_as_of_date,
7678 			    			      l_period_type,
7679 				                      l_record_type_id,
7680 				                      l_comp_type,
7681 				                      l_country,
7682 						      l_view_by,
7683 						      l_cat_id,
7684 						      l_campaign_id,
7685 						      l_curr,
7686 						      l_col_id,
7687 						      l_area,
7688 						      l_media,
7689 						      l_report_name
7690 				                      );
7691 
7692    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
7693    IF l_country IS NULL THEN
7694       l_country := 'N';
7695    END IF;
7696    l_admin_status := GET_ADMIN_STATUS;
7697    /*IF l_admin_flag = 'N' THEN
7698       l_access_table := ',AMS_ACT_ACCESS_DENORM ac ';
7699       l_access_clause := 'AND a.object_type = ac.object_type '||
7700          'AND a.object_id = ac.object_id AND ac.resource_id = '||GET_RESOURCE_ID;
7701    ELSE
7702       l_access_table := '';
7703       l_access_clause := '';
7704    END IF;*/
7705 
7706 --if (l_view_by = 'CAMPAIGN+CAMPAIGN') then
7707 l_sqltext :=
7708 'SELECT campaign_name BIM_ATTRIBUTE1,
7709 camp_lead_count BIM_ATTRIBUTE2
7710 FROM
7711 (
7712 SELECT
7713 campaign_name,camp_lead_count
7714 FROM
7715 (
7716 SELECT
7717 camp.name campaign_name,
7718 sum(a.leads) camp_lead_count
7719 FROM BIM_I_OBJ_METS_MV a,
7720     fii_time_rpt_struct_v cal,
7721     bim_i_obj_name_mv camp
7722     ';
7723 IF l_cat_id is not null then
7724 l_sqltext := l_sqltext ||' , eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
7725 end if;
7726 IF l_admin_status = 'N' THEN
7727 l_sqltext := l_sqltext ||',AMS_ACT_ACCESS_DENORM ac ';
7728 END IF;
7729 
7730 l_sqltext :=  l_sqltext ||
7731 ' WHERE a.time_id = cal.time_id
7732 AND  a.period_type_id = cal.period_type_id
7733 AND a.source_code_id = camp.source_code_id
7734 AND camp.object_type = ''CAMP'' ';
7735 
7736 IF l_admin_status = 'N' THEN
7737 l_sqltext := l_sqltext ||
7738 ' AND camp.object_type = ac.object_type
7739 AND camp.object_id = ac.object_id
7740 AND ac.resource_id = :l_resource_id';
7741 
7742 END IF;
7743 
7744 IF l_cat_id is null then
7745 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
7746 else
7747   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
7748 			   AND edh.object_type = ''CATEGORY_SET''
7749 			   AND edh.object_id = mdcs.category_set_id
7750 			   AND mdcs.functional_area_id = 11
7751 			   AND edh.dbi_flag = ''Y''
7752 			   AND edh.parent_id = :l_cat_id';
7753   end if;
7754 l_sqltext :=  l_sqltext ||
7755 ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
7756 AND a.object_country = :l_country
7757 AND cal.report_date = &BIS_CURRENT_ASOF_DATE
7758 AND cal.calendar_id=-1
7759 AND camp.language =USERENV(''LANG'')
7760 GROUP BY camp.name
7761 having sum(a.leads) >0
7762 order by camp_lead_count desc
7763 ) where rownum<10
7764 ) a
7765 ';
7766 --end if;
7767 
7768 
7769   x_custom_sql := l_sqltext;
7773   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7770   l_custom_rec.attribute_name := ':l_record_type';
7771   l_custom_rec.attribute_value := l_record_type_id;
7772   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7774   x_custom_output.EXTEND;
7775   x_custom_output(1) := l_custom_rec;
7776 
7777   l_custom_rec.attribute_name := ':l_resource_id';
7778   l_custom_rec.attribute_value := GET_RESOURCE_ID;
7779   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7780   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7781   x_custom_output.EXTEND;
7782   x_custom_output(2) := l_custom_rec;
7783 
7784   l_custom_rec.attribute_name := ':l_admin_flag';
7785   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
7786   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7787   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7788   x_custom_output.EXTEND;
7789   x_custom_output(3) := l_custom_rec;
7790 
7791   l_custom_rec.attribute_name := ':l_country';
7792   l_custom_rec.attribute_value := l_country;
7793   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7794   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7795   x_custom_output.EXTEND;
7796   x_custom_output(4) := l_custom_rec;
7797 
7798   l_custom_rec.attribute_name := ':l_cat_id';
7799   l_custom_rec.attribute_value := l_cat_id;
7800   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
7801   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
7802   x_custom_output.EXTEND;
7803   x_custom_output(5) := l_custom_rec;
7804 
7805 
7806 write_debug('GET_TOP_CAMP_OPPS_SQL','QUERY','_',l_sqltext);
7807 --return l_sqltext;
7808 
7809 EXCEPTION
7810 WHEN others THEN
7811 l_sql_errm := SQLERRM;
7812 write_debug('GET_TOP_OPPS_SQL','ERROR',l_sql_errm,l_sqltext);
7813 
7814 END GET_TOP_CAMP_LEAD_SQL;
7815 
7816 
7817 PROCEDURE GET_CPL_KPI(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
7818                           x_custom_sql  OUT NOCOPY VARCHAR2,
7819                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
7820 			  IS
7821 l_sqltext varchar2(5000);
7822 iFlag number;
7823 l_period_type_hc number;
7824 l_as_of_date  DATE;
7825 l_period_type	varchar2(2000);
7826 l_record_type_id NUMBER;
7827 l_comp_type    varchar2(2000);
7828 l_country      varchar2(4000);
7829 l_view_by      varchar2(4000);
7830 l_sql_errm      varchar2(4000);
7831 l_previous_report_start_date DATE;
7832 l_current_report_start_date DATE;
7833 l_previous_as_of_date DATE;
7834 l_period_type_id NUMBER;
7835 l_user_id NUMBER;
7836 l_resource_id NUMBER;
7837 l_time_id_column  VARCHAR2(1000);
7838 l_admin_status VARCHAR2(20);
7839 l_admin_flag VARCHAR2(1);
7840 l_admin_count Number;
7841 l_rsid NUMBER;
7842 l_prev_aod_str varchar2(80);
7843 l_curr_aod_str varchar2(80);
7844 l_country_clause varchar2(4000);
7845 l_admin_clause varchar2(4000);
7846 --l_cat_id NUMBER;
7847 l_campaign_id VARCHAR2(50);
7848 l_cat_id VARCHAR2(50);
7849 l_custom_rec BIS_QUERY_ATTRIBUTES;
7850 l_sql_outer  varchar2(5000);
7851 select1   varchar2(5000);
7852 select2    varchar2(5000);
7853 select3    varchar2(5000);
7854 l_sqltext_final    varchar2(32766);
7855 l_inner            varchar2(5000);
7856 l_sql_inception    varchar2(5000);
7857 l_inner_p           varchar2(5000);
7858 l_sql_inception_p    varchar2(5000);
7859 l_cost                         varchar2(50);
7860 l_top_cond  varchar2(100);
7861 l_top_cond_tot   varchar2(100);
7862 l_curr VARCHAR2(50);
7863 l_curr_suffix VARCHAR2(50);
7864 l_col_id NUMBER;
7865 l_area VARCHAR2(300);
7866 l_report_name VARCHAR2(300);
7867 l_media VARCHAR2(300);
7868 
7869 
7870 BEGIN
7871 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
7872 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
7873 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
7874    			     			      l_as_of_date,
7875 			    			      l_period_type,
7876 				                      l_record_type_id,
7877 				                      l_comp_type,
7878 				                      l_country,
7879 						      l_view_by,
7880 						      l_cat_id,
7881 						      l_campaign_id,
7882 						      l_curr,
7883 						      l_col_id,
7884 						      l_area,
7885 						      l_media,
7886 						      l_report_name
7887 				                      );
7888  l_sqltext :=' ';
7889   IF (l_curr = '''FII_GLOBAL1''')
7890     THEN l_curr_suffix := '';
7891   ELSIF (l_curr = '''FII_GLOBAL2''')
7892     THEN l_curr_suffix := '_s';
7893     ELSE l_curr_suffix := '';
7894   END IF;
7895    l_admin_status := GET_ADMIN_STATUS;
7896 
7897    /*l_previous_as_of_date := BIM_PMV_DBI_UTL_PKG.Previous_Period_Asof_Date(l_as_of_date, l_period_type, l_comp_type);
7898    l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
7899    l_prev_aod_str := 'to_date('||to_char(l_previous_as_of_date,'J')||',''J'')';*/
7900 
7901 
7902 
7903  IF l_prog_cost = 'BIM_ACTUAL_COST' THEN
7904       l_cost  :='actual_cost';
7905    ELSIF l_prog_cost = 'BIM_APPROVED_BUDGET' THEN
7906        l_cost  :='budget_approved';
7907    END IF;
7908 
7909 
7910    l_rsid := GET_RESOURCE_ID;
7911    IF l_country IS NULL THEN
7915 
7912       l_country := 'N';
7913    END IF;
7914 
7916 
7917 l_sql_outer := 'SELECT
7918 decode(decode('''|| l_cost_type ||''',''BIM_PTD_COST'',c_tlds,c_total_leads),0,null,c_tcost/decode('''|| l_cost_type ||''',''BIM_PTD_COST'',c_tlds,c_total_leads)) BIM_MEASURE7,
7919 decode(decode('''|| l_cost_type ||''',''BIM_PTD_COST'',p_tlds,p_total_leads),0,null,p_tcost/decode('''|| l_cost_type ||''',''BIM_PTD_COST'',p_tlds,p_total_leads)) BIM_MEASURE8,
7920 decode(decode('''|| l_cost_type ||''',''BIM_PTD_COST'',c_tlds,c_total_leads),0,null,c_tcost/decode('''|| l_cost_type ||''',''BIM_PTD_COST'',c_tlds,c_total_leads)) BIM_GRAND_TOTAL1,
7921 decode(decode('''|| l_cost_type ||''',''BIM_PTD_COST'',p_tlds,p_total_leads),0,null,p_tcost/decode('''|| l_cost_type ||''',''BIM_PTD_COST'',p_tlds,p_total_leads)) BIM_CGRAND_TOTAL1,
7922 c_tcost  BIM_MEASURE21,
7923 p_tcost BIM_MEASURE22,
7924 c_tcost  BIM_GRAND_TOTAL2,
7925 p_tcost BIM_CGRAND_TOTAL2
7926 FROM
7927 (
7928 SELECT
7929 
7930 
7931 case when '''|| l_prog_cost ||''' = ''BIM_APPROVED_BUDGET'' then
7932    case when '''|| l_cost_type ||''' = ''BIM_PTD_COST''  then
7933       SUM(p_tbapp)
7934      else
7935       sum(p_total_budget)
7936     end
7937   else
7938     case when '''|| l_cost_type ||''' = ''BIM_PTD_COST'' then
7939       SUM(p_tcost)
7940      else
7941       sum(p_total_cost)
7942      end
7943   end p_tcost,
7944 
7945 case when '''|| l_prog_cost ||''' = ''BIM_APPROVED_BUDGET'' then
7946    case when '''|| l_cost_type ||''' = ''BIM_PTD_COST''  then
7947       SUM(c_tbapp)
7948      else
7949       sum(c_total_budget)
7950     end
7951   else
7952     case when '''|| l_cost_type ||''' = ''BIM_PTD_COST'' then
7953       SUM(c_tcost)
7954      else
7955       sum(c_total_cost)
7956      end
7957   end c_tcost,
7958 
7959 sum(c_tlds) c_tlds,
7960 sum(p_tlds) p_tlds,
7961 sum(p_total_leads) p_total_leads,
7962 sum(c_total_leads) c_total_leads
7963 FROM
7964 (';
7965 
7966 select1:='
7967 SELECT
7968 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.actual_cost'||l_curr_suffix||' else 0 end) c_tcost,
7969 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.TOTAL_LEADS else 0 end) c_tlds,
7970 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.BUDGET_APPROVED'||l_curr_suffix||' else 0 end) c_tbapp,
7971 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.actual_cost'||l_curr_suffix||' else 0 end) p_tcost,
7972 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.TOTAL_LEADS else 0 end) p_tlds,
7973 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.BUDGET_APPROVED'||l_curr_suffix||' else 0 end) p_tbapp,
7974 0   c_total_cost,
7975 0   c_total_budget,
7976 0   c_total_leads,
7977 0   p_total_cost,
7978 0   p_total_budget,
7979 0   p_total_leads
7980 FROM  ';
7981 
7982 select2:='
7983 SELECT
7984 0 c_tcost,
7985 0 c_tlds,
7986 0 c_tbapp,
7987 0 p_tcost,
7988 0 p_tlds,
7989 0 p_tbapp,
7990 SUM( au.cost_actual'||l_curr_suffix||')       c_total_cost,
7991 SUM( au.BUDGET_APPROVED'||l_curr_suffix||')   c_total_budget,
7992 SUM( au.LEADS)             c_total_leads,
7993 0   p_total_cost,
7994 0   p_total_budget,
7995 0   p_total_leads
7996 FROM ';
7997 
7998 select3:='
7999 SELECT
8000 0 c_tcost,
8001 0 c_tlds,
8002 0 c_tbapp,
8003 0 p_tcost,
8004 0 p_tlds,
8005 0 p_tbapp,
8006 0   c_total_cost,
8007 0   c_total_budget,
8008 0   c_total_leads,
8009 SUM( au.cost_actual'||l_curr_suffix||')       p_total_cost,
8010 SUM( au.BUDGET_APPROVED'||l_curr_suffix||')   p_total_budget,
8011 SUM( au.LEADS)             p_total_leads
8012 FROM ';
8013 
8014 
8015 
8016 
8017    IF l_admin_status='N' THEN
8018       if l_prog_view='Y' then
8019             l_top_cond_tot :=' AND a.immediate_parent_id is null ';
8020             l_top_cond :=' AND au.immediate_parent_id is null ';
8021       else
8022             l_top_cond :=' and au.object_type in (''CAMP'',''EVEH'',''EONE'') ';
8023 	     l_top_cond_tot :=' and a.object_type in (''CAMP'',''EVEH'',''EONE'') ';
8024       end if;
8025    ELSE
8026            l_top_cond_tot :=' AND a.immediate_parent_id is null ';
8027 	   l_top_cond :=' AND au.immediate_parent_id is null ';
8028 
8029    END IF;
8030 
8031 
8032 /************Inner Query to get current acitve objects *************************/
8033 
8034 l_inner:='select distinct  a.object_id,a.object_type
8035 from BIM_I_CPB_METS_MV a
8036 ,fii_time_rpt_struct_v cal';
8037 
8038 IF l_admin_status='N' THEN
8039   if l_prog_view='Y' then
8040    l_inner:=l_inner||',bim_i_top_objects r ';
8041   else
8042   l_inner:=l_inner||',ams_act_access_denorm r ';
8043   end if;
8044 End if;
8045 
8046 
8047 IF l_cat_id is not null then
8048   l_inner := l_inner ||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
8049 end if;
8050 
8051  l_inner := l_inner || ' WHERE
8052  a.time_id=cal.time_id
8053 AND a.period_type_id=cal.period_type_id
8054 AND cal.calendar_id=-1
8055 AND cal.report_date  =&BIS_CURRENT_ASOF_DATE
8056 AND a.object_country = :l_country
8057 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
8058 and (a.'||l_cost||' <>0 or a.total_leads >0)'||l_top_cond_tot;
8059 
8060 IF l_admin_status = 'N' THEN
8064 IF l_cat_id is null then
8061   l_inner :=  l_inner||' AND a.object_type = r.object_type AND  a.object_id = r.object_id AND r.resource_id = :l_resource_id ';
8062 END IF;
8063 
8065  l_inner :=  l_inner ||' AND a.category_id = -9 ';
8066 else
8067 l_inner :=  l_inner ||' AND a.category_id = edh.child_id
8068 			   AND edh.object_type = ''CATEGORY_SET''
8069 			   AND edh.object_id = mdcs.category_set_id
8070 			   AND mdcs.functional_area_id = 11
8071 			   AND edh.dbi_flag = ''Y''
8072 			   AND edh.parent_id = :l_cat_id ';
8073   end if;
8074 
8075 
8076 
8077 /**********outer query to get current cost from Inception to date***************************/
8078 
8079 
8080 l_sql_inception:=' BIM_I_obj_METS_MV au,( '|| l_inner||') bu,fii_time_rpt_struct_v cu ';
8081 
8082 IF l_admin_status='N' THEN
8083        if l_prog_view='Y' then
8084              l_sql_inception:=l_sql_inception||', BIM_I_TOP_OBJECTS  ru ';
8085 	else
8086 	     l_sql_inception:=l_sql_inception||',ams_act_access_denorm   ru ';
8087 	 end if;
8088 end if;
8089 
8090 IF l_cat_id is not null then
8091   l_sql_inception := l_sql_inception ||', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
8092 end if;
8093 
8094 
8095  l_sql_inception := l_sql_inception || ' WHERE
8096  au.time_id=cu.time_id
8097 AND au.period_type_id=cu.period_type_id
8098 AND cu.calendar_id=-1
8099 AND cu.report_date = trunc(sysdate)
8100 AND au.object_country = :l_country
8101 AND BITAND(cu.record_type_id,1143)=cu.record_type_id
8102 and bu.object_id=au.object_id
8103 and bu.object_type=au.object_type
8104 '||l_top_cond;
8105 
8106 IF l_admin_status = 'N' THEN
8107   l_sql_inception :=  l_sql_inception||'
8108   AND au.object_type = ru.object_type AND  au.object_id = ru.object_id AND ru.resource_id = :l_resource_id ';
8109 END IF;
8110 
8111 IF l_cat_id is null then
8112  l_sql_inception :=  l_sql_inception ||' AND au.category_id = -9 ';
8113 else
8114   l_sql_inception :=  l_sql_inception ||' AND au.category_id = edh.child_id
8115 			   AND edh.object_type = ''CATEGORY_SET''
8116 			   AND edh.object_id = mdcs.category_set_id
8117 			   AND mdcs.functional_area_id = 11
8118 			   AND edh.dbi_flag = ''Y''
8119 			   AND edh.parent_id = :l_cat_id ';
8120   end if;
8121 
8122 
8123 /************************************************************************************/
8124 
8125 
8126 
8127 
8128 /************Inner Query to get previous acitve objects *************************/
8129 
8130 l_inner_p:='select distinct  a.object_id,a.object_type
8131 from BIM_I_CPB_METS_MV a
8132 ,fii_time_rpt_struct_v cal';
8133 
8134 IF l_admin_status='N' THEN
8135   if l_prog_view='Y' then
8136   l_inner_p:=l_inner_p||',bim_i_top_objects r ';
8137   else
8138   l_inner_p:=l_inner_p||',ams_act_access_denorm r ';
8139   end if;
8140 End if;
8141 
8142 IF l_cat_id is not null then
8143   l_inner_p := l_inner_p ||', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
8144 
8145 end if;
8146 
8147  l_inner_p := l_inner_p || ' WHERE
8148  a.time_id=cal.time_id
8149 AND a.period_type_id=cal.period_type_id
8150 AND cal.calendar_id=-1
8151 AND cal.report_date  =&BIS_PREVIOUS_ASOF_DATE
8152 AND a.object_country = :l_country
8153 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
8154 and (a.'||l_cost||' <>0 or a.total_leads >0)'||l_top_cond_tot;
8155 
8156 IF l_admin_status = 'N' THEN
8157   l_inner_p :=  l_inner_p||' AND a.object_type = r.object_type AND  a.object_id = r.object_id AND r.resource_id = :l_resource_id ';
8158 END IF;
8159 
8160 IF l_cat_id is null then
8161  l_inner_p :=  l_inner_p ||' AND a.category_id = -9 ';
8162 else
8163    l_inner_p :=  l_inner_p ||' AND a.category_id = edh.child_id
8164 			   AND edh.object_type = ''CATEGORY_SET''
8165 			   AND edh.object_id = mdcs.category_set_id
8166 			   AND mdcs.functional_area_id = 11
8167 			   AND edh.dbi_flag = ''Y''
8168 			   AND edh.parent_id = :l_cat_id ';
8169 
8170   end if;
8171 
8172 
8173 /**********outer query to get previous cost from Inception to date***************************/
8174 
8175 
8176 l_sql_inception_p:=' BIM_I_obj_METS_MV au,( '|| l_inner_p||') bu,fii_time_rpt_struct_v cu ';
8177 
8178 IF l_admin_status='N' THEN
8179   l_sql_inception_p:=l_sql_inception_p||', BIM_I_TOP_OBJECTS   ru ';
8180 end if;
8181 
8182 IF l_cat_id is not null then
8183   l_sql_inception_p := l_sql_inception_p ||', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
8184 end if;
8185 
8186 
8187  l_sql_inception_p := l_sql_inception_p || ' WHERE
8188  au.time_id=cu.time_id
8189 AND au.period_type_id=cu.period_type_id
8190 AND cu.calendar_id=-1
8191 AND cu.report_date  =   trunc(sysdate)
8192 AND au.object_country = :l_country
8193 AND BITAND(cu.record_type_id,1143)=cu.record_type_id
8194 and bu.object_id=au.object_id
8195 and bu.object_type=au.object_type
8196 '||l_top_cond;
8197 
8198 IF l_admin_status = 'N' THEN
8199   l_sql_inception_p :=  l_sql_inception_p||' AND au.object_type = ru.object_type AND  au.object_id = ru.object_id AND ru.resource_id = :l_resource_id ';
8200 END IF;
8201 
8202 IF l_cat_id is null then
8203  l_sql_inception_p :=  l_sql_inception_p ||' AND au.category_id = -9 ';
8204 else
8205   l_sql_inception_p :=  l_sql_inception_p ||' AND au.category_id = edh.child_id
8206 			   AND edh.object_type = ''CATEGORY_SET''
8210 			   AND edh.parent_id = :l_cat_id ';
8207 			   AND edh.object_id = mdcs.category_set_id
8208 			   AND mdcs.functional_area_id = 11
8209 			   AND edh.dbi_flag = ''Y''
8211 
8212   end if;
8213 
8214 
8215 /************************************************************************************/
8216 
8217 
8218 
8219 
8220 IF l_admin_status='N' THEN
8221 if l_prog_view='Y' then
8222   l_sqltext := l_sqltext ||'BIM_I_CPB_METS_MV  a,fii_time_rpt_struct_v cal, bim_i_top_objects  r ';
8223   else
8224  l_sqltext := l_sqltext ||'BIM_I_CPB_METS_MV  a,fii_time_rpt_struct_v cal,ams_act_access_denorm   r ';
8225 end if;
8226 ELSE
8227   l_sqltext := l_sqltext ||'BIM_I_MKT_CRPL_MV a,fii_time_rpt_struct_v cal';
8228 end if;
8229 
8230 
8231 
8232 
8233 
8234 
8235 IF l_cat_id is not null then
8236   l_sqltext := l_sqltext ||', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
8237 end if;
8238 
8239 
8240  l_sqltext := l_sqltext || ' WHERE a.time_id=cal.time_id
8241 AND a.period_type_id=cal.period_type_id
8242 AND cal.calendar_id=-1';
8243 
8244 
8245 
8246 
8247 
8248 
8249 IF l_admin_status = 'N' THEN
8250  l_sqltext := l_sqltext||' AND a.object_type = r.object_type AND  a.object_id = r.object_id AND r.resource_id = :l_resource_id '|| l_top_cond_tot ;
8251 END IF;
8252 
8253 
8254 
8255 IF l_cat_id is null then
8256 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
8257 else
8258   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
8259 			   AND edh.object_type = ''CATEGORY_SET''
8260 			   AND edh.object_id = mdcs.category_set_id
8261 			   AND mdcs.functional_area_id = 11
8262 			   AND edh.dbi_flag = ''Y''
8263 			   AND edh.parent_id = :l_cat_id ';
8264   end if;
8265 
8266 
8267 l_sqltext := l_sqltext ||
8268 ' AND a.object_country = :l_country ';
8269 
8270 l_sqltext_final := l_sql_outer||
8271                     select1||
8272 		    l_sqltext||
8273 		    ' AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id '||
8274 		    ' AND cal.report_date in(&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) ';
8275 
8276 		    if l_cost_type <> 'BIM_PTD_COST' then
8277 		     l_sqltext_final := l_sqltext_final ||
8278 		     ' UNION ALL ' ||
8279 		     select2||l_sql_inception||
8280 		      ' UNION ALL ' ||
8281 		     select3||l_sql_inception_p;
8282 		     end if;
8283 
8284 		l_sqltext_final := l_sqltext_final || ' ))' ;
8285 
8286 
8287   x_custom_sql := l_sqltext_final;
8288   l_custom_rec.attribute_name := ':l_record_type';
8289   l_custom_rec.attribute_value := l_record_type_id;
8290   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8291   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8292   x_custom_output.EXTEND;
8293   x_custom_output(1) := l_custom_rec;
8294 
8295   l_custom_rec.attribute_name := ':l_resource_id';
8296   l_custom_rec.attribute_value := GET_RESOURCE_ID;
8297   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8298   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8299   x_custom_output.EXTEND;
8300   x_custom_output(2) := l_custom_rec;
8301 
8302   l_custom_rec.attribute_name := ':l_admin_flag';
8303   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
8304   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8305   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8306   x_custom_output.EXTEND;
8307   x_custom_output(3) := l_custom_rec;
8308 
8309   l_custom_rec.attribute_name := ':l_country';
8310   l_custom_rec.attribute_value := l_country;
8311   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8312   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8313   x_custom_output.EXTEND;
8314   x_custom_output(4) := l_custom_rec;
8315 
8316    l_custom_rec.attribute_name := ':l_cat_id';
8317   l_custom_rec.attribute_value := l_cat_id;
8318   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8319   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8320   x_custom_output.EXTEND;
8321   x_custom_output(5) := l_custom_rec;
8322 
8323 write_debug('MKTG KPI SQL','QUERY','test',l_sqltext,NULL,null);
8324 EXCEPTION
8325 WHEN others THEN
8326 l_sql_errm := SQLERRM;
8327 write_debug('MKTG KPI SQL2','ERROR',l_sql_errm,l_sqltext,NULL,null);
8328 END GET_CPL_KPI;
8329 
8330 
8331 PROCEDURE GET_CPL_GRAPH_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
8332                           x_custom_sql  OUT NOCOPY VARCHAR2,
8333                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
8334 	  IS
8335 iFlag number;
8336 l_sqltext	      VARCHAR2(20000) ;
8337 l_as_of_date   DATE;
8338 l_period_type	varchar2(2000);
8339 l_record_type_id NUMBER;
8340 l_comp_type    varchar2(2000);
8341 l_country      varchar2(4000);
8342 l_view_by      varchar2(4000);
8343 l_sql_errm      varchar2(4000);
8344 l_previous_report_start_date DATE;
8345 l_current_report_start_date DATE;
8346 l_previous_as_of_date DATE;
8347 l_period_type_id NUMBER;
8348 l_user_id NUMBER;
8349 l_resource_id NUMBER;
8350 l_time_id_column  VARCHAR2(1000);
8351 l_admin_status VARCHAR2(20);
8352 l_admin_flag VARCHAR2(1);
8353 l_prev_aod_str varchar2(80);
8357 l_table_name varchar2(80);
8354 l_curr_aod_str varchar2(80);
8355 l_curr_start_date_str varchar2(80);
8356 l_prev_start_date_str varchar2(80);
8358 l_country_clause varchar2(4000);
8359 l_admin_clause varchar2(4000);
8360 l_series_name varchar2(4000);
8361 l_time_ids varchar2(4000);
8362 l_record_type varchar2(80) := NULL;
8363 --l_cat_id NUMBER;
8364 l_campaign_id VARCHAR2(50);
8365 l_cat_id VARCHAR2(50);
8366 l_custom_rec BIS_QUERY_ATTRIBUTES;
8367 l_group_by varchar2(500);
8368 l_group_by1 varchar2(500);
8369 l_curr VARCHAR2(50);
8370 l_curr_suffix VARCHAR2(50);
8371 l_col_id NUMBER;
8372 l_area VARCHAR2(300);
8373 l_report_name VARCHAR2(300);
8374 l_media VARCHAR2(300);
8375 BEGIN
8376 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
8377 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
8378 
8379 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
8380    			     			      l_as_of_date,
8381 			    			      l_period_type,
8382 				                      l_record_type_id,
8383 				                      l_comp_type,
8384 				                      l_country,
8385 						      l_view_by,
8386 						      l_cat_id,
8387 						      l_campaign_id,
8388 						      l_curr,
8389 						      l_col_id,
8390 						      l_area,
8391 						      l_media,
8392 						      l_report_name
8393 				                      );
8394 
8395 IF (l_curr = '''FII_GLOBAL1''')
8396     THEN l_curr_suffix := '';
8397   ELSIF (l_curr = '''FII_GLOBAL2''')
8398     THEN l_curr_suffix := '_s';
8399     ELSE l_curr_suffix := '';
8400   END IF;
8401 l_admin_status := GET_ADMIN_STATUS;
8402  --l_admin_status := 'Y';
8403 
8404 
8405    /*
8406    IF l_admin_status = 'N' THEN
8407       l_admin_clause := ' a.admin_flag = ''N'' AND a.resource_id = '||GET_RESOURCE_ID;
8408    ELSE
8409       l_admin_clause := ' a.admin_flag = ''Y''';
8410    END IF;
8411    */
8412 
8413    IF l_country IS NULL THEN
8414       l_country := 'N';
8415    END IF;
8416 
8417 /*
8418     IF l_cat_id is NULL and l_admin_status <> 'N' THEN
8419       l_cat_id := -9;
8420    END IF;
8421 
8422    */
8423 
8424 
8425   IF(l_as_of_date IS NULL)THEN
8426     l_as_of_date := sysdate;
8427   END IF;
8428 
8429   IF(l_comp_type IS NULL) THEN
8430      l_comp_type := 'YEARLY';
8431   END IF;
8432 
8433   IF l_record_type_id = 11 THEN l_period_type_id := 16;
8434   ELSIF l_record_type_id = 23 THEN l_period_type_id := 32;
8435   ELSIF l_record_type_id = 55 THEN l_period_type_id := 64;
8436   ELSIF l_record_type_id = 119 THEN l_period_type_id := 128;
8437   ELSE l_period_type_id := 64;
8438   END IF;
8439 
8440 BIM_PMV_DBI_UTL_PKG.GET_TREND_PARAMS(  p_page_period_type  => l_period_type,
8441                              p_comp_type         => l_comp_type,
8442                              p_curr_as_of_date   => l_as_of_date,
8443                              p_table_name        => l_table_name,
8444                              p_column_name       => l_time_id_column,
8445                              p_curr_start_date   => l_current_report_start_date,
8446                              p_prev_start_date   => l_previous_report_start_date,
8447                              p_prev_end_date     => l_previous_as_of_date,
8448 			     p_series_name       => l_series_name,
8449 			     p_time_ids          => l_time_ids
8450                              );
8451 
8452   l_group_by:='  group by a.time_id,fi.name,fi.sequence ,end_date,start_date';
8453 
8454   l_group_by1:=' group by fi.name ,a.time_id ';
8455 
8456 
8457    IF ( l_comp_type  = 'YEARLY' AND l_period_type <> 'FII_TIME_ENT_YEAR' )  THEN
8458 
8459 
8460 
8461 IF l_admin_status = 'N' THEN
8462   l_sqltext := 'SELECT fi.name VIEWBY, null BIM_ATTRIBUTE1,BIM_ATTRIBUTE2,BIM_ATTRIBUTE3,prev.BIM_ATTRIBUTE4,prev.BIM_ATTRIBUTE5
8463   FROM
8464    (
8465   SELECT name BIM_ATTRIBUTE1,curr.leads BIM_ATTRIBUTE2 ,decode ( curr.leads,0,null,(curr.costs/curr.leads)) BIM_ATTRIBUTE3,
8466   start_date, end_date, seq, time_id
8467   FROM  (
8468   SELECT a.time_id time_id, sum(total_leads) leads, fi.name name,fi.sequence seq,
8469   end_date,start_date, decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',
8470   sum(budget_approved'||l_curr_suffix||'),sum(actual_cost'||l_curr_suffix||')) costs ';
8471 else
8472   l_sqltext := 'SELECT fi.name VIEWBY, null BIM_ATTRIBUTE1,BIM_ATTRIBUTE2,BIM_ATTRIBUTE3,prev.BIM_ATTRIBUTE4,prev.BIM_ATTRIBUTE5
8473   FROM
8474    (
8475   SELECT name BIM_ATTRIBUTE1,curr.leads BIM_ATTRIBUTE2 ,decode ( curr.leads,0,null,(curr.costs/curr.leads)) BIM_ATTRIBUTE3,
8476   start_date, end_date, seq, time_id
8477   FROM  (   SELECT a.time_id time_id, total_leads leads, fi.name name,fi.sequence seq,
8478   end_date,start_date, decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET''
8479   ,budget_approved'||l_curr_suffix||',actual_cost'||l_curr_suffix||') costs ';
8480 end if;
8481 
8482 IF l_admin_status = 'N' THEN
8483 
8484   l_sqltext :=l_sqltext||' FROM BIM_I_CPB_METS_MV a,'||l_period_type||' fi , BIM_I_TOP_OBJECTS  r';
8485 
8486   IF l_cat_id is not null then
8487     l_sqltext :=l_sqltext||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
8488   end if;
8489 
8490   l_sqltext :=l_sqltext|| ' WHERE a.time_id = fi.'||l_time_id_column||'  AND a.period_type_id = :l_period_type_id
8491   AND a.source_code_id = r.source_code_id AND r.resource_id = :l_resource_id';
8492 
8496   else
8493   IF l_cat_id is not null then
8494      l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id   AND edh.object_type = ''CATEGORY_SET''   AND edh.object_id = mdcs.category_set_id
8495      AND mdcs.functional_area_id = 11   AND edh.dbi_flag = ''Y''   AND edh.parent_id = :l_cat_id ';
8497      l_sqltext :=l_sqltext||' AND a.category_id = -9 ';
8498   end if;
8499 
8500 else
8501    l_sqltext :=l_sqltext||' FROM BIM_I_MKT_CRPL_MV a,'||l_period_type||' fi';
8502 
8503     IF l_cat_id is not null then
8504     l_sqltext :=l_sqltext||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
8505   end if;
8506 
8507     l_sqltext :=l_sqltext||' WHERE a.time_id = fi.'||l_time_id_column||'   AND a.period_type_id = :l_period_type_id';
8508 
8509     IF l_cat_id is not null then
8510       l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id   AND edh.object_type = ''CATEGORY_SET''   AND edh.object_id = mdcs.category_set_id
8511      AND mdcs.functional_area_id = 11   AND edh.dbi_flag = ''Y''   AND edh.parent_id = :l_cat_id ';
8512     else
8513       l_sqltext :=l_sqltext||' and  a.category_id = -9';
8514     end if;
8515 
8516 end if;
8517 
8518 
8519 
8520 l_sqltext := l_sqltext||' AND fi.start_date BETWEEN &BIS_CURRENT_REPORT_START_DATE AND &BIS_CURRENT_ASOF_DATE
8521 AND fi.end_date<&BIS_CURRENT_ASOF_DATE AND a.object_country = :l_country ';
8522 
8523 IF l_admin_status = 'N' THEN
8524  l_sqltext := l_sqltext||l_group_by;
8525 end if;
8526 
8527 l_sqltext := l_sqltext||') curr   UNION ALL select name BIM_ATTRIBUTE1,total_leads BIM_ATTTRIBUTE2,decode ( total_leads,0,null,(total_cost)/(total_leads)) BIM_ATTRIBUTE3,
8528 start_date, end_date, seq,time_id from
8529 (  SELECT name ,total_leads,total_cost,start_date, end_date, fi.sequence seq, fi.time_id
8530 FROM (
8531 SELECT name,'||l_time_ids||' time_id, start_date, end_date, sequence, &BIS_CURRENT_ASOF_DATE  report_date
8532 FROM '||l_period_type||'
8533 WHERE start_date <= &BIS_CURRENT_ASOF_DATE  and end_date >= &BIS_CURRENT_ASOF_DATE
8534 ORDER BY start_date desc  ) fi,
8535 (SELECT SUM(a.total_leads) total_leads,
8536 decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',sum(a.budget_approved'||l_curr_suffix||'),sum(a.actual_cost'||l_curr_suffix||')) total_cost,
8537 report_date
8538 FROM   (SELECT report_date, time_id, period_type_id   FROM FII_TIME_RPT_STRUCT
8539 WHERE   calendar_id=-1   AND report_date = &BIS_CURRENT_ASOF_DATE   AND BITAND(record_type_id,:l_record_type) = record_type_id  ) c, ';
8540 
8541 IF l_admin_status='N' THEN
8542 
8543   l_sqltext :=l_sqltext||'BIM_I_CPB_METS_MV a, BIM_I_TOP_OBJECTS  r ';
8544 
8545   IF l_cat_id is not null then
8546     l_sqltext :=l_sqltext||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
8547   end if;
8548 
8549     l_sqltext :=l_sqltext||' WHERE a.time_id = c.time_id AND a.period_type_id = c.period_type_id AND a.object_country = :l_country
8550      AND a.source_code_id = r.source_code_id AND r.resource_id = :l_resource_id';
8551 
8552   IF l_cat_id is not null then
8553     l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id  AND edh.object_type = ''CATEGORY_SET'' AND edh.object_id = mdcs.category_set_id
8554     AND mdcs.functional_area_id = 11	AND edh.dbi_flag = ''Y'' AND edh.parent_id = :l_cat_id ';
8555   else
8556      l_sqltext :=l_sqltext||' AND a.category_id = -9 ';
8557   end if;
8558 
8559 ELSE
8560  l_sqltext :=l_sqltext||'  BIM_I_MKT_CRPL_MV a' ;
8561 
8562     IF l_cat_id is not null then
8563     l_sqltext :=l_sqltext||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
8564   end if;
8565 
8566     l_sqltext :=l_sqltext||' WHERE a.time_id = c.time_id AND a.period_type_id = c.period_type_id  AND a.object_country = :l_country';
8567 
8568     IF l_cat_id is not null then
8569       l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id   AND edh.object_type = ''CATEGORY_SET''   AND edh.object_id = mdcs.category_set_id
8570       AND mdcs.functional_area_id = 11   AND edh.dbi_flag = ''Y''   AND edh.parent_id = :l_cat_id ';
8571     else
8572       l_sqltext :=l_sqltext||' and  a.category_id = -9';
8573     end if;
8574 
8575 END IF;
8576 
8577 /*
8578 IF l_admin_status = 'N' THEN
8579 l_sqltext := l_sqltext||' AND a.admin_flag = :l_admin_flag AND a.resource_id = :l_resource_id ';
8580 ELSE
8581 l_sqltext := l_sqltext||' AND a.admin_flag = :l_admin_flag ';
8582 END IF;
8583 */
8584 
8585 IF l_admin_status='N' THEN
8586 
8587  l_sqltext := l_sqltext ||
8588  ' GROUP BY report_date) a  where a.report_date(+) = fi.report_date  order by start_date asc ) x   ) curr,
8589  (SELECT name BIM_ATTRIBUTE1,leads BIM_ATTRIBUTE4 ,  decode ( leads,0,null,(costs/leads)) BIM_ATTRIBUTE5 ,sequence seq
8590  FROM   ( SELECT fi.name name ,a.time_id time_id,'||l_time_ids||', sum(total_leads) leads,
8591  decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',sum(budget_approved'||l_curr_suffix||'),sum(actual_cost'||l_curr_suffix||')) costs,  fi.sequence sequence , end_date  ';
8592 
8593 else
8594 
8595   l_sqltext := l_sqltext ||
8596  ' GROUP BY report_date) a  where a.report_date(+) = fi.report_date  order by start_date asc ) x   ) curr,
8597   (SELECT name BIM_ATTRIBUTE1,leads BIM_ATTRIBUTE4 ,  decode ( leads,0,null,(costs/leads)) BIM_ATTRIBUTE5 ,sequence seq
8598    FROM   ( SELECT fi.name name ,a.time_id time_id,'||l_time_ids||', total_leads leads,
8599   decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',budget_approved'||l_curr_suffix||',actual_cost'||l_curr_suffix||') costs,  fi.sequence sequence , end_date  ';
8600 
8601 end if;
8602 
8603 
8604 IF l_admin_status='N' THEN
8605 
8609     l_sqltext :=l_sqltext||', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
8606    l_sqltext :=l_sqltext||' FROM BIM_I_CPB_METS_MV a,'||l_period_type||' fi, BIM_I_TOP_OBJECTS  r';
8607 
8608    IF l_cat_id is not null then
8610    end if;
8611 
8612     l_sqltext :=l_sqltext||' WHERE a.time_id = fi.'||l_time_id_column||' AND a.period_type_id = :l_period_type_id
8613     AND  a.source_code_id = r.source_code_id AND r.resource_id = :l_resource_id';
8614 
8615   IF l_cat_id is not null then
8616    l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id  AND edh.object_type = ''CATEGORY_SET''    AND edh.object_id = mdcs.category_set_id
8617    AND mdcs.functional_area_id = 11	AND edh.dbi_flag = ''Y''     AND edh.parent_id = :l_cat_id ';
8618   else
8619    l_sqltext :=l_sqltext||' AND a.category_id = -9 ';
8620   end if;
8621 ELSE
8622 
8623   l_sqltext :=l_sqltext||' FROM BIM_I_MKT_CRPL_MV a,'||l_period_type||' fi';
8624 
8625     IF l_cat_id is not null then
8626      l_sqltext :=l_sqltext||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
8627     end if;
8628 
8629     l_sqltext :=l_sqltext||' WHERE a.time_id = fi.'||l_time_id_column||'   AND a.period_type_id = :l_period_type_id';
8630 
8631     IF l_cat_id is not null then
8632       l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id   AND edh.object_type = ''CATEGORY_SET''   AND edh.object_id = mdcs.category_set_id
8633       AND mdcs.functional_area_id = 11   AND edh.dbi_flag = ''Y''   AND edh.parent_id = :l_cat_id ';
8634     else
8635       l_sqltext :=l_sqltext||' and  a.category_id = -9';
8636     end if;
8637 
8638 END IF;
8639 
8640 
8641 
8642 l_sqltext := l_sqltext ||
8643 ' AND fi.start_date BETWEEN &BIS_PREVIOUS_REPORT_START_DATE AND &BIS_PREVIOUS_ASOF_DATE  AND a.object_country = :l_country';
8644 
8645 IF l_admin_status = 'N' THEN
8646  l_sqltext := l_sqltext||l_group_by;
8647 end if;
8648 
8649 l_sqltext := l_sqltext||' ) k) prev,'||l_period_type||' fi WHERE fi.start_date BETWEEN &BIS_CURRENT_REPORT_START_DATE AND &BIS_CURRENT_ASOF_DATE
8650 AND fi.'||l_time_id_column||' = curr.time_id (+)  AND fi.sequence = curr.seq (+)   AND fi.sequence = prev.seq (+)   ORDER BY fi.start_date   ' ;
8651 
8652 
8653 
8654 ELSE  -- Sequential or entity year.
8655 
8656 
8657 IF l_admin_status = 'N' THEN
8658 
8659 l_sqltext := 'SELECT BIM_ATTRIBUTE1 VIEWBY,null BIM_ATTRIBUTE1,BIM_ATTRIBUTE2,BIM_ATTRIBUTE3,BIM_ATTRIBUTE4,BIM_ATTRIBUTE5 FROM
8660 (SELECT fi.name BIM_ATTRIBUTE1,  curr.leads BIM_ATTRIBUTE2, decode (curr.leads,0,null,(curr.costs/curr.leads)) BIM_ATTRIBUTE3,
8661 prev_leads BIM_ATTRIBUTE4,prev_cost BIM_ATTRIBUTE5,start_date  FROM ( SELECT fi.name name,a.time_id time_id, sum(total_leads) leads,
8662 decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',sum(budget_approved'||l_curr_suffix||'),sum(actual_cost'||l_curr_suffix||')) costs,  0 prev_leads,0 prev_cost ';
8663 
8664 else
8665 
8666 l_sqltext := 'SELECT BIM_ATTRIBUTE1 VIEWBY,null BIM_ATTRIBUTE1,BIM_ATTRIBUTE2,BIM_ATTRIBUTE3,BIM_ATTRIBUTE4,BIM_ATTRIBUTE5 FROM
8667 (SELECT fi.name BIM_ATTRIBUTE1,  curr.leads BIM_ATTRIBUTE2, decode (curr.leads,0,null,(curr.costs/curr.leads)) BIM_ATTRIBUTE3,
8668 prev_leads BIM_ATTRIBUTE4,prev_cost BIM_ATTRIBUTE5,start_date  FROM ( SELECT fi.name name,a.time_id time_id, total_leads leads,
8669 decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',budget_approved'||l_curr_suffix||',actual_cost'||l_curr_suffix||') costs,  0 prev_leads,0 prev_cost ';
8670 
8671 end if;
8672 
8673 
8674 
8675 
8676 IF l_admin_status = 'N' THEN
8677 
8678   l_sqltext :=l_sqltext||' FROM BIM_I_CPB_METS_MV a,'||l_period_type||' fi , BIM_I_TOP_OBJECTS  r';
8679 
8680   IF l_cat_id is not null then
8681     l_sqltext :=l_sqltext||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
8682   end if;
8683 
8684   l_sqltext :=l_sqltext||' WHERE a.time_id = fi.'||l_time_id_column||'
8685   AND a.period_type_id = :l_period_type_id      AND  a.source_code_id = r.source_code_id AND r.resource_id = :l_resource_id';
8686 
8687   IF l_cat_id is not null then
8688      l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id   AND edh.object_type = ''CATEGORY_SET''   AND edh.object_id = mdcs.category_set_id
8689      AND mdcs.functional_area_id = 11   AND edh.dbi_flag = ''Y''   AND edh.parent_id = :l_cat_id ';
8690   else
8691      l_sqltext :=l_sqltext||' AND a.category_id = -9 ';
8692   end if;
8693 
8694 else
8695 
8696  l_sqltext :=l_sqltext||' FROM BIM_I_MKT_CRPL_MV a,'||l_period_type||' fi';
8697 
8698     IF l_cat_id is not null then
8699     l_sqltext :=l_sqltext||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
8700     end if;
8701 
8702     l_sqltext :=l_sqltext||' WHERE a.time_id = fi.'||l_time_id_column||'   AND a.period_type_id = :l_period_type_id';
8703 
8704     IF l_cat_id is not null then
8705       l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id  AND edh.object_type = ''CATEGORY_SET''   AND edh.object_id = mdcs.category_set_id
8706      AND mdcs.functional_area_id = 11   AND edh.dbi_flag = ''Y''   AND edh.parent_id = :l_cat_id ';
8707     else
8708       l_sqltext :=l_sqltext||' and  a.category_id = -9';
8709     end if;
8710 
8711 end if;
8712 
8713 /*
8714 IF l_admin_status = 'N' THEN
8715 l_sqltext := l_sqltext||' AND a.admin_flag = :l_admin_flag AND a.resource_id = :l_resource_id ';
8716 ELSE
8717 l_sqltext := l_sqltext||' AND a.admin_flag = :l_admin_flag ';
8718 END IF;
8719 */
8720 
8721 l_sqltext := l_sqltext||
8725 IF l_admin_status = 'N' THEN
8722 ' AND fi.start_date BETWEEN &BIS_CURRENT_REPORT_START_DATE AND &BIS_CURRENT_ASOF_DATE
8723 AND fi.end_date<&BIS_CURRENT_ASOF_DATE  AND a.object_country =:l_country ';
8724 
8726   l_sqltext := l_sqltext||l_group_by1 ;
8727 end if;
8728 
8729 l_sqltext := l_sqltext||' ) curr, '||l_period_type||' fi WHERE fi.start_date BETWEEN &BIS_CURRENT_REPORT_START_DATE AND &BIS_CURRENT_ASOF_DATE
8730  AND fi.end_date<&BIS_CURRENT_ASOF_DATE  AND fi.'||l_time_id_column||' = curr.time_id (+)
8731 UNION ALL
8732 select name BIM_ATTRIBUTE1,total_leads BIM_ATTTRIBUTE2,  decode ( (total_leads),0,null,(total_cost)/(total_leads)) BIM_ATTRIBUTE3,
8733 0 prev_leads,0 prev_cost,start_date  from  (  SELECT name ,total_leads,total_cost, start_date, end_date, fi.sequence sequence, time_id
8734 FROM (SELECT name,'||l_time_ids||' time_id, start_date, end_date, sequence,  &BIS_CURRENT_ASOF_DATE  report_date
8735 FROM '||l_period_type||'  WHERE start_date <= &BIS_CURRENT_ASOF_DATE
8736 and end_date >= &BIS_CURRENT_ASOF_DATE   ORDER BY start_date  ) fi  , (SELECT SUM(a.total_leads) total_leads,
8737 decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',sum(budget_approved'||l_curr_suffix||'),sum(actual_cost'||l_curr_suffix||')) total_cost,
8738 report_date  FROM   (SELECT report_date, time_id, period_type_id   FROM FII_TIME_RPT_STRUCT
8739 WHERE   calendar_id=-1  AND report_date = &BIS_CURRENT_ASOF_DATE   AND BITAND(record_type_id,:l_record_type) = record_type_id  ) c, ';
8740 
8741 IF l_admin_status = 'N' THEN
8742 
8743 
8744   l_sqltext :=l_sqltext||'BIM_I_CPB_METS_MV a, BIM_I_TOP_OBJECTS  r ';
8745 
8746   IF l_cat_id is not null then
8747     l_sqltext :=l_sqltext||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
8748   end if;
8749 
8750     l_sqltext :=l_sqltext||' WHERE a.time_id = c.time_id AND a.period_type_id = c.period_type_id AND a.object_country = :l_country
8751     AND a.source_code_id = r.source_code_id AND r.resource_id = :l_resource_id';
8752 
8753   IF l_cat_id is not null then
8754     l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id  AND edh.object_type = ''CATEGORY_SET'' AND edh.object_id = mdcs.category_set_id
8755     AND mdcs.functional_area_id = 11	AND edh.dbi_flag = ''Y'' AND edh.parent_id = :l_cat_id ';
8756   else
8757      l_sqltext :=l_sqltext||' AND a.category_id = -9 ';
8758   end if;
8759 
8760 else
8761 
8762 
8763  l_sqltext :=l_sqltext||'  BIM_I_MKT_CRPL_MV a' ;
8764 
8765     IF l_cat_id is not null then
8766     l_sqltext :=l_sqltext||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs ';
8767   end if;
8768 
8769     l_sqltext :=l_sqltext||' WHERE a.time_id = c.time_id AND a.period_type_id = c.period_type_id  AND a.object_country = :l_country';
8770 
8771     IF l_cat_id is not null then
8772       l_sqltext :=l_sqltext||' AND a.category_id = edh.child_id   AND edh.object_type = ''CATEGORY_SET''   AND edh.object_id = mdcs.category_set_id
8773       AND mdcs.functional_area_id = 11   AND edh.dbi_flag = ''Y''   AND edh.parent_id = :l_cat_id ';
8774     else
8775       l_sqltext :=l_sqltext||' and  a.category_id = -9';
8776     end if;
8777 
8778 
8779 end if;
8780 
8781 /*
8782 IF l_admin_status = 'N' THEN
8783 l_sqltext := l_sqltext||' AND a.admin_flag = :l_admin_flag AND a.resource_id = :l_resource_id';
8784 ELSE
8785 l_sqltext := l_sqltext||' AND a.admin_flag = :l_admin_flag ';
8786 END IF;
8787 */
8788 
8789 l_sqltext := l_sqltext ||   ' GROUP BY report_date) a  where a.report_date(+) = fi.report_date  order by start_date  ) x  ) p order by p.start_date  ';
8790 END IF;
8791 
8792 
8793 
8794   x_custom_sql := l_sqltext;
8795   l_custom_rec.attribute_name := ':l_record_type';
8796   l_custom_rec.attribute_value := l_record_type_id;
8797   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8798   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8799   x_custom_output.EXTEND;
8800   x_custom_output(1) := l_custom_rec;
8801 
8802   l_custom_rec.attribute_name := ':l_resource_id';
8803   l_custom_rec.attribute_value := GET_RESOURCE_ID;
8804   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8805   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8806   x_custom_output.EXTEND;
8807   x_custom_output(2) := l_custom_rec;
8808 
8809   l_custom_rec.attribute_name := ':l_admin_flag';
8813   x_custom_output.EXTEND;
8810   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
8811   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8812   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8814   x_custom_output(3) := l_custom_rec;
8815 
8816   l_custom_rec.attribute_name := ':l_country';
8817   l_custom_rec.attribute_value := l_country;
8818   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8819   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8820   x_custom_output.EXTEND;
8821   x_custom_output(4) := l_custom_rec;
8822 
8823   l_custom_rec.attribute_name := ':l_period_type_id';
8824   l_custom_rec.attribute_value := l_period_type_id;
8825   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8826   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8827   x_custom_output.EXTEND;
8828   x_custom_output(5) := l_custom_rec;
8829 
8830   l_custom_rec.attribute_name := ':l_cat_id';
8831   l_custom_rec.attribute_value := l_cat_id;
8832   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
8833   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
8834   x_custom_output.EXTEND;
8835   x_custom_output(6) := l_custom_rec;
8836 
8837 
8838 write_debug('GET_CPL_GRAPH_SQL','QUERY','__',l_sqltext);
8839 EXCEPTION
8840 WHEN others THEN
8841 l_sql_errm := SQLERRM;
8842 write_debug('GET_CPL_GRAPH_SQL','ERROR',l_sql_errm,l_sqltext);
8843 
8844 END GET_CPL_GRAPH_SQL;
8845 
8846 PROCEDURE GET_CPL_RPL_SQL (
8847    p_page_parameter_tbl   IN              bis_pmv_page_parameter_tbl,
8848    x_custom_sql           OUT NOCOPY      VARCHAR2,
8849    x_custom_output        OUT NOCOPY      bis_query_attributes_tbl
8850 )
8851 IS
8852    l_sqltext                      VARCHAR2 (30000);
8853    iflag                          NUMBER;
8854    l_period_type_hc               NUMBER;
8855    l_as_of_date                   DATE;
8856    l_period_type                  VARCHAR2 (2000);
8857    l_record_type_id               NUMBER;
8858    l_comp_type                    VARCHAR2 (2000);
8859    l_country                      VARCHAR2 (4000);
8860    l_view_by                      VARCHAR2 (4000);
8861    l_sql_errm                     VARCHAR2 (4000);
8862    l_previous_report_start_date   DATE;
8863    l_current_report_start_date    DATE;
8864    l_previous_as_of_date          DATE;
8865    l_period_type_id               NUMBER;
8866    l_user_id                      NUMBER;
8867    l_resource_id                  NUMBER;
8868    l_time_id_column               VARCHAR2 (1000);
8869    l_admin_status                 VARCHAR2 (20);
8870    l_admin_flag                   VARCHAR2 (1);
8871    l_admin_count                  NUMBER;
8872    l_rsid                         NUMBER;
8873    l_curr_aod_str                 VARCHAR2 (80);
8874    l_country_clause               VARCHAR2 (4000);
8875    l_access_clause                VARCHAR2 (4000);
8876    l_access_table                 VARCHAR2 (4000);
8877    l_cat_id                       VARCHAR2 (50)        := NULL;
8878    l_campaign_id                  VARCHAR2 (50)        := NULL;
8879    l_select                       VARCHAR2 (20000); -- to build  inner select to pick data from mviews
8880    l_pc_select                    VARCHAR2 (20000); -- to build  inner select to pick data directly assigned to the product category hirerachy
8881    l_select_cal                   VARCHAR2 (20000); -- to build  select calculation part
8882    l_select_cal1                   VARCHAR2 (20000);
8883    l_select_filter                VARCHAR2 (20000); -- to build  select filter part
8884    l_select_filter_camp      VARCHAR2 (20000);
8885    l_from                         VARCHAR2 (20000);   -- assign common table in  clause
8886    l_from_inr                      VARCHAR2 (20000);
8887    l_from1                        VARCHAR2 (20000);-- vairable to get total cost from bim_i_obj_mets_mv
8888    l_where                        VARCHAR2 (20000);  -- static where clause
8889    l_where_inr                      VARCHAR2 (20000);
8890    l_groupby                      VARCHAR2 (2000);  -- to build  group by clause
8891    l_pc_from                      VARCHAR2 (20000);   -- from clause to handle product category
8892    l_pc_from1                     VARCHAR2 (20000);-- vairable to get total cost from bim_i_obj_mets_mv
8893    l_pc_where                     VARCHAR2 (20000);   --  where clause to handle product category
8894    l_filtercol                    VARCHAR2 (2000);
8895    l_pc_col                       VARCHAR2(200);
8896    l_pc_groupby                   VARCHAR2(200);
8897    l_view                         VARCHAR2 (20);
8898    l_select1                      VARCHAR2 (20000);
8899    l_select2                      VARCHAR2 (20000);
8900    l_view_disp                    VARCHAR2(100);
8901    l_url_str                      VARCHAR2(1000);
8902    l_url_str_csch                 varchar2(1000);
8903    l_url_str_csch_jtf             varchar2(3000);
8904    l_url_str_type                 varchar2(3000);
8905    l_camp_sel_col                 varchar2(100);
8906    l_camp_groupby_col             varchar2(100);
8907    l_csch_chnl                    varchar2(100);
8908    l_top_cond                     VARCHAR2(100);
8909    l_meaning                      VARCHAR2 (20);
8910    l_inner                        varchar2(5000);
8911    l_inr_cond                     varchar2(5000);
8912    l_cost                         varchar2(50);
8913    cpl                            varchar2(500);
8914    p_cpl                          varchar2(500);
8918    l_area VARCHAR2(300);
8915    l_union_inc                    varchar2(5000);
8916    l_col_inc                      varchar2(1000);
8917    l_col_id NUMBER;
8919    l_report_name VARCHAR2(300);
8920    l_curr VARCHAR2(50);
8921     l_curr_suffix VARCHAR2(50);
8922    /* variables to hold columns names in l_select clauses */
8923    l_col                          VARCHAR2(1000);
8924    /* cursor to get type of object passed from the page ******/
8925     cursor get_obj_type
8926     is
8927     select object_type
8928     from bim_i_source_codes
8929     where source_code_id=replace(l_campaign_id,'''');
8930     /*********************************************************/
8931    l_custom_rec                   bis_query_attributes;
8932    l_object_type                  varchar2(30);
8933    l_url_link                     varchar2(200);
8934    l_url_camp1                    varchar2(3000);
8935    l_url_camp2                    varchar2(3000);
8936    l_dass                         varchar2(100);  -- variable to store value for  directly assigned lookup value
8937    l_leaf_node_flag               varchar2(25);   -- variable to store value leaf_node_flag column in case of product category
8938    l_media VARCHAR2(300);
8939    l_curr_suffix1 VARCHAR2(50);
8940    l_table_bud VARCHAR2(300);
8941    l_where_bud VARCHAR2(300);
8942    l_prog_cost1 VARCHAR2(30);
8943 BEGIN
8944    x_custom_output := bis_query_attributes_tbl ();
8945    l_custom_rec := bis_pmv_parameters_pub.initialize_query_type;
8946    bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
8947    			     			      l_as_of_date,
8948 			    			      l_period_type,
8949 				                      l_record_type_id,
8950 				                      l_comp_type,
8951 				                      l_country,
8952 						      l_view_by,
8953 						      l_cat_id,
8954 						      l_campaign_id,
8955 						      l_curr,
8956 						      l_col_id,
8957 						      l_area,
8958 						      l_media,
8959 						      l_report_name
8960 				                      );
8961    l_meaning:=' null meaning '; -- assigning default value
8962    l_url_camp1:=',null';
8963    l_url_camp2:=',null';
8964 
8965 IF (l_curr = '''FII_GLOBAL1''')
8966     THEN l_curr_suffix := '';
8967   ELSIF (l_curr = '''FII_GLOBAL2''')
8968     THEN l_curr_suffix := '_s';
8969     ELSE l_curr_suffix := '';
8970   END IF;
8971    l_admin_status := get_admin_status;
8972 
8973    l_url_str :='pFunctionName=BIM_I_CRPL_PHP&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
8974 
8975     cpl:=' (decode(sum(BIM_ATTRIBUTE4) over(),0,0,sum(BIM_ATTRIBUTE2) over() /sum(BIM_ATTRIBUTE4) over() ))' ;
8976      p_cpl:=' (decode(sum(prev_ptd_leads) over() ,0,0,sum(prev_ptd_cost) over() /sum(prev_ptd_leads) over()))' ;
8977 
8978 
8979    IF l_country IS NULL THEN
8980       l_country := 'N';
8981    END IF;
8982 
8983 
8984    IF l_prog_cost = 'BIM_ACTUAL_COST' THEN
8985       l_cost  :='actual_cost';
8986    ELSIF l_prog_cost = 'BIM_APPROVED_BUDGET' THEN
8987        l_cost  :='budget_approved';
8988    END IF;
8989 
8990 
8991     /*********************** security handling for inner query ***********************/
8992 
8993 --change here
8994 /* IF  l_view_by <> 'CAMPAIGN+CAMPAIGN' then
8995      IF   l_campaign_id is null THEN
8996      IF   l_admin_status = 'N' THEN
8997 	     IF l_prog_view='Y' then
8998 	            l_from_inr :=', bim_i_top_objects  inr_r ';
8999 	     ELSE
9000 	         l_from_inr :=', ams_act_access_denorm  inr_r ';
9001            END IF;
9002                 l_where_inr := '  AND a.source_code_id = inr_r.source_code_id  AND inr_r.resource_id = :l_resource_id ';
9003 
9004 	ELSE
9005 	        l_where_inr   :=' AND a.immediate_parent_id is null ';
9006 
9007     END IF;
9008 
9009 
9010    END IF;
9011    END IF;*/
9012 
9013 /************************************************************************/
9014 
9015 
9016 
9017 /************Start Inner Query to get current acitve objects *************************/
9018 
9019 IF   l_campaign_id is null THEN
9020 
9021 if (l_view_by ='GEOGRAPHY+COUNTRY'  and l_cat_id is not null and l_admin_status = 'Y') then
9022  l_inner:='( select distinct  a.source_code_id  from fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a  '||l_from_inr;
9023 else
9024  l_inner:=',( select distinct  a.source_code_id  from fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a  '||l_from_inr;
9025 end if;
9026 
9027 IF l_cat_id is not null then
9028   l_inner := l_inner ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
9029 end if;
9030 
9031  l_inner := l_inner || ' WHERE   a.time_id=cal.time_id  AND a.period_type_id=cal.period_type_id  AND cal.calendar_id=-1   AND cal.report_date   in (&BIS_CURRENT_ASOF_DATE)
9032  AND a.object_country = :l_country  AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id  and (a.'||l_cost||' <>0 or a.total_leads >0)';
9033 
9034 IF l_cat_id is null then
9035  l_inner :=  l_inner ||' AND a.category_id = -9 ';
9036 else
9037 l_inner :=  l_inner ||' AND a.category_id = edh.child_id  AND edh.object_type = ''CATEGORY_SET''    AND edh.object_id = mdcs.category_set_id  AND mdcs.functional_area_id = 11
9038 			   AND edh.dbi_flag = ''Y''   AND edh.parent_id = :l_cat_id ';
9039   end if;
9040 
9041   l_inner := l_inner ||l_where_inr;
9042 
9043 l_inner :=  l_inner ||' ) inr ';
9044 
9045 
9046 
9050 
9047 l_inr_cond:='and a.source_code_id=inr.source_code_id ';
9048 
9049 end if;
9051 /************ End Inner Query to get current acitve objects *************************/
9052 
9053 
9054 /** to add meaning in select clause only in case of campaign view by */
9055   IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
9056   l_meaning:=' ,meaning ';
9057   l_filtercol:=',meaning ';
9058   ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
9059   l_filtercol:=',leaf_node_flag ';
9060   l_meaning:=',null meaning ';
9061   else
9062    l_meaning:=' ,null meaning ';
9063   end if;
9064  /*** to  assigned URL **/
9065 
9066 if l_campaign_id is not null then
9067 -- checking for the object type passed from page
9068  for i in get_obj_type
9069  loop
9070  l_object_type:=i.object_type;
9071  end loop;
9072 end if;
9073 
9074  -- l_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
9075    l_url_str  :='pFunctionName=BIM_I_CRPL_PHP&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
9076    --l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objId=';
9077    l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
9078    l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
9079    l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
9080    &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
9081    IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
9082        l_url_link :=' ,decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) ';
9083        l_view_disp:='viewby';
9084        l_leaf_node_flag :=' ,leaf_node_flag ';
9085    ELSIF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
9086      l_camp_sel_col :=' ,object_id
9087                        ,object_type
9088                        ';
9089      l_camp_groupby_col :=',object_id,object_type ';
9090      l_url_link := ' ,null ';
9091      l_view_disp := 'viewby';
9092 
9093      IF (l_campaign_id is  null or l_object_type='RCAM') then
9094 	l_url_camp1:=', decode(object_type,''EONE'',NULL,'||''''||l_url_str||''''||' )';
9095      ELSIF l_object_type='CAMP' THEN
9096         l_url_camp2:=', '||''''||l_url_str_type||''''||'||object_id ';
9097         --l_url_camp1:=', decode(usage,''LITE'','||''''||l_url_str_csch||''''||'||object_id,'||''''||l_url_str_csch_jtf||''''||'||object_id)';
9098 		l_url_camp1:=','''||l_url_str_csch||''''||'||object_id';
9099 	l_csch_chnl:='|| '' - '' || channel';
9100 	l_camp_sel_col :=l_camp_sel_col|| ',usage,channel';
9101 	l_camp_groupby_col :=l_camp_groupby_col||',usage,channel';
9102      end if;
9103     ELSE
9104      -- l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
9105       l_url_link:=' ,null ';
9106       l_view_disp:='viewby';
9107    END IF;
9108 
9109 
9110 
9111 /* l_select_cal is common part of select statement for campaign view by to calculate grand totals and change */
9112  l_select_cal :='select VIEWBY ,viewbyid,BIM_ATTRIBUTE7,bim_attribute2 ,BIM_ATTRIBUTE4,BIM_ATTRIBUTE3  ,BIM_ATTRIBUTE5 ,BIM_ATTRIBUTE6  ,BIM_ATTRIBUTE8  ,BIM_ATTRIBUTE9,BIM_ATTRIBUTE10
9113 ,bim_url1,bim_url2,bim_url3,BIM_GRAND_TOTAL1 , BIM_GRAND_TOTAL2,BIM_GRAND_TOTAL3 ,BIM_GRAND_TOTAL4 ,BIM_GRAND_TOTAL5 ,BIM_GRAND_TOTAL6 ,BIM_GRAND_TOTAL7 ,BIM_GRAND_TOTAL8
9114 from (  SELECT '||l_view_disp ||' ,viewbyid  ,BIM_ATTRIBUTE7 '||l_csch_chnl||' bim_attribute7 ,bim_attribute2  ,BIM_ATTRIBUTE4
9115 ,decode(prev_cpl,0,null,((BIM_ATTRIBUTE5-prev_cpl)/prev_cpl)*100) BIM_ATTRIBUTE3
9116 ,BIM_ATTRIBUTE5 ,BIM_ATTRIBUTE6  ,BIM_ATTRIBUTE8  ,BIM_ATTRIBUTE9
9117 ,BIM_ATTRIBUTE10 '|| l_url_link || ' bim_url1'|| l_url_camp1|| ' bim_url2 '||
9118 l_url_camp2||' bim_url3 ,BIM_GRAND_TOTAL1 ,decode('||p_cpl||',0,null,(('||cpl||' - '||p_cpl||')/ '||p_cpl||')*100 ) BIM_GRAND_TOTAL2
9119 ,BIM_GRAND_TOTAL3 ,BIM_GRAND_TOTAL4 ,BIM_GRAND_TOTAL5 ,BIM_GRAND_TOTAL6 ,BIM_GRAND_TOTAL7 ,BIM_GRAND_TOTAL8
9120  FROM (
9121 SELECT
9122  name    VIEWBY ,VIEWBYID  ,meaning BIM_ATTRIBUTE7 '||l_camp_sel_col||'
9123 ,ptd_cost BIM_ATTRIBUTE2 ,ptd_leads BIM_ATTRIBUTE4,decode(ptd_leads,0,null,ptd_cost/ptd_leads) BIM_ATTRIBUTE5
9124 ,decode(prev_ptd_leads,0,null,prev_ptd_cost/prev_ptd_leads) prev_cpl
9125 ,total_leads BIM_ATTRIBUTE6 ,total_cost BIM_ATTRIBUTE8  ,decode(total_leads,0,null,total_cost/total_leads) BIM_ATTRIBUTE9
9126 ,total_revenue BIM_ATTRIBUTE10  ,sum(ptd_cost) over() BIM_GRAND_TOTAL1 ,999 BIM_GRAND_TOTAL2 ,sum(ptd_leads) over() BIM_GRAND_TOTAL3
9127 ,decode(sum(ptd_leads) over(),0,null,sum(ptd_cost) over()/sum(ptd_leads) over()) BIM_GRAND_TOTAL4
9128 ,sum(total_leads) over() BIM_GRAND_TOTAL5 ,sum(total_cost) over() BIM_GRAND_TOTAL6
9129 ,decode(sum(total_leads) over(),0,null,sum(total_cost) over()/sum(total_leads) over()) BIM_GRAND_TOTAL7
9130  ,sum(total_revenue) over() BIM_GRAND_TOTAL8 ,prev_ptd_leads  ,prev_ptd_cost
9131            FROM
9132               (
9133                   SELECT
9134           	     viewbyid ,name'|| l_meaning ||l_camp_sel_col|| ',decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(budget_approved),SUM(ptd_cost)) ptd_cost
9135 		     ,SUM(ptd_leads) ptd_leads    ,decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(p_budget_approved),SUM(p_ptd_cost)) Prev_PTD_cost
9136 		     ,SUM(p_ptd_leads) Prev_PTD_leads
9140 		             or (SUM(ptd_leads)>0) ) then Sum(total_leads)  else 0 end total_leads   ,case when ((decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(budget_approved),SUM(ptd_cost))<>0)
9137 		     ,case when ( (decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(budget_approved),SUM(ptd_cost)) <> 0)
9138 		              or (SUM(ptd_leads) > 0)) then   decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(t_budget_approved),SUM(total_cost))
9139 		     else 0 end   total_cost ,case when ( (decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(budget_approved),SUM(ptd_cost)) <>0)
9141 		              or (SUM(ptd_leads)>0) ) then Sum(total_revenue) else 0 end total_revenue
9142                   FROM
9143           	  ( ';
9144 
9145 /*  change this  below query  */
9146 /* l_select_cal1 is common part of select statement for all view by except campaign view by to calculate grand totals and change */
9147 
9148 l_select_cal1 :='
9149          SELECT '||
9150          l_view_disp ||'
9151 	 ,viewbyid
9152  	  ,BIM_ATTRIBUTE7 ,BIM_ATTRIBUTE2 ,BIM_ATTRIBUTE4
9153  	  ,decode(prev_cpl,0,null,((BIM_ATTRIBUTE5-prev_cpl)/prev_cpl)*100) BIM_ATTRIBUTE3
9154  	  ,BIM_ATTRIBUTE5 ,BIM_ATTRIBUTE6,BIM_ATTRIBUTE8,BIM_ATTRIBUTE9
9155 	  ,BIM_ATTRIBUTE10 '||
9156 	  l_url_link|| ' bim_url1'||'
9157 	  ,null BIM_URL2 ,null BIM_URL3  ,BIM_GRAND_TOTAL1
9158  	  ,decode('||p_cpl||',0,null,(('||cpl||' - '||p_cpl||')/ '||p_cpl||')*100 ) BIM_GRAND_TOTAL2
9159  	  ,BIM_GRAND_TOTAL3  ,BIM_GRAND_TOTAL4 ,BIM_GRAND_TOTAL5 ,BIM_GRAND_TOTAL6
9160 	  ,BIM_GRAND_TOTAL7 ,BIM_GRAND_TOTAL8
9161           FROM
9162 	 (
9163             SELECT
9164             name    VIEWBY'||l_leaf_node_flag||'
9165 	    ,VIEWBYID,
9166             meaning BIM_ATTRIBUTE7,
9167 	     ptd_cost BIM_ATTRIBUTE2,
9168 	     ptd_leads BIM_ATTRIBUTE4,
9169              decode(ptd_leads,0,null,ptd_cost/ptd_leads) BIM_ATTRIBUTE5,
9170 	    decode(prev_ptd_leads,0,null,prev_ptd_cost/prev_ptd_leads) prev_cpl,
9171 	    total_leads BIM_ATTRIBUTE6,
9172 	    total_cost BIM_ATTRIBUTE8,
9173 	    decode(total_leads,0,null,total_cost/total_leads) BIM_ATTRIBUTE9,
9174             total_revenue BIM_ATTRIBUTE10,
9175 	    sum(ptd_cost) over() BIM_GRAND_TOTAL1,
9176 	    666 BIM_GRAND_TOTAL2,
9177             sum(ptd_leads) over() BIM_GRAND_TOTAL3,
9178             decode(sum(ptd_leads) over(),0,null,sum(ptd_cost) over()/sum(ptd_leads) over()) BIM_GRAND_TOTAL4,
9179             sum(total_leads) over() BIM_GRAND_TOTAL5,
9180             sum(total_cost) over() BIM_GRAND_TOTAL6,
9181             decode(sum(total_leads) over(),0,null,sum(total_cost) over()/sum(total_leads) over()) BIM_GRAND_TOTAL7,
9182             sum(total_revenue) over() BIM_GRAND_TOTAL8,
9183 	     prev_ptd_leads,
9184              prev_ptd_cost
9185            FROM
9186               (
9187                   SELECT
9188           	     viewbyid,
9189           	     name'||l_leaf_node_flag||
9190 		     l_meaning||
9191 		    ',decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(budget_approved),SUM(ptd_cost)) ptd_cost,
9192 		     SUM(ptd_leads) ptd_leads,
9193 		     decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(p_budget_approved),SUM(p_ptd_cost)) Prev_PTD_cost,
9194 		     SUM(p_ptd_leads) Prev_PTD_leads,
9195                      decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(t_budget_approved),SUM(total_cost)) total_cost,
9196                      Sum(total_leads) total_leads,
9197                      Sum(total_revenue) total_revenue
9198                   FROM
9199           	  ( ';
9200 
9201 l_curr_suffix1 :=l_curr_suffix;
9202 
9203 IF l_object_type in ('CAMP','EVEH','CSCH') AND l_prog_cost ='BIM_APPROVED_BUDGET' and l_view_by = 'CAMPAIGN+CAMPAIGN' THEN
9204 
9205 --l_table_bud :=  ' ,bim_i_marketing_facts facts';
9206 
9207 --l_where_bud := ' AND facts.source_code_id = a.source_code_id';
9208 IF l_curr_suffix is null THEN
9209 l_prog_cost1 := 'a.budget_approved_sch';
9210 ELSE
9211 l_curr_suffix1 := null;
9212 l_prog_cost1 := 'a.budget_approved_sch_s';
9213 END IF;
9214 ELSE
9215 l_prog_cost1 :='a.budget_approved';
9216 END IF;
9217 
9218 
9219 /* l_select1 and l_select2 contains column information common to all select statement for all view by */
9220 
9221 l_select1:=
9222 ' , SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.total_leads else 0 end) ptd_leads,
9223 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.actual_cost'||l_curr_suffix||' else 0 end) ptd_cost,
9224 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then '||l_prog_cost1||l_curr_suffix1||' else 0 end) budget_approved,
9225 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.total_leads else 0 end) p_ptd_leads,
9226 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.actual_cost'||l_curr_suffix||' else 0 end) p_ptd_cost,
9227 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then '||l_prog_cost1||l_curr_suffix1||' else 0 end) p_budget_approved,
9228 0 total_cost,0 t_budget_approved,0 total_leads,0 total_revenue ';
9229 
9230  l_select2 :=  ' ,0  ptd_leads,
9231 		0 ptd_cost,0 budget_approved,0  p_ptd_leads,0 p_ptd_cost,0  p_budget_approved,
9232 		SUM(a.cost_actual'||l_curr_suffix||') total_cost,
9233                 SUM('||l_prog_cost1||l_curr_suffix1||') t_budget_approved, SUM(a.leads)  total_leads,
9234 		SUM(case  '''|| l_revenue ||'''
9235                 when ''BOOKED_AMT'' then    a.orders_booked_amt'||l_curr_suffix||'
9236 		when ''INVOICED_AMT'' then  a.orders_invoiced_amt'||l_curr_suffix||'
9237                when ''WON_OPPR_AMT'' then  a.won_opportunity_amt'||l_curr_suffix||'
9238      end
9239      ) total_revenue ';
9240 
9241 
9245 
9242 /* l_from contains time dimension table common to all select statement for all view by */
9243  l_from  :=',fii_time_rpt_struct_v cal ';
9244 
9246  /* l_where contains where clause to join time dimension table common to all select statement for all view by */
9247 
9248  l_where :=' WHERE a.time_id = cal.time_id  AND  a.period_type_id = cal.period_type_id  AND  cal.calendar_id= -1 ';
9249 
9250 
9251 
9252  /* l_select_filter contains group by and filter clause to remove uneccessary records with zero values */
9253 
9254 l_select_filter := ' ) GROUP BY viewbyid,name '||l_filtercol||l_camp_groupby_col||
9255                   ')  )  WHERE  bim_attribute4 <> 0  or bim_attribute2 <> 0  or prev_ptd_leads <> 0 or prev_ptd_cost <> 0
9256 		  or BIM_ATTRIBUTE6 <> 0 or BIM_ATTRIBUTE8 <> 0 or BIM_ATTRIBUTE10<> 0 &ORDER_BY_CLAUSE ';
9257 
9258 
9259 
9260 
9261 
9262 l_select_filter_camp := ' ) GROUP BY viewbyid,name '||l_filtercol||l_camp_groupby_col||
9263                   ')  )  WHERE  bim_attribute4 <> 0  or bim_attribute2 <> 0  or prev_ptd_leads <> 0 or prev_ptd_cost <> 0
9264  or BIM_ATTRIBUTE6 <> 0 or BIM_ATTRIBUTE8 <> 0 or BIM_ATTRIBUTE10 <> 0) WHERE  bim_attribute4 <> 0  or bim_attribute2 <> 0
9265 		 or  BIM_ATTRIBUTE6 <> 0 or BIM_ATTRIBUTE8 <> 0 or BIM_ATTRIBUTE10 <> 0  &ORDER_BY_CLAUSE ';
9266 
9267  /* get_admin_status to check current user is admin or not */
9268 
9269 
9270   /*********************** security handling ***********************/
9271 /*********************** security handling ***********************/
9272 
9273 IF   l_campaign_id is null THEN /******* no security checking at child level ********/
9274 	IF   l_admin_status = 'N' THEN
9275 		IF  l_view_by = 'CAMPAIGN+CAMPAIGN' then
9276 		/*************** program view is enable **************/
9277 			IF l_prog_view='Y' then
9278 				l_view := ',''RCAM''';
9279 				l_from := l_from ||',bim_i_top_objects ac ';
9280 				l_where := l_where ||' AND a.source_code_id=ac.source_code_id
9281 							AND ac.resource_id = :l_resource_id ';
9282 				/************************************************/
9283 			ELSE
9284 
9285 				l_from := l_from ||',ams_act_access_denorm ac,bim_i_source_codes src ';
9286 				l_where := l_where ||' AND a.source_code_id=src.source_code_id
9287 						       AND src.object_id=ac.object_id
9288 						       AND src.object_type=ac.object_type
9289 						       AND src.object_type NOT IN (''RCAM'')
9290 						       AND ac.resource_id = :l_resource_id ';
9291 
9292 			END IF;
9293 
9294 		ELSE
9295 			l_from := l_from ||',bim_i_top_objects ac ';
9296 			l_where := l_where ||' AND a.source_code_id=ac.source_code_id
9297 				AND ac.resource_id = :l_resource_id ';
9298 		END IF;
9299 
9300 	ELSE
9301 		IF l_view_by = 'CAMPAIGN+CAMPAIGN' then
9302 			IF  l_prog_view='Y' THEN
9303 				l_view := ',''RCAM''';
9304 				l_top_cond :=' AND a.immediate_parent_id is null ';
9305 			END IF;
9306 		ELSE
9307 			/******** to append parent object id is null for other view by (country and product category) ***/
9308 			l_top_cond :=' AND a.immediate_parent_id is null ';
9309 			/***********/
9310 		END IF;
9311 	END IF;
9312 END IF;
9313 /************************************************************************/
9314 
9315    /* product category handling */
9316      IF  l_cat_id is not null then
9317          l_pc_from   :=  ', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
9318 	 l_pc_where  :=  ' AND a.category_id = edh.child_id   AND edh.object_type = ''CATEGORY_SET''
9319 			   AND edh.object_id = mdcs.category_set_id   AND mdcs.functional_area_id = 11
9320 			   AND edh.dbi_flag = ''Y''  AND edh.parent_id = :l_cat_id ';
9321        ELSE
9322         l_pc_where :=     ' AND a.category_id = -9 ';
9323      END IF;
9324 /********************************/
9325 
9326     IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
9327 
9328 
9329      /* forming from clause for the tables which is common to all union all */
9330      if l_cat_id is not null then
9331       l_from1 :=' FROM BIM_I_obj_METS_MV a '||l_from||l_pc_from;
9332      l_from :=' FROM BIM_I_CPB_METS_MV a'||l_from||l_pc_from;
9336         end if;
9333      else
9334       l_from1 :=' FROM BIM_I_obj_METS_MV a '||l_from;
9335      l_from :=' FROM BIM_I_CPB_METS_MV a'||l_from;
9337 
9338 
9339       /* forming where clause which is common to all union all */
9340      l_where :=l_where||'
9341 		 AND a.object_country = :l_country '||
9342 		 l_pc_where;
9343 
9344 
9345     /* forming group by clause for the common columns for all union all */
9346     l_groupby:=' GROUP BY a.source_code_id,camp.object_type_mean, ';
9347 
9348  /*** campaign id null means No drill down and view by is camapign hirerachy*/
9349 
9350   IF l_campaign_id is null THEN
9351 
9352    /*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
9353 
9354      l_sqltext:= l_select_cal||
9355      /******** inner select start from here */
9356 
9357      /* select to get camapigns and programs for current period values */
9358      ' SELECT
9359       a.source_code_id VIEWBYID, camp.name name,camp.object_id object_id, camp.object_type object_type,
9360       camp.object_type_mean meaning '||
9361       l_select1 ||
9362       l_from || ' ,bim_i_obj_name_mv camp '|| l_where ||l_top_cond||
9363     ' AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
9364       AND a.source_code_id=camp.source_code_id
9365       AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
9366       AND camp.language=USERENV(''LANG'')'||
9367       l_groupby||
9368       ' camp.name,camp.object_id,camp.object_type'||
9369      ' UNION ALL      /* select to get camapigns and programs for previous period values */
9370      SELECT
9371       a.source_code_id VIEWBYID, camp.name name, camp.object_id object_id, camp.object_type object_type,
9372       camp.object_type_mean meaning '||
9373       l_select2 ||
9374       l_from1 || ' ,bim_i_obj_name_mv camp '||
9375      l_where ||l_top_cond||
9376     ' AND  BITAND(cal.record_type_id,1143)= cal.record_type_id AND a.source_code_id=camp.source_code_id
9377       AND cal.report_date =trunc(sysdate)  AND camp.language=USERENV(''LANG'')'||
9378       l_groupby|| ' camp.name,camp.object_id,camp.object_type'|| l_select_filter_camp /* appending filter clause */
9379       ;
9380  ELSE
9381  /* source_code_id is passed from the page, object selected from the page to be drill may be program,campaign,event,one off event*****/
9382 /* appending table in l_form and joining conditon for the bim_i_source_codes */
9383 
9384      l_where :=l_where || ' AND  a.immediate_parent_id = :l_campaign_id ' ;
9385 
9386 
9387 
9388 -- if program is selected from the page means it may have childern as programs,campaigns,events or one off events
9389 
9390 
9391 l_curr_suffix1 :=l_curr_suffix;
9392 
9393 
9394 
9395  IF l_object_type='RCAM' THEN
9396  /*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
9400      ' SELECT
9397      l_sqltext:= l_select_cal||
9398      /******** inner select start from here */
9399      /* select to get camapigns and programs for current period values */
9401       a.source_code_id VIEWBYID, camp.name name,camp.object_id object_id, camp.object_type object_type,
9402       camp.object_type_mean meaning '||   l_select1 ||  l_from || ' ,bim_I_obj_name_mv camp '||
9403      l_where ||  ' AND a.source_code_id=camp.source_code_id  AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
9404       AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
9405       AND camp.language=USERENV(''LANG'')'||
9406       l_groupby||
9407       ' camp.name,camp.object_id,camp.object_type'||
9408      ' UNION ALL      /* select to get camapigns and programs for previous period values */
9409      SELECT
9410       a.source_code_id VIEWBYID,camp.name name, camp.object_id object_id, camp.object_type object_type,
9411       camp.object_type_mean meaning '||
9412       l_select2 ||
9413       l_from1 || ' ,bim_I_obj_name_mv camp '||
9414      l_where ||
9415     ' AND a.source_code_id=camp.source_code_id AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
9416      AND cal.report_date =  trunc(sysdate)   AND camp.language=USERENV(''LANG'')'||
9417       l_groupby||
9418       ' camp.name,camp.object_id,camp.object_type'||
9419       l_select_filter_camp ;
9420       /*************** if object type is camp then childern are campaign schedules ***/
9421  ELSIF l_object_type='CAMP' THEN
9422 
9423 
9424 	 l_sqltext:= l_select_cal||
9425  /******** inner select start from here */
9426  /* select to get camapign schedules for current period values */
9427  ' SELECT
9428   a.source_code_id VIEWBYID, camp.name name,
9429   camp.object_id object_id,  camp.object_type object_type,  camp.child_object_usage usage, decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel,  camp.object_type_mean meaning '||
9430   l_select1 ||
9431   l_from || ' ,bim_i_obj_name_mv camp,bim_dimv_media chnl '||
9432  l_where ||
9433 ' AND camp.source_code_id = a.source_code_id  AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
9434   AND camp.object_type =''CSCH''  AND camp.activity_id =chnl.id (+)  AND cal.report_date  in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
9435   AND camp.language=USERENV(''LANG'')'||
9436   l_groupby||
9437 	      ' camp.name,camp.object_id,camp.object_type,camp.child_object_usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
9438    ' UNION ALL      /* select to get camapign schedules for previous period values */
9439    SELECT
9440     a.source_code_id VIEWBYID, camp.name name, camp.object_id object_id,camp.object_type object_type, camp.child_object_usage usage,
9441     decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel, camp.object_type_mean meaning '||
9442     l_select2 || l_from1 || ' ,bim_i_obj_name_mv camp,bim_dimv_media chnl  '||
9443     l_where || ' AND camp.source_code_id = a.source_code_id    AND  BITAND(cal.record_type_id,1143)= cal.record_type_id    AND camp.object_type =''CSCH''
9444     AND camp.activity_id =chnl.id (+)    AND cal.report_date = trunc(sysdate)    AND camp.language=USERENV(''LANG'')'||
9445     l_groupby||   ' camp.name,camp.object_id,camp.object_type,camp.child_object_usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
9446    l_select_filter_camp ;
9447 /*************** if object type is event then childern are event schedules ***/
9448 
9449 
9450   ELSIF l_object_type='EVEH' THEN
9451  l_sqltext:= l_select_cal||
9452      /******** inner select start from here */
9453      /* select to get event schedules for current period values  */
9454      ' SELECT      a.source_code_id VIEWBYID,  camp.name name,camp.object_id object_id, camp.object_type object_type,
9455       camp.object_type_mean meaning '|| l_select1 || l_from || ' ,bim_I_obj_name_mv camp '|| l_where || ' AND  camp.source_code_id = a.source_code_id AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
9456       AND camp.object_type =''EVEO'' AND cal.report_date  in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
9457       AND camp.language=USERENV(''LANG'')'||
9458       l_groupby||
9459       ' camp.name,camp.object_id,camp.object_type'||
9460      ' UNION ALL      /* select to get event schedules for previous period values */
9461      SELECT
9462       a.source_code_id VIEWBYID,
9463       camp.name name, camp.object_id object_id, camp.object_type object_type, camp.object_type_mean meaning '||
9464       l_select2 ||
9468       AND camp.object_type =''EVEO''  AND cal.report_date = trunc(sysdate)  AND camp.language=USERENV(''LANG'')'||
9465       l_from1 || ' ,bim_I_obj_name_mv camp '||
9466       l_where ||
9467     ' AND camp.source_code_id = a.source_code_id  AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
9469       l_groupby||
9470       ' camp.name,camp.object_id,camp.object_type'||
9471       l_select_filter_camp ;
9472   END IF;
9473 
9474  END IF;
9475 
9476  /***** END CAMPAIGN HIRERACHY VIEW HANDLING ******************/
9477 
9478  ELSE
9479  /* view by is product category */
9480  IF (l_view_by ='ITEM+ENI_ITEM_VBH_CAT') THEN
9481 
9482    if l_admin_status='N' then
9483         l_from:=replace(l_from,',fii_time_rpt_struct_v cal');
9484     else
9485         l_from:=null;
9486     end if;
9487 
9488 
9489 /******** handling product category hirerachy ****/
9490 /* picking up value of top level node from product category denorm for category present in  bim_i_obj_mets_mv   */
9491     IF l_cat_id is null then
9492        l_from:=l_from||
9493                ',eni_denorm_hierarchies edh
9494                 ,mtl_default_category_sets mdcs
9495                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
9496                    FROM eni_item_vbh_nodes_v e
9497                    WHERE e.top_node_flag=''Y''
9498                    AND e.child_id = e.parent_id) p ';
9499        l_where := l_where||
9500                          ' AND a.category_id = edh.child_id
9501 			   AND edh.object_type = ''CATEGORY_SET''
9502                            AND edh.object_id = mdcs.category_set_id
9503                            AND mdcs.functional_area_id = 11
9504                            AND edh.dbi_flag = ''Y''
9505                            AND edh.parent_id = p.parent_id';
9506        l_col:=' SELECT  /*+ORDERED*/
9507 		   p.value name,
9508                    p.parent_id viewbyid,
9509 		   p.leaf_node_flag leaf_node_flag,
9510 		   null meaning ';
9511         l_groupby := ' GROUP BY p.value,p.parent_id,p.leaf_node_flag ';
9512     ELSE
9513     /* passing id from page and getting immediate child to build hirerachy  */
9514 
9515     /** reassigning value to l_pc_from and l_pc_where for product category hirerachy drill down for values directly assigned to prodcut select from the page*/
9516 
9517      l_pc_from:= l_from||
9518                    ',(select e.id id,e.value value
9519                       from eni_item_vbh_nodes_v e
9520                       where e.parent_id =  :l_cat_id
9521                       AND e.parent_id = e.child_id
9522                       AND leaf_node_flag <> ''Y''
9523                       ) p ';
9524 
9525     l_pc_where :=l_where||
9526                       ' AND a.category_id = p.id ';
9527 
9528     l_from:= l_from||
9529             ',eni_denorm_hierarchies edh
9530             ,mtl_default_category_sets mdc
9531             ,(select e.id,e.value,e.leaf_node_flag
9532               from eni_item_vbh_nodes_v e
9533           where
9534               e.parent_id =:l_cat_id
9535               AND e.id = e.child_id
9536               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
9537       ) p ';
9538 
9539      l_where := l_where||'
9540                   AND a.category_id = edh.child_id
9541                   AND edh.object_type = ''CATEGORY_SET''
9542                   AND edh.object_id = mdc.category_set_id
9543                   AND mdc.functional_area_id = 11
9544                   AND edh.dbi_flag = ''Y''
9545                   AND edh.parent_id = p.id ';
9546 
9547      l_col:=' SELECT   /*+ORDERED*/
9548 		   p.value name,
9549                    p.id viewbyid,
9550 		   p.leaf_node_flag leaf_node_flag,
9551 		   null meaning ';
9552      l_groupby := ' GROUP BY p.value,p.id,p.leaf_node_flag ';
9553     END IF;
9554 /*********************/
9555 
9556            IF l_campaign_id is null then /* no drilll down in campaign hirerachy */
9557 	      IF l_admin_status ='Y' THEN
9558 	       l_from1:=' FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
9559                               '||l_from;
9560               l_from:=' FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a
9561                               '||l_from;
9562               l_where := l_where ||l_top_cond||
9563                          '   AND  a.object_country = :l_country';
9564                IF l_cat_id is not null then
9565 	          l_pc_from1 := ' FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
9566                               '||l_pc_from;
9567 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a
9568                               '||l_pc_from;
9569 
9570 		  l_pc_where := l_pc_where ||l_top_cond||
9571                          ' AND  a.object_country = :l_country';
9572                END IF;
9573               ELSE
9574 	      l_from1:=' FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
9575                              '||l_from;
9576               l_from:=' FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a
9577                              '||l_from;
9578 
9579               l_where := l_where ||
9580 /*                        ' AND a.parent_object_id is null               */
9581 			  ' AND  a.object_country = :l_country';
9582 
9583 		IF l_cat_id is not null then
9584 		  l_pc_from1 := ' FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
9585                               '||l_pc_from;
9589 		  l_pc_where := l_pc_where ||
9586 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a
9587                               '||l_pc_from;
9588 
9590                    /*      ' AND a.parent_object_id is null   */
9591 			'   AND  a.object_country = :l_country';
9592                END IF;
9593 
9594               END IF;
9595            ELSE
9596 	     l_from1 := ' FROM   fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a'||l_from ;
9597              l_from  := ' FROM   fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a '||l_from ;
9598 
9599               l_where  := l_where ||
9600                         '  AND  a.source_code_id = :l_campaign_id
9601 			   AND  a.object_country = :l_country' ;
9602               IF l_cat_id is not null then
9603 	         l_pc_from1 := ' FROM   fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a '||l_pc_from ;
9604 	         l_pc_from := ' FROM   fii_time_rpt_struct_v cal, BIM_I_CPB_METS_MV a '||l_pc_from ;
9605 
9606                l_pc_where  := l_pc_where ||
9607                         '  AND a.immediate_parent_id = :l_campaign_id
9608 			   AND  a.object_country = :l_country' ;
9609 	      END IF;
9610 	   END IF;
9611    /* building l_pc_select to get values directly assigned to product category passed from the page */
9612    IF l_cat_id is not null  THEN
9613 
9614 
9615 	  l_pc_col:=' SELECT /*+ORDERED*/
9616 		   bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||' name,
9617                    p.id  viewbyid,
9618 		   ''Y'' leaf_node_flag,
9619 		   null meaning ';
9620      l_pc_groupby := ' GROUP BY p.id ';
9621 
9622 
9623  l_pc_select :=
9624               ' UNION ALL ' ||
9625               l_pc_col||
9626               l_select1||
9627 	      l_pc_from||
9628 	      l_pc_where ||' AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
9629 	                   'AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id  '||
9630 	      l_pc_groupby ||
9631 	      ' UNION ALL ' ||
9632 	      l_pc_col||
9633 	      l_select2||
9634 	      l_pc_from1||l_inner||
9635 	      l_pc_where ||' AND cal.report_date =trunc(sysdate) '||
9636 	                   'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  '||
9637 			   l_inr_cond||
9638 	      l_pc_groupby ;
9639    END IF;
9640 
9641  ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
9642    /** product category handling**/
9643 
9644   /** l_union_inc and l_col_inc ,,is for performance gain ,for country and product category for admin combination  */
9645 l_union_inc := ',fii_time_rpt_struct_v cal ,BIM_I_OBJ_METS_MV a,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs
9646 ,fnd_territories_tl  d ';
9647 
9648 l_col_inc   :='SELECT /*+ ordered */ decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
9649 a.object_country viewbyid,    null meaning ';
9650 
9651   IF l_cat_id is null then
9652      l_where := l_where ||l_pc_where;
9653   ELSE
9654      l_from  := l_from ||l_pc_from;
9655      l_where := l_where||l_pc_where;
9656   END IF;
9657 
9658      l_col:=' SELECT decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
9659                 a.object_country viewbyid,    null meaning ';
9660 
9661     l_groupby := ' GROUP BY    decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country  ';
9662  l_from:=' FROM fnd_territories_tl  d '||l_from;
9663 	  IF l_campaign_id is null then
9664 	      IF l_admin_status ='Y' THEN
9665 	        l_from1:=l_from||' ,BIM_I_OBJ_METS_MV a ';
9666 	        l_from:=l_from||'  ,BIM_I_CPB_METS_MV a ';
9667 
9668               l_where := l_where ||l_top_cond||
9669                          '   AND a.object_country =d.territory_code(+)
9670 			     AND D.language(+) = userenv(''LANG'') ';
9671               ELSE
9672 	       l_from1:=l_from||' ,BIM_I_OBJ_METS_MV a ';
9673                l_from:=l_from||'  ,BIM_I_CPB_METS_MV a ';
9674 
9675               l_where := l_where ||
9676 	                 '   AND  a.object_country =d.territory_code(+)
9677 			     AND D.language(+) = userenv(''LANG'') ';
9678               END IF;
9679             ELSE
9680 	       l_from1 := l_from||' ,BIM_I_OBJ_METS_MV a ';
9681                l_from := l_from||' ,BIM_I_CPB_METS_MV a ';
9682 
9683               l_where  := l_where ||
9684                         '  AND a.source_code_id = :l_campaign_id
9685                            AND  a.object_country =d.territory_code(+)
9686 			   AND d.language(+) = userenv(''LANG'') ';
9687 	  END IF;
9688 	  IF  l_country <>'N' THEN
9689 	      l_where  := l_where ||' AND  a.object_country = :l_country';
9690           ELSE
9691 	   l_where  := l_where ||' AND  a.object_country <> ''N''';
9692 	  END IF;
9693 
9694 ELSIF (l_view_by ='MEDIA+MEDIA') THEN
9695 /** product category handling**/
9696    IF l_cat_id is null then
9697      l_where := l_where ||l_pc_where;
9698   ELSE
9699      l_from  := l_from ||l_pc_from;
9700      l_where := l_where||l_pc_where;
9701   END IF;
9702     l_col:=' SELECT
9703 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
9704                      null viewbyid,
9705 		     null meaning ';
9706     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
9710 
9707     l_from:=' FROM bim_dimv_media d '||l_from;
9708 	  IF l_campaign_id is null then
9709 	      IF l_admin_status ='Y' THEN
9711                l_from1:=l_from||' ,BIM_OBJ_CHNL_MV  a ';
9712 	      l_from:=l_from||' ,BIM_I_CPB_CHNL_MV a ';
9713 
9714               l_where := l_where ||
9715                          ' AND  d.id (+)= a.activity_id
9716 			   AND  a.immediate_parent_id is null
9717 			   AND  a.object_country = :l_country';
9718               ELSE
9719 
9720 	      l_from1:=l_from||' ,BIM_OBJ_CHNL_MV a ';
9721               l_from:=l_from||' ,BIM_I_CPB_CHNL_MV a ';
9722 
9723               l_where := l_where ||
9724 	              /*   ' AND  a.parent_object_id is null           */
9725                          '  AND  d.id (+)= a.activity_id
9726    		           AND  a.object_country = :l_country';
9727               END IF;
9728             ELSE
9729 
9730 	     l_from1 := l_from||' ,BIM_OBJ_CHNL_MV a';
9731               l_from := l_from||' ,BIM_I_CPB_CHNL_MV a ';
9732 
9733               l_where  := l_where ||
9734                         '  AND a.source_code_id = :l_campaign_id
9735                            AND  d.id (+)= a.activity_id
9736 			   AND  a.object_country = :l_country';
9737 	  END IF;
9738 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
9739 /** product category handling**/
9740    IF l_cat_id is null then
9741      l_where := l_where ||l_pc_where;
9742   ELSE
9743      l_from  := l_from ||l_pc_from;
9744      l_where := l_where||l_pc_where;
9745   END IF;
9746     l_col:=' SELECT
9747 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
9748                      null viewbyid,
9749 		     null meaning ';
9750     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
9751     l_from:=' FROM bis_areas_v d '||l_from;
9752 	  IF l_campaign_id is null then
9753 	      IF l_admin_status ='Y' THEN
9754                l_from1:=l_from||' ,BIM_OBJ_REGN_MV  a ';
9755 	      l_from:=l_from||' ,BIM_I_CPB_REGN_MV a ';
9756 
9757               l_where := l_where ||
9758                          ' AND  d.id (+)= a.object_region
9759 			   AND  a.immediate_parent_id is null
9760 			   AND  a.object_country = :l_country';
9761               ELSE
9762 	       l_from1:=l_from||' ,BIM_OBJ_REGN_MV a ';
9763               l_from:=l_from||' ,BIM_I_CPB_REGN_MV a ';
9764 
9765               l_where := l_where ||
9766 	              /*   ' AND  a.parent_object_id is null           */
9767                          '  AND  d.id (+)= a.object_region
9768    		           AND  a.object_country = :l_country';
9769               END IF;
9770             ELSE
9771 	      l_from1 := l_from||' ,BIM_OBJ_REGN_MV a ';
9772               l_from := l_from||'  ,BIM_I_CPB_REGN_MV a ';
9773 
9774               l_where  := l_where ||
9775                         '
9776                            AND a.source_code_id = :l_campaign_id
9777                            AND  d.id (+)= a.object_region
9778 			   AND  a.object_country = :l_country';
9779 	  END IF;
9780 
9781 END IF;
9782 
9783 
9784 
9785 /* combine sql one to pick up current period values and  sql two to pick previous period values */
9786 
9787   l_select := l_col||
9788               l_select1||
9789 	      l_from||
9790 	      l_where ||' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
9791 	      'AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id  '||
9792 	      l_groupby ||
9793 	      ' UNION ALL ';
9794 
9795 if (l_view_by ='GEOGRAPHY+COUNTRY'  and l_campaign_id is null and l_cat_id is not null and l_admin_status = 'Y') then
9796 l_select :=   l_select||l_col_inc||l_select2||' from '||l_inner||l_union_inc ;
9797  else
9798 l_select :=   l_select||l_col||l_select2||l_from1||l_inner;
9799 end if;
9800 
9801 l_select :=   l_select||l_where ||' AND cal.report_date = trunc(sysdate) '||
9802 	      'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  '||
9803 	      l_inr_cond||
9804 	      l_groupby||
9805 	      l_pc_select /* l_pc_select only applicable when product category is not all and view by is product category */
9806 	      ;
9807 
9808 
9809 	       /* l_pc_select only applicable when product category is not all and view by is product category */
9810 
9811 
9812 /* prepare final sql */
9813 
9814  l_sqltext:= l_select_cal1||
9815              l_select||
9816 	     l_select_filter;
9817   END IF;
9818 
9819 
9820 
9821    x_custom_sql := l_sqltext;
9822    l_custom_rec.attribute_name := ':l_record_type';
9823    l_custom_rec.attribute_value := l_record_type_id;
9824    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
9825    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
9826    x_custom_output.EXTEND;
9827    x_custom_output (1) := l_custom_rec;
9828    l_custom_rec.attribute_name := ':l_resource_id';
9829    l_custom_rec.attribute_value := get_resource_id;
9830    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
9831    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
9832    x_custom_output.EXTEND;
9833    x_custom_output (2) := l_custom_rec;
9834    l_custom_rec.attribute_name := ':l_admin_flag';
9835    l_custom_rec.attribute_value := get_admin_status;
9836    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
9840    l_custom_rec.attribute_name := ':l_country';
9837    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
9838    x_custom_output.EXTEND;
9839    x_custom_output (3) := l_custom_rec;
9841    l_custom_rec.attribute_value := l_country;
9842    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
9843    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
9844    x_custom_output.EXTEND;
9845    x_custom_output (4) := l_custom_rec;
9846    l_custom_rec.attribute_name := ':l_cat_id';
9847    l_custom_rec.attribute_value := l_cat_id;
9848    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
9849    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
9850    x_custom_output.EXTEND;
9851    x_custom_output (5) := l_custom_rec;
9852    l_custom_rec.attribute_name := ':l_campaign_id';
9853    l_custom_rec.attribute_value := l_campaign_id;
9854    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
9855    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
9856    x_custom_output.EXTEND;
9857    x_custom_output (6) := l_custom_rec;
9858    write_debug ('GET_CPL_RPL_SQL', 'QUERY', '_', l_sqltext);
9859 EXCEPTION
9860    WHEN OTHERS
9861    THEN
9862       l_sql_errm := SQLERRM;
9863       write_debug ('GET_CPL_RPL_SQL', 'ERROR', l_sql_errm, l_sqltext);
9864 END GET_CPL_RPL_SQL;
9865 
9866 
9867 PROCEDURE GET_RPL_CPL_SQL (
9868    p_page_parameter_tbl   IN              bis_pmv_page_parameter_tbl,
9869    x_custom_sql           OUT NOCOPY      VARCHAR2,
9870    x_custom_output        OUT NOCOPY      bis_query_attributes_tbl
9871 )
9872 IS
9873    l_sqltext                      VARCHAR2 (32000);
9874    iflag                          NUMBER;
9875    l_period_type_hc               NUMBER;
9876    l_as_of_date                   DATE;
9877    l_period_type                  VARCHAR2 (2000);
9878    l_record_type_id               NUMBER;
9879    l_comp_type                    VARCHAR2 (2000);
9880    l_country                      VARCHAR2 (4000);
9881    l_view_by                      VARCHAR2 (4000);
9882    l_sql_errm                     VARCHAR2 (4000);
9883    l_previous_report_start_date   DATE;
9884    l_current_report_start_date    DATE;
9885    l_previous_as_of_date          DATE;
9886    l_period_type_id               NUMBER;
9887    l_user_id                      NUMBER;
9888    l_resource_id                  NUMBER;
9889    l_time_id_column               VARCHAR2 (1000);
9890    l_admin_status                 VARCHAR2 (20);
9891    l_admin_flag                   VARCHAR2 (1);
9892    l_admin_count                  NUMBER;
9893    l_rsid                         NUMBER;
9894    l_curr_aod_str                 VARCHAR2 (80);
9895    l_country_clause               VARCHAR2 (4000);
9896    l_access_clause                VARCHAR2 (4000);
9897    l_access_table                 VARCHAR2 (4000);
9898    l_cat_id                       VARCHAR2 (50)        := NULL;
9899    l_campaign_id                  VARCHAR2 (50)        := NULL;
9900    l_select                       VARCHAR2 (20000); -- to build  inner select to pick data from mviews
9901    l_pc_select                    VARCHAR2 (20000); -- to build  inner select to pick data directly assigned to the product category hirerachy
9902    l_select_cal                   VARCHAR2 (25000); -- to build  select calculation part
9903    l_select_cal1                  VARCHAR2 (25000);
9904    l_select_filter                VARCHAR2 (25000); -- to build  select filter part
9905    l_select_filter_camp           VARCHAR2 (25000);
9906    l_from                         VARCHAR2 (20000);   -- assign common table in  clause
9907    l_from_inr                     VARCHAR2 (20000);
9908    l_where_inr                    VARCHAR2 (20000);
9909    l_where                        VARCHAR2 (20000);  -- static where clause
9910    l_groupby                      VARCHAR2 (2000);  -- to build  group by clause
9911    l_pc_from                      VARCHAR2 (20000);   -- from clause to handle product category
9912    l_pc_where                     VARCHAR2 (20000);   --  where clause to handle product category
9913    l_filtercol                    VARCHAR2 (2000);
9914    l_pc_col                       VARCHAR2(200);
9915    l_pc_groupby                   VARCHAR2(200);
9916    l_view                         VARCHAR2 (20);
9917    l_select1                      VARCHAR2 (20000);
9918    l_select2                      VARCHAR2 (20000);
9919    l_view_disp                    VARCHAR2(100);
9920    l_url_str                      VARCHAR2(1000);
9921    l_url_str_csch                 varchar2(1000);
9922    l_url_str_type                 varchar2(1000);
9923    l_url_str_csch_jtf             varchar2(3000);
9924    l_camp_sel_col                 varchar2(100);
9925    l_camp_groupby_col             varchar2(100);
9926    l_csch_chnl                    varchar2(100);
9927    l_top_cond                     VARCHAR2(100);
9928    l_meaning                      VARCHAR2 (20);
9929    l_inner                        varchar2(5000);
9930    l_inr_cond                     varchar2(5000);
9931    l_obj_revenue                  varchar2(50);
9932    l_cpb_revenue                  varchar2(50);
9933    rpl                            varchar2(500);
9934    p_rpl                          varchar2(500);
9935    l_union_inc                    varchar2(5000);
9936    l_col_inc                      varchar2(1000);
9937    l_curr VARCHAR2(50);
9938    l_curr_suffix VARCHAR2(50);
9939    l_col_id NUMBER;
9940    l_area VARCHAR2(300);
9941    l_report_name VARCHAR2(300);
9945     cursor get_obj_type
9942    /* variables to hold columns names in l_select clauses */
9943    l_col                          VARCHAR2(1000);
9944    /* cursor to get type of object passed from the page ******/
9946     is
9947     select object_type
9948     from bim_i_source_codes
9949     where source_code_id=replace(l_campaign_id,'''');
9950     /*********************************************************/
9951    l_custom_rec                   bis_query_attributes;
9952    l_object_type                  varchar2(30);
9953    l_url_link                     varchar2(200);
9954    l_url_camp1                     varchar2(3000);
9955    l_url_camp2                     varchar2(3000);
9956    l_dass                          varchar2(100);  -- variable to store value for  directly assigned lookup value
9957   -- l_una                           varchar2(100);   -- variable to store value for  Unassigned lookup value
9958      l_leaf_node_flag               varchar2(25);   -- variable to store value leaf_node_flag column in case of product category
9959      l_media VARCHAR2(300);
9960     -- l_jtf  varchar2(300);
9961    l_curr_suffix1 VARCHAR2(50);
9962    l_table_bud VARCHAR2(300);
9963    l_where_bud VARCHAR2(300);
9964    l_prog_cost1 VARCHAR2(30);
9965 BEGIN
9966    x_custom_output := bis_query_attributes_tbl ();
9967    l_custom_rec := bis_pmv_parameters_pub.initialize_query_type;
9968 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
9969    			     			      l_as_of_date,
9970 			    			      l_period_type,
9971 				                      l_record_type_id,
9972 				                      l_comp_type,
9973 				                      l_country,
9974 						      l_view_by,
9975 						      l_cat_id,
9976 						      l_campaign_id,
9977 						      l_curr,
9978 						      l_col_id,
9979 						      l_area,
9980 						      l_media,
9981 						      l_report_name
9982 				                      );
9983 
9984    l_meaning:=' null meaning '; -- assigning default value
9985    l_url_camp1:=',null';
9986    l_url_camp2:=',null';
9987    IF (l_curr = '''FII_GLOBAL1''')
9988     THEN l_curr_suffix := '';
9989   ELSIF (l_curr = '''FII_GLOBAL2''')
9990     THEN l_curr_suffix := '_s';
9991     ELSE l_curr_suffix := '';
9992   END IF;
9993 
9994    l_admin_status := get_admin_status;
9995 
9996    IF l_country IS NULL THEN
9997       l_country := 'N';
9998    END IF;
9999 
10000 
10001 
10002  rpl:= ' (decode(sum(BIM_ATTRIBUTE4) over(),0,0,sum(BIM_ATTRIBUTE2) over() /sum(BIM_ATTRIBUTE4) over() )) ' ;
10003  p_rpl:= ' (decode(sum(prev_ptd_leads) over() ,0,0,sum(prev_ptd_revenue) over() /sum(prev_ptd_leads) over())) ';
10004 
10005    IF l_revenue = 'BOOKED_AMT' THEN
10006 
10007     l_obj_revenue :=' orders_booked_amt ';
10008     l_cpb_revenue :='  booked_amt ';
10009 
10010    ELSIF l_revenue = 'INVOICED_AMT'   THEN
10011 
10012     l_obj_revenue :=' orders_invoiced_amt ';
10013     l_cpb_revenue :=' invoiced_amt ';
10014 
10015    ELSIF l_revenue = 'WON_OPPR_AMT' THEN
10016 
10017     l_obj_revenue :=' won_opportunity_amt ';
10018     l_cpb_revenue :='won_opportunity_amt ';
10019 
10020    END IF;
10021 
10022    /*********************** security handling for inner query ***********************/
10023 
10024 /* IF  l_view_by <> 'CAMPAIGN+CAMPAIGN' then
10025      IF   l_campaign_id is null THEN
10026      IF   l_admin_status = 'N' THEN
10027 	     IF l_prog_view='Y' then
10028 	            l_from_inr :=', bim_i_top_objects  inr_r ';
10029 	    ELSE
10030 	         l_from_inr :=', ams_act_access_denorm  inr_r ';
10031              END IF;
10032                 l_where_inr := '  AND a.source_code_id = inr_r.source_code_id  AND inr_r.resource_id = :l_resource_id ';
10033 
10034 	ELSE
10035 	        l_where_inr   :=' AND a.immediate_parent_id is null ';
10036 
10037     END IF;
10038 
10039    END IF;
10040    END IF;*/
10041 
10042 /************************************************************************/
10043 
10044 
10045 /************Start Inner Query to get current acitve objects *************************/
10046 
10047  IF   l_campaign_id is null THEN
10048 
10049  if (l_view_by ='GEOGRAPHY+COUNTRY'  and l_cat_id is not null and l_admin_status = 'Y') then
10050 
10051     l_inner:='  ( select distinct  a.source_code_id   from fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a  '||l_from_inr;
10052 else
10053     l_inner:=', ( select distinct  a.source_code_id   from fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a  '||l_from_inr;
10054 end if;
10055 
10056     IF l_cat_id is not null then
10057      l_inner := l_inner ||', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
10058     end if;
10059 
10060     l_inner := l_inner || ' WHERE   a.time_id=cal.time_id  AND a.period_type_id=cal.period_type_id  AND cal.calendar_id=-1  AND cal.report_date   in (&BIS_CURRENT_ASOF_DATE)
10061     AND a.object_country = :l_country   AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id   and ( a.'||l_obj_revenue||' >0 or a.leads >0)';
10062 
10063 
10064     IF l_cat_id is null then
10065       l_inner :=  l_inner ||' AND a.category_id = -9 ';
10066     else
10067       l_inner :=  l_inner ||' AND a.category_id = edh.child_id  AND edh.object_type = ''CATEGORY_SET''   AND edh.object_id = mdcs.category_set_id
10068 			   AND mdcs.functional_area_id = 11   AND edh.dbi_flag = ''Y''    AND edh.parent_id = :l_cat_id ';
10069     end if;
10070 
10074 
10071     l_inner := l_inner ||l_where_inr;
10072 
10073     l_inner :=  l_inner ||' ) inr ';
10075 
10076 l_inr_cond:='and a.source_code_id=inr.source_code_id ';
10077 
10078 
10079 end if;
10080 
10081 /************ End Inner Query to get current acitve objects *************************/
10082 
10083 /** to add meaning in select clause only in case of campaign view by */
10084   IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
10085   l_meaning:=' ,meaning ';
10086   l_filtercol:=',meaning ';
10087   ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
10088   l_filtercol:=',leaf_node_flag ';
10089   l_meaning:=',null meaning ';
10090   else
10091   l_meaning:=' ,null meaning ';
10092   end if;
10093 
10094   /*** to display Directly assigned  **/
10095 
10096     /*** to  assigned URL **/
10097 
10098 if l_campaign_id is not null then
10099 -- checking for the object type passed from page
10100  for i in get_obj_type
10101  loop
10102  l_object_type:=i.object_type;
10103  end loop;
10104 end if;
10105 
10106   -- l_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
10107    l_url_str  :='pFunctionName=BIM_I_RPL_PHP&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
10108    --l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objId=';
10109    l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
10110    l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
10111    l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
10112    &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
10113    IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
10114        l_url_link :=' ,decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) ';
10115        l_view_disp:='viewby';
10116        l_leaf_node_flag :=' ,leaf_node_flag ';
10117    ELSIF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
10118      l_camp_sel_col :=' ,object_id
10119                        ,object_type
10120                        ';
10121      l_camp_groupby_col :=',object_id,object_type ';
10122      l_url_link := ' ,null ';
10123      l_view_disp := 'viewby';
10124 
10125      IF (l_campaign_id is  null or l_object_type='RCAM') then
10126 	l_url_camp1:=', decode(object_type,''EONE'',NULL,'||''''||l_url_str||''''||' )';
10127      ELSIF l_object_type='CAMP' THEN
10128 	l_url_camp2:=', '||''''||l_url_str_type||''''||'||object_id';
10129          --l_url_camp1:=', decode(usage,''LITE'','||''''||l_url_str_csch||''''||'||object_id,'||''''||l_url_str_csch_jtf||''''||'||object_id)';
10130 		 l_url_camp1:=', '||''''||l_url_str_csch||''''||'||object_id';
10131 	l_csch_chnl:='|| '' - '' || channel';
10132 	l_camp_sel_col :=l_camp_sel_col|| ',usage,channel';
10133 	l_camp_groupby_col :=l_camp_groupby_col||',usage,channel';
10134      end if;
10135     ELSE
10136      -- l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
10137       l_url_link:=' ,null ';
10138       l_view_disp:='viewby';
10139    END IF;
10140 
10141 /* l_select_cal is common part of select statement for all view by to calculate grand totals and change */
10142  l_select_cal :='select VIEWBY ,viewbyid,BIM_ATTRIBUTE7,bim_attribute2 ,BIM_ATTRIBUTE4,BIM_ATTRIBUTE3  ,BIM_ATTRIBUTE5 ,BIM_ATTRIBUTE6  ,BIM_ATTRIBUTE8  ,BIM_ATTRIBUTE9,BIM_ATTRIBUTE10
10143 ,bim_url1,bim_url2,bim_url3,BIM_GRAND_TOTAL1 , BIM_GRAND_TOTAL2,BIM_GRAND_TOTAL3 ,BIM_GRAND_TOTAL4 ,BIM_GRAND_TOTAL5 ,BIM_GRAND_TOTAL6 ,BIM_GRAND_TOTAL7 ,BIM_GRAND_TOTAL8
10144 from (   SELECT '|| l_view_disp ||' ,viewbyid ,BIM_ATTRIBUTE7 '||l_csch_chnl||' bim_attribute7
10145  ,BIM_ATTRIBUTE2 ,BIM_ATTRIBUTE4,decode(prev_rpl,0,null,((BIM_ATTRIBUTE5-prev_rpl)/prev_rpl)*100) BIM_ATTRIBUTE3
10146 ,BIM_ATTRIBUTE5,BIM_ATTRIBUTE6,BIM_ATTRIBUTE8,BIM_ATTRIBUTE9,BIM_ATTRIBUTE10'||
10147 l_url_link || ' bim_url1'||l_url_camp1|| ' bim_url2 '||l_url_camp2||' bim_url3
10148 ,BIM_GRAND_TOTAL1  ,decode('||p_rpl||',0,null,(('||rpl||' - '||p_rpl||')/ '||p_rpl||')*100 ) BIM_GRAND_TOTAL2
10149 ,BIM_GRAND_TOTAL3,BIM_GRAND_TOTAL4,BIM_GRAND_TOTAL5,BIM_GRAND_TOTAL6,BIM_GRAND_TOTAL7,BIM_GRAND_TOTAL8
10150    FROM (
10151 SELECT
10152  name    VIEWBY ,VIEWBYID
10153 ,meaning BIM_ATTRIBUTE7 '||l_camp_sel_col||'
10154 ,ptd_revenue BIM_ATTRIBUTE2,ptd_leads BIM_ATTRIBUTE4,decode(ptd_leads,0,null,ptd_revenue/ptd_leads) BIM_ATTRIBUTE5
10155 ,decode(prev_ptd_leads,0,null,prev_ptd_revenue/prev_ptd_leads) prev_rpl,total_leads BIM_ATTRIBUTE6
10156 ,total_revenue BIM_ATTRIBUTE8,decode(total_leads,0,null,total_revenue/total_leads) BIM_ATTRIBUTE9
10157 ,total_cost  BIM_ATTRIBUTE10,sum(ptd_revenue) over() BIM_GRAND_TOTAL1,99 BIM_GRAND_TOTAL2
10158  ,sum(ptd_leads) over() BIM_GRAND_TOTAL3,decode(sum(ptd_leads) over(),0,null,sum(ptd_revenue) over()/sum(ptd_leads) over()) BIM_GRAND_TOTAL4
10159 ,sum(total_leads) over() BIM_GRAND_TOTAL5,sum(total_revenue) over() BIM_GRAND_TOTAL6
10160 ,decode(sum(total_leads) over(),0,null,sum(total_revenue) over()/sum(total_leads) over()) BIM_GRAND_TOTAL7,sum(total_cost) over() BIM_GRAND_TOTAL8
10161 ,prev_ptd_leads ,prev_ptd_revenue
10162 FROM
10163 (  SELECT
10164 viewbyid,name'||l_meaning ||l_camp_sel_col||',SUM(ptd_revenue) ptd_revenue,SUM(ptd_leads) ptd_leads,SUM(p_ptd_revenue) prev_ptd_revenue
10168 
10165  ,SUM(p_ptd_leads) Prev_PTD_leads ,case when ( (SUM(ptd_revenue) > 0) or (SUM(ptd_leads) > 0)) then SUM(total_revenue) else 0 end   total_revenue,case when ( (SUM(ptd_revenue) > 0)or (SUM(ptd_leads)    >0) ) then
10166 Sum(total_leads)  else 0 end total_leads,case when ((SUM(ptd_revenue) > 0) or (SUM(ptd_leads)>0) ) then decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(t_budget_approved),SUM(total_cost))
10167 else 0 end total_cost  FROM ( ';
10169 /* l_select_cal is common part of select statement for all view by to calculate grand totals and change */
10170  l_select_cal1 :='
10171  SELECT '||l_view_disp ||',viewbyid,BIM_ATTRIBUTE7,BIM_ATTRIBUTE2,BIM_ATTRIBUTE4
10172  ,decode(prev_rpl,0,null,((BIM_ATTRIBUTE5-prev_rpl)/prev_rpl)*100) BIM_ATTRIBUTE3,BIM_ATTRIBUTE5,BIM_ATTRIBUTE6,BIM_ATTRIBUTE8,BIM_ATTRIBUTE9
10173 ,BIM_ATTRIBUTE10'||l_url_link|| ' bim_url1'||' ,null BIM_URL2,null BIM_URL3,BIM_GRAND_TOTAL1 ,decode('||p_rpl||',0,null,(('||rpl||' - '||p_rpl||')/ '||p_rpl||')*100 ) BIM_GRAND_TOTAL2
10174 ,BIM_GRAND_TOTAL3,BIM_GRAND_TOTAL4,BIM_GRAND_TOTAL5,BIM_GRAND_TOTAL6,BIM_GRAND_TOTAL7,BIM_GRAND_TOTAL8
10175    FROM
10176 ( SELECT
10177  name  VIEWBY,VIEWBYID'||l_leaf_node_flag||', meaning BIM_ATTRIBUTE7,ptd_revenue BIM_ATTRIBUTE2,ptd_leads BIM_ATTRIBUTE4,decode(ptd_leads,0,null,ptd_revenue/ptd_leads) BIM_ATTRIBUTE5,
10178 decode(prev_ptd_leads,0,null,prev_ptd_revenue/prev_ptd_leads) prev_rpl,total_leads BIM_ATTRIBUTE6,
10179 total_revenue BIM_ATTRIBUTE8,decode(total_leads,0,null,total_revenue/total_leads) BIM_ATTRIBUTE9,
10180 total_cost  BIM_ATTRIBUTE10,sum(ptd_revenue) over() BIM_GRAND_TOTAL1,66 BIM_GRAND_TOTAL2,
10181 sum(ptd_leads) over() BIM_GRAND_TOTAL3,decode(sum(ptd_leads) over(),0,null,sum(ptd_revenue) over()/sum(ptd_leads) over()) BIM_GRAND_TOTAL4,
10182 sum(total_leads) over() BIM_GRAND_TOTAL5,sum(total_revenue) over() BIM_GRAND_TOTAL6,decode(sum(total_leads) over(),0,null,sum(total_revenue) over()/sum(total_leads) over()) BIM_GRAND_TOTAL7,
10183  sum(total_cost) over() BIM_GRAND_TOTAL8 ,prev_ptd_leads ,prev_ptd_revenue
10184    FROM (
10185 SELECT
10186 viewbyid,name'||l_leaf_node_flag||l_meaning||',SUM(ptd_revenue) ptd_revenue,SUM(ptd_leads) ptd_leads,
10187  SUM(p_ptd_revenue) prev_ptd_revenue,SUM(p_ptd_leads) Prev_PTD_leads,SUM(total_revenue) total_revenue,
10188 Sum(total_leads) total_leads,decode('''|| l_prog_cost ||''',''BIM_APPROVED_BUDGET'',SUM(t_budget_approved),SUM(total_cost)) total_cost
10189   FROM  ( ';
10190 
10191 
10192 
10193 l_curr_suffix1 :=l_curr_suffix;
10194 
10195 IF l_object_type in ('CAMP','EVEH','CSCH') AND l_prog_cost ='BIM_APPROVED_BUDGET' AND l_view_by = 'CAMPAIGN+CAMPAIGN' THEN
10196 
10197 --l_table_bud :=  ' ,bim_i_marketing_facts facts';
10198 --l_where_bud := ' AND facts.source_code_id = a.source_code_id';
10199 IF l_curr_suffix is null THEN
10200 l_prog_cost1 := 'a.budget_approved_sch';
10201 ELSE
10202 l_curr_suffix1 := null;
10203 l_prog_cost1 := 'a.budget_approved_sch_s';
10204 END IF;
10205 ELSE
10206 l_prog_cost1 :='a.budget_approved';
10207 
10208 END IF;
10209 
10210 
10211 /* l_select1 and l_select2 contains column information common to all select statement for all view by */
10212 
10213 l_select1:=
10214 ' , SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.leads else 0 end) ptd_leads,
10215     SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then case  '''|| l_revenue ||'''   when ''BOOKED_AMT'' then    a.orders_booked_amt'||l_curr_suffix||'
10216   when ''INVOICED_AMT'' then  a.orders_invoiced_amt'||l_curr_suffix||'  when ''WON_OPPR_AMT'' then  a.won_opportunity_amt'||l_curr_suffix||'   end  else 0 end) ptd_revenue,
10217 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.leads else 0 end) p_ptd_leads,
10218 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then 	 case  '''|| l_revenue ||'''   when ''BOOKED_AMT'' then    a.orders_booked_amt'||l_curr_suffix||'
10219 when ''INVOICED_AMT'' then  a.orders_invoiced_amt'||l_curr_suffix||'  when ''WON_OPPR_AMT'' then  a.won_opportunity_amt'||l_curr_suffix||' end  else 0 end) p_ptd_revenue,0 total_revenue,0 total_leads,
10220 0 total_cost,0 t_budget_approved';
10221 
10222  l_select2 :=
10223  ' ,0  ptd_leads,   0 ptd_revenue,  0  p_ptd_leads, 0 p_ptd_revenue,  SUM( case  '''|| l_revenue ||''' when ''BOOKED_AMT'' then    a.orders_booked_amt'||l_curr_suffix||'
10224   when ''INVOICED_AMT'' then  a.orders_invoiced_amt'||l_curr_suffix||'   when ''WON_OPPR_AMT'' then  a.won_opportunity_amt'||l_curr_suffix||'  end ) total_revenue,
10225 SUM(a.leads)  total_leads,  SUM(a.cost_actual'||l_curr_suffix||') total_cost, SUM('||l_prog_cost1||l_curr_suffix1||') t_budget_approved ';
10226 
10227 /* l_from contains time dimension table common to all select statement for all view by */
10228  l_from  :=',fii_time_rpt_struct_v cal ';
10229 
10230 
10231  /* l_where contains where clause to join time dimension table common to all select statement for all view by */
10232 
10233  l_where :=' WHERE a.time_id = cal.time_id  AND  a.period_type_id = cal.period_type_id   AND  cal.calendar_id= -1 ';
10234 
10235 
10236 
10237  /* l_select_filter contains group by and filter clause to remove uneccessary records with zero values */
10238 
10239 l_select_filter := ' ) GROUP BY viewbyid,name '||l_filtercol||l_camp_groupby_col||
10240  ')  )  WHERE    bim_attribute4 <> 0     or bim_attribute2 <> 0 or prev_ptd_leads <> 0 or prev_ptd_revenue <> 0
10241   or BIM_ATTRIBUTE6 <> 0 or BIM_ATTRIBUTE8 <> 0 or BIM_ATTRIBUTE10<> 0  &ORDER_BY_CLAUSE ';
10242 
10243 l_select_filter_camp := ' ) GROUP BY viewbyid,name '||l_filtercol||l_camp_groupby_col||
10244 ')  )  WHERE  bim_attribute4 <> 0  or bim_attribute2 <> 0  or prev_ptd_leads <> 0 or  prev_ptd_revenue<> 0
10245  or BIM_ATTRIBUTE6 <> 0 or BIM_ATTRIBUTE8 <> 0 or BIM_ATTRIBUTE10<> 0 )  WHERE  bim_attribute4 <> 0  or bim_attribute2 <> 0 or BIM_ATTRIBUTE6 <> 0
10249 
10246  or BIM_ATTRIBUTE8 <> 0 or BIM_ATTRIBUTE10<> 0 &ORDER_BY_CLAUSE ';
10247 
10248 
10250  /* get_admin_status to check current user is admin or not */
10251 
10252 
10253 
10254 /*********************** security handling ***********************/
10255 
10256 IF   l_campaign_id is null THEN /******* no security checking at child level ********/
10257 	IF   l_admin_status = 'N' THEN
10258 		IF  l_view_by = 'CAMPAIGN+CAMPAIGN' then
10259 		/*************** program view is enable **************/
10260 			IF l_prog_view='Y' then
10261 				l_view := ',''RCAM''';
10262 				l_from := l_from ||',bim_i_top_objects ac ';
10263 				l_where := l_where ||' AND a.source_code_id=ac.source_code_id
10264 							AND ac.resource_id = :l_resource_id ';
10265 				/************************************************/
10266 			ELSE
10267 
10268 				l_from := l_from ||',ams_act_access_denorm ac,bim_i_source_codes src ';
10269 				l_where := l_where ||' AND a.source_code_id=src.source_code_id
10270 						       AND src.object_id=ac.object_id
10271 						       AND src.object_type=ac.object_type
10272 						       AND src.object_type NOT IN (''RCAM'')
10273 						       AND ac.resource_id = :l_resource_id ';
10274 
10275 			END IF;
10276 
10277 		ELSE
10278 			l_from := l_from ||',bim_i_top_objects ac ';
10279 			l_where := l_where ||' AND a.source_code_id=ac.source_code_id
10280 				AND ac.resource_id = :l_resource_id ';
10281 		END IF;
10282 
10283 	ELSE
10284 		IF l_view_by = 'CAMPAIGN+CAMPAIGN' then
10285 			IF  l_prog_view='Y' THEN
10286 				l_view := ',''RCAM''';
10287 				l_top_cond :=' AND a.immediate_parent_id is null ';
10288 			END IF;
10289 		ELSE
10290 			/******** to append parent object id is null for other view by (country and product category) ***/
10291 			l_top_cond :=' AND a.immediate_parent_id is null ';
10292 			/***********/
10293 		END IF;
10294 	END IF;
10295 END IF;
10296 
10297 /************************************************************************/
10298 
10299    /* product category handling */
10300      IF  l_cat_id is not null then
10301          l_pc_from   :=  ', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
10302 	 l_pc_where  :=  ' AND a.category_id = edh.child_id
10303 			   AND edh.object_type = ''CATEGORY_SET''
10304 			   AND edh.object_id = mdcs.category_set_id
10305 			   AND mdcs.functional_area_id = 11
10306 			   AND edh.dbi_flag = ''Y''
10307 			   AND edh.parent_id = :l_cat_id ';
10308        ELSE
10309         l_pc_where :=     ' AND a.category_id = -9 ';
10310      END IF;
10311 /********************************/
10312 
10313     IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
10314 
10315 
10316      /* forming from clause for the tables which is common to all union all */
10317      if l_cat_id is not null then
10318        l_from :=' FROM BIM_I_OBJ_METS_MV a'||l_from||l_pc_from;
10319      else
10320      l_from :=' FROM BIM_I_OBJ_METS_MV a '||l_from;
10321      end if;
10322 
10323 
10324       /* forming where clause which is common to all union all */
10325      l_where :=l_where||'
10326 		 AND a.object_country = :l_country '||
10327 		 l_pc_where;
10328 
10329 
10330     /* forming group by clause for the common columns for all union all */
10331     l_groupby:=' GROUP BY a.source_code_id,camp.object_type_mean, ';
10332 
10333  /*** campaign id null means No drill down and view by is camapign hirerachy*/
10334 
10335   IF l_campaign_id is null THEN
10336 
10337    /*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
10338 
10339      l_sqltext:= l_select_cal||
10340       ' SELECT
10341       a.source_code_id VIEWBYID,camp.name name,
10342       camp.object_id object_id,camp.object_type object_type,
10343       camp.object_type_mean meaning '||
10344       l_select1 ||
10345       l_from || ' ,bim_i_obj_name_mv camp '||
10346      l_where ||l_top_cond||
10347     ' AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
10348       AND a.source_code_id=camp.source_code_id
10349       AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
10350       AND camp.language=USERENV(''LANG'')'||
10351       l_groupby||
10352       ' camp.name,camp.object_id,camp.object_type'||
10353      ' UNION ALL
10354      SELECT
10355       a.source_code_id VIEWBYID,camp.name name,
10356       camp.object_id object_id,camp.object_type object_type,
10357       camp.object_type_mean meaning '||
10358       l_select2 ||
10359       l_from || ' ,bim_i_obj_name_mv camp '||
10360      l_where ||l_top_cond||
10361     ' AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
10362       AND a.source_code_id=camp.source_code_id
10363       AND cal.report_date =trunc(sysdate)
10364       AND camp.language=USERENV(''LANG'')'||
10365       l_groupby||
10366       ' camp.name,camp.object_id,camp.object_type'|| l_select_filter_camp
10367       ;
10368 
10369  ELSE
10370  /* source_code_id is passed from the page, object selected from the page to be drill may be program,campaign,event,one off event*****/
10371 /* appending table in l_form and joining conditon for the bim_i_source_codes */
10372     /* l_from  :=l_from||' ,bim_i_source_codes b';
10373      l_where :=l_where ||
10374               ' AND  a.parent_denorm_type = b.object_type
10375                 AND  a.parent_object_id =  b.object_id
10379 
10376                 AND  b.child_object_id = 0
10377                 AND  b.source_code_id = :l_campaign_id '; */
10378  l_where :=l_where || ' AND  a.immediate_parent_id = :l_campaign_id ';
10380 
10381 -- if program is selected from the page means it may have childern as programs,campaigns,events or one off events
10382 
10383  IF l_object_type='RCAM' THEN
10384  /*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
10385      l_sqltext:= l_select_cal||
10386      /******** inner select start from here */
10387      /* select to get camapigns and programs for current period values */
10388      ' SELECT
10389       a.source_code_id VIEWBYID,  camp.name name, camp.object_id object_id, camp.object_type object_type, camp.object_type_mean meaning '||
10390       l_select1 ||
10391       l_from || ' ,bim_i_obj_name_mv camp '||
10392      l_where ||
10393     ' AND a.source_code_id=camp.source_code_id  AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
10394       AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
10395       AND camp.language=USERENV(''LANG'')'||
10396       l_groupby|| ' camp.name,camp.object_id,camp.object_type'||
10397      ' UNION ALL      /* select to get camapigns and programs for previous period values */
10398      SELECT
10399       a.source_code_id VIEWBYID, camp.name name, camp.object_id object_id, camp.object_type object_type, camp.object_type_mean meaning '||
10400       l_select2 ||
10401       l_from || ' ,bim_i_obj_name_mv camp '||
10402      l_where ||
10403     ' AND a.source_code_id=camp.source_code_id AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
10404       AND cal.report_date =  trunc(sysdate)  AND camp.language=USERENV(''LANG'')'||
10405       l_groupby||
10406       ' camp.name,camp.object_id,camp.object_type'||
10407       l_select_filter_camp;
10408       /*************** if object type is camp then childern are campaign schedules ***/
10409  ELSIF l_object_type='CAMP' THEN
10410 	 l_sqltext:= l_select_cal||
10411 	     /******** inner select start from here */
10412 	     /* select to get camapign schedules for current period values */
10413 	     ' SELECT    a.source_code_id VIEWBYID,   camp.name name,  camp.object_id object_id,    camp.object_type object_type,
10414 	      camp.child_object_usage usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel,  camp.object_type_mean meaning '||
10415 	      l_select1 || l_from || ' ,bim_I_obj_name_mv camp,bim_dimv_media chnl '||
10416 	     l_where ||
10417 	    ' AND camp.source_code_id = a.source_code_id   AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
10418 	      AND camp.object_type =''CSCH''  AND camp.activity_id =chnl.id (+)  AND cal.report_date  in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
10419 	      AND camp.language=USERENV(''LANG'')'||
10420 	      l_groupby||
10421 	      ' camp.name,camp.object_id,camp.object_type,camp.child_object_usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
10422      ' UNION ALL      /* select to get camapign schedules for previous period values */
10423      SELECT
10424       a.source_code_id VIEWBYID, camp.name name, camp.object_id object_id,camp.object_type object_type,
10425       camp.child_object_usage usage, decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel, camp.object_type_mean meaning '||
10426       l_select2 ||
10427       l_from || ' ,bim_I_obj_name_mv camp,bim_dimv_media chnl '||
10428      l_where ||
10429     ' AND camp.source_code_id = a.source_code_id  AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
10430       AND CAMP.object_type =''CSCH''   AND camp.activity_id =chnl.id (+)   AND cal.report_date = trunc(sysdate)
10431       AND camp.language=USERENV(''LANG'')'||
10432       l_groupby||
10433       ' camp.name,camp.object_id,camp.object_type,camp.child_object_usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
10434       l_select_filter_camp;
10435 /*************** if object type is event then childern are event schedules ***/
10436 
10437 
10438 
10439   ELSIF l_object_type='EVEH' THEN
10440  l_sqltext:= l_select_cal||
10441      /******** inner select start from here */
10442      /* select to get event schedules for current period values  */
10443      ' SELECT    a.source_code_id VIEWBYID,  camp.name name,  camp.object_id object_id,  camp.object_type object_type,
10444       camp.object_type_mean meaning '||
10445       l_select1 ||
10449       AND cal.report_date  in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)  AND eve.language=USERENV(''LANG'')'||
10446       l_from || ' ,bim_i_obj_name_mv camp  '||
10447      l_where ||
10448     ' AND  camp.source_code_id = a.source_code_id AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id  AND camp.object_type =''EVEO''
10450       l_groupby||
10451       ' camp.name,camp.object_id,camp.object_type'||
10452      ' UNION ALL      /* select to get event schedules for previous period values */
10453      SELECT
10454       a.source_code_id VIEWBYID, camp.name name, camp.object_id object_id, camp.object_type object_type,  camp.object_type_mean meaning '||
10455       l_select2 ||
10456       l_from || ' ,bim_i_obj_name_mv camp '||
10457       l_where ||
10458     ' AND camp.source_code_id = a.source_code_id  AND  BITAND(cal.record_type_id,1143)= cal.record_type_id AND camp.object_type =''EVEO''
10459       AND cal.report_date =trunc(sysdate)  AND eve.language=USERENV(''LANG'')'||
10460       l_groupby||
10461       ' camp.name,camp.object_id,camp.object_type'||
10462       l_select_filter_camp ;
10463  /*************** if object type is one off event  ***/
10464 /* ELSIF l_object_type='EONE' THEN
10465        l_sqltext:= l_select_cal||
10466      /******** inner select start from here */
10467      /* select to get one off event   */
10468      /*' SELECT   a.source_code_id VIEWBYID, eve.event_offer_name name,a.object_id object_id,a.object_type object_type,l.meaning meaning '||
10469       l_select1 ||
10470       l_from || ' ,ams_event_offers_all_tl eve '||
10471      l_where ||
10472     ' AND  eve.event_offer_id = a.object_id  AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id AND a.object_type =''EONE''
10473       AND cal.report_date  in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)  AND eve.language=USERENV(''LANG'')'||
10474       l_groupby||
10475       ' eve.event_offer_name,a.object_id,a.object_type'||
10476      ' UNION ALL      /* select to get one off previous period values */
10477      /*SELECT   a.source_code_id VIEWBYID, eve.event_offer_name name, a.object_id object_id, a.object_type object_type, l.meaning meaning '||
10478       l_select2 ||
10479       l_from || ' ,ams_event_offers_all_tl eve '||
10480       l_where ||
10481     ' AND eve.event_offer_id = a.object_id AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  AND a.object_type =''EONE''  AND cal.report_date = trunc(sysdate)
10482       AND eve.language=USERENV(''LANG'')'||     l_groupby||
10483       ' eve.event_offer_name,a.object_id,a.object_type'||
10484       l_select_filter_camp; */
10485  END IF;
10486 
10487  END IF;
10488 
10489  /***** END CAMPAIGN HIRERACHY VIEW HANDLING ******************/
10490 
10491  ELSE
10492 
10493  /* view by is product category */
10494  IF (l_view_by ='ITEM+ENI_ITEM_VBH_CAT') THEN
10495 
10496    if l_admin_status='N' then
10497      l_from:=replace(l_from,',fii_time_rpt_struct_v cal');
10498     else
10499      l_from:=null;
10500     end if;
10501 
10502 
10503 /******** handling product category hirerachy ****/
10504 /* picking up value of top level node from product category denorm for category present in  bim_i_obj_mets_mv   */
10505     IF l_cat_id is null then
10506        l_from:=l_from||
10507                ',eni_denorm_hierarchies edh  ,mtl_default_category_sets mdcs ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
10508                    FROM eni_item_vbh_nodes_v e
10509                    WHERE e.top_node_flag=''Y''
10510                    AND e.child_id = e.parent_id) p ';
10511        l_where := l_where||
10512                          ' AND a.category_id = edh.child_id
10513 			   AND edh.object_type = ''CATEGORY_SET''
10514                            AND edh.object_id = mdcs.category_set_id
10515                            AND mdcs.functional_area_id = 11
10516                            AND edh.dbi_flag = ''Y''
10517                            AND edh.parent_id = p.parent_id';
10518        l_col:=' SELECT  /*+ORDERED*/
10519 		   p.value name,
10520                    p.parent_id viewbyid,
10521 		   p.leaf_node_flag leaf_node_flag,
10522 		   null meaning ';
10523         l_groupby := ' GROUP BY p.value,p.parent_id,p.leaf_node_flag ';
10524     ELSE
10525     /* passing id from page and getting immediate child to build hirerachy  */
10526 
10527     /** reassigning value to l_pc_from and l_pc_where for product category hirerachy drill down for values directly assigned to prodcut select from the page*/
10528 
10529      l_pc_from:= l_from||
10530                    ',(select e.id id,e.value value
10531                       from eni_item_vbh_nodes_v e
10532                       where e.parent_id =  :l_cat_id
10533                       AND e.parent_id = e.child_id
10534                       AND leaf_node_flag <> ''Y''
10535                       ) p ';
10536 
10537     l_pc_where :=l_where||
10538                       ' AND a.category_id = p.id ';
10539 
10540     l_from:= l_from||
10541             ',eni_denorm_hierarchies edh
10542             ,mtl_default_category_sets mdc
10543             ,(select e.id,e.value,e.leaf_node_flag
10544               from eni_item_vbh_nodes_v e
10545           where
10546               e.parent_id =:l_cat_id
10547               AND e.id = e.child_id
10548               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
10549       ) p ';
10550 
10551      l_where := l_where||'
10552                   AND a.category_id = edh.child_id
10553                   AND edh.object_type = ''CATEGORY_SET''
10557                   AND edh.parent_id = p.id ';
10554                   AND edh.object_id = mdc.category_set_id
10555                   AND mdc.functional_area_id = 11
10556                   AND edh.dbi_flag = ''Y''
10558 
10559      l_col:=' SELECT  /*+ORDERED*/
10560 		   p.value name,
10561                    p.id viewbyid,
10562 		   p.leaf_node_flag leaf_node_flag,
10563 		   null meaning ';
10564      l_groupby := ' GROUP BY p.value,p.id,p.leaf_node_flag ';
10565     END IF;
10566 /*********************/
10567 
10568            IF l_campaign_id is null then /* no drilll down in campaign hirerachy */
10569 	      IF l_admin_status ='Y' THEN
10570 	      l_from:=' FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
10571                               '||l_from;
10572               l_where := l_where ||l_top_cond||
10573                          '   AND  a.object_country = :l_country';
10574                IF l_cat_id is not null then
10575 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
10576                               '||l_pc_from;
10577 		  l_pc_where := l_pc_where ||l_top_cond||
10578                          ' AND  a.object_country = :l_country';
10579                END IF;
10580               ELSE
10581               l_from:=' FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a   '||l_from;
10582               l_where := l_where ||  ' AND  a.object_country = :l_country';
10583 
10584 		IF l_cat_id is not null then
10585 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a
10586                               '||l_pc_from;
10587 		  l_pc_where := l_pc_where ||
10588                    /*      ' AND a.immediate_parent_id is null   */
10589 			'   AND  a.object_country = :l_country';
10590                END IF;
10591 
10592               END IF;
10593            ELSE
10594 	      l_from := ' FROM   fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a '||l_from ;
10595               l_where  := l_where ||
10596                         '  AND a.source_code_id = :l_campaign_id
10597 			   AND  a.object_country = :l_country' ;
10598               IF l_cat_id is not null then
10599 
10600 	      l_pc_from := ' FROM   fii_time_rpt_struct_v cal,BIM_I_OBJ_METS_MV a '||l_pc_from ;
10601               l_pc_where  := l_pc_where ||
10602                         '  AND a.immediate_parent_id = :l_campaign_id
10603 			   AND  a.object_country = :l_country' ;
10604 	      END IF;
10605 	   END IF;
10606    /* building l_pc_select to get values directly assigned to product category passed from the page */
10607    IF l_cat_id is not null  THEN
10608   l_pc_col:=' SELECT /*+ORDERED*/
10609    		   bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||' name,
10610 		   p.id  viewbyid,
10611 		   ''Y'' leaf_node_flag,
10612 		   null meaning ';
10613      l_pc_groupby := ' GROUP BY p.id ';
10614 
10615    l_pc_select :=
10616               ' UNION ALL ' ||
10617               l_pc_col||
10618               l_select1||
10619 	      l_pc_from||
10620 	      l_pc_where ||' AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
10621 	                   'AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id  '||
10622 	      l_pc_groupby ||
10623 	      ' UNION ALL ' ||
10624 	      l_pc_col||
10625 	      l_select2||
10626 	      l_pc_from||l_inner||
10627 	      l_pc_where ||' AND cal.report_date =trunc(sysdate) '||
10628 	                   'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  '||
10629 			   l_inr_cond||
10630 	      l_pc_groupby ;
10631 
10632    END IF;
10633  ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
10634    /** product category handling**/
10635 
10636    l_union_inc := ',fii_time_rpt_struct_v cal ,BIM_I_OBJ_METS_MV a,eni_denorm_hierarchies edh,mtl_default_category_sets mdcs
10637 ,fnd_territories_tl  d ';
10638 
10639 l_col_inc   :='SELECT /*+ ordered */ decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
10640 a.object_country viewbyid,    null meaning ';
10641 
10642 
10643    IF l_cat_id is null then
10644      l_where := l_where ||l_pc_where;
10645   ELSE
10646      l_from  := l_from ||l_pc_from;
10647      l_where := l_where||l_pc_where;
10648   END IF;
10649     l_col:=' SELECT
10650 		    decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
10651 a.object_country  viewbyid,		     null meaning ';
10652     l_groupby := ' GROUP BY  decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country';
10653     l_from:=' FROM fnd_territories_tl  d '||l_from;
10654 	  IF l_campaign_id is null then
10655 	      IF l_admin_status ='Y' THEN
10656 
10657 	      l_from:=l_from||' ,BIM_I_OBJ_METS_MV a ';
10658               l_where := l_where ||l_top_cond||
10659                          '   AND  a.object_country =d.territory_code(+)
10660 			     AND D.language(+) = userenv(''LANG'') ';
10661               ELSE
10662 
10663               l_from:=l_from||' ,BIM_I_OBJ_METS_MV a ';
10664               l_where := l_where ||
10665 	                 ' AND  a.object_country =d.territory_code(+)
10666 			     AND D.language(+) = userenv(''LANG'') ';
10667               END IF;
10668             ELSE
10669 
10670               l_from := l_from||' ,BIM_I_OBJ_METS_MV a ';
10671               l_where  := l_where ||
10672                         '  AND a.source_code_id = :l_campaign_id
10676 	  IF  l_country <>'N' THEN
10673                            AND  a.object_country =d.territory_code(+)
10674 			     AND D.language(+) = userenv(''LANG'') ';
10675 	  END IF;
10677 	      l_where  := l_where ||' AND  a.object_country = :l_country';
10678           ELSE
10679 	   l_where  := l_where ||' AND  a.object_country <> ''N''';
10680 	  END IF;
10681 
10682 ELSIF (l_view_by ='MEDIA+MEDIA') THEN
10683 /** product category handling**/
10684    IF l_cat_id is null then
10685      l_where := l_where ||l_pc_where;
10686   ELSE
10687      l_from  := l_from ||l_pc_from;
10688      l_where := l_where||l_pc_where;
10689   END IF;
10690     l_col:=' SELECT
10691 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
10692                      null viewbyid,
10693 		     null meaning ';
10694     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
10695     l_from:=' FROM bim_dimv_media d '||l_from;
10696 	  IF l_campaign_id is null then
10697 	      IF l_admin_status ='Y' THEN
10698 
10699 	      /*l_from:=l_from||' ,bim_mkt_chnl_mv a ';*/
10700 
10701  /*	      ,BIM_I_CPL_CHNL_MV  can't be used since object_id ,object_type is not present*/
10702 
10703 
10704 	      l_from:=l_from||' ,BIM_OBJ_CHNL_MV a  ';
10705               l_where := l_where ||
10706                          ' AND  d.id (+)= a.activity_id
10707 			   AND  a.immediate_parent_id is null
10708 			   AND  a.object_country = :l_country';
10709               ELSE
10710 
10711               l_from:=l_from||' ,BIM_OBJ_CHNL_MV a ';
10712               l_where := l_where ||
10713 	               /* AND  a.parent_object_id is null          */
10714                          '  AND  d.id (+)= a.activity_id
10715    		           AND  a.object_country = :l_country';
10716 
10717 
10718               END IF;
10719             ELSE
10720 
10721               l_from := l_from||' ,BIM_OBJ_CHNL_MV a ';
10722               l_where  := l_where ||
10723                         '  AND a.source_code_id = :l_campaign_id
10724                            AND  d.id (+)= a.activity_id
10725 			   AND  a.object_country = :l_country';
10726 	  END IF;
10727 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
10728 /** product category handling**/
10729    IF l_cat_id is null then
10730      l_where := l_where ||l_pc_where;
10731   ELSE
10732      l_from  := l_from ||l_pc_from;
10733      l_where := l_where||l_pc_where;
10734   END IF;
10735     l_col:=' SELECT
10736 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
10737                      null viewbyid,
10738 		     null meaning ';
10739     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
10740     l_from:=' FROM bis_areas_v d '||l_from;
10741 	  IF l_campaign_id is null then
10742 	      IF l_admin_status ='Y' THEN
10743 
10744 	      /*l_from:=l_from||' ,bim_mkt_chnl_mv a ';*/
10745 
10746  /*	      ,BIM_I_CPL_CHNL_MV  can't be used since object_id ,object_type is not present*/
10747 
10748 
10749                l_from:=l_from||' ,BIM_OBJ_REGN_MV a  ';
10750               l_where := l_where ||
10751                          ' AND  d.id (+)= a.object_region
10752 			   AND  a.immediate_parent_id is null
10753 			   AND  a.object_country = :l_country';
10754               ELSE
10755 
10756               l_from:=l_from||' ,BIM_OBJ_REGN_MV a ';
10757               l_where := l_where ||
10758 	               /* AND  a.parent_object_id is null          */
10759                          '  AND  d.id (+)= a.object_region
10760    		           AND  a.object_country = :l_country';
10761 
10762 
10763               END IF;
10764             ELSE
10765 
10766               l_from := l_from||' ,BIM_OBJ_REGN_MV a ';
10767               l_where  := l_where ||
10768                         '  AND a.source_code_id = :l_campaign_id
10769                            AND  d.id (+)= a.object_region
10770 			   AND  a.object_country = :l_country';
10771 	  END IF;
10772 END IF;
10773 
10774 
10775 
10776 /* combine sql one to pick up current period values and  sql two to pick previous period values */
10777  l_select := l_col||
10778               l_select1||
10779 	      l_from||
10780 	      l_where ||' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
10781 	      'AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id  '||
10782 	      l_groupby ||
10783 	      ' UNION ALL ';
10784 
10785 if (l_view_by ='GEOGRAPHY+COUNTRY'  and l_campaign_id is null and l_cat_id is not null and l_admin_status = 'Y') then
10786 l_select :=   l_select||l_col_inc||l_select2||' from '||l_inner||l_union_inc ;
10787  else
10788 l_select :=   l_select||l_col||l_select2||l_from||l_inner;
10789 end if;
10790 l_select :=   l_select||
10791 	      l_where ||' AND cal.report_date =trunc(sysdate) '||
10792 	      'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  '||
10793 	      l_inr_cond||
10794 	      l_groupby||
10795 	      l_pc_select /* l_pc_select only applicable when product category is not all and view by is product category */
10796 	      ;
10797 
10798 
10799 /* prepare final sql */
10800 
10801  l_sqltext:= l_select_cal1||
10802              l_select||
10803 	     l_select_filter;
10804   END IF;
10805 
10809    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
10806    x_custom_sql := l_sqltext;
10807    l_custom_rec.attribute_name := ':l_record_type';
10808    l_custom_rec.attribute_value := l_record_type_id;
10810    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
10811    x_custom_output.EXTEND;
10812    x_custom_output (1) := l_custom_rec;
10813    l_custom_rec.attribute_name := ':l_resource_id';
10814    l_custom_rec.attribute_value := get_resource_id;
10815    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
10816    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
10817    x_custom_output.EXTEND;
10818    x_custom_output (2) := l_custom_rec;
10819    l_custom_rec.attribute_name := ':l_admin_flag';
10820    l_custom_rec.attribute_value := get_admin_status;
10821    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
10822    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
10823    x_custom_output.EXTEND;
10824    x_custom_output (3) := l_custom_rec;
10825    l_custom_rec.attribute_name := ':l_country';
10826    l_custom_rec.attribute_value := l_country;
10827    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
10828    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
10829    x_custom_output.EXTEND;
10830    x_custom_output (4) := l_custom_rec;
10831    l_custom_rec.attribute_name := ':l_cat_id';
10832    l_custom_rec.attribute_value := l_cat_id;
10833    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
10834    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
10835    x_custom_output.EXTEND;
10836    x_custom_output (5) := l_custom_rec;
10837    l_custom_rec.attribute_name := ':l_campaign_id';
10838    l_custom_rec.attribute_value := l_campaign_id;
10839    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
10840    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
10841    x_custom_output.EXTEND;
10842    x_custom_output (6) := l_custom_rec;
10843    write_debug ('GET_RPL_CPL_SQL', 'QUERY', '_', l_sqltext);
10844 EXCEPTION
10845    WHEN OTHERS
10846    THEN
10847       l_sql_errm := SQLERRM;
10848       write_debug ('GET_RPL_CPL_SQL', 'ERROR', l_sql_errm, l_sqltext);
10849 END GET_RPL_CPL_SQL;
10850 
10851 
10852 PROCEDURE GET_LEAD_OPTY_CONV_SQL (
10853    p_page_parameter_tbl   IN              bis_pmv_page_parameter_tbl,
10854    x_custom_sql           OUT NOCOPY      VARCHAR2,
10855    x_custom_output        OUT NOCOPY      bis_query_attributes_tbl
10856 )
10857 IS
10858    l_sqltext                      VARCHAR2 (20000);
10859    iflag                          NUMBER;
10860    l_period_type_hc               NUMBER;
10861    l_as_of_date                   DATE;
10862    l_period_type                  VARCHAR2 (2000);
10863    l_record_type_id               NUMBER;
10864    l_comp_type                    VARCHAR2 (2000);
10865    l_country                      VARCHAR2 (4000);
10866    l_view_by                      VARCHAR2 (4000);
10867    l_sql_errm                     VARCHAR2 (4000);
10868    l_previous_report_start_date   DATE;
10869    l_current_report_start_date    DATE;
10870    l_previous_as_of_date          DATE;
10871    l_period_type_id               NUMBER;
10872    l_user_id                      NUMBER;
10873    l_resource_id                  NUMBER;
10874    l_time_id_column               VARCHAR2 (1000);
10875    l_admin_status                 VARCHAR2 (20);
10876    l_admin_flag                   VARCHAR2 (1);
10877    l_admin_count                  NUMBER;
10878    l_rsid                         NUMBER;
10879    l_curr_aod_str                 VARCHAR2 (80);
10880    l_country_clause               VARCHAR2 (4000);
10881    l_access_clause                VARCHAR2 (4000);
10882    l_access_table                 VARCHAR2 (4000);
10883    l_cat_id                       VARCHAR2 (50)        := NULL;
10884    l_campaign_id                  VARCHAR2 (50)        := NULL;
10885    l_select                       VARCHAR2 (20000); -- to build  inner select to pick data from mviews
10886    l_pc_select                    VARCHAR2 (20000); -- to build  inner select to pick data directly assigned to the product category hirerachy
10887    l_select_cal                   VARCHAR2 (20000); -- to build  select calculation part
10888    l_select_filter                VARCHAR2 (20000); -- to build  select filter part
10889    l_from                         VARCHAR2 (20000);   -- assign common table in  clause
10890    l_where                        VARCHAR2 (20000);  -- static where clause
10891    l_groupby                      VARCHAR2 (2000);  -- to build  group by clause
10892    l_pc_from                      VARCHAR2 (20000);   -- from clause to handle product category
10893    l_pc_where                     VARCHAR2 (20000);   --  where clause to handle product category
10894    l_filtercol                    VARCHAR2 (2000);
10895    l_pc_col                       VARCHAR2(200);
10896    l_pc_groupby                   VARCHAR2(200);
10897    l_view                         VARCHAR2 (20);
10898    l_comm_cols                    VARCHAR2 (20000);
10899    l_comm2_cols                   VARCHAR2 (20000);
10900    l_view_disp                    VARCHAR2(100);
10901    l_url_str                      VARCHAR2(1000);
10902    l_url_str_csch                 varchar2(1000);
10903    l_url_str_csch_jtf             varchar2(3000);
10904    l_url_str_type                 varchar2(3000);
10905    l_csch_chnl                    varchar2(100);
10906    l_camp_sel_col                 varchar2(100);
10910    /* variables to hold columns names in l_select clauses */
10907    l_camp_groupby_col             varchar2(100);
10908    l_top_cond                     VARCHAR2(100);
10909    l_meaning                      VARCHAR2 (20); -- assigning default value
10911    l_col                          VARCHAR2(1000);
10912    /* cursor to get type of object passed from the page ******/
10913     cursor get_obj_type
10914     is
10915     select object_type
10916     from bim_i_source_codes
10917     where source_code_id=replace(l_campaign_id,'''');
10918     /*********************************************************/
10919    l_custom_rec                   bis_query_attributes;
10920    l_object_type                  varchar2(30);
10921    l_url_link                     varchar2(200) ;
10922    l_url_camp1                    varchar2(3000);
10923    l_url_camp2                    varchar2(3000);
10924    l_dass                         varchar2(100);   -- variable to store value for  directly assigned lookup value
10925  --  l_una                        varchar2(100);   -- variable to store value for  Unassigned lookup value
10926    l_leaf_node_flag               varchar2(25);   -- variable to store value leaf_node_flag column in case of product category
10927 l_curr VARCHAR2(50);
10928 l_curr_suffix VARCHAR2(50);
10929 l_col_id NUMBER;
10930 l_area VARCHAR2(300);
10931 l_report_name VARCHAR2(300);
10932 l_media VARCHAR2(300);
10933 BEGIN
10934    x_custom_output := bis_query_attributes_tbl ();
10935    l_custom_rec := bis_pmv_parameters_pub.initialize_query_type;
10936 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
10937    			     			      l_as_of_date,
10938 			    			      l_period_type,
10939 				                      l_record_type_id,
10940 				                      l_comp_type,
10941 				                      l_country,
10942 						      l_view_by,
10943 						      l_cat_id,
10944 						      l_campaign_id,
10945 						      l_curr,
10946 						      l_col_id,
10947 						      l_area,
10948 						      l_media,
10949 						      l_report_name
10950 				                      );
10951 
10952 
10953    l_meaning:=' null meaning '; -- assigning default value
10954    l_url_camp1:=',null';
10955    l_url_camp2:=',null';
10956    IF l_country IS NULL THEN
10957       l_country := 'N';
10958    END IF;
10959 /** to add meaning in select clause only in case of campaign view by */
10960   IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
10961   l_meaning:=' ,meaning ';
10962   l_filtercol:=',meaning ';
10963   ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
10964   l_filtercol:=',leaf_node_flag ';
10965   l_meaning:=',null meaning ';
10966   else
10967   l_meaning:=',null meaning ';
10968   end if;
10969 
10970   /*** to  assigned URL **/
10971 
10972 if l_campaign_id is not null then
10973 -- checking for the object type passed from page
10974  for i in get_obj_type
10975  loop
10976  l_object_type:=i.object_type;
10977  end loop;
10978 end if;
10979 
10980    l_url_str  :='pFunctionName=BIM_I_MKTG_LEAD_OPTY_CONV&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
10981    --l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objId=';
10982    l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
10983    l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
10984    l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
10985    &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
10986 
10987 IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
10988    l_url_link :=',decode(leaf_node_flag,''Y'',NULL,'||''''||l_url_str||''''||' ) ';
10989    l_view_disp:='viewby';
10990    l_leaf_node_flag :=' ,leaf_node_flag ';
10991     ELSIF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
10992      l_camp_sel_col :=' ,object_id
10993                        ,object_type
10994                        ';
10995      l_camp_groupby_col :=',object_id,object_type ';
10996      l_url_link := ' ,null ';
10997      l_view_disp := 'viewby';
10998 
10999      IF (l_campaign_id is  null or l_object_type='RCAM') then
11000 	l_url_camp1:=', decode(object_type,''EONE'',NULL,'||''''||l_url_str||''''||' )';
11001      ELSIF l_object_type='CAMP' THEN
11002        	l_url_camp2:=','||''''||l_url_str_type||''''||'||object_id ';
11003 	--l_url_camp1:=',decode(usage,''LITE'','||''''||l_url_str_csch||''''||'||object_id,'||''''||l_url_str_csch_jtf||''''||'||object_id)';
11004 	l_url_camp1:=', '||''''||l_url_str_csch||''''||'||object_id';
11005 	l_csch_chnl:='|| '' - '' || channel';
11006 	l_camp_sel_col :=l_camp_sel_col|| ',usage,channel';
11007 	l_camp_groupby_col :=l_camp_groupby_col||',usage,channel';
11008      end if;
11009     ELSE
11010       --l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
11011       l_url_link:=' ,null ';
11012       l_view_disp:='viewby';
11013    END IF;
11014 /* l_select_cal is common part of select statement for all view by to calculate grand totals and change */
11015  l_select_cal :=' SELECT '|| l_view_disp ||',viewbyid,bim_attribute2'||l_csch_chnl ||' bim_attribute2,bim_attribute3,bim_attribute4,bim_attribute5,bim_attribute6,bim_attribute7,bim_attribute8
11019 	 ,bim_grand_total1 bim_grand_total9,bim_grand_total4 bim_grand_total10,bim_grand_total11
11016 	 ,bim_attribute4 bim_attribute9,bim_attribute7 bim_attribute10,bim_attribute3 bim_attribute11,bim_attribute6 bim_attribute12,bim_attribute3 bim_attribute13,bim_attribute18
11017 	 ,bim_attribute6 bim_attribute14 '||l_url_link||' bim_attribute19 '||l_url_camp1|| ' bim_attribute22 '||
11018 	 l_url_camp2||' bim_attribute23,bim_attribute20,bim_attribute21,bim_grand_total1,bim_grand_total2,bim_grand_total3,bim_grand_total4,bim_grand_total5,bim_grand_total6,bim_grand_total7,bim_grand_total8
11020           FROM
11021 	 (
11022             SELECT
11023             name    VIEWBY'||l_leaf_node_flag||'
11024             ,meaning BIM_ATTRIBUTE2'||l_camp_sel_col||
11025             ',leads_converted BIM_ATTRIBUTE3
11026             ,DECODE(prev_leads_converted,0,NULL,((leads_converted - prev_leads_converted)/prev_leads_converted)*100) BIM_ATTRIBUTE4
11027 	    ,DECODE(leads_converted,0,NULL,leads_conversion_time/leads_converted) BIM_ATTRIBUTE5
11028             ,aleads_converted BIM_ATTRIBUTE6
11029             ,DECODE(prev_aleads_converted,0,NULL,((aleads_converted - prev_aleads_converted)/prev_aleads_converted)*100) BIM_ATTRIBUTE7
11030             ,DECODE(aleads_converted,0,NULL,aleads_conversion_time/aleads_converted) BIM_ATTRIBUTE8
11031 	    ,decode((prior_open+leads),0,0,100*(leads_converted/(prior_open+leads))) BIM_ATTRIBUTE18
11032 	    ,leads_conv_customer BIM_ATTRIBUTE20
11033             ,leads_conv_prospect BIM_ATTRIBUTE21
11034             ,sum(leads_converted) over() BIM_GRAND_TOTAL1
11035             ,decode(sum(prev_leads_converted) over(),0,null,(((sum(leads_converted- prev_leads_converted) over())/sum(prev_leads_converted)over ())*100)) BIM_GRAND_TOTAL2
11036             ,DECODE(sum(leads_converted) over (),0,NULL,sum(leads_conversion_time) over()/sum(leads_converted)over()) BIM_GRAND_TOTAL3
11037             ,sum(aleads_converted) over () BIM_GRAND_TOTAL4
11038             ,decode(sum(prev_aleads_converted) over(),0,null,(((sum(aleads_converted - prev_aleads_converted) over())/sum(prev_aleads_converted)over ())*100)) BIM_GRAND_TOTAL5
11039             ,DECODE(sum(aleads_converted) over (),0,NULL,sum(aleads_conversion_time) over()/sum(aleads_converted)over()) BIM_GRAND_TOTAL6
11040 	    ,sum(leads_conv_customer) over()     bim_grand_total7
11041             ,sum(leads_conv_prospect) over()     bim_grand_total8
11042 	    ,decode(sum(prior_open+leads) over(),0,0,100*(sum(leads_converted) over()/sum(prior_open+leads) over())) BIM_GRAND_TOTAL11
11043             ,VIEWBYID
11044              FROM
11045               (   SELECT viewbyid,name'||l_leaf_node_flag||l_meaning||l_camp_sel_col||
11046 		    ',sum(leads_converted) leads_converted,sum(leads_conversion_time) leads_conversion_time,sum(aleads_converted) aleads_converted,sum(aleads_conversion_time) aleads_conversion_time
11047  	              ,sum(leads_conv_customer) leads_conv_customer,sum(leads_conv_prospect) leads_conv_prospect,sum(prev_leads_converted) prev_leads_converted,sum(prev_aleads_converted) prev_aleads_converted
11048         	       ,sum(prior_open) prior_open,sum(leads)  leads
11049                   FROM
11050           	  ( ';
11051 /* l_comm_cols  contains column information common to all select statement for all view by */
11052 
11053 l_comm_cols:=    ' , sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_converted,0)) leads_converted ,
11054                    sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_conversion_time,0)) leads_conversion_time,
11055      		   sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.aleads_converted,0))  aleads_converted  ,
11056      		   sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.aleads_conversion_time,0)) aleads_conversion_time,
11057 		   sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_conv_customer,0)) leads_conv_customer,
11058                    sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_conv_prospect,0)) leads_conv_prospect,
11059 	           sum(DECODE(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.leads_converted,0))   prev_leads_converted,
11063 /* l_comm_cols  contains column information common to all select statement for all view by */
11060 		   sum(DECODE(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.aleads_converted,0)) prev_aleads_converted,
11061 		   sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads,0))  leads,
11062 		   0 prior_open ';
11064 
11065 l_comm2_cols:=  ', 0 leads_converted,0 leads_conversion_time,0 aleads_converted,0 aleads_conversion_time,0 leads_conv_customer,0  leads_conv_prospect,
11066 0  prev_leads_converted, 0 prev_aleads_converted,0 leads,sum(a.leads-(a.leads_closed+a.leads_dead+a.leads_converted))  prior_open ';
11067 
11068 /* l_from contains time dimension table common to all select statement for all view by */
11069  l_from  :=',fii_time_rpt_struct_v cal ';
11070  /* l_where contains where clause to join time dimension table common to all select statement for all view by */
11071  l_where :=' WHERE a.time_id = cal.time_id  AND  a.period_type_id = cal.period_type_id AND  cal.calendar_id= -1 ';
11072  /* l_select_filter contains group by and filter clause to remove uneccessary records with zero values */
11073 l_select_filter := ' ) GROUP BY viewbyid,name '||l_filtercol||l_camp_groupby_col||
11074                   ')
11075 		   )
11076          WHERE
11077            bim_attribute3 <> 0
11078            &ORDER_BY_CLAUSE ';
11079 
11080    /* get_admin_status to check current user is admin or not */
11081 
11082    l_admin_status := get_admin_status;
11083 
11084 /*********************** security handling ***********************/
11085 
11086 IF   l_campaign_id is null THEN /******* no security checking at child level ********/
11087 	IF   l_admin_status = 'N' THEN
11088 		IF  l_view_by = 'CAMPAIGN+CAMPAIGN' then
11089 		/*************** program view is enable **************/
11090 			IF l_prog_view='Y' then
11091 				l_view := ',''RCAM''';
11092 				l_from := l_from ||',bim_i_top_objects ac ';
11093 				l_where := l_where ||' AND a.source_code_id=ac.source_code_id
11094 							AND ac.resource_id = :l_resource_id ';
11095 				/************************************************/
11096 			ELSE
11097 
11098 				l_from := l_from ||',ams_act_access_denorm ac,bim_i_source_codes src ';
11099 				l_where := l_where ||' AND a.source_code_id=src.source_code_id
11100 						       AND src.object_id=ac.object_id
11101 						       AND src.object_type=ac.object_type
11102 						       AND src.object_type NOT IN (''RCAM'')
11103 						       AND ac.resource_id = :l_resource_id ';
11104 
11105 			END IF;
11106 
11107 		ELSE
11108 			l_from := l_from ||',bim_i_top_objects ac ';
11109 			l_where := l_where ||' AND a.source_code_id=ac.source_code_id
11110 									AND ac.resource_id = :l_resource_id ';
11111 		END IF;
11112 
11113 	ELSE
11114 		IF l_view_by = 'CAMPAIGN+CAMPAIGN' then
11115 			IF  l_prog_view='Y' THEN
11116 				l_view := ',''RCAM''';
11117 				l_top_cond :=' AND a.immediate_parent_id is null ';
11118 			END IF;
11119 		ELSE
11120 			/******** to append parent object id is null for other view by (country and product category) ***/
11121 			l_top_cond :=' AND a.immediate_parent_id is null ';
11122 			/***********/
11123 		END IF;
11124 	END IF;
11125 END IF;
11126 
11127 /************************************************************************/
11128 
11129    /* product category handling */
11130      IF  l_cat_id is not null then
11131          l_pc_from   :=  ', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
11132 	 l_pc_where  :=  ' AND a.category_id = edh.child_id AND edh.object_type = ''CATEGORY_SET''  AND edh.object_id = mdcs.category_set_id
11133 			   AND mdcs.functional_area_id = 11  AND edh.dbi_flag = ''Y''  AND edh.parent_id = :l_cat_id ';
11134        ELSE
11135         l_pc_where :=     ' AND a.category_id = -9 ';
11136      END IF;
11137 /********************************/
11138 
11139 IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
11140 
11141 	/* forming from clause for the tables which is common to all union all */
11142 
11143 	IF l_cat_id IS NOT NULL THEN
11144 
11148 		l_from :=' FROM bim_i_obj_mets_mv a , bim_i_obj_name_mv name '||l_from;
11145 		l_from :=' FROM bim_i_obj_mets_mv a , bim_i_obj_name_mv name '||l_from||l_pc_from;
11146 
11147 	ELSE
11149 
11150 	END IF;
11151 
11152 	/* forming where clause which is common to all union all */
11153 	IF l_prog_view = 'Y' THEN
11154 
11155 		l_where :=l_where||' AND a.source_code_id = name.source_code_id
11156 							 AND name.language=USERENV(''LANG'')
11157 							 AND a.object_country = :l_country'||
11158 							 l_pc_where;
11159 	ELSE
11160 		l_where :=l_where||' AND a.source_code_id = name.source_code_id
11161 							 AND name.language=USERENV(''LANG'')
11162 							 AND name.object_type NOT IN (''RCAM'')
11163 							 AND a.object_country = :l_country'||
11164 							 l_pc_where;
11165 
11166 	END IF;
11167 
11168 	/* forming group by clause for the common columns for all union all */
11169 	l_groupby:=' GROUP BY a.source_code_id,name.object_type_mean,name.name,name.object_id,name.object_type ';
11170 
11171 	/*** campaign id null means No drill down and view by is camapign hirerachy*/
11172 	IF l_campaign_id is null THEN
11173 		/*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
11174 		l_sqltext:= l_select_cal||
11175 		/******** inner select start from here */
11176 		/* select to get camapigns and programs  */
11177 		' SELECT
11178 		a.source_code_id VIEWBYID,
11179 		name.name name,
11180 		name.object_id object_id,
11181 		name.object_type object_type,
11182 		name.object_type_mean meaning '||
11183 		l_comm_cols ||
11184 		l_from || ' '||
11185 		l_where ||l_top_cond||
11186 		'AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
11187 		AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id'||
11188 		l_groupby||
11189 		' UNION ALL
11190 		SELECT
11191 		a.source_code_id VIEWBYID,
11192 		name.name name,
11193 		name.object_id object_id,
11194 		name.object_type object_type,
11195 		name.object_type_mean meaning  '||
11196 		l_comm2_cols ||
11197 		l_from ||
11198 		l_where ||l_top_cond||
11199 		' AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
11200 		AND BITAND(cal.record_type_id,1143)= cal.record_type_id'||
11201 		l_groupby||
11202 		l_select_filter /* appending filter clause */
11203 		;
11204 	ELSE
11205 
11206 		/* source_code_id is passed from the page, object selected from the page to be drill may be program,campaign,event,one off event*****/
11207 		/* appending table in l_form and joining conditon for the bim_i_source_codes */
11208 
11209 		l_where :=l_where ||' AND a.immediate_parent_id = :l_campaign_id ';
11210 
11211 		-- if program is selected from the page means it may have childern as programs,campaigns,events or one off events
11212 
11213 		IF l_object_type IN ('RCAM','EVEH') THEN
11214 			/*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
11215 			l_sqltext:= l_select_cal||
11216 			/******** inner select start from here */
11217 			' SELECT a.source_code_id VIEWBYID
11218 			,name.name name
11219 			,name.object_id object_id
11220 			,name.object_type object_type
11224 			l_where ||
11221 			,name.object_type_mean meaning '||
11222 			l_comm_cols ||
11223 			l_from||
11225 			' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
11226 			AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
11227 			AND name.language=USERENV(''LANG'')'||
11228 			l_groupby||
11229 			'UNION ALL
11230 			 SELECT a.source_code_id VIEWBYID
11231 			 ,name.name name
11232 			,name.object_id object_id
11233 			,name.object_type object_type
11234 			,name.object_type_mean meaning '||
11235 			l_comm2_cols ||
11236 			l_from ||
11237 			l_where ||
11238 			' AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
11239 			AND BITAND(cal.record_type_id,1143)= cal.record_type_id '||
11240 			l_groupby||
11241 			l_select_filter ;
11242 
11243 			/*************** if object type is camp then childern are campaign schedules ***/
11244 		ELSIF l_object_type='CAMP' THEN
11245 			l_sqltext:= l_select_cal||
11246 			/******** inner select start from here */
11247 			/* select to get camapign schedules  */
11248 			' SELECT
11249 			a.source_code_id VIEWBYID
11250 			,name.name name
11251 			,name.object_id object_id
11252 			,name.object_type object_type
11253 			,name.child_object_usage usage
11254 			,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel
11255 			,name.object_type_mean meaning'||
11256 			l_comm_cols ||
11257 			l_from || ' , bim_dimv_media chnl  '||
11258 			l_where ||
11259 			' AND name.activity_id =chnl.id (+)
11260 			AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
11261 			AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id '||
11262 			l_groupby||' , name.child_object_usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||'
11263 			UNION ALL
11264 			SELECT
11265 			a.source_code_id VIEWBYID
11266 			,name.name name
11267 			,name.object_id object_id
11268 			,name.object_type object_type
11269 			,name.child_object_usage usage
11270 			,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel
11271 			,name.object_type_mean meaning '||
11272 			l_comm2_cols ||
11273 			l_from || ' , bim_dimv_media chnl  '||
11274 			l_where ||
11275 			' AND name.activity_id =chnl.id (+)
11276 			AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
11277 			AND BITAND(cal.record_type_id,1143)= cal.record_type_id'||
11278 			l_groupby||' , name.child_object_usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
11279 			l_select_filter ;
11280 		END IF;
11281 	END IF;
11282 	/***** END CAMPAIGN HIRERACHY VIEW HANDLING ******************/
11283  ELSE
11284  /* view by is product category */
11285  IF (l_view_by ='ITEM+ENI_ITEM_VBH_CAT') THEN
11286 --changing l_from to have fii_time_rpt_struct_v first table in form clause to provide ordered hint in sql
11287   if l_admin_status='N' then
11291  end if;
11288  l_from:=replace(l_from,',fii_time_rpt_struct_v cal');
11289  else
11290  l_from:=null;
11292 /******** handling product category hirerachy ****/
11293 /* picking up value of top level node from product category denorm for category present in  bim_i_obj_mets_mv   */
11294     IF l_cat_id is null then
11295        l_from:=l_from||
11296                ',eni_denorm_hierarchies edh
11297                 ,mtl_default_category_sets mdcs
11298                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
11299                    FROM eni_item_vbh_nodes_v e
11300                    WHERE e.top_node_flag=''Y''
11301                    AND e.child_id = e.parent_id) p ';
11302        l_where := l_where||
11303                          ' AND a.category_id = edh.child_id
11304 			   AND edh.object_type = ''CATEGORY_SET''
11305                            AND edh.object_id = mdcs.category_set_id
11306                            AND mdcs.functional_area_id = 11
11307                            AND edh.dbi_flag = ''Y''
11308                            AND edh.parent_id = p.parent_id';
11309        l_col:=' SELECT /*+ORDERED*/
11310 		   p.value name,
11311                    p.parent_id viewbyid,
11312 		   p.leaf_node_flag leaf_node_flag,
11313 		   null meaning ';
11314         l_groupby := ' GROUP BY p.value,p.parent_id,p.leaf_node_flag ';
11315     ELSE
11316     /* passing id from page and getting immediate child to build hirerachy  */
11317 
11318     /** reassigning value to l_pc_from and l_pc_where for product category hirerachy drill down for values directly assigned to prodcut select from the page*/
11319 
11320      l_pc_from:= l_from||
11321                    ',(select e.id id,e.value value
11322                       from eni_item_vbh_nodes_v e
11323                       where e.parent_id =  :l_cat_id
11324                       AND e.parent_id = e.child_id
11325                       AND leaf_node_flag <> ''Y''
11326                       ) p ';
11327 
11328     l_pc_where :=l_where||
11329                       ' AND a.category_id = p.id ';
11330 
11331     l_from:= l_from||
11332             ',eni_denorm_hierarchies edh
11333             ,mtl_default_category_sets mdc
11334             ,(select e.id,e.value,e.leaf_node_flag leaf_node_flag
11335               from eni_item_vbh_nodes_v e
11336           where
11337               e.parent_id =:l_cat_id
11338               AND e.id = e.child_id
11339               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
11340       ) p ';
11341 
11342      l_where := l_where||'
11343                   AND a.category_id = edh.child_id
11344                   AND edh.object_type = ''CATEGORY_SET''
11345                   AND edh.object_id = mdc.category_set_id
11346                   AND mdc.functional_area_id = 11
11347                   AND edh.dbi_flag = ''Y''
11348                   AND edh.parent_id = p.id ';
11349 
11350      l_col:=' SELECT /*+ORDERED*/
11351 		   p.value name,
11352                    p.id viewbyid,
11353 		   p.leaf_node_flag leaf_node_flag,
11354 		   null meaning ';
11355      l_groupby := ' GROUP BY p.value,p.id,p.leaf_node_flag ';
11356     END IF;
11357 /*********************/
11358 
11359            IF l_campaign_id is null then /* no drilll down in campaign hirerachy */
11360 	      IF l_admin_status ='Y' THEN
11361               l_from:=' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
11362                               '||l_from;
11366 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
11363               l_where := l_where ||l_top_cond||
11364                          '  AND  a.object_country = :l_country';
11365                IF l_cat_id is not null then
11367                               '||l_pc_from;
11368 		  l_pc_where := l_pc_where ||l_top_cond||
11369                          ' AND  a.object_country = :l_country';
11370                END IF;
11371               ELSE
11372               l_from:=' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
11373                              '||l_from;
11374               l_where := l_where ||
11375 	                   ' AND  a.object_country = :l_country';
11376 
11377 		IF l_cat_id is not null then
11378 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
11379                               '||l_pc_from;
11380 		  l_pc_where := l_pc_where ||
11381                          ' AND  a.object_country = :l_country';
11382                END IF;
11383 
11384               END IF;
11385            ELSE
11386               l_from := ' FROM   fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a '||l_from ;
11387               l_where  := l_where ||
11388                         '  AND  a.source_code_id = :l_campaign_id
11389 			   AND  a.object_country = :l_country' ;
11390               IF l_cat_id is not null then
11391 	      l_pc_from := ' FROM   fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a '||l_pc_from ;
11392               l_pc_where  := l_pc_where ||
11393                         '  AND  a.source_code_id = :l_campaign_id
11394 			   AND  a.object_country = :l_country' ;
11395 	      END IF;
11396 	   END IF;
11397    /* building l_pc_select to get values directly assigned to product category passed from the page */
11398    IF l_cat_id is not null  THEN
11399        	  l_pc_col:=' SELECT /*+ORDERED*/
11400 		   bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||' name,
11401                    p.id  viewbyid,
11402 		   ''Y'' leaf_node_flag,
11403 		   null meaning ';
11404      l_pc_groupby := ' GROUP BY p.id';
11405 
11406   l_pc_select :=
11407               ' UNION ALL ' ||
11408               l_pc_col||
11409               l_comm_cols||
11410 	      l_pc_from||
11411 	      l_pc_where ||'  AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
11412 	      AND BITAND(cal.record_type_id,1143)= cal.record_type_id'||
11413 	      l_pc_groupby||
11414 	      ' UNION ALL ' ||
11415               l_pc_col||
11416               l_comm2_cols||
11417 	      l_pc_from||
11418 	      l_pc_where ||'
11419 	      AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
11420               AND BITAND(cal.record_type_id,1143)= cal.record_type_id'||
11421 	      l_pc_groupby ;
11422    END IF;
11423  ELSIF (l_view_by ='CUSTOMER CATEGORY+CUSTOMER CATEGORY') THEN
11424   /** product category handling**/
11425    IF l_cat_id is null then
11426      l_where := l_where ||l_pc_where;
11427   ELSE
11428      l_from  := l_from ||l_pc_from;
11429      l_where := l_where||l_pc_where;
11430   END IF;
11431     l_col:=' SELECT
11432 		   decode(d.customer_category_name,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.customer_category_name) name,
11433                    null viewbyid,
11434 		   null meaning ';
11435     l_groupby := ' GROUP BY decode(d.customer_category_name,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.customer_category_name) ';
11436     l_from:=' FROM bic_cust_category_v d '||l_from;
11437 
11438 	  IF l_campaign_id is null then
11439 	      IF l_admin_status ='Y' THEN
11440 	      l_from:=l_from||' ,bim_mkt_ld_ccat_mv a ';
11441               l_where := l_where ||
11442                          ' AND d.customer_category_code (+) = a.cust_category
11443 			   AND  a.object_country = :l_country';
11444               ELSE
11445               l_from:=l_from||' ,bim_obj_ld_ccat_mv a ';
11446               l_where := l_where ||
11447 	                 ' AND d.customer_category_code (+) = a.cust_category
11448 			   AND  a.object_country = :l_country ';
11449               END IF;
11450             ELSE
11451               l_from := l_from||' ,bim_obj_ld_ccat_mv a ' ; --, bim_i_source_codes b ';
11452               l_where  := l_where ||
11453                         '  AND  a.source_code_id = :l_campaign_id
11454                            AND  d.customer_category_code (+) = a.cust_category
11455 			   AND  a.object_country = :l_country' ;
11456 	  END IF;
11457  ELSIF (l_view_by ='BIM_LEAD_ATTRIBUTES+BIM_LEAD_SOURCE') THEN
11458    /** product category handling**/
11459    IF l_cat_id is null then
11460      l_where := l_where ||l_pc_where;
11464   END IF;
11461   ELSE
11462      l_from  := l_from ||l_pc_from;
11463      l_where := l_where||l_pc_where;
11465     l_col:=' SELECT
11466 		    decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) name,
11467                      null viewbyid,
11468 		     null meaning ';
11469     l_groupby := ' GROUP BY  decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) ';
11470     l_from:=' FROM as_lookups d '||l_from;
11471 	  IF l_campaign_id is null then
11472 	      IF l_admin_status ='Y' THEN
11473 	      l_from:=l_from||' ,bim_mkt_ld_src_mv a ';
11474               l_where := l_where ||
11475                          ' AND  a.lead_source = d.lookup_code(+)
11476 	                   AND  d.lookup_type (+) = ''SOURCE_SYSTEM''
11477 			   AND  a.object_country = :l_country';
11478               ELSE
11479               l_from:=l_from||' ,bim_obj_ld_src_mv a ';
11480               l_where := l_where ||
11481 	                 ' AND  a.lead_source = d.lookup_code(+)
11482 	                   AND  d.lookup_type (+) = ''SOURCE_SYSTEM''
11483 			   AND  a.object_country = :l_country';
11484               END IF;
11485             ELSE
11486               l_from := l_from||' ,bim_obj_ld_src_mv a ' ; --, bim_i_source_codes b ';
11487               l_where  := l_where ||
11488                         '  AND  a.source_code_id = :l_campaign_id
11489                            AND  a.lead_source = d.lookup_code(+)
11490 	                   AND  d.lookup_type (+) = ''SOURCE_SYSTEM''
11491 			   AND  a.object_country = :l_country ';
11492 	  END IF;
11493 ELSIF (l_view_by ='SALES CHANNEL+BIS_SALES_CHANNEL') THEN
11494    /** product category handling**/
11495    IF l_cat_id is null then
11496      l_where := l_where ||l_pc_where;
11497   ELSE
11498      l_from  := l_from ||l_pc_from;
11499      l_where := l_where||l_pc_where;
11500   END IF;
11501     l_col:=' SELECT
11502 		    decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) name,
11503                      null viewbyid,
11504 		     null meaning ';
11505     l_groupby := ' GROUP BY  decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) ';
11506     l_from:=' FROM so_lookups d '||l_from;
11507 	  IF l_campaign_id is null then
11508 	      IF l_admin_status ='Y' THEN
11509 	      l_from:=l_from||' ,bim_mkt_ld_chnl_mv a ';
11510               l_where := l_where ||
11511                          ' AND a.channel_code = d.lookup_code(+)
11512 			   AND d.lookup_type (+) = ''SALES_CHANNEL''
11513 			   AND  a.object_country = :l_country';
11514               ELSE
11515               l_from:=l_from||' ,bim_obj_ld_chnl_mv a ';
11516               l_where := l_where ||
11517 	                 ' AND a.channel_code = d.lookup_code(+)
11518 			   AND d.lookup_type (+) = ''SALES_CHANNEL''
11519    		           AND  a.object_country = :l_country';
11520               END IF;
11521             ELSE
11522               l_from := l_from||' ,bim_obj_ld_chnl_mv a '; --, bim_i_source_codes b ';
11523               l_where  := l_where ||
11524                         '  AND  a.source_code_id = :l_campaign_id
11525                            AND a.channel_code = d.lookup_code(+)
11526 			   AND d.lookup_type (+) = ''SALES_CHANNEL''
11527 			   AND  a.object_country = :l_country';
11528 	  END IF;
11529 ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
11530    /** product category handling**/
11531    IF l_cat_id is null then
11532      l_where := l_where ||l_pc_where;
11533   ELSE
11534      l_from  := l_from ||l_pc_from;
11535      l_where := l_where||l_pc_where;
11536   END IF;
11537     l_col:=' SELECT
11538 		    decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
11539                      a.object_country viewbyid,
11540 		     null meaning ';
11541     l_groupby := ' GROUP BY decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country ';
11542     l_from:=' FROM fnd_territories_tl d '||l_from;
11543 	  IF l_campaign_id is null then
11544 	      IF l_admin_status ='Y' THEN
11545 	      l_from:=l_from||' ,bim_i_obj_mets_mv a ';
11546               l_where := l_where ||l_top_cond||
11547                          ' AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'')';
11548               ELSE
11549               l_from:=l_from||' ,bim_i_obj_mets_mv a ';
11550               l_where := l_where ||
11551 	                 ' AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'') ';
11552               END IF;
11553             ELSE
11554               l_from := l_from||' ,bim_i_obj_mets_mv a ' ; --, bim_i_source_codes b ';
11555               l_where  := l_where ||
11556                         '  AND  a.source_code_id = :l_campaign_id
11557                            AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'')';
11558 	  END IF;
11559 	  IF  l_country <>'N' THEN
11560 	      l_where  := l_where ||' AND  a.object_country = :l_country';
11561           ELSE
11562 	   l_where  := l_where ||' AND  a.object_country <> ''N''';
11563 	  END IF;
11564 ELSIF (l_view_by ='BIM_LEAD_ATTRIBUTES+BIM_LEAD_QUALITY') THEN
11565   /** product category handling**/
11566    IF l_cat_id is null then
11567      l_where := l_where ||l_pc_where;
11568   ELSE
11569      l_from  := l_from ||l_pc_from;
11573 		    decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) name,
11570      l_where := l_where||l_pc_where;
11571   END IF;
11572     l_col:=' SELECT
11574                      null viewbyid,
11575 		     null meaning ';
11576     l_groupby := ' GROUP BY  decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) ';
11577     l_from:=' FROM as_sales_lead_ranks_vl d '||l_from;
11578 	  IF l_campaign_id is null then
11579 	      IF l_admin_status ='Y' THEN
11580 	      l_from:=l_from||' ,bim_mkt_ld_qual_mv a ';
11581               l_where := l_where ||
11582                          ' AND d.rank_id (+)= a.lead_rank_id
11583 			   AND  a.object_country = :l_country';
11584               ELSE
11585               l_from:=l_from||' ,bim_obj_ld_qual_mv a ';
11586               l_where := l_where ||
11587 	                 ' AND d.rank_id (+)= a.lead_rank_id
11588    		           AND  a.object_country = :l_country';
11589               END IF;
11590             ELSE
11591               l_from := l_from||' ,bim_obj_ld_qual_mv a ' ; --, bim_i_source_codes b ';
11592               l_where  := l_where ||
11593                         '  AND  a.source_code_id = :l_campaign_id
11594                            AND d.rank_id (+)= a.lead_rank_id
11595 			   AND  a.object_country = :l_country';
11596 	  END IF;
11597 ELSIF (l_view_by ='MEDIA+MEDIA') THEN
11598 /** product category handling**/
11599    IF l_cat_id is null then
11600      l_where := l_where ||l_pc_where;
11601   ELSE
11602      l_from  := l_from ||l_pc_from;
11603      l_where := l_where||l_pc_where;
11604   END IF;
11605     l_col:=' SELECT
11606 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
11607                      null viewbyid,
11608 		     null meaning ';
11609     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
11610     l_from:=' FROM bim_dimv_media d '||l_from;
11611 	  IF l_campaign_id is null then
11612 	      IF l_admin_status ='Y' THEN
11613 	      l_from:=l_from||' ,bim_mkt_chnl_mv a ';
11614               l_where := l_where ||
11615                          ' AND  d.id (+)= a.activity_id
11616 			   AND  a.object_country = :l_country';
11617               ELSE
11618               l_from:=l_from||' ,bim_obj_chnl_mv a ';
11619               l_where := l_where ||
11620 	                 ' AND  d.id (+)= a.activity_id
11621    		           AND  a.object_country = :l_country';
11622               END IF;
11623             ELSE
11624               l_from := l_from||' ,bim_obj_chnl_mv a '; --, bim_i_source_codes b ';
11625               l_where  := l_where ||
11626                         '  AND  a.source_code_id = :l_campaign_id
11627                            AND  d.id (+)= a.activity_id
11628 			   AND  a.object_country = :l_country';
11629 	  END IF;
11630 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
11631 /** product category handling**/
11632    IF l_cat_id is null then
11633      l_where := l_where ||l_pc_where;
11634   ELSE
11635      l_from  := l_from ||l_pc_from;
11636      l_where := l_where||l_pc_where;
11637   END IF;
11638     l_col:=' SELECT
11639 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
11640                      null viewbyid,
11641 		     null meaning ';
11642     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
11643     l_from:=' FROM bis_areas_v d '||l_from;
11644 	  IF l_campaign_id is null then
11645 	      IF l_admin_status ='Y' THEN
11646 	      l_from:=l_from||' ,bim_mkt_regn_mv a ';
11647               l_where := l_where ||
11648                          ' AND  d.id (+)= a.object_region
11649 			   AND  a.object_country = :l_country';
11650               ELSE
11651               l_from:=l_from||' ,bim_obj_regn_mv a ';
11652               l_where := l_where ||
11653 	                 ' AND  d.id (+)= a.object_region
11654    		           AND  a.object_country = :l_country';
11655               END IF;
11656             ELSE
11657               l_from := l_from||' ,bim_obj_regn_mv a '; --, bim_i_source_codes b ';
11658               l_where  := l_where ||
11659                         '  AND  a.source_code_id = :l_campaign_id
11660                            AND  d.id (+)= a.object_region
11661 			   AND  a.object_country = :l_country';
11662 	  END IF;
11663 END IF;
11664 
11665 /* combine sql one to pick up current period values and  sql two to pick previous period values */
11666   l_select := l_col||
11667               l_comm_cols||
11668 	      l_from||
11669 	      l_where ||' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
11670 	      AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id  '||
11671 	      l_groupby ||'UNION ALL'||l_col||
11672               l_comm2_cols||
11673 	      l_from||
11674 	      l_where ||' AND cal.report_date = &BIS_CURRENT_EFFECTIVE_START_DATE - 1
11675 	      AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  '||
11676 	      l_groupby||
11677 	      l_pc_select /* l_pc_select only applicable when product category is not all and view by is product category */
11678 	      ;
11679 
11680 /* prepare final sql */
11681 
11682  l_sqltext:= l_select_cal||
11683              l_select||
11684 	     l_select_filter;
11685   END IF;
11686 
11687    x_custom_sql := l_sqltext;
11688    l_custom_rec.attribute_name := ':l_record_type';
11692    x_custom_output.EXTEND;
11689    l_custom_rec.attribute_value := l_record_type_id;
11690    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
11691    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
11693    x_custom_output (1) := l_custom_rec;
11694    l_custom_rec.attribute_name := ':l_resource_id';
11695    l_custom_rec.attribute_value := get_resource_id;
11696    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
11697    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
11698    x_custom_output.EXTEND;
11699    x_custom_output (2) := l_custom_rec;
11700    l_custom_rec.attribute_name := ':l_admin_flag';
11701    l_custom_rec.attribute_value := get_admin_status;
11702    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
11703    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
11704    x_custom_output.EXTEND;
11705    x_custom_output (3) := l_custom_rec;
11706    l_custom_rec.attribute_name := ':l_country';
11707    l_custom_rec.attribute_value := l_country;
11708    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
11709    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
11710    x_custom_output.EXTEND;
11711    x_custom_output (4) := l_custom_rec;
11712    l_custom_rec.attribute_name := ':l_cat_id';
11713    l_custom_rec.attribute_value := l_cat_id;
11714    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
11715    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
11716    x_custom_output.EXTEND;
11717    x_custom_output (5) := l_custom_rec;
11718    l_custom_rec.attribute_name := ':l_campaign_id';
11719    l_custom_rec.attribute_value := l_campaign_id;
11720    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
11721    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
11722    x_custom_output.EXTEND;
11723    x_custom_output (6) := l_custom_rec;
11724    write_debug ('GET_LEAD_OPTY_CONV_SQL', 'QUERY', '_', l_sqltext);
11725 EXCEPTION
11726    WHEN OTHERS
11727    THEN
11728       l_sql_errm := SQLERRM;
11729       write_debug ('GET_LEAD_OPTY_CONV_SQL', 'ERROR', l_sql_errm, l_sqltext);
11730 END get_lead_opty_conv_sql;
11731 
11732 PROCEDURE GET_MKTG_A_LEADS_SQL (
11733    p_page_parameter_tbl   IN              bis_pmv_page_parameter_tbl,
11734    x_custom_sql           OUT NOCOPY      VARCHAR2,
11735    x_custom_output        OUT NOCOPY      bis_query_attributes_tbl
11736 )
11737 IS
11738    l_sqltext                      VARCHAR2 (20000);
11739    iflag                          NUMBER;
11740    l_period_type_hc               NUMBER;
11741    l_as_of_date                   DATE;
11742    l_period_type                  VARCHAR2 (2000);
11743    l_record_type_id               NUMBER;
11744    l_comp_type                    VARCHAR2 (2000);
11745    l_country                      VARCHAR2 (4000);
11746    l_view_by                      VARCHAR2 (4000);
11747    l_sql_errm                     VARCHAR2 (4000);
11748    l_previous_report_start_date   DATE;
11749    l_current_report_start_date    DATE;
11750    l_previous_as_of_date          DATE;
11751    l_period_type_id               NUMBER;
11752    l_user_id                      NUMBER;
11753    l_resource_id                  NUMBER;
11754    l_time_id_column               VARCHAR2 (1000);
11755    l_admin_status                 VARCHAR2 (20);
11756    l_admin_flag                   VARCHAR2 (1);
11757    l_admin_count                  NUMBER;
11758    l_rsid                         NUMBER;
11759    l_curr_aod_str                 VARCHAR2 (80);
11760    l_country_clause               VARCHAR2 (4000);
11761    l_access_clause                VARCHAR2 (4000);
11762    l_access_table                 VARCHAR2 (4000);
11763    l_cat_id                       VARCHAR2 (50)        := NULL;
11764    l_campaign_id                  VARCHAR2 (50)        := NULL;
11765    l_select                       VARCHAR2 (20000); -- to build  inner select to pick data from mviews
11766    l_pc_select                    VARCHAR2 (20000); -- to build  inner select to pick data directly assigned to the product category hirerachy
11767    l_select_cal                   VARCHAR2 (20000); -- to build  select calculation part
11768    l_select_filter                VARCHAR2 (20000); -- to build  select filter part
11769    l_from                         VARCHAR2 (20000);   -- assign common table in  clause
11770    l_where                        VARCHAR2 (20000);  -- static where clause
11771    l_groupby                      VARCHAR2 (2000);  -- to build  group by clause
11772    l_pc_from                      VARCHAR2 (20000);   -- from clause to handle product category
11773    l_pc_where                     VARCHAR2 (20000);   --  where clause to handle product category
11774    l_filtercol                    VARCHAR2 (2000);
11775    l_pc_col                       VARCHAR2(200);
11776    l_pc_groupby                   VARCHAR2(200);
11777    l_view                         VARCHAR2 (20);
11778    l_comm_cols                      VARCHAR2 (20000);
11779    l_view_disp                    VARCHAR2(100);
11780    l_url_str                      VARCHAR2(1000);
11781    l_url_str_csch                 varchar2(1000);
11782    l_url_str_csch_jtf             varchar2(3000);
11783    l_url_str_type                 varchar2(3000);
11784    l_csch_chnl                    varchar2(100);
11785    l_camp_sel_col                 varchar2(100);
11786    l_camp_groupby_col             varchar2(100);
11787    l_top_cond                       VARCHAR2(100);
11788    l_meaning                      VARCHAR2 (20); -- assigning default value
11792     cursor get_obj_type
11789    /* variables to hold columns names in l_select clauses */
11790    l_col                          VARCHAR2(1000);
11791    /* cursor to get type of object passed from the page ******/
11793     is
11794     select object_type
11795     from bim_i_source_codes
11796     where source_code_id=replace(l_campaign_id,'''');
11797     /*********************************************************/
11798    l_custom_rec                   bis_query_attributes;
11799    l_object_type                  varchar2(30);
11800    l_url_link                       varchar2(200);
11801    l_url_camp1                     varchar2(3000);
11802    l_url_camp2                     varchar2(3000);
11803    l_dass                         varchar2(100);  -- variable to store value for  directly assigned lookup value
11804  --  l_una                           varchar2(100);   -- variable to store value for  Unassigned lookup value
11805    l_leaf_node_flag               varchar2(25);   -- variable to store value leaf_node_flag column in case of product category
11806    l_curr VARCHAR2(50);
11807    l_curr_suffix VARCHAR2(50);
11808    l_col_id NUMBER;
11809    l_area VARCHAR2(300);
11810    l_report_name VARCHAR2(300);
11811    l_media VARCHAR2(300);
11812    l_jtf                           varchar2(300);
11813 
11814 BEGIN
11815    x_custom_output := bis_query_attributes_tbl ();
11816    l_custom_rec := bis_pmv_parameters_pub.initialize_query_type;
11817 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
11818    			     			      l_as_of_date,
11819 			    			      l_period_type,
11820 				                      l_record_type_id,
11821 				                      l_comp_type,
11822 				                      l_country,
11823 						      l_view_by,
11824 						      l_cat_id,
11825 						      l_campaign_id,
11826 						      l_curr,
11827 						      l_col_id,
11828 						      l_area,
11829 						      l_media,
11830 						      l_report_name
11831 				                      );
11832 
11833 
11834    l_meaning:=' null meaning '; -- assigning default value
11835    l_url_camp1:=',null';
11836    l_url_camp2:=',null';
11837 
11838 
11839    IF l_country IS NULL THEN
11840       l_country := 'N';
11841    END IF;
11842 /** to add meaning in select clause only in case of campaign view by */
11843   IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
11844   l_meaning:=' ,meaning ';
11845   l_filtercol:=',meaning ';
11846   ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
11847   l_filtercol:=',leaf_node_flag ';
11848   l_meaning:=',null meaning ';
11849   else
11850   l_meaning:=' ,null meaning ';
11851   end if;
11852 
11853 /*** to  assigned URL **/
11854 
11855 if l_campaign_id is not null then
11856 -- checking for the object type passed from page
11857  for i in get_obj_type
11858  loop
11859  l_object_type:=i.object_type;
11860  end loop;
11861 end if;
11862 
11863    l_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
11864    l_url_str  :='pFunctionName=BIM_I_MKTG_A_LEADS&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
11865    --l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objId=';
11866    l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
11867    l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
11868    l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
11869    &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
11870    IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
11871    l_url_link :=' ,decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) ';
11872    l_view_disp:='viewby';
11873    l_leaf_node_flag :=' ,leaf_node_flag ';
11874    ELSIF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
11875      l_camp_sel_col :=' ,object_id
11876                        ,object_type
11877                        ';
11878      l_camp_groupby_col :=',object_id,object_type ';
11879      l_url_link := ' ,null ';
11880      l_view_disp := 'viewby';
11881 
11882      IF (l_campaign_id is  null or l_object_type='RCAM') then
11883 	l_url_camp1:=', decode(object_type,''EONE'',NULL,'||''''||l_url_str||''''||' )';
11884      ELSIF l_object_type='CAMP' THEN
11885  	l_url_camp2:=', '||''''||l_url_str_type||''''||'||object_id ';
11886  	--l_url_camp1:=',decode(usage,''LITE'','||''''||l_url_str_csch||''''||'||object_id,'||''''||l_url_str_csch_jtf||''''||'||object_id)';
11887 	l_url_camp1:=', '||''''||l_url_str_csch||''''||'||object_id ';
11888 
11889 		l_csch_chnl:='|| '' - '' || channel';
11890 	l_camp_sel_col :=l_camp_sel_col|| ',usage,channel';
11891 	l_camp_groupby_col :=l_camp_groupby_col||',usage,channel';
11892      end if;
11893     ELSE
11894       --l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
11895       l_url_link:=' ,null ';
11896       l_view_disp:='viewby';
11897    END IF;
11898 /* l_select_cal is common part of select statement for all view by to calculate grand totals and change */
11899  l_select_cal :='
11900          SELECT '||
11901          l_view_disp ||'
11902 	 ,viewbyid
11906  	 ,bim_attribute5
11903  	 ,bim_attribute2 '|| l_csch_chnl ||' bim_attribute2
11904  	 ,bim_attribute3
11905  	 ,bim_attribute4
11907  	 ,bim_attribute6
11908  	 ,bim_attribute7
11909  	 ,bim_attribute8
11910 	 ,bim_attribute9
11911 	 ,bim_attribute10
11912 	 ,bim_attribute11
11913 	 ,bim_attribute9  bim_attribute12
11914 	 ,bim_attribute10 bim_attribute13
11915 	 ,bim_attribute3  bim_attribute14
11916  	 ,bim_attribute3  bim_attribute18
11917 	 ,case when bim_grand_total7=0 then null
11918 	 else
11919 	 (bim_attribute9/bim_grand_total7)*100
11920 	 end bim_attribute20 '||
11921          l_url_link||' bim_attribute19'||
11922 	 l_url_camp1|| ' bim_attribute21 '||
11923 	 l_url_camp2||' bim_attribute22
11924  	 ,bim_grand_total1
11925  	 ,bim_grand_total2
11926  	 ,bim_grand_total3
11927  	 ,bim_grand_total4
11928  	 ,bim_grand_total5
11929  	 ,bim_grand_total6
11930 	 ,bim_grand_total7
11931 	 ,bim_grand_total8
11932 	 ,bim_grand_total9
11933 	,case when bim_grand_total7=0 then null
11934 	 else 100 end  bim_grand_total10
11935 	 ,bim_grand_total1 bim_grand_total11
11936           FROM
11937 	 (
11938             SELECT
11939             name    VIEWBY'||l_leaf_node_flag||'
11940             ,meaning BIM_ATTRIBUTE2'||l_camp_sel_col
11941             ||',rank_a  BIM_ATTRIBUTE3
11942             ,DECODE(prev_rank_a,0,NULL,((rank_a - prev_rank_a)/prev_rank_a)*100) BIM_ATTRIBUTE4
11943             ,rank_b BIM_ATTRIBUTE5
11944             ,rank_c BIM_ATTRIBUTE6
11945 	    ,rank_d BIM_ATTRIBUTE7
11946             ,rank_z BIM_ATTRIBUTE8
11947             ,leads_new BIM_ATTRIBUTE9
11948             ,leads_qualified BIM_ATTRIBUTE10
11949             ,DECODE(prev_leads_qualified,0,NULL,((leads_qualified - prev_leads_qualified)/prev_leads_qualified)*100) BIM_ATTRIBUTE11
11950             ,sum(rank_a) over() BIM_GRAND_TOTAL1
11951 	    ,decode(sum(prev_rank_a) over(),0,null,(((sum(rank_a- prev_rank_a) over())/sum(prev_rank_a)over ())*100)) BIM_GRAND_TOTAL2
11952 	    ,sum(rank_b) over() BIM_GRAND_TOTAL3
11953             ,sum(rank_c) over() BIM_GRAND_TOTAL4
11954 	    ,sum(rank_d) over() BIM_GRAND_TOTAL5
11955 	    ,sum(rank_z) over() BIM_GRAND_TOTAL6
11956             ,sum(leads_new) over() BIM_GRAND_TOTAL7
11957 	    ,sum(leads_qualified) over() BIM_GRAND_TOTAL8
11958    	    ,decode(sum(prev_leads_qualified) over(),0,null,(((sum(leads_qualified- prev_leads_qualified) over())/sum(prev_leads_qualified)over ())*100)) BIM_GRAND_TOTAL9
11959             ,VIEWBYID
11960              FROM
11961               (
11962                   SELECT
11963           	      viewbyid,
11964           	      name'||l_leaf_node_flag||
11965 		      l_meaning||l_camp_sel_col||
11966 		    ',sum(rank_a)                    rank_a
11967           	      ,sum(rank_b)                    rank_b
11968           	      ,sum(rank_c)                    rank_c
11969           	      ,sum(rank_d)                    rank_d
11970           	      ,sum(rank_z)                    rank_z
11971           	      ,sum(leads_new)                 leads_new
11972      		      ,sum(leads_qualified)           leads_qualified
11973 		      ,sum(prev_rank_a)               prev_rank_a
11974      		      ,sum(prev_leads_qualified)      prev_leads_qualified
11975                   FROM
11976           	  ( ';
11977 /* l_comm_cols contains column information common to all select statement for all view by */
11978 
11979 l_comm_cols:=    ' , sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_a,0))   rank_a ,
11980                    sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_b,0))   rank_b,
11981      		   sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_c,0))   rank_c,
11982      		   sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_d,0))   rank_d,
11983      		   sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.rank_z,0))   rank_z,
11984 		   sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_new,0)) leads_new,
11985      		   sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_qualified,0))  leads_qualified,
11986    	           sum(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.rank_a,0)) prev_rank_a ,
11987      		   sum(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.leads_qualified,0)) prev_leads_qualified';
11988 /* l_from contains time dimension table common to all select statement for all view by */
11989  l_from  :=',fii_time_rpt_struct_v cal ';
11990  /* l_where contains where clause to join time dimension table common to all select statement for all view by */
11991  l_where :=' WHERE a.time_id = cal.time_id
11992              AND  a.period_type_id = cal.period_type_id
11993              AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
11994              AND  cal.calendar_id= -1 ';
11995  /* l_select_filter contains group by and filter clause to remove uneccessary records with zero values */
11996 l_select_filter := ' ) GROUP BY viewbyid,name '||l_filtercol||l_camp_groupby_col||
11997                   ')
11998 		   )
11999          WHERE
12000            bim_attribute9 <> 0
12001 	  &ORDER_BY_CLAUSE ';
12002   /* get_admin_status to check current user is admin or not */
12003 
12004    l_admin_status := get_admin_status;
12005 
12006 /*********************** security handling ***********************/
12007 
12008 IF   l_campaign_id is null THEN /******* no security checking at child level ********/
12009 	IF   l_admin_status = 'N' THEN
12010 		IF  l_view_by = 'CAMPAIGN+CAMPAIGN' then
12011 			/*************** program view is enable **************/
12012 			IF l_prog_view='Y' then
12016 				 AND ac.resource_id = :l_resource_id ';
12013 				l_view := ',''RCAM''';
12014 				l_from := l_from ||',bim_i_top_objects ac ';
12015 				l_where := l_where ||' AND a.source_code_id=ac.source_code_id
12017 			/************************************************/
12018 			ELSE
12019 				l_from := l_from ||',ams_act_access_denorm ac , bim_i_source_codes src ';
12020 			    l_where := l_where ||' AND a.source_code_id=src.source_code_id
12021 				 AND src.object_id=ac.object_id
12022 				 AND src.object_type=ac.object_type
12023 				 AND src.object_type NOT IN (''RCAM'')
12024 			     AND ac.resource_id = :l_resource_id ';
12025 			END IF;
12026 
12027 		ELSE
12028 
12029 			l_from := l_from ||',bim_i_top_objects ac ';
12030             l_where := l_where ||' AND a.source_code_id=ac.source_code_id
12031 		    AND ac.resource_id = :l_resource_id ';
12032 
12033 		END IF;
12034 
12035 	ELSE
12036 		IF l_view_by = 'CAMPAIGN+CAMPAIGN' then
12037 			IF  l_prog_view='Y' THEN
12038 				l_view := ',''RCAM''';
12039 				l_top_cond :=' AND a.immediate_parent_id is null ';
12040 			END IF;
12041 		ELSE
12042 			/******** to append parent object id is null for other view by (country and product category) ***/
12043 			l_top_cond :=' AND a.immediate_parent_id is null ';
12044 			/***********/
12045 		END IF;
12046 	END IF;
12047 END IF;
12048 /************************************************************************/
12049 
12050    /* product category handling */
12051      IF  l_cat_id is not null then
12052          l_pc_from   :=  ', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
12053 	 l_pc_where  :=  ' AND a.category_id = edh.child_id
12054 			   AND edh.object_type = ''CATEGORY_SET''
12055 			   AND edh.object_id = mdcs.category_set_id
12056 			   AND mdcs.functional_area_id = 11
12057 			   AND edh.dbi_flag = ''Y''
12058 			   AND edh.parent_id = :l_cat_id ';
12059        ELSE
12060         l_pc_where :=     ' AND a.category_id = -9 ';
12061      END IF;
12062 /********************************/
12063 
12064 IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
12065 
12066      /* forming from clause for the tables which is common to all union all */
12067      IF l_cat_id IS NOT NULL THEN
12068 
12069 		l_from :=' FROM bim_i_obj_mets_mv a ,bim_i_obj_name_mv name '||l_from||l_pc_from;
12070 
12071      ELSE
12072 
12073 		l_from :=' FROM bim_i_obj_mets_mv a, bim_i_obj_name_mv name '||l_from;
12074 
12075      END IF;
12076 
12077      /* forming where clause which is common to all union all */
12078 
12079 	IF l_prog_view = 'Y' THEN
12080 		l_where :=l_where||' AND a.source_code_id = name.source_code_id
12081 						  AND name.language = USERENV(''LANG'')
12082 						  AND a.object_country = :l_country '||
12083 						  l_pc_where;
12084 	ELSE
12085 		l_where :=l_where||' AND a.source_code_id = name.source_code_id
12086 						  AND name.language = USERENV(''LANG'')
12087  						  AND name.object_type NOT IN (''RCAM'')
12088 						  AND a.object_country = :l_country '||
12089 						  l_pc_where;
12090 	END IF;
12091 
12092 
12093 	/* forming group by clause for the common columns for all union all */
12094 	l_groupby:=' GROUP BY a.source_code_id,name.object_type_mean,name.name,name.object_id,name.object_type ';
12095 
12096 	/*** campaign id null means No drill down and view by is camapign hirerachy*/
12097 	IF l_campaign_id is null THEN
12098 		/*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
12099 		l_sqltext:= l_select_cal||
12100 		/******** inner select start from here */
12101 		/* select to get camapigns and programs  */
12102 		' SELECT
12103 		a.source_code_id VIEWBYID,
12104 		name.name name,
12105 		name.object_id object_id,
12106 		name.object_type object_type,
12107 		name.object_type_mean meaning '||
12108 		l_comm_cols ||
12109 		l_from ||
12110 		l_where ||l_top_cond||
12111 		' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)'||
12112 		l_groupby||
12113 		l_select_filter;
12114 	ELSE
12115 		/* source_code_id is passed from the page, object selected from the page to be drill may be program,campaign,event,one off event*****/
12116 		/* appending table in l_form and joining conditon for the bim_i_source_codes */
12117 
12118 		l_where :=l_where ||' AND a.immediate_parent_id = :l_campaign_id ';
12119 
12120 		-- if program is selected from the page means it may have childern as programs,campaigns,events or one off events
12121 
12122 		IF l_object_type in ('RCAM','EVEH') THEN
12123 			/*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
12124 			l_sqltext:= l_select_cal||
12125 			/******** inner select start from here */
12126 			/* select to get camapigns and programs  */
12127 			' SELECT
12128 			a.source_code_id VIEWBYID,
12129 			name.name name,
12130 			name.object_id object_id,
12131 			name.object_type object_type,
12132 			name.object_type_mean meaning '||
12133 			l_comm_cols ||
12134 			l_from ||
12135 			l_where ||
12136 			' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)'||
12137 			l_groupby||
12138 			l_select_filter;
12139 
12140 		ELSIF l_object_type='CAMP' THEN
12141 
12142 			l_sqltext:= l_select_cal||
12143 			/******** inner select start from here */
12144 			/* select to get camapign schedules  */
12145 			' SELECT
12149 			name.object_type object_type,
12146 			a.source_code_id VIEWBYID,
12147 			name.name name,
12148 			name.object_id object_id,
12150 			name.child_object_usage usage,
12151 			decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel
12152 			,name.object_type_mean meaning '||
12153 			l_comm_cols ||
12154 			l_from || ' , bim_dimv_media chnl '||
12155 			l_where ||
12156 			' AND name.activity_id =chnl.id (+)
12157 			AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)'||
12158 			l_groupby||
12159 			' , name.child_object_usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
12160 			l_select_filter ;
12161 		END IF;
12162 	END IF;
12163 
12164  /***** END CAMPAIGN HIRERACHY VIEW HANDLING ******************/
12165 
12166  ELSE
12167  /* view by is product category */
12168  IF (l_view_by ='ITEM+ENI_ITEM_VBH_CAT') THEN
12169  -- changed to make fii_time_rpt_struct_v cal as first table in from CLAUSE to provide ORDERED HINT
12170  if l_admin_status='N' then
12171  l_from:=replace(l_from,',fii_time_rpt_struct_v cal');
12172  else
12173  l_from:=null;
12174  end if;
12175 /******** handling product category hirerachy ****/
12176 /* picking up value of top level node from product category denorm for category present in  bim_i_obj_mets_mv   */
12177     IF l_cat_id is null then
12178        l_from:=l_from||
12179                ',eni_denorm_hierarchies edh
12180                 ,mtl_default_category_sets mdcs
12181                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
12182                    FROM eni_item_vbh_nodes_v e
12183                    WHERE e.top_node_flag=''Y''
12184                    AND e.child_id = e.parent_id) p ';
12185        l_where := l_where||
12186                          ' AND a.category_id = edh.child_id
12187 			   AND edh.object_type = ''CATEGORY_SET''
12188                            AND edh.object_id = mdcs.category_set_id
12189                            AND mdcs.functional_area_id = 11
12190                            AND edh.dbi_flag = ''Y''
12191                            AND edh.parent_id = p.parent_id';
12192        l_col:=' SELECT /*+ORDERED*/
12193 		   p.value name,
12194                    p.parent_id viewbyid,
12195 		   p.leaf_node_flag leaf_node_flag,
12196 		   null meaning ';
12197         l_groupby := ' GROUP BY p.value,p.parent_id,p.leaf_node_flag ';
12198     ELSE
12199     /* passing id from page and getting immediate child to build hirerachy  */
12200 
12201     /** reassigning value to l_pc_from and l_pc_where for product category hirerachy drill down for values directly assigned to prodcut select from the page*/
12202 
12203      l_pc_from:= l_from||
12204                    ',(select e.id id,e.value value
12205                       from eni_item_vbh_nodes_v e
12206                       where e.parent_id =  :l_cat_id
12207                       AND e.parent_id = e.child_id
12208                       AND leaf_node_flag <> ''Y''
12209                       ) p ';
12210 
12211     l_pc_where :=l_where||
12212                       ' AND a.category_id = p.id ';
12213 
12214     l_from:= l_from||
12215             ',eni_denorm_hierarchies edh
12216             ,mtl_default_category_sets mdc
12217             ,(select e.id,e.value,e.leaf_node_flag
12218               from eni_item_vbh_nodes_v e
12219           where
12220               e.parent_id =:l_cat_id
12221               AND e.id = e.child_id
12222               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
12223       ) p ';
12224 
12225      l_where := l_where||'
12226                   AND a.category_id = edh.child_id
12227                   AND edh.object_type = ''CATEGORY_SET''
12228                   AND edh.object_id = mdc.category_set_id
12229                   AND mdc.functional_area_id = 11
12230                   AND edh.dbi_flag = ''Y''
12231                   AND edh.parent_id = p.id ';
12232 
12233      l_col:=' SELECT /*+ORDERED*/
12234 		   p.value name,
12235                    p.id viewbyid,
12236 		   p.leaf_node_flag leaf_node_flag,
12237 		   null meaning ';
12238      l_groupby := ' GROUP BY p.value,p.id,p.leaf_node_flag ';
12239     END IF;
12240 /*********************/
12241 
12242            IF l_campaign_id is null then /* no drilll down in campaign hirerachy */
12243 	      IF l_admin_status ='Y' THEN
12244               l_from:=' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
12245                               '||l_from;
12246               l_where := l_where ||l_top_cond||
12247                          '  AND  a.object_country = :l_country';
12248                IF l_cat_id is not null then
12249 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
12250                               '||l_pc_from;
12251 		  l_pc_where := l_pc_where ||l_top_cond||
12252                          ' AND  a.object_country = :l_country';
12253                END IF;
12254               ELSE
12255               l_from:=' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
12256                              '||l_from;
12257               l_where := l_where ||
12258 	                   ' AND  a.object_country = :l_country';
12259 
12260 		IF l_cat_id is not null then
12261 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
12262                               '||l_pc_from;
12263 		  l_pc_where := l_pc_where ||
12264                          ' AND  a.object_country = :l_country';
12265                END IF;
12266 
12270               l_where  := l_where ||
12267               END IF;
12268            ELSE
12269               l_from := ' FROM   fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a '||l_from ;
12271                         '  AND  a.source_code_id = :l_campaign_id
12272 			   AND  a.object_country = :l_country' ;
12273               IF l_cat_id is not null then
12274 	      l_pc_from := ' FROM   fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a '||l_pc_from ;
12275               l_pc_where  := l_pc_where ||
12276                         '  AND  a.source_code_id = :l_campaign_id
12277 			   AND  a.object_country = :l_country' ;
12278 	      END IF;
12279 	   END IF;
12280    /* building l_pc_select to get values directly assigned to product category passed from the page */
12281    IF l_cat_id is not null  THEN
12282        	  l_pc_col:=' SELECT /*+ORDERED*/
12283 		   bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||' name,
12284                    p.id  viewbyid,
12285 		   ''Y'' leaf_node_flag,
12286 		   null meaning ';
12287      l_pc_groupby := ' GROUP BY p.id ';
12288 
12289   l_pc_select :=
12290               ' UNION ALL ' ||
12291               l_pc_col||
12292               l_comm_cols||
12293 	      l_pc_from||
12294 	      l_pc_where ||' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)'||
12295 	      l_pc_groupby ;
12296    END IF;
12297  ELSIF (l_view_by ='CUSTOMER CATEGORY+CUSTOMER CATEGORY') THEN
12298   /** product category handling**/
12299    IF l_cat_id is null then
12300      l_where := l_where ||l_pc_where;
12301   ELSE
12302      l_from  := l_from ||l_pc_from;
12303      l_where := l_where||l_pc_where;
12304   END IF;
12305     l_col:=' SELECT
12306 		   decode(d.customer_category_name,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.customer_category_name) name,
12307                    null viewbyid,
12308 		   null meaning ';
12309     l_groupby := ' GROUP BY decode(d.customer_category_name,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.customer_category_name) ';
12310     l_from:=' FROM bic_cust_category_v d '||l_from;
12311 
12312 	  IF l_campaign_id is null then
12313 	      IF l_admin_status ='Y' THEN
12314 	      l_from:=l_from||' ,bim_mkt_ld_ccat_mv a ';
12315               l_where := l_where ||
12316                          ' AND d.customer_category_code (+) = a.cust_category
12317 			   AND  a.object_country = :l_country';
12318               ELSE
12319               l_from:=l_from||' ,bim_obj_ld_ccat_mv a ';
12320               l_where := l_where ||
12321 	                 ' AND d.customer_category_code (+) = a.cust_category
12322 			   AND  a.object_country = :l_country ';
12323               END IF;
12324             ELSE
12325               l_from := l_from||' ,bim_obj_ld_ccat_mv a ' ; --, bim_i_source_codes b ';
12326               l_where  := l_where ||
12327                         '  AND  a.source_code_id = :l_campaign_id
12328                            AND  d.customer_category_code (+) = a.cust_category
12329 			   AND  a.object_country = :l_country' ;
12330 	  END IF;
12331  ELSIF (l_view_by ='BIM_LEAD_ATTRIBUTES+BIM_LEAD_SOURCE') THEN
12332    /** product category handling**/
12333    IF l_cat_id is null then
12334      l_where := l_where ||l_pc_where;
12335   ELSE
12336      l_from  := l_from ||l_pc_from;
12337      l_where := l_where||l_pc_where;
12338   END IF;
12339     l_col:=' SELECT
12340 		    decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) name,
12341                      null viewbyid,
12342 		     null meaning ';
12343     l_groupby := ' GROUP BY  decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) ';
12344     l_from:=' FROM as_lookups d '||l_from;
12345 	  IF l_campaign_id is null then
12346 	      IF l_admin_status ='Y' THEN
12347 	      l_from:=l_from||' ,bim_mkt_ld_src_mv a ';
12348               l_where := l_where ||
12349                          ' AND  a.lead_source = d.lookup_code(+)
12350 	                   AND  d.lookup_type (+) = ''SOURCE_SYSTEM''
12351 			   AND  a.object_country = :l_country';
12352               ELSE
12353               l_from:=l_from||' ,bim_obj_ld_src_mv a ';
12354               l_where := l_where ||
12355 	                 ' AND  a.lead_source = d.lookup_code(+)
12356 	                   AND  d.lookup_type (+) = ''SOURCE_SYSTEM''
12357 			   AND  a.object_country = :l_country';
12358               END IF;
12359             ELSE
12360               l_from := l_from||' ,bim_obj_ld_src_mv a ' ; --, bim_i_source_codes b ';
12361               l_where  := l_where ||
12362                         '  AND  a.source_code_id = :l_campaign_id
12363                            AND  a.lead_source = d.lookup_code(+)
12364 	                   AND  d.lookup_type (+) = ''SOURCE_SYSTEM''
12365 			   AND  a.object_country = :l_country ';
12366 	  END IF;
12367 ELSIF (l_view_by ='SALES CHANNEL+BIS_SALES_CHANNEL') THEN
12368    /** product category handling**/
12369    IF l_cat_id is null then
12370      l_where := l_where ||l_pc_where;
12371   ELSE
12372      l_from  := l_from ||l_pc_from;
12373      l_where := l_where||l_pc_where;
12374   END IF;
12375     l_col:=' SELECT
12376 		    decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) name,
12377                      null viewbyid,
12378 		     null meaning ';
12379     l_groupby := ' GROUP BY  decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) ';
12380     l_from:=' FROM so_lookups d '||l_from;
12384               l_where := l_where ||
12381 	  IF l_campaign_id is null then
12382 	      IF l_admin_status ='Y' THEN
12383 	      l_from:=l_from||' ,bim_mkt_ld_chnl_mv a ';
12385                          ' AND a.channel_code = d.lookup_code(+)
12386 			   AND d.lookup_type (+) = ''SALES_CHANNEL''
12387 			   AND  a.object_country = :l_country';
12388               ELSE
12389               l_from:=l_from||' ,bim_obj_ld_chnl_mv a ';
12390               l_where := l_where ||
12391 	                 ' AND a.channel_code = d.lookup_code(+)
12392 			   AND d.lookup_type (+) = ''SALES_CHANNEL''
12393    		           AND  a.object_country = :l_country';
12394               END IF;
12395             ELSE
12396               l_from := l_from||' ,bim_obj_ld_chnl_mv a ' ; --, bim_i_source_codes b ';
12397               l_where  := l_where ||
12398                         '  AND  a.source_code_id = :l_campaign_id
12399                            AND a.channel_code = d.lookup_code(+)
12400 			   AND d.lookup_type (+) = ''SALES_CHANNEL''
12401 			   AND  a.object_country = :l_country';
12402 	  END IF;
12403 ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
12404    /** product category handling**/
12405    IF l_cat_id is null then
12406      l_where := l_where ||l_pc_where;
12407   ELSE
12408      l_from  := l_from ||l_pc_from;
12409      l_where := l_where||l_pc_where;
12410   END IF;
12411     l_col:=' SELECT
12412 		    decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
12413                      a.object_country viewbyid,
12414 		     null meaning ';
12415     l_groupby := ' GROUP BY  decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country ';
12416     l_from:=' FROM fnd_territories_tl d '||l_from;
12417 	  IF l_campaign_id is null then
12418 	      IF l_admin_status ='Y' THEN
12419 	      l_from:=l_from||' ,bim_i_obj_mets_mv a ';
12420               l_where := l_where ||l_top_cond||
12421                          ' AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'')';
12422               ELSE
12423               l_from:=l_from||' ,bim_i_obj_mets_mv a ';
12424               l_where := l_where ||
12425 	                 ' AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'') ';
12426               END IF;
12427             ELSE
12428               l_from := l_from||' ,bim_i_obj_mets_mv a ' ; --, bim_i_source_codes b ';
12429               l_where  := l_where ||
12430                         '  AND  a.source_code_id = :l_campaign_id
12431                            AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'') ';
12432 	  END IF;
12433 	  IF  l_country <>'N' THEN
12434 	      l_where  := l_where ||' AND  a.object_country = :l_country';
12435           ELSE
12436 	   l_where  := l_where ||' AND  a.object_country <> ''N''';
12437 	  END IF;
12438 ELSIF (l_view_by ='BIM_LEAD_ATTRIBUTES+BIM_LEAD_QUALITY') THEN
12439   /** product category handling**/
12440    IF l_cat_id is null then
12441      l_where := l_where ||l_pc_where;
12442   ELSE
12443      l_from  := l_from ||l_pc_from;
12444      l_where := l_where||l_pc_where;
12445   END IF;
12446     l_col:=' SELECT
12447 		    decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) name,
12448                      null viewbyid,
12449 		     null meaning ';
12450     l_groupby := ' GROUP BY  decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) ';
12451     l_from:=' FROM as_sales_lead_ranks_vl d '||l_from;
12452 	  IF l_campaign_id is null then
12453 	      IF l_admin_status ='Y' THEN
12454 	      l_from:=l_from||' ,bim_mkt_ld_qual_mv a ';
12455               l_where := l_where ||
12456                          ' AND d.rank_id (+)= a.lead_rank_id
12457 			   AND  a.object_country = :l_country';
12458               ELSE
12459               l_from:=l_from||' ,bim_obj_ld_qual_mv a ';
12460               l_where := l_where ||
12461 	                 ' AND d.rank_id (+)= a.lead_rank_id
12462    		           AND  a.object_country = :l_country';
12463               END IF;
12464             ELSE
12465               l_from := l_from||' ,bim_obj_ld_qual_mv a ' ; --, bim_i_source_codes b ';
12466               l_where  := l_where ||
12467                         '  AND  a.source_code_id = :l_campaign_id
12468                            AND d.rank_id (+)= a.lead_rank_id
12469 			   AND  a.object_country = :l_country';
12470 	  END IF;
12471 ELSIF (l_view_by ='MEDIA+MEDIA') THEN
12472 /** product category handling**/
12473    IF l_cat_id is null then
12474      l_where := l_where ||l_pc_where;
12475   ELSE
12476      l_from  := l_from ||l_pc_from;
12477      l_where := l_where||l_pc_where;
12478   END IF;
12479     l_col:=' SELECT
12480 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
12481                      null viewbyid,
12482 		     null meaning ';
12483     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
12484     l_from:=' FROM bim_dimv_media d '||l_from;
12485 	  IF l_campaign_id is null then
12486 	      IF l_admin_status ='Y' THEN
12487 	      l_from:=l_from||' ,bim_mkt_chnl_mv a ';
12488               l_where := l_where ||
12489                          ' AND  d.id (+)= a.activity_id
12490 			   AND  a.object_country = :l_country';
12494 	                 ' AND  d.id (+)= a.activity_id
12491               ELSE
12492               l_from:=l_from||' ,bim_obj_chnl_mv a ';
12493               l_where := l_where ||
12495    		           AND  a.object_country = :l_country';
12496               END IF;
12497             ELSE
12498               l_from := l_from||' ,bim_obj_chnl_mv a '; --, bim_i_source_codes b ';
12499               l_where  := l_where ||
12500                         '  AND  a.source_code_id = :l_campaign_id
12501                            AND  d.id (+)= a.activity_id
12502 			   AND  a.object_country = :l_country';
12503 	  END IF;
12504 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
12505 /** product category handling**/
12506    IF l_cat_id is null then
12507      l_where := l_where ||l_pc_where;
12508   ELSE
12509      l_from  := l_from ||l_pc_from;
12510      l_where := l_where||l_pc_where;
12511   END IF;
12512     l_col:=' SELECT
12513 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
12514                      null viewbyid,
12515 		     null meaning ';
12516     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
12517     l_from:=' FROM bis_areas_v d '||l_from;
12518 	  IF l_campaign_id is null then
12519 	      IF l_admin_status ='Y' THEN
12520 	      l_from:=l_from||' ,bim_mkt_regn_mv a ';
12521               l_where := l_where ||
12522                          ' AND  d.id (+)= a.object_region
12523 			   AND  a.object_country = :l_country';
12524               ELSE
12525               l_from:=l_from||' ,bim_obj_regn_mv a ';
12526               l_where := l_where ||
12527 	                 ' AND  d.id (+)= a.object_region
12528    		           AND  a.object_country = :l_country';
12529               END IF;
12530             ELSE
12531               l_from := l_from||' ,bim_obj_regn_mv a ' ; --, bim_i_source_codes b ';
12532               l_where  := l_where ||
12533                         '  AND  a.source_code_id = :l_campaign_id
12534                            AND  d.id (+)= a.object_region
12535 			   AND  a.object_country = :l_country';
12536 	  END IF;
12537 END IF;
12538 
12539 /* combine sql one to pick up current period values and  sql two to pick previous period values */
12540   l_select := l_col||
12541               l_comm_cols||
12542 	      l_from||
12543 	      l_where ||' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
12544 	      l_groupby ||
12545 	      l_pc_select /* l_pc_select only applicable when product category is not all and view by is product category */
12546 	      ;
12547 
12548 /* prepare final sql */
12549 
12550  l_sqltext:= l_select_cal||
12551              l_select||
12552 	     l_select_filter;
12553   END IF;
12554 
12555    x_custom_sql := l_sqltext;
12556    l_custom_rec.attribute_name := ':l_record_type';
12557    l_custom_rec.attribute_value := l_record_type_id;
12558    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
12559    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
12560    x_custom_output.EXTEND;
12561    x_custom_output (1) := l_custom_rec;
12562    l_custom_rec.attribute_name := ':l_resource_id';
12563    l_custom_rec.attribute_value := get_resource_id;
12564    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
12565    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
12566    x_custom_output.EXTEND;
12567    x_custom_output (2) := l_custom_rec;
12568    l_custom_rec.attribute_name := ':l_admin_flag';
12569    l_custom_rec.attribute_value := get_admin_status;
12570    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
12571    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
12572    x_custom_output.EXTEND;
12573    x_custom_output (3) := l_custom_rec;
12574    l_custom_rec.attribute_name := ':l_country';
12575    l_custom_rec.attribute_value := l_country;
12576    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
12577    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
12578    x_custom_output.EXTEND;
12579    x_custom_output (4) := l_custom_rec;
12580    l_custom_rec.attribute_name := ':l_cat_id';
12581    l_custom_rec.attribute_value := l_cat_id;
12582    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
12583    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
12584    x_custom_output.EXTEND;
12585    x_custom_output (5) := l_custom_rec;
12586    l_custom_rec.attribute_name := ':l_campaign_id';
12587    l_custom_rec.attribute_value := l_campaign_id;
12588    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
12589    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
12590    x_custom_output.EXTEND;
12591    x_custom_output (6) := l_custom_rec;
12592    write_debug ('GET_', 'QUERY', '_', l_sqltext);
12593 EXCEPTION
12594    WHEN OTHERS
12595    THEN
12596       l_sql_errm := SQLERRM;
12597       write_debug ('GET_MKTG_A_LEADS_SQL', 'ERROR', l_sql_errm, l_sqltext);
12598 END GET_MKTG_A_LEADS_SQL;
12599 
12600 PROCEDURE GET_MKTG_NEW_LEADS_SQL (
12601    p_page_parameter_tbl   IN              bis_pmv_page_parameter_tbl,
12602    x_custom_sql           OUT NOCOPY      VARCHAR2,
12603    x_custom_output        OUT NOCOPY      bis_query_attributes_tbl
12604 )
12605 IS
12606    l_sqltext                      VARCHAR2 (20000);
12607    iflag                          NUMBER;
12608    l_period_type_hc               NUMBER;
12609    l_as_of_date                   DATE;
12610    l_period_type                  VARCHAR2 (2000);
12614    l_view_by                      VARCHAR2 (4000);
12611    l_record_type_id               NUMBER;
12612    l_comp_type                    VARCHAR2 (2000);
12613    l_country                      VARCHAR2 (4000);
12615    l_sql_errm                     VARCHAR2 (4000);
12616    l_previous_report_start_date   DATE;
12617    l_current_report_start_date    DATE;
12618    l_previous_as_of_date          DATE;
12619    l_period_type_id               NUMBER;
12620    l_user_id                      NUMBER;
12621    l_resource_id                  NUMBER;
12622    l_time_id_column               VARCHAR2 (1000);
12623    l_admin_status                 VARCHAR2 (20);
12624    l_admin_flag                   VARCHAR2 (1);
12625    l_admin_count                  NUMBER;
12626    l_rsid                         NUMBER;
12627    l_curr_aod_str                 VARCHAR2 (80);
12628    l_country_clause               VARCHAR2 (4000);
12629    l_access_clause                VARCHAR2 (4000);
12630    l_access_table                 VARCHAR2 (4000);
12631    l_cat_id                       VARCHAR2 (50)        := NULL;
12632    l_campaign_id                  VARCHAR2 (50)        := NULL;
12633    l_select                       VARCHAR2 (20000); -- to build  inner select to pick data from mviews
12634    l_pc_select                    VARCHAR2 (20000); -- to build  inner select to pick data directly assigned to the product category hirerachy
12635    l_select_cal                   VARCHAR2 (20000); -- to build  select calculation part
12636    l_select_filter                VARCHAR2 (20000); -- to build  select filter part
12637    l_from                         VARCHAR2 (20000);   -- assign common table in  clause
12638    l_where                        VARCHAR2 (20000);  -- static where clause
12639    l_groupby                      VARCHAR2 (2000);  -- to build  group by clause
12640    l_pc_from                      VARCHAR2 (20000);   -- from clause to handle product category
12641    l_pc_where                     VARCHAR2 (20000);   --  where clause to handle product category
12642    l_filtercol                    VARCHAR2 (2000);
12643    l_pc_col                       VARCHAR2(200);
12644    l_pc_groupby                   VARCHAR2(200);
12645    l_comm_cols                    VARCHAR2 (20000);
12646    l_view_disp                    VARCHAR2(100);
12647    l_url_str                      VARCHAR2(1000);
12648    l_url_str_csch                 varchar2(1000);
12649    l_url_str_type                 varchar2(3000);
12650    l_url_str_csch_jtf             varchar2(3000);
12651    l_camp_sel_col                 varchar2(100);
12652    l_camp_groupby_col             varchar2(100);
12653    l_csch_chnl                    varchar2(100);
12654    l_top_cond                     VARCHAR2(100);
12655    l_meaning                      VARCHAR2 (20);
12656    /* variables to hold columns names in l_select clauses */
12657    l_col                          VARCHAR2(1000);
12658    /* cursor to get type of object passed from the page ******/
12659     cursor get_obj_type
12660     is
12661     select object_type
12662     from bim_i_source_codes
12663     where source_code_id=replace(l_campaign_id,'''');
12664     /*********************************************************/
12665    l_custom_rec                  bis_query_attributes;
12666    l_object_type                 varchar2(30);
12667    l_url_link                    varchar2(200);
12668    l_url_camp1                   varchar2(3000);
12669    l_url_camp2                   varchar2(3000);
12670    l_dass                        varchar2(100);  -- variable to store value for  directly assigned lookup value
12671 l_curr VARCHAR2(50);
12672 l_curr_suffix VARCHAR2(50);
12673 --   l_una                        varchar2(100);   -- variable to store value for  Unassigned lookup value
12674    l_leaf_node_flag               varchar2(25);   -- variable to store value leaf_node_flag column in case of product category
12675    l_col_id NUMBER;
12676    l_area VARCHAR2(300);
12677    l_report_name VARCHAR2(300);
12678    l_media VARCHAR2(300);
12679    l_jtf                           varchar2(300);
12680 BEGIN
12681    x_custom_output := bis_query_attributes_tbl ();
12682    l_custom_rec := bis_pmv_parameters_pub.initialize_query_type;
12683 
12684 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
12685    			     			      l_as_of_date,
12686 			    			      l_period_type,
12687 				                      l_record_type_id,
12688 				                      l_comp_type,
12689 				                      l_country,
12690 						      l_view_by,
12691 						      l_cat_id,
12692 						      l_campaign_id,
12693 						      l_curr,
12694 						      l_col_id,
12695 						      l_area,
12696 						      l_media,
12697 						      l_report_name
12698 				                      );
12699    l_meaning:=' null meaning '; -- assigning default value
12700    l_url_camp1:=',null';
12701    l_url_camp2:=',null';
12702 
12703    IF l_country IS NULL THEN
12704       l_country := 'N';
12705    END IF;
12706 /** to add meaning in select clause only in case of campaign view by */
12707   IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
12708   l_meaning:=' ,meaning ';
12709   l_filtercol:=',meaning ';
12710   ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
12711   l_filtercol:=',leaf_node_flag ';
12712   l_meaning:=',null meaning ';
12713   else
12714   l_meaning:=' ,null meaning ';
12715   end if;
12716   /*** to  assigned URL **/
12717 
12718 if l_campaign_id is not null then
12719 -- checking for the object type passed from page
12723  end loop;
12720  for i in get_obj_type
12721  loop
12722  l_object_type:=i.object_type;
12724 end if;
12725    l_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
12726    l_url_str  :='pFunctionName=BIM_I_MKTG_NEW_LEADS&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
12727    --l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objId=';
12728    l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
12729    l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
12730    l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
12731    &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
12732 
12733    IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
12734       l_url_link :=' ,decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) ';
12735       l_view_disp:='viewby';
12736       l_leaf_node_flag :=' ,leaf_node_flag ';
12737    ELSIF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
12738      l_camp_sel_col :=' ,object_id
12739                        ,object_type
12740                        ';
12741      l_camp_groupby_col :=',object_id,object_type ';
12742      l_url_link := ' ,null ';
12743      l_view_disp := 'viewby';
12744 
12745      IF (l_campaign_id is  null or l_object_type='RCAM') then
12746 	l_url_camp1:=', decode(object_type,''EONE'',NULL,'||''''||l_url_str||''''||' )';
12747      ELSIF l_object_type='CAMP' THEN
12748 	l_url_camp2:=', '||''''||l_url_str_type||''''||'||object_id ';
12749 	--l_url_camp1:=',decode(usage,''LITE'','||''''||l_url_str_csch||''''||'||object_id,'||''''||l_url_str_csch_jtf||''''||'||object_id)';
12750 	l_url_camp1:=', '||''''||l_url_str_csch||''''||'||object_id ';
12751 	l_csch_chnl:='|| '' - '' || channel';
12752 	l_camp_sel_col :=l_camp_sel_col|| ',usage,channel';
12753 	l_camp_groupby_col :=l_camp_groupby_col||',usage,channel';
12754      end if;
12755     ELSE
12756       l_url_link:=' ,null ';
12757       l_view_disp:='viewby';
12758    END IF;
12759 
12760 /* l_select_cal is common part of select statement for all view by to calculate grand totals and change */
12761  l_select_cal :='
12762          SELECT '||
12763          l_view_disp ||'
12764 	 ,viewbyid
12765  	 ,bim_attribute2 '||l_csch_chnl||' bim_attribute2
12766  	 ,bim_attribute3
12767  	 ,bim_attribute4
12768  	 ,bim_attribute5
12769  	 ,bim_attribute6
12770 	 ,bim_attribute7
12771  	 ,bim_attribute8
12772          ,bim_attribute6 bim_attribute9
12773 	 ,bim_attribute8 bim_attribute10
12774          ,bim_attribute5 bim_attribute11
12775          ,bim_attribute7 bim_attribute12 '||
12776 	 l_url_link || ' bim_attribute19'||
12777 	 l_url_camp1|| ' bim_attribute20 '||
12778 	 l_url_camp2||' bim_attribute21
12779  	 ,bim_grand_total1
12780  	 ,bim_grand_total2
12781  	 ,bim_grand_total3
12782  	 ,bim_grand_total4
12783  	 ,bim_grand_total5
12784  	 ,bim_grand_total6
12785 	 ,bim_grand_total3 bim_grand_total7
12786          ,bim_grand_total5 bim_grand_total8
12787           FROM
12788 	 (
12789             SELECT
12790             name     VIEWBY'||l_leaf_node_flag||'
12791             ,meaning BIM_ATTRIBUTE2'||l_camp_sel_col||
12792            ' ,leads_new  BIM_ATTRIBUTE3
12793             ,DECODE(prev_leads_new,0,NULL,((leads_new - prev_leads_new)/prev_leads_new)*100) BIM_ATTRIBUTE4
12794             ,leads_customer BIM_ATTRIBUTE5
12795             ,DECODE(prev_leads_customer,0,NULL,((leads_customer - prev_leads_customer)/prev_leads_customer)*100) BIM_ATTRIBUTE6
12796 	    ,leads_prospect BIM_ATTRIBUTE7
12797             ,DECODE(prev_leads_prospect,0,NULL,((leads_prospect - prev_leads_prospect)/prev_leads_prospect)*100)  BIM_ATTRIBUTE8
12798             ,sum(leads_new) over() BIM_GRAND_TOTAL1
12799 	    ,decode(sum(prev_leads_new) over(),0,null,(((sum(leads_new- prev_leads_new) over())/sum(prev_leads_new)over ())*100)) BIM_GRAND_TOTAL2
12800 	    ,sum(leads_customer) over() BIM_GRAND_TOTAL3
12801    	    ,decode(sum(prev_leads_customer) over(),0,null,(((sum(leads_customer- prev_leads_customer) over())/sum(prev_leads_customer)over ())*100)) BIM_GRAND_TOTAL4
12802             ,sum(leads_prospect) over() BIM_GRAND_TOTAL5
12803    	    ,decode(sum(prev_leads_prospect) over(),0,null,(((sum(leads_prospect- prev_leads_prospect) over())/sum(prev_leads_prospect)over ())*100)) BIM_GRAND_TOTAL6
12804             ,VIEWBYID
12805              FROM
12806               (
12807                   SELECT
12808           	      viewbyid
12809           	      ,name'||l_leaf_node_flag||
12810 		      l_meaning||l_camp_sel_col||
12811 		    ', sum(leads_new)        leads_new
12812                        ,sum(leads_customer)   leads_customer
12813      		       ,sum(leads_prospect)   leads_prospect
12814      		       ,sum(prev_leads_new)        prev_leads_new
12815      		       ,sum(prev_leads_customer)  prev_leads_customer
12816 		       ,sum(prev_leads_prospect)   prev_leads_prospect
12817                   FROM
12818           	  ( ';
12819 /* l_comm_cols contains column information common to all select statement for all view by */
12820 
12821 l_comm_cols:=    ' , sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_new,0)) leads_new ,
12825      		   sum(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.leads_customer,0)) prev_leads_customer,
12822                    sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_customer,0))   leads_customer,
12823      		   sum(decode(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.leads_prospect,0))   leads_prospect,
12824      		   sum(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.leads_new,0))   prev_leads_new,
12826 		   sum(decode(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.leads_prospect,0)) prev_leads_prospect ';
12827 
12828 
12829 /* l_from contains time dimension table common to all select statement for all view by */
12830  l_from  :=',fii_time_rpt_struct_v cal ';
12831  /* l_where contains where clause to join time dimension table common to all select statement for all view by */
12832  l_where :=' WHERE a.time_id = cal.time_id
12833              AND  a.period_type_id = cal.period_type_id
12834              AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
12835              AND  cal.calendar_id= -1 ';
12836  /* l_select_filter contains group by and filter clause to remove uneccessary records with zero values */
12837 l_select_filter := ' ) GROUP BY viewbyid,name '||l_filtercol||l_camp_groupby_col||
12838                   ')
12839 		   )
12840          WHERE
12841            bim_attribute3    <> 0
12842           &ORDER_BY_CLAUSE ';
12843  /* get_admin_status to check current user is admin or not */
12844 
12845   l_admin_status := get_admin_status;
12846 
12847 /*********************** security handling ***********************/
12848 
12849 	IF   l_campaign_id is null THEN /******* no security checking at child level ********/
12850 		IF   l_admin_status = 'N' THEN
12851 			IF  l_view_by = 'CAMPAIGN+CAMPAIGN' then
12852 			/*************** program view is enable **************/
12853 				IF l_prog_view='Y' then
12854 					l_from := l_from ||',bim_i_top_objects ac ' ;
12855 					l_where := l_where ||' AND a.source_code_id=ac.source_code_id
12856 					AND ac.resource_id = :l_resource_id ';
12857 					/************************************************/
12858 				ELSE
12859 					l_from := l_from ||',ams_act_access_denorm ac,bim_i_source_codes src ';
12860 					l_where := l_where ||' AND a.source_code_id=src.source_code_id
12861 					AND src.object_id=ac.object_id
12862 					AND src.object_type=ac.object_type
12863 					AND src.object_type NOT IN (''RCAM'')
12864 					AND ac.resource_id = :l_resource_id ';
12865 					END IF;
12866 			ELSE
12867 				l_from := l_from ||',bim_i_top_objects ac ';
12868 				l_where := l_where ||' AND a.source_code_id=ac.source_code_id
12869 				AND ac.resource_id = :l_resource_id ';
12870 			END IF;
12871 
12872 		ELSE
12873 			--i.e Admin User
12874 			IF l_view_by = 'CAMPAIGN+CAMPAIGN' then
12875 
12876 				IF  l_prog_view = 'Y' THEN
12877 
12878 					l_top_cond :=' AND a.immediate_parent_id is null ';
12879 
12880 				END IF;
12881 
12882 			ELSE
12883 				/******** to append parent object id is null for other view by (country and product category) ***/
12884 				l_top_cond :=' AND a.immediate_parent_id is null ';
12885 				/***********/
12886 			END IF;
12887 		END IF;
12888 	END IF;
12889 /************************************************************************/
12890 
12891 
12892    /* product category handling */
12893      IF  l_cat_id is not null then
12894          l_pc_from   :=  ', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
12895 	 l_pc_where  :=  ' AND a.category_id = edh.child_id
12896 			   AND edh.object_type = ''CATEGORY_SET''
12897 			   AND edh.object_id = mdcs.category_set_id
12898 			   AND mdcs.functional_area_id = 11
12899 			   AND edh.dbi_flag = ''Y''
12900 			   AND edh.parent_id = :l_cat_id ';
12901        ELSE
12902         l_pc_where :=     ' AND a.category_id = -9 ';
12903      END IF;
12904 /********************************/
12905 
12906 IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
12907 
12908 	/* forming from clause for the tables which is common to all union all */
12909 	if l_cat_id is not null then
12910 	l_from :=' FROM bim_i_obj_mets_mv a '||l_from||l_pc_from;
12911 	else
12912 	l_from :=' FROM bim_i_obj_mets_mv a '||l_from;
12913 	end if;
12914 
12915 
12916 	/* forming where clause which is common to all union all */
12917 	IF l_prog_view = 'Y' then
12918 
12919 		l_where :=l_where||' AND a.object_country = :l_country '||
12920 							l_pc_where;
12921 
12922 	ELSE
12923 
12924 		l_where :=l_where||' AND a.object_country = :l_country AND name.object_type NOT IN (''RCAM'') '||
12925 						   l_pc_where;
12926 
12927 	END IF;
12928 
12929 
12930 	/* forming group by clause for the common columns for all union all */
12931 	l_groupby:=' GROUP BY a.source_code_id,name.object_type_mean, ';
12932 
12933 	/*** campaign id null means No drill down and view by is camapign hirerachy*/
12934 
12935 	IF l_campaign_id is null THEN
12936 		/*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
12937 		l_sqltext:= l_select_cal||
12938 		/******** inner select start from here */
12939 		/* select to get camapigns and programs  */
12940 		' SELECT
12941 		a.source_code_id VIEWBYID,
12942 		name.name name,
12943 		name.object_id    object_id,
12944 		name.object_type  object_type,
12945 		name.object_type_mean meaning '||
12946 		l_comm_cols ||
12947 		l_from || ' ,bim_i_obj_name_mv name '||
12948 		l_where ||l_top_cond||
12952 		l_groupby|| ' name.name,name.object_id,name.object_type '||
12949 		' AND a.source_code_id=name.source_code_id
12950 		AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
12951 		AND name.language=USERENV(''LANG'')'||
12953 		l_select_filter /* appending filter clause */
12954 		;
12955 	ELSE
12956 		/* source_code_id is passed from the page, object selected from the page to be drill may be program,campaign,event,one off event*****/
12957 		/* appending table in l_form and joining conditon for the bim_i_source_codes */
12958 
12959 		l_where :=l_where ||' AND  a.immediate_parent_id = :l_campaign_id ';
12960 
12961 		-- if program is selected from the page means it may have childern as programs,campaigns,events or one off events
12962 
12963 		IF l_object_type in ('RCAM','EVEH') THEN
12964 			/*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
12965 			l_sqltext:= l_select_cal||
12966 			/******** inner select start from here */
12967 			' SELECT
12968 			a.source_code_id VIEWBYID,
12969 			name.name name,
12970 			name.object_id object_id,
12971 			name.object_type object_type,
12972 			name.object_type_mean meaning '||
12973 			l_comm_cols ||
12974 			l_from || ' ,bim_i_obj_name_mv name '||
12975 			l_where ||
12976 			' AND name.source_code_id = a.source_code_id
12977 			AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
12978 			AND name.language=USERENV(''LANG'')'||
12979 			l_groupby||
12980 			' name.name,name.object_id,name.object_type'||
12981 			l_select_filter ;
12982 		   /*************** if object type is camp then childern are campaign schedules ***/
12983 
12984 		ELSIF l_object_type='CAMP' THEN
12985 
12986 			l_sqltext:= l_select_cal||
12987 			/******** inner select start from here */
12988 			/* select to get camapign schedules  */
12989 			' SELECT
12990 			a.source_code_id VIEWBYID,
12991 			name.name name,
12992 			name.object_id object_id,
12993 			name.object_type object_type,
12994 			name.child_object_usage usage,
12995 			decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel,
12996 			name.object_type_mean meaning '||
12997 			l_comm_cols ||
12998 			l_from || ' ,bim_i_obj_name_mv name,bim_dimv_media chnl '||
12999 			l_where ||
13000 			' AND name.source_code_id = a.source_code_id
13001 			and name.activity_id =chnl.id (+)
13002 			AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
13003 			AND name.language=USERENV(''LANG'')'||
13004 			l_groupby||
13005 			' name.name,name.object_id,name.object_type,name.child_object_usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
13006 			l_select_filter ;
13007 		END IF;
13008 
13009 	END IF;
13010 
13011 	/***** END CAMPAIGN HIRERACHY VIEW HANDLING ******************/
13012 
13013 ELSE
13014  /* view by is product category */
13015  IF (l_view_by ='ITEM+ENI_ITEM_VBH_CAT') THEN
13016   if l_admin_status='N' then
13017  l_from:=replace(l_from,',fii_time_rpt_struct_v cal');
13018  else
13019  l_from:=null;
13020  end if;
13021 /******** handling product category hirerachy ****/
13022 /* picking up value of top level node from product category denorm for category present in  bim_i_obj_mets_mv   */
13023     IF l_cat_id is null then
13024        l_from:=l_from||
13025                ',eni_denorm_hierarchies edh
13026                 ,mtl_default_category_sets mdcs
13027                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
13028                    FROM eni_item_vbh_nodes_v e
13029                    WHERE e.top_node_flag=''Y''
13030                    AND e.child_id = e.parent_id) p ';
13031        l_where := l_where||
13032                          ' AND a.category_id = edh.child_id
13033 			   AND edh.object_type = ''CATEGORY_SET''
13034                            AND edh.object_id = mdcs.category_set_id
13035                            AND mdcs.functional_area_id = 11
13036                            AND edh.dbi_flag = ''Y''
13037                            AND edh.parent_id = p.parent_id';
13038        l_col:=' SELECT /*+ORDERED*/
13039 		   p.value name,
13040                    p.parent_id viewbyid,
13041 		   p.leaf_node_flag leaf_node_flag,
13042 		   null meaning ';
13043         l_groupby := ' GROUP BY p.value,p.parent_id,p.leaf_node_flag  ';
13044     ELSE
13045     /* passing id from page and getting immediate child to build hirerachy  */
13046 
13047     /** reassigning value to l_pc_from and l_pc_where for product category hirerachy drill down for values directly assigned to prodcut select from the page*/
13048 
13049      l_pc_from:= l_from||
13050                    ',(select e.id id,e.value value
13051                       from eni_item_vbh_nodes_v e
13052                       where e.parent_id =  :l_cat_id
13053                       AND e.parent_id = e.child_id
13054                       AND leaf_node_flag <> ''Y''
13055                       ) p ';
13056 
13057     l_pc_where :=l_where||
13058                       ' AND a.category_id = p.id ';
13059 
13060     l_from:= l_from||
13061             ',eni_denorm_hierarchies edh
13062             ,mtl_default_category_sets mdc
13063             ,(select e.id,e.value,e.leaf_node_flag
13064               from eni_item_vbh_nodes_v e
13065           where
13066               e.parent_id =:l_cat_id
13067               AND e.id = e.child_id
13071      l_where := l_where||'
13068               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
13069       ) p ';
13070 
13072                   AND a.category_id = edh.child_id
13073                   AND edh.object_type = ''CATEGORY_SET''
13074                   AND edh.object_id = mdc.category_set_id
13075                   AND mdc.functional_area_id = 11
13076                   AND edh.dbi_flag = ''Y''
13077                   AND edh.parent_id = p.id ';
13078 
13079      l_col:=' SELECT /*+ORDERED*/
13080 		   p.value name,
13081                    p.id viewbyid,
13082 		   p.leaf_node_flag leaf_node_flag,
13083 		   null meaning ';
13084      l_groupby := ' GROUP BY p.value,p.id,p.leaf_node_flag ';
13085     END IF;
13086 /*********************/
13087 
13088            IF l_campaign_id is null then /* no drilll down in campaign hirerachy */
13089 	      IF l_admin_status ='Y' THEN
13090               l_from:=' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
13091                               '||l_from;
13092               l_where := l_where ||l_top_cond||
13093                          '  AND  a.object_country = :l_country';
13094                IF l_cat_id is not null then
13095 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
13096                               '||l_pc_from;
13097 		  l_pc_where := l_pc_where ||l_top_cond||
13098                          ' AND  a.object_country = :l_country';
13099                END IF;
13100               ELSE
13101               l_from:=' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
13102                              '||l_from;
13103               l_where := l_where ||
13104 	                   ' AND  a.object_country = :l_country';
13105 
13106 		IF l_cat_id is not null then
13107 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
13108                               '||l_pc_from;
13109 		  l_pc_where := l_pc_where ||
13110                          ' AND  a.object_country = :l_country';
13111                END IF;
13112 
13113               END IF;
13114            ELSE
13115               l_from := ' FROM   fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a '||l_from ;
13116               l_where  := l_where ||
13117                         '  AND  a.source_code_id = :l_campaign_id
13118                            AND  a.object_country = :l_country' ;
13119               IF l_cat_id is not null then
13120 	      l_pc_from := ' FROM   fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a '||l_pc_from ;
13121               l_pc_where  := l_pc_where ||
13122                         '  AND  a.source_code_id = :l_campaign_id
13123 			   AND  a.object_country = :l_country' ;
13124 	      END IF;
13125 	   END IF;
13126    /* building l_pc_select to get values directly assigned to product category passed from the page */
13127    IF l_cat_id is not null  THEN
13128        	  l_pc_col:=' SELECT /*+ORDERED*/
13129 		   bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||' name,
13130                    p.id  viewbyid,
13131 		   ''Y'' leaf_node_flag,
13132 		   null meaning ';
13133      l_pc_groupby := ' GROUP BY p.id ';
13134 
13135   l_pc_select :=
13136               ' UNION ALL ' ||
13137               l_pc_col||
13138               l_comm_cols||
13139 	      l_pc_from||
13140 	      l_pc_where ||'  AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)'||
13141 	      l_pc_groupby ;
13142    END IF;
13143  ELSIF (l_view_by ='CUSTOMER CATEGORY+CUSTOMER CATEGORY') THEN
13144   /** product category handling**/
13145    IF l_cat_id is null then
13146      l_where := l_where ||l_pc_where;
13147   ELSE
13148      l_from  := l_from ||l_pc_from;
13149      l_where := l_where||l_pc_where;
13150   END IF;
13151     l_col:=' SELECT
13152 		   decode(d.customer_category_name,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.customer_category_name) name,
13153                    null viewbyid,
13154 		   null meaning ';
13155     l_groupby := ' GROUP BY decode(d.customer_category_name,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.customer_category_name) ';
13156     l_from:=' FROM bic_cust_category_v d '||l_from;
13157 
13158 	  IF l_campaign_id is null then
13159 	      IF l_admin_status ='Y' THEN
13160 	      l_from:=l_from||' ,bim_mkt_ld_ccat_mv a ';
13161               l_where := l_where ||
13162                          ' AND d.customer_category_code (+) = a.cust_category
13163 			   AND  a.object_country = :l_country';
13164               ELSE
13165               l_from:=l_from||' ,bim_obj_ld_ccat_mv a ';
13166               l_where := l_where ||
13167 	                 ' AND d.customer_category_code (+) = a.cust_category
13168 			   AND  a.object_country = :l_country ';
13169               END IF;
13170             ELSE
13171               l_from := l_from||' ,bim_obj_ld_ccat_mv a ' ; --, bim_i_source_codes b ';
13172               l_where  := l_where ||
13173                         '  AND  a.source_code_id = :l_campaign_id
13174                            AND  d.customer_category_code (+) = a.cust_category
13175 			   AND  a.object_country = :l_country' ;
13176 	  END IF;
13177  ELSIF (l_view_by ='BIM_LEAD_ATTRIBUTES+BIM_LEAD_SOURCE') THEN
13178    /** product category handling**/
13179    IF l_cat_id is null then
13180      l_where := l_where ||l_pc_where;
13181   ELSE
13182      l_from  := l_from ||l_pc_from;
13183      l_where := l_where||l_pc_where;
13184   END IF;
13188 		     null meaning ';
13185     l_col:=' SELECT
13186 		    decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) name,
13187                      null viewbyid,
13189     l_groupby := ' GROUP BY  decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) ';
13190     l_from:=' FROM as_lookups d '||l_from;
13191 	  IF l_campaign_id is null then
13192 	      IF l_admin_status ='Y' THEN
13193 	      l_from:=l_from||' ,bim_mkt_ld_src_mv a ';
13194               l_where := l_where ||
13195                          ' AND  a.lead_source = d.lookup_code(+)
13196 	                   AND  d.lookup_type (+) = ''SOURCE_SYSTEM''
13197 			   AND  a.object_country = :l_country';
13198               ELSE
13199               l_from:=l_from||' ,bim_obj_ld_src_mv a ';
13200               l_where := l_where ||
13201 	                 ' AND  a.lead_source = d.lookup_code(+)
13202 	                   AND  d.lookup_type (+) = ''SOURCE_SYSTEM''
13203 			   AND  a.object_country = :l_country';
13204               END IF;
13205             ELSE
13206               l_from := l_from||' ,bim_obj_ld_src_mv a ' ; --, bim_i_source_codes b ';
13207               l_where  := l_where ||
13208                         '  AND  a.source_code_id = :l_campaign_id
13209                            AND  a.lead_source = d.lookup_code(+)
13210 	                   AND  d.lookup_type (+) = ''SOURCE_SYSTEM''
13211 			   AND  a.object_country = :l_country ';
13212 	  END IF;
13213 ELSIF (l_view_by ='SALES CHANNEL+BIS_SALES_CHANNEL') THEN
13214    /** product category handling**/
13215    IF l_cat_id is null then
13216      l_where := l_where ||l_pc_where;
13217   ELSE
13218      l_from  := l_from ||l_pc_from;
13219      l_where := l_where||l_pc_where;
13220   END IF;
13221     l_col:=' SELECT
13222 		    decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) name,
13223                      null viewbyid,
13224 		     null meaning ';
13225     l_groupby := ' GROUP BY  decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) ';
13226     l_from:=' FROM so_lookups d '||l_from;
13227 	  IF l_campaign_id is null then
13228 	      IF l_admin_status ='Y' THEN
13229 	      l_from:=l_from||' ,bim_mkt_ld_chnl_mv a ';
13230               l_where := l_where ||
13231                          ' AND a.channel_code = d.lookup_code(+)
13232 			   AND d.lookup_type (+) = ''SALES_CHANNEL''
13233 			   AND  a.object_country = :l_country';
13234               ELSE
13235               l_from:=l_from||' ,bim_obj_ld_chnl_mv a ';
13236               l_where := l_where ||
13237 	                 ' AND a.channel_code = d.lookup_code(+)
13238 			   AND d.lookup_type (+) = ''SALES_CHANNEL''
13239    		           AND  a.object_country = :l_country';
13240               END IF;
13241             ELSE
13242               l_from := l_from||' ,bim_obj_ld_chnl_mv a ' ; --, bim_i_source_codes b ';
13243               l_where  := l_where ||
13244                         '  AND  a.source_code_id = :l_campaign_id
13245 			   AND a.channel_code = d.lookup_code(+)
13246 			   AND d.lookup_type (+) = ''SALES_CHANNEL''
13247 			   AND  a.object_country = :l_country';
13248 	  END IF;
13249 ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
13250    /** product category handling**/
13251    IF l_cat_id is null then
13252      l_where := l_where ||l_pc_where;
13253   ELSE
13254      l_from  := l_from ||l_pc_from;
13255      l_where := l_where||l_pc_where;
13256   END IF;
13257     l_col:=' SELECT
13258 		    decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
13259                      a.object_country viewbyid,
13260 		     null meaning ';
13261     l_groupby := ' GROUP BY  decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country ';
13262     l_from:=' FROM fnd_territories_tl d '||l_from;
13263 	  IF l_campaign_id is null then
13264 	      IF l_admin_status ='Y' THEN
13265 	      l_from:=l_from||' ,bim_i_obj_mets_mv a ';
13266               l_where := l_where ||l_top_cond||
13267                          ' AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'')';
13268               ELSE
13269               l_from:=l_from||' ,bim_i_obj_mets_mv a ';
13270               l_where := l_where ||
13271 	                 ' AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'') ';
13272               END IF;
13273             ELSE
13274               l_from := l_from||' ,bim_i_obj_mets_mv a ';  --, bim_i_source_codes b ';
13275               l_where  := l_where ||
13276                         '  AND  a.source_code_id = :l_campaign_id
13277 			   AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'') ';
13278 	  END IF;
13279 	  IF  l_country <>'N' THEN
13280 	      l_where  := l_where ||' AND  a.object_country = :l_country';
13281           ELSE
13282 	   l_where  := l_where ||' AND  a.object_country <> ''N''';
13283 	  END IF;
13284 ELSIF (l_view_by ='BIM_LEAD_ATTRIBUTES+BIM_LEAD_QUALITY') THEN
13285   /** product category handling**/
13286    IF l_cat_id is null then
13287      l_where := l_where ||l_pc_where;
13288   ELSE
13289      l_from  := l_from ||l_pc_from;
13290      l_where := l_where||l_pc_where;
13291   END IF;
13292     l_col:=' SELECT
13293 		    decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) name,
13294                      null viewbyid,
13295 		     null meaning ';
13299 	      IF l_admin_status ='Y' THEN
13296     l_groupby := ' GROUP BY  decode(d.meaning,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.meaning) ';
13297     l_from:=' FROM as_sales_lead_ranks_vl d '||l_from;
13298 	  IF l_campaign_id is null then
13300 	      l_from:=l_from||' ,bim_mkt_ld_qual_mv a ';
13301               l_where := l_where ||
13302                          ' AND d.rank_id (+)= a.lead_rank_id
13303 			   AND  a.object_country = :l_country';
13304               ELSE
13305               l_from:=l_from||' ,bim_obj_ld_qual_mv a ';
13306               l_where := l_where ||
13307 	                 ' AND d.rank_id (+)= a.lead_rank_id
13308    		           AND  a.object_country = :l_country';
13309               END IF;
13310             ELSE
13311               l_from := l_from||' ,bim_obj_ld_qual_mv a ' ; --, bim_i_source_codes b ';
13312               l_where  := l_where ||
13313                         '  AND  a.source_code_id = :l_campaign_id
13314                            AND d.rank_id (+)= a.lead_rank_id
13315 			   AND  a.object_country = :l_country';
13316 	  END IF;
13317 ELSIF (l_view_by ='MEDIA+MEDIA') THEN
13318 /** product category handling**/
13319    IF l_cat_id is null then
13320      l_where := l_where ||l_pc_where;
13321   ELSE
13322      l_from  := l_from ||l_pc_from;
13323      l_where := l_where||l_pc_where;
13324   END IF;
13325     l_col:=' SELECT
13326 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
13327                      null viewbyid,
13328 		     null meaning ';
13329     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
13330     l_from:=' FROM bim_dimv_media d '||l_from;
13331 	  IF l_campaign_id is null then
13332 	      IF l_admin_status ='Y' THEN
13333 	      l_from:=l_from||' ,bim_mkt_chnl_mv a ';
13334               l_where := l_where ||
13335                          ' AND  d.id (+)= a.activity_id
13336 			   AND  a.object_country = :l_country';
13337               ELSE
13338               l_from:=l_from||' ,bim_obj_chnl_mv a ';
13339               l_where := l_where ||
13340 	                 ' AND  d.id (+)= a.activity_id
13341    		           AND  a.object_country = :l_country';
13342               END IF;
13343             ELSE
13344               l_from := l_from||' ,bim_obj_chnl_mv a ' ; --, bim_i_source_codes b ';
13345               l_where  := l_where ||
13346                         '  AND  a.source_code_id = :l_campaign_id
13347                            AND  d.id (+)= a.activity_id
13348 			   AND  a.object_country = :l_country';
13349 	  END IF;
13350 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
13351 /** product category handling**/
13352    IF l_cat_id is null then
13353      l_where := l_where ||l_pc_where;
13354   ELSE
13355      l_from  := l_from ||l_pc_from;
13356      l_where := l_where||l_pc_where;
13357   END IF;
13358     l_col:=' SELECT
13359 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
13360                      null viewbyid,
13361 		     null meaning ';
13362     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
13363     l_from:=' FROM bis_areas_v d '||l_from;
13364 	  IF l_campaign_id is null then
13365 	      IF l_admin_status ='Y' THEN
13366 	      l_from:=l_from||' ,bim_mkt_regn_mv a ';
13367               l_where := l_where ||
13368                          ' AND  d.id (+)= a.object_region
13369 			   AND  a.object_country = :l_country';
13370               ELSE
13371               l_from:=l_from||' ,bim_obj_regn_mv a ';
13372               l_where := l_where ||
13373 	                 ' AND  d.id (+)= a.object_region
13374    		           AND  a.object_country = :l_country';
13375               END IF;
13376             ELSE
13377               l_from := l_from||' ,bim_obj_regn_mv a '  ; --, bim_i_source_codes b ';
13378               l_where  := l_where ||
13379                         '  AND  a.source_code_id = :l_campaign_id
13380                            AND  d.id (+)= a.object_region
13381 			   AND  a.object_country = :l_country';
13382             END IF;
13383 END IF;
13384 
13385 /* combine sql one to pick up current period values and  sql two to pick previous period values */
13386   l_select := l_col||
13387               l_comm_cols||
13388 	      l_from||
13389 	      l_where ||'  AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
13390 	      l_groupby ||
13391 	      l_pc_select /* l_pc_select only applicable when product category is not all and view by is product category */
13392 	      ;
13393 
13394 /* prepare final sql */
13395 
13396  l_sqltext:= l_select_cal||
13397              l_select||
13398 	     l_select_filter;
13399   END IF;
13400 
13401    x_custom_sql := l_sqltext;
13402    l_custom_rec.attribute_name := ':l_record_type';
13403    l_custom_rec.attribute_value := l_record_type_id;
13404    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
13405    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
13406    x_custom_output.EXTEND;
13407    x_custom_output (1) := l_custom_rec;
13408    l_custom_rec.attribute_name := ':l_resource_id';
13409    l_custom_rec.attribute_value := get_resource_id;
13410    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
13411    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
13412    x_custom_output.EXTEND;
13413    x_custom_output (2) := l_custom_rec;
13414    l_custom_rec.attribute_name := ':l_admin_flag';
13418    x_custom_output.EXTEND;
13415    l_custom_rec.attribute_value := get_admin_status;
13416    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
13417    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
13419    x_custom_output (3) := l_custom_rec;
13420    l_custom_rec.attribute_name := ':l_country';
13421    l_custom_rec.attribute_value := l_country;
13422    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
13423    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
13424    x_custom_output.EXTEND;
13425    x_custom_output (4) := l_custom_rec;
13426    l_custom_rec.attribute_name := ':l_cat_id';
13427    l_custom_rec.attribute_value := l_cat_id;
13428    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
13429    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
13430    x_custom_output.EXTEND;
13431    x_custom_output (5) := l_custom_rec;
13432    l_custom_rec.attribute_name := ':l_campaign_id';
13433    l_custom_rec.attribute_value := l_campaign_id;
13434    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
13435    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
13436    x_custom_output.EXTEND;
13437    x_custom_output (6) := l_custom_rec;
13438    write_debug ('GET_MKTG_NEW_LEADS_SQL', 'QUERY', '_', l_sqltext);
13439 EXCEPTION
13440    WHEN OTHERS
13441    THEN
13442       l_sql_errm := SQLERRM;
13443       write_debug ('GET_MKTG_NEW_LEADS_SQL', 'ERROR', l_sql_errm, l_sqltext);
13444 END GET_MKTG_NEW_LEADS_SQL;
13445 
13446 
13447 
13448 PROCEDURE GET_CAMP_START_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
13449                           x_custom_sql  OUT NOCOPY VARCHAR2,
13450                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
13451  IS
13452 l_sqltext varchar2(15000);
13453 iFlag number;
13454 l_period_type_hc number;
13455 l_as_of_date  DATE;
13456 l_period_type	varchar2(2000);
13457 l_record_type_id NUMBER;
13458 l_comp_type    varchar2(2000);
13459 l_country      varchar2(4000);
13460 l_view_by      varchar2(4000);
13461 l_sql_errm      varchar2(4000);
13462 l_previous_report_start_date DATE;
13463 l_current_report_start_date DATE;
13464 l_previous_as_of_date DATE;
13465 l_period_type_id NUMBER;
13466 l_user_id NUMBER;
13467 l_resource_id NUMBER;
13468 l_time_id_column  VARCHAR2(1000);
13469 l_admin_status VARCHAR2(20);
13470 l_admin_flag VARCHAR2(1);
13471 l_admin_count Number;
13472 l_rsid NUMBER;
13473 l_curr_aod_str varchar2(80);
13474 l_country_clause varchar2(4000);
13475 l_access_clause varchar2(4000);
13476 l_access_table varchar2(4000);
13477 --l_cat_id NUMBER;
13478 l_cat_id VARCHAR2(50);
13479 l_col_id NUMBER;
13480 l_area VARCHAR2(300);
13481 l_report_name VARCHAR2(300);
13482 l_campaign_id VARCHAR2(50);
13483 l_custom_rec BIS_QUERY_ATTRIBUTES;
13484 l_dass                 varchar2(100);  -- variable to store value for  directly assigned lookup value
13485 l_una                  varchar2(100);   -- variable to store value for  Unassigned lookup value
13486 l_curr VARCHAR2(50);
13487 l_curr_suffix VARCHAR2(50);
13488 l_media VARCHAR2(300);
13489 BEGIN
13490 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
13491 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
13492 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
13493    			     			      l_as_of_date,
13494 			    			      l_period_type,
13495 				                      l_record_type_id,
13496 				                      l_comp_type,
13497 				                      l_country,
13498 						      l_view_by,
13499 						      l_cat_id,
13500 						      l_campaign_id,
13501 						      l_curr,
13502 						      l_col_id,
13503 						      l_area,
13504 						      l_media,
13505 						      l_report_name
13506 				                      );
13507 
13508    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
13509   /* IF l_country IS NULL THEN
13510       l_country := 'N';
13511    END IF;*/
13512    l_admin_status := GET_ADMIN_STATUS;
13513    /*IF l_admin_flag = 'N' THEN
13514       l_access_table := ',bim_i_top_objects ac ';
13515       l_access_clause := 'AND a.object_type = ac.object_type '||
13516          'AND a.source_code_id = ac.source_code AND ac.resource_id = '||GET_RESOURCE_ID;
13517    ELSE
13518       l_access_table := '';
13519       l_access_clause := '';
13520    END IF;*/
13521 l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
13522 IF l_admin_status = 'Y' THEN
13523 if (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
13524 IF l_cat_id is null then
13525 l_sqltext :=
13526 '
13527 SELECT
13528 VIEWBY,
13529 VIEWBYID,
13530 BIM_ATTRIBUTE2,
13531 BIM_ATTRIBUTE3,
13532 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
13533 BIM_ATTRIBUTE5,
13534 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
13535 BIM_ATTRIBUTE7,
13536 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
13537 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
13538 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
13539 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
13540 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
13541 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
13545 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
13542 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
13543 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
13544 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
13546 decode(leaf_node_id,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_CAMP_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
13547 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_CAMP_START_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL2,
13548 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_CAMP_START_END&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL3,
13549 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_CAMP_START_ACT&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL4
13550 FROM
13551 (
13552 SELECT
13553 name VIEWBY,
13554 id VIEWBYID,
13555 leaf_node_id leaf_node_id,
13556 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
13557 sum(curr_started) BIM_ATTRIBUTE3,
13558 SUM(prev_started) BIM_ATTRIBUTE4,
13559 sum(curr_ended)  BIM_ATTRIBUTE5,
13560 SUm(prev_ended) BIM_ATTRIBUTE6,
13561 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
13562 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
13563 FROM
13564 (
13565 SELECT
13566 p.value name,
13567 p.parent_id id,
13568 p.parent_id leaf_node_id,
13569 sum(camp_started-camp_ended) curr_prior_active,
13570 0 prev_prior_active,
13571 0 curr_active,
13572 0 prev_active,
13573 0 curr_started,
13574 0 prev_started,
13575 0 curr_ended,
13576 0 prev_ended,
13577 0 curr_act_ended,
13578 0 prev_act_ended
13579 FROM bim_mkt_kpi_cnt_mv a,
13580      fii_time_rpt_struct_v cal,
13581      eni_denorm_hierarchies b,
13582      mtl_default_category_sets mdcs,
13583     (select e.parent_id parent_id ,e.value value
13584       from eni_item_vbh_nodes_v e
13585       where
13586       e.top_node_flag=''Y''
13587       AND e.child_id = e.parent_id
13588       ) p
13589 WHERE
13590      a.time_id = cal.time_id
13591 AND  a.period_type_id = cal.period_type_id
13592 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
13593 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
13594 AND cal.calendar_id=-1
13595 AND a.object_country = :l_country
13596 AND  a.category_id = b.child_id
13597 AND b.object_type = ''CATEGORY_SET''
13598 AND b.object_id = mdcs.category_set_id
13599 AND mdcs.functional_area_id = 11
13600 AND b.dbi_flag = ''Y''
13601 AND p.parent_id = b.parent_id';
13602 IF l_cat_id is not null
13603 then
13604 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
13605 end if;
13606 l_sqltext := l_sqltext ||' group by p.value,p.parent_id';
13607 l_sqltext :=l_sqltext ||
13608 ' UNION ALL
13609 SELECT
13610 p.value name,
13611 p.parent_id id,
13612 p.parent_id leaf_node_id,
13613 0 curr_prior_active,
13614 sum(camp_started-camp_ended) prev_prior_active,
13615 0 curr_active,
13616 0 prev_active,
13617 0 curr_started,
13618 0 prev_started,
13619 0 curr_ended,
13620 0 prev_ended,
13621 0 curr_act_ended,
13622 0 prev_act_ended
13623 FROM bim_mkt_kpi_cnt_mv a,
13624      fii_time_rpt_struct_v cal,
13625      eni_denorm_hierarchies b,
13626      mtl_default_category_sets mdcs,
13627     (select e.parent_id parent_id ,e.value value
13628       from eni_item_vbh_nodes_v e
13629       where
13630       e.top_node_flag=''Y''
13631       AND e.child_id = e.parent_id
13632       ) p
13633 WHERE
13634      a.time_id = cal.time_id
13635 AND  a.period_type_id = cal.period_type_id
13636 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
13637 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
13638 AND cal.calendar_id=-1
13639 AND a.object_country = :l_country
13640 AND  a.category_id = b.child_id
13641 AND b.object_type = ''CATEGORY_SET''
13642 AND b.object_id = mdcs.category_set_id
13643 AND mdcs.functional_area_id = 11
13644 AND b.dbi_flag = ''Y''
13645 AND p.parent_id = b.parent_id ';
13646 IF l_cat_id is not null
13647 then
13648 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
13649 end if;
13650 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
13651  UNION ALL
13652 SELECT
13653 p.value name,
13654 p.parent_id id,
13655 p.parent_id leaf_node_id,
13656 0 curr_prior_active,
13657 0 prev_prior_active,
13658 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
13659 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
13660 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
13661 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
13662 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
13663 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
13664 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
13668      eni_denorm_hierarchies b,
13665 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
13666 FROM bim_mkt_kpi_cnt_mv a,
13667      fii_time_rpt_struct_v cal,
13669      mtl_default_category_sets mdcs,
13670      (select e.parent_id parent_id ,e.value value
13671       from eni_item_vbh_nodes_v e
13672       where
13673       e.top_node_flag=''Y''
13674       AND e.child_id = e.parent_id
13675       ) p
13676 WHERE
13677      a.time_id = cal.time_id
13678 AND  a.period_type_id = cal.period_type_id
13679 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
13680 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
13681 AND cal.calendar_id=-1
13682 AND a.object_country = :l_country
13683 AND  a.category_id = b.child_id
13684     AND b.object_type = ''CATEGORY_SET''
13685     AND b.object_id = mdcs.category_set_id
13686     AND mdcs.functional_area_id = 11
13687     AND b.dbi_flag = ''Y''
13688     AND p.parent_id = b.parent_id ';
13689 IF l_cat_id is not null
13690 then
13691 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
13692 end if;
13693 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
13694 ) group by name,id,leaf_node_id )';
13695 ELSE
13696 -- for product category not equal to all
13697 -- current bookmark
13698 l_sqltext :=
13699 '
13700 SELECT
13701 VIEWBY,
13702 VIEWBYID,
13703 BIM_ATTRIBUTE2,
13704 BIM_ATTRIBUTE3,
13705 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
13706 BIM_ATTRIBUTE5,
13707 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
13708 BIM_ATTRIBUTE7,
13709 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
13710 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
13711 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
13712 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
13713 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
13714 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
13715 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
13716 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
13717 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
13718 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
13719 decode(leaf_node_id,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_CAMP_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
13720 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_CAMP_START_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL2,
13721 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_CAMP_START_END&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL3,
13722 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_CAMP_START_ACT&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL4
13723 
13724 FROM
13725 (
13726 SELECT
13727 name VIEWBY,
13728 id VIEWBYID,
13729 leaf_node_id leaf_node_id,
13730 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
13731 sum(curr_started) BIM_ATTRIBUTE3,
13732 SUM(prev_started) BIM_ATTRIBUTE4,
13733 sum(curr_ended)  BIM_ATTRIBUTE5,
13734 SUm(prev_ended) BIM_ATTRIBUTE6,
13735 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
13736 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
13737 FROM
13738 (
13739 SELECT
13740 p.value name,
13741 b.parent_id id,
13742 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
13743 sum(camp_started-camp_ended) curr_prior_active,
13744 0 prev_prior_active,
13745 0 curr_active,
13746 0 prev_active,
13747 0 curr_started,
13748 0 prev_started,
13749 0 curr_ended,
13750 0 prev_ended,
13751 0 curr_act_ended,
13752 0 prev_act_ended
13753 FROM bim_mkt_kpi_cnt_mv a,
13754      fii_time_rpt_struct_v cal,
13755      eni_denorm_hierarchies b,
13756      mtl_default_category_sets mdcs,
13757     (select e.id id ,e.value value,leaf_node_flag
13758       from eni_item_vbh_nodes_v e
13759       where e.parent_id =:l_cat_id
13760       AND e.id = e.child_id
13761       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
13762       ) p
13763 WHERE
13764      a.time_id = cal.time_id
13765 AND  a.period_type_id = cal.period_type_id
13766 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
13767 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
13768 AND cal.calendar_id=-1
13769 AND a.object_country = :l_country
13770 AND a.category_id = b.child_id
13771 AND b.object_type = ''CATEGORY_SET''
13772 AND b.object_id = mdcs.category_set_id
13773 AND mdcs.functional_area_id = 11
13774 AND b.parent_id = p.id
13775 AND b.dbi_flag = ''Y''';
13776 l_sqltext := l_sqltext ||' group by p.value,decode(p.leaf_node_flag,''Y'',-1,b.parent_id),b.parent_id';
13777 l_sqltext :=l_sqltext ||
13778 ' UNION ALL
13779 SELECT
13780 p.value name,
13781 a.category_id id,
13782 -1 leaf_node_id,
13783 sum(camp_started-camp_ended) curr_prior_active,
13787 0 curr_started,
13784 0 prev_prior_active,
13785 0 curr_active,
13786 0 prev_active,
13788 0 prev_started,
13789 0 curr_ended,
13790 0 prev_ended,
13791 0 curr_act_ended,
13792 0 prev_act_ended
13793 FROM bim_mkt_kpi_cnt_mv a,
13794      fii_time_rpt_struct_v cal,
13795     (select e.id id ,e.value value
13796       from eni_item_vbh_nodes_v e
13797       where e.parent_id =  :l_cat_id
13798       AND e.parent_id = e.child_id
13799       AND leaf_node_flag <> ''Y''
13800       ) p
13801 WHERE
13802      a.time_id = cal.time_id
13803 AND  a.period_type_id = cal.period_type_id
13804 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
13805 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
13806 AND cal.calendar_id=-1
13807 AND a.object_country = :l_country
13808 AND a.category_id = p.id';
13809 /*IF l_cat_id is not null
13810 then
13811 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
13812 end if;*/
13813 l_sqltext := l_sqltext ||' group by p.value,a.category_id
13814 UNION ALL
13815 SELECT
13816 p.value name,
13817 b.parent_id id,
13818 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
13819 0 curr_prior_active,
13820 sum(camp_started-camp_ended) prev_prior_active,
13821 0 curr_active,
13822 0 prev_active,
13823 0 curr_started,
13824 0 prev_started,
13825 0 curr_ended,
13826 0 prev_ended,
13827 0 curr_act_ended,
13828 0 prev_act_ended
13829 FROM bim_mkt_kpi_cnt_mv a,
13830      fii_time_rpt_struct_v cal,
13831      eni_denorm_hierarchies b,
13832      mtl_default_category_sets mdcs,
13833     (select e.id id ,e.value value,leaf_node_flag
13834       from eni_item_vbh_nodes_v e
13835       where e.parent_id =:l_cat_id
13836       AND e.id = e.child_id
13837       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'') ) p
13838 WHERE a.time_id = cal.time_id
13839 AND  a.period_type_id = cal.period_type_id
13840 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
13841 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
13842 AND cal.calendar_id=-1
13843 AND a.object_country = :l_country
13844 AND a.category_id = b.child_id
13845 AND b.object_type = ''CATEGORY_SET''
13846 AND b.object_id = mdcs.category_set_id
13847 AND mdcs.functional_area_id = 11
13848 AND b.dbi_flag =''Y''
13849 AND b.parent_id = p.id ';
13850 /*IF l_cat_id is not null
13851 then
13852 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
13853 end if;*/
13854 l_sqltext := l_sqltext ||' group by p.value,decode(p.leaf_node_flag,''Y'',-1,b.parent_id),b.parent_id
13855 UNION ALL
13856 SELECT
13857 p.value name,
13858 a.category_id id,
13859 -1 leaf_node_id,
13860 0 curr_prior_active,
13861 sum(camp_started-camp_ended) prev_prior_active,
13862 0 curr_active,
13863 0 prev_active,
13864 0 curr_started,
13865 0 prev_started,
13866 0 curr_ended,
13867 0 prev_ended,
13868 0 curr_act_ended,
13869 0 prev_act_ended
13870 FROM bim_mkt_kpi_cnt_mv a,
13871      fii_time_rpt_struct_v cal,
13872     (select e.id id ,e.value value
13873       from eni_item_vbh_nodes_v e
13874       where e.parent_id =  :l_cat_id
13875       AND e.parent_id = e.child_id
13876       AND leaf_node_flag <> ''Y''
13877       ) p
13878 WHERE
13879      a.time_id = cal.time_id
13880 AND  a.period_type_id = cal.period_type_id
13881 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
13882 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
13883 AND cal.calendar_id=-1
13884 AND a.object_country = :l_country
13885 AND a.category_id = p.id
13886 ';
13887 /*IF l_cat_id is not null
13888 then
13889 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
13890 end if;*/
13891 --l_sqltext := l_sqltext ||' group by p.value,a.category_id
13892 l_sqltext := l_sqltext ||' group by p.value,a.category_id
13893  UNION ALL
13894 SELECT
13895 p.value name,
13896 b.parent_id id,
13897 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
13898 0 curr_prior_active,
13899 0 prev_prior_active,
13900 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
13901 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
13902 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
13903 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
13904 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
13905 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
13906 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
13907 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
13908 FROM bim_mkt_kpi_cnt_mv a,
13909      fii_time_rpt_struct_v cal,
13910      eni_denorm_hierarchies b,
13911      mtl_default_category_sets mdcs,
13912      (select e.id id ,e.value value,leaf_node_flag
13913       from eni_item_vbh_nodes_v e
13914       where e.parent_id =:l_cat_id
13915       AND e.id = e.child_id
13916       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')) p
13917 WHERE
13918      a.time_id = cal.time_id
13919 AND  a.period_type_id = cal.period_type_id
13923 AND a.object_country = :l_country
13920 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
13921 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
13922 AND cal.calendar_id=-1
13924 AND a.category_id = b.child_id
13925 AND b.object_type = ''CATEGORY_SET''
13926 AND b.object_id = mdcs.category_set_id
13927 AND mdcs.functional_area_id = 11
13928 AND b.dbi_flag = ''Y''
13929 AND b.parent_id = p.id ';
13930 /*IF l_cat_id is not null
13931 then
13932 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
13933 end if;*/
13934 l_sqltext := l_sqltext ||' group by p.value,decode(p.leaf_node_flag,''Y'',-1,b.parent_id),b.parent_id
13935 UNION ALL
13936 SELECT
13937 p.value name,
13938 a.category_id,
13939 -1 leaf_node_id,
13940 0 curr_prior_active,
13941 0 prev_prior_active,
13942 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
13943 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
13944 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
13945 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
13946 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
13947 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
13948 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
13949 SUM(case when &BIS_PREVIOUS_ASOF_DATE > &BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
13950 FROM bim_mkt_kpi_cnt_mv a,
13951      fii_time_rpt_struct_v cal,
13952      (select e.id id ,e.value value
13953       from eni_item_vbh_nodes_v e
13954       where e.parent_id =  :l_cat_id
13955       AND e.parent_id = e.child_id
13956       AND leaf_node_flag <> ''Y''
13957       ) p
13958 WHERE
13959      a.time_id = cal.time_id
13960 AND  a.period_type_id = cal.period_type_id
13961 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
13962 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
13963 AND cal.calendar_id=-1
13964 AND a.object_country = :l_country
13965 AND a.category_id = p.id
13966  group by p.value,a.category_id) group by name,id,leaf_node_id )';
13967 END IF; -- end product category numm loop
13968 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
13969 l_sqltext :=
13970 '
13971 SELECT
13972 VIEWBY,
13973 VIEWBYID,
13974 BIM_ATTRIBUTE2,
13975 BIM_ATTRIBUTE3,
13976 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
13977 BIM_ATTRIBUTE5,
13978 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
13979 BIM_ATTRIBUTE7,
13980 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
13981 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
13982 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
13983 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
13984 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
13985 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
13986 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
13987 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
13988 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
13989 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
13990 null BIM_URL1,
13991 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_CAMP_START_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=campaign&BIM_PARAMETER5=VIEWBY'') BIM_URL2,
13992 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_CAMP_START_END&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=campaign&BIM_PARAMETER5=VIEWBY'') BIM_URL3,
13993 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_CAMP_START_ACT&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=campaign&BIM_PARAMETER5=VIEWBY'') BIM_URL4
13994 FROM
13995 (
13996 SELECT
13997 name VIEWBY,
13998 id VIEWBYID,
13999 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
14000 sum(curr_started) BIM_ATTRIBUTE3,
14001 SUM(prev_started) BIM_ATTRIBUTE4,
14002 sum(curr_ended)  BIM_ATTRIBUTE5,
14003 SUM(prev_ended) BIM_ATTRIBUTE6,
14004 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
14005 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
14006 FROM
14007 (
14008 SELECT
14009 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
14010 a.object_region id,
14011 sum(camp_started-camp_ended) curr_prior_active,
14012 0 prev_prior_active,
14013 0 curr_active,
14014 0 prev_active,
14015 0 curr_started,
14016 0 prev_started,
14017 0 curr_ended,
14018 0 prev_ended,
14019 0 curr_act_ended,
14020 0 prev_act_ended
14021 FROM bim_mkt_regn_mv a,
14022 fii_time_rpt_struct_v cal,
14023 bis_areas_v d ';
14024  IF l_cat_id is not null then
14025   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
14026   end if;
14030 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14027   l_sqltext := l_sqltext ||
14028 ' WHERE a.time_id = cal.time_id
14029 AND a.period_type_id = cal.period_type_id
14031 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
14032 AND cal.calendar_id=-1
14033 AND  a.object_region =d.id(+)
14034 AND a.object_country=:l_country ';
14035 IF l_cat_id is null then
14036 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
14037 else
14038   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
14039 			     AND edh.object_type = ''CATEGORY_SET''
14040 			     AND edh.object_id = mdc.category_set_id
14041 			     AND mdc.functional_area_id = 11
14042 			     AND edh.dbi_flag = ''Y''
14043 			     AND edh.parent_id = :l_cat_id  ';
14044   end if;
14045  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ,a.object_region ';
14046 l_sqltext :=l_sqltext ||
14047 ' UNION ALL
14048 SELECT
14049 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
14050 a.object_region id,
14051 0 curr_prior_active,
14052 sum(camp_started-camp_ended) prev_prior_active,
14053 0 curr_active,
14054 0 prev_active,
14055 0 curr_started,
14056 0 prev_started,
14057 0 curr_ended,
14058 0 prev_ended,
14059 0 curr_act_ended,
14060 0 prev_act_ended
14061 FROM bim_mkt_regn_mv a,
14062 fii_time_rpt_struct_v cal,
14063 bis_areas_v d';
14064  IF l_cat_id is not null then
14065   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
14066   end if;
14067   l_sqltext := l_sqltext ||
14068 ' WHERE a.time_id = cal.time_id
14069 AND a.period_type_id = cal.period_type_id
14070 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14071 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
14072 AND cal.calendar_id=-1
14073 AND  a.object_region =d.id(+)
14074 AND a.object_country = :l_country';
14075 IF l_cat_id is null then
14076 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
14077 else
14078   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
14079 			     AND edh.object_type = ''CATEGORY_SET''
14080 			     AND edh.object_id = mdc.category_set_id
14081 			     AND mdc.functional_area_id = 11
14082 			     AND edh.dbi_flag = ''Y''
14083 			     AND edh.parent_id = :l_cat_id  ';
14084   end if;
14085  l_sqltext := l_sqltext ||'group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
14086  l_sqltext := l_sqltext ||
14087 ' UNION ALL
14088 SELECT
14089 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
14090 a.object_region id,
14091 0 curr_prior_active,
14092 0 prev_prior_active,
14093 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
14094 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
14095 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
14096 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
14097 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
14098 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
14099 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
14100 SUM(case when &BIS_PREVIOUS_ASOF_DATE > &BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
14101 FROM bim_mkt_regn_mv a,
14102 fii_time_rpt_struct_v cal,
14103 bis_areas_v d';
14104  IF l_cat_id is not null then
14105   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
14106   end if;
14107   l_sqltext := l_sqltext ||
14108 ' WHERE a.time_id = cal.time_id
14109 AND a.period_type_id = cal.period_type_id
14110 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
14111 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1)
14112 AND cal.calendar_id=-1
14113 AND  a.object_region =d.id (+)
14114 AND a.object_country = :l_country ';
14115 IF l_cat_id is null then
14116 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
14117 else
14118   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
14119 			     AND edh.object_type = ''CATEGORY_SET''
14120 			     AND edh.object_id = mdc.category_set_id
14121 			     AND mdc.functional_area_id = 11
14122 			     AND edh.dbi_flag = ''Y''
14123 			     AND edh.parent_id = :l_cat_id ';
14124   end if;
14125 l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region ) group by name,id )';
14126 ELSE
14127 l_sqltext :=
14128 '
14129 SELECT
14130 VIEWBY,
14131 VIEWBYID,
14132 BIM_ATTRIBUTE2,
14133 BIM_ATTRIBUTE3,
14134 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
14135 BIM_ATTRIBUTE5,
14136 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
14137 BIM_ATTRIBUTE7,
14138 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
14139 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
14140 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
14141 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
14145 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
14142 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
14143 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
14144 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
14146 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
14147 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
14148 null BIM_URL1,
14149 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_CAMP_START_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL2,
14150 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_CAMP_START_END&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL3,
14151 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_CAMP_START_ACT&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL4
14152 FROM
14153 (
14154 SELECT
14155 name VIEWBY,
14156 id VIEWBYID,
14157 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
14158 sum(curr_started) BIM_ATTRIBUTE3,
14159 SUM(prev_started) BIM_ATTRIBUTE4,
14160 sum(curr_ended)  BIM_ATTRIBUTE5,
14161 SUM(prev_ended) BIM_ATTRIBUTE6,
14162 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
14163 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
14164 FROM
14165 (
14166 SELECT
14167 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
14168 a.object_country id,
14169 sum(camp_started-camp_ended) curr_prior_active,
14170 0 prev_prior_active,
14171 0 curr_active,
14172 0 prev_active,
14173 0 curr_started,
14174 0 prev_started,
14175 0 curr_ended,
14176 0 prev_ended,
14177 0 curr_act_ended,
14178 0 prev_act_ended
14179 FROM bim_mkt_kpi_cnt_mv a,
14180 fii_time_rpt_struct_v cal,
14181 bis_countries_v d ';
14182  IF l_cat_id is not null then
14183   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
14184   end if;
14185   l_sqltext := l_sqltext ||
14186 ' WHERE a.time_id = cal.time_id
14187 AND a.period_type_id = cal.period_type_id
14188 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14189 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
14190 AND cal.calendar_id=-1
14191 AND  a.object_country =d.country_code (+) ';
14192 IF l_cat_id is null then
14193 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
14194 else
14195   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
14196 			     AND edh.object_type = ''CATEGORY_SET''
14197 			     AND edh.object_id = mdc.category_set_id
14198 			     AND mdc.functional_area_id = 11
14199 			     AND edh.dbi_flag = ''Y''
14200 			     AND edh.parent_id = :l_cat_id  ';
14201   end if;
14202  IF l_country = 'N' THEN
14203      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
14204  ELSE
14205   l_sqltext := l_sqltext ||
14206 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
14207  END IF;
14208 /*l_sqltext := l_sqltext ||
14209 ' group by object_country*/
14210 l_sqltext :=l_sqltext ||
14211 ' UNION ALL
14212 SELECT
14213 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
14214 a.object_country id,
14215 0 curr_prior_active,
14216 sum(camp_started-camp_ended) prev_prior_active,
14217 0 curr_active,
14218 0 prev_active,
14219 0 curr_started,
14220 0 prev_started,
14221 0 curr_ended,
14222 0 prev_ended,
14223 0 curr_act_ended,
14224 0 prev_act_ended
14225 FROM bim_mkt_kpi_cnt_mv a,
14226 fii_time_rpt_struct_v cal,
14227 bis_countries_v d';
14228  IF l_cat_id is not null then
14229   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
14230   end if;
14231   l_sqltext := l_sqltext ||
14232 ' WHERE a.time_id = cal.time_id
14233 AND a.period_type_id = cal.period_type_id
14234 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14235 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
14236 AND cal.calendar_id=-1
14237 AND  a.object_country =d.country_code (+)';
14238 IF l_cat_id is null then
14239 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
14240 else
14241   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
14242 			     AND edh.object_type = ''CATEGORY_SET''
14243 			     AND edh.object_id = mdc.category_set_id
14244 			     AND mdc.functional_area_id = 11
14245 			     AND edh.dbi_flag = ''Y''
14246 			     AND edh.parent_id = :l_cat_id  ';
14247   end if;
14248  IF l_country = 'N' THEN
14249      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
14250  ELSE
14251   l_sqltext := l_sqltext ||
14252 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
14253  END IF;
14254 l_sqltext := l_sqltext ||
14255 ' UNION ALL
14256 SELECT
14260 0 prev_prior_active,
14257 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
14258 a.object_country id,
14259 0 curr_prior_active,
14261 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
14262 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
14263 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
14264 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
14265 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
14266 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
14267 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
14268 SUM(case when &BIS_PREVIOUS_ASOF_DATE > &BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
14269 FROM bim_mkt_kpi_cnt_mv a,
14270 fii_time_rpt_struct_v cal,
14271 bis_countries_v d';
14272  IF l_cat_id is not null then
14273   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
14274   end if;
14275   l_sqltext := l_sqltext ||
14276 ' WHERE a.time_id = cal.time_id
14277 AND a.period_type_id = cal.period_type_id
14278 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
14279 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1)
14280 AND cal.calendar_id=-1
14281 AND  a.object_country =d.country_code (+)';
14282 IF l_cat_id is null then
14283 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
14284 else
14285   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
14286 			     AND edh.object_type = ''CATEGORY_SET''
14287 			     AND edh.object_id = mdc.category_set_id
14288 			     AND mdc.functional_area_id = 11
14289 			     AND edh.dbi_flag = ''Y''
14290 			     AND edh.parent_id = :l_cat_id ';
14291   end if;
14292 IF l_country = 'N' THEN
14293      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id)';
14294  ELSE
14295   l_sqltext := l_sqltext ||
14296 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id,leaf_node_id)';
14297  END IF;
14298 END IF;
14299 ELSE -- if admin_flag is not equal to Y
14300 if (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
14301 IF l_cat_id is null THEN
14302 l_sqltext :=
14303 '
14304 SELECT
14305 VIEWBY,
14306 VIEWBYID,
14307 BIM_ATTRIBUTE2,
14308 BIM_ATTRIBUTE3,
14309 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
14310 BIM_ATTRIBUTE5,
14311 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
14312 BIM_ATTRIBUTE7,
14313 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
14314 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
14315 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
14316 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
14317 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
14318 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
14319 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
14320 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
14321 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
14322 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
14323 decode(leaf_node_id,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_CAMP_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
14324 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_CAMP_START_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL2,
14325 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_CAMP_START_END&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL3,
14326 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_CAMP_START_ACT&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL4
14327 FROM
14328 (
14329 SELECT
14330 name VIEWBY,
14331 id VIEWBYID,
14332 leaf_node_id leaf_node_id,
14333 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
14334 sum(curr_started) BIM_ATTRIBUTE3,
14335 SUM(prev_started) BIM_ATTRIBUTE4,
14336 sum(curr_ended)  BIM_ATTRIBUTE5,
14337 SUm(prev_ended) BIM_ATTRIBUTE6,
14338 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
14339 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
14340 FROM
14341 (
14342 SELECT
14343 p.value name,
14344 p.parent_id id,
14345 p.parent_id leaf_node_id,
14346 sum(camp_started-camp_ended) curr_prior_active,
14347 0 prev_prior_active,
14348 0 curr_active,
14349 0 prev_active,
14350 0 curr_started,
14351 0 prev_started,
14352 0 curr_ended,
14353 0 prev_ended,
14354 0 curr_act_ended,
14355 0 prev_act_ended
14359      mtl_default_category_sets mdcs,';
14356 FROM bim_i_obj_mets_mv a,
14357      fii_time_rpt_struct_v cal,
14358      eni_denorm_hierarchies b,
14360 IF l_prog_view='Y' then
14361   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
14362 ELSE
14363 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
14364 END IF;
14365  l_sqltext := l_sqltext ||' ,(select e.parent_id parent_id ,e.value value
14366       from eni_item_vbh_nodes_v e
14367       where
14368       e.top_node_flag=''Y''
14369       AND e.child_id = e.parent_id
14370       ) p
14371 WHERE
14372      a.time_id = cal.time_id
14373 AND  a.period_type_id = cal.period_type_id
14374 AND a.immediate_parent_id is null
14375 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14376 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
14377 AND cal.calendar_id=-1
14378 AND a.object_country = :l_country
14379 AND  a.category_id = b.child_id
14380 AND b.object_type = ''CATEGORY_SET''
14381 AND b.object_id = mdcs.category_set_id
14382 AND mdcs.functional_area_id = 11
14383 AND b.dbi_flag = ''Y''
14384 AND p.parent_id = b.parent_id
14385 AND ac.resource_id = :l_resource_id';
14386 IF l_prog_view='N' then
14387 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
14388 AND s.object_id = ac.object_id
14389 AND s.object_type=ac.object_type
14390 ';
14391 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
14392 END IF;
14393 IF l_cat_id is not null
14394 then
14395 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
14396 end if;
14397 l_sqltext := l_sqltext ||' group by p.value,p.parent_id';
14398 l_sqltext :=l_sqltext ||
14399 ' UNION ALL
14400 SELECT
14401 p.value name,
14402 p.parent_id id,
14403 p.parent_id leaf_node_id,
14404 0 curr_prior_active,
14405 sum(camp_started-camp_ended) prev_prior_active,
14406 0 curr_active,
14407 0 prev_active,
14408 0 curr_started,
14409 0 prev_started,
14410 0 curr_ended,
14411 0 prev_ended,
14412 0 curr_act_ended,
14413 0 prev_act_ended
14414 FROM bim_i_obj_mets_mv a,
14415      fii_time_rpt_struct_v cal,
14416      eni_denorm_hierarchies b,
14417      mtl_default_category_sets mdcs,';
14418 IF l_prog_view='Y' then
14419   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
14420 ELSE
14421 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
14422 END IF;
14423 l_sqltext := l_sqltext||' ,(select e.parent_id parent_id ,e.value value
14424       from eni_item_vbh_nodes_v e
14425       where
14426       e.top_node_flag=''Y''
14427       AND e.child_id = e.parent_id
14428       ) p
14429 WHERE
14430      a.time_id = cal.time_id
14431 AND  a.period_type_id = cal.period_type_id
14432 AND a.immediate_parent_id is null
14433 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14434 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
14435 AND cal.calendar_id=-1
14436 AND a.object_country = :l_country
14437 AND  a.category_id = b.child_id
14438 AND b.object_type = ''CATEGORY_SET''
14439 AND b.object_id = mdcs.category_set_id
14440 AND mdcs.functional_area_id = 11
14441 AND b.dbi_flag = ''Y''
14442 AND ac.resource_id = :l_resource_id
14443 AND p.parent_id = b.parent_id ';
14444 IF l_prog_view='N' then
14445 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
14446 AND s.object_type = ac.object_type
14447 ';
14448 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id
14449 ';
14450 END IF;
14451 IF l_cat_id is not null
14452 then
14453 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
14454 end if;
14455 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
14456  UNION ALL
14457 SELECT
14458 p.value name,
14459 p.parent_id id,
14460 p.parent_id leaf_node_id,
14461 0 curr_prior_active,
14462 0 prev_prior_active,
14463 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
14464 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
14465 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
14466 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
14467 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
14468 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
14469 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
14470 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
14471 FROM
14472      bim_i_obj_mets_mv a,
14473      fii_time_rpt_struct_v cal,
14474      eni_denorm_hierarchies b,
14475      mtl_default_category_sets mdcs,';
14476 IF l_prog_view='Y' then
14477   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
14478 ELSE
14479 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
14480 END IF;
14481 l_sqltext := l_sqltext||' ,(select e.parent_id parent_id ,e.value value
14482       from eni_item_vbh_nodes_v e
14483       where
14484       e.top_node_flag=''Y''
14485       AND e.child_id = e.parent_id
14489 AND  a.period_type_id = cal.period_type_id
14486       ) p
14487 WHERE
14488      a.time_id = cal.time_id
14490 AND a.immediate_parent_id is null
14491 AND ac.resource_id = :l_resource_id
14492 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
14493 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
14494 AND cal.calendar_id=-1
14495 AND a.object_country = :l_country
14496 AND  a.category_id = b.child_id
14497     AND b.object_type = ''CATEGORY_SET''
14498     AND b.object_id = mdcs.category_set_id
14499     AND mdcs.functional_area_id = 11
14500     AND b.dbi_flag = ''Y''
14501     AND p.parent_id = b.parent_id ';
14502 IF l_prog_view='N' then
14503 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
14504 AND s.object_id = ac.object_id
14505 AND s.object_type=ac.object_type ';
14506 ELSE l_sqltext := l_sqltext ||'
14507 AND a.source_code_id = ac.source_code_id
14508 ';
14509 END IF;
14510 IF l_cat_id is not null
14511 then
14512 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
14513 end if;
14514 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
14515 ) group by name,id,leaf_node_id )';
14516 ELSE -- product category is not null
14517 l_sqltext :=
14518 'SELECT
14519 VIEWBY,
14520 VIEWBYID,
14521 BIM_ATTRIBUTE2,
14522 BIM_ATTRIBUTE3,
14523 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
14524 BIM_ATTRIBUTE5,
14525 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
14526 BIM_ATTRIBUTE7,
14527 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
14528 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
14529 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
14530 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
14531 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
14532 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
14533 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
14534 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
14535 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
14536 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
14537 decode(leaf_node_id,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_CAMP_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
14538 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_CAMP_START_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL2,
14539 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_CAMP_START_END&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL3,
14540 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_CAMP_START_ACT&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL4
14541 FROM
14542 (
14543 SELECT
14544 name VIEWBY,
14545 id VIEWBYID,
14546 leaf_node_id,
14547 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
14548 sum(curr_started) BIM_ATTRIBUTE3,
14549 SUM(prev_started) BIM_ATTRIBUTE4,
14550 sum(curr_ended)  BIM_ATTRIBUTE5,
14551 SUm(prev_ended) BIM_ATTRIBUTE6,
14552 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
14553 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
14554 FROM
14555 (
14556 SELECT
14557 p.value name,
14558 b.parent_id id,
14559 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
14560 sum(camp_started-camp_ended) curr_prior_active,
14561 0 prev_prior_active,
14562 0 curr_active,
14563 0 prev_active,
14564 0 curr_started,
14565 0 prev_started,
14566 0 curr_ended,
14567 0 prev_ended,
14568 0 curr_act_ended,
14569 0 prev_act_ended
14570 FROM bim_i_obj_mets_mv a,
14571      fii_time_rpt_struct_v cal,
14572      eni_denorm_hierarchies b,
14573      mtl_default_category_sets mdcs,';
14574 IF l_prog_view='Y' then
14575   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
14576 ELSE
14577 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
14578 END IF;
14579 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value,e.leaf_node_flag leaf_node_flag
14580       from eni_item_vbh_nodes_v e
14581       where e.parent_id =:l_cat_id
14582       AND e.id = e.child_id
14583       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
14584       ) p
14585 WHERE
14586      a.time_id = cal.time_id
14587 AND  a.period_type_id = cal.period_type_id
14588 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14589 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
14590 AND cal.calendar_id=-1
14591 AND a.immediate_parent_id is null
14592 AND a.object_country = :l_country
14593 AND a.category_id = b.child_id
14594 AND b.object_type = ''CATEGORY_SET''
14595 AND b.object_id = mdcs.category_set_id
14596 AND mdcs.functional_area_id = 11
14597 AND b.parent_id = p.id
14598 AND ac.resource_id = :l_resource_id
14599 AND b.dbi_flag = ''Y''';
14600 IF l_prog_view='N' then
14601 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
14605 ELSE l_sqltext := l_sqltext ||'
14602 AND s.object_id = ac.object_id
14603 AND s.object_type=ac.object_type
14604 ';
14606 AND a.source_code_id = ac.source_code_id
14607 ';
14608 END IF;
14609 l_sqltext := l_sqltext ||' group by p.value,b.parent_id,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)';
14610 l_sqltext :=l_sqltext ||
14611 ' UNION ALL
14612 SELECT
14613 p.value name,
14614 a.category_id id,
14615 -1 leaf_node_id,
14616 sum(camp_started-camp_ended) curr_prior_active,
14617 0 prev_prior_active,
14618 0 curr_active,
14619 0 prev_active,
14620 0 curr_started,
14621 0 prev_started,
14622 0 curr_ended,
14623 0 prev_ended,
14624 0 curr_act_ended,
14625 0 prev_act_ended
14626 FROM bim_i_obj_mets_mv a,
14627      fii_time_rpt_struct_v cal,';
14628 IF l_prog_view='Y' then
14629   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
14630 ELSE
14631 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
14632 END IF;
14633 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value
14634       from eni_item_vbh_nodes_v e
14635       where e.parent_id =  :l_cat_id
14636       AND e.parent_id = e.child_id
14637       AND leaf_node_flag <> ''Y''
14638       ) p
14639 WHERE
14640      a.time_id = cal.time_id
14641 AND  a.period_type_id = cal.period_type_id
14642 AND a.immediate_parent_id is null
14643 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14644 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
14645 AND cal.calendar_id=-1
14646 AND a.object_country = :l_country
14647 AND ac.resource_id = :l_resource_id
14648 AND a.category_id = p.id';
14649 IF l_prog_view='N' then
14650 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
14651 AND s.object_id = ac.object_id
14652 AND s.object_type=ac.object_type
14653 ';
14654 ELSE l_sqltext := l_sqltext ||'
14655 AND a.source_code_id = ac.source_code_id
14656 ';
14657 END IF;
14658 /*IF l_cat_id is not null
14659 then
14660 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
14661 end if;*/
14662 l_sqltext := l_sqltext ||' group by p.value,a.category_id
14663 UNION ALL
14664 SELECT
14665 p.value name,
14666 b.parent_id id,
14667 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
14668 0 curr_prior_active,
14669 sum(camp_started-camp_ended) prev_prior_active,
14670 0 curr_active,
14671 0 prev_active,
14672 0 curr_started,
14673 0 prev_started,
14674 0 curr_ended,
14675 0 prev_ended,
14676 0 curr_act_ended,
14677 0 prev_act_ended
14678 FROM bim_i_obj_mets_mv a,
14679      fii_time_rpt_struct_v cal,
14680      eni_denorm_hierarchies b,
14681      mtl_default_category_sets mdcs,';
14682 IF l_prog_view='Y' then
14683   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
14684 ELSE
14685 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
14686 END IF;
14687 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value,e.leaf_node_flag leaf_node_flag
14688       from eni_item_vbh_nodes_v e
14689       where e.parent_id =:l_cat_id
14690       AND e.id = e.child_id
14691       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'') ) p
14692 WHERE a.time_id = cal.time_id
14693 AND  a.period_type_id = cal.period_type_id
14694 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14695 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
14696 AND cal.calendar_id=-1
14697 AND a.immediate_parent_id is null
14698 AND a.object_country = :l_country
14699 AND a.category_id = b.child_id
14700 AND b.object_type = ''CATEGORY_SET''
14701 AND b.object_id = mdcs.category_set_id
14702 AND mdcs.functional_area_id = 11
14703 AND b.dbi_flag =''Y''
14704 AND ac.resource_id = :l_resource_id
14705 AND b.parent_id = p.id ';
14706 IF l_prog_view='N' then
14707 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
14708 AND s.object_type=ac.object_type
14709 ';
14710 ELSE l_sqltext := l_sqltext ||'
14711 AND a.source_code_id = ac.source_code_id
14712 ';
14713 END IF;
14714 /*IF l_cat_id is not null
14715 then
14716 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
14717 end if;*/
14718 l_sqltext := l_sqltext ||' group by p.value,b.parent_id,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)
14719 UNION ALL
14720 SELECT
14721 p.value name,
14722 a.category_id id,
14723 -1 leaf_node_id,
14724 0 curr_prior_active,
14725 sum(camp_started-camp_ended) prev_prior_active,
14726 0 curr_active,
14727 0 prev_active,
14728 0 curr_started,
14729 0 prev_started,
14730 0 curr_ended,
14731 0 prev_ended,
14732 0 curr_act_ended,
14733 0 prev_act_ended
14734 FROM bim_i_obj_mets_mv a,
14735      fii_time_rpt_struct_v cal,';
14736 IF l_prog_view='Y' then
14737   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
14738 ELSE
14739 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
14740 END IF;
14741 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value
14742       from eni_item_vbh_nodes_v e
14743       where e.parent_id =  :l_cat_id
14744       AND e.parent_id = e.child_id
14745       AND leaf_node_flag <> ''Y''
14746       ) p
14747 WHERE
14748      a.time_id = cal.time_id
14752 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
14749 AND  a.period_type_id = cal.period_type_id
14750 AND a.immediate_parent_id is null
14751 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14753 AND cal.calendar_id=-1
14754 AND a.object_country = :l_country
14755 AND ac.resource_id = :l_resource_id
14756 AND a.category_id = p.id
14757 ';
14758 IF l_prog_view='N' then
14759 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
14760 AND s.object_type=ac.object_type
14761 ';
14762 ELSE l_sqltext := l_sqltext ||'
14763 AND a.source_code_id = ac.source_code_id
14764 ';
14765 END IF;
14766 /*IF l_cat_id is not null
14767 then
14768 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
14769 end if;*/
14770 l_sqltext := l_sqltext ||' group by p.value,a.category_id
14771  UNION ALL
14772 SELECT
14773 p.value name,
14774 b.parent_id id,
14775 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
14776 0 curr_prior_active,
14777 0 prev_prior_active,
14778 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
14779 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
14780 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
14781 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
14782 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
14783 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
14784 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
14785 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
14786 FROM bim_i_obj_mets_mv a,
14787      fii_time_rpt_struct_v cal,
14788      eni_denorm_hierarchies b,
14789      mtl_default_category_sets mdcs,';
14790 IF l_prog_view='Y' then
14791   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
14792 ELSE
14793 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
14794 END IF;
14795 l_sqltext :=l_sqltext||' ,(select e.id id ,e.value value,e.leaf_node_flag leaf_node_flag
14796       from eni_item_vbh_nodes_v e
14797       where e.parent_id =:l_cat_id
14798       AND e.id = e.child_id
14799       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')) p
14800 WHERE
14801      a.time_id = cal.time_id
14802 AND  a.period_type_id = cal.period_type_id
14803 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
14804 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
14805 AND cal.calendar_id=-1
14806 AND a.object_country = :l_country
14807 AND a.immediate_parent_id is null
14808 AND a.category_id = b.child_id
14809 AND b.object_type = ''CATEGORY_SET''
14810 AND b.object_id = mdcs.category_set_id
14811 AND mdcs.functional_area_id = 11
14812 AND b.dbi_flag = ''Y''
14813 AND ac.resource_id = :l_resource_id
14814 AND b.parent_id = p.id ';
14815 IF l_prog_view='N' then
14816 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
14817 AND s.object_type=ac.object_type
14818 ';
14819 ELSE l_sqltext := l_sqltext ||'
14820 AND a.source_code_id = ac.source_code_id
14821 ';
14822 END IF;
14823 /*IF l_cat_id is not null
14824 then
14825 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
14826 end if;*/
14827 l_sqltext := l_sqltext ||' group by p.value,b.parent_id,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)
14828 UNION ALL
14829 SELECT
14830 p.value name,
14831 a.category_id id,
14832 -1 leaf_node_id,
14833 0 curr_prior_active,
14834 0 prev_prior_active,
14835 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
14836 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
14837 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
14838 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
14839 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
14840 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
14841 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
14842 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
14843 FROM bim_i_obj_mets_mv a,
14844      fii_time_rpt_struct_v cal,';
14845 IF l_prog_view='Y' then
14846   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
14847 ELSE
14848 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
14849 END IF;
14850 l_sqltext := l_sqltext ||' ,(select e.id id ,e.value value
14851       from eni_item_vbh_nodes_v e
14852       where e.parent_id =  :l_cat_id
14853       AND e.parent_id = e.child_id
14854       AND leaf_node_flag <> ''Y''
14855       ) p
14856 WHERE
14857      a.time_id = cal.time_id
14858 AND  a.period_type_id = cal.period_type_id
14862 AND a.immediate_parent_id is null
14859 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
14860 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
14861 AND cal.calendar_id=-1
14863 AND a.object_country = :l_country
14864 AND ac.resource_id = :l_resource_id
14865 AND a.category_id = p.id';
14866 IF l_prog_view='N' then
14867 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
14868 AND s.object_type=ac.object_type
14869 ';
14870 ELSE l_sqltext := l_sqltext ||'
14871 AND a.source_code_id = ac.source_code_id
14872 ';
14873 END IF;
14874  l_sqltext:=l_sqltext||' group by p.value,a.category_id
14875  ) group by name,id,leaf_node_id )';
14876 END IF; -- end product category numm loop
14877 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
14878 l_sqltext :=
14879 '
14880 SELECT
14881 VIEWBY,
14882 VIEWBYID,
14883 BIM_ATTRIBUTE2,
14884 BIM_ATTRIBUTE3,
14885 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
14886 BIM_ATTRIBUTE5,
14887 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
14888 BIM_ATTRIBUTE7,
14889 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
14890 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
14891 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
14892 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
14893 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
14894 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
14895 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
14896 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
14897 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
14898 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
14899 null BIM_URL1,
14900 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_CAMP_START_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=campaign&BIM_PARAMETER5=VIEWBY'') BIM_URL2,
14901 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_CAMP_START_END&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=campaign&BIM_PARAMETER5=VIEWBY'') BIM_URL3,
14902 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_CAMP_START_ACT&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=campaign&BIM_PARAMETER5=VIEWBY'') BIM_URL4
14903 FROM
14904 (
14905 SELECT
14906 name VIEWBY,
14907 id VIEWBYID,
14908 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
14909 sum(curr_started) BIM_ATTRIBUTE3,
14910 SUM(prev_started) BIM_ATTRIBUTE4,
14911 sum(curr_ended)  BIM_ATTRIBUTE5,
14912 SUm(prev_ended) BIM_ATTRIBUTE6,
14913 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
14914 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
14915 FROM
14916 (
14917 SELECT
14918 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
14919 a.object_region id,
14920 sum(camp_started-camp_ended) curr_prior_active,
14921 0 prev_prior_active,
14922 0 curr_active,
14923 0 prev_active,
14924 0 curr_started,
14925 0 prev_started,
14926 0 curr_ended,
14927 0 prev_ended,
14928 0 curr_act_ended,
14929 0 prev_act_ended
14930 FROM bim_obj_regn_mv a,
14931 fii_time_rpt_struct_v cal,';
14932 IF l_prog_view='Y' then
14933   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_areas_v d ';
14934 ELSE
14935 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_areas_v d, bim_i_source_codes s ';
14936 END IF;
14937  IF l_cat_id is not null then
14938   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
14939   end if;
14940   l_sqltext := l_sqltext ||
14941 ' WHERE a.time_id = cal.time_id
14942 AND a.period_type_id = cal.period_type_id
14943 AND ac.resource_id = :l_resource_id
14944 AND a.immediate_parent_id is null';
14945 IF l_prog_view='N' then
14946 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
14947 AND s.object_type=ac.object_type
14948 ';
14949 ELSE l_sqltext := l_sqltext ||'
14950 AND a.source_code_id = ac.source_code_id
14951 ';
14952 END IF;
14953 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
14954 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
14955 AND cal.calendar_id=-1
14956 AND  a.object_region =d.id(+)
14957 AND a.object_country = :l_country ';
14958 IF l_cat_id is null then
14959 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
14960 else
14961   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
14962 			   AND edh.object_type = ''CATEGORY_SET''
14963 			   AND edh.object_id = mdc.category_set_id
14964 			   AND mdc.functional_area_id = 11
14965 			   AND edh.dbi_flag = ''Y''
14966 			   AND edh.parent_id = :l_cat_id ';
14967   end if;
14968 
14969  l_sqltext := l_sqltext ||'group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
14970  l_sqltext :=l_sqltext ||
14971 ' UNION ALL
14972 SELECT
14973 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
14977 0 curr_active,
14974 a.object_region id,
14975 0 curr_prior_active,
14976 sum(camp_started-camp_ended) prev_prior_active,
14978 0 prev_active,
14979 0 curr_started,
14980 0 prev_started,
14981 0 curr_ended,
14982 0 prev_ended,
14983 0 curr_act_ended,
14984 0 prev_act_ended
14985 FROM bim_obj_regn_mv a,
14986 fii_time_rpt_struct_v cal,';
14987 IF l_prog_view='Y' then
14988   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_areas_v d ';
14989 ELSE
14990 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_areas_v d , bim_i_source_codes s';
14991 END IF;
14992  IF l_cat_id is not null then
14993   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
14994   end if;
14995   l_sqltext := l_sqltext ||
14996 ' WHERE a.time_id = cal.time_id
14997 AND a.period_type_id = cal.period_type_id
14998 AND ac.resource_id = :l_resource_id
14999 AND a.immediate_parent_id is null';
15000 IF l_prog_view='N' then
15001 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
15002 AND s.object_type=ac.object_type
15003 ';
15004 ELSE l_sqltext := l_sqltext ||'
15005 AND a.source_code_id = ac.source_code_id
15006 ';
15007 END IF;
15008 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15009 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
15010 AND cal.calendar_id=-1
15011 AND  a.object_region =d.id(+)
15012 AND a.object_country = :l_country ';
15013 IF l_cat_id is null then
15014 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
15015 else
15016   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
15017 			     AND edh.object_type = ''CATEGORY_SET''
15018 			     AND edh.object_id = mdc.category_set_id
15019 			     AND mdc.functional_area_id = 11
15020 			     AND edh.dbi_flag = ''Y''
15021 			     AND edh.parent_id = :l_cat_id ';
15022   end if;
15023  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
15024  l_sqltext := l_sqltext ||
15025 ' UNION ALL
15026 SELECT
15027 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
15028 a.object_region id,
15029 0 curr_prior_active,
15030 0 prev_prior_active,
15031 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
15032 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
15033 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
15034 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
15035 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
15036 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
15037 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
15038 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
15039 FROM bim_obj_regn_mv a,
15040 fii_time_rpt_struct_v cal,';
15041 IF l_prog_view='Y' then
15042   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_areas_v d ';
15043 ELSE
15044 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_areas_v d, bim_i_source_codes s ';
15045 END IF;
15046  IF l_cat_id is not null then
15047   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
15048   end if;
15049   l_sqltext := l_sqltext ||
15050 ' WHERE a.time_id = cal.time_id
15051 AND a.period_type_id = cal.period_type_id
15052 AND ac.resource_id = :l_resource_id
15053 AND a.immediate_parent_id is null';
15054 IF l_prog_view='N' then
15055 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
15056 AND s.object_type=ac.object_type
15057 ';
15058 ELSE l_sqltext := l_sqltext ||'
15059 AND a.source_code_id = ac.source_code_id
15060 ';
15061 END IF;
15062 l_sqltext := l_sqltext||' AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
15063 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
15064 AND cal.calendar_id=-1
15065 AND  a.object_region =d.id(+)
15066 AND a.object_country = :l_country ';
15067 IF l_cat_id is null then
15068 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
15069 else
15070   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
15071 			     AND edh.object_type = ''CATEGORY_SET''
15072 			     AND edh.object_id = mdc.category_set_id
15073 			     AND mdc.functional_area_id = 11
15074 			     AND edh.dbi_flag = ''Y''
15075 			     AND edh.parent_id = :l_cat_id ';
15076   end if;
15077  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region ) group by name,id )';
15078 ELSE -- view by country
15079 l_sqltext :=
15080 '
15081 SELECT
15082 VIEWBY,
15083 VIEWBYID,
15084 BIM_ATTRIBUTE2,
15085 BIM_ATTRIBUTE3,
15086 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
15087 BIM_ATTRIBUTE5,
15088 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
15089 BIM_ATTRIBUTE7,
15093 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
15090 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
15091 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
15092 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
15094 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
15095 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
15096 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
15097 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
15098 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
15099 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
15100 null BIM_URL1,
15101 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_CAMP_START_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL2,
15102 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_CAMP_START_END&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL3,
15103 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_CAMP_START_ACT&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=campaign&BIM_PARAMETER5=All'') BIM_URL4
15104 FROM
15105 (
15106 SELECT
15107 name VIEWBY,
15108 id VIEWBYID,
15109 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
15110 sum(curr_started) BIM_ATTRIBUTE3,
15111 SUM(prev_started) BIM_ATTRIBUTE4,
15112 sum(curr_ended)  BIM_ATTRIBUTE5,
15113 SUm(prev_ended) BIM_ATTRIBUTE6,
15114 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
15115 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
15116 FROM
15117 (
15118 SELECT
15119 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
15120 a.object_country id,
15121 sum(camp_started-camp_ended) curr_prior_active,
15122 0 prev_prior_active,
15123 0 curr_active,
15124 0 prev_active,
15125 0 curr_started,
15126 0 prev_started,
15127 0 curr_ended,
15128 0 prev_ended,
15129 0 curr_act_ended,
15130 0 prev_act_ended
15131 FROM bim_i_obj_mets_mv a,
15132 fii_time_rpt_struct_v cal,';
15133 IF l_prog_view='Y' then
15134   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_countries_v d ';
15135 ELSE
15136 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_countries_v d, bim_i_source_codes s  ';
15137 END IF;
15138  IF l_cat_id is not null then
15139   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
15140   end if;
15141   l_sqltext := l_sqltext ||
15142 ' WHERE a.time_id = cal.time_id
15143 AND a.period_type_id = cal.period_type_id
15144 AND ac.resource_id = :l_resource_id
15145 AND a.immediate_parent_id is null';
15146 IF l_prog_view='N' then
15147 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
15148 AND s.object_type=ac.object_type
15149 ';
15150 ELSE l_sqltext := l_sqltext ||'
15151 AND a.source_code_id = ac.source_code_id
15152 ';
15153 END IF;
15154 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15155 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
15156 AND cal.calendar_id=-1
15157 AND  a.object_country =d.country_code (+) ';
15158 IF l_cat_id is null then
15159 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
15160 else
15161   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
15162 			   AND edh.object_type = ''CATEGORY_SET''
15163 			   AND edh.object_id = mdc.category_set_id
15164 			   AND mdc.functional_area_id = 11
15165 			   AND edh.dbi_flag = ''Y''
15166 			   AND edh.parent_id = :l_cat_id ';
15167   end if;
15168  IF l_country = 'N' THEN
15169      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
15170  ELSE
15171   l_sqltext := l_sqltext ||
15172 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
15173  END IF;
15174 /*l_sqltext := l_sqltext ||
15175 --' AND a.object_country = :l_country
15176 'group by object_country*/
15177 l_sqltext :=l_sqltext ||
15178 ' UNION ALL
15179 SELECT
15180 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
15181 a.object_country id,
15182 0 curr_prior_active,
15183 sum(camp_started-camp_ended) prev_prior_active,
15184 0 curr_active,
15185 0 prev_active,
15186 0 curr_started,
15187 0 prev_started,
15188 0 curr_ended,
15189 0 prev_ended,
15190 0 curr_act_ended,
15191 0 prev_act_ended
15192 FROM bim_i_obj_mets_mv a,
15193 fii_time_rpt_struct_v cal,';
15194 IF l_prog_view='Y' then
15195   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_countries_v d ';
15196 ELSE
15197 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_countries_v d , bim_i_source_codes s ';
15198 END IF;
15199  IF l_cat_id is not null then
15203 ' WHERE a.time_id = cal.time_id
15200   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
15201   end if;
15202   l_sqltext := l_sqltext ||
15204 AND a.period_type_id = cal.period_type_id
15205 AND ac.resource_id = :l_resource_id
15206 AND a.immediate_parent_id is null';
15207 IF l_prog_view='N' then
15208 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
15209 AND s.object_type=ac.object_type
15210 ';
15211 ELSE l_sqltext := l_sqltext ||'
15212 AND a.source_code_id = ac.source_code_id
15213 ';
15214 END IF;
15215 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15216 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
15217 AND cal.calendar_id=-1
15218 AND  a.object_country =d.country_code (+)';
15219 IF l_cat_id is null then
15220 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
15221 else
15222   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
15223 			     AND edh.object_type = ''CATEGORY_SET''
15224 			     AND edh.object_id = mdc.category_set_id
15225 			     AND mdc.functional_area_id = 11
15226 			     AND edh.dbi_flag = ''Y''
15227 			     AND edh.parent_id = :l_cat_id ';
15228   end if;
15229  IF l_country = 'N' THEN
15230      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
15231  ELSE
15232   l_sqltext := l_sqltext ||
15233 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
15234  END IF;
15235 l_sqltext := l_sqltext ||
15236 ' UNION ALL
15237 SELECT
15238 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
15239 a.object_country id,
15240 0 curr_prior_active,
15241 0 prev_prior_active,
15242 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_started-camp_ended) else 0 end) curr_active,
15243 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_started-camp_ended) else 0 end) prev_active,
15244 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_started else 0 end) curr_started,
15245 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_started else 0 end) prev_started,
15246 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_ended else 0 end) curr_ended,
15247 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_ended else 0 end) prev_ended,
15248 SUM(case when &BIS_CURRENT_ASOF_DATE > &BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_ended else 0 end) curr_act_ended,
15249 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_ended else 0 end) prev_act_ended
15250 FROM bim_i_obj_mets_mv a,
15251 fii_time_rpt_struct_v cal,';
15252 IF l_prog_view='Y' then
15253   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_countries_v d ';
15254 ELSE
15255 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_countries_v d ,bim_i_source_codes s ';
15256 END IF;
15257  IF l_cat_id is not null then
15258   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
15259   end if;
15260   l_sqltext := l_sqltext ||
15261 ' WHERE a.time_id = cal.time_id
15262 AND a.period_type_id = cal.period_type_id
15263 AND ac.resource_id = :l_resource_id
15264 AND a.immediate_parent_id is null
15265 ';
15266 IF l_prog_view='N' then
15267 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP'' AND s.object_id = ac.object_id
15268 AND s.object_type=ac.object_type
15269 ';
15270 ELSE l_sqltext := l_sqltext ||'
15271 AND a.source_code_id = ac.source_code_id
15272 ';
15273 END IF;
15274 l_sqltext := l_sqltext||' AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
15275 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
15276 AND cal.calendar_id=-1
15277 AND  a.object_country =d.country_code (+)';
15278 IF l_cat_id is null then
15279 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
15280 else
15281   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
15282 			     AND edh.object_type = ''CATEGORY_SET''
15283 			     AND edh.object_id = mdc.category_set_id
15284 			     AND mdc.functional_area_id = 11
15285 			     AND edh.dbi_flag = ''Y''
15286 			     AND edh.parent_id = :l_cat_id ';
15287   end if;
15288 IF l_country = 'N' THEN
15289      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
15290  ELSE
15291   l_sqltext := l_sqltext ||
15292 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
15293 END IF; -- product category All loop
15294  END IF; --end view by product category
15295 
15296 END IF; -- end if admin_flag = Y loop
15297 
15298 
15299 
15300 
15301 
15302   x_custom_sql := l_sqltext;
15303   l_custom_rec.attribute_name := ':l_record_type';
15304   l_custom_rec.attribute_value := l_record_type_id;
15305   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
15306   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
15307   x_custom_output.EXTEND;
15308   x_custom_output(1) := l_custom_rec;
15309 
15310   l_custom_rec.attribute_name := ':l_resource_id';
15311   l_custom_rec.attribute_value := GET_RESOURCE_ID;
15315   x_custom_output(2) := l_custom_rec;
15312   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
15313   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
15314   x_custom_output.EXTEND;
15316 
15317   l_custom_rec.attribute_name := ':l_admin_flag';
15318   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
15319   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
15320   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
15321   x_custom_output.EXTEND;
15322   x_custom_output(3) := l_custom_rec;
15323 
15324   l_custom_rec.attribute_name := ':l_country';
15325   l_custom_rec.attribute_value := l_country;
15326   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
15327   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
15328   x_custom_output.EXTEND;
15329   x_custom_output(4) := l_custom_rec;
15330 
15331   l_custom_rec.attribute_name := ':l_cat_id';
15332   l_custom_rec.attribute_value := l_cat_id;
15333   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
15334   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
15335   x_custom_output.EXTEND;
15336   x_custom_output(5) := l_custom_rec;
15337 
15338 
15339 write_debug('GET_CAMP_START_SQL','QUERY','_',l_sqltext);
15340 
15341 EXCEPTION
15342 WHEN others THEN
15343 l_sql_errm := SQLERRM;
15344 write_debug('GET_CAMP_START_SQL','ERROR',l_sql_errm,l_sqltext);
15345 
15346 END GET_CAMP_START_SQL;
15347 
15348 
15349 PROCEDURE GET_EVEH_START_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
15350                           x_custom_sql  OUT NOCOPY VARCHAR2,
15351                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
15352  IS
15353 l_sqltext varchar2(15000);
15354 iFlag number;
15355 l_period_type_hc number;
15356 l_as_of_date  DATE;
15357 l_period_type	varchar2(2000);
15358 l_record_type_id NUMBER;
15359 l_comp_type    varchar2(2000);
15360 l_country      varchar2(4000);
15361 l_view_by      varchar2(4000);
15362 l_sql_errm      varchar2(4000);
15363 l_previous_report_start_date DATE;
15364 l_current_report_start_date DATE;
15365 l_previous_as_of_date DATE;
15366 l_period_type_id NUMBER;
15367 l_user_id NUMBER;
15368 l_resource_id NUMBER;
15369 l_time_id_column  VARCHAR2(1000);
15370 l_admin_status VARCHAR2(20);
15371 l_admin_flag VARCHAR2(1);
15372 l_admin_count Number;
15373 l_rsid NUMBER;
15374 l_curr_aod_str varchar2(80);
15375 l_country_clause varchar2(4000);
15376 l_access_clause varchar2(4000);
15377 l_access_table varchar2(4000);
15378 --l_cat_id NUMBER;
15379 l_cat_id VARCHAR2(50);
15380 l_col_id NUMBER;
15381 l_area VARCHAR2(300);
15382 l_report_name VARCHAR2(300);
15383 l_campaign_id VARCHAR2(50);
15384 l_custom_rec BIS_QUERY_ATTRIBUTES;
15385 l_dass                 varchar2(100);  -- variable to store value for  directly assigned lookup value
15386 l_una                   varchar2(100);   -- variable to store value for  Unassigned lookup value
15387  l_curr VARCHAR2(50);
15388 l_curr_suffix VARCHAR2(50);
15389 l_media VARCHAR2(300);
15390 BEGIN
15391 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
15392 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
15393 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
15394    			     			      l_as_of_date,
15395 			    			      l_period_type,
15396 				                      l_record_type_id,
15397 				                      l_comp_type,
15398 				                      l_country,
15399 						      l_view_by,
15400 						      l_cat_id,
15401 						      l_campaign_id,
15402 						      l_curr,
15403 						      l_col_id,
15404 						      l_area,
15405 						      l_media,
15406 						      l_report_name
15407 				                      );
15408 l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
15409 
15410    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
15411   /* IF l_country IS NULL THEN
15412       l_country := 'N';
15413    END IF;*/
15414    l_admin_status := GET_ADMIN_STATUS;
15415 IF l_admin_status = 'Y' THEN
15416 if (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
15417 IF l_cat_id is null THEN
15418 l_sqltext :=
15419 '
15420 SELECT
15421 VIEWBY,
15422 VIEWBYID,
15423 BIM_ATTRIBUTE2,
15424 BIM_ATTRIBUTE3,
15425 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
15426 BIM_ATTRIBUTE5,
15427 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
15428 BIM_ATTRIBUTE7,
15429 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
15430 BIM_ATTRIBUTE3 BIM_ATTRIBUTE9,
15431 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
15432 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
15433 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
15434 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
15435 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
15436 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
15437 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
15438 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL8,
15439 decode(leaf_node_id,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_EVEH_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
15443 FROM
15440 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_EVEH_START_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL2,
15441 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_EVEH_END_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL3,
15442 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_EVEH_ACT_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL4
15444 (
15445 SELECT
15446 name VIEWBY,
15447 id VIEWBYID,
15448 leaf_node_id leaf_node_id,
15449 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
15450 sum(curr_started) BIM_ATTRIBUTE3,
15451 SUM(prev_started) BIM_ATTRIBUTE4,
15452 sum(curr_ended)  BIM_ATTRIBUTE5,
15453 SUm(prev_ended) BIM_ATTRIBUTE6,
15454 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
15455 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
15456 FROM
15457 (
15458 SELECT
15459 p.value name,
15460 p.parent_id id,
15461 p.parent_id leaf_node_id,
15462 sum(even_started-even_ended) curr_prior_active,
15463 0 prev_prior_active,
15464 0 curr_active,
15465 0 prev_active,
15466 0 curr_started,
15467 0 prev_started,
15468 0 curr_ended,
15469 0 prev_ended,
15470 0 curr_act_ended,
15471 0 prev_act_ended
15472 FROM bim_mkt_kpi_cnt_mv a,
15473      fii_time_rpt_struct_v cal,
15474      eni_denorm_hierarchies b,
15475      mtl_default_category_sets mdcs,
15476     (select e.parent_id parent_id ,e.value value
15477       from eni_item_vbh_nodes_v e
15478       where
15479       e.top_node_flag=''Y''
15480       AND e.child_id = e.parent_id
15481       ) p
15482 WHERE
15483      a.time_id = cal.time_id
15484 AND  a.period_type_id = cal.period_type_id
15485 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15486 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
15487 AND cal.calendar_id=-1
15488 AND a.object_country = :l_country
15489 AND  a.category_id = b.child_id
15490 AND b.object_type = ''CATEGORY_SET''
15491 AND b.object_id = mdcs.category_set_id
15492 AND mdcs.functional_area_id = 11
15493 AND b.dbi_flag = ''Y''
15494 AND p.parent_id = b.parent_id';
15495 IF l_cat_id is not null
15496 then
15497 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
15498 end if;
15499 l_sqltext := l_sqltext ||' group by p.value,p.parent_id';
15500 l_sqltext :=l_sqltext ||
15501 ' UNION ALL
15502 SELECT
15503 p.value name,
15504 p.parent_id id,
15505 p.parent_id leaf_node_id,
15506 0 curr_prior_active,
15507 sum(even_started-even_ended) prev_prior_active,
15508 0 curr_active,
15509 0 prev_active,
15510 0 curr_started,
15511 0 prev_started,
15512 0 curr_ended,
15513 0 prev_ended,
15514 0 curr_act_ended,
15515 0 prev_act_ended
15516 FROM bim_mkt_kpi_cnt_mv a,
15517      fii_time_rpt_struct_v cal,
15518      eni_denorm_hierarchies b,
15519      mtl_default_category_sets mdcs,
15520     (select e.parent_id parent_id ,e.value value
15521       from eni_item_vbh_nodes_v e
15522       where
15523       e.top_node_flag=''Y''
15524       AND e.child_id = e.parent_id
15525       ) p
15526 WHERE
15527      a.time_id = cal.time_id
15528 AND  a.period_type_id = cal.period_type_id
15529 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15530 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
15531 AND cal.calendar_id=-1
15532 AND a.object_country = :l_country
15533 AND  a.category_id = b.child_id
15534 AND b.object_type = ''CATEGORY_SET''
15535 AND b.object_id = mdcs.category_set_id
15536 AND mdcs.functional_area_id = 11
15537 AND b.dbi_flag = ''Y''
15538 AND p.parent_id = b.parent_id ';
15539 IF l_cat_id is not null
15540 then
15541 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
15542 end if;
15543 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
15544  UNION ALL
15545 SELECT
15546 p.value name,
15547 p.parent_id id,
15548 p.parent_id leaf_node_id,
15549 0 curr_prior_active,
15550 0 prev_prior_active,
15551 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
15552 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
15553 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
15554 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
15555 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
15556 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
15557 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
15558 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
15559 
15560 FROM bim_mkt_kpi_cnt_mv a,
15561      fii_time_rpt_struct_v cal,
15562      eni_denorm_hierarchies b,
15563      mtl_default_category_sets mdcs,
15564      (select e.parent_id parent_id ,e.value value
15565       from eni_item_vbh_nodes_v e
15566       where
15567       e.top_node_flag=''Y''
15568       AND e.child_id = e.parent_id
15569       ) p
15570 WHERE
15571      a.time_id = cal.time_id
15572 AND  a.period_type_id = cal.period_type_id
15576 AND a.object_country = :l_country
15573 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
15574 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
15575 AND cal.calendar_id=-1
15577 AND  a.category_id = b.child_id
15578     AND b.object_type = ''CATEGORY_SET''
15579     AND b.object_id = mdcs.category_set_id
15580     AND mdcs.functional_area_id = 11
15581     AND b.dbi_flag = ''Y''
15582     AND p.parent_id = b.parent_id ';
15583 IF l_cat_id is not null
15584 then
15585 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
15586 end if;
15587 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
15588 ) group by name,id,leaf_node_id )';
15589 ELSE
15590 -- for product category not equal to all
15591 -- current bookmark2
15592 l_sqltext :=
15593 '
15594 SELECT
15595 VIEWBY,
15596 VIEWBYID,
15597 BIM_ATTRIBUTE2,
15598 BIM_ATTRIBUTE3,
15599 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
15600 BIM_ATTRIBUTE5,
15601 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
15602 BIM_ATTRIBUTE7,
15603 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
15604 BIM_ATTRIBUTE3 BIM_ATTRIBUTE9,
15605 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
15606 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
15607 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
15608 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
15609 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
15610 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
15611 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
15612 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL8,
15613 decode(leaf_node_id,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_EVEH_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
15614 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_EVEH_START_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL2,
15615 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_EVEH_END_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL3,
15616 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_EVEH_ACT_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL4
15617 FROM
15618 (
15619 SELECT
15620 name VIEWBY,
15621 id VIEWBYID,
15622 leaf_node_id leaf_node_id,
15623 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
15624 sum(curr_started) BIM_ATTRIBUTE3,
15625 SUM(prev_started) BIM_ATTRIBUTE4,
15626 sum(curr_ended)  BIM_ATTRIBUTE5,
15627 SUm(prev_ended) BIM_ATTRIBUTE6,
15628 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
15629 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
15630 FROM
15631 (
15632 SELECT
15633 p.value name,
15634 b.parent_id id,
15635 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
15636 sum(even_started-even_ended) curr_prior_active,
15637 0 prev_prior_active,
15638 0 curr_active,
15639 0 prev_active,
15640 0 curr_started,
15641 0 prev_started,
15642 0 curr_ended,
15643 0 prev_ended,
15644 0 curr_act_ended,
15645 0 prev_act_ended
15646 FROM bim_mkt_kpi_cnt_mv a,
15647      fii_time_rpt_struct_v cal,
15648      eni_denorm_hierarchies b,
15649      mtl_default_category_sets mdcs,
15650     (select e.id id ,e.value value,leaf_node_flag
15651       from eni_item_vbh_nodes_v e
15652       where e.parent_id =:l_cat_id
15653       AND e.id = e.child_id
15654       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
15655       ) p
15656 WHERE
15657      a.time_id = cal.time_id
15658 AND  a.period_type_id = cal.period_type_id
15659 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15660 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
15661 AND cal.calendar_id=-1
15662 AND a.object_country = :l_country
15663 AND a.category_id = b.child_id
15664 AND b.object_type = ''CATEGORY_SET''
15665 AND b.object_id = mdcs.category_set_id
15666 AND mdcs.functional_area_id = 11
15667 AND b.parent_id = p.id
15668 AND b.dbi_flag = ''Y''';
15669 l_sqltext := l_sqltext ||' group by p.value,b.parent_id,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)';
15670 l_sqltext :=l_sqltext ||
15671 ' UNION ALL
15672 SELECT
15673 p.value name,
15674 a.category_id id,
15675 -1 leaf_node_id,
15676 sum(even_started-even_ended) curr_prior_active,
15677 0 prev_prior_active,
15678 0 curr_active,
15679 0 prev_active,
15680 0 curr_started,
15681 0 prev_started,
15682 0 curr_ended,
15683 0 prev_ended,
15684 0 curr_act_ended,
15685 0 prev_act_ended
15686 FROM bim_mkt_kpi_cnt_mv a,
15687      fii_time_rpt_struct_v cal,
15688     (select e.id id ,e.value value
15689       from eni_item_vbh_nodes_v e
15690       where e.parent_id =  :l_cat_id
15691       AND e.parent_id = e.child_id
15692       AND leaf_node_flag <> ''Y''
15693       ) p
15694 WHERE
15695      a.time_id = cal.time_id
15696 AND  a.period_type_id = cal.period_type_id
15697 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15701 AND a.category_id = p.id';
15698 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
15699 AND cal.calendar_id=-1
15700 AND a.object_country = :l_country
15702 /*IF l_cat_id is not null
15703 then
15704 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
15705 end if;*/
15706 l_sqltext := l_sqltext ||' group by p.value,a.category_id
15707 UNION ALL
15708 SELECT
15709 p.value name,
15710 b.parent_id id,
15711 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
15712 0 curr_prior_active,
15713 sum(even_started-even_ended) prev_prior_active,
15714 0 curr_active,
15715 0 prev_active,
15716 0 curr_started,
15717 0 prev_started,
15718 0 curr_ended,
15719 0 prev_ended,
15720 0 curr_act_ended,
15721 0 prev_act_ended
15722 FROM bim_mkt_kpi_cnt_mv a,
15723      fii_time_rpt_struct_v cal,
15724      eni_denorm_hierarchies b,
15725      mtl_default_category_sets mdcs,
15726     (select e.id id ,e.value value,leaf_node_flag
15727       from eni_item_vbh_nodes_v e
15728       where e.parent_id =:l_cat_id
15729       AND e.id = e.child_id
15730       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'') ) p
15731 WHERE a.time_id = cal.time_id
15732 AND  a.period_type_id = cal.period_type_id
15733 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15734 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
15735 AND cal.calendar_id=-1
15736 AND a.object_country = :l_country
15737 AND a.category_id = b.child_id
15738 AND b.object_type = ''CATEGORY_SET''
15739 AND b.object_id = mdcs.category_set_id
15740 AND mdcs.functional_area_id = 11
15741 AND b.dbi_flag =''Y''
15742 AND b.parent_id = p.id ';
15743 /*IF l_cat_id is not null
15744 then
15745 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
15746 end if;*/
15747 l_sqltext := l_sqltext ||' group by p.value,b.parent_id,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)
15748 UNION ALL
15749 SELECT
15750 p.value name,
15751 a.category_id id,
15752 -1 leaf_node_id,
15753 0 curr_prior_active,
15754 sum(even_started-even_ended) prev_prior_active,
15755 0 curr_active,
15756 0 prev_active,
15757 0 curr_started,
15758 0 prev_started,
15759 0 curr_ended,
15760 0 prev_ended,
15761 0 curr_act_ended,
15762 0 prev_act_ended
15763 FROM bim_mkt_kpi_cnt_mv a,
15764      fii_time_rpt_struct_v cal,
15765     (select e.id id ,e.value value
15766       from eni_item_vbh_nodes_v e
15767       where e.parent_id =  :l_cat_id
15768       AND e.parent_id = e.child_id
15769       AND leaf_node_flag <> ''Y''
15770       ) p
15771 WHERE
15772      a.time_id = cal.time_id
15773 AND  a.period_type_id = cal.period_type_id
15774 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15775 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
15776 AND cal.calendar_id=-1
15777 AND a.object_country = :l_country
15778 AND a.category_id = p.id
15779 ';
15780 /*IF l_cat_id is not null
15781 then
15782 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
15783 end if;*/
15784 l_sqltext := l_sqltext ||' group by p.value,a.category_id
15785  UNION ALL
15786 SELECT
15787 p.value name,
15788 b.parent_id id,
15789 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
15790 0 curr_prior_active,
15791 0 prev_prior_active,
15792 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
15793 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
15794 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
15795 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
15796 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
15797 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
15798 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
15799 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
15800 FROM bim_mkt_kpi_cnt_mv a,
15801      fii_time_rpt_struct_v cal,
15802      eni_denorm_hierarchies b,
15803      mtl_default_category_sets mdcs,
15804      (select e.id id ,e.value value,leaf_node_flag
15805       from eni_item_vbh_nodes_v e
15806       where e.parent_id =:l_cat_id
15807       AND e.id = e.child_id
15808       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')) p
15809 WHERE
15810      a.time_id = cal.time_id
15811 AND  a.period_type_id = cal.period_type_id
15812 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
15813 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
15814 AND cal.calendar_id=-1
15815 AND a.object_country = :l_country
15816 AND a.category_id = b.child_id
15817 AND b.object_type = ''CATEGORY_SET''
15818 AND b.object_id = mdcs.category_set_id
15819 AND mdcs.functional_area_id = 11
15820 AND b.dbi_flag = ''Y''
15821 AND b.parent_id = p.id ';
15822 /*IF l_cat_id is not null
15823 then
15824 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
15825 end if;*/
15826 l_sqltext := l_sqltext ||' group by p.value,b.parent_id,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)
15827 UNION ALL
15828 SELECT
15829 p.value name,
15830 a.category_id id,
15831 -1 leaf_node_id,
15832 0 curr_prior_active,
15833 0 prev_prior_active,
15837 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
15834 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
15835 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
15836 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
15838 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
15839 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
15840 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
15841 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
15842 FROM bim_mkt_kpi_cnt_mv a,
15843      fii_time_rpt_struct_v cal,
15844      (select e.id id ,e.value value
15845       from eni_item_vbh_nodes_v e
15846       where e.parent_id =  :l_cat_id
15847       AND e.parent_id = e.child_id
15848       AND leaf_node_flag <> ''Y''
15849       ) p
15850 WHERE
15851      a.time_id = cal.time_id
15852 AND  a.period_type_id = cal.period_type_id
15853 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
15854 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
15855 AND cal.calendar_id=-1
15856 AND a.object_country = :l_country
15857 AND a.category_id = p.id
15858  group by p.value,a.category_id) group by name,id,leaf_node_id )';
15859 END IF; -- end product category numm loop
15860 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN --view by area
15861 l_sqltext :=
15862 '
15863 SELECT
15864 VIEWBY,
15865 VIEWBYID,
15866 BIM_ATTRIBUTE2,
15867 BIM_ATTRIBUTE3,
15868 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
15869 BIM_ATTRIBUTE5,
15870 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
15871 BIM_ATTRIBUTE7,
15872 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
15873 BIM_ATTRIBUTE3 BIM_ATTRIBUTE9,
15874 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
15875 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
15876 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
15877 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
15878 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
15879 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
15880 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
15881 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL8,
15882 null BIM_URL1,
15883 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_EVEH_START_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=event&BIM_PARAMETER5=VIEWBY'') BIM_URL2,
15884 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_EVEH_END_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=event&BIM_PARAMETER5=VIEWBY'') BIM_URL3,
15885 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_EVEH_ACT_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=event&BIM_PARAMETER5=VIEWBY'') BIM_URL4
15886 FROM
15887 (
15888 SELECT
15889 name VIEWBY,
15890 id VIEWBYID,
15891 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
15892 sum(curr_started) BIM_ATTRIBUTE3,
15893 SUM(prev_started) BIM_ATTRIBUTE4,
15894 sum(curr_ended)  BIM_ATTRIBUTE5,
15895 SUm(prev_ended) BIM_ATTRIBUTE6,
15896 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
15897 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
15898 FROM
15899 (
15900 SELECT
15901 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
15902 a.object_region id,
15903 sum(even_started-even_ended) curr_prior_active,
15904 0 prev_prior_active,
15905 0 curr_active,
15906 0 prev_active,
15907 0 curr_started,
15908 0 prev_started,
15909 0 curr_ended,
15910 0 prev_ended,
15911 0 curr_act_ended,
15912 0 prev_act_ended
15913 FROM bim_mkt_regn_mv a,
15914 fii_time_rpt_struct_v cal,
15915 bis_areas_v d ';
15916  IF l_cat_id is not null then
15917   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
15918   end if;
15919   l_sqltext := l_sqltext ||
15920 ' WHERE a.time_id = cal.time_id
15921 AND a.period_type_id = cal.period_type_id
15922 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15923 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
15924 AND cal.calendar_id=-1
15925 AND  a.object_region =d.id(+)
15926 AND a.object_country = :l_country ';
15927 IF l_cat_id is null then
15928 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
15929 else
15930   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
15931 			     AND edh.object_type = ''CATEGORY_SET''
15932 			     AND edh.object_id = mdc.category_set_id
15933 			     AND mdc.functional_area_id = 11
15934 			     AND edh.dbi_flag = ''Y''
15935 			     AND edh.parent_id = :l_cat_id  ';
15936   end if;
15937   l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
15938 
15942 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
15939 l_sqltext :=l_sqltext ||
15940 ' UNION ALL
15941 SELECT
15943 a.object_region id,
15944 0 curr_prior_active,
15945 sum(even_started-even_ended) prev_prior_active,
15946 0 curr_active,
15947 0 prev_active,
15948 0 curr_started,
15949 0 prev_started,
15950 0 curr_ended,
15951 0 prev_ended,
15952 0 curr_act_ended,
15953 0 prev_act_ended
15954 FROM bim_mkt_regn_mv a,
15955 fii_time_rpt_struct_v cal,
15956 bis_areas_v d';
15957  IF l_cat_id is not null then
15958   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
15959   end if;
15960   l_sqltext := l_sqltext ||
15961 ' WHERE a.time_id = cal.time_id
15962 AND a.period_type_id = cal.period_type_id
15963 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
15964 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
15965 AND cal.calendar_id=-1
15966 AND  a.object_region =d.id(+)
15967 AND  a.object_country = :l_country ';
15968 IF l_cat_id is null then
15969 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
15970 else
15971   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
15972 			     AND edh.object_type = ''CATEGORY_SET''
15973 			     AND edh.object_id = mdc.category_set_id
15974 			     AND mdc.functional_area_id = 11
15975 			     AND edh.dbi_flag = ''Y''
15976 			     AND edh.parent_id = :l_cat_id  ';
15977   end if;
15978  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
15979  l_sqltext := l_sqltext ||
15980 ' UNION ALL
15981 SELECT
15982 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
15983 a.object_region id,
15984 0 curr_prior_active,
15985 0 prev_prior_active,
15986 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
15987 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
15988 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
15989 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
15990 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
15991 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
15992 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
15993 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
15994 FROM bim_mkt_regn_mv a,
15995 fii_time_rpt_struct_v cal,
15996 bis_areas_v d';
15997  IF l_cat_id is not null then
15998   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
15999   end if;
16000   l_sqltext := l_sqltext ||
16001 ' WHERE a.time_id = cal.time_id
16002 AND a.period_type_id = cal.period_type_id
16003 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
16004 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
16005 AND cal.calendar_id=-1
16006 AND  a.object_region =d.id (+)
16007 AND a.object_country = :l_country ';
16008 IF l_cat_id is null then
16009 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
16010 else
16011   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
16012 			     AND edh.object_type = ''CATEGORY_SET''
16013 			     AND edh.object_id = mdc.category_set_id
16014 			     AND mdc.functional_area_id = 11
16015 			     AND edh.dbi_flag = ''Y''
16016 			     AND edh.parent_id = :l_cat_id  ';
16017   end if;
16018 
16019   l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region ) group by name,id )';
16020 
16021  ELSE --view by country
16022 l_sqltext :=
16023 '
16024 SELECT
16025 VIEWBY,
16026 VIEWBYID,
16027 BIM_ATTRIBUTE2,
16028 BIM_ATTRIBUTE3,
16029 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
16030 BIM_ATTRIBUTE5,
16031 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
16032 BIM_ATTRIBUTE7,
16033 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
16034 BIM_ATTRIBUTE3 BIM_ATTRIBUTE9,
16035 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
16036 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
16037 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
16038 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
16039 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
16040 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
16041 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
16042 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL8,
16043 null BIM_URL1,
16044 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_EVEH_START_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL2,
16048 (
16045 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_EVEH_END_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL3,
16046 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_EVEH_ACT_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL4
16047 FROM
16049 SELECT
16050 name VIEWBY,
16051 id VIEWBYID,
16052 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
16053 sum(curr_started) BIM_ATTRIBUTE3,
16054 SUM(prev_started) BIM_ATTRIBUTE4,
16055 sum(curr_ended)  BIM_ATTRIBUTE5,
16056 SUm(prev_ended) BIM_ATTRIBUTE6,
16057 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
16058 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
16059 FROM
16060 (
16061 SELECT
16062 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
16063 a.object_country id,
16064 sum(even_started-even_ended) curr_prior_active,
16065 0 prev_prior_active,
16066 0 curr_active,
16067 0 prev_active,
16068 0 curr_started,
16069 0 prev_started,
16070 0 curr_ended,
16071 0 prev_ended,
16072 0 curr_act_ended,
16073 0 prev_act_ended
16074 FROM bim_mkt_kpi_cnt_mv a,
16075 fii_time_rpt_struct_v cal,
16076 bis_countries_v d ';
16077  IF l_cat_id is not null then
16078   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
16079   end if;
16080   l_sqltext := l_sqltext ||
16081 ' WHERE a.time_id = cal.time_id
16082 AND a.period_type_id = cal.period_type_id
16083 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16084 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
16085 AND cal.calendar_id=-1
16086 AND  a.object_country =d.country_code (+) ';
16087 IF l_cat_id is null then
16088 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
16089 else
16090   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
16091 			     AND edh.object_type = ''CATEGORY_SET''
16092 			     AND edh.object_id = mdc.category_set_id
16093 			     AND mdc.functional_area_id = 11
16094 			     AND edh.dbi_flag = ''Y''
16095 			     AND edh.parent_id = :l_cat_id  ';
16096   end if;
16097  IF l_country = 'N' THEN
16098      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
16099  ELSE
16100   l_sqltext := l_sqltext ||
16101 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
16102  END IF;
16103 /*l_sqltext := l_sqltext ||
16104 'group by object_country*/
16105 l_sqltext :=l_sqltext ||
16106 ' UNION ALL
16107 SELECT
16108 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
16109 a.object_country id,
16110 0 curr_prior_active,
16111 sum(even_started-even_ended) prev_prior_active,
16112 0 curr_active,
16113 0 prev_active,
16114 0 curr_started,
16115 0 prev_started,
16116 0 curr_ended,
16117 0 prev_ended,
16118 0 curr_act_ended,
16119 0 prev_act_ended
16120 FROM bim_mkt_kpi_cnt_mv a,
16121 fii_time_rpt_struct_v cal,
16122 bis_countries_v d';
16123  IF l_cat_id is not null then
16124   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
16125   end if;
16126   l_sqltext := l_sqltext ||
16127 ' WHERE a.time_id = cal.time_id
16128 AND a.period_type_id = cal.period_type_id
16129 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16130 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
16131 AND cal.calendar_id=-1
16132 AND  a.object_country =d.country_code (+)';
16133 IF l_cat_id is null then
16134 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
16135 else
16136   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
16137 			     AND edh.object_type = ''CATEGORY_SET''
16138 			     AND edh.object_id = mdc.category_set_id
16139 			     AND mdc.functional_area_id = 11
16140 			     AND edh.dbi_flag = ''Y''
16141 			     AND edh.parent_id = :l_cat_id  ';
16142   end if;
16143  IF l_country = 'N' THEN
16144      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
16145  ELSE
16146   l_sqltext := l_sqltext ||
16147 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
16148  END IF;
16149 l_sqltext := l_sqltext ||
16150 ' UNION ALL
16151 SELECT
16152 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
16153 a.object_country id,
16154 0 curr_prior_active,
16155 0 prev_prior_active,
16156 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
16157 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
16158 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
16159 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
16160 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
16161 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
16162 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
16166 bis_countries_v d';
16163 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
16164 FROM bim_mkt_kpi_cnt_mv a,
16165 fii_time_rpt_struct_v cal,
16167  IF l_cat_id is not null then
16168   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
16169   end if;
16170   l_sqltext := l_sqltext ||
16171 ' WHERE a.time_id = cal.time_id
16172 AND a.period_type_id = cal.period_type_id
16173 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
16174 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
16175 AND cal.calendar_id=-1
16176 AND  a.object_country =d.country_code (+) ';
16177 IF l_cat_id is null then
16178 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
16179 else
16180   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
16181 			     AND edh.object_type = ''CATEGORY_SET''
16182 			     AND edh.object_id = mdc.category_set_id
16183 			     AND mdc.functional_area_id = 11
16184 			     AND edh.dbi_flag = ''Y''
16185 			     AND edh.parent_id = :l_cat_id  ';
16186   end if;
16187 IF l_country = 'N' THEN
16188      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
16189  ELSE
16190   l_sqltext := l_sqltext ||
16191 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
16192  END IF; -- end product category numm loop
16193  END IF; --end view by product category
16194 ELSE -- if admin_flag is not equal to Y
16195 if (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
16196 IF l_cat_id is null THEN
16197 l_sqltext :=
16198 '
16199 SELECT
16200 VIEWBY,
16201 VIEWBYID,
16202 BIM_ATTRIBUTE2,
16203 BIM_ATTRIBUTE3,
16204 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
16205 BIM_ATTRIBUTE5,
16206 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
16207 BIM_ATTRIBUTE7,
16208 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
16209 BIM_ATTRIBUTE3 BIM_ATTRIBUTE9,
16210 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
16211 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
16212 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
16213 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
16214 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
16215 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
16216 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
16217 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL8,
16218 decode(leaf_node_id,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_EVEH_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
16219 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_EVEH_START_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL2,
16220 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_EVEH_END_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL3,
16221 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_EVEH_ACT_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL4
16222 FROM
16223 (
16224 SELECT
16225 name VIEWBY,
16226 id VIEWBYID,
16227 leaf_node_id leaf_node_id,
16228 nvl(sum(curr_prior_active),0)  BIM_ATTRIBUTE2,
16229 sum(curr_started)  BIM_ATTRIBUTE3,
16230 SUM(prev_started)  BIM_ATTRIBUTE4,
16231 sum(curr_ended)  BIM_ATTRIBUTE5,
16232 SUm(prev_ended) BIM_ATTRIBUTE6,
16233 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
16234 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended)   BIM_ATTRIBUTE8
16235 FROM
16236 (
16237 SELECT
16238 p.value name,
16239 p.parent_id id,
16240 p.parent_id leaf_node_id,
16241 sum(even_started-even_ended) curr_prior_active,
16242 0 prev_prior_active,
16243 0 curr_active,
16244 0 prev_active,
16245 0 curr_started,
16246 0 prev_started,
16247 0 curr_ended,
16248 0 prev_ended,
16249 0 curr_act_ended,
16250 0 prev_act_ended
16251 FROM bim_i_obj_mets_mv a,
16252      fii_time_rpt_struct_v cal,
16253      eni_denorm_hierarchies b,
16254      mtl_default_category_sets mdcs,';
16255 IF l_prog_view='Y' then
16256   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
16257 ELSE
16258 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
16259 END IF;
16260  l_sqltext := l_sqltext ||' ,(select e.parent_id parent_id ,e.value value
16261       from eni_item_vbh_nodes_v e
16262       where
16263       e.top_node_flag=''Y''
16264       AND e.child_id = e.parent_id
16265       ) p
16266 WHERE
16267      a.time_id = cal.time_id
16268 AND  a.period_type_id = cal.period_type_id
16269 AND a.immediate_parent_id is null
16270 AND ac.resource_id = :l_resource_id
16271 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16272 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
16273 AND cal.calendar_id=-1
16274 AND a.object_country = :l_country
16278 AND mdcs.functional_area_id = 11
16275 AND  a.category_id = b.child_id
16276 AND b.object_type = ''CATEGORY_SET''
16277 AND b.object_id = mdcs.category_set_id
16279 AND b.dbi_flag = ''Y''
16280 AND p.parent_id = b.parent_id';
16281 IF l_prog_view='N' then
16282 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16283 AND a.source_code_id = s.source_code_id
16284 AND s.object_id = ac.object_id
16285 AND s.object_type=ac.object_type
16286 ';
16287 ELSE
16288 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16289 END IF;
16290 IF l_cat_id is not null
16291 then
16292 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
16293 end if;
16294 l_sqltext := l_sqltext ||' group by p.value,p.parent_id';
16295 l_sqltext :=l_sqltext ||
16296 ' UNION ALL
16297 SELECT
16298 p.value name,
16299 p.parent_id id,
16300 p.parent_id leaf_node_id,
16301 0 curr_prior_active,
16302 sum(even_started-even_ended) prev_prior_active,
16303 0 curr_active,
16304 0 prev_active,
16305 0 curr_started,
16306 0 prev_started,
16307 0 curr_ended,
16308 0 prev_ended,
16309 0 curr_act_ended,
16310 0 prev_act_ended
16311 FROM bim_i_obj_mets_mv a,
16312      fii_time_rpt_struct_v cal,
16313      eni_denorm_hierarchies b,
16314      mtl_default_category_sets mdcs,';
16315 IF l_prog_view='Y' then
16316   l_sqltext := l_sqltext ||' bim_i_top_objects ac ';
16317 ELSE
16318 l_sqltext := l_sqltext ||' ams_act_access_denorm ac, bim_i_source_codes s ';
16319 END IF;
16320 l_sqltext := l_sqltext||' ,(select e.parent_id parent_id ,e.value value
16321       from eni_item_vbh_nodes_v e
16322       where
16323       e.top_node_flag=''Y''
16324       AND e.child_id = e.parent_id
16325       ) p
16326 WHERE
16327      a.time_id = cal.time_id
16328 AND  a.period_type_id = cal.period_type_id
16329 AND a.immediate_parent_id is null
16330 AND ac.resource_id = :l_resource_id
16331 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16332 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
16333 AND cal.calendar_id=-1
16334 AND a.object_country = :l_country
16335 AND  a.category_id = b.child_id
16336 AND b.object_type = ''CATEGORY_SET''
16337 AND b.object_id = mdcs.category_set_id
16338 AND mdcs.functional_area_id = 11
16339 AND b.dbi_flag = ''Y''
16340 AND p.parent_id = b.parent_id ';
16341 IF l_prog_view='N' then
16342 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16343 AND a.source_code_id = s.source_code_id
16344 AND s.object_id = ac.object_id
16345 AND s.object_type=ac.object_type
16346 ';
16347 ELSE
16348 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16349 END IF;
16350 IF l_cat_id is not null
16351 then
16352 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
16353 end if;
16354 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
16355  UNION ALL
16356 SELECT
16357 p.value name,
16358 p.parent_id id,
16359 p.parent_id leaf_node_id,
16360 0 curr_prior_active,
16361 0 prev_prior_active,
16362 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
16363 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
16364 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
16365 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
16366 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
16367 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
16368 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
16369 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
16370 FROM bim_i_obj_mets_mv a,
16371      fii_time_rpt_struct_v cal,
16372      eni_denorm_hierarchies b,
16373      mtl_default_category_sets mdcs,';
16374 IF l_prog_view='Y' then
16375   l_sqltext := l_sqltext ||' bim_i_top_objects ac ';
16376 ELSE
16377 l_sqltext := l_sqltext ||' ams_act_access_denorm ac , bim_i_source_codes s ';
16378 END IF;
16379 l_sqltext := l_sqltext||' ,(select e.parent_id parent_id ,e.value value
16380       from eni_item_vbh_nodes_v e
16381       where
16382       e.top_node_flag=''Y''
16383       AND e.child_id = e.parent_id
16384       ) p
16385 WHERE
16386      a.time_id = cal.time_id
16387 AND  a.period_type_id = cal.period_type_id
16388 AND a.immediate_parent_id is null
16389 AND ac.resource_id = :l_resource_id
16390 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
16391 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
16392 AND cal.calendar_id=-1
16393 AND a.object_country = :l_country
16394 AND  a.category_id = b.child_id
16395     AND b.object_type = ''CATEGORY_SET''
16396     AND b.object_id = mdcs.category_set_id
16397     AND mdcs.functional_area_id = 11
16398     AND b.dbi_flag = ''Y''
16399     AND p.parent_id = b.parent_id ';
16400 IF l_prog_view='N' then
16401 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16402 AND a.source_code_id = s.source_code_id
16403 AND s.object_id = ac.object_id
16404 AND s.object_type=ac.object_type
16405 ';
16406 ELSE
16410 then
16407 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16408 END IF;
16409 IF l_cat_id is not null
16411 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
16412 end if;
16413 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
16414 ) group by name,id,leaf_node_id )';
16415 ELSE -- product category is not null
16416 l_sqltext :=
16417 'SELECT
16418 VIEWBY,
16419 VIEWBYID,
16420 BIM_ATTRIBUTE2,
16421 BIM_ATTRIBUTE3,
16422 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
16423 BIM_ATTRIBUTE5,
16424 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
16425 BIM_ATTRIBUTE7,
16426 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
16427 BIM_ATTRIBUTE3 BIM_ATTRIBUTE9,
16428 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
16429 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
16430 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
16431 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
16432 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
16433 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
16434 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
16435 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL8,
16436 decode(leaf_node_id,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_EVEH_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
16437 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_EVEH_START_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL2,
16438 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_EVEH_END_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL3,
16439 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_EVEH_ACT_DETL&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL4
16440 FROM
16441 (
16442 SELECT
16443 name VIEWBY,
16444 id VIEWBYID,
16445 leaf_node_id leaf_node_id,
16446 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
16447 sum(curr_started) BIM_ATTRIBUTE3,
16448 SUM(prev_started) BIM_ATTRIBUTE4,
16449 sum(curr_ended)  BIM_ATTRIBUTE5,
16450 SUm(prev_ended) BIM_ATTRIBUTE6,
16451 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
16452 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
16453 FROM
16454 (
16455 SELECT
16456 p.value name,
16457 b.parent_id id,
16458 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
16459 sum(even_started-even_ended) curr_prior_active,
16460 0 prev_prior_active,
16461 0 curr_active,
16462 0 prev_active,
16463 0 curr_started,
16464 0 prev_started,
16465 0 curr_ended,
16466 0 prev_ended,
16467 0 curr_act_ended,
16468 0 prev_act_ended
16469 FROM bim_i_obj_mets_mv a,
16470      fii_time_rpt_struct_v cal,
16471      eni_denorm_hierarchies b,
16472      mtl_default_category_sets mdcs,';
16473 IF l_prog_view='Y' then
16474   l_sqltext := l_sqltext ||' bim_i_top_objects ac ';
16475 ELSE
16476 l_sqltext := l_sqltext ||' ams_act_access_denorm ac ,bim_i_source_codes s ';
16477 END IF;
16478 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value,e.leaf_node_flag leaf_node_flag
16479       from eni_item_vbh_nodes_v e
16480       where e.parent_id =:l_cat_id
16481       AND e.id = e.child_id
16482       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
16483       ) p
16484 WHERE
16485      a.time_id = cal.time_id
16486 AND  a.period_type_id = cal.period_type_id
16487 AND a.immediate_parent_id is null
16488 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16489 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
16490 AND cal.calendar_id=-1
16491 AND a.object_country = :l_country
16492 AND ac.resource_id = :l_resource_id
16493 AND a.category_id = b.child_id
16494 AND b.object_type = ''CATEGORY_SET''
16495 AND b.object_id = mdcs.category_set_id
16496 AND mdcs.functional_area_id = 11
16497 AND b.parent_id = p.id
16498 AND b.dbi_flag = ''Y''';
16499 IF l_prog_view='N' then
16500 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16501 AND a.source_code_id = s.source_code_id
16502 AND s.object_id = ac.object_id
16503 AND s.object_type=ac.object_type
16504 ';
16505 ELSE
16506 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16507 END IF;
16508 l_sqltext := l_sqltext ||' group by p.value,b.parent_id,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)';
16509 l_sqltext :=l_sqltext ||
16510 ' UNION ALL
16511 SELECT
16512 p.value name,
16513 a.category_id id,
16514 -1 leaf_node_id,
16515 sum(even_started-even_ended) curr_prior_active,
16516 0 prev_prior_active,
16517 0 curr_active,
16518 0 prev_active,
16519 0 curr_started,
16520 0 prev_started,
16521 0 curr_ended,
16522 0 prev_ended,
16523 0 curr_act_ended,
16524 0 prev_act_ended
16525 FROM bim_i_obj_mets_mv a,
16526      fii_time_rpt_struct_v cal,';
16527 IF l_prog_view='Y' then
16528   l_sqltext := l_sqltext ||' bim_i_top_objects ac ';
16529 ELSE
16530 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bim_i_source_codes s ';
16531 END IF;
16535       AND e.parent_id = e.child_id
16532 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value
16533       from eni_item_vbh_nodes_v e
16534       where e.parent_id =  :l_cat_id
16536       AND leaf_node_flag <> ''Y''
16537       ) p
16538 WHERE
16539      a.time_id = cal.time_id
16540 AND  a.period_type_id = cal.period_type_id
16541 AND a.immediate_parent_id is null
16542 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16543 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
16544 AND cal.calendar_id=-1
16545 AND a.object_country = :l_country
16546 AND ac.resource_id = :l_resource_id
16547 AND a.category_id = p.id';
16548 IF l_prog_view='N' then
16549 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16550 AND a.source_code_id = s.source_code_id
16551 AND s.object_id = ac.object_id
16552 AND s.object_type=ac.object_type
16553 ';
16554 ELSE
16555 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16556 END IF;
16557 l_sqltext := l_sqltext ||' group by p.value,a.category_id
16558 UNION ALL
16559 SELECT
16560 p.value name,
16561 b.parent_id id,
16562 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
16563 0 curr_prior_active,
16564 sum(even_started-even_ended) prev_prior_active,
16565 0 curr_active,
16566 0 prev_active,
16567 0 curr_started,
16568 0 prev_started,
16569 0 curr_ended,
16570 0 prev_ended,
16571 0 curr_act_ended,
16572 0 prev_act_ended
16573 FROM bim_i_obj_mets_mv a,
16574      fii_time_rpt_struct_v cal,
16575      eni_denorm_hierarchies b,
16576      mtl_default_category_sets mdcs,';
16577 IF l_prog_view='Y' then
16578   l_sqltext := l_sqltext ||' bim_i_top_objects ac ';
16579 ELSE
16580 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bim_i_source_codes s ';
16581 END IF;
16582 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value,e.leaf_node_flag leaf_node_flag
16583       from eni_item_vbh_nodes_v e
16584       where e.parent_id =:l_cat_id
16585       AND e.id = e.child_id
16586       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'') ) p
16587 WHERE a.time_id = cal.time_id
16588 AND  a.period_type_id = cal.period_type_id
16589 AND a.immediate_parent_id is null
16590 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16591 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
16592 AND cal.calendar_id=-1
16593 AND a.object_country = :l_country
16594 AND ac.resource_id = :l_resource_id
16595 AND a.category_id = b.child_id
16596 AND b.object_type = ''CATEGORY_SET''
16597 AND b.object_id = mdcs.category_set_id
16598 AND mdcs.functional_area_id = 11
16599 AND b.dbi_flag =''Y''
16600 AND b.parent_id = p.id ';
16601 IF l_prog_view='N' then
16602 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16603 AND a.source_code_id = s.source_code_id
16604 AND s.object_id = ac.object_id
16605 AND s.object_type=ac.object_type
16606 ';
16607 ELSE
16608 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16609 END IF;
16610 l_sqltext := l_sqltext ||' group by p.value,b.parent_id,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)
16611 UNION ALL
16612 SELECT
16613 p.value name,
16614 a.category_id id,
16615 -1 leaf_node_id,
16616 0 curr_prior_active,
16617 sum(even_started-even_ended) prev_prior_active,
16618 0 curr_active,
16619 0 prev_active,
16620 0 curr_started,
16621 0 prev_started,
16622 0 curr_ended,
16623 0 prev_ended,
16624 0 curr_act_ended,
16625 0 prev_act_ended
16626 FROM bim_i_obj_mets_mv a,
16627      fii_time_rpt_struct_v cal,';
16628 IF l_prog_view='Y' then
16629   l_sqltext := l_sqltext ||' bim_i_top_objects ac ';
16630 ELSE
16631 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bim_i_source_codes s ';
16632 END IF;
16633 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value
16634       from eni_item_vbh_nodes_v e
16635       where e.parent_id =  :l_cat_id
16636       AND e.parent_id = e.child_id
16637       AND leaf_node_flag <> ''Y''
16638       ) p
16639 WHERE
16640      a.time_id = cal.time_id
16641 AND  a.period_type_id = cal.period_type_id
16642 AND a.immediate_parent_id is null
16643 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16644 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
16645 AND cal.calendar_id=-1
16646 AND a.object_country = :l_country
16647 AND ac.resource_id = :l_resource_id
16648 AND a.category_id = p.id
16649 ';
16650 IF l_prog_view='N' then
16651 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16652 AND a.source_code_id = s.source_code_id
16653 AND s.object_id = ac.object_id
16654 AND s.object_type=ac.object_type
16655 ';
16656 ELSE
16657 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16658 END IF;
16659 l_sqltext := l_sqltext ||' group by p.value,a.category_id
16660  UNION ALL
16661 SELECT
16662 p.value name,
16663 b.parent_id id,
16664 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) leaf_node_id,
16665 0 curr_prior_active,
16666 0 prev_prior_active,
16667 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
16668 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
16669 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
16670 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
16674 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
16671 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
16672 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
16673 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
16675 FROM bim_i_obj_mets_mv a,
16676      fii_time_rpt_struct_v cal,
16677      eni_denorm_hierarchies b,
16678      mtl_default_category_sets mdcs,';
16679 IF l_prog_view='Y' then
16680   l_sqltext := l_sqltext ||' bim_i_top_objects ac ';
16681 ELSE
16682 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bim_i_source_codes s ';
16683 END IF;
16684 l_sqltext :=l_sqltext||' ,(select e.id id ,e.value value,e.leaf_node_flag leaf_node_flag
16685       from eni_item_vbh_nodes_v e
16686       where e.parent_id =:l_cat_id
16687       AND e.id = e.child_id
16688       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')) p
16689 WHERE
16690      a.time_id = cal.time_id
16691 AND  a.period_type_id = cal.period_type_id
16692 AND a.immediate_parent_id is null
16693 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
16694 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
16695 AND cal.calendar_id=-1
16696 AND a.object_country = :l_country
16697 AND a.category_id = b.child_id
16698 AND ac.resource_id = :l_resource_id
16699 AND b.object_type = ''CATEGORY_SET''
16700 AND b.object_id = mdcs.category_set_id
16701 AND mdcs.functional_area_id = 11
16702 AND b.dbi_flag = ''Y''
16703 AND b.parent_id = p.id ';
16704 IF l_prog_view='N' then
16705 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16706 AND a.source_code_id = s.source_code_id
16707 AND s.object_id = ac.object_id
16708 AND s.object_type=ac.object_type
16709 ';
16710 ELSE
16711 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16712 END IF;
16713 l_sqltext := l_sqltext ||' group by p.value,b.parent_id,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)
16714 UNION ALL
16715 SELECT
16716 p.value name,
16717 a.category_id id,
16718 -1 leaf_node_id,
16719 0 curr_prior_active,
16720 0 prev_prior_active,
16721 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
16722 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
16723 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
16724 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
16725 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
16726 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
16727 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
16728 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
16729 FROM bim_i_obj_mets_mv a,
16730      fii_time_rpt_struct_v cal,';
16731 IF l_prog_view='Y' then
16732   l_sqltext := l_sqltext ||' bim_i_top_objects ac ';
16733 ELSE
16734 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bim_i_source_codes s ';
16735 END IF;
16736 l_sqltext := l_sqltext ||' ,(select e.id id ,e.value value
16737       from eni_item_vbh_nodes_v e
16738       where e.parent_id =  :l_cat_id
16739       AND e.parent_id = e.child_id
16740       AND leaf_node_flag <> ''Y''
16741       ) p
16742 WHERE
16743      a.time_id = cal.time_id
16744 AND  a.period_type_id = cal.period_type_id
16745 AND a.immediate_parent_id is null
16746 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
16747 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
16748 AND cal.calendar_id=-1
16749 AND a.object_country = :l_country
16750 AND ac.resource_id = :l_resource_id
16751 AND a.category_id = p.id';
16752 IF l_prog_view='N' then
16753 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16754 AND a.source_code_id = s.source_code_id
16755 AND s.object_id = ac.object_id
16756 AND s.object_type=ac.object_type
16757 ';
16758 ELSE
16759 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16760 END IF;
16761  l_sqltext:=l_sqltext||' group by p.value,a.category_id
16762  ) group by name,id,leaf_node_id )';
16763  END IF; -- product category All loop
16764 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
16765 l_sqltext :=
16766 '
16767 SELECT
16768 VIEWBY,
16769 VIEWBYID,
16770 BIM_ATTRIBUTE2,
16771 BIM_ATTRIBUTE3,
16772 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
16773 BIM_ATTRIBUTE5,
16774 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
16775 BIM_ATTRIBUTE7,
16776 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
16777 BIM_ATTRIBUTE3 BIM_ATTRIBUTE9,
16778 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
16779 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
16783 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
16780 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
16781 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
16782 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
16784 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
16785 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL8,
16786 null BIM_URL1,
16787 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_EVEH_START_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=event&BIM_PARAMETER5=VIEWBY'') BIM_URL2,
16788 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_EVEH_END_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=event&BIM_PARAMETER5=VIEWBY'') BIM_URL3,
16789 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_EVEH_ACT_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY+AREA&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=event&BIM_PARAMETER5=VIEWBY'') BIM_URL4
16790 FROM
16791 (
16792 SELECT
16793 name VIEWBY,
16794 id VIEWBYID,
16795 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
16796 sum(curr_started) BIM_ATTRIBUTE3,
16797 SUM(prev_started)  BIM_ATTRIBUTE4,
16798 sum(curr_ended)  BIM_ATTRIBUTE5,
16799 SUm(prev_ended) BIM_ATTRIBUTE6,
16800 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
16801 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
16802 FROM
16803 (
16804 SELECT
16805 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
16806 a.object_region id,
16807 sum(even_started-even_ended) curr_prior_active,
16808 0 prev_prior_active,
16809 0 curr_active,
16810 0 prev_active,
16811 0 curr_started,
16812 0 prev_started,
16813 0 curr_ended,
16814 0 prev_ended,
16815 0 curr_act_ended,
16816 0 prev_act_ended
16817 FROM bim_obj_regn_mv a,
16818 fii_time_rpt_struct_v cal,';
16819 IF l_prog_view='Y' then
16820   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_areas_v d  ';
16821 ELSE
16822 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_areas_v d,bim_i_source_codes s ';
16823 END IF;
16824  IF l_cat_id is not null then
16825   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
16826   end if;
16827   l_sqltext := l_sqltext ||
16828 ' WHERE a.time_id = cal.time_id
16829 AND a.period_type_id = cal.period_type_id
16830 AND a.immediate_parent_id is null
16831 AND ac.resource_id = :l_resource_id';
16832 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16833 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
16834 AND cal.calendar_id=-1
16835 AND  a.object_region =d.id(+)
16836 AND a.object_country = :l_country ';
16837 IF l_prog_view='N' then
16838 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16839 AND a.source_code_id = s.source_code_id
16840 AND s.object_id = ac.object_id
16841 AND s.object_type=ac.object_type
16842 ';
16843 ELSE
16844 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16845 END IF;
16846 IF l_cat_id is null then
16847 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
16848 else
16849   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
16850 			     AND edh.object_type = ''CATEGORY_SET''
16851 			     AND edh.object_id = mdc.category_set_id
16852 			     AND mdc.functional_area_id = 11
16853 			     AND edh.dbi_flag = ''Y''
16854 			     AND edh.parent_id = :l_cat_id ';
16855   end if;
16856  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
16857  l_sqltext :=l_sqltext ||
16858 ' UNION ALL
16859 SELECT
16860 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
16861 a.object_region id,
16862 0 curr_prior_active,
16863 sum(even_started-even_ended) prev_prior_active,
16864 0 curr_active,
16865 0 prev_active,
16866 0 curr_started,
16867 0 prev_started,
16868 0 curr_ended,
16869 0 prev_ended,
16870 0 curr_act_ended,
16871 0 prev_act_ended
16872 FROM bim_obj_regn_mv a,
16873 fii_time_rpt_struct_v cal,';
16874 IF l_prog_view='Y' then
16875   l_sqltext := l_sqltext ||' bim_i_top_objects ac ,bis_areas_v d ';
16876 ELSE
16877 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_areas_v d,bim_i_source_codes s ';
16878 END IF;
16879  IF l_cat_id is not null then
16880   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
16881   end if;
16882   l_sqltext := l_sqltext ||
16883 ' WHERE a.time_id = cal.time_id
16884 AND a.period_type_id = cal.period_type_id
16885 AND a.immediate_parent_id is null
16886 AND ac.resource_id = :l_resource_id';
16887 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
16888 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
16889 AND cal.calendar_id=-1
16890 AND  a.object_region =d.id(+)
16891 AND a.object_country = :l_country ';
16892 IF l_prog_view='N' then
16893 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16894 AND a.source_code_id = s.source_code_id
16895 AND s.object_id = ac.object_id
16896 AND s.object_type=ac.object_type
16897 ';
16898 ELSE
16899 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16900 END IF;
16901 IF l_cat_id is null then
16902 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
16903 else
16907 			     AND mdc.functional_area_id = 11
16904   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
16905 			     AND edh.object_type = ''CATEGORY_SET''
16906 			     AND edh.object_id = mdc.category_set_id
16908 			     AND edh.dbi_flag = ''Y''
16909 			     AND edh.parent_id = :l_cat_id  ';
16910   end if;
16911 l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
16912 l_sqltext := l_sqltext ||
16913 ' UNION ALL
16914 SELECT
16915 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
16916 a.object_region id,
16917 0 curr_prior_active,
16918 0 prev_prior_active,
16919 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
16920 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
16921 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
16922 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
16923 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
16924 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
16925 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
16926 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
16927 FROM bim_obj_regn_mv a,
16928 fii_time_rpt_struct_v cal,';
16929 IF l_prog_view='Y' then
16930   l_sqltext := l_sqltext ||' bim_i_top_objects ac ,bis_areas_v d ';
16931 ELSE
16932 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_areas_v d,bim_i_source_codes s ';
16933 END IF;
16934  IF l_cat_id is not null then
16935   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
16936   end if;
16937   l_sqltext := l_sqltext ||
16938 ' WHERE a.time_id = cal.time_id
16939 AND a.period_type_id = cal.period_type_id
16940 AND a.immediate_parent_id is null
16941 AND ac.resource_id = :l_resource_id';
16942 l_sqltext := l_sqltext||' AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
16943 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
16944 AND cal.calendar_id=-1
16945 AND  a.object_region =d.id(+)
16946 AND a.object_country = :l_country ';
16947 IF l_prog_view='N' then
16948 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
16949 AND a.source_code_id = s.source_code_id
16950 AND s.object_id = ac.object_id
16951 AND s.object_type=ac.object_type
16952 ';
16953 ELSE
16954 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
16955 END IF;
16956 IF l_cat_id is null then
16957 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
16958 else
16959   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
16960 			     AND edh.object_type = ''CATEGORY_SET''
16961 			     AND edh.object_id = mdc.category_set_id
16962 			     AND mdc.functional_area_id = 11
16963 			     AND edh.dbi_flag = ''Y''
16964 			     AND edh.parent_id = :l_cat_id  ';
16965   end if;
16966  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region ) group by name,id )';
16967 
16968 ELSE --view by country
16969 l_sqltext :=
16970 '
16971 SELECT
16972 VIEWBY,
16973 VIEWBYID,
16974 BIM_ATTRIBUTE2,
16975 BIM_ATTRIBUTE3,
16976 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
16977 BIM_ATTRIBUTE5,
16978 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
16979 BIM_ATTRIBUTE7,
16980 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
16981 BIM_ATTRIBUTE3 BIM_ATTRIBUTE9,
16982 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
16983 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
16984 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
16985 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
16986 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
16987 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
16988 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
16989 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL8,
16990 null BIM_URL1,
16991 decode(BIM_ATTRIBUTE3,0,NULL,''pFunctionName=BIM_I_EVEH_START_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=1&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL2,
16992 decode(BIM_ATTRIBUTE5,0,NULL,''pFunctionName=BIM_I_EVEH_END_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=2&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL3,
16993 decode(BIM_ATTRIBUTE7,0,NULL,''pFunctionName=BIM_I_EVEH_ACT_DETL&pParamIds=Y&VIEW_BY=GEOGRAPHY_COUNTRY&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER1=3&BIM_PARAMETER2=event&BIM_PARAMETER5=All'') BIM_URL4
16994 FROM
16995 (
16996 SELECT
16997 name VIEWBY,
16998 id VIEWBYID,
16999 nvl(sum(curr_prior_active),0)  BIM_ATTRIBUTE2,
17000 sum(curr_started)  BIM_ATTRIBUTE3,
17001 SUM(prev_started) BIM_ATTRIBUTE4,
17002 sum(curr_ended)  BIM_ATTRIBUTE5,
17003 SUm(prev_ended) BIM_ATTRIBUTE6,
17004 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
17008 SELECT
17005 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
17006 FROM
17007 (
17009 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
17010 a.object_country id,
17011 sum(even_started-even_ended) curr_prior_active,
17012 0 prev_prior_active,
17013 0 curr_active,
17014 0 prev_active,
17015 0 curr_started,
17016 0 prev_started,
17017 0 curr_ended,
17018 0 prev_ended,
17019 0 curr_act_ended,
17020 0 prev_act_ended
17021 FROM bim_i_obj_mets_mv a,
17022 fii_time_rpt_struct_v cal,';
17023 IF l_prog_view='Y' then
17024   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_countries_v d ';
17025 ELSE
17026 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_countries_v d,bim_i_source_codes s  ';
17027 END IF;
17028  IF l_cat_id is not null then
17029   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
17030   end if;
17031   l_sqltext := l_sqltext ||
17032 ' WHERE a.time_id = cal.time_id
17033 AND a.period_type_id = cal.period_type_id
17034 AND a.immediate_parent_id is null
17035 AND ac.resource_id = :l_resource_id';
17036 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
17037 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
17038 AND cal.calendar_id=-1
17039 AND  a.object_country =d.country_code (+) ';
17040 IF l_prog_view='N' then
17041 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
17042 AND a.source_code_id = s.source_code_id
17043 AND s.object_id = ac.object_id
17044 AND s.object_type=ac.object_type
17045 ';
17046 ELSE
17047 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
17048 END IF;
17049 IF l_cat_id is null then
17050 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
17051 else
17052   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
17053 			     AND edh.object_type = ''CATEGORY_SET''
17054 			     AND edh.object_id = mdc.category_set_id
17055 			     AND mdc.functional_area_id = 11
17056 			     AND edh.dbi_flag = ''Y''
17057 			     AND edh.parent_id = :l_cat_id ';
17058   end if;
17059  IF l_country = 'N' THEN
17060      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
17061  ELSE
17062   l_sqltext := l_sqltext ||
17063 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
17064  END IF;
17065 /*l_sqltext := l_sqltext ||
17066 --' AND a.object_country = :l_country
17067 'group by object_country*/
17068 l_sqltext :=l_sqltext ||
17069 ' UNION ALL
17070 SELECT
17071 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
17072 a.object_country id,
17073 0 curr_prior_active,
17074 sum(even_started-even_ended) prev_prior_active,
17075 0 curr_active,
17076 0 prev_active,
17077 0 curr_started,
17078 0 prev_started,
17079 0 curr_ended,
17080 0 prev_ended,
17081 0 curr_act_ended,
17082 0 prev_act_ended
17083 FROM bim_i_obj_mets_mv a,
17084 fii_time_rpt_struct_v cal,';
17085 IF l_prog_view='Y' then
17086   l_sqltext := l_sqltext ||' bim_i_top_objects ac, bis_countries_v d ';
17087 ELSE
17088 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_countries_v d,bim_i_source_codes s  ';
17089 END IF;
17090  IF l_cat_id is not null then
17091   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
17092   end if;
17093   l_sqltext := l_sqltext ||
17094 ' WHERE a.time_id = cal.time_id
17095 AND a.period_type_id = cal.period_type_id
17096 AND a.immediate_parent_id is null
17097 AND ac.resource_id = :l_resource_id';
17098 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
17099 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
17100 AND cal.calendar_id=-1
17101 AND  a.object_country =d.country_code (+)';
17102 IF l_prog_view='N' then
17103 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
17104 AND a.source_code_id = s.source_code_id
17105 AND s.object_id = ac.object_id
17106 AND s.object_type=ac.object_type
17107 ';
17108 ELSE
17109 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
17110 END IF;
17111 IF l_cat_id is null then
17112 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
17113 else
17114   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
17115 			   AND edh.object_type = ''CATEGORY_SET''
17116 			   AND edh.object_id = mdc.category_set_id
17117 			   AND mdc.functional_area_id = 11
17118 			   AND edh.dbi_flag = ''Y''
17119 			   AND edh.parent_id = :l_cat_id  ';
17120   end if;
17121  IF l_country = 'N' THEN
17122      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
17123  ELSE
17124   l_sqltext := l_sqltext ||
17125 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
17126  END IF;
17127 l_sqltext := l_sqltext ||
17128 ' UNION ALL
17129 SELECT
17130 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
17131 a.object_country id,
17132 0 curr_prior_active,
17133 0 prev_prior_active,
17134 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (even_started-even_ended) else 0 end) curr_active,
17138 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then even_ended else 0 end) curr_ended,
17135 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (even_started-even_ended) else 0 end) prev_active,
17136 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  even_started else 0 end) curr_started,
17137 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_started else 0 end) prev_started,
17139 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then even_ended else 0 end) prev_ended,
17140 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then even_ended else 0 end) curr_act_ended,
17141 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then even_ended else 0 end) prev_act_ended
17142 FROM bim_i_obj_mets_mv a,
17143 fii_time_rpt_struct_v cal,';
17144 IF l_prog_view='Y' then
17145   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_countries_v d ';
17146 ELSE
17147 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_countries_v d,bim_i_source_codes s  ';
17148 END IF;
17149  IF l_cat_id is not null then
17150   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
17151   end if;
17152   l_sqltext := l_sqltext ||
17153 ' WHERE a.time_id = cal.time_id
17154 AND a.period_type_id = cal.period_type_id
17155 AND a.immediate_parent_id is null
17156 AND ac.resource_id = :l_resource_id';
17157 IF l_prog_view='N' then
17158 l_sqltext := l_sqltext ||' AND s.object_type in (''EVEH'',''EONE'')
17159 AND a.source_code_id = s.source_code_id
17160 AND s.object_id = ac.object_id
17161 AND s.object_type=ac.object_type
17162 ';
17163 ELSE
17164 l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
17165 END IF;
17166 l_sqltext := l_sqltext||' AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
17167 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
17168 AND cal.calendar_id=-1
17169 AND  a.object_country =d.country_code (+)';
17170 IF l_cat_id is null then
17171 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
17172 else
17173   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
17174 			     AND edh.object_type = ''CATEGORY_SET''
17175 			     AND edh.object_id = mdc.category_set_id
17176 			     AND mdc.functional_area_id = 11
17177 			     AND edh.dbi_flag = ''Y''
17178 			     AND edh.parent_id = :l_cat_id  ';
17179   end if;
17180 IF l_country = 'N' THEN
17181      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
17182  ELSE
17183   l_sqltext := l_sqltext ||
17184 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
17185  END IF; -- product category All loop
17186  END IF; --end view by product category
17187  END IF; -- end if admin_flag = Y loop
17188 
17189 
17190 
17191 
17192 
17193   x_custom_sql := l_sqltext;
17194   l_custom_rec.attribute_name := ':l_record_type';
17195   l_custom_rec.attribute_value := l_record_type_id;
17196   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17197   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17198   x_custom_output.EXTEND;
17199   x_custom_output(1) := l_custom_rec;
17200 
17201   l_custom_rec.attribute_name := ':l_resource_id';
17202   l_custom_rec.attribute_value := GET_RESOURCE_ID;
17203   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17204   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17205   x_custom_output.EXTEND;
17206   x_custom_output(2) := l_custom_rec;
17207 
17208   l_custom_rec.attribute_name := ':l_admin_flag';
17209   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
17210   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17211   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17212   x_custom_output.EXTEND;
17213   x_custom_output(3) := l_custom_rec;
17214 
17215   l_custom_rec.attribute_name := ':l_country';
17216   l_custom_rec.attribute_value := l_country;
17217   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17218   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17219   x_custom_output.EXTEND;
17220   x_custom_output(4) := l_custom_rec;
17221 
17222   l_custom_rec.attribute_name := ':l_cat_id';
17223   l_custom_rec.attribute_value := l_cat_id;
17224   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17225   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17226   x_custom_output.EXTEND;
17227   x_custom_output(5) := l_custom_rec;
17228 
17229 
17230 write_debug('GET_TOP_even_OPPS_SQL','QUERY','_',l_sqltext);
17231 --return l_sqltext;
17232 
17233 EXCEPTION
17234 WHEN others THEN
17235 l_sql_errm := SQLERRM;
17236 write_debug('GET_TOP_OPPS_SQL','ERROR',l_sql_errm,l_sqltext);
17237 
17238 END GET_EVEH_START_SQL;
17239 
17240 --campaign detail
17241 
17242 PROCEDURE GET_CAMP_DETL_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
17243                           x_custom_sql  OUT NOCOPY VARCHAR2,
17244                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
17245  IS
17246 l_sqltext varchar2(15000);
17247 l_period_type_hc number;
17248 l_as_of_date  DATE;
17249 l_period_type	varchar2(2000);
17250 l_record_type_id NUMBER;
17251 l_comp_type    varchar2(2000);
17252 l_country      varchar2(4000);
17256 l_user_id NUMBER;
17253 l_view_by      varchar2(4000);
17254 l_sql_errm      varchar2(4000);
17255 l_period_type_id NUMBER;
17257 l_resource_id NUMBER;
17258 l_time_id_column  VARCHAR2(1000);
17259 l_admin_status VARCHAR2(20);
17260 l_admin_flag VARCHAR2(1);
17261 l_cat_id VARCHAR2(50);
17262 l_campaign_id VARCHAR2(50);
17263 l_custom_rec BIS_QUERY_ATTRIBUTES;
17264 l_curr VARCHAR2(50);
17265 l_curr_suffix VARCHAR2(50);
17266 l_una     VARCHAR2(100);
17267 l_url_str VARCHAR2(1000);
17268 l_url_str2 VARCHAR2(1000);
17269 l_url_str3 VARCHAR2(1000);
17270 l_url_str4 VARCHAR2(1000);
17271 l_url_str5 VARCHAR2(1000);
17272 l_url_str6 VARCHAR2(1000);
17273 l_col_id NUMBER;
17274 l_area VARCHAR2(300);
17275 l_report_name VARCHAR2(300);
17276 l_media VARCHAR2(300);
17277 l_qry	varchar2(5000);
17278 l_qry1	varchar2(5000);
17279 l_qry2	varchar2(5000);
17280 l_where   varchar2(5000);
17281 l_where1   varchar2(5000);
17282 l_where2   varchar2(5000);
17283 l_group_by1 varchar2(5000);
17284 l_group_by2 varchar2(5000);
17285 l_group_by varchar2(5000);
17286 
17287 BEGIN
17288 	x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
17289 	l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
17290 
17291 	bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
17292 											l_as_of_date,
17293 											l_period_type,
17294 											l_record_type_id,
17295 											l_comp_type,
17296 											l_country,
17297 											l_view_by,
17298 											l_cat_id,
17299 											l_campaign_id,
17300 											l_curr,
17301 											l_col_id,
17302 											l_area,
17303 											l_media,
17304 											l_report_name
17305 											);
17306 
17307 	IF l_country IS NULL THEN
17308 
17309 		l_country := 'N';
17310 
17311 	END IF;
17312 
17313 	l_admin_status := GET_ADMIN_STATUS;
17314 	l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
17315 
17316 	IF (l_curr = '''FII_GLOBAL1''')	THEN
17317 
17318 		l_curr_suffix := '';
17319 
17320 	ELSIF (l_curr = '''FII_GLOBAL2''')	THEN
17321 
17322 		l_curr_suffix := '_s';
17323 	ELSE
17324 		l_curr_suffix := '';
17325 	END IF;
17326 
17327 	--l_url_str :='pFunctionName=BIM_I_CAMP_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=18917&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CAMP&PAGE.OBJ.objAttribute=DETL';
17328 	--l_url_str :='&PAGE.OBJ.objType=CAMP&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
17329 	---l_url_str4 :='pFunctionName=BIM_I_CAMP_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=';
17330 	l_url_str :='pFunctionName=AMS_WB_CAMP_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CAMP&objId=';
17331 	l_url_str2 :='&PAGE.OBJ.objType=EVEH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
17332 	l_url_str5 :='pFunctionName=BIM_I_EVEH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=';
17333 	l_url_str3 :='&PAGE.OBJ.objType=EONE&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
17334 	l_url_str6 :='pFunctionName=BIM_I_EONE_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=';
17335 
17336 	IF l_report_name = 'campaign' THEN
17337 
17338 		l_qry := '	SELECT
17339 					BIM_ATTRIBUTE1,
17340 					BIM_ATTRIBUTE2,
17341 					BIM_ATTRIBUTE3,
17342 					BIM_ATTRIBUTE4,
17343 					BIM_ATTRIBUTE5,
17344 					(BIM_ATTRIBUTE4 - BIM_ATTRIBUTE5) BIM_ATTRIBUTE6,
17345 					BIM_ATTRIBUTE7,
17346 					sum(BIM_ATTRIBUTE4) over() BIM_GRAND_TOTAL1,
17347 					sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL2,
17348 					sum(BIM_ATTRIBUTE4) over() - sum(BIM_ATTRIBUTE5) over()  BIM_GRAND_TOTAL3,
17349 					'||''''||l_url_str||''''||'||BIM_ATTRIBUTE7 BIM_URL1
17350 				  FROM
17351 				  (
17352 					SELECT
17353 						b.name BIM_ATTRIBUTE1,
17354 						c.start_date BIM_ATTRIBUTE2,
17355 						c.end_date BIM_ATTRIBUTE3,
17356 						c.object_id BIM_ATTRIBUTE7,
17357 						ab.custom_setup_id BIM_ATTRIBUTE8,
17358 						sum(nvl(a.budget_approved'||l_curr_suffix||',0)) BIM_ATTRIBUTE4,
17359 						sum(nvl(a.cost_actual'||l_curr_suffix||',0)) BIM_ATTRIBUTE5
17360 				   FROM
17361 						bim_i_marketing_facts a,
17362 						bim_i_obj_name_mv b,  bim_i_source_codes c ,
17363 						ams_campaigns_all_b ab';
17364 
17365 		l_where :=' WHERE
17366 					c.source_code_id = b.source_code_id
17367 					AND c.object_type = ''CAMP''
17368 					AND a.source_code_id(+) = c.source_code_id
17369 					AND c.child_object_id = 0
17370 					AND c.object_id = ab.campaign_id
17371 					AND b.language = userenv(''LANG'')';
17372 
17373 		l_group_by := ' GROUP BY b.name,c.object_id,ab.custom_setup_id,c.start_date,c.end_date
17374 					)  &ORDER_BY_CLAUSE ';
17375 
17376 		IF l_admin_status = 'N' THEN
17377 
17378 			l_qry := l_qry ||',ams_act_access_denorm ac  ';
17379 			l_where := l_where|| ' AND c.object_type = ac.object_type
17380 			AND c.object_type=''CAMP''
17381 			AND c.object_id = ac.object_id
17382 			AND ac.resource_id = :l_resource_id ';
17383 
17384 		END IF;
17385 
17386 		IF l_cat_id is not null  then
17387 			l_qry:= l_qry ||
17388 					' , eni_denorm_hierarchies edh , mtl_default_category_sets mdcs';
17389 
17390 		l_where := l_where||'  AND edh.parent_id =:l_cat_id and nvl(c.category_id,-1) = edh.child_id
17391 					AND edh.object_type = ''CATEGORY_SET''
17392 					AND edh.object_id = mdcs.category_set_id
17396 
17393 					AND mdcs.functional_area_id = 11
17394 					AND edh.dbi_flag = ''Y''	';
17395 		END IF;
17397 		IF (l_country <> 'N' ) THEN
17398 			l_where := l_where||' AND c.object_country = :l_country ';
17399 		END If;
17400 
17401 		IF l_area is not null THEN
17402 			l_where := l_where||' AND c.object_region = :l_area ';
17403 		END If;
17404 
17405 		IF l_col_id = 1 THEN /*Started*/
17406 			l_where := l_where||'
17407 			and c.adj_start_date between &BIS_CURRENT_EFFECTIVE_START_DATE and &BIS_CURRENT_ASOF_DATE ';
17408 		ELSIF (l_col_id = 2) THEN/*Ended*/
17409 			l_where := l_where||'
17410 						and c.adj_end_date between &BIS_CURRENT_EFFECTIVE_START_DATE and &BIS_CURRENT_ASOF_DATE ';
17411 		ELSIF (l_col_id = 3) THEN/*Current Active*/
17412 			l_where := l_where||'
17413 						AND c.adj_start_date<=&BIS_CURRENT_ASOF_DATE
17414 						AND c.adj_end_date >= &BIS_CURRENT_ASOF_DATE ';
17415 		END IF;
17416 
17417 		l_sqltext:=l_qry||l_where||l_group_by;
17418 		---end for campaign
17419 	ELSE --- event detail report
17420 		l_qry :=  'SELECT
17421 						BIM_ATTRIBUTE1,
17422 						BIM_ATTRIBUTE2,
17423 						BIM_ATTRIBUTE3,
17424 						BIM_ATTRIBUTE4,
17425 						BIM_ATTRIBUTE5,
17426 						(BIM_ATTRIBUTE4 - BIM_ATTRIBUTE5) BIM_ATTRIBUTE6,
17427 						BIM_ATTRIBUTE7,
17428 						sum(BIM_ATTRIBUTE4) over() BIM_GRAND_TOTAL1,
17429 						sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL2,
17430 						sum(BIM_ATTRIBUTE4) over() - sum(BIM_ATTRIBUTE5) over()  BIM_GRAND_TOTAL3,
17431 						decode(object_type,''EVEH'', '||''''|| l_url_str5|| ''''||'||BIM_ATTRIBUTE8||'||''''|| l_url_str2|| ''''||'||BIM_ATTRIBUTE7,'||''''|| l_url_str6|| ''''||'||BIM_ATTRIBUTE8||'||''''|| l_url_str3|| ''''||'||BIM_ATTRIBUTE7) BIM_URL1
17432 					FROM
17433 						(   ';
17434 
17435 		l_qry1 :='SELECT
17436 					b.name BIM_ATTRIBUTE1,
17437 					c.start_date BIM_ATTRIBUTE2,
17438 					c.end_date BIM_ATTRIBUTE3,
17439 					c.object_id BIM_ATTRIBUTE7,
17440 					ab.setup_type_id BIM_ATTRIBUTE8,
17441 					SUM(nvl(a.budget_approved'||l_curr_suffix||',0)) BIM_ATTRIBUTE4,
17442 					SUM(nvl(a.cost_actual'||l_curr_suffix||',0)) BIM_ATTRIBUTE5,
17443 					''EVEH'' object_type
17444 				FROM	bim_i_marketing_facts a,
17445 						bim_i_obj_name_mv b,
17446 						ams_event_headers_all_b ab,
17447 						bim_i_source_codes c ';
17448 
17449 		l_where1 :=' WHERE
17450 					c.source_code_id = b.source_code_id
17451 					AND c.object_type = ''EVEH''
17452 					AND c.object_id = ab.event_header_id
17453 					AND c.source_code_id = a.source_code_id(+)
17454 					AND c.child_object_id = 0
17455 					AND b.language = userenv(''LANG'')';
17456 
17457 		l_group_by1 := ' GROUP BY b.name,c.object_id,ab.setup_type_id,c.start_date,c.end_date ';
17458 
17459 
17460 		l_qry2 :=' UNION ALL -- for one-off events
17461 				   SELECT
17462 						b.name BIM_ATTRIBUTE1,
17463 						c.start_date BIM_ATTRIBUTE2,
17464 						c.end_date BIM_ATTRIBUTE3,
17465 						c.object_id BIM_ATTRIBUTE7,
17466 						ab.setup_type_id BIM_ATTRIBUTE8,
17467 						SUM(nvl(a.budget_approved'||l_curr_suffix||',0)) BIM_ATTRIBUTE4,
17468 						SUM(nvl(a.cost_actual'||l_curr_suffix||',0)) BIM_ATTRIBUTE5,
17469 						''EONE'' object_type
17470 					FROM bim_i_marketing_facts a,
17471 						bim_i_obj_name_mv b,
17472 						ams_event_offers_all_b ab,
17473 						bim_i_source_codes c';
17474 
17475 		l_where2 :=' WHERE
17476 					c.source_code_id = a.source_code_id (+)
17477 					AND c.object_type = ''EONE''
17478 					AND c.source_code_id = b.source_code_id
17479 					AND c.object_id = ab.event_offer_id
17480 					AND c.child_object_id = 0
17481 					AND b.language = userenv(''LANG'')';
17482 
17483 		l_group_by2 := ' GROUP BY b.name,c.object_id,ab.setup_type_id,c.start_date,c.end_date
17484 					  )  &ORDER_BY_CLAUSE ';
17485 
17486 		IF l_admin_status = 'N' THEN
17487 
17488 			l_qry1 := l_qry1 ||',ams_act_access_denorm ac  ';
17489 
17490 			l_where1 := l_where1|| ' AND c.object_type = ac.object_type
17491 									AND c.object_type=''EVEH''
17492 									AND c.object_id = ac.object_id
17493 									AND ac.resource_id = :l_resource_id ';
17494 
17495 			l_qry2 := l_qry2 ||',ams_act_access_denorm ac  ';
17496 
17497 			l_where2 := l_where2|| ' AND c.object_type = ac.object_type
17498 								AND c.object_type=''EONE''
17499 								AND c.object_id = ac.object_id
17500 								AND ac.resource_id = :l_resource_id ';
17501 		END IF;
17502 
17503 		IF l_cat_id IS NOT NULL  THEN
17504 
17505 			l_qry1:= l_qry1 ||' , eni_denorm_hierarchies edh , mtl_default_category_sets mdcs ';
17506 			l_where1 := l_where1||'  AND edh.parent_id =:l_cat_id
17507 									 AND nvl(c.category_id,-1) = edh.child_id
17508 									 AND edh.object_type = ''CATEGORY_SET''
17509 									AND edh.object_id = mdcs.category_set_id
17510 									AND mdcs.functional_area_id = 11
17511 									AND edh.dbi_flag = ''Y''	';
17512 
17513 			l_qry2:= l_qry2 ||	' , eni_denorm_hierarchies edh , mtl_default_category_sets mdcs';
17514 			l_where2 := l_where2||'  AND edh.parent_id =:l_cat_id
17515 									 AND nvl(c.category_id,-1) = edh.child_id
17516 									 AND edh.object_type = ''CATEGORY_SET''
17517 									AND edh.object_id = mdcs.category_set_id
17518 									AND mdcs.functional_area_id = 11
17519 									AND edh.dbi_flag = ''Y''	';
17520 
17521 		END IF;
17522 
17523 		IF (l_country <> 'N' ) THEN
17524 
17525 			l_where1 := l_where1||' AND c.object_country = :l_country ';
17526 			l_where2 := l_where2||' AND c.object_country = :l_country ';
17527 
17528 		END IF;
17529 
17530 		IF l_area IS NOT NULL THEN
17531 
17535 
17532 			l_where1 := l_where1||' AND c.object_region = :l_area ';
17533 			l_where2 := l_where2||' AND c.object_region = :l_area ';
17534 		END If;
17536 		IF l_col_id = 1 THEN /*Started*/
17537 
17538 			l_where1 := l_where1||'	and c.adj_start_date between &BIS_CURRENT_EFFECTIVE_START_DATE and &BIS_CURRENT_ASOF_DATE ';
17539 			l_where2 := l_where2||'	and c.adj_start_date between &BIS_CURRENT_EFFECTIVE_START_DATE and &BIS_CURRENT_ASOF_DATE ';
17540 
17541 		ELSIF (l_col_id = 2) THEN/*Ended*/
17542 
17543 			l_where1 := l_where1||'	and c.adj_end_date between &BIS_CURRENT_EFFECTIVE_START_DATE and &BIS_CURRENT_ASOF_DATE ';
17544 			l_where2 := l_where2||'	and c.adj_end_date between &BIS_CURRENT_EFFECTIVE_START_DATE and &BIS_CURRENT_ASOF_DATE ';
17545 
17546 		ELSIF (l_col_id = 3) THEN/*Current Active*/
17547 
17548 			l_where1 := l_where1||'	and c.adj_start_date<=&BIS_CURRENT_ASOF_DATE and c.adj_end_date >= &BIS_CURRENT_ASOF_DATE ';
17549 			l_where2 := l_where2||'	and c.adj_start_date<=&BIS_CURRENT_ASOF_DATE and c.adj_end_date >= &BIS_CURRENT_ASOF_DATE ';
17550 
17551 		END IF;
17552 
17553 		l_sqltext:=l_qry||l_qry1||l_where1||l_group_by1||l_qry2||l_where2||l_group_by2;
17554 
17555 	 END IF;
17556 
17557 	  x_custom_sql := l_sqltext;
17558 	  l_custom_rec.attribute_name := ':l_record_type';
17559 	  l_custom_rec.attribute_value := l_record_type_id;
17560 	  l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17561 	  l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17562 	  x_custom_output.EXTEND;
17563 	  x_custom_output(1) := l_custom_rec;
17564 
17565 	  l_custom_rec.attribute_name := ':l_resource_id';
17566 	  l_custom_rec.attribute_value := GET_RESOURCE_ID;
17567 	  l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17568 	  l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17569 	  x_custom_output.EXTEND;
17570 	  x_custom_output(2) := l_custom_rec;
17571 
17572 	  l_custom_rec.attribute_name := ':l_admin_flag';
17573 	  l_custom_rec.attribute_value := GET_ADMIN_STATUS;
17574 	  l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17575 	  l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17576 	  x_custom_output.EXTEND;
17577 	  x_custom_output(3) := l_custom_rec;
17578 
17579 	  l_custom_rec.attribute_name := ':l_country';
17580 	  l_custom_rec.attribute_value := l_country;
17581 	  l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17582 	  l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17583 	  x_custom_output.EXTEND;
17584 	  x_custom_output(4) := l_custom_rec;
17585 
17586 	  l_custom_rec.attribute_name := ':l_cat_id';
17587 	  l_custom_rec.attribute_value := l_cat_id;
17588 	  l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17589 	  l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17590 	  x_custom_output.EXTEND;
17591 	  x_custom_output(5) := l_custom_rec;
17592 
17593 	  l_custom_rec.attribute_name := ':l_area';
17594 	  l_custom_rec.attribute_value := l_area;
17595 	  l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
17596 	  l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
17597 	  x_custom_output.EXTEND;
17598 	  x_custom_output(6) := l_custom_rec;
17599 
17600 EXCEPTION
17601 	WHEN others THEN
17602 		l_sql_errm := SQLERRM;
17603 		write_debug('GET_CAMP_DETL_SQL','ERROR',l_sql_errm,l_sqltext);
17604 
17605 END GET_CAMP_DETL_SQL;
17606 
17607 
17608 
17609 PROCEDURE GET_CSCH_START_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
17610                           x_custom_sql  OUT NOCOPY VARCHAR2,
17611                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
17612  IS
17613 l_sqltext varchar2(15000);
17614 iFlag number;
17615 l_period_type_hc number;
17616 l_as_of_date  DATE;
17617 l_period_type	varchar2(2000);
17618 l_record_type_id NUMBER;
17619 l_comp_type    varchar2(2000);
17620 l_country      varchar2(4000);
17621 l_view_by      varchar2(4000);
17622 l_sql_errm      varchar2(4000);
17623 l_previous_report_start_date DATE;
17624 l_current_report_start_date DATE;
17625 l_previous_as_of_date DATE;
17626 l_period_type_id NUMBER;
17627 l_user_id NUMBER;
17628 l_resource_id NUMBER;
17629 l_time_id_column  VARCHAR2(1000);
17630 l_admin_status VARCHAR2(20);
17631 l_admin_flag VARCHAR2(1);
17632 l_admin_count Number;
17633 l_rsid NUMBER;
17634 l_curr_aod_str varchar2(80);
17635 l_country_clause varchar2(4000);
17636 l_access_clause varchar2(4000);
17637 l_access_table varchar2(4000);
17638 l_cat_id VARCHAR2(50);
17639 l_media VARCHAR2(50);
17640 l_col_id NUMBER;
17641 l_view_by_name VARCHAR2(50);
17642 l_area VARCHAR2(50);
17643 l_campaign_id VARCHAR2(50);
17644 l_custom_rec BIS_QUERY_ATTRIBUTES;
17645 l_dass                 varchar2(100);  -- variable to store value for  directly assigned lookup value
17646 l_una                   varchar2(100);   -- variable to store value for  Unassigned lookup value
17647 l_eve                   varchar2(100);   -- variable to store value for  Events lookup value
17648 l_curr VARCHAR2(50);
17649 l_curr_suffix VARCHAR2(50);
17650 l_report_name VARCHAR2(50);
17651 l_url_str1 VARCHAR2(1000);
17652 l_url_str2 VARCHAR2(1000);
17653 l_url_str3 VARCHAR2(1000);
17654 l_url_str1_mc VARCHAR2(1000);
17655 l_url_str2_mc VARCHAR2(1000);
17656 l_url_str3_mc VARCHAR2(1000);
17657 l_view_name VARCHAR2(100);
17658 l_url_str1_r VARCHAR2(1000);
17659 l_url_str2_r VARCHAR2(1000);
17660 l_url_str3_r VARCHAR2(1000);
17661 
17662 BEGIN
17666    			     			      l_as_of_date,
17663 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
17664 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
17665 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
17667 			    			      l_period_type,
17668 				                      l_record_type_id,
17669 				                      l_comp_type,
17670 				                      l_country,
17671 						      l_view_by,
17672 						      l_cat_id,
17673 						      l_campaign_id,
17674 						      l_curr,
17675 						      l_col_id,
17676 						      l_area,
17677 						      l_media,
17678 						      l_report_name
17679 				                      );
17680 
17681 l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
17682 l_eve := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('EVE');
17683 
17684 IF   l_view_by ='GEOGRAPHY+COUNTRY' then
17685 l_view_name :=L_viewby_c; --'Country'
17686 ELSIF l_view_by = 'ITEM+ENI_ITEM_VBH_CAT' then
17687 l_view_name:=L_viewby_pc ;     --'Product Category'
17688 ELSIF l_view_by ='MEDIA+MEDIA' then
17689  l_view_name :=L_viewby_mc;
17690 ELSIF l_view_by='GEOGRAPHY+AREA' then
17691  l_view_name :=L_viewby_r; --'Region'
17692 END IF;
17693 
17694 l_url_str1:='pFunctionName=BIM_I_CSCH_START_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER5='||l_view_name||' :'||'''||VIEWBY||''&BIM_PARAMETER1=1';
17695 l_url_str2:='pFunctionName=BIM_I_CSCH_START_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER5='||l_view_name||' :'||'''||VIEWBY||''&BIM_PARAMETER1=2';
17696 l_url_str3:='pFunctionName=BIM_I_CSCH_START_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER5='||l_view_name||' :'||'''||VIEWBY||''&BIM_PARAMETER1=3';
17697 
17698 l_url_str1_mc:='pFunctionName=BIM_I_CSCH_START_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER5='||l_view_name||' :'||'''||VIEWBY||''&BIM_PARAMETER1=1&BIM_PARAMETER4=3';
17699 l_url_str2_mc:='pFunctionName=BIM_I_CSCH_START_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER5='||l_view_name||' :'||'''||VIEWBY||''&BIM_PARAMETER1=2&BIM_PARAMETER4=3';
17700 l_url_str3_mc:='pFunctionName=BIM_I_CSCH_START_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER5='||l_view_name||' :'||'''||VIEWBY||''&BIM_PARAMETER1=3&BIM_PARAMETER4=3';
17701 
17702 l_url_str1_r:='pFunctionName=BIM_I_CSCH_START_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER5='||l_view_name||' :'||'''||VIEWBY||''&BIM_PARAMETER1=1&BIM_PARAMETER4=4';
17703 l_url_str2_r:='pFunctionName=BIM_I_CSCH_START_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER5='||l_view_name||' :'||'''||VIEWBY||''&BIM_PARAMETER1=2&BIM_PARAMETER4=4';
17704 l_url_str3_r:='pFunctionName=BIM_I_CSCH_START_DETL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&BIM_PARAMETER5='||l_view_name||' :'||'''||VIEWBY||''&BIM_PARAMETER1=3&BIM_PARAMETER4=4';
17705 
17706    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
17707   /* IF l_country IS NULL THEN
17708       l_country := 'N';
17709    END IF;*/
17710    l_admin_status := GET_ADMIN_STATUS;
17711  IF l_admin_status = 'Y' THEN
17712 if (l_view_by = 'GEOGRAPHY+COUNTRY') then
17713 l_sqltext :=
17714 '
17715 SELECT
17716 VIEWBY,
17717 VIEWBYID,
17718 BIM_ATTRIBUTE2,
17719 BIM_ATTRIBUTE3,
17720 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
17721 BIM_ATTRIBUTE5,
17722 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
17723 BIM_ATTRIBUTE7,
17724 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
17725 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
17726 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
17727 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
17728 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
17729 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
17730 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
17731 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
17732 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
17733 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
17734 null BIM_URL1,
17735 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1||''''||')  BIM_URL2,
17736 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2||''''||')  BIM_URL3,
17737 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3||''''||')  BIM_URL4
17738 FROM
17739 (
17740 SELECT
17741 name VIEWBY,
17742 id VIEWBYID,
17743 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
17744 sum(curr_started) BIM_ATTRIBUTE3,
17745 SUM(prev_started) BIM_ATTRIBUTE4,
17746 sum(curr_ended)  BIM_ATTRIBUTE5,
17747 SUm(prev_ended) BIM_ATTRIBUTE6,
17748 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
17749 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
17750 FROM
17751 (
17752 SELECT
17753 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
17754 a.object_country id,
17755 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
17756 0 prev_prior_active,
17757 0 curr_active,
17758 0 prev_active,
17759 0 curr_started,
17760 0 prev_started,
17761 0 curr_ended,
17762 0 prev_ended,
17763 0 curr_act_ended,
17764 0 prev_act_ended
17765 FROM bim_mkt_kpi_cnt_mv a,
17766 fii_time_rpt_struct_v cal,
17767 bis_countries_v d';
17768  IF l_cat_id is not null then
17769   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
17770   end if;
17774 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
17771   l_sqltext := l_sqltext ||
17772 ' WHERE a.time_id = cal.time_id
17773 AND a.period_type_id = cal.period_type_id
17775 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
17776 AND cal.calendar_id=-1
17777 AND  a.object_country =d.country_code (+)';
17778 IF l_cat_id is null then
17779 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
17780 else
17781   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
17782 			     AND edh.object_type = ''CATEGORY_SET''
17783 			     AND edh.object_id = mdc.category_set_id
17784 			     AND mdc.functional_area_id = 11
17785 			     AND edh.dbi_flag = ''Y''
17786 			     AND edh.parent_id = :l_cat_id';
17787   end if;
17788  IF l_country = 'N' THEN
17789      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
17790  ELSE
17791   l_sqltext := l_sqltext ||
17792 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
17793  END IF;
17794 /*l_sqltext := l_sqltext ||
17795 --' AND a.object_country = :l_country
17796 'group by object_country*/
17797 l_sqltext :=l_sqltext ||
17798 ' UNION ALL
17799 SELECT
17800 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
17801 a.object_country id,
17802 0 curr_prior_active,
17803 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
17804 0 curr_active,
17805 0 prev_active,
17806 0 curr_started,
17807 0 prev_started,
17808 0 curr_ended,
17809 0 prev_ended,
17810 0 curr_act_ended,
17811 0 prev_act_ended
17812 FROM bim_mkt_kpi_cnt_mv a,
17813 fii_time_rpt_struct_v cal,
17814 bis_countries_v d';
17815  IF l_cat_id is not null then
17816   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
17817   end if;
17818   l_sqltext := l_sqltext ||
17819 ' WHERE a.time_id = cal.time_id
17820 AND a.period_type_id = cal.period_type_id
17821 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
17822 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
17823 AND cal.calendar_id=-1
17824 AND  a.object_country =d.country_code (+)';
17825 IF l_cat_id is null then
17826 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
17827 else
17828   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
17829 			     AND edh.object_type = ''CATEGORY_SET''
17830 			     AND edh.object_id = mdc.category_set_id
17831 			     AND mdc.functional_area_id = 11
17832 			     AND edh.dbi_flag = ''Y''
17833 			     AND edh.parent_id = :l_cat_id ';
17834   end if;
17835  IF l_country = 'N' THEN
17836      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
17837  ELSE
17838   l_sqltext := l_sqltext ||
17839 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
17840  END IF;
17841 l_sqltext := l_sqltext ||
17842 ' UNION ALL
17843 SELECT
17844 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
17845 a.object_country id,
17846 0 curr_prior_active,
17847 0 prev_prior_active,
17848 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
17849 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
17850 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
17851 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
17852 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
17853 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
17854 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
17855 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
17856 FROM bim_mkt_kpi_cnt_mv a,
17857 fii_time_rpt_struct_v cal,
17858 bis_countries_v d';
17859  IF l_cat_id is not null then
17860   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
17861   end if;
17862   l_sqltext := l_sqltext ||
17863 ' WHERE a.time_id = cal.time_id
17864 AND a.period_type_id = cal.period_type_id
17865 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
17866 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
17867 AND cal.calendar_id=-1
17868 AND  a.object_country =d.country_code (+)';
17869 IF l_cat_id is null then
17870 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
17871 else
17872   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
17873 			     AND edh.object_type = ''CATEGORY_SET''
17874 			     AND edh.object_id = mdc.category_set_id
17875 			     AND mdc.functional_area_id = 11
17876 			     AND edh.dbi_flag = ''Y''
17877 			     AND edh.parent_id = :l_cat_id ';
17878   end if;
17879 IF l_country = 'N' THEN
17880      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
17881  ELSE
17882   l_sqltext := l_sqltext ||
17886 l_sqltext :=
17883 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
17884  END IF;
17885 ELSIF (l_view_by = 'GEOGRAPHY+AREA') then
17887 '
17888 SELECT
17889 VIEWBY,
17890 VIEWBYID,
17891 BIM_ATTRIBUTE2,
17892 BIM_ATTRIBUTE3,
17893 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
17894 BIM_ATTRIBUTE5,
17895 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
17896 BIM_ATTRIBUTE7,
17897 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
17898 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
17899 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
17900 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
17901 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
17902 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
17903 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
17904 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
17905 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
17906 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
17907 null BIM_URL1,
17908 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1_r||''''||')  BIM_URL2,
17909 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2_r||''''||')  BIM_URL3,
17910 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3_r||''''||')  BIM_URL4
17911 FROM
17912 (
17913 SELECT
17914 name VIEWBY,
17915 id VIEWBYID,
17916 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
17917 sum(curr_started) BIM_ATTRIBUTE3,
17918 SUM(prev_started) BIM_ATTRIBUTE4,
17919 sum(curr_ended)  BIM_ATTRIBUTE5,
17920 SUm(prev_ended) BIM_ATTRIBUTE6,
17921 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
17922 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
17923 FROM
17924 (
17925 SELECT
17926 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
17927 a.object_region id,
17928 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
17929 0 prev_prior_active,
17930 0 curr_active,
17931 0 prev_active,
17932 0 curr_started,
17933 0 prev_started,
17934 0 curr_ended,
17935 0 prev_ended,
17936 0 curr_act_ended,
17937 0 prev_act_ended
17938 FROM bim_mkt_regn_mv a,
17939 fii_time_rpt_struct_v cal,
17940 bis_areas_v d';
17941  IF l_cat_id is not null then
17942   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
17943   end if;
17944   l_sqltext := l_sqltext ||
17945 ' WHERE a.time_id = cal.time_id
17946 AND a.period_type_id = cal.period_type_id
17947 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
17948 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
17949 AND cal.calendar_id=-1
17950 AND  a.object_region =d.id(+)
17951 AND a.object_country = :l_country ';
17952 IF l_cat_id is null then
17953 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
17954 else
17955   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
17956 			     AND edh.object_type = ''CATEGORY_SET''
17957 			     AND edh.object_id = mdc.category_set_id
17958 			     AND mdc.functional_area_id = 11
17959 			     AND edh.dbi_flag = ''Y''
17960 			     AND edh.parent_id = :l_cat_id';
17961   end if;
17962  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
17963 l_sqltext :=l_sqltext ||
17964 ' UNION ALL
17965 SELECT
17966 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
17967 a.object_region id,
17968 0 curr_prior_active,
17969 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
17970 0 curr_active,
17971 0 prev_active,
17972 0 curr_started,
17973 0 prev_started,
17974 0 curr_ended,
17975 0 prev_ended,
17976 0 curr_act_ended,
17977 0 prev_act_ended
17978 FROM bim_mkt_regn_mv a,
17979 fii_time_rpt_struct_v cal,
17980 bis_areas_v d';
17981  IF l_cat_id is not null then
17982   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
17983   end if;
17984   l_sqltext := l_sqltext ||
17985 ' WHERE a.time_id = cal.time_id
17986 AND a.period_type_id = cal.period_type_id
17987 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
17988 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
17989 AND cal.calendar_id=-1
17990 AND  a.object_region = d.id(+)
17991 AND a.object_country =:l_country ';
17992 IF l_cat_id is null then
17993 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
17994 else
17995   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
17996 			     AND edh.object_type = ''CATEGORY_SET''
17997 			     AND edh.object_id = mdc.category_set_id
17998 			     AND mdc.functional_area_id = 11
17999 			     AND edh.dbi_flag = ''Y''
18000 			     AND edh.parent_id = :l_cat_id ';
18001   end if;
18002  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
18003 l_sqltext := l_sqltext ||
18004 ' UNION ALL
18005 SELECT
18009 0 prev_prior_active,
18006 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
18007 a.object_region id,
18008 0 curr_prior_active,
18010 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
18011 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
18012 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
18013 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
18014 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
18015 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
18016 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
18017 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
18018 FROM bim_mkt_regn_mv a,
18019 fii_time_rpt_struct_v cal,
18020 bis_areas_v d';
18021  IF l_cat_id is not null then
18022   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc';
18023   end if;
18024   l_sqltext := l_sqltext ||
18025 ' WHERE a.time_id = cal.time_id
18026 AND a.period_type_id = cal.period_type_id
18027 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
18028 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
18029 AND cal.calendar_id=-1
18030 AND  a.object_region =d.id(+)
18031 AND a.object_country = :l_country ';
18032 IF l_cat_id is null then
18033 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
18034 else
18035   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
18036 			     AND edh.object_type = ''CATEGORY_SET''
18037 			     AND edh.object_id = mdc.category_set_id
18038 			     AND mdc.functional_area_id = 11
18039 			     AND edh.dbi_flag = ''Y''
18040 			     AND edh.parent_id = :l_cat_id ';
18041   end if;
18042   l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region) group by name,id )';
18043 -- View By Marketing Channel
18044 ELSIF (l_view_by = 'MEDIA+MEDIA') then
18045 l_sqltext :=
18046 '
18047  SELECT
18048 VIEWBY,
18049 VIEWBYID,
18050 BIM_ATTRIBUTE2,
18051 BIM_ATTRIBUTE3,
18052 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
18053 BIM_ATTRIBUTE5,
18054 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
18055 BIM_ATTRIBUTE7,
18056 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
18057 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
18058 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
18059 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
18060 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
18061 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
18062 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
18063 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
18064 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
18065 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
18066 null BIM_URL1,
18067 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1_mc||''''||')  BIM_URL2,
18068 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2_mc||''''||')  BIM_URL3,
18069 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3_mc||''''||')  BIM_URL4
18070 FROM
18071 (
18072 SELECT
18073 name VIEWBY,
18074 id VIEWBYID,
18075 sum(nvl(curr_prior_active,0)) BIM_ATTRIBUTE2,
18076 sum(nvl(curr_started,0)) BIM_ATTRIBUTE3,
18077 SUM(nvl(prev_started,0)) BIM_ATTRIBUTE4,
18078 sum(nvl(curr_ended,0))  BIM_ATTRIBUTE5,
18079 SUm(nvl(prev_ended,0)) BIM_ATTRIBUTE6,
18080 sum(nvl(curr_prior_active,0))+sum(nvl(curr_started,0))-sum(nvl(curr_act_ended,0)) BIM_ATTRIBUTE7,
18081 sum(nvl(prev_prior_active,0))+sum(nvl(prev_started,0))-sum(nvl(prev_act_ended,0)) BIM_ATTRIBUTE8
18082 FROM
18083 (
18084 SELECT
18085 decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name) name,
18086 media_id id,
18087 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
18088 0 prev_prior_active,
18089 0 curr_active,
18090 0 prev_active,
18091 0 curr_started,
18092 0 prev_started,
18093 0 curr_ended,
18094 0 prev_ended,
18095 0 curr_act_ended,
18096 0 prev_act_ended
18097 FROM ams_media_tl d ,
18098     fii_time_rpt_struct_v cal  ,
18099     bim_mkt_chnl_mv a ';
18100  IF l_cat_id is not null then
18101   l_sqltext := l_sqltext ||' ,eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
18102   end if;
18103   l_sqltext := l_sqltext ||
18104 ' WHERE a.time_id = cal.time_id
18105 AND a.period_type_id = cal.period_type_id
18106 AND  d.media_id(+) = a.activity_id
18107 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18108 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
18109 AND cal.calendar_id=-1
18110 AND d.language(+)=USERENV(''LANG'')
18111 AND a.object_country = :l_country  ';
18115   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
18112 IF l_cat_id is null then
18113 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
18114 else
18116 			   AND edh.object_type = ''CATEGORY_SET''
18117 			   AND edh.object_id = mdc.category_set_id
18118 			   AND mdc.functional_area_id = 11
18119 			   AND edh.dbi_flag = ''Y''
18120 			   AND edh.parent_id = :l_cat_id  ';
18121   end if;
18122  l_sqltext := l_sqltext ||' group by media_id,decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name)';
18123 
18124 l_sqltext :=l_sqltext ||
18125 ' UNION ALL
18126 SELECT
18127 decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name) name,
18128 media_id id,
18129 0 curr_prior_active,
18130 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
18131 0 curr_active,
18132 0 prev_active,
18133 0 curr_started,
18134 0 prev_started,
18135 0 curr_ended,
18136 0 prev_ended,
18137 0 curr_act_ended,
18138 0 prev_act_ended
18139 FROM ams_media_tl d ,
18140     fii_time_rpt_struct_v cal  ,
18141     bim_mkt_chnl_mv a ';
18142  IF l_cat_id is not null then
18143   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
18144  end if;
18145   l_sqltext := l_sqltext ||
18146 ' WHERE a.time_id = cal.time_id
18147 AND  d.media_id(+) = a.activity_id
18148 AND a.period_type_id = cal.period_type_id
18149 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18150 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
18151 AND cal.calendar_id=-1
18152 AND d.language(+)=USERENV(''LANG'')
18153 AND a.object_country = :l_country ';
18154 IF l_cat_id is null then
18155 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
18156 else
18157   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
18158 			     AND edh.object_type = ''CATEGORY_SET''
18159 			     AND edh.object_id = mdc.category_set_id
18160 			     AND mdc.functional_area_id = 11
18161 			     AND edh.dbi_flag = ''Y''
18162 			     AND edh.parent_id = :l_cat_id  ';
18163   end if;
18164   l_sqltext := l_sqltext ||' group by media_id,decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name)';
18165   l_sqltext := l_sqltext ||
18166 ' UNION ALL
18167 SELECT
18168 decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name) name,
18169 media_id id,
18170 0 curr_prior_active,
18171 0 prev_prior_active,
18172 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
18173 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
18174 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
18175 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
18176 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
18177 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
18178 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
18179 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
18180 FROM ams_media_tl d ,
18181     fii_time_rpt_struct_v cal  ,
18182     bim_mkt_chnl_mv a ';
18183  IF l_cat_id is not null then
18184   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
18185  end if;
18186   l_sqltext := l_sqltext ||
18187 ' WHERE a.time_id = cal.time_id
18188 AND  d.media_id(+) = a.activity_id
18189 AND a.period_type_id = cal.period_type_id
18190 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
18191 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
18192 AND cal.calendar_id=-1
18193 AND d.language(+)=USERENV(''LANG'')
18194 AND a.object_country = :l_country ';
18195 IF l_cat_id is null then
18196 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
18197 else
18198   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
18199 			     AND edh.object_type = ''CATEGORY_SET''
18200 			     AND edh.object_id = mdc.category_set_id
18201 			     AND mdc.functional_area_id = 11
18202 			     AND edh.dbi_flag = ''Y''
18203 			     AND edh.parent_id = :l_cat_id ';
18204   end if;
18205      l_sqltext := l_sqltext ||' group by media_id,decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name) ) group by name,id )';
18206 
18207  --- View By Product Category for admin
18208 ELSE
18209 -- for Product Category  All
18210 IF l_cat_id is null then
18211 l_sqltext :=
18212 '
18213 SELECT
18214 VIEWBY,
18215 VIEWBYID,
18216 BIM_ATTRIBUTE2,
18217 BIM_ATTRIBUTE3,
18218 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
18219 BIM_ATTRIBUTE5,
18220 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
18221 BIM_ATTRIBUTE7,
18222 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
18223 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
18224 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
18225 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
18226 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
18227 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
18231 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
18228 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
18229 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
18230 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
18232 decode(viewbyid,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_CSCH_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
18233 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1||''''||')  BIM_URL2,
18234 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2||''''||')  BIM_URL3,
18235 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3||''''||')  BIM_URL4
18236 FROM
18237 (
18238 SELECT
18239 name VIEWBY,
18240 id VIEWBYID,
18241 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
18242 sum(curr_started) BIM_ATTRIBUTE3,
18243 SUM(prev_started) BIM_ATTRIBUTE4,
18244 sum(curr_ended)  BIM_ATTRIBUTE5,
18245 SUm(prev_ended) BIM_ATTRIBUTE6,
18246 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
18247 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
18248 FROM
18249 (
18250 SELECT
18251 p.value name,
18252 p.parent_id id,
18253 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
18254 0 prev_prior_active,
18255 0 curr_active,
18256 0 prev_active,
18257 0 curr_started,
18258 0 prev_started,
18259 0 curr_ended,
18260 0 prev_ended,
18261 0 curr_act_ended,
18262 0 prev_act_ended
18263 FROM bim_mkt_kpi_cnt_mv a,
18264      fii_time_rpt_struct_v cal,
18265      eni_denorm_hierarchies b,
18266      mtl_default_category_sets mdcs,
18267     (select e.parent_id parent_id ,e.value value
18268       from eni_item_vbh_nodes_v e
18269       where
18270       e.top_node_flag=''Y''
18271       AND e.child_id = e.parent_id
18272       ) p
18273 WHERE
18274      a.time_id = cal.time_id
18275 AND  a.period_type_id = cal.period_type_id
18276 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18277 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
18278 AND cal.calendar_id=-1
18279 AND a.object_country = :l_country
18280 AND  a.category_id = b.child_id
18281 AND b.object_type = ''CATEGORY_SET''
18282 AND b.object_id = mdcs.category_set_id
18283 AND mdcs.functional_area_id = 11
18284 AND b.dbi_flag = ''Y''
18285 AND p.parent_id = b.parent_id';
18286 IF l_cat_id is not null
18287 then
18288 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
18289 end if;
18290 l_sqltext := l_sqltext ||' group by p.value,p.parent_id';
18291 l_sqltext :=l_sqltext ||
18292 ' UNION ALL
18293 SELECT
18294 p.value name,
18295 p.parent_id id,
18296 0 curr_prior_active,
18297 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
18298 0 curr_active,
18299 0 prev_active,
18300 0 curr_started,
18301 0 prev_started,
18302 0 curr_ended,
18303 0 prev_ended,
18304 0 curr_act_ended,
18305 0 prev_act_ended
18306 FROM bim_mkt_kpi_cnt_mv a,
18307      fii_time_rpt_struct_v cal,
18308      eni_denorm_hierarchies b,
18309      mtl_default_category_sets mdcs,
18310     (select e.parent_id parent_id ,e.value value
18311       from eni_item_vbh_nodes_v e
18312       where
18313       e.top_node_flag=''Y''
18314       AND e.child_id = e.parent_id
18315       ) p
18316 WHERE
18317      a.time_id = cal.time_id
18318 AND  a.period_type_id = cal.period_type_id
18319 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18320 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
18321 AND cal.calendar_id=-1
18322 AND a.object_country = :l_country
18323 AND  a.category_id = b.child_id
18324 AND b.object_type = ''CATEGORY_SET''
18325 AND b.object_id = mdcs.category_set_id
18326 AND mdcs.functional_area_id = 11
18327 AND b.dbi_flag = ''Y''
18328 AND p.parent_id = b.parent_id ';
18329 IF l_cat_id is not null
18330 then
18331 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
18332 end if;
18333 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
18334  UNION ALL
18335 SELECT
18336 p.value name,
18337 p.parent_id id,
18338 0 curr_prior_active,
18339 0 prev_prior_active,
18340 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
18341 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
18342 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
18343 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
18344 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
18345 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
18346 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
18347 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
18348 FROM bim_mkt_kpi_cnt_mv a,
18349      fii_time_rpt_struct_v cal,
18350      eni_denorm_hierarchies b,
18351      mtl_default_category_sets mdcs,
18352      (select e.parent_id parent_id ,e.value value
18353       from eni_item_vbh_nodes_v e
18354       where
18355       e.top_node_flag=''Y''
18356       AND e.child_id = e.parent_id
18357       ) p
18358 WHERE
18359      a.time_id = cal.time_id
18360 AND  a.period_type_id = cal.period_type_id
18364 AND a.object_country = :l_country
18361 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
18362 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
18363 AND cal.calendar_id=-1
18365 AND  a.category_id = b.child_id
18366     AND b.object_type = ''CATEGORY_SET''
18367     AND b.object_id = mdcs.category_set_id
18368     AND mdcs.functional_area_id = 11
18369     AND b.dbi_flag = ''Y''
18370     AND p.parent_id = b.parent_id ';
18371 IF l_cat_id is not null
18372 then
18373 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
18374 end if;
18375 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
18376 ) group by name,id )';
18377 ELSE
18378 -- for product category not equal to all
18379 -- current bookmark
18380 l_sqltext :=
18381 '
18382 SELECT
18383 VIEWBY,
18384 VIEWBYID,
18385 BIM_ATTRIBUTE2,
18386 BIM_ATTRIBUTE3,
18387 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
18388 BIM_ATTRIBUTE5,
18389 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
18390 BIM_ATTRIBUTE7,
18391 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
18392 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
18393 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
18394 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
18395 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
18396 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
18397 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
18398 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
18399 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
18400 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
18401 decode(viewbyid,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_CSCH_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
18402 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1||''''||')  BIM_URL2,
18403 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2||''''||')  BIM_URL3,
18404 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3||''''||')  BIM_URL4
18405 FROM
18406 (
18407 SELECT
18408 name VIEWBY,
18409 id VIEWBYID,
18410 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
18411 sum(curr_started) BIM_ATTRIBUTE3,
18412 SUM(prev_started) BIM_ATTRIBUTE4,
18413 sum(curr_ended)  BIM_ATTRIBUTE5,
18414 SUm(prev_ended) BIM_ATTRIBUTE6,
18415 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
18416 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
18417 FROM
18418 (
18419 SELECT
18420 p.value name,
18421 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) id,
18422 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
18423 0 prev_prior_active,
18424 0 curr_active,
18425 0 prev_active,
18426 0 curr_started,
18427 0 prev_started,
18428 0 curr_ended,
18429 0 prev_ended,
18430 0 curr_act_ended,
18431 0 prev_act_ended
18432 FROM bim_mkt_kpi_cnt_mv a,
18433      fii_time_rpt_struct_v cal,
18434      eni_denorm_hierarchies b,
18435      mtl_default_category_sets mdcs,
18436     (select e.id id ,e.value value,leaf_node_flag
18437       from eni_item_vbh_nodes_v e
18438       where e.parent_id =:l_cat_id
18439       AND e.id = e.child_id
18440       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
18441       ) p
18442 WHERE
18443      a.time_id = cal.time_id
18444 AND  a.period_type_id = cal.period_type_id
18445 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18446 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
18447 AND cal.calendar_id=-1
18448 AND a.object_country = :l_country
18449 AND a.category_id = b.child_id
18450 AND b.object_type = ''CATEGORY_SET''
18451 AND b.object_id = mdcs.category_set_id
18452 AND mdcs.functional_area_id = 11
18453 AND b.parent_id = p.id
18454 AND b.dbi_flag = ''Y''';
18455 l_sqltext := l_sqltext ||' group by p.value,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)';
18456 l_sqltext :=l_sqltext ||
18457 ' UNION ALL
18458 SELECT
18459 p.value name,
18460 -1 id,
18461 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
18462 0 prev_prior_active,
18463 0 curr_active,
18464 0 prev_active,
18465 0 curr_started,
18466 0 prev_started,
18467 0 curr_ended,
18468 0 prev_ended,
18469 0 curr_act_ended,
18470 0 prev_act_ended
18471 FROM bim_mkt_kpi_cnt_mv a,
18472      fii_time_rpt_struct_v cal,
18473     (select e.id id ,e.value value
18474       from eni_item_vbh_nodes_v e
18475       where e.parent_id =  :l_cat_id
18476       AND e.parent_id = e.child_id
18477       AND leaf_node_flag <> ''Y''
18478       ) p
18479 WHERE
18480      a.time_id = cal.time_id
18481 AND  a.period_type_id = cal.period_type_id
18482 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18483 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
18484 AND cal.calendar_id=-1
18485 AND a.object_country = :l_country
18486 AND a.category_id = p.id';
18487 /*IF l_cat_id is not null
18488 then
18489 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
18490 end if;*/
18491 l_sqltext := l_sqltext ||' group by p.value
18492 UNION ALL
18493 SELECT
18494 p.value name,
18495 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) id,
18496 0 curr_prior_active,
18500 0 curr_started,
18497 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
18498 0 curr_active,
18499 0 prev_active,
18501 0 prev_started,
18502 0 curr_ended,
18503 0 prev_ended,
18504 0 curr_act_ended,
18505 0 prev_act_ended
18506 FROM bim_mkt_kpi_cnt_mv a,
18507      fii_time_rpt_struct_v cal,
18508      eni_denorm_hierarchies b,
18509      mtl_default_category_sets mdcs,
18510     (select e.id id ,e.value value,leaf_node_flag
18511       from eni_item_vbh_nodes_v e
18512       where e.parent_id =:l_cat_id
18513       AND e.id = e.child_id
18514       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'') ) p
18515 WHERE a.time_id = cal.time_id
18516 AND  a.period_type_id = cal.period_type_id
18517 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18518 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
18519 AND cal.calendar_id=-1
18520 AND a.object_country = :l_country
18521 AND a.category_id = b.child_id
18522 AND b.object_type = ''CATEGORY_SET''
18523 AND b.object_id = mdcs.category_set_id
18524 AND mdcs.functional_area_id = 11
18525 AND b.dbi_flag =''Y''
18526 AND b.parent_id = p.id ';
18527 /*IF l_cat_id is not null
18528 then
18529 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
18530 end if;*/
18531 l_sqltext := l_sqltext ||' group by p.value,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)
18532 UNION ALL
18533 SELECT
18534 p.value name,
18535 -1 id,
18536 0 curr_prior_active,
18537 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
18538 0 curr_active,
18539 0 prev_active,
18540 0 curr_started,
18541 0 prev_started,
18542 0 curr_ended,
18543 0 prev_ended,
18544 0 curr_act_ended,
18545 0 prev_act_ended
18546 FROM bim_mkt_kpi_cnt_mv a,
18547      fii_time_rpt_struct_v cal,
18548     (select e.id id ,e.value value
18549       from eni_item_vbh_nodes_v e
18550       where e.parent_id =  :l_cat_id
18551       AND e.parent_id = e.child_id
18552       AND leaf_node_flag <> ''Y''
18553       ) p
18554 WHERE
18555      a.time_id = cal.time_id
18556 AND  a.period_type_id = cal.period_type_id
18557 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18558 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
18559 AND cal.calendar_id=-1
18560 AND a.object_country = :l_country
18561 AND a.category_id = p.id
18562 ';
18563 /*IF l_cat_id is not null
18564 then
18565 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
18566 end if;*/
18567 l_sqltext := l_sqltext ||' group by p.value
18568  UNION ALL
18569 SELECT
18570 p.value name,
18571 decode(p.leaf_node_flag,''Y'',-1,b.parent_id) id,
18572 0 curr_prior_active,
18573 0 prev_prior_active,
18574 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
18575 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
18576 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
18577 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
18578 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
18579 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
18580 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
18581 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
18582 FROM bim_mkt_kpi_cnt_mv a,
18583      fii_time_rpt_struct_v cal,
18584      eni_denorm_hierarchies b,
18585      mtl_default_category_sets mdcs,
18586      (select e.id id ,e.value value,leaf_node_flag
18587       from eni_item_vbh_nodes_v e
18588       where e.parent_id =:l_cat_id
18589       AND e.id = e.child_id
18590       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')) p
18591 WHERE
18592      a.time_id = cal.time_id
18593 AND  a.period_type_id = cal.period_type_id
18594 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
18595 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
18596 AND cal.calendar_id=-1
18597 AND a.object_country = :l_country
18598 AND a.category_id = b.child_id
18599 AND b.object_type = ''CATEGORY_SET''
18600 AND b.object_id = mdcs.category_set_id
18601 AND mdcs.functional_area_id = 11
18602 AND b.dbi_flag = ''Y''
18603 AND b.parent_id = p.id ';
18604 /*IF l_cat_id is not null
18605 then
18606 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
18607 end if;*/
18608 l_sqltext := l_sqltext ||' group by p.value,decode(p.leaf_node_flag,''Y'',-1,b.parent_id)
18609 UNION ALL
18610 SELECT
18611 p.value name,
18612 -1 id,
18613 0 curr_prior_active,
18614 0 prev_prior_active,
18615 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
18616 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
18617 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
18618 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
18619 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
18620 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
18624      fii_time_rpt_struct_v cal,
18621 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
18622 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
18623 FROM bim_mkt_kpi_cnt_mv a,
18625      (select e.id id ,e.value value
18626       from eni_item_vbh_nodes_v e
18627       where e.parent_id =  :l_cat_id
18628       AND e.parent_id = e.child_id
18629       AND leaf_node_flag <> ''Y''
18630       ) p
18631 WHERE
18632      a.time_id = cal.time_id
18633 AND  a.period_type_id = cal.period_type_id
18634 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
18635 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
18636 AND cal.calendar_id=-1
18637 AND a.object_country = :l_country
18638 AND a.category_id = p.id
18639  group by p.value) group by name,id )';
18640 END IF; -- end product category numm loop
18641  END IF; --end view by product category
18642 ELSE -- if admin_flag is not equal to Y
18643 if (l_view_by = 'GEOGRAPHY+COUNTRY') then
18644 l_sqltext :=
18645 '
18646 SELECT
18647 VIEWBY,
18648 VIEWBYID,
18649 BIM_ATTRIBUTE2,
18650 BIM_ATTRIBUTE3,
18651 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
18652 BIM_ATTRIBUTE5,
18653 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
18654 BIM_ATTRIBUTE7,
18655 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
18656 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
18657 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
18658 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
18659 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
18660 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
18661 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
18662 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
18663 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
18664 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
18665 null BIM_URL1,
18666 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1||''''||')  BIM_URL2,
18667 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2||''''||')  BIM_URL3,
18668 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3||''''||')  BIM_URL4
18669 FROM
18670 (
18671 SELECT
18672 name VIEWBY,
18673 id VIEWBYID,
18674 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
18675 sum(curr_started) BIM_ATTRIBUTE3,
18676 SUM(prev_started) BIM_ATTRIBUTE4,
18677 sum(curr_ended)  BIM_ATTRIBUTE5,
18678 SUm(prev_ended) BIM_ATTRIBUTE6,
18679 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
18680 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
18681 FROM
18682 (
18683 SELECT
18684 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
18685 a.object_country id,
18686 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
18687 0 prev_prior_active,
18688 0 curr_active,
18689 0 prev_active,
18690 0 curr_started,
18691 0 prev_started,
18692 0 curr_ended,
18693 0 prev_ended,
18694 0 curr_act_ended,
18695 0 prev_act_ended
18696 FROM bim_i_obj_mets_mv a,
18697 fii_time_rpt_struct_v cal,';
18698 IF l_prog_view='Y' then
18699   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_countries_v d ';
18700 ELSE
18701 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_countries_v d, bim_i_source_codes s  ';
18702 END IF;
18703  IF l_cat_id is not null then
18704   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
18705   end if;
18706   l_sqltext := l_sqltext ||
18707 ' WHERE a.time_id = cal.time_id
18708 AND a.period_type_id = cal.period_type_id
18709 AND ac.resource_id = :l_resource_id';
18710 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18711 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
18712 AND cal.calendar_id=-1
18713 AND  a.object_country =d.country_code (+) ';
18714 IF l_prog_view='N' then
18715 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
18716 AND a.source_code_id = s.source_code_id
18717 AND s.object_id = ac.object_id
18718 AND s.object_type=ac.object_type
18719 ';
18720 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
18721 END IF;
18722 IF l_cat_id is null then
18723 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
18724 else
18725   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
18726 			     AND edh.object_type = ''CATEGORY_SET''
18727 			     AND edh.object_id = mdc.category_set_id
18728 			     AND mdc.functional_area_id = 11
18729 			     AND edh.dbi_flag = ''Y''
18730 			     AND edh.parent_id = :l_cat_id  ';
18731   end if;
18732  IF l_country = 'N' THEN
18733      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
18734  ELSE
18735   l_sqltext := l_sqltext ||
18736 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
18737  END IF;
18738 l_sqltext :=l_sqltext ||
18739 ' UNION ALL
18740 SELECT
18744 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
18741 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
18742 a.object_country id,
18743 0 curr_prior_active,
18745 0 curr_active,
18746 0 prev_active,
18747 0 curr_started,
18748 0 prev_started,
18749 0 curr_ended,
18750 0 prev_ended,
18751 0 curr_act_ended,
18752 0 prev_act_ended
18753 FROM bim_i_obj_mets_mv a,
18754 fii_time_rpt_struct_v cal,';
18755 IF l_prog_view='Y' then
18756   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_countries_v d ';
18757 ELSE
18758 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_countries_v d, bim_i_source_codes s  ';
18759 END IF;
18760  IF l_cat_id is not null then
18761   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
18762   end if;
18763   l_sqltext := l_sqltext ||
18764 ' WHERE a.time_id = cal.time_id
18765 AND a.period_type_id = cal.period_type_id
18766 AND ac.resource_id = :l_resource_id';
18767 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18768 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
18769 AND cal.calendar_id=-1
18770 AND  a.object_country =d.country_code (+)';
18771 IF l_prog_view='N' then
18772 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
18773 AND a.source_code_id = s.source_code_id
18774 AND s.object_id = ac.object_id
18775 AND s.object_type=ac.object_type
18776 ';
18777 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
18778 END IF;
18779 IF l_cat_id is null then
18780 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
18781 else
18782   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
18783 			   AND edh.object_type = ''CATEGORY_SET''
18784 			   AND edh.object_id = mdc.category_set_id
18785 			   AND mdc.functional_area_id = 11
18786 			   AND edh.dbi_flag = ''Y''
18787 			   AND edh.parent_id = :l_cat_id ';
18788   end if;
18789  IF l_country = 'N' THEN
18790      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
18791  ELSE
18792   l_sqltext := l_sqltext ||
18793 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country';
18794  END IF;
18795 l_sqltext := l_sqltext ||
18796 ' UNION ALL
18797 SELECT
18798 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
18799 a.object_country id,
18800 0 curr_prior_active,
18801 0 prev_prior_active,
18802 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
18803 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
18804 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
18805 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
18806 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
18807 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
18808 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
18809 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
18810 FROM bim_i_obj_mets_mv a,
18811 fii_time_rpt_struct_v cal,';
18812 IF l_prog_view='Y' then
18813   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_countries_v d ';
18814 ELSE
18815 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_countries_v d, bim_i_source_codes s  ';
18816 END IF;
18817  IF l_cat_id is not null then
18818   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
18819   end if;
18820   l_sqltext := l_sqltext ||
18821 ' WHERE a.time_id = cal.time_id
18822 AND a.period_type_id = cal.period_type_id
18823 AND ac.resource_id = :l_resource_id';
18824 l_sqltext := l_sqltext||' AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
18825 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
18826 AND cal.calendar_id=-1
18827 AND  a.object_country =d.country_code (+)';
18828 IF l_prog_view='N' then
18829 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
18830 AND a.source_code_id = s.source_code_id
18831 AND s.object_id = ac.object_id
18832 AND s.object_type=ac.object_type
18833 ';
18834 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
18835 END IF;
18836 IF l_cat_id is null then
18837 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
18838 else
18839   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
18840 			   AND edh.object_type = ''CATEGORY_SET''
18841 			   AND edh.object_id = mdc.category_set_id
18842 			   AND mdc.functional_area_id = 11
18843 			   AND edh.dbi_flag = ''Y''
18844 			   AND edh.parent_id = :l_cat_id ';
18845   end if;
18846 IF l_country = 'N' THEN
18847      l_sqltext := l_sqltext ||' AND a.object_country <> ''N'' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
18848  ELSE
18849   l_sqltext := l_sqltext ||
18850 ' AND a.object_country = :l_country group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_country ) group by name,id )';
18851  END IF;
18855 SELECT
18852 ELSIF (l_view_by = 'GEOGRAPHY+AREA') then
18853 l_sqltext :=
18854 '
18856 VIEWBY,
18857 VIEWBYID,
18858 BIM_ATTRIBUTE2,
18859 BIM_ATTRIBUTE3,
18860 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
18861 BIM_ATTRIBUTE5,
18862 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
18863 BIM_ATTRIBUTE7,
18864 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
18865 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
18866 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
18867 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
18868 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
18869 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
18870 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
18871 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
18872 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
18873 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
18874 null BIM_URL1,
18875 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1_r||''''||')  BIM_URL2,
18876 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2_r||''''||')  BIM_URL3,
18877 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3_r||''''||')  BIM_URL4
18878 FROM
18879 (
18880 SELECT
18881 name VIEWBY,
18882 id VIEWBYID,
18883 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
18884 sum(curr_started) BIM_ATTRIBUTE3,
18885 SUM(prev_started) BIM_ATTRIBUTE4,
18886 sum(curr_ended)  BIM_ATTRIBUTE5,
18887 SUm(prev_ended) BIM_ATTRIBUTE6,
18888 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
18889 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
18890 FROM
18891 (
18892 SELECT
18893 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
18894 a.object_region id,
18895 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
18896 0 prev_prior_active,
18897 0 curr_active,
18898 0 prev_active,
18899 0 curr_started,
18900 0 prev_started,
18901 0 curr_ended,
18902 0 prev_ended,
18903 0 curr_act_ended,
18904 0 prev_act_ended
18905 FROM bim_obj_regn_mv a,
18906 fii_time_rpt_struct_v cal,';
18907 IF l_prog_view='Y' then
18908   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_areas_v d ';
18909 ELSE
18910 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_areas_v d, bim_i_source_codes s  ';
18911 END IF;
18912  IF l_cat_id is not null then
18913   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
18914   end if;
18915   l_sqltext := l_sqltext ||
18916 ' WHERE a.time_id = cal.time_id
18917 AND a.period_type_id = cal.period_type_id
18918 AND ac.resource_id = :l_resource_id';
18919 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18920 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
18921 AND cal.calendar_id=-1
18922 AND  a.object_region =d.id(+)
18923 AND a.object_country = :l_country ';
18924 IF l_prog_view='N' then
18925 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
18926 AND a.source_code_id = s.source_code_id
18927 AND s.object_id = ac.object_id
18928 AND s.object_type=ac.object_type
18929 ';
18930 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
18931 END IF;
18932 IF l_cat_id is null then
18933 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
18934 else
18935   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
18936 			     AND edh.object_type = ''CATEGORY_SET''
18937 			     AND edh.object_id = mdc.category_set_id
18938 			     AND mdc.functional_area_id = 11
18939 			     AND edh.dbi_flag = ''Y''
18940 			     AND edh.parent_id = :l_cat_id  ';
18941   end if;
18942  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
18943 l_sqltext :=l_sqltext ||
18944 ' UNION ALL
18945 SELECT
18946 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
18947 a.object_region id,
18948 0 curr_prior_active,
18949 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
18950 0 curr_active,
18951 0 prev_active,
18952 0 curr_started,
18953 0 prev_started,
18954 0 curr_ended,
18955 0 prev_ended,
18956 0 curr_act_ended,
18957 0 prev_act_ended
18958 FROM bim_obj_regn_mv a,
18959 fii_time_rpt_struct_v cal,';
18960 IF l_prog_view='Y' then
18961   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_areas_v d ';
18962 ELSE
18963 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_areas_v d, bim_i_source_codes s  ';
18964 END IF;
18965 IF l_cat_id is not null then
18966   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
18967   end if;
18968   l_sqltext := l_sqltext ||
18969 ' WHERE a.time_id = cal.time_id
18970 AND a.period_type_id = cal.period_type_id
18971 AND ac.resource_id = :l_resource_id';
18972 l_sqltext := l_sqltext ||' AND BITAND(cal.record_type_id,1143)=cal.record_type_id
18973 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
18974 AND cal.calendar_id=-1
18975 AND  a.object_region =d.id(+)
18976 AND a.object_country = :l_country ';
18977 IF l_prog_view='N' then
18978 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
18979 AND a.source_code_id = s.source_code_id
18980 AND s.object_id = ac.object_id
18981 AND s.object_type=ac.object_type
18985 IF l_cat_id is null then
18982 ';
18983 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
18984 END IF;
18986 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
18987 else
18988   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
18989 			     AND edh.object_type = ''CATEGORY_SET''
18990 			     AND edh.object_id = mdc.category_set_id
18991 			     AND mdc.functional_area_id = 11
18992 			     AND edh.dbi_flag = ''Y''
18993 			     AND edh.parent_id = :l_cat_id ';
18994   end if;
18995  l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region';
18996  l_sqltext := l_sqltext ||
18997 ' UNION ALL
18998 SELECT
18999 decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
19000 a.object_region id,
19001 0 curr_prior_active,
19002 0 prev_prior_active,
19003 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
19004 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
19005 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
19006 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
19007 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
19008 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
19009 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
19010 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
19011 FROM bim_obj_regn_mv a,
19012 fii_time_rpt_struct_v cal,';
19013 IF l_prog_view='Y' then
19014   l_sqltext := l_sqltext ||' bim_i_top_objects ac,bis_areas_v d ';
19015 ELSE
19016 l_sqltext := l_sqltext ||' ams_act_access_denorm ac,bis_areas_v d, bim_i_source_codes s  ';
19017 END IF;
19018  IF l_cat_id is not null then
19019   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
19020   end if;
19021   l_sqltext := l_sqltext ||
19022 ' WHERE a.time_id = cal.time_id
19023 AND a.period_type_id = cal.period_type_id
19024 AND ac.resource_id = :l_resource_id';
19025 l_sqltext := l_sqltext||' AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
19026 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
19027 AND cal.calendar_id=-1
19028 AND  a.object_region =d.id(+)
19029 AND a.object_country = :l_country ';
19030 IF l_prog_view='N' then
19031 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19032 AND a.source_code_id = s.source_code_id
19033 AND s.object_id = ac.object_id
19034 AND s.object_type=ac.object_type
19035 ';
19036 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19037 END IF;
19038 IF l_cat_id is null then
19039 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
19040 else
19041   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
19042 			     AND edh.object_type = ''CATEGORY_SET''
19043 			     AND edh.object_id = mdc.category_set_id
19044 			     AND mdc.functional_area_id = 11
19045 			     AND edh.dbi_flag = ''Y''
19046 			     AND edh.parent_id = :l_cat_id ';
19047   end if;
19048 l_sqltext := l_sqltext ||' group by decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value),a.object_region ) group by name,id )';
19049 ELSIF (l_view_by = 'MEDIA+MEDIA') then
19050 l_sqltext :=
19051 '
19052  SELECT
19053 VIEWBY,
19054 VIEWBYID,
19055 BIM_ATTRIBUTE2,
19056 BIM_ATTRIBUTE3,
19057 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
19058 BIM_ATTRIBUTE5,
19059 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
19060 BIM_ATTRIBUTE7,
19061 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
19062 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
19063 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
19064 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
19065 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
19066 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
19067 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
19068 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
19069 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
19070 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
19071 null BIM_URL1,
19072 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1_mc||''''||')  BIM_URL2,
19073 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2_mc||''''||')  BIM_URL3,
19074 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3_mc||''''||')  BIM_URL4
19075 FROM
19076 (
19077 SELECT
19078 name VIEWBY,
19079 id VIEWBYID,
19080 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
19081 sum(curr_started) BIM_ATTRIBUTE3,
19082 SUM(prev_started) BIM_ATTRIBUTE4,
19083 sum(curr_ended)  BIM_ATTRIBUTE5,
19084 SUm(prev_ended) BIM_ATTRIBUTE6,
19085 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
19086 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
19087 FROM
19088 (
19089 SELECT
19093 0 prev_prior_active,
19090 decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name) name,
19091 media_id id,
19092 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
19094 0 curr_active,
19095 0 prev_active,
19096 0 curr_started,
19097 0 prev_started,
19098 0 curr_ended,
19099 0 prev_ended,
19100 0 curr_act_ended,
19101 0 prev_act_ended
19102 FROM ams_media_tl d ,
19103     fii_time_rpt_struct_v cal  ,
19104     bim_obj_chnl_mv a ,';
19105 IF l_prog_view='Y' then
19106   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19107 ELSE
19108 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19109 END IF;
19110  IF l_cat_id is not null then
19111   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
19112   end if;
19113   l_sqltext := l_sqltext ||
19114 ' WHERE a.time_id = cal.time_id
19115 AND a.period_type_id = cal.period_type_id
19116 AND ac.resource_id = :l_resource_id
19117 AND  d.media_id(+) = a.activity_id
19118 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
19119 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE -1)
19120 AND cal.calendar_id=-1
19121 AND d.language(+)=USERENV(''LANG'')
19122 AND a.object_country = :l_country ';
19123 IF l_prog_view='N' then
19124 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19125 AND a.source_code_id = s.source_code_id
19126 AND s.object_id = ac.object_id
19127 AND s.object_type=ac.object_type
19128 ';
19129 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19130 END IF;
19131 IF l_cat_id is null then
19132 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
19133 else
19134   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
19135 			   AND edh.object_type = ''CATEGORY_SET''
19136 			   AND edh.object_id = mdc.category_set_id
19137 			   AND mdc.functional_area_id = 11
19138 			   AND edh.dbi_flag = ''Y''
19139 			   AND edh.parent_id = :l_cat_id ';
19140   end if;
19141      l_sqltext := l_sqltext ||' group by media_id,decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name)';
19142  l_sqltext :=l_sqltext ||
19143 ' UNION ALL
19144 SELECT
19145 decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name) name,
19146 media_id id,
19147 0 curr_prior_active,
19148 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
19149 0 curr_active,
19150 0 prev_active,
19151 0 curr_started,
19152 0 prev_started,
19153 0 curr_ended,
19154 0 prev_ended,
19155 0 curr_act_ended,
19156 0 prev_act_ended
19157 FROM ams_media_tl d ,
19158     fii_time_rpt_struct_v cal  ,
19159     bim_obj_chnl_mv a ,';
19160 IF l_prog_view='Y' then
19161   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19162 ELSE
19163 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19164 END IF;
19165  IF l_cat_id is not null then
19166   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
19167  end if;
19168   l_sqltext := l_sqltext ||
19169 ' WHERE a.time_id = cal.time_id
19170 AND ac.resource_id = :l_resource_id
19171 AND  d.media_id(+) = a.activity_id
19172 AND a.period_type_id = cal.period_type_id
19173 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
19174 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE -1)
19175 AND cal.calendar_id=-1
19176 AND d.language(+)=USERENV(''LANG'')
19177 AND a.object_country = :l_country ';
19178 IF l_prog_view='N' then
19179 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19180 AND a.source_code_id = s.source_code_id
19181 AND s.object_id = ac.object_id
19182 AND s.object_type=ac.object_type
19183 ';
19184 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19185 END IF;
19186 IF l_cat_id is null then
19187 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
19188 else
19189   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
19190 			   AND edh.object_type = ''CATEGORY_SET''
19194 			   AND edh.parent_id = :l_cat_id ';
19191 			   AND edh.object_id = mdc.category_set_id
19192 			   AND mdc.functional_area_id = 11
19193 			   AND edh.dbi_flag = ''Y''
19195   end if;
19196  l_sqltext := l_sqltext ||' group by media_id,decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name)';
19197 
19198  l_sqltext := l_sqltext ||
19199 ' UNION ALL
19200 SELECT
19201 decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name) name,
19202 media_id id,
19203 0 curr_prior_active,
19204 0 prev_prior_active,
19205 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
19206 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
19207 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
19208 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
19209 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
19210 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
19211 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
19212 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
19213 FROM ams_media_tl d ,
19214     fii_time_rpt_struct_v cal  ,
19215     bim_obj_chnl_mv a ,';
19216 IF l_prog_view='Y' then
19217   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19218 ELSE
19219 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19220 END IF;
19221  IF l_cat_id is not null then
19222   l_sqltext := l_sqltext ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdc ';
19223  end if;
19224   l_sqltext := l_sqltext ||
19225 ' WHERE a.time_id = cal.time_id
19226 AND ac.resource_id = :l_resource_id
19227 AND  d.media_id(+) = a.activity_id
19228 AND a.period_type_id = cal.period_type_id
19229 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
19230 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
19231 AND cal.calendar_id=-1
19232 AND d.language(+)=USERENV(''LANG'')
19233 AND a.object_country = :l_country ';
19234 IF l_prog_view='N' then
19235 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19236 AND a.source_code_id = s.source_code_id
19237 AND s.object_id = ac.object_id
19238 AND s.object_type=ac.object_type
19239 ';
19240 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19241 END IF;
19242 IF l_cat_id is null then
19243 l_sqltext := l_sqltext ||' AND a.category_id = -9 ';
19244 else
19245   l_sqltext := l_sqltext ||' AND a.category_id = edh.child_id
19246 			   AND edh.object_type = ''CATEGORY_SET''
19247 			   AND edh.object_id = mdc.category_set_id
19248 			   AND mdc.functional_area_id = 11
19249 			   AND edh.dbi_flag = ''Y''
19250 			   AND edh.parent_id = :l_cat_id ';
19251   end if;
19255 IF l_cat_id is null THEN
19252   l_sqltext := l_sqltext ||' group by media_id,decode(d.media_name,null,'||''''||l_eve||''''||',d.media_name) ) group by name,id )';
19253  --- View By Product Category for admin
19254 ELSE
19256 l_sqltext :=
19257 '
19258 SELECT
19259 VIEWBY,
19260 VIEWBYID,
19261 BIM_ATTRIBUTE2,
19262 BIM_ATTRIBUTE3,
19263 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
19264 BIM_ATTRIBUTE5,
19265 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
19266 BIM_ATTRIBUTE7,
19267 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
19268 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
19269 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
19270 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
19271 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
19272 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
19273 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
19274 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
19275 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
19276 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
19277 decode(viewbyid,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_CSCH_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
19278 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1||''''||')  BIM_URL2,
19279 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2||''''||')  BIM_URL3,
19280 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3||''''||')  BIM_URL4
19281 FROM
19282 (
19283 SELECT
19284 name VIEWBY,
19285 id VIEWBYID,
19286 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
19287 sum(curr_started) BIM_ATTRIBUTE3,
19288 SUM(prev_started) BIM_ATTRIBUTE4,
19289 sum(curr_ended)  BIM_ATTRIBUTE5,
19290 SUm(prev_ended) BIM_ATTRIBUTE6,
19291 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
19292 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
19293 FROM
19294 (
19295 SELECT
19296 p.value name,
19297 p.parent_id id,
19298 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
19299 0 prev_prior_active,
19300 0 curr_active,
19301 0 prev_active,
19302 0 curr_started,
19303 0 prev_started,
19304 0 curr_ended,
19305 0 prev_ended,
19306 0 curr_act_ended,
19307 0 prev_act_ended
19308 FROM bim_i_obj_mets_mv a,
19309      fii_time_rpt_struct_v cal,
19310      eni_denorm_hierarchies b,
19311      mtl_default_category_sets mdcs,';
19312 IF l_prog_view='Y' then
19313   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19314 ELSE
19315 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19316 END IF;
19317  l_sqltext := l_sqltext ||' ,(select e.parent_id parent_id ,e.value value
19318       from eni_item_vbh_nodes_v e
19319       where
19320       e.top_node_flag=''Y''
19321       AND e.child_id = e.parent_id
19322       ) p
19323 WHERE
19324      a.time_id = cal.time_id
19325 AND  a.period_type_id = cal.period_type_id
19326 AND ac.resource_id = :l_resource_id
19327 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
19328 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
19329 AND cal.calendar_id=-1
19330 AND a.object_country = :l_country
19331 AND  a.category_id = b.child_id
19332 AND b.object_type = ''CATEGORY_SET''
19333 AND b.object_id = mdcs.category_set_id
19334 AND mdcs.functional_area_id = 11
19335 AND b.dbi_flag = ''Y''
19336 AND p.parent_id = b.parent_id';
19337 IF l_prog_view='N' then
19338 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19339 AND a.source_code_id = s.source_code_id
19340 AND s.object_id = ac.object_id
19341 AND s.object_type=ac.object_type
19342 ';
19343 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19344 END IF;
19345 IF l_cat_id is not null
19346 then
19347 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
19348 end if;
19349 l_sqltext := l_sqltext ||' group by p.value,p.parent_id';
19350 l_sqltext :=l_sqltext ||
19351 ' UNION ALL
19352 SELECT
19353 p.value name,
19354 p.parent_id id,
19355 0 curr_prior_active,
19356 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
19357 0 curr_active,
19358 0 prev_active,
19359 0 curr_started,
19360 0 prev_started,
19361 0 curr_ended,
19362 0 prev_ended,
19363 0 curr_act_ended,
19364 0 prev_act_ended
19365 FROM bim_i_obj_mets_mv a,
19366      fii_time_rpt_struct_v cal,
19367      eni_denorm_hierarchies b,
19368      mtl_default_category_sets mdcs,';
19369 IF l_prog_view='Y' then
19370   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19371 ELSE
19372 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19373 END IF;
19374 l_sqltext := l_sqltext||' ,(select e.parent_id parent_id ,e.value value
19375       from eni_item_vbh_nodes_v e
19376       where
19377       e.top_node_flag=''Y''
19378       AND e.child_id = e.parent_id
19379       ) p
19380 WHERE
19381      a.time_id = cal.time_id
19382 AND  a.period_type_id = cal.period_type_id
19386 AND cal.calendar_id=-1
19383 AND ac.resource_id = :l_resource_id
19384 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
19385 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
19387 AND a.object_country = :l_country
19388 AND  a.category_id = b.child_id
19389 AND b.object_type = ''CATEGORY_SET''
19390 AND b.object_id = mdcs.category_set_id
19391 AND mdcs.functional_area_id = 11
19392 AND b.dbi_flag = ''Y''
19393 AND p.parent_id = b.parent_id ';
19394 IF l_prog_view='N' then
19395 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19396 AND a.source_code_id = s.source_code_id
19397 AND s.object_id = ac.object_id
19398 AND s.object_type=ac.object_type
19399 ';
19400 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19401 END IF;
19402 IF l_cat_id is not null
19403 then
19404 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
19405 end if;
19406 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
19407  UNION ALL
19408 SELECT
19409 p.value name,
19410 p.parent_id id,
19411 0 curr_prior_active,
19412 0 prev_prior_active,
19413 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
19414 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
19415 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
19416 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
19417 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
19418 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
19419 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
19420 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
19421 FROM bim_i_obj_mets_mv a,
19422      fii_time_rpt_struct_v cal,
19423      eni_denorm_hierarchies b,
19424      mtl_default_category_sets mdcs,';
19425 IF l_prog_view='Y' then
19426   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19427 ELSE
19428 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19429 END IF;
19430 l_sqltext := l_sqltext||' ,(select e.parent_id parent_id ,e.value value
19431       from eni_item_vbh_nodes_v e
19432       where
19433       e.top_node_flag=''Y''
19434       AND e.child_id = e.parent_id
19435       ) p
19436 WHERE
19437      a.time_id = cal.time_id
19438 AND  a.period_type_id = cal.period_type_id
19439 AND ac.resource_id = :l_resource_id
19440 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
19441 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
19442 AND cal.calendar_id=-1
19443 AND a.object_country = :l_country
19444 AND  a.category_id = b.child_id
19445     AND b.object_type = ''CATEGORY_SET''
19446     AND b.object_id = mdcs.category_set_id
19447     AND mdcs.functional_area_id = 11
19448     AND b.dbi_flag = ''Y''
19449     AND p.parent_id = b.parent_id ';
19450 IF l_prog_view='N' then
19451 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19452 AND a.source_code_id = s.source_code_id
19453 AND s.object_id = ac.object_id
19454 AND s.object_type=ac.object_type
19455 ';
19456 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19457 END IF;
19458 IF l_cat_id is not null
19459 then
19460 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
19461 end if;
19462 l_sqltext := l_sqltext ||' group by p.value,p.parent_id
19463 ) group by name,id )';
19464 ELSE -- product category is not null
19465 l_sqltext :=
19466 'SELECT
19467 VIEWBY,
19468 VIEWBYID,
19469 BIM_ATTRIBUTE2,
19470 BIM_ATTRIBUTE3,
19471 decode(BIM_ATTRIBUTE4,0,null,100*(BIM_ATTRIBUTE3-BIM_ATTRIBUTE4)/BIM_ATTRIBUTE4) BIM_ATTRIBUTE4,
19472 BIM_ATTRIBUTE5,
19473 decode(BIM_ATTRIBUTE6,0,null,100*(BIM_ATTRIBUTE5-BIM_ATTRIBUTE6)/BIM_ATTRIBUTE6) BIM_ATTRIBUTE6,
19474 BIM_ATTRIBUTE7,
19475 decode(BIM_ATTRIBUTE8,0,null,100*(BIM_ATTRIBUTE7-BIM_ATTRIBUTE8)/BIM_ATTRIBUTE8) BIM_ATTRIBUTE8,
19476 BIM_ATTRIBUTE7 BIM_ATTRIBUTE9,
19477 sum(BIM_ATTRIBUTE2) over() BIM_GRAND_TOTAL1,
19478 sum(BIM_ATTRIBUTE3) over() BIM_GRAND_TOTAL2,
19479 decode(sum(BIM_ATTRIBUTE4) over(),0,null,100*(sum(BIM_ATTRIBUTE3) over()-sum(BIM_ATTRIBUTE4) over())/sum(BIM_ATTRIBUTE4) over()) BIM_GRAND_TOTAL3,
19480 sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL4,
19481 decode(sum(BIM_ATTRIBUTE6) over(),0,null,100*(sum(BIM_ATTRIBUTE5) over()-sum(BIM_ATTRIBUTE6) over())/sum(BIM_ATTRIBUTE6) over()) BIM_GRAND_TOTAL5,
19482 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL6,
19483 decode(sum(BIM_ATTRIBUTE8) over(),0,null,100*(sum(BIM_ATTRIBUTE7) over()-sum(BIM_ATTRIBUTE8) over())/sum(BIM_ATTRIBUTE8) over()) BIM_GRAND_TOTAL7,
19484 sum(BIM_ATTRIBUTE7) over() BIM_GRAND_TOTAL8,
19485 decode(viewbyid,-1,NULL,-1,NULL,-1,null,''pFunctionName=BIM_I_CSCH_STARTED&pParamIds=Y&VIEW_BY=ITEM+ENI_ITEM_VBH_CAT&VIEW_BY_NAME=VIEW_BY_ID'' ) BIM_URL1,
19486 decode(BIM_ATTRIBUTE3, 0,NULL,'||''''||l_url_str1||''''||')  BIM_URL2,
19487 decode(BIM_ATTRIBUTE5, 0,NULL,'||''''||l_url_str2||''''||')  BIM_URL3,
19488 decode(BIM_ATTRIBUTE7, 0,NULL,'||''''||l_url_str3||''''||')  BIM_URL4
19489 FROM
19490 (
19494 nvl(sum(curr_prior_active),0) BIM_ATTRIBUTE2,
19491 SELECT
19492 name VIEWBY,
19493 id VIEWBYID,
19495 sum(curr_started) BIM_ATTRIBUTE3,
19496 SUM(prev_started) BIM_ATTRIBUTE4,
19497 sum(curr_ended)  BIM_ATTRIBUTE5,
19498 SUm(prev_ended) BIM_ATTRIBUTE6,
19499 nvl(sum(curr_prior_active),0)+sum(curr_started)-sum(curr_act_ended) BIM_ATTRIBUTE7,
19500 nvl(sum(prev_prior_active),0)+sum(prev_started)-sum(prev_act_ended) BIM_ATTRIBUTE8
19501 FROM
19502 (
19503 SELECT
19504 p.value name,
19505 p.id id,
19506 p.leaf_node_flag leaf_node_flag ,
19507 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
19508 0 prev_prior_active,
19509 0 curr_active,
19510 0 prev_active,
19511 0 curr_started,
19512 0 prev_started,
19513 0 curr_ended,
19514 0 prev_ended,
19515 0 curr_act_ended,
19516 0 prev_act_ended
19517 FROM bim_i_obj_mets_mv a,
19518      fii_time_rpt_struct_v cal,
19519      eni_denorm_hierarchies b,
19520      mtl_default_category_sets mdcs,';
19521 IF l_prog_view='Y' then
19522   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19523 ELSE
19524 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19525 END IF;
19526 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value , e.leaf_node_flag leaf_node_flag
19527       from eni_item_vbh_nodes_v e
19528       where e.parent_id =:l_cat_id
19529       AND e.id = e.child_id
19530       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
19531       ) p
19532 WHERE
19533      a.time_id = cal.time_id
19534 AND  a.period_type_id = cal.period_type_id
19535 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
19536 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
19537 AND cal.calendar_id=-1
19538 AND a.object_country = :l_country
19539 AND ac.resource_id = :l_resource_id
19540 AND a.category_id = b.child_id
19541 AND b.object_type = ''CATEGORY_SET''
19542 AND b.object_id = mdcs.category_set_id
19543 AND mdcs.functional_area_id = 11
19544 AND b.parent_id = p.id
19545 AND b.dbi_flag = ''Y''';
19546 IF l_prog_view='N' then
19547 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19548 AND a.source_code_id = s.source_code_id
19549 AND s.object_id = ac.object_id
19550 AND s.object_type=ac.object_type
19551 ';
19552 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19553 END IF;
19554 l_sqltext := l_sqltext ||' group by p.value,p.id, p.leaf_node_flag ';
19555 l_sqltext :=l_sqltext ||
19556 ' UNION ALL
19557 SELECT
19558 p.id ID,
19559 bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||'   name,
19560 ''Y'' leaf_node_flag ,
19561 sum(camp_sch_started-camp_sch_ended) curr_prior_active,
19562 0 prev_prior_active,
19563 0 curr_active,
19564 0 prev_active,
19565 0 curr_started,
19566 0 prev_started,
19567 0 curr_ended,
19568 0 prev_ended,
19569 0 curr_act_ended,
19570 0 prev_act_ended
19571 FROM bim_i_obj_mets_mv a,
19572      fii_time_rpt_struct_v cal,';
19573 IF l_prog_view='Y' then
19574   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19575 ELSE
19576 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19577 END IF;
19578 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value , e.leaf_node_flag leaf_node_flag
19579       from eni_item_vbh_nodes_v e
19580       where e.parent_id =  :l_cat_id
19581       AND e.parent_id = e.child_id
19582       AND leaf_node_flag <> ''Y''
19583       ) p
19584 WHERE
19585      a.time_id = cal.time_id
19586 AND  a.period_type_id = cal.period_type_id
19587 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
19588 AND cal.report_date in (&BIS_CURRENT_EFFECTIVE_START_DATE-1)
19589 AND cal.calendar_id=-1
19590 AND a.object_country = :l_country
19591 AND ac.resource_id = :l_resource_id
19592 AND a.category_id = p.id';
19593 IF l_prog_view='N' then
19594 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19595 AND a.source_code_id = s.source_code_id
19596 AND s.object_id = ac.object_id
19597 AND s.object_type=ac.object_type
19598 ';
19599 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19600 END IF;
19601 /*IF l_cat_id is not null
19602 then
19603 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
19604 end if;*/
19605 l_sqltext := l_sqltext ||' group by p.id
19606 UNION ALL
19607 SELECT
19608 p.value name,
19609 p.id id,
19610 p.leaf_node_flag leaf_node_flag ,
19611 0 curr_prior_active,
19612 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
19613 0 curr_active,
19614 0 prev_active,
19615 0 curr_started,
19616 0 prev_started,
19617 0 curr_ended,
19618 0 prev_ended,
19619 0 curr_act_ended,
19620 0 prev_act_ended
19621 FROM bim_i_obj_mets_mv a,
19622      fii_time_rpt_struct_v cal,
19623      eni_denorm_hierarchies b,
19624      mtl_default_category_sets mdcs,';
19625 IF l_prog_view='Y' then
19626   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19627 ELSE
19628 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19629 END IF;
19630 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value e.leaf_node_flag leaf_node_flag ,
19631       from eni_item_vbh_nodes_v e
19632       where e.parent_id =:l_cat_id
19633       AND e.id = e.child_id
19634       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'') ) p
19638 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
19635 WHERE a.time_id = cal.time_id
19636 AND  a.period_type_id = cal.period_type_id
19637 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
19639 AND cal.calendar_id=-1
19640 AND a.object_country = :l_country
19641 AND ac.resource_id = :l_resource_id
19642 AND a.category_id = b.child_id
19643 AND b.object_type = ''CATEGORY_SET''
19644 AND b.object_id = mdcs.category_set_id
19645 AND mdcs.functional_area_id = 11
19646 AND b.dbi_flag =''Y''
19647 AND b.parent_id = p.id ';
19648 IF l_prog_view='N' then
19649 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19650 AND a.source_code_id = s.source_code_id
19651 AND s.object_id = ac.object_id
19652 AND s.object_type=ac.object_type
19653 ';
19654 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19655 END IF;
19656 /*IF l_cat_id is not null
19657 then
19658 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
19659 end if;*/
19660 l_sqltext := l_sqltext ||' group by p.id  , p.value , p.leaf_node_flag
19661 UNION ALL
19662 SELECT
19663 p.id ID,
19664 bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||'   name,
19665 ''Y'' leaf_node_flag ,
19666 0 curr_prior_active,
19667 sum(camp_sch_started-camp_sch_ended) prev_prior_active,
19668 0 curr_active,
19669 0 prev_active,
19670 0 curr_started,
19671 0 prev_started,
19672 0 curr_ended,
19673 0 prev_ended,
19674 0 curr_act_ended,
19675 0 prev_act_ended
19676 FROM bim_i_obj_mets_mv a,
19677      fii_time_rpt_struct_v cal,';
19678 IF l_prog_view='Y' then
19679   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19680 ELSE
19681 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19682 END IF;
19683 l_sqltext:=l_sqltext||' ,(select e.id id ,e.value value
19684       from eni_item_vbh_nodes_v e
19685       where e.parent_id =  :l_cat_id
19686       AND e.parent_id = e.child_id
19687       AND leaf_node_flag <> ''Y''
19688       ) p
19689 WHERE
19690      a.time_id = cal.time_id
19691 AND  a.period_type_id = cal.period_type_id
19692 AND BITAND(cal.record_type_id,1143)=cal.record_type_id
19693 AND cal.report_date in (&BIS_PREVIOUS_EFFECTIVE_START_DATE-1)
19694 AND cal.calendar_id=-1
19695 AND a.object_country = :l_country
19696 AND ac.resource_id = :l_resource_id
19697 AND a.category_id = p.id
19698 ';
19699 IF l_prog_view='N' then
19700 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19701 AND a.source_code_id = s.source_code_id
19702 AND s.object_id = ac.object_id
19703 AND s.object_type=ac.object_type
19704 ';
19705 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19706 END IF;
19707 /*IF l_cat_id is not null
19708 then
19709 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
19710 end if;*/
19711 l_sqltext := l_sqltext ||' group by p.id
19712  UNION ALL
19713 SELECT
19714 p.id ID,
19715 p.value   name,
19716 p.leaf_node_flag leaf_node_flag ,
19717 0 curr_prior_active,
19718 0 prev_prior_active,
19719 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
19720 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
19721 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
19722 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
19723 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
19724 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
19725 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
19726 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
19727 FROM bim_i_obj_mets_mv a,
19728      fii_time_rpt_struct_v cal,
19729      eni_denorm_hierarchies b,
19730      mtl_default_category_sets mdcs,';
19731 IF l_prog_view='Y' then
19732   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19733 ELSE
19734 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19735 END IF;
19736 l_sqltext :=l_sqltext||' ,(select e.id id ,e.value value , e.leaf_node_flag
19737       from eni_item_vbh_nodes_v e
19738       where e.parent_id =:l_cat_id
19739       AND e.id = e.child_id
19740       AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')) p
19741 WHERE
19742      a.time_id = cal.time_id
19743 AND  a.period_type_id = cal.period_type_id
19744 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
19745 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
19746 AND cal.calendar_id=-1
19747 AND a.object_country = :l_country
19748 AND a.category_id = b.child_id
19749 AND ac.resource_id = :l_resource_id
19750 AND b.object_type = ''CATEGORY_SET''
19751 AND b.object_id = mdcs.category_set_id
19752 AND mdcs.functional_area_id = 11
19753 AND b.dbi_flag = ''Y''
19754 AND b.parent_id = p.id ';
19755 IF l_prog_view='N' then
19756 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19757 AND a.source_code_id = s.source_code_id
19758 AND s.object_id = ac.object_id
19759 AND s.object_type=ac.object_type
19763 /*IF l_cat_id is not null
19760 ';
19761 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19762 END IF;
19764 then
19765 l_sqltext := l_sqltext ||' AND p.parent_id = :l_cat_id ';
19766 end if;*/
19767 l_sqltext := l_sqltext ||' group by p.value,p.id , p.leaf_node_flag
19768 UNION ALL
19769 SELECT
19770 p.id ID,
19771 bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||'   name,
19772 ''Y'' leaf_node_flag ,
19773 0 curr_prior_active,
19774 0 prev_prior_active,
19775 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) curr_active,
19776 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then (camp_sch_started-camp_sch_ended) else 0 end) prev_active,
19777 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then  camp_sch_started else 0 end) curr_started,
19778 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_started else 0 end) prev_started,
19779 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then camp_sch_ended else 0 end) curr_ended,
19780 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then camp_sch_ended else 0 end) prev_ended,
19781 SUM(case when &BIS_CURRENT_ASOF_DATE >&BIS_CURRENT_EFFECTIVE_START_DATE and cal.report_date=&BIS_CURRENT_ASOF_DATE-1 then camp_sch_ended else 0 end) curr_act_ended,
19782 SUM(case when &BIS_PREVIOUS_ASOF_DATE >&BIS_PREVIOUS_EFFECTIVE_START_DATE and cal.report_date=&BIS_PREVIOUS_ASOF_DATE-1 then camp_sch_ended else 0 end) prev_act_ended
19783 FROM bim_i_obj_mets_mv a,
19784      fii_time_rpt_struct_v cal,';
19785 IF l_prog_view='Y' then
19786   l_sqltext := l_sqltext ||' bim_i_top_objects ac';
19787 ELSE
19788 l_sqltext := l_sqltext ||'  ams_act_access_denorm ac, bim_i_source_codes s';
19789 END IF;
19790 l_sqltext := l_sqltext ||' ,(select e.id id ,e.value value
19791       from eni_item_vbh_nodes_v e
19792       where e.parent_id =  :l_cat_id
19793       AND e.parent_id = e.child_id
19794       AND leaf_node_flag <> ''Y''
19795       ) p
19796 WHERE
19797      a.time_id = cal.time_id
19798 AND  a.period_type_id = cal.period_type_id
19799 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
19800 AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE,&BIS_CURRENT_ASOF_DATE-1,&BIS_PREVIOUS_ASOF_DATE-1)
19801 AND cal.calendar_id=-1
19802 AND a.object_country = :l_country
19803 AND ac.resource_id = :l_resource_id
19804 AND a.category_id = p.id';
19805 IF l_prog_view='N' then
19806 l_sqltext := l_sqltext ||' AND s.object_type=''CAMP''
19807 AND a.source_code_id = s.source_code_id
19808 AND s.object_id = ac.object_id
19809 AND s.object_type=ac.object_type
19810 ';
19811 ELSE l_sqltext := l_sqltext ||' AND a.source_code_id = ac.source_code_id';
19812 END IF;
19813  l_sqltext:=l_sqltext||' group by p.id
19814  ) group by name,id )';
19815  END IF; -- product category All loop
19816  END IF; --end view by product category
19817 END IF; -- end if admin_flag = Y loop
19818 
19819 
19820   x_custom_sql := l_sqltext;
19821   l_custom_rec.attribute_name := ':l_record_type';
19822   l_custom_rec.attribute_value := l_record_type_id;
19823   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
19824   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
19825   x_custom_output.EXTEND;
19826   x_custom_output(1) := l_custom_rec;
19827 
19828   l_custom_rec.attribute_name := ':l_resource_id';
19829   l_custom_rec.attribute_value := GET_RESOURCE_ID;
19830   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
19831   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
19832   x_custom_output.EXTEND;
19836   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
19833   x_custom_output(2) := l_custom_rec;
19834 
19835   l_custom_rec.attribute_name := ':l_admin_flag';
19837   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
19838   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
19839   x_custom_output.EXTEND;
19840   x_custom_output(3) := l_custom_rec;
19841 
19842   l_custom_rec.attribute_name := ':l_country';
19843   l_custom_rec.attribute_value := l_country;
19844   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
19845   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
19846   x_custom_output.EXTEND;
19847   x_custom_output(4) := l_custom_rec;
19848 
19849   l_custom_rec.attribute_name := ':l_cat_id';
19850   l_custom_rec.attribute_value := l_cat_id;
19851   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
19852   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
19853   x_custom_output.EXTEND;
19854   x_custom_output(5) := l_custom_rec;
19855 
19856   l_custom_rec.attribute_name := ':l_view_by';
19857   l_custom_rec.attribute_value := l_view_by;
19858   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
19859   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
19860   x_custom_output.EXTEND;
19861   x_custom_output(6) := l_custom_rec;
19862 
19863  l_custom_rec.attribute_name := ':l_area';
19864   l_custom_rec.attribute_value := l_area;
19865   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
19866   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
19867   x_custom_output.EXTEND;
19868   x_custom_output(7) := l_custom_rec;
19869 
19870    l_custom_rec.attribute_name := ':l_media';
19871   l_custom_rec.attribute_value := l_media;
19872   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
19873   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
19874   x_custom_output.EXTEND;
19875   x_custom_output(8) := l_custom_rec;
19876 
19877 --write_debug('GET_CSCH_START_SQL','QUERY','_',l_sqltext);
19878 --return l_sqltext;
19879 
19880 EXCEPTION
19881 WHEN others THEN
19882 l_sql_errm := SQLERRM;
19883 write_debug('GET_CSCH_START_SQL','ERROR',l_sql_errm,l_sqltext);
19884 END GET_CSCH_START_SQL;
19885 
19886 PROCEDURE GET_CSCH_DETL_SQL(p_page_parameter_tbl in BIS_PMV_PAGE_PARAMETER_TBL,
19887                           x_custom_sql  OUT NOCOPY VARCHAR2,
19888                           x_custom_output OUT NOCOPY BIS_QUERY_ATTRIBUTES_TBL)
19889  IS
19890 l_sqltext varchar2(15000);
19891 iFlag number;
19892 l_period_type_hc number;
19893 l_as_of_date  DATE;
19894 l_period_type	varchar2(2000);
19895 l_record_type_id NUMBER;
19896 l_comp_type    varchar2(2000);
19900 l_current_report_start_date DATE;
19897 l_country      varchar2(4000);
19898 l_sql_errm      varchar2(4000);
19899 l_previous_report_start_date DATE;
19901 l_previous_as_of_date DATE;
19902 l_period_type_id NUMBER;
19903 l_user_id NUMBER;
19904 l_resource_id NUMBER;
19905 l_time_id_column  VARCHAR2(1000);
19906 l_admin_status VARCHAR2(20);
19907 l_admin_flag VARCHAR2(1);
19908 l_admin_count Number;
19909 l_rsid NUMBER;
19910 l_curr_aod_str varchar2(80);
19911 l_country_clause varchar2(4000);
19912 l_access_clause varchar2(4000);
19913 l_access_table varchar2(4000);
19914 --l_cat_id NUMBER;
19915 l_cat_id VARCHAR2(50);
19916 l_campaign_id VARCHAR2(50);
19917 l_custom_rec BIS_QUERY_ATTRIBUTES;
19918 l_dass                 varchar2(100);  -- variable to store value for  directly assigned lookup value
19919 l_una                  varchar2(100);   -- variable to store value for  Unassigned lookup value
19920 l_url_str VARCHAR2(1000);
19921 l_url_str_csch VARCHAR2(1000);
19922 l_col_id NUMBER;
19923 l_viewby_id NUMBER;
19924 l_view_by VARCHAR2(100);
19925 l_area VARCHAR2(300);
19926 l_media VARCHAR2(50);
19927 p_report_name VARCHAR2(300);
19928 l_curr VARCHAR2(50);
19929 l_curr_suffix VARCHAR2(50);
19930 l_view_id     varchar2(5000);
19931       l_qry	varchar2(5000);
19932       l_from    varchar2(5000);
19933       l_where   varchar2(5000);
19934       l_view_col varchar2(5000);
19935       l_group_by varchar2(5000);
19936       l_report_name varchar2(50);
19937       l_curr_suffix1 VARCHAR2(50);
19938 
19939 BEGIN
19940 x_custom_output := BIS_QUERY_ATTRIBUTES_TBL();
19941 l_custom_rec := BIS_PMV_PARAMETERS_PUB.INITIALIZE_QUERY_TYPE;
19942 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
19943                                                       l_as_of_date,
19944                                                       l_period_type,
19945                                                       l_record_type_id,
19946                                                       l_comp_type,
19947                                                       l_country,
19948                                                       l_view_by,
19949                                                       l_cat_id,
19950                                                       l_campaign_id,
19951                                                       l_curr,
19952                                                       l_col_id,
19953                                                       l_area,
19954                                                       l_media,
19955                                                       l_report_name
19956                                                       );
19957 
19958 bim_pmv_dbi_utl_pkg.get_viewby_id(p_page_parameter_tbl,l_viewby_id);
19959 
19960 
19961  IF (l_curr = '''FII_GLOBAL1''')
19962     THEN l_curr_suffix := '';
19963   ELSIF (l_curr = '''FII_GLOBAL2''')
19964     THEN l_curr_suffix := '_s';
19965     ELSE l_curr_suffix := '';
19966   END IF;
19967    --l_curr_aod_str := 'to_date('||to_char(l_as_of_date,'J')||',''J'')';
19968    IF l_country IS NULL THEN
19969       l_country := 'N';
19970    END IF;
19971  l_admin_status := GET_ADMIN_STATUS;
19972 
19973 l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
19974 
19975 --l_url_str :='pFunctionName=BIM_I_CAMP_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=18917&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CAMP&PAGE.OBJ.objAttribute=DETL';
19976 --l_url_str :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&objType=CSCH&objId=';
19977     l_url_str :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID&PAGE.OBJ.ID_NAME1=customSetupId&PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
19978 --l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&objId=';
19979 l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
19980 
19981 if l_curr_suffix is null then
19982 l_curr_suffix1:=1;
19983 else
19984 l_curr_suffix1:=2;
19985 end if;
19986 
19987 l_qry := '        SELECT
19988           BIM_ATTRIBUTE1,
19989           BIM_ATTRIBUTE8,
19990           BIM_ATTRIBUTE2,
19991           BIM_ATTRIBUTE3,
19992 	  BIM_ATTRIBUTE9,
19993           BIM_ATTRIBUTE4,
19994           BIM_ATTRIBUTE5,
19995           (BIM_ATTRIBUTE4 - BIM_ATTRIBUTE5) BIM_ATTRIBUTE6,
19996 	  BIM_ATTRIBUTE7,
19997 	  sum(BIM_ATTRIBUTE4) over() BIM_GRAND_TOTAL1,
19998 	  sum(BIM_ATTRIBUTE5) over() BIM_GRAND_TOTAL2,
19999 	  sum(BIM_ATTRIBUTE4) over() - sum(BIM_ATTRIBUTE5) over()  BIM_GRAND_TOTAL3,
20000 	 '||''''|| l_url_str_csch|| ''''||'||BIM_ATTRIBUTE7 	BIM_URL1
20001 	  FROM
20002           (
20006 		  c.campaign_name BIM_ATTRIBUTE8,
20003            select b.schedule_name BIM_ATTRIBUTE1,
20004 	          s.start_date BIM_ATTRIBUTE2,
20005                   s.end_date BIM_ATTRIBUTE3,
20007                   b.schedule_id BIM_ATTRIBUTE7,
20008 		  d.media_name BIM_ATTRIBUTE9,
20009 		  s.child_object_usage usage,
20010                   sum(nvl(a.metric'||l_curr_suffix1||',0)) BIM_ATTRIBUTE4,
20011                   sum(nvl(a.cost_actual'||l_curr_suffix||',0)) BIM_ATTRIBUTE5
20012            from
20013 		   bim_i_marketing_facts a , bim_i_source_codes s ,
20014            ams_campaign_schedules_tl b, ams_campaigns_all_tl c,
20015 			ams_media_tl d
20016 	    ';
20017 l_where :=' WHERE
20018 			    s.object_id = c.campaign_id
20019 			AND c.language=USERENV(''LANG'')
20020 			AND d.media_id(+) = s.activity_id
20021 			AND d.language(+)=USERENV(''LANG'')
20022 			AND b.language(+)=USERENV(''LANG'')
20023 			AND s.object_type = ''CAMP''
20024 			AND s.child_object_type=''CSCH''
20025 			AND a.source_code_id(+) = s.source_code_id
20026 			AND s.child_object_id = b.schedule_id ';
20027 l_group_by := ' group by c.campaign_name, d.media_name,b.schedule_name,b.schedule_id,s.start_date,s.end_date,s.child_object_usage
20028            )  &ORDER_BY_CLAUSE';
20029 
20030 	IF l_admin_status = 'N' THEN
20031 		l_qry := l_qry ||',ams_act_access_denorm ac  ';
20032 		l_where := l_where|| ' AND s.child_object_type = ac.object_type
20033 							AND s.child_object_id = ac.object_id
20034 							AND ac.resource_id = :l_resource_id ';
20035 	END IF;
20036 
20037   if l_cat_id is not null  then
20038    l_qry:= l_qry || ' , eni_denorm_hierarchies edh , mtl_default_category_sets mdcs';
20039    l_where := l_where||'  AND edh.parent_id ='||l_cat_id||
20040 					   ' and nvl(s.category_id,-1) = edh.child_id
20041    					    AND edh.object_type = ''CATEGORY_SET''
20042 						AND edh.object_id = mdcs.category_set_id
20043 						AND mdcs.functional_area_id = 11
20044 						AND edh.dbi_flag = ''Y'' ';
20045   end if;
20046 IF l_col_id = 1 THEN /*Started*/
20047    l_where := l_where||' and s.adj_start_date between &BIS_CURRENT_EFFECTIVE_START_DATE and &BIS_CURRENT_ASOF_DATE ';
20048 ELSIF (l_col_id = 2) THEN/*Ended*/
20049  l_where := l_where||'  and s.adj_end_date between &BIS_CURRENT_EFFECTIVE_START_DATE and &BIS_CURRENT_ASOF_DATE ';
20050 ELSIF (l_col_id = 3) THEN/*Current Active*/
20051 l_where := l_where||' and s.adj_start_date<=&BIS_CURRENT_ASOF_DATE	 and s.adj_end_date >= &BIS_CURRENT_ASOF_DATE ';
20052 END IF;
20053  /************  Query for View By Selection  ************/
20054  if l_viewby_id = 3 then
20055     l_view_by :='MEDIA+MEDIA';
20056  ELSIF l_viewby_id = 4 then
20057     l_view_by:='GEOGRAPHY+AREA' ;
20058  END IF;
20059 -- if l_view_by = 'GEOGRAPHY+COUNTRY' then
20060 if l_country <> 'N' THEN
20061     l_where:=l_where||' AND s.object_country = '''||l_country||'''';
20065      l_where:=l_where||' AND s.activity_id ='||l_media||'';
20062  elsif l_view_by = 'GEOGRAPHY+AREA'  THEN
20063     l_where:=l_where||' AND s.object_region = :l_area';
20064  elsif l_view_by = 'MEDIA+MEDIA' THEN
20066  end if;
20067  l_sqltext:=l_qry||l_where||l_group_by;
20068 
20069   x_custom_sql := l_sqltext;
20070   l_custom_rec.attribute_name := ':l_record_type';
20071   l_custom_rec.attribute_value := l_record_type_id;
20072   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
20073   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
20074   x_custom_output.EXTEND;
20075   x_custom_output(1) := l_custom_rec;
20076 
20077   l_custom_rec.attribute_name := ':l_resource_id';
20078   l_custom_rec.attribute_value := GET_RESOURCE_ID;
20079   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
20080   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
20081   x_custom_output.EXTEND;
20082   x_custom_output(2) := l_custom_rec;
20083 
20084   l_custom_rec.attribute_name := ':l_admin_flag';
20085   l_custom_rec.attribute_value := GET_ADMIN_STATUS;
20086   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
20087   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
20088   x_custom_output.EXTEND;
20089   x_custom_output(3) := l_custom_rec;
20090 
20091   l_custom_rec.attribute_name := ':l_country';
20092   l_custom_rec.attribute_value := l_country;
20093   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
20094   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
20095   x_custom_output.EXTEND;
20096   x_custom_output(4) := l_custom_rec;
20097 
20098   l_custom_rec.attribute_name := ':l_cat_id';
20099   l_custom_rec.attribute_value := l_cat_id;
20100   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
20101   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
20102   x_custom_output.EXTEND;
20103   x_custom_output(5) := l_custom_rec;
20104 
20105  l_custom_rec.attribute_name := ':l_media';
20106  l_custom_rec.attribute_value := l_media;
20107  l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
20108   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
20109   x_custom_output.EXTEND;
20110   x_custom_output(4) := l_custom_rec;
20111 
20112   l_custom_rec.attribute_name := ':l_area';
20113   l_custom_rec.attribute_value := l_area;
20114   l_custom_rec.attribute_type := BIS_PMV_PARAMETERS_PUB.BIND_TYPE;
20115   l_custom_rec.attribute_data_type := BIS_PMV_PARAMETERS_PUB.VARCHAR2_BIND;
20116   x_custom_output.EXTEND;
20117   x_custom_output(5) := l_custom_rec;
20118 
20119 --write_debug('GET_CSCH_DETL_SQL','QUERY','_',l_sqltext);
20120 
20121 EXCEPTION
20122 WHEN others THEN
20123 l_sql_errm := SQLERRM;
20124 write_debug('GET_CSCH_DETL_SQL','ERROR',l_sql_errm,l_sqltext);
20125 
20126 END GET_CSCH_DETL_SQL;
20127 
20128 
20129   PROCEDURE GET_RESP_SUM_SQL(p_page_parameter_tbl IN bis_pmv_page_parameter_tbl,
20130                                  x_custom_sql         OUT NOCOPY VARCHAR2,
20131                                  x_custom_output      OUT NOCOPY bis_query_attributes_tbl) IS
20132     l_sqltext                    VARCHAR2(32000);
20133     iflag                        NUMBER;
20134     l_period_type_hc             NUMBER;
20135     l_as_of_date                 DATE;
20136     l_period_type                VARCHAR2(2000);
20137     l_record_type_id             NUMBER;
20138     l_comp_type                  VARCHAR2(2000);
20139     l_country                    VARCHAR2(4000);
20140     l_view_by                    VARCHAR2(4000);
20141     l_sql_errm                   VARCHAR2(4000);
20142     l_previous_report_start_date DATE;
20143     l_current_report_start_date  DATE;
20144     l_previous_as_of_date        DATE;
20145     l_period_type_id             NUMBER;
20146     l_user_id                    NUMBER;
20147     l_resource_id                NUMBER;
20148     l_time_id_column             VARCHAR2(1000);
20149     l_admin_status               VARCHAR2(20);
20150     l_admin_flag                 VARCHAR2(1);
20151     l_admin_count                NUMBER;
20152     l_rsid                       NUMBER;
20153     l_curr_aod_str               VARCHAR2(80);
20154     l_country_clause             VARCHAR2(4000);
20155     l_access_clause              VARCHAR2(4000);
20156     l_access_table               VARCHAR2(4000);
20157     l_cat_id                     VARCHAR2(50) := NULL;
20158     l_campaign_id                VARCHAR2(50) := NULL;
20159     l_select                     VARCHAR2(20000); -- to build  inner select to pick data from mviews
20160     l_pc_select                  VARCHAR2(20000); -- to build  inner select to pick data directly assigned to the product category hirerachy
20161     l_select_cal                 VARCHAR2(20000); -- to build  select calculation part
20162     l_select_cal1                VARCHAR2(20000);
20163     l_select_filter              VARCHAR2(20000); -- to build  select filter part
20164     l_from                       VARCHAR2(20000); -- assign common table in  clause
20165     l_where                      VARCHAR2(20000); -- static where clause
20166     l_groupby                    VARCHAR2(2000); -- to build  group by clause
20167     l_pc_from                    VARCHAR2(20000); -- from clause to handle product category
20168     l_pc_where                   VARCHAR2(20000); --  where clause to handle product category
20172     l_view                       VARCHAR2(20);
20169     l_filtercol                  VARCHAR2(2000);
20170     l_pc_col                     VARCHAR2(200);
20171     l_pc_groupby                 VARCHAR2(200);
20173     l_select1                    VARCHAR2(20000);
20174     l_select2                    VARCHAR2(20000);
20175     l_view_disp                  VARCHAR2(100);
20176     l_url_str                    VARCHAR2(1000);
20177     l_url_str_csch               varchar2(1000);
20178     l_url_str_type               varchar2(3000);
20179     l_url_str_csch_jtf           varchar2(3000);
20180     l_camp_sel_col               varchar2(100);
20181     l_camp_groupby_col           varchar2(100);
20182     l_csch_chnl                  varchar2(100);
20183     l_top_cond                   VARCHAR2(100);
20184     l_meaning                    VARCHAR2(20);
20185     l_inner                      varchar2(5000);
20186     l_inr_cond                   varchar2(5000);
20187     l_from_inr                   VARCHAR2(20000);
20188     l_where_inr                  VARCHAR2(20000);
20189     l_curr                       VARCHAR2(50);
20190     l_curr_suffix                VARCHAR2(50);
20191     l_viewby1 varchar2(200);
20192     l_grp_by1 varchar2(200);
20193     l_where1  varchar2(100);
20194     l_from1   varchar2(50);
20195     L_TOP_SQl varchar2(3200);
20196     /* variables to hold columns names in l_select clauses */
20197     l_col VARCHAR2(1000);
20198     /* cursor to get type of object passed from the page ******/
20199     cursor get_obj_type is
20200       select object_type
20201         from bim_i_source_codes
20202        where source_code_id = replace(l_campaign_id, '''');
20203     /*********************************************************/
20204     l_custom_rec  bis_query_attributes;
20205     l_object_type varchar2(30);
20206     l_url_link    varchar2(200);
20207     l_url_camp1   varchar2(3000);
20208     l_url_camp2   varchar2(3000);
20209     l_dass        varchar2(100); -- variable to store value for  directly assigned lookup value
20210     --l_una                        varchar2(100);   -- variable to store value for  Unassigned lookup value
20211     l_leaf_node_flag varchar2(25); -- variable to store value leaf_node_flag column in case of product category
20212     l_col_id         NUMBER;
20213     l_area           VARCHAR2(300);
20214     l_report_name    VARCHAR2(300);
20215     l_media          VARCHAR2(300);
20216     l_curr_suffix1 VARCHAR2(50);
20217     l_table_bud VARCHAR2(300);
20218     l_where_bud VARCHAR2(300);
20219     l_prog_cost1 VARCHAR2(30);
20220 
20221   BEGIN
20222     x_custom_output := bis_query_attributes_tbl();
20223     l_custom_rec    := bis_pmv_parameters_pub.initialize_query_type;
20224     bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
20225                                             l_as_of_date,
20226                                             l_period_type,
20227                                             l_record_type_id,
20228                                             l_comp_type,
20229                                             l_country,
20230                                             l_view_by,
20231                                             l_cat_id,
20232                                             l_campaign_id,
20233                                             l_curr,
20234                                             l_col_id,
20235                                             l_area,
20236                                             l_media,
20237                                             l_report_name);
20238     l_meaning   := ' null meaning '; -- assigning default value
20239     l_url_camp1 := ',null';
20240     l_url_camp2 := ',null';
20241     IF (l_curr = '''FII_GLOBAL1''') THEN
20242       l_curr_suffix := '';
20243     ELSIF (l_curr = '''FII_GLOBAL2''') THEN
20244       l_curr_suffix := '_s';
20245     ELSE
20246       l_curr_suffix := '';
20247     END IF;
20248     IF l_country IS NULL THEN
20249       l_country := 'N';
20250     END IF;
20251     /************Start Inner Query to get current acitve objects *************************/
20252     l_inner := ', ( select distinct  codes.source_code_id from BIM_I_obj_METS_MV a,BIM_I_SOURCE_CODES codes
20253 ,fii_time_rpt_struct_v cal';
20254     IF l_admin_status = 'N' THEN
20255       l_inner := l_inner || ',bim_i_top_objects r ';
20256     end if;
20257     IF l_cat_id is not null then
20258       l_inner := l_inner ||
20259                  ',eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
20260     end if;
20261     l_inner := l_inner ||
20262                ' WHERE  a.source_code_id = codes.source_code_id and a.time_id=cal.time_id
20263 AND a.period_type_id=cal.period_type_id  AND cal.calendar_id=-1 AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
20264 AND a.object_country = :l_country AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id    and (responses_positive >0)';
20265     IF l_admin_status = 'N' THEN
20266       l_inner := l_inner ||
20267                  ' AND a.source_code_id = r.source_code_id AND r.resource_id = :l_resource_id ';
20268     END IF;
20269     IF l_cat_id is null then
20270       l_inner := l_inner || ' AND a.category_id = -9 ';
20271     else
20272       l_inner := l_inner ||' AND a.category_id = edh.child_id AND edh.object_type = ''CATEGORY_SET''  AND edh.object_id = mdcs.category_set_id  AND mdcs.functional_area_id = 11 AND edh.dbi_flag = ''Y''   AND edh.parent_id = :l_cat_id ';
20273     end if;
20277     /************ End Inner Query to get current acitve objects *************************/
20274     l_inner := l_inner || ' ) inr ';
20275 --    l_inr_cond := ' and a.object_id= inr.object_id  and a.object_type=inr.object_type ';
20276     l_inr_cond := ' and inr.source_code_id = a.source_code_id  ';
20278     /** to add meaning in select clause only in case of campaign view by */
20279     IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
20280       l_meaning   := ',meaning ';
20281       l_filtercol := ',meaning ';
20282     ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
20283       l_filtercol := ',leaf_node_flag ';
20284       l_meaning   := ' ,null meaning';
20285     else
20286       l_meaning := ' ,null meaning ';
20287     end if;
20288     /*** to  assigned URL **/
20289     if l_campaign_id is not null then
20290       -- checking for the object type passed from page
20291       for i in get_obj_type loop
20292         l_object_type := i.object_type;
20293       end loop;
20294     end if;
20295       l_url_str  :='pFunctionName=BIM_I_MKTG_RESP_SUM_PHP&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
20296    --l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objId=';
20297    l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
20298    l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
20299    l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
20300    &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
20301 
20302     IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
20303       l_url_link       := ' ,decode(leaf_node_flag,''Y'',null,' || '''' ||l_url_str || '''' || ' ) ';
20304       l_view_disp      := ' viewby';
20305       l_leaf_node_flag := ' ,leaf_node_flag ';
20306     ELSIF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
20307       l_camp_sel_col     := ',object_id,object_type ';
20308       l_camp_groupby_col := ',object_id,object_type ';
20309       l_url_link         := ' ,null ';
20310       l_view_disp        := 'viewby';
20311       IF (l_campaign_id is null or l_object_type = 'RCAM') then
20312         l_url_camp1 := ', decode(object_type,''EONE'',NULL,' || '''' ||l_url_str || '''' || ' )';
20313       ELSIF l_object_type = 'CAMP' THEN
20314         l_url_camp2        := ', ' || '''' || l_url_str_type || '''' ||'||object_id';
20315 		--l_url_camp1        := ', decode(usage,''LITE'',' || '''' ||l_url_str_csch || '''' || '||object_id,' || '''' ||
20316           ---                    l_url_str_csch_jtf || '''' || '||object_id)';
20317 		l_url_camp1        := ', '|| '''' ||l_url_str_csch || '''' || '||object_id ';
20318 
20319         l_csch_chnl        := '|| '' - '' || channel';
20320         l_camp_sel_col     := l_camp_sel_col || ',usage,channel';
20321         l_camp_groupby_col := l_camp_groupby_col || ',usage,channel';
20322       end if;
20323     ELSE
20324       l_url_link  := ' ,null ';
20325       l_view_disp := 'viewby';
20326     END IF;
20327     /* l_select_cal is common part of select statement for all view by to calculate grand totals and change */
20328 --------------------------------------------------------------------------------------------------
20329 ---------------------common part of top of the query
20330 --------------------------------------------------------------------------------------------------
20331     l_select_cal := '
20332 SELECT ' || l_view_disp || ', viewbyid, BIM_ATTRIBUTE1' ||
20333                     l_csch_chnl ||
20334                     '   BIM_ATTRIBUTE1 , BIM_ATTRIBUTE2  ,BIM_ATTRIBUTE3 , BIM_ATTRIBUTE11, BIM_ATTRIBUTE4 , BIM_ATTRIBUTE5 , BIM_ATTRIBUTE6
20335  , decode(p_cpr,0,NULL,((BIM_ATTRIBUTE6-p_cpr)/p_cpr)*100) BIM_ATTRIBUTE7  ' ||
20336                     l_url_link || ' bim_url1' || l_url_camp1 ||
20337                     ' bim_url2 ' || l_url_camp2 ||
20338                     ' bim_url3, BIM_GRAND_TOTAL1 , BIM_GRAND_TOTAL2
20339   ,BIM_GRAND_TOTAL3,  BIM_GRAND_TOTAL4 ,BIM_GRAND_TOTAL5 ,BIM_GRAND_TOTAL6,decode(p_cpr_tot,0,NULL,((BIM_GRAND_TOTAL6-p_cpr_tot)/p_cpr_tot)*100) BIM_GRAND_TOTAL7
20340 FROM
20341 ( SELECT
20342  name   VIEWBY ,  VIEWBYID  ,meaning BIM_ATTRIBUTE1' ||
20343                     l_camp_sel_col || ' , total_forecast BIM_ATTRIBUTE2,decode(total_forecast,0,NULL,((total_response-total_forecast)/total_forecast)*100) BIM_ATTRIBUTE3
20344 , total_response BIM_ATTRIBUTE4 , ptd_response BIM_ATTRIBUTE11 ,decode(prev_ptd_response,0,NULL,((ptd_response-prev_ptd_response)/prev_ptd_response)*100) BIM_ATTRIBUTE5
20345 ,  decode(ptd_response,0,NULL,ptd_cost/ptd_response) BIM_ATTRIBUTE6 , decode(prev_ptd_response,0,NULL,prev_ptd_cost/prev_ptd_response) p_cpr, decode(SUM(prev_ptd_response) over(),0,NULL,
20346 SUM(prev_ptd_cost) over()/SUM(prev_ptd_response) over()) p_cpr_tot ,sum(total_forecast) over() BIM_GRAND_TOTAL1,decode(sum(total_forecast) over(),0,NULL,(((sum(total_response-total_forecast) over())/sum(total_forecast) over())*100)) BIM_GRAND_TOTAL2
20347 ,sum(ptd_response) over() BIM_GRAND_TOTAL3 ,sum(total_response) over() BIM_GRAND_TOTAL4 , decode(sum(prev_ptd_response) over(),0,NULL,(((sum(ptd_response-prev_ptd_response) over())/sum(prev_ptd_response) over())*100)) BIM_GRAND_TOTAL5
20348 , decode(sum(ptd_response) over(),0,NULL,sum(ptd_cost) over()/sum(ptd_response) over()) BIM_GRAND_TOTAL6     , 111 BIM_GRAND_TOTAL7
20349 FROM
20350 (     SELECT   viewbyid    ,name' || l_meaning ||
20351                     l_camp_sel_col || ',
20355                     l_prog_cost ||
20352  decode(''' || l_prog_cost ||
20353                     ''',''BIM_APPROVED_BUDGET'',SUM(budget_approved),SUM(ptd_cost))   ptd_cost
20354   ,   SUM(ptd_response) ptd_response  , case when SUM(ptd_response) > 0 then  SUM(total_forecast) else 0  end total_forecast ,  decode(''' ||
20356                     ''',''BIM_APPROVED_BUDGET'',SUM(p_budget_approved),SUM(p_ptd_cost)) Prev_PTD_cost
20357  ,    SUM(p_ptd_response) Prev_PTD_response	 ,    case when SUM(ptd_response) > 0 then decode(''' ||
20358                     l_prog_cost || ''',''BIM_APPROVED_BUDGET'',SUM(t_budget_approved),SUM(total_cost))		     else 0 end   total_cost
20359  ,   case when SUM(ptd_response)>0 then   Sum(total_response)	  else 0 end total_response
20360  FROM ( ';
20361 --------------------------------------------------------------------------------
20362 --------------------- End common part of top of the query  ---------------------
20363 --------------------------------------------------------------------------------
20364     /*  change this  below query  */
20365 
20366 
20367     l_select_cal1 := ' SELECT ' || l_view_disp ||' ,viewbyid  ,BIM_ATTRIBUTE1
20368  	  ,BIM_ATTRIBUTE2 	  ,BIM_ATTRIBUTE3          ,BIM_ATTRIBUTE11	  ,BIM_ATTRIBUTE4
20369  	  ,BIM_ATTRIBUTE5 	  ,BIM_ATTRIBUTE6
20370  	  ,decode(p_cpr,0,NULL,((BIM_ATTRIBUTE6-p_cpr)/p_cpr)*100) BIM_ATTRIBUTE7' ||
20371                      l_url_link || ' bim_url1' || '
20372 	  ,null BIM_URL2
20373  	  ,null BIM_URL3
20374 	  ,BIM_GRAND_TOTAL1
20375           ,BIM_GRAND_TOTAL2
20376  	  ,BIM_GRAND_TOTAL3
20377  	  ,BIM_GRAND_TOTAL4
20378  	  ,BIM_GRAND_TOTAL5
20379  	  ,BIM_GRAND_TOTAL6
20380           ,decode(p_cpr_tot,0,NULL,((BIM_GRAND_TOTAL6-p_cpr_tot)/p_cpr_tot)*100) BIM_GRAND_TOTAL7
20381       FROM
20382 	 (
20383             SELECT
20384             name    VIEWBY' || l_leaf_node_flag || ',
20385      	    VIEWBYID,
20386             meaning BIM_ATTRIBUTE1,
20387 	    total_forecast BIM_ATTRIBUTE2,
20388             decode(total_forecast,0,NULL,((total_response-total_forecast)/total_forecast)*100) BIM_ATTRIBUTE3,
20389 	    total_response BIM_ATTRIBUTE4,
20390 	    ptd_response BIM_ATTRIBUTE11,
20391             decode(prev_ptd_response,0,NULL,((ptd_response-prev_ptd_response)/prev_ptd_response)*100) BIM_ATTRIBUTE5,
20392 	    decode(ptd_response,0,NULL,ptd_cost/ptd_response) BIM_ATTRIBUTE6,
20393             decode(prev_ptd_response,0,NULL,prev_ptd_cost/prev_ptd_response) p_cpr,
20394             decode(SUM(prev_ptd_response) over(),0,NULL,SUM(prev_ptd_cost) over()/SUM(prev_ptd_response) over()) p_cpr_tot,
20395   	    sum(total_forecast) over() BIM_GRAND_TOTAL1,
20396 	    decode(sum(total_forecast) over(),0,NULL,(((sum(total_response-total_forecast) over())/sum(total_forecast) over())*100)) BIM_GRAND_TOTAL2,
20397             sum(ptd_response) over() BIM_GRAND_TOTAL3,
20398             sum(total_response) over() BIM_GRAND_TOTAL4,
20399             decode(sum(prev_ptd_response) over(),0,NULL,(((sum(ptd_response-prev_ptd_response) over())/sum(prev_ptd_response) over())*100)) BIM_GRAND_TOTAL5,
20400             decode(sum(ptd_response) over(),0,NULL,sum(ptd_cost) over()/sum(ptd_response) over()) BIM_GRAND_TOTAL6,
20401             111 BIM_GRAND_TOTAL7
20402              FROM
20403               (
20404                   SELECT
20405           	     viewbyid
20406           	   ,  name' || l_meaning || l_leaf_node_flag ||
20407                      ', decode(''' || l_prog_cost || ''',''BIM_APPROVED_BUDGET'',SUM(budget_approved),SUM(ptd_cost))  ptd_cost
20408 		   ,  SUM(ptd_response) ptd_response
20409                    ,   SUM(total_forecast)  total_forecast
20410 		   ,  decode(''' || l_prog_cost || ''',''BIM_APPROVED_BUDGET'',SUM(p_budget_approved),SUM(p_ptd_cost)) Prev_PTD_cost
20411 		   ,  SUM(p_ptd_response) Prev_PTD_response
20412 		   ,  decode(''' || l_prog_cost || ''',''BIM_APPROVED_BUDGET'',SUM(t_budget_approved),SUM(total_cost)) total_cost
20413 		  ,   sum(total_response) total_response
20414                   FROM
20415           	  ( ';
20416     /* l_select1 and l_select2 contains column information common to all select statement for all view by */
20417 
20418 	IF l_object_type in ('CAMP','EVEH','CSCH') AND l_prog_cost ='BIM_APPROVED_BUDGET' and l_view_by = 'CAMPAIGN+CAMPAIGN' THEN
20419 
20420 		--l_table_bud :=  ' ,bim_i_marketing_facts facts';
20421 		--l_where_bud := ' AND facts.source_code_id = a.source_code_id';
20422 
20423 		IF l_curr_suffix is null THEN
20424 			l_prog_cost1 := 'a.budget_approved_sch';
20425 		ELSE
20426 			l_curr_suffix1 := null;
20427 			l_prog_cost1 := 'a.budget_approved_sch_s';
20428 
20429 		END IF;
20430 
20431 	ELSE
20432 
20433 		l_prog_cost1 :='a.budget_approved';
20434 
20435 	END IF;
20436 
20437 l_select1:=' , SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.responses_positive else 0 end) ptd_response,SUM(case when (cal.report_date=&BIS_CURRENT_ASOF_DATE ) then a.actual_cost'||l_curr_suffix||' else 0 end) ptd_cost,
20438 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.responses_forecasted else 0 end) ptd_forecast,SUM(case when (cal.report_date=&BIS_CURRENT_ASOF_DATE ) then a.budget_approved'||l_curr_suffix||' else 0 end) budget_approved,
20439 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.responses_positive else 0 end) p_ptd_response,SUM(case when (cal.report_date=&BIS_PREVIOUS_ASOF_DATE ) then a.actual_cost'||l_curr_suffix||' else 0 end) p_ptd_cost,
20440 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.responses_forecasted else 0 end) p_ptd_forecast,SUM(case when (cal.report_date=&BIS_PREVIOUS_ASOF_DATE )then '||l_prog_cost1||l_curr_suffix1||' else 0 end)
20444     /* l_from contains time dimension table common to all select statement for all view by */
20441 p_budget_approved,0 total_cost,0 total_forecast,0 t_budget_approved,0 total_response ';
20442  l_select2 := ' ,0  ptd_response,0 ptd_cost,0 ptd_forecast,0 budget_approved,0  p_ptd_response,0 p_ptd_cost,0 p_ptd_forecast,0  p_budget_approved,
20443 SUM(a.actual_cost'||l_curr_suffix||') total_cost,SUM(a.responses_forecasted) total_forecast,SUM('||l_prog_cost1||l_curr_suffix1||') t_budget_approved,SUM(a.responses_positive)  total_response ';
20445 l_from := ',fii_time_rpt_struct_v cal ';
20446     /* l_where contains where clause to join time dimension table common to all select statement for all view by */
20447 l_where := ' WHERE a.time_id = cal.time_id AND  a.period_type_id = cal.period_type_id  AND  cal.calendar_id= -1 ';
20448     /* l_select_filter contains group by and filter clause to remove uneccessary records with zero values */
20449 l_select_filter := ' ) GROUP BY viewbyid,name ' || l_filtercol ||l_camp_groupby_col || ')
20450 			  	     ) WHERE bim_attribute11 <> 0  ';
20451     /* get_admin_status to check current user is admin or not */
20452     l_admin_status := get_admin_status;
20453     /*********************** security handling ***********************/
20454 
20455 	IF l_campaign_id is null THEN
20456 
20457 		/******* no security checking at child level ********/
20458 		IF l_admin_status = 'N' THEN
20459 			IF l_view_by = 'CAMPAIGN+CAMPAIGN' then
20460 			/*************** program view is enable **************/
20461 				IF l_prog_view = 'Y' then
20462 					l_view := ',''RCAM''';
20463 					l_from := l_from || ',bim_i_top_objects   ac ';
20464 					l_where := l_where ||' AND a.source_code_id=ac.source_code_id
20465 					AND ac.resource_id = :l_resource_id ';
20466 					/************************************************/
20467 				ELSE
20468 					l_from := l_from ||',bim_i_top_objects ac,bim_i_source_codes src ';
20469 					l_where := l_where ||' AND a.source_code_id=src.source_code_id
20470 					AND a.source_code_id = ac.source_code_id
20471 					AND ac.resource_id = :l_resource_id
20472 					AND src.object_type NOT IN (''RCAM'')';
20473 				END IF;
20474 
20475 			ELSE
20476 				l_from := l_from ||',bim_i_top_objects ac ';
20477 				l_where := l_where ||' AND a.source_code_id=ac.source_code_id
20478 				AND ac.resource_id = :l_resource_id ';
20479 
20480 			END IF;
20481 
20482 		ELSE
20483 
20484 			IF l_view_by = 'CAMPAIGN+CAMPAIGN' then
20485 				IF l_prog_view = 'Y' THEN
20486 
20487 					l_view     := ',''RCAM''';
20488 					l_top_cond := ' AND a.immediate_parent_id is null ';
20489 
20490 				ELSE
20491 					l_top_cond := ' AND name.object_type NOT IN (''RCAM'')';
20492 
20493 				END IF;
20494 
20495 			ELSE
20496 				/******** to append parent object id is null for other view by (country and product category) ***/
20497 				l_top_cond := ' AND a.immediate_parent_id is null ';
20498 				/***********/
20499 			END IF;
20500 		END IF;
20501 	END IF;
20502     /************************************************************************/
20503     /* product category handling */
20504     IF l_cat_id is not null then
20505       l_pc_from  := ', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
20506       l_pc_where := ' AND a.category_id = edh.child_id
20507 			   AND edh.object_type = ''CATEGORY_SET''
20508 			   AND edh.object_id = mdcs.category_set_id
20509 			   AND mdcs.functional_area_id = 11
20510 			   AND edh.dbi_flag = ''Y''
20511 			   AND edh.parent_id = :l_cat_id ';
20512     ELSE
20513       l_pc_where := ' AND a.category_id = -9 ';
20514     END IF;
20515     /********************************/
20516     IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
20517       /* forming from clause for the tables which is common to all union all */
20518       if l_cat_id is not null then
20519         l_from := ' FROM BIM_I_CPB_METS_MV a ' || l_from ||l_pc_from;
20520       else
20521         l_from := ' FROM BIM_I_CPB_METS_MV a ' || l_from;
20522       end if;
20523       /* forming where clause which is common to all union all */
20524       l_where := l_where || ' AND a.object_country = :l_country ' || l_pc_where;
20525       /* forming group by clause for the common columns for all union all */
20526       l_groupby := ' GROUP BY a.source_code_id,name.object_type_mean, ';
20527       /*** campaign id null means No drill down and view by is camapign hirerachy*/
20528       IF l_campaign_id is null THEN
20529         /*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
20533                      l_select1 || l_from || ' ,bim_i_obj_name_mv name ' ||
20530         l_sqltext := l_select_cal ||
20531                     /******** inner select start from here */
20532     		   ' SELECT a.source_code_id VIEWBYID, name.name name, NAME.object_id object_id, NAME.object_type object_type,  name.object_type_mean meaning ' ||
20534                      l_where || l_top_cond ||
20535                      ' AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
20536  					   AND a.source_code_id = name.source_code_id
20537 				       AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
20538 				       AND NAME.language=USERENV(''LANG'')' || l_groupby ||
20539                      ' name.name,name.object_id,name.object_type' ||
20540                ' UNION ALL
20541   			     SELECT a.source_code_id VIEWBYID, name.name name, name.object_id object_id, name.object_type object_type,
20542       				  name.object_type_mean meaning ' || l_select2 || l_from ||
20543                       ' ,bim_i_obj_name_mv name ' || l_where ||
20544                       l_top_cond || ' AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
20545 				      AND a.source_code_id = name.source_code_id
20546 				      AND cal.report_date = trunc(sysdate)
20547 				      AND NAME.language=USERENV(''LANG'')' || l_groupby ||
20548                      ' name.name,name.object_id,name.object_type'
20549 		        ||l_select_filter;
20550       ELSE
20551         /* source_code_id is passed from the page, object selected from the page to be drill may be program,campaign,event,one off event*****/
20552         /* appending table in l_form and joining conditon for the bim_i_source_codes */
20553 
20554 	IF l_object_type='CAMP' then
20555 
20556 		l_sqltext := l_select_cal ||
20557 			    /******** inner select start from here */
20558 			   ' SELECT a.source_code_id VIEWBYID, name.name name, NAME.object_id object_id,
20559 NAME.object_type object_type,  name.object_type_mean meaning ,decode(name.activity_id,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',name.activity_id) channel ,
20560 name.child_object_usage usage ' ||
20561 			     l_select1 || l_from ||' ,bim_i_obj_name_mv name , bim_dimv_media chnl ' ||
20562 			     l_where || l_top_cond ||
20563 			     ' AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
20564 						   AND a.source_code_id = name.source_code_id
20565 						   AND name.activity_id =chnl.id (+)
20566 						   AND immediate_parent_id = '||l_campaign_id||'
20567 					       AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
20568 					       AND NAME.language=USERENV(''LANG'')' || l_groupby ||
20569 			     ' name.name,name.object_id,name.object_type , decode(name.activity_id,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',name.activity_id) , name.child_object_usage ' ||
20570 		       ' UNION ALL
20571 				     SELECT a.source_code_id VIEWBYID, name.name name, name.object_id object_id, name.object_type object_type,
20572 					  name.object_type_mean meaning ,decode(name.activity_id,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',name.activity_id) channel , name.child_object_usage usage ' || l_select2 || l_from ||
20573 			      ' ,bim_i_obj_name_mv name , bim_dimv_media chnl '
20574 			      || l_where || l_top_cond ||
20575 			      ' AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
20576 					      AND a.source_code_id = name.source_code_id
20577 					      AND name.activity_id =chnl.id (+)
20578 					      AND immediate_parent_id = '||l_campaign_id||'
20579 					      AND cal.report_date = trunc(sysdate)
20580 					      AND NAME.language=USERENV(''LANG'')' || l_groupby ||
20581 			     ' name.name,name.object_id,name.object_type , decode(name.activity_id,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',name.activity_id) , name.child_object_usage '
20582 				||l_select_filter;
20583 	ELSE
20584 		l_sqltext := l_select_cal ||
20585 			    /******** inner select start from here */
20589 			     ' AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
20586 			   ' SELECT a.source_code_id VIEWBYID, name.name name, NAME.object_id object_id, NAME.object_type object_type,  name.object_type_mean meaning ' ||
20587 			     l_select1 || l_from || ' ,bim_i_obj_name_mv name ' ||
20588 			     l_where || l_top_cond ||
20590 						   AND a.source_code_id = name.source_code_id
20591 						   AND immediate_parent_id = '||l_campaign_id||'
20592 					       AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
20593 					       AND NAME.language=USERENV(''LANG'')' || l_groupby ||
20594 			     ' name.name,name.object_id,name.object_type' ||
20595 		       ' UNION ALL
20596 				     SELECT a.source_code_id VIEWBYID, name.name name, name.object_id object_id, name.object_type object_type,
20597 					  name.object_type_mean meaning ' || l_select2 || l_from ||
20598 			      ' ,bim_i_obj_name_mv name ' || l_where ||
20599 			      l_top_cond || ' AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
20600 					      AND a.source_code_id = name.source_code_id
20601 					      AND immediate_parent_id = '||l_campaign_id||'
20602 					      AND cal.report_date = trunc(sysdate)
20603 					      AND NAME.language=USERENV(''LANG'')' || l_groupby ||
20604 			     ' name.name,name.object_id,name.object_type'
20605 				||l_select_filter;
20606 	END IF;
20607       END IF;
20608     ELSE
20609       /* view by is product category */
20610       IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') THEN
20611         if l_admin_status = 'N' then
20612           l_from := replace(l_from, ',fii_time_rpt_struct_v cal');
20613         else
20614           l_from := null;
20615         end if;
20616         /******** handling product category hirerachy ****/
20617         /* picking up value of top level node from product category denorm for category present in  bim_i_obj_mets_mv   */
20618         IF l_cat_id is null then
20619           l_from := l_from ||',eni_denorm_hierarchies edh ,mtl_default_category_sets mdcs
20620                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
20621                    FROM eni_item_vbh_nodes_v e
20622                    WHERE e.top_node_flag=''Y''
20623                    AND e.child_id = e.parent_id) p ';
20624           l_where   := l_where ||' AND a.category_id = edh.child_id
20625 			   AND edh.object_type = ''CATEGORY_SET''
20626                            AND edh.object_id = mdcs.category_set_id
20627                            AND mdcs.functional_area_id = 11
20628                            AND edh.dbi_flag = ''Y''
20629                            AND edh.parent_id = p.parent_id
20630 			   AND a.source_code_id = name.source_code_id ';
20631           l_col     := ' SELECT  /*+ORDERED*/ p.value name, p.parent_id viewbyid,
20632 		   p.leaf_node_flag leaf_node_flag,
20633 		   null meaning ';
20634           l_groupby := ' GROUP BY p.value,p.parent_id,p.leaf_node_flag ';
20635         ELSE
20636           /* passing id from page and getting immediate child to build hirerachy  */
20637           /** reassigning value to l_pc_from and l_pc_where for product category hirerachy drill down for values directly assigned to prodcut select from the page*/
20638           l_pc_from := l_from || ',(select e.id id,e.value value
20639                       from eni_item_vbh_nodes_v e
20640                       where e.parent_id =  :l_cat_id
20641                       AND e.parent_id = e.child_id
20642                       AND leaf_node_flag <> ''Y''
20643                       ) p ';
20644           l_pc_where := l_where || ' AND a.category_id = p.id ';
20645           l_from := l_from || ',eni_denorm_hierarchies edh
20646             ,mtl_default_category_sets mdc
20647             ,(select e.id,e.value,e.leaf_node_flag
20648               from eni_item_vbh_nodes_v e
20649           where
20650               e.parent_id =:l_cat_id
20651               AND e.id = e.child_id
20652               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
20653       ) p ';
20654           l_where := l_where || '
20655                   AND a.category_id = edh.child_id
20656                   AND edh.object_type = ''CATEGORY_SET''
20657                   AND edh.object_id = mdc.category_set_id
20658                   AND mdc.functional_area_id = 11
20659                   AND edh.dbi_flag = ''Y''
20660                   AND edh.parent_id = p.id
20661 		  AND a.source_code_id = name.source_code_id ';
20662           l_col     := ' SELECT /*+ORDERED*/
20663 		   p.value name,
20664                    p.id    viewbyid,
20665 		   p.leaf_node_flag leaf_node_flag,
20666 		   null    meaning ';
20667           l_groupby := ' GROUP BY p.value,p.id,p.leaf_node_flag ';
20668         END IF;
20669         /*********************/
20670         IF l_campaign_id is null then
20671           /* no drilll down in campaign hirerachy */
20672           IF l_admin_status = 'Y' THEN
20673             l_from  := ' FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a,bim_i_obj_name_mv name ' || l_from;
20674             l_where := l_where || l_top_cond ||
20675                        '   AND  a.object_country = :l_country';
20676             IF l_cat_id is not null then
20677               l_pc_from  := ' FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a,bim_i_obj_name_mv name ' || l_pc_from;
20678               l_pc_where := l_pc_where || l_top_cond ||
20679                             ' AND  a.object_country = :l_country';
20680             END IF;
20681           ELSE
20685                        ' AND  a.object_country = :l_country';
20682             l_from  := ' FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a,bim_i_obj_name_mv name ' || l_from;
20683             l_where := l_where ||
20684                       /*                         ' AND a.immediate_parent_id is null               */
20686             IF l_cat_id is not null then
20687               l_pc_from  := ' FROM fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a,bim_i_obj_name_mv name  ' || l_pc_from;
20688               l_pc_where := l_pc_where ||
20689                            /*      ' AND a.immediate_parent_id is null   */
20690                             '   AND  a.object_country = :l_country';
20691             END IF;
20692           END IF;
20693         ELSE
20694           l_from  := ' FROM   fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a ,bim_i_obj_name_mv name ' ||l_from;
20695           l_where := l_where || '  AND  a.source_code_id = :l_campaign_id  AND  a.object_country = :l_country';
20696           IF l_cat_id is not null then
20697             l_pc_from  := ' FROM   fii_time_rpt_struct_v cal,BIM_I_CPB_METS_MV a,bim_i_obj_name_mv name ' ||l_pc_from;
20698             l_pc_where := l_pc_where ||'  AND  a.source_code_id = :l_campaign_id
20699 			   AND  a.object_country = :l_country';
20700           END IF;
20701         END IF;
20702         /* building l_pc_select to get values directly assigned to product category passed from the page */
20703         IF l_cat_id is not null THEN
20704           l_pc_col     := ' SELECT /*+ORDERED*/
20705 		   bim_pmv_dbi_utl_pkg.get_lookup_value(' || '''' ||
20706                           'DASS' || '''' || ')' || ' name,
20707                    p.id  viewbyid,
20708 		   ''Y'' leaf_node_flag,
20709 		   null meaning ';
20710           l_pc_groupby := ' GROUP BY p.id ';
20711           l_pc_select := ' UNION ALL ' || l_pc_col || l_select2 ||
20712                          l_pc_from || l_pc_where ||
20713                          ' AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) ' ||
20714                          'AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id  ' ||
20715                          l_pc_groupby || ' UNION ALL ' || l_pc_col ||
20716                          l_select2 || l_pc_from || l_inner || l_pc_where ||
20717                          ' AND cal.report_date =  trunc(sysdate) ' ||
20718                          'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  ' ||
20719                          l_inr_cond || l_pc_groupby;
20720         END IF;
20721       ELSIF (l_view_by = 'GEOGRAPHY+COUNTRY') THEN
20722   	     l_inr_cond := ' and a.source_code_id = inr.source_code_id ';
20723         /** product category handling**/
20724         IF l_cat_id is null then
20725           l_where := l_where || l_pc_where;
20726         ELSE
20727           l_from  := l_from || l_pc_from;
20728           l_where := l_where || l_pc_where;
20729         END IF;
20730         l_col     := ' SELECT  decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value(' || '''' ||
20731                      'UNA' || '''' || ')' || ',d.TERRITORY_SHORT_NAME) name, a.object_country viewbyid, null meaning ';
20732         l_groupby := ' GROUP BY  decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value(' || '''' ||
20733                      'UNA' || '''' || ')' ||',d.TERRITORY_SHORT_NAME),a.object_country ';
20734         l_from    := ' FROM fnd_territories_tl  d ' || l_from;
20735         IF l_campaign_id is null then
20736           IF l_admin_status = 'Y' THEN
20737             l_from  := l_from || ' ,BIM_I_CPB_METS_MV a ';
20738             l_where := l_where || l_top_cond ||' AND  a.object_country =d.territory_code(+) AND D.language(+) = userenv(''LANG'') ';
20739           ELSE
20740             l_from  := l_from || ' ,BIM_I_CPB_METS_MV a ';
20741             l_where := l_where ||'  AND  a.object_country =d.territory_code(+)  AND D.language(+) = userenv(''LANG'') ';
20742           END IF;
20743         ELSE
20744           l_from  := l_from || ' ,BIM_I_CPB_METS_MV a ';
20745           l_where := l_where ||'  AND  a.source_code_id = :l_campaign_id AND  a.object_country =d.territory_code(+) AND D.language(+) = userenv(''LANG'') ';
20746         END IF;
20747         IF l_country <> 'N' THEN
20748           l_where := l_where || ' AND  a.object_country = :l_country';
20749         ELSE
20750           l_where := l_where || ' AND  a.object_country <> ''N''';
20751         END IF;
20752 
20753       ELSIF (l_view_by = 'MEDIA+MEDIA') THEN
20754         /** product category handling**/
20755         IF l_cat_id is null then
20756           l_where := l_where || l_pc_where;
20757         ELSE
20758           l_from  := l_from || l_pc_from;
20759           l_where := l_where || l_pc_where;
20760         END IF;
20761         l_col     := ' SELECT
20762 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value(' || '''' ||
20763                      'UNA' || '''' || ')' || ',d.value) name,
20764                      null viewbyid,
20765 		     null meaning ';
20766         l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value(' || '''' ||
20767                      'UNA' || '''' || ')' || ',d.value) ';
20768         l_from    := ' FROM bim_dimv_media d ' || l_from;
20769         IF l_campaign_id is null then
20770           IF l_admin_status = 'Y' THEN
20771             /*l_from:=l_from||' ,bim_mkt_chnl_mv a ';*/
20772             /*        ,BIM_I_CPL_CHNL_MV  can't be used since object_id ,object_type is not present*/
20773             l_from  := l_from || ' ,BIM_I_CPB_CHNL_MV a ';
20777           ELSE
20774             l_where := l_where || '  AND  d.id (+)= a.activity_id
20775 			   AND  a.immediate_parent_id is null
20776 			   AND  a.object_country = :l_country';
20778             l_from  := l_from || ' ,BIM_I_CPB_CHNL_MV a ';
20779             l_where := l_where ||
20780                       /*   ' AND  a.immediate_parent_id is null           */
20781                        '  AND  d.id (+)= a.activity_id
20782    		           AND  a.object_country = :l_country';
20783           END IF;
20784         ELSE
20785           l_from  := l_from || ' ,BIM_I_CPB_CHNL_MV a ';
20786           l_where := l_where || '  AND  a.source_code_id = :l_campaign_id
20787                            AND  d.id (+)= a.activity_id
20788 			   AND  a.object_country = :l_country';
20789         END IF;
20790 
20791       ELSIF (l_view_by = 'GEOGRAPHY+AREA') THEN
20792         /** product category handling**/
20793         IF l_cat_id is null then
20794           l_where := l_where || l_pc_where;
20795         ELSE
20796           l_from  := l_from || l_pc_from;
20797           l_where := l_where || l_pc_where;
20798         END IF;
20799         l_col     := ' SELECT decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value(' || '''' ||
20800                      'UNA' || '''' || ')' || ',d.value) name, null viewbyid, null meaning ';
20801         l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value(' || '''' ||
20802                      'UNA' || '''' || ')' || ',d.value) ';
20803         l_from    := ' FROM bis_areas_v d ' || l_from;
20804         IF l_campaign_id is null then
20805           IF l_admin_status = 'Y' THEN
20806             /*        ,BIM_I_CPL_CHNL_MV  can't be used since object_id ,object_type is not present*/
20807             l_from  := l_from || ' ,BIM_I_CPB_REGN_MV a  ';
20808             l_where := l_where || '
20809 				    AND  d.id (+)= a.object_region
20810 			            AND  a.immediate_parent_id is null
20811 			            AND  a.object_country = :l_country';
20812           ELSE
20813             l_from  := l_from || ' ,BIM_I_CPB_REGN_MV a  ';
20814             l_where := l_where ||
20815                       /*   ' AND  a.parent_object_id is null           */
20816                        '    AND  d.id (+)= a.object_region
20817    		           AND  a.object_country = :l_country';
20818           END IF;
20819         ELSE
20820           l_from  := l_from || ' ,BIM_I_CPB_REGN_MV a ';
20821           l_where := l_where || ' AND  a.source_code_id = :l_campaign_id
20822 			   AND  d.id (+)= a.object_region
20823 			   AND  a.object_country = :l_country';
20824         END IF;
20825       END IF;
20826     /* combine sql one to pick up current period values and  sql two to pick previous period values */
20827       l_select := l_col || l_select1 || l_from || l_where ||
20828                   ' AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) ' ||
20829                   'AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id ' ||
20830                   l_groupby || ' UNION ALL ' || l_col || l_select2 ||
20831                   l_from || l_inner || l_where ||
20832                   ' AND cal.report_date =  trunc(sysdate) ' ||
20833                   'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  ' ||
20834                   l_inr_cond || l_groupby || l_pc_select
20835      /* l_pc_select only applicable when product category is not all and view by is product category */
20836        ;
20837       /* prepare final sql */
20838       l_sqltext := l_select_cal1 || l_select || l_select_filter;
20839     END IF;
20840     x_custom_sql := l_sqltext;
20841     l_custom_rec.attribute_name      := ':l_record_type';
20842     l_custom_rec.attribute_value     := l_record_type_id;
20843     l_custom_rec.attribute_type      := bis_pmv_parameters_pub.bind_type;
20844     l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
20845     x_custom_output.EXTEND;
20846     x_custom_output(1) := l_custom_rec;
20847     l_custom_rec.attribute_name := ':l_resource_id';
20848     l_custom_rec.attribute_value := get_resource_id;
20849     l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
20850     l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
20851     x_custom_output.EXTEND;
20852     x_custom_output(2) := l_custom_rec;
20853     l_custom_rec.attribute_name := ':l_admin_flag';
20854     l_custom_rec.attribute_value := get_admin_status;
20855     l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
20856     l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
20857     x_custom_output.EXTEND;
20858     x_custom_output(3) := l_custom_rec;
20859     l_custom_rec.attribute_name := ':l_country';
20860     l_custom_rec.attribute_value := l_country;
20861     l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
20862     l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
20863     x_custom_output.EXTEND;
20864     x_custom_output(4) := l_custom_rec;
20865     l_custom_rec.attribute_name := ':l_cat_id';
20866     l_custom_rec.attribute_value := l_cat_id;
20867     l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
20868     l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
20869     x_custom_output.EXTEND;
20870     x_custom_output(5) := l_custom_rec;
20871     l_custom_rec.attribute_name := ':l_campaign_id';
20872     l_custom_rec.attribute_value := l_campaign_id;
20876     x_custom_output(6) := l_custom_rec;
20873     l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
20874     l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
20875     x_custom_output.EXTEND;
20877     write_debug('GET_RESP_SUM_SQL', 'QUERY', '_', l_sqltext);
20878   EXCEPTION
20879     WHEN OTHERS THEN
20880       l_sql_errm := SQLERRM;
20881       write_debug('GET_RESP_SUM_SQL', 'ERROR', l_sql_errm, l_sqltext);
20882   END GET_RESP_SUM_SQL;
20883 
20884 PROCEDURE GET_RESP_RATE_SQL (
20885    p_page_parameter_tbl   IN              bis_pmv_page_parameter_tbl,
20886    x_custom_sql           OUT NOCOPY      VARCHAR2,
20887    x_custom_output        OUT NOCOPY      bis_query_attributes_tbl
20888 )
20889 IS
20890    l_sqltext                      VARCHAR2 (20000);
20891    iflag                          NUMBER;
20892    l_period_type_hc               NUMBER;
20893    l_as_of_date                   DATE;
20894    l_period_type                  VARCHAR2 (2000);
20895    l_record_type_id               NUMBER;
20896    l_comp_type                    VARCHAR2 (2000);
20897    l_country                      VARCHAR2 (4000);
20898    l_view_by                      VARCHAR2 (4000);
20899    l_sql_errm                     VARCHAR2 (4000);
20900    l_previous_report_start_date   DATE;
20901    l_current_report_start_date    DATE;
20902    l_previous_as_of_date          DATE;
20903    l_period_type_id               NUMBER;
20904    l_user_id                      NUMBER;
20905    l_resource_id                  NUMBER;
20906    l_time_id_column               VARCHAR2 (1000);
20907    l_admin_status                 VARCHAR2 (20);
20908    l_admin_flag                   VARCHAR2 (1);
20909    l_admin_count                  NUMBER;
20910    l_rsid                         NUMBER;
20911    l_curr_aod_str                 VARCHAR2 (80);
20912    l_country_clause               VARCHAR2 (4000);
20913    l_access_clause                VARCHAR2 (4000);
20914    l_access_table                 VARCHAR2 (4000);
20915    l_cat_id                       VARCHAR2 (50)        := NULL;
20916    l_campaign_id                  VARCHAR2 (50)        := NULL;
20917    l_select                       VARCHAR2 (20000); -- to build  inner select to pick data from mviews
20918    l_pc_select                    VARCHAR2 (20000); -- to build  inner select to pick data directly assigned to the product category hirerachy
20919    l_select_cal                   VARCHAR2 (20000); -- to build  select calculation part
20920    l_select_filter                VARCHAR2 (20000); -- to build  select filter part
20921    l_from                         VARCHAR2 (20000);   -- assign common table in  clause
20922    l_where                        VARCHAR2 (20000);  -- static where clause
20923    l_groupby                      VARCHAR2 (2000);  -- to build  group by clause
20924    l_pc_from                      VARCHAR2 (20000);   -- from clause to handle product category
20925    l_pc_where                     VARCHAR2 (20000);   --  where clause to handle product category
20926    l_filtercol                    VARCHAR2 (2000);
20927    l_pc_col                       VARCHAR2(200);
20928    l_pc_groupby                   VARCHAR2(200);
20929    l_view                         VARCHAR2 (20);
20930    l_comm_cols                    VARCHAR2 (20000);
20931    l_view_disp                    VARCHAR2(100);
20932    l_url_str                      VARCHAR2(1000);
20933    l_url_str_csch                 varchar2(1000);
20934    l_url_str_type                 varchar2(3000);
20935    l_url_str_csch_jtf             varchar2(3000);
20936    l_camp_sel_col                 varchar2(100);
20937    l_camp_groupby_col             varchar2(100);
20938    l_csch_chnl                    varchar2(100);
20939    l_top_cond                     VARCHAR2(100);
20940    l_comm_cols2                   VARCHAR2 (20000);
20941    l_meaning                      VARCHAR2 (20);
20942    /* variables to hold columns names in l_select clauses */
20943    l_col                          VARCHAR2(1000);
20944    /* cursor to get type of object passed from the page ******/
20945     cursor get_obj_type
20946     is
20947     select object_type
20948     from bim_i_source_codes
20949     where source_code_id=replace(l_campaign_id,'''');
20950     /*********************************************************/
20951    l_custom_rec                   bis_query_attributes;
20952    l_object_type                  varchar2(30);
20953    l_inner                        varchar2(5000);
20954    l_inr_cond                     varchar2(5000);
20955    l_p_inner                      varchar2(5000);
20956    l_p_inr_cond                   varchar2(5000);
20957    l_select_cal1                  VARCHAR2 (20000);
20958    l_select1                      VARCHAR2 (20000);
20959    l_select2                      VARCHAR2 (20000);
20960    l_select3                      VARCHAR2 (20000);
20961    l_url_str_tga                  varchar2(3000);
20962    l_camp_url                     varchar2(500);
20963    l_url_link                     varchar2(200);
20964    l_url_camp1                    varchar2(3000);
20965    l_url_camp2                    varchar2(3000);
20966    l_url_camp3                    varchar2(3000);
20967    l_dass                         varchar2(100);  -- variable to store value for  directly assigned lookup value
20968    l_leaf_node_flag               varchar2(25);   -- variable to store value leaf_node_flag column in case of product category
20969    l_curr VARCHAR2(50);
20970    l_curr_suffix VARCHAR2(50);
20974    l_media VARCHAR2(300);
20971    l_col_id NUMBER;
20972    l_area VARCHAR2(300);
20973    l_report_name VARCHAR2(300);
20975 BEGIN
20976    x_custom_output := bis_query_attributes_tbl ();
20977    l_custom_rec := bis_pmv_parameters_pub.initialize_query_type;
20978   bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
20979    			     			      l_as_of_date,
20980 			    			      l_period_type,
20981 				                      l_record_type_id,
20982 				                      l_comp_type,
20983 				                      l_country,
20984 						      l_view_by,
20985 						      l_cat_id,
20986 						      l_campaign_id,
20987 						      l_curr,
20988 						      l_col_id,
20989 						      l_area,
20990 						      l_media,
20991 						      l_report_name
20992 				                      );
20993    l_meaning:=' null meaning '; -- assigning default value
20994    l_url_camp1:=',null';
20995    l_url_camp2:=',null';
20996    l_url_camp3:=',null';
20997    IF l_country IS NULL THEN
20998       l_country := 'N';
20999    END IF;
21000 /************Start Inner Query to get current acitve objects *************************/
21001 l_inner:=', ( select distinct  name.object_id,name.object_type
21002 from BIM_I_obj_METS_MV a, bim_i_obj_name_mv name
21003 ,fii_time_rpt_struct_v cal';
21004 IF l_admin_status='N' THEN
21005   l_inner:=l_inner||',bim_i_top_objects  r ';
21006 end if;
21007 IF l_cat_id is not null then
21008   l_inner := l_inner ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
21009 end if;
21010  l_inner := l_inner || ' WHERE a.source_code_id = name.source_code_id
21011 And a.time_id=cal.time_id
21012 AND a.period_type_id=cal.period_type_id
21013 AND cal.calendar_id=-1
21014 AND cal.report_date   in (&BIS_CURRENT_ASOF_DATE)
21015 AND a.object_country = :l_country
21016 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
21017 and (responses_positive >0)';
21018 
21019 IF l_admin_status = 'N' THEN
21020   l_inner :=  l_inner||' AND a.source_code_id = r.source_code_id AND r.resource_id = :l_resource_id ';
21021 END IF;
21022 
21023 IF l_cat_id is null then
21024  l_inner :=  l_inner ||' AND a.category_id = -9 ';
21025 else
21026    l_inner :=  l_inner ||' AND a.category_id = edh.child_id
21027 			   AND edh.object_type = ''CATEGORY_SET''
21028 			   AND edh.object_id = mdcs.category_set_id
21029 			   AND mdcs.functional_area_id = 11
21030 			   AND edh.dbi_flag = ''Y''
21031 			   AND edh.parent_id = :l_cat_id ';
21032   end if;
21033 l_inner :=  l_inner ||' ) inr ';
21034 
21035 l_inr_cond:='and name.object_id= inr.object_id
21036              and name.object_type=inr.object_type ';
21037 /************ End Inner Query to get current acitve objects *************************/
21038 /************Start Inner Query to get Previous  acitve objects *************************/
21039 l_p_inner:=', ( select distinct  name.object_id,name.object_type
21040 from BIM_I_obj_METS_MV a, bim_i_obj_name_mv name
21041 ,fii_time_rpt_struct_v cal';
21042 
21043 IF l_admin_status='N' THEN
21044   l_p_inner:=l_p_inner||', bim_i_top_objects r ';
21045 end if;
21046 
21047 IF l_cat_id is not null then
21048   l_p_inner := l_p_inner ||',eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
21049 end if;
21050 
21051  l_p_inner := l_p_inner || ' WHERE a.source_code_id = name.source_code_id
21052 and a.time_id=cal.time_id
21053 AND a.period_type_id=cal.period_type_id
21054 AND cal.calendar_id=-1
21055 AND cal.report_date   in (&BIS_PREVIOUS_ASOF_DATE)
21056 AND a.object_country = :l_country
21057 AND BITAND(cal.record_type_id,:l_record_type)=cal.record_type_id
21058 and (responses_positive >0)';
21059 
21060 IF l_admin_status = 'N' THEN
21061   l_p_inner :=  l_p_inner||' AND a.source_code_id  = r.source_code_id  AND r.resource_id = :l_resource_id ';
21062 END IF;
21063 
21064 IF l_cat_id is null then
21065  l_p_inner :=  l_p_inner ||' AND a.category_id = -9 ';
21066 else
21067    l_p_inner :=  l_p_inner ||' AND a.category_id = edh.child_id
21068 			   AND edh.object_type = ''CATEGORY_SET''
21069 			   AND edh.object_id = mdcs.category_set_id
21070 			   AND mdcs.functional_area_id = 11
21071 			   AND edh.dbi_flag = ''Y''
21072 			   AND edh.parent_id = :l_cat_id ';
21073   end if;
21074 
21075 l_p_inner :=  l_p_inner ||' ) p_inr ';
21076 
21077 l_p_inr_cond:='and name.object_id= p_inr.object_id   and name.object_type=p_inr.object_type ';
21078 /************ End Inner Query to get Previous  acitve objects *************************/
21079 /** to add meaning in select clause only in case of campaign view by */
21080 
21081   IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
21082   l_meaning:=' meaning ';
21083   l_filtercol:=',meaning ';
21084   ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
21085   l_filtercol:=',leaf_node_flag ';
21086   l_meaning:=' null meaning';
21087   else
21088   l_meaning:=' null meaning ';
21092 -- checking for the object type passed from page
21089   end if;
21090 
21091  if l_campaign_id is not null then
21093 
21094  for i in get_obj_type
21095  loop
21096  l_object_type:=i.object_type;
21097  end loop;
21098 
21099 end if;
21100    l_url_str_tga  :='pFunctionName=AMS_LIST_UPDATE_PG&retainAM=Y&MidTab=ChartsRN&addBreadCrumb=Y&NavMode=UPD&OAPB=AMS_AUDIENCE_USER_BRANDING&ListHeaderId=';
21101    l_url_str  :='pFunctionName=BIM_I_MKTG_RESP_RATE_PHP&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
21102  --  l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objId=';
21103    l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
21104    l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
21105    l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
21106    &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
21107 ---------------------------------------------------------------------------
21108 ---------------------------------------------------------------------------
21109    IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
21110 ---------------------------------------------------------------------------
21111 ---------------------------------------------------------------------------
21112       l_url_link :=' ,decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) ';
21113       l_view_disp:='viewby';
21114       l_leaf_node_flag :=' ,leaf_node_flag ';
21115    ELSIF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
21116      l_camp_sel_col :=' ,object_id ,object_type   ';
21117      l_camp_groupby_col :=',object_id,object_type ';
21118      l_url_link := ' ,null ';
21119      l_view_disp := 'viewby';
21120      IF (l_campaign_id is  null or l_object_type='RCAM') then
21121 	l_url_camp1:=', decode(object_type,''EONE'',NULL,'||''''||l_url_str||''''||' )';
21122      ELSIF l_object_type='CAMP' THEN
21123 	l_url_camp2:=', '||''''||l_url_str_type||''''||'||object_id';
21124     --  l_url_camp1:=',decode(usage,''LITE'','||''''||l_url_str_csch||''''||'||object_id,'||''''||l_url_str_csch_jtf||''''||'||object_id)';
21125     l_url_camp1 := ', '||''''||l_url_str_csch||''''||'||object_id ';
21126 	l_url_camp3:=',decode(usage,''LITE'',decode(list_header_id,null,null,'||''''||l_url_str_tga||''''||'||list_header_id),NULL)';
21127 	l_csch_chnl:='|| '' - '' || channel';
21128 	l_camp_sel_col :=l_camp_sel_col|| ',usage,channel,list_header_id';
21129 	l_camp_groupby_col :=l_camp_groupby_col||',usage,channel,list_header_id';
21130      end if;
21131     ELSE
21132      -- l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
21133       l_url_link:=' ,null ';
21134       l_view_disp:='viewby';
21135    END IF;
21136 /* l_select_cal is common part of select statement for all view by to calculate grand totals and change */
21137  l_select_cal :='
21138         SELECT '||
21139          l_view_disp ||'
21140 	 , viewbyid
21141  	 ,  BIM_ATTRIBUTE1'||l_csch_chnl||'    BIM_ATTRIBUTE1
21142  	 , BIM_ATTRIBUTE2 ,BIM_ATTRIBUTE8 ,BIM_ATTRIBUTE5,BIM_ATTRIBUTE7
21143  	 ,RESPONSE_RATE BIM_ATTRIBUTE9
21144          , DECODE(PREV_RESPONSE_RATE,0,NULL,((RESPONSE_RATE - PREV_RESPONSE_RATE)/PREV_RESPONSE_RATE)*100) BIM_ATTRIBUTE10'||	 l_url_link || ' bim_url1'||	 l_url_camp1||
21145 	 ' bim_url2 '||	 l_url_camp2||' bim_url3 '||
21146 	 l_url_camp3||' BIM_URL4
21147 	 , BIM_GRAND_TOTAL4 ,BIM_GRAND_TOTAL7, BIM_GRAND_TOTAL6, BIM_GRAND_TOTAL1 ,BIM_GRAND_TOTAL8
21148 	 , decode(PREV_RESPONSE_RATE_TOT,0,null,((BIM_GRAND_TOTAL8- PREV_RESPONSE_RATE_TOT)/PREV_RESPONSE_RATE_TOT)*100) BIM_GRAND_TOTAL9
21149 	 FROM
21150 	 (
21151          SELECT  name    VIEWBY,
21152 	    VIEWBYID,
21153             meaning BIM_ATTRIBUTE1'||l_camp_sel_col||' ,
21154             CUSTOMERS_TARGETED BIM_ATTRIBUTE5, total_response BIM_ATTRIBUTE7,
21155             ptd_response BIM_ATTRIBUTE2,DECODE(Prev_ptd_response,0,NULL,((ptd_response - Prev_ptd_response)/Prev_ptd_response)*100) BIM_ATTRIBUTE8,
21156             decode(CUSTOMERS_TARGETED,0,NULL,(total_response/CUSTOMERS_TARGETED)*100) RESPONSE_RATE,
21157             decode(P_CUSTOMERS_TARGETED,0,NULL,(prev_total_response/P_CUSTOMERS_TARGETED*100)) PREV_RESPONSE_RATE,
21158             decode(SUM(P_CUSTOMERS_TARGETED) OVER(),0,NULL,(SUM(prev_total_response) OVER()/SUM(P_CUSTOMERS_TARGETED) OVER() *100)) PREV_RESPONSE_RATE_TOT,
21159             sum(CUSTOMERS_TARGETED) over() BIM_GRAND_TOTAL4,sum(total_response) over() BIM_GRAND_TOTAL6,sum(ptd_response) over() BIM_GRAND_TOTAL1,
21160             decode(sum(Prev_ptd_response) over(),0,null,(((sum(ptd_response- Prev_ptd_response) over())/sum(Prev_ptd_response)over ())*100))     BIM_GRAND_TOTAL7,
21161             decode(sum(CUSTOMERS_TARGETED) over(),0,null,((sum(total_response) over()/sum(CUSTOMERS_TARGETED)over ())*100)) BIM_GRAND_TOTAL8
21162 	     FROM
21163               (
21164                  SELECT viewbyid,name,'||l_meaning||l_camp_sel_col||  ',
21165 		  SUM(ptd_response) ptd_response,case when SUM(ptd_response) > 0 then SUM(CUSTOMERS_TARGETED) else 0 end CUSTOMERS_TARGETED,
21166 	          case when SUM(p_ptd_response) > 0 then SUM(CUSTOMERS_TARGETED) else 0 end P_CUSTOMERS_TARGETED,
21167                   SUM(p_ptd_response)      Prev_PTD_response,case when SUM(ptd_response)>0 then  Sum(total_response) else 0 end total_response  ,
21171 /*  change this  below query  */
21168 		  case when SUM(p_ptd_response)>0 then Sum(total_response) else 0 end prev_total_response
21169              FROM
21170           	  ( ';
21172 l_select_cal1 :='
21173          SELECT '||
21174          l_view_disp ||'
21175 	, viewbyid
21176  	 , BIM_ATTRIBUTE1 ,BIM_ATTRIBUTE8,BIM_ATTRIBUTE2,BIM_ATTRIBUTE5,BIM_ATTRIBUTE7,RESPONSE_RATE BIM_ATTRIBUTE9
21177        , decode(prev_response_rate,0,null,((response_rate - prev_response_rate)/prev_response_rate)*100) bim_attribute10'||
21178 	  l_url_link|| ' bim_url1'||'
21179 	  ,null BIM_URL2 ,null BIM_URL3 ,null BIM_URL4,BIM_GRAND_TOTAL4,BIM_GRAND_TOTAL7,BIM_GRAND_TOTAL1
21180  	 , BIM_GRAND_TOTAL6,BIM_GRAND_TOTAL8
21181           ,decode(PREV_RESPONSE_RATE_TOT,0,null,((BIM_GRAND_TOTAL8- PREV_RESPONSE_RATE_TOT)/PREV_RESPONSE_RATE_TOT)*100) BIM_GRAND_TOTAL9
21182 	  FROM
21183 	 (
21184             SELECT   name    VIEWBY'||l_leaf_node_flag||',
21185 	    VIEWBYID,  meaning BIM_ATTRIBUTE1, CUSTOMERS_TARGETED BIM_ATTRIBUTE5, total_response BIM_ATTRIBUTE7,
21186             ptd_response BIM_ATTRIBUTE2, DECODE(Prev_ptd_response,0,NULL,((ptd_response - Prev_ptd_response)/Prev_ptd_response)*100) BIM_ATTRIBUTE8,
21187             decode(CUSTOMERS_TARGETED,0,NULL,(total_response/CUSTOMERS_TARGETED)*100) RESPONSE_RATE,
21188             decode(p_customers_targeted,0,NULL,(prev_total_response/(p_customers_targeted)*100)) PREV_RESPONSE_RATE,
21189             decode(SUM(p_customers_targeted) OVER(),0,NULL,(SUM(prev_total_response) OVER()/SUM(p_customers_targeted) OVER() *100)) PREV_RESPONSE_RATE_TOT,
21190             sum(CUSTOMERS_TARGETED) over() BIM_GRAND_TOTAL4,sum(total_response) over() BIM_GRAND_TOTAL6,sum(ptd_response) over() BIM_GRAND_TOTAL1,
21191             decode(sum(Prev_ptd_response) over(),0,null,(((sum(ptd_response- Prev_ptd_response) over())/sum(Prev_ptd_response)over ())*100)) BIM_GRAND_TOTAL7,
21192             decode(sum(CUSTOMERS_TARGETED) over(),0,null,((sum(total_response) over()/sum(CUSTOMERS_TARGETED)over ())*100)) BIM_GRAND_TOTAL8
21193 	    FROM
21194               (
21195                  SELECT viewbyid, name,'||l_meaning||l_leaf_node_flag||
21196 		 ', SUM(ptd_response) ptd_response,SUM(CUSTOMERS_TARGETED)  CUSTOMERS_TARGETED,
21197                  SUM(p_ptd_response) Prev_PTD_response, SUM(total_response)  total_response,
21198 		 SUM ( prev_total_response)   prev_total_response  ,   SUM(p_customers_targeted) p_customers_targeted
21199              FROM
21200           	  ( ';
21201 /* l_select1 and l_select2 contains column information common to all select statement for all view by */
21202 l_select1:=
21203 ' , SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.responses_positive else 0 end) ptd_response,
21204 SUM(case when cal.report_date=&BIS_CURRENT_ASOF_DATE then a.CUSTOMERS_TARGETED else 0 end) ptd_CUSTOMERS_TARGETED,
21205 SUM(case when cal.report_date=&BIS_PREVIOUS_ASOF_DATE then a.responses_positive else 0 end) p_ptd_response,
21206 0  customers_targeted,
21207 0 total_response,
21208 0  p_customers_targeted ,
21209 0  prev_total_response ';
21210  l_select2 :=
21211                    ' ,0  ptd_response,
21212 0 ptd_customers_targeted,
21213 0  p_ptd_response,
21214 sum(customers_targeted) customers_targeted,
21215 SUM(responses_positive)  total_response,
21216 0  p_customers_targeted ,
21217 0  prev_total_response '   ;
21218 l_select3 :=
21219                    ' ,0  ptd_response,
21220 0  ptd_customers_targeted,
21221 0  p_ptd_response,
21222 0  customers_targeted,
21223 0  total_response,
21224 sum(customers_targeted)  p_customers_targeted ,
21225 SUM(responses_positive)  prev_total_response '   ;
21226 /* l_from contains time dimension table common to all select statement for all view by */
21227  l_from  :=',fii_time_rpt_struct_v cal ';
21228  /* l_where contains where clause to join time dimension table common to all select statement for all view by */
21229  l_where :=' WHERE a.time_id = cal.time_id
21230              AND  a.period_type_id = cal.period_type_id
21231             AND  cal.calendar_id= -1 ';
21232  /* l_select_filter contains group by and filter clause to remove uneccessary records with zero values */
21233 l_select_filter := ' ) GROUP BY viewbyid,name '||l_filtercol||l_camp_groupby_col||
21234                   ')
21235 		   )
21236       WHERE
21237        bim_attribute2 <> 0
21238        or bim_attribute5 <> 0
21239         &ORDER_BY_CLAUSE ';
21240  /* get_admin_status to check current user is admin or not */
21241    l_admin_status := get_admin_status;
21242    /*********************** security handling ***********************/
21243      IF   l_campaign_id is null THEN /******* no security checking at child level ********/
21244      IF   l_admin_status = 'N' THEN
21245 	  IF  l_view_by = 'CAMPAIGN+CAMPAIGN' then
21246 	  /*************** program view is enable **************/
21247 	     IF l_prog_view='Y' then
21248 	        l_view := ',''RCAM''';
21249 	        l_from := l_from ||',bim_i_top_objects   ac ';
21250 			l_where := l_where ||' AND a.source_code_id=ac.source_code_id
21251 	 		AND ac.resource_id = :l_resource_id ';
21252 	  /************************************************/
21253 	      ELSE
21254 		l_from := l_from ||',ams_act_access_denorm ac,bim_i_source_codes src ';
21255 		l_where := l_where ||' AND a.source_code_id=src.source_code_id
21256 		AND src.object_id=ac.object_id
21257 		AND src.object_type=ac.object_type
21258 		AND ac.resource_id = :l_resource_id
21259 		AND src.object_type NOT IN (''RCAM'')';
21260               END IF;
21261            ELSE
21265 	  END IF;
21262 			l_from := l_from ||',bim_i_top_objects ac ';
21263 			l_where := l_where ||' AND a.source_code_id=ac.source_code_id
21264 			AND ac.resource_id = :l_resource_id ';
21266 	ELSE
21267 	     IF l_view_by = 'CAMPAIGN+CAMPAIGN' then
21268 	        IF  l_prog_view='Y' THEN
21269 				l_view := ',''RCAM''';
21270 				l_top_cond :=' AND a.immediate_parent_id is null ';
21271 
21272 			ELSE
21273 
21274 				l_top_cond := ' AND name.object_type NOT IN (''RCAM'')';
21275 
21276 		END IF;
21277 	      ELSE
21278       	  /******** to append parent object id is null for other view by (country and product category) ***/
21279 		l_top_cond :=' AND a.immediate_parent_id is null ';
21280 	  /***********/
21281 	      END IF;
21282        END IF;
21283    END IF;
21284 /************************************************************************/
21285    /* product category handling */
21286      IF  l_cat_id is not null then
21287          l_pc_from   :=  ', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
21288 	 l_pc_where  :=  ' AND a.category_id = edh.child_id
21289 			   AND edh.object_type = ''CATEGORY_SET''
21290 			   AND edh.object_id = mdcs.category_set_id
21291 			   AND mdcs.functional_area_id = 11
21292 			   AND edh.dbi_flag = ''Y''
21293 			   AND edh.parent_id = :l_cat_id ';
21294        ELSE
21295         l_pc_where :=     ' AND a.category_id = -9 ';
21296      END IF;
21297 /********************************/
21298 ---------------------------------------------------------------------------
21299 ---------------------------------------------------------------------------
21300     IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
21301 ---------------------------------------------------------------------------
21302 ---------------------------------------------------------------------------
21303      /* forming from clause for the tables which is common to all union all */
21304      if l_cat_id is not null then
21305      l_from :=' FROM BIM_I_obj_METS_MV a,bim_i_obj_name_mv name '||l_from||l_pc_from;
21306      else
21307      l_from :=' FROM BIM_I_obj_METS_MV a,bim_i_obj_name_mv name '||l_from;
21308      end if;
21309       /* forming where clause which is common to all union all */
21310      l_where :=l_where||' and a.source_code_id =name.source_code_id
21311 		AND name.language=USERENV(''LANG'')
21312 		AND a.object_country = :l_country '||
21313 		 l_pc_where;
21314     /* forming group by clause for the common columns for all union all */
21315     l_groupby:=' GROUP BY a.source_code_id,name.object_type_mean,name.name,name.object_id,name.object_type ';
21316  /*** campaign id null means No drill down and view by is camapign hirerachy*/
21317   IF l_campaign_id is null THEN
21318    /*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
21319      l_sqltext:= l_select_cal||
21320      /******** inner select start from here */
21321      /* select to get camapigns and programs for current period values */
21322      ' SELECT
21323       a.source_code_id VIEWBYID,
21324       name.name name , name.object_id object_id, name.object_type object_type,
21325       name.object_type_mean meaning '||
21326       l_select1 ||
21327       l_from ||
21328      l_where ||l_top_cond||
21329     ' AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
21330       AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
21331       l_groupby||
21332        ' UNION ALL      /* select to get camapigns and programs for previous period values */
21333      SELECT
21334       a.source_code_id VIEWBYID,
21335       name.name name , name.object_id object_id, name.object_type object_type,
21336       name.object_type_mean meaning '||
21337       l_select2 ||
21338       l_from ||
21339      l_where ||l_top_cond||
21340     ' AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
21341       AND cal.report_date =trunc(sysdate) '||
21342       l_groupby|| l_select_filter /* appending filter clause */
21343       ;
21344  ELSE
21345  /* source_code_id is passed from the page, object selected from the page to be drill may be program,campaign,event,one off event*****/
21346 /* appending table in l_form and joining conditon for the bim_i_source_codes */
21347      l_where :=l_where ||
21348               ' AND a.immediate_parent_id = :l_campaign_id ';
21349 -- if program is selected from the page means it may have childern as programs,campaigns,events or one off events
21350  IF l_object_type in ('RCAM','EVEH') THEN
21351  /*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
21352      l_sqltext:= l_select_cal||
21353      /******** inner select start from here */
21354      /* select to get camapigns and programs for current period values */
21355      ' SELECT
21356       a.source_code_id VIEWBYID,
21357       name.name name , name.object_id object_id, name.object_type object_type,
21358       name.object_type_mean meaning '||
21359       l_select1 ||
21360       l_from ||
21361      l_where ||
21362     ' AND BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
21363       AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)' ||
21364       l_groupby||
21365      ' UNION ALL      /* select to get camapigns and programs for previous period values */
21366      SELECT
21367       a.source_code_id VIEWBYID,
21371       l_from ||
21368       name.name name , name.object_id object_id, name.object_type object_type,
21369       name.object_type_mean meaning '||
21370       l_select2 ||
21372      l_where ||
21373     ' AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
21374       AND cal.report_date = trunc(sysdate)'||
21375       l_groupby||
21376       l_select_filter ;
21377       /*************** if object type is camp then childern are campaign schedules ***/
21378  ELSIF l_object_type='CAMP' THEN
21379 	 l_sqltext:= l_select_cal||
21380 	     /******** inner select start from here */
21381 	     /* select to get camapign schedules for current period values */
21382 	     ' SELECT
21383 	      a.source_code_id VIEWBYID, name.object_id object_id, name.object_type object_type,
21384 	      name.name name,
21385 	      name.child_object_usage usage,
21386 	      aal.list_header_id list_header_id,
21387               decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel,
21388 	      name.object_type_mean meaning '||
21389 	      l_select1 ||
21390 	      l_from || ' ,bim_dimv_media chnl,ams_act_lists aal '||
21391 	     l_where ||
21392 	    ' AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
21393 	      AND name.activity_id =chnl.id (+)
21394 	      AND  name.object_id = aal.list_used_by_id (+)
21395               AND  aal.list_act_type(+) = ''TARGET''
21396               AND  aal.list_used_by(+) = ''CSCH''
21397 	      AND cal.report_date  in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
21398 	      '||
21399 	      l_groupby||
21400 	      ' ,name.child_object_usage,aal.list_header_id,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
21401      ' UNION ALL      /* select to get camapign schedules for previous period values */
21402      SELECT
21403       a.source_code_id VIEWBYID, name.object_id object_id, name.object_type object_type,
21404       name.name name,
21405       name.child_object_usage usage,
21406       aal.list_header_id list_header_id,
21407       decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel,
21408       name.object_type_mean meaning  '||
21409       l_select2 ||
21410       l_from || ' ,bim_dimv_media chnl,ams_act_lists aal  '||
21411      l_where ||
21412     ' AND  BITAND(cal.record_type_id,1143)= cal.record_type_id
21413       AND name.activity_id =chnl.id (+)
21414       AND name.object_id = aal.list_used_by_id (+)
21415       AND  aal.list_act_type(+) = ''TARGET''
21416       AND  aal.list_used_by(+) = ''CSCH''
21417       AND cal.report_date = trunc(sysdate) '||
21418       l_groupby||
21419       ' ,name.child_object_usage,aal.list_header_id,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
21420       l_select_filter ;
21421  END IF;
21422  END IF;
21423  /***** END CAMPAIGN HIRERACHY VIEW HANDLING ******************/
21424  ELSE
21425  /* view by is product category */
21426 ---------------------------------------------------------------------------
21427 ---------------------------------------------------------------------------
21428  IF (l_view_by ='ITEM+ENI_ITEM_VBH_CAT') THEN
21429 ---------------------------------------------------------------------------
21430 ---------------------------------------------------------------------------
21431    if l_admin_status='N' then
21432      l_from:=replace(l_from,',fii_time_rpt_struct_v cal');
21433    else
21434      l_from:=null;
21435     end if;
21436 /******** handling product category hirerachy ****/
21437 /* picking up value of top level node from product category denorm for category present in  bim_i_obj_mets_mv   */
21438     IF l_cat_id is null then
21439        l_from:=l_from||
21440                ',eni_denorm_hierarchies edh
21441                 ,mtl_default_category_sets mdcs
21442                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
21443                    FROM eni_item_vbh_nodes_v e
21444                    WHERE e.top_node_flag=''Y''
21445                    AND e.child_id = e.parent_id) p ';
21446        l_where := l_where||
21447                          ' AND a.source_code_id = name.source_code_id
21448 			   AND a.category_id = edh.child_id
21449 			   AND edh.object_type = ''CATEGORY_SET''
21450                            AND edh.object_id = mdcs.category_set_id
21451                            AND mdcs.functional_area_id = 11
21452                            AND edh.dbi_flag = ''Y''
21453                            AND edh.parent_id = p.parent_id';
21454        l_col:=' SELECT /*+ORDERED*/
21455 		   p.value name,
21456                    p.parent_id viewbyid,
21457 		   p.leaf_node_flag leaf_node_flag,
21458 		   null meaning ';
21459         l_groupby := ' GROUP BY p.value,p.parent_id,p.leaf_node_flag ';
21460     ELSE
21461     /* passing id from page and getting immediate child to build hirerachy  */
21462     /** reassigning value to l_pc_from and l_pc_where for product category hirerachy drill down for values directly assigned to prodcut select from the page*/
21463      l_pc_from:= l_from||
21464                    ',(select e.id id,e.value value
21465                       from eni_item_vbh_nodes_v e
21466                       where e.parent_id =  :l_cat_id
21467                       AND e.parent_id = e.child_id
21468                       AND leaf_node_flag <> ''Y''
21469                       ) p ';
21470     l_pc_where :=l_where||
21471                       ' AND a.category_id = p.id ';
21472     l_from:= l_from||
21473             ',eni_denorm_hierarchies edh
21474             ,mtl_default_category_sets mdc
21475             ,(select e.id,e.value,e.leaf_node_flag
21479               AND e.id = e.child_id
21476               from eni_item_vbh_nodes_v e
21477           where
21478               e.parent_id =:l_cat_id
21480               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
21481       ) p ';
21482      l_where := l_where||' AND a.source_code_id = name.source_code_id
21483                   AND a.category_id = edh.child_id
21484                   AND edh.object_type = ''CATEGORY_SET''
21485                   AND edh.object_id = mdc.category_set_id
21486                   AND mdc.functional_area_id = 11
21487                   AND edh.dbi_flag = ''Y''
21488                   AND edh.parent_id = p.id ';
21489      l_col:=' SELECT /*+ORDERED*/
21490 		   p.value name,
21491                    p.id viewbyid,
21492 		   p.leaf_node_flag leaf_node_flag,
21493 		   null meaning ';
21494      l_groupby := ' GROUP BY p.value,p.id,p.leaf_node_flag ';
21495     END IF;
21496 /*********************/
21497            IF l_campaign_id is null then /* no drilll down in campaign hirerachy */
21498 	      IF l_admin_status ='Y' THEN
21499               l_from:=' FROM fii_time_rpt_struct_v cal,BIM_I_obj_METS_MV a,bim_i_obj_name_mv name
21500                               '||l_from;
21501               l_where := l_where ||l_top_cond||
21502                          '  And a.source_code_id =   name.source_code_id  AND  a.object_country = :l_country';
21503                IF l_cat_id is not null then
21504 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,BIM_I_obj_METS_MV a,bim_i_obj_name_mv name
21505                               '||l_pc_from;
21506 		  l_pc_where := l_pc_where ||l_top_cond||
21507                          '  And a.source_code_id =   name.source_code_id AND  a.object_country = :l_country';
21508                END IF;
21509               ELSE
21510               l_from:=' FROM fii_time_rpt_struct_v cal,BIM_I_obj_METS_MV a,bim_i_obj_name_mv name
21511                              '||l_from;
21512               l_where := l_where ||
21513 /*                         ' And a.source_code_id =   name.source_code_id
21514 			    AND a.parent_object_id is null               */
21515 			  '  And a.source_code_id =   name.source_code_id AND  a.object_country = :l_country';
21516 		IF l_cat_id is not null then
21517 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,BIM_I_obj_METS_MV a,bim_i_obj_name_mv name
21518                               '||l_pc_from;
21519 		  l_pc_where := l_pc_where ||
21520                    /*      ' AND a.parent_object_id is null   */
21521 			'  And a.source_code_id =   name.source_code_id AND  a.object_country = :l_country';
21522                END IF;
21523               END IF;
21524            ELSE
21525               l_from := ' FROM   fii_time_rpt_struct_v cal,BIM_I_obj_METS_MV a,bim_i_obj_name_mv name '||l_from ;
21526               l_where  := l_where ||
21527                         '  And a.source_code_id =   name.source_code_id
21528                            AND  a.source_code_id = :l_campaign_id
21529 			   AND  a.object_country = :l_country' ;
21530               IF l_cat_id is not null then
21531 	      l_pc_from := ' FROM  fii_time_rpt_struct_v cal, BIM_I_obj_METS_MV a,bim_i_obj_name_mv name '||l_pc_from ;
21532               l_pc_where  := l_pc_where ||
21533                         '  And a.source_code_id = Name.source_code_id
21534                            AND  a.source_code_id = :l_campaign_id
21535 			   AND  a.object_country = :l_country' ;
21536 	      END IF;
21537 	   END IF;
21538    /* building l_pc_select to get values directly assigned to product category passed from the page */
21539    IF l_cat_id is not null  THEN
21540 	  l_pc_col:=' SELECT  /*+ORDERED*/
21541 		   bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||' name,
21542                    p.id  viewbyid,
21543 		   ''Y'' leaf_node_flag,
21544 		   null meaning ';
21545      l_pc_groupby := ' GROUP BY p.id ';
21546   l_pc_select :=
21547               ' UNION ALL ' ||
21548               l_pc_col||
21549               l_select1||
21550 	      l_pc_from||
21551 	      l_pc_where ||' AND cal.report_date in ( &BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
21552 	                   'AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
21553                     and a.responses_positive>0  '||
21554 	      l_pc_groupby ||
21555 	      ' UNION ALL ' ||
21556 	      l_pc_col||
21557 	      l_select2||
21558 	      l_pc_from||l_inner||
21559 	      l_pc_where ||' AND cal.report_date = trunc(sysdate) '||
21560 	                   'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  '||
21561 			   l_inr_cond||
21562 	      l_pc_groupby  ||
21563 	        ' UNION ALL ' ||
21564 	      l_pc_col||
21565 	      l_select3||
21566 	      l_pc_from||l_p_inner||
21567 	      l_pc_where ||' AND cal.report_date = trunc(sysdate) '||
21568 	                   'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  '||
21569 			  l_p_inr_cond||
21570 	      l_pc_groupby;
21571    END IF;
21572 ---------------------------------------------------------------------------
21573 ---------------------------------------------------------------------------
21574  ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
21575 ---------------------------------------------------------------------------
21576 ---------------------------------------------------------------------------
21577    /** product category handling**/
21578    IF l_cat_id is null then
21579      l_where := l_where ||l_pc_where;
21583   END IF;
21580   ELSE
21581      l_from  := l_from ||l_pc_from;
21582      l_where := l_where||l_pc_where;
21584     l_col:=' SELECT
21585 		    decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
21586                      a.object_country viewbyid,
21587 		     null meaning ';
21588     l_groupby := ' GROUP BY  decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country ';
21589     l_from:=' FROM fnd_territories_tl  d '||l_from;
21590 	  IF l_campaign_id is null then
21591 	      IF l_admin_status ='Y' THEN
21592 	      l_from:=l_from||' ,BIM_I_obj_METS_MV a ,bim_i_obj_name_mv name';
21593               l_where := l_where ||l_top_cond||
21594                          '   AND a.source_code_id = name.source_code_id
21595 			 AND  a.object_country =d.territory_code(+)
21596 			     AND D.language(+) = userenv(''LANG'') ';
21597               ELSE
21598               l_from:=l_from||' ,BIM_I_obj_METS_MV a,bim_i_obj_name_mv name ';
21599               l_where := l_where ||
21600 	                 '   AND a.source_code_id = name.source_code_id
21601 			 AND  a.object_country =d.territory_code(+)
21602 			     AND D.language(+) = userenv(''LANG'') ';
21603               END IF;
21604             ELSE
21605               l_from := l_from||' ,BIM_I_obj_METS_MV a,bim_i_obj_name_mv name ';
21606               l_where  := l_where ||
21607                         '  AND	a.source_code_id = name.source_code_id
21608                            AND  a.source_code_id = :l_campaign_id
21609                            AND  a.object_country =d.territory_code(+)
21610 			   AND D.language(+) = userenv(''LANG'') ';
21611 	  END IF;
21612 	  IF  l_country <>'N' THEN
21613 	      l_where  := l_where ||' AND  a.object_country = :l_country';
21614           ELSE
21615 	   l_where  := l_where ||' AND  a.object_country <> ''N''';
21616 	  END IF;
21617 ---------------------------------------------------------------------------
21618 ---------------------------------------------------------------------------
21619 ELSIF (l_view_by ='MEDIA+MEDIA') THEN
21620 ---------------------------------------------------------------------------
21621 ---------------------------------------------------------------------------
21622 /** product category handling**/
21623    IF l_cat_id is null then
21624      l_where := l_where ||l_pc_where;
21625   ELSE
21626      l_from  := l_from ||l_pc_from;
21627      l_where := l_where||l_pc_where;
21628   END IF;
21629     l_col:=' SELECT
21630 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
21631                      null viewbyid,
21632 		     null meaning ';
21633     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
21634     l_from:=' FROM bim_dimv_media d '||l_from;
21635 	  IF l_campaign_id is null then
21636 	      IF l_admin_status ='Y' THEN
21637 	      /*l_from:=l_from||' ,bim_mkt_chnl_mv a';*/
21638 /*	      ,BIM_I_CPL_CHNL_MV  can't be used since object_id ,object_type is not present*/
21639 	      l_from:=l_from||' ,BIM_obj_CHNL_MV a,bim_i_obj_name_mv name ';
21640               l_where := l_where ||' AND a.source_code_id = name.source_code_id
21641 			   AND  d.id (+)= a.activity_id
21642 			   AND a.immediate_parent_id is null
21643 			   AND  a.object_country = :l_country';
21644               ELSE
21645               l_from:=l_from||' ,BIM_obj_CHNL_MV  a,bim_i_obj_name_mv name ';
21646               l_where := l_where ||' AND a.source_code_id = name.source_code_id
21647 			   AND  d.id (+)= a.activity_id
21648    		           AND  a.object_country = :l_country';
21649               END IF;
21650             ELSE
21651               l_from := l_from||' ,BIM_obj_CHNL_MV  a,bim_i_obj_name_mv name ';
21652               l_where  := l_where ||'  AND a.source_code_id = name.source_code_id
21653                            AND  a.source_code_id = :l_campaign_id
21654                            AND  d.id (+)= a.activity_id
21655 			   AND  a.object_country = :l_country';
21656 	  END IF;
21657 ---------------------------------------------------------------------------
21658 ---------------------------------------------------------------------------
21659 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
21660 ---------------------------------------------------------------------------
21661 ---------------------------------------------------------------------------
21662 /** product category handling**/
21663    IF l_cat_id is null then
21664      l_where := l_where ||l_pc_where;
21665   ELSE
21666      l_from  := l_from ||l_pc_from;
21667      l_where := l_where||l_pc_where;
21668   END IF;
21669     l_col:=' SELECT
21670 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
21671                      null viewbyid,
21672 		     null meaning ';
21673     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
21674     l_from:=' FROM bis_areas_v d '||l_from;
21675 	  IF l_campaign_id is null then
21676 	      IF l_admin_status ='Y' THEN
21677 	      /*l_from:=l_from||' ,bim_mkt_chnl_mv a ';*/
21678 /*	      ,BIM_I_CPL_CHNL_MV  can't be used since object_id ,object_type is not present*/
21679 	      l_from:=l_from||' ,BIM_obj_REGN_MV a,bim_i_obj_name_mv name ';
21680               l_where := l_where ||
21684 			   AND  a.object_country = :l_country';
21681                          ' AND a.source_code_id = name.source_code_id
21682 			   AND  d.id (+)= a.object_region
21683 			   AND  a.immediate_parent_id is null
21685               ELSE
21686               l_from:=l_from||' ,BIM_obj_REGN_MV a,bim_i_obj_name_mv name ';
21687               l_where := l_where ||
21688 	              /*   ' AND  a.parent_object_id is null           */
21689                          ' AND a.source_code_id = name.source_code_id
21690 			   AND  d.id (+)= a.object_region
21691    		           AND  a.object_country = :l_country';
21692               END IF;
21693             ELSE
21694               l_from := l_from||' ,BIM_obj_REGN_MV a,bim_i_obj_name_mv name';
21695               l_where  := l_where ||
21696                         '  AND a.source_code_id = name.source_code_id
21697                            AND  a.source_code_id = :l_campaign_id
21698                            AND  d.id (+)= a.object_region
21699 			   AND  a.object_country = :l_country';
21700 	  END IF;
21701 END IF;
21702 /* combine sql one to pick up current period values and  sql two to pick previous period values */
21703   l_select := l_col||
21704               l_select1||
21705 	      l_from||
21706 	      l_where ||' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
21707 	      'AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
21708           and a.responses_positive>0   '||
21709 	      l_groupby ||
21710 	      ' UNION ALL ' ||
21711 	      l_col||
21712 	      l_select2||
21713 	      l_from||l_inner||
21714 	      l_where ||' AND cal.report_date =trunc(sysdate) '||
21715 	      'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  '||
21716 	      l_inr_cond||
21717 	      l_groupby  /* ||
21718 	       ' UNION ALL ' ||
21719 	      l_col||
21720 	      l_select3||
21721 	      l_from|| l_p_inner||
21722 	      l_where ||' AND cal.report_date =trunc(sysdate) '||
21723 	      'AND  BITAND(cal.record_type_id,1143)= cal.record_type_id  '||
21724 	      l_p_inr_cond||
21725 	      l_groupby||
21726               l_pc_select*/ /* l_pc_select only applicable when product category is not all and view by is product category */
21727 	      ;
21728 /* prepare final sql */
21729  l_sqltext:= l_select_cal1||
21730              l_select||
21731 	     l_select_filter;
21732   END IF;
21733    x_custom_sql := l_sqltext;
21734    l_custom_rec.attribute_name := ':l_record_type';
21735    l_custom_rec.attribute_value := l_record_type_id;
21736    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
21737    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
21738    x_custom_output.EXTEND;
21739    x_custom_output (1) := l_custom_rec;
21740    l_custom_rec.attribute_name := ':l_resource_id';
21741    l_custom_rec.attribute_value := get_resource_id;
21742    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
21743    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
21744    x_custom_output.EXTEND;
21745    x_custom_output (2) := l_custom_rec;
21746    l_custom_rec.attribute_name := ':l_admin_flag';
21747    l_custom_rec.attribute_value := get_admin_status;
21748    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
21749    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
21750    x_custom_output.EXTEND;
21751    x_custom_output (3) := l_custom_rec;
21752    l_custom_rec.attribute_name := ':l_country';
21753    l_custom_rec.attribute_value := l_country;
21754    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
21755    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
21756    x_custom_output.EXTEND;
21757    x_custom_output (4) := l_custom_rec;
21758    l_custom_rec.attribute_name := ':l_cat_id';
21759    l_custom_rec.attribute_value := l_cat_id;
21760    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
21761    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
21762    x_custom_output.EXTEND;
21763    x_custom_output (5) := l_custom_rec;
21764    l_custom_rec.attribute_name := ':l_campaign_id';
21765    l_custom_rec.attribute_value := l_campaign_id;
21766    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
21767    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
21768    x_custom_output.EXTEND;
21769    x_custom_output (6) := l_custom_rec;
21770    write_debug ('GET_RESP_RATE_SQL', 'QUERY', '_', l_sqltext);
21771 EXCEPTION
21772    WHEN OTHERS
21773    THEN
21774       l_sql_errm := SQLERRM;
21775       write_debug ('GET_RESP_RATE_SQL', 'ERROR', l_sql_errm, l_sqltext);
21776 END GET_RESP_RATE_SQL;
21777 
21778 
21779 PROCEDURE GET_WON_OPTY_SQL (
21780    p_page_parameter_tbl   IN              bis_pmv_page_parameter_tbl,
21781    x_custom_sql           OUT NOCOPY      VARCHAR2,
21782    x_custom_output        OUT NOCOPY      bis_query_attributes_tbl
21783 )
21784 IS
21785    l_sqltext                      VARCHAR2 (20000);
21786    iflag                          NUMBER;
21787    l_period_type_hc               NUMBER;
21788    l_as_of_date                   DATE;
21789    l_period_type                  VARCHAR2 (2000);
21790    l_record_type_id               NUMBER;
21791    l_comp_type                    VARCHAR2 (2000);
21792    l_country                      VARCHAR2 (4000);
21793    l_view_by                      VARCHAR2 (4000);
21794    l_sql_errm                     VARCHAR2 (4000);
21798    l_period_type_id               NUMBER;
21795    l_previous_report_start_date   DATE;
21796    l_current_report_start_date    DATE;
21797    l_previous_as_of_date          DATE;
21799    l_user_id                      NUMBER;
21800    l_resource_id                  NUMBER;
21801    l_time_id_column               VARCHAR2 (1000);
21802    l_admin_status                 VARCHAR2 (20);
21803    l_admin_flag                   VARCHAR2 (1);
21804    l_admin_count                  NUMBER;
21805    l_rsid                         NUMBER;
21806    l_curr_aod_str                 VARCHAR2 (80);
21807    l_country_clause               VARCHAR2 (4000);
21808    l_access_clause                VARCHAR2 (4000);
21809    l_access_table                 VARCHAR2 (4000);
21810    l_cat_id                       VARCHAR2 (50)        := NULL;
21811    l_campaign_id                  VARCHAR2 (50)        := NULL;
21812    l_select                       VARCHAR2 (20000); -- to build  inner select to pick data from mviews
21813    l_pc_select                    VARCHAR2 (20000); -- to build  inner select to pick data directly assigned to the product category hirerachy
21814    l_select_cal                   VARCHAR2 (20000); -- to build  select calculation part
21815    l_select_filter                VARCHAR2 (20000); -- to build  select filter part
21816    l_from                         VARCHAR2 (20000);   -- assign common table in  clause
21817    l_where                        VARCHAR2 (20000);  -- static where clause
21818    l_groupby                      VARCHAR2 (2000);  -- to build  group by clause
21819    l_pc_from                      VARCHAR2 (20000);   -- from clause to handle product category
21820    l_pc_where                     VARCHAR2 (20000);   --  where clause to handle product category
21821    l_filtercol                    VARCHAR2 (2000);
21822    l_pc_col                       VARCHAR2(200);
21823    l_pc_groupby                   VARCHAR2(200);
21824    l_view                         VARCHAR2 (20);
21825    l_comm_cols                    VARCHAR2 (20000);
21826    l_view_disp                    VARCHAR2(100);
21827    l_url_str                      VARCHAR2(3000);
21828    l_url_str_csch                 varchar2(3000);
21829    l_url_str_csch_jtf             varchar2(3000);
21830    l_url_str_type                 varchar2(3000);
21831    l_camp_sel_col                 varchar2(100);
21832    l_camp_groupby_col             varchar2(100);
21833    l_csch_chnl                    varchar2(100);
21834    l_top_cond                     VARCHAR2(100);
21835    l_meaning                      VARCHAR2 (20);
21836    /* variables to hold columns names in l_select clauses */
21837    l_col                          VARCHAR2(1000);
21838    /* cursor to get type of object passed from the page ******/
21839     cursor get_obj_type
21840     is
21841     select object_type
21842     from bim_i_source_codes
21843     where source_code_id=replace(l_campaign_id,'''');
21844     /*********************************************************/
21845    l_custom_rec                   bis_query_attributes;
21846    l_object_type                  varchar2(30);
21847    l_url_link                     varchar2(200) ;
21848    l_url_camp1                    varchar2(3000);
21849    l_url_camp2                    varchar2(3000);
21850    l_dass                         varchar2(100);  -- variable to store value for  directly assigned lookup value
21851    l_leaf_node_flag               varchar2(100);   -- variable to store value leaf_node_flag column in case of product category
21852    l_curr VARCHAR2(50);
21853    l_curr_suffix VARCHAR2(50);
21854    l_col_id NUMBER;
21855    l_area VARCHAR2(300);
21856    l_report_name VARCHAR2(300);
21857    l_media VARCHAR2(300);
21858 BEGIN
21859    x_custom_output := bis_query_attributes_tbl ();
21860    l_custom_rec := bis_pmv_parameters_pub.initialize_query_type;
21861 bim_pmv_dbi_utl_pkg.get_bim_page_params(p_page_parameter_tbl,
21862    			     			      l_as_of_date,
21863 			    			      l_period_type,
21864 				                      l_record_type_id,
21865 				                      l_comp_type,
21866 				                      l_country,
21867 						      l_view_by,
21868 						      l_cat_id,
21869 						      l_campaign_id,
21870 						      l_curr,
21871 						      l_col_id,
21872 						      l_area,
21873 						      l_media,
21874 						      l_report_name
21875 				                      );
21876    l_meaning:=' null meaning '; -- assigning default value
21877    l_url_camp1:=',null';
21878    l_url_camp2:=',null';
21879 IF (l_curr = '''FII_GLOBAL1''')
21880     THEN l_curr_suffix := '';
21881   ELSIF (l_curr = '''FII_GLOBAL2''')
21882     THEN l_curr_suffix := '_s';
21883     ELSE l_curr_suffix := '';
21884   END IF;
21885 
21886    IF l_country IS NULL THEN
21887       l_country := 'N';
21888    END IF;
21889 /** to add meaning in select clause only in case of campaign view by */
21890   IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
21891   l_meaning:=' ,meaning ';
21892   l_filtercol:=',meaning ';
21893   ELSIF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
21894   l_filtercol:=',leaf_node_flag ';
21898   end if;
21895   l_meaning:=',null meaning ';
21896   else
21897   l_meaning:=' ,null meaning ';
21899    /*** to  assigned URL **/
21900 
21901 if l_campaign_id is not null then
21902 -- checking for the object type passed from page
21903  for i in get_obj_type
21904  loop
21905  l_object_type:=i.object_type;
21906  end loop;
21907 end if;
21908 
21909    l_url_str  :='pFunctionName=BIM_I_MKTG_WON_OPTY&pParamIds=Y&VIEW_BY='||l_view_by||'&VIEW_BY_NAME=VIEW_BY_ID';
21910    ---l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&omomode=UPDATE&MidTab=TargetAccDSCRN&searchType=customize&OA_SubTabIdx=3&retainAM=Y&addBreadCrumb=S&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objId=';
21911    l_url_str_csch :='pFunctionName=AMS_WB_CSCH_UPDATE&pParamIds=Y&VIEW_BY='||l_view_by||'&objType=CSCH&objId=';
21912    l_url_str_type :='pFunctionName=AMS_WB_CSCH_RPRT&addBreadCrumb=Y&OAPB=AMS_CAMP_WORKBENCH_BRANDING&objType=CSCH&objId=';
21913    l_url_str_csch_jtf :='pFunctionName=BIM_I_CSCH_START_DRILL&pParamIds=Y&VIEW_BY='||l_view_by||'&PAGE.OBJ.ID_NAME1=customSetupId&VIEW_BY_NAME=VIEW_BY_ID
21914    &PAGE.OBJ.ID1=1&PAGE.OBJ.objType=CSCH&PAGE.OBJ.objAttribute=DETL&PAGE.OBJ.ID_NAME0=objId&PAGE.OBJ.ID0=';
21915 
21916 IF (l_view_by = 'ITEM+ENI_ITEM_VBH_CAT') then
21917    l_url_link :=' ,decode(leaf_node_flag,''Y'',null,'||''''||l_url_str||''''||' ) ';
21918    l_view_disp:='viewby';
21919    l_leaf_node_flag :=' ,leaf_node_flag ';
21920 
21921  ELSIF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
21922      l_camp_sel_col :=' ,object_id
21923                        ,object_type
21924                        ';
21925      l_camp_groupby_col :=',object_id,object_type ';
21926      l_url_link := ' ,null ';
21927      l_view_disp := 'viewby';
21928 
21929      IF (l_campaign_id is  null or l_object_type='RCAM') then
21930 	l_url_camp1:=', decode(object_type,''EONE'',NULL,'||''''||l_url_str||''''||' )';
21931      ELSIF l_object_type='CAMP' THEN
21932 	l_url_camp2:=','||''''||l_url_str_type||''''||'||object_id';
21933 	--l_url_camp1:=',decode(usage,''LITE'','||''''||l_url_str_csch||''''||'||object_id,'||''''||l_url_str_csch_jtf||''''||'||object_id)';
21934 	l_url_camp1:=', '||''''||l_url_str_csch||''''||'||object_id ';
21935 	l_csch_chnl:='|| '' - '' || channel';
21936 	l_camp_sel_col :=l_camp_sel_col|| ',usage,channel';
21937 	l_camp_groupby_col :=l_camp_groupby_col||',usage,channel';
21938      end if;
21939     ELSE
21940     --  l_una := BIM_PMV_DBI_UTL_PKG.GET_LOOKUP_VALUE('UNA');
21941       l_url_link:=' ,null ';
21942       l_view_disp:='viewby';
21943    END IF;
21944 
21945 /* l_select_cal is common part of select statement for all view by to calculate grand totals and change */
21946  l_select_cal :='
21947          SELECT '||
21948          l_view_disp ||'
21949 	 ,viewbyid
21950  	 ,bim_attribute2'||l_csch_chnl ||' bim_attribute2
21951  	 ,bim_attribute3
21952  	 ,bim_attribute4
21953  	 ,bim_attribute5
21954  	 ,bim_attribute6
21955  	 ,bim_attribute7
21956  	 ,bim_attribute8
21957 	 ,bim_attribute9
21958 	 ,bim_attribute5 bim_attribute10
21959 	 ,bim_attribute7 bim_attribute11
21960 	 ,bim_attribute6 bim_attribute12
21961 	 ,bim_attribute8 bim_attribute13
21962 	 ,bim_attribute5 bim_attribute14
21963 	 '||l_url_link||' bim_attribute19'||
21964 	 l_url_camp1|| ' bim_attribute20 '||
21965 	 l_url_camp2||' bim_attribute21
21966  	 ,bim_grand_total1
21967  	 ,bim_grand_total2
21971  	 ,bim_grand_total6
21968  	 ,bim_grand_total3
21969  	 ,bim_grand_total4
21970  	 ,bim_grand_total5
21972  	 ,bim_grand_total7
21973 	 ,bim_grand_total3 bim_grand_total8
21974           FROM
21975 	 (
21976             SELECT
21977             name    VIEWBY '||l_leaf_node_flag||'
21978             ,meaning BIM_ATTRIBUTE2'||l_camp_sel_col||
21979            ' ,new_opportunity_amt BIM_ATTRIBUTE3
21980             ,DECODE(prev_new_opportunity_amt,0,NULL,((new_opportunity_amt - prev_new_opportunity_amt)/prev_new_opportunity_amt)*100) BIM_ATTRIBUTE4
21981             ,won_opportunity_amt BIM_ATTRIBUTE5
21982             ,DECODE(prev_won_opportunity_amt,0,NULL,((won_opportunity_amt - prev_won_opportunity_amt)/prev_won_opportunity_amt)*100) BIM_ATTRIBUTE6
21983             ,lost_opportunity_amt BIM_ATTRIBUTE7
21984             ,DECODE(prev_lost_opportunity_amt,0,NULL,((lost_opportunity_amt - prev_lost_opportunity_amt)/prev_lost_opportunity_amt)*100) BIM_ATTRIBUTE8
21985             ,DECODE(lost_opportunity_amt,0,NULL,won_opportunity_amt/lost_opportunity_amt) BIM_ATTRIBUTE9
21986 	    ,sum(new_opportunity_amt) over() BIM_GRAND_TOTAL1
21987 	    ,case
21988               when sum(prev_new_opportunity_amt) over()=0 then null
21989               else
21990                    ((sum(new_opportunity_amt) over()-sum(prev_new_opportunity_amt) over ()) /sum(prev_new_opportunity_amt)over () )*100
21991             end  BIM_GRAND_TOTAL2
21992             ,sum(won_opportunity_amt) over() BIM_GRAND_TOTAL3
21993 	    ,case
21994               when sum(prev_won_opportunity_amt) over()=0 then null
21995               else
21996                    ((sum(won_opportunity_amt) over()-sum(prev_won_opportunity_amt) over ()) /sum(prev_won_opportunity_amt)over () )*100
21997             end  BIM_GRAND_TOTAL4
21998             ,sum(lost_opportunity_amt) over() BIM_GRAND_TOTAL5
21999 	    ,case
22000               when sum(prev_lost_opportunity_amt) over()=0 then null
22001               else
22002                    ((sum(lost_opportunity_amt) over()-sum(prev_lost_opportunity_amt) over ()) /sum(prev_lost_opportunity_amt)over () )*100
22003             end  BIM_GRAND_TOTAL6
22004             ,DECODE(sum(lost_opportunity_amt) over (),0,NULL,sum(won_opportunity_amt) over()/sum(lost_opportunity_amt)over()) BIM_GRAND_TOTAL7
22005 	    ,VIEWBYID
22006              FROM
22007               (
22008                   SELECT
22009           	      viewbyid,
22010           	      name'||l_leaf_node_flag||
22011 		      l_meaning||l_camp_sel_col||
22012 		    ',sum(new_opportunity_amt)       new_opportunity_amt
22013           	      ,sum(won_opportunity_amt)       won_opportunity_amt
22014           	      ,sum(lost_opportunity_amt)      lost_opportunity_amt
22015           	      ,sum(prev_new_opportunity_amt)  prev_new_opportunity_amt
22016           	      ,sum(prev_won_opportunity_amt)  prev_won_opportunity_amt
22017           	      ,sum(prev_lost_opportunity_amt) prev_lost_opportunity_amt
22018                   FROM
22019           	  ( ';
22020 /* l_comm_cols  contains column information common to all select statement for all view by */
22021 
22022 l_comm_cols:=    ' , sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.new_opportunity_amt'||l_curr_suffix||',0))  new_opportunity_amt,
22023                    sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.won_opportunity_amt'||l_curr_suffix||',0))  won_opportunity_amt,
22024      		   sum(DECODE(cal.report_date,&BIS_CURRENT_ASOF_DATE,a.lost_opportunity_amt'||l_curr_suffix||',0)) lost_opportunity_amt,
22025      		   sum(DECODE(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.new_opportunity_amt'||l_curr_suffix||',0)) prev_new_opportunity_amt,
22026 		   sum(DECODE(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.won_opportunity_amt'||l_curr_suffix||',0)) prev_won_opportunity_amt,
22027 		   sum(DECODE(cal.report_date,&BIS_PREVIOUS_ASOF_DATE,a.lost_opportunity_amt'||l_curr_suffix||',0)) prev_lost_opportunity_amt ';
22028 /* l_from contains time dimension table common to all select statement for all view by */
22029  l_from  :=',fii_time_rpt_struct_v cal ';
22030  /* l_where contains where clause to join time dimension table common to all select statement for all view by */
22031  l_where :=' WHERE a.time_id = cal.time_id
22032              AND  a.period_type_id = cal.period_type_id
22033              AND  BITAND(cal.record_type_id,:l_record_type)= cal.record_type_id
22034              AND  cal.calendar_id= -1 ';
22035  /* l_select_filter contains group by and filter clause to remove uneccessary records with zero values */
22036 l_select_filter := ' ) GROUP BY viewbyid,name '||l_filtercol||l_camp_groupby_col||
22037                   ')
22038 		   )
22039          WHERE
22040            bim_attribute3 <> 0
22041           or bim_attribute5 <> 0
22042           or bim_attribute7 <> 0
22043           &ORDER_BY_CLAUSE ';
22044 
22045    /* get_admin_status to check current user is admin or not */
22046 
22047    l_admin_status := get_admin_status;
22048 
22049 /*********************** security handling ***********************/
22050 
22051       IF   l_campaign_id is null THEN /******* no security checking at child level ********/
22052      IF   l_admin_status = 'N' THEN
22053 	  IF  l_view_by = 'CAMPAIGN+CAMPAIGN' then
22054 	  /*************** program view is enable **************/
22055 	     IF l_prog_view='Y' then
22056 	        l_from := l_from ||',bim_i_top_objects ac ' ;
22057 		 l_where := l_where ||' AND a.source_code_id=ac.source_code_id
22058 		  AND ac.resource_id = :l_resource_id ';
22059 	  /************************************************/
22060 	      ELSE
22064 		 AND src.object_type=ac.object_type
22061 	         l_from := l_from ||',ams_act_access_denorm ac,bim_i_source_codes src ';
22062 		 l_where := l_where ||' AND a.source_code_id=src.source_code_id
22063 		 AND src.object_id=ac.object_id
22065 		 AND src.object_type NOT IN (''RCAM'')
22066 	     AND ac.resource_id = :l_resource_id ';
22067               END IF;
22068            ELSE
22069             l_from := l_from ||',bim_i_top_objects ac ';
22070             l_where := l_where ||' AND a.source_code_id=ac.source_code_id
22071 	    AND ac.resource_id = :l_resource_id ';
22072 	  END IF;
22073 	ELSE
22074 	     IF l_view_by = 'CAMPAIGN+CAMPAIGN' then
22075 	        IF  l_prog_view='Y' THEN
22076 		    l_top_cond :=' AND a.immediate_parent_id is null ';
22077 		ELSE
22078 			l_top_cond :=  ' AND name.object_type NOT IN (''RCAM'')';
22079 		END IF;
22080 	      ELSE
22081       	  /******** to append parent object id is null for other view by (country and product category) ***/
22082 		l_top_cond := ' AND a.immediate_parent_id is null ';
22083 	  /***********/
22084 	      END IF;
22085        END IF;
22086    END IF;
22087 /************************************************************************/
22088 
22089    /* product category handling */
22090      IF  l_cat_id is not null then
22091          l_pc_from   :=  ', eni_denorm_hierarchies edh,mtl_default_category_sets mdcs';
22092 	 l_pc_where  :=  ' AND a.category_id = edh.child_id
22093 			   AND edh.object_type = ''CATEGORY_SET''
22094 			   AND edh.object_id = mdcs.category_set_id
22095 			   AND mdcs.functional_area_id = 11
22096 			   AND edh.dbi_flag = ''Y''
22097 			   AND edh.parent_id = :l_cat_id ';
22098        ELSE
22099         l_pc_where :=     ' AND a.category_id = -9 ';
22100      END IF;
22101 /********************************/
22102 
22103     IF (l_view_by = 'CAMPAIGN+CAMPAIGN') THEN
22104 
22105      /* forming from clause for the tables which is common to all union all */
22106      if l_cat_id is not null then
22107      l_from :=' FROM bim_i_obj_mets_mv a,bim_i_obj_name_mv name '||l_from||l_pc_from;
22108      else
22109      l_from :=' FROM bim_i_obj_mets_mv a,bim_i_obj_name_mv name '||l_from;
22110      end if;
22111 
22112 
22113       /* forming where clause which is common to all union all */
22114      l_where :=l_where||' AND a.source_code_id = name.source_code_id
22115 		 AND a.object_country = :l_country '||
22116 		 l_pc_where;
22117 
22118 
22119     /* forming group by clause for the common columns for all union all */
22120     l_groupby:=' GROUP BY a.source_code_id,name.object_type_mean, ';
22121 
22122  /*** campaign id null means No drill down and view by is camapign hirerachy*/
22123   IF l_campaign_id is null THEN
22124    /*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
22125      l_sqltext:= l_select_cal||
22126      /******** inner select start from here */
22127      /* select to get camapigns and programs  */
22128      ' SELECT
22129       a.source_code_id VIEWBYID,
22130       name.name name,
22131        name.object_id object_id,
22132       name.object_type object_type,
22133       name.object_type_mean meaning '||
22134       l_comm_cols ||
22135       l_from ||
22136      l_where ||l_top_cond||
22137      ' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
22138       AND name.language=USERENV(''LANG'')'||
22139       l_groupby||
22140       ' name.name,name.object_id,name.object_type'||
22141        l_select_filter /* appending filter clause */
22142       ;
22143  ELSE
22144  /* source_code_id is passed from the page, object selected from the page to be drill may be program,campaign,event,one off event*****/
22145 /* appending table in l_form and joining conditon for the bim_i_source_codes */
22146 
22147      l_where :=l_where ||' AND a.immediate_parent_id=:l_campaign_id ';
22148 
22149 -- if program is selected from the page means it may have childern as programs,campaigns,events or one off events
22150 
22151  IF l_object_type in ('RCAM','EVEH') THEN
22152  /*appending l_select_cal for calculation and sql clause to pick data and filter clause to filter records with zero values***/
22153      l_sqltext:= l_select_cal||
22154      /******** inner select start from here */
22155      /* select to get camapigns and programs  */
22156      ' SELECT
22157       a.source_code_id VIEWBYID,
22158       name.name name,
22159        name.object_id object_id,
22160       name.object_type object_type,
22161       name.object_type_mean meaning '||
22162       l_comm_cols ||
22163       l_from ||
22164      l_where ||
22165     ' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
22166       AND name.language=USERENV(''LANG'')'||
22167       l_groupby||
22168       ' name.name,name.object_id,name.object_type'||
22169       l_select_filter ;
22170       /*************** if object type is camp then childern are campaign schedules ***/
22171  ELSIF l_object_type='CAMP' THEN
22172  l_sqltext:= l_select_cal||
22173      /******** inner select start from here */
22174      /* select to get camapign schedules  */
22175      ' SELECT
22176       a.source_code_id VIEWBYID,
22177       name.name name,
22178       name.object_id object_id,
22179       name.object_type object_type,
22180       name.child_object_usage usage,
22184       l_from || ' , bim_dimv_media chnl '||
22181       decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value) channel,
22182       name.object_type_mean meaning '||
22183       l_comm_cols ||
22185      l_where ||
22186     ' AND name.activity_id =chnl.id (+)
22187       AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)
22188       AND name.language=USERENV(''LANG'')'||
22189       l_groupby||
22190       ' name.name,name.object_id,name.object_type,name.child_object_usage,decode(chnl.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',chnl.value)'||
22191       l_select_filter ;
22192  END IF;
22193 
22194  END IF;
22195 
22196  /***** END CAMPAIGN HIRERACHY VIEW HANDLING ******************/
22197 
22198  ELSE
22199  /* view by is product category */
22200  IF (l_view_by ='ITEM+ENI_ITEM_VBH_CAT') THEN
22201 --changing l_from to have fii_time_rpt_struct_v first table in form clause to provide ordered hint in sql
22202   if l_admin_status='N' then
22203  l_from:=replace(l_from,',fii_time_rpt_struct_v cal');
22204  else
22205  l_from:=null;
22206  end if;
22207 /******** handling product category hirerachy ****/
22208 /* picking up value of top level node from product category denorm for category present in  bim_i_obj_mets_mv   */
22209     IF l_cat_id is null then
22210        l_from:=l_from||
22211                ',eni_denorm_hierarchies edh
22212                 ,mtl_default_category_sets mdcs
22213                 ,( SELECT e.parent_id parent_id ,e.value value,e.leaf_node_flag leaf_node_flag
22214                    FROM eni_item_vbh_nodes_v e
22215                    WHERE e.top_node_flag=''Y''
22216                    AND e.child_id = e.parent_id) p ';
22217        l_where := l_where||
22218                          ' AND a.category_id = edh.child_id
22219 			   AND edh.object_type = ''CATEGORY_SET''
22220                            AND edh.object_id = mdcs.category_set_id
22221                            AND mdcs.functional_area_id = 11
22222                            AND edh.dbi_flag = ''Y''
22223                            AND edh.parent_id = p.parent_id';
22224        l_col:=' SELECT /*+ORDERED*/
22225 		   p.value name,
22226                    p.parent_id viewbyid,
22227 		   p.leaf_node_flag leaf_node_flag,
22228 		   null meaning ';
22229         l_groupby := ' GROUP BY p.value,p.parent_id,p.leaf_node_flag ';
22230     ELSE
22231     /* passing id from page and getting immediate child to build hirerachy  */
22232 
22233     /** reassigning value to l_pc_from and l_pc_where for product category hirerachy drill down for values directly assigned to prodcut select from the page*/
22234 
22235      l_pc_from:= l_from||
22236                    ',(select e.id id,e.value value
22237                       from eni_item_vbh_nodes_v e
22238                       where e.parent_id =  :l_cat_id
22239                       AND e.parent_id = e.child_id
22240                       AND leaf_node_flag <> ''Y''
22241                       ) p ';
22242 
22243     l_pc_where :=l_where||
22244                       ' AND a.category_id = p.id ';
22245 
22246     l_from:= l_from||
22247             ',eni_denorm_hierarchies edh
22248             ,mtl_default_category_sets mdc
22249             ,(select e.id,e.value,e.leaf_node_flag leaf_node_flag
22250               from eni_item_vbh_nodes_v e
22251           where
22252               e.parent_id =:l_cat_id
22253               AND e.id = e.child_id
22254               AND((e.leaf_node_flag=''N'' AND e.parent_id<>e.id) OR e.leaf_node_flag=''Y'')
22255       ) p ';
22256 
22257      l_where := l_where||'
22258                   AND a.category_id = edh.child_id
22259                   AND edh.object_type = ''CATEGORY_SET''
22260                   AND edh.object_id = mdc.category_set_id
22261                   AND mdc.functional_area_id = 11
22262                   AND edh.dbi_flag = ''Y''
22263                   AND edh.parent_id = p.id ';
22264 
22265      l_col:=' SELECT /*+ORDERED*/
22266 		   p.value name,
22267                    p.id viewbyid,
22268 		   p.leaf_node_flag leaf_node_flag,
22269 		   null meaning ';
22270      l_groupby := ' GROUP BY p.value,p.id,p.leaf_node_flag ';
22271     END IF;
22272 /*********************/
22273 
22274            IF l_campaign_id is null then /* no drilll down in campaign hirerachy */
22275 	      IF l_admin_status ='Y' THEN
22276               l_from:=' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
22277                               '||l_from;
22278               l_where := l_where ||l_top_cond||
22279                          '  AND  a.object_country = :l_country';
22280                IF l_cat_id is not null then
22281 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
22282                               '||l_pc_from;
22283 		  l_pc_where := l_pc_where ||l_top_cond||
22287               l_from:=' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
22284                          ' AND  a.object_country = :l_country';
22285                END IF;
22286               ELSE
22288                              '||l_from;
22289               l_where := l_where ||
22290 	                   ' AND  a.object_country = :l_country';
22291 
22292 		IF l_cat_id is not null then
22293 	          l_pc_from := ' FROM fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a
22294                               '||l_pc_from;
22295 		  l_pc_where := l_pc_where ||
22296                          ' AND  a.object_country = :l_country';
22297                END IF;
22298 
22299               END IF;
22300            ELSE
22301               l_from := ' FROM   fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a '||l_from ;
22302               l_where  := l_where ||
22303                         '  AND  a.source_code_id = :l_campaign_id
22304 			   AND  a.object_country = :l_country' ;
22305               IF l_cat_id is not null then
22306 	      l_pc_from := ' FROM     fii_time_rpt_struct_v cal,bim_i_obj_mets_mv a '||l_pc_from ;
22307               l_pc_where  := l_pc_where ||
22308                         '  AND  a.source_code_id = :l_campaign_id
22309 			   AND  a.object_country = :l_country' ;
22310 	      END IF;
22311 	   END IF;
22312    /* building l_pc_select to get values directly assigned to product category passed from the page */
22313    IF l_cat_id is not null  THEN
22314        	  l_pc_col:=' SELECT /*+ORDERED*/
22315 		   bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'DASS'||''''||')'||' name,
22316                    p.id  viewbyid,
22317 		   ''Y'' leaf_node_flag,
22318 		   null meaning ';
22319      l_pc_groupby := ' GROUP BY p.id ';
22320 
22321   l_pc_select :=
22322               ' UNION ALL ' ||
22323               l_pc_col||
22324               l_comm_cols||
22325 	      l_pc_from||
22326 	      l_pc_where ||'  AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE)'||
22327 	      l_pc_groupby ;
22328    END IF;
22329 ELSIF (l_view_by ='GEOGRAPHY+COUNTRY') THEN
22330    /** product category handling**/
22331    IF l_cat_id is null then
22332      l_where := l_where ||l_pc_where;
22333   ELSE
22334      l_from  := l_from ||l_pc_from;
22335      l_where := l_where||l_pc_where;
22336   END IF;
22337     l_col:=' SELECT
22338 		     decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME) name,
22339                      a.object_country viewbyid,
22340 		     null meaning ';
22341     l_groupby := ' GROUP BY  decode(d.TERRITORY_SHORT_NAME,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.TERRITORY_SHORT_NAME),a.object_country ';
22342     l_from:=' FROM fnd_territories_tl d '||l_from;
22343 	  IF l_campaign_id is null then
22344 	      IF l_admin_status ='Y' THEN
22345 	      l_from:=l_from||' ,bim_i_obj_mets_mv a ';
22346               l_where := l_where ||l_top_cond||
22347                          ' AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'')';
22348               ELSE
22349               l_from:=l_from||' ,bim_i_obj_mets_mv a ';
22350               l_where := l_where ||
22351 	                 ' AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'') ';
22352               END IF;
22353             ELSE
22354               l_from := l_from||' ,bim_i_obj_mets_mv a ' ; --, bim_i_source_codes b ';
22355               l_where  := l_where ||
22356                         '  AND  a.source_code_id = :l_campaign_id
22357                           AND  a.object_country =d.territory_code(+) AND d.language(+) = userenv(''LANG'') ';
22358 	  END IF;
22359 	  IF  l_country <>'N' THEN
22360 	      l_where  := l_where ||' AND  a.object_country = :l_country';
22361           ELSE
22362 	   l_where  := l_where ||' AND  a.object_country <> ''N''';
22363 	  END IF;
22364 ELSIF (l_view_by ='MEDIA+MEDIA') THEN
22365 /** product category handling**/
22366    IF l_cat_id is null then
22367      l_where := l_where ||l_pc_where;
22368   ELSE
22369      l_from  := l_from ||l_pc_from;
22370      l_where := l_where||l_pc_where;
22371   END IF;
22372     l_col:=' SELECT
22373 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
22374                      null viewbyid,
22375 		     null meaning ';
22376     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
22377     l_from:=' FROM bim_dimv_media d '||l_from;
22378 	  IF l_campaign_id is null then
22379 	      IF l_admin_status ='Y' THEN
22380 	      l_from:=l_from||' ,bim_mkt_chnl_mv a ';
22381               l_where := l_where ||
22382                          ' AND  d.id (+)= a.activity_id
22383 			   AND  a.object_country = :l_country';
22384               ELSE
22385               l_from:=l_from||' ,bim_obj_chnl_mv a ';
22386               l_where := l_where ||
22387 	                 ' AND  d.id (+)= a.activity_id
22388    		           AND  a.object_country = :l_country';
22389               END IF;
22390             ELSE
22391               l_from := l_from||' ,bim_obj_chnl_mv a ' ; --, bim_i_source_codes b ';
22392               l_where  := l_where ||
22393                         '  AND  a.source_code_id = :l_campaign_id
22394                            AND  d.id (+)= a.activity_id
22395 			   AND  a.object_country = :l_country';
22396 	  END IF;
22397 ELSIF (l_view_by ='GEOGRAPHY+AREA') THEN
22398 /** product category handling**/
22399    IF l_cat_id is null then
22400      l_where := l_where ||l_pc_where;
22404   END IF;
22401   ELSE
22402      l_from  := l_from ||l_pc_from;
22403      l_where := l_where||l_pc_where;
22405     l_col:=' SELECT
22406 		    decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) name,
22407                      null viewbyid,
22408 		     null meaning ';
22409     l_groupby := ' GROUP BY  decode(d.value,null,bim_pmv_dbi_utl_pkg.get_lookup_value('||''''||'UNA'||''''||')'||',d.value) ';
22410     l_from:=' FROM bis_areas_v d '||l_from;
22411 	  IF l_campaign_id is null then
22412 	      IF l_admin_status ='Y' THEN
22413 	      l_from:=l_from||' ,bim_mkt_regn_mv a ';
22414               l_where := l_where ||
22415                          ' AND  d.id (+)= a.object_region
22416 			   AND  a.object_country = :l_country';
22417               ELSE
22418               l_from:=l_from||' ,bim_obj_regn_mv a ';
22419               l_where := l_where ||
22420 	                 ' AND  d.id (+)= a.object_region
22421    		           AND  a.object_country = :l_country';
22422               END IF;
22423             ELSE
22424               l_from := l_from||' ,bim_obj_regn_mv a '; --, bim_i_source_codes b ';
22425               l_where  := l_where ||
22426                         '  AND  a.source_code_id = :l_campaign_id
22427                            AND  d.id (+)= a.object_region
22428 			   AND  a.object_country = :l_country';
22429 	  END IF;
22430 END IF;
22431 
22432 /* combine sql one to pick up current period values and  sql two to pick previous period values */
22433   l_select := l_col||
22434               l_comm_cols||
22435 	      l_from||
22436 	      l_where ||' AND cal.report_date in (&BIS_CURRENT_ASOF_DATE,&BIS_PREVIOUS_ASOF_DATE) '||
22437 	      l_groupby ||
22438 	      l_pc_select /* l_pc_select only applicable when product category is not all and view by is product category */
22439 	      ;
22440 
22441 /* prepare final sql */
22442 
22443  l_sqltext:= l_select_cal||
22444              l_select||
22445 	     l_select_filter;
22446   END IF;
22447    x_custom_sql := l_sqltext;
22448    l_custom_rec.attribute_name := ':l_record_type';
22449    l_custom_rec.attribute_value := l_record_type_id;
22450    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
22451    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
22452    x_custom_output.EXTEND;
22453    x_custom_output (1) := l_custom_rec;
22454    l_custom_rec.attribute_name := ':l_resource_id';
22455    l_custom_rec.attribute_value := get_resource_id;
22456    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
22457    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
22458    x_custom_output.EXTEND;
22459    x_custom_output (2) := l_custom_rec;
22460    l_custom_rec.attribute_name := ':l_admin_flag';
22461    l_custom_rec.attribute_value := get_admin_status;
22462    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
22463    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
22464    x_custom_output.EXTEND;
22465    x_custom_output (3) := l_custom_rec;
22466    l_custom_rec.attribute_name := ':l_country';
22467    l_custom_rec.attribute_value := l_country;
22468    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
22469    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
22470    x_custom_output.EXTEND;
22471    x_custom_output (4) := l_custom_rec;
22472    l_custom_rec.attribute_name := ':l_cat_id';
22473    l_custom_rec.attribute_value := l_cat_id;
22474    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
22475    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
22476    x_custom_output.EXTEND;
22477    x_custom_output (5) := l_custom_rec;
22478    l_custom_rec.attribute_name := ':l_campaign_id';
22479    l_custom_rec.attribute_value := l_campaign_id;
22480    l_custom_rec.attribute_type := bis_pmv_parameters_pub.bind_type;
22481    l_custom_rec.attribute_data_type := bis_pmv_parameters_pub.varchar2_bind;
22482    x_custom_output.EXTEND;
22483    x_custom_output (6) := l_custom_rec;
22484    write_debug ('GET_WON_OPTY_SQL', 'QUERY', '_', l_sqltext);
22485 EXCEPTION
22486    WHEN OTHERS
22487    THEN
22488       l_sql_errm := SQLERRM;
22489       write_debug ('GET_WON_OPTY_SQL', 'ERROR', l_sql_errm, l_sqltext);
22490 END get_won_opty_sql;
22491 
22492 
22493 
22494 PROCEDURE BIM_MKTG_LEAD_ACT_SQL (
22495    p_page_parameter_tbl   IN              bis_pmv_page_parameter_tbl,
22496    x_custom_sql           OUT NOCOPY      VARCHAR2,
22497    x_custom_output        OUT NOCOPY      bis_query_attributes_tbl
22498 )
22499 IS
22500    l_sqltext                      VARCHAR2 (20000);
22501    iflag                          NUMBER;
22502    l_period_type_hc               NUMBER;
22503    l_as_of_date                   DATE;
22504    l_period_type                  VARCHAR2 (2000);
22505    l_record_type_id               NUMBER;
22506    l_comp_type                    VARCHAR2 (2000);
22507    l_country                      VARCHAR2 (4000);
22508    l_view_by                      VARCHAR2 (4000);
22509    l_sql_errm                     VARCHAR2 (4000);
22510    l_previous_report_start_date   DATE;
22511    l_current_report_start_date    DATE;
22512    l_previous_as_of_date          DATE;
22513    l_period_type_id               NUMBER;
22514    l_user_id                      NUMBER;
22515    l_resource_id                  NUMBER;
22516    l_time_id_column               VARCHAR2 (1000);
22520    l_rsid                         NUMBER;
22517    l_admin_status                 VARCHAR2 (20);
22518    l_admin_flag                   VARCHAR2 (1);
22519    l_admin_count                  NUMBER;
22521    l_curr_aod_str                 VARCHAR2 (80);
22522    l_country_clause               VARCHAR2 (4000);
22523    l_access_clause                VARCHAR2 (4000);
22524    l_access_table                 VARCHAR2 (4000);
22525    l_cat_id                       VARCHAR2 (50)        := NULL;
22526    l_campaign_id                  VARCHAR2 (50)        := NULL;
22527    l_select                       VARCHAR2 (20000); -- to build  inner select to pick data from mviews
22528    l_pc_select                    VARCHAR2 (20000); -- to build  inner select to pick data directly assigned to the product category hirerachy
22529    l_select_cal                   VARCHAR2 (20000); -- to build  select calculation part
22530    l_select_filter                VARCHAR2 (20000); -- to build  select filter part
22531    l_from                         VARCHAR2 (20000);   -- assign common table in  clause
22532    l_where                        VARCHAR2 (20000);  -- static where clause
22533    l_groupby                      VARCHAR2 (2000);  -- to build  group by clause
22534    l_pc_from                      VARCHAR2 (20000);   -- from clause to handle product category
22535    l_pc_where                     VARCHAR2 (20000);   --  where clause to handle product category
22536    l_filtercol                    VARCHAR2 (2000);
22537    l_pc_col                       VARCHAR2(100);
22538    l_pc_groupby                   VARCHAR2(200);
22539    l_view                         VARCHAR2 (20);
22540    l_comm_cols                    VARCHAR2 (20000);
22541    l_comm_cols2                   VARCHAR2 (20000);
22542    l_view_disp                    VARCHAR2(100);
22543    l_url_str                      VARCHAR2(1000);
22544    l_url_str_csch                 varchar2(1000);
22545    l_url_str_type                 varchar2(1000);
22546    l_camp_sel_col                 varchar2(100);
22547    l_camp_groupby_col             varchar2(100);
22548    l_csch_chnl                    varchar2(100);
22549    l_top_cond                     VARCHAR2(100);
22550    l_meaning                      VARCHAR2 (20);
22551    /* variables to hold columns names in l_select clauses */
22552    l_col                          VARCHAR2(1000);
22553    /* cursor to get type of object passed from the page ******/
22554     cursor get_obj_type
22555     is
22556     select object_type
22557     from bim_i_source_codes
22558     where source_code_id=replace(l_campaign_id,'''');
22559     /*********************************************************/
22560    l_custom_rec                   bis_query_attributes;
22561    l_object_type                  varchar2(30);
22562    l_url_link                     varchar2(200);
22563    l_url_camp1                     varchar2(500);
22564    l_url_camp2                     varchar2(500);
22565    l_dass                          varchar2(100);  -- variable to store value for  directly assigned lookup value
22566    l_una                           varchar2(100);   -- variable to store value for  Unassigned lookup value
22567    l_curr VARCHAR2(50);
22568    l_curr_suffix VARCHAR2(50);
22569    l_col_id NUMBER;
22570    l_area VARCHAR2(300);
22571    l_report_name VARCHAR2(300);
22572    l_media VARCHAR2(300);
22573 
22574 BEGIN
22575 
22576 	NULL;
22577 
22578 EXCEPTION
22579    WHEN OTHERS
22580    THEN
22581       l_sql_errm := SQLERRM;
22582       write_debug ('GET_MKTG_NEW_LEADS_SQL', 'ERROR', l_sql_errm, l_sqltext);
22583 END BIM_MKTG_LEAD_ACT_SQL;
22584 END BIM_DBI_MKTG_MGMT_PVT;