DBA Data[Home] [Help]

PACKAGE BODY: APPS.BIL_BI_PIPELINE_F_PKG

Source


1 PACKAGE BODY BIL_BI_PIPELINE_F_PKG AS
2 /*$Header: bilbpipb.pls 120.32.12010000.3 2008/12/02 05:44:53 annsrini ship $*/
3 
4  G_global_start_date    DATE;
5  G_request_id         NUMBER;
6  G_appl_id        NUMBER;
7  G_program_id        NUMBER;
8  G_user_id        NUMBER;
9  G_login_id        NUMBER;
10  g_row_num        NUMBER;
11  g_prim_currency        VARCHAR2(10);
12  g_prim_rate_type       VARCHAR2(15);
13 
14  G_sec_Currency         VARCHAR2(10);
15  G_sec_Rate_Type        VARCHAR2(15);
16 
17 
18  g_credit_type_id       VARCHAR2(10);
19  g_pkg       VARCHAR2(100);
20  g_setup_error_flag    BOOLEAN;
21  g_program_start        DATE;
22 
23 
24  G_BIS_SETUP_ERROR  EXCEPTION;
25  G_SETUP_VAL_ERROR  EXCEPTION;
26  G_INVALID_DIM    EXCEPTION;
27 
28 
29 
30  PROCEDURE Summary_Err_Check(p_mode IN varchar2,
31  			     x_valid_curr OUT NOCOPY VARCHAR2,
32                                x_valid_date OUT NOCOPY VARCHAR2,
33                                x_valid_prod OUT NOCOPY VARCHAR2,
34                                x_return_warn OUT NOCOPY VARCHAR2
35                                );
36 
37  PROCEDURE Summary_Err_Check_SmallGap(x_valid_curr OUT NOCOPY VARCHAR2,
38                             x_valid_date OUT NOCOPY VARCHAR2,
39                             x_valid_prod OUT NOCOPY VARCHAR2,
40                             x_return_warn OUT NOCOPY VARCHAR2);
41 
42  PROCEDURE Build_BaseLine(p_start_date IN DATE);
43 
44  PROCEDURE Build_Denlog(p_start_date IN DATE, p_end_date IN DATE, p_coll_start IN DATE,p_mode IN varchar2);
45 
46  PROCEDURE Gap_Fill(p_start_date IN DATE, p_end_date IN DATE, p_curr_coll_start IN DATE
47  , p_mode IN VARCHAR2, p_small_gap IN BOOLEAN);
48 
49  PROCEDURE Populate_Currency_Rate(p_mode IN varchar2);
50 
51  PROCEDURE Populate_Curr_Rate_SmallGap;
52 
53  PROCEDURE Report_Missing_Rates;
54 
55  PROCEDURE Report_Profile_Error (p_profile_name IN varchar2,
56           p_value IN varchar2,
57           p_exp_value IN varchar2) ;
58 
59  PROCEDURE Report_Missing_Profile (p_profile_name IN varchar2) ;
60 
61  PROCEDURE Insert_Into_Summary(p_mode IN varchar2);
62 
63  FUNCTION get_first_success_period(p_object_name in varchar2) return varchar2 ;
64 
65  FUNCTION get_number_of_runs( p_object_name in varchar2) return number;
66 
67 FUNCTION get_start_curr_coll return date;
68 
69 FUNCTION is_date_end_of_prd(p_date IN DATE) return boolean;
70 
71 PROCEDURE populate_hist_dates(p_start_date IN DATE, p_end_date IN DATE);
72 
73 PROCEDURE delete_from_curr(p_curr_coll_start IN DATE);
74 
75 PROCEDURE Insert_Into_Curr_sumry(p_date IN DATE, p_week IN NUMBER, p_period IN NUMBER,
76 		  						 p_qtr IN NUMBER, p_year IN NUMBER, p_min_date_id IN NUMBER,
77 								 p_max_date_id IN NUMBER);
78 
79 PROCEDURE Ins_Into_CurrSum_SmGap(p_date IN DATE, p_week IN NUMBER, p_period IN NUMBER,
80 		  						 p_qtr IN NUMBER, p_year IN NUMBER, p_min_date_id IN NUMBER,
81 								 p_max_date_id IN NUMBER);
82 
83 PROCEDURE get_period_ids(p_date IN DATE,
84 		  				 x_day OUT NOCOPY NUMBER,
85 		  			     x_week OUT NOCOPY NUMBER,
86 						 x_period OUT NOCOPY NUMBER,
87 						 x_qtr OUT NOCOPY NUMBER,
88 						 x_year OUT NOCOPY NUMBER,
89 						 x_min_date_id OUT NOCOPY NUMBER,
90 						 x_max_date_id OUT NOCOPY NUMBER);
91 
92 
93 PROCEDURE Insert_Into_Stg_SmallGap(p_start_date IN DATE, p_end_date IN DATE, p_first_fact_run IN DATE);
94 
95    PROCEDURE Load
96      (
97        errbuf              IN OUT NOCOPY VARCHAR2,
98        retcode             IN OUT NOCOPY VARCHAR2,
99        p_start_date        IN   VARCHAR2,
100        p_truncate          IN   VARCHAR2
101      );
102 
103 PROCEDURE Populate_Curr_Rate_SmallGap IS
104   l_proc     VARCHAR2(100);
105  BEGIN
106  l_proc  := 'Populate_Currency_Rate_SmallGap';
107  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
108    bil_bi_util_collection_pkg.writeLog(
109              p_log_level => fnd_log.LEVEL_PROCEDURE,
110              p_module => g_pkg || l_proc || ' begin',
111              p_msg => 'Start of Procedure '|| l_proc);
112  END IF;
113 
114 
115      MERGE INTO BIL_BI_CURRENCY_RATE sumry
116      USING
117      (
118        SELECT
119          txn_currency,
120          effective_date,
121          DECODE(txn_currency,g_prim_currency,1,fii_currency.get_global_rate_primary(txn_currency,
122                                                             trunc(least(sysdate,effective_date)))) prate,
123          DECODE(g_sec_currency,NULL,NULL,decode(txn_currency,g_sec_currency,1,
124            fii_currency.get_global_rate_secondary(txn_currency,trunc(least(sysdate,effective_date))))) srate
125        FROM
126        (
127          SELECT
128            DISTINCT txn_currency,
129            effective_date
130          FROM
131            bil_bi_pipeline_stg stg
132        )
133      ) rates
134      ON
135      (
136        rates.txn_currency = sumry.currency_code
137        AND rates.effective_date = sumry.exchange_date
138      )
139      WHEN MATCHED THEN
140        UPDATE SET sumry.exchange_rate = rates.prate,sumry.exchange_rate_s = rates.srate
141      WHEN NOT MATCHED THEN
142        INSERT
143        (
144          sumry.currency_code,
145          sumry.exchange_date,
146          sumry.exchange_rate,
147          sumry.exchange_rate_s
148        )
149        VALUES
150        (
151          rates.txn_currency,
152          rates.effective_date,
153          rates.prate,
154          rates.srate
155        );
156   commit;
157 
158   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
159         bil_bi_util_collection_pkg.writeLog(
160                 p_log_level => fnd_log.LEVEL_EVENT,
161     p_module => g_pkg || l_proc ,
162     p_msg => 'Inserted  '||sql%rowcount||' into bil_bi_currency_rate table');
163   END IF;
164 
165   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
166     bil_bi_util_collection_pkg.writeLog(
167              p_log_level => fnd_log.LEVEL_PROCEDURE,
168              p_module => g_pkg || l_proc || ' end',
169              p_msg => 'End of Procedure '|| l_proc);
170  END IF;
171  EXCEPTION
172    WHEN OTHERS THEN
173       fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
174       fnd_message.set_token('ERRNO' ,SQLCODE);
175       fnd_message.set_token('REASON' ,SQLERRM);
176 fnd_message.set_token('ROUTINE' , l_proc);
177       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
178   p_module => g_pkg || l_proc || ' proc_error',
179   p_msg => fnd_message.get,
180   p_force_log => TRUE);
181 
182   RAISE;
183  END   Populate_Curr_Rate_SmallGap;
184 
185 
186 PROCEDURE Summary_Err_Check_SmallGap(x_valid_curr OUT NOCOPY VARCHAR2,
187                             x_valid_date OUT NOCOPY VARCHAR2,
188                             x_valid_prod OUT NOCOPY VARCHAR2,
189                             x_return_warn OUT NOCOPY VARCHAR2) IS
190 
191       l_time_cnt          NUMBER;
192       l_conv_rate_cnt     NUMBER;
193       l_stg_min           NUMBER;
194       l_stg_max           NUMBER;
195       l_stg_min_txn_dt         DATE;
196       l_stg_max_txn_dt         DATE;
197       l_stg_min_eff_dt         DATE;
198       l_stg_max_eff_dt         DATE;
199       l_stg_min_dt         DATE;
200       l_stg_max_dt         DATE;
201       l_day_min           NUMBER;
202       l_day_max           NUMBER;
203       l_has_missing_date   BOOLEAN;
204       l_count       NUMBER;
205       l_lead_num    NUMBER;
206       l_eff_date    DATE;
207       l_number_of_rows     NUMBER;
208       l_int_type       VARCHAR2(100);
209       l_prim_code      VARCHAR2(100);
210       l_sec_code       VARCHAR2(100);
211       l_warn       VARCHAR2(1);
212 
213 --      l_prim_miss BOOLEAN :=FALSE;
214 --      l_sec_miss  BOOLEAN :=FALSE;
215 
216      cursor c_item_prod is
217       SELECT lead_number  FROM bil_bi_pipeline_stg
218       WHERE  nvl(product_category_id,-1)=-1;
219 
220    l_proc VARCHAR2(100);
221 BEGIN
222 
223         l_time_cnt :=0;
224       l_conv_rate_cnt:=0;
225       l_has_missing_date := FALSE;
226       l_count :=0;
227       l_number_of_rows :=0;
228       l_proc := 'Summary_Err_Check_SmallGap';
229 
230   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
231    bil_bi_util_collection_pkg.writeLog(
232              p_log_level => fnd_log.LEVEL_PROCEDURE,
233              p_module => g_pkg || l_proc || ' begin',
234              p_msg => 'Start of Procedure '|| l_proc);
235   END IF;
236 
237 
238       UPDATE  bil_bi_pipeline_stg stg
239          SET  (stg.prim_conversion_rate,conversion_rate_s) =
240            (
241              select
242                exchange_rate,exchange_rate_s
243              from BIL_BI_CURRENCY_RATE
244              where
245                currency_code = stg.txn_currency and
246                exchange_date = stg.effective_date
247            );
248 -- DOUBT
249       /*
250        WHERE
251        (
252          (
253            (prim_conversion_rate IS NOT NULL) AND (prim_conversion_rate < 0)
254          )
255          OR  prim_conversion_rate IS NULL
256        );
257       */
258 
259       -- need this commit for the rollup not to roll back all the currencys, doesn't really matter anyway
260       commit;
261 
262       IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
263         bil_bi_util_collection_pkg.writeLog(
264                 p_log_level => fnd_log.LEVEL_EVENT,
265     p_module => g_pkg || l_proc ,
266     p_msg => 'Updated rates for '|| sql%rowcount || ' rows');
267   END IF;
268 
269       --IF (g_refresh_flag <> 'Y') THEN
270 
271       SELECT count(1)
272       INTO   l_conv_rate_cnt
273       FROM   BIL_BI_PIPELINE_STG
274       WHERE  (prim_conversion_rate < 0  OR  prim_conversion_rate IS NULL)
275              OR (g_sec_currency IS NOT NULL and (conversion_rate_s < 0  OR  conversion_rate_s IS NULL)) ;
276 
277      IF (l_conv_rate_cnt >0) THEN
278        IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
279          bil_bi_util_collection_pkg.writeLog
280          (
281            p_log_level =>fnd_log.LEVEL_ERROR,
282            p_module => g_pkg || l_proc ,
283            p_msg => 'Missing currency conversion rates found, program will exit with error status.'||
284            'Please fix the missing conversion rates'
285          );
286        END IF;
287        Report_Missing_Rates;
288 --       l_prim_miss := TRUE;
289       x_valid_curr := 'N';
290     ELSE
291       x_valid_curr := 'Y';
292      END IF;
293 
294 
295 /*also for effective date. need to call the time api*/
296 
297       SELECT   MIN(stg.SNAP_DATE), Max(stg.SNAP_DATE)
298              ,MIN(stg.Effective_DATE), Max(stg.Effective_DATE)
299       INTO   l_stg_min_txn_dt, l_stg_max_txn_dt, l_stg_min_eff_dt, l_stg_max_eff_dt
300       FROM   BIL_BI_PIPELINE_STG stg;
301 
302       IF (l_stg_min_txn_dt < l_stg_min_eff_dt) THEN
303          l_stg_min_dt := l_stg_min_txn_dt;
304       ELSE
305          l_stg_min_dt := l_stg_min_eff_dt;
306       END IF;
307 
308       IF (l_stg_max_txn_dt < l_stg_max_eff_dt) THEN
309          l_stg_max_dt := l_stg_max_eff_dt;
310       ELSE
311          l_stg_max_dt := l_stg_max_txn_dt;
312       END IF;
313 
314 
315       --write_log(p_msg => 'Date range:'||l_stg_min_dt||'  '||l_stg_max_dt, p_log => 'N');
316 IF l_stg_min_dt IS NOT NULL AND l_stg_max_dt IS NOT NULL THEN
317      FII_TIME_API.check_missing_date (l_stg_min_dt,l_stg_max_dt,l_has_missing_date);
318 END IF;
319      l_has_missing_date := FALSE;
320 
321 	  IF (l_has_missing_date) THEN
322         IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
323           bil_bi_util_collection_pkg.writeLog
324           (
325             p_log_level => fnd_log.LEVEL_EVENT,
326             p_module => g_pkg || l_proc ,
327             p_msg => 'Time Dimension is not fully populated. '||
328               ' Please populate Time dimension to cover the date range you are collecting');
329         END IF;
330          x_valid_date := 'N';
331       Else
332          x_valid_date := 'Y';
333       END IF;
334 
335 --   ELSE
336  --        x_valid_curr := 'Y';
337  --     x_valid_date := 'Y';
338  --  END IF;        --- incremental mode only
339 
340       --- The following check applies both initial and incremental mode
341 
342       -- check for oppty close date  => don't need in this case since it is already restricted.
343 
344        -- check for bad item/product
345   l_number_of_rows := 0;
346       OPEN c_item_prod;
347         LOOP
348            FETCH c_item_prod into
349           l_lead_num;
350            EXIT WHEN c_item_prod%NOTFOUND ;
351      l_number_of_rows :=l_number_of_rows + 1;
352 
353       IF(l_number_of_rows=1) THEN
354       -- print header
355 
356         IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
357           bil_bi_util_collection_pkg.writeLog
358           (
359             p_log_level => fnd_log.LEVEL_EVENT,
360             p_module => g_pkg || l_proc ,
361             p_msg => ' Some opportunties had null product category. They have not been collected.'
362           );
363         END IF;
364 
365      fnd_message.set_name('BIL','BIL_BI_ITEM_PROD_WARN_HDR');
366      bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_ERROR,
367                             p_module => g_pkg || l_proc || ' proc_error',
368                           p_msg => fnd_message.get,
369                           p_force_log => TRUE);
370       END IF;
371 
372       -- print detail
373 
374      fnd_message.set_name('BIL','BIL_BI_ITEM_PROD_ERR_DTL');
375      fnd_message.set_token('OPPNUM', l_lead_num);
376      bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_ERROR,
377                             p_module => g_pkg || l_proc || ' proc_error',
378                           p_msg => fnd_message.get,
379                           p_force_log => TRUE);
380 
381 
382         END LOOP;
383      CLOSE c_item_prod;
384 
385 
386   IF ( l_number_of_rows  > 0) THEN
387       x_valid_prod := 'N';
388       x_return_warn := 'Y';
389   ELSE
390       x_valid_prod := 'Y';
391       x_return_warn := 'N';
392   END IF;
393 
394 
395      IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
396     bil_bi_util_collection_pkg.writeLog(
397              p_log_level => fnd_log.LEVEL_PROCEDURE,
398              p_module => g_pkg || l_proc || ' end',
399              p_msg => 'End of Procedure '|| l_proc);
400      END IF;
401 
402 EXCEPTION
403       WHEN OTHERS THEN
404         fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
405       fnd_message.set_token('ERRNO' ,SQLCODE);
406       fnd_message.set_token('REASON' ,SQLERRM);
407   fnd_message.set_token('ROUTINE' , l_proc);
408       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
409   p_module => g_pkg || l_proc || ' proc_error',
410   p_msg => fnd_message.get,
411   p_force_log => TRUE);
412   Raise;
413 
414 END Summary_Err_Check_SmallGap;
415 
416 
417 
418 
419 PROCEDURE get_period_ids(p_date IN DATE,
420 		  				 x_day OUT NOCOPY NUMBER,
421 		  			     x_week OUT NOCOPY NUMBER,
422 						 x_period OUT NOCOPY NUMBER,
423 						 x_qtr OUT NOCOPY NUMBER,
424 						 x_year OUT NOCOPY NUMBER,
425 						 x_min_date_id OUT NOCOPY NUMBER,
426 						 x_max_date_id OUT NOCOPY NUMBER) IS
427 
428 l_proc VARCHAR2(100);
429 
430 BEGIN
431 
432 l_proc := 'get_period_ids';
433 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
434     bil_bi_util_collection_pkg.writeLog(
435              p_log_level => fnd_log.LEVEL_PROCEDURE,
436              p_module => g_pkg || l_proc || ' begin',
437              p_msg => 'Start of Procedure '|| l_proc);
438  END IF;
439 
440 				SELECT
441 				       time.report_date_julian, time.week_id, time.ent_period_id,
442 				       time.ent_qtr_id, time.ent_year_id,  to_number(to_char(LEAST(time.ent_year_start_date, time1.week_start_date), 'J')),
443 				       to_number(to_char(GREATEST(time.ent_year_end_date,time2.week_end_date), 'J'))
444 				 INTO
445 				       x_day, x_week, x_period, x_qtr, x_year, x_min_date_id, x_max_date_id
446 				 FROM
447 				       FII_TIME_DAY time,
448 				       FII_TIME_DAY time1,
449                                        FII_TIME_DAY time2
450 				 WHERE
451 				       time.report_date = TRUNC(p_date)
452 				       AND time1.report_date = time.ent_year_start_date
453                                        AND time2.report_date = time.ent_year_end_date ;
454 
455 
456  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
457      	bil_bi_util_collection_pkg.writeLog(
458              p_log_level => fnd_log.LEVEL_PROCEDURE,
459              p_module => g_pkg || l_proc || ' end',
460              p_msg => 'End of Procedure '|| l_proc);
461    	 END IF;
462 
463 
464 
465     Exception
466   WHEN OTHERS THEN
467    /*Generic Exception Handling block.*/
468          fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
469       fnd_message.set_token('ERRNO' ,SQLCODE);
470       fnd_message.set_token('REASON', SQLERRM);
471       fnd_message.set_token('ROUTINE', l_proc);
472       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
473   p_module => g_pkg || l_proc || ' proc_error',
474   p_msg => fnd_message.get,
475   p_force_log => TRUE);
476 
477   RAISE;
478 
479 
480 
481 END get_period_ids;
482 
483 
484 FUNCTION is_date_end_of_prd(p_date IN DATE) return boolean IS
485 
486 l_proc VARCHAR2(100);
487 l_week_end DATE;
488 l_period_end DATE;
489 l_quarter_end DATE;
490 l_year_end DATE;
491 l_is_eop BOOLEAN;
492 
493   begin
494          l_proc := 'is_date_end_of_prd';
495 
496     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
497     bil_bi_util_collection_pkg.writeLog(
498              p_log_level => fnd_log.LEVEL_PROCEDURE,
499              p_module => g_pkg || l_proc || ' begin',
500              p_msg => 'Start of Procedure '|| l_proc);
501     END IF;
502 
503 
504 	 SELECT week_end_date, ent_period_end_date, ent_qtr_end_date, ent_year_end_date
505 	 INTO  l_week_end, l_period_end , l_quarter_end, l_year_end
506 	 from fii_time_day
507 	 where report_date = p_date;
508 
509 	 IF p_date = l_week_end OR p_date = l_period_end OR p_date = l_quarter_end
510 	 	OR p_date = l_year_end THEN
511 		   l_is_eop := TRUE;
512      ELSE
513 	 		l_is_eop := FALSE;
514 	 END IF;
515 
516 
517 	 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
518      	bil_bi_util_collection_pkg.writeLog(
519              p_log_level => fnd_log.LEVEL_PROCEDURE,
520              p_module => g_pkg || l_proc || ' end',
521              p_msg => 'End of Procedure '|| l_proc);
522    	 END IF;
523 
524 	 return l_is_eop;
525 
526     Exception
527   WHEN OTHERS THEN
528    /*Generic Exception Handling block.*/
529          fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
530       fnd_message.set_token('ERRNO' ,SQLCODE);
531       fnd_message.set_token('REASON', SQLERRM);
532       fnd_message.set_token('ROUTINE', l_proc);
533       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
534   p_module => g_pkg || l_proc || ' proc_error',
535   p_msg => fnd_message.get,
536   p_force_log => TRUE);
537 
538   RAISE;
539 
540 
541 
542 END is_date_end_of_prd;
543 
544 
545 PROCEDURE delete_from_curr(p_curr_coll_start IN DATE) IS
546   l_proc VARCHAR2(100);
547 
548 
549   begin
550          l_proc := 'delete_from_curr';
551 
552     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
553     bil_bi_util_collection_pkg.writeLog(
554              p_log_level => fnd_log.LEVEL_PROCEDURE,
555              p_module => g_pkg || l_proc || ' begin',
556              p_msg => 'Start of Procedure '|| l_proc);
557     END IF;
558 
559 
560    -- delete the oldest week of data from current pipe fact, but first:
561    -- insert into historical pipe fact those rows that
562    --we are deleting from current and that are end of period (week, month, etc) rows
563 
564 
565 
566 
567 	-- by joining to fii_time_day, we ensure that only end of period records
568 	-- are inserted into hist pipe fact
569 
570 	INSERT  into bil_bi_pipeline_f f(
571    SALES_GROUP_ID,
572    SALESREP_ID,
573    CREATED_BY ,
574    CREATION_DATE,
575    LAST_UPDATED_BY,
576    LAST_UPDATE_DATE,
577    LAST_UPDATE_LOGIN,
578    REQUEST_ID,
579    PROGRAM_APPLICATION_ID,
580    PROGRAM_ID,
581    PROGRAM_UPDATE_DATE,
582    SNAP_DATE,
583    ITEM_ID,
584    ITEM_ORGANIZATION_ID,
585    WIN_PROBABILITY,
586    PRODUCT_CATEGORY_ID,
587    PIPELINE_AMT_DAY,
588    PIPELINE_AMT_WEEK,
589    PIPELINE_AMT_PERIOD,
590    PIPELINE_AMT_Quarter,
591    PIPELINE_AMT_YEAR,
592    OPEN_AMT_DAY,
593    OPEN_AMT_WEEK,
594    OPEN_AMT_PERIOD,
595    OPEN_AMT_Quarter,
596    OPEN_AMT_YEAR,
597    PIPELINE_AMT_DAY_S,
598    PIPELINE_AMT_WEEK_S,
599    PIPELINE_AMT_PERIOD_S,
600    PIPELINE_AMT_Quarter_S,
601    PIPELINE_AMT_YEAR_S,
602    OPEN_AMT_DAY_S,
603    OPEN_AMT_WEEK_S,
604    OPEN_AMT_PERIOD_S,
605    OPEN_AMT_Quarter_S,
606    OPEN_AMT_YEAR_S
607  )
608  SELECT
609    f.SALES_GROUP_ID,
610    f.SALESREP_ID,
611    g_user_id,
612    sysdate,
613    g_user_id,
614    sysdate,
615    G_Login_Id,
616    G_request_id,
617    G_appl_id,
618    G_program_id,
619    sysdate,
620    f.snap_date,
621    f.ITEM_ID,
622    f.ITEM_ORGANIZATION_ID,
623    f.WIN_PROBABILITY,
624    f.PRODUCT_CATEGORY_ID,
625    f.PIPELINE_AMT_DAY,
626    f.PIPELINE_AMT_WEEK,
627    f.PIPELINE_AMT_PERIOD,
628    f.PIPELINE_AMT_Quarter,
629    f.PIPELINE_AMT_YEAR,
630    f.OPEN_AMT_DAY,
631    f.OPEN_AMT_WEEK,
632    f.OPEN_AMT_PERIOD,
633    f.OPEN_AMT_Quarter,
634    f.OPEN_AMT_YEAR,
635    f.PIPELINE_AMT_DAY_S,
636    f.PIPELINE_AMT_WEEK_S,
637    f.PIPELINE_AMT_PERIOD_S,
638    f.PIPELINE_AMT_Quarter_S,
639    f.PIPELINE_AMT_YEAR_S,
640    f.OPEN_AMT_DAY_S,
641    f.OPEN_AMT_WEEK_S,
642    f.OPEN_AMT_PERIOD_S,
643    f.OPEN_AMT_Quarter_S,
644    f.OPEN_AMT_YEAR_S
645    FROM BIL_BI_PIPEC_F f
646    WHERE f.snap_date < p_curr_coll_start
647    and f.snap_date IN (
648    SELECT report_date from fii_time_day
649    	   			   	   where report_date=week_end_date
650 					   UNION
651 					   SELECT report_date from fii_time_day
652    	   			   	   where report_date = ent_period_end_date
653 					   UNION
654 					   SELECT report_date from fii_time_day
655    	   			   	   where report_date = ent_qtr_end_date
656 					   UNION
657 					   SELECT report_date from fii_time_day
658    	   			   	   where report_date =ent_year_end_date
659    );
660 
661    commit;
662 
663     DELETE FROM BIL_BI_PIPEC_F
664     where snap_date < p_curr_coll_start;
665     commit;
666 
667   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
668     bil_bi_util_collection_pkg.writeLog(
669              p_log_level => fnd_log.LEVEL_PROCEDURE,
670              p_module => g_pkg || l_proc || ' end',
671              p_msg => 'End of Procedure '|| l_proc);
672    END IF;
673 
674 
675     Exception
676   WHEN OTHERS THEN
677    /*Generic Exception Handling block.*/
678          fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
679       fnd_message.set_token('ERRNO' ,SQLCODE);
680       fnd_message.set_token('REASON', SQLERRM);
681       fnd_message.set_token('ROUTINE', l_proc);
682       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
683   p_module => g_pkg || l_proc || ' proc_error',
684   p_msg => fnd_message.get,
685   p_force_log => TRUE);
686 
687   RAISE;
688 
689 
690 END delete_from_curr;
691 
692 
693 
694 PROCEDURE populate_hist_dates(p_start_date DATE, p_end_date DATE) IS
695   l_proc VARCHAR2(100);
696 
697 
698   begin
699          l_proc := 'populate_hist_dates';
700 
701     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
702     bil_bi_util_collection_pkg.writeLog(
703              p_log_level => fnd_log.LEVEL_PROCEDURE,
704              p_module => g_pkg || l_proc || ' begin',
705              p_msg => 'Start of Procedure '|| l_proc);
706     END IF;
707 
708  INSERT INTO BIL_BI_TIME
709 (select
710 CASE WHEN week_start_date < ent_period_start_date then ent_period_start_date
711 WHEN week_start_date < ent_qtr_start_date then ent_qtr_start_date
712 WHEN week_start_date < ent_year_start_date then ent_year_start_date
713 ELSE week_start_date
714 END start_date
715 ,CASE WHEN week_end_date > ent_period_end_date then ent_period_end_date
716 WHEN week_end_date > ent_qtr_end_date then ent_qtr_end_date
717 WHEN week_end_date > ent_year_end_date then ent_year_end_date
718 ELSE week_end_date	END  end_date
719 from fii_time_day time1
720 where (time1.report_date between p_start_date and p_end_date
721 and time1.report_date=time1.week_start_date)
722 OR
723 (time1.report_date between p_start_date and p_end_date
724 and time1.report_date = time1.week_end_date
725 and (time1.week_start_date < time1.ent_period_start_date OR
726 time1.week_start_date < time1.ent_qtr_start_date OR
727 time1.week_start_date < time1.ent_period_start_date))
728 );
729 commit;
730 
731        IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
732     bil_bi_util_collection_pkg.writeLog(
733              p_log_level => fnd_log.LEVEL_PROCEDURE,
734              p_module => g_pkg || l_proc || ' end',
735              p_msg => 'End of Procedure '|| l_proc);
736    END IF;
737 
738 
739     Exception
740   WHEN OTHERS THEN
741    /*Generic Exception Handling block.*/
742          fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
743       fnd_message.set_token('ERRNO' ,SQLCODE);
744       fnd_message.set_token('REASON', SQLERRM);
745       fnd_message.set_token('ROUTINE', l_proc);
746       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
747   p_module => g_pkg || l_proc || ' proc_error',
748   p_msg => fnd_message.get,
749   p_force_log => TRUE);
750 
751   RAISE;
752 
753 
754 END populate_hist_dates;
755 
756 
757 FUNCTION get_start_curr_coll RETURN DATE IS
758 
759   l_curr_collect_start DATE;
760   l_proc VARCHAR2(100);
761   l_last_fact_run DATE;
762 
763   begin
764          l_proc := 'get_start_curr_coll';
765 
766     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
767     bil_bi_util_collection_pkg.writeLog(
768              p_log_level => fnd_log.LEVEL_PROCEDURE,
769              p_module => g_pkg || l_proc || ' begin',
770              p_msg => 'Start of Procedure '|| l_proc);
771     END IF;
772 
773     l_last_fact_run :=
774       trunc(fnd_date.displaydt_to_date(BIS_COLLECTION_UTILITIES.get_last_refresh_period('BIL_BI_OPDTL_F')));
775 
776 
777    	SELECT end_date+1
778     INTO l_curr_collect_start
779 	FROM fii_time_week
780     WHERE l_last_fact_run-21 BETWEEN start_date AND end_date;
781 
782        IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
783     bil_bi_util_collection_pkg.writeLog(
784              p_log_level => fnd_log.LEVEL_PROCEDURE,
785              p_module => g_pkg || l_proc || ' end',
786              p_msg => 'End of Procedure '|| l_proc);
787    END IF;
788 
789     return l_curr_collect_start;
790 
791     Exception
792   WHEN OTHERS THEN
793    /*Generic Exception Handling block.*/
794          fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
795       fnd_message.set_token('ERRNO' ,SQLCODE);
796       fnd_message.set_token('REASON', SQLERRM);
797       fnd_message.set_token('ROUTINE', l_proc);
798       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
799   p_module => g_pkg || l_proc || ' proc_error',
800   p_msg => fnd_message.get,
801   p_force_log => TRUE);
802 
803   RAISE;
804 
805 
806 END get_start_curr_coll;
807 
808 
809 
810 PROCEDURE get_last_failure_periods(p_object_name in varchar2,
811 p_period_from OUT NOCOPY DATE,
812 p_period_to OUT NOCOPY DATE ) is
813 l_date   date;
814 l_date_disp varchar2(100);
815 l_proc VARCHAR2(100);
816 l_status VARCHAR2(50);
817 
818  cursor last_refresh_date_cursor(p_obj_name varchar2, l_status varchar2) is
819     select period_from, period_to
820   from bis_refresh_log
821   where object_name = p_obj_name and status=l_status
822   and last_update_date =( select max(last_update_date)
823        from bis_refresh_log
824           where object_name= p_obj_name and  status=l_status ) ;
825 
826 
827 begin
828          l_proc := 'get_last_failure_period';
829 		 l_status := 'FAILURE';
830  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
831     bil_bi_util_collection_pkg.writeLog(
832              p_log_level => fnd_log.LEVEL_PROCEDURE,
833              p_module => g_pkg || l_proc || ' begin',
834              p_msg => 'Start of Procedure '|| l_proc);
835    END IF;
836 
837     open last_refresh_date_cursor(p_object_name, l_status);
838     fetch last_refresh_date_cursor into p_period_from, p_period_to;
839     if(last_refresh_date_cursor%ROWCOUNT < 1) then
840         p_period_from:=null;
841         p_period_to:=null;
842     end if;
843     close last_refresh_date_cursor;
844 
845 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
846     bil_bi_util_collection_pkg.writeLog(
847              p_log_level => fnd_log.LEVEL_PROCEDURE,
848              p_module => g_pkg || l_proc || ' end',
849              p_msg => 'End of Procedure '|| l_proc);
850    END IF;
851 
852     Exception
853          WHEN NO_DATA_FOUND THEN
854 
855             p_period_from:=null;
856             p_period_to:=null;
857 
858   WHEN OTHERS THEN
859    /*Generic Exception Handling block.*/
860          fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
861       fnd_message.set_token('ERRNO' ,SQLCODE);
862       fnd_message.set_token('REASON', SQLERRM);
863       fnd_message.set_token('ROUTINE', l_proc);
864       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
865   p_module => g_pkg || l_proc || ' proc_error',
866   p_msg => fnd_message.get,
867   p_force_log => TRUE);
868 
869   RAISE;
870 end get_last_failure_periods;
871 
872 
873 function get_first_success_period(p_object_name in varchar2) return varchar2 is
874 l_date   date;
875 l_date_disp varchar2(100);
876 l_proc VARCHAR2(100);
877 l_status VARCHAR2(50);
878 
879 begin
880    l_proc  := 'get_first_success_period';
881    l_status := 'SUCCESS';
882 
883    IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
884     bil_bi_util_collection_pkg.writeLog(
885              p_log_level => fnd_log.LEVEL_PROCEDURE,
886              p_module => g_pkg || l_proc || ' begin',
887              p_msg => 'Start of Procedure '|| l_proc);
888    END IF;
889 
890 
891     SELECT MIN(period_from) INTO l_date
892     FROM bis_refresh_log
893     WHERE   object_name = p_object_name AND
894       status=l_status AND
895       last_update_date =
896     (SELECT MIN(last_update_date)
897      FROM bis_refresh_log
898      WHERE object_name= p_object_name AND
899            status=l_status ) ;
900 
901     IF (l_date IS NULL) THEN
902   l_date:= to_date(fnd_profile.value('BIS_GLOBAL_START_DATE'), 'mm/dd/yyyy');
903     END IF;
904 
905     l_date_disp := fnd_date.date_to_displaydt (l_date);
906 
907    IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
908     bil_bi_util_collection_pkg.writeLog(
909              p_log_level => fnd_log.LEVEL_PROCEDURE,
910              p_module => g_pkg || l_proc || ' end',
911              p_msg => 'End of Procedure '|| l_proc);
912    END IF;
913 
914     return l_date_disp;
915 
916     Exception
917   WHEN OTHERS THEN
918    /*Generic Exception Handling block.*/
919          fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
920       fnd_message.set_token('ERRNO' ,SQLCODE);
921       fnd_message.set_token('REASON', SQLERRM);
922       fnd_message.set_token('ROUTINE', l_proc);
923       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
924   p_module => g_pkg || l_proc || ' proc_error',
925   p_msg => fnd_message.get,
926   p_force_log => TRUE);
927 
928   RAISE;
929 end get_first_success_period;
930 
931 
932 FUNCTION get_number_of_runs( p_object_name in varchar2) return number is
933 	l_num NUMBER;
934 	l_proc VARCHAR2(100);
935 	l_status VARCHAR2(100);
936 
937 begin
938    l_proc  := 'get_number_of_runs';
939    l_status := 'SUCCESS';
940 
941    IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
942     bil_bi_util_collection_pkg.writeLog(
943              p_log_level => fnd_log.LEVEL_PROCEDURE,
944              p_module => g_pkg || l_proc || ' begin',
945              p_msg => 'Start of Procedure '|| l_proc);
946    END IF;
947 
948 
949     SELECT count(*) INTO l_num
950     FROM bis_refresh_log
951     WHERE   object_name = p_object_name
952     and status=l_status ;
953 
954 
955    IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
956     bil_bi_util_collection_pkg.writeLog(
957              p_log_level => fnd_log.LEVEL_PROCEDURE,
958              p_module => g_pkg || l_proc || ' end',
959              p_msg => 'End of Procedure '|| l_proc);
960    END IF;
961 
962     return l_num;
963 
964     Exception
965   WHEN OTHERS THEN
966    /*Generic Exception Handling block.*/
967          fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
968       fnd_message.set_token('ERRNO' ,SQLCODE);
969       fnd_message.set_token('REASON', SQLERRM);
970       fnd_message.set_token('ROUTINE', l_proc);
971       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
972   p_module => g_pkg || l_proc || ' proc_error',
973   p_msg => fnd_message.get,
974   p_force_log => TRUE);
975 
976   RAISE;
977 END get_number_of_runs;
978 
979    PROCEDURE incr_load
980      (
981        errbuf              IN OUT NOCOPY VARCHAR2,
982        retcode             IN OUT NOCOPY VARCHAR2
983      ) IS
984     BEGIN
985       load(errbuf, retcode, TO_CHAR(SYSDATE,'YYYY/MM/DD HH24:MI:SS'),'N');
986     END incr_load;
987 
988    PROCEDURE init_load (errbuf         IN OUT NOCOPY VARCHAR2,
989                    retcode             IN OUT NOCOPY VARCHAR2,
990 		   p_start_date        IN   VARCHAR2 ,
991                    p_truncate          IN   VARCHAR2)
992     IS
993     BEGIN
994       load(errbuf,retcode,p_start_date,p_truncate);
995     END init_load;
996 
997 
998 
999 
1000  /*PROCEDURE Load
1001       (
1002 	   errbuf              IN OUT NOCOPY VARCHAR2,
1003 	   retcode             IN OUT NOCOPY VARCHAR2,
1004 	   p_truncate          IN      VARCHAR2
1005 						         ) IS
1006 BEGIN
1007        load(errbuf, retcode, '2004/12/23 0:0:0', p_truncate);
1008 END LOAD;
1009 */
1010  PROCEDURE Load
1011      (
1012        errbuf              IN OUT NOCOPY VARCHAR2,
1013        retcode             IN OUT NOCOPY VARCHAR2,
1014        p_start_date        IN   VARCHAR2,
1015        p_truncate          IN   VARCHAR2
1016      ) IS
1017    l_proc                          VARCHAR2(100);
1018    l_day                           NUMBER;
1019    l_week                          NUMBER;
1020    l_period                        NUMBER;
1021    l_qtr                           NUMBER;
1022    l_year                          NUMBER;
1023    l_date                          DATE;
1024    l_last_run                      DATE;
1025    l_last_fact_run                 DATE;
1026    l_first_fact_run                DATE;
1027    l_min_date_id                   NUMBER;
1028    l_max_date_id                   NUMBER;
1029    l_valid_setup                   BOOLEAN ;
1030    l_int_date_format               VARCHAR2(21);
1031    l_int_date_format1              VARCHAR2(21);
1032    l_count                         NUMBER;
1033    l_resume_flag                   VARCHAR2(1);
1034    l_failure_from                  DATE;
1035    l_failure_to                    DATE;
1036    l_number_of_runs		   NUMBER;
1037    l_list                          DBMS_SQL.VARCHAR2_TABLE;
1038    l_val                           DBMS_SQL.VARCHAR2_TABLE;
1039    l_stg_count                     NUMBER;
1040    l_opty_cnt                      NUMBER;
1041    l_valid_curr                    VARCHAR2(1);
1042    l_valid_date                    VARCHAR2(1);
1043    l_valid_prod                    VARCHAR2(1);
1044    l_return_warn                   VARCHAR2(1);
1045    l_return_warn_resume            VARCHAR2(1);
1046    l_asn_implemented              VARCHAR2(1);
1047 
1048    l_mode 			   VARCHAR2(30);
1049    l_start_date                    DATE;
1050    l_date_format      		   VARCHAR2(21);
1051    l_sysdate		           DATE;
1052 
1053        l_sd_lyr                         DATE; --same date last year
1054     l_sd_lper                        DATE; --same date last period
1055     l_sd_lqtr                        DATE; -- same date last quarter
1056     l_sd_lwk                         DATE; --same date last week
1057 
1058     l_sd_lyr_end                         DATE; --same date last year (end of the year/qtr/period/week)
1059     l_sd_lper_end                        DATE; --same date last period (end of the year/qtr/period/week)
1060     l_sd_lqtr_end                       DATE; -- same date last quarter (end of the year/qtr/period/week)
1061     l_sd_lwk_end                         DATE; --same date last week (end of the year/qtr/period/week)
1062 
1063     l_dynamic_sql VARCHAR2(2000);
1064     l_curr_coll_start DATE;
1065     l_min_curr_date DATE;
1066 
1067     l_period_from                VARCHAR2(240); -- By TR to store this info in pipec_f
1068     l_period_to                  VARCHAR2(240); -- By TR to store this info in pipec_f
1069 
1070     l_min_lead_date DATE;
1071 	l_min_line_date DATE;
1072 	l_min_credit_date DATE;
1073 	l_min_log_date DATE;
1074 	l_small_gap BOOLEAN;
1075 
1076    BEGIN
1077 
1078  G_request_id  := 0;
1079  G_appl_id     := 0;
1080  G_program_id  := 0;
1081  G_user_id     := 0;
1082  G_login_id    := 0;
1083  g_row_num     := 0;
1084  g_pkg         := 'bil.patch.115.sql.bil_bi_pipeline_f_pkg.';
1085 
1086      G_request_id  := 0;
1087  G_appl_id     := 0;
1088  G_program_id  := 0;
1089  G_user_id     := 0;
1090  G_login_id    := 0;
1091  g_row_num     := 0;
1092  g_pkg         := 'bil.patch.115.sql.bil_bi_pipeline_f_pkg.';
1093 
1094       G_request_id := FND_GLOBAL.CONC_REQUEST_ID();
1095      G_appl_id    := FND_GLOBAL.PROG_APPL_ID();
1096      G_program_id := FND_GLOBAL.CONC_PROGRAM_ID();
1097      G_user_id    := FND_GLOBAL.USER_ID();
1098      G_login_id   := FND_GLOBAL.CONC_LOGIN_ID();
1099 
1100      g_setup_error_flag:= FALSE;
1101      g_row_num := 0;
1102      g_program_start:= sysdate;
1103 
1104    l_proc:= 'Load';
1105    l_int_date_format:='DD/MM/YYYY HH24:MI:SS';
1106    l_int_date_format1:='MM/DD/YYYY HH24:MI:SS';
1107    l_resume_flag:= 'N';
1108    l_sysdate := sysdate;
1109 
1110    l_valid_setup := BIS_COLLECTION_UTILITIES.SETUP('BIL_BI_PIPELINE_F');
1111 
1112 
1113 
1114 
1115 
1116    IF (not l_valid_setup) THEN
1117      IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1118         bil_bi_util_collection_pkg.writeLog
1119         (
1120           p_log_level => fnd_log.LEVEL_EVENT,
1121           p_module => g_pkg || l_proc ,
1122           p_msg => 'BIS_COLLECTION_UTILITIES.SETUP failed'
1123         );
1124      END IF;
1125     RAISE G_BIS_SETUP_ERROR;
1126   END IF;
1127 
1128 
1129    IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
1130     bil_bi_util_collection_pkg.writeLog(
1131              p_log_level => fnd_log.LEVEL_PROCEDURE,
1132              p_module => g_pkg || l_proc || ' begin',
1133              p_msg => 'Start of Procedure '|| l_proc);
1134    END IF;
1135 
1136 
1137 
1138         -- get profiles
1139 
1140        l_list(1) := 'BIS_GLOBAL_START_DATE';
1141        l_list(2) := 'AS_OPP_ENABLE_LOG';
1142        l_list(3) := 'AS_OPP_LINE_ENABLE_LOG';
1143        l_list(4) := 'AS_OPP_SC_ENABLE_LOG';
1144        l_list(5) := 'AS_OPP_LOG_TIMEFRAME';
1145        --l_list(6) := 'BIL_BI_ASN_IMPLEMENTED';
1146 
1147        -- 'ASN_FRCST_CREDIT_TYPE_ID'
1148        -- 'AS_FORECAST_CREDIT_TYPE_ID';
1149 
1150        l_list(6) := 'BIS_PRIMARY_CURRENCY_CODE';
1151        l_list(7) := 'BIS_PRIMARY_RATE_TYPE';
1152 
1153 
1154 
1155 
1156        IF (bis_common_parameters.check_global_parameters(l_list)) THEN
1157          bis_common_parameters.get_global_parameters(l_list, l_val);
1158          g_global_start_date := TO_DATE(l_val(1), l_int_date_format1);
1159          IF (l_val(2) <> 'Y' or l_val(3) <> 'Y' or l_val(4) <> 'Y') THEN
1160            FOR i IN 2..4  LOOP
1161              Report_Profile_Error(l_list(i), l_val(i), 'Y');
1162            END LOOP;
1163          END IF;
1164            IF (l_val(5) <> 'DAY' AND l_val(5) <> 'HOUR' AND l_val(5) <> 'MIN' AND l_val(5) <> 'NONE') THEN
1165 		 Report_Profile_Error(l_list(5), l_val(5), 'DAY or HOUR or MINUTE or NONE');
1166          END IF;
1167 
1168            g_credit_type_id:=fnd_profile.value('ASN_FRCST_CREDIT_TYPE_ID');
1169            IF(g_credit_type_id is null) THEN
1170              Report_Missing_Profile('ASN_FRCST_CREDIT_TYPE_ID');
1171            END IF;
1172 
1173        ELSE
1174         IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1175           bil_bi_util_collection_pkg.writeLog
1176             (
1177               p_log_level => fnd_log.LEVEL_EVENT,
1178               p_module => g_pkg || l_proc ,
1179               p_msg => 'Missing profiles' );
1180         END IF;
1181         bis_common_parameters.get_global_parameters(l_list, l_val);
1182         FOR v_counter IN 1..7 LOOP
1183           IF (l_val(v_counter) IS  NULL) THEN
1184             Report_Missing_Profile(l_list(v_counter));
1185           END IF;
1186         END LOOP;
1187       END IF;
1188 
1189      --g_prim_currency := bis_common_parameters.get_currency_code;
1190      --g_prim_rate_type := bis_common_parameters.get_rate_type;
1191      g_prim_currency := l_val(6);
1192      g_prim_rate_type := l_val(7);
1193 
1194     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_STATEMENT) THEN
1195       bil_bi_util_collection_pkg.writeLog
1196       (
1197         p_log_level => fnd_log.LEVEL_STATEMENT,
1198         p_module => g_pkg || l_proc ,
1199         p_msg => 'prim curr : rate type = '||g_prim_currency||' : '||g_prim_rate_type
1200       );
1201     END IF;
1202 
1203 
1204      l_list(8) := 'BIS_SECONDARY_CURRENCY_CODE';
1205      l_list(9) := 'BIS_SECONDARY_RATE_TYPE';
1206      l_val(8) := bis_common_parameters.get_secondary_currency_code;
1207      l_val(9) := bis_common_parameters.get_secondary_rate_type;
1208 
1209     -- don't reget all values with the 2 new profiles!!
1210     --bis_common_parameters.get_global_parameters(l_list, l_val);
1211 
1212 
1213 -- sec curr not set up at all
1214      IF (l_val(8) IS NULL) THEN
1215        IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_STATEMENT) THEN
1216           bil_bi_util_collection_pkg.writeLog
1217           (p_log_level => fnd_log.LEVEL_STATEMENT,p_module => g_pkg || l_proc ,
1218            p_msg => ' Secondary curency not set up '
1219          );
1220        END IF;
1221      END IF;
1222 
1223 -- sec curr set up but rate type not set up : ERROR
1224      IF (l_val(8) IS NOT NULL AND l_val(9) IS NULL ) THEN
1225       Report_Missing_Profile(l_list(9));
1226        IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_STATEMENT) THEN
1227           bil_bi_util_collection_pkg.writeLog
1228           (p_log_level => fnd_log.LEVEL_STATEMENT,p_module => g_pkg || l_proc ,
1229            p_msg => ' Secondary curency set up but rate type not set up: ERROR '
1230          );
1231        END IF;
1232      END IF;
1233 
1234 -- sec curr and rate type properly set up
1235      IF (l_val(8) IS NOT NULL AND l_val(9) IS NOT NULL ) THEN
1236 
1237        g_sec_currency := bis_common_parameters.get_secondary_currency_code;
1238        g_sec_rate_type := bis_common_parameters.get_secondary_rate_type;
1239 
1240        IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_STATEMENT) THEN
1241           bil_bi_util_collection_pkg.writeLog
1242           (
1243            p_log_level => fnd_log.LEVEL_STATEMENT,
1244            p_module => g_pkg || l_proc ,
1245            p_msg => 'sec curr : rate type = '||g_sec_currency||' : '||g_sec_rate_type
1246          );
1247        END IF;
1248      END IF;
1249 
1250 
1251 
1252 
1253 
1254     IF p_start_date IS NULL THEN
1255        l_Start_Date :=    G_global_start_date;
1256     ELSIF (TO_DATE(p_start_date, 'YYYY/MM/DD HH24:MI:SS') > l_sysdate) THEN
1257       fnd_message.set_name('BIL','BIL_BI_DATE_PARAM_RESET');
1258       fnd_message.set_token('RESET_DATE',
1259                   TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS'));
1260       bil_bi_util_collection_pkg.writeLog
1261         (
1262           p_log_level => fnd_log.LEVEL_ERROR,
1263           p_module => g_pkg || l_proc ,
1264           p_msg => fnd_message.get,
1265           p_force_log => TRUE
1266         );
1267         l_start_date := TRUNC(l_sysdate);
1268         l_return_warn := 'Y';
1269 
1270     ELSE
1271         l_Start_Date := TO_DATE(p_start_date, 'YYYY/MM/DD HH24:MI:SS');
1272     END IF;
1273 
1274 
1275 
1276 
1277  /*  IF (TO_DATE(p_start_date, 'YYYY/MM/DD HH24:MI:SS') < l_min_lead_date) THEN
1278 	    l_start_date := TRUNC(l_min_lead_date);
1279    ELSE l_start_date := TO_DATE(p_start_date, 'YYYY/MM/DD HH24:MI:SS');
1280    END IF;
1281 */
1282   -- l_date_format:= 'YYYY/MM/DD HH24:MI:SS';
1283   --l_Start_date := TO_DATE(p_start_date, l_date_format);
1284 
1285 -------------------------------------------------------------
1286         -- CHECK FOR OPTY LOGS.
1287 --------------------------------------------------------------
1288 
1289       SELECT SUM(cnt)  into l_opty_cnt FROM
1290       (
1291         SELECT /*+ index_ffs(as_leads_log) parallel_index(as_leads_log) */ count(*) cnt
1292         FROM as_leads_log
1293         UNION ALL
1294         SELECT -cnt FROM
1295           (SELECT /*+ index_ffs(as_leads_all) parallel_index(as_leads_all) */ count(*) cnt FROM as_leads_all)
1296       );
1297 
1298   IF (l_opty_cnt < 0 ) THEN
1299     -- log table has not been populated.
1300     IF (g_setup_error_flag = FALSE) THEN
1301       g_setup_error_flag := TRUE;
1302       fnd_message.set_name('BIL','BIL_BI_SETUP_INCOMPLETE');
1303       bil_bi_util_collection_pkg.writeLog
1304       (
1305         p_log_level => fnd_log.LEVEL_ERROR,
1306         p_module => g_pkg || l_proc || ' proc_error',
1307         p_msg => fnd_message.get,
1308         p_force_log => TRUE
1309       );
1310     END IF;
1311     fnd_message.set_name('BIL','BIL_BI_OPTY_LOG_MISSING');
1312     bil_bi_util_collection_pkg.writeLog
1313     (
1314       p_log_level => fnd_log.LEVEL_ERROR,
1315       p_module => g_pkg || l_proc || ' proc_error',
1316       p_msg => fnd_message.get,
1317       p_force_log => TRUE
1318     );
1319   END IF;
1320 
1321   IF (g_setup_error_flag) THEN
1322     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1323       bil_bi_util_collection_pkg.writeLog
1324       (
1325         p_log_level => fnd_log.LEVEL_EVENT,
1326         p_module => g_pkg || l_proc ,
1327         p_msg => 'Setup Validition failed'
1328       );
1329     END IF;
1330     RAISE G_SETUP_VAL_ERROR;
1331   END IF;
1332 
1333 
1334     l_curr_coll_start := get_start_curr_coll;
1335 
1336 
1337    l_first_fact_run := fnd_date.displaydt_to_date(get_first_success_period('BIL_BI_OPDTL_F'));
1338 
1339 
1340  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1341       bil_bi_util_collection_pkg.writeLog
1342       (
1343         p_log_level => fnd_log.LEVEL_EVENT,
1344         p_module => g_pkg || l_proc ,
1345         p_msg => 'p_truncate is ' || p_truncate
1346       );
1347     END IF;
1348 
1349 
1350 
1351   IF (p_truncate = 'Y') THEN
1352     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1353       bil_bi_util_collection_pkg.writeLog
1354       (
1355         p_log_level => fnd_log.LEVEL_EVENT,
1356         p_module => g_pkg || l_proc ,
1357         p_msg => 'Truncating'
1358       );
1359     END IF;
1360     BIS_COLLECTION_UTILITIES.deleteLogForObject ('BIL_BI_PIPELINE_F');
1361 	BIS_COLLECTION_UTILITIES.deleteLogForObject ('BIL_BI_PIPEC_F');
1362     bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_PIPELINE_STG');
1363     bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_PIPELINE_F');
1364     bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_PIPEC_F');
1365     bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_DENLOG_STG');
1366 	bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_TIME');
1367     bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_OPDTL_DENLOG_TMP');
1368   ELSE
1369            ----------------------------------------------------
1370                 -- CHECK FOR RESUME
1371            ----------------------------------------------------
1372     SELECT sum(cnt) INTO l_count FROM (select count(1) cnt from bil_bi_pipeline_stg
1373      union all select count(1) cnt from BIL_BI_DENLOG_STG)
1374     ;
1375     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1376       bil_bi_util_collection_pkg.writeLog
1377       (
1378         p_log_level => fnd_log.LEVEL_EVENT,
1379         p_module => g_pkg || l_proc ,
1380         p_msg => 'No. of Rows in Staging are :'||l_count
1381       );
1382     END IF;
1383 
1384     IF (l_count > 0) THEN
1385       get_last_failure_periods('BIL_BI_PIPELINE_F', l_failure_from, l_failure_to);
1386       l_number_of_runs := get_number_of_runs('BIL_BI_PIPELINE_F');
1387 
1388       IF(l_number_of_runs <=1 ) then
1389          l_mode := 'INIT';
1390       ELSE
1391       	 l_mode := 'INCR';
1392       END IF;
1393 
1394        Populate_Currency_Rate(l_mode);
1395 
1396 	bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_TIME');
1397 
1398        populate_hist_dates(l_failure_from+1, l_failure_to);
1399 
1400 	   get_period_ids(l_failure_from, x_day=>l_day,
1401 		x_week => l_week, x_period => l_period, x_qtr => l_qtr,
1402 		  x_year => l_year, x_min_date_id => l_min_date_id, x_max_date_id => l_max_date_id);
1403 
1404 
1405 
1406       -- rebuild baseline since stage table can be in any state
1407 
1408       bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_PIPELINE_STG');
1409 
1410    --if gap is lessthan 3 days, we don't need to rebuild baseline
1411    -- since we will use 7.0.1 approach to insert into staging
1412 
1413    IF (l_failure_to - l_failure_from)  > 3 THEN
1414 
1415 	l_small_gap := FALSE;
1416 
1417 	  Build_Baseline(l_failure_from);
1418 
1419 
1420       IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1421             bil_bi_util_collection_pkg.writeLog
1422             (
1423               p_log_level => fnd_log.LEVEL_EVENT,
1424               p_module => g_pkg || l_proc ,
1425               p_msg =>'Gap Is' || l_failure_to - l_failure_from
1426             );
1427         END IF;
1428 
1429 
1430 
1431 
1432        /*Update the staging with new conversion rates.*/
1433       Summary_Err_Check(l_mode,
1434       			 x_valid_curr => l_valid_curr,
1435                           x_valid_date => l_valid_date,
1436                           x_valid_prod => l_valid_prod,
1437                           x_return_warn => l_return_warn_resume);
1438       IF ((l_valid_curr = 'Y') AND (l_valid_date = 'Y')) THEN
1439         IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1440             bil_bi_util_collection_pkg.writeLog
1441             (
1442               p_log_level => fnd_log.LEVEL_EVENT,
1443               p_module => g_pkg || l_proc ,
1444               p_msg =>'Summary Error Check Successful!'
1445             );
1446         END IF;
1447 
1448         bil_bi_util_collection_pkg.analyze_table('BIL_BI_PIPELINE_STG',TRUE, 10, 'GLOBAL');
1449         bil_bi_util_collection_pkg.analyze_table('BIL_BI_DENLOG_STG',TRUE, 10, 'GLOBAL');
1450         bil_bi_util_collection_pkg.analyze_table('BIL_BI_OPDTL_DENLOG_TMP',TRUE, 10, 'GLOBAL');
1451 
1452 
1453 
1454 		-- this will insert the first day record
1455        -- if l_last_run+1 < curr collection start, insert into hist. otherwise curr and
1456 
1457 
1458 	   IF l_failure_from < l_curr_coll_start THEN
1459         Insert_Into_Summary(l_mode);
1460 
1461 	   ELSE
1462 	      Insert_Into_Curr_Sumry(l_failure_from,  l_week, l_period, l_qtr,
1463 		  l_year, l_min_date_id, l_max_date_id);
1464 	   END IF;
1465 
1466 
1467 
1468 
1469 	ELSE
1470         RAISE G_INVALID_DIM;
1471       END IF;
1472 
1473 
1474 	ELSE --Gap less or equal to 3 days
1475 
1476 	l_small_gap := TRUE;
1477 
1478 	 Insert_Into_Stg_SmallGap(l_failure_from, l_failure_to, l_first_fact_run);
1479 
1480 
1481 	 Populate_Curr_Rate_SmallGap;
1482 
1483 
1484 	 select count(1)
1485 	 into l_stg_count
1486 	 from bil_bi_pipeline_stg;
1487 
1488 
1489 	IF l_stg_count > 0 THEN
1490      Summary_Err_Check_SmallGap(x_valid_curr => l_valid_curr,
1491                         x_valid_date => l_valid_date,
1492                         x_valid_prod => l_valid_prod,
1493                         x_return_warn => l_return_warn);
1494 	END IF;
1495 
1496 
1497 
1498 	IF ((l_valid_curr = 'Y') AND (l_valid_date = 'Y')) THEN
1499         IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1500             bil_bi_util_collection_pkg.writeLog
1501             (
1502               p_log_level => fnd_log.LEVEL_EVENT,
1503               p_module => g_pkg || l_proc ,
1504               p_msg =>'Summary Error Check Successful!'
1505             );
1506         END IF;
1507 
1508         bil_bi_util_collection_pkg.analyze_table('BIL_BI_PIPELINE_STG',TRUE, 10, 'GLOBAL');
1509 
1510 
1511 	--Insert the first day of the gap, since gap_fill will only insert from 2nd day on
1512 	 Ins_Into_CurrSum_SmGap(l_failure_from,  l_week, l_period, l_qtr,
1513 		  l_year, l_min_date_id, l_max_date_id);
1514 
1515 	ELSE
1516         RAISE G_INVALID_DIM;
1517       END IF;
1518 
1519 
1520 
1521 	END IF; -- gap less then 3 days
1522 
1523 
1524 
1525 Gap_Fill(l_failure_from+1, l_failure_to, l_curr_coll_start, l_mode, l_small_gap);
1526 
1527 
1528         l_resume_flag := 'Y';
1529         bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_PIPELINE_STG');
1530          bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_DENLOG_STG');
1531          --execute immediate 'Truncate table BIL_BI_DENLOG_STG';
1532          bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_OPDTL_DENLOG_TMP');
1533 
1534        -- Added by TR (to populate the correct period_to and period_from dates in Attribute
1535        --  columns of bis_refresh_log)
1536        SELECT TO_CHAR(MIN(snap_date),'DD-MM-YYYY')
1537 	    INTO l_period_from
1538          FROM bil_bi_pipec_f;
1539 
1540 		SELECT TO_CHAR(MAX(snap_date),'DD-MM-YYYY')
1541 	    INTO l_period_to
1542          FROM bil_bi_pipec_f;
1543 
1544 
1545 
1546 	 IF l_curr_coll_start between l_failure_from+1 and l_failure_to THEN
1547 
1548 	    INSERT INTO bis_refresh_log
1549         (
1550           Request_id,
1551           Concurrent_id,
1552           Object_name,
1553           Status,
1554           Start_date,
1555           Period_from,
1556           Period_to,
1557           Number_processed_record,
1558           Exception_message,
1559           Creation_date,
1560           Created_by,
1561           Last_update_date,
1562           Last_update_login,
1563           Last_updated_by
1564         )
1565         VALUES
1566         (
1567           g_request_id,
1568           g_program_id,
1569           'BIL_BI_PIPELINE_F',
1570           'SUCCESS',
1571           g_program_start,
1572           l_failure_from,
1573           l_curr_coll_start-1,
1574           g_row_num,
1575           'Successful resumed from last gap fill',
1576           sysdate,
1577           g_user_id,
1578           sysdate,
1579           g_login_id,
1580           g_user_id
1581         );
1582 		   INSERT INTO bis_refresh_log
1583         (
1584           Request_id,
1585           Concurrent_id,
1586           Object_name,
1587           Status,
1588           Start_date,
1589           Period_from,
1590           Period_to,
1591           Number_processed_record,
1592           Exception_message,
1593           Creation_date,
1594           Created_by,
1595           Last_update_date,
1596           Last_update_login,
1597           Last_updated_by,
1598           Attribute1,
1599           Attribute2
1600         )
1601         VALUES
1602         (
1603           g_request_id,
1604           g_program_id,
1605           'BIL_BI_PIPEC_F',
1606           'SUCCESS',
1607           g_program_start,
1608           l_curr_coll_start,
1609           l_failure_to,
1610           g_row_num,
1611           'Successful resumed from last gap fill',
1612           sysdate,
1613           g_user_id,
1614           sysdate,
1615           g_login_id,
1616           g_user_id,
1617           l_period_from,
1618           l_period_to
1619         );
1620 
1621 	ELSE
1622 		  INSERT INTO bis_refresh_log
1623         (
1624           Request_id,
1625           Concurrent_id,
1626           Object_name,
1627           Status,
1628           Start_date,
1629           Period_from,
1630           Period_to,
1631           Number_processed_record,
1632           Exception_message,
1633           Creation_date,
1634           Created_by,
1635           Last_update_date,
1636           Last_update_login,
1637           Last_updated_by,
1638           Attribute1,
1639           Attribute2
1640         )
1641         VALUES
1642         (
1643           g_request_id,
1644           g_program_id,
1645           'BIL_BI_PIPEC_F',
1646           'SUCCESS',
1647           g_program_start,
1648           l_failure_from,
1649           l_failure_to,
1650           g_row_num,
1651           'Successful resumed from last gap fill',
1652           sysdate,
1653           g_user_id,
1654           sysdate,
1655           g_login_id,
1656           g_user_id,
1657           l_period_from,
1658           l_period_to
1659         );
1660 	END IF;
1661 
1662       commit;
1663     END IF; -- count > 0
1664   END IF; -- if not purge then resume
1665 
1666 
1667        -- disabled this logic
1668        /*
1669        IF sysdate < (trunc(sysdate) + 18/24) THEN
1670            -- if before 6pm , count as last day
1671            l_date := trunc(sysdate) - 1;
1672        ELSE
1673            l_date =trunc(sysdate) ;
1674        END IF;
1675        */
1676 
1677 
1678     -- resume during gap fill is handled by reading l_last_run after resume
1679 
1680     l_last_run :=
1681       trunc(fnd_date.displaydt_to_date(BIS_COLLECTION_UTILITIES.get_last_refresh_period('BIL_BI_PIPEC_F')));
1682     l_last_fact_run :=
1683       trunc(fnd_date.displaydt_to_date(BIS_COLLECTION_UTILITIES.get_last_refresh_period('BIL_BI_OPDTL_F')));
1684 
1685     l_date := l_last_fact_run; -- fact could be run on previous day or the same day
1686 
1687 
1688 --------------------------------------------------------------
1689     -- IF FACT HASN'T BEEN RUN, CAN'T RUN PIPELINE EITHER.
1690 --------------------------------------------------------------
1691 
1692    IF (l_last_fact_run = G_global_start_date) THEN
1693 
1694        IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_ERROR) THEN
1695         -- This error message is an unusual case of standalone run of CP in incremental mode
1696         -- hence it doesn't use a translated msg string
1697          bil_bi_util_collection_pkg.writeLog
1698          (
1699            p_log_level =>fnd_log.LEVEL_ERROR,
1700            p_module => g_pkg || l_proc ,
1701            p_msg => 'Load Sales Opportunity Base Summary program must be run before this program.'
1702          );
1703        END IF;
1704 
1705       BIS_COLLECTION_UTILITIES.wrapup(FALSE,
1706            g_row_num,
1707            'Load Sales Opportunity Base Summary program not run.',
1708            l_date,
1709            l_date
1710            );
1711      retcode := -1;
1712      return;
1713 
1714    END IF;
1715 
1716 --------------------------------------------------------------
1717 -- DETECT GAPS AND FILL THEM. (Treat initial as a big gap)
1718 --------------------------------------------------------------
1719 
1720 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1721          bil_bi_util_collection_pkg.writeLog
1722          (
1723            p_log_level =>fnd_log.LEVEL_ERROR,
1724            p_module => g_pkg || l_proc ,
1725            p_msg => 'l_date is ' || l_date || ' l_last_run is ' || l_last_run || ' g_global is' || g_global_start_date
1726          );
1727        END IF;
1728 
1729 
1730 --we want to make sure that there is data in as_leads_log
1731 --for the day that we want to build the baseline on
1732 --this is true in any case, not only wher p_start_date the MIN date in as_leads_log
1733 --for this purpose, we replace l_start_date with
1734 --the closest date in as_leads_log from l_start_date,
1735 --that is in the range we are collecting for
1736 --if there is data for l_start_date itself, we will build baseline on l_start_date
1737 
1738 
1739 --get the earliest dates in the as log tables
1740 BEGIN
1741 
1742 SELECT MIN(last_update_date)
1743 into l_min_lead_date
1744 from as_leads_log
1745 WHERE last_update_date between l_start_date and l_date;
1746 
1747 
1748 EXCEPTION
1749 		 WHEN NO_DATA_FOUND THEN
1750 
1751     null;
1752 END;
1753 
1754 l_start_date := NVL(TRUNC(l_min_lead_date), l_start_date);
1755 
1756 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1757          bil_bi_util_collection_pkg.writeLog
1758          (
1759            p_log_level =>fnd_log.LEVEL_ERROR,
1760            p_module => g_pkg || l_proc ,
1761            p_msg => 'l_min_lead_date is ' || l_min_lead_date || ' l_start_date is ' || l_start_date || ' g_global is' || g_global_start_date
1762          );
1763 END IF;
1764 
1765 
1766    IF (l_last_run < l_date - 1) THEN
1767     IF (l_last_run =  G_global_start_date) THEN
1768       -- initial load (gap from global_start_date to l_date-1)
1769 
1770      -- truncate table here since there is a possibibility the build base line was
1771      -- successful on the 1st run and the rest failed.
1772 
1773      bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_PIPELINE_F');
1774      bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_PIPEC_F');
1775 
1776 
1777       -- setup baseline on the global start date
1778       Build_Baseline(l_start_date);
1779 
1780 	  bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_TIME');
1781 
1782 	  l_small_gap := FALSE;
1783 
1784         populate_hist_dates(l_start_date+1, l_date);
1785 
1786 
1787       -- build up the denlog_tmp table for the gap range
1788       -- l_date instead of l_date -1 is because denlog will check for time < l_date
1789       Build_Denlog(l_start_date+1, l_date, l_curr_coll_start, 'INIT');
1790 
1791       bil_bi_util_collection_pkg.analyze_table('BIL_BI_PIPELINE_STG',TRUE, 10, 'GLOBAL');
1792       bil_bi_util_collection_pkg.analyze_table('BIL_BI_DENLOG_STG',TRUE, 10, 'GLOBAL');
1793       bil_bi_util_collection_pkg.analyze_table('BIL_BI_OPDTL_DENLOG_TMP',TRUE, 10, 'GLOBAL');
1794 
1795       Populate_Currency_Rate('INIT');
1796 
1797       l_failure_from := g_global_start_date;
1798       l_failure_to   := l_date-1;
1799 
1800       Summary_Err_Check('INIT',
1801       			x_valid_curr => l_valid_curr,
1802                         x_valid_date => l_valid_date,
1803                         x_valid_prod => l_valid_prod,
1804                         x_return_warn => l_return_warn);
1805 
1806      IF ((l_valid_curr = 'Y') AND (l_valid_date = 'Y') ) THEN
1807       -- this will insert the 1st day record.
1808       Insert_Into_Summary('INIT');
1809 
1810       -- incrementally build up the rest of the days
1811 	  Gap_Fill(l_start_date+1, l_date-1, l_curr_coll_start, 'INIT', l_small_gap) ;
1812       bil_bi_util_collection_pkg.analyze_table('BIL_BI_PIPELINE_F',TRUE, 10, 'GLOBAL');
1813      Else
1814        RAISE G_INVALID_DIM;
1815      END IF; -- errcheck ok
1816 
1817 
1818     ELSE
1819 
1820 	  bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_TIME');
1821 
1822       populate_hist_dates(l_last_run+2, l_date);
1823 
1824 
1825 	-- incr   load
1826     -- Delete data from current pipe fact if it is older than 3 weeks (from beggining of period
1827     -- when we start to collect daily)
1828 
1829       -- get the first day in the current pipeline table to see if we need to delete
1830    SELECT MIN(snap_date)
1831    into l_min_curr_date
1832    FROM bil_bi_pipec_f;
1833 
1834    IF l_min_curr_date < l_curr_coll_start THEN
1835     DELETE_FROM_CURR(l_curr_coll_start);
1836    END IF;
1837 
1838     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_STATEMENT) THEN
1839       bil_bi_util_collection_pkg.writeLog
1840       (
1841         p_log_level => fnd_log.LEVEL_EVENT,
1842         p_module => g_pkg || l_proc ,
1843         p_msg => 'Incr gap fill: l_min_curr_date is :' ||l_min_curr_date || ' l_curr_coll_start is : ' ||l_curr_coll_start);
1844 
1845     END IF;
1846 
1847 
1848      IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1849        bil_bi_util_collection_pkg.writeLog
1850        (
1851          p_log_level => fnd_log.LEVEL_EVENT,
1852          p_module => g_pkg || l_proc ,
1853          p_msg => 'snapshot gap found: gcredit_typeid is ' || g_credit_type_id ||
1854            ' global date is '|| g_global_start_date
1855        );
1856      END IF;
1857 
1858       -- gap exists
1859 
1860    DELETE FROM bil_bi_pipeline_f WHERE snap_date between l_last_run+1 and l_date-1;
1861    commit;
1862 
1863       IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1864      bil_bi_util_collection_pkg.writeLog
1865      (
1866        p_log_level => fnd_log.LEVEL_EVENT,
1867        p_module => g_pkg || l_proc ,
1868        p_msg => 'Deleted  '|| sql%rowcount ||' from BIL_BI_PIPELINE_F table for gap between' || (l_last_run+1) ||
1869          ' and ' || (l_date-1));
1870    END IF;
1871 
1872    DELETE FROM bil_bi_pipec_f WHERE snap_date between l_last_run+1 and l_date-1;
1873    commit;
1874 
1875       IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1876      bil_bi_util_collection_pkg.writeLog
1877      (
1878        p_log_level => fnd_log.LEVEL_EVENT,
1879        p_module => g_pkg || l_proc ,
1880        p_msg => 'Deleted  '|| sql%rowcount ||' from BIL_BI_PIPEC_F table for gap between' || (l_last_run+1) ||
1881          ' and ' || (l_date-1));
1882    END IF;
1883 
1884 
1885        get_period_ids(l_last_run+1, x_day => l_day,
1886 	   				  x_week => l_week, x_period => l_period, x_qtr => l_qtr,
1887 		  			  x_year => l_year, x_min_date_id => l_min_date_id, x_max_date_id => l_max_date_id);
1888 
1889 	  --if gap is lessthan 3 days, we don't need to build baseline
1890    -- since we will use 7.0.1 approach to insert into staging
1891 
1892    IF l_date - (l_last_run+1)>  3 THEN
1893 
1894     l_small_gap := FALSE;
1895 
1896 	--we want to make sure that there is data in as_leads_log
1897 --for the day that we want to build the baseline on
1898 --this is true in any case, not only wher p_start_date the MIN date in as_leads_log
1899 --for this purpose, we replace l_start_date with
1900 --the closest date in as_leads_log from l_start_date,
1901 --that is in the range we are collecting for
1902 --if there is data for l_start_date itself, we will build baseline on l_start_date
1903 
1904 
1905 --get the earliest dates in the as log tables
1906 BEGIN
1907 
1908 SELECT MIN(last_update_date)
1909 into l_min_lead_date
1910 from as_leads_log
1911 WHERE last_update_date between l_last_run+1 and l_date;
1912 
1913 
1914 EXCEPTION
1915 		 WHEN NO_DATA_FOUND THEN
1916 
1917     null;
1918 END;
1919 
1920 
1921 
1922 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1923          bil_bi_util_collection_pkg.writeLog
1924          (
1925            p_log_level =>fnd_log.LEVEL_ERROR,
1926            p_module => g_pkg || l_proc ,
1927            p_msg => 'l_min_lead_date is ' || l_min_lead_date || ' l_last_run+1 is ' || l_start_date || ' g_global is' || g_global_start_date
1928          );
1929 END IF;
1930 
1931      IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1932    	    bil_bi_util_collection_pkg.writeLog
1933       (
1934         p_log_level => fnd_log.LEVEL_EVENT,
1935         p_module => g_pkg || l_proc ,
1936         p_msg => 'Inside gap more than 3 days');
1937 	 END IF;
1938 
1939     -- setup baseline on the global start date
1940       Build_Baseline(NVL(l_min_lead_date, l_last_run+1));
1941 
1942 
1943       Build_Denlog(l_last_run+2, l_date, l_curr_coll_start, 'INCR');
1944 
1945 	  bil_bi_util_collection_pkg.analyze_table('BIL_BI_PIPELINE_STG',TRUE, 10, 'GLOBAL');
1946       bil_bi_util_collection_pkg.analyze_table('BIL_BI_DENLOG_STG',TRUE, 10, 'GLOBAL');
1947       bil_bi_util_collection_pkg.analyze_table('BIL_BI_OPDTL_DENLOG_TMP',TRUE, 10, 'GLOBAL');
1948 
1949 
1950 
1951      Populate_Currency_Rate('INCR');
1952      l_failure_from := l_last_run+1;
1953      l_failure_to   := l_date-1;
1954 
1955       --- if time and currency, stop
1956       --- if product information , warning and skip
1957 
1958      Summary_Err_Check('INCR',
1959      			x_valid_curr => l_valid_curr,
1960                         x_valid_date => l_valid_date,
1961                         x_valid_prod => l_valid_prod,
1962                         x_return_warn => l_return_warn);
1963 
1964      IF ((l_valid_curr = 'Y') AND (l_valid_date = 'Y') ) THEN
1965        -- this will insert the firs day record
1966        -- if l_last_run+1 < curr collection start, insert into hist. otherwise curr and
1967        -- if end of period, hist. as well
1968 
1969 
1970 	   IF l_last_run+1 < l_curr_coll_start THEN
1971               Insert_Into_Summary('INCR');
1972 
1973 	   ELSE
1974 	      Insert_Into_Curr_Sumry(l_last_run+1, l_week, l_period, l_qtr,
1975 		  l_year, l_min_date_id, l_max_date_id);
1976 	   END IF;
1977 
1978 
1979 
1980      Else
1981        RAISE G_INVALID_DIM;
1982      END IF; -- errcheck ok
1983 
1984 
1985 
1986    	ELSE --Gap less than 3 days
1987 
1988 
1989 	l_small_gap := TRUE;
1990 
1991 	      IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
1992 	    bil_bi_util_collection_pkg.writeLog
1993       (
1994         p_log_level => fnd_log.LEVEL_EVENT,
1995         p_module => g_pkg || l_proc ,
1996         p_msg => 'Inside gap less than 3 days');
1997          END IF;
1998 
1999 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
2000      bil_bi_util_collection_pkg.writeLog
2001      (
2002        p_log_level => fnd_log.LEVEL_EVENT,
2003        p_module => g_pkg || l_proc ,
2004        p_msg => 'Dates: l_last_run+1' || (l_last_run+1) ||
2005          ' and l_date-1' || (l_date-1)  ||
2006          ' and l_first_fact_run' || (l_first_fact_run) );
2007    END IF;
2008 
2009 
2010 	 Insert_Into_Stg_SmallGap(l_last_run+1, l_date-1, l_first_fact_run);
2011 
2012 
2013 
2014 	 Populate_Curr_Rate_SmallGap;
2015      l_failure_from := l_last_run+1;
2016      l_failure_to   := l_date-1;
2017 
2018 
2019      Summary_Err_Check_SmallGap(x_valid_curr => l_valid_curr,
2020                         x_valid_date => l_valid_date,
2021                         x_valid_prod => l_valid_prod,
2022                         x_return_warn => l_return_warn);
2023 
2024 
2025 
2026 
2027 
2028 	 IF ((l_valid_curr = 'Y') AND (l_valid_date = 'Y') ) THEN
2029 
2030 	 --Insert the first day of the gap, since gap_fill will only insert from 2nd day on
2031 	 Ins_Into_CurrSum_SmGap(l_last_run+1,  l_week, l_period, l_qtr,
2032 		  l_year, l_min_date_id, l_max_date_id);
2033 
2034 	 ELSE
2035 	 	 RAISE G_INVALID_DIM;
2036 	END IF;
2037 
2038 
2039 	END IF; --gap more or less than 3 days
2040 
2041 
2042        -- this will loop through the rest
2043        Gap_Fill(l_last_run+2, l_date-1, l_curr_coll_start, 'INCR', l_small_gap) ;
2044 
2045 
2046 
2047  END IF; -- initial/incr gap
2048 
2049        -- Added by TR (to populate the correct period_to and period_from dates in Attribute
2050        --  columns of bis_refresh_log)
2051        SELECT TO_CHAR(MIN(snap_date),'DD-MM-YYYY')
2052          INTO l_period_from
2053          FROM bil_bi_pipec_f;
2054 
2055 		  SELECT TO_CHAR(MAX(snap_date),'DD-MM-YYYY')
2056          INTO  l_period_to
2057          FROM bil_bi_pipec_f;
2058 
2059  IF l_curr_coll_start BETWEEN l_last_run+2 and l_date-1 THEN
2060 --Insert into bis_refresh_log for both tables: bil_bi_pipeline_f and bil_bi_pipec_f
2061 
2062 
2063      INSERT INTO bis_refresh_log
2064      (
2065        Request_id,
2066        Concurrent_id,
2067        Object_name,
2068        Status,
2069        Start_date,
2070        Period_from,
2071        Period_to,
2072        Number_processed_record,
2073        Exception_message,
2074        Creation_date,
2075        Created_by,
2076        Last_update_date,
2077        Last_update_login,
2078        Last_updated_by
2079      )
2080      VALUES
2081      (
2082        g_request_id,
2083        g_program_id,
2084        'BIL_BI_PIPELINE_F',
2085        'SUCCESS',
2086        g_program_start,
2087        l_last_run+1,
2088        l_curr_coll_start-1,
2089        g_row_num,
2090        'Successful gap fill',
2091        sysdate,
2092        g_user_id,
2093        sysdate,
2094        g_login_id,
2095        g_user_id
2096      );
2097      commit;
2098 
2099 
2100      INSERT INTO bis_refresh_log
2101      (
2102        Request_id,
2103        Concurrent_id,
2104        Object_name,
2105        Status,
2106        Start_date,
2107        Period_from,
2108        Period_to,
2109        Number_processed_record,
2110        Exception_message,
2111        Creation_date,
2112        Created_by,
2113        Last_update_date,
2114        Last_update_login,
2115        Last_updated_by,
2116        Attribute1,
2117        Attribute2
2118      )
2119      VALUES
2120      (
2121        g_request_id,
2122        g_program_id,
2123        'BIL_BI_PIPEC_F',
2124        'SUCCESS',
2125        g_program_start,
2126        l_curr_coll_start,
2127        l_date-1,
2128        g_row_num,
2129        'Successful gap fill',
2130        sysdate,
2131        g_user_id,
2132        sysdate,
2133        g_login_id,
2134        g_user_id,
2135        l_period_from,
2136        l_period_to
2137      );
2138      commit;
2139 
2140 ELSE
2141 -- l_coll_start is before the beggining of the gap
2142 -- so innsert only in the current pipeline table
2143 --this is the only other possible case, since we are always
2144 --collecting up to sysdate (or opty base sumry last run)
2145      INSERT INTO bis_refresh_log
2146      (
2147        Request_id,
2148        Concurrent_id,
2149        Object_name,
2150        Status,
2151        Start_date,
2152        Period_from,
2153        Period_to,
2154        Number_processed_record,
2155        Exception_message,
2156        Creation_date,
2157        Created_by,
2158        Last_update_date,
2159        Last_update_login,
2160        Last_updated_by,
2161        Attribute1,
2162        Attribute2
2163      )
2164      VALUES
2165      (
2166        g_request_id,
2167        g_program_id,
2168        'BIL_BI_PIPEC_F',
2169        'SUCCESS',
2170        g_program_start,
2171        l_last_run+1,
2172        l_date-1,
2173        g_row_num,
2174        'Successful gap fill',
2175        sysdate,
2176        g_user_id,
2177        sysdate,
2178        g_login_id,
2179        g_user_id,
2180        l_period_from,
2181        l_period_to
2182      );
2183      commit;
2184 
2185 
2186 END IF;
2187 
2188 
2189 
2190 END IF; -- gap exists
2191 
2192 
2193     -- clean up
2194 
2195      bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_PIPELINE_STG');
2196      bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_DENLOG_STG');
2197      --execute immediate 'Truncate table BIL_BI_DENLOG_STG';
2198      bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_OPDTL_DENLOG_TMP');
2199    -- regular snapshot
2200 
2201    -- delete the existing snapshot, since could be multiple runs anyway in the same day
2202 
2203     DELETE FROM bil_bi_pipec_f WHERE snap_date = l_date;
2204     commit;
2205 
2206 
2207 	   -- get the first day in the current pipeline table to see if we need to delete
2208    SELECT MIN(snap_date)
2209    into l_min_curr_date
2210    FROM bil_bi_pipec_f;
2211 
2212    --delete from current pipeline table if data is older than 3 weeks
2213       IF l_min_curr_date < l_curr_coll_start THEN
2214     DELETE_FROM_CURR(l_curr_coll_start);
2215    END IF;
2216 
2217     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_STATEMENT) THEN
2218       bil_bi_util_collection_pkg.writeLog
2219       (
2220         p_log_level => fnd_log.LEVEL_EVENT,
2221         p_module => g_pkg || l_proc ,
2222         p_msg => 'l_min_curr_date is :' ||l_min_curr_date || ' l_curr_coll_start is : ' ||l_curr_coll_start);
2223 
2224     END IF;
2225 
2226 
2227 
2228     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
2229       bil_bi_util_collection_pkg.writeLog
2230       (
2231         p_log_level => fnd_log.LEVEL_EVENT,
2232         p_module => g_pkg || l_proc ,
2233         p_msg => 'Deleted  '|| sql%rowcount ||' rows from  BIL_BI_PIPEC_F for '||l_date
2234       );
2235     END IF;
2236 
2237 
2238 
2239      --- directly insert from opdtl_f into the pipec_f table
2240 
2241    	get_period_ids(l_date, x_day => l_day,
2242 		x_week => l_week, x_period => l_period, x_qtr => l_qtr,
2243 		  x_year => l_year, x_min_date_id => l_min_date_id, x_max_date_id => l_max_date_id);
2244 
2245 
2246 		 BEGIN
2247                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.sd_pwk(:2); END;';
2248                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lwk, IN l_date;
2249 
2250                  SELECT week_end_date
2251                  INTO l_sd_lwk_end
2252                  from fii_time_day
2253                  where report_date=l_sd_lwk;
2254 
2255                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.ent_sd_pper_end(:2); END;';
2256                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lper, IN l_date;
2257 
2258                  SELECT LEAST(week_end_date, ent_period_end_date)
2259                  INTO l_sd_lper_end
2260                  from fii_time_day
2261                  where report_date=l_sd_lper;
2262 
2263                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.ent_sd_pqtr_end(:2); END;';
2264                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lqtr, IN l_date;
2265 
2266                  SELECT LEAST(week_end_date, ent_qtr_end_date)
2267                  INTO l_sd_lqtr_end
2268                  from fii_time_day
2269                  where report_date=l_sd_lqtr;
2270 
2271                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.ent_sd_lyr_end(:2); END;';
2272                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lyr, IN l_date;
2273 
2274                   SELECT LEAST(week_end_date, ent_year_end_date)
2275                  INTO l_sd_lyr_end
2276                  from fii_time_day
2277                  where report_date=l_sd_lyr;
2278 
2279 EXCEPTION WHEN NO_DATA_FOUND THEN
2280     NULL;
2281 END;
2282 
2283 
2284        --also insert prior amounts from bil_bi_pipeline_f
2285 --since we may not have data for the same day last week/quarter/period/year in the hist. table
2286 --we need to get the end of the week/quarter ...
2287 --if the same day last year is closer to the end of a certain week/quarter/period
2288 --than to the end of te year, get the closest date
2289 -- this can be achieved by using the bil_bi_time table:
2290 
2291 			 INSERT /*+ append parallel(f) */INTO bil_bi_pipec_f f
2292 			      (
2293 			        sales_group_id,
2294 			        salesrep_id,
2295 			        created_by ,
2296 			        creation_date,
2297 			        last_updated_by,
2298 			        last_update_date,
2299 			        last_update_login,
2300 			        request_id,
2301 			        program_application_id,
2302 			        program_id,
2303 			        program_update_date,
2304 			        snap_date,
2305 			        item_id,
2306 			        item_organization_id,
2307 			        win_probability,
2308 			        product_category_id,
2309 			        pipeline_amt_day,
2310 			        pipeline_amt_week,
2311 			        pipeline_amt_period,
2312 			        pipeline_amt_quarter,
2313 			        pipeline_amt_year,
2314 			        open_amt_day,
2315 			        open_amt_week,
2316 			        open_amt_period,
2317 			        open_amt_quarter,
2318 			        open_amt_year,
2319 			        pipeline_amt_day_s,
2320 			        pipeline_amt_week_s,
2321 			        pipeline_amt_period_s,
2322 			        pipeline_amt_quarter_s,
2323 			        pipeline_amt_year_s,
2324 			        open_amt_day_s,
2325 			        open_amt_week_s,
2326 			        open_amt_period_s,
2327 			        open_amt_quarter_s,
2328 			        open_amt_year_s,
2329 
2330                     prvprd_pipe_amt_wk  ,
2331                     prvprd_pipe_amt_PRD ,
2332                     prvprd_pipe_amt_qtr ,
2333                     prvprd_pipe_amt_yr  ,
2334                     prvprd_open_amt_wk  ,
2335                     prvprd_open_amt_PRD ,
2336                     prvprd_open_amt_qtr ,
2337                     prvprd_open_amt_yr  ,
2338                     prvprd_pipe_amt_wk_s,
2339                     prvprd_pipe_amt_PRD_s,
2340                     prvprd_pipe_amt_qtr_s,
2341                     prvprd_pipe_amt_yr_s,
2342                     prvprd_open_amt_wk_s,
2343                     prvprd_open_amt_PRD_s,
2344                     prvprd_open_amt_qtr_s,
2345                     prvprd_open_amt_yr_s,
2346 
2347                     prvyr_pipe_amt_wk   ,
2348                     prvyr_pipe_amt_PRD  ,
2349                     prvyr_pipe_amt_qtr  ,
2350                     prvyr_pipe_amt_yr   ,
2351                                         prvyr_open_amt_wk   ,
2352                     prvyr_open_amt_PRD  ,
2353                     prvyr_open_amt_qtr  ,
2354                     prvyr_open_amt_yr   ,
2355                     prvyr_pipe_amt_wk_s ,
2356                     prvyr_pipe_amt_PRD_s,
2357                     prvyr_pipe_amt_qtr_s,
2358                     prvyr_pipe_amt_yr_s ,
2359                     prvyr_open_amt_wk_s ,
2360                     prvyr_open_amt_PRD_s,
2361                     prvyr_open_amt_qtr_s,
2362                     prvyr_open_amt_yr_s
2363                       )
2364 
2365                             SELECT sales_group_id,
2366 			        salesrep_id,
2367 		            g_user_id created_by,
2368 			        SYSDATE creation_date,
2369 			        g_user_id last_updated_by,
2370 			        SYSDATE last_update_date,
2371 			        G_Login_Id last_update_login,
2372         			G_request_id request_id,
2373         			G_appl_id program_application_id,
2374         			G_program_id program_id,
2375 			        SYSDATE program_update_date,	        snap_date,
2376 			        item_id,
2377 			        item_organization_id,
2378 			        win_probability,
2379 			        product_category_id,
2380                     SUM(pipeline_amt_day) pipeline_amt_day,
2381                     SUM(pipeline_amt_week) pipeline_amt_week,
2382                     SUM(pipeline_amt_period) pipeline_amt_period,
2383                     SUM(pipeline_amt_quarter) pipeline_amt_quarter,
2384                     SUM(pipeline_amt_year) pipeline_amt_year,
2385                     SUM(open_amt_day)  open_amt_day   ,
2386                     SUM(open_amt_week)  open_amt_week  ,
2387                     SUM(open_amt_period) open_amt_period ,
2388                     SUM(open_amt_quarter) open_amt_quarter,
2389                     SUM(open_amt_year) open_amt_year   ,
2390                     SUM(pipeline_amt_day_s) pipeline_amt_day_s,
2391                     SUM(pipeline_amt_week_s) pipeline_amt_week_s,
2392                     SUM(pipeline_amt_period_s) pipeline_amt_period_s,
2393                     SUM(pipeline_amt_quarter_s) pipeline_amt_quarter_s,
2394                     SUM(pipeline_amt_year_s) pipeline_amt_year_s,
2395                     SUM(open_amt_day_s) open_amt_day_s  ,
2396                     SUM(open_amt_week_s) open_amt_week_s ,
2397                     SUM(open_amt_period_s) open_amt_period_s,
2398                     SUM(open_amt_quarter_s) open_amt_quarter_s,
2399                     SUM(open_amt_year_s) open_amt_year_s ,
2400                                 SUM(prvprd_pipe_amt_wk)  prvprd_pipe_amt_wk,
2401                 SUM(prvprd_pipe_amt_PRD) prvprd_pipe_amt_PRD,
2402                 SUM(prvprd_pipe_amt_qtr) prvprd_pipe_amt_qtr,
2403                 SUM(prvprd_pipe_amt_yr) prvprd_pipe_amt_yr ,
2404                 SUM(prvprd_open_amt_wk) prvprd_open_amt_wk ,
2405                 SUM(prvprd_open_amt_PRD) prvprd_open_amt_PRD,
2406                 SUM(prvprd_open_amt_qtr) prvprd_open_amt_qtr,
2407                 SUM(prvprd_open_amt_yr)  prvprd_open_amt_yr,
2408                 SUM(prvprd_pipe_amt_wk_s)  prvprd_pipe_amt_wk_s,
2409                 SUM(prvprd_pipe_amt_PRD_s) prvprd_pipe_amt_PRD_s,
2410                 SUM(prvprd_pipe_amt_qtr_s) prvprd_pipe_amt_qtr_s,
2411                 SUM(prvprd_pipe_amt_yr_s) prvprd_pipe_amt_yr_s ,
2412                 SUM(prvprd_open_amt_wk_s) prvprd_open_amt_wk_s ,
2413                 SUM(prvprd_open_amt_PRD_s) prvprd_open_amt_PRD_s,
2414                 SUM(prvprd_open_amt_qtr_s) prvprd_open_amt_qtr_s,
2415                 SUM(prvprd_open_amt_yr_s) prvprd_open_amt_yr_s,
2416                 SUM(prvyr_pipe_amt_wk)  prvyr_pipe_amt_wk ,
2417                 SUM(prvyr_pipe_amt_PRD)  prvyr_pipe_amt_PRD,
2418                 SUM(prvyr_pipe_amt_qtr)  prvyr_pipe_amt_qtr,
2419                 SUM(prvyr_pipe_amt_yr) prvyr_pipe_amt_yr  ,
2420                 SUM(prvyr_open_amt_wk) prvyr_open_amt_wk   ,
2421                 SUM(prvyr_open_amt_PRD) prvyr_open_amt_PRD ,
2422                 SUM(prvyr_open_amt_qtr) prvyr_open_amt_qtr ,
2423                 SUM(prvyr_open_amt_yr) prvyr_open_amt_yr  ,
2424                 SUM(prvyr_pipe_amt_wk_s)  prvyr_pipe_amt_wk_s ,
2425                 SUM(prvyr_pipe_amt_PRD_s) prvyr_pipe_amt_PRD_s ,
2426                 SUM(prvyr_pipe_amt_qtr_s) prvyr_pipe_amt_qtr_s ,
2427                 SUM(prvyr_pipe_amt_yr_s)   prvyr_pipe_amt_yr_s,
2428                 SUM(prvyr_open_amt_wk_s) prvyr_open_amt_wk_s  ,
2429                 SUM(prvyr_open_amt_PRD_s) prvyr_open_amt_PRD_s ,
2430                 SUM(prvyr_open_amt_qtr_s) prvyr_open_amt_qtr_s ,
2431                 SUM(prvyr_open_amt_yr_s) prvyr_open_amt_yr_s
2432                   FROM (
2433 			      SELECT /*+ no_merge parallel(FACT) parallel(TIME) */
2434 			        SALES_GROUP_ID,
2435 			        SALESREP_ID,
2436 			        l_date snap_date,
2437 			        ITEM_ID,
2438 			        ITEM_ORGANIZATION_ID,
2439 			        decode(fact.win_loss_indicator, 'W', 100, fact.WIN_PROBABILITY) win_probability,
2440 			        PRODUCT_CATEGORY_ID,
2441 
2442         SUM(CASE
2443             WHEN time.report_date = l_date
2444             THEN fact.sales_credit_amt ELSE NULL END
2445         ) pipeline_amt_day,
2446         SUM(CASE
2447             WHEN time.week_id = l_week
2448             THEN fact.sales_credit_amt ELSE NULL END
2449         ) pipeline_amt_week,
2450         SUM(CASE
2451             WHEN time.ent_period_id = l_period
2452             THEN fact.sales_credit_amt ELSE NULL END ) pipeline_amt_period,
2453         SUM(CASE
2454             WHEN time.ent_qtr_id = l_qtr
2455             THEN fact.sales_credit_amt ELSE NULL END ) pipeline_amt_quarter,
2456         SUM(CASE
2457             WHEN time.ent_year_id = l_year
2458             THEN fact.sales_credit_amt ELSE NULL END ) pipeline_amt_year,
2459          SUM( CASE
2460             WHEN time.report_date = l_date  and open_status_flag = 'Y'
2461             THEN fact.sales_credit_amt ELSE NULL END) open_amt_day,
2462         SUM( CASE
2463             WHEN time.week_id = l_week and open_status_flag = 'Y'
2464             THEN fact.sales_credit_amt ELSE NULL END) open_amt_week,
2465         SUM(CASE
2466             WHEN time.ent_period_id = l_period  and open_status_flag = 'Y'
2467             THEN fact.sales_credit_amt ELSE NULL END ) open_amt_period,
2468         SUM(CASE
2469             WHEN time.ent_qtr_id = l_qtr  and open_status_flag = 'Y'
2470             THEN fact.sales_credit_amt ELSE NULL END ) open_amt_quarter,
2471         SUM(CASE
2472             WHEN time.ent_year_id = l_year  and open_status_flag = 'Y'
2473             THEN fact.sales_credit_amt ELSE NULL END ) open_amt_year,
2474         SUM(CASE
2475             WHEN time.report_date = l_date
2476             THEN fact.sales_credit_amt_s ELSE NULL END
2477         ) pipeline_amt_day_s,
2478         SUM(CASE
2479             WHEN time.week_id = l_week
2480             THEN fact.sales_credit_amt_s ELSE NULL END
2481         ) pipeline_amt_week_s,
2482         SUM(CASE
2483             WHEN time.ent_period_id = l_period
2484             THEN fact.sales_credit_amt_s ELSE NULL END ) pipeline_amt_period_s,
2485         SUM(CASE
2486             WHEN time.ent_qtr_id = l_qtr
2487             THEN fact.sales_credit_amt_s ELSE NULL END ) pipeline_amt_quarter_s,
2488         SUM(CASE
2489             WHEN time.ent_year_id = l_year
2490             THEN fact.sales_credit_amt_s ELSE NULL END ) pipeline_amt_year_s,
2491          SUM( CASE
2492             WHEN time.report_date = l_date and open_status_flag = 'Y'
2493             THEN fact.sales_credit_amt_s ELSE NULL END) open_amt_day_s,
2494         SUM( CASE
2495             WHEN time.week_id = l_week  and open_status_flag = 'Y'
2496             THEN fact.sales_credit_amt_s ELSE NULL END) open_amt_week_s,
2497         SUM(CASE
2498             WHEN time.ent_period_id = l_period  and open_status_flag = 'Y'
2499             THEN fact.sales_credit_amt_s ELSE NULL END ) open_amt_period_s,
2500         SUM(CASE
2501             WHEN time.ent_qtr_id = l_qtr  and open_status_flag = 'Y'
2502             THEN fact.sales_credit_amt_s ELSE NULL END ) open_amt_quarter_s,
2503         SUM(CASE
2504             WHEN time.ent_year_id = l_year  and open_status_flag = 'Y'
2505             THEN fact.sales_credit_amt_s ELSE NULL END ) open_amt_year_s,
2506                 null prvprd_pipe_amt_wk  ,
2507                 null prvprd_pipe_amt_PRD ,
2508                 null prvprd_pipe_amt_qtr ,
2509                 null prvprd_pipe_amt_yr  ,
2510                 null prvprd_open_amt_wk  ,
2511                 null prvprd_open_amt_PRD ,
2512                 null prvprd_open_amt_qtr ,
2513                 null prvprd_open_amt_yr  ,
2514                 null prvprd_pipe_amt_wk_s,
2515                 null prvprd_pipe_amt_PRD_s,
2516                 null prvprd_pipe_amt_qtr_s,
2517                 null prvprd_pipe_amt_yr_s,
2518                 null prvprd_open_amt_wk_s,
2519                 null prvprd_open_amt_PRD_s,
2520                 null prvprd_open_amt_qtr_s,
2521                 null prvprd_open_amt_yr_s,
2522                 null prvyr_pipe_amt_wk   ,
2523                 null prvyr_pipe_amt_PRD  ,
2524                 null prvyr_pipe_amt_qtr  ,
2525                 null prvyr_pipe_amt_yr   ,
2526                 null prvyr_open_amt_wk   ,
2527                 null prvyr_open_amt_PRD  ,
2528                 null prvyr_open_amt_qtr  ,
2529                 null prvyr_open_amt_yr   ,
2530                 null prvyr_pipe_amt_wk_s ,
2531                 null prvyr_pipe_amt_PRD_s,
2532                 null prvyr_pipe_amt_qtr_s,
2533                 null prvyr_pipe_amt_yr_s ,
2534                 null prvyr_open_amt_wk_s ,
2535                 null prvyr_open_amt_PRD_s,
2536                 null prvyr_open_amt_qtr_s,
2537                 null prvyr_open_amt_yr_s
2538 
2539 		        FROM
2540         bil_bi_opdtl_f fact,
2541         fii_time_day time
2542       WHERE
2543         fact.OPTY_CLOSE_TIME_ID =  time.report_date_julian
2544         and forecast_rollup_flag = 'Y'
2545         and fact.OPTY_CLOSE_TIME_ID between l_min_date_id and l_max_date_id
2546       GROUP BY
2547         sales_group_id,
2548         salesrep_id,
2549         item_id,
2550         item_organization_id,
2551         decode(fact.win_loss_indicator, 'W', 100, fact.WIN_PROBABILITY),
2552         product_category_id
2553       HAVING
2554         SUM(CASE
2555             WHEN time.week_id = l_week
2556             THEN fact.sales_credit_amt ELSE NULL END
2557         ) is not null or
2558         SUM(CASE
2559             WHEN time.ent_year_id = l_year
2560             THEN fact.sales_credit_amt ELSE NULL END ) is not null
2561      UNION ALL
2562                       SELECT /*+ parallel(f) */ sales_group_id,
2563 			        salesrep_id,
2564 			        l_date snap_date,
2565 			        item_id,
2566 			        item_organization_id,
2567 			        win_probability,
2568 			        product_category_id,
2569                     null pipeline_amt_day ,
2570                     null pipeline_amt_week,
2571                     null pipeline_amt_period,
2572                     null pipeline_amt_quarter,
2573                     null pipeline_amt_year,
2574                     null open_amt_day     ,
2575                     null open_amt_week    ,
2576                     null open_amt_period  ,
2577                     null open_amt_quarter ,
2578                     null open_amt_year    ,
2579                     null pipeline_amt_day_s ,
2580                     null pipeline_amt_week_s,
2581                     null pipeline_amt_period_s ,
2582                     null pipeline_amt_quarter_s,
2583                     null pipeline_amt_year_s,
2584                     null open_amt_day_s   ,
2585                     null open_amt_week_s  ,
2586                     null open_amt_period_s,
2587                     null open_amt_quarter_s,
2588                     null open_amt_year_s,
2589 				decode(f.snap_date, l_sd_lwk_end, pipeline_amt_week, null) prvprd_pipe_amt_wk  ,
2590                 decode(f.snap_date, l_sd_lper_end, pipeline_amt_period, null) prvprd_pipe_amt_PRD ,
2591                 decode(f.snap_date, l_sd_lqtr_end, pipeline_amt_quarter, null) prvprd_pipe_amt_qtr ,
2592                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year, null) prvprd_pipe_amt_yr  ,
2593                 decode(f.snap_date, l_sd_lwk_end, open_amt_week, null) prvprd_open_amt_wk  ,
2594                 decode(f.snap_date, l_sd_lper_end, open_amt_period, null) prvprd_open_amt_PRD ,
2595                 decode(f.snap_date, l_sd_lqtr_end, open_amt_quarter, null) prvprd_open_amt_qtr ,
2596                 decode(f.snap_date, l_sd_lyr_end, open_amt_year, null) prvprd_open_amt_yr  ,
2597                 decode(f.snap_date, l_sd_lwk_end, pipeline_amt_week_s, null) prvprd_pipe_amt_wk_s  ,
2598                 decode(f.snap_date, l_sd_lper_end, pipeline_amt_period_s, null) prvprd_pipe_amt_PRD_s ,
2599                 decode(f.snap_date, l_sd_lqtr_end, pipeline_amt_quarter_s, null) prvprd_pipe_amt_qtr_s ,
2600                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year_s, null) prvprd_pipe_amt_yr_s  ,
2601                 decode(f.snap_date, l_sd_lwk_end, open_amt_week_s, null) prvprd_open_amt_wk_s  ,
2602                 decode(f.snap_date, l_sd_lper_end, open_amt_period_s, null) prvprd_open_amt_PRD_s ,
2603                 decode(f.snap_date, l_sd_lqtr_end, open_amt_quarter_s, null) prvprd_open_amt_qtr_s ,
2604                 decode(f.snap_date, l_sd_lyr_end, open_amt_year_s, null) prvprd_open_amt_yr_s,
2605                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_week, null) prvyr_pipe_amt_wk   ,
2606                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_period, null) prvyr_pipe_amt_PRD  ,
2607                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_quarter, null) prvyr_pipe_amt_qtr  ,
2608                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year, null) prvyr_pipe_amt_yr   ,
2609                 decode(f.snap_date, l_sd_lyr_end, open_amt_week, null) prvyr_open_amt_wk   ,
2610                 decode(f.snap_date, l_sd_lyr_end, open_amt_period, null) prvyr_open_amt_PRD  ,
2611                 decode(f.snap_date, l_sd_lyr_end, open_amt_quarter, null) prvyr_open_amt_qtr  ,
2612                 decode(f.snap_date, l_sd_lyr_end, open_amt_year, null) prvyr_open_amt_yr   ,
2613                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_week_s, null) prvyr_pipe_amt_wk_s   ,
2614                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_period_s, null) prvyr_pipe_amt_PRD_s  ,
2615                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_quarter_s, null) prvyr_pipe_amt_qtr_s  ,
2616                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year_s, null) prvyr_pipe_amt_yr_s   ,
2617                 decode(f.snap_date, l_sd_lyr_end, open_amt_week_s, null) prvyr_open_amt_wk_s   ,
2618                 decode(f.snap_date, l_sd_lyr_end, open_amt_period, null) prvyr_open_amt_PRD_s  ,
2619                 decode(f.snap_date, l_sd_lyr_end, open_amt_quarter, null) prvyr_open_amt_qtr_s  ,
2620                 decode(f.snap_date, l_sd_lyr_end, open_amt_year, null) prvyr_open_amt_yr_s
2621                     FROM BIL_BI_PIPELINE_F f
2622                     where snap_date in (l_sd_lwk_end, l_sd_lper_end
2623                     ,l_sd_lqtr_end, l_sd_lyr_end)
2624 					UNION ALL
2625                       SELECT  sales_group_id,
2626 			        salesrep_id,
2627 			        l_date snap_date,
2628 			        item_id,
2629 			        item_organization_id,
2630 			        win_probability,
2631 			        product_category_id,
2632                     null pipeline_amt_day ,
2633                     null pipeline_amt_week,
2634                     null pipeline_amt_period,
2635                     null pipeline_amt_quarter,
2636                     null pipeline_amt_year,
2637                     null open_amt_day     ,
2638                     null open_amt_week    ,
2639                     null open_amt_period  ,
2640                     null open_amt_quarter ,
2641                     null open_amt_year    ,
2642                     null pipeline_amt_day_s ,
2643                     null pipeline_amt_week_s,
2644                     null pipeline_amt_period_s ,
2645                     null pipeline_amt_quarter_s,
2646                     null pipeline_amt_year_s,
2647                     null open_amt_day_s   ,
2648                     null open_amt_week_s  ,
2649                     null open_amt_period_s,
2650                     null open_amt_quarter_s,
2651                     null open_amt_year_s  ,
2652                 pipeline_amt_week prvprd_pipe_amt_wk  ,
2653                 null prvprd_pipe_amt_PRD ,
2654                 null prvprd_pipe_amt_qtr ,
2655                 null prvprd_pipe_amt_yr  ,
2656                 open_amt_week prvprd_open_amt_wk  ,
2657                 null prvprd_open_amt_PRD ,
2658                 null prvprd_open_amt_qtr ,
2659                 null prvprd_open_amt_yr  ,
2660                 pipeline_amt_week_s prvprd_pipe_amt_wk_s  ,
2661                 null prvprd_pipe_amt_PRD_s ,
2662                 null prvprd_pipe_amt_qtr_s ,
2663                 null prvprd_pipe_amt_yr_s  ,
2664                 open_amt_week_s prvprd_open_amt_wk_s  ,
2665                 null prvprd_open_amt_PRD_s ,
2666                 null prvprd_open_amt_qtr_s ,
2667                 null prvprd_open_amt_yr_s,
2668                 null prvyr_pipe_amt_wk   ,
2669                 null prvyr_pipe_amt_PRD  ,
2670                 null prvyr_pipe_amt_qtr  ,
2671                 null prvyr_pipe_amt_yr   ,
2672                 null prvyr_open_amt_wk   ,
2673                 null prvyr_open_amt_PRD  ,
2674                 null prvyr_open_amt_qtr  ,
2675                 null prvyr_open_amt_yr   ,
2676                 null prvyr_pipe_amt_wk_s   ,
2677                 null prvyr_pipe_amt_PRD_s  ,
2678                 null prvyr_pipe_amt_qtr_s  ,
2679                 null prvyr_pipe_amt_yr_s   ,
2680                 null prvyr_open_amt_wk_s   ,
2681                 null prvyr_open_amt_PRD_s  ,
2682                 null prvyr_open_amt_qtr_s  ,
2683                 null prvyr_open_amt_yr_s
2684                     FROM BIL_BI_PIPEC_F f
2685                     where snap_date = l_sd_lwk
2686                      )
2687                     GROUP BY
2688                        sales_group_id,
2689 			        salesrep_id,
2690 			        snap_date,
2691 			        item_id,
2692 			        item_organization_id,
2693 			        win_probability,
2694 			        product_category_id
2695 
2696                     ;
2697 
2698 
2699 
2700 
2701 
2702 
2703      g_row_num:= sql%rowcount;
2704      IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
2705        bil_bi_util_collection_pkg.writeLog
2706        (
2707          p_log_level => fnd_log.LEVEL_EVENT,
2708          p_module => g_pkg || l_proc ,
2709          p_msg => 'Inserted  '|| g_row_num ||' into BIL_BI_PIPEC_F table from BIL_BI_OPDTL_F for date'
2710            || l_date
2711        );
2712      END IF;
2713 
2714   commit;
2715 --   BIS_COLLECTION_UTILITIES.wrapup(TRUE,
2716   --         g_row_num,
2717     --       'Snapshot taken for ' || l_date,
2718       --     l_date,
2719         --   l_date );
2720 
2721        SELECT TO_CHAR(MIN(snap_date),'DD-MM-YYYY')
2722          INTO l_period_from
2723          FROM bil_bi_pipec_f;
2724 
2725 
2726 		        SELECT
2727               TO_CHAR(MAX(snap_date),'DD-MM-YYYY')
2728          INTO
2729               l_period_to
2730          FROM bil_bi_pipec_f;
2731 
2732 
2733 		INSERT INTO bis_refresh_log
2734      (
2735        Request_id,
2736        Concurrent_id,
2737        Object_name,
2738        Status,
2739        Start_date,
2740        Period_from,
2741        Period_to,
2742        Number_processed_record,
2743        Exception_message,
2744        Creation_date,
2745        Created_by,
2746        Last_update_date,
2747        Last_update_login,
2748        Last_updated_by,
2749        Attribute1,
2750        Attribute2
2751      )
2752      VALUES
2753      (
2754        g_request_id,
2755        g_program_id,
2756        'BIL_BI_PIPEC_F',
2757        'SUCCESS',
2758        g_program_start,
2759        l_date,
2760        l_date,
2761        g_row_num,
2762        'Snapshot taken for ' || l_date,
2763        sysdate,
2764        g_user_id,
2765        sysdate,
2766        g_login_id,
2767        g_user_id,
2768        l_period_from,
2769        l_period_to
2770      );
2771 
2772     commit;
2773 
2774      IF (l_return_warn = 'Y' or l_return_warn_resume= 'Y') THEN
2775         retcode := 1;
2776         ELSE
2777         retcode := 0;
2778      END IF;
2779 
2780 
2781 /*
2782   A generic line in the log file that requests the user to see the o/p file for
2783   further info.
2784 */
2785     IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
2786       bil_bi_util_collection_pkg.writeLog
2787       (
2788         p_log_level => fnd_log.LEVEL_EVENT,
2789         p_module => g_pkg || l_proc,
2790         p_msg =>
2791           ' If there have been errors, Please refer to the Concurrent program output file for more information '
2792       );
2793     END IF;
2794 
2795    IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
2796     bil_bi_util_collection_pkg.writeLog(
2797              p_log_level => fnd_log.LEVEL_PROCEDURE,
2798              p_module => g_pkg || l_proc || ' end',
2799              p_msg => 'End of Procedure '|| l_proc);
2800    END IF;
2801 
2802    EXCEPTION
2803    WHEN G_BIS_SETUP_ERROR THEN
2804      ROLLBACK;
2805      -- no point calling wrapup if setup is not successful
2806      retcode :=  -1;
2807 
2808    WHEN G_SETUP_VAL_ERROR THEN
2809      ROLLBACK;
2810      BIS_COLLECTION_UTILITIES.wrapup(FALSE,
2811            g_row_num,
2812            'Setup issues detected.',
2813            l_date,
2814            l_date
2815            );
2816      retcode := -1;
2817 
2818   WHEN G_INVALID_DIM THEN
2819      -- no need to rollback or truncate, resume will take care this
2820      commit;
2821 
2822 
2823      BIS_COLLECTION_UTILITIES.wrapup(FALSE,
2824            g_row_num,
2825            'Time/Currency dimension not setup.',
2826            l_failure_from,
2827            l_failure_to
2828            );
2829      retcode := -1;
2830    WHEN OTHERS Then
2831 
2832 
2833       bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_PIPELINE_STG');
2834       bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_DENLOG_STG');
2835       --execute immediate 'Truncate table BIL_BI_DENLOG_STG';
2836      bil_bi_util_collection_pkg.Truncate_Table('BIL_BI_OPDTL_DENLOG_TMP');
2837 
2838   commit;
2839   BIS_COLLECTION_UTILITIES.wrapup(FALSE,
2840            g_row_num,
2841            SQLERRM,
2842            l_date,
2843            l_date
2844            );
2845 
2846        fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
2847        fnd_message.set_token('ERRNO' ,SQLCODE);
2848        fnd_message.set_token('REASON' ,SQLERRM);
2849   fnd_message.set_token('ROUTINE' , l_proc);
2850        bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
2851   p_module => g_pkg || l_proc || ' proc_error',
2852   p_msg => fnd_message.get,
2853   p_force_log => TRUE);
2854   retcode := -1;
2855    END;
2856 
2857 
2858 PROCEDURE Populate_Currency_Rate(p_mode IN varchar2) IS
2859   l_proc     VARCHAR2(100);
2860  BEGIN
2861  l_proc  := 'Populate_Currency_Rate';
2862 
2863  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
2864    bil_bi_util_collection_pkg.writeLog(
2865              p_log_level => fnd_log.LEVEL_PROCEDURE,
2866              p_module => g_pkg || l_proc || ' begin',
2867              p_msg => 'Start of Procedure '|| l_proc);
2868  END IF;
2869 
2870    IF (p_mode = 'INIT') THEN
2871      MERGE /*+ parallel(sumry) */ INTO BIL_BI_CURRENCY_RATE sumry
2872      USING
2873      (
2874        SELECT
2875          txn_currency,
2876          effective_date,
2877                 DECODE(txn_currency,g_prim_currency,1,fii_currency.get_global_rate_primary(txn_currency,
2878                                                             trunc(least(sysdate,effective_date))))  prate,
2879          DECODE(g_sec_currency,NULL,NULL,decode(txn_currency,g_sec_currency,1,
2880            fii_currency.get_global_rate_secondary(txn_currency,trunc(least(sysdate,effective_date))))) srate
2881 
2882        FROM
2883        (
2884          SELECT
2885            DISTINCT txn_currency,
2886            effective_date
2887          FROM
2888          (
2889            select /*+ parallel(stg) */ txn_currency, effective_date
2890            from bil_bi_pipeline_stg stg
2891          where effective_date >= g_global_start_date
2892            UNION ALL
2893            select /*+ parallel(stg) */ currency_code, decision_date
2894            from BIL_BI_DENLOG_STG stg
2895            where decision_date >= g_global_start_date
2896          )
2897        )
2898      ) rates
2899      ON
2900      (
2901        rates.txn_currency = sumry.currency_code
2902        AND rates.effective_date = sumry.exchange_date
2903      )
2904      WHEN MATCHED THEN
2905        UPDATE SET sumry.exchange_rate = rates.prate,sumry.exchange_rate_s = rates.srate
2906      WHEN NOT MATCHED THEN
2907        INSERT
2908        (
2909          sumry.currency_code,
2910          sumry.exchange_date,
2911          sumry.exchange_rate,
2912          sumry.exchange_rate_s
2913        )
2914        VALUES
2915        (
2916          rates.txn_currency,
2917          rates.effective_date,
2918          rates.prate,
2919          rates.srate
2920        );
2921 
2922 	   	--gather stats on bil_bi_currency_rate during initial load
2923 	bil_bi_util_collection_pkg.analyze_table('BIL_BI_CURRENCY_RATE',TRUE, 10, 'GLOBAL');
2924 
2925     ELSE
2926       MERGE INTO BIL_BI_CURRENCY_RATE sumry
2927      USING
2928      (
2929        SELECT
2930          txn_currency,
2931          effective_date,
2932         DECODE(txn_currency,g_prim_currency,1,fii_currency.get_global_rate_primary(txn_currency,
2933                                                             trunc(least(sysdate,effective_date))))  prate,
2934          DECODE(g_sec_currency,NULL,NULL,decode(txn_currency,g_sec_currency,1,
2935            fii_currency.get_global_rate_secondary(txn_currency,trunc(least(sysdate,effective_date)))))  srate
2936 
2937        FROM
2938        (
2939          SELECT
2940            DISTINCT txn_currency,
2941            effective_date
2942          FROM
2943          (
2944            select txn_currency, effective_date
2945            from bil_bi_pipeline_stg stg
2946            where effective_date >= g_global_start_date
2947            UNION ALL
2948            select currency_code, decision_date
2949            from BIL_BI_DENLOG_STG stg
2950            where decision_date >= g_global_start_date
2951          )
2952        )
2953      ) rates
2954      ON
2955      (
2956        rates.txn_currency = sumry.currency_code
2957        AND rates.effective_date = sumry.exchange_date
2958      )
2959      WHEN MATCHED THEN
2960        UPDATE SET sumry.exchange_rate = rates.prate,sumry.exchange_rate_s = rates.srate
2961      WHEN NOT MATCHED THEN
2962        INSERT
2963        (
2964          sumry.currency_code,
2965          sumry.exchange_date,
2966          sumry.exchange_rate,
2967          sumry.exchange_rate_s
2968        )
2969        VALUES
2970        (
2971          rates.txn_currency,
2972          rates.effective_date,
2973          rates.prate,
2974          rates.srate
2975        );
2976     END IF;
2977 
2978   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
2979         bil_bi_util_collection_pkg.writeLog(
2980                 p_log_level => fnd_log.LEVEL_EVENT,
2981     p_module => g_pkg || l_proc ,
2982     p_msg => 'Inserted  '||sql%rowcount||' into bil_bi_currency_rate table');
2983   END IF;
2984   commit;
2985   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
2986     bil_bi_util_collection_pkg.writeLog(
2987              p_log_level => fnd_log.LEVEL_PROCEDURE,
2988              p_module => g_pkg || l_proc || ' end',
2989              p_msg => 'End of Procedure '|| l_proc);
2990  END IF;
2991  EXCEPTION
2992    WHEN OTHERS THEN
2993       fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
2994       fnd_message.set_token('ERRNO' ,SQLCODE);
2995       fnd_message.set_token('REASON' ,SQLERRM);
2996 fnd_message.set_token('ROUTINE' , l_proc);
2997       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
2998   p_module => g_pkg || l_proc || ' proc_error',
2999   p_msg => fnd_message.get,
3000   p_force_log => TRUE);
3001 
3002   RAISE;
3003  END   Populate_Currency_Rate;
3004 
3005 
3006 PROCEDURE Summary_Err_Check(p_mode IN Varchar2,
3007 			    x_valid_curr OUT NOCOPY VARCHAR2,
3008                             x_valid_date OUT NOCOPY VARCHAR2,
3009                             x_valid_prod OUT NOCOPY VARCHAR2,
3010                             x_return_warn OUT NOCOPY VARCHAR2
3011                             ) IS
3012 
3013       l_time_cnt          NUMBER;
3014       l_conv_rate_cnt     NUMBER;
3015       l_stg_min           NUMBER;
3016       l_stg_max           NUMBER;
3017       l_stg_min_txn_dt         DATE;
3018       l_stg_max_txn_dt         DATE;
3019       l_stg_min_eff_dt         DATE;
3020       l_stg_max_eff_dt         DATE;
3021       l_stg_min_dt         DATE;
3022       l_stg_max_dt         DATE;
3023       l_day_min           NUMBER;
3024       l_day_max           NUMBER;
3025       l_has_missing_date   BOOLEAN;
3026       l_count       NUMBER;
3027       l_lead_num    NUMBER;
3028       l_eff_date    DATE;
3029       l_number_of_rows     NUMBER;
3030       l_int_type       VARCHAR2(100);
3031       l_prim_code      VARCHAR2(100);
3032       l_sec_code       VARCHAR2(100);
3033       l_warn       VARCHAR2(1);
3034 
3035 --      l_prim_miss BOOLEAN :=FALSE;
3036 --      l_sec_miss  BOOLEAN :=FALSE;
3037 
3038      cursor c_item_prod is
3039       SELECT lead_number  FROM bil_bi_pipeline_stg
3040       WHERE  nvl(product_category_id,-1)=-1
3041       and effective_date >= g_global_start_date
3042       union all
3043       SELECT lead_number  FROM BIL_BI_DENLOG_STG
3044       WHERE  nvl(product_category_id,-1)=-1
3045       and decision_date >= g_global_start_date;
3046 
3047 
3048    l_proc VARCHAR2(100);
3049 BEGIN
3050 
3051       l_time_cnt :=0;
3052       l_conv_rate_cnt:=0;
3053       l_has_missing_date := FALSE;
3054       l_count :=0;
3055       l_number_of_rows :=0;
3056       l_proc := 'Summary_Err_Check';
3057 
3058   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
3059    bil_bi_util_collection_pkg.writeLog(
3060              p_log_level => fnd_log.LEVEL_PROCEDURE,
3061              p_module => g_pkg || l_proc || ' begin',
3062              p_msg => 'Start of Procedure '|| l_proc);
3063   END IF;
3064 
3065 IF(p_mode = 'INIT') THEN
3066       UPDATE /*+ parallel(stg) */ bil_bi_pipeline_stg stg
3067          SET  (stg.prim_conversion_rate,conversion_rate_s) =
3068            (
3069              select
3070                exchange_rate,exchange_rate_s
3071              from BIL_BI_CURRENCY_RATE
3072              where
3073                currency_code = stg.txn_currency and
3074                exchange_date = stg.effective_date
3075            );
3076 
3077 
3078        MERGE /*+ PARALLEL(stg) */ INTO bil_bi_denlog_stg stg
3079 USING
3080   (SELECT /*+ PARALLEL(rates) */
3081     exchange_rate,
3082     exchange_rate_s,
3083     exchange_date,
3084     currency_code
3085   FROM
3086     bil_bi_currency_rate rates) curr_rate
3087 ON (curr_rate.EXCHANGE_DATE = stg.decision_date AND curr_rate.currency_code = stg.currency_code)
3088 WHEN MATCHED THEN
3089   UPDATE SET
3090     stg.prim_conversion_rate = curr_rate.exchange_rate,
3091     stg.conversion_rate_s = curr_rate.exchange_rate_s;
3092 
3093 ELSE
3094      UPDATE  bil_bi_pipeline_stg stg
3095          SET  (stg.prim_conversion_rate,conversion_rate_s) =
3096            (
3097              select
3098                exchange_rate,exchange_rate_s
3099              from BIL_BI_CURRENCY_RATE
3100              where
3101                currency_code = stg.txn_currency and
3102                exchange_date = stg.effective_date
3103            );
3104 
3105 
3106       UPDATE BIL_BI_DENLOG_STG stg
3107          SET  (stg.prim_conversion_rate,conversion_rate_s) =
3108            (
3109              select
3110                exchange_rate,exchange_rate_s
3111              from BIL_BI_CURRENCY_RATE
3112              where
3113                currency_code = stg.currency_code and
3114                exchange_date = stg.decision_date
3115                 );
3116 
3117 END IF;
3118 
3119 
3120       IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
3121         bil_bi_util_collection_pkg.writeLog(
3122                 p_log_level => fnd_log.LEVEL_EVENT,
3123     p_module => g_pkg || l_proc ,
3124     p_msg => 'Updated rates for '|| sql%rowcount || ' rows');
3125   END IF;
3126 
3127  -- need this commit for the rollup not to roll back all the currencys, doesn't really matter anyway
3128       commit;
3129 
3130 
3131 
3132 IF(p_mode = 'INIT') THEN
3133       SELECT sum(cnt)
3134       INTO   l_conv_rate_cnt
3135       FROM   (
3136                select /*+ parallel(stg) */ count(1) cnt from BIL_BI_PIPELINE_STG stg
3137              WHERE  ((prim_conversion_rate < 0  OR  prim_conversion_rate IS NULL)
3138              OR (g_sec_currency IS NOT NULL and (conversion_rate_s < 0  OR  conversion_rate_s IS NULL)))
3139              and effective_date >= g_global_start_date
3140               union all
3141               select /*+ parallel(stg) */ count(1) cnt from BIL_BI_DENLOG_STG stg
3142              WHERE  ((prim_conversion_rate < 0  OR  prim_conversion_rate IS NULL)
3143              OR (g_sec_currency IS NOT NULL and (conversion_rate_s < 0  OR  conversion_rate_s IS NULL)))
3144              and decision_date >= g_global_start_date
3145              );
3146 ELSE
3147      SELECT sum(cnt)
3148       INTO   l_conv_rate_cnt
3149       FROM   (
3150                select count(1) cnt from BIL_BI_PIPELINE_STG
3151              WHERE  ((prim_conversion_rate < 0  OR  prim_conversion_rate IS NULL)
3152              OR (g_sec_currency IS NOT NULL and (conversion_rate_s < 0  OR  conversion_rate_s IS NULL)))
3153              and effective_date >= g_global_start_date
3154               union all
3155               select count(1) cnt from BIL_BI_DENLOG_STG
3156              WHERE  ((prim_conversion_rate < 0  OR  prim_conversion_rate IS NULL)
3157              OR (g_sec_currency IS NOT NULL and (conversion_rate_s < 0  OR  conversion_rate_s IS NULL)))
3158              and decision_date >= g_global_start_date
3159              );
3160 
3161 END IF;
3162 
3163      IF (l_conv_rate_cnt >0) THEN
3164        IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
3165          bil_bi_util_collection_pkg.writeLog
3166          (
3167            p_log_level =>fnd_log.LEVEL_ERROR,
3168            p_module => g_pkg || l_proc ,
3169            p_msg => 'Missing currency conversion rates found, program will exit with error status.'||
3170            'Please fix the missing conversion rates'
3171          );
3172        END IF;
3173        Report_Missing_Rates;
3174 --       l_prim_miss := TRUE;
3175       x_valid_curr := 'N';
3176     ELSE
3177       x_valid_curr := 'Y';
3178      END IF;
3179 
3180 
3181 /*also for effective date. need to call the time api*/
3182 
3183 IF(p_mode = 'INIT') THEN
3184      SELECT   MIN(stg.SNAP_DATE), Max(stg.SNAP_DATE)
3185              ,MIN(stg.Effective_DATE), Max(stg.Effective_DATE)
3186       INTO   l_stg_min_txn_dt, l_stg_max_txn_dt, l_stg_min_eff_dt, l_stg_max_eff_dt
3187       FROM   (select /*+ parallel(stg) stg*/ snap_date , effective_date from BIL_BI_PIPELINE_STG stg
3188               where effective_date >= g_global_start_date
3189               union all
3190               select /*+ parallel(stg) stg*/last_update_date snap_date,  decision_date effective_date from BIL_BI_DENLOG_STG stg
3191               where decision_date >= g_global_start_date) stg;
3192 ELSE
3193      SELECT   MIN(stg.SNAP_DATE), Max(stg.SNAP_DATE)
3194              ,MIN(stg.Effective_DATE), Max(stg.Effective_DATE)
3195       INTO   l_stg_min_txn_dt, l_stg_max_txn_dt, l_stg_min_eff_dt, l_stg_max_eff_dt
3196       FROM   (select snap_date , effective_date from BIL_BI_PIPELINE_STG
3197               where effective_date >= g_global_start_date
3198               union all
3199               select last_update_date snap_date,  decision_date effective_date from BIL_BI_DENLOG_STG
3200               where decision_date >= g_global_start_date) stg;
3201 END IF;
3202 
3203       IF (l_stg_min_txn_dt < l_stg_min_eff_dt) THEN
3204          l_stg_min_dt := l_stg_min_txn_dt;
3205       ELSE
3206          l_stg_min_dt := l_stg_min_eff_dt;
3207       END IF;
3208 
3209       IF (l_stg_max_txn_dt < l_stg_max_eff_dt) THEN
3210          l_stg_max_dt := l_stg_max_eff_dt;
3211       ELSE
3212          l_stg_max_dt := l_stg_max_txn_dt;
3213       END IF;
3214 
3215 
3216       --write_log(p_msg => 'Date range:'||l_stg_min_dt||'  '||l_stg_max_dt, p_log => 'N');
3217 IF l_stg_min_dt IS NOT NULL AND l_stg_max_dt IS NOT NULL THEN
3218       FII_TIME_API.check_missing_date (l_stg_min_dt,l_stg_max_dt,l_has_missing_date);
3219 
3220 END IF;
3221       IF (l_has_missing_date) THEN
3222         IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
3223           bil_bi_util_collection_pkg.writeLog
3224           (
3225             p_log_level => fnd_log.LEVEL_EVENT,
3226             p_module => g_pkg || l_proc ,
3227             p_msg => 'Time Dimension is not fully populated. '||
3228               ' Please populate Time dimension to cover the date range you are collecting');
3229         END IF;
3230          x_valid_date := 'N';
3231       Else
3232          x_valid_date := 'Y';
3233       END IF;
3234 
3235 --   ELSE
3236  --        x_valid_curr := 'Y';
3237  --     x_valid_date := 'Y';
3238  --  END IF;        --- incremental mode only
3239 
3240       --- The following check applies both initial and incremental mode
3241 
3242       -- check for oppty close date  => don't need in this case since it is already restricted.
3243 
3244        -- check for bad item/product
3245   l_number_of_rows := 0;
3246       OPEN c_item_prod;
3247         LOOP
3248            FETCH c_item_prod into
3249           l_lead_num;
3250            EXIT WHEN c_item_prod%NOTFOUND ;
3251      l_number_of_rows :=l_number_of_rows + 1;
3252 
3253       IF(l_number_of_rows=1) THEN
3254       -- print header
3255 
3256         IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
3257           bil_bi_util_collection_pkg.writeLog
3258           (
3259             p_log_level => fnd_log.LEVEL_EVENT,
3260             p_module => g_pkg || l_proc ,
3261             p_msg => ' Some opportunties had null product category. They have not been collected.'
3262           );
3263         END IF;
3264 
3265      fnd_message.set_name('BIL','BIL_BI_ITEM_PROD_WARN_HDR');
3266      bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_ERROR,
3267                             p_module => g_pkg || l_proc || ' proc_error',
3268                           p_msg => fnd_message.get,
3269                           p_force_log => TRUE);
3270       END IF;
3271 
3272       -- print detail
3273 
3274      fnd_message.set_name('BIL','BIL_BI_ITEM_PROD_ERR_DTL');
3275      fnd_message.set_token('OPPNUM', l_lead_num);
3276      bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_ERROR,
3277                             p_module => g_pkg || l_proc || ' proc_error',
3278                           p_msg => fnd_message.get,
3279                           p_force_log => TRUE);
3280 
3281 
3282         END LOOP;
3283      CLOSE c_item_prod;
3284 
3285 
3286   IF ( l_number_of_rows  > 0) THEN
3287       x_valid_prod := 'N';
3288       x_return_warn := 'Y';
3289   ELSE
3290       x_valid_prod := 'Y';
3291       x_return_warn := 'N';
3292   END IF;
3293 
3294 
3295      IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
3296     bil_bi_util_collection_pkg.writeLog(
3297              p_log_level => fnd_log.LEVEL_PROCEDURE,
3298              p_module => g_pkg || l_proc || ' end',
3299              p_msg => 'End of Procedure '|| l_proc);
3300      END IF;
3301 
3302 EXCEPTION
3303       WHEN OTHERS THEN
3304         fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
3305       fnd_message.set_token('ERRNO' ,SQLCODE);
3306       fnd_message.set_token('REASON' ,SQLERRM);
3307   fnd_message.set_token('ROUTINE' , l_proc);
3308       bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
3309   p_module => g_pkg || l_proc || ' proc_error',
3310   p_msg => fnd_message.get,
3311   p_force_log => TRUE);
3312   Raise;
3313 
3314 END Summary_Err_Check;
3315 
3316 
3317 PROCEDURE Report_Profile_Error(p_profile_name IN varchar2,
3318                                p_value IN varchar2,
3319                                p_exp_value IN varchar2) IS
3320 l_proc       VARCHAR2(100);
3321 BEGIN
3322 
3323  l_proc     := 'Report_Profile_Error';
3324 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
3325   bil_bi_util_collection_pkg.writeLog(
3326              p_log_level => fnd_log.LEVEL_PROCEDURE,
3327              p_module => g_pkg || l_proc || ' begin',
3328              p_msg => 'Start of Procedure '|| l_proc);
3329  END IF;
3330 
3331       IF (g_setup_error_flag = FALSE) THEN
3332 
3333         g_setup_error_flag := TRUE;
3334 
3335         fnd_message.set_name('BIL','BIL_BI_SETUP_INCOMPLETE');
3336      IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_ERROR) THEN
3337         bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_ERROR,
3338                       p_module => g_pkg || l_proc || ' proc_error',
3339                     p_msg => fnd_message.get,
3340                     p_force_log => TRUE);
3341 
3342           END IF;
3343 
3344       END IF;
3345                       IF (p_value <> p_exp_value) THEN
3346       fnd_message.set_name('BIL','BIL_BI_PROFILE_WRONG_VALUE');
3347         fnd_message.set_token('PROFILE_USER_NAME' ,bil_bi_util_collection_pkg.get_user_profile_name(p_profile_name));
3348         fnd_message.set_token('PROFILE_INTERNAL_NAME' ,p_profile_name);
3349         fnd_message.set_token('PROFILE_VALUE' , p_exp_value);
3350 
3351      IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_ERROR) THEN
3352         bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_ERROR,
3353                            p_module => g_pkg || l_proc || ' proc_error',
3354                    p_msg => fnd_message.get,
3355                    p_force_log => TRUE);
3356 
3357       END IF;
3358      END IF;
3359 
3360 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
3361   bil_bi_util_collection_pkg.writeLog(
3362              p_log_level => fnd_log.LEVEL_PROCEDURE,
3363              p_module => g_pkg || l_proc || ' end',
3364              p_msg => 'End of Procedure '|| l_proc);
3365  END IF;
3366 
3367 EXCEPTION
3368 
3369 WHEN OTHERS THEN
3370     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
3371     fnd_message.set_token('ERRNO' ,SQLCODE);
3372     fnd_message.set_token('REASON' ,SQLERRM);
3373   fnd_message.set_token('ROUTINE' , l_proc);
3374     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
3375   p_module => g_pkg || l_proc || ' proc_error',
3376   p_msg => fnd_message.get,
3377   p_force_log => TRUE);
3378 
3379     raise;
3380 
3381 END  Report_Profile_Error;
3382 
3383 PROCEDURE Report_Missing_Profile (p_profile_name IN varchar2) IS
3384   l_proc       VARCHAR2(100);
3385 BEGIN
3386   l_proc  := 'Report_Missing_Profile';
3387 
3388 
3389   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
3390     bil_bi_util_collection_pkg.writeLog
3391     (
3392       p_log_level => fnd_log.LEVEL_PROCEDURE,
3393       p_module => g_pkg || l_proc || ' begin',
3394       p_msg => 'Start of Procedure '|| l_proc
3395     );
3396   END IF;
3397 
3398  -- print the header
3399       IF (g_setup_error_flag = FALSE) THEN
3400         g_setup_error_flag := TRUE;
3401         fnd_message.set_name('BIL','BIL_BI_SETUP_INCOMPLETE');
3402         bil_bi_util_collection_pkg.writeLog
3403         (
3404           p_log_level => fnd_log.LEVEL_ERROR,
3405           p_module => g_pkg || l_proc || ' proc_error',
3406           p_msg => fnd_message.get,
3407           p_force_log => TRUE
3408         );
3409       END IF;
3410 
3411         fnd_message.set_name('BIL','BIL_BI_PROFILE_MISSING');
3412       fnd_message.set_token('PROFILE_USER_NAME' ,bil_bi_util_collection_pkg.get_user_profile_name(p_profile_name));
3413       fnd_message.set_token('PROFILE_INTERNAL_NAME' ,p_profile_name);
3414 
3415 
3416         bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_ERROR,
3417                      p_module => g_pkg || l_proc || ' proc_error',
3418                    p_msg => fnd_message.get,
3419                    p_force_log => TRUE);
3420 
3421 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
3422   bil_bi_util_collection_pkg.writeLog(
3423              p_log_level => fnd_log.LEVEL_PROCEDURE,
3424              p_module => g_pkg || l_proc || ' end',
3425              p_msg => 'End of Procedure '|| l_proc);
3426  END IF;
3427 
3428 EXCEPTION
3429 
3430 WHEN OTHERS THEN
3431     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
3432     fnd_message.set_token('ERRNO' ,SQLCODE);
3433     fnd_message.set_token('REASON' ,SQLERRM);
3434   fnd_message.set_token('ROUTINE' , l_proc);
3435     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
3436   p_module => g_pkg || l_proc || ' proc_error',
3437   p_msg => fnd_message.get,
3438   p_force_log => TRUE);
3439 
3440     raise;
3441 
3442 
3443 END Report_Missing_Profile;
3444 
3445 
3446 PROCEDURE Report_Missing_Rates IS
3447 
3448   l_stmt  VARCHAR2(500);
3449   l_count  NUMBER;
3450 
3451   cursor MissingRate_p is
3452     SELECT
3453       DISTINCT stg.txn_currency txn_currency,
3454       trunc( decode(stg.prim_conversion_rate,-3,to_date('01/01/1999','MM/DD/RRRR'),
3455             least(sysdate, stg.effective_date)) ) txn_date,
3456        decode(sign(nvl(stg.prim_conversion_rate,-1)),-1,'P') prim_curr_type,
3457        decode(sign(nvl(stg.CONVERSION_RATE_S,-1)),-1,'S')    sec_curr_type
3458     FROM  ( select txn_currency , effective_date, prim_conversion_rate, CONVERSION_RATE_S
3459             from  BIL_BI_PIPELINE_STG
3460             WHERE
3461             ((prim_conversion_rate < 0 OR prim_conversion_rate IS NULL )
3462             OR (g_sec_currency IS NOT NULL and (conversion_rate_s < 0  OR  conversion_rate_s IS NULL)))
3463             and effective_date >= g_global_start_date
3464             union all
3465             select currency_code , decision_date, prim_conversion_rate, CONVERSION_RATE_S
3466             from  BIL_BI_DENLOG_STG
3467             WHERE
3468             ((prim_conversion_rate < 0 OR prim_conversion_rate IS NULL )
3469             OR (g_sec_currency IS NOT NULL and (conversion_rate_s < 0  OR  conversion_rate_s IS NULL)))
3470             and decision_date >= g_global_start_date
3471       ) stg;
3472 
3473 l_proc VARCHAR2(100);
3474 
3475 BEGIN
3476 
3477 l_proc := 'Report_Missing_Rates';
3478 
3479 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
3480   bil_bi_util_collection_pkg.writeLog(
3481              p_log_level => fnd_log.LEVEL_PROCEDURE,
3482              p_module => g_pkg || l_proc || ' begin',
3483              p_msg => 'Start of Procedure '|| l_proc);
3484  END IF;
3485 
3486    BIS_COLLECTION_UTILITIES.WriteMissingRateHeader;
3487 
3488 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_STATEMENT) THEN
3489   bil_bi_util_collection_pkg.writeLog(
3490              p_log_level => fnd_log.LEVEL_STATEMENT,
3491              p_module => g_pkg || l_proc,
3492              p_msg => 'Missing conversions');
3493  END IF;
3494 
3495   FOR rate_record in MissingRate_p  LOOP
3496 
3497      IF rate_record.prim_curr_type = 'P' THEN
3498 
3499     BIS_COLLECTION_UTILITIES.writemissingrate(
3500           g_prim_rate_type,
3501           rate_record.txn_currency,
3502           g_prim_currency,
3503           rate_record.txn_date);
3504 
3505  END IF;
3506 
3507      IF rate_record.sec_curr_type = 'S' THEN
3508 
3509        BIS_COLLECTION_UTILITIES.writemissingrate(
3510           g_sec_rate_type,
3511           rate_record.txn_currency,
3512           G_sec_Currency,
3513           rate_record.txn_date);
3514 
3515      END IF;
3516 
3517   END LOOP;
3518 
3519 
3520 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
3521   bil_bi_util_collection_pkg.writeLog(
3522              p_log_level => fnd_log.LEVEL_PROCEDURE,
3523              p_module => g_pkg || l_proc || ' end',
3524              p_msg => 'End of Procedure '|| l_proc);
3525  END IF;
3526 
3527 EXCEPTION
3528 
3529 WHEN OTHERS THEN
3530     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
3531     fnd_message.set_token('ERRNO' ,SQLCODE);
3532     fnd_message.set_token('REASON' ,SQLERRM);
3533   fnd_message.set_token('ROUTINE' , l_proc);
3534     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
3535   p_module => g_pkg || l_proc || ' proc_error',
3536   p_msg => fnd_message.get,
3537   p_force_log => TRUE);
3538 
3539     raise;
3540 
3541 END Report_Missing_Rates;
3542 
3543 PROCEDURE Insert_Into_Curr_sumry(p_date IN DATE,  p_week IN NUMBER, p_period IN NUMBER,
3544 		  						 p_qtr IN NUMBER, p_year IN NUMBER, p_min_date_id IN NUMBER,
3545 								 p_max_date_id IN NUMBER) IS
3546 
3547 l_sd_lwk_end DATE;
3548 l_sd_lper_end DATE;
3549 l_sd_lqtr_end DATE;
3550 l_sd_lyr_end DATE;
3551 l_sd_lwk DATE;
3552 l_sd_lper DATE;
3553 l_sd_lqtr DATE;
3554 l_sd_lyr DATE;
3555 l_proc VARCHAR2(100);
3556 l_dynamic_sql VARCHAR2(1000);
3557 
3558 BEGIN
3559 
3560 l_proc := 'Insert_Into_Curr_sumry';
3561 
3562  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
3563     bil_bi_util_collection_pkg.writeLog(
3564              p_log_level => fnd_log.LEVEL_PROCEDURE,
3565              p_module => g_pkg || l_proc || ' begin',
3566              p_msg => 'Start of Procedure '|| l_proc);
3567     END IF;
3568 
3569 BEGIN
3570                               l_dynamic_sql := 'BEGIN :1 := fii_time_api.sd_pwk(:2); END;';
3571                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lwk, IN p_date;
3572 
3573                  SELECT week_end_date
3574                  INTO l_sd_lwk_end
3575                  from fii_time_day
3576                  where report_date=l_sd_lwk;
3577 
3578                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.ent_sd_pper_end(:2); END;';
3579                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lper, IN p_date;
3580 
3581                  SELECT LEAST(week_end_date, ent_period_end_date)
3582                  INTO l_sd_lper_end
3583                  from fii_time_day
3584                  where report_date=l_sd_lper;
3585 
3586                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.ent_sd_pqtr_end(:2); END;';
3587                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lqtr, IN p_date;
3588 
3589                  SELECT LEAST(week_end_date, ent_qtr_end_date)
3590                  INTO l_sd_lqtr_end
3591                  from fii_time_day
3592                  where report_date=l_sd_lqtr;
3593 
3594                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.ent_sd_lyr_end(:2); END;';
3595                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lyr, IN p_date;
3596 
3597                   SELECT LEAST(week_end_date, ent_year_end_date)
3598                  INTO l_sd_lyr_end
3599                  from fii_time_day
3600                  where report_date=l_sd_lyr;
3601 
3602 EXCEPTION WHEN NO_DATA_FOUND THEN
3603     NULL;
3604 END;
3605 
3606 
3607        --also insert prior amounts from bil_bi_pipeline_f
3608 --since we may not have data for the same day last week/quarter/period/year in the hist. table
3609 --we need to get the end of the week/quarter ...
3610 --if the same day last year is closer to the end of a certain week/quarter/period
3611 --than to the end of te year, get the closest date
3612 -- this can be achieved by using the bil_bi_time table:
3613 
3614 	 INSERT /*+ append parallel(f) */INTO bil_bi_pipec_f f
3615 			      (
3616 			        sales_group_id,
3617 			        salesrep_id,
3618 			        created_by ,
3619 			        creation_date,
3620 			        last_updated_by,
3621 			        last_update_date,
3622 			        last_update_login,
3623 			        request_id,
3624 			        program_application_id,
3625 			        program_id,
3626 			        program_update_date,
3627 			        snap_date,
3628 			        item_id,
3629 			        item_organization_id,
3630 			        win_probability,
3631 			        product_category_id,
3632 			        pipeline_amt_day,
3633 			        pipeline_amt_week,
3634 			        pipeline_amt_period,
3635 			        pipeline_amt_quarter,
3636 			        pipeline_amt_year,
3637 			        open_amt_day,
3638 			        open_amt_week,
3639 			        open_amt_period,
3640 			        open_amt_quarter,
3641 			        open_amt_year,
3642 			        pipeline_amt_day_s,
3643 			        pipeline_amt_week_s,
3644 			        pipeline_amt_period_s,
3645 			        pipeline_amt_quarter_s,
3646 			        pipeline_amt_year_s,
3647 			        open_amt_day_s,
3648 			        open_amt_week_s,
3649 			        open_amt_period_s,
3650 			        open_amt_quarter_s,
3651 			        open_amt_year_s,
3652 
3653                     prvprd_pipe_amt_wk  ,
3654                     prvprd_pipe_amt_PRD ,
3655                     prvprd_pipe_amt_qtr ,
3656                     prvprd_pipe_amt_yr  ,
3657                     prvprd_open_amt_wk  ,
3658                     prvprd_open_amt_PRD ,
3659                     prvprd_open_amt_qtr ,
3660                     prvprd_open_amt_yr  ,
3661                     prvprd_pipe_amt_wk_s,
3662                     prvprd_pipe_amt_PRD_s,
3663                     prvprd_pipe_amt_qtr_s,
3664                     prvprd_pipe_amt_yr_s,
3665                     prvprd_open_amt_wk_s,
3666                     prvprd_open_amt_PRD_s,
3667                     prvprd_open_amt_qtr_s,
3668                     prvprd_open_amt_yr_s,
3669 
3670                     prvyr_pipe_amt_wk   ,
3671                     prvyr_pipe_amt_PRD  ,
3672                     prvyr_pipe_amt_qtr  ,
3673                     prvyr_pipe_amt_yr   ,
3674                                         prvyr_open_amt_wk   ,
3675                     prvyr_open_amt_PRD  ,
3676                     prvyr_open_amt_qtr  ,
3677                     prvyr_open_amt_yr   ,
3678                     prvyr_pipe_amt_wk_s ,
3679                     prvyr_pipe_amt_PRD_s,
3680                     prvyr_pipe_amt_qtr_s,
3681                     prvyr_pipe_amt_yr_s ,
3682                     prvyr_open_amt_wk_s ,
3683                     prvyr_open_amt_PRD_s,
3684                     prvyr_open_amt_qtr_s,
3685                     prvyr_open_amt_yr_s
3686                       )
3687 
3688                             SELECT /*+ parallel(fc) */ sales_group_id,
3689 			        salesrep_id,
3690 		            g_user_id created_by,
3691 			        SYSDATE creation_date,
3692 			        g_user_id last_updated_by,
3693 			        SYSDATE last_update_date,
3694 			        G_Login_Id last_update_login,
3695         			G_request_id request_id,
3696         			G_appl_id program_application_id,
3697         			G_program_id program_id,
3698 			        SYSDATE program_update_date,	        snap_date,
3699 			        item_id,
3700 			        item_organization_id,
3701 			        win_probability,
3702 			        product_category_id,
3703                     SUM(pipeline_amt_day) pipeline_amt_day,
3704                     SUM(pipeline_amt_week) pipeline_amt_week,
3705                     SUM(pipeline_amt_period) pipeline_amt_period,
3706                     SUM(pipeline_amt_quarter) pipeline_amt_quarter,
3707                     SUM(pipeline_amt_year) pipeline_amt_year,
3708                     SUM(open_amt_day)  open_amt_day   ,
3709                     SUM(open_amt_week)  open_amt_week  ,
3710                     SUM(open_amt_period) open_amt_period ,
3711                     SUM(open_amt_quarter) open_amt_quarter,
3712                     SUM(open_amt_year) open_amt_year   ,
3713                     SUM(pipeline_amt_day_s) pipeline_amt_day_s,
3714                     SUM(pipeline_amt_week_s) pipeline_amt_week_s,
3715                     SUM(pipeline_amt_period_s) pipeline_amt_period_s,
3716                     SUM(pipeline_amt_quarter_s) pipeline_amt_quarter_s,
3717                     SUM(pipeline_amt_year_s) pipeline_amt_year_s,
3718                     SUM(open_amt_day_s) open_amt_day_s  ,
3719                     SUM(open_amt_week_s) open_amt_week_s ,
3720                     SUM(open_amt_period_s) open_amt_period_s,
3721                     SUM(open_amt_quarter_s) open_amt_quarter_s,
3722                     SUM(open_amt_year_s) open_amt_year_s ,
3723                                 SUM(prvprd_pipe_amt_wk)  prvprd_pipe_amt_wk,
3724                 SUM(prvprd_pipe_amt_PRD) prvprd_pipe_amt_PRD,
3725                 SUM(prvprd_pipe_amt_qtr) prvprd_pipe_amt_qtr,
3726                 SUM(prvprd_pipe_amt_yr) prvprd_pipe_amt_yr ,
3727                 SUM(prvprd_open_amt_wk) prvprd_open_amt_wk ,
3728                 SUM(prvprd_open_amt_PRD) prvprd_open_amt_PRD,
3729                 SUM(prvprd_open_amt_qtr) prvprd_open_amt_qtr,
3730                 SUM(prvprd_open_amt_yr)  prvprd_open_amt_yr,
3731                 SUM(prvprd_pipe_amt_wk_s)  prvprd_pipe_amt_wk_s,
3732                 SUM(prvprd_pipe_amt_PRD_s) prvprd_pipe_amt_PRD_s,
3733                 SUM(prvprd_pipe_amt_qtr_s) prvprd_pipe_amt_qtr_s,
3734                 SUM(prvprd_pipe_amt_yr_s) prvprd_pipe_amt_yr_s ,
3735                 SUM(prvprd_open_amt_wk_s) prvprd_open_amt_wk_s ,
3736                 SUM(prvprd_open_amt_PRD_s) prvprd_open_amt_PRD_s,
3737                 SUM(prvprd_open_amt_qtr_s) prvprd_open_amt_qtr_s,
3738                 SUM(prvprd_open_amt_yr_s) prvprd_open_amt_yr_s,
3739                 SUM(prvyr_pipe_amt_wk)  prvyr_pipe_amt_wk ,
3740                 SUM(prvyr_pipe_amt_PRD)  prvyr_pipe_amt_PRD,
3741                 SUM(prvyr_pipe_amt_qtr)  prvyr_pipe_amt_qtr,
3742                 SUM(prvyr_pipe_amt_yr) prvyr_pipe_amt_yr  ,
3743                 SUM(prvyr_open_amt_wk) prvyr_open_amt_wk   ,
3744                 SUM(prvyr_open_amt_PRD) prvyr_open_amt_PRD ,
3745                 SUM(prvyr_open_amt_qtr) prvyr_open_amt_qtr ,
3746                 SUM(prvyr_open_amt_yr) prvyr_open_amt_yr  ,
3747                 SUM(prvyr_pipe_amt_wk_s)  prvyr_pipe_amt_wk_s ,
3748                 SUM(prvyr_pipe_amt_PRD_s) prvyr_pipe_amt_PRD_s ,
3749                 SUM(prvyr_pipe_amt_qtr_s) prvyr_pipe_amt_qtr_s ,
3750                 SUM(prvyr_pipe_amt_yr_s)   prvyr_pipe_amt_yr_s,
3751                 SUM(prvyr_open_amt_wk_s) prvyr_open_amt_wk_s  ,
3752                 SUM(prvyr_open_amt_PRD_s) prvyr_open_amt_PRD_s ,
3753                 SUM(prvyr_open_amt_qtr_s) prvyr_open_amt_qtr_s ,
3754                 SUM(prvyr_open_amt_yr_s) prvyr_open_amt_yr_s
3755                   FROM (
3756 			      SELECT /*+ parallel(stg) USE_MERGE(time) */
3757 			        SALES_GROUP_ID,
3758 			        SALESREP_ID,
3759 			        p_date snap_date,
3760 			        ITEM_ID,
3761 			        ITEM_ORGANIZATION_ID,
3762 			        DECODE(stg.win_loss_indicator, 'W', 100, stg.WIN_PROBABILITY) win_probability,
3763 			        PRODUCT_CATEGORY_ID,
3764 			        SUM(CASE
3765 			            WHEN TIME.report_date = p_date
3766 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3767 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
3768 			        ) pipeline_amt_day,
3769 			        SUM(CASE
3770 			            WHEN TIME.week_id = p_week
3771 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3772 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
3773 			        ) pipeline_amt_week,
3774 			        SUM(CASE
3775 			            WHEN TIME.ent_period_id = p_period
3776 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3777 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END )pipeline_amt_period,
3778 			        SUM(CASE
3779 			            WHEN TIME.ent_qtr_id = p_qtr
3780 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3781 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) pipeline_amt_quarter,
3782 			        SUM(CASE
3783 			            WHEN TIME.ent_year_id = p_year
3784 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3785 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) pipeline_amt_year,
3786 			         SUM( CASE
3787 			            WHEN TIME.report_date = p_date  AND OPP_OPEN_STATUS_FLAG = 'Y'
3788 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3789 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END) open_amt_day,
3790 			        SUM( CASE
3791 			            WHEN TIME.week_id = p_week  AND OPP_OPEN_STATUS_FLAG = 'Y'
3792 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3793 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END) open_amt_week,
3794 			        SUM(CASE
3795 			            WHEN TIME.ent_period_id = p_period  AND OPP_OPEN_STATUS_FLAG = 'Y'
3796 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3797 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) open_amt_period,
3798 			        SUM(CASE
3799 			            WHEN TIME.ent_qtr_id = p_qtr  AND OPP_OPEN_STATUS_FLAG = 'Y'
3800 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3801 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) open_amt_quarter,
3802 			        SUM(CASE
3803 			            WHEN TIME.ent_year_id = p_year  AND OPP_OPEN_STATUS_FLAG = 'Y'
3804 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3805 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) open_amt_year,
3806 			        SUM(CASE
3807 			            WHEN TIME.report_date = p_date
3808 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3809 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
3810 			        ) pipeline_amt_day_s,
3811 			        SUM(CASE
3812 			            WHEN TIME.week_id = p_week
3813 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3814 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END
3815 			        ) pipeline_amt_week_s,
3816 			        SUM(CASE
3817 			            WHEN TIME.ent_period_id = p_period
3818 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3819 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ) pipeline_amt_period_s,
3820 			        SUM(CASE
3821 			            WHEN TIME.ent_qtr_id = p_qtr
3822 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3823 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ) pipeline_amt_quarter_s,
3824 			        SUM(CASE
3825 			            WHEN TIME.ent_year_id = p_year
3826 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3827 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END) pipeline_amt_year_s,
3828 			         SUM( CASE
3829 			            WHEN TIME.report_date = p_date  AND OPP_OPEN_STATUS_FLAG = 'Y'
3830 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3831 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END) open_amt_day_s,
3832 			        SUM( CASE
3833 			            WHEN TIME.week_id = p_week  AND OPP_OPEN_STATUS_FLAG = 'Y'
3834 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3835 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END) open_amt_week_s,
3836 			        SUM(CASE
3837 			            WHEN TIME.ent_period_id = p_period  AND OPP_OPEN_STATUS_FLAG = 'Y'
3838 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3839 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ) open_amt_period_s,
3840 			        SUM(CASE
3841 			            WHEN TIME.ent_qtr_id = p_qtr  AND OPP_OPEN_STATUS_FLAG = 'Y'
3842 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3843 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END) open_amt_quarter_s,
3844 			        SUM(CASE
3845 			            WHEN TIME.ent_year_id = p_year  AND OPP_OPEN_STATUS_FLAG = 'Y'
3846 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
3847 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ) open_amt_year_s,
3848                 null prvprd_pipe_amt_wk  ,
3849                 null prvprd_pipe_amt_PRD ,
3850                 null prvprd_pipe_amt_qtr ,
3851                 null prvprd_pipe_amt_yr  ,
3852                 null prvprd_open_amt_wk  ,
3853                 null prvprd_open_amt_PRD ,
3854                 null prvprd_open_amt_qtr ,
3855                 null prvprd_open_amt_yr  ,
3856                 null prvprd_pipe_amt_wk_s,
3857                 null prvprd_pipe_amt_PRD_s,
3858                 null prvprd_pipe_amt_qtr_s,
3859                 null prvprd_pipe_amt_yr_s,
3860                 null prvprd_open_amt_wk_s,
3861                 null prvprd_open_amt_PRD_s,
3862                 null prvprd_open_amt_qtr_s,
3863                 null prvprd_open_amt_yr_s,
3864                 null prvyr_pipe_amt_wk   ,
3865                 null prvyr_pipe_amt_PRD  ,
3866                 null prvyr_pipe_amt_qtr  ,
3867                 null prvyr_pipe_amt_yr   ,
3868                 null prvyr_open_amt_wk   ,
3869                 null prvyr_open_amt_PRD  ,
3870                 null prvyr_open_amt_qtr  ,
3871                 null prvyr_open_amt_yr   ,
3872                 null prvyr_pipe_amt_wk_s ,
3873                 null prvyr_pipe_amt_PRD_s,
3874                 null prvyr_pipe_amt_qtr_s,
3875                 null prvyr_pipe_amt_yr_s ,
3876                 null prvyr_open_amt_wk_s ,
3877                 null prvyr_open_amt_PRD_s,
3878                 null prvyr_open_amt_qtr_s,
3879                 null prvyr_open_amt_yr_s
3880 
3881                   FROM
3882 			        bil_bi_pipeline_stg stg,
3883 			        fii_time_day time
3884 			      WHERE stg.effective_date =  TIME.report_date
3885 			        AND forecast_rollup_flag = 'Y'
3886 			        AND TIME.report_date_julian >= p_min_date_id AND TIME.report_date_julian+0 <= p_max_date_id
3887 			      GROUP BY
3888 			        sales_group_id,
3889 			        salesrep_id,
3890 			        item_id,
3891 			        item_organization_id,
3892 			        DECODE(stg.win_loss_indicator, 'W', 100, stg.WIN_PROBABILITY),
3893 			        product_category_id
3894 			      HAVING
3895 			        SUM(CASE
3896 			            WHEN TIME.week_id = p_week
3897 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3898 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
3899 			        )IS NOT NULL OR
3900 			        SUM(CASE
3901 			            WHEN TIME.ent_year_id = p_year
3902 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
3903 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END )
3904 			         IS NOT NULL
3905      UNION ALL
3906                       SELECT /*+ parallel(f) */ sales_group_id,
3907 			        salesrep_id,
3908 			        p_date snap_date,
3909 			        item_id,
3910 			        item_organization_id,
3911 			        win_probability,
3912 			        product_category_id,
3913                     null pipeline_amt_day ,
3914                     null pipeline_amt_week,
3915                     null pipeline_amt_period,
3916                     null pipeline_amt_quarter,
3917                     null pipeline_amt_year,
3918                     null open_amt_day     ,
3919                     null open_amt_week    ,
3920                     null open_amt_period  ,
3921                     null open_amt_quarter ,
3922                     null open_amt_year    ,
3923                     null pipeline_amt_day_s ,
3924                     null pipeline_amt_week_s,
3925                     null pipeline_amt_period_s ,
3926                     null pipeline_amt_quarter_s,
3927                     null pipeline_amt_year_s,
3928                     null open_amt_day_s   ,
3929                     null open_amt_week_s  ,
3930                     null open_amt_period_s,
3931                     null open_amt_quarter_s,
3932                     null open_amt_year_s,
3933 				decode(f.snap_date, l_sd_lwk_end, pipeline_amt_week, null) prvprd_pipe_amt_wk  ,
3934                 decode(f.snap_date, l_sd_lper_end, pipeline_amt_period, null) prvprd_pipe_amt_PRD ,
3935                 decode(f.snap_date, l_sd_lqtr_end, pipeline_amt_quarter, null) prvprd_pipe_amt_qtr ,
3936                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year, null) prvprd_pipe_amt_yr  ,
3937                 decode(f.snap_date, l_sd_lwk_end, open_amt_week, null) prvprd_open_amt_wk  ,
3938                 decode(f.snap_date, l_sd_lper_end, open_amt_period, null) prvprd_open_amt_PRD ,
3939                 decode(f.snap_date, l_sd_lqtr_end, open_amt_quarter, null) prvprd_open_amt_qtr ,
3940                 decode(f.snap_date, l_sd_lyr_end, open_amt_year, null) prvprd_open_amt_yr  ,
3941                 decode(f.snap_date, l_sd_lwk_end, pipeline_amt_week_s, null) prvprd_pipe_amt_wk_s  ,
3942                 decode(f.snap_date, l_sd_lper_end, pipeline_amt_period_s, null) prvprd_pipe_amt_PRD_s ,
3943                 decode(f.snap_date, l_sd_lqtr_end, pipeline_amt_quarter_s, null) prvprd_pipe_amt_qtr_s ,
3944                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year_s, null) prvprd_pipe_amt_yr_s  ,
3945                 decode(f.snap_date, l_sd_lwk_end, open_amt_week_s, null) prvprd_open_amt_wk_s  ,
3946                 decode(f.snap_date, l_sd_lper_end, open_amt_period_s, null) prvprd_open_amt_PRD_s ,
3947                 decode(f.snap_date, l_sd_lqtr_end, open_amt_quarter_s, null) prvprd_open_amt_qtr_s ,
3948                 decode(f.snap_date, l_sd_lyr_end, open_amt_year_s, null) prvprd_open_amt_yr_s,
3949                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_week, null) prvyr_pipe_amt_wk   ,
3950                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_period, null) prvyr_pipe_amt_PRD  ,
3951                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_quarter, null) prvyr_pipe_amt_qtr  ,
3952                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year, null) prvyr_pipe_amt_yr   ,
3953                 decode(f.snap_date, l_sd_lyr_end, open_amt_week, null) prvyr_open_amt_wk   ,
3954                 decode(f.snap_date, l_sd_lyr_end, open_amt_period, null) prvyr_open_amt_PRD  ,
3955                 decode(f.snap_date, l_sd_lyr_end, open_amt_quarter, null) prvyr_open_amt_qtr  ,
3956                 decode(f.snap_date, l_sd_lyr_end, open_amt_year, null) prvyr_open_amt_yr   ,
3957                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_week_s, null) prvyr_pipe_amt_wk_s   ,
3958                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_period_s, null) prvyr_pipe_amt_PRD_s  ,
3959                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_quarter_s, null) prvyr_pipe_amt_qtr_s  ,
3960                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year_s, null) prvyr_pipe_amt_yr_s   ,
3961                 decode(f.snap_date, l_sd_lyr_end, open_amt_week_s, null) prvyr_open_amt_wk_s   ,
3962                 decode(f.snap_date, l_sd_lyr_end, open_amt_period, null) prvyr_open_amt_PRD_s  ,
3963                 decode(f.snap_date, l_sd_lyr_end, open_amt_quarter, null) prvyr_open_amt_qtr_s  ,
3964                 decode(f.snap_date, l_sd_lyr_end, open_amt_year, null) prvyr_open_amt_yr_s
3965                     FROM BIL_BI_PIPELINE_F f
3966                     where snap_date in (l_sd_lwk_end, l_sd_lper_end
3967                     ,l_sd_lqtr_end, l_sd_lyr_end)
3968 					UNION ALL
3969                       SELECT  sales_group_id,
3970 			        salesrep_id,
3971 			        p_date snap_date,
3972 			        item_id,
3973 			        item_organization_id,
3974 			        win_probability,
3975 			        product_category_id,
3976                     null pipeline_amt_day ,
3977                     null pipeline_amt_week,
3978                     null pipeline_amt_period,
3979                     null pipeline_amt_quarter,
3980                     null pipeline_amt_year,
3981                     null open_amt_day     ,
3982                     null open_amt_week    ,
3983                     null open_amt_period  ,
3984                     null open_amt_quarter ,
3985                     null open_amt_year    ,
3986                     null pipeline_amt_day_s ,
3987                     null pipeline_amt_week_s,
3988                     null pipeline_amt_period_s ,
3989                     null pipeline_amt_quarter_s,
3990                     null pipeline_amt_year_s,
3991                     null open_amt_day_s   ,
3992                     null open_amt_week_s  ,
3993                     null open_amt_period_s,
3994                     null open_amt_quarter_s,
3995                     null open_amt_year_s  ,
3996                 pipeline_amt_week prvprd_pipe_amt_wk  ,
3997                 null prvprd_pipe_amt_PRD ,
3998                 null prvprd_pipe_amt_qtr ,
3999                 null prvprd_pipe_amt_yr  ,
4000                 open_amt_week prvprd_open_amt_wk  ,
4001                 null prvprd_open_amt_PRD ,
4002                 null prvprd_open_amt_qtr ,
4003                 null prvprd_open_amt_yr  ,
4004                 pipeline_amt_week_s prvprd_pipe_amt_wk_s  ,
4005                 null prvprd_pipe_amt_PRD_s ,
4006                 null prvprd_pipe_amt_qtr_s ,
4007                 null prvprd_pipe_amt_yr_s  ,
4008                 open_amt_week_s prvprd_open_amt_wk_s  ,
4009                 null prvprd_open_amt_PRD_s ,
4010                 null prvprd_open_amt_qtr_s ,
4011                 null prvprd_open_amt_yr_s,
4012                 null prvyr_pipe_amt_wk   ,
4013                 null prvyr_pipe_amt_PRD  ,
4014                 null prvyr_pipe_amt_qtr  ,
4015                 null prvyr_pipe_amt_yr   ,
4016                 null prvyr_open_amt_wk   ,
4017                 null prvyr_open_amt_PRD  ,
4018                 null prvyr_open_amt_qtr  ,
4019                 null prvyr_open_amt_yr   ,
4020                 null prvyr_pipe_amt_wk_s   ,
4021                 null prvyr_pipe_amt_PRD_s  ,
4022                 null prvyr_pipe_amt_qtr_s  ,
4023                 null prvyr_pipe_amt_yr_s   ,
4024                 null prvyr_open_amt_wk_s   ,
4025                 null prvyr_open_amt_PRD_s  ,
4026                 null prvyr_open_amt_qtr_s  ,
4027                 null prvyr_open_amt_yr_s
4028                     FROM BIL_BI_PIPEC_F f
4029                     where snap_date = l_sd_lwk
4030                      )
4031                     GROUP BY
4032                        sales_group_id,
4033 			        salesrep_id,
4034 			        snap_date,
4035 			        item_id,
4036 			        item_organization_id,
4037 			        win_probability,
4038 			        product_category_id
4039                     ;
4040                commit;
4041 
4042 	  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
4043   bil_bi_util_collection_pkg.writeLog(
4044              p_log_level => fnd_log.LEVEL_PROCEDURE,
4045              p_module => g_pkg || l_proc || ' end',
4046              p_msg => 'End of Procedure '|| l_proc);
4047  END IF;
4048 
4049  EXCEPTION
4050 
4051 WHEN OTHERS THEN
4052     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
4053     fnd_message.set_token('ERRNO' ,SQLCODE);
4054     fnd_message.set_token('REASON' ,SQLERRM);
4055   fnd_message.set_token('ROUTINE' , l_proc);
4056     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
4057   p_module => g_pkg || l_proc || ' proc_error',
4058   p_msg => fnd_message.get,
4059   p_force_log => TRUE);
4060 
4061     raise;
4062 
4063 
4064 END Insert_Into_curr_sumry;
4065 
4066 
4067 PROCEDURE Insert_Into_Summary (p_mode IN varchar2) IS
4068  l_proc VARCHAR2(100);
4069 BEGIN
4070 
4071  l_proc:= 'Insert_Into_Summary';
4072 
4073 
4074 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
4075   bil_bi_util_collection_pkg.writeLog(
4076              p_log_level => fnd_log.LEVEL_PROCEDURE,
4077              p_module => g_pkg || l_proc || ' begin',
4078              p_msg => 'Start of Procedure '|| l_proc);
4079  END IF;
4080 
4081 
4082 
4083 IF(p_mode = 'INIT') THEN
4084  INSERT /*+ append parallel(f) */ into bil_bi_pipeline_f f(
4085    SALES_GROUP_ID,
4086    SALESREP_ID,
4087    CREATED_BY ,
4088    CREATION_DATE,
4089    LAST_UPDATED_BY,
4090    LAST_UPDATE_DATE,
4091    LAST_UPDATE_LOGIN,
4092    REQUEST_ID,
4093    PROGRAM_APPLICATION_ID,
4094    PROGRAM_ID,
4095    PROGRAM_UPDATE_DATE,
4096    SNAP_DATE,
4097    ITEM_ID,
4098    ITEM_ORGANIZATION_ID,
4099    WIN_PROBABILITY,
4100    PRODUCT_CATEGORY_ID,
4101    PIPELINE_AMT_DAY,
4102    PIPELINE_AMT_WEEK,
4103    PIPELINE_AMT_PERIOD,
4104    PIPELINE_AMT_Quarter,
4105    PIPELINE_AMT_YEAR,
4106    OPEN_AMT_DAY,
4107    OPEN_AMT_WEEK,
4108    OPEN_AMT_PERIOD,
4109    OPEN_AMT_Quarter,
4110    OPEN_AMT_YEAR,
4111    PIPELINE_AMT_DAY_S,
4112    PIPELINE_AMT_WEEK_S,
4113    PIPELINE_AMT_PERIOD_S,
4114    PIPELINE_AMT_Quarter_S,
4115    PIPELINE_AMT_YEAR_S,
4116    OPEN_AMT_DAY_S,
4117    OPEN_AMT_WEEK_S,
4118    OPEN_AMT_PERIOD_S,
4119    OPEN_AMT_Quarter_S,
4120    OPEN_AMT_YEAR_S
4121  )
4122  SELECT /*+ parallel(stg) use_merge(time1) */
4123    SALES_GROUP_ID,
4124    SALESREP_ID,
4125    g_user_id,
4126    sysdate,
4127    g_user_id,
4128    sysdate,
4129    G_Login_Id,
4130    G_request_id,
4131    G_appl_id,
4132    G_program_id,
4133    sysdate,
4134    stg.snap_date,
4135    ITEM_ID,
4136    ITEM_ORGANIZATION_ID,
4137    WIN_PROBABILITY,
4138    PRODUCT_CATEGORY_ID,
4139    SUM(CASE
4140        WHEN time.report_date = time1.report_date
4141        THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4142          stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4143    SUM(CASE
4144         WHEN time.week_id = time1.week_id
4145         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4146           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4147    SUM(CASE
4148         WHEN time.ent_period_id = time1.ent_period_id
4149         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4150           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
4151    SUM(CASE
4152         WHEN time.ent_qtr_id = time1.ent_qtr_id
4153         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4154           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4155    SUM(CASE
4156         WHEN time.ent_year_id =time1.ent_year_id
4157         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4158           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
4159    SUM( CASE
4160         WHEN time.report_date =time1.report_date  and opp_open_status_flag = 'Y'
4161         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4162           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4163    SUM( CASE
4164         WHEN time.week_id =  time1.week_id  and opp_open_status_flag = 'Y'
4165         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4166           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4167    SUM(CASE
4168         WHEN time.ent_period_id = time1.ent_period_id   and opp_open_status_flag = 'Y'
4169         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4170           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4171    SUM(CASE
4172         WHEN time.ent_qtr_id = time1.ent_qtr_id  and opp_open_status_flag = 'Y'
4173         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4174           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
4175    SUM(CASE
4176         WHEN time.ent_year_id = time1.ent_year_id  and opp_open_status_flag = 'Y'
4177         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4178           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4179    SUM(CASE
4180        WHEN time.report_date = time1.report_date
4181        THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4182          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4183    SUM(CASE
4184         WHEN time.week_id = time1.week_id
4185         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4186           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4187    SUM(CASE
4188         WHEN time.ent_period_id = time1.ent_period_id
4189         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4190           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
4191    SUM(CASE
4192         WHEN time.ent_qtr_id = time1.ent_qtr_id
4193         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4194           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4195    SUM(CASE
4196         WHEN time.ent_year_id =time1.ent_year_id
4197         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4198           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
4199    SUM( CASE
4200         WHEN time.report_date =time1.report_date  and opp_open_status_flag = 'Y'
4201         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4202           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4203    SUM( CASE
4204         WHEN time.week_id =  time1.week_id  and opp_open_status_flag = 'Y'
4205         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4206           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4207    SUM(CASE
4208         WHEN time.ent_period_id = time1.ent_period_id   and opp_open_status_flag = 'Y'
4209         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4210           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4211    SUM(CASE
4212         WHEN time.ent_qtr_id = time1.ent_qtr_id  and opp_open_status_flag = 'Y'
4213         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4214           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
4215    SUM(CASE
4216         WHEN time.ent_year_id = time1.ent_year_id and opp_open_status_flag = 'Y'
4217         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4218           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END)
4219  FROM
4220    bil_bi_pipeline_stg stg,
4221    fii_time_day time,
4222    fii_time_day time1
4223  WHERE
4224    stg.snap_date = time1.report_date
4225    and stg.PRIM_CONVERSION_RATE > 0
4226    and stg.conversion_rate_s>0
4227    and stg.product_category_id is not null
4228    and stg.effective_date =  time.report_date
4229    and stg.effective_date <= GREATEST(time.ent_year_end_date,time.week_end_date)
4230  GROUP BY
4231    SALES_GROUP_ID,
4232    SALESREP_ID,
4233    ITEM_ID,
4234    ITEM_ORGANIZATION_ID,
4235    WIN_PROBABILITY,
4236    PRODUCT_CATEGORY_ID,
4237    stg.snap_date
4238  HAVING
4239   SUM(CASE
4240       WHEN time.week_id = time1.week_id
4241       THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL, stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
4242   ) is not null or
4243   SUM(CASE
4244       WHEN time.ent_year_id =time1.ent_year_id
4245       THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL, stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END )
4246       is not null ;
4247 
4248   ELSE
4249   INSERT into bil_bi_pipeline_f f(
4250    SALES_GROUP_ID,
4251    SALESREP_ID,
4252    CREATED_BY ,
4253    CREATION_DATE,
4254    LAST_UPDATED_BY,
4255    LAST_UPDATE_DATE,
4256    LAST_UPDATE_LOGIN,
4257    REQUEST_ID,
4258    PROGRAM_APPLICATION_ID,
4259    PROGRAM_ID,
4260    PROGRAM_UPDATE_DATE,
4261    SNAP_DATE,
4262    ITEM_ID,
4263    ITEM_ORGANIZATION_ID,
4264    WIN_PROBABILITY,
4265    PRODUCT_CATEGORY_ID,
4266    PIPELINE_AMT_DAY,
4267    PIPELINE_AMT_WEEK,
4268    PIPELINE_AMT_PERIOD,
4269    PIPELINE_AMT_Quarter,
4270    PIPELINE_AMT_YEAR,
4271    OPEN_AMT_DAY,
4272    OPEN_AMT_WEEK,
4273    OPEN_AMT_PERIOD,
4274    OPEN_AMT_Quarter,
4275    OPEN_AMT_YEAR,
4276    PIPELINE_AMT_DAY_S,
4277    PIPELINE_AMT_WEEK_S,
4278    PIPELINE_AMT_PERIOD_S,
4279    PIPELINE_AMT_Quarter_S,
4280    PIPELINE_AMT_YEAR_S,
4281    OPEN_AMT_DAY_S,
4282    OPEN_AMT_WEEK_S,
4283    OPEN_AMT_PERIOD_S,
4284    OPEN_AMT_Quarter_S,
4285    OPEN_AMT_YEAR_S
4286  )
4287  SELECT
4288    SALES_GROUP_ID,
4289    SALESREP_ID,
4290    g_user_id,
4291    sysdate,
4292    g_user_id,
4293    sysdate,
4294    G_Login_Id,
4295    G_request_id,
4296    G_appl_id,
4297    G_program_id,
4298    sysdate,
4299    stg.snap_date,
4300    ITEM_ID,
4301    ITEM_ORGANIZATION_ID,
4302    WIN_PROBABILITY,
4303    PRODUCT_CATEGORY_ID,
4304    SUM(CASE
4305        WHEN time.report_date = time1.report_date
4306        THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4307          stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4308    SUM(CASE
4309         WHEN time.week_id = time1.week_id
4310         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4311           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4312    SUM(CASE
4313         WHEN time.ent_period_id = time1.ent_period_id
4314         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4315           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
4316    SUM(CASE
4317         WHEN time.ent_qtr_id = time1.ent_qtr_id
4318         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4319           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4320    SUM(CASE
4321         WHEN time.ent_year_id =time1.ent_year_id
4322         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4323           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
4324    SUM( CASE
4325         WHEN time.report_date =time1.report_date  and opp_open_status_flag = 'Y'
4326         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4327           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4328    SUM( CASE
4329         WHEN time.week_id =  time1.week_id  and opp_open_status_flag = 'Y'
4330         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4331           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4332    SUM(CASE
4333         WHEN time.ent_period_id = time1.ent_period_id   and opp_open_status_flag = 'Y'
4334         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4335           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4336    SUM(CASE
4337         WHEN time.ent_qtr_id = time1.ent_qtr_id  and opp_open_status_flag = 'Y'
4338         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4339           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
4340    SUM(CASE
4341         WHEN time.ent_year_id = time1.ent_year_id  and opp_open_status_flag = 'Y'
4342         THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
4343           stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
4344    SUM(CASE
4345        WHEN time.report_date = time1.report_date
4346        THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4347          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4348    SUM(CASE
4349         WHEN time.week_id = time1.week_id
4350         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4351           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4352    SUM(CASE
4353         WHEN time.ent_period_id = time1.ent_period_id
4354         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4355           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
4356    SUM(CASE
4357         WHEN time.ent_qtr_id = time1.ent_qtr_id
4358         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4359           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4360    SUM(CASE
4361         WHEN time.ent_year_id =time1.ent_year_id
4362         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4363           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
4364    SUM( CASE
4365         WHEN time.report_date =time1.report_date  and opp_open_status_flag = 'Y'
4366         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4367           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4368    SUM( CASE
4369         WHEN time.week_id =  time1.week_id  and opp_open_status_flag = 'Y'
4370         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4371           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4372    SUM(CASE
4373         WHEN time.ent_period_id = time1.ent_period_id   and opp_open_status_flag = 'Y'
4374         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4375           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
4376    SUM(CASE
4377         WHEN time.ent_qtr_id = time1.ent_qtr_id  and opp_open_status_flag = 'Y'
4378         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4379           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
4380    SUM(CASE
4381         WHEN time.ent_year_id = time1.ent_year_id and opp_open_status_flag = 'Y'
4382         THEN decode(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
4383           stg.sales_credit_amount*conversion_rate_s) ELSE NULL END)
4384  FROM
4385    bil_bi_pipeline_stg stg,
4386    fii_time_day time,
4387    fii_time_day time1
4388  WHERE
4389    stg.snap_date = time1.report_date
4390    and stg.PRIM_CONVERSION_RATE > 0
4391    and stg.conversion_rate_s>0
4392    and stg.product_category_id is not null
4393    and stg.effective_date =  time.report_date
4394  GROUP BY
4395    SALES_GROUP_ID,
4396    SALESREP_ID,
4397    ITEM_ID,
4398    ITEM_ORGANIZATION_ID,
4399    WIN_PROBABILITY,
4400    PRODUCT_CATEGORY_ID,
4401    stg.snap_date
4402  HAVING
4403   SUM(CASE
4404       WHEN time.week_id = time1.week_id
4405       THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL, stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
4406   ) is not null or
4407   SUM(CASE
4408       WHEN time.ent_year_id =time1.ent_year_id
4409       THEN decode(stg.sales_credit_amount*prim_conversion_rate, 0, NULL, stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END )
4410       is not null ;
4411 
4412   END IF;
4413 
4414 
4415     g_row_num:= sql%rowcount;
4416     commit;
4417 
4418    IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
4419         bil_bi_util_collection_pkg.writeLog(
4420                 p_log_level => fnd_log.LEVEL_EVENT,
4421     p_module => g_pkg || l_proc ,
4422     p_msg => 'Inserted  '|| g_row_num||' into BIL_BI_PIPELINE_F table from BIL_BI_PIPELINE_STG ');
4423    END IF;
4424 
4425 
4426 
4427 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
4428   bil_bi_util_collection_pkg.writeLog(
4429              p_log_level => fnd_log.LEVEL_PROCEDURE,
4430              p_module => g_pkg || l_proc || ' end',
4431              p_msg => 'End of Procedure '|| l_proc);
4432  END IF;
4433 
4434 EXCEPTION
4435 
4436 WHEN OTHERS THEN
4437     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
4438     fnd_message.set_token('ERRNO' ,SQLCODE);
4439     fnd_message.set_token('REASON' ,SQLERRM);
4440   fnd_message.set_token('ROUTINE' , l_proc);
4441     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
4442   p_module => g_pkg || l_proc || ' proc_error',
4443   p_msg => fnd_message.get,
4444   p_force_log => TRUE);
4445 
4446     raise;
4447 
4448 
4449 END Insert_Into_Summary;
4450 
4451 
4452 
4453 PROCEDURE Build_BaseLine(p_start_date IN DATE) IS
4454 l_proc VARCHAR2(100);
4455 l_max_decision_end DATE;
4456 l_report_date DATE;
4457 l_report_start DATE;
4458 l_report_end DATE;
4459 l_limit_date DATE; --added by annsrini --fix for bug 5953589
4460 
4461 
4462 BEGIN
4463 
4464 l_proc := 'Build_BaseLine';
4465 l_limit_date := add_months(trunc(g_program_start),24); --added by annsrini --fix for bug 5953589
4466 
4467 
4468 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
4469   bil_bi_util_collection_pkg.writeLog(
4470              p_log_level => fnd_log.LEVEL_PROCEDURE,
4471              p_module => g_pkg || l_proc || ' begin',
4472              p_msg => 'Start of Procedure '|| l_proc);
4473  END IF;
4474 
4475 
4476 
4477    IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_STATEMENT) THEN
4478   bil_bi_util_collection_pkg.writeLog(
4479              p_log_level => fnd_log.LEVEL_STATEMENT,
4480              p_module => g_pkg || l_proc ,
4481              p_msg => 'p_start_date is '|| p_start_date);
4482 
4483 	 END IF;
4484 
4485 
4486 select greatest(week_end_date, ent_year_end_date)
4487 into l_max_decision_end
4488 from fii_time_day
4489 where end_date = p_start_date;
4490    --- base line query , note  can't just produce baseline for x and x year, but for x on forward
4491 
4492 SELECT report_date,
4493 LEAST(ent_year_start_date, week_start_date) start_date,
4494 GREATEST(ent_year_end_date,week_end_date) end_date
4495 into l_report_date, l_report_start, l_report_end
4496 FROM
4497 fii_time_day
4498 WHERE report_date = trunc(p_start_date);
4499 
4500 
4501 
4502     INSERT /*+ APPEND PARALLEL(stg) */  INTO bil_bi_pipeline_stg stg(
4503 	line_id,
4504 	credit_id,
4505 	SALES_GROUP_ID,
4506    	SALESREP_ID,
4507 	LEAD_ID,
4508 	WIN_PROBABILITY,
4509 	TXN_CURRENCY,
4510 	WIN_LOSS_INDICATOR,
4511 	FORECAST_ROLLUP_FLAG,
4512 	ITEM_ID,
4513 	ITEM_ORGANIZATION_ID,
4514 	LEAD_NUMBER,
4515 	PRODUCT_CATEGORY_ID,
4516 	OPP_OPEN_STATUS_FLAG,
4517 	SALES_CREDIT_AMOUNT,
4518 	SNAP_DATE,
4519 	EFFECTIVE_DATE
4520    )
4521    SELECT  /*+ parallel(linelog) parallel(creditlog) parallel(leads)
4522            pq_distribute(leads,hash,hash) pq_distribute(linelog,hash,hash) pq_distribute(creditlog,hash,hash)
4523            full(linelog) full(creditlog) full(leads) */
4524   linelog.lead_line_id,
4525   creditlog.sales_credit_id,
4526  creditlog.salesgroup_id
4527  ,creditlog.salesforce_id
4528  ,maxlog.lead_id
4529  ,decode(maxlog.win_loss_indicator,'W', 100,  maxlog.WIN_PROBABILITY)
4530  ,maxlog.currency_code
4531  ,maxlog.win_loss_indicator  win_loss_indicator
4532  ,maxlog.FORECAST_ROLLUP_FLAG forecast_rollup_flag
4533         ,nvl(linelog.inventory_item_id, -1)
4534         , decode(linelog.Inventory_item_id, null, -99,
4535 nvl(linelog.organization_id, -99))
4536         ,leads.lead_number
4537         , linelog.product_category_id
4538         , maxlog.opp_open_status_flag open_status_flag
4539         , sum(creditlog.credit_amount)
4540         , maxlog.report_date
4541         , nvl(linelog.forecast_date, maxlog.decision_date)
4542    FROM  as_leads_all leads
4543        , as_lead_lines_log linelog
4544        , as_sales_credits_log creditlog
4545        , (-- alias maxlog
4546  SELECT  /*+ parallel(linelog1) parallel(creditlog1) full(linelog1) full(creditlog1)
4547                pq_distribute(creditlog1,hash,hash) pq_distribute(linelog1,hash,hash) no_merge */
4548              leadlog2.lead_id     lead_id
4549            , MAX(leadlog2.max_id)      lead_log_id
4550            , linelog1.lead_line_id    lead_line_id
4551            , MAX(linelog1.log_id) lead_line_log_id
4552            , creditlog1.sales_credit_id sales_credit_id
4553            , MAX(creditlog1.log_id) sales_credit_log_id
4554            , leadlog2.report_date report_date
4555            , leadlog2.currency_code
4556            , leadlog2.FORECAST_ROLLUP_FLAG
4557            , leadlog2.win_loss_indicator
4558            , leadlog2.opp_open_status_flag
4559            , leadlog2.win_probability
4560            , leadlog2.decision_date
4561           FROM
4562              as_sales_credits_log creditlog1,
4563              as_lead_lines_log linelog1,
4564             ( -- alias leadlog2
4565              SELECT /*+ full(llog) parallel(llog) pq_distribute(STATUS1,none,broadcast) swap_join_inputs(STATUS1) no_merge */
4566 			  maxlead.report_date, maxlead.start_date,maxlead.lead_id, maxlead.max_id,
4567               llog.decision_date, llog.win_probability, status1.FORECAST_ROLLUP_FLAG,
4568               status1.win_loss_indicator, status1.opp_open_status_flag,
4569               llog.currency_code
4570               FROM
4571               ( -- alias maxlead
4572                 SELECT /*+ parallel(leadlog1) full(leadlog1) NO_MERGE */
4573 				--gapdays.report_date,
4574 				--gapdays.start_date,
4575 				--gapdays.end_date
4576 				l_report_date report_date,
4577 				l_report_start start_date,
4578 				l_report_end end_date
4579 				,leadlog1.lead_id, max(leadlog1.log_id) max_id
4580                  FROM as_leads_log leadlog1
4581                     -- ( -- alias gapdays
4582                     --  SELECT report_date,
4583                      --        LEAST(ent_year_start_date, week_start_date) start_date,
4584                       --       GREATEST(ent_year_end_date,week_end_date) end_date
4585                       --   FROM
4586                       --        fii_time_day
4587                       --   WHERE report_date = trunc(p_start_date)
4588                      --) gapdays
4589                 WHERE leadlog1.last_update_date < l_report_date+1
4590 		    GROUP BY lead_id, --gapdays.report_date, gapdays.start_date, gapdays.end_date
4591 				l_report_date, l_report_start, l_report_end
4592               ) maxlead,
4593               as_leads_log llog,
4594               as_statuses_b status1
4595               WHERE maxlead.max_id = llog.log_id
4596                 and status1.status_Code = llog.status_Code
4597                 and llog.decision_date >= g_global_start_date
4598                 AND llog.decision_date >= maxlead.start_date
4599 		    AND llog.decision_date <= l_limit_date --added by annsrini --fix for bug 5953589
4600                 AND status1.FORECAST_ROLLUP_FLAG = 'Y'
4601             ) leadlog2
4602         WHERE linelog1.lead_id=leadlog2.lead_id
4603           AND creditlog1.lead_line_id=linelog1.lead_line_id
4604           AND creditlog1.lead_id = leadlog2.lead_id
4605           AND linelog1.lead_id = creditlog1.lead_id
4606           AND linelog1.last_update_date < leadlog2.report_date+1
4607           AND creditlog1.last_update_date < leadlog2.report_date+1
4608           AND nvl(linelog1.forecast_date,leadlog2.decision_date) >= G_Global_Start_Date  --addedby annsrini --fix for bug 5953589
4609           AND nvl(linelog1.forecast_date,leadlog2.decision_date) >= leadlog2.start_date
4610 	    AND nvl(linelog1.forecast_date,leadlog2.decision_date) <= l_limit_date
4611           AND ((creditlog1.log_mode in ('U', 'I')
4612                AND creditlog1.salesgroup_id IS NOT NULL
4613                AND creditlog1.CREDIT_TYPE_ID = g_credit_type_id)
4614                OR
4615                (creditlog1.log_mode='D'))
4616          GROUP BY
4617            creditlog1.sales_credit_id
4618          , leadlog2.lead_id
4619          , linelog1.lead_line_id
4620          , leadlog2.report_date
4621          , leadlog2.currency_code
4622            , leadlog2.FORECAST_ROLLUP_FLAG
4623            , leadlog2.win_loss_indicator
4624            , leadlog2.opp_open_status_flag
4625            , leadlog2.win_probability
4626            , leadlog2.decision_date
4627           ) maxlog
4628    WHERE maxlog.lead_line_log_id = linelog.log_id
4629      AND maxlog.sales_credit_log_id = creditlog.log_id
4630      AND creditlog.salesgroup_id is not null
4631      AND leads.lead_id = maxlog.lead_id
4632 	 AND NVL(linelog.forecast_date, maxlog.decision_date) <= l_max_decision_end
4633    GROUP BY linelog.lead_line_id
4634            ,creditlog.sales_credit_id
4635 	   ,creditlog.salesgroup_id
4636            ,creditlog.salesforce_id
4637            ,maxlog.lead_id
4638            ,maxlog.WIN_PROBABILITY
4639            ,maxlog.currency_code
4640            ,maxlog.win_loss_indicator
4641            ,maxlog.FORECAST_ROLLUP_FLAG
4642            ,nvl(linelog.inventory_item_id, -1)
4643            ,decode(linelog.Inventory_item_id, null, -99, nvl(linelog.organization_id, -99))
4644            ,leads.lead_number
4645            ,linelog.product_category_id
4646            ,maxlog.opp_open_status_flag
4647            ,maxlog.report_date
4648            ,NVL(linelog.forecast_date, maxlog.decision_date);
4649 
4650 commit;
4651 
4652 
4653 
4654 
4655 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
4656   bil_bi_util_collection_pkg.writeLog(
4657              p_log_level => fnd_log.LEVEL_PROCEDURE,
4658              p_module => g_pkg || l_proc || ' end',
4659              p_msg => 'End of Procedure '|| l_proc);
4660  END IF;
4661 
4662 
4663 EXCEPTION
4664 
4665 WHEN NO_DATA_FOUND THEN
4666 NULL;
4667 
4668 WHEN OTHERS THEN
4669     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
4670     fnd_message.set_token('ERRNO' ,SQLCODE);
4671     fnd_message.set_token('REASON' ,SQLERRM);
4672   fnd_message.set_token('ROUTINE' , l_proc);
4673     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
4674   p_module => g_pkg || l_proc || ' proc_error',
4675   p_msg => fnd_message.get,
4676   p_force_log => TRUE);
4677 
4678     raise;
4679 
4680 END Build_BaseLine;
4681 
4682 PROCEDURE Build_Denlog(p_start_date IN DATE, p_end_Date IN DATE, p_coll_start IN DATE, p_mode IN varchar2) IS
4683 l_proc VARCHAR2(100);
4684 l_min_decision_start DATE;
4685 l_max_decision_end DATE;
4686 l_rank NUMBER;
4687 l_limit_date  DATE ; --added by annsrini --fix for bug 5953589
4688 
4689 BEGIN
4690 
4691 l_proc := 'Build_Denlog';
4692 l_rank := 1;
4693 l_limit_date := add_months(trunc(g_program_start),24); --added by annsrini --fix for bug 5953589
4694 
4695 
4696 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
4697   bil_bi_util_collection_pkg.writeLog(
4698              p_log_level => fnd_log.LEVEL_PROCEDURE,
4699              p_module => g_pkg || l_proc || ' begin',
4700              p_msg => 'Start of Procedure '|| l_proc);
4701  END IF;
4702 
4703 BEGIN
4704 select least(week_start_date, ent_year_start_date)
4705 into l_min_decision_start
4706 from fii_time_day
4707 where start_date = p_start_date;
4708 
4709 select greatest(week_end_date, ent_year_end_date)
4710 into l_max_decision_end
4711 from fii_time_day
4712 where end_date = p_end_date;
4713 
4714 EXCEPTION
4715 WHEN OTHERS THEN
4716     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
4717     fnd_message.set_token('ERRNO' ,SQLCODE);
4718     fnd_message.set_token('REASON' ,SQLERRM);
4719   fnd_message.set_token('ROUTINE' , l_proc);
4720     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
4721   p_module => g_pkg || l_proc || ' ERROR while getting the dates',
4722   p_msg => fnd_message.get,
4723   p_force_log => TRUE);
4724 
4725     raise;
4726 END;
4727 
4728 IF (p_mode = 'INIT') THEN
4729 
4730 
4731 BEGIN
4732 
4733 INSERT /*+ append parallel(tmp) */ INTO bil_bi_opdtl_denlog_tmp tmp
4734 (lead_id,
4735 lead_log_id,
4736 lead_line_id,
4737 lead_line_log_id,
4738 sales_credit_id,
4739 sales_credit_log_id,
4740 last_update_date,
4741 rev_flag)
4742 SELECT lead_id,
4743 					 lead_log_id,
4744 					 lead_line_id,
4745 					 lead_line_log_id,
4746 					 sales_credit_id,
4747 					 sales_credit_log_id,
4748 					 last_update_date,
4749 					 rev_flag FROM
4750 (
4751 
4752 SELECT   /*+  leading(linelog) use_hash(creditlog leadlog) parallel(creditlog) parallel(linelog) */
4753   leadlog.lead_id     lead_id
4754 , leadlog.log_id      lead_log_id
4755 , linelog.lead_line_id    lead_line_id
4756 , MAX(linelog.log_id) lead_line_log_id
4757 , creditlog.sales_credit_id sales_credit_id
4758 , MAX(creditlog.log_id) sales_credit_log_id
4759 , TRUNC(leadlog.last_update_date)  last_update_date
4760 , DECODE(creditlog.log_mode,'D','D','N') rev_flag
4761 FROM as_sales_credits_log creditlog
4762 , as_lead_lines_log linelog
4763 , (SELECT
4764 log_mode,
4765 last_update_date,
4766 decision_date,
4767 log_id,
4768 lead_id
4769 FROM
4770 (
4771 SELECT  /*+ parallel(LLOG) use_hash(LLOG)*/
4772 log_mode,
4773 DECODE(GREATEST(TRUNC(llog.last_update_date), p_coll_start),TRUNC(llog.last_update_date),
4774 TRUNC(llog.last_update_date),time.end_date) last_update_date,
4775 decision_date,
4776 llog.log_id,
4777 lead_id,
4778 RANK() OVER(PARTITION BY
4779 DECODE(GREATEST(TRUNC(llog.last_update_date), p_coll_start),TRUNC(llog.last_update_date),
4780 TRUNC(llog.last_update_date),time.end_date)
4781 ,llog.lead_id ORDER BY llog.log_id desc) log_id_rank
4782 FROM
4783 as_leads_log llog,
4784 bil_bi_time time
4785 WHERE
4786 llog.last_update_date >= p_start_date AND llog.last_update_date+0 < p_end_date
4787 AND(llog.last_update_date >= time.start_date and llog.last_update_date  < time.end_date+1)
4788 AND llog.endday_log_flag = 'Y'
4789 ) maxlog
4790 WHERE
4791 log_id_rank = 1)
4792 leadlog
4793 WHERE  linelog.lead_id=leadlog.lead_id
4794 
4795 AND (
4796 (creditlog.log_mode IN ('U', 'I')
4797 AND creditlog.salesgroup_id IS NOT NULL
4798 AND creditlog.CREDIT_TYPE_ID = g_credit_type_id)
4799 OR
4800 (creditlog.log_mode='D'))
4801 AND creditlog.lead_line_id=linelog.lead_line_id
4802 AND linelog.lead_id = creditlog.lead_id
4803 AND TRUNC(linelog.last_update_date) <= TRUNC(leadlog.last_update_date)
4804 AND TRUNC(creditlog.last_update_date) <= TRUNC(leadlog.last_update_date)
4805 AND linelog.endday_log_flag = 'Y'
4806 AND creditlog.endday_log_flag = 'Y'
4807 GROUP BY
4808 leadlog.log_id
4809 , leadlog.lead_id
4810 , linelog.lead_line_id
4811 , creditlog.sales_credit_id
4812 , TRUNC(leadlog.last_update_date)
4813 ,  DECODE(creditlog.log_mode,'D','D','N')
4814 UNION
4815 SELECT /*+  leading(leadlog) use_hash(creditlog,linelog) parallel(leadlog) parallel(creditlog) */
4816  leadlog.lead_id      lead_id
4817 , MAX(leadlog.log_id)  lead_log_id
4818 , linelog.lead_line_id lead_line_id
4819 , linelog.log_id		  lead_line_log_id
4820 , creditlog.sales_credit_id sales_credit_id
4821 , MAX(creditlog.log_id) sales_credit_log_id
4822 , TRUNC(linelog.last_update_date)  last_update_date
4823 , DECODE(creditlog.log_mode, 'D','D', 'N') rev_flag
4824 FROM as_sales_credits_log creditlog
4825 , (
4826 select log_mode, last_update_date, forecast_date,
4827 log_id, lead_Line_id, lead_id from
4828 ( SELECT /*+ parallel(LLOG) use_hash(LLOG) */
4829 log_mode,
4830 DECODE(GREATEST(TRUNC(llog.last_update_date), p_coll_start),TRUNC(llog.last_update_date),
4831 TRUNC(llog.last_update_date),time.end_date) last_update_date,
4832 forecast_date,
4833 llog.log_id,
4834 lead_id,
4835 lead_line_id,
4836 RANK() OVER(PARTITION BY
4837 DECODE(GREATEST(TRUNC(llog.last_update_date), p_coll_start),TRUNC(llog.last_update_date),
4838 TRUNC(llog.last_update_date),time.end_date)
4839 ,llog.lead_line_id ORDER BY llog.log_id desc) log_id_rank
4840 FROM as_lead_lines_log llog, bil_bi_time time
4841 WHERE
4842 (llog.last_update_date >= p_start_date AND llog.last_update_date+0 < p_end_date)
4843 AND(llog.last_update_date >= time.start_date and llog.last_update_date  < time.end_date+1)
4844 AND llog.endday_log_flag = 'Y'
4845 ) maxlog
4846 where log_id_rank = 1
4847 
4848 ) linelog
4849 , as_leads_log leadlog
4850 WHERE  linelog.lead_id=leadlog.lead_id
4851 AND creditlog.lead_line_id=linelog.lead_line_id
4852 AND linelog.lead_id = creditlog.lead_id
4853 
4854 AND (
4855 (creditlog.log_mode IN ('U', 'I')
4856 AND creditlog.salesgroup_id IS NOT NULL
4857 AND creditlog.CREDIT_TYPE_ID = g_credit_type_id)
4858 OR
4859 (creditlog.log_mode='D'))
4860 AND TRUNC(leadlog.last_update_date) <= TRUNC(linelog.last_update_date)
4861 AND TRUNC(creditlog.last_update_date) <= TRUNC(linelog.last_update_date)
4862 AND leadlog.endday_log_flag = 'Y'
4863 AND creditlog.endday_log_flag = 'Y'
4864 GROUP BY
4865 leadlog.lead_id
4866 , linelog.lead_line_id
4867 , linelog.log_id
4868 , creditlog.sales_credit_id
4869 , TRUNC(linelog.last_update_date)
4870 ,DECODE(creditlog.log_mode, 'D','D','N')
4871 UNION
4872 SELECT /*+ leading(leadlog) use_hash(linelog,creditlog) parallel(linelog) parallel(leadlog) */
4873   leadlog.lead_id      lead_id
4874 , MAX(leadlog.log_id)  lead_log_id
4875 , linelog.lead_line_id lead_line_id
4876 , MAX(linelog.log_id)  lead_line_log_id
4877 , creditlog.sales_credit_id sales_credit_id
4878 , creditlog.log_id sales_credit_log_id
4879 , TRUNC(creditlog.last_update_date) last_update_date
4880 ,DECODE(creditlog.log_mode,'D','D','N')
4881 FROM (
4882 select log_mode, salesgroup_id, last_update_date, credit_type_id,
4883 log_id, sales_credit_id, lead_Line_id, lead_id
4884 from (
4885 select  /*+  parallel(CLOG) use_hash(CLOG)*/
4886 log_mode, salesgroup_id, DECODE(GREATEST(TRUNC(clog.last_update_date), p_coll_start),TRUNC(clog.last_update_date),
4887 TRUNC(clog.last_update_date),time.end_date) last_update_date, credit_type_id,
4888 clog.log_id, sales_credit_id, lead_Line_id, lead_id,
4889 RANK() OVER(PARTITION BY
4890 DECODE(GREATEST(TRUNC(clog.last_update_date), p_coll_start),TRUNC(clog.last_update_date),
4891 TRUNC(clog.last_update_date),time.end_date)
4892 ,clog.sales_credit_id ORDER BY clog.log_id desc) log_id_rank
4893  from
4894 as_sales_credits_log clog, bil_bi_time time
4895 WHERE (clog.last_update_date >= p_start_date AND clog.last_update_date+0 < p_end_date)
4896 AND (clog.last_update_date >= time.start_date and
4897 clog.last_update_date  < time.end_date+1)
4898 AND clog.endday_log_flag = 'Y'
4899 ) maxlog
4900 where log_id_rank = l_rank
4901 
4902 )  creditlog
4903 , as_lead_lines_log linelog
4904 , as_leads_log leadlog
4905 WHERE  linelog.lead_id=leadlog.lead_id
4906 AND creditlog.lead_line_id=linelog.lead_line_id
4907 AND linelog.lead_id = creditlog.lead_id
4908 
4909 AND (
4910 (creditlog.log_mode IN ('U', 'I')
4911 AND creditlog.salesgroup_id IS NOT NULL
4912 AND creditlog.CREDIT_TYPE_ID = g_credit_type_id)
4913 OR
4914 (creditlog.log_mode='D'))
4915 AND TRUNC(leadlog.last_update_date) <= TRUNC(creditlog.last_update_date)
4916 AND TRUNC(linelog.last_update_date) <= TRUNC(creditlog.last_update_date)
4917 GROUP BY
4918 leadlog.lead_id
4919 , linelog.lead_line_id
4920 , creditlog.sales_credit_id
4921 , creditlog.log_id
4922 , TRUNC(creditlog.last_update_date)
4923 ,DECODE(creditlog.log_mode,'D','D','N')
4924 )
4925 ;
4926 commit;
4927 
4928 EXCEPTION WHEN OTHERS THEN
4929     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
4930     fnd_message.set_token('ERRNO' ,SQLCODE);
4931     fnd_message.set_token('REASON' ,SQLERRM);
4932   fnd_message.set_token('ROUTINE' , l_proc);
4933     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
4934   p_module => g_pkg || l_proc || ' ERROR while inserting into denlog_tmp',
4935   p_msg => fnd_message.get,
4936   p_force_log => TRUE);
4937 
4938     raise;
4939 
4940 	END;
4941 
4942 	 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
4943  	  bil_bi_util_collection_pkg.writeLog
4944      (
4945        p_log_level => fnd_log.LEVEL_EVENT,
4946        p_module => g_pkg || l_proc ,
4947        p_msg => 'inserted ' || sql%rowcount || 'into denlog_tmp from leadlog');
4948     END IF;
4949 
4950 
4951 
4952                 -- we don't need to delete any more since we get
4953                 --latest update for the day with endday_log_flag
4954 --					delete /*+ parallel(tmp1) */ from BIL_BI_OPDTL_DENLOG_TMP tmp1
4955 	/*					where exists (select 1 from BIL_BI_OPDTL_DENLOG_TMP tmp2
4956 						where tmp1.lead_id = tmp2.lead_id
4957 						and tmp1.lead_line_id = tmp2.lead_line_id
4958 						and tmp1.sales_credit_id = tmp2.sales_credit_id
4959 						and tmp1.last_update_date = tmp2.last_update_date
4960 						and tmp2.rev_flag = 'D')
4961 						and rev_flag <> 'D'; */
4962 
4963 
4964 					commit;
4965 
4966 
4967 		--- ok to filter stuff out here since checking is using denlog_tmp table not denlog_stg
4968 BEGIN
4969 					INSERT /*+ append parallel(denlog_stg) */ INTO BIL_BI_DENLOG_STG  denlog_stg
4970 					(
4971 
4972 					LEAD_LINE_ID ,
4973   					SALES_CREDIT_ID       ,
4974   					SALESGROUP_ID         ,
4975  				        SALESFORCE_ID         ,
4976   					LEAD_ID               ,
4977   					WIN_PROBABILITY       ,
4978   					CURRENCY_CODE         ,
4979   					WIN_LOSS_INDICATOR    ,
4980   					FORECAST_ROLLUP_FLAG  ,
4981   					ITEM_ID               ,
4982   					ITEM_ORGANIZATION_ID  ,
4983   					LEAD_NUMBER           ,
4984   					PRODUCT_CATEGORY_ID   ,
4985   					OPEN_STATUS_FLAG      ,
4986   					CREDIT_AMOUNT         ,
4987   					LAST_UPDATE_DATE      ,
4988   					DECISION_DATE
4989 					)
4990 					SELECT /*+ parallel(tmp) parallel(creditlog) parallel(linelog) parallel(leadlog) parallel(lead) full(tmp) full(creditlog) full(linelog) full(leadlog) full(lead)*/
4991    					 tmp.lead_line_id
4992    					 ,tmp.sales_credit_id
4993   					 ,creditlog.salesgroup_id
4994  					 ,creditlog.salesforce_id
4995 					 ,leadlog.lead_id
4996 					 ,decode(status.win_loss_indicator,'W', 100,  leadlog.WIN_PROBABILITY)
4997 					 ,leadlog.currency_code
4998 					 ,status.win_loss_indicator  win_loss_indicator
4999 					 ,status.FORECAST_ROLLUP_FLAG forecast_rollup_flag
5000 					 ,NVL(linelog.inventory_item_id, -1) item_id
5001 					 , DECODE(linelog.Inventory_item_id, NULL, -99,
5002 						  NVL(linelog.organization_id, -99)) ITEM_ORGANIZATION_ID
5003 				        ,lead.lead_number
5004 				        , linelog.product_category_id
5005 				        , status.opp_open_status_flag open_status_flag
5006 				        , creditlog.credit_amount
5007 				        , tmp.last_update_date
5008 				        , NVL(linelog.forecast_date, leadlog.decision_date)
5009 				     FROM BIL_BI_OPDTL_DENLOG_TMP  tmp
5010 					   ,as_sales_credits_log creditlog
5011 					   , as_lead_lines_log linelog
5012 					   , as_leads_log leadlog
5013 					   , as_statuses_b status
5014 					  , as_leads_all lead
5015 			             WHERE tmp.rev_flag = 'N'
5016 					   and tmp.lead_log_id = leadlog.log_id
5017 					   AND tmp.lead_line_log_id = linelog.log_id
5018 					   AND tmp.sales_credit_log_id = creditlog.log_id
5019 					   AND creditlog.salesgroup_id IS NOT NULL
5020 					   AND lead.lead_id = tmp.lead_id
5021 					   AND status.status_Code = leadlog.status_Code
5022 					   AND status.FORECAST_ROLLUP_FLAG = 'Y'
5023 					   AND NVL(linelog.forecast_date, leadlog.decision_date) >= G_Global_Start_Date
5024 					   AND NVL(linelog.forecast_date, leadlog.decision_date) BETWEEN l_min_decision_start AND l_max_decision_end
5025 					   AND tmp.last_update_date >= G_Global_Start_Date  --added by annsrini --fix for bug 5953589
5026 					   AND tmp.last_update_date <= l_limit_date
5027 					   AND NVL(linelog.forecast_date, leadlog.decision_date) <=l_limit_date
5028 					   AND lead.decision_date >= G_Global_Start_Date
5029 					   AND lead.decision_date <= l_limit_date;
5030 
5031 	commit;
5032 
5033 	EXCEPTION WHEN OTHERS THEN
5034     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
5035     fnd_message.set_token('ERRNO' ,SQLCODE);
5036     fnd_message.set_token('REASON' ,SQLERRM);
5037   fnd_message.set_token('ROUTINE' , l_proc);
5038     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
5039   p_module => g_pkg || l_proc || ' ERROR while inserting into denlog_stg',
5040   p_msg => fnd_message.get,
5041   p_force_log => TRUE);
5042 
5043     raise;
5044 
5045 	END;
5046 		                  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5047  	  bil_bi_util_collection_pkg.writeLog
5048      (
5049        p_log_level => fnd_log.LEVEL_EVENT,
5050        p_module => g_pkg || l_proc ,
5051        p_msg => 'inserted ' || sql%rowcount || 'into denlog_tmp_stg');
5052     END IF;
5053 
5054 
5055 ELSE
5056 
5057 INSERT  INTO bil_bi_opdtl_denlog_tmp tmp
5058 (lead_id,
5059 lead_log_id,
5060 lead_line_id,
5061 lead_line_log_id,
5062 sales_credit_id,
5063 sales_credit_log_id,
5064 last_update_date,
5065 rev_flag)
5066 SELECT lead_id,
5067 					 lead_log_id,
5068 					 lead_line_id,
5069 					 lead_line_log_id,
5070 					 sales_credit_id,
5071 					 sales_credit_log_id,
5072 					 last_update_date,
5073 					 rev_flag FROM
5074 (
5075 
5076 SELECT   /*+  full(linelog) full(creditlog) */
5077 --SELECT
5078 leadlog.lead_id     lead_id
5079 , leadlog.log_id      lead_log_id
5080 , linelog.lead_line_id    lead_line_id
5081 , MAX(linelog.log_id) lead_line_log_id
5082 , creditlog.sales_credit_id sales_credit_id
5083 , MAX(creditlog.log_id) sales_credit_log_id
5084 , TRUNC(leadlog.last_update_date)  last_update_date
5085 , DECODE(creditlog.log_mode,'D','D','N') rev_flag
5086 FROM as_sales_credits_log creditlog
5087 , as_lead_lines_log linelog
5088 , (SELECT
5089 log_mode,
5090 last_update_date,
5091 decision_date,
5092 log_id,
5093 lead_id
5094 FROM
5095 (
5096 SELECT
5097 log_mode,
5098 DECODE(GREATEST(TRUNC(llog.last_update_date), p_coll_start),TRUNC(llog.last_update_date),
5099 TRUNC(llog.last_update_date),time.end_date) last_update_date,
5100 decision_date,
5101 llog.log_id,
5102 lead_id,
5103 RANK() OVER(PARTITION BY
5104 DECODE(GREATEST(TRUNC(llog.last_update_date), p_coll_start),TRUNC(llog.last_update_date),
5105 TRUNC(llog.last_update_date),time.end_date)
5106 ,llog.lead_id ORDER BY llog.log_id desc) log_id_rank
5107 FROM
5108 as_leads_log llog,
5109 bil_bi_time time
5110 WHERE
5111 llog.last_update_date >= p_start_date AND llog.last_update_date < p_end_date
5112 AND(llog.last_update_date >= time.start_date and llog.last_update_date  < time.end_date+1)
5113 AND llog.endday_log_flag = 'Y'
5114 ) maxlog
5115 WHERE
5116 log_id_rank = 1)
5117 leadlog
5118 WHERE  linelog.lead_id=leadlog.lead_id
5119 
5120 AND (
5121 (creditlog.log_mode IN ('U', 'I')
5122 AND creditlog.salesgroup_id IS NOT NULL
5123 AND creditlog.CREDIT_TYPE_ID = g_credit_type_id)
5124 OR
5125 (creditlog.log_mode='D'))
5126 AND creditlog.lead_line_id=linelog.lead_line_id
5127 AND linelog.lead_id = creditlog.lead_id
5128 AND TRUNC(linelog.last_update_date) <= TRUNC(leadlog.last_update_date)
5129 AND TRUNC(creditlog.last_update_date) <= TRUNC(leadlog.last_update_date)
5130 AND linelog.endday_log_flag = 'Y'
5131 AND creditlog.endday_log_flag = 'Y'
5132 GROUP BY
5133 leadlog.log_id
5134 , leadlog.lead_id
5135 , linelog.lead_line_id
5136 , creditlog.sales_credit_id
5137 , TRUNC(leadlog.last_update_date)
5138 ,  DECODE(creditlog.log_mode,'D','D','N')
5139 UNION
5140 SELECT /*+  full(leadlog) full(creditlog)  */
5141 --SELECT
5142 leadlog.lead_id      lead_id
5143 , MAX(leadlog.log_id)  lead_log_id
5144 , linelog.lead_line_id lead_line_id
5145 , linelog.log_id		  lead_line_log_id
5146 , creditlog.sales_credit_id sales_credit_id
5147 , MAX(creditlog.log_id) sales_credit_log_id
5148 , TRUNC(linelog.last_update_date)  last_update_date
5149 , DECODE(creditlog.log_mode, 'D','D', 'N') rev_flag
5150 FROM as_sales_credits_log creditlog
5151 , (
5152 
5153 select log_mode, last_update_date, forecast_date,
5154 log_id, lead_Line_id, lead_id from
5155 ( SELECT
5156 log_mode,
5157 DECODE(GREATEST(TRUNC(llog.last_update_date), p_coll_start),TRUNC(llog.last_update_date),
5158 TRUNC(llog.last_update_date),time.end_date) last_update_date,
5159 forecast_date,
5160 llog.log_id,
5161 lead_id,
5162 lead_line_id,
5163 RANK() OVER(PARTITION BY
5164 DECODE(GREATEST(TRUNC(llog.last_update_date), p_coll_start),TRUNC(llog.last_update_date),
5165 TRUNC(llog.last_update_date),time.end_date)
5166 ,llog.lead_line_id ORDER BY llog.log_id desc) log_id_rank
5167 FROM as_lead_lines_log llog, bil_bi_time time
5168 WHERE
5169 (llog.last_update_date >= p_start_date AND llog.last_update_date < p_end_date)
5170 AND(llog.last_update_date >= time.start_date and llog.last_update_date  < time.end_date+1)
5171 AND llog.endday_log_flag = 'Y'
5172 ) maxlog
5173 where log_id_rank = 1
5174 
5175 ) linelog
5176 , as_leads_log leadlog
5177 WHERE  linelog.lead_id=leadlog.lead_id
5178 AND creditlog.lead_line_id=linelog.lead_line_id
5179 AND linelog.lead_id = creditlog.lead_id
5180 
5181 AND (
5182 (creditlog.log_mode IN ('U', 'I')
5183 AND creditlog.salesgroup_id IS NOT NULL
5184 AND creditlog.CREDIT_TYPE_ID = g_credit_type_id)
5185 OR
5186 (creditlog.log_mode='D'))
5187 AND TRUNC(leadlog.last_update_date) <= TRUNC(linelog.last_update_date)
5188 AND TRUNC(creditlog.last_update_date) <= TRUNC(linelog.last_update_date)
5189 AND leadlog.endday_log_flag = 'Y'
5190 AND creditlog.endday_log_flag = 'Y'
5191 GROUP BY
5192 leadlog.lead_id
5193 , linelog.lead_line_id
5194 , linelog.log_id
5195 , creditlog.sales_credit_id
5196 , TRUNC(linelog.last_update_date)
5197 ,DECODE(creditlog.log_mode, 'D','D','N')
5198 UNION
5199 SELECT /*+  full(leadlog) full(linelog) */
5200 --SELECT
5201 leadlog.lead_id      lead_id
5202 , MAX(leadlog.log_id)  lead_log_id
5203 , linelog.lead_line_id lead_line_id
5204 , MAX(linelog.log_id)  lead_line_log_id
5205 , creditlog.sales_credit_id sales_credit_id
5206 , creditlog.log_id sales_credit_log_id
5207 , TRUNC(creditlog.last_update_date) last_update_date
5208 ,DECODE(creditlog.log_mode,'D','D','N')
5209 FROM (
5210 select log_mode, salesgroup_id, last_update_date, credit_type_id,
5211 log_id, sales_credit_id, lead_Line_id, lead_id
5212 from (
5213 select /* leading(TIME) use_merge(CLOG) parallel(TIME) parallel(CLOG) */
5214 --SELECT
5215 log_mode, salesgroup_id, DECODE(GREATEST(TRUNC(clog.last_update_date), p_coll_start),TRUNC(clog.last_update_date),
5216 TRUNC(clog.last_update_date),time.end_date) last_update_date, credit_type_id,
5217 clog.log_id, sales_credit_id, lead_Line_id, lead_id,
5218 RANK() OVER(PARTITION BY
5219 DECODE(GREATEST(TRUNC(clog.last_update_date), p_coll_start),TRUNC(clog.last_update_date),
5220 TRUNC(clog.last_update_date),time.end_date)
5221 ,clog.sales_credit_id ORDER BY clog.log_id desc) log_id_rank
5222  from
5223 as_sales_credits_log clog, bil_bi_time time
5224 WHERE (clog.last_update_date >= p_start_date AND clog.last_update_date < p_end_date)
5225 AND (clog.last_update_date >= time.start_date and
5226 clog.last_update_date  < time.end_date+1)
5227 AND clog.endday_log_flag = 'Y'
5228 ) maxlog
5229 where log_id_rank = l_rank
5230 
5231 )  creditlog
5232 , as_lead_lines_log linelog
5233 , as_leads_log leadlog
5234 WHERE  linelog.lead_id=leadlog.lead_id
5235 AND creditlog.lead_line_id=linelog.lead_line_id
5236 AND linelog.lead_id = creditlog.lead_id
5237 
5238 AND (
5239 (creditlog.log_mode IN ('U', 'I')
5240 AND creditlog.salesgroup_id IS NOT NULL
5241 AND creditlog.CREDIT_TYPE_ID = g_credit_type_id)
5242 OR
5243 (creditlog.log_mode='D'))
5244 AND TRUNC(leadlog.last_update_date) <= TRUNC(creditlog.last_update_date)
5245 AND TRUNC(linelog.last_update_date) <= TRUNC(creditlog.last_update_date)
5246 GROUP BY
5247 leadlog.lead_id
5248 , linelog.lead_line_id
5249 , creditlog.sales_credit_id
5250 , creditlog.log_id
5251 , TRUNC(creditlog.last_update_date)
5252 ,DECODE(creditlog.log_mode,'D','D','N')
5253 );
5254 commit;
5255 
5256 
5257 	IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5258  	  bil_bi_util_collection_pkg.writeLog
5259      (
5260        p_log_level => fnd_log.LEVEL_EVENT,
5261        p_module => g_pkg || l_proc ,
5262        p_msg => 'inserted ' || sql%rowcount || 'into denlog_tmp');
5263     END IF;
5264 
5265 					/*delete  from BIL_BI_OPDTL_DENLOG_TMP tmp1
5266 						where exists (select 1 from BIL_BI_OPDTL_DENLOG_TMP tmp2
5267 						where tmp1.lead_id = tmp2.lead_id
5268 						and tmp1.lead_line_id = tmp2.lead_line_id
5269 						and tmp1.sales_credit_id = tmp2.sales_credit_id
5270 						and tmp1.last_update_date = tmp2.last_update_date
5271 						and tmp2.rev_flag = 'D')
5272 						and rev_flag <> 'D'; */
5273 
5274 
5275 					 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5276  	  bil_bi_util_collection_pkg.writeLog
5277      (
5278        p_log_level => fnd_log.LEVEL_EVENT,
5279        p_module => g_pkg || l_proc ,
5280        p_msg => 'deleted ' || sql%rowcount || 'from denlog_tmp for same day update ');
5281     END IF;
5282 
5283 					INSERT  INTO BIL_BI_DENLOG_STG  denlog_stg
5284 					(
5285 
5286 					LEAD_LINE_ID ,
5287   					SALES_CREDIT_ID       ,
5288   					SALESGROUP_ID         ,
5289  				        SALESFORCE_ID         ,
5290   					LEAD_ID               ,
5291   					WIN_PROBABILITY       ,
5292   					CURRENCY_CODE         ,
5293   					WIN_LOSS_INDICATOR    ,
5294   					FORECAST_ROLLUP_FLAG  ,
5295   					ITEM_ID               ,
5296   					ITEM_ORGANIZATION_ID  ,
5297   					LEAD_NUMBER           ,
5298   					PRODUCT_CATEGORY_ID   ,
5299   					OPEN_STATUS_FLAG      ,
5300   					CREDIT_AMOUNT         ,
5301   					LAST_UPDATE_DATE      ,
5302   					DECISION_DATE
5303 					)
5304 					SELECT
5305    					 tmp.lead_line_id
5306    					 ,tmp.sales_credit_id
5307   					 ,creditlog.salesgroup_id
5308  					 ,creditlog.salesforce_id
5309 					 ,leadlog.lead_id
5310 					,decode(status.win_loss_indicator,'W', 100,  leadlog.WIN_PROBABILITY)
5311 					 ,leadlog.currency_code
5312 					 ,status.win_loss_indicator  win_loss_indicator
5313 					 ,status.FORECAST_ROLLUP_FLAG forecast_rollup_flag
5314 					 ,NVL(linelog.inventory_item_id, -1) item_id
5315 					 , DECODE(linelog.Inventory_item_id, NULL, -99,
5316 						  NVL(linelog.organization_id, -99)) ITEM_ORGANIZATION_ID
5317 				        ,lead.lead_number
5318 				        , linelog.product_category_id
5319 				        , status.opp_open_status_flag open_status_flag
5320 				        , creditlog.credit_amount
5321 				        , tmp.last_update_date
5322 				        , NVL(linelog.forecast_date, leadlog.decision_date)
5323 				     FROM BIL_BI_OPDTL_DENLOG_TMP  tmp
5324 					   ,as_sales_credits_log creditlog
5325 					   , as_lead_lines_log linelog
5326 					   , as_leads_log leadlog
5327 					   , as_statuses_b status
5328 					  , as_leads_all lead
5329 			             WHERE tmp.rev_flag = 'N'
5330 					   and tmp.lead_log_id = leadlog.log_id
5331 					   AND tmp.lead_line_log_id = linelog.log_id
5332 					   AND tmp.sales_credit_log_id = creditlog.log_id
5333 					   AND creditlog.salesgroup_id IS NOT NULL
5334 					   AND lead.lead_id = tmp.lead_id
5335 					   AND status.status_Code = leadlog.status_Code
5336 					   AND status.FORECAST_ROLLUP_FLAG = 'Y'
5337 					   AND NVL(linelog.forecast_date, leadlog.decision_date) >=G_Global_Start_Date
5338 					   AND NVL(linelog.forecast_date, leadlog.decision_date) between l_min_decision_start AND l_max_decision_end
5339 					   AND tmp.last_update_date >= G_Global_Start_Date  --added by annsrini --fix for bug 5953589
5340 					   AND tmp.last_update_date <= l_limit_date
5341 					   AND NVL(linelog.forecast_date, leadlog.decision_date) <=l_limit_date
5342 					   AND lead.decision_date >= G_Global_Start_Date
5343 					   AND lead.decision_date <= l_limit_date;
5344 
5345 		 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5346  	  bil_bi_util_collection_pkg.writeLog
5347      (
5348        p_log_level => fnd_log.LEVEL_EVENT,
5349        p_module => g_pkg || l_proc ,
5350        p_msg => 'inserted ' || sql%rowcount || 'into denlog_stg');
5351     END IF;
5352 
5353 		 commit;
5354 		END IF;
5355 
5356 
5357 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
5358   bil_bi_util_collection_pkg.writeLog(
5359              p_log_level => fnd_log.LEVEL_PROCEDURE,
5360              p_module => g_pkg || l_proc || ' end',
5361              p_msg => 'End of Procedure '|| l_proc);
5362  END IF;
5363 
5364 EXCEPTION
5365 
5366 WHEN OTHERS THEN
5367     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
5368     fnd_message.set_token('ERRNO' ,SQLCODE);
5369     fnd_message.set_token('REASON' ,SQLERRM);
5370   fnd_message.set_token('ROUTINE' , l_proc);
5371     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
5372   p_module => g_pkg || l_proc || ' proc_error',
5373   p_msg => fnd_message.get,
5374   p_force_log => TRUE);
5375 
5376     raise;
5377 
5378  END Build_Denlog;
5379 
5380 PROCEDURE Gap_Fill(p_start_date IN DATE, p_end_date IN DATE, p_curr_coll_start IN DATE, p_mode IN VARCHAR2, p_small_gap IN BOOLEAN) IS
5381 
5382    l_proc VARCHAR2(100);
5383 
5384     l_cur_date DATE;
5385    l_pre_date DATE;
5386    l_year_start_date DATE;
5387 
5388    l_year_end_date DATE;
5389 
5390 
5391    l_day   NUMBER;
5392    l_week    NUMBER;
5393    l_period    NUMBER;
5394    l_qtr   NUMBER;
5395    l_year    NUMBER;
5396 
5397     l_min_date_id   NUMBER;
5398    l_max_date_id   NUMBER;
5399 
5400 
5401    l_prev_year_end   NUMBER;
5402 
5403 
5404   l_dynamic_sql   VARCHAR2(2000);
5405 
5406   l_sd_lyr   DATE; --same date last year
5407   l_sd_lper    DATE; --same date last period
5408   l_sd_lqtr    DATE; -- same date last quarter
5409   l_sd_lwk   DATE; --same date last week
5410 
5411 
5412   l_sd_lyr_end   DATE; --same date last year (end of the year/qtr/period/week)
5413   l_sd_lper_end    DATE; --same date last period (end of the year/qtr/period/week)
5414   l_sd_lqtr_end   DATE; -- same date last quarter (end of the year/qtr/period/week)
5415   l_sd_lwk_end   DATE; --same date last week (end of the year/qtr/period/week)
5416   l_pipe_tbl_type  VARCHAR2(10);
5417 
5418   -- This cursor will return every day in the current collection time range: tbl='CURR'
5419   -- and every week/period end date for historical and **current tbl**='HST' - we need
5420   -- **current tbl**
5421   --also because we insert into hist pipe tbl dta for end of weeks/periods in the current
5422   --time range as well, to help FE queries performance
5423 
5424 
5425     CURSOR c1(p_curr_coll_start DATE, p_start_date DATE, p_end_date DATE) IS
5426  select report_date, tbl FROM
5427  (SELECT report_date, 'CURR' tbl
5428  FROM fii_time_day day
5429  WHERE  report_date BETWEEN decode(greatest(p_start_date, p_curr_coll_start), p_start_date,
5430      p_start_date, p_curr_coll_start)
5431       AND p_end_date
5432  UNION ALL
5433  SELECT end_date report_date, 'HIST' tbl
5434  FROM BIL_BI_TIME time
5435 
5436  -- WHERE end_date <= p_end_date  -- Commented by TR bcos we dont want overlap of HIST and CURR data
5437  WHERE end_date < p_curr_coll_start
5438  )
5439  order by report_date;
5440 
5441  BEGIN
5442 
5443   l_proc := 'Gap_Fill';
5444 
5445   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
5446   bil_bi_util_collection_pkg.writeLog(
5447 
5448    p_log_level => fnd_log.LEVEL_PROCEDURE,
5449    p_module => g_pkg || l_proc || ' begin',
5450    p_msg => 'Start of Procedure '|| l_proc);
5451   END IF;
5452 
5453 
5454 
5455       OPEN c1(p_curr_coll_start, p_start_date, p_end_date);
5456       LOOP
5457        FETCH c1 INTO
5458         l_cur_date, l_pipe_tbl_type;
5459 
5460        EXIT WHEN c1%NOTFOUND ;
5461 
5462 
5463 
5464 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5465  bil_bi_util_collection_pkg.writeLog
5466  (
5467  p_log_level => fnd_log.LEVEL_EVENT,
5468  p_module => g_pkg || l_proc ,
5469  p_msg => 'working on ' || l_cur_date || 'l_type: ' || l_pipe_tbl_type);
5470   END IF;
5471 
5472 
5473         SELECT
5474          time.report_date_julian, time.week_id, time.ent_period_id,
5475          time.ent_qtr_id, time.ent_year_id,  to_number(to_char(LEAST(time.ent_year_start_date, time1.week_start_date), 'J')),
5476          to_number(to_char(GREATEST(time.ent_year_end_date,time2.week_end_date), 'J'))
5477          INTO
5478          l_day, l_week, l_period, l_qtr, l_year, l_min_date_id, l_max_date_id
5479          FROM
5480          FII_TIME_DAY time,
5481          FII_TIME_DAY time1,
5482      FII_TIME_DAY time2
5483 
5484          WHERE
5485          time.report_date = l_cur_date
5486          AND time1.report_date = time.ent_year_start_date
5487      AND time2.report_date = time.ent_year_end_date ;
5488 
5489 
5490 
5491 
5492 
5493 IF (p_mode = 'INIT') THEN
5494 
5495 IF (p_small_gap = FALSE) THEN
5496       -- if gap bigger than 3 days, delet  and  insert into staging for current date
5497           DELETE /*+  parallel(stg1) */ FROM bil_bi_pipeline_stg stg1
5498          WHERE EXISTS (SELECT /*+ parallel(tmp) */ 1 FROM bil_bi_opdtl_denlog_tmp tmp
5499          WHERE tmp.lead_id = stg1.lead_id
5500          AND tmp.lead_line_id = stg1.line_id
5501          AND tmp.sales_credit_id = stg1.credit_id
5502          AND last_update_date = l_cur_date);
5503 
5504           IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5505            bil_bi_util_collection_pkg.writeLog
5506 
5507          (
5508          p_log_level => fnd_log.LEVEL_EVENT,
5509          p_module => g_pkg || l_proc ,
5510          p_msg => 'deleted ' || sql%rowcount || 'from stg');
5511           END IF;
5512 
5513         commit;
5514 
5515 
5516 /*
5517          IF(l_prev_year_end is NULL) THEN
5518 
5519           l_prev_year_end := l_max_date_id;
5520          END IF;
5521 
5522          IF(l_day > l_prev_year_end) THEN
5523           -- delete most of last year's data */
5524            --  DELETE /*+parallel(stg) */FROM bil_bi_pipeline_stg stg
5525          /*  WHERE to_number(to_char(EFFECTIVE_DATE, 'J'))  < l_min_date_id;
5526 
5527 
5528           IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5529            bil_bi_util_collection_pkg.writeLog
5530 
5531          (
5532          p_log_level => fnd_log.LEVEL_EVENT,
5533          p_module => g_pkg || l_proc ,
5534          p_msg => 'deleted ' || sql%rowcount || 'from stg for last year ');
5535           END IF;
5536           commit;
5537 
5538           -- update l_prev_year_end
5539           l_prev_year_end := l_max_date_id;
5540 
5541          END IF;
5542 
5543 
5544 */
5545 
5546 -- if gap bigger than 3 days, delete and insert into staging for current date
5547 
5548           INSERT /*+ append */ INTO bil_bi_pipeline_stg stg(
5549           line_id,
5550           credit_id,
5551           SALES_GROUP_ID,
5552          SALESREP_ID,
5553           LEAD_ID,
5554 
5555           WIN_PROBABILITY,
5556           TXN_CURRENCY,
5557           WIN_LOSS_INDICATOR,
5558           FORECAST_ROLLUP_FLAG,
5559           ITEM_ID,
5560           ITEM_ORGANIZATION_ID,
5561           LEAD_NUMBER,
5562           PRODUCT_CATEGORY_ID,
5563           OPP_OPEN_STATUS_FLAG,
5564           SALES_CREDIT_AMOUNT,
5565           snap_date,
5566 
5567           EFFECTIVE_DATE,
5568           PRIM_CONVERSION_RATE,
5569           CONVERSION_RATE_S
5570          ) SELECT /*+ parallel(tmp) */
5571          lead_line_id,
5572          sales_credit_id
5573           ,salesgroup_id
5574           ,salesforce_id
5575           ,lead_id
5576           ,WIN_PROBABILITY
5577           ,currency_code
5578 
5579           ,win_loss_indicator
5580           ,forecast_rollup_flag
5581           ,item_id
5582           ,item_organization_id
5583           ,lead_number
5584           ,product_category_id
5585           ,open_status_flag
5586           ,credit_amount
5587           , last_update_date
5588           , decision_date
5589           , prim_conversion_rate
5590 
5591           , conversion_rate_s
5592         FROM BIL_BI_DENLOG_STG tmp
5593         WHERE tmp.last_update_date = l_cur_date
5594         AND product_category_id is not null;
5595 
5596 
5597 
5598   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5599           bil_bi_util_collection_pkg.writeLog
5600  (
5601  p_log_level => fnd_log.LEVEL_EVENT,
5602 
5603  p_module => g_pkg || l_proc ,
5604  p_msg => 'inserted' || sql%rowcount || 'into stg');
5605   END IF;
5606 
5607   commit;
5608 
5609 END IF;
5610 
5611   IF l_pipe_tbl_type = 'HIST' THEN
5612       -- parallel hints are disabled since they actually offer less performance in testing
5613       -- for this query
5614 
5615       INSERT /*+ append */INTO bil_bi_pipeline_f f
5616         (
5617         sales_group_id,
5618         salesrep_id,
5619         created_by ,
5620         creation_date,
5621         last_updated_by,
5622         last_update_date,
5623         last_update_login,
5624         request_id,
5625         program_application_id,
5626 
5627         program_id,
5628         program_update_date,
5629         snap_date,
5630         item_id,
5631         item_organization_id,
5632         win_probability,
5633         product_category_id,
5634         pipeline_amt_day,
5635         pipeline_amt_week,
5636         pipeline_amt_period,
5637         pipeline_amt_quarter,
5638 
5639         pipeline_amt_year,
5640         open_amt_day,
5641         open_amt_week,
5642         open_amt_period,
5643         open_amt_quarter,
5644         open_amt_year,
5645         pipeline_amt_day_s,
5646         pipeline_amt_week_s,
5647         pipeline_amt_period_s,
5648         pipeline_amt_quarter_s,
5649         pipeline_amt_year_s,
5650 
5651         open_amt_day_s,
5652         open_amt_week_s,
5653         open_amt_period_s,
5654         open_amt_quarter_s,
5655         open_amt_year_s
5656         )
5657         SELECT /*+ parallel(stg) */
5658         SALES_GROUP_ID,
5659         SALESREP_ID,
5660         g_user_id,
5661         SYSDATE,
5662 
5663         g_user_id,
5664         SYSDATE,
5665         G_Login_Id,
5666         G_request_id,
5667         G_appl_id,
5668         G_program_id,
5669         SYSDATE,
5670         l_cur_date,
5671         ITEM_ID,
5672         ITEM_ORGANIZATION_ID,
5673         DECODE(stg.win_loss_indicator, 'W', 100, stg.WIN_PROBABILITY) win_probability,
5674 
5675         PRODUCT_CATEGORY_ID,
5676         SUM(CASE
5677         WHEN TIME.report_date = l_cur_date
5678         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5679        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
5680         ),
5681         SUM(CASE
5682         WHEN TIME.week_id = l_week
5683         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5684        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
5685         ),
5686 
5687         SUM(CASE
5688         WHEN TIME.ent_period_id = l_period
5689         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5690        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
5691         SUM(CASE
5692         WHEN TIME.ent_qtr_id = l_qtr
5693         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5694        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
5695         SUM(CASE
5696         WHEN TIME.ent_year_id = l_year
5697         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5698 
5699        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
5700        SUM( CASE
5701         WHEN TIME.report_date = l_cur_date  AND OPP_OPEN_STATUS_FLAG = 'Y'
5702         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5703        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
5704         SUM( CASE
5705         WHEN TIME.week_id = l_week  AND OPP_OPEN_STATUS_FLAG = 'Y'
5706         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5707        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
5708         SUM(CASE
5709         WHEN TIME.ent_period_id = l_period  AND OPP_OPEN_STATUS_FLAG = 'Y'
5710 
5711         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5712        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
5713         SUM(CASE
5714         WHEN TIME.ent_qtr_id = l_qtr  AND OPP_OPEN_STATUS_FLAG = 'Y'
5715         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5716        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
5717         SUM(CASE
5718         WHEN TIME.ent_year_id = l_year  AND OPP_OPEN_STATUS_FLAG = 'Y'
5719         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5720        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
5721         SUM(CASE
5722 
5723         WHEN TIME.report_date = l_cur_date
5724         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5725        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
5726         ),
5727         SUM(CASE
5728         WHEN TIME.week_id = l_week
5729         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
5730         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END
5731         ),
5732         SUM(CASE
5733         WHEN TIME.ent_period_id = l_period
5734 
5735         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
5736         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
5737         SUM(CASE
5738         WHEN TIME.ent_qtr_id = l_qtr
5739         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
5740         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
5741         SUM(CASE
5742         WHEN TIME.ent_year_id = l_year
5743         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
5744         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
5745        SUM( CASE
5746 
5747         WHEN TIME.report_date = l_cur_date  AND OPP_OPEN_STATUS_FLAG = 'Y'
5748         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
5749         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
5750         SUM( CASE
5751         WHEN TIME.week_id = l_week  AND OPP_OPEN_STATUS_FLAG = 'Y'
5752         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
5753         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
5754         SUM(CASE
5755         WHEN TIME.ent_period_id = l_period  AND OPP_OPEN_STATUS_FLAG = 'Y'
5756         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
5757         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
5758 
5759         SUM(CASE
5760         WHEN TIME.ent_qtr_id = l_qtr  AND OPP_OPEN_STATUS_FLAG = 'Y'
5761         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
5762         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
5763         SUM(CASE
5764         WHEN TIME.ent_year_id = l_year  AND OPP_OPEN_STATUS_FLAG = 'Y'
5765         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
5766         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END )
5767         FROM
5768         bil_bi_pipeline_stg stg,
5769         fii_time_day time
5770 
5771         WHERE stg.effective_date =  TIME.report_date
5772         AND forecast_rollup_flag = 'Y'
5773         AND TIME.report_date_julian BETWEEN l_min_date_id AND l_max_date_id
5774         GROUP BY
5775         sales_group_id,
5776         salesrep_id,
5777         item_id,
5778         item_organization_id,
5779         DECODE(stg.win_loss_indicator, 'W', 100, stg.WIN_PROBABILITY),
5780         product_category_id
5781         HAVING
5782 
5783         SUM(CASE
5784         WHEN TIME.week_id = l_week
5785         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5786        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
5787         )IS NOT NULL OR
5788         SUM(CASE
5789         WHEN TIME.ent_year_id = l_year
5790         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5791        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END )
5792        IS NOT NULL ;
5793 
5794 
5795 
5796 ELSE
5797 --insert into bil_bi_pipec_f
5798 
5799     Insert_into_curr_sumry(l_cur_date,  l_week, l_period
5800       , l_qtr, l_year, l_min_date_id, l_max_date_id);
5801 
5802 
5803 END IF ; --INIT/INCR
5804 
5805        ELSE
5806 	   IF (p_small_gap = FALSE) THEN --gap bigger than 3 days
5807          DELETE /*+ index(stg1,BIL_BI_PIPELINE_STG_U1)  */ FROM bil_bi_pipeline_stg stg1
5808          WHERE EXISTS (SELECT  1 FROM bil_bi_opdtl_denlog_tmp tmp
5809          WHERE tmp.lead_id = stg1.lead_id
5810          AND tmp.lead_line_id = stg1.line_id
5811 
5812          AND tmp.sales_credit_id = stg1.credit_id
5813          AND last_update_date = l_cur_date);
5814 
5815           IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5816            bil_bi_util_collection_pkg.writeLog
5817          (
5818          p_log_level => fnd_log.LEVEL_EVENT,
5819          p_module => g_pkg || l_proc ,
5820          p_msg => 'deleted ' || sql%rowcount || 'from stg');
5821           END IF;
5822 
5823 
5824         commit;
5825 /*
5826          IF(l_prev_year_end is NULL) THEN
5827           l_prev_year_end := l_max_date_id;
5828          END IF;
5829 
5830          IF(l_day > l_prev_year_end) THEN
5831           -- delete most of last year's data
5832            DELETE  FROM bil_bi_pipeline_stg stg
5833          WHERE to_number(to_char(EFFECTIVE_DATE, 'J'))  < l_min_date_id;
5834 
5835 
5836 
5837           IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5838            bil_bi_util_collection_pkg.writeLog
5839          (
5840          p_log_level => fnd_log.LEVEL_EVENT,
5841          p_module => g_pkg || l_proc ,
5842          p_msg => 'deleted ' || sql%rowcount || 'from stg for last year ');
5843           END IF;
5844           commit;
5845 
5846           -- update l_prev_year_end
5847 
5848           l_prev_year_end := l_max_date_id;
5849 
5850          END IF;
5851 */
5852 
5853 
5854 
5855           INSERT INTO bil_bi_pipeline_stg stg(
5856           line_id,
5857           credit_id,
5858           SALES_GROUP_ID,
5859 
5860            SALESREP_ID,
5861           LEAD_ID,
5862           WIN_PROBABILITY,
5863           TXN_CURRENCY,
5864           WIN_LOSS_INDICATOR,
5865           FORECAST_ROLLUP_FLAG,
5866           ITEM_ID,
5867           ITEM_ORGANIZATION_ID,
5868           LEAD_NUMBER,
5869           PRODUCT_CATEGORY_ID,
5870           OPP_OPEN_STATUS_FLAG,
5871 
5872           SALES_CREDIT_AMOUNT,
5873           snap_date,
5874           EFFECTIVE_DATE,
5875           PRIM_CONVERSION_RATE,
5876           CONVERSION_RATE_S
5877          ) SELECT
5878          lead_line_id,
5879          sales_credit_id
5880           ,salesgroup_id
5881           ,salesforce_id
5882           ,lead_id
5883 
5884           ,WIN_PROBABILITY
5885           ,currency_code
5886           ,win_loss_indicator
5887           ,forecast_rollup_flag
5888           ,item_id
5889           ,item_organization_id
5890           ,lead_number
5891           ,product_category_id
5892           ,open_status_flag
5893           ,credit_amount
5894           , last_update_date
5895 
5896           , decision_date
5897           , prim_conversion_rate
5898           , conversion_rate_s
5899         FROM BIL_BI_DENLOG_STG tmp
5900         WHERE tmp.last_update_date = l_cur_date
5901         AND product_category_id is not null;
5902 
5903 
5904 
5905 IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
5906           bil_bi_util_collection_pkg.writeLog
5907 
5908  (
5909  p_log_level => fnd_log.LEVEL_EVENT,
5910  p_module => g_pkg || l_proc ,
5911  p_msg => 'inserted' || sql%rowcount || 'into stg');
5912   END IF;
5913 
5914   commit;
5915 
5916 END IF; --gap smaller than 3 days
5917 
5918   IF l_pipe_tbl_type = 'HIST' THEN
5919 
5920       INSERT INTO bil_bi_pipeline_f f
5921 
5922         (
5923         sales_group_id,
5924         salesrep_id,
5925         created_by ,
5926         creation_date,
5927         last_updated_by,
5928         last_update_date,
5929         last_update_login,
5930         request_id,
5931         program_application_id,
5932         program_id,
5933 
5934         program_update_date,
5935         snap_date,
5936         item_id,
5937         item_organization_id,
5938         win_probability,
5939         product_category_id,
5940         pipeline_amt_day,
5941         pipeline_amt_week,
5942         pipeline_amt_period,
5943         pipeline_amt_quarter,
5944         pipeline_amt_year,
5945 
5946         open_amt_day,
5947         open_amt_week,
5948         open_amt_period,
5949         open_amt_quarter,
5950         open_amt_year,
5951         pipeline_amt_day_s,
5952         pipeline_amt_week_s,
5953         pipeline_amt_period_s,
5954         pipeline_amt_quarter_s,
5955         pipeline_amt_year_s,
5956         open_amt_day_s,
5957 
5958         open_amt_week_s,
5959         open_amt_period_s,
5960         open_amt_quarter_s,
5961         open_amt_year_s
5962         )
5963         SELECT
5964         SALES_GROUP_ID,
5965         SALESREP_ID,
5966         g_user_id,
5967         SYSDATE,
5968         g_user_id,
5969 
5970         SYSDATE,
5971         G_Login_Id,
5972         G_request_id,
5973         G_appl_id,
5974         G_program_id,
5975         SYSDATE,
5976         l_cur_date,
5977         ITEM_ID,
5978         ITEM_ORGANIZATION_ID,
5979         DECODE(stg.win_loss_indicator, 'W', 100, stg.WIN_PROBABILITY) win_probability,
5980         PRODUCT_CATEGORY_ID,
5981 
5982         SUM(CASE
5983         WHEN TIME.report_date = l_cur_date
5984         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5985        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
5986         ),
5987         SUM(CASE
5988         WHEN TIME.week_id = l_week
5989         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5990        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
5991         ),
5992         SUM(CASE
5993 
5994         WHEN TIME.ent_period_id = l_period
5995         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
5996        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
5997         SUM(CASE
5998         WHEN TIME.ent_qtr_id = l_qtr
5999         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6000        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
6001         SUM(CASE
6002         WHEN TIME.ent_year_id = l_year
6003         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6004        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
6005 
6006        SUM( CASE
6007         WHEN TIME.report_date = l_cur_date  AND OPP_OPEN_STATUS_FLAG = 'Y'
6008         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6009        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
6010         SUM( CASE
6011         WHEN TIME.week_id = l_week  AND OPP_OPEN_STATUS_FLAG = 'Y'
6012         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6013        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END),
6014         SUM(CASE
6015         WHEN TIME.ent_period_id = l_period  AND OPP_OPEN_STATUS_FLAG = 'Y'
6016         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6017 
6018        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
6019         SUM(CASE
6020         WHEN TIME.ent_qtr_id = l_qtr  AND OPP_OPEN_STATUS_FLAG = 'Y'
6021         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6022        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
6023         SUM(CASE
6024         WHEN TIME.ent_year_id = l_year  AND OPP_OPEN_STATUS_FLAG = 'Y'
6025         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6026        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ),
6027         SUM(CASE
6028         WHEN TIME.report_date = l_cur_date
6029 
6030         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6031        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
6032         ),
6033         SUM(CASE
6034         WHEN TIME.week_id = l_week
6035         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6036         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END
6037         ),
6038         SUM(CASE
6039         WHEN TIME.ent_period_id = l_period
6040         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6041 
6042         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
6043         SUM(CASE
6044         WHEN TIME.ent_qtr_id = l_qtr
6045         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6046         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
6047         SUM(CASE
6048         WHEN TIME.ent_year_id = l_year
6049         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6050         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
6051        SUM( CASE
6052         WHEN TIME.report_date = l_cur_date  AND OPP_OPEN_STATUS_FLAG = 'Y'
6053 
6054         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6055         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
6056         SUM( CASE
6057         WHEN TIME.week_id = l_week  AND OPP_OPEN_STATUS_FLAG = 'Y'
6058         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6059         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
6060         SUM(CASE
6061         WHEN TIME.ent_period_id = l_period  AND OPP_OPEN_STATUS_FLAG = 'Y'
6062         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6063         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ),
6064         SUM(CASE
6065 
6066         WHEN TIME.ent_qtr_id = l_qtr  AND OPP_OPEN_STATUS_FLAG = 'Y'
6067         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6068         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END),
6069         SUM(CASE
6070         WHEN TIME.ent_year_id = l_year  AND OPP_OPEN_STATUS_FLAG = 'Y'
6071         THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6072         stg.sales_credit_amount*conversion_rate_s) ELSE NULL END )
6073         FROM
6074         bil_bi_pipeline_stg stg,
6075         fii_time_day time
6076         WHERE stg.effective_date =  TIME.report_date
6077 
6078         AND forecast_rollup_flag = 'Y'
6079         AND TIME.report_date_julian BETWEEN l_min_date_id AND l_max_date_id
6080         GROUP BY
6081         sales_group_id,
6082         salesrep_id,
6083         item_id,
6084         item_organization_id,
6085         DECODE(stg.win_loss_indicator, 'W', 100, stg.WIN_PROBABILITY),
6086         product_category_id
6087         HAVING
6088         SUM(CASE
6089 
6090         WHEN TIME.week_id = l_week
6091         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6092        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
6093         )IS NOT NULL OR
6094         SUM(CASE
6095         WHEN TIME.ent_year_id = l_year
6096         THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6097        stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END )
6098        IS NOT NULL ;
6099 
6100 
6101 
6102 ELSE
6103 --insert into bil_bi_pipec_f
6104 
6105 --also insert prior amounts from bil_bi_pipeline_f
6106 --since we may not have data for the same day last week/quarter/period/year in the hist. table
6107 --we need to get the end of the week/quarter ...
6108 --if the same day last year is closer to the end of a certain week/quarter/period
6109 --than to the end of te year, get the closest date
6110 -- this can be acheived by using the bil_bi_time table:
6111 
6112     IF (p_small_gap = FALSE) THEN
6113     Insert_into_curr_sumry(l_cur_date,  l_week, l_period
6114       , l_qtr, l_year, l_min_date_id, l_max_date_id);
6115   ELSE
6116      Ins_into_CurrSum_SmGap(l_cur_date,  l_week, l_period
6117       , l_qtr, l_year, l_min_date_id, l_max_date_id);
6118   END IF;
6119 
6120 
6121 
6122 END IF ; --if pipe_tbl_type='HIST'
6123 
6124 END IF;
6125 
6126  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
6127      bil_bi_util_collection_pkg.writeLog
6128  (
6129  p_log_level => fnd_log.LEVEL_EVENT,
6130  p_module => g_pkg || l_proc ,
6131  p_msg => 'inserted ' || sql%rowcount || 'into fact');
6132   END IF;
6133 
6134 
6135   commit;
6136 
6137         END LOOP;
6138 
6139        CLOSE c1;
6140 
6141 
6142  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
6143   bil_bi_util_collection_pkg.writeLog(
6144    p_log_level => fnd_log.LEVEL_PROCEDURE,
6145 
6146    p_module => g_pkg || l_proc || ' end',
6147    p_msg => 'End of Procedure '|| l_proc);
6148  END IF;
6149 
6150  EXCEPTION
6151 
6152 WHEN OTHERS THEN
6153   fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
6154   fnd_message.set_token('ERRNO' ,SQLCODE);
6155   fnd_message.set_token('REASON' ,SQLERRM);
6156   fnd_message.set_token('ROUTINE' , l_proc);
6157 
6158   bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
6159   p_module => g_pkg || l_proc || ' proc_error',
6160   p_msg => fnd_message.get,
6161   p_force_log => TRUE);
6162 
6163   raise;
6164 
6165  END Gap_Fill;
6166 
6167 PROCEDURE Insert_Into_Stg_SmallGap(p_start_date IN DATE, p_end_date IN DATE, p_first_fact_run IN DATE) IS
6168 
6169 l_proc VARCHAR2(100);
6170 l_count NUMBER;
6171 l_limit_date DATE; --added by annsrini --fix for bug 5953589
6172 
6173 BEGIN
6174 
6175 l_proc := 'Insert_Into_Stg_SmallGap';
6176 l_limit_date := add_months(trunc(g_program_start),24); --added by annsrini --fix for bug 5953589
6177 
6178   IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
6179             bil_bi_util_collection_pkg.writeLog(
6180              p_log_level => fnd_log.LEVEL_PROCEDURE,
6181              p_module => g_pkg || l_proc || ' begin',
6182              p_msg => 'Start of Procedure '|| l_proc);
6183   END IF;
6184 
6185 --Removed hints for FTS in select -- fix for bug 7595743
6186 
6187 INSERT /*+ APPEND PARALLEL(stg) */  INTO bil_bi_pipeline_stg stg(
6188 	SALES_GROUP_ID,
6189    	SALESREP_ID,
6190 	LEAD_ID,
6191 	WIN_PROBABILITY,
6192 	TXN_CURRENCY,
6193 	WIN_LOSS_INDICATOR,
6194 	FORECAST_ROLLUP_FLAG,
6195 	ITEM_ID,
6196 	ITEM_ORGANIZATION_ID,
6197 	LEAD_NUMBER,
6198 	PRODUCT_CATEGORY_ID,
6199 	OPP_OPEN_STATUS_FLAG,
6200 	SALES_CREDIT_AMOUNT,
6201 	SNAP_DATE,
6202 	EFFECTIVE_DATE
6203    )
6204    SELECT
6205   creditlog.salesgroup_id
6206  ,creditlog.salesforce_id
6207  ,maxlog.lead_id
6208  ,decode(status.win_loss_indicator,'W', 100,  maxlog.WIN_PROBABILITY)
6209  ,maxlog.currency_code
6210  ,status.win_loss_indicator  win_loss_indicator
6211  ,status.FORECAST_ROLLUP_FLAG forecast_rollup_flag
6212         ,nvl(linelog.inventory_item_id, -1)
6213         , decode(linelog.Inventory_item_id, null, -99,
6214 nvl(linelog.organization_id, -99))
6215         ,lead.lead_number
6216         , linelog.product_category_id
6217         , status.opp_open_status_flag open_status_flag
6218         , sum(creditlog.credit_amount)
6219         , maxlog.report_date
6220         , maxlog.decision_date
6221    FROM  as_leads_all lead
6222        , as_lead_lines_log linelog
6223        , as_statuses_b status
6224        , as_sales_credits_log creditlog
6225        , (-- alias maxlog
6226  SELECT
6227              leadlog2.lead_id     lead_id
6228            , MAX(leadlog2.max_id)      lead_log_id
6229            , linelog1.lead_line_id    lead_line_id
6230            , MAX(linelog1.log_id) lead_line_log_id
6231            , creditlog1.sales_credit_id sales_credit_id
6232            , MAX(creditlog1.log_id) sales_credit_log_id
6233            , leadlog2.report_date report_date
6234            , leadlog2.currency_code
6235            , leadlog2.status_code
6236            , leadlog2.win_probability
6237            ,  NVL(linelog1.forecast_date, leadlog2.decision_date) decision_date
6238           FROM
6239              as_sales_credits_log creditlog1,
6240              as_lead_lines_log linelog1,
6241             ( -- alias leadlog2
6242              SELECT  maxlead.report_date, maxlead.start_date,maxlead.end_date,maxlead.lead_id, maxlead.max_id,
6243               llog.decision_date, llog.win_probability, llog.status_code,
6244               llog.currency_code
6245               FROM
6246               ( -- alias maxlead
6247                 SELECT  gapdays.report_date, gapdays.start_date, gapdays.end_date,
6248                       leadlog1.lead_id, max(leadlog1.log_id) max_id
6249                  FROM as_leads_log leadlog1,
6250                      ( -- alias gapdays
6251                        SELECT report_date,
6252                               LEAST(year.start_date, week.start_date) start_date,
6253                               GREATEST(year.end_date,week.end_date) end_date
6254                          FROM
6255                               fii_time_ent_year  year
6256                             , fii_time_week week
6257                             , fii_time_day day
6258                          WHERE report_date between p_start_date and p_end_date
6259                            AND day.week_id = week.week_id
6260                            AND day.ent_year_id = year.ent_year_id
6261                      ) gapdays
6262                 WHERE leadlog1.last_update_date < gapdays.report_date+1
6263 		    GROUP BY lead_id, gapdays.report_date, gapdays.start_date, gapdays.end_date
6264               ) maxlead,
6265               as_leads_log llog
6266               WHERE maxlead.max_id = llog.log_id
6267                 and llog.decision_date >= p_first_fact_run
6268 		    and llog.decision_date <= l_limit_date --added by annsrini  --fix for bug 5953589
6269                 AND llog.decision_date between maxlead.start_date and maxlead.end_date
6270             ) leadlog2
6271         WHERE linelog1.lead_id=leadlog2.lead_id
6272           AND creditlog1.lead_line_id=linelog1.lead_line_id
6273           AND creditlog1.lead_id = leadlog2.lead_id
6274           AND linelog1.lead_id = creditlog1.lead_id
6275           AND linelog1.last_update_date < leadlog2.report_date+1
6276           AND creditlog1.last_update_date < leadlog2.report_date+1
6277 	    AND nvl(linelog1.forecast_date,leadlog2.decision_date) >= p_first_fact_run  --added by annsrini  --fix for bug 5953589
6278 	    AND nvl(linelog1.forecast_date,leadlog2.decision_date) between leadlog2.start_date and leadlog2.end_date
6279           AND nvl(linelog1.forecast_date,leadlog2.decision_date) <= l_limit_date
6280           AND ((creditlog1.log_mode in ('U', 'I')
6281                AND creditlog1.salesgroup_id IS NOT NULL
6282                AND creditlog1.CREDIT_TYPE_ID = g_credit_type_id)
6283                OR
6284                (creditlog1.log_mode='D'))
6285          GROUP BY
6286            creditlog1.sales_credit_id
6287          , leadlog2.lead_id
6288          , linelog1.lead_line_id
6289          , leadlog2.report_date
6290          , leadlog2.currency_code
6291            , leadlog2.status_code
6292            , leadlog2.win_probability
6293            ,NVL(linelog1.forecast_date, leadlog2.decision_date)
6294           ) maxlog
6295    WHERE maxlog.lead_line_log_id = linelog.log_id
6296      AND maxlog.sales_credit_log_id = creditlog.log_id
6297      AND creditlog.salesgroup_id is not null
6298      AND lead.lead_id = maxlog.lead_id
6299      AND status.status_Code = maxlog.status_Code
6300    GROUP BY creditlog.salesgroup_id
6301            ,creditlog.salesforce_id
6302            ,maxlog.lead_id
6303            ,maxlog.WIN_PROBABILITY
6304            ,maxlog.currency_code
6305            ,status.win_loss_indicator
6306            ,status.FORECAST_ROLLUP_FLAG
6307            ,nvl(linelog.inventory_item_id, -1)
6308            ,decode(linelog.Inventory_item_id, null, -99, nvl(linelog.organization_id, -99))
6309            ,lead.lead_number
6310            ,linelog.product_category_id
6311            ,status.opp_open_status_flag
6312            ,maxlog.report_date
6313            ,maxlog.decision_date;
6314 commit;
6315 
6316 
6317 SELECT COUNT(1) INTO l_count FROM BIL_BI_PIPELINE_STG;
6318 
6319 
6320  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
6321   bil_bi_util_collection_pkg.writeLog(
6322              p_log_level => fnd_log.LEVEL_EVENT,
6323              p_module => g_pkg || l_proc || ' end',
6324              p_msg => 'Inserted into staging from as log tables '|| l_count);
6325  END IF;
6326 
6327  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
6328   bil_bi_util_collection_pkg.writeLog(
6329              p_log_level => fnd_log.LEVEL_PROCEDURE,
6330              p_module => g_pkg || l_proc || ' end',
6331              p_msg => 'End of Procedure '|| l_proc);
6332  END IF;
6333 
6334  EXCEPTION
6335 
6336 WHEN OTHERS THEN
6337     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
6338     fnd_message.set_token('ERRNO' ,SQLCODE);
6339     fnd_message.set_token('REASON' ,SQLERRM);
6340   fnd_message.set_token('ROUTINE' , l_proc);
6341     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
6342   p_module => g_pkg || l_proc || ' proc_error',
6343   p_msg => fnd_message.get,
6344   p_force_log => TRUE);
6345 
6346     raise;
6347 END Insert_Into_Stg_SmallGap;
6348 
6349 PROCEDURE Ins_Into_CurrSum_SmGap(p_date IN DATE,  p_week IN NUMBER, p_period IN NUMBER,
6350 		  						 p_qtr IN NUMBER, p_year IN NUMBER, p_min_date_id IN NUMBER,
6351 								 p_max_date_id IN NUMBER) IS
6352 
6353 l_sd_lwk_end DATE;
6354 l_sd_lper_end DATE;
6355 l_sd_lqtr_end DATE;
6356 l_sd_lyr_end DATE;
6357 l_sd_lwk DATE;
6358 l_sd_lper DATE;
6359 l_sd_lqtr DATE;
6360 l_sd_lyr DATE;
6361 l_proc VARCHAR2(100);
6362 l_dynamic_sql VARCHAR2(1000);
6363 
6364 BEGIN
6365 
6366 l_proc := 'Ins_Into_CurrSum_SmGap';
6367 
6368  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
6369     bil_bi_util_collection_pkg.writeLog(
6370              p_log_level => fnd_log.LEVEL_PROCEDURE,
6371              p_module => g_pkg || l_proc || ' begin',
6372              p_msg => 'Start of Procedure '|| l_proc);
6373     END IF;
6374 
6375 BEGIN
6376                               l_dynamic_sql := 'BEGIN :1 := fii_time_api.sd_pwk(:2); END;';
6377                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lwk, IN p_date;
6378 
6379                  SELECT week_end_date
6380                  INTO l_sd_lwk_end
6381                  from fii_time_day
6382                  where report_date=l_sd_lwk;
6383 
6384                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.ent_sd_pper_end(:2); END;';
6385                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lper, IN p_date;
6386 
6387                  SELECT LEAST(week_end_date, ent_period_end_date)
6388                  INTO l_sd_lper_end
6389                  from fii_time_day
6390                  where report_date=l_sd_lper;
6391 
6392                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.ent_sd_pqtr_end(:2); END;';
6393                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lqtr, IN p_date;
6394 
6395                  SELECT LEAST(week_end_date, ent_qtr_end_date)
6396                  INTO l_sd_lqtr_end
6397                  from fii_time_day
6398                  where report_date=l_sd_lqtr;
6399 
6400                  l_dynamic_sql := 'BEGIN :1 := fii_time_api.ent_sd_lyr_end(:2); END;';
6401                  EXECUTE IMMEDIATE l_dynamic_sql USING OUT l_sd_lyr, IN p_date;
6402 
6403                   SELECT LEAST(week_end_date, ent_year_end_date)
6404                  INTO l_sd_lyr_end
6405                  from fii_time_day
6406                  where report_date=l_sd_lyr;
6407 
6408 EXCEPTION WHEN NO_DATA_FOUND THEN
6409     NULL;
6410 	IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_EVENT) THEN
6411  bil_bi_util_collection_pkg.writeLog
6412  (
6413  p_log_level => fnd_log.LEVEL_EVENT,
6414  p_module => g_pkg || l_proc ,
6415  p_msg => 'inside no_data_found in  insert into curr sumry ');
6416   END IF;
6417 
6418 END;
6419 
6420 
6421        --also insert prior amounts from bil_bi_pipeline_f
6422 --since we may not have data for the same day last week/quarter/period/year in the hist. table
6423 --we need to get the end of the week/quarter ...
6424 --if the same day last year is closer to the end of a certain week/quarter/period
6425 --than to the end of te year, get the closest date
6426 -- this can be achieved by using the bil_bi_time table:
6427 
6428 
6429 
6430 						  INSERT /*+ append parallel(f) */INTO bil_bi_pipec_f f
6431 			      (
6432 			        sales_group_id,
6433 			        salesrep_id,
6434 			        created_by ,
6435 			        creation_date,
6436 			        last_updated_by,
6437 			        last_update_date,
6438 			        last_update_login,
6439 			        request_id,
6440 			        program_application_id,
6441 			        program_id,
6442 			        program_update_date,
6443 			        snap_date,
6444 			        item_id,
6445 			        item_organization_id,
6446 			        win_probability,
6447 			        product_category_id,
6448 			        pipeline_amt_day,
6449 			        pipeline_amt_week,
6450 			        pipeline_amt_period,
6451 			        pipeline_amt_quarter,
6452 			        pipeline_amt_year,
6453 			        open_amt_day,
6454 			        open_amt_week,
6455 			        open_amt_period,
6456 			        open_amt_quarter,
6457 			        open_amt_year,
6458 			        pipeline_amt_day_s,
6459 			        pipeline_amt_week_s,
6460 			        pipeline_amt_period_s,
6461 			        pipeline_amt_quarter_s,
6462 			        pipeline_amt_year_s,
6463 			        open_amt_day_s,
6464 			        open_amt_week_s,
6465 			        open_amt_period_s,
6466 			        open_amt_quarter_s,
6467 			        open_amt_year_s,
6468 
6469                     prvprd_pipe_amt_wk  ,
6470                     prvprd_pipe_amt_PRD ,
6471                     prvprd_pipe_amt_qtr ,
6472                     prvprd_pipe_amt_yr  ,
6473                     prvprd_open_amt_wk  ,
6474                     prvprd_open_amt_PRD ,
6475                     prvprd_open_amt_qtr ,
6476                     prvprd_open_amt_yr  ,
6477                     prvprd_pipe_amt_wk_s,
6478                     prvprd_pipe_amt_PRD_s,
6479                     prvprd_pipe_amt_qtr_s,
6480                     prvprd_pipe_amt_yr_s,
6481                     prvprd_open_amt_wk_s,
6482                     prvprd_open_amt_PRD_s,
6483                     prvprd_open_amt_qtr_s,
6484                     prvprd_open_amt_yr_s,
6485 
6486                     prvyr_pipe_amt_wk   ,
6487                     prvyr_pipe_amt_PRD  ,
6488                     prvyr_pipe_amt_qtr  ,
6489                     prvyr_pipe_amt_yr   ,
6490                                         prvyr_open_amt_wk   ,
6491                     prvyr_open_amt_PRD  ,
6492                     prvyr_open_amt_qtr  ,
6493                     prvyr_open_amt_yr   ,
6494                     prvyr_pipe_amt_wk_s ,
6495                     prvyr_pipe_amt_PRD_s,
6496                     prvyr_pipe_amt_qtr_s,
6497                     prvyr_pipe_amt_yr_s ,
6498                     prvyr_open_amt_wk_s ,
6499                     prvyr_open_amt_PRD_s,
6500                     prvyr_open_amt_qtr_s,
6501                     prvyr_open_amt_yr_s
6502                       )
6503 
6504                             SELECT sales_group_id,
6505 			        salesrep_id,
6506 		            g_user_id created_by,
6507 			        SYSDATE creation_date,
6508 			        g_user_id last_updated_by,
6509 			        SYSDATE last_update_date,
6510 			        G_Login_Id last_update_login,
6511         			G_request_id request_id,
6512         			G_appl_id program_application_id,
6513         			G_program_id program_id,
6514 			        SYSDATE program_update_date,	        snap_date,
6515 			        item_id,
6516 			        item_organization_id,
6517 			        win_probability,
6518 			        product_category_id,
6519                     SUM(pipeline_amt_day) pipeline_amt_day,
6520                     SUM(pipeline_amt_week) pipeline_amt_week,
6521                     SUM(pipeline_amt_period) pipeline_amt_period,
6522                     SUM(pipeline_amt_quarter) pipeline_amt_quarter,
6523                     SUM(pipeline_amt_year) pipeline_amt_year,
6524                     SUM(open_amt_day)  open_amt_day   ,
6525                     SUM(open_amt_week)  open_amt_week  ,
6526                     SUM(open_amt_period) open_amt_period ,
6527                     SUM(open_amt_quarter) open_amt_quarter,
6528                     SUM(open_amt_year) open_amt_year   ,
6529                     SUM(pipeline_amt_day_s) pipeline_amt_day_s,
6530                     SUM(pipeline_amt_week_s) pipeline_amt_week_s,
6531                     SUM(pipeline_amt_period_s) pipeline_amt_period_s,
6532                     SUM(pipeline_amt_quarter_s) pipeline_amt_quarter_s,
6533                     SUM(pipeline_amt_year_s) pipeline_amt_year_s,
6534                     SUM(open_amt_day_s) open_amt_day_s  ,
6535                     SUM(open_amt_week_s) open_amt_week_s ,
6536                     SUM(open_amt_period_s) open_amt_period_s,
6537                     SUM(open_amt_quarter_s) open_amt_quarter_s,
6538                     SUM(open_amt_year_s) open_amt_year_s ,
6539                                 SUM(prvprd_pipe_amt_wk)  prvprd_pipe_amt_wk,
6540                 SUM(prvprd_pipe_amt_PRD) prvprd_pipe_amt_PRD,
6541                 SUM(prvprd_pipe_amt_qtr) prvprd_pipe_amt_qtr,
6542                 SUM(prvprd_pipe_amt_yr) prvprd_pipe_amt_yr ,
6543                 SUM(prvprd_open_amt_wk) prvprd_open_amt_wk ,
6544                 SUM(prvprd_open_amt_PRD) prvprd_open_amt_PRD,
6545                 SUM(prvprd_open_amt_qtr) prvprd_open_amt_qtr,
6546                 SUM(prvprd_open_amt_yr)  prvprd_open_amt_yr,
6547                 SUM(prvprd_pipe_amt_wk_s)  prvprd_pipe_amt_wk_s,
6548                 SUM(prvprd_pipe_amt_PRD_s) prvprd_pipe_amt_PRD_s,
6549                 SUM(prvprd_pipe_amt_qtr_s) prvprd_pipe_amt_qtr_s,
6550                 SUM(prvprd_pipe_amt_yr_s) prvprd_pipe_amt_yr_s ,
6551                 SUM(prvprd_open_amt_wk_s) prvprd_open_amt_wk_s ,
6552                 SUM(prvprd_open_amt_PRD_s) prvprd_open_amt_PRD_s,
6553                 SUM(prvprd_open_amt_qtr_s) prvprd_open_amt_qtr_s,
6554                 SUM(prvprd_open_amt_yr_s) prvprd_open_amt_yr_s,
6555                 SUM(prvyr_pipe_amt_wk)  prvyr_pipe_amt_wk ,
6556                 SUM(prvyr_pipe_amt_PRD)  prvyr_pipe_amt_PRD,
6557                 SUM(prvyr_pipe_amt_qtr)  prvyr_pipe_amt_qtr,
6558                 SUM(prvyr_pipe_amt_yr) prvyr_pipe_amt_yr  ,
6559                 SUM(prvyr_open_amt_wk) prvyr_open_amt_wk   ,
6560                 SUM(prvyr_open_amt_PRD) prvyr_open_amt_PRD ,
6561                 SUM(prvyr_open_amt_qtr) prvyr_open_amt_qtr ,
6562                 SUM(prvyr_open_amt_yr) prvyr_open_amt_yr  ,
6563                 SUM(prvyr_pipe_amt_wk_s)  prvyr_pipe_amt_wk_s ,
6564                 SUM(prvyr_pipe_amt_PRD_s) prvyr_pipe_amt_PRD_s ,
6565                 SUM(prvyr_pipe_amt_qtr_s) prvyr_pipe_amt_qtr_s ,
6566                 SUM(prvyr_pipe_amt_yr_s)   prvyr_pipe_amt_yr_s,
6567                 SUM(prvyr_open_amt_wk_s) prvyr_open_amt_wk_s  ,
6568                 SUM(prvyr_open_amt_PRD_s) prvyr_open_amt_PRD_s ,
6569                 SUM(prvyr_open_amt_qtr_s) prvyr_open_amt_qtr_s ,
6570                 SUM(prvyr_open_amt_yr_s) prvyr_open_amt_yr_s
6571                   FROM (
6572 			      SELECT /*+ parallel(stg) */
6573 			        SALES_GROUP_ID,
6574 			        SALESREP_ID,
6575 			        p_date snap_date,
6576 			        ITEM_ID,
6577 			        ITEM_ORGANIZATION_ID,
6578 			        DECODE(stg.win_loss_indicator, 'W', 100, stg.WIN_PROBABILITY) win_probability,
6579 			        PRODUCT_CATEGORY_ID,
6580 			        SUM(CASE
6581 			            WHEN TIME.report_date = p_date
6582 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6583 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
6584 			        ) pipeline_amt_day,
6585 			        SUM(CASE
6586 			            WHEN TIME.week_id = p_week
6587 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6588 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
6589 			        ) pipeline_amt_week,
6590 			        SUM(CASE
6591 			            WHEN TIME.ent_period_id = p_period
6592 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6593 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END )pipeline_amt_period,
6594 			        SUM(CASE
6595 			            WHEN TIME.ent_qtr_id = p_qtr
6596 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6597 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) pipeline_amt_quarter,
6598 			        SUM(CASE
6599 			            WHEN TIME.ent_year_id = p_year
6600 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6601 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) pipeline_amt_year,
6602 			         SUM( CASE
6603 			            WHEN TIME.report_date = p_date  AND OPP_OPEN_STATUS_FLAG = 'Y'
6604 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6605 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END) open_amt_day,
6606 			        SUM( CASE
6607 			            WHEN TIME.week_id = p_week  AND OPP_OPEN_STATUS_FLAG = 'Y'
6608 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6609 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END) open_amt_week,
6610 			        SUM(CASE
6611 			            WHEN TIME.ent_period_id = p_period  AND OPP_OPEN_STATUS_FLAG = 'Y'
6612 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6613 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) open_amt_period,
6614 			        SUM(CASE
6615 			            WHEN TIME.ent_qtr_id = p_qtr  AND OPP_OPEN_STATUS_FLAG = 'Y'
6616 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6617 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) open_amt_quarter,
6618 			        SUM(CASE
6619 			            WHEN TIME.ent_year_id = p_year  AND OPP_OPEN_STATUS_FLAG = 'Y'
6620 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6621 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END ) open_amt_year,
6622 			        SUM(CASE
6623 			            WHEN TIME.report_date = p_date
6624 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6625 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
6626 			        ) pipeline_amt_day_s,
6627 			        SUM(CASE
6628 			            WHEN TIME.week_id = p_week
6629 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6630 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END
6631 			        ) pipeline_amt_week_s,
6632 			        SUM(CASE
6633 			            WHEN TIME.ent_period_id = p_period
6634 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6635 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ) pipeline_amt_period_s,
6636 			        SUM(CASE
6637 			            WHEN TIME.ent_qtr_id = p_qtr
6638 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6639 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ) pipeline_amt_quarter_s,
6640 			        SUM(CASE
6641 			            WHEN TIME.ent_year_id = p_year
6642 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6643 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END) pipeline_amt_year_s,
6644 			         SUM( CASE
6645 			            WHEN TIME.report_date = p_date  AND OPP_OPEN_STATUS_FLAG = 'Y'
6646 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6647 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END) open_amt_day_s,
6648 			        SUM( CASE
6649 			            WHEN TIME.week_id = p_week  AND OPP_OPEN_STATUS_FLAG = 'Y'
6650 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6651 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END) open_amt_week_s,
6652 			        SUM(CASE
6653 			            WHEN TIME.ent_period_id = p_period  AND OPP_OPEN_STATUS_FLAG = 'Y'
6654 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6655 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ) open_amt_period_s,
6656 			        SUM(CASE
6657 			            WHEN TIME.ent_qtr_id = p_qtr  AND OPP_OPEN_STATUS_FLAG = 'Y'
6658 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6659 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END) open_amt_quarter_s,
6660 			        SUM(CASE
6661 			            WHEN TIME.ent_year_id = p_year  AND OPP_OPEN_STATUS_FLAG = 'Y'
6662 			            THEN DECODE(stg.sales_credit_amount*conversion_rate_s, 0, NULL,
6663 			          stg.sales_credit_amount*conversion_rate_s) ELSE NULL END ) open_amt_year_s,
6664 
6665                 null prvprd_pipe_amt_wk  ,
6666                 null prvprd_pipe_amt_PRD ,
6667                 null prvprd_pipe_amt_qtr ,
6668                 null prvprd_pipe_amt_yr  ,
6669                 null prvprd_open_amt_wk  ,
6670                 null prvprd_open_amt_PRD ,
6671                 null prvprd_open_amt_qtr ,
6672                 null prvprd_open_amt_yr  ,
6673                 null prvprd_pipe_amt_wk_s,
6674                 null prvprd_pipe_amt_PRD_s,
6675                 null prvprd_pipe_amt_qtr_s,
6676                 null prvprd_pipe_amt_yr_s,
6677                 null prvprd_open_amt_wk_s,
6678                 null prvprd_open_amt_PRD_s,
6679                 null prvprd_open_amt_qtr_s,
6680                 null prvprd_open_amt_yr_s,
6681                 null prvyr_pipe_amt_wk   ,
6682                 null prvyr_pipe_amt_PRD  ,
6683                 null prvyr_pipe_amt_qtr  ,
6684                 null prvyr_pipe_amt_yr   ,
6685                 null prvyr_open_amt_wk   ,
6686                 null prvyr_open_amt_PRD  ,
6687                 null prvyr_open_amt_qtr  ,
6688                 null prvyr_open_amt_yr   ,
6689                 null prvyr_pipe_amt_wk_s ,
6690                 null prvyr_pipe_amt_PRD_s,
6691                 null prvyr_pipe_amt_qtr_s,
6692                 null prvyr_pipe_amt_yr_s ,
6693                 null prvyr_open_amt_wk_s ,
6694                 null prvyr_open_amt_PRD_s,
6695                 null prvyr_open_amt_qtr_s,
6696                 null prvyr_open_amt_yr_s
6697 
6698                   FROM
6699 			        bil_bi_pipeline_stg stg,
6700 			        fii_time_day time
6701 			      WHERE stg.effective_date =  TIME.report_date
6702 			        AND forecast_rollup_flag = 'Y'
6703 			        AND TIME.report_date_julian>= p_min_date_id AND TIME.report_date_julian+0<=p_max_date_id
6704 					AND stg.snap_date = p_date
6705 			      GROUP BY
6706 			        sales_group_id,
6707 			        salesrep_id,
6708 			        item_id,
6709 			        item_organization_id,
6710 			        DECODE(stg.win_loss_indicator, 'W', 100, stg.WIN_PROBABILITY),
6711 			        product_category_id
6712 			      HAVING
6713 			        SUM(CASE
6714 			            WHEN TIME.week_id = p_week
6715 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6716 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END
6717 			        )IS NOT NULL OR
6718 			        SUM(CASE
6719 			            WHEN TIME.ent_year_id = p_year
6720 			            THEN DECODE(stg.sales_credit_amount*prim_conversion_rate, 0, NULL,
6721 			         stg.sales_credit_amount*prim_conversion_rate) ELSE NULL END )
6722 			         IS NOT NULL
6723  UNION ALL
6724                       SELECT /*+ parallel(f) */ sales_group_id,
6725 			        salesrep_id,
6726 			        p_date snap_date,
6727 			        item_id,
6728 			        item_organization_id,
6729 			        win_probability,
6730 			        product_category_id,
6731                     null pipeline_amt_day ,
6732                     null pipeline_amt_week,
6733                     null pipeline_amt_period,
6734                     null pipeline_amt_quarter,
6735                     null pipeline_amt_year,
6736                     null open_amt_day     ,
6737                     null open_amt_week    ,
6738                     null open_amt_period  ,
6739                     null open_amt_quarter ,
6740                     null open_amt_year    ,
6741                     null pipeline_amt_day_s ,
6742                     null pipeline_amt_week_s,
6743                     null pipeline_amt_period_s ,
6744                     null pipeline_amt_quarter_s,
6745                     null pipeline_amt_year_s,
6746                     null open_amt_day_s   ,
6747                     null open_amt_week_s  ,
6748                     null open_amt_period_s,
6749                     null open_amt_quarter_s,
6750                     null open_amt_year_s,
6751 				decode(f.snap_date, l_sd_lwk_end, pipeline_amt_week, null) prvprd_pipe_amt_wk  ,
6752                 decode(f.snap_date, l_sd_lper_end, pipeline_amt_period, null) prvprd_pipe_amt_PRD ,
6753                 decode(f.snap_date, l_sd_lqtr_end, pipeline_amt_quarter, null) prvprd_pipe_amt_qtr ,
6754                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year, null) prvprd_pipe_amt_yr  ,
6755                 decode(f.snap_date, l_sd_lwk_end, open_amt_week, null) prvprd_open_amt_wk  ,
6756                 decode(f.snap_date, l_sd_lper_end, open_amt_period, null) prvprd_open_amt_PRD ,
6757                 decode(f.snap_date, l_sd_lqtr_end, open_amt_quarter, null) prvprd_open_amt_qtr ,
6758                 decode(f.snap_date, l_sd_lyr_end, open_amt_year, null) prvprd_open_amt_yr  ,
6759                 decode(f.snap_date, l_sd_lwk_end, pipeline_amt_week_s, null) prvprd_pipe_amt_wk_s  ,
6760                 decode(f.snap_date, l_sd_lper_end, pipeline_amt_period_s, null) prvprd_pipe_amt_PRD_s ,
6761                 decode(f.snap_date, l_sd_lqtr_end, pipeline_amt_quarter_s, null) prvprd_pipe_amt_qtr_s ,
6762                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year_s, null) prvprd_pipe_amt_yr_s  ,
6763                 decode(f.snap_date, l_sd_lwk_end, open_amt_week_s, null) prvprd_open_amt_wk_s  ,
6764                 decode(f.snap_date, l_sd_lper_end, open_amt_period_s, null) prvprd_open_amt_PRD_s ,
6765                 decode(f.snap_date, l_sd_lqtr_end, open_amt_quarter_s, null) prvprd_open_amt_qtr_s ,
6766                 decode(f.snap_date, l_sd_lyr_end, open_amt_year_s, null) prvprd_open_amt_yr_s,
6767                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_week, null) prvyr_pipe_amt_wk   ,
6768                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_period, null) prvyr_pipe_amt_PRD  ,
6769                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_quarter, null) prvyr_pipe_amt_qtr  ,
6770                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year, null) prvyr_pipe_amt_yr   ,
6771                 decode(f.snap_date, l_sd_lyr_end, open_amt_week, null) prvyr_open_amt_wk   ,
6772                 decode(f.snap_date, l_sd_lyr_end, open_amt_period, null) prvyr_open_amt_PRD  ,
6773                 decode(f.snap_date, l_sd_lyr_end, open_amt_quarter, null) prvyr_open_amt_qtr  ,
6774                 decode(f.snap_date, l_sd_lyr_end, open_amt_year, null) prvyr_open_amt_yr   ,
6775                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_week_s, null) prvyr_pipe_amt_wk_s   ,
6776                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_period_s, null) prvyr_pipe_amt_PRD_s  ,
6777                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_quarter_s, null) prvyr_pipe_amt_qtr_s  ,
6778                 decode(f.snap_date, l_sd_lyr_end, pipeline_amt_year_s, null) prvyr_pipe_amt_yr_s   ,
6779                 decode(f.snap_date, l_sd_lyr_end, open_amt_week_s, null) prvyr_open_amt_wk_s   ,
6780                 decode(f.snap_date, l_sd_lyr_end, open_amt_period, null) prvyr_open_amt_PRD_s  ,
6781                 decode(f.snap_date, l_sd_lyr_end, open_amt_quarter, null) prvyr_open_amt_qtr_s  ,
6782                 decode(f.snap_date, l_sd_lyr_end, open_amt_year, null) prvyr_open_amt_yr_s
6783                     FROM BIL_BI_PIPELINE_F f
6784                     where snap_date in (l_sd_lwk_end, l_sd_lper_end
6785                     ,l_sd_lqtr_end, l_sd_lyr_end)
6786 					UNION ALL
6787                       SELECT  sales_group_id,
6788 			        salesrep_id,
6789 			        p_date snap_date,
6790 			        item_id,
6791 			        item_organization_id,
6792 			        win_probability,
6793 			        product_category_id,
6794                     null pipeline_amt_day ,
6795                     null pipeline_amt_week,
6796                     null pipeline_amt_period,
6797                     null pipeline_amt_quarter,
6798                     null pipeline_amt_year,
6799                     null open_amt_day     ,
6800                     null open_amt_week    ,
6801                     null open_amt_period  ,
6802                     null open_amt_quarter ,
6803                     null open_amt_year    ,
6804                     null pipeline_amt_day_s ,
6805                     null pipeline_amt_week_s,
6806                     null pipeline_amt_period_s ,
6807                     null pipeline_amt_quarter_s,
6808                     null pipeline_amt_year_s,
6809                     null open_amt_day_s   ,
6810                     null open_amt_week_s  ,
6811                     null open_amt_period_s,
6812                     null open_amt_quarter_s,
6813                     null open_amt_year_s  ,
6814                 pipeline_amt_week prvprd_pipe_amt_wk  ,
6815                 null prvprd_pipe_amt_PRD ,
6816                 null prvprd_pipe_amt_qtr ,
6817                 null prvprd_pipe_amt_yr  ,
6818                 open_amt_week prvprd_open_amt_wk  ,
6819                 null prvprd_open_amt_PRD ,
6820                 null prvprd_open_amt_qtr ,
6821                 null prvprd_open_amt_yr  ,
6822                 pipeline_amt_week_s prvprd_pipe_amt_wk_s  ,
6823                 null prvprd_pipe_amt_PRD_s ,
6824                 null prvprd_pipe_amt_qtr_s ,
6825                 null prvprd_pipe_amt_yr_s  ,
6826                 open_amt_week_s prvprd_open_amt_wk_s  ,
6827                 null prvprd_open_amt_PRD_s ,
6828                 null prvprd_open_amt_qtr_s ,
6829                 null prvprd_open_amt_yr_s,
6830                 null prvyr_pipe_amt_wk   ,
6831                 null prvyr_pipe_amt_PRD  ,
6832                 null prvyr_pipe_amt_qtr  ,
6833                 null prvyr_pipe_amt_yr   ,
6834                 null prvyr_open_amt_wk   ,
6835                 null prvyr_open_amt_PRD  ,
6836                 null prvyr_open_amt_qtr  ,
6837                 null prvyr_open_amt_yr   ,
6838                 null prvyr_pipe_amt_wk_s   ,
6839                 null prvyr_pipe_amt_PRD_s  ,
6840                 null prvyr_pipe_amt_qtr_s  ,
6841                 null prvyr_pipe_amt_yr_s   ,
6842                 null prvyr_open_amt_wk_s   ,
6843                 null prvyr_open_amt_PRD_s  ,
6844                 null prvyr_open_amt_qtr_s  ,
6845                 null prvyr_open_amt_yr_s
6846                     FROM BIL_BI_PIPEC_F f
6847                     where snap_date = l_sd_lwk
6848                      )
6849                     GROUP BY
6850                        sales_group_id,
6851 			        salesrep_id,
6852 			        snap_date,
6853 			        item_id,
6854 			        item_organization_id,
6855 			        win_probability,
6856 			        product_category_id
6857 
6858 
6859                     ;
6860 
6861                commit;
6862 
6863 	  IF bil_bi_util_collection_pkg.chkLogLevel(fnd_log.LEVEL_PROCEDURE) THEN
6864   bil_bi_util_collection_pkg.writeLog(
6865              p_log_level => fnd_log.LEVEL_PROCEDURE,
6866              p_module => g_pkg || l_proc || ' end',
6867              p_msg => 'End of Procedure '|| l_proc);
6868  END IF;
6869 
6870  EXCEPTION
6871 
6872 WHEN OTHERS THEN
6873     fnd_message.set_name('FND','SQL_PLSQL_ERROR'); -- Seeded Message
6874     fnd_message.set_token('ERRNO' ,SQLCODE);
6875     fnd_message.set_token('REASON' ,SQLERRM);
6876   fnd_message.set_token('ROUTINE' , l_proc);
6877     bil_bi_util_collection_pkg.writeLog(p_log_level => fnd_log.LEVEL_UNEXPECTED,
6878   p_module => g_pkg || l_proc || ' proc_error',
6879   p_msg => fnd_message.get,
6880   p_force_log => TRUE);
6881 
6882     raise;
6883 
6884 
6885 END Ins_Into_CurrSum_SmGap;
6886 
6887 
6888 END BIL_BI_PIPELINE_F_PKG;